Completed
Push — add/feature-rollout ( 16c199...2484fa )
by
unknown
10:53
created

class.jetpack-options.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
				'feature_rollout',             // (array) An array of features that are enabled or disabled.
55
			);
56
57
		case 'private' :
58
			return array(
59
				'register',
60
				'authorize',
61
				'activate_manage',
62
				'blog_token',                  // (string) The Client Secret/Blog Token of this site.
63
				'user_token',                  // (string) The User Token of this site. (deprecated)
64
				'user_tokens'                  // (array)  User Tokens for each user of this site who has connected to jetpack.wordpress.com.
65
			);
66
		}
67
68
		return array(
69
			'id',                           // (int)    The Client ID/WP.com Blog ID of this site.
70
			'publicize_connections',        // (array)  An array of Publicize connections from WordPress.com
71
			'master_user',                  // (int)    The local User ID of the user who connected this site to jetpack.wordpress.com.
72
			'version',                      // (string) Used during upgrade procedure to auto-activate new modules. version:time
73
			'old_version',                  // (string) Used to determine which modules are the most recently added. previous_version:time
74
			'fallback_no_verify_ssl_certs', // (int)    Flag for determining if this host must skip SSL Certificate verification due to misconfigured SSL.
75
			'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' )
76
			'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.
77
			'videopress',                   // (array)  VideoPress options array.
78
			'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.
79
			'social_links',                 // (array)  The specified links for each social networking site.
80
			'identity_crisis_whitelist',    // (array)  An array of options, each having an array of the values whitelisted for it.
81
			'gplus_authors',                // (array)  The Google+ authorship information for connected users.
82
			'last_heartbeat',               // (int)    The timestamp of the last heartbeat that fired.
83
			'jumpstart',                    // (string) A flag for whether or not to show the Jump Start.  Accepts: new_connection, jumpstart_activated, jetpack_action_taken, jumpstart_dismissed.
84
			'hide_jitm',                    // (array)  A list of just in time messages that we should not show because they have been dismissed by the user
85
			'custom_css_4.7_migration',     // (bool)   Whether Custom CSS has scanned for and migrated any legacy CSS CPT entries to the new Core format.
86
		);
87
	}
88
89
	/**
90
	 * Is the option name valid?
91
	 *
92
	 * @param string      $name  The name of the option
93
	 * @param string|null $group The name of the group that the option is in. Default to null, which will search non_compact.
94
	 *
95
	 * @return bool Is the option name valid?
96
	 */
97
	public static function is_valid( $name, $group = null ) {
98
		if ( is_array( $name ) ) {
99
			$compact_names = array();
100
			foreach ( array_keys( self::$grouped_options ) as $_group ) {
101
				$compact_names = array_merge( $compact_names, self::get_option_names( $_group ) );
102
			}
103
104
			$result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names );
105
106
			return empty( $result );
107
		}
108
109 View Code Duplication
		if ( is_null( $group ) || 'non_compact' === $group ) {
110
			if ( in_array( $name, self::get_option_names( $group ) ) ) {
111
				return true;
112
			}
113
		}
114
115
		foreach ( array_keys( self::$grouped_options ) as $_group ) {
116 View Code Duplication
			if ( is_null( $group ) || $group === $_group ) {
117
				if ( in_array( $name, self::get_option_names( $_group ) ) ) {
118
					return true;
119
				}
120
			}
121
		}
122
123
		return false;
124
	}
125
126
	/**
127
	 * Returns the requested option.  Looks in jetpack_options or jetpack_$name as appropriate.
128
	 *
129
	 * @param string $name Option name
130
	 * @param mixed $default (optional)
131
	 *
132
	 * @return mixed
133
	 */
134
	public static function get_option( $name, $default = false ) {
135
		if ( self::is_valid( $name, 'non_compact' ) ) {
136
			return get_option( "jetpack_$name", $default );
137
		}
138
139
		foreach ( array_keys( self::$grouped_options ) as $group ) {
140
			if ( self::is_valid( $name, $group ) ) {
141
				return self::get_grouped_option( $group, $name, $default );
142
			}
143
		}
144
145
		trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
146
147
		return $default;
148
	}
149
150
	/**
151
	 * Returns the requested option, and ensures it's autoloaded in the future.
152
	 * This does _not_ adjust the prefix in any way (does not prefix jetpack_%)
153
	 *
154
	 * @param string $name Option name
155
	 * @param mixed $default (optional)
156
	 *
157
	 * @return mixed|void
158
	 */
