Completed
Push — fix/videopress-always-active-o... ( 7f45ce...653bbf )
by
unknown
07:08
created

class.jetpack-options.php (2 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
	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
				'autoupdate_plugins',          // (array)  An array of plugin ids ( eg. jetpack/jetpack ) that should be autoupdated
27
				'autoupdate_themes',           // (array)  An array of theme ids ( eg. twentyfourteen ) that should be autoupdated
28
				'autoupdate_themes_translations', // (array)  An array of theme ids ( eg. twentyfourteen ) that should autoupdated translation files.
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
				'restapi_stats_cache',         // (array) Stats Cache data.
36
				'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
37
				'protect_whitelist',           // (array) IP Address for the Protect module to ignore
38
				'sync_error_idc',              // (bool|array) false or array containing the site's home and siteurl at time of IDC error
39
				'safe_mode_confirmed',         // (bool) True if someone confirms that this site was correctly put into safe mode automatically after an identity crisis is discovered.
40
				'migrate_for_idc',             // (bool) True if someone confirms that this site should migrate stats and subscribers from its previous URL
41
				'connection_banner_ab',        // (int) 1 or 2, which will represent which connection banner to show.
42
			);
43
44
		case 'private' :
45
			return array(
46
				'register',
47
				'authorize',
48
				'activate_manage',
49
				'blog_token',                  // (string) The Client Secret/Blog Token of this site.
50
				'user_token',                  // (string) The User Token of this site. (deprecated)
51
				'user_tokens'                  // (array)  User Tokens for each user of this site who has connected to jetpack.wordpress.com.
52
			);
53
		}
54
55
		return array(
56
			'id',                           // (int)    The Client ID/WP.com Blog ID of this site.
57
			'publicize_connections',        // (array)  An array of Publicize connections from WordPress.com
58
			'master_user',                  // (int)    The local User ID of the user who connected this site to jetpack.wordpress.com.
59
			'version',                      // (string) Used during upgrade procedure to auto-activate new modules. version:time
60
			'old_version',                  // (string) Used to determine which modules are the most recently added. previous_version:time
61
			'fallback_no_verify_ssl_certs', // (int)    Flag for determining if this host must skip SSL Certificate verification due to misconfigured SSL.
62
			'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' )
63
			'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.
64
			'videopress',                   // (array)  VideoPress options array.
65
			'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.
66
			'social_links',                 // (array)  The specified links for each social networking site.
67
			'identity_crisis_whitelist',    // (array)  An array of options, each having an array of the values whitelisted for it.
68
			'gplus_authors',                // (array)  The Google+ authorship information for connected users.
69
			'last_heartbeat',               // (int)    The timestamp of the last heartbeat that fired.
70
			'jumpstart',                    // (string) A flag for whether or not to show the Jump Start.  Accepts: new_connection, jumpstart_activated, jetpack_action_taken, jumpstart_dismissed.
71
			'hide_jitm'                     // (array)  A list of just in time messages that we should not show because they have been dismissed by the user
72
		);
73
	}
74
75
	public static function is_valid( $name, $group = null ) {
76
		if ( is_array( $name ) ) {
77
			$compact_names = array();
78
			foreach ( array_keys( self::$grouped_options ) as $_group ) {
79
				$compact_names = array_merge( $compact_names, self::get_option_names( $_group ) );
80
			}
81
82
			$result = array_diff( $name, self::get_option_names( 'non_compact' ), $compact_names );
83
84
			return empty( $result );
85
		}
86
87 View Code Duplication
		if ( is_null( $group ) || 'non_compact' === $group ) {
88
			if ( in_array( $name, self::get_option_names( $group ) ) ) {
89
				return true;
90
			}
91
		}
92
93
		foreach ( array_keys( self::$grouped_options ) as $_group ) {
94 View Code Duplication
			if ( is_null( $group ) || $group === $_group ) {
95
				if ( in_array( $name, self::get_option_names( $_group ) ) ) {
96
					return true;
97
				}
98
			}
99
		}
100
101
		return false;
102
	}
103
104
	/**
105
	 * Returns the requested option.  Looks in jetpack_options or jetpack_$name as appropriate.
106
	 *
107
	 * @param string $name Option name
108
	 * @param mixed $default (optional)
109
	 */
110
	public static function get_option( $name, $default = false ) {
111
		if ( self::is_valid( $name, 'non_compact' ) ) {
112
			return get_option( "jetpack_$name", $default );
113
		}
114
115
		foreach ( array_keys( self::$grouped_options ) as $group ) {
116
			if ( self::is_valid( $name, $group ) ) {
117
				return self::get_grouped_option( $group, $name, $default );
118
			}
119
		}
120
121
		trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
122
123
		return $default;
124
	}
