class.jetpack-options.php
| 1 | <?php |
| 2 | |
| 3 | class Jetpack_Options { |
| 4 | |
| 5 | /** |
| 6 | * An array that maps a grouped option type to an option name. |
| 7 | * @var array |
| 8 | */ |
| 9 | private static $grouped_options = array( |
| 10 | 'compact' => 'jetpack_options', |
| 11 | 'private' => 'jetpack_private_options' |
| 12 | ); |
| 13 | |
| 14 | /** |
| 15 | * Returns an array of option names for a given type. |
| 16 | * |
| 17 | * @param string $type The type of option to return. Defaults to 'compact'. |
| 18 | * |
| 19 | * @return array |
| 20 | */ |
| 21 | public static function get_option_names( $type = 'compact' ) { |
| 22 | switch ( $type ) { |
| 23 | case 'non-compact' : |
| 24 | case 'non_compact' : |
| 25 | return array( |
| 26 | 'activated', |
| 27 | 'active_modules', |
| 28 | 'available_modules', |
| 29 | 'do_activate', |
| 30 | 'log', |
| 31 | 'slideshow_background_color', |
| 32 | 'widget_twitter', |
| 33 | 'wpcc_options', |
| 34 | 'relatedposts', |
| 35 | 'file_data', |
| 36 | 'autoupdate_plugins', // (array) An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated |
| 37 | 'autoupdate_plugins_translations', // (array) An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated translation files. |
| 38 | 'autoupdate_themes', // (array) An array of theme ids ( eg. twentyfourteen ) that should be autoupdated |
| 39 | 'autoupdate_themes_translations', // (array) An array of theme ids ( eg. twentyfourteen ) that should autoupdated translation files. |
| 40 | 'autoupdate_core', // (bool) Whether or not to autoupdate core |
| 41 | 'autoupdate_translations', // (bool) Whether or not to autoupdate all translations |
| 42 | 'json_api_full_management', // (bool) Allow full management (eg. Activate, Upgrade plugins) of the site via the JSON API. |
| 43 | 'sync_non_public_post_stati', // (bool) Allow synchronisation of posts and pages with non-public status. |
| 44 | 'site_icon_url', // (string) url to the full site icon |
| 45 | 'site_icon_id', // (int) Attachment id of the site icon file |
| 46 | 'dismissed_manage_banner', // (bool) Dismiss Jetpack manage banner allows the user to dismiss the banner permanently |
| 47 | 'restapi_stats_cache', // (array) Stats Cache data. |
| 48 | 'unique_connection', // (array) A flag to determine a unique connection to wordpress.com two values "connected" and "disconnected" with values for how many times each has occured |
| 49 | 'protect_whitelist', // (array) IP Address for the Protect module to ignore |
| 50 | 'sync_error_idc', // (bool|array) false or array containing the site's home and siteurl at time of IDC error |
| 51 | 'safe_mode_confirmed', // (bool) True if someone confirms that this site was correctly put into safe mode automatically after an identity crisis is discovered. |
| 52 | 'migrate_for_idc', // (bool) True if someone confirms that this site should migrate stats and subscribers from its previous URL |
| 53 | ); |
| 54 | |
| 55 | case 'private' : |
| 56 | return array( |
| 57 | 'blog_token', // (string) The Client Secret/Blog Token of this site. |
| 58 | 'user_token', // (string) The User Token of this site. (deprecated) |
| 59 | 'user_tokens' // (array) User Tokens for each user of this site who has connected to jetpack.wordpress.com. |
| 60 | ); |
| 61 | |
| 62 | case 'network' : |
| 63 | return array( |
| 64 | 'file_data' // (array) List of absolute paths to all Jetpack modules |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | return array( |
| 69 | 'id', // (int) The Client ID/WP.com Blog ID of this site. |
| 70 | 'publicize_connections', // (array) An array of Publicize connections from WordPress.com |
| 71 | 'master_user', // (int) The local User ID of the user who connected this site to jetpack.wordpress.com. |
| 72 | 'version', // (string) Used during upgrade procedure to auto-activate new modules. version:time |
| 73 | 'old_version', // (string) Used to determine which modules are the most recently added. previous_version:time |
| 74 | 'fallback_no_verify_ssl_certs', // (int) Flag for determining if this host must skip SSL Certificate verification due to misconfigured SSL. |
| 75 | 'time_diff', // (int) Offset between Jetpack server's clocks and this server's clocks. Jetpack Server Time = time() + (int) Jetpack_Options::get_option( 'time_diff' ) |
| 76 | 'public', // (int|bool) If we think this site is public or not (1, 0), false if we haven't yet tried to figure it out. |
| 77 | 'videopress', // (array) VideoPress options array. |
| 78 | 'is_network_site', // (int|bool) If we think this site is a network or a single blog (1, 0), false if we haven't yet tried to figue it out. |
| 79 | 'social_links', // (array) The specified links for each social networking site. |
| 80 | 'identity_crisis_whitelist', // (array) An array of options, each having an array of the values whitelisted for it. |
| 81 | 'gplus_authors', // (array) The Google+ authorship information for connected users. |
| 82 | 'last_heartbeat', // (int) The timestamp of the last heartbeat that fired. |
| 83 | 'jumpstart', // (string) A flag for whether or not to show the Jump Start. Accepts: new_connection, jumpstart_activated, jetpack_action_taken, jumpstart_dismissed. |
| 84 | 'hide_jitm', // (array) A list of just in time messages that we should not show because they have been dismissed by the user |
| 85 | 'custom_css_4.7_migration', // (bool) Whether Custom CSS has scanned for and migrated any legacy CSS CPT entries to the new Core format. |
| 86 | 'image_widget_migration', // (bool) Whether any legacy Image Widgets have been converted to the new Core widget |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Is the option name valid? |
| 92 | * |
| 93 | * @param string $name The name of the option |
| 94 | * @param string|null $group The name of the group that the option is in. Default to null, which will search non_compact. |
| 95 | * |
| 96 | * @return bool Is the option name valid? |
| 97 | */ |
| 98 | public static function is_valid( $name, $group = null ) { |
| 99 | if ( is_array( $name ) ) { |
| 100 | $compact_names = array(); |
| 101 | foreach ( array_keys( self::$grouped_options ) as $_group ) { |
| 102 | $compact_names = array_merge( $compact_names, self::get_option_names( $_group ) ); |
| 103 | } |
| 104 | |
| 105 | $result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names ); |
| 106 | |
| 107 | return empty( $result ); |
| 108 | } |
| 109 | |
| 110 | if ( is_null( $group ) || 'non_compact' === $group ) { |
| 111 | if ( in_array( $name, self::get_option_names( $group ) ) ) { |
| 112 | return true; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | foreach ( array_keys( self::$grouped_options ) as $_group ) { |
| 117 | if ( is_null( $group ) || $group === $_group ) { |
| 118 | if ( in_array( $name, self::get_option_names( $_group ) ) ) { |
| 119 | return true; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Checks if an option must be saved for the whole network in WP Multisite |
| 129 | * |
| 130 | * @param string $option_name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
| 131 | * |
| 132 | * @return bool |
| 133 | */ |
| 134 | public static function is_network_option( $option_name ) { |
| 135 | if ( ! is_multisite() ) { |
| 136 | return false; |
| 137 | } |
| 138 | return in_array( $option_name, self::get_option_names( 'network' ) ); |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
| 143 | * |
| 144 | * @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
| 145 | * @param mixed $default (optional) |
| 146 | * |
| 147 | * @return mixed |
| 148 | */ |
| 149 | public static function get_option( $name, $default = false ) { |
| 150 | if ( self::is_valid( $name, 'non_compact' ) ) { |
| 151 | if ( self::is_network_option( $name ) ) { |
| 152 | return get_site_option( "jetpack_$name", $default ); |
| 153 | } |
| 154 | |
| 155 | return get_option( "jetpack_$name", $default ); |
| 156 | } |
| 157 | |
| 158 | foreach ( array_keys( self::$grouped_options ) as $group ) { |
| 159 | if ( self::is_valid( $name, $group ) ) { |
| 160 | return self::get_grouped_option( $group, $name, $default ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING ); |
| 165 | |
| 166 | return $default; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Returns the requested option, and ensures it's autoloaded in the future. |
| 171 | * This does _not_ adjust the prefix in any way (does not prefix jetpack_%) |
| 172 | * |
| 173 | * @param string $name Option name |
| 174 | * @param mixed $default (optional) |
| 175 | * |
| 176 | * @return mixed |
| 177 | */ |
| 178 | public static function get_option_and_ensure_autoload( $name, $default ) { |
| 179 | // In this function the name is not adjusted by prefixing jetpack_ |
| 180 | // so if it has already prefixed, we'll replace it and then |
| 181 | // check if the option name is a network option or not |
| 182 | $jetpack_name = preg_replace( '/^jetpack_/', '', $name, 1 ); |
| 183 | $is_network_option = self::is_network_option( $jetpack_name ); |
| 184 | $value = $is_network_option ? get_site_option( $name ) : get_option( $name ); |
| 185 | |
| 186 | if ( $value === false && $default !== false ) { |
| 187 | if ( $is_network_option ) { |
| 188 | update_site_option( $name, $default ); |
| 189 | } else { |
| 190 | update_option( $name, $default ); |
| 191 | } |
| 192 | $value = $default; |
| 193 | } |
| 194 | |
| 195 | return $value; |
| 196 | } |
| 197 | |
| 198 | private static function update_grouped_option( $group, $name, $value ) { |
| 199 | $options = get_option( self::$grouped_options[ $group ] ); |
| 200 | if ( ! is_array( $options ) ) { |
| 201 | $options = array(); |
| 202 | } |
| 203 | $options[ $name ] = $value; |
| 204 | |
| 205 | return update_option( self::$grouped_options[ $group ], $options ); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
| 210 | * |
| 211 | * @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
| 212 | * @param mixed $value Option value |
| 213 | * @param string $autoload If not compact option, allows specifying whether to autoload or not. |
| 214 | * |
| 215 | * @return bool Was the option successfully updated? |
| 216 | */ |
| 217 | public static function update_option( $name, $value, $autoload = null ) { |
| 218 | /** |
| 219 | * Fires before Jetpack updates a specific option. |
| 220 | * |
| 221 | * @since 3.0.0 |
| 222 | * |
| 223 | * @param str $name The name of the option being updated. |
| 224 | * @param mixed $value The new value of the option. |
| 225 | */ |
| 226 | do_action( 'pre_update_jetpack_option_' . $name, $name, $value ); |
| 227 | if ( self::is_valid( $name, 'non_compact' ) ) { |
| 228 | if ( self::is_network_option( $name ) ) { |
| 229 | return update_site_option( "jetpack_$name", $value ); |
| 230 | } |
| 231 | |
| 232 | return update_option( "jetpack_$name", $value, $autoload ); |
| 233 | |
| 234 | } |
| 235 | |
| 236 | foreach ( array_keys( self::$grouped_options ) as $group ) { |
| 237 | if ( self::is_valid( $name, $group ) ) { |
| 238 | return self::update_grouped_option( $group, $name, $value ); |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING ); |
| 243 | |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
| 249 | * |
| 250 | * @param array $array array( option name => option value, ... ) |
| 251 | */ |
| 252 | public static function update_options( $array ) { |
| 253 | $names = array_keys( $array ); |
| 254 | |
| 255 | foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ), self::get_option_names( 'private' ) ) as $unknown_name ) { |
| 256 | trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING ); |
| 257 | unset( $array[ $unknown_name ] ); |
| 258 | } |
| 259 | |
| 260 | foreach ( $names as $name ) { |
| 261 | self::update_option( $name, $array[ $name ] ); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Deletes the given option. May be passed multiple option names as an array. |
| 267 | * Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
| 268 | * |
| 269 | * @param string|array $names Option names. They must come _without_ `jetpack_%` prefix. The method will prefix the option names. |
| 270 | * |
| 271 | * @return bool Was the option successfully deleted? |
| 272 | */ |
| 273 | public static function delete_option( $names ) { |
| 274 | $result = true; |
| 275 | $names = (array) $names; |
| 276 | |
| 277 | if ( ! self::is_valid( $names ) ) { |
| 278 | trigger_error( sprintf( 'Invalid Jetpack option names: %s', print_r( $names, 1 ) ), E_USER_WARNING ); |
| 279 | return false; |
| 280 | } |
| 281 | |
| 282 | foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) { |
| 283 | if ( self::is_network_option( $name ) ) { |
| 284 | $result = delete_site_option( "jetpack_$name" ); |
| 285 | } else { |
| 286 | $result = delete_option( "jetpack_$name" ); |
| 287 | } |
| 288 | |
| 289 | } |
| 290 | |
| 291 | foreach ( array_keys( self::$grouped_options ) as $group ) { |
| 292 | if ( ! self::delete_grouped_option( $group, $names ) ) { |
| 293 | $result = false; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | return $result; |
| 298 | } |
| 299 | |
| 300 | private static function get_grouped_option( $group, $name, $default ) { |
| 301 | $options = get_option( self::$grouped_options[ $group ] ); |
| 302 | if ( is_array( $options ) && isset( $options[ $name ] ) ) { |
| 303 | return $options[ $name ]; |
| 304 | } |
| 305 | |
| 306 | return $default; |
| 307 | } |
| 308 | |
| 309 | private static function delete_grouped_option( $group, $names ) { |
| 310 | $options = get_option( self::$grouped_options[ $group ], array() ); |
| 311 | |
| 312 | $to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) ); |
| 313 | if ( $to_delete ) { |
| 314 | foreach ( $to_delete as $name ) { |
| 315 | unset( $options[ $name ] ); |
| 316 | } |
| 317 | |
| 318 | return update_option( self::$grouped_options[ $group ], $options ); |
| 319 | } |
| 320 | |
| 321 | return true; |
| 322 | } |
| 323 | |
| 324 | // Raw option methods allow Jetpack to get / update / delete options via direct DB queries, including options |
| 325 | // that are not created by the Jetpack plugin. This is helpful only in rare cases when we need to bypass |
| 326 | // cache and filters. |
| 327 | |
| 328 | /** |
| 329 | * Deletes an option via $wpdb query. |
| 330 | * |
| 331 | * @param string $name Option name. |
| 332 | * |
| 333 | * @return bool Is the option deleted? |
| 334 | */ |
| 335 | static function delete_raw_option( $name ) { |
| 336 | global $wpdb; |
| 337 | $result = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name = %s", $name ) ); |
| 338 | return $result; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Updates an option via $wpdb query. |
| 343 | * |
| 344 | * @param string $name Option name. |
| 345 | * @param mixed $value Option value. |
| 346 | * @param bool $autoload Specifying whether to autoload or not. |
| 347 | * |
| 348 | * @return bool Is the option updated? |
| 349 | */ |
| 350 | static function update_raw_option( $name, $value, $autoload = false ) { |
| 351 | global $wpdb; |
| 352 | $autoload_value = $autoload ? 'yes' : 'no'; |
| 353 | |
| 354 | $serialized_value = maybe_serialize( $value ); |
| 355 | // try updating, if no update then insert |
| 356 | // TODO: try to deal with the fact that unchanged values can return updated_num = 0 |
| 357 | // below we used "insert ignore" to at least suppress the resulting error |
| 358 | $updated_num = $wpdb->query( |
| 359 | $wpdb->prepare( |
| 360 | "UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", |
| 361 | $serialized_value, |
| 362 | $name |
| 363 | ) |
| 364 | ); |
| 365 | |
| 366 | if ( ! $updated_num ) { |
| 367 | $updated_num = $wpdb->query( |
| 368 | $wpdb->prepare( |
| 369 | "INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, '$autoload_value' )", |
| 370 | $name, |
| 371 | $serialized_value |
| 372 | ) |
| 373 | ); |
| 374 | } |
| 375 | return $updated_num; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Gets an option via $wpdb query. |
| 380 | * |
| 381 | * @param string $name Option name. |
| 382 | * @param mixed $default Default option value if option is not found. |
| 383 | * |
| 384 | * @return mixed Option value, or null if option is not found and default is not specified. |
| 385 | */ |
| 386 | static function get_raw_option( $name, $default = null ) { |
| 387 | global $wpdb; |
| 388 | $value = $wpdb->get_var( |
| 389 | $wpdb->prepare( |
| 390 | "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", |
| 391 | $name |
| 392 | ) |
| 393 | ); |
| 394 | $value = maybe_unserialize( $value ); |
| 395 | |
| 396 | if ( $value === null && $default !== null ) { |
| 397 | return $default; |
| 398 | } |
| 399 | |
| 400 | return $value; |
| 401 | } |
| 402 | |
| 403 | } |
| 404 |