diff --git a/_inc/lib/core-api/wpcom-endpoints/subscribers.php b/_inc/lib/core-api/wpcom-endpoints/subscribers.php new file mode 100644 index 0000000000000..558091e6c6776 --- /dev/null +++ b/_inc/lib/core-api/wpcom-endpoints/subscribers.php @@ -0,0 +1,55 @@ +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//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(); + $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' ); +} diff --git a/tests/php/core-api/wpcom-fields/test_class.subscribers-endpoint.php b/tests/php/core-api/wpcom-fields/test_class.subscribers-endpoint.php new file mode 100644 index 0000000000000..d89bc1edc3752 --- /dev/null +++ b/tests/php/core-api/wpcom-fields/test_class.subscribers-endpoint.php @@ -0,0 +1,43 @@ +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 ); + } + +}