125
126
	/**
127
	 * Returns the requested option, and ensures it's autoloaded in the future.
128
	 * This does _not_ adjust the prefix in any way (does not prefix jetpack_%)
129
	 *
130
	 * @param string $name Option name
131
	 * @param mixed $default (optional)
132
	 *
133
	 * @return mixed|void
134
	 */
135
	public static function get_option_and_ensure_autoload( $name, $default ) {
136
		$value = get_option( $name );
137
138
		if ( $value === false && $default !== false ) {
139
			update_option( $name, $default );
140
			$value = $default;
141
		}
142
143
		return $value;
144
	}
145
146
	private static function update_grouped_option( $group, $name, $value ) {
147
		$options = get_option( self::$grouped_options[ $group ] );
148
		if ( ! is_array( $options ) ) {
149
			$options = array();
150
		}
151
		$options[ $name ] = $value;
152
153
		return update_option( self::$grouped_options[ $group ], $options );
154
	}
155
156
	/**
157
	 * Updates the single given option.  Updates jetpack_options or jetpack_$name as appropriate.
158
	 *
159
	 * @param string $name Option name
160
	 * @param mixed $value Option value
161
	 * @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...
162
	 */
163
	public static function update_option( $name, $value, $autoload = null ) {
164
		/**
165
		 * Fires before Jetpack updates a specific option.
166
		 *
167
		 * @since 3.0.0
168
		 *
169
		 * @param str $name The name of the option being updated.
170
		 * @param mixed $value The new value of the option.
171
		 */
172
		do_action( 'pre_update_jetpack_option_' . $name, $name, $value );
173
		if ( self::is_valid( $name, 'non_compact' ) ) {
174
			return update_option( "jetpack_$name", $value, $autoload );
175
		}
176
177
		foreach ( array_keys( self::$grouped_options ) as $group ) {
178
			if ( self::is_valid( $name, $group ) ) {
179
				return self::update_grouped_option( $group, $name, $value );
180
			}
181
		}
182
183
		trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING );
184
185
		return false;
186
	}
187
188
	/**
189
	 * Updates the multiple given options.  Updates jetpack_options and/or jetpack_$name as appropriate.
190
	 *
191
	 * @param array $array array( option name => option value, ... )
192
	 */
193
	public static function update_options( $array ) {
194
		$names = array_keys( $array );
195
196
		foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ), self::get_option_names( 'private' ) ) as $unknown_name ) {
197
			trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING );
198
			unset( $array[ $unknown_name ] );
199
		}
200
201
		foreach ( $names as $name ) {
202
			self::update_option( $name, $array[ $name ] );
203
		}
204
	}
205
206
	/**
207
	 * Deletes the given option.  May be passed multiple option names as an array.
208
	 * Updates jetpack_options and/or deletes jetpack_$name as appropriate.
209
	 *
210
	 * @param string|array $names
211
	 */
212
	public static function delete_option( $names ) {
213
		$result = true;
214
		$names  = (array) $names;
215
216
		if ( ! self::is_valid( $names ) ) {
217
			trigger_error( sprintf( 'Invalid Jetpack option names: %s', print_r( $names, 1 ) ), E_USER_WARNING );
218
219
			return false;
220
		}
221
222
		foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) {
223
			if ( ! delete_option( "jetpack_$name" ) ) {
224
				$result = false;
225
			}
226
		}
227
228
		foreach ( array_keys( self::$grouped_options ) as $group ) {
229
			if ( ! self::delete_grouped_option( $group, $names ) ) {
230
				$result = false;
231
			}
232
		}
233
234
		return $result;
235
	}
236
237
	private static function get_grouped_option( $group, $name, $default ) {
238
		$options = get_option( self::$grouped_options[ $group ] );
239
		if ( is_array( $options ) && isset( $options[ $name ] ) ) {
240
			return $options[ $name ];
241
		}
242
243
		return $default;
244
	}
245
246
	private static function delete_grouped_option( $group, $names ) {
247
		$options = get_option( self::$grouped_options[ $group ], array() );
248
249
		$to_delete = array_intersect( $names, self::get_option_names( $group ), array_keys( $options ) );
250
		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...
251
			foreach ( $to_delete as $name ) {
252
				unset( $options[ $name ] );
253
			}
254
255
			return update_option( self::$grouped_options[ $group ], $options );
256
		}
257
258
		return true;
259
	}
260
261
}
262