Completed
Push — add/whitelist-post_meta_and_co... ( 684fcf )
by
unknown
11:18
created

Jetpack_Sync_Settings::get_setting()   C

Complexity

Conditions 8
Paths 18

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 24
nc 18
nop 1
dl 0
loc 37
rs 5.3846
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
		'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
63
		switch( $setting ) {
64
			case 'post_types_blacklist':
65
				$value = array_unique( array_merge( $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
				$value = array_unique( array_merge( $value, Jetpack_Sync_Defaults::$default_post_meta_whitelist ) );
0 ignored issues
show
Bug introduced by
The property default_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
				$value = array_unique( array_merge( $value, Jetpack_Sync_Defaults::$default_comment_meta_whitelist ) );
0 ignored issues
show
Bug introduced by
The property default_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
		self::$settings_cache[ $setting ] = $value;
76
77
		return $value;
78
	}
79
80
	static function update_settings( $new_settings ) {
81
		$validated_settings = array_intersect_key( $new_settings, self::$valid_settings );
82
		foreach ( $validated_settings as $setting => $value ) {
83
			update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true );
84
			unset( self::$settings_cache[ $setting ] );
85
86
			// if we set the disabled option to true, clear the queues
87
			if ( 'disable' === $setting && !! $value ) {
88
				require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php';
89
				$listener = Jetpack_Sync_Listener::get_instance();
90
				$listener->get_sync_queue()->reset();
91
				$listener->get_full_sync_queue()->reset();
92
			}
93
		}
94
	}
95
96
	// returns escapted SQL that can be injected into a WHERE clause
97
	static function get_blacklisted_post_types_sql() {
98
		return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')';
99
	}
100
101
	static function reset_data() {
102
		$valid_settings       = self::$valid_settings;
103
		self::$settings_cache = array();
104
		foreach ( $valid_settings as $option => $value ) {
105
			delete_option( self::SETTINGS_OPTION_PREFIX . $option );
106
		}
107
		self::set_importing( null );
108
		self::set_doing_cron( null );
109
		self::set_is_syncing( null );
110
		self::set_is_sending( null );
111
	}
112
113
	static function set_importing( $is_importing ) {
114
		// set to NULL to revert to WP_IMPORTING, the standard behaviour
115
		self::$is_importing = $is_importing;
116
	}
117
118
	static function is_importing() {
119
		if ( ! is_null( self::$is_importing ) ) {
120
			return self::$is_importing;
121
		}
122
123
		return defined( 'WP_IMPORTING' ) && WP_IMPORTING;
124
	}
125
126
	static function set_doing_cron( $is_doing_cron ) {
127
		// set to NULL to revert to WP_IMPORTING, the standard behaviour
128
		self::$is_doing_cron = $is_doing_cron;
129
	}
130
131
	static function is_doing_cron() {
132
		if ( ! is_null( self::$is_doing_cron ) ) {
133
			return self::$is_doing_cron;
134
		}
135
136
		return defined( 'DOING_CRON' ) && DOING_CRON;
137
	}
138
139
	static function is_syncing() {
140
		return (bool) self::$is_syncing;
141
	}
142
143
	static function set_is_syncing( $is_syncing ) {
144
		self::$is_syncing = $is_syncing;
145
	}
146
147
	static function is_sending() {
148
		return (bool) self::$is_sending;
149
	}
150
151
	static function set_is_sending( $is_sending ) {
152
		self::$is_sending = $is_sending;
153
	}
154
}
155