← All changes
|
jetpack_vendor/automattic/jetpack-blaze/src/class-rest-controller.php
+64
-19
12.7.3
→
16.3-beta
View file →
| @@ -25,38 +25,75 @@ | ||
| 25 | 25 | */ |
| 26 | 26 | public static $namespace = 'jetpack/v4/blaze'; |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | + * Connection manager object. | |
| 30 | + * | |
| 31 | + * @var \Automattic\Jetpack\Connection\Manager | |
| 32 | + */ | |
| 33 | + private $connection; | |
| 34 | + | |
| 35 | + /** | |
| 36 | + * Creates the REST_Controller object. | |
| 37 | + * | |
| 38 | + * @param \Automattic\Jetpack\Connection\Manager $connection The connection manager object. | |
| 39 | + */ | |
| 40 | + public function __construct( $connection = null ) { | |
| 41 | + $this->connection = $connection ?? new Connection_Manager(); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * Registers the REST routes on the `rest_api_init` hook. | |
| 46 | + * | |
| 47 | + * Instantiated here, rather than eagerly, so the controller class only loads | |
| 48 | + * on requests that reach `rest_api_init`. Static so the callback can be | |
| 49 | + * unregistered. | |
| 50 | + * | |
| 51 | + * @access public | |
| 52 | + */ | |
| 53 | + public static function register() { | |
| 54 | + ( new self() )->register_rest_routes(); | |
| 55 | + } | |
| 56 | + | |
| 57 | + /** | |
| 29 | 58 | * Registers the REST routes. |
| 30 | 59 | * |
| 31 | 60 | * @access public |
| 32 | - * @static | |
| 33 | 61 | */ |
| 34 | 62 | public function register_rest_routes() { |
| 35 | 63 | $site_id = $this->get_site_id(); |
| 36 | - if ( is_wp_error( $site_id ) ) { | |
| 37 | - return; | |
| 64 | + | |
| 65 | + if ( ! is_wp_error( $site_id ) ) { | |
| 66 | + register_rest_route( | |
| 67 | + static::$namespace, | |
| 68 | + 'eligibility', | |
| 69 | + array( | |
| 70 | + 'methods' => WP_REST_Server::READABLE, | |
| 71 | + 'callback' => array( $this, 'blaze_eligibility' ), | |
| 72 | + 'permission_callback' => array( $this, 'can_user_view_blaze_settings' ), | |
| 73 | + ) | |
| 74 | + ); | |
| 75 | + | |
| 76 | + register_rest_route( | |
| 77 | + static::$namespace, | |
| 78 | + 'dashboard', | |
| 79 | + array( | |
| 80 | + 'methods' => WP_REST_Server::READABLE, | |
| 81 | + 'callback' => array( $this, 'is_dashboard_enabled' ), | |
| 82 | + 'permission_callback' => array( $this, 'can_user_view_blaze_settings' ), | |
| 83 | + ) | |
| 84 | + ); | |
| 38 | 85 | } |
| 39 | 86 | |
| 40 | 87 | register_rest_route( |
| 41 | 88 | static::$namespace, |
| 42 | - 'eligibility', | |
| 89 | + 'active-campaigns', | |
| 43 | 90 | array( |
| 44 | 91 | 'methods' => WP_REST_Server::READABLE, |
| 45 | - 'callback' => array( $this, 'blaze_eligibility' ), | |
| 92 | + 'callback' => array( $this, 'get_active_campaigns' ), | |
| 46 | 93 | 'permission_callback' => array( $this, 'can_user_view_blaze_settings' ), |
| 47 | 94 | ) |
| 48 | 95 | ); |
| 49 | - | |
| 50 | - register_rest_route( | |
| 51 | - static::$namespace, | |
| 52 | - 'dashboard', | |
| 53 | - array( | |
| 54 | - 'methods' => WP_REST_Server::READABLE, | |
| 55 | - 'callback' => array( $this, 'is_dashboard_enabled' ), | |
| 56 | - 'permission_callback' => array( $this, 'can_user_view_blaze_settings' ), | |
| 57 | - ) | |
| 58 | - ); | |
| 59 | 96 | } |
| 60 | 97 | |
| 61 | 98 | /** |
| 62 | 99 | * Only administrators can access the API. |
| @@ -93,14 +130,23 @@ | ||
| 93 | 130 | * |
| 94 | 131 | * @return bool |
| 95 | 132 | */ |
| 96 | 133 | public function is_dashboard_enabled() { |
| 134 | + return (bool) Blaze::is_dashboard_enabled(); | |
| 135 | + } | |
| 136 | + | |
| 137 | + /** | |
| 138 | + * Get active Blaze campaign status for the site. | |
| 139 | + * | |
| 140 | + * @return array | |
| 141 | + */ | |
| 142 | + public function get_active_campaigns() { | |
| 97 | 143 | $site_id = $this->get_site_id(); |
| 98 | 144 | if ( is_wp_error( $site_id ) ) { |
| 99 | - return false; | |
| 145 | + return Blaze::get_active_campaigns_status( 0 ); | |
| 100 | 146 | } |
| 101 | 147 | |
| 102 | - return (bool) Blaze::is_dashboard_enabled( $site_id ); | |
| 148 | + return Blaze::get_active_campaigns_status( $site_id ); | |
| 103 | 149 | } |
| 104 | 150 | |
| 105 | 151 | /** |
| 106 | 152 | * Check if the current user is connected. |
| @@ -112,10 +158,9 @@ | ||
| 112 | 158 | if ( ( new Host() )->is_wpcom_simple() ) { |
| 113 | 159 | return true; |
| 114 | 160 | } |
| 115 | 161 | |
| 116 | - $connection = new Connection_Manager(); | |
| 117 | - return $connection->is_connected() && $connection->is_user_connected(); | |
| 162 | + return $this->connection->is_connected() && $this->connection->is_user_connected(); | |
| 118 | 163 | } |
| 119 | 164 | |
| 120 | 165 | /** |
| 121 | 166 | * Return a WP_Error object with a forbidden error. |