PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.3.4
Jetpack – WP Security, Backup, Speed, & Growth v7.3.4
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / class.jetpack-options.php
class.jetpack-options.php
605 lines 21.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 'edit_links_calypso_redirect', // (bool) Whether post/page edit links on front end should point to Calypso.
31 'log',
32 'slideshow_background_color',
33 'widget_twitter',
34 'wpcc_options',
35 'relatedposts',
36 'file_data',
37 'autoupdate_plugins', // (array) An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated
38 'autoupdate_plugins_translations', // (array) An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated translation files.
39 'autoupdate_themes', // (array) An array of theme ids ( eg. twentyfourteen ) that should be autoupdated
40 'autoupdate_themes_translations', // (array) An array of theme ids ( eg. twentyfourteen ) that should autoupdated translation files.
41 'autoupdate_core', // (bool) Whether or not to autoupdate core
42 'autoupdate_translations', // (bool) Whether or not to autoupdate all translations
43 'json_api_full_management', // (bool) Allow full management (eg. Activate, Upgrade plugins) of the site via the JSON API.
44 'sync_non_public_post_stati', // (bool) Allow synchronisation of posts and pages with non-public status.
45 'site_icon_url', // (string) url to the full site icon
46 'site_icon_id', // (int) Attachment id of the site icon file
47 'dismissed_manage_banner', // (bool) Dismiss Jetpack manage banner allows the user to dismiss the banner permanently
48 'restapi_stats_cache', // (array) Stats Cache data.
49 '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
50 'protect_whitelist', // (array) IP Address for the Protect module to ignore
51 'sync_error_idc', // (bool|array) false or array containing the site's home and siteurl at time of IDC error
52 'safe_mode_confirmed', // (bool) True if someone confirms that this site was correctly put into safe mode automatically after an identity crisis is discovered.
53 'migrate_for_idc', // (bool) True if someone confirms that this site should migrate stats and subscribers from its previous URL
54 'dismissed_connection_banner', // (bool) True if the connection banner has been dismissed
55 'ab_connect_banner_green_bar', // (int) Version displayed of the A/B test for the green bar at the top of the connect banner.
56 'onboarding', // (string) Auth token to be used in the onboarding connection flow
57 'tos_agreed', // (bool) Whether or not the TOS for connection has been agreed upon.
58 'static_asset_cdn_files', // (array) An nested array of files that we can swap out for cdn versions.
59 'mapbox_api_key', // (string) Mapbox API Key, for use with Map block.
60 'mailchimp', // (string) Mailchimp keyring data, for mailchimp block.
61 );
62
63 case 'private' :
64 return array(
65 'blog_token', // (string) The Client Secret/Blog Token of this site.
66 'user_token', // (string) The User Token of this site. (deprecated)
67 'user_tokens' // (array) User Tokens for each user of this site who has connected to jetpack.wordpress.com.
68 );
69
70 case 'network' :
71 return array(
72 'onboarding', // (string) Auth token to be used in the onboarding connection flow
73 'file_data' // (array) List of absolute paths to all Jetpack modules
74 );
75 }
76
77 return array(
78 'id', // (int) The Client ID/WP.com Blog ID of this site.
79 'publicize_connections', // (array) An array of Publicize connections from WordPress.com
80 'master_user', // (int) The local User ID of the user who connected this site to jetpack.wordpress.com.
81 'version', // (string) Used during upgrade procedure to auto-activate new modules. version:time
82 'old_version', // (string) Used to determine which modules are the most recently added. previous_version:time
83 'fallback_no_verify_ssl_certs', // (int) Flag for determining if this host must skip SSL Certificate verification due to misconfigured SSL.
84 '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' )
85 '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.
86 'videopress', // (array) VideoPress options array.
87 '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.
88 'social_links', // (array) The specified links for each social networking site.
89 'identity_crisis_whitelist', // (array) An array of options, each having an array of the values whitelisted for it.
90 'gplus_authors', // (array) The Google+ authorship information for connected users.
91 'last_heartbeat', // (int) The timestamp of the last heartbeat that fired.
92 'jumpstart', // (string) A flag for whether or not to show the Jump Start. Accepts: new_connection, jumpstart_activated, jumpstart_dismissed.
93 'hide_jitm', // (array) A list of just in time messages that we should not show because they have been dismissed by the user
94 'custom_css_4.7_migration', // (bool) Whether Custom CSS has scanned for and migrated any legacy CSS CPT entries to the new Core format.
95 'image_widget_migration', // (bool) Whether any legacy Image Widgets have been converted to the new Core widget
96 'gallery_widget_migration', // (bool) Whether any legacy Gallery Widgets have been converted to the new Core widget
97 'sso_first_login', // (bool) Is this the first time the user logins via SSO.
98 'dismissed_hints', // (array) Part of Plugin Search Hints. List of cards that have been dismissed.
99 'first_admin_view', // (bool) Set to true the first time the user views the admin. Usually after the initial connection.
100 );
101 }
102
103 /**
104 * Is the option name valid?
105 *
106 * @param string $name The name of the option
107 * @param string|null $group The name of the group that the option is in. Default to null, which will search non_compact.
108 *
109 * @return bool Is the option name valid?
110 */
111 public static function is_valid( $name, $group = null ) {
112 if ( is_array( $name ) ) {
113 $compact_names = array();
114 foreach ( array_keys( self::$grouped_options ) as $_group ) {
115 $compact_names = array_merge( $compact_names, self::get_option_names( $_group ) );
116 }
117
118 $result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names );
119
120 return empty( $result );
121 }
122
123 if ( is_null( $group ) || 'non_compact' === $group ) {
124 if ( in_array( $name, self::get_option_names( $group ) ) ) {
125 return true;
126 }
127 }
128
129 foreach ( array_keys( self::$grouped_options ) as $_group ) {
130 if ( is_null( $group ) || $group === $_group ) {
131 if ( in_array( $name, self::get_option_names( $_group ) ) ) {
132 return true;
133 }
134 }
135 }
136
137 return false;
138 }
139
140 /**
141 * Checks if an option must be saved for the whole network in WP Multisite
142 *
143 * @param string $option_name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name.
144 *
145 * @return bool
146 */
147 public static function is_network_option( $option_name ) {
148 if ( ! is_multisite() ) {
149 return false;
150 }
151 return in_array( $option_name, self::get_option_names( 'network' ) );
152 }
153
154 /**
155 * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate.
156 *
157 * @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name.
158 * @param mixed $default (optional)
159 *
160 * @return mixed
161 */
162 public static function get_option( $name, $default = false ) {
163 if ( self::is_valid( $name, 'non_compact' ) ) {
164 if ( self::is_network_option( $name ) ) {
165 return get_site_option( "jetpack_$name", $default );
166 }
167
168 return get_option( "jetpack_$name", $default );
169 }
170
171 foreach ( array_keys( self::$grouped_options ) as $group ) {
172 if ( self::is_valid( $name, $group ) ) {
173 return self::get_grouped_option( $group, $name, $default );
174 }
175 }
176
177 trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
178
179 return $default;
180 }
181
182 /**
183 * Returns the requested option, and ensures it's autoloaded in the future.
184 * This does _not_ adjust the prefix in any way (does not prefix jetpack_%)
185 *
186 * @param string $name Option name
187 * @param mixed $default (optional)
188 *
189 * @return mixed
190 */
191 public static function get_option_and_ensure_autoload( $name, $default ) {
192 // In this function the name is not adjusted by prefixing jetpack_
193 // so if it has already prefixed, we'll replace it and then
194 // check if the option name is a network option or not
195 $jetpack_name = preg_replace( '/^jetpack_/', '', $name, 1 );
196 $is_network_option = self::is_network_option( $jetpack_name );
197 $value = $is_network_option ? get_site_option( $name ) : get_option( $name );
198
199 if ( false === $value && false !== $default ) {
200 if ( $is_network_option ) {
201 add_site_option( $name, $default );
202 } else {
203 add_option( $name, $default );
204 }
205 $value = $default;
206 }
207
208 return $value;
209 }
210
211 private static function update_grouped_option( $group, $name, $value ) {
212 $options = get_option( self::$grouped_options[ $group ] );
213 if ( ! is_array( $options ) ) {
214 $options = array();
215 }
216 $options[ $name ] = $value;
217
218 return update_option( self::$grouped_options[ $group ], $options );
219 }
220
221 /**
222 * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate.
223 *
224 * @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name.
225 * @param mixed $value Option value
226 * @param string $autoload If not compact option, allows specifying whether to autoload or not.
227 *
228 * @return bool Was the option successfully updated?
229 */
230 public static function update_option( $name, $value, $autoload = null ) {
231 /**
232 * Fires before Jetpack updates a specific option.
233 *
234 * @since 3.0.0
235 *
236 * @param str $name The name of the option being updated.
237 * @param mixed $value The new value of the option.
238 */
239 do_action( 'pre_update_jetpack_option_' . $name, $name, $value );
240 if ( self::is_valid( $name, 'non_compact' ) ) {
241 if ( self::is_network_option( $name ) ) {
242 return update_site_option( "jetpack_$name", $value );
243 }
244
245 return update_option( "jetpack_$name", $value, $autoload );
246
247 }
248
249 foreach ( array_keys( self::$grouped_options ) as $group ) {
250 if ( self::is_valid( $name, $group ) ) {
251 return self::update_grouped_option( $group, $name, $value );
252 }
253 }
254
255 trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
256
257 return false;
258 }
259
260 /**
261 * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate.
262 *
263 * @param array $array array( option name => option value, ... )
264 */
265 public static function update_options( $array ) {
266 $names = array_keys( $array );
267
268 foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ), self::get_option_names( 'private' ) ) as $unknown_name ) {
269 trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING );
270 unset( $array[ $unknown_name ] );
271 }
272
273 foreach ( $names as $name ) {
274 self::update_option( $name, $array[ $name ] );
275 }
276 }
277
278 /**
279 * Deletes the given option. May be passed multiple option names as an array.
280 * Updates jetpack_options and/or deletes jetpack_$name as appropriate.
281 *
282 * @param string|array $names Option names. They must come _without_ `jetpack_%` prefix. The method will prefix the option names.
283 *
284 * @return bool Was the option successfully deleted?
285 */
286 public static function delete_option( $names ) {
287 $result = true;
288 $names = (array) $names;
289
290 if ( ! self::is_valid( $names ) ) {
291 trigger_error( sprintf( 'Invalid Jetpack option names: %s', print_r( $names, 1 ) ), E_USER_WARNING );
292 return false;
293 }
294
295 foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) {
296 if ( self::is_network_option( $name ) ) {
297 $result = delete_site_option( "jetpack_$name" );
298 } else {
299 $result = delete_option( "jetpack_$name" );
300 }
301
302 }
303
304 foreach ( array_keys( self::$grouped_options ) as $group ) {
305 if ( ! self::delete_grouped_option( $group, $names ) ) {
306 $result = false;
307 }
308 }
309
310 return $result;
311 }
312
313 private static function get_grouped_option( $group, $name, $default ) {
314 $options = get_option( self::$grouped_options[ $group ] );
315 if ( is_array( $options ) && isset( $options[ $name ] ) ) {
316 return $options[ $name ];
317 }
318
319 return $default;
320 }
321
322 private static function delete_grouped_option( $group, $names ) {
323 $options = get_option( self::$grouped_options[ $group ], array() );
324
325 $to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) );
326 if ( $to_delete ) {
327 foreach ( $to_delete as $name ) {
328 unset( $options[ $name ] );
329 }
330
331 return update_option( self::$grouped_options[ $group ], $options );
332 }
333
334 return true;
335 }
336
337 // Raw option methods allow Jetpack to get / update / delete options via direct DB queries, including options
338 // that are not created by the Jetpack plugin. This is helpful only in rare cases when we need to bypass
339 // cache and filters.
340
341 /**
342 * Deletes an option via $wpdb query.
343 *
344 * @param string $name Option name.
345 *
346 * @return bool Is the option deleted?
347 */
348 static function delete_raw_option( $name ) {
349 if ( self::bypass_raw_option( $name ) ) {
350 return delete_option( $name );
351 }
352 global $wpdb;
353 $result = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->options WHERE option_name = %s", $name ) );
354 return $result;
355 }
356
357 /**
358 * Updates an option via $wpdb query.
359 *
360 * @param string $name Option name.
361 * @param mixed $value Option value.
362 * @param bool $autoload Specifying whether to autoload or not.
363 *
364 * @return bool Is the option updated?
365 */
366 static function update_raw_option( $name, $value, $autoload = false ) {
367 if ( self::bypass_raw_option( $name ) ) {
368 return update_option( $name, $value, $autoload );
369 }
370 global $wpdb;
371 $autoload_value = $autoload ? 'yes' : 'no';
372
373 $old_value = $wpdb->get_var(
374 $wpdb->prepare(
375 "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
376 $name
377 )
378 );
379 if ( $old_value === $value ) {
380 return false;
381 }
382
383 $serialized_value = maybe_serialize( $value );
384 // below we used "insert ignore" to at least suppress the resulting error
385 $updated_num = $wpdb->query(
386 $wpdb->prepare(
387 "UPDATE $wpdb->options SET option_value = %s WHERE option_name = %s",
388 $serialized_value,
389 $name
390 )
391 );
392
393 // Try inserting the option if the value doesn't exits.
394 if ( ! $updated_num ) {
395 $updated_num = $wpdb->query(
396 $wpdb->prepare(
397 "INSERT IGNORE INTO $wpdb->options ( option_name, option_value, autoload ) VALUES ( %s, %s, '$autoload_value' )",
398 $name,
399 $serialized_value
400 )
401 );
402 }
403 return (bool) $updated_num;
404 }
405
406 /**
407 * Gets an option via $wpdb query.
408 *
409 * @since 5.4.0
410 *
411 * @param string $name Option name.
412 * @param mixed $default Default option value if option is not found.
413 *
414 * @return mixed Option value, or null if option is not found and default is not specified.
415 */
416 static function get_raw_option( $name, $default = null ) {
417 if ( self::bypass_raw_option( $name ) ) {
418 return get_option( $name, $default );
419 }
420
421 global $wpdb;
422 $value = $wpdb->get_var(
423 $wpdb->prepare(
424 "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
425 $name
426 )
427 );
428 $value = maybe_unserialize( $value );
429
430 if ( $value === null && $default !== null ) {
431 return $default;
432 }
433
434 return $value;
435 }
436
437 /**
438 * 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.
439 * Options can be selectively managed via a blacklist by filtering option names via the jetpack_disabled_raw_option filter.
440 *
441 * @param $name Option name
442 *
443 * @return bool
444 */
445 static function bypass_raw_option( $name ) {
446
447 if ( Jetpack_Constants::get_constant( 'JETPACK_DISABLE_RAW_OPTIONS' ) ) {
448 return true;
449 }
450 /**
451 * Allows to disable particular raw options.
452 * @since 5.5.0
453 *
454 * @param array $disabled_raw_options An array of option names that you can selectively blacklist from being managed via direct database queries.
455 */
456 $disabled_raw_options = apply_filters( 'jetpack_disabled_raw_options', array() );
457 return isset( $disabled_raw_options[ $name ] );
458 }
459
460 /**
461 * Gets all known options that are used by Jetpack and managed by Jetpack_Options.
462 *
463 * @since 5.4.0
464 *
465 * @param boolean $strip_unsafe_options If true, and by default, will strip out options necessary for the connection to WordPress.com.
466 * @return array An array of all options managed via the Jetpack_Options class.
467 */
468 static function get_all_jetpack_options( $strip_unsafe_options = true ) {
469 $jetpack_options = self::get_option_names();
470 $jetpack_options_non_compat = self::get_option_names( 'non_compact' );
471 $jetpack_options_private = self::get_option_names( 'private' );
472
473 $all_jp_options = array_merge( $jetpack_options, $jetpack_options_non_compat, $jetpack_options_private );
474
475 if ( $strip_unsafe_options ) {
476 // Flag some Jetpack options as unsafe
477 $unsafe_options = array(
478 'id', // (int) The Client ID/WP.com Blog ID of this site.
479 'master_user', // (int) The local User ID of the user who connected this site to jetpack.wordpress.com.
480 'version', // (string) Used during upgrade procedure to auto-activate new modules. version:time
481 'jumpstart', // (string) A flag for whether or not to show the Jump Start. Accepts: new_connection, jumpstart_activated, jumpstart_dismissed.
482
483 // non_compact
484 'activated',
485
486 // private
487 'register',
488 'blog_token', // (string) The Client Secret/Blog Token of this site.
489 'user_token', // (string) The User Token of this site. (deprecated)
490 'user_tokens'
491 );
492
493 // Remove the unsafe Jetpack options
494 foreach ( $unsafe_options as $unsafe_option ) {
495 if ( false !== ( $key = array_search( $unsafe_option, $all_jp_options ) ) ) {
496 unset( $all_jp_options[ $key ] );
497 }
498 }
499 }
500
501 return $all_jp_options;
502 }
503
504 /**
505 * Get all options that are not managed by the Jetpack_Options class that are used by Jetpack.
506 *
507 * @since 5.4.0
508 *
509 * @return array
510 */
511 static function get_all_wp_options() {
512 // A manual build of the wp options
513 return array(
514 'sharing-options',
515 'disabled_likes',
516 'disabled_reblogs',
517 'jetpack_comments_likes_enabled',
518 'wp_mobile_excerpt',
519 'wp_mobile_featured_images',
520 'wp_mobile_app_promos',
521 'stats_options',
522 'stats_dashboard_widget',
523 'safecss_preview_rev',
524 'safecss_rev',
525 'safecss_revision_migrated',
526 'nova_menu_order',
527 'jetpack_portfolio',
528 'jetpack_portfolio_posts_per_page',
529 'jetpack_testimonial',
530 'jetpack_testimonial_posts_per_page',
531 'wp_mobile_custom_css',
532 'sharedaddy_disable_resources',
533 'sharing-options',
534 'sharing-services',
535 'site_icon_temp_data',
536 'featured-content',
537 'site_logo',
538 'jetpack_dismissed_notices',
539 'jetpack-twitter-cards-site-tag',
540 'jetpack-sitemap-state',
541 'jetpack_sitemap_post_types',
542 'jetpack_sitemap_location',
543 'jetpack_protect_key',
544 'jetpack_protect_blocked_attempts',
545 'jetpack_protect_activating',
546 'jetpack_connection_banner_ab',
547 'jetpack_active_plan',
548 'jetpack_activation_source',
549 'jetpack_sso_match_by_email',
550 'jetpack_sso_require_two_step',
551 'jetpack_sso_remove_login_form',
552 'jetpack_last_connect_url_check',
553 'jpo_business_address',
554 'jpo_site_type',
555 'jpo_homepage_format',
556 'jpo_contact_page',
557 'jetpack_excluded_extensions',
558 );
559 }
560
561 /**
562 * Gets all options that can be safely reset by CLI.
563 *
564 * @since 5.4.0
565 *
566 * @return array array Associative array containing jp_options which are managed by the Jetpack_Options class and wp_options which are not.
567 */
568 static function get_options_for_reset() {
569 $all_jp_options = self::get_all_jetpack_options();
570
571 $wp_options = self::get_all_wp_options();
572
573 $options = array(
574 'jp_options' => $all_jp_options,
575 'wp_options' => $wp_options
576 );
577
578 return $options;
579 }
580
581 /**
582 * Delete all known options
583 *
584 * @since 5.4.0
585 *
586 * @return void
587 */
588 static function delete_all_known_options() {
589 // Delete all compact options
590 foreach ( (array) self::$grouped_options as $option_name ) {
591 delete_option( $option_name );
592 }
593
594 // Delete all non-compact Jetpack options
595 foreach ( (array) self::get_option_names( 'non-compact' ) as $option_name ) {
596 Jetpack_Options::delete_option( $option_name );
597 }
598
599 // Delete all options that can be reset via CLI, that aren't Jetpack options
600 foreach ( (array) self::get_all_wp_options() as $option_name ) {
601 delete_option( $option_name );
602 }
603 }
604 }
605