Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
61.49% |
91 / 148 |
|
25.00% |
2 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Rest | |
61.49% |
91 / 148 |
|
25.00% |
2 / 8 |
22.65 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| register_routes | |
100.00% |
57 / 57 |
|
100.00% |
1 / 1 |
1 | |||
| get_licence_details | |
53.33% |
8 / 15 |
|
0.00% |
0 / 1 |
2.41 | |||
| get_licence_details_args | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| get_licence_response_schema | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
2 | |||
| set_licence_key | |
61.11% |
11 / 18 |
|
0.00% |
0 / 1 |
3.53 | |||
| activate_licence | |
50.00% |
7 / 14 |
|
0.00% |
0 / 1 |
2.50 | |||
| deactivate_licence | |
50.00% |
7 / 14 |
|
0.00% |
0 / 1 |
2.50 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * REST API for setting licence key, activating, deactivating and getting licence details and product information. |
| 4 | * |
| 5 | * {@see openapi/example-plugin-openapi.json} for the generated OpenAPI spec. |
| 6 | * |
| 7 | * @package brianhenryie/bh-wp-plugin-updater |
| 8 | */ |
| 9 | |
| 10 | namespace BrianHenryIE\WP_Plugin_Updater\WP_Includes; |
| 11 | |
| 12 | use BrianHenryIE\WP_Plugin_Updater\API_Interface; |
| 13 | use BrianHenryIE\WP_Plugin_Updater\Licence; |
| 14 | use BrianHenryIE\WP_Plugin_Updater\Settings_Interface; |
| 15 | use WP_REST_Request; |
| 16 | use WP_REST_Response; |
| 17 | |
| 18 | class Rest { |
| 19 | /** |
| 20 | * Constructor. |
| 21 | * |
| 22 | * @param API_Interface $api The main API class where the functionality is implemented. |
| 23 | */ |
| 24 | public function __construct( |
| 25 | protected API_Interface $api, |
| 26 | protected Settings_Interface $settings |
| 27 | ) { |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * @hooked rest_api_init |
| 32 | */ |
| 33 | public function register_routes(): void { |
| 34 | $route_namespace = "{$this->settings->get_rest_base()}/v1"; |
| 35 | |
| 36 | register_rest_route( |
| 37 | $route_namespace, |
| 38 | '/licence', |
| 39 | array( |
| 40 | array( |
| 41 | 'methods' => 'GET', |
| 42 | 'callback' => array( $this, 'get_licence_details' ), |
| 43 | 'args' => array(), |
| 44 | 'permission_callback' => function () { |
| 45 | return current_user_can( 'manage_options' ); |
| 46 | }, |
| 47 | ), |
| 48 | 'schema' => array( $this, 'get_licence_response_schema' ), |
| 49 | ), |
| 50 | ); |
| 51 | |
| 52 | register_rest_route( |
| 53 | $route_namespace, |
| 54 | '/licence/set-key', |
| 55 | array( |
| 56 | 'methods' => 'POST', |
| 57 | 'args' => array( |
| 58 | 'licence_key' => array( |
| 59 | 'required' => false, // i.e. can be null to remove it. |
| 60 | 'type' => 'string', |
| 61 | ), |
| 62 | 'activate' => array( |
| 63 | 'required' => false, |
| 64 | // default false |
| 65 | 'type' => 'boolean', |
| 66 | ), |
| 67 | ), |
| 68 | 'callback' => array( $this, 'set_licence_key' ), |
| 69 | 'permission_callback' => function () { |
| 70 | return current_user_can( 'manage_options' ); |
| 71 | }, |
| 72 | ) |
| 73 | ); |
| 74 | |
| 75 | register_rest_route( |
| 76 | $route_namespace, |
| 77 | '/licence/activate', |
| 78 | array( |
| 79 | 'methods' => 'POST', |
| 80 | 'callback' => array( $this, 'activate_licence' ), |
| 81 | 'permission_callback' => function () { |
| 82 | return current_user_can( 'manage_options' ); |
| 83 | }, |
| 84 | ) |
| 85 | ); |
| 86 | |
| 87 | register_rest_route( |
| 88 | $route_namespace, |
| 89 | '/licence/deactivate', |
| 90 | array( |
| 91 | 'methods' => 'POST', |
| 92 | 'callback' => array( $this, 'deactivate_licence' ), |
| 93 | 'permission_callback' => '__return_true', |
| 94 | ) |
| 95 | ); |
| 96 | } |
| 97 | |
| 98 | public function get_licence_details( WP_REST_Request $request ): WP_REST_Response { |
| 99 | $refresh = $request->get_param( 'refresh' ) === true; |
| 100 | |
| 101 | try { |
| 102 | $licence = $this->api->get_licence_details( $refresh ); |
| 103 | } catch ( \Exception $exception ) { |
| 104 | $result = array( |
| 105 | 'success' => false, |
| 106 | 'error' => get_class( $exception ), |
| 107 | 'message' => $exception->getMessage(), |
| 108 | ); |
| 109 | |
| 110 | return new WP_REST_Response( $result, 500 ); |
| 111 | } |
| 112 | |
| 113 | $result = array( |
| 114 | 'success' => true, |
| 115 | 'message' => 'Licence details retrieved.', |
| 116 | 'data' => $licence, |
| 117 | ); |
| 118 | |
| 119 | return new WP_REST_Response( $result ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * The argument schema / the arguments required in the request |
| 124 | */ |
| 125 | public function get_licence_details_args(): array { |
| 126 | $args = array(); |
| 127 | |
| 128 | // Here we add our PHP representation of JSON Schema. |
| 129 | $args['refresh'] = array( |
| 130 | 'description' => esc_html__( 'Should the licence check perform a remote request to the licence server.', 'bh-wp-plugin-updater' ), |
| 131 | 'type' => 'boolean', |
| 132 | // 'validate_callback' => 'prefix_validate_my_arg', |
| 133 | // 'sanitize_callback' => 'prefix_sanitize_my_arg', |
| 134 | 'required' => true, |
| 135 | // default: false |
| 136 | ); |
| 137 | |
| 138 | return $args; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * @return array{schema:string,title:string,type:string,properties:array} |
| 143 | */ |
| 144 | public function get_licence_response_schema(): array { |
| 145 | $schema = array( |
| 146 | // This tells the spec of JSON Schema we are using which is draft 4. |
| 147 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 148 | // The title property marks the identity of the resource. |
| 149 | 'title' => 'licence', |
| 150 | 'type' => 'object', |
| 151 | // In JSON Schema you can specify object properties in the properties attribute. |
| 152 | 'properties' => array( |
| 153 | 'success' => array( |
| 154 | 'description' => esc_html__( 'Was the request successful?', 'bh-wp-plugin-updater' ), |
| 155 | 'type' => 'boolean', |
| 156 | ), |
| 157 | 'message' => array( |
| 158 | 'description' => esc_html__( 'A friendly message.', 'bh-wp-plugin-updater' ), |
| 159 | 'type' => 'string', |
| 160 | ), |
| 161 | 'data' => array( |
| 162 | 'description' => esc_html__( 'The licence data.', 'bh-wp-plugin-updater' ), |
| 163 | 'type' => 'object', |
| 164 | 'properties' => Licence::get_licence_object_schema_properties(), |
| 165 | ), |
| 166 | ), |
| 167 | ); |
| 168 | |
| 169 | return $schema; |
| 170 | } |
| 171 | |
| 172 | |
| 173 | |
| 174 | public function set_licence_key( WP_REST_Request $request ): WP_REST_Response { |
| 175 | $licence_key = $request->get_param( 'licence_key' ); |
| 176 | $activate_licence = (bool) $request->get_param( 'activate' ); |
| 177 | |
| 178 | try { |
| 179 | $licence = $this->api->set_license_key( $licence_key ); |
| 180 | if ( $activate_licence ) { |
| 181 | $licence = $this->api->activate_licence(); |
| 182 | } |
| 183 | } catch ( \Exception $exception ) { |
| 184 | $result = array( |
| 185 | 'success' => false, |
| 186 | 'error' => get_class( $exception ), |
| 187 | 'message' => $exception->getMessage(), |
| 188 | ); |
| 189 | |
| 190 | return new WP_REST_Response( $result, 500 ); |
| 191 | } |
| 192 | |
| 193 | // assert( $licence->get_licence_key() === $licence_key ); |
| 194 | |
| 195 | $result = array( |
| 196 | 'success' => true, |
| 197 | 'message' => 'Licence key set.', |
| 198 | 'data' => $licence, |
| 199 | ); |
| 200 | |
| 201 | return new WP_REST_Response( $result ); |
| 202 | } |
| 203 | |
| 204 | public function activate_licence( WP_REST_Request $request ): WP_REST_Response { |
| 205 | |
| 206 | try { |
| 207 | $licence = $this->api->activate_licence(); |
| 208 | } catch ( \Exception $exception ) { |
| 209 | $result = array( |
| 210 | 'success' => false, |
| 211 | 'error' => get_class( $exception ), |
| 212 | 'message' => $exception->getMessage(), |
| 213 | ); |
| 214 | |
| 215 | return new WP_REST_Response( $result, 500 ); |
| 216 | } |
| 217 | |
| 218 | $result = array( |
| 219 | 'success' => true, |
| 220 | 'message' => 'Licence activated.', |
| 221 | 'data' => $licence, |
| 222 | ); |
| 223 | |
| 224 | return new WP_REST_Response( $result ); |
| 225 | } |
| 226 | |
| 227 | public function deactivate_licence( WP_REST_Request $request ): WP_REST_Response { |
| 228 | |
| 229 | try { |
| 230 | $licence = $this->api->deactivate_licence(); |
| 231 | } catch ( \Exception $exception ) { |
| 232 | $result = array( |
| 233 | 'success' => false, |
| 234 | 'error' => get_class( $exception ), |
| 235 | 'message' => $exception->getMessage(), |
| 236 | ); |
| 237 | |
| 238 | return new WP_REST_Response( $result, 500 ); |
| 239 | } |
| 240 | |
| 241 | $result = array( |
| 242 | 'success' => true, |
| 243 | 'message' => 'Licence deactivated.', |
| 244 | 'data' => $licence, |
| 245 | ); |
| 246 | |
| 247 | return new WP_REST_Response( $result ); |
| 248 | } |
| 249 | } |