public function get_protect_key()
{
$protect_blog_id = Jetpack_Protect_Module::get_main_blog_jetpack_id();
// If we can't find the the blog id, that means we are on multisite, and the main site never connected
// the protect api key is linked to the main blog id - instruct the user to connect their main blog
if (!$protect_blog_id) {
$this->api_key_error = __('Your main blog is not connected to WordPress.com. Please connect to get an API key.', 'jetpack');
return false;
}
$request = array('jetpack_blog_id' => $protect_blog_id, 'bruteprotect_api_key' => get_site_option('bruteprotect_api_key'), 'multisite' => '0');
// Send the number of blogs on the network if we are on multisite
if (is_multisite()) {
$request['multisite'] = get_blog_count();
if (!$request['multisite']) {
global $wpdb;
$request['multisite'] = $wpdb->get_var("SELECT COUNT(blog_id) as c FROM {$wpdb->blogs} WHERE spam = '0' AND deleted = '0' and archived = '0'");
}
}
// Request the key
Jetpack::load_xml_rpc_client();
$xml = new Jetpack_IXR_Client(array('user_id' => get_current_user_id()));
$xml->query('jetpack.protect.requestKey', $request);
// Hmm, can't talk to wordpress.com
if ($xml->isError()) {
$code = $xml->getErrorCode();
$message = $xml->getErrorMessage();
$this->api_key_error = sprintf(__('Error connecting to WordPress.com. Code: %1$s, %2$s', 'jetpack'), $code, $message);
return false;
}
$response = $xml->getResponse();
// Hmm. Can't talk to the protect servers ( api.bruteprotect.com )
if (!isset($response['data'])) {
$this->api_key_error = __('No reply from Jetpack servers', 'jetpack');
return false;
}
// There was an issue generating the key
if (empty($response['success'])) {
$this->api_key_error = $response['data'];
return false;
}
// Key generation successful!
$active_plugins = Jetpack::get_active_plugins();
// We only want to deactivate BruteProtect if we successfully get a key
if (in_array('bruteprotect/bruteprotect.php', $active_plugins)) {
Jetpack_Client_Server::deactivate_plugin('bruteprotect/bruteprotect.php', 'BruteProtect');
}
$key = $response['data'];
update_site_option('jetpack_protect_key', $key);
return $key;
}