class-activate-plugin.php
142 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API route/endpoint to activate free plugin. |
| 4 | * |
| 5 | * @link https://wpmudev.com/ |
| 6 | * @since 1.0.0 |
| 7 | * |
| 8 | * @author WPMUDEV (https://wpmudev.com) |
| 9 | * @package WPMUDEV\Plugin_Cross_Sell |
| 10 | * |
| 11 | * @copyright (c) 2025, WPMU DEV (http://wpmudev.com) |
| 12 | */ |
| 13 | |
| 14 | namespace WPMUDEV\Modules\Plugin_Cross_Sell\App\Rest_Endpoints; |
| 15 | |
| 16 | use WPMUDEV\Modules\Plugin_Cross_Sell\Rest_Api; |
| 17 | |
| 18 | // Abort if called directly. |
| 19 | defined( 'WPINC' ) || die; |
| 20 | |
| 21 | /** |
| 22 | * Summary of Activate_Plugin |
| 23 | */ |
| 24 | class Activate_Plugin extends Rest_Api { |
| 25 | /** |
| 26 | * Endpoint for activating plugin. |
| 27 | * |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $endpoint = '/plugincrosssell/activate_plugin'; |
| 31 | |
| 32 | /** |
| 33 | * Register the routes for handling confirmation functionality. |
| 34 | * |
| 35 | * @since 1.0.0 |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public function register_routes(): void { |
| 40 | // Route to get auth url. |
| 41 | register_rest_route( |
| 42 | $this->get_namespace(), |
| 43 | $this->get_endpoint(), |
| 44 | array( |
| 45 | array( |
| 46 | 'methods' => \WP_REST_Server::EDITABLE, |
| 47 | 'callback' => array( $this, 'activate' ), |
| 48 | 'permission_callback' => array( $this, 'check_permission' ), |
| 49 | 'args' => $this->input_args(), |
| 50 | ), |
| 51 | ) |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Get the input arguments schema for the endpoint. |
| 57 | * |
| 58 | * @since 1.0.0 |
| 59 | * |
| 60 | * @return array |
| 61 | */ |
| 62 | protected function input_args(): array { |
| 63 | return array( |
| 64 | 'plugin_slug' => array( |
| 65 | 'type' => 'string', |
| 66 | 'required' => true, |
| 67 | ), |
| 68 | 'current_slug' => array( |
| 69 | 'type' => 'string', |
| 70 | 'required' => false, |
| 71 | ), |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Check if the current user has permission to activate plugins. |
| 77 | * |
| 78 | * @param \WP_REST_Request $request Request object. |
| 79 | * @return bool |
| 80 | */ |
| 81 | public function check_permission( \WP_REST_Request $request ): bool { |
| 82 | return $this->has_permission( $request, 'activate_plugins' ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Callback to activate the plugin. |
| 87 | * |
| 88 | * @param \WP_REST_Request $request Request object. |
| 89 | * |
| 90 | * @since 1.0.0 |
| 91 | * |
| 92 | * @return \WP_REST_Response |
| 93 | */ |
| 94 | public function activate( \WP_REST_Request $request ): \WP_REST_Response { |
| 95 | $response_message = ''; |
| 96 | $success = true; |
| 97 | |
| 98 | $plugin_slug = $request->get_param( 'plugin_slug' ); |
| 99 | $plugin_path = $this->utilities->get_plugin_path_by_slug( $plugin_slug ); |
| 100 | |
| 101 | if ( empty( $plugin_path ) ) { |
| 102 | $response_message = __( 'Plugin not found.', 'plugin-cross-sell-textdomain' ); |
| 103 | $success = false; |
| 104 | } |
| 105 | |
| 106 | $activate = $this->activate_plugin( $plugin_path ); |
| 107 | |
| 108 | if ( is_wp_error( $activate ) ) { |
| 109 | $response_message = $activate->get_error_message(); |
| 110 | $success = false; |
| 111 | } |
| 112 | |
| 113 | $response = $this->get_response( array( 'message' => $response_message ), $success ); |
| 114 | |
| 115 | return $response; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Activate the plugin. |
| 120 | * |
| 121 | * @param string $plugin_slug Plugin slug. |
| 122 | * |
| 123 | * @since 1.0.0 |
| 124 | * |
| 125 | * @return bool|\WP_Error |
| 126 | */ |
| 127 | public function activate_plugin( string $plugin_slug ) { |
| 128 | if ( ! function_exists( 'activate_plugin' ) ) { |
| 129 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 130 | } |
| 131 | |
| 132 | // Function activate_plugin returns null on success and WP_Error on failure. |
| 133 | $activate = activate_plugin( $plugin_slug ); |
| 134 | |
| 135 | if ( is_wp_error( $activate ) ) { |
| 136 | return $activate; |
| 137 | } |
| 138 | |
| 139 | return true; |
| 140 | } |
| 141 | } |
| 142 |