1 | <?php |
||
5 | class Jetpack_Sync_Settings { |
||
6 | const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_'; |
||
7 | |||
8 | static $valid_settings = array( |
||
|
|||
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 | ); |
||
21 | |||
22 | static $is_importing; |
||
23 | static $is_doing_cron; |
||
24 | static $is_syncing; |
||
25 | static $is_sending; |
||
26 | |||
27 | static $settings_cache = array(); // some settings can be expensive to compute - let's cache them |
||
28 | |||
29 | static function get_settings() { |
||
30 | $settings = array(); |
||
31 | foreach ( array_keys( self::$valid_settings ) as $setting ) { |
||
32 | $settings[ $setting ] = self::get_setting( $setting ); |
||
33 | } |
||
34 | |||
35 | return $settings; |
||
36 | } |
||
37 | |||
38 | // Fetches the setting. It saves it if the setting doesn't exist, so that it gets |
||
39 | // autoloaded on page load rather than re-queried every time. |
||
40 | static function get_setting( $setting ) { |
||
41 | if ( ! isset( self::$valid_settings[ $setting ] ) ) { |
||
42 | return false; |
||
43 | } |
||
44 | |||
45 | if ( isset( self::$settings_cache[ $setting ] ) ) { |
||
46 | return self::$settings_cache[ $setting ]; |
||
47 | } |
||
48 | |||
49 | $value = get_option( self::SETTINGS_OPTION_PREFIX . $setting ); |
||
50 | |||
51 | if ( false === $value ) { |
||
52 | $default_name = "default_$setting"; // e.g. default_dequeue_max_bytes |
||
53 | $value = Jetpack_Sync_Defaults::$$default_name; |
||
54 | update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true ); |
||
55 | } |
||
56 | |||
57 | if ( is_numeric( $value ) ) { |
||
58 | $value = intval( $value ); |
||
59 | } |
||
60 | |||
61 | // specifically for the post_types blacklist, we want to include the hardcoded settings |
||
62 | if ( $setting === 'post_types_blacklist' ) { |
||
63 | $value = array_unique( array_merge( $value, Jetpack_Sync_Defaults::$blacklisted_post_types ) ); |
||
64 | } |
||
65 | |||
66 | self::$settings_cache[ $setting ] = $value; |
||
67 | |||
68 | return $value; |
||
69 | } |
||
70 | |||
71 | static function update_settings( $new_settings ) { |
||
72 | $validated_settings = array_intersect_key( $new_settings, self::$valid_settings ); |
||
73 | foreach ( $validated_settings as $setting => $value ) { |
||
74 | update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true ); |
||
75 | unset( self::$settings_cache[ $setting ] ); |
||
76 | |||
77 | // if we set the disabled option to true, clear the queues |
||
78 | if ( 'disable' === $setting && !! $value ) { |
||
79 | require_once dirname( __FILE__ ) . '/class.jetpack-sync-listener.php'; |
||
80 | $listener = Jetpack_Sync_Listener::get_instance(); |
||
81 | $listener->get_sync_queue()->reset(); |
||
82 | $listener->get_full_sync_queue()->reset(); |
||
83 | } |
||
84 | } |
||
85 | } |
||
86 | |||
87 | // returns escapted SQL that can be injected into a WHERE clause |
||
88 | static function get_blacklisted_post_types_sql() { |
||
89 | return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')'; |
||
90 | } |
||
91 | |||
92 | static function reset_data() { |
||
93 | $valid_settings = self::$valid_settings; |
||
94 | self::$settings_cache = array(); |
||
95 | foreach ( $valid_settings as $option => $value ) { |
||
96 | delete_option( self::SETTINGS_OPTION_PREFIX . $option ); |
||
97 | } |
||
98 | self::set_importing( null ); |
||
99 | self::set_doing_cron( null ); |
||
100 | self::set_is_syncing( null ); |
||
101 | self::set_is_sending( null ); |
||
102 | } |
||
103 | |||
104 | static function set_importing( $is_importing ) { |
||
108 | |||
109 | static function is_importing() { |
||
116 | |||
117 | static function set_doing_cron( $is_doing_cron ) { |
||
118 | // set to NULL to revert to WP_IMPORTING, the standard behaviour |
||
119 | self::$is_doing_cron = $is_doing_cron; |
||
120 | } |
||
121 | |||
122 | static function is_doing_cron() { |
||
129 | |||
130 | static function is_syncing() { |
||
133 | |||
134 | static function set_is_syncing( $is_syncing ) { |
||
137 | |||
138 | static function is_sending() { |
||
141 | |||
142 | static function set_is_sending( $is_sending ) { |
||
145 | } |
||
146 |
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.