159
	public static function get_option_and_ensure_autoload( $name, $default ) {
160
		$value = get_option( $name );
161
162
		if ( $value === false && $default !== false ) {
163
			update_option( $name, $default );
164
			$value = $default;
165
		}
166
167
		return $value;
168
	}
169
170
	private static function update_grouped_option( $group, $name, $value ) {
171
		$options = get_option( self::$grouped_options[ $group ] );
172
		if ( ! is_array( $options ) ) {
173
			$options = array();
174
		}
175
		$options[ $name ] = $value;
176
177
		return update_option( self::$grouped_options[ $group ], $options );
178
	}
179
180
	/**
181
	 * Updates the single given option.  Updates jetpack_options or jetpack_$name as appropriate.
182
	 *
183
	 * @param string $name Option name
184
	 * @param mixed $value Option value
185
	 * @param string $autoload If not compact option, allows specifying whether to autoload or not.
186
	 *
187
	 * @return bool Was the option successfully updated?
188
	 */
189
	public static function update_option( $name, $value, $autoload = null ) {
190
		/**
191
		 * Fires before Jetpack updates a specific option.
192
		 *
193
		 * @since 3.0.0
194
		 *
195
		 * @param str $name The name of the option being updated.
196
		 * @param mixed $value The new value of the option.
197
		 */
198
		do_action( 'pre_update_jetpack_option_' . $name, $name, $value );
199
		if ( self::is_valid( $name, 'non_compact' ) ) {
200
			return update_option( "jetpack_$name", $value, $autoload );
201
		}
202
203
		foreach ( array_keys( self::$grouped_options ) as $group ) {
204
			if ( self::is_valid( $name, $group ) ) {
205
				return self::update_grouped_option( $group, $name, $value );
206
			}
207
		}
208
209
		trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
210
211
		return false;
212
	}
213
214
	/**
215
	 * Updates the multiple given options.  Updates jetpack_options and/or jetpack_$name as appropriate.
216
	 *
217
	 * @param array $array array( option name => option value, ... )
218
	 */
219
	public static function update_options( $array ) {
220
		$names = array_keys( $array );
221
222
		foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ), self::get_option_names( 'private' ) ) as $unknown_name ) {
223
			trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING );
224
			unset( $array[ $unknown_name ] );
225
		}
226
227
		foreach ( $names as $name ) {
228
			self::update_option( $name, $array[ $name ] );
229
		}
230
	}
231
232
	/**
233
	 * Deletes the given option.  May be passed multiple option names as an array.
234
	 * Updates jetpack_options and/or deletes jetpack_$name as appropriate.
235
	 *
236
	 * @param string|array $names
237
	 *
238
	 * @return bool Was the option successfully deleted?
239
	 */
240
	public static function delete_option( $names ) {
241
		$result = true;
242
		$names  = (array) $names;
243
244
		if ( ! self::is_valid( $names ) ) {
0 ignored issues
show
$names is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
245
			trigger_error( sprintf( 'Invalid Jetpack option names: %s', print_r( $names, 1 ) ), E_USER_WARNING );
246
			return false;
247
		}
248
249
		foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) {
250
			if ( ! delete_option( "jetpack_$name" ) ) {
251
				$result = false;
252
			}
253
		}
254
255
		foreach ( array_keys( self::$grouped_options ) as $group ) {
256
			if ( ! self::delete_grouped_option( $group, $names ) ) {
257
				$result = false;
258
			}
259
		}
260
261
		return $result;
262
	}
263
264
	private static function get_grouped_option( $group, $name, $default ) {
265
		$options = get_option( self::$grouped_options[ $group ] );
266
		if ( is_array( $options ) && isset( $options[ $name ] ) ) {
267
			return $options[ $name ];
268
		}
269
270
		return $default;
271
	}
272
273
	private static function delete_grouped_option( $group, $names ) {
274
		$options = get_option( self::$grouped_options[ $group ], array() );
275
276
		$to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) );
277
		if ( $to_delete ) {
278
			foreach ( $to_delete as $name ) {
279
				unset( $options[ $name ] );
280
			}
281
282
			return update_option( self::$grouped_options[ $group ], $options );
283
		}
284
285
		return true;
286
	}
287
288
}
289