Settings.php 12 KB
Newer Older
ale's avatar
ale committed
1
2
3
4
5
6
7
8
9
10
11
<?php

namespace WP_Piwik;

/**
 * Manage WP-Piwik settings
 *
 * @author Andr&eacute; Br&auml;kling
 * @package WP_Piwik
 */
class Settings {
lechuck's avatar
lechuck committed
12

ale's avatar
ale committed
13
14
15
16
17
	/**
	 *
	 * @var Environment variables and default settings container
	 */
	private static $wpPiwik, $defaultSettings;
lechuck's avatar
lechuck committed
18

ale's avatar
ale committed
19
20
21
22
23
24
25
26
27
	/**
	 *
	 * @var Define callback functions for changed settings
	 */
	private $checkSettings = array (
			'piwik_url' => 'checkPiwikUrl',
			'piwik_token' => 'checkPiwikToken',
			'site_id' => 'requestPiwikSiteID',
			'tracking_code' => 'prepareTrackingCode',
lechuck's avatar
lechuck committed
28
			'noscript_code' => 'prepareNocscriptCode'
ale's avatar
ale committed
29
	);
lechuck's avatar
lechuck committed
30

ale's avatar
ale committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
	/**
	 *
	 * @var Register default configuration set
	 */
	private $globalSettings = array (
			// Plugin settings
			'revision' => 0,
			'last_settings_update' => 0,
			// User settings: Piwik configuration
			'piwik_mode' => 'http',
			'piwik_url' => '',
			'piwik_path' => '',
			'piwik_user' => '',
			'piwik_token' => '',
			'auto_site_config' => true,
			// User settings: Stats configuration
			'default_date' => 'yesterday',
			'stats_seo' => false,
lucha's avatar
lucha committed
49
            'stats_ecommerce' => false,
ale's avatar
ale committed
50
			'dashboard_widget' => false,
lucha's avatar
lucha committed
51
			'dashboard_ecommerce' => false,
ale's avatar
ale committed
52
53
54
55
			'dashboard_chart' => false,
			'dashboard_seo' => false,
			'toolbar' => false,
			'capability_read_stats' => array (
lechuck's avatar
lechuck committed
56
					'administrator' => true
ale's avatar
ale committed
57
58
59
60
61
62
63
64
65
66
67
68
69
70
			),
			'perpost_stats' => false,
			'plugin_display_name' => 'WP-Piwik',
			'piwik_shortcut' => false,
			'shortcodes' => false,
			// User settings: Tracking configuration
			'track_mode' => 'disabled',
			'track_codeposition' => 'footer',
			'track_noscript' => false,
			'track_nojavascript' => false,
			'proxy_url' => '',
			'track_content' => 'disabled',
			'track_search' => false,
			'track_404' => false,
lucha's avatar
lucha committed
71
			'add_post_annotations' => array(),
ale's avatar
ale committed
72
73
			'add_customvars_box' => false,
			'add_download_extensions' => '',
lucha's avatar
lucha committed
74
			'set_download_extensions' => '',
lucha's avatar
lucha committed
75
76
			'set_link_classes' => '',
			'set_download_classes' => '',
ale's avatar
ale committed
77
78
79
80
81
82
83
84
85
			'disable_cookies' => false,
			'limit_cookies' => false,
			'limit_cookies_visitor' => 34186669, // Piwik default 13 months
			'limit_cookies_session' => 1800, // Piwik default 30 minutes
			'limit_cookies_referral' => 15778463, // Piwik default 6 months
			'track_admin' => false,
			'capability_stealth' => array (),
			'track_across' => false,
			'track_across_alias' => false,
lucha's avatar
lucha committed
86
			'track_crossdomain_linking' => false,
ale's avatar
ale committed
87
88
89
90
			'track_feed' => false,
			'track_feed_addcampaign' => false,
			'track_feed_campaign' => 'feed',
			'track_heartbeat' => 0,
lechuck's avatar
lechuck committed
91
			'track_user_id' => 'disabled',
ale's avatar
ale committed
92
93
94
95
96
97
98
			// User settings: Expert configuration
			'cache' => true,
			'http_connection' => 'curl',
			'http_method' => 'post',
			'disable_timelimit' => false,
			'connection_timeout' => 5,
			'disable_ssl_verify' => false,
lucha's avatar
lucha committed
99
			'disable_ssl_verify_host' => false,
ale's avatar
ale committed
100
101
			'piwik_useragent' => 'php',
			'piwik_useragent_string' => 'WP-Piwik',
lucha's avatar
lucha committed
102
            'dnsprefetch' => false,
ale's avatar
ale committed
103
104
105
106
107
108
109
110
111
112
113
			'track_datacfasync' => false,
			'track_cdnurl' => '',
			'track_cdnurlssl' => '',
			'force_protocol' => 'disabled',
			'update_notice' => 'enabled'
	), $settings = array (
			'name' => '',
			'site_id' => NULL,
			'noscript_code' => '',
			'tracking_code' => '',
			'last_tracking_code_update' => 0,
lechuck's avatar
lechuck committed
114
			'dashboard_revision' => 0
ale's avatar
ale committed
115
	), $settingsChanged = false;
lechuck's avatar
lechuck committed
116

ale's avatar
ale committed
117
118
119
120
121
122
123
124
125
126
127
	/**
	 * Constructor class to prepare settings manager
	 *
	 * @param WP_Piwik $wpPiwik
	 *        	active WP-Piwik instance
	 */
	public function __construct($wpPiwik) {
		self::$wpPiwik = $wpPiwik;
		self::$wpPiwik->log ( 'Store default settings' );
		self::$defaultSettings = array (
				'globalSettings' => $this->globalSettings,
lechuck's avatar
lechuck committed
128
				'settings' => $this->settings
ale's avatar
ale committed
129
130
131
132
133
134
135
136
		);
		self::$wpPiwik->log ( 'Load settings' );
		foreach ( $this->globalSettings as $key => $default ) {
			$this->globalSettings [$key] = ($this->checkNetworkActivation () ? get_site_option ( 'wp-piwik_global-' . $key, $default ) : get_option ( 'wp-piwik_global-' . $key, $default ));
		}
		foreach ( $this->settings as $key => $default )
			$this->settings [$key] = get_option ( 'wp-piwik-' . $key, $default );
	}
lechuck's avatar
lechuck committed
137

ale's avatar
ale committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
	/**
	 * Save all settings as WordPress options
	 */
	public function save() {
		if (! $this->settingsChanged) {
			self::$wpPiwik->log ( 'No settings changed yet' );
			return;
		}
		self::$wpPiwik->log ( 'Save settings' );
		foreach ( $this->globalSettings as $key => $value ) {
			if ( $this->checkNetworkActivation() )
				update_site_option ( 'wp-piwik_global-' . $key, $value );
			else
				update_option ( 'wp-piwik_global-' . $key, $value );
		}
		foreach ( $this->settings as $key => $value ) {
			update_option ( 'wp-piwik-' . $key, $value );
		}
		global $wp_roles;
		if (! is_object ( $wp_roles ))
			$wp_roles = new \WP_Roles ();
		if (! is_object ( $wp_roles ))
			die ( "STILL NO OBJECT" );
		foreach ( $wp_roles->role_names as $strKey => $strName ) {
			$objRole = get_role ( $strKey );
			foreach ( array (
					'stealth',
lechuck's avatar
lechuck committed
165
					'read_stats'
ale's avatar
ale committed
166
167
168
169
170
171
172
173
174
			) as $strCap ) {
				$aryCaps = $this->getGlobalOption ( 'capability_' . $strCap );
				if (isset ( $aryCaps [$strKey] ) && $aryCaps [$strKey])
					$wp_roles->add_cap ( $strKey, 'wp-piwik_' . $strCap );
				else $wp_roles->remove_cap ( $strKey, 'wp-piwik_' . $strCap );
			}
		}
		$this->settingsChanged = false;
	}
lechuck's avatar
lechuck committed
175

ale's avatar
ale committed
176
177
178
179
180
181
182
183
184
185
	/**
	 * Get a global option's value
	 *
	 * @param string $key
	 *        	option key
	 * @return string option value
	 */
	public function getGlobalOption($key) {
		return isset ( $this->globalSettings [$key] ) ? $this->globalSettings [$key] : self::$defaultSettings ['globalSettings'] [$key];
	}
lechuck's avatar
lechuck committed
186

ale's avatar
ale committed
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
	/**
	 * Get an option's value related to a specific blog
	 *
	 * @param string $key
	 *        	option key
	 * @param int $blogID
	 *        	blog ID (default: current blog)
	 * @return \WP_Piwik\Register
	 */
	public function getOption($key, $blogID = null) {
		if ($this->checkNetworkActivation () && ! empty ( $blogID )) {
			return get_blog_option ( $blogID, 'wp-piwik-'.$key );
		}
		return isset ( $this->settings [$key] ) ? $this->settings [$key] : self::$defaultSettings ['settings'] [$key];
	}
lechuck's avatar
lechuck committed
202

ale's avatar
ale committed
203
204
205
206
207
208
209
210
211
212
213
214
215
	/**
	 * Set a global option's value
	 *
	 * @param string $key
	 *        	option key
	 * @param string $value
	 *        	new option value
	 */
	public function setGlobalOption($key, $value) {
		$this->settingsChanged = true;
		self::$wpPiwik->log ( 'Changed global option ' . $key . ': ' . (is_array ( $value ) ? serialize ( $value ) : $value) );
		$this->globalSettings [$key] = $value;
	}
lechuck's avatar
lechuck committed
216

ale's avatar
ale committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
	/**
	 * Set an option's value related to a specific blog
	 *
	 * @param string $key
	 *        	option key
	 * @param int $blogID
	 *        	blog ID (default: current blog)
	 * @param string $value
	 *        	new option value
	 */
	public function setOption($key, $value, $blogID = null) {
		$this->settingsChanged = true;
		self::$wpPiwik->log ( 'Changed option ' . $key . ': ' . $value );
		if ($this->checkNetworkActivation () && ! empty ( $blogID )) {
			add_blog_option ( $blogID, 'wp-piwik-'.$key, $value );
		} else
			$this->settings [$key] = $value;
	}
lechuck's avatar
lechuck committed
235

ale's avatar
ale committed
236
237
238
239
240
241
242
243
244
245
	/**
	 * Reset settings to default
	 */
	public function resetSettings() {
		self::$wpPiwik->log ( 'Reset WP-Piwik settings' );
		global $wpdb;
		if ( $this->checkNetworkActivation() ) {
			$aryBlogs = self::getBlogList();
			if (is_array($aryBlogs))
				foreach ($aryBlogs as $aryBlog) {
lucha's avatar
lucha committed
246
                    switch_to_blog($aryBlog['blog_id']);
ale's avatar
ale committed
247
248
249
250
251
252
253
					$wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'wp-piwik-%'");
					restore_current_blog();
				}
			$wpdb->query("DELETE FROM $wpdb->sitemeta WHERE meta_key LIKE 'wp-piwik_global-%'");
		}
		else $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'wp-piwik_global-%'");
	}
lechuck's avatar
lechuck committed
254

ale's avatar
ale committed
255
256
257
	/**
	 * Get blog list
	 */
lucha's avatar
lucha committed
258
	public static function getBlogList($limit = null, $page = null, $search = '') {
lechuck's avatar
lechuck committed
259
		if ($limit && $page)
ale's avatar
ale committed
260
261
			$queryLimit = ' LIMIT '.(int) (($page - 1) * $limit).','.(int) $limit;
		global $wpdb;
lucha's avatar
lucha committed
262
		return $wpdb->get_results($wpdb->prepare('SELECT blog_id FROM '.$wpdb->blogs.' WHERE CONCAT(domain, path) LIKE "%%%s%%" AND spam = 0 AND deleted = 0 ORDER BY blog_id'.$queryLimit, $search), ARRAY_A);
ale's avatar
ale committed
263
	}
lechuck's avatar
lechuck committed
264

ale's avatar
ale committed
265
266
267
268
269
270
271
272
273
274
	/**
	 * Check if plugin is network activated
	 *
	 * @return boolean Is network activated?
	 */
	public function checkNetworkActivation() {
		if (! function_exists ( "is_plugin_active_for_network" ))
			require_once (ABSPATH . 'wp-admin/includes/plugin.php');
		return is_plugin_active_for_network ( 'wp-piwik/wp-piwik.php' );
	}
lechuck's avatar
lechuck committed
275

ale's avatar
ale committed
276
277
278
279
280
281
282
	/**
	 * Apply new configuration
	 *
	 * @param array $in
	 *        	new configuration set
	 */
	public function applyChanges($in) {
lucha's avatar
lucha committed
283
284
		if (!self::$wpPiwik->isValidOptionsPost())
			die("Invalid config changes.");
ale's avatar
ale committed
285
286
287
288
289
290
291
292
293
		$in = $this->checkSettings ( $in );
		self::$wpPiwik->log ( 'Apply changed settings:' );
		foreach ( self::$defaultSettings ['globalSettings'] as $key => $val )
			$this->setGlobalOption ( $key, isset ( $in [$key] ) ? $in [$key] : $val );
		foreach ( self::$defaultSettings ['settings'] as $key => $val )
			$this->setOption ( $key, isset ( $in [$key] ) ? $in [$key] : $val );
		$this->setGlobalOption ( 'last_settings_update', time () );
		$this->save ();
	}
lechuck's avatar
lechuck committed
294

ale's avatar
ale committed
295
296
297
298
299
300
301
302
303
304
305
306
	/**
	 * Apply callback function on new settings
	 *
	 * @param array $in
	 *        	new configuration set
	 * @return array configuration set after callback functions were applied
	 */
	private function checkSettings($in) {
		foreach ( $this->checkSettings as $key => $value )
			if (isset ( $in [$key] ))
				$in [$key] = call_user_func_array ( array (
						$this,
lechuck's avatar
lechuck committed
307
						$value
ale's avatar
ale committed
308
309
				), array (
						$in [$key],
lechuck's avatar
lechuck committed
310
						$in
ale's avatar
ale committed
311
312
313
				) );
		return $in;
	}
lechuck's avatar
lechuck committed
314

ale's avatar
ale committed
315
316
317
318
319
320
321
322
323
324
325
326
	/**
	 * Add slash to Piwik URL if necessary
	 *
	 * @param string $value
	 *        	Piwik URL
	 * @param array $in
	 *        	configuration set
	 * @return string Piwik URL
	 */
	private function checkPiwikUrl($value, $in) {
		return substr ( $value, - 1, 1 ) != '/' ? $value . '/' : $value;
	}
lechuck's avatar
lechuck committed
327

ale's avatar
ale committed
328
329
330
331
332
333
334
335
336
337
338
339
	/**
	 * Remove &amp;token_auth= from auth token
	 *
	 * @param string $value
	 *        	Piwik auth token
	 * @param array $in
	 *        	configuration set
	 * @return string Piwik auth token
	 */
	private function checkPiwikToken($value, $in) {
		return str_replace ( '&token_auth=', '', $value );
	}
lechuck's avatar
lechuck committed
340

ale's avatar
ale committed
341
342
343
344
345
346
347
348
349
350
351
352
353
354
	/**
	 * Request the site ID (if not set before)
	 *
	 * @param string $value
	 *        	tracking code
	 * @param array $in
	 *        	configuration set
	 * @return int Piwik site ID
	 */
	private function requestPiwikSiteID($value, $in) {
		if ($in ['auto_site_config'] && ! $value)
			return self::$wpPiwik->getPiwikSiteId();
		return $value;
	}
lechuck's avatar
lechuck committed
355

ale's avatar
ale committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
	/**
	 * Prepare the tracking code
	 *
	 * @param string $value
	 *        	tracking code
	 * @param array $in
	 *        	configuration set
	 * @return string tracking code
	 */
	private function prepareTrackingCode($value, $in) {
		if ($in ['track_mode'] == 'manually' || $in ['track_mode'] == 'disabled') {
			$value = stripslashes ( $value );
			if ($this->checkNetworkActivation ())
				add_site_option ( 'wp-piwik-manually', $value );
			return $value;
		}
		/*$result = self::$wpPiwik->updateTrackingCode ();
		echo '<pre>'; print_r($result); echo '</pre>';
		$this->setOption ( 'noscript_code', $result ['noscript'] );*/
		return; // $result ['script'];
	}
lechuck's avatar
lechuck committed
377

ale's avatar
ale committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
	/**
	 * Prepare the nocscript code
	 *
	 * @param string $value
	 *        	noscript code
	 * @param array $in
	 *        	configuration set
	 * @return string noscript code
	 */
	private function prepareNocscriptCode($value, $in) {
		if ($in ['track_mode'] == 'manually')
			return stripslashes ( $value );
		return $this->getOption ( 'noscript_code' );
	}
lechuck's avatar
lechuck committed
392

ale's avatar
ale committed
393
394
395
396
397
398
399
400
401
402
403
404
405
	/**
	 * Get debug data
	 *
	 * @return array WP-Piwik settings for debug output
	 */
	public function getDebugData() {
		$debug = array(
			'global_settings' => $this->globalSettings,
			'settings' => $this->settings
		);
		$debug['global_settings']['piwik_token'] = !empty($debug['global_settings']['piwik_token'])?'set':'not set';
		return $debug;
	}
lechuck's avatar
lechuck committed
406
}