From bc62103c16e287460d79b1e67d4aa25134f1c2e1 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 2 Dec 2025 15:35:29 +0100 Subject: [PATCH 1/6] Added: Show what page the search was made on. --- src/Actions.php | 5 +++-- src/Admin/Provisioning.php | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Actions.php b/src/Actions.php index 74deef3..561996c 100644 --- a/src/Actions.php +++ b/src/Actions.php @@ -126,8 +126,9 @@ public function maybe_register_assets() { [ 'props' => [ // convert queries to lowercase and remove trailing whitespace to ensure same terms are grouped together - 'search_query' => strtolower( trim( get_search_query() ) ), - 'result_count' => $wp_query->found_posts, + 'search_query' => strtolower( trim( get_search_query() ) ), + 'result_count' => $wp_query->found_posts, + 'search_source' => wp_get_referer(), ], ] ); diff --git a/src/Admin/Provisioning.php b/src/Admin/Provisioning.php index 0569ec6..112889b 100644 --- a/src/Admin/Provisioning.php +++ b/src/Admin/Provisioning.php @@ -66,6 +66,7 @@ class Provisioning { private $custom_search_properties = [ 'search_query', 'result_count', + 'search_source', ]; /** @@ -365,7 +366,7 @@ public function maybe_create_custom_properties( $old_settings, $settings ) { $caps = get_option( 'plausible_analytics_api_token_caps', [] ); foreach ( $this->custom_search_properties as $property ) { - if ( empty( $caps[ 'props' ] ) && $property === 'result_count' ) { + if ( empty( $caps[ 'props' ] ) && ( $property === 'result_count' || $property == 'search_source' ) ) { continue; } From dade96147214ca6956df277b79826548f72af1a2 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 2 Dec 2025 15:50:28 +0100 Subject: [PATCH 2/6] Improved: added migration script for when Search Queries is already enabled. --- src/Admin/Upgrades.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/Admin/Upgrades.php b/src/Admin/Upgrades.php index c882211..5ea6749 100644 --- a/src/Admin/Upgrades.php +++ b/src/Admin/Upgrades.php @@ -84,6 +84,10 @@ public function run() { $this->upgrade_to_231(); } + if ( version_compare( $plausible_analytics_version, '2.4.2', '<' ) ) { + $this->upgrade_to_242(); + } + // Add required upgrade routines for future versions here. } @@ -296,4 +300,26 @@ public function upgrade_to_231() { update_option( 'plausible_analytics_version', '2.3.1' ); } + + /** + * If Search Queries is enabled, make sure the custom properties are created after updating. + * + * @return void + */ + public function upgrade_to_242() { + $settings = Helpers::get_settings(); + + if ( Helpers::is_enhanced_measurement_enabled( 'search' ) ) { + $provisioning = new Provisioning(); + + // No token entered. + if ( ! $provisioning->client instanceof Client ) { + return; + } + + $provisioning->maybe_create_custom_properties( [], $settings ); + } + + update_option( 'plausible_analytics_version', '2.4.2' ); + } } From 1303a7a6d9abddc74b88f38e52da7d83d26d2795 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 2 Dec 2025 17:24:51 +0100 Subject: [PATCH 3/6] Improved: add hidden input field to search block to make sure we always have the referer. --- src/Actions.php | 77 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 3 deletions(-) diff --git a/src/Actions.php b/src/Actions.php index 561996c..dbf98e8 100644 --- a/src/Actions.php +++ b/src/Actions.php @@ -19,6 +19,8 @@ public function __construct() { add_action( 'wp_head', [ $this, 'maybe_insert_version_meta_tag' ] ); add_action( 'wp_enqueue_scripts', [ $this, 'maybe_register_assets' ] ); add_action( 'admin_bar_menu', [ $this, 'admin_bar_node' ], 100 ); + add_filter( 'get_search_form', [ $this, 'maybe_add_hidden_input_to_search_form' ], 10, 1 ); + add_filter( 'render_block', [ $this, 'maybe_add_hidden_input_to_search_block' ], 10, 2 ); } /** @@ -122,17 +124,18 @@ public function maybe_register_assets() { if ( Helpers::is_enhanced_measurement_enabled( 'search' ) && is_search() ) { global $wp_query; - $data = wp_json_encode( + $search_source = isset( $_REQUEST[ 'search_source' ] ) ? sanitize_text_field( $_REQUEST[ 'search_source' ] ) : wp_get_referer(); + $data = wp_json_encode( [ 'props' => [ // convert queries to lowercase and remove trailing whitespace to ensure same terms are grouped together 'search_query' => strtolower( trim( get_search_query() ) ), 'result_count' => $wp_query->found_posts, - 'search_source' => wp_get_referer(), + 'search_source' => $search_source, ], ] ); - $script = "plausible('WP Search Queries', $data );"; + $script = "plausible('WP Search Queries', $data );"; wp_add_inline_script( 'plausible-analytics', @@ -144,6 +147,74 @@ public function maybe_register_assets() { do_action( 'plausible_analytics_after_register_assets', $settings ); } + /** + * Adds a hidden input field to the search form for enhanced measurement of search referrer. + * + * This method checks if enhanced measurement is enabled for search, and if so, it appends + * a hidden input field containing a reference to the search's referrer URL. + * + * @param string $form The HTML markup of the search form. + * + * @return string The modified HTML markup of the search form with the hidden input added, + * or the original form if enhanced measurement is not enabled. + */ + public function maybe_add_hidden_input_to_search_form( $form ) { + if ( ! Helpers::is_enhanced_measurement_enabled( 'search' ) ) { + return $form; + } + + $referrer = $this->get_referrer(); + $hidden_input = ''; + + return str_replace( '', $hidden_input . '', $form ); + } + + /** + * Retrieves the current page URL to be used as a referrer. + * + * This method constructs the referrer by obtaining the current page URL and ensures + * it is sanitized. If the referrer cannot be determined, an empty string is returned. + * + * @return string The sanitized referrer URL or an empty string if unavailable. + */ + private function get_referrer() { + $referrer = esc_url( home_url( add_query_arg( null, null ) ) ); + + if ( ! $referrer ) { + $referrer = ''; + } + + return esc_attr( $referrer ); + } + + /** + * Adds a hidden input field to the content of a search block for enhanced measurement of search referrer. + * + * This method checks if the given block is a WordPress core search block. If so, it appends + * a hidden input field containing a reference to the current page's URL as the search's referrer. + * The hidden input is inserted before the button element if present or at the end of the block content otherwise. + * + * @param string $block_content The current content of the block. + * @param array $block The block attributes and settings. + * + * @return string The modified content of the block with the hidden input added if it is a core search block, + * or the original block content if the block is not a search block. + */ + public function maybe_add_hidden_input_to_search_block( $block_content, $block ) { + if ( $block[ 'blockName' ] === 'core/search' ) { + $referrer = $this->get_referrer(); + $hidden_input = ''; + + if ( str_contains( $block_content, ' Date: Tue, 2 Dec 2025 17:26:24 +0100 Subject: [PATCH 4/6] PHPDoc. --- src/Actions.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Actions.php b/src/Actions.php index dbf98e8..3a28d9d 100644 --- a/src/Actions.php +++ b/src/Actions.php @@ -56,6 +56,7 @@ private function get_plugin_version() { * @since 1.0.0 * @access public * @return void + * @throws \Exception */ public function maybe_register_assets() { $settings = Helpers::get_settings(); From 78b42640b2c4a54b0ea811d62b99657b2e50b719 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 2 Dec 2025 17:26:56 +0100 Subject: [PATCH 5/6] No need to test this. --- src/Admin/Upgrades.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Admin/Upgrades.php b/src/Admin/Upgrades.php index 5ea6749..5debab0 100644 --- a/src/Admin/Upgrades.php +++ b/src/Admin/Upgrades.php @@ -305,6 +305,8 @@ public function upgrade_to_231() { * If Search Queries is enabled, make sure the custom properties are created after updating. * * @return void + * + * @codeCoverageIgnore because all we'd be doing is testing the Plugins API. */ public function upgrade_to_242() { $settings = Helpers::get_settings(); From f7718efde27380f4ce1859b2eaa6b57a22bd5669 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Tue, 2 Dec 2025 17:32:52 +0100 Subject: [PATCH 6/6] Install composer dependencies on all PHP versions. --- .github/workflows/push.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index fe19099..0413624 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -64,7 +64,6 @@ jobs: # Install dependencies and handle caching in one go. # @link https://github.com/marketplace/actions/install-php-dependencies-with-composer - name: Install Composer dependencies - normal - if: matrix.php != '8.4' uses: "ramsey/composer-install@v3" continue-on-error: true with: