/** * @return WP_Error|string secret_2 on success, WP_Error( error_code => error_code, error_message => error description, error_data => status code ) on failure * * Possible error_codes: * * verify_secret_1_missing * verify_secret_1_malformed * verify_secrets_missing: No longer have verification secrets stored * verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com */ function verify_action($params) { $action = $params[0]; $verify_secret = $params[1]; if (empty($verify_secret)) { return $this->error(new Jetpack_Error('verify_secret_1_missing', sprintf('The required "%s" parameter is missing.', 'secret_1'), 400)); } else { if (!is_string($verify_secret)) { return $this->error(new Jetpack_Error('verify_secret_1_malformed', sprintf('The required "%s" parameter is malformed.', 'secret_1'), 400)); } } $secrets = Jetpack_Options::get_option($action); if (!$secrets || is_wp_error($secrets)) { Jetpack_Options::delete_option($action); return $this->error(new Jetpack_Error('verify_secrets_missing', 'Verification took too long', 400)); } @(list($secret_1, $secret_2, $secret_eol) = explode(':', $secrets)); if (empty($secret_1) || empty($secret_2) || empty($secret_eol) || $secret_eol < time()) { Jetpack_Options::delete_option($action); return $this->error(new Jetpack_Error('verify_secrets_missing', 'Verification took too long', 400)); } if ($verify_secret !== $secret_1) { Jetpack_Options::delete_option($action); return $this->error(new Jetpack_Error('verify_secrets_mismatch', 'Secret mismatch', 400)); } Jetpack_Options::delete_option($action); return $secret_2; }
public function test_does_not_fire_jetpack_publicize_post_on_save_as_published() { $this->post->post_status = 'publish'; Jetpack_Options::delete_option(array('publicize_connections')); wp_insert_post($this->post->to_array()); $this->assertPublicized(false, $this->post); }
function test_delete_non_compact_option_returns_true_when_successfully_deleted() { Jetpack_Options::update_option('migrate_for_idc', true); // Make sure the option is set $this->assertTrue(Jetpack_Options::get_option('migrate_for_idc')); $deleted = Jetpack_Options::delete_option('migrate_for_idc'); // Was the option successfully deleted? $this->assertFalse(Jetpack_Options::get_option('migrate_for_idc')); // Did Jetpack_Options::delete_option() properly return true? $this->assertTrue($deleted); }
public function test_site_icon_update_to_null_is_synced_using_jetpack_function() { global $wp_version; // verify that we started with an icon. $this->assertEquals('http://foo.com/icon.gif', $this->server_replica_storage->get_option('jetpack_site_icon_url')); if (version_compare($wp_version, '4.4', '>=')) { remove_filter('get_site_icon_url', array($this, '_get_site_icon'), 99, 3); update_option('site_icon', 0); } else { // wp 4.3 or less Jetpack_Options::delete_option('site_icon_url'); } $this->client->do_sync(); $this->assertEmpty($this->server_replica_storage->get_option('jetpack_site_icon_url')); $this->assertEquals(Jetpack_Options::get_option('site_icon_url'), $this->server_replica_storage->get_option('jetpack_site_icon_url')); }
public function reindex_status() { $response = array('status' => 'ERROR'); // Assume reindexing is done if it was not triggered in the first place if (false === Jetpack_Options::get_option('sync_bulk_reindexing')) { return array('status' => 'DONE'); } Jetpack::load_xml_rpc_client(); $client = new Jetpack_IXR_Client(array('user_id' => JETPACK_MASTER_USER)); $client->query('jetpack.reindexStatus'); if (!$client->isError()) { $response = $client->getResponse(); if ('DONE' == $response['status']) { Jetpack_Options::delete_option('sync_bulk_reindexing'); } } return $response; }
function test_sync_error_idc_validation_returns_true_when_option_matches_expected() { add_filter('jetpack_sync_idc_optin', '__return_true'); Jetpack_Options::update_option('sync_error_idc', Jetpack::get_sync_error_idc_option()); $this->assertTrue(Jetpack::validate_sync_error_idc_option()); Jetpack_Options::delete_option('sync_error_idc'); remove_filter('jetpack_sync_idc_optin', '__return_true'); }
/** * Manage Jetpack Options * * ## OPTIONS * * list : List all jetpack options and their values * delete : Delete an option * - can only delete options that are white listed. * update : update an option * - can only update option strings * get : get the value of an option * * ## EXAMPLES * * wp jetpack options list * wp jetpack options get <option_name> * wp jetpack options delete <option_name> * wp jetpack options update <option_name> [<option_value>] * * @synopsis <list|get|delete|update> [<option_name>] [<option_value>] */ public function options($args, $assoc_args) { $action = isset($args[0]) ? $args[0] : 'list'; $safe_to_modify = Jetpack::get_jetpack_options_for_reset(); // Jumpstart is special array_push($safe_to_modify, 'jumpstart'); // Is the option flagged as unsafe? $flagged = !in_array($args[1], $safe_to_modify); if (!in_array($action, array('list', 'get', 'delete', 'update'))) { WP_CLI::error(sprintf(__('%s is not a valid command.', 'jetpack'), $action)); } if (isset($args[0])) { if ('get' == $args[0] && isset($args[1])) { $action = 'get'; } else { if ('delete' == $args[0] && isset($args[1])) { $action = 'delete'; } else { if ('update' == $args[0] && isset($args[1])) { $action = 'update'; } else { $action = 'list'; } } } } // Bail if the option isn't found $option = isset($args[1]) ? Jetpack_Options::get_option($args[1]) : false; if (isset($args[1]) && !$option && 'update' !== $args[0]) { WP_CLI::error(__('Option not found or is empty. Use "list" to list option names', 'jetpack')); } // Let's print_r the option if it's an array // Used in the 'get' and 'list' actions $option = is_array($option) ? print_r($option) : $option; switch ($action) { case 'get': WP_CLI::success("\t" . $option); break; case 'delete': jetpack_cli_are_you_sure($flagged); Jetpack_Options::delete_option($args[1]); WP_CLI::success(sprintf(__('Deleted option: %s', 'jetpack'), $args[1])); break; case 'update': jetpack_cli_are_you_sure($flagged); // Updating arrays would get pretty tricky... $value = Jetpack_Options::get_option($args[1]); if ($value && is_array($value)) { WP_CLI::error(__('Sorry, no updating arrays at this time', 'jetpack')); } Jetpack_Options::update_option($args[1], $args[2]); WP_CLI::success(sprintf(_x('Updated option: %s to "%s"', 'Updating an option from "this" to "that".', 'jetpack'), $args[1], $args[2])); break; case 'list': $options_compact = Jetpack_Options::get_option_names(); $options_non_compact = Jetpack_Options::get_option_names('non_compact'); $options_private = Jetpack_Options::get_option_names('private'); $options = array_merge($options_compact, $options_non_compact, $options_private); // Table headers WP_CLI::line("\t" . str_pad(__('Option', 'jetpack'), 30) . __('Value', 'jetpack')); // List out the options and their values // Tell them if the value is empty or not // Tell them if it's an array foreach ($options as $option) { $value = Jetpack_Options::get_option($option); if (!$value) { WP_CLI::line("\t" . str_pad($option, 30) . 'Empty'); continue; } if (!is_array($value)) { WP_CLI::line("\t" . str_pad($option, 30) . $value); } else { if (is_array($value)) { WP_CLI::line("\t" . str_pad($option, 30) . 'Array - Use "get <option>" to read option array.'); } } } $option_text = '{' . _x('option', 'a variable command that a user can write, provided in the printed instructions', 'jetpack') . '}'; $value_text = '{' . _x('value', 'the value that they want to update the option to', 'jetpack') . '}'; WP_CLI::success(_x("Above are your options. You may 'get', 'delete', and 'update' them.", "'get', 'delete', and 'update' are commands - do not translate.", 'jetpack') . "\n" . str_pad('wp jetpack options get', 26) . $option_text . "\n" . str_pad('wp jetpack options delete', 26) . $option_text . "\n" . str_pad('wp jetpack options update', 26) . "{$option_text} {$value_text}" . "\n" . _x("Type 'wp jetpack options' for more info.", "'wp jetpack options' is a command - do not translate.", 'jetpack') . "\n"); break; } }
/** * Runs when the VideoPress module is deactivated. */ function jetpack_module_deactivated() { Jetpack_Options::delete_option($this->option_name); }
/** * Reset Jetpack options * * @since 4.3.0 * * @param WP_REST_Request $data { * Array of parameters received by request. * * @type string $options Available options to reset are options|modules * } * * @return bool|WP_Error True if options were reset. Otherwise, a WP_Error instance with the corresponding error. */ public static function reset_jetpack_options($data) { $param = $data->get_json_params(); if (!isset($param['reset']) || $param['reset'] !== true) { return new WP_Error('invalid_param', esc_html__('Invalid Parameter', 'jetpack'), array('status' => 404)); } if (isset($data['options'])) { $data = $data['options']; switch ($data) { case 'options': $options_to_reset = Jetpack::get_jetpack_options_for_reset(); // Reset the Jetpack options foreach ($options_to_reset['jp_options'] as $option_to_reset) { Jetpack_Options::delete_option($option_to_reset); } foreach ($options_to_reset['wp_options'] as $option_to_reset) { delete_option($option_to_reset); } // Reset to default modules $default_modules = Jetpack::get_default_modules(); Jetpack::update_active_modules($default_modules); // Jumpstart option is special Jetpack_Options::update_option('jumpstart', 'new_connection'); return rest_ensure_response(array('code' => 'success', 'message' => esc_html__('Jetpack options reset.', 'jetpack'))); break; case 'modules': $default_modules = Jetpack::get_default_modules(); Jetpack::update_active_modules($default_modules); return rest_ensure_response(array('code' => 'success', 'message' => esc_html__('Modules reset to default.', 'jetpack'))); break; default: return new WP_Error('invalid_param', esc_html__('Invalid Parameter', 'jetpack'), array('status' => 404)); } } return new WP_Error('required_param', esc_html__('Missing parameter "type".', 'jetpack'), array('status' => 404)); }
function authorize() { $data = stripslashes_deep($_GET); $args = array(); $redirect = isset($data['redirect']) ? esc_url_raw((string) $data['redirect']) : ''; do { $jetpack = Jetpack::init(); $role = $jetpack->translate_current_user_to_role(); if (!$role) { Jetpack::state('error', 'no_role'); break; } $cap = $jetpack->translate_role_to_cap($role); if (!$cap) { Jetpack::state('error', 'no_cap'); break; } check_admin_referer("jetpack-authorize_{$role}_{$redirect}"); if (!empty($data['error'])) { Jetpack::state('error', $data['error']); break; } if (empty($data['state'])) { Jetpack::state('error', 'no_state'); break; } if (!ctype_digit($data['state'])) { Jetpack::state('error', 'invalid_state'); break; } $current_user_id = get_current_user_id(); if ($current_user_id != $data['state']) { Jetpack::state('error', 'wrong_state'); break; } if (empty($data['code'])) { Jetpack::state('error', 'no_code'); break; } $token = $this->get_token($data); if (is_wp_error($token)) { if ($error = $token->get_error_code()) { Jetpack::state('error', $error); } else { Jetpack::state('error', 'invalid_token'); } Jetpack::state('error_description', $token->get_error_message()); break; } if (!$token) { Jetpack::state('error', 'no_token'); break; } $is_master_user = !Jetpack::is_active(); Jetpack::update_user_token($current_user_id, sprintf('%s.%d', $token, $current_user_id), $is_master_user); if ($is_master_user) { Jetpack::state('message', 'authorized'); } else { Jetpack::state('message', 'linked'); // Don't activate anything since we are just connecting a user. break; } if ($active_modules = Jetpack_Options::get_option('active_modules')) { Jetpack_Options::delete_option('active_modules'); Jetpack::activate_default_modules(999, 1, $active_modules); } else { Jetpack::activate_default_modules(); } $jetpack->sync->register('noop'); // Spawn a sync to make sure the Jetpack Servers know what modules are active. // Start nonce cleaner wp_clear_scheduled_hook('jetpack_clean_nonces'); wp_schedule_event(time(), 'hourly', 'jetpack_clean_nonces'); } while (false); if (wp_validate_redirect($redirect)) { wp_safe_redirect($redirect); } else { wp_safe_redirect(Jetpack::admin_url()); } exit; }
function _returns_true_when_option_matches_expected() { Jetpack_Options::update_option('sync_error_idc', Jetpack::get_sync_error_idc_option()); $this->assertTrue(Jetpack::validate_sync_error_idc_option()); Jetpack_Options::delete_option('sync_error_idc'); }
/** * Disconnects from the Jetpack servers. * Forgets all connection details and tells the Jetpack servers to do the same. * @static */ public static function disconnect($update_activated_state = true) { wp_clear_scheduled_hook('jetpack_clean_nonces'); Jetpack::clean_nonces(true); Jetpack::load_xml_rpc_client(); $xml = new Jetpack_IXR_Client(); $xml->query('jetpack.deregister'); Jetpack_Options::delete_option(array('register', 'blog_token', 'user_token', 'user_tokens', 'master_user', 'time_diff', 'fallback_no_verify_ssl_certs')); if ($update_activated_state) { Jetpack_Options::update_option('activated', 4); } if ($jetpack_unique_connection = Jetpack_Options::get_option('unique_connection')) { // Check then record unique disconnection if site has never been disconnected previously if (-1 == $jetpack_unique_connection['disconnected']) { $jetpack_unique_connection['disconnected'] = 1; } else { if (0 == $jetpack_unique_connection['disconnected']) { //track unique disconnect $jetpack = Jetpack::init(); $jetpack->stat('connections', 'unique-disconnect'); $jetpack->do_stats('server_side'); } // increment number of times disconnected $jetpack_unique_connection['disconnected'] += 1; } Jetpack_Options::update_option('unique_connection', $jetpack_unique_connection); } // Delete all the sync related data. Since it could be taking up space. require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-sender.php'; Jetpack_Sync_Sender::get_instance()->uninstall(); // Disable the Heartbeat cron Jetpack_Heartbeat::init()->deactivate(); }
/** * Clears all IDC specific options. This method is used on disconnect and reconnect. */ static function clear_all_idc_options() { Jetpack_Options::delete_option(array('sync_error_idc', 'safe_mode_confirmed', 'migrate_for_idc')); }
function jetpack_sync_core_icon() { if (function_exists('get_site_icon_url')) { $url = get_site_icon_url(); } else { return; } require_once JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php'; // If there's a core icon, maybe update the option. If not, fall back to Jetpack's. if (!empty($url) && $url !== jetpack_site_icon_url()) { // This is the option that is synced with dotcom Jetpack_Options::update_option('site_icon_url', $url); } else { if (empty($url)) { Jetpack_Options::delete_option('site_icon_url'); } } }
function jetpack_custom_css_undo_data_migration_cli() { Jetpack_Options::delete_option('custom_css_4.7_migration'); WP_CLI::success(__('Option deleted, re-migrate via `wp jetpack custom-css migrate`.', 'jetpack')); }
/** * Disconnects from the Jetpack servers. * Forgets all connection details and tells the Jetpack servers to do the same. * @static */ public static function disconnect($update_activated_state = true) { wp_clear_scheduled_hook('jetpack_clean_nonces'); Jetpack::clean_nonces(true); Jetpack::load_xml_rpc_client(); $xml = new Jetpack_IXR_Client(); $xml->query('jetpack.deregister'); Jetpack_Options::delete_option(array('register', 'blog_token', 'user_token', 'user_tokens', 'master_user', 'time_diff', 'fallback_no_verify_ssl_certs')); if ($update_activated_state) { Jetpack_Options::update_option('activated', 4); } $jetpack_unique_connection = Jetpack_Options::get_option('unique_connection'); // Check then record unique disconnection if site has never been disconnected previously if ($jetpack_unique_connection['disconnected'] < 1) { //track unique disconnect $jetpack = Jetpack::init(); $jetpack->stat('connections', 'unique-disconnect'); $jetpack->do_stats('server_side'); } // increment number of times disconnected $jetpack_unique_connection['disconnected'] += 1; Jetpack_Options::update_option('unique_connection', $jetpack_unique_connection); // Disable the Heartbeat cron Jetpack_Heartbeat::init()->deactivate(); }
/** * Delete the blavatar and all the attached data * * @param $id * * @return mixed */ public static function delete_site_icon($id) { // We add the filter to make sure that we also delete all the added images add_filter('intermediate_image_sizes', array('Jetpack_Site_Icon', 'intermediate_image_sizes')); wp_delete_attachment($id, true); remove_filter('intermediate_image_sizes', array('Jetpack_Site_Icon', 'intermediate_image_sizes')); // for good measure also self::delete_temporay_data(); // Delete the URL from the Jetpack Options array Jetpack_Options::delete_option('site_icon_url'); return Jetpack_Options::delete_option('site_icon_id'); }
function authorize($data = array()) { $redirect = isset($data['redirect']) ? esc_url_raw((string) $data['redirect']) : ''; $jetpack_unique_connection = Jetpack_Options::get_option('unique_connection'); // Checking if site has been active/connected previously before recording unique connection if (!$jetpack_unique_connection) { // jetpack_unique_connection option has never been set $jetpack_unique_connection = array('connected' => 0, 'disconnected' => 0, 'version' => '3.6.1'); update_option('jetpack_unique_connection', $jetpack_unique_connection); //track unique connection $jetpack = Jetpack::init(); $jetpack->stat('connections', 'unique-connection'); $jetpack->do_stats('server_side'); } // increment number of times connected $jetpack_unique_connection['connected'] += 1; Jetpack_Options::update_option('unique_connection', $jetpack_unique_connection); do { $jetpack = $this->get_jetpack(); $role = $jetpack->translate_current_user_to_role(); if (!$role) { return new Jetpack_Error('no_role', 'Invalid request.', 400); } $cap = $jetpack->translate_role_to_cap($role); if (!$cap) { return new Jetpack_Error('no_cap', 'Invalid request.', 400); } if (!empty($data['error'])) { return new Jetpack_Error($data['error'], 'Error included in the request.', 400); } if (!isset($data['state'])) { return new Jetpack_Error('no_state', 'Request must include state.', 400); } if (!ctype_digit($data['state'])) { return new Jetpack_Error($data['error'], 'State must be an integer.', 400); } $current_user_id = get_current_user_id(); if ($current_user_id != $data['state']) { return new Jetpack_Error('wrong_state', 'State does not match current user.', 400); } if (empty($data['code'])) { return new Jetpack_Error('no_code', 'Request must include an authorization code.', 400); } $token = $this->get_token($data); if (is_wp_error($token)) { $code = $token->get_error_code(); if (empty($code)) { $code = 'invalid_token'; } return new Jetpack_Error($code, $token->get_error_message(), 400); } if (!$token) { return new Jetpack_Error('no_token', 'Error generating token.', 400); } $is_master_user = !Jetpack::is_active(); Jetpack::update_user_token($current_user_id, sprintf('%s.%d', $token, $current_user_id), $is_master_user); if (!$is_master_user) { // Don't activate anything since we are just connecting a user. return 'linked'; } $redirect_on_activation_error = 'client' === $data['auth_type'] ? true : false; if ($active_modules = Jetpack_Options::get_option('active_modules')) { Jetpack_Options::delete_option('active_modules'); Jetpack::activate_default_modules(999, 1, $active_modules, $redirect_on_activation_error); } else { Jetpack::activate_default_modules(false, false, array(), $redirect_on_activation_error); } // Sync all registers options and constants /** This action is documented in class.jetpack.php */ do_action('jetpack_sync_all_registered_options'); // Start nonce cleaner wp_clear_scheduled_hook('jetpack_clean_nonces'); wp_schedule_event(time(), 'hourly', 'jetpack_clean_nonces'); } while (false); return 'authorized'; }
/** * Runs when the VideoPress module is deactivated. */ public static function delete_options() { Jetpack_Options::delete_option(self::$option_name); self::$options = array(); }
function test_home_site_urls_synced_while_migrate_for_idc_set() { delete_transient(Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME); delete_option(Jetpack_Sync_Module_Callables::CALLABLES_CHECKSUM_OPTION_NAME); $home_option = get_option('home'); $siteurl_option = get_option('siteurl'); $main_network = network_site_url(); // First, let's see if the original values get synced $this->sender->do_sync(); $this->assertEquals($home_option, $this->server_replica_storage->get_callable('home_url')); $this->assertEquals($siteurl_option, $this->server_replica_storage->get_callable('site_url')); $this->assertEquals($main_network, $this->server_replica_storage->get_callable('main_network_site')); // Second, let's make sure that values don't get synced again if the migrate_for_idc option is not set $this->server_replica_storage->reset(); delete_transient(Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME); $this->sender->do_sync(); $this->assertEquals(null, $this->server_replica_storage->get_callable('home_url')); $this->assertEquals(null, $this->server_replica_storage->get_callable('site_url')); $this->assertEquals(null, $this->server_replica_storage->get_callable('main_network_site')); // Third, let's test that values get syncd with the option set Jetpack_Options::update_option('migrate_for_idc', true); $this->server_replica_storage->reset(); delete_transient(Jetpack_Sync_Module_Callables::CALLABLES_AWAIT_TRANSIENT_NAME); $this->sender->do_sync(); $this->assertEquals($home_option, $this->server_replica_storage->get_callable('home_url')); $this->assertEquals($siteurl_option, $this->server_replica_storage->get_callable('site_url')); $this->assertEquals($main_network, $this->server_replica_storage->get_callable('main_network_site')); Jetpack_Options::delete_option('migrate_for_idc'); }
/** * Deletes the given option. May be passed multiple option names as an array. * Updates jetpack_options and/or deletes jetpack_$name as appropriate. * * @param string|array $names */ public static function delete_option($names) { return Jetpack_Options::delete_option($names); }
/** * @return WP_Error|string secret_2 on success, WP_Error( error_code => error_code, error_message => error description, error_data => status code ) on failure * * Possible error_codes: * * verify_secret_1_missing * verify_secret_1_malformed * verify_secrets_missing: No longer have verification secrets stored * verify_secrets_mismatch: stored secret_1 does not match secret_1 sent by Jetpack.WordPress.com * * The 'authorize' and 'register' actions have additional error codes * * state_missing: a state ( user id ) was not supplied * state_malformed: state is not the correct data type * invalid_state: supplied state does not match the stored state */ function verify_action($params) { $action = $params[0]; $verify_secret = $params[1]; $state = isset($params[2]) ? $params[2] : ''; if (empty($verify_secret)) { return $this->error(new Jetpack_Error('verify_secret_1_missing', sprintf('The required "%s" parameter is missing.', 'secret_1'), 400)); } else { if (!is_string($verify_secret)) { return $this->error(new Jetpack_Error('verify_secret_1_malformed', sprintf('The required "%s" parameter is malformed.', 'secret_1'), 400)); } } $secrets = Jetpack_Options::get_option($action); if (!$secrets || is_wp_error($secrets)) { Jetpack_Options::delete_option($action); return $this->error(new Jetpack_Error('verify_secrets_missing', 'Verification took too long', 400)); } @(list($secret_1, $secret_2, $secret_eol, $user_id) = explode(':', $secrets)); if (empty($secret_1) || empty($secret_2) || empty($secret_eol) || $secret_eol < time()) { Jetpack_Options::delete_option($action); return $this->error(new Jetpack_Error('verify_secrets_missing', 'Verification took too long', 400)); } if (!hash_equals($verify_secret, $secret_1)) { Jetpack_Options::delete_option($action); return $this->error(new Jetpack_Error('verify_secrets_mismatch', 'Secret mismatch', 400)); } if (in_array($action, array('authorize', 'register'))) { // 'authorize' and 'register' actions require further testing if (empty($state)) { return $this->error(new Jetpack_Error('state_missing', sprintf('The required "%s" parameter is missing.', 'state'), 400)); } else { if (!ctype_digit($state)) { return $this->error(new Jetpack_Error('state_malformed', sprintf('The required "%s" parameter is malformed.', 'state'), 400)); } } if (empty($user_id) || $user_id !== $state) { Jetpack_Options::delete_option($action); return $this->error(new Jetpack_Error('invalid_state', 'State is invalid', 400)); } } Jetpack_Options::delete_option($action); return $secret_2; }
/** * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. * * @return bool */ public static function validate_sync_error_idc_option() { $is_valid = false; $idc_allowed = get_transient('jetpack_idc_allowed'); if (false === $idc_allowed) { $response = wp_remote_get('https://jetpack.com/is-idc-allowed/'); if (200 === (int) wp_remote_retrieve_response_code($response)) { $json = json_decode(wp_remote_retrieve_body($response)); $idc_allowed = isset($json, $json->result) && $json->result ? '1' : '0'; $transient_duration = HOUR_IN_SECONDS; } else { // If the request failed for some reason, then assume IDC is allowed and set shorter transient. $idc_allowed = '1'; $transient_duration = 5 * MINUTE_IN_SECONDS; } set_transient('jetpack_idc_allowed', $idc_allowed, $transient_duration); } // Is the site opted in and does the stored sync_error_idc option match what we now generate? $sync_error = Jetpack_Options::get_option('sync_error_idc'); $local_options = self::get_sync_error_idc_option(); if ($idc_allowed && $sync_error && self::sync_idc_optin()) { if ($sync_error['home'] === $local_options['home'] && $sync_error['siteurl'] === $local_options['siteurl']) { $is_valid = true; } } /** * Filters whether the sync_error_idc option is valid. * * @since 4.4.0 * * @param bool $is_valid If the sync_error_idc is valid or not. */ $is_valid = (bool) apply_filters('jetpack_sync_error_idc_validation', $is_valid); if (!$idc_allowed || !$is_valid && $sync_error) { // Since the option exists, and did not validate, delete it Jetpack_Options::delete_option('sync_error_idc'); } return $is_valid; }
function test_get_random_connection_banner_value_if_not_set() { Jetpack_Options::delete_option('connection_banner_ab'); $this->assertNotEquals(false, Jetpack_Connection_Banner::get_random_connection_banner_value()); $this->assertNotEquals(false, Jetpack_Options::get_option('connection_banner_ab')); }
public static function resolve_identity_crisis_ajax_callback() { check_ajax_referer('resolve-identity-crisis', 'ajax-nonce'); switch ($_POST['crisis_resolution_action']) { case 'site_migrated': Jetpack::resolve_identity_crisis(); echo 'resolved'; break; case 'whitelist': Jetpack::whitelist_current_url(); echo 'whitelisted'; break; case 'reset_connection': // Delete the options first so it doesn't get confused which site to disconnect dotcom-side Jetpack_Options::delete_option(array('register', 'blog_token', 'user_token', 'user_tokens', 'master_user', 'time_diff', 'fallback_no_verify_ssl_certs', 'id')); delete_transient('jetpack_has_identity_crisis'); echo 'reset-connection-success'; break; default: echo 'missing action'; break; } wp_die(); }
function authorize() { $data = stripslashes_deep($_GET); $args = array(); $redirect = isset($data['redirect']) ? esc_url_raw((string) $data['redirect']) : ''; $jetpack_unique_connection = Jetpack_Options::get_option('unique_connection'); // Checking if site has been active/connected previously before recording unique connection if (!$jetpack_unique_connection) { // jetpack_unique_connection option has never been set $jetpack_unique_connection = array('connected' => 0, 'disconnected' => 0); update_option('jetpack_unique_connection', $jetpack_unique_connection); //track unique connection $jetpack = Jetpack::init(); $jetpack->stat('connections', 'unique-connection'); $jetpack->do_stats('server_side'); } // increment number of times connected $jetpack_unique_connection['connected'] += 1; Jetpack_Options::update_option('unique_connection', $jetpack_unique_connection); do { $jetpack = $this->get_jetpack(); $role = $jetpack->translate_current_user_to_role(); if (!$role) { Jetpack::state('error', 'no_role'); break; } $cap = $jetpack->translate_role_to_cap($role); if (!$cap) { Jetpack::state('error', 'no_cap'); break; } $this->check_admin_referer("jetpack-authorize_{$role}_{$redirect}"); if (!empty($data['error'])) { Jetpack::state('error', $data['error']); break; } if (empty($data['state'])) { Jetpack::state('error', 'no_state'); break; } if (!ctype_digit($data['state'])) { Jetpack::state('error', 'invalid_state'); break; } $current_user_id = get_current_user_id(); if ($current_user_id != $data['state']) { Jetpack::state('error', 'wrong_state'); break; } if (empty($data['code'])) { Jetpack::state('error', 'no_code'); break; } $token = $this->get_token($data); if (is_wp_error($token)) { if ($error = $token->get_error_code()) { Jetpack::state('error', $error); } else { Jetpack::state('error', 'invalid_token'); } Jetpack::state('error_description', $token->get_error_message()); break; } if (!$token) { Jetpack::state('error', 'no_token'); break; } $is_master_user = !Jetpack::is_active(); Jetpack::update_user_token($current_user_id, sprintf('%s.%d', $token, $current_user_id), $is_master_user); if ($is_master_user) { Jetpack::state('message', 'authorized'); } else { Jetpack::state('message', 'linked'); // Don't activate anything since we are just connecting a user. break; } if ($active_modules = Jetpack_Options::get_option('active_modules')) { Jetpack_Options::delete_option('active_modules'); Jetpack::activate_default_modules(999, 1, $active_modules); } else { Jetpack::activate_default_modules(); } // Sync all registers options and constants do_action('jetpack_sync_all_registered_options'); // Start nonce cleaner wp_clear_scheduled_hook('jetpack_clean_nonces'); wp_schedule_event(time(), 'hourly', 'jetpack_clean_nonces'); } while (false); if (wp_validate_redirect($redirect)) { $this->wp_safe_redirect($redirect); } else { $this->wp_safe_redirect(Jetpack::admin_url()); } $this->do_exit(); }
/** * Disconnects from the Jetpack servers. * Forgets all connection details and tells the Jetpack servers to do the same. * @static */ public static function disconnect($update_activated_state = true) { wp_clear_scheduled_hook('jetpack_clean_nonces'); Jetpack::clean_nonces(true); Jetpack::load_xml_rpc_client(); $xml = new Jetpack_IXR_Client(); $xml->query('jetpack.deregister'); Jetpack_Options::delete_option(array('register', 'blog_token', 'user_token', 'user_tokens', 'master_user', 'time_diff', 'fallback_no_verify_ssl_certs')); if ($update_activated_state) { Jetpack_Options::update_option('activated', 4); } // Disable the Heartbeat cron Jetpack_Heartbeat::init()->deactivate(); }
/** * Checks whether the sync_error_idc option is valid or not, and if not, will do cleanup. * * @return bool */ public static function validate_sync_error_idc_option() { $is_valid = false; $sync_error = Jetpack_Options::get_option('sync_error_idc'); // Is the site opted in and does the stored sync_error_idc option match what we now generate? if ($sync_error && self::sync_idc_optin()) { $error_diff = array_diff_assoc($sync_error, self::get_sync_error_idc_option()); if (empty($error_diff)) { $is_valid = true; } } /** * Filters whether the sync_error_idc option is valid. * * @since 4.4.0 * * @param bool $is_valid If the sync_error_idc is valid or not. */ $is_valid = (bool) apply_filters('jetpack_sync_error_idc_validation', $is_valid); if (!$is_valid && $sync_error) { // Since the option exists, and did not validate, delete it Jetpack_Options::delete_option('sync_error_idc'); } return $is_valid; }