class.jetpack-options.php 21 KB

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