| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class PluginInstallerController |
| 5 |
* |
| 6 |
* Handles the installation, activation, and management of plugins within the application. |
| 7 |
* Provides methods for installing new plugins, activating/deactivating existing plugins, |
| 8 |
* and managing plugin-related actions via HTTP requests. |
| 9 |
* |
| 10 |
* @package YourPluginNamespace\Controllers |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace WPFunnels\Rest\Controllers; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class PluginInstallerController |
| 17 |
* |
| 18 |
* @since 3.5.25 |
| 19 |
*/ |
| 20 |
class PluginInstallerController extends Wpfnl_REST_Controller{ |
| 21 |
|
| 22 |
/** |
| 23 |
* Endpoint namespace. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
* @since 3.5.25 |
| 27 |
*/ |
| 28 |
protected $namespace = 'wpfunnels/v1'; |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Route base. |
| 33 |
* |
| 34 |
* @var string |
| 35 |
* @since 3.5.25 |
| 36 |
*/ |
| 37 |
protected $rest_base = 'activate-plugin'; |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Register rest routes |
| 42 |
* |
| 43 |
* @since 3.5.25 |
| 44 |
*/ |
| 45 |
public function register_routes(){ |
| 46 |
|
| 47 |
register_rest_route( |
| 48 |
$this->namespace, |
| 49 |
$this->rest_base, |
| 50 |
array( |
| 51 |
array( |
| 52 |
'methods' => \WP_REST_Server::CREATABLE, |
| 53 |
'callback' => array($this, 'activate_plugin'), |
| 54 |
'permission_callback' => function () { |
| 55 |
return current_user_can('manage_options'); |
| 56 |
}, |
| 57 |
), |
| 58 |
) |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Activate a plugin by its basename and slug. |
| 64 |
* |
| 65 |
* This method checks if the plugin is already installed. If not, it attempts to install it |
| 66 |
* from the WordPress.org repository. After installation, it activates the plugin. |
| 67 |
* |
| 68 |
* @param \WP_REST_Request $request The REST request object containing parameters. |
| 69 |
* @return \WP_REST_Response The response object containing the result of the activation. |
| 70 |
* |
| 71 |
* @since 3.5.25 |
| 72 |
*/ |
| 73 |
public function activate_plugin(\WP_REST_Request $request) { |
| 74 |
$basename = $request->get_param('basename'); |
| 75 |
$slug = $request->get_param('slug'); |
| 76 |
$response = array(); |
| 77 |
|
| 78 |
if (empty($basename) || empty($slug)) { |
| 79 |
return new \WP_REST_Response(array('error' => 'Plugin basename or slug is missing.'), 400); |
| 80 |
} |
| 81 |
|
| 82 |
// Check if plugin file exists |
| 83 |
if (!file_exists(WP_PLUGIN_DIR . '/' . $basename)) { |
| 84 |
// Try to install the plugin from WordPress.org |
| 85 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 86 |
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 87 |
require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php'; |
| 88 |
require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 89 |
require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 90 |
|
| 91 |
$api = plugins_api('plugin_information', array('slug' => $slug, 'fields' => array('sections' => false))); |
| 92 |
if (is_wp_error($api)) { |
| 93 |
return new \WP_REST_Response(array('error' => 'Plugin not found in WordPress.org repository.'), 400); |
| 94 |
} |
| 95 |
|
| 96 |
$upgrader = new \Plugin_Upgrader(new \Automatic_Upgrader_Skin()); |
| 97 |
$install_result = $upgrader->install($api->download_link); |
| 98 |
|
| 99 |
if (is_wp_error($install_result)) { |
| 100 |
return new \WP_REST_Response(array('error' => $install_result->get_error_message()), 400); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// Activate plugin |
| 105 |
if (!is_plugin_active($basename)) { |
| 106 |
$result = activate_plugin($basename); |
| 107 |
if (is_wp_error($result)) { |
| 108 |
return new \WP_REST_Response(array('error' => $result->get_error_message()), 400); |
| 109 |
} |
| 110 |
$response['message'] = sprintf(__('Plugin %s installed and activated successfully.', 'wpfnl'), $slug); |
| 111 |
} else { |
| 112 |
$response['message'] = sprintf(__('Plugin %s is already active.', 'wpfnl'), $slug); |
| 113 |
} |
| 114 |
|
| 115 |
$response['success'] = true; |
| 116 |
$response['basename'] = $basename; |
| 117 |
$response['slug'] = $slug; |
| 118 |
return new \WP_REST_Response($response, 200); |
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|