| 1 |
<?php |
| 2 |
|
| 3 |
class Jetpack_Options { |
| 4 |
|
| 5 |
public static function get_option_names( $type = 'compact' ) { |
| 6 |
switch ( $type ) { |
| 7 |
case 'non-compact' : |
| 8 |
case 'non_compact' : |
| 9 |
return array( |
| 10 |
'register', |
| 11 |
'activated', |
| 12 |
'active_modules', |
| 13 |
'do_activate', |
| 14 |
'log', |
| 15 |
'publicize', |
| 16 |
'slideshow_background_color', |
| 17 |
'widget_twitter', |
| 18 |
'wpcc_options', |
| 19 |
'relatedposts', |
| 20 |
); |
| 21 |
} |
| 22 |
|
| 23 |
return array( |
| 24 |
'id', // (int) The Client ID/WP.com Blog ID of this site. |
| 25 |
'blog_token', // (string) The Client Secret/Blog Token of this site. |
| 26 |
'user_token', // (string) The User Token of this site. (deprecated) |
| 27 |
'publicize_connections', // (array) An array of Publicize connections from WordPress.com |
| 28 |
'master_user', // (int) The local User ID of the user who connected this site to jetpack.wordpress.com. |
| 29 |
'user_tokens', // (array) User Tokens for each user of this site who has connected to jetpack.wordpress.com. |
| 30 |
'version', // (string) Used during upgrade procedure to auto-activate new modules. version:time |
| 31 |
'old_version', // (string) Used to determine which modules are the most recently added. previous_version:time |
| 32 |
'fallback_no_verify_ssl_certs', // (int) Flag for determining if this host must skip SSL Certificate verification due to misconfigured SSL. |
| 33 |
'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' ) |
| 34 |
'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. |
| 35 |
'videopress', // (array) VideoPress options array. |
| 36 |
'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. |
| 37 |
'social_links', // (array) The specified links for each social networking site. |
| 38 |
'identity_crisis_whitelist', // (array) An array of options, each having an array of the values whitelisted for it. |
| 39 |
'gplus_authors', // (array) The Google+ authorship information for connected users. |
| 40 |
'last_heartbeat', // (int) The timestamp of the last heartbeat that fired. |
| 41 |
'sync_bulk_reindexing', // (bool) If a bulk reindex is currently underway. |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Returns the requested option. Looks in jetpack_options or jetpack_$name as appropriate. |
| 47 |
* |
| 48 |
* @param string $name Option name |
| 49 |
* @param mixed $default (optional) |
| 50 |
*/ |
| 51 |
public static function get_option( $name, $default = false ) { |
| 52 |
if ( in_array( $name, self::get_option_names( 'non_compact' ) ) ) { |
| 53 |
return get_option( "jetpack_$name" ); |
| 54 |
} else if ( !in_array( $name, self::get_option_names() ) ) { |
| 55 |
trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING ); |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
$options = get_option( 'jetpack_options' ); |
| 60 |
if ( is_array( $options ) && isset( $options[$name] ) ) { |
| 61 |
return $options[$name]; |
| 62 |
} |
| 63 |
|
| 64 |
return $default; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Updates the single given option. Updates jetpack_options or jetpack_$name as appropriate. |
| 69 |
* |
| 70 |
* @param string $name Option name |
| 71 |
* @param mixed $value Option value |
| 72 |
*/ |
| 73 |
public static function update_option( $name, $value ) { |
| 74 |
do_action( 'pre_update_jetpack_option_' . $name, $name, $value ); |
| 75 |
if ( in_array( $name, self::get_option_names( 'non_compact' ) ) ) { |
| 76 |
return update_option( "jetpack_$name", $value ); |
| 77 |
} else if ( !in_array( $name, self::get_option_names() ) ) { |
| 78 |
trigger_error( sprintf( 'Invalid Jetpack option name: %s', $name ), E_USER_WARNING ); |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
$options = get_option( 'jetpack_options' ); |
| 83 |
if ( !is_array( $options ) ) { |
| 84 |
$options = array(); |
| 85 |
} |
| 86 |
|
| 87 |
$options[$name] = $value; |
| 88 |
|
| 89 |
return update_option( 'jetpack_options', $options ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Updates the multiple given options. Updates jetpack_options and/or jetpack_$name as appropriate. |
| 94 |
* |
| 95 |
* @param array $array array( option name => option value, ... ) |
| 96 |
*/ |
| 97 |
public static function update_options( $array ) { |
| 98 |
$names = array_keys( $array ); |
| 99 |
|
| 100 |
foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ) ) as $unknown_name ) { |
| 101 |
trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING ); |
| 102 |
unset( $array[$unknown_name] ); |
| 103 |
} |
| 104 |
|
| 105 |
foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) { |
| 106 |
update_option( "jetpack_$name", $array[$name] ); |
| 107 |
unset( $array[$name] ); |
| 108 |
} |
| 109 |
|
| 110 |
$options = get_option( 'jetpack_options' ); |
| 111 |
if ( !is_array( $options ) ) { |
| 112 |
$options = array(); |
| 113 |
} |
| 114 |
|
| 115 |
return update_option( 'jetpack_options', array_merge( $options, $array ) ); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Deletes the given option. May be passed multiple option names as an array. |
| 120 |
* Updates jetpack_options and/or deletes jetpack_$name as appropriate. |
| 121 |
* |
| 122 |
* @param string|array $names |
| 123 |
*/ |
| 124 |
public static function delete_option( $names ) { |
| 125 |
$names = (array) $names; |
| 126 |
|
| 127 |
foreach ( array_diff( $names, self::get_option_names(), self::get_option_names( 'non_compact' ) ) as $unknown_name ) { |
| 128 |
trigger_error( sprintf( 'Invalid Jetpack option name: %s', $unknown_name ), E_USER_WARNING ); |
| 129 |
} |
| 130 |
|
| 131 |
foreach ( array_intersect( $names, self::get_option_names( 'non_compact' ) ) as $name ) { |
| 132 |
delete_option( "jetpack_$name" ); |
| 133 |
} |
| 134 |
|
| 135 |
$options = get_option( 'jetpack_options' ); |
| 136 |
if ( !is_array( $options ) ) { |
| 137 |
$options = array(); |
| 138 |
} |
| 139 |
|
| 140 |
$to_delete = array_intersect( $names, self::get_option_names(), array_keys( $options ) ); |
| 141 |
if ( $to_delete ) { |
| 142 |
foreach ( $to_delete as $name ) { |
| 143 |
unset( $options[$name] ); |
| 144 |
} |
| 145 |
|
| 146 |
return update_option( 'jetpack_options', $options ); |
| 147 |
} |
| 148 |
|
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
|