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