| 1 |
<?php |
| 2 |
/** |
| 3 |
Central management of options storage for Project Sync. |
| 4 |
Written by Chris Jean for iThemes.com |
| 5 |
Version 1.2.0 |
| 6 |
|
| 7 |
Version History |
| 8 |
1.0.0 - 2013-10-01 - Chris Jean |
| 9 |
Initial version |
| 10 |
1.0.1 - 2013-11-18 - Chris Jean |
| 11 |
Updated brace format. |
| 12 |
1.1.0 - 2013-11-19 - Chris Jean |
| 13 |
Added the show_sync option. |
| 14 |
1.2.0 - 2014-03-20 - Chris Jean |
| 15 |
Added validate_authentications(), validate_authentication(), and do_ping_check(). |
| 16 |
*/ |
| 17 |
|
| 18 |
use SolidWP\Central\Central_Server\Central_Server_Client; |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Ithemes_Sync_Settings. |
| 22 |
*/ |
| 23 |
class Ithemes_Sync_Settings { |
| 24 |
|
| 25 |
/** |
| 26 |
* @readonly |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
private $option_name = 'ithemes-sync-cache'; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var array|false |
| 33 |
*/ |
| 34 |
private $options = false; |
| 35 |
|
| 36 |
/** |
| 37 |
* Whether the settings have been modified. |
| 38 |
* |
| 39 |
* Used on shutdown to determine whether to save to the database. |
| 40 |
* |
| 41 |
* @var bool |
| 42 |
*/ |
| 43 |
private $options_modified = false; |
| 44 |
|
| 45 |
/** |
| 46 |
* @var bool |
| 47 |
*/ |
| 48 |
private $initialized = false; |
| 49 |
|
| 50 |
/** |
| 51 |
* @readonly |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
private $default_options = [ |
| 55 |
'authentications' => [], |
| 56 |
'show_sync' => true, |
| 57 |
]; |
| 58 |
|
| 59 |
|
| 60 |
/** |
| 61 |
* Create a settings instance. |
| 62 |
* |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function __construct() { |
| 66 |
$GLOBALS['ithemes-sync-settings'] = $this; |
| 67 |
|
| 68 |
add_action( 'shutdown', [ $this, 'shutdown' ] ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Initialized the settings. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function init() { |
| 77 |
if ( $this->initialized ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
$this->initialized = true; |
| 82 |
|
| 83 |
$this->load(); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function load() { |
| 90 |
if ( false !== $this->options ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$this->options = get_site_option( $this->option_name, false ); |
| 95 |
|
| 96 |
if ( ( false === $this->options ) || ! is_array( $this->options ) ) { |
| 97 |
$this->options = []; |
| 98 |
} |
| 99 |
|
| 100 |
$this->options = array_merge( $this->default_options, $this->options ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Unloads the settings state. |
| 105 |
* |
| 106 |
* @return void |
| 107 |
*/ |
| 108 |
public function unload() { |
| 109 |
$this->initialized = false; |
| 110 |
$this->options = false; |
| 111 |
$this->options_modified = false; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @return void |
| 116 |
*/ |
| 117 |
public function shutdown() { |
| 118 |
if ( $this->options_modified ) { |
| 119 |
update_site_option( $this->option_name, $this->options ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @return array |
| 125 |
*/ |
| 126 |
public function get_options(): array { |
| 127 |
$this->init(); |
| 128 |
|
| 129 |
return $this->options; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* @param string $key The settings identifier. |
| 134 |
* |
| 135 |
* @return mixed|null |
| 136 |
*/ |
| 137 |
public function get_option( string $key ) { |
| 138 |
$this->init(); |
| 139 |
|
| 140 |
if ( isset( $this->options[ $key ] ) ) { |
| 141 |
return $this->options[ $key ]; |
| 142 |
} |
| 143 |
|
| 144 |
return null; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @param array $updates The new options to merge. |
| 149 |
* |
| 150 |
* @return void |
| 151 |
*/ |
| 152 |
public function update_options( array $updates ) { |
| 153 |
$this->init(); |
| 154 |
|
| 155 |
$this->options = array_merge( $this->options, $updates ); |
| 156 |
$this->options_modified = true; |
| 157 |
} |
| 158 |
/** |
| 159 |
* Add an authentication to the settings option. |
| 160 |
* |
| 161 |
* @param int $central_site_id The ID of the site to authenticate. |
| 162 |
* @param string $solidwp_username The username of the Kadence account. |
| 163 |
* @param string $key The private key for the authentication. |
| 164 |
* @param string|false $wp_user_login The WordPress user login to associate with the authentication. |
| 165 |
* |
| 166 |
* @return true|WP_Error |
| 167 |
*/ |
| 168 |
public function add_authentication( $central_site_id, $solidwp_username, $key, $wp_user_login = false ) { |
| 169 |
$this->init(); |
| 170 |
|
| 171 |
if ( ! isset( $this->options['authentications'] ) || ! is_array( $this->options['authentications'] ) ) { |
| 172 |
$this->options['authentications'] = []; |
| 173 |
} |
| 174 |
|
| 175 |
if ( empty( $wp_user_login ) ) { |
| 176 |
$local_user = wp_get_current_user(); |
| 177 |
$wp_user_login = $local_user->user_login; |
| 178 |
} |
| 179 |
|
| 180 |
$this->options['authentications'][ $central_site_id ] = [ |
| 181 |
'key' => $key, |
| 182 |
'timestamp' => time(), |
| 183 |
'local_user' => $wp_user_login, |
| 184 |
'username' => $solidwp_username, |
| 185 |
]; |
| 186 |
|
| 187 |
// Calling `update_site_options` directly, rather than waiting for shutdown, |
| 188 |
// we want to return an error if the update fails. |
| 189 |
if ( ! update_site_option( $this->option_name, $this->options ) ) { |
| 190 |
return new WP_Error( |
| 191 |
'solid-central.auth.add-authentication-failed', |
| 192 |
__( 'Kadence Central authorization failed, unable to save authentication details.', 'it-l10n-ithemes-sync' ), |
| 193 |
[ |
| 194 |
'status' => 500, |
| 195 |
] |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
// Any other previous modifications were also saved. |
| 200 |
$this->options_modified = false; |
| 201 |
|
| 202 |
if ( ! empty( $this->options['authentications'] ) ) { |
| 203 |
update_site_option( 'ithemes-sync-authenticated', true ); |
| 204 |
|
| 205 |
return true; |
| 206 |
} |
| 207 |
|
| 208 |
delete_site_option( 'ithemes_sync_hide_authenticate_notice' ); |
| 209 |
|
| 210 |
return new WP_Error( |
| 211 |
'solid-central.auth.add-authentication-failed', |
| 212 |
__( 'Kadence Central authorization failed, encountered an unknown error.', 'it-l10n-ithemes-sync' ), |
| 213 |
[ |
| 214 |
'status' => 500, |
| 215 |
] |
| 216 |
); |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Remove a Kadence Central user authentication. |
| 221 |
* |
| 222 |
* @param int $user_id The WordPress user ID. |
| 223 |
* @param string $username The Kadence username. |
| 224 |
* |
| 225 |
* @return true|WP_Error |
| 226 |
*/ |
| 227 |
public function remove_authentication( int $user_id, string $username ) { |
| 228 |
$this->init(); |
| 229 |
|
| 230 |
|
| 231 |
if ( ! isset( $this->options['authentications'] ) || ! is_array( $this->options['authentications'] ) ) { |
| 232 |
$this->options['authentications'] = []; |
| 233 |
} |
| 234 |
|
| 235 |
if ( ! isset( $this->options['authentications'][ $user_id ] ) ) { |
| 236 |
return new WP_Error( 'unauthenticated-user', __( 'The user is not authenticated. Could not remove authentication for the user.', 'it-l10n-ithemes-sync' ) ); |
| 237 |
} |
| 238 |
|
| 239 |
unset( $this->options['authentications'][ $user_id ] ); |
| 240 |
|
| 241 |
$this->options_modified = true; |
| 242 |
|
| 243 |
|
| 244 |
if ( empty( $this->options['authentications'] ) ) { |
| 245 |
delete_site_option( 'ithemes-sync-authenticated' ); |
| 246 |
delete_site_option( 'ithemes_sync_hide_authenticate_notice' ); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
return true; |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* @param int $user_id The WordPress user ID. |
| 255 |
* |
| 256 |
* @return false|mixed The user authentications or false if none. |
| 257 |
*/ |
| 258 |
public function get_authentication_details( int $user_id ) { |
| 259 |
if ( ! isset( $this->options['authentications'][ $user_id ] ) ) { |
| 260 |
return false; |
| 261 |
} |
| 262 |
|
| 263 |
return $this->options['authentications'][ $user_id ]; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Validate all previously saved user authentications. |
| 268 |
* |
| 269 |
* @return array<int,bool> A map of WordPress user IDs to a bool representing |
| 270 |
* authentication status. |
| 271 |
*/ |
| 272 |
public function validate_authentications(): array { |
| 273 |
$validations = []; |
| 274 |
|
| 275 |
foreach ( $this->options['authentications'] as $user_id => $details ) { |
| 276 |
$validations[ $user_id ] = $this->validate_authentication( $user_id ); |
| 277 |
} |
| 278 |
|
| 279 |
return $validations; |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* Validate a previously saved user authentication. |
| 284 |
* |
| 285 |
* If a user is not provided the first authentication is used. |
| 286 |
* |
| 287 |
* @param int|false $user_id The WordPress user ID, optional. |
| 288 |
* |
| 289 |
* @return bool |
| 290 |
*/ |
| 291 |
public function validate_authentication( $user_id ): bool { |
| 292 |
$authentication = $this->get_authentication_details( $user_id ); |
| 293 |
|
| 294 |
if ( empty( $authentication ) ) { |
| 295 |
return false; |
| 296 |
} |
| 297 |
|
| 298 |
$result = Central_Server_Client::validate( |
| 299 |
[ |
| 300 |
'site_id' => $user_id, |
| 301 |
'username' => $authentication['username'], |
| 302 |
'private_key' => $authentication['key'], |
| 303 |
] |
| 304 |
); |
| 305 |
|
| 306 |
return is_array( $result ); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Use a validation request the Central server to verify connection. |
| 311 |
* |
| 312 |
* If a user is not provided the first authentication is used. |
| 313 |
* |
| 314 |
* @param int|false $user_id The WordPress user ID, optional. |
| 315 |
* |
| 316 |
* @return array|WP_Error |
| 317 |
*/ |
| 318 |
public function do_ping_check( $user_id = false ) { |
| 319 |
if ( empty( $user_id ) ) { |
| 320 |
$user_id = current( array_keys( $this->options['authentications'] ) ); |
| 321 |
} |
| 322 |
|
| 323 |
$authentication = $this->get_authentication_details( $user_id ); |
| 324 |
|
| 325 |
if ( empty( $authentication ) ) { |
| 326 |
return new WP_Error( |
| 327 |
'ithemes-sync-invalid-user', |
| 328 |
__( |
| 329 |
'A valid user was unable to be found. A valid user is required in order to do a successful ping.', |
| 330 |
'it-l10n-ithemes-sync' |
| 331 |
) |
| 332 |
); |
| 333 |
} |
| 334 |
|
| 335 |
return Central_Server_Client::ping( |
| 336 |
[ |
| 337 |
'site_id' => $user_id, |
| 338 |
'username' => $authentication['username'], |
| 339 |
'private_key' => $authentication['key'], |
| 340 |
] |
| 341 |
); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
new Ithemes_Sync_Settings(); |
| 346 |
|