PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.3
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.3
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-stats / src / class-rest-provider.php

class-rest-provider.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.3, at jetpack_vendor/automattic/jetpack-stats/src/class-rest-provider.php

91 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The Stats REST Provider class.
4 *
5 * @package @automattic/jetpack-stats
6 */
7
8 namespace Automattic\Jetpack\Stats;
9
10 use Automattic\Jetpack\Connection\Rest_Authentication;
11 use Automattic\Jetpack\Connection\REST_Connector;
12 use WP_Error;
13 use WP_REST_Server;
14
15 /**
16 * The REST API provider class.
17 *
18 * @since 0.12.0
19 */
20 class REST_Provider {
21 /**
22 * Singleton instance.
23 *
24 * @var REST_Provider
25 **/
26 private static $instance = null;
27
28 /**
29 * Private constructor.
30 *
31 * Use the static::init() method to get an instance.
32 */
33 private function __construct() {
34 add_action( 'rest_api_init', array( $this, 'initialize_rest_api' ) );
35 }
36
37 /**
38 * Initialize class and get back a singleton instance.
39 *
40 * @param bool $new_instance Force create new instance.
41 *
42 * @return static
43 */
44 public static function init( $new_instance = false ) {
45 if ( null === self::$instance || $new_instance ) {
46 self::$instance = new static();
47 }
48
49 return self::$instance;
50 }
51
52 /**
53 * Initialize the REST API.
54 *
55 * @return void
56 */
57 public function initialize_rest_api() {
58 register_rest_route(
59 'jetpack/v4',
60 '/stats/blog',
61 array(
62 'methods' => WP_REST_Server::READABLE,
63 'callback' => array( $this, 'get_blog' ),
64 'permission_callback' => array( $this, 'get_blog_permission_check' ),
65 )
66 );
67 }
68
69 /**
70 * Get the stats blog data.
71 *
72 * @since 0.12.0
73 *
74 * @return array
75 */
76 public function get_blog() {
77 return XMLRPC_Provider::init()->get_blog();
78 }
79
80 /**
81 * Check permissions for the `/stats/blog` endpoint.
82 *
83 * @return WP_Error|true
84 */
85 public function get_blog_permission_check() {
86 return Rest_Authentication::is_signed_with_blog_token()
87 ? true
88 : new WP_Error( 'invalid_permission_stats_get_blog', REST_Connector::get_user_permissions_error_msg(), array( 'status' => rest_authorization_required_code() ) );
89 }
90 }
91