Automattic /
jetpack
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | use Automattic\Jetpack\Sync\Listener; |
||
| 4 | use Automattic\Jetpack\Sync\Defaults; |
||
| 5 | |||
| 6 | class Jetpack_Sync_Settings { |
||
| 7 | const SETTINGS_OPTION_PREFIX = 'jetpack_sync_settings_'; |
||
| 8 | |||
| 9 | static $valid_settings = array( |
||
| 10 | 'dequeue_max_bytes' => true, |
||
| 11 | 'upload_max_bytes' => true, |
||
| 12 | 'upload_max_rows' => true, |
||
| 13 | 'sync_wait_time' => true, |
||
| 14 | 'sync_wait_threshold' => true, |
||
| 15 | 'enqueue_wait_time' => true, |
||
| 16 | 'max_queue_size' => true, |
||
| 17 | 'max_queue_lag' => true, |
||
| 18 | 'queue_max_writes_sec' => true, |
||
| 19 | 'post_types_blacklist' => true, |
||
| 20 | 'disable' => true, |
||
| 21 | 'network_disable' => true, |
||
| 22 | 'render_filtered_content' => true, |
||
| 23 | 'post_meta_whitelist' => true, |
||
| 24 | 'comment_meta_whitelist' => true, |
||
| 25 | 'max_enqueue_full_sync' => true, |
||
| 26 | 'max_queue_size_full_sync' => true, |
||
| 27 | 'sync_via_cron' => true, |
||
| 28 | 'cron_sync_time_limit' => true, |
||
| 29 | 'known_importers' => true, |
||
| 30 | ); |
||
| 31 | |||
| 32 | static $is_importing; |
||
| 33 | static $is_doing_cron; |
||
| 34 | static $is_syncing; |
||
| 35 | static $is_sending; |
||
| 36 | |||
| 37 | static $settings_cache = array(); // some settings can be expensive to compute - let's cache them |
||
| 38 | |||
| 39 | static function get_settings() { |
||
| 40 | $settings = array(); |
||
| 41 | foreach ( array_keys( self::$valid_settings ) as $setting ) { |
||
| 42 | $settings[ $setting ] = self::get_setting( $setting ); |
||
| 43 | } |
||
| 44 | |||
| 45 | return $settings; |
||
| 46 | } |
||
| 47 | |||
| 48 | // Fetches the setting. It saves it if the setting doesn't exist, so that it gets |
||
| 49 | // autoloaded on page load rather than re-queried every time. |
||
| 50 | static function get_setting( $setting ) { |
||
| 51 | if ( ! isset( self::$valid_settings[ $setting ] ) ) { |
||
| 52 | return false; |
||
| 53 | } |
||
| 54 | |||
| 55 | if ( isset( self::$settings_cache[ $setting ] ) ) { |
||
| 56 | return self::$settings_cache[ $setting ]; |
||
| 57 | } |
||
| 58 | |||
| 59 | if ( self::is_network_setting( $setting ) ) { |
||
| 60 | if ( is_multisite() ) { |
||
| 61 | $value = get_site_option( self::SETTINGS_OPTION_PREFIX . $setting ); |
||
| 62 | } else { |
||
| 63 | // On single sites just return the default setting |
||
| 64 | $value = Defaults::get_default_setting( $setting ); |
||
| 65 | self::$settings_cache[ $setting ] = $value; |
||
| 66 | return $value; |
||
| 67 | } |
||
| 68 | } else { |
||
| 69 | $value = get_option( self::SETTINGS_OPTION_PREFIX . $setting ); |
||
| 70 | } |
||
| 71 | |||
| 72 | if ( false === $value ) { // no default value is set. |
||
| 73 | $value = Defaults::get_default_setting( $setting ); |
||
| 74 | if ( self::is_network_setting( $setting ) ) { |
||
| 75 | update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value ); |
||
| 76 | } else { |
||
| 77 | // We set one so that it gets autoloaded |
||
| 78 | update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true ); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | if ( is_numeric( $value ) ) { |
||
| 83 | $value = intval( $value ); |
||
| 84 | } |
||
| 85 | $default_array_value = null; |
||
| 86 | switch ( $setting ) { |
||
| 87 | case 'post_types_blacklist': |
||
| 88 | $default_array_value = Defaults::$blacklisted_post_types; |
||
|
0 ignored issues
–
show
|
|||
| 89 | break; |
||
| 90 | case 'post_meta_whitelist': |
||
| 91 | $default_array_value = Defaults::get_post_meta_whitelist(); |
||
| 92 | break; |
||
| 93 | case 'comment_meta_whitelist': |
||
| 94 | $default_array_value = Defaults::get_comment_meta_whitelist(); |
||
| 95 | break; |
||
| 96 | case 'known_importers': |
||
| 97 | $default_array_value = Defaults::get_known_importers(); |
||
| 98 | break; |
||
| 99 | } |
||
| 100 | |||
| 101 | if ( $default_array_value ) { |
||
| 102 | if ( is_array( $value ) ) { |
||
| 103 | $value = array_unique( array_merge( $value, $default_array_value ) ); |
||
| 104 | } else { |
||
| 105 | $value = $default_array_value; |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | self::$settings_cache[ $setting ] = $value; |
||
| 110 | |||
| 111 | return $value; |
||
| 112 | } |
||
| 113 | |||
| 114 | static function update_settings( $new_settings ) { |
||
| 115 | $validated_settings = array_intersect_key( $new_settings, self::$valid_settings ); |
||
| 116 | foreach ( $validated_settings as $setting => $value ) { |
||
| 117 | |||
| 118 | if ( self::is_network_setting( $setting ) ) { |
||
| 119 | if ( is_multisite() && is_main_site() ) { |
||
| 120 | update_site_option( self::SETTINGS_OPTION_PREFIX . $setting, $value ); |
||
| 121 | } |
||
| 122 | } else { |
||
| 123 | update_option( self::SETTINGS_OPTION_PREFIX . $setting, $value, true ); |
||
| 124 | } |
||
| 125 | |||
| 126 | unset( self::$settings_cache[ $setting ] ); |
||
| 127 | |||
| 128 | // if we set the disabled option to true, clear the queues |
||
| 129 | if ( ( 'disable' === $setting || 'network_disable' === $setting ) && ! ! $value ) { |
||
| 130 | $listener = Listener::get_instance(); |
||
| 131 | $listener->get_sync_queue()->reset(); |
||
| 132 | $listener->get_full_sync_queue()->reset(); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | static function is_network_setting( $setting ) { |
||
| 138 | return strpos( $setting, 'network_' ) === 0; |
||
| 139 | } |
||
| 140 | |||
| 141 | // returns escapted SQL that can be injected into a WHERE clause |
||
| 142 | static function get_blacklisted_post_types_sql() { |
||
| 143 | return 'post_type NOT IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_types_blacklist' ) ) ) . '\')'; |
||
| 144 | } |
||
| 145 | |||
| 146 | static function get_whitelisted_post_meta_sql() { |
||
| 147 | return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'post_meta_whitelist' ) ) ) . '\')'; |
||
| 148 | } |
||
| 149 | |||
| 150 | static function get_whitelisted_comment_meta_sql() { |
||
| 151 | return 'meta_key IN (\'' . join( '\', \'', array_map( 'esc_sql', self::get_setting( 'comment_meta_whitelist' ) ) ) . '\')'; |
||
| 152 | } |
||
| 153 | |||
| 154 | static function get_comments_filter_sql() { |
||
| 155 | return "comment_approved <> 'spam'"; |
||
| 156 | } |
||
| 157 | |||
| 158 | static function reset_data() { |
||
| 159 | $valid_settings = self::$valid_settings; |
||
| 160 | self::$settings_cache = array(); |
||
| 161 | foreach ( $valid_settings as $option => $value ) { |
||
| 162 | delete_option( self::SETTINGS_OPTION_PREFIX . $option ); |
||
| 163 | } |
||
| 164 | self::set_importing( null ); |
||
| 165 | self::set_doing_cron( null ); |
||
| 166 | self::set_is_syncing( null ); |
||
| 167 | self::set_is_sending( null ); |
||
| 168 | } |
||
| 169 | |||
| 170 | static function set_importing( $is_importing ) { |
||
| 171 | // set to NULL to revert to WP_IMPORTING, the standard behavior |
||
| 172 | self::$is_importing = $is_importing; |
||
| 173 | } |
||
| 174 | |||
| 175 | static function is_importing() { |
||
| 176 | if ( ! is_null( self::$is_importing ) ) { |
||
| 177 | return self::$is_importing; |
||
| 178 | } |
||
| 179 | |||
| 180 | return defined( 'WP_IMPORTING' ) && WP_IMPORTING; |
||
| 181 | } |
||
| 182 | |||
| 183 | static function is_sync_enabled() { |
||
| 184 | return ! ( self::get_setting( 'disable' ) || self::get_setting( 'network_disable' ) ); |
||
| 185 | } |
||
| 186 | |||
| 187 | static function set_doing_cron( $is_doing_cron ) { |
||
| 188 | // set to NULL to revert to WP_IMPORTING, the standard behavior |
||
| 189 | self::$is_doing_cron = $is_doing_cron; |
||
| 190 | } |
||
| 191 | |||
| 192 | static function is_doing_cron() { |
||
| 193 | if ( ! is_null( self::$is_doing_cron ) ) { |
||
| 194 | return self::$is_doing_cron; |
||
| 195 | } |
||
| 196 | |||
| 197 | return defined( 'DOING_CRON' ) && DOING_CRON; |
||
| 198 | } |
||
| 199 | |||
| 200 | static function is_syncing() { |
||
| 201 | return (bool) self::$is_syncing || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ); |
||
| 202 | } |
||
| 203 | |||
| 204 | static function set_is_syncing( $is_syncing ) { |
||
| 205 | self::$is_syncing = $is_syncing; |
||
| 206 | } |
||
| 207 | |||
| 208 | static function is_sending() { |
||
| 209 | return (bool) self::$is_sending; |
||
| 210 | } |
||
| 211 | |||
| 212 | static function set_is_sending( $is_sending ) { |
||
| 213 | self::$is_sending = $is_sending; |
||
| 214 | } |
||
| 215 | } |
||
| 216 |
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.