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