Jetpack_Options

The Jetpack by WordPress.com Jetpack Options class.

Defined (1)

The class is defined in the following location(s).

/class.jetpack-options.php  
  1. class Jetpack_Options { 
  2.  
  3. private static $grouped_options = array( 
  4. 'compact' => 'jetpack_options',  
  5. 'private' => 'jetpack_private_options' 
  6. ); 
  7.  
  8. public static function get_option_names( $type = 'compact' ) { 
  9. switch ( $type ) { 
  10. case 'non-compact' : 
  11. case 'non_compact' : 
  12. return array( 
  13. 'activated',  
  14. 'active_modules',  
  15. 'available_modules',  
  16. 'do_activate',  
  17. 'log',  
  18. 'publicize',  
  19. 'slideshow_background_color',  
  20. 'widget_twitter',  
  21. 'wpcc_options',  
  22. 'relatedposts',  
  23. 'file_data',  
  24. 'security_report',  
  25. 'autoupdate_plugins', // (array) An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated 
  26. 'autoupdate_themes', // (array) An array of theme ids ( eg. twentyfourteen ) that should be autoupdated 
  27. 'autoupdate_core', // (bool) Whether or not to autoupdate core 
  28. 'json_api_full_management', // (bool) Allow full management (eg. Activate, Upgrade plugins) of the site via the JSON API. 
  29. 'sync_non_public_post_stati', // (bool) Allow synchronisation of posts and pages with non-public status. 
  30. 'site_icon_url', // (string) url to the full site icon 
  31. 'site_icon_id', // (int) Attachment id of the site icon file 
  32. 'dismissed_manage_banner', // (bool) Dismiss Jetpack manage banner allows the user to dismiss the banner permanently 
  33. 'restapi_stats_cache', // (array) Stats Cache data. 
  34. '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 
  35. 'protect_whitelist' // (array) IP Address for the Protect module to ignore 
  36. ); 
  37.  
  38. case 'private' : 
  39. return array( 
  40. 'register',  
  41. 'blog_token', // (string) The Client Secret/Blog Token of this site. 
  42. 'user_token', // (string) The User Token of this site. (deprecated) 
  43. 'user_tokens' // (array) User Tokens for each user of this site who has connected to jetpack.wordpress.com. 
  44. ); 
  45.  
  46. return array( 
  47. 'id', // (int) The Client ID/WP.com Blog ID of this site. 
  48. 'publicize_connections', // (array) An array of Publicize connections from WordPress.com 
  49. 'master_user', // (int) The local User ID of the user who connected this site to jetpack.wordpress.com. 
  50. 'version', // (string) Used during upgrade procedure to auto-activate new modules. version:time 
  51. 'old_version', // (string) Used to determine which modules are the most recently added. previous_version:time 
  52. 'fallback_no_verify_ssl_certs', // (int) Flag for determining if this host must skip SSL Certificate verification due to misconfigured SSL. 
  53. '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' ) 
  54. '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. 
  55. 'videopress', // (array) VideoPress options array. 
  56. '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. 
  57. 'social_links', // (array) The specified links for each social networking site. 
  58. 'identity_crisis_whitelist', // (array) An array of options, each having an array of the values whitelisted for it. 
  59. 'gplus_authors', // (array) The Google+ authorship information for connected users. 
  60. 'last_heartbeat', // (int) The timestamp of the last heartbeat that fired. 
  61. 'last_security_report', // (int) The timestamp of the last security report that was run. 
  62. 'sync_bulk_reindexing', // (bool) If a bulk reindex is currently underway. 
  63. 'jumpstart', // (string) A flag for whether or not to show the Jump Start. Accepts: new_connection, jumpstart_activated, jetpack_action_taken, jumpstart_dismissed. 
  64. 'hide_jitm' // (array) A list of just in time messages that we should not show because they have been dismissed by the user 
  65. ); 
  66.  
  67. public static function is_valid( $name, $group = null ) { 
  68. if ( is_array( $name ) ) { 
  69. $compact_names = array(); 
  70. foreach ( array_keys( self::$grouped_options ) as $_group ) { 
  71. $compact_names = array_merge( $compact_names, self::get_option_names( $_group ) ); 
  72.  
  73. $result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names ); 
  74.  
  75. return empty( $result ); 
  76.  
  77. if ( is_null( $group ) || 'non_compact' === $group ) { 
  78. if ( in_array( $name, self::get_option_names( $group ) ) ) { 
  79. return true; 
  80.  
  81. foreach ( array_keys( self::$grouped_options ) as $_group ) { 
  82. if ( is_null( $group ) || $group === $_group ) { 
  83. if ( in_array( $name, self::get_option_names( $_group ) ) ) { 
  84. return true; 
  85.  
  86. return false; 
  87.  
  88. /** 
  89. * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. 
  90. * @param string $name Option name 
  91. * @param mixed $default (optional) 
  92. */ 
  93. public static function get_option( $name, $default = false ) { 
  94. if ( self::is_valid( $name, 'non_compact' ) ) { 
  95. return get_option( "jetpack_$name", $default ); 
  96.  
  97. foreach ( array_keys( self::$grouped_options ) as $group ) { 
  98. if ( self::is_valid( $name, $group ) ) { 
  99. return self::get_grouped_option( $group, $name, $default ); 
  100.  
  101. trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING ); 
  102.  
  103. return $default; 
  104.  
  105. private static function update_grouped_option( $group, $name, $value ) { 
  106. $options = get_option( self::$grouped_options[ $group ] ); 
  107. if ( ! is_array( $options ) ) { 
  108. $options = array(); 
  109. $options[ $name ] = $value; 
  110.  
  111. return update_option( self::$grouped_options[ $group ], $options ); 
  112.  
  113. /** 
  114. * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. 
  115. * @param string $name Option name 
  116. * @param mixed $value Option value 
  117. * @param string $autoload If not compact option, allows specifying whether to autoload or not. 
  118. */ 
  119. public static function update_option( $name, $value, $autoload = null ) { 
  120.  
  121. /** 
  122. * Fires before Jetpack updates a specific option. 
  123. * @since 3.0.0 
  124. * @param str $name The name of the option being updated. 
  125. * @param mixed $value The new value of the option. 
  126. */ 
  127. do_action( 'pre_update_jetpack_option_' . $name, $name, $value ); 
  128. if ( self::is_valid( $name, 'non_compact' ) ) { 
  129. /** 
  130. * Allowing update_option to change autoload status only shipped in WordPress v4.2 
  131. * @link https://github.com/WordPress/WordPress/commit/305cf8b95 
  132. */ 
  133. if ( version_compare( $GLOBALS['wp_version'], '4.2', '>=' ) ) { 
  134. return update_option( "jetpack_$name", $value, $autoload ); 
  135. return update_option( "jetpack_$name", $value ); 
  136.  
  137. foreach ( array_keys( self::$grouped_options ) as $group ) { 
  138. if ( self::is_valid( $name, $group ) ) { 
  139. return self::update_grouped_option( $group, $name, $value ); 
  140.  
  141. trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING ); 
  142.  
  143. return false; 
  144.  
  145. /** 
  146. * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. 
  147. * @param array $array array( option name => option value, ... ) 
  148. */ 
  149. public static function update_options( $array ) { 
  150. $names = array_keys( $array ); 
  151.  
  152. foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ), self::get_option_names( 'private' ) ) as $unknown_name ) { 
  153. trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING ); 
  154. unset( $array[ $unknown_name ] ); 
  155.  
  156. foreach ( $names as $name ) { 
  157. self::update_option( $name, $array[ $name ] ); 
  158.  
  159. /** 
  160. * Deletes the given option. May be passed multiple option names as an array. 
  161. * Updates jetpack_options and/or deletes jetpack_$name as appropriate. 
  162. * @param string|array $names 
  163. */ 
  164. public static function delete_option( $names ) { 
  165. $result = true; 
  166. $names = (array) $names; 
  167.  
  168. if ( ! self::is_valid( $names ) ) { 
  169. trigger_error( sprintf( 'Invalid Jetpack option names: %s', print_r( $names, 1 ) ), E_USER_WARNING ); 
  170.  
  171. return false; 
  172.  
  173. foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) { 
  174. if ( ! delete_option( "jetpack_$name" ) ) { 
  175. $result = false; 
  176.  
  177. foreach ( array_keys( self::$grouped_options ) as $group ) { 
  178. if ( ! self::delete_grouped_option( $group, $names ) ) { 
  179. $result = false; 
  180.  
  181. return $result; 
  182.  
  183. private static function get_grouped_option( $group, $name, $default ) { 
  184. $options = get_option( self::$grouped_options[ $group ] ); 
  185. if ( is_array( $options ) && isset( $options[ $name ] ) ) { 
  186. return $options[ $name ]; 
  187.  
  188. return $default; 
  189.  
  190. private static function delete_grouped_option( $group, $names ) { 
  191. $options = get_option( self::$grouped_options[ $group ], array() ); 
  192.  
  193. $to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) ); 
  194. if ( $to_delete ) { 
  195. foreach ( $to_delete as $name ) { 
  196. unset( $options[ $name ] ); 
  197.  
  198. return update_option( self::$grouped_options[ $group ], $options ); 
  199.  
  200. return true; 
  201.