Example #1
0
 /**
  * Jetpack_Subscriptions::subscribe()
  *
  * Send a synchronous XML-RPC subscribe to blog posts or subscribe to post comments request.
  *
  * @param string $email
  * @param array  $post_ids (optional) defaults to 0 for blog posts only: array of post IDs to subscribe to blog's posts
  * @param bool   $async    (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;
 }
 private static function get_jetpack_user()
 {
     if (!class_exists('Jetpack')) {
         return false;
     }
     Jetpack::load_xml_rpc_client();
     $xml = new Jetpack_IXR_ClientMulticall(array('user_id' => get_current_user_id()));
     $xml->addCall('wpcom.getUserID');
     $xml->addCall('akismet.getAPIKey');
     $xml->query();
     Akismet::log(compact('xml'));
     if (!$xml->isError()) {
         $responses = $xml->getResponse();
         if (count($responses) > 1) {
             $api_key = array_shift($responses[0]);
             $user_id = (int) array_shift($responses[1]);
             return compact('api_key', 'user_id');
         }
     }
     return false;
 }
Example #3
0
 /**
  * Helper method for multicall XMLRPC.
  */
 function xmlrpc_async_call()
 {
     static $client = null;
     if (!isset($client)) {
         Jetpack::load_xml_rpc_client();
         $client = new Jetpack_IXR_ClientMulticall(array('user_id' => get_current_user_id()));
         ignore_user_abort(true);
         add_action('shutdown', array('Jetpack', 'xmlrpc_async_call'));
     }
     $args = func_get_args();
     if (!empty($args[0])) {
         call_user_func_array(array(&$client, 'addCall'), $args);
     } elseif (!empty($client->calls)) {
         $client->query();
     }
 }