PluginProbe
Borderless – Addons and Templates for Elementor / trunk
Borderless – Addons and Templates for Elementor vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 All 78 releases
borderless / includes / library / inc / PluginInstaller.php

PluginInstaller.php in Borderless – Addons and Templates for Elementor trunk, at includes/library/inc/PluginInstaller.php

308 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LIBRARY;
4
5 class PluginInstaller {
6
7 private $plugins;
8
9 public function init() {
10 $this->set_plugins();
11
12 add_action( 'library/plugin_intaller_before_plugin_activation', array( $this, 'before_plugin_activation' ) );
13
14 add_action( 'wp_ajax_library_install_plugin', array( $this, 'install_plugin_callback' ) );
15 }
16
17 public function before_plugin_activation( $slug ) {
18 if ( $slug === 'wpforms-lite' ) {
19 update_option( 'wpforms_activation_redirect', true );
20 }
21
22 if ( $slug === 'all-in-one-seo-pack' ) {
23 update_option( 'aioseo_activation_redirect', true );
24 }
25
26 if ( $slug === 'wp-mail-smtp' ) {
27 update_option( 'wp_mail_smtp_activation_prevent_redirect', true );
28 }
29 }
30
31 public function set_plugins() {
32 $all_plugins = Helpers::apply_filters( 'library/register_plugins', array() );
33
34 $this->plugins = $this->filter_plugins( $all_plugins );
35 }
36
37 public function get_theme_plugins() {
38 $theme_plugins = Helpers::apply_filters( 'library/register_plugins', array() );
39
40 return $this->filter_plugins( $theme_plugins );
41 }
42
43 public function install_plugin_callback() {
44 check_ajax_referer( 'library-ajax-verification', 'security' );
45
46 if ( ! current_user_can( 'install_plugins' ) ) {
47 wp_send_json_error( esc_html__( 'Could not install the plugin. You don\'t have permission to install plugins.', 'borderless' ) );
48 }
49
50 $slug = ! empty( $_POST['slug'] ) ? sanitize_key( wp_unslash( $_POST['slug'] ) ) : '';
51
52 if ( empty( $slug ) ) {
53 wp_send_json_error( esc_html__( 'Could not install the plugin. Plugin slug is missing.', 'borderless' ) );
54 }
55
56 if ( $this->is_plugin_active( $slug ) ) {
57 wp_send_json_success( esc_html__( 'Plugin is already installed and activated!', 'borderless' ) );
58 }
59
60 if ( $this->is_plugin_installed( $slug ) ) {
61 $activated = $this->activate_plugin( $this->get_plugin_basename_from_slug( $slug ), $slug );
62
63 if ( ! is_wp_error( $activated ) ) {
64 wp_send_json_success( esc_html__( 'Plugin was already installed! We activated it for you.', 'borderless' ) );
65 } else {
66 wp_send_json_error( $activated->get_error_message() );
67 }
68 }
69
70 if ( ! $this->filesystem_permissions_allowed() ) {
71 wp_send_json_error( esc_html__( 'Could not install the plugin. Don\'t have file permission.', 'borderless' ) );
72 }
73
74 remove_action( 'upgrader_process_complete', [ 'Language_Pack_Upgrader', 'async_upgrade' ], 20 );
75
76 $extra = array();
77 $extra['slug'] = $slug;
78 $source = $this->get_download_url( $slug );
79 $api = empty( $this->get_plugin_data( $slug )['source'] ) ? $this->get_plugins_api( $slug ) : null;
80 $api = ( false !== $api ) ? $api : null;
81
82 if ( ! empty( $api ) && is_wp_error( $api ) ) {
83 wp_send_json_error( $api->get_error_message() );
84 }
85
86 if ( ! class_exists( '\Plugin_Upgrader', false ) ) {
87 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
88 }
89
90 $skin_args = array(
91 'type' => 'web',
92 'plugin' => '',
93 'api' => $api,
94 'extra' => $extra,
95 );
96
97 $upgrader = new \Plugin_Upgrader( new PluginInstallerSkin( $skin_args ) );
98
99 $upgrader->install( $source );
100
101 wp_cache_flush();
102
103 if ( $upgrader->plugin_info() ) {
104 $activated = $this->activate_plugin( $upgrader->plugin_info(), $slug );
105
106 if ( ! is_wp_error( $activated ) ) {
107 wp_send_json_success(
108 esc_html__( 'Plugin installed and activated succesfully.', 'borderless' )
109 );
110 } else {
111 wp_send_json_success( $activated->get_error_message() );
112 }
113 }
114
115 wp_send_json_error( esc_html__( 'Could not install the plugin. WP Plugin installer could not retrieve plugin information.', 'borderless' ) );
116 }
117
118 public function install_plugin( $slug ) {
119 if ( empty( $slug ) ) {
120 return false;
121 }
122
123 if ( ! current_user_can( 'install_plugins' ) ) {
124 return false;
125 }
126
127 if ( $this->is_plugin_active( $slug ) ) {
128 return true;
129 }
130
131 if ( $this->is_plugin_installed( $slug ) ) {
132 $activated = $this->activate_plugin( $this->get_plugin_basename_from_slug( $slug ), $slug );
133
134 return ! is_wp_error( $activated );
135 }
136
137 if ( ! $this->filesystem_permissions_allowed() ) {
138 return false;
139 }
140
141 remove_action( 'upgrader_process_complete', [ 'Language_Pack_Upgrader', 'async_upgrade' ], 20 );
142
143 $extra = array();
144 $extra['slug'] = $slug;
145 $source = $this->get_download_url( $slug );
146 $api = empty( $this->get_plugin_data( $slug )['source'] ) ? $this->get_plugins_api( $slug ) : null;
147 $api = ( false !== $api ) ? $api : null;
148
149 if ( ! empty( $api ) && is_wp_error( $api ) ) {
150 return false;
151 }
152
153 if ( ! class_exists( '\Plugin_Upgrader', false ) ) {
154 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
155 }
156
157 $skin_args = array(
158 'type' => 'web',
159 'plugin' => '',
160 'api' => $api,
161 'extra' => $extra,
162 );
163
164 $upgrader = new \Plugin_Upgrader( new PluginInstallerSkinSilent( $skin_args ) );
165
166 $upgrader->install( $source );
167
168 wp_cache_flush();
169
170 if ( $upgrader->plugin_info() ) {
171 $activated = $this->activate_plugin( $upgrader->plugin_info(), $slug );
172
173 if ( ! is_wp_error( $activated ) ) {
174 return true;
175 }
176 }
177
178 return false;
179 }
180
181 private function activate_plugin( $plugin_filename, $slug ) {
182 Helpers::do_action( 'library/plugin_intaller_before_plugin_activation', $slug );
183
184 $activated = activate_plugin( $plugin_filename );
185
186 return $activated;
187 }
188
189 private function filesystem_permissions_allowed() {
190 $library = BorderlessLibraryImporter::get_instance();
191 $url = esc_url_raw( $library->get_plugin_settings_url() );
192 $creds = request_filesystem_credentials( $url, '', false, false, null );
193
194 if ( false === $creds || ! WP_Filesystem( $creds ) ) {
195 return false;
196 }
197
198 return true;
199 }
200
201 public function get_plugin_data( $slug ) {
202 $data = [];
203
204 foreach ( $this->plugins as $plugin ) {
205 if ( $plugin['slug'] === $slug ) {
206 $data = $plugin;
207 break;
208 }
209 }
210
211 return $data;
212 }
213
214 public function get_download_url( $slug ) {
215 $plugin_data = $this->get_plugin_data( $slug );
216
217 if ( ! empty( $plugin_data['source'] ) ) {
218 return $plugin_data['source'];
219 }
220
221 return $this->get_wp_repo_download_url( $slug );
222 }
223
224 protected function get_wp_repo_download_url( $slug ) {
225 $source = '';
226 $api = $this->get_plugins_api( $slug );
227
228 if ( false !== $api && isset( $api->download_link ) ) {
229 $source = $api->download_link;
230 }
231
232 return $source;
233 }
234
235 protected function get_plugins_api( $slug ) {
236 static $api = array(); // Cache received responses.
237
238 if ( ! isset( $api[ $slug ] ) ) {
239 if ( ! function_exists( 'plugins_api' ) ) {
240 require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
241 }
242
243 $api[ $slug ] = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) );
244 }
245
246 return $api[ $slug ];
247 }
248
249 public function get_plugins( $plugin_folder = '' ) {
250 if ( ! function_exists( 'get_plugins' ) ) {
251 require_once ABSPATH . 'wp-admin/includes/plugin.php';
252 }
253
254 return get_plugins( $plugin_folder );
255 }
256
257 protected function get_plugin_basename_from_slug( $slug ) {
258 $keys = array_keys( $this->get_plugins() );
259
260 foreach ( $keys as $key ) {
261 if ( preg_match( '/^' . $slug . '\//', $key ) ) {
262 return $key;
263 }
264 }
265
266 return false;
267 }
268
269 public function is_plugin_installed( $slug ) {
270 return ( ! empty( $this->get_plugin_basename_from_slug( $slug ) ) );
271 }
272
273 public function is_plugin_active( $slug ) {
274 $plugin_path = $this->get_plugin_basename_from_slug( $slug );
275
276 if ( empty( $plugin_path ) ) {
277 return false;
278 }
279
280 return is_plugin_active( $plugin_path );
281 }
282
283 public function get_missing_plugins() {
284 $missing = [];
285
286 foreach ( $this->plugins as $plugin_data ) {
287 if ( ! $this->is_plugin_active( $plugin_data['slug'] ) ) {
288 $missing[] = $plugin_data;
289 }
290 }
291
292 return $missing;
293 }
294
295 private function filter_plugins( $plugins ) {
296 return array_filter(
297 $plugins,
298 function ( $plugin ) {
299 if ( empty( $plugin['slug'] ) || empty( $plugin['name'] ) ) {
300 return false;
301 }
302
303 return true;
304 }
305 );
306 }
307 }
308