PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.13.1
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.13.1
3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 2.8.17 2.8.18 All 259 releases
wpfunnels / includes / core / rest-api / Controllers / PluginInstallerController.php

PluginInstallerController.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.1, at includes/core/rest-api/Controllers/PluginInstallerController.php

122 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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