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

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