Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.19% covered (warning)
76.19%
32 / 42
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Subscribers
88.89% covered (warning)
88.89%
32 / 36
80.00% covered (warning)
80.00%
4 / 5
6.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 register_routes
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
1 / 1
1
 readable_permission_check
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_subscriber_count
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 get_subscriber_counts
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Get subscriber count from Jetpack's Subscriptions module.
4 *
5 * @package automattic/jetpack
6 */
7use Automattic\Jetpack\Constants;
8
9if ( ! defined( 'ABSPATH' ) ) {
10    exit( 0 );
11}
12
13require_once dirname( __DIR__, 4 ) . '/modules/subscriptions/class-jetpack-subscriber-data.php';
14
15/**
16 * Subscribers: Get subscriber count
17 *
18 * @since 6.9
19 */
20class WPCOM_REST_API_V2_Endpoint_Subscribers extends WP_REST_Controller {
21    /**
22     * Constructor.
23     */
24    public function __construct() {
25        $this->namespace = 'wpcom/v2';
26        $this->rest_base = 'subscribers';
27        // This endpoint *does not* need to connect directly to Jetpack sites.
28        $this->wpcom_is_wpcom_only_endpoint = true;
29        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
30    }
31
32    /**
33     * Register API routes.
34     */
35    public function register_routes() {
36        // GET /sites/<blog_id>/subscribers/count - Return number of subscribers for this site.
37        register_rest_route(
38            $this->namespace,
39            '/' . $this->rest_base . '/count',
40            array(
41                array(
42                    'methods'             => WP_REST_Server::READABLE,
43                    'callback'            => array( $this, 'get_subscriber_count' ),
44                    'permission_callback' => array( $this, 'readable_permission_check' ),
45                ),
46            )
47        );
48        // GET /sites/<blog_id>/subscriber/counts - Return splitted number of subscribers for this site
49        register_rest_route(
50            $this->namespace,
51            '/' . $this->rest_base . '/counts',
52            array(
53                array(
54                    'methods'             => WP_REST_Server::READABLE,
55                    'callback'            => array( $this, 'get_subscriber_counts' ),
56                    'permission_callback' => array( $this, 'readable_permission_check' ),
57                ),
58            )
59        );
60    }
61
62    /**
63     * Permission check. Only authors can access this endpoint.
64     */
65    public function readable_permission_check() {
66
67        if ( ! current_user_can_for_site( get_current_blog_id(), 'edit_posts' ) ) {
68            return new WP_Error( 'authorization_required', 'Only users with the permission to edit posts can see the subscriber count.', array( 'status' => 401 ) );
69        }
70
71        return true;
72    }
73
74    /**
75     * Retrieves subscriber count
76     *
77     * @param WP_REST_Request $request incoming API request info.
78     * @return array data object containing subscriber count
79     */
80    public function get_subscriber_count( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
81        $refresh = ! Constants::is_defined( 'TESTING_IN_JETPACK' );
82
83        return array(
84            'count' => Jetpack_Subscriber_Data::get_count( $refresh ),
85        );
86    }
87
88    /**
89     * Retrieves splitted subscriber counts
90     *
91     * @return array data object containing subscriber counts.
92     */
93    public function get_subscriber_counts() {
94        $refresh         = ! Constants::is_defined( 'TESTING_IN_JETPACK' );
95        $subscriber_info = Jetpack_Subscriber_Data::get_counts( $refresh );
96
97        return array( 'counts' => $subscriber_info['value'] );
98    }
99}
100
101if (
102    Jetpack::is_module_active( 'subscriptions' ) ||
103    ( Constants::is_defined( 'TESTING_IN_JETPACK' ) && Constants::get_constant( 'TESTING_IN_JETPACK' ) )
104) {
105    wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Subscribers' );
106}