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