PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.0.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.0.2
5.13.0 5.12.1 5.12.0 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 / Settings.php
matomo / classes / WpMatomo Last commit date
Admin 5 years ago Commands 5 years ago Db 5 years ago Ecommerce 5 years ago Report 6 years ago Site 5 years ago TrackingCode 5 years ago Updater 5 years ago User 5 years ago views 6 years ago API.php 5 years ago Access.php 6 years ago AjaxTracker.php 5 years ago Annotations.php 6 years ago Bootstrap.php 6 years ago Capabilities.php 6 years ago Compatibility.php 6 years ago Email.php 5 years ago Installer.php 5 years ago Logger.php 5 years ago OptOut.php 6 years ago Paths.php 6 years ago PrivacyBadge.php 6 years ago Referral.php 6 years ago Roles.php 6 years ago ScheduledTasks.php 5 years ago Settings.php 5 years ago Site.php 6 years ago TrackingCode.php 5 years ago Uninstaller.php 6 years ago Updater.php 5 years ago User.php 6 years ago
Settings.php
488 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 * Code Based on
9 * @author Andr&eacute; Br&auml;kling
10 * https://github.com/braekling/matomo
11 *
12 */
13
14 namespace WpMatomo;
15
16 use WpMatomo\Admin\TrackingSettings;
17
18 if ( ! defined( 'ABSPATH' ) ) {
19 exit; // if accessed directly
20 }
21
22 class Settings {
23
24 const OPTION_PREFIX = 'matomo-';
25 const GLOBAL_OPTION_PREFIX = 'matomo_global-';
26 const OPTION = 'matomo-option';
27 const OPTION_GLOBAL = 'matomo-global-option';
28 const OPTION_KEY_CAPS_ACCESS = 'caps_access';
29 const OPTION_KEY_STEALTH = 'caps_tracking';
30 const OPTION_LAST_TRACKING_SETTINGS_CHANGE = 'last_tracking_settings_update';
31 const OPTION_LAST_TRACKING_CODE_UPDATE = 'last_tracking_code_update';
32 const SHOW_GET_STARTED_PAGE = 'show_get_started_page';
33 const DELETE_ALL_DATA_ON_UNINSTALL = 'delete_all_data_uninstall';
34 const SITE_CURRENCY = 'site_currency';
35 const NETWORK_CONFIG_OPTIONS = 'config_options';
36
37 public static $is_doing_action_tracking_related = false;
38 /**
39 * @internal tests only
40 * @var bool
41 */
42 public $force_disable_addhandler = false;
43
44 /**
45 * Tests only
46 *
47 * @ignore
48 * @var bool
49 */
50 private $assume_is_network_enabled_in_tests = false;
51
52 /**
53 * Register default configuration set
54 *
55 * @var array
56 */
57 private $default_global_settings = array(
58 // Plugin settings
59 'last_settings_update' => 0,
60 self::OPTION_LAST_TRACKING_SETTINGS_CHANGE => 0,
61 self::OPTION_KEY_STEALTH => array(),
62 self::OPTION_KEY_CAPS_ACCESS => array(),
63 self::NETWORK_CONFIG_OPTIONS => array(),
64 self::DELETE_ALL_DATA_ON_UNINSTALL => true,
65 self::SITE_CURRENCY => 'USD',
66 // User settings: Stats configuration
67 // User settings: Tracking configuration
68 'track_mode' => 'disabled',
69 'track_js_endpoint' => 'default',
70 'track_api_endpoint' => 'default',
71 'track_codeposition' => 'footer',
72 'track_noscript' => false,
73 'track_content' => 'disabled',
74 'track_ecommerce' => true,
75 'track_search' => false,
76 'track_404' => false,
77 'tagmanger_container_ids' => array(),
78 'add_post_annotations' => array(),
79 'add_customvars_box' => false,
80 'js_manually' => '',
81 'noscript_manually' => '',
82 'add_download_extensions' => '',
83 'set_download_extensions' => '',
84 'set_link_classes' => '',
85 'set_download_classes' => '',
86 'core_version' => '',
87 'version_history' => array(),
88 'mail_history' => array(),
89 'disable_cookies' => false,
90 'force_post' => false,
91 'limit_cookies' => false,
92 'limit_cookies_visitor' => 34186669, // Matomo default 13 months
93 'limit_cookies_session' => 1800, // Matomo default 30 minutes
94 'limit_cookies_referral' => 15778463, // Matomo default 6 months
95 'track_admin' => false,
96 'track_across' => false,
97 'track_across_alias' => false,
98 'track_crossdomain_linking' => false,
99 'track_feed' => false,
100 'track_feed_addcampaign' => false,
101 'track_feed_campaign' => 'feed',
102 'track_heartbeat' => 0,
103 'track_user_id' => 'disabled',
104 'track_datacfasync' => false,
105 'track_jserrors' => false,
106 'force_protocol' => 'disabled',
107 'maxmind_license_key' => '',
108 self::SHOW_GET_STARTED_PAGE => 1,
109 );
110
111 /**
112 * Settings stored per blog
113 *
114 * @var array
115 */
116 private $default_blog_settings = array(
117 'noscript_code' => '',
118 'tracking_code' => '',
119 self::OPTION_LAST_TRACKING_CODE_UPDATE => 0,
120 );
121
122 private $global_settings = array();
123 private $blog_settings = array();
124
125 private $settings_changed = array();
126
127 /**
128 * @var Logger
129 */
130 private $logger;
131
132 /**
133 * Constructor class to prepare settings manager
134 *
135 */
136 public function __construct() {
137 $this->logger = new Logger();
138
139 $this->init_settings();
140 }
141
142 public function init_settings() {
143 $this->settings_changed = array();
144 $this->global_settings = array();
145 $this->blog_settings = array();
146
147 if ( $this->is_network_enabled() ) {
148 $global_settings = get_site_option( self::OPTION_GLOBAL, array() );
149 } else {
150 $global_settings = get_option( self::OPTION_GLOBAL, array() );
151 }
152
153 if ( ! empty( $global_settings ) && is_array( $global_settings ) ) {
154 $this->global_settings = $global_settings;
155 }
156
157 $settings = get_option( self::OPTION, array() );
158
159 if ( ! empty( $settings ) && is_array( $settings ) ) {
160 $this->blog_settings = $settings;
161 }
162 }
163
164 public function get_customised_global_settings() {
165 $custom_settings = array();
166
167 foreach ( $this->global_settings as $key => $val ) {
168 if ( isset( $this->default_global_settings[ $key ] )
169 && $this->default_global_settings[ $key ] != $val ) {
170 $custom_settings[ $key ] = $val;
171 }
172 }
173
174 return $custom_settings;
175 }
176
177 public function uninstall() {
178 Uninstaller::uninstall_options( self::OPTION_PREFIX );
179 Uninstaller::uninstall_options( self::GLOBAL_OPTION_PREFIX );
180 Uninstaller::uninstall_site_meta( self::GLOBAL_OPTION_PREFIX );
181 $this->init_settings();
182 }
183
184 public function is_multisite() {
185 return function_exists( 'is_multisite' ) && is_multisite();
186 }
187
188 /**
189 * @api
190 */
191 public function is_network_enabled() {
192 if ( $this->assume_is_network_enabled_in_tests ) {
193 return true;
194 }
195
196 if ( ! function_exists( 'is_plugin_active_for_network' ) ) {
197 require_once ABSPATH . 'wp-admin/includes/plugin.php';
198 }
199
200 return is_plugin_active_for_network( 'matomo/matomo.php' );
201 }
202
203 /**
204 * Save all settings as WordPress options
205 */
206 public function save() {
207 if ( empty( $this->settings_changed ) ) {
208 $this->logger->log( 'No settings changed yet' );
209
210 return;
211 }
212
213 $this->logger->log( 'Save settings' );
214
215 if ( $this->is_network_enabled() ) {
216 update_site_option( self::OPTION_GLOBAL, $this->global_settings );
217 } else {
218 update_option( self::OPTION_GLOBAL, $this->global_settings );
219 }
220
221 update_option( self::OPTION, $this->blog_settings );
222
223 $keys_changed = array_values( array_unique( $this->settings_changed ) );
224 $this->settings_changed = array();
225
226 foreach ( $keys_changed as $key_changed ) {
227 do_action( 'matomo_setting_change_' . $key_changed );
228 }
229 }
230
231 /**
232 * Get a global option's value
233 *
234 * @param string $key
235 * option key
236 *
237 * @return string|array option value
238 * @api
239 */
240 public function get_global_option( $key ) {
241 if ( isset( $this->global_settings[ $key ] ) ) {
242 return $this->global_settings[ $key ];
243 }
244
245 if ( isset( $this->default_global_settings[ $key ] ) ) {
246 return $this->default_global_settings[ $key ];
247 }
248 }
249
250 /**
251 * Get an option's value related to a specific blog
252 *
253 * @param string $key option key
254 *
255 * @return string|array
256 * @api
257 */
258 public function get_option( $key ) {
259 if ( isset( $this->blog_settings[ $key ] ) ) {
260 return $this->blog_settings[ $key ];
261 }
262
263 if ( isset( $this->default_blog_settings[ $key ] ) ) {
264 return $this->default_blog_settings[ $key ];
265 }
266 }
267
268 private function convert_type( $value, $type ) {
269 if ( $type === 'array' && empty( $value ) ) {
270 $value = array(); // prevent eg converting '' to array('')
271 } else {
272 settype( $value, $type );
273 }
274 return $value;
275 }
276
277 /**
278 * Set a global option's value
279 *
280 * @param string $key option key
281 * @param string|array $value new option value
282 */
283 public function set_global_option( $key, $value ) {
284 if ( isset( $this->default_global_settings[ $key ] ) ) {
285 $type = gettype( $this->default_global_settings[ $key ] );
286 $value = $this->convert_type( $value, $type );
287 }
288
289 if ( ! isset( $this->global_settings[ $key ] )
290 || $this->global_settings[ $key ] !== $value ) {
291 $this->settings_changed[] = $key;
292 $this->logger->log( 'Changed global option ' . $key . ': ' . ( is_array( $value ) ? wp_json_encode( $value ) : $value ) );
293
294 $this->global_settings[ $key ] = $value;
295 }
296 }
297
298 /**
299 * Set an option's value related to a specific blog
300 *
301 * @param string $key option key
302 * @param string $value new option value
303 */
304 public function set_option( $key, $value ) {
305 if ( isset( $this->default_blog_settings[ $key ] ) ) {
306 $type = gettype( $this->default_blog_settings[ $key ] );
307 $value = $this->convert_type( $value, $type );
308 }
309
310 if ( ! isset( $this->blog_settings[ $key ] )
311 || $this->blog_settings[ $key ] !== $value ) {
312 $this->settings_changed[] = $key;
313 $this->logger->log( 'Changed option ' . $key . ': ' . $value );
314 $this->blog_settings[ $key ] = $value;
315 }
316 }
317
318 /**
319 * @param $values
320 *
321 * @api
322 */
323 public function apply_tracking_related_changes( $values ) {
324 $this->set_global_option( self::OPTION_LAST_TRACKING_SETTINGS_CHANGE, time() );
325
326 $this->apply_changes( $values );
327
328 if ( ! self::$is_doing_action_tracking_related ) {
329 // prevent recurison if any plugin was listening to this event and calling this method again
330 self::$is_doing_action_tracking_related = true;
331 do_action( 'matomo_tracking_settings_changed', $this, $values );
332 self::$is_doing_action_tracking_related = false;
333 }
334 }
335
336 public function should_disable_addhandler()
337 {
338 if ($this->force_disable_addhandler) {
339 return true;
340 }
341 return defined('MATOMO_DISABLE_ADDHANDLER') && MATOMO_DISABLE_ADDHANDLER;
342 }
343
344 /**
345 * Apply new configuration
346 *
347 * @param array $settings
348 * new configuration set
349 *
350 * @api
351 */
352 public function apply_changes( $settings ) {
353 $this->logger->log( 'Apply changed settings:' );
354 foreach ( $this->default_global_settings as $key => $val ) {
355 if ( isset( $settings[ $key ] ) ) {
356 $this->set_global_option( $key, $settings[ $key ] );
357 }
358 }
359 foreach ( $this->default_blog_settings as $key => $val ) {
360 if ( isset( $settings[ $key ] ) ) {
361 $this->set_option( $key, $settings[ $key ] );
362 }
363 }
364 $this->set_global_option( 'last_settings_update', time() );
365
366 if ( $this->should_save_tracking_code_across_sites() ) {
367 // special case for when the same tracking code needs to be used across all instances
368 $this->set_global_option( 'js_manually', $this->get_option( 'tracking_code' ) );
369 $this->set_global_option( 'noscript_manually', $this->get_option( 'noscript_code' ) );
370 }
371
372 $this->save();
373 }
374
375 private function should_save_tracking_code_across_sites() {
376 return $this->is_network_enabled()
377 && $this->get_global_option( 'track_mode' ) === TrackingSettings::TRACK_MODE_MANUALLY;
378 }
379
380 public function get_js_tracking_code() {
381 if ( $this->should_save_tracking_code_across_sites() ) {
382 return $this->get_global_option( 'js_manually' );
383 }
384
385 return $this->get_option( 'tracking_code' );
386 }
387
388 public function get_noscript_tracking_code() {
389 if ( $this->should_save_tracking_code_across_sites() ) {
390 return $this->get_global_option( 'noscript_manually' );
391 }
392
393 return $this->get_option( 'noscript_code' );
394 }
395
396 public function is_cross_domain_linking_enabled() {
397 return $this->get_global_option( 'track_crossdomain_linking' );
398 }
399
400 public function get_tracking_cookie_domain() {
401 if ( $this->get_global_option( 'track_across' )
402 || $this->get_global_option( 'track_crossdomain_linking' ) ) {
403 $host = @parse_url( home_url(), PHP_URL_HOST );
404 if ( ! empty( $host ) ) {
405 return '*.' . $host;
406 }
407 }
408
409 return '';
410 }
411
412 public function should_delete_all_data_on_uninstall()
413 {
414 if (defined( 'MATOMO_REMOVE_ALL_DATA' )) {
415 return (bool) MATOMO_REMOVE_ALL_DATA;
416 }
417
418 return (bool) $this->get_global_option(self::DELETE_ALL_DATA_ON_UNINSTALL);
419 }
420
421 /**
422 * Check if feed tracking is enabled
423 *
424 * @return boolean Is feed tracking enabled?
425 */
426 public function is_track_feed() {
427 return $this->get_global_option( 'track_feed' );
428 }
429
430 /**
431 * Check if admin tracking is enabled
432 *
433 * @return boolean Is admin tracking enabled?
434 */
435 public function is_admin_tracking_enabled() {
436 return $this->get_global_option( 'track_admin' ) && is_admin();
437 }
438
439 public function is_current_tracking_code() {
440 $last_tracking_code_update = $this->get_option( self::OPTION_LAST_TRACKING_CODE_UPDATE );
441 $last_tracking_settings_update = $this->get_global_option( self::OPTION_LAST_TRACKING_SETTINGS_CHANGE );
442
443 return $last_tracking_code_update && $last_tracking_code_update > $last_tracking_settings_update;
444 }
445
446 /**
447 * Check if feed permalinks get a campaign parameter
448 *
449 * @return boolean Add campaign parameter to feed permalinks?
450 */
451 public function is_add_feed_campaign() {
452 return $this->get_global_option( 'track_feed_addcampaign' );
453 }
454
455 public function is_tracking_enabled() {
456 return $this->get_global_option( 'track_mode' ) != 'disabled';
457 }
458
459 /**
460 * Check if noscript code insertion is enabled
461 *
462 * @return boolean Insert noscript code?
463 */
464 public function is_add_no_script_code() {
465 return $this->get_global_option( 'track_noscript' );
466 }
467
468 public function get_tracking_code_position() {
469 return $this->get_global_option( 'track_codeposition' );
470 }
471
472 public function track_404_enabled() {
473 return $this->get_global_option( 'track_404' );
474 }
475
476 public function track_user_id_enabled() {
477 return $this->get_global_option( 'track_user_id' ) != 'disabled';
478 }
479
480 public function track_search_enabled() {
481 return ( is_search() && $this->get_global_option( 'track_search' ) );
482 }
483
484 public function set_assume_is_network_enabled_in_tests( $network_enabled = true ) {
485 $this->assume_is_network_enabled_in_tests = $network_enabled;
486 }
487 }
488