class-install-plugin.php
192 lines
| 1 | <?php |
| 2 | /** |
| 3 | * REST API route/endpoint to install 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 | use WPMUDEV\Modules\Plugin_Cross_Sell\Container; |
| 18 | |
| 19 | // Abort if called directly. |
| 20 | defined( 'WPINC' ) || die; |
| 21 | |
| 22 | /** |
| 23 | * Class Install_Plugin |
| 24 | * |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | class Install_Plugin extends Rest_Api { |
| 28 | /** |
| 29 | * The endpoint for the route. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | protected $endpoint = '/plugincrosssell/install_plugin'; |
| 34 | |
| 35 | /** |
| 36 | * Register the routes for handling confirmation functionality. |
| 37 | * |
| 38 | * @since 1.0.0 |
| 39 | * |
| 40 | * @return void |
| 41 | */ |
| 42 | public function register_routes(): void { |
| 43 | // Route to get auth url. |
| 44 | register_rest_route( |
| 45 | $this->get_namespace(), |
| 46 | $this->get_endpoint(), |
| 47 | array( |
| 48 | array( |
| 49 | 'methods' => \WP_REST_Server::EDITABLE, |
| 50 | 'callback' => array( $this, 'install' ), |
| 51 | 'permission_callback' => array( $this, 'check_permission' ), |
| 52 | 'args' => $this->input_args(), |
| 53 | ), |
| 54 | ) |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * The input arguments schema for the route. |
| 60 | * |
| 61 | * @since 1.0.0 |
| 62 | * @return array{current_slug: array{required: bool, type: string, plugin_slug: array{required: bool, type: string}}} |
| 63 | */ |
| 64 | protected function input_args(): array { |
| 65 | return array( |
| 66 | 'plugin_slug' => array( |
| 67 | 'type' => 'string', |
| 68 | 'required' => true, |
| 69 | ), |
| 70 | 'current_slug' => array( |
| 71 | 'type' => 'string', |
| 72 | 'required' => false, |
| 73 | ), |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Check if the current user has permission to install plugins. |
| 79 | * |
| 80 | * @since 1.0.0 |
| 81 | * @param \WP_REST_Request $request The Request object. |
| 82 | * @return bool |
| 83 | */ |
| 84 | public function check_permission( \WP_REST_Request $request ): bool { |
| 85 | return $this->has_permission( $request, 'install_plugins' ); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Save the client id and secret. |
| 90 | * |
| 91 | * @param \WP_REST_Request $request Request object. |
| 92 | * |
| 93 | * @since 1.0.0 |
| 94 | * |
| 95 | * @return \WP_REST_Response |
| 96 | */ |
| 97 | public function install( \WP_REST_Request $request ): \WP_REST_Response { |
| 98 | $response_message = ''; |
| 99 | $success = true; |
| 100 | $plugin_slug = $request->get_param( 'plugin_slug' ); |
| 101 | $submenu_params = ( $this->di_container instanceof Container ) ? $this->di_container->get( 'submenu_data' ) : null; |
| 102 | $current_slug = ! empty( $submenu_params['slug'] ) ? $submenu_params['slug'] : ''; |
| 103 | |
| 104 | // First let's make sure that we are not modifying the current plugin. |
| 105 | if ( $plugin_slug === $current_slug ) { |
| 106 | $response_message = __( 'You are already using this plugin.', 'plugin-cross-sell-textdomain' ); |
| 107 | $success = false; |
| 108 | } elseif ( ! $this->utilities->is_plugin_installed( $plugin_slug ) ) { |
| 109 | // Install the plugin. |
| 110 | $plugin = $this->install_plugin( $plugin_slug ); |
| 111 | if ( is_wp_error( $plugin ) ) { |
| 112 | $response_message = $plugin->get_error_message(); |
| 113 | $success = false; |
| 114 | } else { |
| 115 | $response_message = __( 'Plugin installed successfully.', 'plugin-cross-sell-textdomain' ); |
| 116 | } |
| 117 | } else { |
| 118 | $response_message = __( 'Plugin already installed.', 'plugin-cross-sell-textdomain' ); |
| 119 | } |
| 120 | |
| 121 | return $this->get_response( |
| 122 | array( |
| 123 | 'message' => $response_message, |
| 124 | 'success' => $success, |
| 125 | ), |
| 126 | $success |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Performs the actual plugin installation. |
| 132 | * |
| 133 | * @param string $plugin_slug Will be used to get the path to the plugin. |
| 134 | * @return bool|\WP_Error |
| 135 | */ |
| 136 | protected function install_plugin( string $plugin_slug ) { |
| 137 | // Include required files for plugin installation. |
| 138 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 139 | require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 140 | require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 141 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 142 | |
| 143 | $api = plugins_api( |
| 144 | 'plugin_information', |
| 145 | array( |
| 146 | 'slug' => $plugin_slug, |
| 147 | 'fields' => array( |
| 148 | 'short_description' => false, |
| 149 | 'sections' => false, |
| 150 | 'requires' => false, |
| 151 | 'rating' => false, |
| 152 | 'ratings' => false, |
| 153 | 'downloaded' => false, |
| 154 | 'last_updated' => false, |
| 155 | 'added' => false, |
| 156 | 'tags' => false, |
| 157 | 'compatibility' => false, |
| 158 | 'homepage' => false, |
| 159 | 'donate_link' => false, |
| 160 | ), |
| 161 | ) |
| 162 | ); |
| 163 | |
| 164 | if ( is_wp_error( $api ) ) { |
| 165 | return $api; |
| 166 | } |
| 167 | |
| 168 | $install = $this->install_from_api( $api ); |
| 169 | |
| 170 | return $install; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Implements the install using Plugin_Upgrader, WP built in class. |
| 175 | * |
| 176 | * @param \stdClass $api Plugin installer data in object, see plugins_api(). |
| 177 | * @return bool|\WP_Error |
| 178 | */ |
| 179 | protected function install_from_api( \stdClass $api ) { |
| 180 | |
| 181 | // Include necessary plugin functions. |
| 182 | $installer = new \Plugin_Upgrader( new \WP_Ajax_Upgrader_Skin() ); |
| 183 | $install = $installer->install( $api->download_link ); |
| 184 | |
| 185 | if ( empty( $install ) || is_wp_error( $install ) ) { |
| 186 | return $install; |
| 187 | } |
| 188 | |
| 189 | return true; |
| 190 | } |
| 191 | } |
| 192 |