diff --git a/readme.txt b/readme.txt index 7a0490c3..3137b49b 100644 --- a/readme.txt +++ b/readme.txt @@ -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. diff --git a/src/Integrations/FormSubmit.php b/src/Integrations/FormSubmit.php index 40d8f00a..b1895f25 100644 --- a/src/Integrations/FormSubmit.php +++ b/src/Integrations/FormSubmit.php @@ -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. * diff --git a/tests/integration/Integrations/FormSubmitTest.php b/tests/integration/Integrations/FormSubmitTest.php new file mode 100644 index 00000000..b3fafe7e --- /dev/null +++ b/tests/integration/Integrations/FormSubmitTest.php @@ -0,0 +1,89 @@ +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 ); + } +}