PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.1.0
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.1.0
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / Core / PluginInstaller.php

PluginInstaller.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.1.0, at includes/Core/PluginInstaller.php

233 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace WPDeveloper\BetterDocs\Core;
3
4 if (!defined('ABSPATH')) {
5 exit;
6 } // Exit if accessed directly.
7
8 use \WP_Error;
9
10 class PluginInstaller
11 {
12 public function __construct() {
13 add_action( 'wp_ajax_wpdeveloper_auto_active_even_not_installed', [ $this, 'ajax_auto_active_even_not_installed' ] );
14 add_action( 'wp_ajax_wpdeveloper_install_plugin', [ $this, 'ajax_install_plugin' ] );
15 add_action( 'wp_ajax_wpdeveloper_upgrade_plugin', [ $this, 'ajax_upgrade_plugin' ] );
16 add_action( 'wp_ajax_wpdeveloper_activate_plugin', [ $this, 'ajax_activate_plugin' ] );
17 add_action( 'wp_ajax_wpdeveloper_deactivate_plugin', [ $this, 'ajax_deactivate_plugin' ] );
18 }
19
20 /**
21 * get_local_plugin_data
22 *
23 * @param mixed $basename
24 * @return array|false
25 */
26 public function get_local_plugin_data($basename = '')
27 {
28 if (empty($basename)) {
29 return false;
30 }
31
32 if (!function_exists('get_plugins')) {
33 include_once ABSPATH . 'wp-admin/includes/plugin.php';
34 }
35
36 $plugins = get_plugins();
37
38 if (!isset($plugins[$basename])) {
39 return false;
40 }
41
42 return $plugins[$basename];
43 }
44
45 /**
46 * get_remote_plugin_data
47 *
48 * @param mixed $slug
49 * @return mixed array|WP_Error
50 */
51 public function get_remote_plugin_data($slug = '')
52 {
53 if (empty($slug)) {
54 return new WP_Error('empty_arg', __('Argument should not be empty.', 'betterdocs'));
55 }
56
57 $response = wp_remote_post(
58 'http://api.wordpress.org/plugins/info/1.0/',
59 [
60 'body' => [
61 'action' => 'plugin_information',
62 'request' => serialize((object) [
63 'slug' => $slug,
64 'fields' => [
65 'version' => false,
66 ],
67 ]),
68 ],
69 ]
70 );
71
72 if (is_wp_error($response)) {
73 return $response;
74 }
75
76 return unserialize(wp_remote_retrieve_body($response));
77 }
78
79 /**
80 * install_plugin
81 *
82 * @param mixed $slug
83 * @param bool $active
84 * @return mixed bool|WP_Error
85 */
86 public function install_plugin($slug = '', $active = true)
87 {
88 if (empty($slug)) {
89 return new WP_Error('empty_arg', __('Argument should not be empty.', 'betterdocs'));
90 }
91
92 include_once ABSPATH . 'wp-admin/includes/file.php';
93 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
94 include_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php';
95
96 $plugin_data = $this->get_remote_plugin_data($slug);
97
98 if (is_wp_error($plugin_data)) {
99 return $plugin_data;
100 }
101
102 $upgrader = new \Plugin_Upgrader(new \Automatic_Upgrader_Skin());
103
104 // install plugin
105 $install = $upgrader->install($plugin_data->download_link);
106
107 if (is_wp_error($install)) {
108 return $install;
109 }
110
111 // activate plugin
112 if ($install === true && $active) {
113 $active = activate_plugin($upgrader->plugin_info(), '', false, true);
114
115 if (is_wp_error($active)) {
116 return $active;
117 }
118
119 return $active === null;
120 }
121
122 return $install;
123 }
124
125 /**
126 * upgrade_plugin
127 *
128 * @param mixed $basename
129 * @return mixed bool|WP_Error
130 */
131 public function upgrade_plugin($basename = '')
132 {
133 if (empty($slug)) {
134 return new WP_Error('empty_arg', __('Argument should not be empty.', 'betterdocs'));
135 }
136
137 include_once ABSPATH . 'wp-admin/includes/file.php';
138 include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
139 include_once ABSPATH . 'wp-admin/includes/class-automatic-upgrader-skin.php';
140
141 $upgrader = new \Plugin_Upgrader(new \Automatic_Upgrader_Skin());
142
143 // upgrade plugin
144 return $upgrader->upgrade($basename);
145 }
146
147 public function ajax_install_plugin()
148 {
149 check_ajax_referer('betterdocs-wpdeveloper-plugins', 'security');
150
151 if(!current_user_can( 'install_plugins' )) {
152 wp_send_json_error(__('you are not allowed to do this action', 'betterdocs'));
153 }
154
155 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( $_POST['slug'] ) : '';
156 $result = $this->install_plugin( $slug );
157
158 if( isset( $_POST['promotype'] ) && 'eb-banner' === $_POST['promotype'] ) {
159 wp_remote_get( 'https://essential-addons.com/essential-blocks-install-gutenberg' );
160 }
161
162 if ( is_wp_error( $result ) ) {
163 wp_send_json_error( $result->get_error_message() );
164 }
165
166 wp_send_json_success(__('Plugin is installed successfully!', 'betterdocs'));
167 }
168
169 public function ajax_upgrade_plugin()
170 {
171 check_ajax_referer('betterdocs-wpdeveloper-plugins', 'security');
172 //check user capabilities
173 if(!current_user_can( 'update_plugins' )) {
174 wp_send_json_error(__('you are not allowed to do this action', 'betterdocs'));
175 }
176
177 $basename = isset( $_POST['basename'] ) ? sanitize_text_field( $_POST['basename'] ) : '';
178 $result = $this->upgrade_plugin( $basename );
179
180 if (is_wp_error($result)) {
181 wp_send_json_error($result->get_error_message());
182 }
183
184 wp_send_json_success(__('Plugin is updated successfully!', 'betterdocs'));
185 }
186
187 public function ajax_activate_plugin()
188 {
189 check_ajax_referer('betterdocs-wpdeveloper-plugins', 'security');
190
191 //check user capabilities
192 if(!current_user_can( 'activate_plugins' )) {
193 wp_send_json_error(__('you are not allowed to do this action', 'betterdocs'));
194 }
195
196 $basename = isset( $_POST['basename'] ) ? sanitize_text_field( $_POST['basename'] ) : '';
197 $result = activate_plugin( $basename, '', false, true );
198
199 if ( is_wp_error( $result ) ) {
200 wp_send_json_error( $result->get_error_message() );
201 }
202
203 if ($result === false) {
204 wp_send_json_error(__('Plugin couldn\'t be activated.', 'betterdocs'));
205 }
206 wp_send_json_success(__('Plugin is activated successfully!', 'betterdocs'));
207 }
208
209 public function ajax_deactivate_plugin() {
210 check_ajax_referer( 'betterdocs-wpdeveloper-plugins', 'security' );
211
212 //check user capabilities
213 if ( ! current_user_can( 'activate_plugins' ) ) {
214 wp_send_json_error( __( 'you are not allowed to do this action', 'betterdocs' ) );
215 }
216
217 $basename = isset( $_POST['basename'] ) ? sanitize_text_field( $_POST['basename'] ) : '';
218 deactivate_plugins( $basename, true );
219
220 wp_send_json_success( __( 'Plugin is deactivated successfully!', 'betterdocs' ) );
221 }
222
223 public function ajax_auto_active_even_not_installed() {
224 check_ajax_referer( 'betterdocs-wpdeveloper-plugins', 'security' );
225
226 if ( $this->get_local_plugin_data( $_POST['basename'] ) === false ) {
227 $this->ajax_install_plugin();
228 } else {
229 $this->ajax_activate_plugin();
230 }
231 }
232 }
233