| 1 |
<?php |
| 2 |
/** |
| 3 |
* Legacy Jetpack_Options class. |
| 4 |
* |
| 5 |
* @package automattic/jetpack-connection |
| 6 |
*/ |
| 7 |
|
| 8 |
use Automattic\Jetpack\Constants; |
| 9 |
|
| 10 |
/** |
| 11 |
* Class Jetpack_Options |
| 12 |
*/ |
| 13 |
class Jetpack_Options { |
| 14 |
|
| 15 |
/** |
| 16 |
* An array that maps a grouped option type to an option name. |
| 17 |
* |
| 18 |
* @var array |
| 19 |
*/ |
| 20 |
private static $grouped_options = array( |
| 21 |
'compact' => 'jetpack_options', |
| 22 |
'private' => 'jetpack_private_options', |
| 23 |
); |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns an array of option names for a given type. |
| 27 |
* |
| 28 |
* @param string $type The type of option to return. Defaults to 'compact'. |
| 29 |
* |
| 30 |
* @return array |
| 31 |
*/ |
| 32 |
public static function get_option_names( $type = 'compact' ) { |
| 33 |
switch ( $type ) { |
| 34 |
case 'non-compact': |
| 35 |
case 'non_compact': |
| 36 |
return array( |
| 37 |
'activated', |
| 38 |
'active_modules', |
| 39 |
'active_modules_initialized', // (bool) used to determine that all the default modules were activated, so we know how to act on a reconnection. |
| 40 |
'allowed_xsite_search_ids', // (array) Array of WP.com blog ids that are allowed to search the content of this site |
| 41 |
'available_modules', |
| 42 |
'do_activate', |
| 43 |
'log', |
| 44 |
'slideshow_background_color', |
| 45 |
'widget_twitter', |
| 46 |
'wpcc_options', |
| 47 |
'relatedposts', |
| 48 |
'file_data', |
| 49 |
'autoupdate_plugins', // (array) An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated |
| 50 |
'autoupdate_plugins_translations', // (array) An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated translation files. |
| 51 |
'autoupdate_themes', // (array) An array of theme ids ( eg. twentyfourteen ) that should be autoupdated |
| 52 |
'autoupdate_themes_translations', // (array) An array of theme ids ( eg. twentyfourteen ) that should autoupdated translation files. |
| 53 |
'autoupdate_core', // (bool) Whether or not to autoupdate core |
| 54 |
'autoupdate_translations', // (bool) Whether or not to autoupdate all translations |
| 55 |
'json_api_full_management', // (bool) Allow full management (eg. Activate, Upgrade plugins) of the site via the JSON API. |
| 56 |
'sync_non_public_post_stati', // (bool) Allow synchronisation of posts and pages with non-public status. |
| 57 |
'site_icon_url', // (string) url to the full site icon |
| 58 |
'site_icon_id', // (int) Attachment id of the site icon file |
| 59 |
'dismissed_manage_banner', // (bool) Dismiss Jetpack manage banner allows the user to dismiss the banner permanently |
| 60 |
'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 occurred |
| 61 |
'unique_registrations', // (integer) A counter of how many times the site was registered |
| 62 |
'protect_whitelist', // (array) IP Address for the Protect module to ignore |
| 63 |
'sync_error_idc', // (bool|array) false or array containing the site's home and siteurl at time of IDC error |
| 64 |
'sync_health_status', // (bool|array) An array of data relating to Jetpack's sync health. |
| 65 |
'safe_mode_confirmed', // (bool) True if someone confirms that this site was correctly put into safe mode automatically after an identity crisis is discovered. |
| 66 |
'migrate_for_idc', // (bool) True if someone confirms that this site should migrate stats and subscribers from its previous URL |
| 67 |
'ab_connect_banner_green_bar', // (int) Version displayed of the A/B test for the green bar at the top of the connect banner. |
| 68 |
'tos_agreed', // (bool) Whether or not the TOS for connection has been agreed upon. |
| 69 |
'static_asset_cdn_files', // (array) An nested array of files that we can swap out for cdn versions. |
| 70 |
'mapbox_api_key', // (string) Mapbox API Key, for use with Map block. |
| 71 |
'mailchimp', // (string) Mailchimp keyring data, for mailchimp block. |
| 72 |
'xmlrpc_errors', // (array) Keys are XML-RPC signature error codes. Values are truthy. |
| 73 |
'dismissed_wizard_banner', // (int) (DEPRECATED) True if the Wizard banner has been dismissed. |
| 74 |
); |
| 75 |
|
| 76 |
case 'private': |
| 77 |
return array( |
| 78 |
'blog_token', // (string) The Client Secret/Blog Token of this site. |
| 79 |
'user_token', // (string) The User Token of this site. (deprecated) |
| 80 |
'user_tokens', // (array) User Tokens for each user of this site who has connected to jetpack.wordpress.com. |
| 81 |
'purchase_token', // (string) Token for logged out user purchases. |
| 82 |
'token_lock', // (string) Token lock in format `expiration_date|||site_url`. |
| 83 |
); |
| 84 |
|
| 85 |
case 'network': |
| 86 |
return array( |
| 87 |
'file_data', // (array) List of absolute paths to all Jetpack modules |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
return array( |
| 92 |
'id', // (int) The Client ID/WP.com Blog ID of this site. |
| 93 |
'publicize_connections', // (array) An array of Publicize connections from WordPress.com. |
| 94 |
'master_user', // (int) The local User ID of the user who connected this site to jetpack.wordpress.com. |
| 95 |
'protected_owner', // (array) Anchor identifying the locked connection owner. WordPress.com is authoritative; this is a local cache. Keys: wpcom_user_id, email, local_user_id, locked, confirmed_at, confirmed_by. |
| 96 |
'version', // (string) Used during upgrade procedure to auto-activate new modules. version:time. |
| 97 |
'old_version', // (string) Used to determine which modules are the most recently added. previous_version:time. |
| 98 |
'fallback_no_verify_ssl_certs', // (int) Flag for determining if this host must skip SSL Certificate verification due to misconfigured SSL. |
| 99 |
'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' ) |
| 100 |
'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. |
| 101 |
'videopress', // (array) VideoPress options array. |
| 102 |
'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. |
| 103 |
'social_links', // (array) The specified links for each social networking site. |
| 104 |
'identity_crisis_whitelist', // (array) An array of options, each having an array of the values whitelisted for it. |
| 105 |
'gplus_authors', // (array) The Google+ authorship information for connected users. |
| 106 |
'last_heartbeat', // (int) The timestamp of the last heartbeat that fired. |
| 107 |
'hide_jitm', // (array) A list of just in time messages that we should not show because they have been dismissed by the user. |
| 108 |
'custom_css_4.7_migration', // (bool) Whether Custom CSS has scanned for and migrated any legacy CSS CPT entries to the new Core format. |
| 109 |
'image_widget_migration', // (bool) Whether any legacy Image Widgets have been converted to the new Core widget. |
| 110 |
'gallery_widget_migration', // (bool) Whether any legacy Gallery Widgets have been converted to the new Core widget. |
| 111 |
'sso_first_login', // (bool) Is this the first time the user logins via SSO. |
| 112 |
'dismissed_hints', // (array) Part of Plugin Search Hints. List of cards that have been dismissed. |
| 113 |
'first_admin_view', // (bool) Set to true the first time the user views the admin. Usually after the initial connection. |
| 114 |
'setup_wizard_questionnaire', // (array) (DEPRECATED) List of user choices from the setup wizard. |
| 115 |
'setup_wizard_status', // (string) (DEPRECATED) Status of the setup wizard. |
| 116 |
'licensing_error', // (string) Last error message occurred while attaching licenses that is yet to be surfaced to the user. |
| 117 |
'recommendations_data', // (array) The user choice and other data for the recommendations. |
| 118 |
'recommendations_step', // (string) The current step of the recommendations. |
| 119 |
'recommendations_conditional', // (array) An array of action-based recommendations. |
| 120 |
'licensing_activation_notice_dismiss', // (array) The `last_detached_count` and the `last_dismissed_time` for the user-license activation notice. |
| 121 |
'has_seen_wc_connection_modal', // (bool) Whether the site has displayed the WooCommerce Connection modal |
| 122 |
'partner_coupon', // (string) A Jetpack partner issued coupon to promote a sale together with Jetpack. |
| 123 |
'partner_coupon_added', // (string) A date for when `partner_coupon` was added, so we can auto-purge after a certain time interval. |
| 124 |
'dismissed_backup_review_restore', // (bool) Determines if the component review request is dismissed for successful restore requests. |
| 125 |
'dismissed_backup_review_backups', // (bool) Determines if the component review request is dismissed for successful backup requests. |
| 126 |
'identity_crisis_url_secret', // (array) The IDC URL secret and its expiration date. |
| 127 |
'identity_crisis_ip_requester', // (array) The IDC IP address and its expiration date. |
| 128 |
'dismissed_welcome_banner', // (bool) Determines if the welcome banner has been dismissed or not. |
| 129 |
'dismissed_a4a_banner', // (bool) Determines if the Automattic for Agencies banner has been dismissed or not. |
| 130 |
'recommendations_evaluation', // (object) Catalog of recommended modules with corresponding score following successful site evaluation in Welcome Banner. |
| 131 |
'dismissed_recommendations', // (bool) Determines if the recommendations have been dismissed or not. |
| 132 |
'recommendations_first_run', // (bool) Determines if the current recommendations are the initial default auto-loaded ones (without user input). |
| 133 |
'historically_active_modules', // (array) List of installed plugins/enabled modules that have at one point in time been active and working |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Is the option name valid? |
| 139 |
* |
| 140 |
* @param string $name The name of the option. |
| 141 |
* @param string|null $group The name of the group that the option is in. Default to null, which will search non_compact. |
| 142 |
* |
| 143 |
* @return bool Is the option name valid? |
| 144 |
*/ |
| 145 |
public static function is_valid( $name, $group = null ) { |
| 146 |
if ( is_array( $name ) ) { |
| 147 |
$compact_names = array(); |
| 148 |
foreach ( array_keys( self::$grouped_options ) as $_group ) { |
| 149 |
$compact_names = array_merge( $compact_names, self::get_option_names( $_group ) ); |
| 150 |
} |
| 151 |
|
| 152 |
$result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names ); |
| 153 |
|
| 154 |
return empty( $result ); |
| 155 |
} |
| 156 |
|
| 157 |
if ( $group === null || 'non_compact' === $group ) { |
| 158 |
if ( in_array( $name, self::get_option_names( $group ), true ) ) { |
| 159 |
return true; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
foreach ( array_keys( self::$grouped_options ) as $_group ) { |
| 164 |
if ( $group === null || $group === $_group ) { |
| 165 |
if ( in_array( $name, self::get_option_names( $_group ), true ) ) { |
| 166 |
return true; |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
return false; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Checks if an option must be saved for the whole network in WP Multisite |
| 176 |
* |
| 177 |
* @param string $option_name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
| 178 |
* |
| 179 |
* @return bool |
| 180 |
*/ |
| 181 |
public static function is_network_option( $option_name ) { |
| 182 |
if ( ! is_multisite() ) { |
| 183 |
return false; |
| 184 |
} |
| 185 |
return in_array( $option_name, self::get_option_names( 'network' ), true ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Filters the requested option. |
| 190 |
* This is a wrapper around `get_option_from_database` so that we can filter the option. |
| 191 |
* |
| 192 |
* @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
| 193 |
* @param mixed $default (optional). |
| 194 |
* |
| 195 |
* @return mixed |
| 196 |
*/ |
| 197 |
public static function get_option( $name, $default = false ) { |
| 198 |
// Check if external storage should be used for this option |
| 199 |
if ( self::should_use_external_storage( $name ) ) { |
| 200 |
// Try external storage |
| 201 |
if ( class_exists( 'Automattic\Jetpack\Connection\External_Storage' ) ) { |
| 202 |
$external_value = \Automattic\Jetpack\Connection\External_Storage::get_value( $name ); |
| 203 |
if ( null !== $external_value ) { |
| 204 |
return $external_value; |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Filter Jetpack Options. |
| 211 |
* Can be useful in environments when Jetpack is running with a different setup |
| 212 |
* |
| 213 |
* @since 1.7.0 |
| 214 |
* |
| 215 |
* @param string $value The value from the database. |
| 216 |
* @param string $name Option name, _without_ `jetpack_%` prefix. |
| 217 |
* @return string $value, unless the filters modify it. |
| 218 |
*/ |
| 219 |
return apply_filters( 'jetpack_options', self::get_option_from_database( $name, $default ), $name ); |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* Checks whether an option has a stored value, distinguishing an absent option from one stored |
| 224 |
* as a falsy value. Reads from the same storage as `get_option` (external storage or database) |
| 225 |
* but does not apply the `jetpack_options` filter, so a filter override is not mistaken for a |
| 226 |
* stored value. |
| 227 |
* |
| 228 |
* @since 8.11.0 |
| 229 |
* |
| 230 |
* @param string $name Option name. It must come _without_ `jetpack_%` prefix. |
| 231 |
* |
| 232 |
* @return bool Whether the option is stored. |
| 233 |
*/ |
| 234 |
public static function option_exists( $name ) { |
| 235 |
if ( self::should_use_external_storage( $name ) |
| 236 |
&& class_exists( 'Automattic\Jetpack\Connection\External_Storage' ) |
| 237 |
&& null !== \Automattic\Jetpack\Connection\External_Storage::get_value( $name ) |
| 238 |
) { |
| 239 |
return true; |
| 240 |
} |
| 241 |
|
| 242 |
// A value no caller would ever store, so getting it back means the option is absent. |
| 243 |
$sentinel = '__jetpack_option_absent__'; |
| 244 |
return $sentinel !== self::get_option_from_database( $name, $sentinel ); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
| 249 |
* |
| 250 |
* @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
| 251 |
* @param mixed $default (optional). |
| 252 |
* |
| 253 |
* @return mixed |
| 254 |
*/ |
| 255 |
private static function get_option_from_database( $name, $default = false ) { |
| 256 |
if ( self::is_valid( $name, 'non_compact' ) ) { |
| 257 |
if ( self::is_network_option( $name ) ) { |
| 258 |
return get_site_option( "jetpack_$name", $default ); |
| 259 |
} |
| 260 |
|
| 261 |
return get_option( "jetpack_$name", $default ); |
| 262 |
} |
| 263 |
|
| 264 |
foreach ( array_keys( self::$grouped_options ) as $group ) { |
| 265 |
if ( self::is_valid( $name, $group ) ) { |
| 266 |
return self::get_grouped_option( $group, $name, $default ); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
return $default; |
| 271 |
} |
| 272 |
|
| 273 |
/** |
| 274 |
* Options that can be stored in external storage. |
| 275 |
* |
| 276 |
* @since 6.18.0 |
| 277 |
* |
| 278 |
* @var array |
| 279 |
*/ |
| 280 |
private static $external_storage_allowlist = array( 'blog_token', 'id', 'master_user', 'protected_owner', 'user_tokens' ); |
| 281 |
|
| 282 |
/** |
| 283 |
* Determines if external storage should be used for a given option. |
| 284 |
* Simple allowlist check with global killswitch. |
| 285 |
* |
| 286 |
* @since 6.17.0 |
| 287 |
* |
| 288 |
* @param string $name Option name, _without_ `jetpack_%` prefix. |
| 289 |
* @return bool True if external storage should be checked for this option. |
| 290 |
*/ |
| 291 |
private static function should_use_external_storage( $name ) { |
| 292 |
// Check allowlist and global killswitch |
| 293 |
if ( ! in_array( $name, self::$external_storage_allowlist, true ) || |
| 294 |
( defined( 'JETPACK_EXTERNAL_STORAGE_DISABLED' ) && constant( 'JETPACK_EXTERNAL_STORAGE_DISABLED' ) ) ) { |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
return true; |
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Returns the requested option, and ensures it's autoloaded in the future. |
| 303 |
* This does _not_ adjust the prefix in any way (does not prefix jetpack_%) |
| 304 |
* |
| 305 |
* @param string $name Option name. |
| 306 |
* @param mixed $default (optional). |
| 307 |
* |
| 308 |
* @return mixed |
| 309 |
*/ |
| 310 |
public static function get_option_and_ensure_autoload( $name, $default ) { |
| 311 |
// In this function the name is not adjusted by prefixing jetpack_ |
| 312 |
// so if it has already prefixed, we'll replace it and then |
| 313 |
// check if the option name is a network option or not. |
| 314 |
$jetpack_name = preg_replace( '/^jetpack_/', '', $name, 1 ); |
| 315 |
$is_network_option = self::is_network_option( $jetpack_name ); |
| 316 |
$value = $is_network_option ? get_site_option( $name ) : get_option( $name ); |
| 317 |
|
| 318 |
if ( false === $value && false !== $default ) { |
| 319 |
if ( $is_network_option ) { |
| 320 |
add_site_option( $name, $default ); |
| 321 |
} else { |
| 322 |
add_option( $name, $default ); |
| 323 |
} |
| 324 |
$value = $default; |
| 325 |
} |
| 326 |
|
| 327 |
return $value; |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Update grouped option |
| 332 |
* |
| 333 |
* @param string $group Options group. |
| 334 |
* @param string $name Options name. |
| 335 |
* @param mixed $value Options value. |
| 336 |
* |
| 337 |
* @return bool Success or failure. |
| 338 |
*/ |
| 339 |
private static function update_grouped_option( $group, $name, $value ) { |
| 340 |
$options = get_option( self::$grouped_options[ $group ] ); |
| 341 |
if ( ! is_array( $options ) ) { |
| 342 |
$options = array(); |
| 343 |
} |
| 344 |
$options[ $name ] = $value; |
| 345 |
|
| 346 |
return update_option( self::$grouped_options[ $group ], $options ); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
* Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
| 351 |
* |
| 352 |
* @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name. |
| 353 |
* @param mixed $value Option value. |
| 354 |
* @param bool|null $autoload If not compact option, allows specifying whether to autoload or not. |
| 355 |
* |
| 356 |
* @return bool Was the option successfully updated? |
| 357 |
*/ |
| 358 |
public static function update_option( $name, $value, $autoload = null ) { |
| 359 |
/** |
| 360 |
* Fires before Jetpack updates a specific option. |
| 361 |
* |
| 362 |
* @since 1.1.2 |
| 363 |
* @since-jetpack 3.0.0 |
| 364 |
* |
| 365 |
* @param string $name The name of the option being updated. |
| 366 |
* @param mixed $value The new value of the option. |
| 367 |
*/ |
| 368 |
do_action( 'pre_update_jetpack_option_' . $name, $name, $value ); |
| 369 |
if ( self::is_valid( $name, 'non_compact' ) ) { |
| 370 |
if ( self::is_network_option( $name ) ) { |
| 371 |
return update_site_option( "jetpack_$name", $value ); |
| 372 |
} |
| 373 |
|
| 374 |
return update_option( "jetpack_$name", $value, $autoload ); |
| 375 |
|
| 376 |
} |
| 377 |
|
| 378 |
foreach ( array_keys( self::$grouped_options ) as $group ) { |
| 379 |
if ( self::is_valid( $name, $group ) ) { |
| 380 |
return self::update_grouped_option( $group, $name, $value ); |
| 381 |
} |
| 382 |
} |
| 383 |
|
| 384 |
trigger_error( sprintf( 'Invalid Jetpack option name: %s', esc_html( $name ) ), E_USER_WARNING ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error -- Don't want to change legacy behavior. |
| 385 |
|
| 386 |
return false; |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
| 391 |
* |
| 392 |
* @param array $array array( option name => option value, ... ). |
| 393 |
*/ |
| 394 |
public static function update_options( $array ) { |
| 395 |
$names = array_keys( $array ); |
| 396 |
|
| 397 |
foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ), self::get_option_names( 'private' ) ) as $unknown_name ) { |
| 398 |
trigger_error( sprintf( 'Invalid Jetpack option name: %s', esc_html( $unknown_name ) ), E_USER_WARNING ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error -- Don't change legacy behavior. |
| 399 |
unset( $array[ $unknown_name ] ); |
| 400 |
} |
| 401 |
|
| 402 |
foreach ( $names as $name ) { |
| 403 |
self::update_option( $name, $array[ $name ] ); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
/** |
| 408 |
* Deletes the given option. May be passed multiple option names as an array. |
| 409 |
* Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
| 410 |
* |
| 411 |
* @param string|array $names Option names. They must come _without_ `jetpack_%` prefix. The method will prefix the option names. |
| 412 |
* |
| 413 |
* @return bool Was the option successfully deleted? |
| 414 |
*/ |
| 415 |
public static function delete_option( $names ) { |
| 416 |
$result = true; |
| 417 |
$names = (array) $names; |
| 418 |
|
| 419 |
if ( ! self::is_valid( $names ) ) { |
| 420 |
// phpcs:disable -- This line triggers a handful of errors; ignoring to avoid changing legacy behavior. |
| 421 |
trigger_error( sprintf( 'Invalid Jetpack option names: %s', print_r( $names, 1 ) ), E_USER_WARNING ); |
| 422 |
// phpcs:enable |
| 423 |
return false; |
| 424 |
} |
| 425 |
|
| 426 |
foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) { |
| 427 |
if ( self::is_network_option( $name ) ) { |
| 428 |
$result = delete_site_option( "jetpack_$name" ); |
| 429 |
} else { |
| 430 |
$result = delete_option( "jetpack_$name" ); |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
foreach ( array_keys( self::$grouped_options ) as $group ) { |
| 435 |
if ( ! self::delete_grouped_option( $group, $names ) ) { |
| 436 |
$result = false; |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
return $result; |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Get group option. |
| 445 |
* |
| 446 |
* @param string $group Option group name. |
| 447 |
* @param string $name Option name. |
| 448 |
* @param mixed $default Default option value. |
| 449 |
* |
| 450 |
* @return mixed Option. |
| 451 |
*/ |
| 452 |
private static function get_grouped_option( $group, $name, $default ) { |
| 453 |
$options = get_option( self::$grouped_options[ $group ] ); |
| 454 |
if ( is_array( $options ) && isset( $options[ $name ] ) ) { |
| 455 |
return $options[ $name ]; |
| 456 |
} |
| 457 |
|
| 458 |
return $default; |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* Delete grouped option. |
| 463 |
* |
| 464 |
* @param string $group Option group name. |
| 465 |
* @param array $names Option names. |
| 466 |
* |
| 467 |
* @return bool Success or failure. |
| 468 |
*/ |
| 469 |
private static function delete_grouped_option( $group, $names ) { |
| 470 |
$options = get_option( self::$grouped_options[ $group ], array() ); |
| 471 |
|
| 472 |
$to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) ); |
| 473 |
if ( $to_delete ) { |
| 474 |
foreach ( $to_delete as $name ) { |
| 475 |
unset( $options[ $name ] ); |
| 476 |
} |
| 477 |
|
| 478 |
return update_option( self::$grouped_options[ $group ], $options ); |
| 479 |
} |
| 480 |
|
| 481 |
return true; |
| 482 |
} |
| 483 |
|
| 484 |
/* |
| 485 |
* Raw option methods allow Jetpack to get / update / delete options via direct DB queries, including options |
| 486 |
* that are not created by the Jetpack plugin. This is helpful only in rare cases when we need to bypass |
| 487 |
* cache and filters. |
| 488 |
*/ |
| 489 |
|
| 490 |
/** |
| 491 |
* Deletes an option via $wpdb query. |
| 492 |
* |
| 493 |
* @param string $name Option name. |
| 494 |
* |
| 495 |
* @return bool Is the option deleted? |
| 496 |
*/ |
| 497 |
public static function delete_raw_option( $name ) { |
| 498 |
if ( self::bypass_raw_option( $name ) ) { |
| 499 |
return delete_option( $name ); |
| 500 |
} |
| 501 |
global $wpdb; |
| 502 |
$result = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name = %s", $name ) ); |
| 503 |
return $result; |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* Updates an option via $wpdb query. |
| 508 |
* |
| 509 |
* @param string $name Option name. |
| 510 |
* @param mixed $value Option value. |
| 511 |
* @param bool $autoload Specifying whether to autoload or not. |
| 512 |
* |
| 513 |
* @return bool Is the option updated? |
| 514 |
*/ |
| 515 |
public static function update_raw_option( $name, $value, $autoload = false ) { |
| 516 |
if ( self::bypass_raw_option( $name ) ) { |
| 517 |
return update_option( $name, $value, $autoload ); |
| 518 |
} |
| 519 |
global $wpdb; |
| 520 |
$autoload_value = $autoload ? 'yes' : 'no'; |
| 521 |
|
| 522 |
$old_value = $wpdb->get_var( |
| 523 |
$wpdb->prepare( |
| 524 |
"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", |
| 525 |
$name |
| 526 |
) |
| 527 |
); |
| 528 |
if ( $old_value === $value ) { |
| 529 |
return false; |
| 530 |
} |
| 531 |
|
| 532 |
$serialized_value = maybe_serialize( $value ); |
| 533 |
// below we used "insert ignore" to at least suppress the resulting error. |
| 534 |
$updated_num = $wpdb->query( |
| 535 |
$wpdb->prepare( |
| 536 |
"UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s", |
| 537 |
$serialized_value, |
| 538 |
$name |
| 539 |
) |
| 540 |
); |
| 541 |
|
| 542 |
// Try inserting the option if the value doesn't exits. |
| 543 |
if ( ! $updated_num ) { |
| 544 |
$updated_num = $wpdb->query( |
| 545 |
$wpdb->prepare( |
| 546 |
"INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, %s )", |
| 547 |
$name, |
| 548 |
$serialized_value, |
| 549 |
$autoload_value |
| 550 |
) |
| 551 |
); |
| 552 |
} |
| 553 |
return (bool) $updated_num; |
| 554 |
} |
| 555 |
|
| 556 |
/** |
| 557 |
* Gets an option via $wpdb query. |
| 558 |
* |
| 559 |
* @since 1.1.2 |
| 560 |
* @since-jetpack 5.4.0 |
| 561 |
* |
| 562 |
* @param string $name Option name. |
| 563 |
* @param mixed $default Default option value if option is not found. |
| 564 |
* |
| 565 |
* @return mixed Option value, or null if option is not found and default is not specified. |
| 566 |
*/ |
| 567 |
public static function get_raw_option( $name, $default = null ) { |
| 568 |
if ( self::bypass_raw_option( $name ) ) { |
| 569 |
return get_option( $name, $default ); |
| 570 |
} |
| 571 |
|
| 572 |
global $wpdb; |
| 573 |
$value = $wpdb->get_var( |
| 574 |
$wpdb->prepare( |
| 575 |
"SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", |
| 576 |
$name |
| 577 |
) |
| 578 |
); |
| 579 |
$value = maybe_unserialize( $value ); |
| 580 |
|
| 581 |
if ( null === $value && null !== $default ) { |
| 582 |
return $default; |
| 583 |
} |
| 584 |
|
| 585 |
return $value; |
| 586 |
} |
| 587 |
|
| 588 |
/** |
| 589 |
* This function checks for a constant that, if present, will disable direct DB queries Jetpack uses to manage certain options and force Jetpack to always use Options API instead. |
| 590 |
* Options can be selectively managed via a blocklist by filtering option names via the jetpack_disabled_raw_option filter. |
| 591 |
* |
| 592 |
* @param string $name Option name. |
| 593 |
* |
| 594 |
* @return bool |
| 595 |
*/ |
| 596 |
public static function bypass_raw_option( $name ) { |
| 597 |
|
| 598 |
if ( Constants::get_constant( 'JETPACK_DISABLE_RAW_OPTIONS' ) ) { |
| 599 |
return true; |
| 600 |
} |
| 601 |
/** |
| 602 |
* Allows to disable particular raw options. |
| 603 |
* |
| 604 |
* @since 1.1.2 |
| 605 |
* @since-jetpack 5.5.0 |
| 606 |
* |
| 607 |
* @param array $disabled_raw_options An array of option names that you can selectively blocklist from being managed via direct database queries. |
| 608 |
*/ |
| 609 |
$disabled_raw_options = apply_filters( 'jetpack_disabled_raw_options', array() ); |
| 610 |
return isset( $disabled_raw_options[ $name ] ); |
| 611 |
} |
| 612 |
|
| 613 |
/** |
| 614 |
* Gets all known options that are used by Jetpack and managed by Jetpack_Options. |
| 615 |
* |
| 616 |
* @since 1.1.2 |
| 617 |
* @since-jetpack 5.4.0 |
| 618 |
* |
| 619 |
* @param boolean $strip_unsafe_options If true, and by default, will strip out options necessary for the connection to WordPress.com. |
| 620 |
* @return array An array of all options managed via the Jetpack_Options class. |
| 621 |
*/ |
| 622 |
public static function get_all_jetpack_options( $strip_unsafe_options = true ) { |
| 623 |
$jetpack_options = self::get_option_names(); |
| 624 |
$jetpack_options_non_compat = self::get_option_names( 'non_compact' ); |
| 625 |
$jetpack_options_private = self::get_option_names( 'private' ); |
| 626 |
|
| 627 |
$all_jp_options = array_merge( $jetpack_options, $jetpack_options_non_compat, $jetpack_options_private ); |
| 628 |
|
| 629 |
if ( $strip_unsafe_options ) { |
| 630 |
// Flag some Jetpack options as unsafe. |
| 631 |
$unsafe_options = array( |
| 632 |
'id', // (int) The Client ID/WP.com Blog ID of this site. |
| 633 |
'master_user', // (int) The local User ID of the user who connected this site to jetpack.wordpress.com. |
| 634 |
'protected_owner', // (array) Anchor identifying the locked connection owner. Resetting it would unlock ownership while the connection survives. |
| 635 |
'version', // (string) Used during upgrade procedure to auto-activate new modules. version:time |
| 636 |
|
| 637 |
// non_compact. |
| 638 |
'activated', |
| 639 |
|
| 640 |
// private. |
| 641 |
'register', |
| 642 |
'blog_token', // (string) The Client Secret/Blog Token of this site. |
| 643 |
'user_token', // (string) The User Token of this site. (deprecated) |
| 644 |
'user_tokens', |
| 645 |
); |
| 646 |
|
| 647 |
// Remove the unsafe Jetpack options. |
| 648 |
foreach ( $unsafe_options as $unsafe_option ) { |
| 649 |
$key = array_search( $unsafe_option, $all_jp_options, true ); |
| 650 |
if ( false !== $key ) { |
| 651 |
unset( $all_jp_options[ $key ] ); |
| 652 |
} |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
return $all_jp_options; |
| 657 |
} |
| 658 |
|
| 659 |
/** |
| 660 |
* Get all options that are not managed by the Jetpack_Options class that are used by Jetpack. |
| 661 |
* |
| 662 |
* @since 1.1.2 |
| 663 |
* @since-jetpack 5.4.0 |
| 664 |
* |
| 665 |
* @return array |
| 666 |
*/ |
| 667 |
public static function get_all_wp_options() { |
| 668 |
// A manual build of the wp options. |
| 669 |
return array( |
| 670 |
'sharing-options', |
| 671 |
'disabled_likes', |
| 672 |
'disabled_reblogs', |
| 673 |
'jetpack_comments_likes_enabled', |
| 674 |
'stats_options', |
| 675 |
'stats_dashboard_widget', |
| 676 |
'safecss_preview_rev', |
| 677 |
'safecss_rev', |
| 678 |
'safecss_revision_migrated', |
| 679 |
'nova_menu_order', |
| 680 |
'jetpack_portfolio', |
| 681 |
'jetpack_portfolio_posts_per_page', |
| 682 |
'jetpack_testimonial', |
| 683 |
'jetpack_testimonial_posts_per_page', |
| 684 |
'sharedaddy_disable_resources', |
| 685 |
'sharing-options', |
| 686 |
'sharing-services', |
| 687 |
'site_icon_temp_data', |
| 688 |
'featured-content', |
| 689 |
'site_logo', |
| 690 |
'jetpack_dismissed_notices', |
| 691 |
'jetpack-twitter-cards-site-tag', |
| 692 |
'jetpack-sitemap-state', |
| 693 |
'jetpack_sitemap_post_types', |
| 694 |
'jetpack_sitemap_location', |
| 695 |
'jetpack_protect_key', |
| 696 |
'jetpack_protect_blocked_attempts', |
| 697 |
'jetpack_protect_activating', |
| 698 |
'jetpack_active_plan', |
| 699 |
'jetpack_activation_source', |
| 700 |
'jetpack_site_products', |
| 701 |
'jetpack_sso_match_by_email', |
| 702 |
'jetpack_sso_require_two_step', |
| 703 |
'jetpack_sso_remove_login_form', |
| 704 |
'jetpack_last_connect_url_check', |
| 705 |
'jetpack_excluded_extensions', |
| 706 |
); |
| 707 |
} |
| 708 |
|
| 709 |
/** |
| 710 |
* Gets all options that can be safely reset by CLI. |
| 711 |
* |
| 712 |
* @since 1.1.2 |
| 713 |
* @since-jetpack 5.4.0 |
| 714 |
* |
| 715 |
* @return array array Associative array containing jp_options which are managed by the Jetpack_Options class and wp_options which are not. |
| 716 |
*/ |
| 717 |
public static function get_options_for_reset() { |
| 718 |
$all_jp_options = self::get_all_jetpack_options(); |
| 719 |
|
| 720 |
$wp_options = self::get_all_wp_options(); |
| 721 |
|
| 722 |
$options = array( |
| 723 |
'jp_options' => $all_jp_options, |
| 724 |
'wp_options' => $wp_options, |
| 725 |
); |
| 726 |
|
| 727 |
return $options; |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* Delete all known options |
| 732 |
* |
| 733 |
* @since 1.1.2 |
| 734 |
* @since-jetpack 5.4.0 |
| 735 |
* |
| 736 |
* @return void |
| 737 |
*/ |
| 738 |
public static function delete_all_known_options() { |
| 739 |
// Delete all compact options. |
| 740 |
foreach ( (array) self::$grouped_options as $option_name ) { |
| 741 |
delete_option( $option_name ); |
| 742 |
} |
| 743 |
|
| 744 |
// Delete all non-compact Jetpack options. |
| 745 |
foreach ( (array) self::get_option_names( 'non-compact' ) as $option_name ) { |
| 746 |
self::delete_option( $option_name ); |
| 747 |
} |
| 748 |
|
| 749 |
// Delete all options that can be reset via CLI, that aren't Jetpack options. |
| 750 |
foreach ( (array) self::get_all_wp_options() as $option_name ) { |
| 751 |
delete_option( $option_name ); |
| 752 |
} |
| 753 |
} |
| 754 |
} |
| 755 |
|