Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Subscriber API Endpoint #10741

Merged
merged 12 commits into from
Nov 29, 2018
55 changes: 55 additions & 0 deletions _inc/lib/core-api/wpcom-endpoints/subscribers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* Subscribers: Get subscriber count
*
* @since 6.9
*/
class WPCOM_REST_API_V2_Endpoint_Subscribers extends WP_REST_Controller {
function __construct() {
$this->namespace = 'wpcom/v2';
$this->rest_base = 'subscribers';
// This endpoint *does not* need to connect directly to Jetpack sites.
$this->wpcom_is_wpcom_only_endpoint = true;
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}

public function register_routes() {
// GET /sites/<blog_id>/subscribers/count - Return number of subscribers for this site.
register_rest_route( $this->namespace, '/' . $this->rest_base . '/count', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_subscriber_count' ),
'permission_callback' => array( $this, 'readable_permission_check' ),
)
) );
}

public function readable_permission_check() {
if ( ! current_user_can_for_blog( get_current_blog_id(), 'edit_posts' ) ) {
return new WP_Error( 'authorization_required', 'Only users with the permission to edit posts can see the subscriber count.', array( 'status' => 401 ) );
}

return true;
}

/**
* Retrieves subscriber count
*
* @param WP_REST_Request $request incoming API request info
* @return array data object containing subscriber count
*/
public function get_subscriber_count( $request ) {
$subscriptions = new Jetpack_Subscriptions_Widget();
gititon marked this conversation as resolved.
Show resolved Hide resolved
$subscriber_info = $subscriptions->fetch_subscriber_count();
$subscriber_count = $subscriber_info['value'];

return array(
'count' => $subscriber_count
);
}
}

if ( Jetpack::is_module_active( 'subscriptions ') || ( defined( 'TESTING_IN_JETPACK' ) && TESTING_IN_JETPACK ) ) {
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Subscribers' );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

require_once JETPACK__PLUGIN_DIR . '/tests/php/lib/class-wp-test-jetpack-rest-testcase.php';
require_once JETPACK__PLUGIN_DIR . '/tests/php/lib/class-wp-test-spy-rest-server.php';
gititon marked this conversation as resolved.
Show resolved Hide resolved

/**
* @group publicize
* @group rest-api
*/
class Test_WPCOM_REST_API_V2_Subscribers_Endpoint extends WP_Test_Jetpack_REST_Testcase {

static $editor_user_id;
static $subscriber_user_id;

public static function wpSetUpBeforeClass( $factory ) {
self::$editor_user_id = $factory->user->create( array( 'role' => 'editor' ) );
self::$subscriber_user_id = $factory->user->create( array( 'role' => 'subscriber' ) );

set_transient( 'wpcom_subscribers_total', array('value' => 100, 'status' => 'success' ) );
}

public function test_get_subscriber_count_with_edit_permission() {
wp_set_current_user( self::$editor_user_id );

$request = new WP_REST_Request( WP_REST_Server::READABLE, '/wpcom/v2/subscribers/count' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();

$this->assertEquals( $data['count'], 100 );
}

public function test_get_subscriber_count_without_edit_permission() {
wp_set_current_user( self::$subscriber_user_id );

$request = new WP_REST_Request( WP_REST_Server::READABLE, '/wpcom/v2/subscribers/count' );
$response = $this->server->dispatch( $request );
$data = $response->get_data();

$this->assertFalse( isset( $data['count'] ) );
$this->assertEquals( $data['data']['status'], 401 );
}

}