| @@ -54,9 +54,9 @@ | ||
| 54 | 54 | 'permission_callback' => [ $this, 'manage_license_permissions' ], |
| 55 | 55 | 'args' => [ |
| 56 | 56 | 'licenseKey' => [ |
| 57 | 57 | 'required' => true, |
| 58 | - 'validate_callback' => function ( $param, $request, $key ) { | |
| 58 | + 'validate_callback' => function ( $param, $request, $key ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter | |
| 59 | 59 | return is_string( $param ); |
| 60 | 60 | }, |
| 61 | 61 | ], |
| 62 | 62 | ], |
| @@ -79,9 +79,9 @@ | ||
| 79 | 79 | 'permission_callback' => [ $this, 'manage_license_permissions' ], |
| 80 | 80 | 'args' => [ |
| 81 | 81 | 'licenseKey' => [ |
| 82 | 82 | 'required' => false, |
| 83 | - 'validate_callback' => function ( $param, $request, $key ) { | |
| 83 | + 'validate_callback' => function ( $param, $request, $key ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter | |
| 84 | 84 | return is_string( $param ); |
| 85 | 85 | }, |
| 86 | 86 | ], |
| 87 | 87 | ], |
| @@ -116,8 +116,20 @@ | ||
| 116 | 116 | 'permission_callback' => [ $this, 'manage_license_permissions' ], |
| 117 | 117 | ], |
| 118 | 118 | ] |
| 119 | 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 | + ); | |
| 120 | 132 | } |
| 121 | 133 | |
| 122 | 134 | /** |
| 123 | 135 | * Clean private or unnecessary data from license data before returning it. |
| @@ -134,9 +146,13 @@ | ||
| 134 | 146 | /** |
| 135 | 147 | * Clean license status. |
| 136 | 148 | * |
| 137 | 149 | * @param array{key:string,status:array<string,mixed>} $license_status License status. |
| 138 | - * @return array{key:string,status:array<string,mixed>} | |
| 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} | |
| 139 | 155 | */ |
| 140 | 156 | public function clean_license_status( $license_status ) { |
| 141 | 157 | // Remove customer_email, customer_name, payment_id, ..., checksum keys from status array. |
| 142 | 158 | $license_status = array_diff_key( |
| @@ -262,8 +278,38 @@ | ||
| 262 | 278 | } |
| 263 | 279 | |
| 264 | 280 | return new WP_Error( '404', $message, [ 'status' => 404 ] ); |
| 265 | 281 | } |
| 282 | + } | |
| 283 | + | |
| 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 ); | |
| 266 | 312 | } |
| 267 | 313 | |
| 268 | 314 | /** |
| 269 | 315 | * Deactivate plugin license. |