| @@ -14,19 +14,9 @@ | ||
| 14 | 14 | |
| 15 | 15 | defined( 'ABSPATH' ) || exit; |
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | - * Temporary REST bridge for Pro releases older than 1.3.0. | |
| 19 | - * | |
| 20 | - * Note for WordPress.org Plugin Review Team: these routes are registered only | |
| 21 | - * while Core's deprecated old-Pro license bridge is active. Every route also | |
| 22 | - * requires Content Control's manage-settings capability. Core-only sites and | |
| 23 | - * sites running Pro 1.3.0 or later do not register these routes. | |
| 24 | - * | |
| 25 | - * @see \ContentControl\Plugin\Core::is_legacy_pro_active() | |
| 26 | - * @see \ContentControl\Controllers\RestAPI::register_routes() | |
| 27 | - * | |
| 28 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 18 | + * Rest API Licensing Controller Class. | |
| 29 | 19 | */ |
| 30 | 20 | class License extends WP_REST_Controller { |
| 31 | 21 | |
| 32 | 22 | /** |
| @@ -46,9 +36,8 @@ | ||
| 46 | 36 | /** |
| 47 | 37 | * Register API endpoint routes. |
| 48 | 38 | * |
| 49 | 39 | * @return void |
| 50 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 51 | 40 | */ |
| 52 | 41 | public function register_routes() { |
| 53 | 42 | register_rest_route( |
| 54 | 43 | $this->namespace, |
| @@ -117,9 +106,9 @@ | ||
| 117 | 106 | '/' . $this->base . '/status', |
| 118 | 107 | [ |
| 119 | 108 | [ |
| 120 | 109 | 'methods' => WP_REST_Server::READABLE, |
| 121 | - 'callback' => [ $this, 'get_status' ], | |
| 110 | + 'callback' => [ $this, 'get_license_status' ], | |
| 122 | 111 | 'permission_callback' => [ $this, 'manage_license_permissions' ], |
| 123 | 112 | ], |
| 124 | 113 | [ |
| 125 | 114 | 'methods' => WP_REST_Server::EDITABLE, |
| @@ -127,8 +116,20 @@ | ||
| 127 | 116 | 'permission_callback' => [ $this, 'manage_license_permissions' ], |
| 128 | 117 | ], |
| 129 | 118 | ] |
| 130 | 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 | + ); | |
| 131 | 132 | } |
| 132 | 133 | |
| 133 | 134 | /** |
| 134 | 135 | * Clean private or unnecessary data from license data before returning it. |
| @@ -134,9 +135,8 @@ | ||
| 134 | 135 | * Clean private or unnecessary data from license data before returning it. |
| 135 | 136 | * |
| 136 | 137 | * @param array{key:string,status:array<string,mixed>} $license_data License data. |
| 137 | 138 | * @return array{key:string,status:array<string,mixed>} |
| 138 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 139 | 139 | */ |
| 140 | 140 | public function clean_license_data( $license_data ) { |
| 141 | 141 | $license_data['status'] = $this->clean_license_status( $license_data['status'] ); |
| 142 | 142 | |
| @@ -151,9 +151,8 @@ | ||
| 151 | 151 | * @return (array|string)[] |
| 152 | 152 | * |
| 153 | 153 | * @psalm-return array<'key'|'status', array<string, mixed>|string> |
| 154 | 154 | * @phpstan-return array{key: string, status: array<string, mixed>|string} |
| 155 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 156 | 155 | */ |
| 157 | 156 | public function clean_license_status( $license_status ) { |
| 158 | 157 | // Remove customer_email, customer_name, payment_id, ..., checksum keys from status array. |
| 159 | 158 | $license_status = array_diff_key( |
| @@ -176,9 +175,8 @@ | ||
| 176 | 175 | /** |
| 177 | 176 | * Get plugin license. |
| 178 | 177 | * |
| 179 | 178 | * @return \WP_Error|\WP_REST_Response |
| 180 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 181 | 179 | */ |
| 182 | 180 | public function get_license() { |
| 183 | 181 | $license_data = plugin( 'license' )->get_license_data(); |
| 184 | 182 | |
| @@ -194,9 +192,8 @@ | ||
| 194 | 192 | * |
| 195 | 193 | * @param \WP_REST_Request<array<string,mixed>> $request Request object. |
| 196 | 194 | * |
| 197 | 195 | * @return \WP_Error|\WP_REST_Response |
| 198 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 199 | 196 | */ |
| 200 | 197 | public function update_license_key( $request ) { |
| 201 | 198 | $license_key = sanitize_text_field( $request->get_param( 'licenseKey' ) ); |
| 202 | 199 | |
| @@ -204,9 +201,9 @@ | ||
| 204 | 201 | $old_key = plugin( 'license' )->get_license_key(); |
| 205 | 202 | |
| 206 | 203 | if ( $old_key === $license_key ) { |
| 207 | 204 | $license_status = plugin( 'license' )->get_license_status(); |
| 208 | - return new WP_REST_Response( [ 'status' => $this->clean_license_status( $license_status ) ], 200 ); | |
| 205 | + return new WP_REST_Response( $this->clean_license_status( $license_status ), 200 ); | |
| 209 | 206 | } |
| 210 | 207 | |
| 211 | 208 | if ( plugin( 'license' )->is_license_active() ) { |
| 212 | 209 | plugin( 'license' )->deactivate_license(); |
| @@ -215,14 +212,17 @@ | ||
| 215 | 212 | plugin( 'license' )->update_license_key( $license_key ); |
| 216 | 213 | |
| 217 | 214 | $license_status = plugin( 'license' )->activate_license( $license_key ); |
| 218 | 215 | |
| 219 | - return new WP_REST_Response( | |
| 220 | - [ | |
| 221 | - 'status' => $this->clean_license_status( $license_status ), | |
| 222 | - ], | |
| 223 | - 200 | |
| 224 | - ); | |
| 216 | + $response = [ | |
| 217 | + 'status' => $this->clean_license_status( $license_status ), | |
| 218 | + ]; | |
| 219 | + | |
| 220 | + if ( ! plugin()->is_pro_installed() ) { | |
| 221 | + $response['connectInfo'] = plugin( 'connect' )->get_connect_info( $license_key ); | |
| 222 | + } | |
| 223 | + | |
| 224 | + return new WP_REST_Response( $response, 200 ); | |
| 225 | 225 | } catch ( \Exception $e ) { |
| 226 | 226 | $message = __( 'Something went wrong, the license key could not be saved.', 'content-control' ); |
| 227 | 227 | |
| 228 | 228 | if ( '' !== $e->getMessage() ) { |
| @@ -236,9 +236,8 @@ | ||
| 236 | 236 | /** |
| 237 | 237 | * Remove plugin license key. |
| 238 | 238 | * |
| 239 | 239 | * @return \WP_Error|\WP_REST_Response |
| 240 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 241 | 240 | */ |
| 242 | 241 | public function remove_license() { |
| 243 | 242 | plugin( 'license' )->remove_license(); |
| 244 | 243 | |
| @@ -254,24 +253,24 @@ | ||
| 254 | 253 | * |
| 255 | 254 | * @param WP_REST_Request<array<string,mixed>> $request Request object. |
| 256 | 255 | * |
| 257 | 256 | * @return \WP_Error|\WP_REST_Response |
| 258 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 259 | 257 | */ |
| 260 | 258 | public function activate_license( $request ) { |
| 261 | - $license_key = $request->has_param( 'licenseKey' ) | |
| 262 | - ? sanitize_text_field( $request->get_param( 'licenseKey' ) ) | |
| 263 | - : null; | |
| 259 | + $license_key = sanitize_text_field( $request->get_param( 'licenseKey' ) ); | |
| 264 | 260 | |
| 265 | 261 | try { |
| 266 | 262 | $license_status = plugin( 'license' )->activate_license( $license_key ); |
| 267 | 263 | |
| 268 | - return new WP_REST_Response( | |
| 269 | - [ | |
| 270 | - 'status' => $this->clean_license_status( $license_status ), | |
| 271 | - ], | |
| 272 | - 200 | |
| 273 | - ); | |
| 264 | + $response = [ | |
| 265 | + 'status' => $this->clean_license_status( $license_status ), | |
| 266 | + ]; | |
| 267 | + | |
| 268 | + if ( ! plugin()->is_pro_installed() ) { | |
| 269 | + $response['connectInfo'] = plugin( 'connect' )->get_connect_info( plugin( 'license' )->get_license_key() ); | |
| 270 | + } | |
| 271 | + | |
| 272 | + return new WP_REST_Response( $response, 200 ); | |
| 274 | 273 | } catch ( \Exception $e ) { |
| 275 | 274 | $message = __( 'Something went wrong, the license could not be activated.', 'content-control' ); |
| 276 | 275 | |
| 277 | 276 | if ( '' !== $e->getMessage() ) { |
| @@ -282,12 +281,41 @@ | ||
| 282 | 281 | } |
| 283 | 282 | } |
| 284 | 283 | |
| 285 | 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 | + /** | |
| 286 | 315 | * Deactivate plugin license. |
| 287 | 316 | * |
| 288 | 317 | * @return \WP_Error|\WP_REST_Response |
| 289 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 290 | 318 | */ |
| 291 | 319 | public function deactivate_license() { |
| 292 | 320 | try { |
| 293 | 321 | $license_status = plugin( 'license' )->deactivate_license(); |
| @@ -307,9 +335,8 @@ | ||
| 307 | 335 | /** |
| 308 | 336 | * Get plugin license status. |
| 309 | 337 | * |
| 310 | 338 | * @return \WP_Error|\WP_REST_Response |
| 311 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 312 | 339 | */ |
| 313 | 340 | public function get_status() { |
| 314 | 341 | $license_status = plugin( 'license' )->get_license_status(); |
| 315 | 342 | |
| @@ -323,9 +350,8 @@ | ||
| 323 | 350 | /** |
| 324 | 351 | * Refresh plugin license status. |
| 325 | 352 | * |
| 326 | 353 | * @return \WP_Error|\WP_REST_Response |
| 327 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 328 | 354 | */ |
| 329 | 355 | public function refresh_license_status() { |
| 330 | 356 | $license_status = plugin( 'license' )->get_license_status( true ); |
| 331 | 357 | |
| @@ -339,10 +365,9 @@ | ||
| 339 | 365 | /** |
| 340 | 366 | * Check update settings permissions. |
| 341 | 367 | * |
| 342 | 368 | * @return bool |
| 343 | - * @deprecated 2.7.0 Content Control Pro 1.3.0+ owns license REST routes. | |
| 344 | 369 | */ |
| 345 | 370 | public function manage_license_permissions() { |
| 346 | - return current_user_can( plugin()->get_permission( 'manage_settings' ) ); | |
| 371 | + return current_user_can( 'manage_options' ) || current_user_can( 'activate_plugins' ); | |
| 347 | 372 | } |
| 348 | 373 | } |