| @@ -7,9 +7,9 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace ContentControl\RestAPI; |
| 10 | 10 | |
| 11 | -use WP_Rest_Controller, WP_REST_Response, WP_REST_Server, WP_Error, WP_REST_Request; | |
| 11 | +use WP_REST_Controller, WP_REST_Response, WP_REST_Server, WP_Error, WP_REST_Request; | |
| 12 | 12 | |
| 13 | 13 | use function ContentControl\plugin; |
| 14 | 14 | |
| 15 | 15 | defined( 'ABSPATH' ) || exit; |
| @@ -34,8 +34,10 @@ | ||
| 34 | 34 | protected $base = 'license'; |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * Register API endpoint routes. |
| 38 | + * | |
| 39 | + * @return void | |
| 38 | 40 | */ |
| 39 | 41 | public function register_routes() { |
| 40 | 42 | register_rest_route( |
| 41 | 43 | $this->namespace, |
| @@ -52,9 +54,9 @@ | ||
| 52 | 54 | 'permission_callback' => [ $this, 'manage_license_permissions' ], |
| 53 | 55 | 'args' => [ |
| 54 | 56 | 'licenseKey' => [ |
| 55 | 57 | 'required' => true, |
| 56 | - 'validate_callback' => function ( $param, $request, $key ) { | |
| 58 | + 'validate_callback' => function ( $param, $request, $key ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter | |
| 57 | 59 | return is_string( $param ); |
| 58 | 60 | }, |
| 59 | 61 | ], |
| 60 | 62 | ], |
| @@ -77,9 +79,9 @@ | ||
| 77 | 79 | 'permission_callback' => [ $this, 'manage_license_permissions' ], |
| 78 | 80 | 'args' => [ |
| 79 | 81 | 'licenseKey' => [ |
| 80 | 82 | 'required' => false, |
| 81 | - 'validate_callback' => function ( $param, $request, $key ) { | |
| 83 | + 'validate_callback' => function ( $param, $request, $key ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter | |
| 82 | 84 | return is_string( $param ); |
| 83 | 85 | }, |
| 84 | 86 | ], |
| 85 | 87 | ], |
| @@ -114,15 +116,27 @@ | ||
| 114 | 116 | 'permission_callback' => [ $this, 'manage_license_permissions' ], |
| 115 | 117 | ], |
| 116 | 118 | ] |
| 117 | 119 | ); |
| 120 | + | |
| 121 | + register_rest_route( | |
| 122 | + $this->namespace, | |
| 123 | + '/' . $this->base . '/activate-pro', | |
| 124 | + [ | |
| 125 | + [ | |
| 126 | + 'methods' => WP_REST_Server::EDITABLE, | |
| 127 | + 'callback' => [ $this, 'activate_pro' ], | |
| 128 | + 'permission_callback' => [ $this, 'manage_license_permissions' ], | |
| 129 | + ], | |
| 130 | + ], | |
| 131 | + ); | |
| 118 | 132 | } |
| 119 | 133 | |
| 120 | 134 | /** |
| 121 | 135 | * Clean private or unnecessary data from license data before returning it. |
| 122 | 136 | * |
| 123 | - * @param array $license_data License data. | |
| 124 | - * @return array | |
| 137 | + * @param array{key:string,status:array<string,mixed>} $license_data License data. | |
| 138 | + * @return array{key:string,status:array<string,mixed>} | |
| 125 | 139 | */ |
| 126 | 140 | public function clean_license_data( $license_data ) { |
| 127 | 141 | $license_data['status'] = $this->clean_license_status( $license_data['status'] ); |
| 128 | 142 | |
| @@ -131,10 +145,14 @@ | ||
| 131 | 145 | |
| 132 | 146 | /** |
| 133 | 147 | * Clean license status. |
| 134 | 148 | * |
| 135 | - * @param array $license_status License status. | |
| 136 | - * @return array | |
| 149 | + * @param array{key:string,status:array<string,mixed>} $license_status License status. | |
| 150 | + * | |
| 151 | + * @return (array|string)[] | |
| 152 | + * | |
| 153 | + * @psalm-return array<'key'|'status', array<string, mixed>|string> | |
| 154 | + * @phpstan-return array{key: string, status: array<string, mixed>|string} | |
| 137 | 155 | */ |
| 138 | 156 | public function clean_license_status( $license_status ) { |
| 139 | 157 | // Remove customer_email, customer_name, payment_id, ..., checksum keys from status array. |
| 140 | 158 | $license_status = array_diff_key( |
| @@ -156,9 +174,9 @@ | ||
| 156 | 174 | |
| 157 | 175 | /** |
| 158 | 176 | * Get plugin license. |
| 159 | 177 | * |
| 160 | - * @return WP_Error|WP_REST_Response | |
| 178 | + * @return \WP_Error|\WP_REST_Response | |
| 161 | 179 | */ |
| 162 | 180 | public function get_license() { |
| 163 | 181 | $license_data = plugin( 'license' )->get_license_data(); |
| 164 | 182 | |
| @@ -171,11 +189,11 @@ | ||
| 171 | 189 | |
| 172 | 190 | /** |
| 173 | 191 | * Update plugin license key. |
| 174 | 192 | * |
| 175 | - * @param WP_REST_Request $request Request object. | |
| 193 | + * @param \WP_REST_Request<array<string,mixed>> $request Request object. | |
| 176 | 194 | * |
| 177 | - * @return WP_Error|WP_REST_Response | |
| 195 | + * @return \WP_Error|\WP_REST_Response | |
| 178 | 196 | */ |
| 179 | 197 | public function update_license_key( $request ) { |
| 180 | 198 | $license_key = sanitize_text_field( $request->get_param( 'licenseKey' ) ); |
| 181 | 199 | |
| @@ -217,9 +235,9 @@ | ||
| 217 | 235 | |
| 218 | 236 | /** |
| 219 | 237 | * Remove plugin license key. |
| 220 | 238 | * |
| 221 | - * @return WP_Error|WP_REST_Response | |
| 239 | + * @return \WP_Error|\WP_REST_Response | |
| 222 | 240 | */ |
| 223 | 241 | public function remove_license() { |
| 224 | 242 | plugin( 'license' )->remove_license(); |
| 225 | 243 | |
| @@ -232,11 +250,11 @@ | ||
| 232 | 250 | |
| 233 | 251 | /** |
| 234 | 252 | * Activate plugin license. |
| 235 | 253 | * |
| 236 | - * @param WP_REST_Request $request Request object. | |
| 254 | + * @param WP_REST_Request<array<string,mixed>> $request Request object. | |
| 237 | 255 | * |
| 238 | - * @return WP_Error|WP_REST_Response | |
| 256 | + * @return \WP_Error|\WP_REST_Response | |
| 239 | 257 | */ |
| 240 | 258 | public function activate_license( $request ) { |
| 241 | 259 | $license_key = sanitize_text_field( $request->get_param( 'licenseKey' ) ); |
| 242 | 260 | |
| @@ -263,17 +281,47 @@ | ||
| 263 | 281 | } |
| 264 | 282 | } |
| 265 | 283 | |
| 266 | 284 | /** |
| 285 | + * Activate pro plugin. | |
| 286 | + * | |
| 287 | + * @return \WP_Error|\WP_REST_Response | |
| 288 | + */ | |
| 289 | + public function activate_pro() { | |
| 290 | + // Check if its installed. | |
| 291 | + if ( ! plugin()->is_pro_installed() ) { | |
| 292 | + return new WP_Error( '404', __( 'Pro plugin is not installed.', 'content-control' ), [ 'status' => 404 ] ); | |
| 293 | + } | |
| 294 | + | |
| 295 | + // Check if its active. | |
| 296 | + if ( plugin()->is_pro_active() ) { | |
| 297 | + return new WP_Error( '404', __( 'Pro plugin is already active.', 'content-control' ), [ 'status' => 404 ] ); | |
| 298 | + } | |
| 299 | + | |
| 300 | + // // Activate pro plugin. | |
| 301 | + if ( ! function_exists( 'activate_plugin' ) ) { | |
| 302 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; | |
| 303 | + } | |
| 304 | + | |
| 305 | + $activate = \activate_plugin( 'content-control-pro/content-control-pro.php', '', false, true ); | |
| 306 | + | |
| 307 | + if ( is_wp_error( $activate ) ) { | |
| 308 | + return new WP_Error( '404', __( 'Something went wrong, the pro plugin could not be activated.', 'content-control' ), [ 'status' => 404 ] ); | |
| 309 | + } | |
| 310 | + | |
| 311 | + return new WP_REST_Response( true, 200 ); | |
| 312 | + } | |
| 313 | + | |
| 314 | + /** | |
| 267 | 315 | * Deactivate plugin license. |
| 268 | 316 | * |
| 269 | - * @return WP_Error|WP_REST_Response | |
| 317 | + * @return \WP_Error|\WP_REST_Response | |
| 270 | 318 | */ |
| 271 | 319 | public function deactivate_license() { |
| 272 | 320 | try { |
| 273 | 321 | $license_status = plugin( 'license' )->deactivate_license(); |
| 274 | 322 | |
| 275 | - return new WP_REST_Response( $this->clean_license_status( [ 'status' => $license_status ] ), 200 ); | |
| 323 | + return new WP_REST_Response( [ 'status' => $this->clean_license_status( $license_status ) ], 200 ); | |
| 276 | 324 | } catch ( \Exception $e ) { |
| 277 | 325 | $message = __( 'Something went wrong, the license could not be deactivated.', 'content-control' ); |
| 278 | 326 | |
| 279 | 327 | if ( '' !== $e->getMessage() ) { |
| @@ -286,9 +334,9 @@ | ||
| 286 | 334 | |
| 287 | 335 | /** |
| 288 | 336 | * Get plugin license status. |
| 289 | 337 | * |
| 290 | - * @return WP_Error|WP_REST_Response | |
| 338 | + * @return \WP_Error|\WP_REST_Response | |
| 291 | 339 | */ |
| 292 | 340 | public function get_status() { |
| 293 | 341 | $license_status = plugin( 'license' )->get_license_status(); |
| 294 | 342 | |
| @@ -301,12 +349,12 @@ | ||
| 301 | 349 | |
| 302 | 350 | /** |
| 303 | 351 | * Refresh plugin license status. |
| 304 | 352 | * |
| 305 | - * @return WP_Error|WP_REST_Response | |
| 353 | + * @return \WP_Error|\WP_REST_Response | |
| 306 | 354 | */ |
| 307 | 355 | public function refresh_license_status() { |
| 308 | - $license_status = plugin( 'license' )->refresh_license_status(); | |
| 356 | + $license_status = plugin( 'license' )->get_license_status( true ); | |
| 309 | 357 | |
| 310 | 358 | if ( $license_status ) { |
| 311 | 359 | return new WP_REST_Response( [ 'status' => $this->clean_license_status( $license_status ) ], 200 ); |
| 312 | 360 | } else { |