PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 4.9.3
Jetpack – WP Security, Backup, Speed, & Growth v4.9.3
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / class.jetpack-options.php
class.jetpack-options.php
327 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Jetpack_Options {
4
5 /**
6 * An array that maps a grouped option type to an option name.
7 * @var array
8 */
9 private static $grouped_options = array(
10 'compact' => 'jetpack_options',
11 'private' => 'jetpack_private_options'
12 );
13
14 /**
15 * Returns an array of option names for a given type.
16 *
17 * @param string $type The type of option to return. Defaults to 'compact'.
18 *
19 * @return array
20 */
21 public static function get_option_names( $type = 'compact' ) {
22 switch ( $type ) {
23 case 'non-compact' :
24 case 'non_compact' :
25 return array(
26 'activated',
27 'active_modules',
28 'available_modules',
29 'do_activate',
30 'log',
31 'publicize',
32 'slideshow_background_color',
33 'widget_twitter',
34 'wpcc_options',
35 'relatedposts',
36 'file_data',
37 'autoupdate_plugins', // (array) An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated
38 'autoupdate_plugins_translations', // (array) An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated translation files.
39 'autoupdate_themes', // (array) An array of theme ids ( eg. twentyfourteen ) that should be autoupdated
40 'autoupdate_themes_translations', // (array) An array of theme ids ( eg. twentyfourteen ) that should autoupdated translation files.
41 'autoupdate_core', // (bool) Whether or not to autoupdate core
42 'autoupdate_translations', // (bool) Whether or not to autoupdate all translations
43 'json_api_full_management', // (bool) Allow full management (eg. Activate, Upgrade plugins) of the site via the JSON API.
44 'sync_non_public_post_stati', // (bool) Allow synchronisation of posts and pages with non-public status.
45 'site_icon_url', // (string) url to the full site icon
46 'site_icon_id', // (int) Attachment id of the site icon file
47 'dismissed_manage_banner', // (bool) Dismiss Jetpack manage banner allows the user to dismiss the banner permanently
48 'restapi_stats_cache', // (array) Stats Cache data.
49 '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
50 'protect_whitelist', // (array) IP Address for the Protect module to ignore
51 'sync_error_idc', // (bool|array) false or array containing the site's home and siteurl at time of IDC error
52 'safe_mode_confirmed', // (bool) True if someone confirms that this site was correctly put into safe mode automatically after an identity crisis is discovered.
53 'migrate_for_idc', // (bool) True if someone confirms that this site should migrate stats and subscribers from its previous URL
54 );
55
56 case 'private' :
57 return array(
58 'register',
59 'authorize',
60 'blog_token', // (string) The Client Secret/Blog Token of this site.
61 'user_token', // (string) The User Token of this site. (deprecated)
62 'user_tokens' // (array) User Tokens for each user of this site who has connected to jetpack.wordpress.com.
63 );
64
65 case 'network' :
66 return array(
67 'file_data' // (array) List of absolute paths to all Jetpack modules
68 );
69 }
70
71 return array(
72 'id', // (int) The Client ID/WP.com Blog ID of this site.
73 'publicize_connections', // (array) An array of Publicize connections from WordPress.com
74 'master_user', // (int) The local User ID of the user who connected this site to jetpack.wordpress.com.
75 'version', // (string) Used during upgrade procedure to auto-activate new modules. version:time
76 'old_version', // (string) Used to determine which modules are the most recently added. previous_version:time
77 'fallback_no_verify_ssl_certs', // (int) Flag for determining if this host must skip SSL Certificate verification due to misconfigured SSL.
78 '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' )
79 '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.
80 'videopress', // (array) VideoPress options array.
81 '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.
82 'social_links', // (array) The specified links for each social networking site.
83 'identity_crisis_whitelist', // (array) An array of options, each having an array of the values whitelisted for it.
84 'gplus_authors', // (array) The Google+ authorship information for connected users.
85 'last_heartbeat', // (int) The timestamp of the last heartbeat that fired.
86 'jumpstart', // (string) A flag for whether or not to show the Jump Start. Accepts: new_connection, jumpstart_activated, jetpack_action_taken, jumpstart_dismissed.
87 'hide_jitm', // (array) A list of just in time messages that we should not show because they have been dismissed by the user
88 'custom_css_4.7_migration', // (bool) Whether Custom CSS has scanned for and migrated any legacy CSS CPT entries to the new Core format.
89 );
90 }
91
92 /**
93 * Is the option name valid?
94 *
95 * @param string $name The name of the option
96 * @param string|null $group The name of the group that the option is in. Default to null, which will search non_compact.
97 *
98 * @return bool Is the option name valid?
99 */
100 public static function is_valid( $name, $group = null ) {
101 if ( is_array( $name ) ) {
102 $compact_names = array();
103 foreach ( array_keys( self::$grouped_options ) as $_group ) {
104 $compact_names = array_merge( $compact_names, self::get_option_names( $_group ) );
105 }
106
107 $result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names );
108
109 return empty( $result );
110 }
111
112 if ( is_null( $group ) || 'non_compact' === $group ) {
113 if ( in_array( $name, self::get_option_names( $group ) ) ) {
114 return true;
115 }
116 }
117
118 foreach ( array_keys( self::$grouped_options ) as $_group ) {
119 if ( is_null( $group ) || $group === $_group ) {
120 if ( in_array( $name, self::get_option_names( $_group ) ) ) {
121 return true;
122 }
123 }
124 }
125
126 return false;
127 }
128
129 /**
130 * Checks if an option must be saved for the whole network in WP Multisite
131 *
132 * @param string $option_name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name.
133 *
134 * @return bool
135 */
136 public static function is_network_option( $option_name ) {
137 if ( ! is_multisite() ) {
138 return false;
139 }
140 return in_array( $option_name, self::get_option_names( 'network' ) );
141 }
142
143 /**
144 * Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate.
145 *
146 * @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name.
147 * @param mixed $default (optional)
148 *
149 * @return mixed
150 */
151 public static function get_option( $name, $default = false ) {
152 if ( self::is_valid( $name, 'non_compact' ) ) {
153 if ( self::is_network_option( $name ) ) {
154 return get_site_option( "jetpack_$name", $default );
155 }
156
157 return get_option( "jetpack_$name", $default );
158 }
159
160 foreach ( array_keys( self::$grouped_options ) as $group ) {
161 if ( self::is_valid( $name, $group ) ) {
162 return self::get_grouped_option( $group, $name, $default );
163 }
164 }
165
166 trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
167
168 return $default;
169 }
170
171 /**
172 * Returns the requested option, and ensures it's autoloaded in the future.
173 * This does _not_ adjust the prefix in any way (does not prefix jetpack_%)
174 *
175 * @param string $name Option name
176 * @param mixed $default (optional)
177 *
178 * @return mixed
179 */
180 public static function get_option_and_ensure_autoload( $name, $default ) {
181 // In this function the name is not adjusted by prefixing jetpack_
182 // so if it has already prefixed, we'll replace it and then
183 // check if the option name is a network option or not
184 $jetpack_name = preg_replace( '/^jetpack_/', '', $name, 1 );
185 $is_network_option = self::is_network_option( $jetpack_name );
186 $value = $is_network_option ? get_site_option( $name ) : get_option( $name );
187
188 if ( $value === false && $default !== false ) {
189 if ( $is_network_option ) {
190 update_site_option( $name, $default );
191 } else {
192 update_option( $name, $default );
193 }
194 $value = $default;
195 }
196
197 return $value;
198 }
199
200 private static function update_grouped_option( $group, $name, $value ) {
201 $options = get_option( self::$grouped_options[ $group ] );
202 if ( ! is_array( $options ) ) {
203 $options = array();
204 }
205 $options[ $name ] = $value;
206
207 return update_option( self::$grouped_options[ $group ], $options );
208 }
209
210 /**
211 * Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate.
212 *
213 * @param string $name Option name. It must come _without_ `jetpack_%` prefix. The method will prefix the option name.
214 * @param mixed $value Option value
215 * @param string $autoload If not compact option, allows specifying whether to autoload or not.
216 *
217 * @return bool Was the option successfully updated?
218 */
219 public static function update_option( $name, $value, $autoload = null ) {
220 /**
221 * Fires before Jetpack updates a specific option.
222 *
223 * @since 3.0.0
224 *
225 * @param str $name The name of the option being updated.
226 * @param mixed $value The new value of the option.
227 */
228 do_action( 'pre_update_jetpack_option_' . $name, $name, $value );
229 if ( self::is_valid( $name, 'non_compact' ) ) {
230 if ( self::is_network_option( $name ) ) {
231 return update_site_option( "jetpack_$name", $value );
232 }
233
234 return update_option( "jetpack_$name", $value, $autoload );
235
236 }
237
238 foreach ( array_keys( self::$grouped_options ) as $group ) {
239 if ( self::is_valid( $name, $group ) ) {
240 return self::update_grouped_option( $group, $name, $value );
241 }
242 }
243
244 trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
245
246 return false;
247 }
248
249 /**
250 * Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate.
251 *
252 * @param array $array array( option name => option value, ... )
253 */
254 public static function update_options( $array ) {
255 $names = array_keys( $array );
256
257 foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ), self::get_option_names( 'private' ) ) as $unknown_name ) {
258 trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING );
259 unset( $array[ $unknown_name ] );
260 }
261
262 foreach ( $names as $name ) {
263 self::update_option( $name, $array[ $name ] );
264 }
265 }
266
267 /**
268 * Deletes the given option. May be passed multiple option names as an array.
269 * Updates jetpack_options and/or deletes jetpack_$name as appropriate.
270 *
271 * @param string|array $names Option names. They must come _without_ `jetpack_%` prefix. The method will prefix the option names.
272 *
273 * @return bool Was the option successfully deleted?
274 */
275 public static function delete_option( $names ) {
276 $result = true;
277 $names = (array) $names;
278
279 if ( ! self::is_valid( $names ) ) {
280 trigger_error( sprintf( 'Invalid Jetpack option names: %s', print_r( $names, 1 ) ), E_USER_WARNING );
281 return false;
282 }
283
284 foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) {
285 if ( self::is_network_option( $name ) ) {
286 $result = delete_site_option( "jetpack_$name" );
287 } else {
288 $result = delete_option( "jetpack_$name" );
289 }
290
291 }
292
293 foreach ( array_keys( self::$grouped_options ) as $group ) {
294 if ( ! self::delete_grouped_option( $group, $names ) ) {
295 $result = false;
296 }
297 }
298
299 return $result;
300 }
301
302 private static function get_grouped_option( $group, $name, $default ) {
303 $options = get_option( self::$grouped_options[ $group ] );
304 if ( is_array( $options ) && isset( $options[ $name ] ) ) {
305 return $options[ $name ];
306 }
307
308 return $default;
309 }
310
311 private static function delete_grouped_option( $group, $names ) {
312 $options = get_option( self::$grouped_options[ $group ], array() );
313
314 $to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) );
315 if ( $to_delete ) {
316 foreach ( $to_delete as $name ) {
317 unset( $options[ $name ] );
318 }
319
320 return update_option( self::$grouped_options[ $group ], $options );
321 }
322
323 return true;
324 }
325
326 }
327