Jetpack::register PHP Method

register() public static method

public static register ( ) : boolean | WP_Error
return boolean | WP_Error
    public static function register()
    {
        add_action('pre_update_jetpack_option_register', array('Jetpack_Options', 'delete_option'));
        $secrets = Jetpack::init()->generate_secrets('register');
        @(list($secret_1, $secret_2, $secret_eol) = explode(':', $secrets));
        if (empty($secret_1) || empty($secret_2) || empty($secret_eol) || $secret_eol < time()) {
            return new Jetpack_Error('missing_secrets');
        }
        $timeout = Jetpack::init()->get_remote_query_timeout_limit();
        $gmt_offset = get_option('gmt_offset');
        if (!$gmt_offset) {
            $gmt_offset = 0;
        }
        $stats_options = get_option('stats_options');
        $stats_id = isset($stats_options['blog_id']) ? $stats_options['blog_id'] : null;
        $args = array('method' => 'POST', 'body' => array('siteurl' => site_url(), 'home' => home_url(), 'gmt_offset' => $gmt_offset, 'timezone_string' => (string) get_option('timezone_string'), 'site_name' => (string) get_option('blogname'), 'secret_1' => $secret_1, 'secret_2' => $secret_2, 'site_lang' => get_locale(), 'timeout' => $timeout, 'stats_id' => $stats_id, 'state' => get_current_user_id()), 'headers' => array('Accept' => 'application/json'), 'timeout' => $timeout);
        $response = Jetpack_Client::_wp_remote_request(Jetpack::fix_url_for_bad_hosts(Jetpack::api_url('register')), $args, true);
        // Make sure the response is valid and does not contain any Jetpack errors
        $valid_response = Jetpack::init()->validate_remote_register_response($response);
        if (is_wp_error($valid_response) || !$valid_response) {
            return $valid_response;
        }
        // Grab the response values to work with
        $code = wp_remote_retrieve_response_code($response);
        $entity = wp_remote_retrieve_body($response);
        if ($entity) {
            $json = json_decode($entity);
        } else {
            $json = false;
        }
        if (empty($json->jetpack_secret) || !is_string($json->jetpack_secret)) {
            return new Jetpack_Error('jetpack_secret', '', $code);
        }
        if (isset($json->jetpack_public)) {
            $jetpack_public = (int) $json->jetpack_public;
        } else {
            $jetpack_public = false;
        }
        Jetpack_Options::update_options(array('id' => (int) $json->jetpack_id, 'blog_token' => (string) $json->jetpack_secret, 'public' => $jetpack_public));
        /**
         * Fires when a site is registered on WordPress.com.
         *
         * @since 3.7.0
         *
         * @param int $json->jetpack_id Jetpack Blog ID.
         * @param string $json->jetpack_secret Jetpack Blog Token.
         * @param int|bool $jetpack_public Is the site public.
         */
        do_action('jetpack_site_registered', $json->jetpack_id, $json->jetpack_secret, $jetpack_public);
        // Initialize Jump Start for the first and only time.
        if (!Jetpack_Options::get_option('jumpstart')) {
            Jetpack_Options::update_option('jumpstart', 'new_connection');
            $jetpack = Jetpack::init();
            $jetpack->stat('jumpstart', 'unique-views');
            $jetpack->do_stats('server_side');
        }
        return true;
    }

Usage Example

 /**
  * Attempts Jetpack registration.  If it fail, a state flag is set: @see ::admin_page_load()
  */
 public static function try_registration()
 {
     $result = Jetpack::register();
     // If there was an error with registration and the site was not registered, record this so we can show a message.
     if (!$result || is_wp_error($result)) {
         return $result;
     } else {
         return true;
     }
 }
All Usage Examples Of Jetpack::register
Jetpack