Completed
Push — add/react-videopress-settings ( 1a2dcb...a2519f )
by
unknown
275:56 queued 263:52
created

Jetpack_Sync_Settings::update_settings()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 1
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
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
		'enqueue_wait_time'       => true,
15
		'max_queue_size'          => true,
16
		'max_queue_lag'           => true,
17
		'queue_max_writes_sec'    => true,
18
		'post_types_blacklist'    => true,
19
		'disable'                 => true,
20
		'render_filtered_content' => true,
21
		'post_meta_whitelist'     => true,
22
		'comment_meta_whitelist'  => true,
23
		'max_enqueue_full_sync'   => true,
24
		'max_queue_size_full_sync'=> true,
25
	);
26
27
	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...
28
	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...
29
	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...
30
	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...
31
32
	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...
33
34
	static function get_settings() {
35
		$settings = array();
36
		foreach ( array_keys( self::$valid_settings ) as $setting ) {
37
			$settings[ $setting ] = self::get_setting( $setting );
38
		}
39
40
		return $settings;
41
	}
42
43
	// Fetches the setting. It saves it if the setting doesn't exist, so that it gets
44
	// autoloaded on page load rather than re-queried every time.
45
	static function get_setting( $setting ) {
46
		if ( ! isset( self::$valid_settings[ $setting ] ) ) {
47
			return false;
48
		}
49
50
		if ( isset( self::$settings_cache[ $setting ] ) ) {
51
			return self::$settings_cache[ $setting ];
52
		}
53
54
		$value = get_option( self::SETTINGS_OPTION_PREFIX . $setting );
55
56
		if ( false === $value ) {
57
			$default_name = "default_$setting"; // e.g. default_dequeue_max_bytes
58
			$value        = Jetpack_Sync_Defaults::$$default_name;
59
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
60
		}
61
62
		if ( is_numeric( $value ) ) {
63
			$value = intval( $value );
64
		}
65
		$default_array_value = null;
66
		switch( $setting ) {
67
			case 'post_types_blacklist':
68
				$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...
69
				break;
70
			case 'post_meta_whitelist':
71
				$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...
72
				break;
73
			case 'comment_meta_whitelist':
74
				$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...
75
				break;
76
		}
77
78
		if ( $default_array_value ) {
79
			if ( is_array( $value ) ) {
80
				$value = array_unique( array_merge( $value, $default_array_value ) );
81
			} else {
82
				$value = $default_array_value;
83
			}
84
		}
85
86
		self::$settings_cache[ $setting ] = $value;
87
88
		return $value;
89
	}
90
91
	static function update_settings( $new_settings ) {
92
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
93
		foreach ( $validated_settings as $setting => $value ) {
94
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
95
			unset( self::$settings_cache[ $setting ] );
96
97
			// if we set the disabled option to true, clear the queues
98
			if ( 'disable' === $setting && !! $value ) {
99
				require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
100
				$listener = Jetpack_Sync_Listener::get_instance();
101
				$listener->get_sync_queue()->reset();
102
				$listener->get_full_sync_queue()->reset();
103
			}
104
		}
105
	}
106
107
	// returns escapted SQL that can be injected into a WHERE clause
108
	static function get_blacklisted_post_types_sql() {
109
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
110
	}
111
112
	static function get_whitelisted_post_meta_sql() {
113
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ) ) . '\')';
114
	}
115
116
	static function get_whitelisted_comment_meta_sql() {
117
		return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ) ) . '\')';
118
	}
119
120
	static function get_comments_filter_sql() {
121
		return "comment_approved <> 'spam'";
122
	}
123
124
	static function reset_data() {
125
		$valid_settings       = self::$valid_settings;
126
		self::$settings_cache = array();
127
		foreach ( $valid_settings as $option => $value ) {
128
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
129
		}
130
		self::set_importing( null );
131
		self::set_doing_cron( null );
132
		self::set_is_syncing( null );
133
		self::set_is_sending( null );
134
	}
135
136
	static function set_importing( $is_importing ) {
137
		// set to NULL to revert to WP_IMPORTING, the standard behaviour
138
		self::$is_importing = $is_importing;
139
	}
140
141
	static function is_importing() {
142
		if ( ! is_null( self::$is_importing ) ) {
143
			return self::$is_importing;
144
		}
145
146
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
147
	}
148
149
	static function set_doing_cron( $is_doing_cron ) {
150
		// set to NULL to revert to WP_IMPORTING, the standard behaviour
151
		self::$is_doing_cron = $is_doing_cron;
152
	}
153
154
	static function is_doing_cron() {
155
		if ( ! is_null( self::$is_doing_cron ) ) {
156
			return self::$is_doing_cron;
157
		}
158
159
		return defined( 'DOING_CRON' ) && DOING_CRON;
160
	}
161
162
	static function is_syncing() {
163
		return (bool) self::$is_syncing;
164
	}
165
166
	static function set_is_syncing( $is_syncing ) {
167
		self::$is_syncing = $is_syncing;
168
	}
169
170
	static function is_sending() {
171
		return (bool) self::$is_sending;
172
	}
173
174
	static function set_is_sending( $is_sending ) {
175
		self::$is_sending = $is_sending;
176
	}
177
}
178