Completed
Push — branch-4.4 ( bdb832...3609d2 )
by
unknown
49:52 queued 41:57
created

class.jetpack-options.php (3 issues)

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_themes',           // (array)  An array of theme ids ( eg. twentyfourteen ) that should be autoupdated
39
				'autoupdate_core',             // (bool)   Whether or not to autoupdate core
40
				'json_api_full_management',    // (bool)   Allow full management (eg. Activate, Upgrade plugins) of the site via the JSON API.
41
				'sync_non_public_post_stati',  // (bool)   Allow synchronisation of posts and pages with non-public status.
42
				'site_icon_url',               // (string) url to the full site icon
43
				'site_icon_id',                // (int)    Attachment id of the site icon file
44
				'dismissed_manage_banner',     // (bool) Dismiss Jetpack manage banner allows the user to dismiss the banner permanently
45
				'restapi_stats_cache',         // (array) Stats Cache data.
46
				'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
47
				'protect_whitelist',           // (array) IP Address for the Protect module to ignore
48
				'sync_error_idc',              // (bool|array) false or array containing the site's home and siteurl at time of IDC error
49
				'safe_mode_confirmed',         // (bool) True if someone confirms that this site was correctly put into safe mode automatically after an identity crisis is discovered.
50
				'migrate_for_idc',             // (bool) True if someone confirms that this site should migrate stats and subscribers from its previous URL
51
				'connection_banner_ab',        // (int) 1 or 2, which will represent which connection banner to show.
52
			);
53
54
		case 'private' :
55
			return array(
56
				'register',
57
				'authorize',
58
				'activate_manage',
59
				'blog_token',                  // (string) The Client Secret/Blog Token of this site.
60
				'user_token',                  // (string) The User Token of this site. (deprecated)
61
				'user_tokens'                  // (array)  User Tokens for each user of this site who has connected to jetpack.wordpress.com.
62
			);
63
		}
64
65
		return array(
66
			'id',                           // (int)    The Client ID/WP.com Blog ID of this site.
67
			'publicize_connections',        // (array)  An array of Publicize connections from WordPress.com
68
			'master_user',                  // (int)    The local User ID of the user who connected this site to jetpack.wordpress.com.
69
			'version',                      // (string) Used during upgrade procedure to auto-activate new modules. version:time
70
			'old_version',                  // (string) Used to determine which modules are the most recently added. previous_version:time
71
			'fallback_no_verify_ssl_certs', // (int)    Flag for determining if this host must skip SSL Certificate verification due to misconfigured SSL.
72
			'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' )
73
			'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.
74
			'videopress',                   // (array)  VideoPress options array.
75
			'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.
76
			'social_links',                 // (array)  The specified links for each social networking site.
77
			'identity_crisis_whitelist',    // (array)  An array of options, each having an array of the values whitelisted for it.
78
			'gplus_authors',                // (array)  The Google+ authorship information for connected users.
79
			'last_heartbeat',               // (int)    The timestamp of the last heartbeat that fired.
80
			'jumpstart',                    // (string) A flag for whether or not to show the Jump Start.  Accepts: new_connection, jumpstart_activated, jetpack_action_taken, jumpstart_dismissed.
81
			'hide_jitm'                     // (array)  A list of just in time messages that we should not show because they have been dismissed by the user
82
		);
83
	}
84
85
	/**
86
	 * Is the option name valid?
87
	 *
88
	 * @param string      $name  The name of the option
89
	 * @param string|null $group The name of the group that the option is in. Default to null, which will search non_compact.
90
	 *
91
	 * @return bool Is the option name valid?
92
	 */
93
	public static function is_valid( $name, $group = null ) {
94
		if ( is_array( $name ) ) {
95
			$compact_names = array();
96
			foreach ( array_keys( self::$grouped_options ) as $_group ) {
97
				$compact_names = array_merge( $compact_names, self::get_option_names( $_group ) );
98
			}
99
100
			$result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names );
101
102
			return empty( $result );
103
		}
104
105 View Code Duplication
		if ( is_null( $group ) || 'non_compact' === $group ) {
106
			if ( in_array( $name, self::get_option_names( $group ) ) ) {
107
				return true;
108
			}
109
		}
110
111
		foreach ( array_keys( self::$grouped_options ) as $_group ) {
112 View Code Duplication
			if ( is_null( $group ) || $group === $_group ) {
113
				if ( in_array( $name, self::get_option_names( $_group ) ) ) {
114
					return true;
115
				}
116
			}
117
		}
118
119
		return false;
120
	}
121
122
	/**
123
	 * Returns the requested option.  Looks in jetpack_options or jetpack_$name as appropriate.
124
	 *
125
	 * @param string $name Option name
126
	 * @param mixed $default (optional)
127
	 *
128
	 * @return mixed
129
	 */
