PageRenderTime 28ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/jetpack/class.jetpack-options.php

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