PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Gashler/sg
PHP | 236 lines | 171 code | 37 blank | 28 comment | 20 complexity | 49fa082df2803ef53c6a57c6d0d2af4a 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. 'security_report',
  24. 'autoupdate_plugins', // (array) An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated
  25. 'autoupdate_themes', // (array) An array of theme ids ( eg. twentyfourteen ) that should be autoupdated
  26. 'autoupdate_core', // (bool) Whether or not to autoupdate core
  27. 'json_api_full_management', // (bool) Allow full management (eg. Activate, Upgrade plugins) of the site via the JSON API.
  28. 'sync_non_public_post_stati', // (bool) Allow synchronisation of posts and pages with non-public status.
  29. 'site_icon_url', // (string) url to the full site icon
  30. 'site_icon_id', // (int) Attachment id of the site icon file
  31. 'dismissed_manage_banner', // (bool) Dismiss Jetpack manage banner allows the user to dismiss the banner permanently
  32. 'updates', // (array) information about available updates to plugins, theme, WordPress core, and if site is under version control
  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. case 'private' :
  38. return array(
  39. 'register',
  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. 'last_security_report', // (int) The timestamp of the last security report that was run.
  61. 'sync_bulk_reindexing', // (bool) If a bulk reindex is currently underway.
  62. 'jumpstart' // (string) A flag for whether or not to show the Jump Start. Accepts: new_connection, jumpstart_activated, jetpack_action_taken, jumpstart_dismissed.
  63. );
  64. }
  65. public static function is_valid( $name, $group = null ) {
  66. if ( is_array( $name ) ) {
  67. $compact_names = array();
  68. foreach ( array_keys( self::$grouped_options ) as $_group ) {
  69. $compact_names = array_merge( $compact_names, self::get_option_names( $_group ) );
  70. }
  71. $result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names );
  72. return empty( $result );
  73. }
  74. if ( is_null( $group ) || 'non_compact' === $group ) {
  75. if ( in_array( $name, self::get_option_names( $group ) ) ) {
  76. return true;
  77. }
  78. }
  79. foreach ( array_keys( self::$grouped_options ) as $_group ) {
  80. if ( is_null( $group ) || $group === $_group ) {
  81. if ( in_array( $name, self::get_option_names( $_group ) ) ) {
  82. return true;
  83. }
  84. }
  85. }
  86. return false;
  87. }
  88. /**
  89. * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate.
  90. *
  91. * @param string $name Option name
  92. * @param mixed $default (optional)
  93. */
  94. public static function get_option( $name, $default = false ) {
  95. if ( self::is_valid( $name, 'non_compact' ) ) {
  96. return get_option( "jetpack_$name", $default );
  97. }
  98. foreach ( array_keys( self::$grouped_options ) as $group ) {
  99. if ( self::is_valid( $name, $group ) ) {
  100. return self::get_grouped_option( $group, $name, $default );
  101. }
  102. }
  103. trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
  104. return $default;
  105. }
  106. private static function update_grouped_option( $group, $name, $value ) {
  107. $options = get_option( self::$grouped_options[ $group ] );
  108. if ( ! is_array( $options ) ) {
  109. $options = array();
  110. }
  111. $options[ $name ] = $value;
  112. return update_option( self::$grouped_options[ $group ], $options );
  113. }
  114. /**
  115. * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate.
  116. *
  117. * @param string $name Option name
  118. * @param mixed $value Option value
  119. * @param string $autoload If not compact option, allows specifying whether to autoload or not.
  120. */
  121. public static function update_option( $name, $value, $autoload = null ) {
  122. do_action( 'pre_update_jetpack_option_' . $name, $name, $value );
  123. if ( self::is_valid( $name, 'non_compact' ) ) {
  124. /**
  125. * Allowing update_option to change autoload status only shipped in WordPress v4.2
  126. * @link https://github.com/WordPress/WordPress/commit/305cf8b95
  127. */
  128. if ( version_compare( $GLOBALS['wp_version'], '4.2', '>=' ) ) {
  129. return update_option( "jetpack_$name", $value, $autoload );
  130. }
  131. return update_option( "jetpack_$name", $value );
  132. }
  133. foreach ( array_keys( self::$grouped_options ) as $group ) {
  134. if ( self::is_valid( $name, $group ) ) {
  135. return self::update_grouped_option( $group, $name, $value );
  136. }
  137. }
  138. trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
  139. return false;
  140. }
  141. /**
  142. * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate.
  143. *
  144. * @param array $array array( option name => option value, ... )
  145. */
  146. public static function update_options( $array ) {
  147. $names = array_keys( $array );
  148. foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ), self::get_option_names( 'private' ) ) as $unknown_name ) {
  149. trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING );
  150. unset( $array[ $unknown_name ] );
  151. }
  152. foreach ( $names as $name ) {
  153. self::update_option( $name, $array[ $name ] );
  154. }
  155. }
  156. /**
  157. * Deletes the given option. May be passed multiple option names as an array.
  158. * Updates jetpack_options and/or deletes jetpack_$name as appropriate.
  159. *
  160. * @param string|array $names
  161. */
  162. public static function delete_option( $names ) {
  163. $result = true;
  164. $names = (array) $names;
  165. if ( ! self::is_valid( $names ) ) {
  166. trigger_error( sprintf( 'Invalid Jetpack option names: %s', print_r( $names, 1 ) ), E_USER_WARNING );
  167. return false;
  168. }
  169. foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) {
  170. if ( ! delete_option( "jetpack_$name" ) ) {
  171. $result = false;
  172. }
  173. }
  174. foreach ( array_keys( self::$grouped_options ) as $group ) {
  175. if ( ! self::delete_grouped_option( $group, $names ) ) {
  176. $result = false;
  177. }
  178. }
  179. return $result;
  180. }
  181. private static function get_grouped_option( $group, $name, $default ) {
  182. $options = get_option( self::$grouped_options[ $group ] );
  183. if ( is_array( $options ) && isset( $options[ $name ] ) ) {
  184. return $options[ $name ];
  185. }
  186. return $default;
  187. }
  188. private static function delete_grouped_option( $group, $names ) {
  189. $options = get_option( self::$grouped_options[ $group ], array() );
  190. $to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) );
  191. if ( $to_delete ) {
  192. foreach ( $to_delete as $name ) {
  193. unset( $options[ $name ] );
  194. }
  195. return update_option( self::$grouped_options[ $group ], $options );
  196. }
  197. return true;
  198. }
  199. }