PluginProbe
Plausible Analytics / trunk
Plausible Analytics vtrunk
2.6.1 2.6.0 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 52 releases
plausible-analytics / src / Integrations / Search.php

Search.php in Plausible Analytics trunk, at src/Integrations/Search.php

106 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plausible Analytics | Integrations | Search
4 */
5
6 namespace Plausible\Analytics\WP\Integrations;
7
8 use Plausible\Analytics\WP\EnhancedMeasurements;
9
10 class Search {
11 /**
12 * Build class.
13 *
14 * @codeCoverageIgnore
15 */
16 public function __construct() {
17 $this->init();
18 }
19
20 /**
21 * Filter/action hooks.
22 *
23 * @return void
24 *
25 * @codeCoverageIgnore
26 */
27 private function init() {
28 add_filter( 'get_search_form', [ $this, 'maybe_add_hidden_input_to_search_form' ] );
29 add_filter( 'render_block', [ $this, 'maybe_add_hidden_input_to_search_block' ], 10, 2 );
30 }
31
32 /**
33 * Adds a hidden input field to the search form for enhanced measurement of search referrer.
34 *
35 * This method checks if enhanced measurement is enabled for search, and if so, it appends
36 * a hidden input field containing a reference to the search's referrer URL.
37 *
38 * @param string $form The HTML markup of the search form.
39 *
40 * @return string The modified HTML markup of the search form with the hidden input added,
41 * or the original form if enhanced measurement is not enabled.
42 *
43 * @codeCoverageIgnore because we wouldn't be testing anything here. Whether this works depends on the filter, and that'd only break if WordPress changes the name of it.
44 */
45 public function maybe_add_hidden_input_to_search_form( $form ) {
46 if ( ! EnhancedMeasurements::is_enabled( EnhancedMeasurements::SEARCH_QUERIES ) ) {
47 return $form;
48 }
49
50 $referrer = $this->get_referrer();
51 $hidden_input = '<input type="hidden" name="search_source" value="' . $referrer . '" />';
52
53 return str_replace( '</form>', $hidden_input . '</form>', $form );
54 }
55
56 /**
57 * Retrieves the current page URL to be used as a referrer.
58 *
59 * This method constructs the referrer by obtaining the current page URL and ensures
60 * it is sanitized. If the referrer cannot be determined, an empty string is returned.
61 *
62 * @return string The sanitized referrer URL or an empty string if unavailable.
63 *
64 * @codeCoverageIgnore because it's parent methods aren't tested either.
65 */
66 private function get_referrer() {
67 $referrer = esc_url( home_url( add_query_arg( null, null ) ) );
68
69 if ( ! $referrer ) {
70 $referrer = '';
71 }
72
73 return esc_attr( $referrer );
74 }
75
76 /**
77 * Adds a hidden input field to the content of a search block for enhanced measurement of search referrer.
78 *
79 * This method checks if the given block is a WordPress core search block. If so, it appends
80 * a hidden input field containing a reference to the current page's URL as the search's referrer.
81 * The hidden input is inserted before the button element if present or at the end of the block content otherwise.
82 *
83 * @param string $block_content The current content of the block.
84 * @param array $block The block attributes and settings.
85 *
86 * @return string The modified content of the block with the hidden input added if it is a core search block,
87 * or the original block content if the block is not a search block.
88 *
89 * @codeCoverageIgnore because we wouldn't be testing anything here. Whether this works depends on the filter, and that'd only break if WordPress changes the name of it.
90 */
91 public function maybe_add_hidden_input_to_search_block( $block_content, $block ) {
92 if ( $block['blockName'] === 'core/search' ) {
93 $referrer = $this->get_referrer();
94 $hidden_input = '<input type="hidden" name="search_source" value="' . $referrer . '"/>';
95
96 if ( str_contains( $block_content, '<button' ) ) {
97 $block_content = str_replace( '<button', $hidden_input . '<button', $block_content );
98 } else {
99 $block_content .= $hidden_input;
100 }
101 }
102
103 return $block_content;
104 }
105 }
106