Completed
Push — core/custom-css-4.7 ( cbe1bb...6ab2f2 )
by
unknown
21:43 queued 12:06
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_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
			'custom_css_4.7_migration',     // (bool)   Whether Custom CSS has scanned for and migrated any legacy CSS CPT entries to the new Core format.
83
		);
84
	}
85
86
	/**
87
	 * Is the option name valid?
88
	 *
89
	 * @param string      $name  The name of the option
90
	 * @param string|null $group The name of the group that the option is in. Default to null, which will search non_compact.
91
	 *
92
	 * @return bool Is the option name valid?
93
	 */
94
	public static function is_valid( $name, $group = null ) {
95
		if ( is_array( $name ) ) {
96
			$compact_names = array();
97
			foreach ( array_keys( self::$grouped_options ) as $_group ) {
98
				$compact_names = array_merge( $compact_names, self::get_option_names( $_group ) );
99
			}
100
101
			$result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names );
102
103
			return empty( $result );
104
		}
105
106 View Code Duplication
		if ( is_null( $group ) || 'non_compact' === $group ) {
107
			if ( in_array( $name, self::get_option_names( $group ) ) ) {
108
				return true;
109
			}
110
		}
111
112
		foreach ( array_keys( self::$grouped_options ) as $_group ) {
113 View Code Duplication
			if ( is_null( $group ) || $group === $_group ) {
114
				if ( in_array( $name, self::get_option_names( $_group ) ) ) {
115
					return true;
116
				}
117
			}
118
		}
119
120
		return false;
121
	}
122
123
	/**
124
	 * Returns the requested option.  Looks in jetpack_options or jetpack_$name as appropriate.
125
	 *
126
	 * @param string $name Option name
127
	 * @param mixed $default (optional)
128
	 *
129
	 * @return mixed
130
	 */
131
	public static function get_option( $name, $default = false ) {
132
		if ( self::is_valid( $name, 'non_compact' ) ) {
133
			return get_option( "jetpack_$name", $default );
134
		}
135
136
		foreach ( array_keys( self::$grouped_options ) as $group ) {
137
			if ( self::is_valid( $name, $group ) ) {
138
				return self::get_grouped_option( $group, $name, $default );
139
			}
140
		}
141
142
		trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
143
144
		return $default;
145
	}
146
147
	/**
148
	 * Returns the requested option, and ensures it's autoloaded in the future.
149
	 * This does _not_ adjust the prefix in any way (does not prefix jetpack_%)
150
	 *
151
	 * @param string $name Option name
152
	 * @param mixed $default (optional)
153
	 *
154
	 * @return mixed|void
155
	 */
156
	public static function get_option_and_ensure_autoload( $name, $default ) {
157
		$value = get_option( $name );
158
159
		if ( $value === false && $default !== false ) {
160
			update_option( $name, $default );
161
			$value = $default;
162
		}
163
164
		return $value;
165
	}
166
167
	private static function update_grouped_option( $group, $name, $value ) {
168
		$options = get_option( self::$grouped_options[ $group ] );
169
		if ( ! is_array( $options ) ) {
170
			$options = array();
171
		}
172
		$options[ $name ] = $value;
173
174
		return update_option( self::$grouped_options[ $group ], $options );
175
	}
176
177
	/**
178
	 * Updates the single given option.  Updates jetpack_options or jetpack_$name as appropriate.
179
	 *
180
	 * @param string $name Option name
181
	 * @param mixed $value Option value
182
	 * @param string $autoload If not compact option, allows specifying whether to autoload or not.
183
	 *
184
	 * @return bool Was the option successfully updated?
185
	 */
186
	public static function update_option( $name, $value, $autoload = null ) {
187
		/**
188
		 * Fires before Jetpack updates a specific option.
189
		 *
190
		 * @since 3.0.0
191
		 *
192
		 * @param str $name The name of the option being updated.
193
		 * @param mixed $value The new value of the option.
194
		 */
195
		do_action( 'pre_update_jetpack_option_' . $name, $name, $value );
196
		if ( self::is_valid( $name, 'non_compact' ) ) {
197
			return update_option( "jetpack_$name", $value, $autoload );
198
		}
199
200
		foreach ( array_keys( self::$grouped_options ) as $group ) {
201
			if ( self::is_valid( $name, $group ) ) {
202
				return self::update_grouped_option( $group, $name, $value );
203
			}
204
		}
205
206
		trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
207
208
		return false;
209
	}
210
211
	/**
212
	 * Updates the multiple given options.  Updates jetpack_options and/or jetpack_$name as appropriate.
213
	 *
214
	 * @param array $array array( option name => option value, ... )
215
	 */
216
	public static function update_options( $array ) {
217
		$names = array_keys( $array );
218
219
		foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ), self::get_option_names( 'private' ) ) as $unknown_name ) {
220
			trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING );
221
			unset( $array[ $unknown_name ] );
222
		}
223
224
		foreach ( $names as $name ) {
225
			self::update_option( $name, $array[ $name ] );
226
		}
227
	}
228
229
	/**
230
	 * Deletes the given option.  May be passed multiple option names as an array.
231
	 * Updates jetpack_options and/or deletes jetpack_$name as appropriate.
232
	 *
233
	 * @param string|array $names
234
	 *
235
	 * @return bool Was the option successfully deleted?
236
	 */
237
	public static function delete_option( $names ) {
238
		$result = true;
239
		$names  = (array) $names;
240
241
		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...
242
			trigger_error( sprintf( 'Invalid Jetpack option names: %s', print_r( $names, 1 ) ), E_USER_WARNING );
243
			return false;
244
		}
245
246
		foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) {
247
			if ( ! delete_option( "jetpack_$name" ) ) {
248
				$result = false;
249
			}
250
		}
251
252
		foreach ( array_keys( self::$grouped_options ) as $group ) {
253
			if ( ! self::delete_grouped_option( $group, $names ) ) {
254
				$result = false;
255
			}
256
		}
257
258
		return $result;
259
	}
260
261
	private static function get_grouped_option( $group, $name, $default ) {
262
		$options = get_option( self::$grouped_options[ $group ] );
263
		if ( is_array( $options ) && isset( $options[ $name ] ) ) {
264
			return $options[ $name ];
265
		}
266
267
		return $default;
268
	}
269
270
	private static function delete_grouped_option( $group, $names ) {
271
		$options = get_option( self::$grouped_options[ $group ], array() );
272
273
		$to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) );
274
		if ( $to_delete ) {
275
			foreach ( $to_delete as $name ) {
276
				unset( $options[ $name ] );
277
			}
278
279
			return update_option( self::$grouped_options[ $group ], $options );
280
		}
281
282
		return true;
283
	}
284
285
}
286