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