Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ Please make sure you make a backup of your database before updating any version

= 2.6.5 =
* Fixed: the user_logged_in property followed the "Authors and categories" option instead of "Logged-in user status".
* Fixed: the user_logged_in value for logged-out visitors is no longer translated, so it's always "no".
* Fixed: the user_logged_in value for logged-out visitors is no longer translated.
* Fixed: PHP warning when a Contact Form 7 form outside the loop (e.g. in a widget) is submitted.

= 2.6.4 =
* Improved: the proxy now also forwards the visitor's Accept-Language and User-Agent Client Hints (Sec-CH-UA*) headers to Plausible Analytics, so its bot detection works for proxied requests, too.
Expand Down
26 changes: 21 additions & 5 deletions src/Integrations/FormSubmit.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,38 @@ public function add_js() {
* @param array $tags Array of tags associated with the form fields.
*
* @return \WPCF7_Validation
*
* @codeCoverageIgnore because we can't test XHR requests here.
*/
public function maybe_track_submission( $result, $tags ) {
$invalid_fields = $result->get_invalid_fields();

if ( empty( $invalid_fields ) ) {
$post = get_post( $_POST['_wpcf7_container_post'] );
$uri = '/' . $post->post_name . '/';
$uri = $this->get_cf7_submission_path();

$this->track_submission( $uri );
if ( $uri ) {
$this->track_submission( $uri );
}
}

return $result;
}

/**
* Returns the path of the page containing the CF7 form. Falls back to the referer when the form isn't rendered inside
* a post (e.g. in a widget), where CF7 sends 0 as the container post.
*
* @return string Empty if the path can't be determined.
*/
private function get_cf7_submission_path() {
$post_id = absint( $_POST['_wpcf7_container_post'] ?? 0 );
$post = $post_id ? get_post( $post_id ) : null;

if ( $post ) {
return '/' . $post->post_name . '/';
}

return (string) wp_parse_url( (string) wp_get_raw_referer(), PHP_URL_PATH );
}

/**
* Track submission using the Proxy.
*
Expand Down
89 changes: 89 additions & 0 deletions tests/integration/Integrations/FormSubmitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Plausible Analytics | Form Submissions
*/

namespace Plausible\Analytics\Tests\Integration\Integrations;

use Plausible\Analytics\Tests\TestCase;
use Plausible\Analytics\WP\Integrations\FormSubmit;

class FormSubmitTest extends TestCase {
/**
* @var array The paths of the events that would have been sent to Plausible Analytics.
*/
private $sent = [];

/**
* @return void
*/
protected function set_up() {
parent::set_up();

$this->sent = [];

add_filter( 'pre_http_request', [ $this, 'capture_event' ], 10, 3 );
}

/**
* @return void
*/
protected function tear_down() {
remove_filter( 'pre_http_request', [ $this, 'capture_event' ] );
unset( $_POST['_wpcf7_container_post'], $_SERVER['HTTP_REFERER'] );

parent::tear_down();
}

/**
* Records events instead of sending them.
*
* @param false|array $response
* @param array $args
* @param string $url
*
* @return false|array
*/
public function capture_event( $response, $args, $url ) {
if ( strpos( $url, '/api/event' ) === false ) {
return $response;
}

$this->sent[] = json_decode( $args['body'], true )['p']['path'] ?? '';

return [ 'headers' => [], 'body' => '', 'response' => [ 'code' => 202, 'message' => 'Accepted' ], 'cookies' => [], 'filename' => null ];
}

/**
* @see FormSubmit::maybe_track_submission()
* @return void
*/
public function testMaybeTrackSubmission() {
$class = $this->getMockBuilder( FormSubmit::class )->disableOriginalConstructor()->onlyMethods( [] )->getMock();
$result = $this->getMockBuilder( \stdClass::class )->addMethods( [ 'get_invalid_fields' ] )->getMock();
$result->method( 'get_invalid_fields' )->willReturn( [] );

// Form inside a post.
$post_id = wp_insert_post( [ 'post_title' => 'Contact', 'post_name' => 'contact', 'post_status' => 'publish' ] );
$_POST['_wpcf7_container_post'] = (string) $post_id;
$class->maybe_track_submission( $result, [] );
wp_delete_post( $post_id, true );

// Form outside the loop, e.g. in a widget.
$_POST['_wpcf7_container_post'] = '0';
$_SERVER['HTTP_REFERER'] = home_url( '/about/team/' );
$class->maybe_track_submission( $result, [] );

// Non-AJAX submission, which posts to the page containing the form.
$request_uri = $_SERVER['REQUEST_URI'] ?? null;
$_SERVER['REQUEST_URI'] = '/about/team/';
$class->maybe_track_submission( $result, [] );
$_SERVER['REQUEST_URI'] = $request_uri;

// No container post and no referer.
unset( $_POST['_wpcf7_container_post'], $_SERVER['HTTP_REFERER'] );
$class->maybe_track_submission( $result, [] );

$this->assertEquals( [ '/contact/', '/about/team/', '/about/team/' ], $this->sent );
}
}
Loading