Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
73.91% covered (warning)
73.91%
34 / 46
40.00% covered (danger)
40.00%
2 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Subscribers
82.93% covered (warning)
82.93%
34 / 41
40.00% covered (danger)
40.00%
2 / 5
8.32
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
71.43% covered (warning)
71.43%
5 / 7
0.00% covered (danger)
0.00%
0 / 1
2.09
 get_subscriber_counts
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
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
13/**
14 * Subscribers: Get subscriber count
15 *
16 * @since 6.9
17 */
18class WPCOM_REST_API_V2_Endpoint_Subscribers extends WP_REST_Controller {
19    /**
20     * Constructor.
21     */
22    public function __construct() {
23        $this->namespace = 'wpcom/v2';
24        $this->rest_base = 'subscribers';
25        // This endpoint *does not* need to connect directly to Jetpack sites.
26        $this->wpcom_is_wpcom_only_endpoint = true;
27        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
28    }
29
30    /**
31     * Register API routes.
32     */
33    public function register_routes() {
34        // GET /sites/<blog_id>/subscribers/count - Return number of subscribers for this site.
35        register_rest_route(
36            $this->namespace,
37            '/' . $this->rest_base . '/count',
38            array(
39                array(
40                    'methods'             => WP_REST_Server::READABLE,
41                    'callback'            => array( $this, 'get_subscriber_count' ),
42                    'permission_callback' => array( $this, 'readable_permission_check' ),
43                ),
44            )
45        );
46        // GET /sites/<blog_id>/subscriber/counts - Return splitted number of subscribers for this site
47        register_rest_route(
48            $this->namespace,
49            '/' . $this->rest_base . '/counts',
50            array(
51                array(
52                    'methods'             => WP_REST_Server::READABLE,
53                    'callback'            => array( $this, 'get_subscriber_counts' ),
54                    'permission_callback' => array( $this, 'readable_permission_check' ),
55                ),
56            )
57        );
58    }
59
60    /**
61     * Permission check. Only authors can access this endpoint.
62     */
63    public function readable_permission_check() {
64
65        if ( ! current_user_can_for_site( get_current_blog_id(), 'edit_posts' ) ) {
66            return new WP_Error( 'authorization_required', 'Only users with the permission to edit posts can see the subscriber count.', array( 'status' => 401 ) );
67        }
68
69        return true;
70    }
71
72    /**
73     * Retrieves subscriber count
74     *
75     * @param WP_REST_Request $request incoming API request info.
76     * @return array data object containing subscriber count
77     */
78    public function get_subscriber_count( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
79        // Get the most up to date subscriber count when request is not a test.
80        if ( ! Constants::is_defined( 'TESTING_IN_JETPACK' ) ) {
81            delete_transient( 'wpcom_subscribers_total' );
82            delete_transient( 'wpcom_subscribers_total_no_publicize' );
83        }
84        $subscriber_count = Jetpack_Subscriptions_Widget::fetch_subscriber_count();
85
86        return array(
87            'count' => $subscriber_count,
88        );
89    }
90
91    /**
92     * Retrieves splitted subscriber counts
93     *
94     * @return array data object containing subscriber counts.
95     */
96    public function get_subscriber_counts() {
97        if ( ! Constants::is_defined( 'TESTING_IN_JETPACK' ) ) {
98            delete_transient( 'wpcom_subscribers_totals' );
99        }
100
101        $subscriber_info   = Automattic\Jetpack\Extensions\Subscriptions\fetch_subscriber_counts();
102        $subscriber_counts = $subscriber_info['value'];
103
104        return array( 'counts' => $subscriber_counts );
105    }
106}
107
108if (
109    Jetpack::is_module_active( 'subscriptions' ) ||
110    ( Constants::is_defined( 'TESTING_IN_JETPACK' ) && Constants::get_constant( 'TESTING_IN_JETPACK' ) )
111) {
112    wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Subscribers' );
113}