Jetpack_Subscriptions::subscribe PHP Method

subscribe() public method

Send a synchronous XML-RPC subscribe to blog posts or subscribe to post comments request.
public subscribe ( string $email, array $post_ids, boolean $async = true, $extra_data = [] ) : true | Jetpack_Error
$email string
$post_ids array (optional) defaults to 0 for blog posts only: array of post IDs to subscribe to blog's posts
$async boolean (optional) Should the subscription be performed asynchronously? Defaults to true.
return true | Jetpack_Error true on success invalid_email : not a valid email address invalid_post_id : not a valid post ID unknown_post_id : unknown post not_subscribed : strange error. Jetpack servers at WordPress.com could subscribe the email. disabled : Site owner has disabled subscriptions. active : Already subscribed. unknown : strange error. Jetpack servers at WordPress.com returned something malformed. unknown_status : strange error. Jetpack servers at WordPress.com returned something I didn't understand.
    function subscribe($email, $post_ids = 0, $async = true, $extra_data = array())
    {
        if (!is_email($email)) {
            return new Jetpack_Error('invalid_email');
        }
        if (!$async) {
            Jetpack::load_xml_rpc_client();
            $xml = new Jetpack_IXR_ClientMulticall();
        }
        foreach ((array) $post_ids as $post_id) {
            $post_id = (int) $post_id;
            if ($post_id < 0) {
                return new Jetpack_Error('invalid_post_id');
            } else {
                if ($post_id && !($post = get_post($post_id))) {
                    return new Jetpack_Error('unknown_post_id');
                }
            }
            if ($async) {
                Jetpack::xmlrpc_async_call('jetpack.subscribeToSite', $email, $post_id, serialize($extra_data));
            } else {
                $xml->addCall('jetpack.subscribeToSite', $email, $post_id, serialize($extra_data));
            }
        }
        if ($async) {
            return;
        }
        // Call
        $xml->query();
        if ($xml->isError()) {
            return $xml->get_jetpack_error();
        }
        $responses = $xml->getResponse();
        $r = array();
        foreach ((array) $responses as $response) {
            if (isset($response['faultCode']) || isset($response['faultString'])) {
                $r[] = $xml->get_jetpack_error($response['faultCode'], $response['faultString']);
                continue;
            }
            if (!is_array($response[0]) || empty($response[0]['status'])) {
                $r[] = new Jetpack_Error('unknown');
                continue;
            }
            switch ($response[0]['status']) {
                case 'error':
                    $r[] = new Jetpack_Error('not_subscribed');
                    continue 2;
                case 'disabled':
                    $r[] = new Jetpack_Error('disabled');
                    continue 2;
                case 'active':
                    $r[] = new Jetpack_Error('active');
                    continue 2;
                case 'pending':
                    $r[] = true;
                    continue 2;
                default:
                    $r[] = new Jetpack_Error('unknown_status', (string) $response[0]['status']);
                    continue 2;
            }
        }
        return $r;
    }

Usage Example

Example #1
0
 /**
  * Check Jetpack Subscription
  * @since 1.0.5
  * @version 1.0
  */
 protected function check_jetpack_subscription($email = NULL, $post_ids = NULL)
 {
     if ($email === NULL) {
         return 'missing';
     }
     if (!class_exists('Jetpack') && defined('JETPACK__PLUGIN_DIR')) {
         require_once JETPACK__PLUGIN_DIR . 'jetpack.php';
     }
     if (!class_exists('Jetpack_Subscriptions') && defined('JETPACK__PLUGIN_DIR')) {
         require_once JETPACK__PLUGIN_DIR . 'modules/subscriptions.php';
     }
     if ($post_ids === NULL) {
         $subscribe = Jetpack_Subscriptions::subscribe($email, 0, false);
     } else {
         $subscribe = Jetpack_Subscriptions::subscribe($email, $post_ids, false);
     }
     if (is_wp_error($subscribe)) {
         $error = $subscribe->get_error_code();
     } else {
         $error = false;
         foreach ($subscribe as $response) {
             if (is_wp_error($response)) {
                 $error = $response->get_error_code();
                 break;
             }
         }
     }
     if ($error) {
         switch ($error) {
             case 'invalid_email':
                 $return = 'invalid';
                 break;
             case 'active':
                 $return = 'active';
                 break;
             case 'pending':
                 $return = 'pending';
                 break;
             default:
                 $return = '';
                 break;
         }
     } else {
         if (is_array($subscribe) && $subscribe[0] === true) {
             $error = true;
         }
         $return = 'pending';
     }
     if ($error) {
         return $return;
     }
     return 'new';
 }
All Usage Examples Of Jetpack_Subscriptions::subscribe