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;
}