PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.10.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.10.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / Admin / TrackingSettings.php
matomo / classes / WpMatomo / Admin Last commit date
Marketplace 1 month ago PluginSuggestions 2 months ago TrackingSettings 1 month ago views 1 month ago AccessSettings.php 4 years ago AdBlockDetector.php 6 months ago Admin.php 1 month ago AdminSettings.php 2 months ago AdminSettingsInterface.php 6 years ago AdvancedSettings.php 10 months ago Chart.php 1 month ago CookieConsent.php 4 years ago Dashboard.php 2 months ago ExclusionSettings.php 6 months ago GeolocationSettings.php 2 years ago GetStarted.php 2 months ago ImportWpStatistics.php 2 months ago Info.php 2 months ago InvalidIpException.php 4 years ago Marketplace.php 1 month ago MarketplaceSetupWizard.php 1 month ago MarketplaceSetupWizardBody.php 1 month ago MatomoPage.php 2 months ago MatomoPageContent.php 2 months ago Menu.php 1 month ago PluginMeasurableSettings.php 2 years ago PrivacySettings.php 1 year ago SafeModeMenu.php 2 months ago Summary.php 2 months ago SystemReport.php 1 month ago TrackingSettings.php 1 month ago WhatsNewNotifications.php 2 months ago
TrackingSettings.php
545 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 * @package matomo
8 */
9
10 namespace WpMatomo\Admin;
11
12 use Exception;
13 use WpMatomo\Capabilities;
14 use WpMatomo\Settings;
15 use WpMatomo\Site;
16 use WpMatomo\Site\Sync\SyncConfig as SiteConfigSync;
17 use WpMatomo\TrackingCode\GeneratorOptions;
18 use WpMatomo\TrackingCode\TrackingCodeGenerator;
19
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit; // if accessed directly
22 }
23
24 /**
25 * TODO: maybe we can move the form data collection to a single class
26 * Note: nonce verification exists, but phpcs can't tell since it's in a method
27 * that calls other methods that then access post data.
28 * phpcs:disable WordPress.Security.NonceVerification.Missing
29 */
30 class TrackingSettings implements AdminSettingsInterface {
31 const FORM_NAME = 'matomo';
32 const NONCE_NAME = 'matomo_settings';
33 const TRACK_MODE_DEFAULT = 'default';
34 const TRACK_MODE_DISABLED = 'disabled';
35 const TRACK_MODE_MANUALLY = 'manually';
36 const TRACK_MODE_TAGMANAGER = 'tagmanager';
37 const NONCE_NAME_GENERATE_TRACKING_CODE_AJAX = 'matomo-tracking-settings-code';
38
39 /**
40 * @var Settings
41 */
42 private $settings;
43
44 /**
45 * @param Settings $settings
46 */
47 public function __construct( $settings ) {
48 $this->settings = $settings;
49 $this->add_hooks();
50 }
51
52 /**
53 * Returns true if WordPress is configured to use the advanced-cache.php
54 * file, and if such a file exists.
55 *
56 * @return bool
57 */
58 public static function is_advanced_cache_used() {
59 return defined( 'WP_CACHE' )
60 && WP_CACHE
61 && is_file( WP_CONTENT_DIR . '/advanced-cache.php' );
62 }
63
64 /**
65 * To track AI bots when the advanced-cache.php file is in use, a
66 * special code snippet must be added to a user's wp-config.php.
67 *
68 * This function checks if the required snippet has been added to
69 * this WordPress' wp-config.php file.
70 *
71 * @param string $abspath_override only used for tests.
72 * @return bool|null true if the snippet is detected, false if it is not,
73 * and null if the wp-config.php file cannot be read for
74 * some reason
75 */
76 public static function is_track_script_used_in_wp_config( $abspath_override = null ) {
77 $abspath_override = ! empty( $abspath_override ) ? $abspath_override : ABSPATH;
78
79 $wp_config_path = $abspath_override . '/wp-config.php';
80
81 if ( ! is_readable( $wp_config_path ) ) {
82 return null;
83 }
84
85 // phpcs:disable WordPress.PHP.NoSilencedErrors.Discouraged
86 // phpcs:disable WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
87 $wp_config_contents = @file_get_contents( $wp_config_path );
88
89 // some systems may disable reading of files outside of wp-content
90 if ( ! is_string( $wp_config_contents ) ) {
91 return null;
92 }
93
94 $is_track_ai_bot_script_used = preg_match( '/require_once.*?track_ai_bot\.php/', $wp_config_contents ) === 1;
95
96 return $is_track_ai_bot_script_used;
97 }
98
99 public static function is_htaccess_serving_cache_files() {
100 if ( ! is_file( ABSPATH . '/.htaccess' ) ) {
101 return false;
102 }
103
104 if ( ! function_exists( 'apache_get_modules' ) ) {
105 return false; // not using apache
106 }
107
108 $htaccess_contents = file_get_contents( ABSPATH . '/.htaccess' );
109 $is_rewrite_rule_found = preg_match( '%RewriteRule.*?/wp-content/cache/%', $htaccess_contents ) === 1;
110
111 return $is_rewrite_rule_found;
112 }
113
114 public function get_title() {
115 return esc_html__( 'Tracking', 'matomo' );
116 }
117
118 private function update_if_submitted() {
119 if ( $this->form_submitted() === true
120 && check_admin_referer( self::NONCE_NAME ) ) {
121 $this->apply_settings();
122
123 return true;
124 }
125
126 return false;
127 }
128
129 public function can_user_manage() {
130 return current_user_can( Capabilities::KEY_SUPERUSER );
131 }
132
133 private function apply_settings() {
134 $keys_to_keep = [
135 'track_mode',
136 'track_across',
137 'track_across_alias',
138 'track_crossdomain_linking',
139 'track_feed',
140 'track_feed_addcampaign',
141 'track_feed_campaign',
142 'track_heartbeat',
143 'track_user_id',
144 'track_datacfasync',
145 Settings::TRACK_AI_BOTS,
146 Settings::TRACK_AI_BOTS_USING_ESI,
147 'tagmanger_container_ids',
148 'set_download_extensions',
149 'set_download_classes',
150 'set_link_classes',
151 'track_admin',
152 'limit_cookies_referral',
153 'limit_cookies_session',
154 'limit_cookies_visitor',
155 'limit_cookies',
156 'force_post',
157 'disable_cookies',
158 'cookie_consent',
159 'add_download_extensions',
160 'track_404',
161 'track_search',
162 'add_post_annotations',
163 'track_content',
164 'track_ecommerce',
165 'track_noscript',
166 'noscript_code',
167 'track_codeposition',
168 'tracking_code',
169 'force_protocol',
170 'track_js_endpoint',
171 'track_jserrors',
172 'track_api_endpoint',
173 Settings::SITE_CURRENCY,
174 Settings::USE_SESSION_VISITOR_ID_OPTION_NAME,
175 ];
176
177 if ( matomo_has_tag_manager() ) {
178 $keys_to_keep[] = 'tagmanger_container_ids';
179 }
180
181 $values = [];
182
183 // default value in case no role/ post type is selected to make sure we unset it if no role /post type is selected
184 $values['add_post_annotations'] = [];
185 $values['tagmanger_container_ids'] = [];
186
187 $valid_currencies = $this->get_supported_currencies();
188
189 if ( ! empty( $_POST[ self::FORM_NAME ]['tracker_debug'] ) ) {
190 $site_config_sync = new SiteConfigSync( $this->settings );
191 switch ( $_POST[ self::FORM_NAME ]['tracker_debug'] ) {
192 case 'always':
193 $site_config_sync->set_config_value( 'Tracker', 'debug', 1 );
194 $site_config_sync->set_config_value( 'Tracker', 'debug_on_demand', 0 );
195 break;
196 case 'on_demand':
197 $site_config_sync->set_config_value( 'Tracker', 'debug', 0 );
198 $site_config_sync->set_config_value( 'Tracker', 'debug_on_demand', 1 );
199 break;
200 default:
201 $site_config_sync->set_config_value( 'Tracker', 'debug', 0 );
202 $site_config_sync->set_config_value( 'Tracker', 'debug_on_demand', 0 );
203 }
204 }
205
206 if ( empty( $_POST[ self::FORM_NAME ][ Settings::SITE_CURRENCY ] )
207 || ! array_key_exists( sanitize_text_field( wp_unslash( $_POST[ self::FORM_NAME ][ Settings::SITE_CURRENCY ] ) ), $valid_currencies ) ) {
208 $_POST[ self::FORM_NAME ][ Settings::SITE_CURRENCY ] = 'USD';
209 }
210
211 if ( ! empty( $_POST[ self::FORM_NAME ]['track_mode'] ) ) {
212 $track_mode = $this->get_track_mode();
213 if ( self::TRACK_MODE_TAGMANAGER === $track_mode ) {
214 // no noscript mode in this case
215 $_POST[ self::FORM_NAME ]['track_noscript'] = '';
216 $_POST[ self::FORM_NAME ]['noscript_code'] = '';
217 } else {
218 unset( $_POST['tagmanger_container_ids'] );
219 }
220 if ( $this->must_update_tracker() === true ) {
221 // We want to keep the tracking code when user switches between disabled and manually or disabled to disabled.
222 if ( ! empty( $_POST[ self::FORM_NAME ]['tracking_code'] ) ) {
223 // don't process, this is a script
224 // phpcs:disable WordPress.Security.ValidatedSanitizedInput
225 $_POST[ self::FORM_NAME ]['tracking_code'] = stripslashes( $_POST[ self::FORM_NAME ]['tracking_code'] );
226 // phpcs:enable WordPress.Security.ValidatedSanitizedInput
227 } else {
228 $_POST[ self::FORM_NAME ]['tracking_code'] = '';
229 }
230 if ( ! empty( $_POST[ self::FORM_NAME ]['noscript_code'] ) ) {
231 // don't process, this is a script
232 // phpcs:disable WordPress.Security.ValidatedSanitizedInput
233 $_POST[ self::FORM_NAME ]['noscript_code'] = stripslashes( $_POST[ self::FORM_NAME ]['noscript_code'] );
234 // phpcs:enable WordPress.Security.ValidatedSanitizedInput
235 } else {
236 $_POST[ self::FORM_NAME ]['noscript_code'] = '';
237 }
238 } else {
239 $_POST[ self::FORM_NAME ]['noscript_code'] = '';
240 $_POST[ self::FORM_NAME ]['tracking_code'] = '';
241 }
242 }
243 // phpcs:disable WordPress.Security.ValidatedSanitizedInput
244 foreach ( $_POST[ self::FORM_NAME ] as $name => $value ) {
245 if ( in_array( $name, $keys_to_keep, true ) ) {
246 $values[ $name ] = $value;
247 }
248 }
249 // phpcs:enable WordPress.Security.ValidatedSanitizedInput
250 $this->settings->apply_tracking_related_changes( $values );
251
252 return true;
253 }
254
255 private function get_track_mode() {
256 if ( ! empty( $_POST[ self::FORM_NAME ]['track_mode'] ) ) {
257 return sanitize_text_field( wp_unslash( $_POST[ self::FORM_NAME ]['track_mode'] ) );
258 }
259 return '';
260 }
261 /**
262 * Reauires form to be posted
263 *
264 * @return bool
265 */
266 private function must_update_tracker() {
267 $track_mode = $this->get_track_mode();
268 $previus_track_mode = $this->settings->get_global_option( 'track_mode' );
269 $must_update = false;
270 if ( self::TRACK_MODE_MANUALLY === $track_mode
271 || ( self::TRACK_MODE_DISABLED === $track_mode &&
272 in_array( $previus_track_mode, [ self::TRACK_MODE_DISABLED, self::TRACK_MODE_MANUALLY ], true ) ) ) {
273 // We want to keep the tracking code when user switches between disabled and manually or disabled to disabled.
274 $must_update = true;
275 }
276
277 return $must_update;
278 }
279
280 /**
281 * @return bool
282 */
283 private function form_submitted() {
284 return isset( $_POST ) && ! empty( $_POST[ self::FORM_NAME ] )
285 && is_admin()
286 && $this->can_user_manage();
287 }
288
289 /**
290 * @param string $field
291 *
292 * @return bool
293 */
294 private function has_valid_html_comments( $field ) {
295 $valid = true;
296 if ( $this->form_submitted() === true ) {
297 if ( $this->must_update_tracker() === true ) {
298 if ( ! empty( $_POST[ self::FORM_NAME ][ $field ] ) ) {
299 // phpcs:disable WordPress.Security.ValidatedSanitizedInput
300 $valid = $this->validate_html_comments( $_POST[ self::FORM_NAME ][ $field ] );
301 // phpcs:enable WordPress.Security.ValidatedSanitizedInput
302 }
303 }
304 }
305
306 return $valid;
307 }
308
309 /**
310 * @param string $html html content to validate
311 *
312 * @returns boolean
313 */
314 public function validate_html_comments( $html ) {
315 $opening = substr_count( $html, '<!--' );
316 $closing = substr_count( $html, '-->' );
317
318 return ( $opening === $closing );
319 }
320
321 public function show_settings() {
322 $was_updated = false;
323 $settings_errors = [];
324 if ( $this->has_valid_html_comments( 'tracking_code' ) !== true ) {
325 $settings_errors[] = __( 'Settings have not been saved. There is an issue with the HTML comments in the field "Tracking code". Make sure all opened comments (<!--) are closed (-->) correctly.', 'matomo' );
326 }
327 if ( $this->has_valid_html_comments( 'noscript_code' ) !== true ) {
328 $settings_errors[] = __( 'Settings have not been saved. There is an issue with the HTML comments in the field "Noscript code". Make sure all opened comments (<!--) are closed (-->) correctly.', 'matomo' );
329 }
330 if ( count( $settings_errors ) === 0 ) {
331 $was_updated = $this->update_if_submitted();
332 }
333
334 $settings = $this->settings;
335
336 $containers = $this->get_active_containers();
337
338 $track_modes = [
339 self::TRACK_MODE_DEFAULT => [
340 'name' => esc_html__( 'Auto (recommended)', 'matomo' ),
341 'disabled' => false,
342 ],
343 self::TRACK_MODE_MANUALLY => [
344 'name' => esc_html__( 'Manual', 'matomo' ),
345 'disabled' => false,
346 ],
347 self::TRACK_MODE_TAGMANAGER => [
348 'name' => esc_html__( 'Tag Manager', 'matomo' ),
349 'disabled' => false,
350 ],
351 self::TRACK_MODE_DISABLED => [
352 'name' => esc_html__( 'Disabled', 'matomo' ),
353 'disabled' => false,
354 ],
355 ];
356
357 $matomo_track_mode_descriptions = [
358 self::TRACK_MODE_DISABLED => esc_html__( 'Matomo will not add the tracking code itself. Use this mode if you want to add the tracking code by hand to your template files or you want to use another plugin to add the tracking code.', 'matomo' ),
359 self::TRACK_MODE_DEFAULT => esc_html__( 'Matomo will automatically generate and embed the tracking code based on the Auto Tracking settings below.', 'matomo' ) . ' ' . esc_html__( 'This is the recommended mode for most users.', 'matomo' ),
360 self::TRACK_MODE_MANUALLY => sprintf(
361 esc_html__( '%1$sDefine your own tracking JavaScript by hand below%2$s, and Matomo will embed it into your website.', 'matomo' ) . ( $settings->is_network_enabled() ? ' ' . esc_html__( 'Make sure to use the placeholder {ID} to add the Matomo site ID.', 'matomo' ) : '' ),
362 '<a href="#manual-tracking-settings">',
363 '</a>'
364 ),
365 self::TRACK_MODE_TAGMANAGER => esc_html__( 'If you\'ve created containers in the Tag Manager, you can use this tracking mode to embed one or more of them into your website automatically.', 'matomo' ),
366 ];
367
368 if ( empty( $containers ) ) {
369 $track_modes[ self::TRACK_MODE_TAGMANAGER ]['disabled'] = true;
370 $track_modes[ self::TRACK_MODE_TAGMANAGER ]['tooltip'] = esc_html__( 'No containers were found. Create one to be able to use the Tag Manager tracking mode.', 'matomo' );
371 $matomo_track_mode_descriptions[ self::TRACK_MODE_TAGMANAGER ] .= ' ' . esc_html__( 'This mode is not selectable since no containers have been created in the Tag Manager.', 'matomo' );
372 } else {
373 $container_select = '<div style="margin-left:1.5em" class="tagmanager-container-select">'
374 . '<label for="tagmanger_container_ids">' . esc_html__( 'Select which Tag Manager containers will be added to each page', 'matomo' ) . ':</label>';
375
376 $selected_container_ids = $settings->get_global_option( 'tagmanger_container_ids' );
377 foreach ( $containers as $container_id => $container_name ) {
378 $container_select .= '<input type="checkbox" ' . ( isset( $selected_container_ids [ $container_id ] ) && $selected_container_ids [ $container_id ] ? 'checked="checked" ' : '' ) . 'value="1" name="matomo[tagmanger_container_ids][' . esc_attr( $container_id ) . ']" /> <strong>' . esc_html( $container_name ) . '</strong> (ID: ' . esc_html( $container_id ) . ')&nbsp; <br />';
379 }
380
381 $container_select .= '<a style="margin-top:.5em;display:inline-block;" href="' . esc_url( menu_page_url( \WpMatomo\Admin\Menu::SLUG_TAGMANAGER, false ) ) . '" rel="noreferrer noopener" target="_blank">Edit containers <span class="dashicons-before dashicons-external"></span></a>';
382 $container_select .= '<p style="margin-top:1em"><span class="dashicons dashicons-info-outline"></span> ' . sprintf( esc_html__( 'For Matomo to track you will need to %1$sadd a Matomo Tag to the container%2$s. It otherwise won\'t track automatically.', 'matomo' ), '<a href="https://matomo.org/faq/tag-manager/how-do-i-track-pageviews-of-my-website-using-matomo-tag-manager/" target="_blank" rel="noreferrer noopener">', '</a>' ) . '</p>';
383 $container_select .= '</div>';
384
385 $matomo_track_mode_descriptions[ self::TRACK_MODE_TAGMANAGER ] .= $container_select;
386 }
387
388 $matomo_track_mode_descriptions[ self::TRACK_MODE_TAGMANAGER ] .= '<a id="tagmanager-read-more-link" style="display:inline-block" href="https://matomo.org/guide/tag-manager/getting-started-with-tag-manager/" target="_blank" rel="noreferrer noopener">' . esc_html__( 'Read our documentation on the Matomo Tag Manager to learn more.', 'matomo' ) . '</a>';
389
390 $site = new Site();
391 $idsite = $site->get_current_matomo_site_id();
392
393 $matomo_currencies = $this->get_supported_currencies();
394
395 $cookie_consent_modes = $this->get_cookie_consent_modes();
396
397 $tracking_code_generator = new TrackingCodeGenerator( $this->settings, new GeneratorOptions( $this->settings ) );
398 $matomo_default_tracking_code = $tracking_code_generator->prepare_tracking_code( $idsite );
399
400 $matomo_exclusion_settings_url = home_url( '/wp-admin/admin.php?page=matomo-settings&tab=exlusions' );
401
402 $matomo_is_advanced_cache_used = self::is_advanced_cache_used();
403 $matomo_is_track_script_used_in_wp_config = self::is_track_script_used_in_wp_config();
404 $matomo_is_htaccess_serving_cache_files = self::is_htaccess_serving_cache_files();
405
406 $matomo_is_track_ai_enabled = $this->settings->is_ai_bot_tracking_enabled();
407 $matomo_is_track_via_esi_enabled = $this->settings->is_track_via_esi_enabled();
408
409 $matomo_is_using_litespeed = $this->is_using_litespeed_web_server();
410 $matomo_is_using_litespeed_cache = $this->is_using_litespeed_cache_plugin();
411 $matomo_is_esi_enabled_in_litespeed = $this->is_litespeed_esi_enabled_in_webserver();
412
413 include dirname( __FILE__ ) . '/views/tracking.php';
414 }
415
416 /**
417 * @return string[]
418 */
419 private function get_cookie_consent_modes() {
420 $modes = [];
421 foreach ( CookieConsent::get_available_options() as $option => $description ) {
422 $modes[ $option ] = $description;
423 }
424
425 return $modes;
426 }
427
428 private function get_supported_currencies() {
429 $all = include dirname( MATOMO_ANALYTICS_FILE ) . '/app/core/Intl/Data/Resources/currencies.php';
430 $currencies = [];
431 foreach ( $all as $key => $single ) {
432 $currencies[ $key ] = $single[0] . ' ' . $single[1];
433 }
434
435 return $currencies;
436 }
437
438 public function get_active_containers() {
439 // we don't use Matomo API here to avoid needing to bootstrap Matomo which is slow and could break things
440 $containers = [];
441 if ( matomo_has_tag_manager() ) {
442 global $wpdb;
443 $db_settings = new \WpMatomo\Db\Settings();
444 $container_table = $db_settings->prefix_table_name( 'tagmanager_container' );
445 try {
446 // phpcs:disable WordPress.DB
447 $containers = $wpdb->get_results( sprintf( 'SELECT `idcontainer`, `name` FROM %s where `status` = "active"', $container_table ) );
448 // phpcs:enable WordPress.DB
449 } catch ( Exception $e ) {
450 // table may not exist yet etc
451 $containers = [];
452 }
453 }
454 $by_id = [];
455 foreach ( $containers as $container ) {
456 $by_id[ $container->idcontainer ] = $container->name;
457 }
458
459 return $by_id;
460 }
461
462 private function add_hooks() {
463 add_action(
464 'admin_enqueue_scripts',
465 function ( $page ) {
466 if ( 'matomo-analytics_page_matomo-settings' !== $page ) {
467 return;
468 }
469
470 wp_enqueue_script(
471 'matomo-tracking-settings',
472 plugins_url( '/assets/js/settings.js', MATOMO_ANALYTICS_FILE ),
473 [ 'jquery' ],
474 matomo_get_asset_version(),
475 true
476 );
477
478 wp_localize_script(
479 'matomo-tracking-settings',
480 'mtmTrackingSettingsAjax',
481 [
482 'ajax_url' => admin_url( 'admin-ajax.php' ),
483 'nonce' => wp_create_nonce( self::NONCE_NAME_GENERATE_TRACKING_CODE_AJAX ),
484 ]
485 );
486 }
487 );
488 }
489
490 public static function register_ajax() {
491 add_action( 'wp_ajax_matomo_generate_tracking_code', [ self::class, 'generate_tracking_code' ] );
492 }
493
494 public static function generate_tracking_code() {
495 check_ajax_referer( self::NONCE_NAME_GENERATE_TRACKING_CODE_AJAX );
496
497 $blog_id = get_current_blog_id();
498 $idsite = Site::get_matomo_site_id( $blog_id );
499
500 // phpcs complains if reading from $_POST is not done this way
501 $overrides = [
502 'track_datacfasync' => isset( $_POST['track_datacfasync'] ) ? ( boolval( wp_unslash( $_POST['track_datacfasync'] ) ) ) : false,
503 'track_content' => isset( $_POST['track_content'] ) ? ( sanitize_text_field( wp_unslash( $_POST['track_content'] ) ) ) : '',
504 'track_heartbeat' => isset( $_POST['track_heartbeat'] ) ? ( intval( wp_unslash( $_POST['track_heartbeat'] ) ) ) : 0,
505 'limit_cookies' => isset( $_POST['limit_cookies'] ) ? ( boolval( wp_unslash( $_POST['limit_cookies'] ) ) ) : false,
506 'limit_cookies_visitor' => isset( $_POST['limit_cookies_visitor'] ) ? ( intval( wp_unslash( $_POST['limit_cookies_visitor'] ) ) ) : 0,
507 'limit_cookies_session' => isset( $_POST['limit_cookies_session'] ) ? ( intval( wp_unslash( $_POST['limit_cookies_session'] ) ) ) : 0,
508 'limit_cookies_referral' => isset( $_POST['limit_cookies_referral'] ) ? ( intval( wp_unslash( $_POST['limit_cookies_referral'] ) ) ) : 0,
509 'cookie_consent' => isset( $_POST['cookie_consent'] ) ? ( sanitize_text_field( wp_unslash( $_POST['cookie_consent'] ) ) ) : '',
510 'force_post' => isset( $_POST['force_post'] ) ? ( wp_unslash( boolval( $_POST['force_post'] ) ) ) : false,
511 'track_across_alias' => isset( $_POST['track_across_alias'] ) ? ( boolval( wp_unslash( $_POST['track_across_alias'] ) ) ) : false,
512 'track_across' => isset( $_POST['track_across'] ) ? ( boolval( wp_unslash( $_POST['track_across'] ) ) ) : false,
513 'track_crossdomain_linking' => isset( $_POST['track_crossdomain_linking'] ) ? ( boolval( wp_unslash( $_POST['track_crossdomain_linking'] ) ) ) : false,
514 'track_jserrors' => isset( $_POST['track_jserrors'] ) ? ( boolval( wp_unslash( $_POST['track_jserrors'] ) ) ) : false,
515 'disable_cookies' => isset( $_POST['disable_cookies'] ) ? ( boolval( wp_unslash( $_POST['disable_cookies'] ) ) ) : false,
516 'set_link_classes' => isset( $_POST['set_link_classes'] ) ? ( sanitize_text_field( wp_unslash( $_POST['set_link_classes'] ) ) ) : '',
517 'set_download_classes' => isset( $_POST['set_download_classes'] ) ? ( sanitize_text_field( wp_unslash( $_POST['set_download_classes'] ) ) ) : '',
518 'add_download_extensions' => isset( $_POST['add_download_extensions'] ) ? ( sanitize_text_field( wp_unslash( $_POST['add_download_extensions'] ) ) ) : '',
519 'track_api_endpoint' => isset( $_POST['track_api_endpoint'] ) ? ( sanitize_text_field( wp_unslash( $_POST['track_api_endpoint'] ) ) ) : '',
520 'force_protocol' => isset( $_POST['force_protocol'] ) ? ( boolval( wp_unslash( $_POST['force_protocol'] ) ) ) : false,
521 'track_js_endpoint' => isset( $_POST['track_js_endpoint'] ) ? ( sanitize_text_field( wp_unslash( $_POST['track_js_endpoint'] ) ) ) : '',
522 'set_download_extensions' => isset( $_POST['set_download_extensions'] ) ? ( sanitize_text_field( wp_unslash( $_POST['set_download_extensions'] ) ) ) : '',
523 ];
524
525 $generator = new TrackingCodeGenerator( \WpMatomo::$settings, new GeneratorOptions( \WpMatomo::$settings, $overrides ) );
526 $tracking_code = $generator->prepare_tracking_code( $idsite );
527
528 wp_send_json( $tracking_code );
529 }
530
531 public function is_using_litespeed_web_server() {
532 return php_sapi_name() === 'litespeed';
533 }
534
535 public function is_using_litespeed_cache_plugin() {
536 return is_plugin_active( 'litespeed-cache/litespeed-cache.php' );
537 }
538
539 private function is_litespeed_esi_enabled_in_webserver() {
540 // see https://docs.litespeedtech.com/lscache/lscwp/api/#get-esi-enable-status
541 // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
542 return (bool) apply_filters( 'litespeed_esi_status', false );
543 }
544 }
545