Completed
Push — add/whitelist-post_meta_and_co... ( 684fcf...dca051 )
by
unknown
09:36
created

Jetpack_Sync_Settings   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 158
rs 9.8
c 0
b 0
f 0
wmc 31
lcom 1
cbo 2

13 Methods

Rating   Name   Duplication   Size   Complexity  
A get_settings() 0 8 2
D get_setting() 0 45 10
A update_settings() 0 15 4
A get_blacklisted_post_types_sql() 0 3 1
A reset_data() 0 11 2
A set_importing() 0 4 1
A is_importing() 0 7 3
A set_doing_cron() 0 4 1
A is_doing_cron() 0 7 3
A is_syncing() 0 3 1
A set_is_syncing() 0 3 1
A is_sending() 0 3 1
A set_is_sending() 0 3 1
1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-defaults.php';
4
5
class Jetpack_Sync_Settings {
6
	const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_';
7
8
	static $valid_settings = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $valid_settings.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
9
		'dequeue_max_bytes'    => true,
10
		'upload_max_bytes'     => true,
11
		'upload_max_rows'      => true,
12
		'sync_wait_time'       => true,
13
		'sync_wait_threshold'  => true,
14
		'max_queue_size'       => true,
15
		'max_queue_lag'        => true,
16
		'queue_max_writes_sec' => true,
17
		'post_types_blacklist' => true,
18
		'disable'              => true,
19
		'render_filtered_content' => true,
20
		'post_meta_whitelist' => true,
21
		'comment_meta_whitelist' => true,
22
	);
23
24
	static $is_importing;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_importing.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
25
	static $is_doing_cron;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_doing_cron.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
26
	static $is_syncing;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_syncing.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
27
	static $is_sending;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_sending.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
28
29
	static $settings_cache = array(); // some settings can be expensive to compute - let's cache them
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $settings_cache.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
30
31
	static function get_settings() {
32
		$settings = array();
33
		foreach ( array_keys( self::$valid_settings ) as $setting ) {
34
			$settings[ $setting ] = self::get_setting( $setting );
35
		}
36
37
		return $settings;
38
	}
39
40
	// Fetches the setting. It saves it if the setting doesn't exist, so that it gets
41
	// autoloaded on page load rather than re-queried every time.
42
	static function get_setting( $setting ) {
43
		if ( ! isset( self::$valid_settings[ $setting ] ) ) {
44
			return false;
45
		}
46
47
		if ( isset( self::$settings_cache[ $setting ] ) ) {
48
			return self::$settings_cache[ $setting ];
49
		}
50
51
		$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting );
52
53
		if ( false === $value ) {
54
			$default_name = "default_$setting"; // e.g. default_dequeue_max_bytes
55
			$value        = Jetpack_Sync_Defaults::$$default_name;
56
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
57
		}
58
59
		if ( is_numeric( $value ) ) {
60
			$value = intval( $value );
61
		}
62
		$default_array_value = null;
63
		switch( $setting ) {
64
			case 'post_types_blacklist':
65
				$default_array_value = Jetpack_Sync_Defaults::$blacklisted_post_types;
0 ignored issues
show
Bug introduced by
The property blacklisted_post_types cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
66
				break;
67
			case 'post_meta_whitelist':
68
				$default_array_value = Jetpack_Sync_Defaults::$post_meta_whitelist;
0 ignored issues
show
Bug introduced by
The property post_meta_whitelist cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
69
				break;
70
			case 'comment_meta_whitelist':
71
				$default_array_value = Jetpack_Sync_Defaults::$comment_meta_whitelist;
0 ignored issues
show
Bug introduced by
The property comment_meta_whitelist cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
72
				break;
73
		}
74
75
		if ( $default_array_value ) {
76
			if ( is_array( $value ) ) {
77
				$value = array_unique( array_merge( $value, $default_array_value ) );
78
			} else {
79
				$value = $default_array_value;
80
			}
81
		}
82
83
		self::$settings_cache[ $setting ] = $value;
84
85
		return $value;
86
	}
87
88
	static function update_settings( $new_settings ) {
89
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
90
		foreach ( $validated_settings as $setting => $value ) {
91
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
92
			unset( self::$settings_cache[ $setting ] );
93
94
			// if we set the disabled option to true, clear the queues
95
			if ( 'disable' === $setting && !! $value ) {
96
				require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
97
				$listener = Jetpack_Sync_Listener::get_instance();
98
				$listener->get_sync_queue()->reset();
99
				$listener->get_full_sync_queue()->reset();
100
			}
101
		}
102
	}
103
104
	// returns escapted SQL that can be injected into a WHERE clause
105
	static function get_blacklisted_post_types_sql() {
106
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
107
	}
108
109
	static function reset_data() {
110
		$valid_settings       = self::$valid_settings;
111
		self::$settings_cache = array();
112
		foreach ( $valid_settings as $option => $value ) {
113
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
114
		}
115
		self::set_importing( null );
116
		self::set_doing_cron( null );
117
		self::set_is_syncing( null );
118
		self::set_is_sending( null );
119
	}
120
121
	static function set_importing( $is_importing ) {
122
		// set to NULL to revert to WP_IMPORTING, the standard behaviour
123
		self::$is_importing = $is_importing;
124
	}
125
126
	static function is_importing() {
127
		if ( ! is_null( self::$is_importing ) ) {
128
			return self::$is_importing;
129
		}
130
131
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
132
	}
133
134
	static function set_doing_cron( $is_doing_cron ) {
135
		// set to NULL to revert to WP_IMPORTING, the standard behaviour
136
		self::$is_doing_cron = $is_doing_cron;
137
	}
138
139
	static function is_doing_cron() {
140
		if ( ! is_null( self::$is_doing_cron ) ) {
141
			return self::$is_doing_cron;
142
		}
143
144
		return defined( 'DOING_CRON' ) && DOING_CRON;
145
	}
146
147
	static function is_syncing() {
148
		return (bool) self::$is_syncing;
149
	}
150
151
	static function set_is_syncing( $is_syncing ) {
152
		self::$is_syncing = $is_syncing;
153
	}
154
155
	static function is_sending() {
156
		return (bool) self::$is_sending;
157
	}
158
159
	static function set_is_sending( $is_sending ) {
160
		self::$is_sending = $is_sending;
161
	}
162
}
163