function ui_load()
{
if (!current_user_can('manage_options')) {
return;
}
// run code that might be updating the registration key
if (isset($_POST['action']) && 'register' == $_POST['action']) {
check_admin_referer('vaultpress_register');
// reset the connection info so messages don't cross
$this->clear_connection();
// if registering via Jetpack, get a key...
if (isset($_POST['key_source']) && 'jetpack' === $_POST['key_source']) {
$registration_key = $this->register_via_jetpack();
if (is_wp_error($registration_key)) {
$this->update_option('connection_error_code', -2);
$this->update_option('connection_error_message', sprintf(__('<strong>Failed to register VaultPress via Jetpack</strong>: %s. If you’re still having issues please <a href="%1$s">contact the VaultPress Safekeepers</a>.', 'vaultpress'), esc_html($registration_key->get_error_message()), 'http://vaultpress.com/contact/'));
wp_redirect(admin_url('admin.php?page=vaultpress&error=true'));
exit;
}
} else {
$registration_key = trim($_POST['registration_key']);
}
if (empty($registration_key)) {
$this->update_option('connection_error_code', 1);
$this->update_option('connection_error_message', sprintf(__('<strong>That\'s not a valid registration key.</strong> Head over to the <a href="%1$s" title="Sign in to your VaultPress Dashboard">VaultPress Dashboard</a> to find your key.', 'vaultpress'), 'https://dashboard.vaultpress.com/'));
wp_redirect(admin_url('admin.php?page=vaultpress&error=true'));
exit;
}
// try to register the plugin
$nonce = wp_create_nonce('vp_register_' . $registration_key);
$args = array('registration_key' => $registration_key, 'nonce' => $nonce);
$response = $this->contact_service('register', $args);
// we received an error from the VaultPress servers
if (!empty($response['faultCode'])) {
$this->update_option('connection_error_code', $response['faultCode']);
$this->update_option('connection_error_message', $response['faultString']);
wp_redirect(admin_url('admin.php?page=vaultpress&error=true'));
exit;
}
// make sure the returned data looks valid
if (empty($response['key']) || empty($response['secret']) || empty($response['nonce']) || $nonce != $response['nonce']) {
$this->update_option('connection_error_code', 1);
$this->update_option('connection_error_message', sprintf(__('There was a problem trying to register your subscription. Please try again. If you’re still having issues please <a href="%1$s">contact the VaultPress Safekeepers</a>.', 'vaultpress'), 'http://vaultpress.com/contact/'));
wp_redirect(admin_url('admin.php?page=vaultpress&error=true'));
exit;
}
// need to update these values in the db so the servers can try connecting to the plugin
$this->update_option('key', $response['key']);
$this->update_option('secret', $response['secret']);
if ($this->check_connection(true)) {
wp_redirect(admin_url('admin.php?page=vaultpress'));
exit;
}
// reset the key and secret
$this->update_option('key', '');
$this->update_option('secret', '');
wp_redirect(admin_url('admin.php?page=vaultpress&error=true'));
exit;
}
}