PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.2 6.2.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 All 155 releases
mainwp / pages / page-mainwp-updates-handler.php

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

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