PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.1
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 / pages / page-mainwp-updates-handler.php

page-mainwp-updates-handler.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.1, at pages/page-mainwp-updates-handler.php

1,328 lines 60.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Updates Handler.
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 // Exit if accessed directly.
11 if ( ! defined( 'ABSPATH' ) ) {
12 exit;
13 }
14
15 /**
16 * Class MainWP_Updates_Handler
17 *
18 * @package MainWP\Dashboard
19 */
20 class MainWP_Updates_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
21
22 /**
23 * Get Class Name
24 *
25 * @return string __CLASS__
26 */
27 public static function get_class_name() {
28 return __CLASS__;
29 }
30
31 /**
32 * Method upgrade_site()
33 *
34 * Check Child Site ID & Update.
35 *
36 * @param int $id Child site ID.
37 *
38 * @return string
39 * @throws MainWP_Exception Error messages.
40 *
41 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
42 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
43 * @uses \MainWP\Dashboard\MainWP_DB::get_website_option()
44 * @uses \MainWP\Dashboard\MainWP_Exception
45 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
46 */
47 public static function upgrade_site( $id ) { // phpcs:ignore -- NOSONAR - complex.
48 if ( isset( $id ) && MainWP_Utility::ctype_digit( $id ) ) {
49
50 $website = MainWP_DB::instance()->get_website_by_id( $id );
51
52 if ( MainWP_System_Utility::is_suspended_site( $website ) ) {
53 throw new MainWP_Exception( 'ERROR', '<i class="pause circular yellow inverted icon"></i> ' . esc_html__( 'Suspended site.', 'mainwp' ), 'SUSPENDED_SITE' ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
54 }
55
56 $information = static::upgrade_website( $website );
57
58 if ( is_array( $information ) ) {
59 $thrw_error = '';
60 $thrw_extra = '';
61 $success = false;
62 if ( isset( $information['upgrade'] ) && 'SUCCESS' === $information['upgrade'] ) {
63 MainWP_DB::instance()->update_website_option( $website, 'wp_upgrades', wp_json_encode( array() ) );
64 do_action( 'mainwp_after_upgrade_wp_success', $website, $information );
65 // Get Icon for the largest change scope of ext regression testing.
66 $html_regression_icon = apply_filters( 'mainwp_html_regression_largest_change_scope', $website->id, true ) ?: ''; // phpcs:ignore -- NOSONAR
67 if ( $website->id === $html_regression_icon ) {
68 $html_regression_icon = '';
69 }
70 $raw_msg = esc_html__( 'Update successful', 'mainwp' );
71 $msg_success = '<span data-inverted="" data-position="left center" data-tooltip="' . esc_html__( 'Update successful', 'mainwp' ) . '"><i class="green check icon"></i></span>' . $html_regression_icon;
72 $success = true;
73 } elseif ( isset( $information['upgrade'] ) ) {
74 $errorMsg = '';
75 if ( 'LOCALIZATION' === $information['upgrade'] ) {
76 $raw_msg = esc_html__( 'No update found for the set locale.', 'mainwp' );
77 $errorMsg = '<i class="red times icon"></i> ' . esc_html__( 'No update found for the set locale.', 'mainwp' );
78 } elseif ( 'NORESPONSE' === $information['upgrade'] ) {
79 $raw_msg = esc_html__( 'No response from the child site server.', 'mainwp' );
80 $errorMsg = '<i class="red times icon"></i> ' . esc_html__( 'No response from the child site server.', 'mainwp' );
81 }
82 $thrw_error = 'WPERROR';
83 $thrw_extra = $errorMsg;
84 } elseif ( isset( $information['error'] ) ) {
85 $thrw_error = 'WPERROR';
86 $thrw_extra = esc_html( $information['error'] );
87 $raw_msg = esc_html( $information['error'] );
88 } else {
89 $thrw_error = 'ERROR';
90 $thrw_extra = '<i class="red times icon"></i> ' . esc_html__( 'Invalid response from child site.', 'mainwp' );
91 $raw_msg = esc_html__( 'Invalid response from child site.', 'mainwp' );
92 }
93
94 do_action( 'mainwp_after_upgrade_wp_core', $website, $information, $success, $raw_msg );
95
96 if ( ! empty( $thrw_error ) ) {
97 throw new MainWP_Exception( $thrw_error, $thrw_extra ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- NOSONAR -escaped.
98 } elseif ( ! empty( $msg_success ) ) {
99 return $msg_success;
100 }
101 }
102 }
103
104 throw new MainWP_Exception( 'ERROR', '<i class="red times icon"></i> ' . esc_html__( 'Site ID not found. Please reload the page and try again.', 'mainwp' ) );
105 }
106
107 /**
108 * Method upgrade_website()
109 *
110 * Update WP for site.
111 *
112 * @param object $website Child site object.
113 *
114 * @return mixed|false update result or false.
115 * @throws MainWP_Exception Error message.
116 *
117 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
118 */
119 public static function upgrade_website( $website ) { // phpcs:ignore -- NOSONAR - complex.
120 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
121
122 $wpcore_update_disabled_by = MainWP_System_Utility::disabled_wpcore_update_by( $website );
123 if ( ! empty( $wpcore_update_disabled_by ) ) {
124 throw new MainWP_Exception( 'ERROR', $wpcore_update_disabled_by ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
125 }
126 /**
127 * Action: mainwp_before_wp_update
128 *
129 * Fires before WP update.
130 *
131 * @since 4.1
132 */
133 do_action( 'mainwp_before_wp_update', $website );
134
135 $information = MainWP_Connect::fetch_url_authed( $website, 'upgrade' );
136
137 static::activity_log_upgrade( $website, $information );
138
139 /**
140 * Action: mainwp_after_wp_update
141 *
142 * Fires after WP update.
143 *
144 * @since 4.1
145 */
146 do_action( 'mainwp_after_wp_update', $information, $website );
147
148 return $information;
149 }
150 return false;
151 }
152
153 /**
154 * Log upgrade activities.
155 *
156 * @param object $website Child site object.
157 * @param array $information Upgrade information.
158 */
159 public static function activity_log_upgrade( $website, $information ) {
160 // Implementation for logging upgrade activities.
161 $error = '';
162 $success = false;
163
164 if ( is_array( $information ) ) {
165 if ( isset( $information['upgrade'] ) && ( 'SUCCESS' === $information['upgrade'] ) ) {
166 $success = true;
167 } elseif ( isset( $information['upgrade'] ) ) {
168 if ( 'LOCALIZATION' === $information['upgrade'] ) {
169 $error = esc_html__( 'No update found for the set locale.', 'mainwp' );
170 } elseif ( 'NORESPONSE' === $information['upgrade'] ) {
171 $error = esc_html__( 'No response from the child site server.', 'mainwp' );
172 }
173 } elseif ( isset( $information['error'] ) ) {
174 $error = esc_html( $information['error'] );
175 } else {
176 $error = esc_html__( 'Invalid response from child site.', 'mainwp' );
177 }
178 }
179
180 $output_array = array(
181 'old_version' => is_array( $information ) && isset( $information['old_version'] ) ? $information['old_version'] : '',
182 'version' => is_array( $information ) && isset( $information['version'] ) ? $information['version'] : '',
183 'success' => $success ? 1 : 0,
184 'error' => $error,
185 );
186 $actions_handler = mainwp_get_actions_handler_instance();
187 if ( is_object( $actions_handler ) ) {
188 $actions_handler->do_action_mainwp_install_actions( $website, 'updated', $output_array, 'core' );
189 }
190 }
191
192 /**
193 * Add a plugin or theme to the ignor list.
194 *
195 * @param mixed $type plugin|theme.
196 * @param mixed $slug Plugin or Theme Slug.
197 * @param mixed $name Plugin or Theme Name.
198 * @param mixed $id Child Site ID.
199 * @param string $ver version ignore.
200 *
201 * @return string success.
202 *
203 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
204 * @uses \MainWP\Dashboard\MainWP_DB::update_website_values()
205 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
206 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
207 */
208 public static function ignore_plugin_theme( $type, $slug, $name, $id, $ver = '' ) { // phpcs:ignore -- NOSONAR - complex.
209 if ( isset( $id ) && MainWP_Utility::ctype_digit( $id ) ) {
210 $website = MainWP_DB::instance()->get_website_by_id( $id );
211 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
212 $slug = urldecode( $slug );
213 if ( 'plugin' === $type ) {
214 $decodedIgnoredPlugins = json_decode( $website->ignored_plugins, true );
215 /**
216 * Action: mainwp_before_plugin_ignore
217 *
218 * Fires before plugin ignore.
219 *
220 * @since 4.1
221 */
222 do_action( 'mainwp_before_plugin_ignore', $decodedIgnoredPlugins, $website );
223
224 $ignored_info = is_array( $decodedIgnoredPlugins ) && isset( $decodedIgnoredPlugins[ $slug ] ) ? $decodedIgnoredPlugins[ $slug ] : array();
225 if ( ! is_array( $ignored_info ) ) {
226 $ignored_info = array();
227 }
228
229 $ignored_info['Name'] = urldecode( $name );
230
231 $ignored_vers = is_array( $ignored_info ) && isset( $ignored_info['ignored_versions'] ) ? $ignored_info['ignored_versions'] : array();
232 if ( ! is_array( $ignored_vers ) ) {
233 $ignored_vers = array();
234 }
235 if ( empty( $ver ) || 'all_versions' === $ver ) { // ignore all.
236 $ignored_vers = array( 'all_versions' );
237 } else {
238 $ver = urldecode( $ver );
239 if ( ! in_array( $ver, $ignored_vers ) ) {
240 $ignored_vers[] = $ver;
241 }
242 }
243 $ignored_info['ignored_versions'] = $ignored_vers;
244
245 $decodedIgnoredPlugins[ $slug ] = $ignored_info;
246
247 MainWP_DB::instance()->update_website_values( $website->id, array( 'ignored_plugins' => wp_json_encode( $decodedIgnoredPlugins ) ) );
248
249 /**
250 * Action: mainwp_after_plugin_ignore
251 *
252 * Fires after plugin ignore.
253 *
254 * @since 4.1
255 */
256 do_action( 'mainwp_after_plugin_ignore', $decodedIgnoredPlugins, $website );
257
258 } elseif ( 'theme' === $type ) {
259 $decodedIgnoredThemes = json_decode( $website->ignored_themes, true );
260
261 /**
262 * Action: mainwp_before_theme_ignore
263 *
264 * Fires before theme ignore.
265 *
266 * @since 4.1
267 */
268 do_action( 'mainwp_before_theme_ignore', $decodedIgnoredThemes, $website );
269
270 $ignored_info = is_array( $decodedIgnoredThemes ) && isset( $decodedIgnoredThemes[ $slug ] ) ? $decodedIgnoredThemes[ $slug ] : array();
271 if ( ! is_array( $ignored_info ) ) {
272 $ignored_info = array();
273 }
274 $ignored_info['Name'] = urldecode( $name );
275
276 $ignored_vers = is_array( $ignored_info ) && isset( $ignored_info['ignored_versions'] ) ? $ignored_info['ignored_versions'] : array();
277 if ( ! is_array( $ignored_vers ) ) {
278 $ignored_vers = array();
279 }
280 if ( empty( $ver ) || 'all_versions' === $ver ) { // ignore all.
281 $ignored_vers = array( 'all_versions' );
282 } else {
283 $ver = urldecode( $ver );
284 if ( ! in_array( $ver, $ignored_vers ) ) {
285 $ignored_vers[] = $ver;
286 }
287 }
288 $ignored_info['ignored_versions'] = $ignored_vers;
289
290 $decodedIgnoredThemes[ $slug ] = $ignored_info;
291 MainWP_DB::instance()->update_website_values( $website->id, array( 'ignored_themes' => wp_json_encode( $decodedIgnoredThemes ) ) );
292 /**
293 * Action: mainwp_after_theme_ignore
294 *
295 * Fires after theme ignore.
296 *
297 * @since 4.1
298 */
299 do_action( 'mainwp_after_theme_ignore', $website, $decodedIgnoredThemes );
300 }
301 }
302 }
303 return 'success';
304 }
305
306
307 /**
308 * Remove a plugin or theme from the ignore list.
309 *
310 * @param mixed $type plugin|theme.
311 * @param mixed $slug Plugin or Theme slug.
312 * @param mixed $id Plugin or Theme name.
313 * @param string $ver Plugin or Theme version.
314 *
315 * @return string success.
316 *
317 * @uses \MainWP\Dashboard\MainWP_DB::query()
318 * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
319 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
320 * @uses \MainWP\Dashboard\MainWP_DB::update_website_values()
321 * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
322 * @uses \MainWP\Dashboard\MainWP_DB::free_result()
323 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
324 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
325 */
326 public static function unignore_plugin_theme( $type, $slug, $id, $ver ) { // phpcs:ignore -- NOSONAR - complex.
327 if ( ! empty( $id ) ) {
328
329 /**
330 * Action: mainwp_before_plugin_theme_unignore
331 *
332 * Fires after plugin/theme unignore.
333 *
334 * @since 4.1
335 */
336 do_action( 'mainwp_before_plugin_theme_unignore', $type, $slug, $id );
337
338 if ( '_ALL_' === $id ) {
339 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
340 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
341 if ( 'plugin' === $type ) {
342
343 /**
344 * Action: mainwp_before_plugin_unignore
345 *
346 * Fires before plugin unignore.
347 *
348 * @since 4.1
349 */
350 do_action( 'mainwp_before_plugin_unignore', array(), $website );
351
352 MainWP_DB::instance()->update_website_values( $website->id, array( 'ignored_plugins' => wp_json_encode( array() ) ) );
353
354 /**
355 * Action: mainwp_after_plugin_unignore
356 *
357 * Fires after plugin unignore.
358 *
359 * @since 4.1
360 */
361 do_action( 'mainwp_after_plugin_unignore', array(), $website );
362
363 } elseif ( 'theme' === $type ) {
364 /**
365 * Action: mainwp_before_theme_unignore
366 *
367 * Fires before theme unignore.
368 *
369 * @since 4.1
370 */
371 do_action( 'mainwp_before_theme_unignore', array(), $website );
372 MainWP_DB::instance()->update_website_values( $website->id, array( 'ignored_themes' => wp_json_encode( array() ) ) );
373
374 /**
375 * Action: mainwp_after_theme_unignore
376 *
377 * Fires after theme unignore.
378 *
379 * @since 4.1
380 */
381 do_action( 'mainwp_after_theme_unignore', array(), $website );
382 }
383 }
384 MainWP_DB::free_result( $websites );
385 } elseif ( MainWP_Utility::ctype_digit( $id ) ) {
386 $website = MainWP_DB::instance()->get_website_by_id( $id );
387 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
388 $slug = urldecode( $slug );
389 if ( 'plugin' === $type ) {
390 $decodedIgnoredPlugins = json_decode( $website->ignored_plugins, true );
391 if ( isset( $decodedIgnoredPlugins[ $slug ] ) ) {
392 /**
393 * Action: mainwp_before_plugin_unignore
394 *
395 * Fires before plugin unignore.
396 *
397 * @since 4.1
398 */
399 do_action( 'mainwp_before_plugin_unignore', $decodedIgnoredPlugins, $website );
400
401 if ( is_string( $decodedIgnoredPlugins[ $slug ] ) ) { // old ignored info.
402 unset( $decodedIgnoredPlugins[ $slug ] );
403 } elseif ( is_array( $decodedIgnoredPlugins[ $slug ] ) ) {
404 $ignored_info = $decodedIgnoredPlugins[ $slug ];
405 $ignored_vers = is_array( $ignored_info ) && isset( $ignored_info['ignored_versions'] ) ? $ignored_info['ignored_versions'] : array();
406
407 if ( ! is_array( $ignored_vers ) ) {
408 $ignored_vers = array();
409 }
410
411 if ( empty( $ver ) || 'all_versions' === $ver ) { // ignore all.
412 unset( $decodedIgnoredPlugins[ $slug ] );
413 } else {
414 $ver = urldecode( $ver );
415 if ( in_array( $ver, $ignored_vers ) ) {
416 $ignored_vers = array_diff( $ignored_vers, array( $ver ) );
417 }
418 $ignored_info['ignored_versions'] = $ignored_vers;
419 if ( empty( $ignored_vers ) ) {
420 unset( $decodedIgnoredPlugins[ $slug ] );
421 } else {
422 $decodedIgnoredPlugins[ $slug ] = $ignored_info;
423 }
424 }
425 }
426 MainWP_DB::instance()->update_website_values( $website->id, array( 'ignored_plugins' => wp_json_encode( $decodedIgnoredPlugins ) ) );
427 /**
428 * Action: mainwp_after_plugin_unignore
429 *
430 * Fires after plugin unignore.
431 *
432 * @since 4.1
433 */
434 do_action( 'mainwp_after_plugin_unignore', $decodedIgnoredPlugins, $website );
435 }
436 } elseif ( 'theme' === $type ) {
437 $decodedIgnoredThemes = json_decode( $website->ignored_themes, true );
438 if ( isset( $decodedIgnoredThemes[ $slug ] ) ) {
439
440 /**
441 * Action: mainwp_before_theme_unignore
442 *
443 * Fires before theme unignore.
444 *
445 * @since 4.1
446 */
447 do_action( 'mainwp_before_theme_unignore', $decodedIgnoredThemes, $website );
448
449 if ( is_string( $decodedIgnoredThemes[ $slug ] ) ) { // old ignored info.
450 unset( $decodedIgnoredThemes[ $slug ] );
451 } elseif ( is_array( $decodedIgnoredThemes[ $slug ] ) ) {
452 $ignored_info = $decodedIgnoredThemes[ $slug ];
453 $ignored_vers = is_array( $ignored_info ) && isset( $ignored_info['ignored_versions'] ) ? $ignored_info['ignored_versions'] : array();
454
455 if ( ! is_array( $ignored_vers ) ) {
456 $ignored_vers = array();
457 }
458
459 if ( empty( $ver ) || 'all_versions' === $ver ) { // ignore all.
460 unset( $decodedIgnoredThemes[ $slug ] );
461 } else {
462 $ver = urldecode( $ver );
463 if ( in_array( $ver, $ignored_vers ) ) {
464 $ignored_vers = array_diff( $ignored_vers, array( $ver ) );
465 }
466 $ignored_info['ignored_versions'] = $ignored_vers;
467 if ( empty( $ignored_vers ) ) {
468 unset( $decodedIgnoredThemes[ $slug ] );
469 } else {
470 $decodedIgnoredThemes[ $slug ] = $ignored_info;
471 }
472 }
473 }
474
475 MainWP_DB::instance()->update_website_values( $website->id, array( 'ignored_themes' => wp_json_encode( $decodedIgnoredThemes ) ) );
476
477 /**
478 * Action: mainwp_after_theme_unignore
479 *
480 * Fires after theme unignore.
481 *
482 * @since 4.1
483 */
484 do_action( 'mainwp_after_theme_unignore', $decodedIgnoredThemes, $website );
485 }
486 }
487 }
488 }
489 }
490
491 return 'success';
492 }
493
494
495 /**
496 * Remove WP from the ignore list.
497 *
498 * @param mixed $id Plugin or Theme name.
499 * @param string $ver Plugin or Theme version.
500 *
501 * @return string success.
502 */
503 public static function unignore_core_updates( $id, $ver ) { // phpcs:ignore -- NOSONAR - complex.
504 if ( ! empty( $id ) ) {
505
506 if ( '_ALL_' === $id ) {
507 $params = array(
508 'view' => 'updates_view',
509 'others_fields' => array( 'premium_upgrades' ),
510 );
511 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user_by_params( $params ) );
512 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
513
514 /**
515 * Action: mainwp_before_core_unignore
516 *
517 * Fires before plugin unignore.
518 *
519 * @since 5.2
520 */
521 do_action( 'mainwp_before_core_unignore', '_ALL_', $website );
522
523 MainWP_DB::instance()->update_website_option( $website->id, 'ignored_wp_upgrades', wp_json_encode( array() ) );
524 /**
525 * Action: mainwp_after_core_unignore
526 *
527 * Fires after plugin unignore.
528 *
529 * @since 5.2
530 */
531 do_action( 'mainwp_before_core_unignore', '_ALL_', $website );
532
533 }
534 MainWP_DB::free_result( $websites );
535 return 'success';
536 } elseif ( MainWP_Utility::ctype_digit( $id ) ) {
537 $website = MainWP_DB::instance()->get_website_by_id( $id, false, array( 'ignored_wp_upgrades' ) );
538 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
539 $ignored_info = ! empty( $website->ignored_wp_upgrades ) ? json_decode( $website->ignored_wp_upgrades, true ) : array();
540
541 if ( ! is_array( $ignored_info ) ) {
542 $ignored_info = array();
543 }
544 /**
545 * Action: mainwp_before_core_unignore
546 *
547 * Fires before plugin unignore.
548 *
549 * @since 5.2
550 */
551 do_action( 'mainwp_before_core_unignore', $ignored_info, $website );
552
553 $ignored_vers = isset( $ignored_info['ignored_versions'] ) ? $ignored_info['ignored_versions'] : array();
554
555 if ( ! is_array( $ignored_vers ) ) {
556 $ignored_vers = array();
557 }
558
559 if ( empty( $ver ) || 'all_versions' === $ver ) { // unignore all.
560 $ignored_vers = array();
561 } elseif ( in_array( $ver, $ignored_vers ) ) {
562 $ignored_vers = array_diff( $ignored_vers, array( $ver ) );
563 }
564 $ignored_info['ignored_versions'] = $ignored_vers;
565 MainWP_DB::instance()->update_website_option( $website->id, 'ignored_wp_upgrades', wp_json_encode( $ignored_info ) );
566 /**
567 * Action: mainwp_after_core_unignore
568 *
569 * Fires after plugin unignore.
570 *
571 * @since 5.2
572 */
573 do_action( 'mainwp_after_core_unignore', $ignored_info, $website );
574 return 'success';
575 }
576 }
577 }
578 return 'failed';
579 }
580
581 /**
582 * Unignore Plugins or Themes.
583 *
584 * @param string $slug _ALL_|empty.
585 * @param string $ver version info.
586 *
587 * @return string success.
588 *
589 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
590 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension()
591 */
592 public static function unignore_global_cores( $slug, $ver ) { // phpcs:ignore -- NOSONAR - complex.
593 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
594 if ( '_ALL_' === $slug ) {
595 $decodedIgnoredCores = array();
596 } else {
597 $decodedIgnoredCores = json_decode( $userExtension->ignored_plugins, true );
598 if ( ! is_array( $decodedIgnoredCores ) ) {
599 $decodedIgnoredCores = array();
600 }
601 if ( 'all_versions' === $ver ) {
602 $decodedIgnoredCores = array();
603 } else {
604 $ignored_vers = is_array( $decodedIgnoredCores ) && isset( $decodedIgnoredCores['ignored_versions'] ) ? $decodedIgnoredCores['ignored_versions'] : array();
605 if ( is_array( $ignored_vers ) && in_array( $ver, $ignored_vers ) ) {
606 $ignored_vers = array_diff( $ignored_vers, array( $ver ) );
607 if ( empty( $ignored_vers ) ) {
608 $decodedIgnoredCores = array();
609 } else {
610 $decodedIgnoredCores['ignored_versions'] = $ignored_vers;
611 }
612 }
613 }
614 }
615 MainWP_DB_Common::instance()->update_user_extension(
616 array(
617 'userid' => null,
618 'ignored_wp_upgrades' => wp_json_encode( $decodedIgnoredCores ),
619 )
620 );
621
622 return 'success';
623 }
624
625 /**
626 * Method ignore_plugins_themes()
627 *
628 * Ignore Plugins or Themes.
629 *
630 * @param string $type Plugin or theme.
631 * @param string $slug Plugin or tempheme slug.
632 * @param string $name Plugin or theme name.
633 * @param string $ver Plugin or theme version.
634 *
635 * @return string 'success'.
636 *
637 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
638 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension()
639 */
640 public static function ignore_plugins_themes( $type, $slug, $name, $ver = '' ) { //phpcs:ignore -- NOSONAR complex function.
641 $slug = urldecode( $slug );
642 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
643 if ( 'plugin' === $type ) {
644 $decodedIgnoredPlugins = json_decode( $userExtension->ignored_plugins, true );
645 if ( ! is_array( $decodedIgnoredPlugins ) ) {
646 $decodedIgnoredPlugins = array();
647 }
648
649 $ignored_info = is_array( $decodedIgnoredPlugins ) && isset( $decodedIgnoredPlugins[ $slug ] ) ? $decodedIgnoredPlugins[ $slug ] : array();
650 if ( ! is_array( $ignored_info ) ) {
651 $ignored_info = array();
652 }
653
654 $ignored_info['Name'] = urldecode( $name );
655
656 $ignored_vers = is_array( $ignored_info ) && isset( $ignored_info['ignored_versions'] ) ? $ignored_info['ignored_versions'] : array();
657 if ( ! is_array( $ignored_vers ) ) {
658 $ignored_vers = array();
659 }
660
661 if ( empty( $ver ) || 'all_versions' === $ver ) { // ignore all.
662 $ignored_vers = array( 'all_versions' );
663 } else {
664 $ver = urldecode( $ver );
665 if ( ! in_array( $ver, $ignored_vers ) ) {
666 $ignored_vers[] = $ver;
667 }
668 }
669
670 $ignored_info['ignored_versions'] = $ignored_vers;
671 $decodedIgnoredPlugins[ $slug ] = $ignored_info;
672
673 MainWP_DB_Common::instance()->update_user_extension(
674 array(
675 'userid' => null,
676 'ignored_plugins' => wp_json_encode( $decodedIgnoredPlugins ),
677 )
678 );
679 } elseif ( 'theme' === $type ) {
680 $decodedIgnoredThemes = json_decode( $userExtension->ignored_themes, true );
681 if ( ! is_array( $decodedIgnoredThemes ) ) {
682 $decodedIgnoredThemes = array();
683 }
684
685 $ignored_info = is_array( $decodedIgnoredThemes ) && isset( $decodedIgnoredThemes[ $slug ] ) ? $decodedIgnoredThemes[ $slug ] : array();
686 if ( ! is_array( $ignored_info ) ) {
687 $ignored_info = array();
688 }
689
690 $ignored_info['Name'] = urldecode( $name );
691
692 $ignored_vers = is_array( $ignored_info ) && isset( $ignored_info['ignored_versions'] ) ? $ignored_info['ignored_versions'] : array();
693 if ( ! is_array( $ignored_vers ) ) {
694 $ignored_vers = array();
695 }
696 if ( empty( $ver ) || 'all_versions' === $ver ) { // ignore all.
697 $ignored_vers = array( 'all_versions' );
698 } else {
699 $ver = urldecode( $ver );
700 if ( ! in_array( $ver, $ignored_vers ) ) {
701 $ignored_vers[] = $ver;
702 }
703 }
704 $ignored_info['ignored_versions'] = $ignored_vers;
705 $decodedIgnoredThemes[ $slug ] = $ignored_info;
706
707 MainWP_DB_Common::instance()->update_user_extension(
708 array(
709 'userid' => null,
710 'ignored_themes' => wp_json_encode( $decodedIgnoredThemes ),
711 )
712 );
713 }
714
715 return 'success';
716 }
717
718 /**
719 * Unignore Plugins or Themes.
720 *
721 * @param mixed $type plugin|themes.
722 * @param mixed $slug Plugin or Themes slug.
723 * @param string $ver Plugin or Themes version.
724 *
725 * @return string success.
726 *
727 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
728 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension()
729 */
730 public static function unignore_global_plugins_themes( $type, $slug, $ver ) { // phpcs:ignore -- NOSONAR - complex.
731 $slug = urldecode( $slug );
732 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
733 if ( 'plugin' === $type ) {
734 if ( '_ALL_' === $slug ) {
735 $decodedIgnoredPlugins = array();
736 } else {
737 $decodedIgnoredPlugins = json_decode( $userExtension->ignored_plugins, true );
738 if ( ! is_array( $decodedIgnoredPlugins ) ) {
739 $decodedIgnoredPlugins = array();
740 }
741 if ( isset( $decodedIgnoredPlugins[ $slug ] ) ) {
742 if ( 'all_versions' === $ver ) {
743 unset( $decodedIgnoredPlugins[ $slug ] );
744 } else {
745 $ignored_vers = is_array( $decodedIgnoredPlugins[ $slug ] ) && isset( $decodedIgnoredPlugins[ $slug ]['ignored_versions'] ) ? $decodedIgnoredPlugins[ $slug ]['ignored_versions'] : array();
746 if ( is_array( $ignored_vers ) && in_array( $ver, $ignored_vers ) ) {
747 $ignored_vers = array_diff( $ignored_vers, array( $ver ) );
748 if ( empty( $ignored_vers ) ) {
749 unset( $decodedIgnoredPlugins[ $slug ] );
750 } else {
751 $decodedIgnoredPlugins[ $slug ]['ignored_versions'] = $ignored_vers;
752 }
753 }
754 }
755 }
756 }
757 MainWP_DB_Common::instance()->update_user_extension(
758 array(
759 'userid' => null,
760 'ignored_plugins' => wp_json_encode( $decodedIgnoredPlugins ),
761 )
762 );
763 } elseif ( 'theme' === $type ) {
764 if ( '_ALL_' === $slug ) {
765 $decodedIgnoredThemes = array();
766 } else {
767 $decodedIgnoredThemes = json_decode( $userExtension->ignored_themes, true );
768 if ( ! is_array( $decodedIgnoredThemes ) ) {
769 $decodedIgnoredThemes = array();
770 }
771 if ( isset( $decodedIgnoredThemes[ $slug ] ) ) {
772 if ( 'all_versions' === $ver ) {
773 unset( $decodedIgnoredThemes[ $slug ] );
774 } else {
775 $ignored_vers = is_array( $decodedIgnoredThemes[ $slug ] ) && isset( $decodedIgnoredThemes[ $slug ]['ignored_versions'] ) ? $decodedIgnoredThemes[ $slug ]['ignored_versions'] : array();
776 if ( is_array( $ignored_vers ) && in_array( $ver, $ignored_vers ) ) {
777 $ignored_vers = array_diff( $ignored_vers, array( $ver ) );
778 if ( empty( $ignored_vers ) ) {
779 unset( $decodedIgnoredThemes[ $slug ] );
780 } else {
781 $decodedIgnoredThemes[ $slug ]['ignored_versions'] = $ignored_vers;
782 }
783 }
784 }
785 }
786 }
787 MainWP_DB_Common::instance()->update_user_extension(
788 array(
789 'userid' => null,
790 'ignored_themes' => wp_json_encode( $decodedIgnoredThemes ),
791 )
792 );
793 }
794
795 return 'success';
796 }
797
798
799 /**
800 * Unignor abandoned plugins or themes.
801 *
802 * @param mixed $type plugin|themes.
803 * @param mixed $slug Plugin or Themes slug.
804 * @param mixed $id Child Site ID.
805 *
806 * @return string success.
807 *
808 * @uses \MainWP\Dashboard\MainWP_DB::query()
809 * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
810 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
811 * @uses \MainWP\Dashboard\MainWP_DB::get_website_option()
812 * @uses \MainWP\Dashboard\MainWP_DB::update_website_option()
813 * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
814 * @uses \MainWP\Dashboard\MainWP_DB::free_result()
815 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
816 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
817 */
818 public static function unignore_abandoned_plugin_theme( $type, $slug, $id ) { // phpcs:ignore -- NOSONAR - complex.
819 if ( isset( $id ) ) {
820 if ( '_ALL_' === $id ) {
821 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
822 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
823 if ( 'plugin' === $type ) {
824 MainWP_DB::instance()->update_website_option( $website, 'plugins_outdate_dismissed', wp_json_encode( array() ) );
825 } elseif ( 'theme' === $type ) {
826 MainWP_DB::instance()->update_website_option( $website, 'themes_outdate_dismissed', wp_json_encode( array() ) );
827 }
828 }
829 MainWP_DB::free_result( $websites );
830 } elseif ( MainWP_Utility::ctype_digit( $id ) ) {
831 $website = MainWP_DB::instance()->get_website_by_id( $id );
832 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
833 $slug = urldecode( $slug );
834 if ( 'plugin' === $type ) {
835 $decodedIgnoredPlugins = MainWP_DB::instance()->get_website_option( $website, 'plugins_outdate_dismissed' );
836 $decodedIgnoredPlugins = ! empty( $decodedIgnoredPlugins ) ? json_decode( $decodedIgnoredPlugins, true ) : array();
837
838 if ( isset( $decodedIgnoredPlugins[ $slug ] ) ) {
839 unset( $decodedIgnoredPlugins[ $slug ] );
840 MainWP_DB::instance()->update_website_option( $website, 'plugins_outdate_dismissed', wp_json_encode( $decodedIgnoredPlugins ) );
841 }
842 } elseif ( 'theme' === $type ) {
843 $decodedIgnoredThemes = MainWP_DB::instance()->get_website_option( $website, 'themes_outdate_dismissed' );
844 $decodedIgnoredThemes = ! empty( $decodedIgnoredThemes ) ? json_decode( $decodedIgnoredThemes, true ) : array();
845
846 if ( isset( $decodedIgnoredThemes[ $slug ] ) ) {
847 unset( $decodedIgnoredThemes[ $slug ] );
848 MainWP_DB::instance()->update_website_option( $website, 'themes_outdate_dismissed', wp_json_encode( $decodedIgnoredThemes ) );
849 }
850 }
851 }
852 }
853 }
854
855 return 'success';
856 }
857
858
859 /**
860 * Unignore abandoned plugins or themes.
861 *
862 * @param mixed $type plugin|theme.
863 * @param mixed $slug Plugin or Themes slug.
864 *
865 * @return string success.
866 *
867 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
868 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension()
869 */
870 public static function unignore_abandoned_plugins_themes( $type, $slug ) { // phpcs:ignore -- NOSONAR - complex.
871 $slug = urldecode( $slug );
872 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
873 if ( 'plugin' === $type ) {
874 if ( '_ALL_' === $slug ) {
875 $decodedIgnoredPlugins = array();
876 } else {
877 $decodedIgnoredPlugins = json_decode( $userExtension->dismissed_plugins, true );
878 if ( ! is_array( $decodedIgnoredPlugins ) ) {
879 $decodedIgnoredPlugins = array();
880 }
881 if ( isset( $decodedIgnoredPlugins[ $slug ] ) ) {
882 unset( $decodedIgnoredPlugins[ $slug ] );
883 }
884 }
885 MainWP_DB_Common::instance()->update_user_extension(
886 array(
887 'userid' => null,
888 'dismissed_plugins' => wp_json_encode( $decodedIgnoredPlugins ),
889 )
890 );
891 } elseif ( 'theme' === $type ) {
892 if ( '_ALL_' === $slug ) {
893 $decodedIgnoredThemes = array();
894 } else {
895 $decodedIgnoredThemes = json_decode( $userExtension->dismissed_themes, true );
896 if ( ! is_array( $decodedIgnoredThemes ) ) {
897 $decodedIgnoredThemes = array();
898 }
899 if ( isset( $decodedIgnoredThemes[ $slug ] ) ) {
900 unset( $decodedIgnoredThemes[ $slug ] );
901 }
902 }
903 MainWP_DB_Common::instance()->update_user_extension(
904 array(
905 'userid' => null,
906 'dismissed_themes' => wp_json_encode( $decodedIgnoredThemes ),
907 )
908 );
909 }
910
911 return 'success';
912 }
913
914 /**
915 * Dismis Plugin or Theme.
916 *
917 * @param mixed $type plugin|theme.
918 * @param mixed $slug Plugin or Theme slug.
919 * @param mixed $name Plugin or Theme name.
920 * @param mixed $id Child Site ID.
921 *
922 * @return string success.
923 *
924 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
925 * @uses \MainWP\Dashboard\MainWP_DB::get_website_option()
926 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
927 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
928 */
929 public static function dismiss_plugin_theme( $type, $slug, $name, $id ) { // phpcs:ignore -- NOSONAR - complex.
930 if ( isset( $id ) && MainWP_Utility::ctype_digit( $id ) ) {
931 $website = MainWP_DB::instance()->get_website_by_id( $id );
932 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
933 $slug = urldecode( $slug );
934 if ( 'plugin' === $type ) {
935 $decodedDismissedPlugins = MainWP_DB::instance()->get_website_option( $website, 'plugins_outdate_dismissed' );
936 $decodedDismissedPlugins = ! empty( $decodedDismissedPlugins ) ? json_decode( $decodedDismissedPlugins, true ) : array();
937
938 if ( ! isset( $decodedDismissedPlugins[ $slug ] ) ) {
939 $decodedDismissedPlugins[ $slug ] = urldecode( $name );
940 MainWP_DB::instance()->update_website_option( $website, 'plugins_outdate_dismissed', wp_json_encode( $decodedDismissedPlugins ) );
941 }
942 } elseif ( 'theme' === $type ) {
943 $decodedDismissedThemes = MainWP_DB::instance()->get_website_option( $website, 'themes_outdate_dismissed' );
944 $decodedDismissedThemes = ! empty( $decodedDismissedThemes ) ? json_decode( $decodedDismissedThemes, true ) : array();
945
946 if ( ! isset( $decodedDismissedThemes[ $slug ] ) ) {
947 $decodedDismissedThemes[ $slug ] = urldecode( $name );
948 MainWP_DB::instance()->update_website_option( $website, 'themes_outdate_dismissed', wp_json_encode( $decodedDismissedThemes ) );
949 }
950 }
951 }
952 }
953
954 return 'success';
955 }
956
957 /**
958 * Dismiss plugins or themes.
959 *
960 * @param mixed $type plugin|theme.
961 * @param mixed $slug Plugin or Theme slug.
962 * @param mixed $name Plugin or Theme name.
963 *
964 * @return string success.
965 *
966 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
967 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension()
968 */
969 public static function dismiss_plugins_themes( $type, $slug, $name ) {
970 $slug = urldecode( $slug );
971 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
972 if ( 'plugin' === $type ) {
973 $decodedDismissedPlugins = json_decode( $userExtension->dismissed_plugins, true );
974 if ( ! is_array( $decodedDismissedPlugins ) ) {
975 $decodedDismissedPlugins = array();
976 }
977 $decodedDismissedPlugins[ $slug ] = urldecode( $name );
978 MainWP_DB_Common::instance()->update_user_extension(
979 array(
980 'userid' => null,
981 'dismissed_plugins' => wp_json_encode( $decodedDismissedPlugins ),
982 )
983 );
984 } elseif ( 'theme' === $type ) {
985 $decodedDismissedThemes = json_decode( $userExtension->dismissed_themes, true );
986 if ( ! is_array( $decodedDismissedThemes ) ) {
987 $decodedDismissedThemes = array();
988 }
989 $decodedDismissedThemes[ $slug ] = urldecode( $name );
990 MainWP_DB_Common::instance()->update_user_extension(
991 array(
992 'userid' => null,
993 'dismissed_themes' => wp_json_encode( $decodedDismissedThemes ),
994 )
995 );
996 }
997
998 return 'success';
999 }
1000
1001 /**
1002 * Method upgrade_plugin_theme_translation()
1003 *
1004 * Upgrade plugin or theme translations.
1005 *
1006 * @param int $id Child site ID.
1007 * @param string $type Plugin or theme.
1008 * @param array $list_items List of theme or plugin names seperated by comma.
1009 *
1010 * @return array
1011 * @throws MainWP_Exception Error messages.
1012 *
1013 * @uses \MainWP\Dashboard\MainWP_Connect::fetch_url_authed()
1014 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
1015 * @uses \MainWP\Dashboard\MainWP_Exception
1016 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
1017 */
1018 public static function upgrade_plugin_theme_translation( $id, $type, $list_items ) { // phpcs:ignore -- NOSONAR - complex method.
1019 if ( isset( $id ) && MainWP_Utility::ctype_digit( $id ) ) {
1020 $website = MainWP_DB::instance()->get_website_by_id( $id, false, array( 'premium_upgrades', 'rollback_updates_data' ) ); // to fix loading premium_upgrades.
1021 if ( MainWP_System_Utility::is_suspended_site( $website ) ) {
1022 throw new MainWP_Exception( 'ERROR', esc_html__( 'Suspended site.', 'mainwp' ), 'SUSPENDED_SITE' );
1023 }
1024
1025 $result = static::update_plugin_theme_translation( $website, $type, $list_items );
1026 if ( is_array( $result ) ) {
1027
1028 $return_results = array();
1029
1030 // logging feature.
1031 $_type = '';
1032 if ( 'plugin' === $type || 'theme' === $type ) {
1033 $_type = $type;
1034 } elseif ( 'translation' === $type ) {
1035 $_type = 'trans';
1036 }
1037
1038 $undefined = true;
1039
1040 if ( isset( $result['upgrades_error'] ) ) {
1041 foreach ( $result['upgrades_error'] as $k => $v ) {
1042 $return_results['result_error'][ rawurlencode( $k ) ] = esc_html( $v );
1043 }
1044 }
1045
1046 if ( ! empty( $_type ) && isset( $result['other_data'] ) ) { // ok.
1047 $output_array = $result['other_data']; // updated_data: plugins,themes,trans.
1048 mainwp_get_actions_handler_instance()->do_action_mainwp_install_actions( $website, 'updated', $output_array, $_type );
1049 if ( is_array( $output_array ) ) {
1050 $updated_data = isset( $output_array['updated_data'] ) ? $output_array['updated_data'] : array();
1051 if ( is_array( $updated_data ) ) {
1052
1053 $saved_roll_items = ! empty( $website->rollback_updates_data ) ? json_decode( $website->rollback_updates_data, true ) : array();
1054 if ( ! is_array( $saved_roll_items ) ) {
1055 $saved_roll_items = array();
1056 }
1057
1058 $update = false;
1059 foreach ( $updated_data as $item ) {
1060 $version = isset( $item['version'] ) ? $item['version'] : '';
1061 if ( ! empty( $item ) && isset( $item['slug'] ) && ! empty( $item['rollback'] ) ) {
1062 $msg = MainWP_Updates_Helper::get_roll_msg( $item );
1063 $return_results['result_error'][ rawurlencode( $item['slug'] ) ] = '[Roll]' . esc_html( $msg );
1064
1065 $name = isset( $item['name'] ) ? $item['name'] : '';
1066 $old_version = isset( $item['old_version'] ) ? $item['old_version'] : '';
1067
1068 if ( ! empty( $version ) ) {
1069 if ( ! isset( $saved_roll_items[ $_type ] ) ) {
1070 $saved_roll_items[ $_type ] = array();
1071 }
1072
1073 $saved_item = array(
1074 'name' => $name,
1075 'old_version' => $old_version,
1076 'version' => $version,
1077 'created' => time(),
1078 );
1079
1080 if ( ! empty( $item['error'] ) ) {
1081 $saved_item['error'] = $item['error'];
1082 }
1083
1084 $saved_roll_items[ $_type ][ $item['slug'] ][ $version ] = $saved_item;
1085 $update = true;
1086 }
1087 } elseif ( ! empty( $item ) && ! empty( $item['slug'] ) && ! empty( $item['success'] ) ) {
1088 if ( ! empty( $version ) && isset( $saved_roll_items[ $_type ][ $item['slug'] ][ $version ] ) ) {
1089 unset( $saved_roll_items[ $_type ][ $item['slug'] ][ $version ] );
1090 $update = true;
1091 }
1092 }
1093 }
1094 if ( $update ) {
1095 MainWP_DB::instance()->update_website_option( $website, 'rollback_updates_data', wp_json_encode( $saved_roll_items ) );
1096 }
1097 }
1098 }
1099 }
1100
1101 if ( isset( $result['upgrades'] ) ) {
1102 if ( isset( $result['upgrades'] ) ) {
1103 foreach ( $result['upgrades'] as $k => $v ) {
1104 if ( ! empty( $v ) ) { // updated success.
1105 $return_results['result'][ rawurlencode( $k ) ] = $v;
1106 }
1107 }
1108 }
1109
1110 if ( 'plugin' === $type ) {
1111 $website = MainWP_DB::instance()->get_website_by_id( $id, false ); // Call new data the before a updated.
1112 if ( ! empty( $website->plugin_upgrades ) ) {
1113 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
1114 if ( is_array( $plugin_upgrades ) ) {
1115 $updated = false;
1116 if ( isset( $return_results['result'] ) && is_array( $return_results['result'] ) && ! empty( $return_results['result'] ) ) {
1117 foreach ( $return_results['result'] as $k => $v ) {
1118 $k = rawurldecode( $k );
1119 if ( isset( $plugin_upgrades[ $k ] ) ) {
1120 unset( $plugin_upgrades[ $k ] ); // updated.
1121 $updated = true;
1122 }
1123 }
1124 }
1125 if ( $updated ) {
1126 MainWP_DB::instance()->update_website_values( $id, array( 'plugin_upgrades' => wp_json_encode( $plugin_upgrades ) ) );
1127 }
1128 }
1129 }
1130
1131 if ( isset( $return_results['result'] ) && is_array( $return_results['result'] ) && ! empty( $return_results['result'] ) ) {
1132 $decodedPremiumUpgrades = MainWP_DB::instance()->get_website_option( $website, 'premium_upgrades' );
1133 $decodedPremiumUpgrades = ! empty( $decodedPremiumUpgrades ) ? json_decode( $decodedPremiumUpgrades, true ) : array();
1134 if ( is_array( $decodedPremiumUpgrades ) && ! empty( $decodedPremiumUpgrades ) ) {
1135 $updated = false;
1136 foreach ( $return_results['result'] as $k => $v ) {
1137 $k = rawurldecode( $k );
1138 if ( isset( $decodedPremiumUpgrades[ $k ] ) ) {
1139 unset( $decodedPremiumUpgrades[ $k ] ); // updated.
1140 $updated = true;
1141 }
1142 }
1143 if ( $updated ) {
1144 MainWP_DB::instance()->update_website_option( $website, 'premium_upgrades', wp_json_encode( $decodedPremiumUpgrades ) );
1145 }
1146 }
1147 }
1148 }
1149 return $return_results;
1150 } elseif ( isset( $result['error'] ) ) {
1151 throw new MainWP_Exception( 'WPERROR', esc_html( $result['error'] ) );
1152 } elseif ( isset( $result['notices'] ) ) {
1153 $noti = $result['notices'];
1154 if ( ! empty( $noti ) && is_string( $noti ) ) {
1155 throw new MainWP_Exception( $noti, '', 'MAINWP_NOTICE' ); //phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
1156 }
1157 }
1158
1159 if ( $undefined ) {
1160 throw new MainWP_Exception( 'ERROR', esc_html__( 'Invalid response retured from the child site. Please try again.', 'mainwp' ) );
1161 }
1162 }
1163 }
1164 throw new MainWP_Exception( 'ERROR', esc_html__( 'Site ID not found. Please reload the page and try again.', 'mainwp' ) );
1165 }
1166
1167 /**
1168 * Method update_plugin_theme_translation()
1169 *
1170 * Upgrade plugin or theme translations.
1171 *
1172 * @param int $website Child site object.
1173 * @param string $type Plugin or theme.
1174 * @param array $list_items List of theme or plugin names seperated by comma.
1175 *
1176 * @return array|false update result or false.
1177 * @throws \MainWP_Exception Error message.
1178 *
1179 * @uses \MainWP\Dashboard\MainWP_System_Utility
1180 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
1181 */
1182 public static function update_plugin_theme_translation( $website, $type, $list_items ) {
1183 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
1184 /**
1185 * Action: mainwp_before_plugin_theme_translation_update
1186 *
1187 * Fires before plugin/theme/translation update actions.
1188 *
1189 * @since 4.1
1190 */
1191 do_action( 'mainwp_before_plugin_theme_translation_update', $type, $list_items, $website );
1192
1193 $information = MainWP_Connect::fetch_url_authed(
1194 $website,
1195 ( 'translation' === $type ? 'upgradetranslation' : 'upgradeplugintheme' ),
1196 array(
1197 'type' => $type,
1198 'list' => urldecode( $list_items ),
1199 ),
1200 true
1201 );
1202 /**
1203 * Action: mainwp_after_plugin_theme_translation_update
1204 *
1205 * Fires before plugin/theme/translation update actions.
1206 *
1207 * @since 4.1
1208 */
1209 do_action( 'mainwp_after_plugin_theme_translation_update', $information, $type, $list_items, $website );
1210
1211 return $information;
1212 }
1213 return false;
1214 }
1215
1216 /**
1217 * Get plugin or theme slugs.
1218 *
1219 * @param int $id Child Site ID.
1220 * @param string $type plugin|theme.
1221 *
1222 * @return array List of plugins or themes.
1223 *
1224 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
1225 * @uses \MainWP\Dashboard\MainWP_DB::query()
1226 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
1227 * @uses \MainWP\Dashboard\MainWP_DB::get_website_option()
1228 * @uses \MainWP\Dashboard\MainWP_DB::fetch_object()
1229 */
1230 public static function get_plugin_theme_slugs( $id, $type ) { // phpcs:ignore -- NOSONAR - complex method. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
1231
1232 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
1233 $sql = MainWP_DB::instance()->get_sql_website_by_id( $id );
1234 $websites = MainWP_DB::instance()->query( $sql );
1235 $website = MainWP_DB::fetch_object( $websites );
1236
1237 $slugs = array();
1238 if ( 'plugin' === $type ) {
1239 if ( $website->is_ignorePluginUpdates ) {
1240 return '';
1241 }
1242
1243 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
1244 $decodedPremiumUpgrades = MainWP_DB::instance()->get_website_option( $website, 'premium_upgrades' );
1245 $decodedPremiumUpgrades = ! empty( $decodedPremiumUpgrades ) ? json_decode( $decodedPremiumUpgrades, true ) : array();
1246
1247 if ( is_array( $decodedPremiumUpgrades ) ) {
1248 foreach ( $decodedPremiumUpgrades as $crrSlug => $premiumUpgrade ) {
1249 $premiumUpgrade['premium'] = true;
1250
1251 if ( 'plugin' === $premiumUpgrade['type'] ) {
1252 if ( ! is_array( $plugin_upgrades ) ) {
1253 $plugin_upgrades = array();
1254 }
1255 $premiumUpgrade = array_filter( $premiumUpgrade );
1256 $plugin_upgrades[ $crrSlug ] = array_merge( $plugin_upgrades[ $crrSlug ], $premiumUpgrade );
1257 }
1258 }
1259 }
1260
1261 $ignored_plugins = json_decode( $website->ignored_plugins, true );
1262 if ( is_array( $ignored_plugins ) ) {
1263 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
1264
1265 }
1266
1267 $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
1268 if ( is_array( $ignored_plugins ) ) {
1269 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
1270
1271 }
1272
1273 if ( is_array( $plugin_upgrades ) ) {
1274 foreach ( $plugin_upgrades as $slug => $plugin_upgrade ) {
1275 $slugs[] = rawurlencode( $slug );
1276 }
1277 }
1278 } elseif ( 'theme' === $type ) {
1279
1280 if ( $website->is_ignoreThemeUpdates ) {
1281 return '';
1282 }
1283
1284 $theme_upgrades = json_decode( $website->theme_upgrades, true );
1285 $decodedPremiumUpgrades = MainWP_DB::instance()->get_website_option( $website, 'premium_upgrades' );
1286 $decodedPremiumUpgrades = ! empty( $decodedPremiumUpgrades ) ? json_decode( $decodedPremiumUpgrades, true ) : array();
1287
1288 if ( is_array( $decodedPremiumUpgrades ) ) {
1289 foreach ( $decodedPremiumUpgrades as $crrSlug => $premiumUpgrade ) {
1290 $premiumUpgrade['premium'] = true;
1291
1292 if ( 'theme' === $premiumUpgrade['type'] ) {
1293 if ( ! is_array( $theme_upgrades ) ) {
1294 $theme_upgrades = array();
1295 }
1296 $theme_upgrades[ $crrSlug ] = $premiumUpgrade;
1297 }
1298 }
1299 }
1300
1301 $ignored_themes = json_decode( $website->ignored_themes, true );
1302 if ( is_array( $ignored_themes ) ) {
1303 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
1304 }
1305
1306 $ignored_themes = json_decode( $userExtension->ignored_themes, true );
1307 if ( is_array( $ignored_themes ) ) {
1308 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
1309 }
1310
1311 if ( is_array( $theme_upgrades ) ) {
1312 foreach ( $theme_upgrades as $slug => $theme_upgrade ) {
1313 $slugs[] = $slug;
1314 }
1315 }
1316 } elseif ( 'translation' === $type ) {
1317 $translation_upgrades = json_decode( $website->translation_upgrades, true );
1318 if ( is_array( $translation_upgrades ) ) {
1319 foreach ( $translation_upgrades as $translation_upgrade ) {
1320 $slugs[] = $translation_upgrade['slug'];
1321 }
1322 }
1323 }
1324
1325 return implode( ',', $slugs );
1326 }
1327 }
1328