← All changes
|
_inc/lib/core-api/wpcom-endpoints/subscribers.php
+113
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,113 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | +/** | |
| 3 | + * Get subscriber count from Jetpack's Subscriptions module. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack | |
| 6 | + */ | |
| 7 | +use Automattic\Jetpack\Constants; | |
| 8 | + | |
| 9 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 10 | + exit( 0 ); | |
| 11 | +} | |
| 12 | + | |
| 13 | +/** | |
| 14 | + * Subscribers: Get subscriber count | |
| 15 | + * | |
| 16 | + * @since 6.9 | |
| 17 | + */ | |
| 18 | +class 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 | + | |
| 108 | +if ( | |
| 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 | +} | |