From 4f76095efcd88a37750eacb7e9426d1338d90af6 Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:11:44 +0100 Subject: [PATCH 1/3] Fix PHP warning when a CF7 form outside the loop is submitted --- readme.txt | 1 + src/Integrations/FormSubmit.php | 26 ++++-- .../Integrations/FormSubmitTest.php | 83 +++++++++++++++++++ 3 files changed, 105 insertions(+), 5 deletions(-) create mode 100644 tests/integration/Integrations/FormSubmitTest.php diff --git a/readme.txt b/readme.txt index 7a0490c3..856c9075 100644 --- a/readme.txt +++ b/readme.txt @@ -281,6 +281,7 @@ 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: 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..68152387 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_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..f19bf7c6 --- /dev/null +++ b/tests/integration/Integrations/FormSubmitTest.php @@ -0,0 +1,83 @@ +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, [] ); + + // No container post and no referer. + unset( $_POST['_wpcf7_container_post'], $_SERVER['HTTP_REFERER'] ); + $class->maybe_track_submission( $result, [] ); + + $this->assertEquals( [ '/contact/', '/about/team/' ], $this->sent ); + } +} From ff46ef44a23ee1501634bdfb07cebeaed584292e Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:56:39 +0100 Subject: [PATCH 2/3] Keep the referer for same-page CF7 submissions --- src/Integrations/FormSubmit.php | 2 +- tests/integration/Integrations/FormSubmitTest.php | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Integrations/FormSubmit.php b/src/Integrations/FormSubmit.php index 68152387..b1895f25 100644 --- a/src/Integrations/FormSubmit.php +++ b/src/Integrations/FormSubmit.php @@ -110,7 +110,7 @@ private function get_cf7_submission_path() { return '/' . $post->post_name . '/'; } - return (string) wp_parse_url( (string) wp_get_referer(), PHP_URL_PATH ); + return (string) wp_parse_url( (string) wp_get_raw_referer(), PHP_URL_PATH ); } /** diff --git a/tests/integration/Integrations/FormSubmitTest.php b/tests/integration/Integrations/FormSubmitTest.php index f19bf7c6..b3fafe7e 100644 --- a/tests/integration/Integrations/FormSubmitTest.php +++ b/tests/integration/Integrations/FormSubmitTest.php @@ -74,10 +74,16 @@ public function testMaybeTrackSubmission() { $_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/' ], $this->sent ); + $this->assertEquals( [ '/contact/', '/about/team/', '/about/team/' ], $this->sent ); } } From 67e403cdd5420f987351878dc1ae7aa71be6f9eb Mon Sep 17 00:00:00 2001 From: Daan van den Bergh <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 17 Nov 2026 10:59:12 +0100 Subject: [PATCH 3/3] Update readme.txt --- readme.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.txt b/readme.txt index 856c9075..3137b49b 100644 --- a/readme.txt +++ b/readme.txt @@ -280,7 +280,7 @@ 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 =