130
	public static function get_option( $name, $default = false ) {
131
		if ( self::is_valid( $name, 'non_compact' ) ) {
132
			return get_option( "jetpack_$name", $default );
133
		}
134
135
		foreach ( array_keys( self::$grouped_options ) as $group ) {
136
			if ( self::is_valid( $name, $group ) ) {
137
				return self::get_grouped_option( $group, $name, $default );
138
			}
139
		}
140
141
		trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
142
143
		return $default;
144
	}
145
146
	/**
147
	 * Returns the requested option, and ensures it's autoloaded in the future.
148
	 * This does _not_ adjust the prefix in any way (does not prefix jetpack_%)
149
	 *
150
	 * @param string $name Option name
151
	 * @param mixed $default (optional)
152
	 *
153
	 * @return mixed|void
154
	 */
155
	public static function get_option_and_ensure_autoload( $name, $default ) {
156
		$value = get_option( $name );
157
158
		if ( $value === false && $default !== false ) {
159
			update_option( $name, $default );
160
			$value = $default;
161
		}
162
163
		return $value;
164
	}
165
166
	private static function update_grouped_option( $group, $name, $value ) {
167
		$options = get_option( self::$grouped_options[ $group ] );
168
		if ( ! is_array( $options ) ) {
169
			$options = array();
170
		}
171
		$options[ $name ] = $value;
172
173
		return update_option( self::$grouped_options[ $group ], $options );
174
	}
175
176
	/**
177
	 * Updates the single given option.  Updates jetpack_options or jetpack_$name as appropriate.
178
	 *
179
	 * @param string $name Option name
180
	 * @param mixed $value Option value
181
	 * @param string $autoload If not compact option, allows specifying whether to autoload or not.
0 ignored issues
show
Should the type for parameter $autoload not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
182
	 *
183
	 * @return bool Was the option successfully updated?
184
	 */
185
	public static function update_option( $name, $value, $autoload = null ) {
186
		/**
187
		 * Fires before Jetpack updates a specific option.
188
		 *
189
		 * @since 3.0.0
190
		 *
191
		 * @param str $name The name of the option being updated.
192
		 * @param mixed $value The new value of the option.
193
		 */
194
		do_action( 'pre_update_jetpack_option_' . $name, $name, $value );
195
		if ( self::is_valid( $name, 'non_compact' ) ) {
196
			return update_option( "jetpack_$name", $value, $autoload );
197
		}
198
199
		foreach ( array_keys( self::$grouped_options ) as $group ) {
200
			if ( self::is_valid( $name, $group ) ) {
201
				return self::update_grouped_option( $group, $name, $value );
202
			}
203
		}
204
205
		trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
206
207
		return false;
208
	}
209
210
	/**
211
	 * Updates the multiple given options.  Updates jetpack_options and/or jetpack_$name as appropriate.
212
	 *
213
	 * @param array $array array( option name => option value, ... )
214
	 */
215
	public static function update_options( $array ) {
216
		$names = array_keys( $array );
217
218
		foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ), self::get_option_names( 'private' ) ) as $unknown_name ) {
219
			trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING );
220
			unset( $array[ $unknown_name ] );
221
		}
222
223
		foreach ( $names as $name ) {
224
			self::update_option( $name, $array[ $name ] );
225
		}
226
	}
227
228
	/**
229
	 * Deletes the given option.  May be passed multiple option names as an array.
230
	 * Updates jetpack_options and/or deletes jetpack_$name as appropriate.
231
	 *
232
	 * @param string|array $names
233
	 *
234
	 * @return bool Was the option successfully deleted?
235
	 */
236
	public static function delete_option( $names ) {
237
		$result = true;
238
		$names  = (array) $names;
239
240
		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...
241
			trigger_error( sprintf( 'Invalid Jetpack option names: %s', print_r( $names, 1 ) ), E_USER_WARNING );
242
			return false;
243
		}
244
245
		foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) {
246
			if ( ! delete_option( "jetpack_$name" ) ) {
247
				$result = false;
248
			}
249
		}
250
251
		foreach ( array_keys( self::$grouped_options ) as $group ) {
252
			if ( ! self::delete_grouped_option( $group, $names ) ) {
253
				$result = false;
254
			}
255
		}
256
257
		return $result;
258
	}
259
260
	private static function get_grouped_option( $group, $name, $default ) {
261
		$options = get_option( self::$grouped_options[ $group ] );
262
		if ( is_array( $options ) && isset( $options[ $name ] ) ) {
263
			return $options[ $name ];
264
		}
265
266
		return $default;
267
	}
268
269
	private static function delete_grouped_option( $group, $names ) {
270
		$options = get_option( self::$grouped_options[ $group ], array() );
271
272
		$to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) );
273
		if ( $to_delete ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $to_delete of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
274
			foreach ( $to_delete as $name ) {
275
				unset( $options[ $name ] );
276
			}
277
278
			return update_option( self::$grouped_options[ $group ], $options );
279
		}
280
281
		return true;
282
	}
283
284
}
285