PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.2.2
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-post-plugin-theme-handler.php

class-mainwp-post-plugin-theme-handler.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.2.2, at class/class-mainwp-post-plugin-theme-handler.php

991 lines 41.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post Plugin Theme Handler.
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 /**
11 * Class MainWP_Post_Plugin_Theme_Handler
12 *
13 * @package MainWP\Dashboard
14 *
15 * @uses \MainWP\Dashboard\MainWP_Post_Base_Handler
16 */
17 class MainWP_Post_Plugin_Theme_Handler extends MainWP_Post_Base_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
18 // phpcs:disable Generic.Metrics.CyclomaticComplexity -- complexity.
19 /**
20 * Protected static variable to hold the single instance of the class.
21 *
22 * @var mixed Default null
23 */
24 private static $instance = null;
25
26 /**
27 * Method instance()
28 *
29 * Create public static instance.
30 *
31 * @static
32 * @return MainWP_Post_Plugin_Theme_Handler
33 */
34 public static function instance() {
35 if ( null === static::$instance ) {
36 static::$instance = new self();
37 }
38 return static::$instance;
39 }
40
41 /**
42 * Init plugins/themes actions
43 */
44 public function init() {
45 // Page: ManageSites.
46 $this->add_action( 'mainwp_ext_prepareinstallplugintheme', array( &$this, 'mainwp_ext_prepareinstallplugintheme' ) );
47 $this->add_action( 'mainwp_ext_performinstallplugintheme', array( &$this, 'mainwp_ext_performinstallplugintheme' ) );
48
49 // Page: InstallPlugins/Themes.
50 $this->add_action( 'mainwp_preparebulkinstallplugintheme', array( &$this, 'mainwp_preparebulkinstallplugintheme' ) );
51 $this->add_action( 'mainwp_installbulkinstallplugintheme', array( &$this, 'mainwp_installbulkinstallplugintheme' ) );
52 $this->add_action( 'mainwp_preparebulkuploadplugintheme', array( &$this, 'mainwp_preparebulkuploadplugintheme' ) );
53 $this->add_action( 'mainwp_installbulkuploadplugintheme', array( &$this, 'mainwp_installbulkuploadplugintheme' ) );
54 $this->add_action( 'mainwp_cleanbulkuploadplugintheme', array( &$this, 'mainwp_cleanbulkuploadplugintheme' ) );
55 $this->add_action( 'mainwp_preparebulkinstallcheckplugin', array( &$this, 'hook_prepare_installcheck_plugin' ) );
56
57 // Widget: RightNow.
58 $this->add_action( 'mainwp_upgradewp', array( &$this, 'mainwp_upgradewp' ) );
59 $this->add_action( 'mainwp_upgradeplugintheme', array( &$this, 'mainwp_upgrade_plugintheme' ) );
60 $this->add_action( 'mainwp_ignoreplugintheme', array( &$this, 'mainwp_ignoreplugintheme' ) );
61 $this->add_action( 'mainwp_unignoreplugintheme', array( &$this, 'mainwp_unignoreplugintheme' ) );
62 $this->add_action( 'mainwp_ignorepluginsthemes', array( &$this, 'mainwp_ignorepluginsthemes' ) );
63 $this->add_action( 'mainwp_unignorepluginsthemes', array( &$this, 'mainwp_unignore_global_pluginsthemes' ) );
64 $this->add_action( 'mainwp_unignoreabandonedplugintheme', array( &$this, 'mainwp_unignoreabandonedplugintheme' ) );
65 $this->add_action( 'mainwp_unignoreabandonedpluginsthemes', array( &$this, 'mainwp_unignoreabandonedpluginsthemes' ) );
66 $this->add_action( 'mainwp_dismissoutdateplugintheme', array( &$this, 'mainwp_dismissoutdateplugintheme' ) );
67 $this->add_action( 'mainwp_dismissoutdatepluginsthemes', array( &$this, 'mainwp_dismissoutdatepluginsthemes' ) );
68 $this->add_action( 'mainwp_trust_plugin', array( &$this, 'mainwp_trust_plugin' ) );
69 $this->add_action( 'mainwp_trust_theme', array( &$this, 'mainwp_trust_theme' ) );
70 $this->add_action( 'mainwp_updates_ignore_upgrades', array( &$this, 'ajax_ignore_core_updates' ) );
71 $this->add_action( 'mainwp_updates_unignore_upgrades', array( &$this, 'ajax_unignore_core_updates' ) );
72 $this->add_action( 'mainwp_updates_unignore_global_upgrades', array( &$this, 'ajax_unignore_global_upgrades' ) );
73
74 // Page: Themes.
75 $this->add_action( 'mainwp_themes_search', array( &$this, 'mainwp_themes_search' ) );
76 $this->add_action( 'mainwp_themes_search_all', array( &$this, 'mainwp_themes_search_all' ) );
77 if ( mainwp_current_user_have_right( 'dashboard', 'activate_themes' ) ) {
78 $this->add_action( 'mainwp_theme_activate', array( &$this, 'mainwp_theme_activate' ) );
79 }
80 if ( mainwp_current_user_have_right( 'dashboard', 'delete_themes' ) ) {
81 $this->add_action( 'mainwp_theme_delete', array( &$this, 'mainwp_theme_delete' ) );
82 }
83 $this->add_action( 'mainwp_trusted_theme_notes_save', array( &$this, 'mainwp_trusted_theme_notes_save' ) );
84 if ( mainwp_current_user_have_right( 'dashboard', 'ignore_unignore_updates' ) ) {
85 $this->add_action( 'mainwp_theme_ignore_updates', array( &$this, 'mainwp_theme_ignore_updates' ) );
86 }
87
88 // Page: Plugins.
89 $this->add_action( 'mainwp_plugins_search', array( &$this, 'mainwp_plugins_search' ) );
90 $this->add_action( 'mainwp_plugins_search_all_active', array( &$this, 'mainwp_plugins_search_all_active' ) );
91
92 if ( mainwp_current_user_have_right( 'dashboard', 'activate_deactivate_plugins' ) ) {
93 $this->add_action( 'mainwp_plugin_activate', array( &$this, 'mainwp_plugin_activate' ) );
94 $this->add_action( 'mainwp_plugin_deactivate', array( &$this, 'mainwp_plugin_deactivate' ) );
95 }
96 if ( mainwp_current_user_have_right( 'dashboard', 'delete_plugins' ) ) {
97 $this->add_action( 'mainwp_plugin_delete', array( &$this, 'mainwp_plugin_delete' ) );
98 }
99
100 if ( mainwp_current_user_have_right( 'dashboard', 'ignore_unignore_updates' ) ) {
101 $this->add_action( 'mainwp_plugin_ignore_updates', array( &$this, 'mainwp_plugin_ignore_updates' ) );
102 }
103 $this->add_action( 'mainwp_trusted_plugin_notes_save', array( &$this, 'mainwp_trusted_plugin_notes_save' ) );
104
105 // Widget: Plugins.
106 $this->add_action( 'mainwp_widget_plugin_activate', array( &$this, 'mainwp_widget_plugin_activate' ) );
107 $this->add_action( 'mainwp_widget_plugin_deactivate', array( &$this, 'mainwp_widget_plugin_deactivate' ) );
108 $this->add_action( 'mainwp_widget_plugin_delete', array( &$this, 'mainwp_widget_plugin_delete' ) );
109
110 // Widget: Themes.
111 $this->add_action( 'mainwp_widget_theme_activate', array( &$this, 'mainwp_widget_theme_activate' ) );
112 $this->add_action( 'mainwp_widget_theme_delete', array( &$this, 'mainwp_widget_theme_delete' ) );
113 }
114
115 /**
116 * Method mainwp_themes_search()
117 *
118 * Search handler for Themes.
119 *
120 * @uses \MainWP\Dashboard\MainWP_Cache::init_session()
121 *
122 * @uses \MainWP\Dashboard\MainWP_Themes::render_table()
123 */
124 public function mainwp_themes_search() {
125 $this->secure_request( 'mainwp_themes_search' );
126 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
127 $keyword = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : '';
128 $status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : '';
129 $groups = isset( $_POST['groups'] ) && is_array( $_POST['groups'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['groups'] ) ) : array();
130 $sites = isset( $_POST['sites'] ) && is_array( $_POST['sites'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['sites'] ) ) : array();
131 $clients = isset( $_POST['clients'] ) && is_array( $_POST['clients'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['clients'] ) ) : array();
132
133 $not_criteria = isset( $_POST['not_criteria'] ) && 'true' === $_POST['not_criteria'] ? true : false;
134 // phpcs:enable
135 MainWP_Cache::init_session();
136 $result = MainWP_Themes::render_table( $keyword, $status, $groups, $sites, $not_criteria, $clients );
137 wp_send_json( $result );
138 }
139
140 /**
141 * Method mainwp_theme_activate()
142 *
143 * Activate Theme,
144 * Page: Themes.
145 *
146 * @uses \MainWP\Dashboard\MainWP_Themes_Handler::activate_theme()
147 */
148 public function mainwp_theme_activate() {
149 $this->secure_request( 'mainwp_theme_activate' );
150 MainWP_Themes_Handler::activate_theme();
151 die();
152 }
153
154 /**
155 * Method mainwp_theme_delete()
156 *
157 * Delete Theme,
158 * Page: Themes.
159 *
160 * @uses \MainWP\Dashboard\MainWP_Themes_Handler::delete_themes()
161 */
162 public function mainwp_theme_delete() {
163 $this->secure_request( 'mainwp_theme_delete' );
164 MainWP_Themes_Handler::delete_themes();
165 die();
166 }
167
168 /**
169 * Method mainwp_theme_ignore_updates()
170 *
171 * Ignore theme updates,
172 * Page: Themes.
173 *
174 * @uses \MainWP\Dashboard\MainWP_Themes_Handler::ignore_updates()
175 */
176 public function mainwp_theme_ignore_updates() {
177 $this->secure_request( 'mainwp_theme_ignore_updates' );
178 MainWP_Themes_Handler::ignore_updates();
179 die();
180 }
181
182 /**
183 * Method mainwp_themes_search_all()
184 *
185 * Search ALL handler for,
186 * Page: Themes.
187 *
188 * @uses \MainWP\Dashboard\MainWP_Cache::init_session()
189 * @uses \MainWP\Dashboard\MainWP_Themes::render_all_themes_table()
190 */
191 public function mainwp_themes_search_all() {
192 $this->secure_request( 'mainwp_themes_search_all' );
193 MainWP_Cache::init_session();
194 MainWP_Themes::render_all_themes_table();
195 die();
196 }
197
198 /**
199 * Method mainwp_trusted_theme_notes_save()
200 *
201 * Save trusted theme notes,
202 * Page: Themes.
203 *
204 * @uses \MainWP\Dashboard\MainWP_Themes_Handler::save_trusted_theme_note()
205 */
206 public function mainwp_trusted_theme_notes_save() {
207 $this->secure_request( 'mainwp_trusted_theme_notes_save' );
208 MainWP_Themes_Handler::save_trusted_theme_note();
209 die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) );
210 }
211
212 /**
213 * Method mainwp_plugins_search()
214 *
215 * Search handler for Plugins.
216 *
217 * @uses \MainWP\Dashboard\MainWP_Cache::init_session()
218 * @uses \MainWP\Dashboard\MainWP_Plugins::render_table()
219 */
220 public function mainwp_plugins_search() {
221 $this->secure_request( 'mainwp_plugins_search' );
222 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
223 $keyword = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : '';
224 $status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : '';
225 $groups = isset( $_POST['groups'] ) && is_array( $_POST['groups'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['groups'] ) ) : '';
226 $sites = isset( $_POST['sites'] ) && is_array( $_POST['sites'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['sites'] ) ) : '';
227 $clients = isset( $_POST['clients'] ) && is_array( $_POST['clients'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['clients'] ) ) : '';
228 $not_criteria = isset( $_POST['not_criteria'] ) && 'true' === $_POST['not_criteria'] ? true : false;
229 // phpcs:enable
230 MainWP_Cache::init_session();
231 $result = MainWP_Plugins::render_table( $keyword, $status, $groups, $sites, $not_criteria, $clients );
232 wp_send_json( $result );
233 }
234
235 /**
236 * Method mainwp_plugins_search_all_active()
237 *
238 * Search all Active handler for,
239 * Page: Plugins.
240 *
241 * @uses \MainWP\Dashboard\MainWP_Cache::init_session()
242 * @uses \MainWP\Dashboard\MainWP_Plugins::render_all_active_table()
243 */
244 public function mainwp_plugins_search_all_active() {
245 $this->secure_request( 'mainwp_plugins_search_all_active' );
246 MainWP_Cache::init_session();
247 MainWP_Plugins::render_all_active_table();
248 die();
249 }
250
251 /**
252 * Method mainwp_plugin_activate()
253 *
254 * Activate plugins,
255 * Page: Plugins.
256 *
257 * @uses \MainWP\Dashboard\MainWP_Plugins_Handler::activate_plugins()
258 */
259 public function mainwp_plugin_activate() {
260 $this->secure_request( 'mainwp_plugin_activate' );
261 MainWP_Plugins_Handler::activate_plugins();
262 die();
263 }
264
265 /**
266 * Method mainwp_plugin_deactivate()
267 *
268 * Deactivate plugins,
269 * Page: Plugins.
270 *
271 * @uses \MainWP\Dashboard\MainWP_Plugins_Handler::deactivate_plugins()
272 */
273 public function mainwp_plugin_deactivate() {
274 $this->secure_request( 'mainwp_plugin_deactivate' );
275 MainWP_Plugins_Handler::deactivate_plugins();
276 die();
277 }
278
279 /**
280 * Method mainwp_plugin_delete()
281 *
282 * Delete plugins,
283 * Page: Plugins.
284 *
285 * @uses \MainWP\Dashboard\MainWP_Plugins_Handler::delete_plugins()
286 */
287 public function mainwp_plugin_delete() {
288 $this->secure_request( 'mainwp_plugin_delete' );
289 MainWP_Plugins_Handler::delete_plugins();
290 die();
291 }
292
293 /**
294 * Method mainwp_plugin_ignore_updates()
295 *
296 * Ignore plugins updates,
297 * Page: Plugins.
298 *
299 * @uses \MainWP\Dashboard\MainWP_Plugins_Handler::ignore_updates()
300 */
301 public function mainwp_plugin_ignore_updates() {
302 $this->secure_request( 'mainwp_plugin_ignore_updates' );
303 MainWP_Plugins_Handler::ignore_updates();
304 die();
305 }
306
307 /**
308 * Method mainwp_trusted_plugin_notes_save()
309 *
310 * Save trusted plugin notes,
311 * Page: Plugins.
312 *
313 * @uses \MainWP\Dashboard\MainWP_Plugins_Handler::save_trusted_plugin_note()
314 */
315 public function mainwp_trusted_plugin_notes_save() {
316 $this->secure_request( 'mainwp_trusted_plugin_notes_save' );
317 MainWP_Plugins_Handler::save_trusted_plugin_note();
318 die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) );
319 }
320
321 /**
322 * Method mainwp_widget_plugin_activate()
323 *
324 * Activate plugin,
325 * Widget: Plugins.
326 *
327 * @uses \MainWP\Dashboard\MainWP_Widget_Plugins::activate_plugin()
328 */
329 public function mainwp_widget_plugin_activate() {
330 $this->secure_request( 'mainwp_widget_plugin_activate' );
331 MainWP_Widget_Plugins::activate_plugin();
332 }
333
334 /**
335 * Method mainwp_widget_plugin_deactivate()
336 *
337 * Deactivate plugin,
338 * Widget: Plugins.
339 *
340 * @uses \MainWP\Dashboard\MainWP_Widget_Plugins::deactivate_plugin()
341 */
342 public function mainwp_widget_plugin_deactivate() {
343 $this->secure_request( 'mainwp_widget_plugin_deactivate' );
344 MainWP_Widget_Plugins::deactivate_plugin();
345 }
346
347 /**
348 * Method mainwp_widget_plugin_delete()
349 *
350 * Delete plugin,
351 * Widget: Plugins.
352 *
353 * @uses \MainWP\Dashboard\MainWP_Widget_Plugins::delete_plugin()
354 */
355 public function mainwp_widget_plugin_delete() {
356 $this->secure_request( 'mainwp_widget_plugin_delete' );
357 MainWP_Widget_Plugins::delete_plugin();
358 }
359
360 /**
361 * Method mainwp_widget_theme_activate()
362 *
363 * Activate theme,
364 * Widget: Themes.
365 *
366 * @uses \MainWP\Dashboard\MainWP_Widget_Themes::activate_theme()
367 */
368 public function mainwp_widget_theme_activate() {
369 $this->secure_request( 'mainwp_widget_theme_activate' );
370 MainWP_Widget_Themes::activate_theme();
371 }
372
373 /**
374 * Method mainwp_widget_theme_delete()
375 *
376 * Delete theme,
377 * Widget: Themes.
378 *
379 * @uses \MainWP\Dashboard\MainWP_Widget_Themes::delete_theme()
380 */
381 public function mainwp_widget_theme_delete() {
382 $this->secure_request( 'mainwp_widget_theme_delete' );
383 MainWP_Widget_Themes::delete_theme();
384 }
385
386 /**
387 * Method mainwp_preparebulkinstallplugintheme()
388 *
389 * Prepair bulk installation of plugins & themes,
390 * Page: InstallPlugins/Themes.
391 *
392 * @uses \MainWP\Dashboard\MainWP_Install_Bulk::prepare_install()
393 */
394 public function mainwp_preparebulkinstallplugintheme() {
395 $this->secure_request( 'mainwp_preparebulkinstallplugintheme' );
396 MainWP_Install_Bulk::prepare_install();
397 }
398
399 /**
400 * Method mainwp_installbulkinstallplugintheme()
401 *
402 * Installation of plugins & themes,
403 * Page: InstallPlugins/Themes.
404 *
405 * @uses \MainWP\Dashboard\MainWP_Install_Bulk::perform_install()
406 */
407 public function mainwp_installbulkinstallplugintheme() {
408 $this->secure_request( 'mainwp_installbulkinstallplugintheme' );
409 MainWP_Install_Bulk::perform_install();
410 }
411
412 /**
413 * Method mainwp_preparebulkuploadplugintheme()
414 *
415 * Prepair bulk upload of plugins & themes,
416 * Page: InstallPlugins/Themes.
417 *
418 * @uses \MainWP\Dashboard\MainWP_Install_Bulk::prepare_upload()
419 */
420 public function mainwp_preparebulkuploadplugintheme() {
421 $this->secure_request( 'mainwp_preparebulkuploadplugintheme' );
422 MainWP_Install_Bulk::prepare_upload();
423 }
424
425 /**
426 * Method mainwp_installbulkuploadplugintheme()
427 *
428 * Bulk upload of plugins & themes,
429 * Page: InstallPlugins/Themes.
430 *
431 * @uses \MainWP\Dashboard\MainWP_Install_Bulk::perform_upload()
432 */
433 public function mainwp_installbulkuploadplugintheme() {
434 $this->secure_request( 'mainwp_installbulkuploadplugintheme' );
435 MainWP_Install_Bulk::perform_upload();
436 }
437
438 /**
439 * Method mainwp_cleanbulkuploadplugintheme()
440 *
441 * Clean upload of plugins & themes,
442 * Page: InstallPlugins/Themes.
443 *
444 * @uses \MainWP\Dashboard\MainWP_Install_Bulk::clean_upload()
445 */
446 public function mainwp_cleanbulkuploadplugintheme() {
447 $this->secure_request( 'mainwp_cleanbulkuploadplugintheme' );
448 MainWP_Install_Bulk::clean_upload();
449 }
450
451 /**
452 * Method mainwp_ext_prepareinstallplugintheme()
453 *
454 * Prepair Installation of plugins & themes,
455 * Page: ManageSites.
456 */
457 public function mainwp_ext_prepareinstallplugintheme() {
458 do_action( 'mainwp_prepareinstallplugintheme' );
459 }
460
461 /**
462 * Method mainwp_ext_performinstallplugintheme()
463 *
464 * Installation of plugins & themes,
465 * Page: ManageSites.
466 */
467 public function mainwp_ext_performinstallplugintheme() {
468 $this->secure_request( 'mainwp_ext_performinstallplugintheme' );
469 do_action( 'mainwp_performinstallplugintheme' );
470 }
471
472 /**
473 * Method hook_prepare_installcheck_plugin()
474 *
475 * Prepair bulk installation of plugins,
476 */
477 public function hook_prepare_installcheck_plugin() {
478 $this->secure_request( 'mainwp_preparebulkinstallcheckplugin' );
479 include_once ABSPATH . '/wp-admin/includes/plugin-install.php'; // NOSONAR - WP compatible.
480 $api = MainWP_System_Utility::get_plugin_theme_info(
481 'plugin',
482 array(
483 'slug' => isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '', // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
484 'fields' => array( 'sections' => false ),
485 )
486 ); // Save on a bit of bandwidth.
487 $url = $api->download_link;
488 $output = array();
489 $output['url'] = $url;
490 wp_send_json( $output );
491 }
492
493 /**
494 * Method mainwp_upgradewp()
495 *
496 * Update a specific WP core.
497 *
498 * @uses \MainWP\Dashboard\MainWP_Exception
499 * @uses \MainWP\Dashboard\MainWP_Updates_Handler::upgrade_site()
500 */
501 public function mainwp_upgradewp() {
502 if ( ! mainwp_current_user_have_right( 'dashboard', 'update_wordpress' ) ) {
503 die( wp_json_encode( array( 'error' => mainwp_do_not_have_permissions( esc_html__( 'update WordPress', 'mainwp' ), false ) ) ) );
504 }
505
506 $this->secure_request( 'mainwp_upgradewp' );
507
508 try {
509 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
510 die( wp_json_encode( array( 'result' => MainWP_Updates_Handler::upgrade_site( $id ) ) ) ); // ok.
511 } catch ( MainWP_Exception $e ) {
512 die(
513 wp_json_encode(
514 array(
515 'error' => array(
516 'message' => $e->getMessage(),
517 'extra' => $e->get_message_extra(),
518 ),
519 )
520 )
521 );
522 }
523 }
524
525 /**
526 * Method mainwp_upgrade_plugintheme()
527 *
528 * Update plugin or theme.
529 *
530 * @uses \MainWP\Dashboard\MainWP_DB_Backup::backup_full_task_running()
531 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
532 * @uses \MainWP\Dashboard\MainWP_Exception
533 * @uses \MainWP\Dashboard\MainWP_Updates_Handler::get_plugin_theme_slugs()
534 * @uses \MainWP\Dashboard\MainWP_Updates_Handler::upgrade_plugin_theme_translation()
535 */
536 public function mainwp_upgrade_plugintheme() { // phpcs:ignore -- NOSONAR -Current complexity is the only way to achieve desired results, pull request solutions appreciated.
537
538 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
539 if ( ! isset( $_POST['type'] ) ) {
540 die( wp_json_encode( array( 'error' => '<i class="red times icon"></i> ' . esc_html__( 'Plugin or theme not specified. Please reload the page and try again.', 'mainwp' ) ) ) );
541 }
542
543 if ( 'plugin' === $_POST['type'] && ! mainwp_current_user_have_right( 'dashboard', 'update_plugins' ) ) {
544 die( wp_json_encode( array( 'error' => mainwp_do_not_have_permissions( esc_html__( 'update plugins', 'mainwp' ), false ) ) ) );
545 }
546
547 if ( 'theme' === $_POST['type'] && ! mainwp_current_user_have_right( 'dashboard', 'update_themes' ) ) {
548 die( wp_json_encode( array( 'error' => mainwp_do_not_have_permissions( esc_html__( 'update themes', 'mainwp' ), false ) ) ) );
549 }
550
551 if ( 'translation' === $_POST['type'] && ! mainwp_current_user_have_right( 'dashboard', 'update_translations' ) ) {
552 die( wp_json_encode( array( 'error' => mainwp_do_not_have_permissions( esc_html__( 'update translations', 'mainwp' ), false ) ) ) );
553 }
554
555 $this->secure_request( 'mainwp_upgradeplugintheme' );
556
557 // support chunk update for manage sites page only.
558 $chunk_support = ! empty( $_POST['chunk_support'] ) ? true : false;
559 $max_update = 0;
560 $websiteId = null;
561 $slugs = '';
562
563 if ( isset( $_POST['websiteId'] ) ) {
564 $websiteId = intval( $_POST['websiteId'] );
565
566 if ( $chunk_support ) {
567 /**
568 * Filter: mainwp_update_plugintheme_max
569 *
570 * Filters the max number of plugins/themes to be updated in one run in order to improve performance.
571 *
572 * @param int $websiteId Child site ID.
573 *
574 * @since Unknown
575 */
576 $max_update = apply_filters( 'mainwp_update_plugintheme_max', false, $websiteId );
577 if ( empty( $max_update ) ) {
578 $chunk_support = false; // there is no hook so disable chunk update support.
579 }
580 }
581 if ( $chunk_support ) {
582 if ( isset( $_POST['chunk_slugs'] ) ) {
583 $slugs = wp_unslash( $_POST['chunk_slugs'] );
584 } else {
585 $slugs = MainWP_Updates_Handler::get_plugin_theme_slugs( $websiteId, sanitize_text_field( wp_unslash( $_POST['type'] ) ) );
586 }
587 } elseif ( isset( $_POST['slug'] ) ) {
588 $slugs = wp_unslash( $_POST['slug'] );
589 } else {
590 $slugs = MainWP_Updates_Handler::get_plugin_theme_slugs( $websiteId, sanitize_text_field( wp_unslash( $_POST['type'] ) ) );
591 }
592 }
593 // phpcs:enable
594
595 if ( MainWP_DB_Backup::instance()->backup_full_task_running( $websiteId ) ) {
596 die( wp_json_encode( array( 'error' => esc_html__( 'Backup process in progress on the child site. Please, try again later.', 'mainwp' ) ) ) );
597 }
598
599 $chunk_slugs = array();
600
601 if ( $chunk_support && $max_update ) {
602 $slugs = explode( ',', $slugs );
603 $chunk_slugs = array_slice( $slugs, $max_update );
604 $update_slugs = array_diff( $slugs, $chunk_slugs );
605 $slugs = implode( ',', $update_slugs );
606 }
607
608 if ( empty( $slugs ) && ! $chunk_support ) {
609 die( wp_json_encode( array( 'message' => esc_html__( 'Item slug could not be found. Update process could not be executed.', 'mainwp' ) ) ) );
610 }
611 $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
612 try {
613 $info = array(
614 'result' => array(),
615 'result_error' => array(),
616 );
617
618 $result = MainWP_Updates_Handler::upgrade_plugin_theme_translation( $websiteId, sanitize_text_field( wp_unslash( $_POST['type'] ) ), $slugs ); // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
619
620 if ( is_array( $result ) ) {
621 if ( isset( $result['result'] ) ) {
622 $info['result'] = $result['result'];
623 }
624 if ( isset( $result['result_error'] ) ) {
625 $info['result_error'] = $result['result_error'];
626 }
627 }
628
629 if ( $chunk_support && ( ! empty( $chunk_slugs ) ) ) {
630 $info['chunk_slugs'] = implode( ',', $chunk_slugs );
631 }
632
633 if ( ! empty( $website ) ) {
634 $info['site_url'] = esc_url( $website->url );
635 }
636 wp_send_json( $info );
637 } catch ( MainWP_Exception $e ) {
638 die(
639 wp_json_encode(
640 array(
641 'error' => array(
642 'message' => $e->getMessage(),
643 'extra' => $e->get_message_extra(),
644 'errorCode' => $e->get_message_error_code(),
645 ),
646 )
647 )
648 );
649 }
650 }
651
652 /**
653 * Method mainwp_ignoreplugintheme()
654 *
655 * Ignores a plugin or a theme.
656 */
657 public function mainwp_ignoreplugintheme() {
658 $this->secure_request( 'mainwp_ignoreplugintheme' );
659
660 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
661 if ( ! isset( $_POST['id'] ) ) {
662 die( wp_json_encode( array( 'error' => esc_html__( 'Item ID not found. Please reload the page and try again.', 'mainwp' ) ) ) );
663 }
664 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
665 $slug = isset( $_POST['slug'] ) ? esc_html( wp_unslash( $_POST['slug'] ) ) : '';
666 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
667 $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
668 $ver = isset( $_POST['ignore_ver'] ) ? sanitize_text_field( wp_unslash( $_POST['ignore_ver'] ) ) : '';
669 // phpcs:enable
670 wp_send_json( array( 'result' => MainWP_Updates_Handler::ignore_plugin_theme( $type, $slug, $name, $id, $ver ) ) );
671 }
672
673
674 /**
675 * Method mainwp_unignore_global_pluginsthemes()
676 *
677 * Unignore plugins or themes.
678 */
679 public function mainwp_unignore_global_pluginsthemes() {
680 $this->secure_request( 'mainwp_unignorepluginsthemes' );
681
682 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
683 if ( ! isset( $_POST['slug'] ) ) {
684 die( wp_json_encode( array( 'error' => esc_html__( 'Item slug not found. Please reload the page and try again.', 'mainwp' ) ) ) );
685 }
686 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
687 $slug = isset( $_POST['slug'] ) ? wp_unslash( $_POST['slug'] ) : '';
688 $ver = isset( $_POST['ignore_ver'] ) ? wp_unslash( $_POST['ignore_ver'] ) : '';
689 // phpcs:enable
690 die( wp_json_encode( array( 'result' => MainWP_Updates_Handler::unignore_global_plugins_themes( $type, $slug, $ver ) ) ) );
691 }
692
693 /**
694 * Method mainwp_unignoreabandonedplugintheme()
695 *
696 * Unignore abandoned plugin or theme.
697 *
698 * @uses \MainWP\Dashboard\MainWP_Updates_Handler::ignore_plugin_theme()
699 * @uses \MainWP\Dashboard\MainWP_Updates_Handler::unignore_abandoned_plugin_theme()
700 */
701 public function mainwp_unignoreabandonedplugintheme() {
702 $this->secure_request( 'mainwp_unignoreabandonedplugintheme' );
703
704 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
705 if ( ! isset( $_POST['id'] ) ) {
706 die( wp_json_encode( array( 'error' => esc_html__( 'Item ID not found. Please reload the page and try again.', 'mainwp' ) ) ) );
707 }
708
709 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
710 $slug = isset( $_POST['slug'] ) ? esc_html( wp_unslash( $_POST['slug'] ) ) : '';
711 $id = isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : 0; // string|int.
712 // phpcs:enable
713 die( wp_json_encode( array( 'result' => MainWP_Updates_Handler::unignore_abandoned_plugin_theme( $type, $slug, $id ) ) ) ); // ok.
714 }
715
716 /**
717 * Method mainwp_unignoreabandonedpluginthemes()
718 *
719 * Unignore abandoned plugins or themes.
720 *
721 * @uses \MainWP\Dashboard\MainWP_Updates_Handler::unignore_abandoned_plugins_themes()
722 */
723 public function mainwp_unignoreabandonedpluginsthemes() {
724 $this->secure_request( 'mainwp_unignoreabandonedpluginsthemes' );
725
726 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
727 if ( ! isset( $_POST['slug'] ) ) {
728 die( wp_json_encode( array( 'error' => esc_html__( 'Item slug not found. Please reload the page and try again.', 'mainwp' ) ) ) );
729 }
730
731 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
732 $slug = isset( $_POST['slug'] ) ? esc_html( wp_unslash( $_POST['slug'] ) ) : '';
733 // phpcs:enable
734 die( wp_json_encode( array( 'result' => MainWP_Updates_Handler::unignore_abandoned_plugins_themes( $type, $slug ) ) ) );
735 }
736
737 /**
738 * Method mainwp_dismissoutdateplugintheme()
739 *
740 * Dismiss outdated plugin or theme.
741 *
742 * @uses \MainWP\Dashboard\MainWP_Updates_Handler::dismiss_plugin_theme()
743 */
744 public function mainwp_dismissoutdateplugintheme() {
745 $this->secure_request( 'mainwp_dismissoutdateplugintheme' );
746
747 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
748 if ( ! isset( $_POST['id'] ) ) {
749 die( wp_json_encode( array( 'error' => esc_html__( 'Item ID not found. Please reload the page and try again.', 'mainwp' ) ) ) );
750 }
751 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
752 $slug = isset( $_POST['slug'] ) ? esc_html( wp_unslash( $_POST['slug'] ) ) : '';
753 $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
754 $id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
755 // phpcs:enable
756 die( wp_json_encode( array( 'result' => MainWP_Updates_Handler::dismiss_plugin_theme( $type, $slug, $name, $id ) ) ) );
757 }
758
759 /**
760 * Method mainwp_dismissoutdatepluginthemes()
761 *
762 * Dismiss outdated plugins or themes.
763 *
764 * @uses \MainWP\Dashboard\MainWP_Updates_Handler::dismiss_plugins_themes()
765 */
766 public function mainwp_dismissoutdatepluginsthemes() {
767 $this->secure_request( 'mainwp_dismissoutdatepluginsthemes' );
768
769 if ( ! mainwp_current_user_have_right( 'dashboard', 'ignore_unignore_updates' ) ) {
770 die( wp_json_encode( array( 'error' => mainwp_do_not_have_permissions( esc_html__( 'ignore/unignore updates', 'mainwp' ) ) ) ) );
771 }
772
773 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
774 if ( ! isset( $_POST['slug'] ) ) {
775 die( wp_json_encode( array( 'error' => esc_html__( 'Item slug not found. Please reload the page and try again.', 'mainwp' ) ) ) );
776 }
777
778 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
779 $slug = isset( $_POST['slug'] ) ? esc_html( wp_unslash( $_POST['slug'] ) ) : '';
780 $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
781 // phpcs:enable
782
783 die( wp_json_encode( array( 'result' => MainWP_Updates_Handler::dismiss_plugins_themes( $type, $slug, $name ) ) ) );
784 }
785
786 /**
787 * Method mainwp_unignoreplugintheme()
788 *
789 * Unignore plugin or theme.
790 *
791 * @uses \MainWP\Dashboard\MainWP_Updates_Handler::unignore_plugin_theme()
792 */
793 public function mainwp_unignoreplugintheme() {
794 $this->secure_request( 'mainwp_unignoreplugintheme' );
795
796 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
797 if ( ! isset( $_POST['id'] ) ) {
798 die( wp_json_encode( array( 'error' => esc_html__( 'Item ID not found. Please reload the page and try again.', 'mainwp' ) ) ) );
799 }
800 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
801 $slug = isset( $_POST['slug'] ) ? wp_unslash( $_POST['slug'] ) : '';
802 $id = isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : '';
803 $ver = isset( $_POST['ignore_ver'] ) ? sanitize_text_field( wp_unslash( $_POST['ignore_ver'] ) ) : '';
804
805 // phpcs:enable
806 die( wp_json_encode( array( 'result' => MainWP_Updates_Handler::unignore_plugin_theme( $type, $slug, $id, $ver ) ) ) );
807 }
808
809 /**
810 * Method mainwp_ignorepluginsthemes()
811 *
812 * Ignore plugins or themes.
813 *
814 * @uses \MainWP\Dashboard\MainWP_Updates_Handler::ignore_plugins_themes()
815 */
816 public function mainwp_ignorepluginsthemes() {
817 $this->secure_request( 'mainwp_ignorepluginsthemes' );
818
819 if ( ! mainwp_current_user_have_right( 'dashboard', 'ignore_unignore_updates' ) ) {
820 die( wp_json_encode( array( 'error' => mainwp_do_not_have_permissions( esc_html__( 'ignore/unignore updates', 'mainwp' ) ) ) ) );
821 }
822
823 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
824 if ( ! isset( $_POST['slug'] ) ) {
825 die( wp_json_encode( array( 'error' => esc_html__( 'Item slug not found. Please reload the page and try again.', 'mainwp' ) ) ) );
826 }
827
828 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
829 $slug = isset( $_POST['slug'] ) ? esc_html( wp_unslash( $_POST['slug'] ) ) : '';
830 $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '';
831 $ver = isset( $_POST['ignore_ver'] ) ? sanitize_text_field( wp_unslash( $_POST['ignore_ver'] ) ) : '';
832
833 // phpcs:enable
834 die( wp_json_encode( array( 'result' => MainWP_Updates_Handler::ignore_plugins_themes( $type, $slug, $name, $ver ) ) ) );
835 }
836
837 /**
838 * Method ajax_ignore_core_updates()
839 *
840 * Ignore plugins or themes.
841 *
842 * @uses \MainWP\Dashboard\MainWP_Updates_Handler::ignore_plugins_themes()
843 */
844 public function ajax_ignore_core_updates() { //phpcs:ignore -- NOSONAR complex function.
845
846 $this->secure_request( 'mainwp_updates_ignore_upgrades' );
847
848 if ( ! mainwp_current_user_have_right( 'dashboard', 'ignore_unignore_updates' ) ) {
849 die( wp_json_encode( array( 'error' => mainwp_do_not_have_permissions( esc_html__( 'ignore/unignore updates', 'mainwp' ) ) ) ) );
850 }
851
852 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
853 if ( empty( $_POST['slug'] ) ) {
854 die( wp_json_encode( array( 'error' => esc_html__( 'Item slug not found. Please reload the page and try again.', 'mainwp' ) ) ) );
855 }
856
857 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
858 $ignore = isset( $_POST['ignore'] ) ? sanitize_text_field( wp_unslash( $_POST['ignore'] ) ) : '';
859 $site_id = isset( $_POST['site_id'] ) ? intval( $_POST['site_id'] ) : 0;
860 $ver = isset( $_POST['ignore_ver'] ) ? sanitize_text_field( wp_unslash( $_POST['ignore_ver'] ) ) : '';
861
862 if ( 'wp' === $type ) {
863 if ( ( 'this_version' === $ignore || 'all_versions' === $ignore ) && $site_id ) {
864 $website = MainWP_DB::instance()->get_website_by_id( $site_id, false, array( 'ignored_wp_upgrades' ) );
865
866 if ( empty( $website ) ) {
867 wp_send_json( array( 'error' => esc_html__( 'Site ID invalid or site not found. Please try again.', 'mainwp' ) ) );
868 }
869
870 $ignored_upgrades = ! empty( $website->ignored_wp_upgrades ) ? json_decode( $website->ignored_wp_upgrades, true ) : array();
871
872 if ( ! is_array( $ignored_upgrades ) ) {
873 $ignored_upgrades = array();
874 }
875
876 $ignored_vers = is_array( $ignored_upgrades ) && isset( $ignored_upgrades['ignored_versions'] ) ? $ignored_upgrades['ignored_versions'] : array();
877 if ( ! is_array( $ignored_vers ) ) {
878 $ignored_vers = array();
879 }
880
881 if ( empty( $ver ) || 'all_versions' === $ver ) { // ignore all.
882 $ignored_vers = array( 'all_versions' );
883 } else {
884 $ver = urldecode( $ver );
885 if ( ! in_array( $ver, $ignored_vers ) ) {
886 $ignored_vers[] = $ver;
887 }
888 }
889
890 $ignored_upgrades['ignored_versions'] = $ignored_vers;
891
892 MainWP_DB::instance()->update_website_option(
893 $site_id,
894 'ignored_wp_upgrades',
895 wp_json_encode( $ignored_upgrades )
896 );
897
898 wp_send_json( array( 'success' => 1 ) );
899
900 } elseif ( 'this_version_global' === $ignore ) {
901
902 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
903 $decodedIgnoredCores = ! empty( $userExtension->ignored_wp_upgrades ) ? json_decode( $userExtension->ignored_wp_upgrades, true ) : array();
904
905 if ( ! is_array( $decodedIgnoredCores ) ) {
906 $decodedIgnoredCores = array();
907 }
908
909 $ignored_vers = isset( $decodedIgnoredCores['ignored_versions'] ) ? $decodedIgnoredCores['ignored_versions'] : array();
910
911 if ( ! is_array( $ignored_vers ) ) {
912 $ignored_vers = array();
913 }
914
915 $ver = urldecode( $ver );
916 if ( ! in_array( $ver, $ignored_vers ) ) {
917 $ignored_vers[] = $ver;
918 }
919
920 $decodedIgnoredCores['ignored_versions'] = $ignored_vers;
921
922 MainWP_DB_Common::instance()->update_user_extension(
923 array(
924 'userid' => null,
925 'ignored_wp_upgrades' => wp_json_encode( $decodedIgnoredCores ),
926 )
927 );
928 wp_send_json( array( 'success' => 1 ) );
929 }
930 }
931 wp_send_json( array( 'error' => esc_html__( 'Invalid types. Please try again.', 'mainwp' ) ) );
932 }
933
934 /**
935 * Method ajax_unignore_core_updates()
936 *
937 * Unignore plugin or theme.
938 */
939 public function ajax_unignore_core_updates() {
940 $this->secure_request( 'mainwp_updates_unignore_upgrades' );
941 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
942 $id = isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : ''; // numberic id or _ALL_.
943 $ver = isset( $_POST['ignore_ver'] ) ? sanitize_text_field( wp_unslash( $_POST['ignore_ver'] ) ) : '';
944 // phpcs:enable
945 die( wp_json_encode( array( 'result' => MainWP_Updates_Handler::unignore_core_updates( $id, $ver ) ) ) );
946 }
947
948
949 /**
950 * Method ajax_unignore_global_upgrades()
951 *
952 * Unignore plugin or theme.
953 */
954 public function ajax_unignore_global_upgrades() {
955 $this->secure_request( 'mainwp_updates_unignore_global_upgrades' );
956 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
957 $slug = isset( $_POST['slug'] ) ? wp_unslash( $_POST['slug'] ) : '';
958 $ver = isset( $_POST['ignore_ver'] ) ? sanitize_text_field( wp_unslash( $_POST['ignore_ver'] ) ) : '';
959 // phpcs:enable
960 die( wp_json_encode( array( 'result' => MainWP_Updates_Handler::unignore_global_cores( $slug, $ver ) ) ) );
961 }
962
963
964 /**
965 * Method mainwp_trust_plugin()
966 *
967 * Trust plugin.
968 *
969 * @uses \MainWP\Dashboard\MainWP_Plugins_Handler::trust_post()
970 * @uses \MainWP\Dashboard\MainWP_Themes_Handler::trust_post()
971 */
972 public function mainwp_trust_plugin() {
973 $this->secure_request( 'mainwp_trust_plugin' );
974
975 MainWP_Plugins_Handler::trust_post();
976 die( wp_json_encode( array( 'result' => true ) ) );
977 }
978
979 /**
980 * Method mainwp_trust_theme()
981 *
982 * Trust theme.
983 */
984 public function mainwp_trust_theme() {
985 $this->secure_request( 'mainwp_trust_theme' );
986
987 MainWP_Themes_Handler::trust_post();
988 die( wp_json_encode( array( 'result' => true ) ) );
989 }
990 }
991