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

class-mainwp-wp-cli-handle.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.1.2, at class/class-mainwp-wp-cli-handle.php

1,975 lines 82.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP-CLI
4 *
5 * This file extends the WP-CLI and provides a set of SubCommands to Control your
6 * Child Sites that are added to the MainWP Dashboard.
7 *
8 * @package MainWP/Dashboard
9 */
10
11 namespace MainWP\Dashboard;
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 // Exit if access directly.
19 if ( ! defined( 'WP_CLI' ) ) {
20 return; // NOSONAR - skip CLI.
21 }
22
23 /**
24 * Class MainWP_WP_CLI_Handle
25 *
26 * Manage all child sites added to the MainWP Dashboard via WP CLI.
27 *
28 * @package MainWP\Dashboard
29 */
30 class MainWP_WP_CLI_Handle extends \WP_CLI_Command { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
31
32 // phpcs:disable Generic.Metrics.CyclomaticComplexity -- complexity.
33
34 /**
35 * Singleton.
36 *
37 * @var null $instance
38 */
39 private static $instance = null;
40
41 /**
42 * MainWP WP CLI Handle Instance.
43 *
44 * @return self $instance
45 */
46 public static function instance() {
47 if ( null === static::$instance ) {
48 static::$instance = new self();
49 }
50 return static::$instance;
51 }
52
53 /**
54 * Return available MainWP WP CLI Commands.
55 *
56 * @param string $comm MainWP WP CLI Command.
57 *
58 * @return array $cli_commands CLI Commands.
59 */
60 public static function get_assoc_handle_commands( $comm ) {
61 $cli_commands = array(
62 'sites' => array(
63 'all-sites',
64 'all-sites-count',
65 'connected-sites',
66 'connected-sites-count',
67 'disconnected-sites',
68 'disconnected-sites-count',
69 'sync-sites',
70 'check-sites',
71 'disconnect-sites',
72 ),
73 'site' => array(
74 'site',
75 'site-info',
76 'site-installed-plugins',
77 'site-installed-plugins-count',
78 'site-active-plugins',
79 'site-active-plugins-count',
80 'site-inactive-plugins',
81 'site-inactive-plugins-count',
82 'site-installed-themes',
83 'site-installed-themes-count',
84 'site-active-themes',
85 'site-inactive-themes',
86 'site-inactive-themes-count',
87 'site-available-updates',
88 'site-available-updates-count',
89 'site-abandoned-plugins',
90 'site-abandoned-plugins-count',
91 'site-abandoned-themes',
92 'site-abandoned-themes-count',
93 'site-http-status',
94 'site-health-score',
95 'site-security-issues',
96 'add-site',
97 'edit-site',
98 'sync-site',
99 'reconnect-site',
100 'disconnect-site',
101 'remove-site',
102 'site-update-wordpress',
103 'site-update-plugins',
104 'site-update-themes',
105 'site-update-translations',
106 'site-update-item',
107 'site-manage-plugin',
108 'site-manage-theme',
109 'check-site-http-status',
110 ),
111 'updates' => array(
112 'available-updates',
113 'ignored-plugins-updates',
114 'site-ignored-plugins-updates',
115 'ignored-themes-updates',
116 'site-ignored-themes-updates',
117 'ignore-updates',
118 'ignore-update',
119 'unignore-updates',
120 'unignore-update',
121 ),
122 );
123 return isset( $cli_commands[ $comm ] ) ? $cli_commands[ $comm ] : array();
124 }
125
126 /**
127 * Gets associated agruments for the command.
128 *
129 * @param string $cli_com MainWP WP CLI command.
130 * @param array $assoc_args Associated arguments for the command.
131 *
132 * @return string $callback Callback method.
133 */
134 public static function get_assoc_args_commands( $cli_com, $assoc_args ) {
135 $commands = static::get_assoc_handle_commands( $cli_com );
136 if ( empty( $commands ) ) {
137 return false;
138 }
139 foreach ( $commands as $comm ) {
140 if ( isset( $assoc_args[ $comm ] ) ) {
141 return str_replace( '-', '_', $comm );
142 }
143 }
144 return false;
145 }
146
147 /**
148 * Calls correct Callback.
149 *
150 * @param string $cli_com CLI Command.
151 * @param array $args Arguments.
152 * @param array $assoc_args Correct callback.
153 *
154 * @return bool True on success, false or error.
155 */
156 public static function handle_cli_callback( $cli_com, $args, $assoc_args ) { // phpcs:ignore -- NOSONAR - complex.
157 $callback = static::get_assoc_args_commands( $cli_com, $assoc_args );
158 if ( ! empty( $callback ) && method_exists( static::class, 'callback_' . $cli_com . '_' . $callback ) ) {
159 $website = false;
160
161 $requires_site_id = false;
162 $site_id = 0;
163
164 if ( 'site' === $cli_com && ! isset( $assoc_args['add-site'] ) ) {
165 $requires_site_id = true;
166 } elseif ( 'updates' === $cli_com && ( isset( $assoc_args['site-ignored-plugins-updates'] ) || isset( $assoc_args['site-ignored-themes-updates'] ) || isset( $assoc_args['ignore-update'] ) || isset( $assoc_args['unignore_update'] ) ) ) {
167 $requires_site_id = true;
168 } elseif ( 'updates' === $cli_com && ( isset( $assoc_args['ignored-plugins-updates'] ) || isset( $assoc_args['ignored-themes-updates'] ) ) ) {
169 $site_id = isset( $args[0] ) ? intval( $args[0] ) : false;
170 }
171
172 if ( $requires_site_id ) {
173 $site_id = static::get_cli_params( $args, $assoc_args, 'site_id' );
174 if ( empty( $site_id ) ) {
175 \WP_CLI::error( 'Empty site id.' );
176 return false;
177 }
178 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
179 if ( empty( $website ) ) {
180 \WP_CLI::error( 'Site not found.' );
181 return false;
182 }
183 } elseif ( $site_id ) {
184 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
185 if ( empty( $website ) ) {
186 \WP_CLI::error( 'Site not found.' );
187 return false;
188 }
189 }
190 call_user_func_array( array( static::class, 'callback_' . $cli_com . '_' . $callback ), array( $args, $assoc_args, $website ) );
191 return true;
192 }
193 return false;
194 }
195
196
197 /**
198 * Gets parameters.
199 *
200 * @param array $args Arguments.
201 * @param array $assoc_args Arguments.
202 * @param string $what Targetted action.
203 *
204 * @return array Required data.
205 */
206 public static function get_cli_params( $args, $assoc_args, $what ) { // phpcs:ignore -- NOSONAR - complex.
207 if ( is_string( $what ) ) {
208 if ( 'sites' === $what ) {
209 $sites = array();
210 if ( ! empty( $args ) ) {
211 $args_exploded = explode( ',', $args[0] );
212 foreach ( $args_exploded as $arg ) {
213 if ( ! is_numeric( trim( $arg ) ) ) {
214 \WP_CLI::error( 'Child site ids should be numeric.' );
215 }
216 $sites[] = trim( $arg );
217 }
218 }
219 return $sites;
220 } elseif ( 'site_id' === $what ) {
221 $site_id = $args[0];
222 if ( ! is_numeric( trim( $site_id ) ) ) {
223 \WP_CLI::error( 'Child site id should be numeric.' );
224 }
225 return $site_id;
226 } elseif ( 'add-site' === $what ) {
227 $allow_fields = array(
228 'site-url',
229 'name',
230 'admin',
231 'uniqueid',
232 'ssl_verify',
233 'force_use_ipv4',
234 'ssl_version',
235 'http_user',
236 'http_pass',
237 'groupids',
238 );
239
240 $required_fields = array(
241 'site-url',
242 'name',
243 'admin',
244 );
245
246 $data = static::map_assoc_args( $assoc_args, $allow_fields, $required_fields );
247 if ( isset( $data['site-url'] ) ) {
248 $data['url'] = $data['site-url'];
249 unset( $data['site-url'] );
250 }
251 return $data;
252 } elseif ( 'edit-site' === $what ) {
253 $allow_fields = array(
254 'http_user',
255 'http_pass',
256 'name',
257 'admin',
258 'sslversion',
259 'uniqueid',
260 );
261 $required_fields = array(); // required_fields.
262 return static::map_assoc_args( $assoc_args, $allow_fields, $required_fields );
263 }
264 } elseif ( is_array( $what ) && ! empty( $what ) ) {
265 $map_fields = $what;
266 return static::map_assoc_args( $assoc_args, $map_fields );
267 }
268
269 return false;
270 }
271
272 /**
273 * Maps arguments.
274 *
275 * @param array $assoc_args Arguments.
276 * @param array $fields Fields.
277 * @param array|string $required_fields Fields that are required, default 'all': all fields are required.
278 *
279 * @return array $data Required data.
280 */
281 public static function map_assoc_args( $assoc_args, $fields, $required_fields = 'all' ) {
282 $data = array();
283 foreach ( $fields as $field ) {
284 if ( isset( $assoc_args[ $field ] ) ) {
285 $data[ $field ] = $assoc_args[ $field ];
286 } elseif ( 'all' === $required_fields ) {
287 \WP_CLI::error( 'Missing field: ' . $field );
288 } elseif ( is_array( $required_fields ) && ! empty( $required_fields ) && in_array( $field, $required_fields ) ) {
289 \WP_CLI::error( 'Missing field: ' . $field );
290 }
291 }
292 return $data;
293 }
294
295
296 /**
297 * Lists all sites.
298 *
299 * Command Example: wp mainwp sites --all-sites.
300 */
301 public static function callback_sites_all_sites() {
302 // get data.
303 $data = MainWP_DB::instance()->get_websites_for_current_user();
304 if ( empty( $data ) ) {
305 \WP_CLI::line( esc_html__( 'No child sites added to your MainWP Dashboard.', 'mainwp' ) );
306 } else {
307 static::print_sites( $data, true );
308 }
309 }
310
311
312 /**
313 * Returns number of child sites.
314 *
315 * Command Example: wp mainwp sites --all-sites-count.
316 */
317 public static function callback_sites_all_sites_count() {
318 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
319 $count = MainWP_DB::num_rows( $websites );
320 MainWP_DB::free_result( $websites );
321 \WP_CLI::line( '' );
322 \WP_CLI::line( esc_html__( 'Number of child sites: ', 'mainwp' ) . $count );
323 \WP_CLI::line( '' );
324 }
325
326 /**
327 * Lists all connected child sites.
328 *
329 * Command Example: wp mainwp sites --connected-sites.
330 */
331 public static function callback_sites_connected_sites() {
332 $data = MainWP_DB::instance()->get_connected_websites();
333 if ( empty( $data ) ) {
334 \WP_CLI::line( esc_html__( 'No connected child sites fount.', 'mainwp' ) );
335 } else {
336 static::print_sites( $data );
337 }
338 }
339
340 /**
341 * Returns number of connected sites.
342 *
343 * Command Example: wp mainwp sites --connected-sites-count.
344 */
345 public static function callback_sites_connected_sites_count() {
346 $websites = MainWP_DB::instance()->get_connected_websites();
347 \WP_CLI::line( esc_html__( 'Number of connected child sites: ', 'mainwp' ) . count( $websites ) );
348 }
349
350 /**
351 * Lists all disconnected child sites.
352 *
353 * Command Example: wp mainwp sites --disconnected-sites.
354 */
355 public static function callback_sites_disconnected_sites() {
356 $data = MainWP_DB::instance()->get_disconnected_websites();
357 if ( empty( $data ) ) {
358 \WP_CLI::line( esc_html__( 'No disconnected child sites found.', 'mainwp' ) );
359 } else {
360 static::print_sites( $data );
361 }
362 }
363
364 /**
365 * Returns number of disconnected child sites.
366 *
367 * Command Example: wp mainwp sites --disconnected-sites-count.
368 */
369 public static function callback_sites_disconnected_sites_count() {
370 $data = MainWP_DB::instance()->get_disconnected_websites();
371 \WP_CLI::line( 'Number of disconnected child sites: ' . count( $data ) );
372 }
373
374 /**
375 * Syncs all child sites.
376 *
377 * Command Example: wp mainwp sites --sync-sites.
378 *
379 * @uses handle_sync_sites();
380 */
381 public static function callback_sites_sync_sites() {
382 static::handle_sync_sites();
383 }
384
385 /**
386 * Checks all child sites (HTTP Status).
387 *
388 * Command Example: wp mainwp sites --check-sites.
389 *
390 * @param array $args Arguments.
391 * @param array $assoc_args Arguments.
392 */
393 public static function callback_sites_check_sites( $args, $assoc_args ) {
394
395 $sites = static::get_cli_params( $args, $assoc_args, 'sites' );
396
397 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
398 \WP_CLI::line( '' );
399 \WP_CLI::line( esc_html__( 'Check started. Please wait...', 'mainwp' ) );
400 \WP_CLI::line( '' );
401 $errors = 0;
402 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
403 if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
404 continue;
405 }
406 \WP_CLI::line( ' -> ' . $website->name . ' (' . $website->url . ')' );
407 try {
408 MainWP_Monitoring_Handler::handle_check_website( $website );
409 } catch ( \Exception $e ) {
410 \WP_CLI::error( ' Check failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
411 ++$errors;
412 }
413 }
414 MainWP_DB::free_result( $websites );
415 if ( $errors > 0 ) {
416 \WP_CLI::error( 'Check completed with errors' );
417 } else {
418 \WP_CLI::success( 'Check completed' );
419 }
420 }
421
422 /**
423 * Disconnects all child sites.
424 *
425 * Command Example: wp mainwp sites --disconnect-sites.
426 *
427 * @param array $args Arguments.
428 * @param array $assoc_args Arguments.
429 */
430 public static function callback_sites_disconnect_sites( $args = array(), $assoc_args = false ) {
431
432 $sites = static::get_cli_params( $args, $assoc_args, 'sites' );
433
434 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
435 \WP_CLI::line( 'Disconnect started' );
436 $errors = 0;
437 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
438 if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
439 continue;
440 }
441 \WP_CLI::line( ' -> ' . $website->name . ' (' . $website->url . ')' );
442 try {
443 MainWP_Connect::fetch_url_authed( $website, 'disconnect' );
444 } catch ( \Exception $e ) {
445 \WP_CLI::error( ' Disconnect failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
446 ++$errors;
447 }
448 }
449 MainWP_DB::free_result( $websites );
450 if ( $errors > 0 ) {
451 \WP_CLI::error( 'Disconnect completed with errors' );
452 } else {
453 \WP_CLI::success( 'Disconnect completed' );
454 }
455 }
456
457 /**
458 * Lists child site data.
459 *
460 * Command Example: wp mainwp site --site [<websiteid>].
461 *
462 * @param array $args Arguments.
463 * @param array $assoc_args Arguments.
464 * @param object $website Object containing child site data.
465 */
466 public static function callback_site_site( $args = array(), $assoc_args = array(), $website = false ) {
467 \WP_CLI::line( \WP_CLI::colorize( '%gSite Name:%n ' ) . $website->name );
468 \WP_CLI::line( \WP_CLI::colorize( '%gSite URL:%n ' ) . $website->url );
469 \WP_CLI::line( \WP_CLI::colorize( '%gID:%n ' ) . $website->id );
470 \WP_CLI::line( \WP_CLI::colorize( '%gAdmin Username:%n ' ) . $website->adminname );
471 \WP_CLI::line( \WP_CLI::colorize( '%gHTTP Response:%n ' ) . $website->http_response_code );
472 \WP_CLI::line( \WP_CLI::colorize( '%gNotes:%n ' ) . $website->note );
473 \WP_CLI::line( \WP_CLI::colorize( '%gSecurity Issues:%n ' ) . $website->securityIssues );
474 \WP_CLI::line( \WP_CLI::colorize( '%gHealth Issues Total:%n ' ) . $website->health_issues_total );
475 \WP_CLI::line( \WP_CLI::colorize( '%gHealth Issues:%n ' ) . $website->health_issues );
476 \WP_CLI::line( \WP_CLI::colorize( '%gHealth Value:%n ' ) . $website->health_value );
477 }
478
479 /**
480 * Shows child site info.
481 *
482 * Command Example: wp mainwp site --site-info [<websiteid>].
483 *
484 * @param array $args Arguments.
485 * @param array $assoc_args Arguments.
486 * @param object $website Object containing child site data.
487 */
488 public static function callback_site_site_info( $args = array(), $assoc_args = array(), $website = false ) {
489 $data = array(
490 'wpversion' => '',
491 'phpversion' => '',
492 'child_version' => '',
493 'memory_limit' => '',
494 'mysql_version' => '',
495 'themeactivated' => '',
496 'ip' => '',
497 );
498
499 $site_info = MainWP_DB::instance()->get_website_option( $website, 'site_info' );
500 $site_info = ! empty( $site_info ) ? json_decode( $site_info, true ) : array();
501
502 if ( ! is_array( $site_info ) ) {
503 $site_info = array();
504 }
505
506 $data = array_merge( $data, $site_info );
507
508 \WP_CLI::line( \WP_CLI::colorize( '%gSite Name:%n ' ) . $website->name );
509 \WP_CLI::line( \WP_CLI::colorize( '%gSite URL:%n ' ) . $website->url );
510 \WP_CLI::line( \WP_CLI::colorize( '%gWP Version:%n ' ) . $data['wpversion'] );
511 \WP_CLI::line( \WP_CLI::colorize( '%gPHP Version:%n ' ) . $data['phpversion'] );
512 \WP_CLI::line( \WP_CLI::colorize( '%gMainWP Child Version:%n ' ) . $data['child_version'] );
513 \WP_CLI::line( \WP_CLI::colorize( '%gPHP Memory Limit:%n ' ) . $data['memory_limit'] );
514 \WP_CLI::line( \WP_CLI::colorize( '%gMySQL Version:%n ' ) . $data['mysql_version'] );
515 \WP_CLI::line( \WP_CLI::colorize( '%gActive Theme:%n ' ) . $data['themeactivated'] );
516 \WP_CLI::line( \WP_CLI::colorize( '%gIP Address:%n ' ) . $data['ip'] );
517 }
518
519 /**
520 * Lists installed plugins on a child site.
521 *
522 * Command Example: wp mainwp site --site-installed-plugins [<websiteid>].
523 *
524 * @param array $args Arguments.
525 * @param array $assoc_args Arguments.
526 * @param object $website Object containing child site data.
527 */
528 public static function callback_site_site_installed_plugins( $args = array(), $assoc_args = array(), $website = false ) {
529 $plugins = json_decode( $website->plugins, 1 );
530
531 \WP_CLI::line( '' );
532 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Installed Plugins', 'mainwp' ) . '%n' ) );
533 \WP_CLI::line( '' );
534
535 foreach ( $plugins as $plugin ) {
536 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Name:%n ' ) . $plugin['name'] );
537 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Slug:%n ' ) . $plugin['slug'] );
538 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Description:%n ' ) . $plugin['description'] );
539 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Version:%n ' ) . $plugin['version'] );
540 if ( '1' === $plugin['active'] ) {
541 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Status:%n ' ) . esc_html__( 'Active', 'mainwp' ) );
542 } else {
543 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Status:%n ' ) . esc_html__( 'Inactive', 'mainwp' ) );
544 }
545 \WP_CLI::line( '' );
546 }
547 }
548
549 /**
550 * Returns the number of installed plugins on a child site.
551 *
552 * Command Example: wp mainwp site --site-installed-plugins-count [<websiteid>].
553 *
554 * @param array $args Arguments.
555 * @param array $assoc_args Arguments.
556 * @param object $website Object containing child site data.
557 */
558 public static function callback_site_site_installed_plugins_count( $args = array(), $assoc_args = array(), $website = false ) {
559 $plugins = json_decode( $website->plugins, 1 );
560
561 \WP_CLI::line( '' );
562 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Installed plugins: ', 'mainwp' ) . '%n' . count( $plugins ) ) );
563 \WP_CLI::line( '' );
564 }
565
566
567 /**
568 * Lists all active plugins on a child site.
569 *
570 * Command Example: wp mainwp site --site-active-plugins [<websiteid>].
571 *
572 * @param array $args Arguments.
573 * @param array $assoc_args Arguments.
574 * @param object $website Object containing child site data.
575 */
576 public static function callback_site_site_active_plugins( $args = array(), $assoc_args = array(), $website = false ) {
577 $plugins = json_decode( $website->plugins, 1 );
578 $data = MainWP_Utility::get_sub_array_having( $plugins, 'active', 1 );
579
580 \WP_CLI::line( '' );
581 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Active Plugins', 'mainwp' ) . '%n' ) );
582 \WP_CLI::line( '' );
583
584 foreach ( $data as $plugin ) {
585 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Name:%n ' ) . $plugin['name'] );
586 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Slug:%n ' ) . $plugin['slug'] );
587 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Description:%n ' ) . $plugin['description'] );
588 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Version:%n ' ) . $plugin['version'] );
589 \WP_CLI::line( '' );
590 }
591 }
592
593 /**
594 * Returns a number of active plugins on a child site.
595 *
596 * Command Example: wp mainwp site --site-active-plugins-count [<websiteid>].
597 *
598 * @param array $args Arguments.
599 * @param array $assoc_args Arguments.
600 * @param object $website Object containing child site data.
601 */
602 public static function callback_site_site_active_plugins_count( $args = array(), $assoc_args = array(), $website = false ) {
603 $plugins = json_decode( $website->plugins, 1 );
604 $data = MainWP_Utility::get_sub_array_having( $plugins, 'active', 1 );
605
606 \WP_CLI::line( '' );
607 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Active plugins: ', 'mainwp' ) . '%n' . count( $data ) ) );
608 \WP_CLI::line( '' );
609 }
610
611 /**
612 * Lists all inactive plugins on a child site.
613 *
614 * Command Example: wp mainwp site --site-inactive-plugins [<websiteid>].
615 *
616 * @param array $args Arguments.
617 * @param array $assoc_args Arguments.
618 * @param object $website Object containing child site data.
619 */
620 public static function callback_site_site_inactive_plugins( $args = array(), $assoc_args = array(), $website = false ) {
621 $plugins = json_decode( $website->plugins, 1 );
622 $data = MainWP_Utility::get_sub_array_having( $plugins, 'active', 0 );
623
624 \WP_CLI::line( '' );
625 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Inactive Plugins', 'mainwp' ) . '%n' ) );
626 \WP_CLI::line( '' );
627
628 foreach ( $data as $plugin ) {
629 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Name:%n ' ) . $plugin['name'] );
630 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Slug:%n ' ) . $plugin['slug'] );
631 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Description:%n ' ) . $plugin['description'] );
632 \WP_CLI::line( \WP_CLI::colorize( '%gPlugin Version:%n ' ) . $plugin['version'] );
633 \WP_CLI::line( '' );
634 }
635 }
636
637 /**
638 * Returns the number of inactive plugins on a child site.
639 *
640 * Command Example: wp mainwp site --site-inactive-plugins-count [<websiteid>].
641 *
642 * @param array $args Arguments.
643 * @param array $assoc_args Arguments.
644 * @param object $website Object containing child site data.
645 */
646 public static function callback_site_site_inactive_plugins_count( $args = array(), $assoc_args = array(), $website = false ) {
647 $plugins = json_decode( $website->plugins, 1 );
648 $data = MainWP_Utility::get_sub_array_having( $plugins, 'active', 0 );
649
650 \WP_CLI::line( '' );
651 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Inctive plugins: ', 'mainwp' ) . '%n' . count( $data ) ) );
652 \WP_CLI::line( '' );
653 }
654
655
656 /**
657 * Lists all installed themes on a child site.
658 *
659 * Command Example: wp mainwp site --site-installed-themes [<websiteid>].
660 *
661 * @param array $args Arguments.
662 * @param array $assoc_args Arguments.
663 * @param object $website Object containing child site data.
664 */
665 public static function callback_site_site_installed_themes( $args = array(), $assoc_args = array(), $website = false ) {
666 $themes = json_decode( $website->themes, 1 );
667
668 \WP_CLI::line( '' );
669 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Installed Themes', 'mainwp' ) . '%n' ) );
670 \WP_CLI::line( '' );
671
672 foreach ( $themes as $theme ) {
673 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Name:%n ' ) . $theme['name'] );
674 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Slug:%n ' ) . $theme['slug'] );
675 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Description:%n ' ) . $theme['description'] );
676 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Version:%n ' ) . $theme['version'] );
677 if ( '1' === $theme['active'] ) {
678 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Status:%n ' ) . esc_html__( 'Active', 'mainwp' ) );
679 } else {
680 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Status:%n ' ) . esc_html__( 'Inactive', 'mainwp' ) );
681 }
682 \WP_CLI::line( '' );
683 }
684 }
685
686 /**
687 * Returns the number of installed themes.
688 *
689 * Command Example: wp mainwp site --site-installed-themes-count [<websiteid>].
690 *
691 * @param array $args Arguments.
692 * @param array $assoc_args Arguments.
693 * @param object $website Object containing child site data.
694 */
695 public static function callback_site_site_installed_themes_count( $args = array(), $assoc_args = array(), $website = false ) {
696 $themes = json_decode( $website->themes, 1 );
697
698 \WP_CLI::line( '' );
699 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Installed themes: ', 'mainwp' ) . '%n' . count( $themes ) ) );
700 \WP_CLI::line( '' );
701 }
702
703
704 /**
705 * Shows the active theme on the child site.
706 *
707 * Command Example: wp mainwp site --site-active-themes [<websiteid>].
708 *
709 * @param array $args Arguments.
710 * @param array $assoc_args Arguments.
711 * @param object $website Object containing child site data.
712 */
713 public static function callback_site_site_active_themes( $args = array(), $assoc_args = array(), $website = false ) {
714 $themes = json_decode( $website->themes, 1 );
715 $data = MainWP_Utility::get_sub_array_having( $themes, 'active', 1 );
716
717 \WP_CLI::line( '' );
718 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Active Theme', 'mainwp' ) . '%n' ) );
719 \WP_CLI::line( '' );
720
721 foreach ( $data as $theme ) {
722 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Name:%n ' ) . $theme['name'] );
723 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Slug:%n ' ) . $theme['slug'] );
724 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Description:%n ' ) . $theme['description'] );
725 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Version:%n ' ) . $theme['version'] );
726 \WP_CLI::line( '' );
727 }
728 }
729
730 /**
731 * Lists all inactive themes on a child site.
732 *
733 * Command Example: wp mainwp site --site-inactive-themes [<websiteid>].
734 *
735 * @param array $args Arguments.
736 * @param array $assoc_args Arguments.
737 * @param object $website Object containing child site data.
738 */
739 public static function callback_site_site_inactive_themes( $args = array(), $assoc_args = array(), $website = false ) {
740 $themes = json_decode( $website->themes, 1 );
741 $data = MainWP_Utility::get_sub_array_having( $themes, 'active', 0 );
742
743 \WP_CLI::line( '' );
744 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Inactive Themes', 'mainwp' ) . '%n' ) );
745 \WP_CLI::line( '' );
746
747 foreach ( $data as $theme ) {
748 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Name:%n ' ) . $theme['name'] );
749 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Slug:%n ' ) . $theme['slug'] );
750 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Description:%n ' ) . $theme['description'] );
751 \WP_CLI::line( \WP_CLI::colorize( '%gTheme Version:%n ' ) . $theme['version'] );
752 \WP_CLI::line( '' );
753 }
754 }
755
756 /**
757 * Returns the number of inactive themes.
758 *
759 * Command Example: wp mainwp site --site-inactive-themes-count [<websiteid>].
760 *
761 * @param array $args Arguments.
762 * @param array $assoc_args Arguments.
763 * @param object $website Object containing child site data.
764 */
765 public static function callback_site_site_inactive_themes_count( $args = array(), $assoc_args = array(), $website = false ) {
766 $themes = json_decode( $website->themes, 1 );
767 $data = MainWP_Utility::get_sub_array_having( $themes, 'active', 0 );
768
769 \WP_CLI::line( '' );
770 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Inactive themes: ', 'mainwp' ) . '%n' . count( $data ) ) );
771 \WP_CLI::line( '' );
772 }
773
774 /**
775 * Lists available updates for a child site.
776 *
777 * Command Example: wp mainwp site --site-available-updates [<websiteid>].
778 *
779 * @param array $args Arguments.
780 * @param array $assoc_args Arguments.
781 * @param object $website Object containing child site data.
782 */
783 public static function callback_site_site_available_updates( $args = array(), $assoc_args = array(), $website = false ) { // phpcs:ignore -- NOSONAR - complex.
784 $site_options = MainWP_DB::instance()->get_website_options_array( $website, array( 'wp_upgrades', 'ignored_trans_updates' ) );
785 $wp_upgrades = isset( $site_options['wp_upgrades'] ) ? json_decode( $site_options['wp_upgrades'], true ) : array();
786 $ignored_core_upgrades = isset( $site_options['ignored_wp_upgrades'] ) ? json_decode( $site_options['ignored_wp_upgrades'], true ) : array();
787
788 if ( $website->is_ignoreCoreUpdates || MainWP_Common_Functions::instance()->is_ignored_updates( $wp_upgrades, $ignored_core_upgrades, 'core' ) ) {
789 $wp_upgrades = array();
790 }
791
792 $ignored_trans_updates = isset( $site_options['ignored_trans_updates'] ) ? (int) $site_options['ignored_trans_updates'] : false;
793
794 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
795 $theme_upgrades = json_decode( $website->theme_upgrades, true );
796 $translation_upgrades = json_decode( $website->translation_upgrades, true );
797
798 if ( $website->is_ignorePluginUpdates ) {
799 $plugin_upgrades = array();
800 }
801 if ( $website->is_ignoreThemeUpdates ) {
802 $theme_upgrades = array();
803 }
804
805 if ( $ignored_trans_updates ) {
806 $translation_upgrades = array();
807 }
808
809 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
810 if ( is_array( $plugin_upgrades ) && ! empty( $plugin_upgrades ) ) {
811 $ignored_plugins = json_decode( $website->ignored_plugins, true );
812 if ( is_array( $ignored_plugins ) ) {
813 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
814
815 }
816 $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
817 if ( is_array( $ignored_plugins ) ) {
818 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
819
820 }
821 }
822
823 if ( is_array( $theme_upgrades ) && ! empty( $theme_upgrades ) ) {
824 $ignored_themes = json_decode( $website->ignored_themes, true );
825 if ( is_array( $ignored_themes ) ) {
826 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
827 }
828
829 $ignored_themes = json_decode( $userExtension->ignored_themes, true );
830 if ( is_array( $ignored_themes ) ) {
831 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
832 }
833 }
834
835 \WP_CLI::line( '' );
836 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Available Updates', 'mainwp' ) . '%n' ) );
837 \WP_CLI::line( '' );
838
839 if ( ! empty( $wp_upgrades ) ) {
840 \WP_CLI::line( '' );
841 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'WordPress Core', 'mainwp' ) . '%n' ) );
842 \WP_CLI::line( '' );
843
844 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $wp_upgrades['current'] );
845 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $wp_upgrades['new'] );
846 }
847
848 if ( ! empty( $plugin_upgrades ) ) {
849 \WP_CLI::line( '' );
850 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Plugin Updates', 'mainwp' ) . '%n' ) );
851 \WP_CLI::line( '' );
852
853 foreach ( $plugin_upgrades as $plugin_upgrade ) {
854 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $plugin_upgrade['Name'] );
855 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $plugin_upgrade['Version'] );
856 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $plugin_upgrade['update']['new_version'] );
857 \WP_CLI::line( '' );
858 }
859 }
860
861 if ( ! empty( $theme_upgrades ) ) {
862 \WP_CLI::line( '' );
863 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Theme Updates', 'mainwp' ) . '%n' ) );
864 \WP_CLI::line( '' );
865
866 foreach ( $theme_upgrades as $theme_upgrade ) {
867 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $theme_upgrade['Name'] );
868 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $theme_upgrade['Version'] );
869 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $theme_upgrade['update']['new_version'] );
870 \WP_CLI::line( '' );
871 }
872 }
873
874 if ( ! empty( $translation_upgrades ) ) {
875 \WP_CLI::line( '' );
876 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Translation Updates', 'mainwp' ) . '%n' ) );
877 \WP_CLI::line( '' );
878
879 foreach ( $translation_upgrades as $translation_upgrade ) {
880 if ( ! is_array( $translation_upgrade ) ) {
881 $translation_upgrade = array();
882 }
883 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . ( isset( $translation_upgrade['Name'] ) ? $translation_upgrade['Name'] : '' ) );
884 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . ( isset( $translation_upgrade['Version'] ) ? $translation_upgrade['Version'] : '' ) );
885 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . ( isset( $translation_upgrade['update']['new_version'] ) ? $translation_upgrade['update']['new_version'] : '' ) );
886 \WP_CLI::line( '' );
887 }
888 }
889 }
890
891 /**
892 * Returns the number of available updates for a child site.
893 *
894 * Command Example: wp mainwp site --site-available-updates-count [<websiteid>].
895 *
896 * @param array $args Arguments.
897 * @param array $assoc_args Arguments.
898 * @param object $website Object containing child site data.
899 */
900 public static function callback_site_site_available_updates_count( $args = array(), $assoc_args = array(), $website = false ) { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR - complexity.
901 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
902 $theme_upgrades = json_decode( $website->theme_upgrades, true );
903
904 if ( $website->is_ignorePluginUpdates ) {
905 $plugin_upgrades = array();
906 }
907 if ( $website->is_ignoreThemeUpdates ) {
908 $theme_upgrades = array();
909 }
910
911 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
912 if ( is_array( $plugin_upgrades ) && ! empty( $plugin_upgrades ) ) {
913 $ignored_plugins = json_decode( $website->ignored_plugins, true );
914 if ( is_array( $ignored_plugins ) ) {
915 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
916
917 }
918 $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
919 if ( is_array( $ignored_plugins ) ) {
920 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
921
922 }
923 }
924
925 if ( is_array( $theme_upgrades ) && ! empty( $theme_upgrades ) ) {
926 $ignored_themes = json_decode( $website->ignored_themes, true );
927 if ( is_array( $ignored_themes ) ) {
928 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
929 }
930
931 $ignored_themes = json_decode( $userExtension->ignored_themes, true );
932 if ( is_array( $ignored_themes ) ) {
933 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
934 }
935 }
936
937 $translations = json_decode( $website->translation_upgrades, true );
938 $wp = MainWP_DB::instance()->get_website_option( $website, 'wp_upgrades' );
939 $wp = ! empty( $wp ) ? json_decode( $wp, true ) : array();
940
941 if ( ! empty( $wp ) ) {
942 $wp = 1;
943 } else {
944 $wp = 0;
945 }
946 $total = array_merge( $plugin_upgrades, $theme_upgrades, $translations );
947 $data = array(
948 'total' => count( $total ) + $wp,
949 'wp' => $wp,
950 'plugins' => count( $plugin_upgrades ),
951 'themes' => count( $theme_upgrades ),
952 'translations' => count( $translations ),
953 );
954
955 \WP_CLI::line( '' );
956 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Updates', 'mainwp' ) . '%n' ) );
957 \WP_CLI::line( '' );
958
959 \WP_CLI::line( \WP_CLI::colorize( '%gWordPress:%n ' ) . $data['wp'] );
960 \WP_CLI::line( \WP_CLI::colorize( '%gPlugins:%n ' ) . $data['plugins'] );
961 \WP_CLI::line( \WP_CLI::colorize( '%gThemes:%n ' ) . $data['themes'] );
962 \WP_CLI::line( \WP_CLI::colorize( '%gTranslations:%n ' ) . $data['translations'] );
963 \WP_CLI::line( '' );
964 \WP_CLI::line( \WP_CLI::colorize( '%gTotal:%n ' ) . $data['total'] );
965 \WP_CLI::line( '' );
966 }
967
968 /**
969 * Lists all abandoned plugins on a child site.
970 *
971 * Command Example: wp mainwp site --site-abandoned-plugins [<websiteid>].
972 *
973 * @param array $args Arguments.
974 * @param array $assoc_args Arguments.
975 * @param object $website Object containing child site data.
976 */
977 public static function callback_site_site_abandoned_plugins( $args = array(), $assoc_args = array(), $website = false ) {
978 $plugins = MainWP_DB::instance()->get_website_option( $website, 'plugins_outdate_info' );
979 $plugins = ! empty( $plugins ) ? json_decode( $plugins, true ) : array();
980
981 \WP_CLI::line( '' );
982 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Abandoned Plugins', 'mainwp' ) . '%n' ) );
983 \WP_CLI::line( '' );
984
985 foreach ( $plugins as $plugin ) {
986 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $plugin['Name'] );
987 \WP_CLI::line( \WP_CLI::colorize( '%gURI:%n ' ) . $plugin['PluginURI'] );
988 \WP_CLI::line( \WP_CLI::colorize( '%gVersion:%n ' ) . $plugin['Version'] );
989 \WP_CLI::line( \WP_CLI::colorize( '%gLatest Update:%n ' ) . date( 'F j, Y', $plugin['last_updated'] ) ); // phpcs:ignore -- local time.
990 \WP_CLI::line( '' );
991 }
992 }
993
994 /**
995 * Returns the number of abaindoned plugins on a child site.
996 *
997 * Command Example: wp mainwp site --site-abandoned-plugins-count [<websiteid>].
998 *
999 * @param array $args Arguments.
1000 * @param array $assoc_args Arguments.
1001 * @param object $website Object containing child site data.
1002 */
1003 public static function callback_site_site_abandoned_plugins_count( $args = array(), $assoc_args = array(), $website = false ) {
1004 $plugins = MainWP_DB::instance()->get_website_option( $website, 'plugins_outdate_info' );
1005 $plugins = ! empty( $plugins ) ? json_decode( $plugins, true ) : array();
1006
1007 \WP_CLI::line( '' );
1008 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . ' ' . esc_html__( 'Abandoned plugins: ', 'mainwp' ) . '%n' . count( $plugins ) ) );
1009 \WP_CLI::line( '' );
1010 }
1011
1012 /**
1013 * Lists all abandoned themes on a child site.
1014 *
1015 * Command Example: wp mainwp site --site-abandoned-themes [<websiteid>].
1016 *
1017 * @param array $args Arguments.
1018 * @param array $assoc_args Arguments.
1019 * @param object $website Object containing child site data.
1020 */
1021 public static function callback_site_site_abandoned_themes( $args = array(), $assoc_args = array(), $website = false ) {
1022 $themes = MainWP_DB::instance()->get_website_option( $website, 'themes_outdate_info' );
1023 $themes = ! empty( $themes ) ? json_decode( $themes, true ) : array();
1024
1025 \WP_CLI::line( '' );
1026 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Abandoned Themes', 'mainwp' ) . '%n' ) );
1027 \WP_CLI::line( '' );
1028
1029 foreach ( $themes as $theme ) {
1030 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $theme['Name'] );
1031 \WP_CLI::line( \WP_CLI::colorize( '%gVersion:%n ' ) . $theme['Version'] );
1032 \WP_CLI::line( \WP_CLI::colorize( '%gLatest Update:%n ' ) . date( 'F j, Y', $theme['last_updated'] ) ); // phpcs:ignore -- local time.
1033 \WP_CLI::line( '' );
1034 }
1035 }
1036
1037 /**
1038 * Returns the number of abandoned themes on a child site.
1039 *
1040 * Command Example: wp mainwp site --site-abandoned-themes-count [<websiteid>].
1041 *
1042 * @param array $args Arguments.
1043 * @param array $assoc_args Arguments.
1044 * @param object $website Object containing child site data.
1045 */
1046 public static function callback_site_site_abandoned_themes_count( $args = array(), $assoc_args = array(), $website = false ) {
1047 $themes = MainWP_DB::instance()->get_website_option( $website, 'themes_outdate_info' );
1048 $themes = ! empty( $themes ) ? json_decode( $themes, true ) : array();
1049
1050 \WP_CLI::line( '' );
1051 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . ' ' . esc_html__( 'Abandoned themes: ', 'mainwp' ) . '%n' . count( $themes ) ) );
1052 \WP_CLI::line( '' );
1053 }
1054
1055 /**
1056 * Returns child site HTTP status.
1057 *
1058 * Command Example: wp mainwp site --site-http-status [<websiteid>].
1059 *
1060 * @param array $args Arguments.
1061 * @param array $assoc_args Arguments.
1062 * @param object $website Object containing child site data.
1063 */
1064 public static function callback_site_site_http_status( $args = array(), $assoc_args = array(), $website = false ) {
1065 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1066 MainWP_Monitoring_Handler::handle_check_website( $website );
1067 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Process ran successfully on ', 'mainwp' ) . $website->name . '%n' ) );
1068 }
1069
1070 /**
1071 * Returns child site Health score.
1072 *
1073 * Command Example: wp mainwp site --site-health-score [<websiteid>].
1074 *
1075 * @param array $args Arguments.
1076 * @param array $assoc_args Arguments.
1077 * @param object $website Object containing child site data.
1078 */
1079 public static function callback_site_site_health_score( $args = array(), $assoc_args = array(), $website = false ) {
1080 $website = MainWP_DB::instance()->get_website_by_id( $website->id, false, array( 'health_site_status' ) );
1081 $health_status = isset( $website->health_site_status ) ? json_decode( $website->health_site_status, true ) : array();
1082 $hstatus = MainWP_Utility::get_site_health( $health_status );
1083 $hval = $hstatus['val'];
1084 $critical = $hstatus['critical'];
1085 if ( 80 <= $hval && empty( $critical ) ) {
1086 $health_score = 'Good';
1087 } else {
1088 $health_score = 'Should be improved';
1089 }
1090
1091 \WP_CLI::line( '' );
1092 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . ' Health: %n ' ) . $health_score );
1093 \WP_CLI::line( '' );
1094 }
1095
1096 /**
1097 * Lists child site security issues.
1098 *
1099 * Command Example: wp mainwp site --site-security-issues [<websiteid>].
1100 *
1101 * @param array $args Arguments.
1102 * @param array $assoc_args Arguments.
1103 * @param object $website Object containing child site data.
1104 */
1105 public static function callback_site_site_security_issues( $args = array(), $assoc_args = array(), $website = false ) {
1106 $data = MainWP_Connect::fetch_url_authed( $website, 'security' );
1107
1108 \WP_CLI::line( '' );
1109 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Potential Secuirty Issues', 'mainwp' ) . '%n' ) );
1110 \WP_CLI::line( '' );
1111
1112 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Database error reporting disabled:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['db_reporting'] ? 'NO' : 'YES' ) );
1113 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'PHP error reporting disabled:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['php_reporting'] ? 'NO' : 'YES' ) );
1114 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'WordPress is not up to date:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['wp_uptodate'] ? 'NO' : 'YES' ) );
1115 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'PHP version does not match the WordPress requirement:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['phpversion_matched'] ? 'NO' : 'YES' ) );
1116 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'SSL protocol is not in place:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['sslprotocol'] ? 'NO' : 'YES' ) );
1117 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'WP Config debugging is enabled:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['debug_disabled'] ? 'NO' : 'YES' ) );
1118 }
1119
1120 /**
1121 * Adds child site.
1122 *
1123 * Command Example: wp mainwp site --add-site [<websiteid>].
1124 *
1125 * @param array $args Arguments.
1126 * @param array $assoc_args Arguments.
1127 * @param object $website Object containing child site data.
1128 */
1129 public static function callback_site_add_site( $args = array(), $assoc_args = array(), $website = false ) {
1130 $fields = static::get_cli_params( $args, $assoc_args, 'add-site' );
1131 $data = MainWP_Manage_Sites_Handler::rest_api_add_site( $fields );
1132 if ( is_array( $data ) && ! empty( $data['siteid'] ) ) {
1133 \WP_CLI::success( ' -> Add site result: ' . print_r( $data, true ) ); // phpcs:ignore -- for cli result.
1134 } else {
1135 \WP_CLI::error( ' -> Add site result: ' . print_r( $data, true ) ); // phpcs:ignore -- for cli result.
1136 }
1137 }
1138
1139 /**
1140 * Edits child site.
1141 *
1142 * Command Example: wp mainwp site --edit-site [<websiteid>].
1143 *
1144 * @param array $args Arguments.
1145 * @param array $assoc_args Arguments.
1146 * @param object $website Object containing child site data.
1147 */
1148 public static function callback_site_edit_site( $args = array(), $assoc_args = array(), $website = false ) {
1149 $fields = static::get_cli_params( $args, $assoc_args, 'edit-site' );
1150 $data = MainWP_DB_Common::instance()->rest_api_update_website( $website->id, $fields );
1151 $website = MainWP_DB::instance()->get_website_by_id( $website->id );
1152 \WP_CLI::line( ' -> ' . $website->name . ' (' . $website->url . ')' );
1153 \WP_CLI::line( ' -> Edit site result: ' . ( $data ? 'successed' : 'failed' ) );
1154 }
1155
1156 /**
1157 * Syncs child site.
1158 *
1159 * Command Example: wp mainwp site --sync-site [<websiteid>].
1160 *
1161 * @param array $args Arguments.
1162 * @param array $assoc_args Arguments.
1163 * @param object $website Object containing child site data.
1164 */
1165 public static function callback_site_sync_site( $args = array(), $assoc_args = array(), $website = false ) {
1166 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1167 $error = false;
1168 try {
1169 MainWP_Sync::sync_site( $website );
1170 } catch ( \Exception $e ) {
1171 $error = $e->getMessage();
1172 }
1173
1174 if ( empty( $error ) ) {
1175 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' synced successfully.', 'mainwp' ) . '%n' ) );
1176 } else {
1177 \WP_CLI::line( \WP_CLI::colorize( '%r' . esc_html__( 'Process failed with error:', 'mainwp' ) . '%n' ) );
1178 \WP_CLI::line( $error );
1179 }
1180 }
1181
1182 /**
1183 * Reconnects child site.
1184 *
1185 * Command Example: wp mainwp site --reconnect-site [<websiteid>].
1186 *
1187 * @param array $args Arguments.
1188 * @param array $assoc_args Arguments.
1189 * @param object $website Object containing child site data.
1190 */
1191 public static function callback_site_reconnect_site( $args = array(), $assoc_args = array(), $website = false ) {
1192 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1193 $error = false;
1194 try {
1195 MainWP_Manage_Sites_View::m_reconnect_site( $website );
1196 } catch ( \Exception $e ) {
1197 $error = $e->getMessage();
1198 }
1199
1200 if ( empty( $error ) ) {
1201 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' reconnected successfully.', 'mainwp' ) . '%n' ) );
1202 } else {
1203 \WP_CLI::line( \WP_CLI::colorize( '%r' . esc_html__( 'Process failed with error:', 'mainwp' ) . '%n' ) );
1204 \WP_CLI::line( $error );
1205 }
1206 }
1207
1208 /**
1209 * Disconnects child site.
1210 *
1211 * Command Example: wp mainwp site --disconnect-site [<websiteid>].
1212 *
1213 * @param array $args Arguments.
1214 * @param array $assoc_args Arguments.
1215 * @param object $website Object containing child site data.
1216 */
1217 public static function callback_site_disconnect_site( $args = array(), $assoc_args = array(), $website = false ) {
1218 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1219
1220 $error = false;
1221 try {
1222 MainWP_Connect::fetch_url_authed( $website, 'disconnect' );
1223 } catch ( \Exception $e ) {
1224 $error = $e->getMessage();
1225 }
1226
1227 if ( empty( $error ) ) {
1228 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' disconnected successfully.', 'mainwp' ) . '%n' ) );
1229 } else {
1230 \WP_CLI::line( \WP_CLI::colorize( '%r' . esc_html__( 'Process failed with error:', 'mainwp' ) . '%n' ) );
1231 \WP_CLI::line( $error );
1232 }
1233 }
1234
1235 /**
1236 * Removes child site from the MainWP Dashboard.
1237 *
1238 * Command Example: wp mainwp site --remove-site [<websiteid>].
1239 *
1240 * @param array $args Arguments.
1241 * @param array $assoc_args Arguments.
1242 * @param object $website Object containing child site data.
1243 */
1244 public static function callback_site_remove_site( $args = array(), $assoc_args = array(), $website = false ) {
1245 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1246 MainWP_Manage_Sites_Handler::remove_website( $website->id );
1247 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Site removed successfully.', 'mainwp' ) . '%n' ) );
1248 }
1249
1250 /**
1251 * Updates WP Core on a child site.
1252 *
1253 * Command Example: wp mainwp site --site-update-wordpress [<websiteid>].
1254 *
1255 * @param array $args Arguments.
1256 * @param array $assoc_args Arguments.
1257 * @param object $website Object containing child site data.
1258 */
1259 public static function callback_site_site_update_wordpress( $args = array(), $assoc_args = array(), $website = false ) {
1260 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1261 try {
1262 MainWP_Updates_Handler::upgrade_website( $website );
1263 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' updated successfully.', 'mainwp' ) . '%n' ) );
1264 } catch ( \Exception $e ) {
1265 \WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
1266 if ( $e->getMesage() === 'WPERROR' ) {
1267 \WP_CLI::debug( 'Error: ' . MainWP_Utility::value_to_string( $e->get_message_extra(), 1 ) );
1268 }
1269 }
1270 }
1271
1272 /**
1273 * Updates all plugins on a child site.
1274 *
1275 * Command Example: wp mainwp site --site-update-plugins [<websiteid>].
1276 *
1277 * @param array $args Arguments.
1278 * @param array $assoc_args Arguments.
1279 * @param object $website Object containing child site data.
1280 */
1281 public static function callback_site_site_update_plugins( $args = array(), $assoc_args = array(), $website = false ) {
1282 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1283
1284 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
1285
1286 if ( $website->is_ignorePluginUpdates ) {
1287 $plugin_upgrades = array();
1288 }
1289
1290 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
1291 if ( is_array( $plugin_upgrades ) && ! empty( $plugin_upgrades ) ) {
1292 $ignored_plugins = json_decode( $website->ignored_plugins, true );
1293 if ( is_array( $ignored_plugins ) ) {
1294 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
1295
1296 }
1297 $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
1298 if ( is_array( $ignored_plugins ) ) {
1299 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
1300
1301 }
1302 }
1303
1304 $slugs = array();
1305 foreach ( $plugin_upgrades as $slug => $plugin ) {
1306 $slugs[] = $slug;
1307 }
1308 $list = urldecode( implode( ',', $slugs ) );
1309
1310 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' plugins updated successfully.', 'mainwp' ) . '%n' ) );
1311
1312 try {
1313 MainWP_Connect::fetch_url_authed(
1314 $website,
1315 'upgradeplugintheme',
1316 array(
1317 'type' => 'plugin',
1318 'list' => $list,
1319 )
1320 );
1321 } catch ( \Exception $e ) {
1322 MainWP_Error_Helper::get_console_error_message( $e );
1323 }
1324 }
1325
1326 /**
1327 * Updates all themes on a child site.
1328 *
1329 * Command Example: wp mainwp site --site-update-themes [<websiteid>].
1330 *
1331 * @param array $args Arguments.
1332 * @param array $assoc_args Arguments.
1333 * @param object $website Object containing child site data.
1334 */
1335 public static function callback_site_site_update_themes( $args = array(), $assoc_args = array(), $website = false ) {
1336 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1337 $theme_upgrades = json_decode( $website->theme_upgrades, true );
1338 $slugs = array();
1339 foreach ( $theme_upgrades as $slug => $theme ) {
1340 $slugs[] = $slug;
1341 }
1342 $list = urldecode( implode( ',', $slugs ) );
1343
1344 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' themes updated successfully.', 'mainwp' ) . '%n' ) );
1345
1346 try {
1347 MainWP_Connect::fetch_url_authed(
1348 $website,
1349 'upgradeplugintheme',
1350 array(
1351 'type' => 'theme',
1352 'list' => $list,
1353 )
1354 );
1355 } catch ( \Exception $e ) {
1356 MainWP_Error_Helper::get_console_error_message( $e );
1357 }
1358 }
1359
1360 /**
1361 * Updates translations on a child site.
1362 *
1363 * Command Example: wp mainwp site --site-update-translations [<websiteid>].
1364 *
1365 * @param array $args Arguments.
1366 * @param array $assoc_args Arguments.
1367 * @param object $website Object containing child site data.
1368 */
1369 public static function callback_site_site_update_translations( $args = array(), $assoc_args = array(), $website = false ) {
1370 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1371
1372 $translation_upgrades = json_decode( $website->translation_upgrades, true );
1373 $slugs = array();
1374 foreach ( $translation_upgrades as $translation_upgrade ) {
1375 $slugs[] = $translation_upgrade['slug'];
1376 }
1377 $list = urldecode( implode( ',', $slugs ) );
1378
1379 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' translations updated successfully.', 'mainwp' ) . '%n' ) );
1380
1381 try {
1382 MainWP_Connect::fetch_url_authed(
1383 $website,
1384 'upgradetranslation',
1385 array(
1386 'type' => 'translation',
1387 'list' => $list,
1388 )
1389 );
1390 } catch ( \Exception $e ) {
1391 MainWP_Error_Helper::get_console_error_message( $e );
1392 }
1393 }
1394
1395 /**
1396 * Updates single item on a child site.
1397 *
1398 * Command Example: wp mainwp site --site-update-item [<websiteid>] --type=[type] --slug=[slug].
1399 *
1400 * @param array $args Arguments.
1401 * @param array $assoc_args Arguments.
1402 * @param object $website Object containing child site data.
1403 */
1404 public static function callback_site_site_update_item( $args = array(), $assoc_args = array(), $website = false ) {
1405
1406 $params = static::get_cli_params( $args, $assoc_args, array( 'type', 'slug' ) );
1407
1408 $type = $params['type'];
1409 $slug = $params['slug'];
1410
1411 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1412
1413 try {
1414 MainWP_Connect::fetch_url_authed(
1415 $website,
1416 'upgradeplugintheme',
1417 array(
1418 'type' => $type,
1419 'list' => urldecode( $slug ),
1420 )
1421 );
1422 } catch ( \Exception $e ) {
1423 MainWP_Error_Helper::get_console_error_message( $e );
1424 }
1425
1426 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' item updated successfully.', 'mainwp' ) . '%n' ) );
1427 }
1428
1429 /**
1430 * Manages plgins on a child site.
1431 *
1432 * Command Example: wp mainwp site --site-manage-plugin [<websiteid>] --action=[action] --plugin=[plugin].
1433 *
1434 * Action: activate|deactivate|delete
1435 * Plugin: plugin slugs separated by ||.
1436 *
1437 * @param array $args Arguments.
1438 * @param array $assoc_args Arguments.
1439 * @param object $website Object containing child site data.
1440 */
1441 public static function callback_site_site_manage_plugin( $args = array(), $assoc_args = array(), $website = false ) {
1442 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1443
1444 $params = static::get_cli_params( $args, $assoc_args, array( 'plugin', 'action' ) );
1445 $plugin = $params['plugin'];
1446 $action = $params['action'];
1447
1448 \WP_CLI::line( \WP_CLI::colorize( '%g' . $plugin . ' ' . $action . 'd' . esc_html__( ' successfully.', 'mainwp' ) . '%n' ) );
1449
1450 try {
1451 MainWP_Connect::fetch_url_authed(
1452 $website,
1453 'plugin_action',
1454 array(
1455 'action' => $action,
1456 'plugin' => $plugin,
1457 )
1458 );
1459 } catch ( \Exception $e ) {
1460 // ok.
1461 }
1462 }
1463
1464 /**
1465 * Manages themes on a child site.
1466 *
1467 * Command Example: wp mainwp site --site-manage-theme [<websiteid>] --action=[action] --theme=[theme].
1468 *
1469 * action: activate|delete.
1470 * theme: theme name.
1471 *
1472 * @param array $args Arguments.
1473 * @param array $assoc_args Arguments.
1474 * @param object $website Object containing child site data.
1475 */
1476 public static function callback_site_site_manage_theme( $args = array(), $assoc_args = array(), $website = false ) {
1477 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1478
1479 $params = static::get_cli_params( $args, $assoc_args, array( 'theme', 'action' ) );
1480 $theme = $params['theme'];
1481 $action = $params['action'];
1482
1483 \WP_CLI::line( \WP_CLI::colorize( '%g' . $theme . ' ' . $action . 'd' . esc_html__( ' successfully.', 'mainwp' ) . '%n' ) );
1484
1485 try {
1486 MainWP_Connect::fetch_url_authed(
1487 $website,
1488 'theme_action',
1489 array(
1490 'action' => $action,
1491 'theme' => $theme,
1492 )
1493 );
1494 } catch ( \Exception $e ) {
1495 // ok.
1496 }
1497 }
1498
1499 /**
1500 * Checks child site for HTTP Status.
1501 *
1502 * Command Example: wp mainwp site --check-site-http-status [<websiteid>].
1503 *
1504 * @param array $args Arguments.
1505 * @param array $assoc_args Arguments.
1506 * @param object $website Object containing child site data.
1507 */
1508 public static function callback_site_check_site_http_status( $args = array(), $assoc_args = array(), $website = false ) {
1509 \WP_CLI::line( '' );
1510 \WP_CLI::line( esc_html__( 'Checking ', 'mainwp' ) . $website->name . ' (' . $website->url . '). ' . esc_html__( 'Please wait... ', 'mainwp' ) );
1511 \WP_CLI::line( '' );
1512 $data = MainWP_Monitoring_Handler::handle_check_website( $website );
1513 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'HTTP Status: ', 'mainwp' ) . '%n' . $data['httpCode'] . ' (' . $data['httpCodeString'] . ')' ) );
1514 \WP_CLI::line( '' );
1515 }
1516
1517 /**
1518 * Lists all available update for all sites.
1519 *
1520 * Command Example: wp mainwp updates --available-updates [<websiteid>].
1521 *
1522 * @param array $args Arguments.
1523 * @param array $assoc_args Arguments.
1524 */
1525 public static function callback_updates_available_updates( $args = array(), $assoc_args = array() ) { // phpcs:ignore -- NOSONAR - complex.
1526
1527 \WP_CLI::line( '' );
1528 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Updates', 'mainwp' ) . '%n' ) );
1529 \WP_CLI::line( '' );
1530
1531 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
1532 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
1533
1534 $site_options = MainWP_DB::instance()->get_website_options_array( $website, array( 'wp_upgrades', 'ignored_trans_updates' ) );
1535 $wp_upgrades = isset( $site_options['wp_upgrades'] ) ? json_decode( $site_options['wp_upgrades'], true ) : array();
1536 $ignored_core_upgrades = isset( $site_options['ignored_wp_upgrades'] ) ? json_decode( $site_options['ignored_wp_upgrades'], true ) : array();
1537
1538 if ( $website->is_ignoreCoreUpdates || MainWP_Common_Functions::instance()->is_ignored_updates( $wp_upgrades, $ignored_core_upgrades, 'core' ) ) {
1539 $wp_upgrades = array();
1540 }
1541
1542 $ignored_trans_updates = isset( $site_options['ignored_trans_updates'] ) ? (int) $site_options['ignored_trans_updates'] : false;
1543
1544 $wp_upgrades = MainWP_DB::instance()->get_website_option( $website, 'wp_upgrades' );
1545 $wp_upgrades = ! empty( $wp_upgrades ) ? json_decode( $wp_upgrades, true ) : array();
1546
1547 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
1548 $theme_upgrades = json_decode( $website->theme_upgrades, true );
1549 $translation_upgrades = json_decode( $website->translation_upgrades, true );
1550
1551 if ( $website->is_ignorePluginUpdates ) {
1552 $plugin_upgrades = array();
1553 }
1554 if ( $website->is_ignoreThemeUpdates ) {
1555 $theme_upgrades = array();
1556 }
1557
1558 if ( $ignored_trans_updates ) {
1559 $translation_upgrades = array();
1560 }
1561
1562 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
1563 if ( is_array( $plugin_upgrades ) && ! empty( $plugin_upgrades ) ) {
1564 $ignored_plugins = json_decode( $website->ignored_plugins, true );
1565 if ( is_array( $ignored_plugins ) ) {
1566 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
1567
1568 }
1569 $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
1570 if ( is_array( $ignored_plugins ) ) {
1571 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
1572
1573 }
1574 }
1575
1576 if ( is_array( $theme_upgrades ) && ! empty( $theme_upgrades ) ) {
1577 $ignored_themes = json_decode( $website->ignored_themes, true );
1578 if ( is_array( $ignored_themes ) ) {
1579 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
1580 }
1581
1582 $ignored_themes = json_decode( $userExtension->ignored_themes, true );
1583 if ( is_array( $ignored_themes ) ) {
1584 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
1585 }
1586 }
1587
1588 \WP_CLI::line( \WP_CLI::colorize( '%B' . $website->name . ' (' . $website->url . ')%n' ) );
1589 if ( ! empty( $wp_upgrades ) ) {
1590 \WP_CLI::line( '' );
1591 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'WordPress Core', 'mainwp' ) . '%n' ) );
1592 \WP_CLI::line( '' );
1593
1594 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $wp_upgrades['current'] );
1595 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $wp_upgrades['new'] );
1596 }
1597
1598 if ( ! empty( $plugin_upgrades ) ) {
1599 \WP_CLI::line( '' );
1600 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Plugin Updates', 'mainwp' ) . '%n' ) );
1601 \WP_CLI::line( '' );
1602
1603 foreach ( $plugin_upgrades as $plugin_upgrade ) {
1604 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $plugin_upgrade['Name'] );
1605 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $plugin_upgrade['Version'] );
1606 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $plugin_upgrade['update']['new_version'] );
1607 \WP_CLI::line( '' );
1608 }
1609 }
1610
1611 if ( ! empty( $theme_upgrades ) ) {
1612 \WP_CLI::line( '' );
1613 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Theme Updates', 'mainwp' ) . '%n' ) );
1614 \WP_CLI::line( '' );
1615
1616 foreach ( $theme_upgrades as $theme_upgrade ) {
1617 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $theme_upgrade['Name'] );
1618 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $theme_upgrade['Version'] );
1619 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $theme_upgrade['update']['new_version'] );
1620 \WP_CLI::line( '' );
1621 }
1622 }
1623
1624 if ( ! empty( $translation_upgrades ) ) {
1625 \WP_CLI::line( '' );
1626 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Translation Updates', 'mainwp' ) . '%n' ) );
1627 \WP_CLI::line( '' );
1628
1629 foreach ( $translation_upgrades as $translation_upgrade ) {
1630 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $translation_upgrade['Name'] );
1631 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $translation_upgrade['Version'] );
1632 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $translation_upgrade['update']['new_version'] );
1633 \WP_CLI::line( '' );
1634 }
1635 }
1636 }
1637 MainWP_DB::free_result( $websites );
1638 }
1639
1640 /**
1641 * Lists all ignored plugins for a child site.
1642 *
1643 * Command Example: wp mainwp updates --ignored-plugins-updates [<websiteid>].
1644 *
1645 * @param array $args Arguments.
1646 * @param array $assoc_args Arguments.
1647 * @param object|bool $website Website object.
1648 */
1649 public static function callback_updates_ignored_plugins_updates( $args = array(), $assoc_args = array(), $website = false ) { //phpcs:ignore -- NOSONAR - complex.
1650 $ignored_all = false;
1651 if ( $website ) {
1652 \WP_CLI::line( '' );
1653 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Ignored Updates', 'mainwp' ) . '%n' ) );
1654 \WP_CLI::line( '' );
1655
1656 if ( $website->is_ignorePluginUpdates ) {
1657 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'All ignored', 'mainwp' ) . '%n' ) );
1658 $ignored_all = true;
1659 }
1660 }
1661
1662 if ( ! $ignored_all ) {
1663 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
1664 $data = json_decode( $userExtension->ignored_plugins, true );
1665 foreach ( $data as $value ) {
1666 $name = is_array( $value ) && ! empty( $value['Name'] ) ? $value['Name'] : '';
1667 $name = empty( $name ) && is_string( $value ) ? $value : $name;
1668
1669 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $name );
1670 }
1671 if ( $website ) {
1672 $ignored_plugins = json_decode( $website->ignored_plugins, true );
1673 if ( is_array( $ignored_plugins ) ) {
1674 foreach ( $ignored_plugins as $value ) {
1675 $name = is_array( $value ) && ! empty( $value['Name'] ) ? $value['Name'] : '';
1676 $name = empty( $name ) && is_string( $value ) ? $value : $name;
1677 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $name );
1678 }
1679 }
1680 }
1681 }
1682
1683 \WP_CLI::line( '' );
1684 }
1685
1686 /**
1687 * Lists all per site ignored plugin updates for a child site.
1688 *
1689 * Command Example: wp mainwp updates --site-ignored-plugins-updates [<websiteid>].
1690 *
1691 * @param array $args Arguments.
1692 * @param array $assoc_args Arguments.
1693 * @param object $website Object containing child site data.
1694 */
1695 public static function callback_updates_site_ignored_plugins_updates( $args = array(), $assoc_args = array(), $website = false ) {
1696 $data = json_decode( $website->ignored_plugins, true );
1697
1698 \WP_CLI::line( '' );
1699 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Ignored Updates', 'mainwp' ) . '%n' ) );
1700 \WP_CLI::line( '' );
1701
1702 foreach ( $data as $value ) {
1703 $name = is_array( $value ) && ! empty( $value['Name'] ) ? $value['Name'] : '';
1704 $name = empty( $name ) && is_string( $value ) ? $value : $name;
1705 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $name );
1706 }
1707
1708 \WP_CLI::line( '' );
1709 }
1710
1711 /**
1712 * Lists all ignored theme updates for a child site.
1713 *
1714 * Command Example: wp mainwp updates --ignored-themes-updates [<websiteid>].
1715 *
1716 * @param array $args Arguments.
1717 * @param array $assoc_args Arguments.
1718 * @param object $website Object containing child site data.
1719 */
1720 public static function callback_updates_ignored_themes_updates( $args = array(), $assoc_args = array(), $website = false ) {
1721
1722 $ignored_all = false;
1723 if ( $website ) {
1724 if ( $website->is_ignoreThemeUpdates ) {
1725 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'All ignored', 'mainwp' ) . '%n' ) );
1726 $ignored_all = true;
1727 }
1728 \WP_CLI::line( '' );
1729 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Ignored Updates', 'mainwp' ) . '%n' ) );
1730 \WP_CLI::line( '' );
1731 }
1732
1733 if ( ! $ignored_all ) {
1734 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
1735 $data = json_decode( $userExtension->ignored_themes, true );
1736 foreach ( $data as $value ) {
1737 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $value );
1738 }
1739 if ( $website ) {
1740 $ignored_themes = json_decode( $website->ignored_themes, true );
1741 if ( is_array( $ignored_themes ) ) {
1742 foreach ( $ignored_themes as $name ) {
1743 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $name );
1744 }
1745 }
1746 }
1747 }
1748
1749 \WP_CLI::line( '' );
1750 }
1751
1752 /**
1753 * Lists all per site ignored theme updates for a child site.
1754 *
1755 * Command Example: wp mainwp updates --site-ignored-themes-updates [<websiteid>].
1756 *
1757 * @param array $args Arguments.
1758 * @param array $assoc_args Arguments.
1759 * @param object $website Object containing child site data.
1760 */
1761 public static function callback_updates_site_ignored_themes_updates( $args = array(), $assoc_args = array(), $website = false ) {
1762
1763 $data = json_decode( $website->ignored_themes, true );
1764
1765 \WP_CLI::line( '' );
1766 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Ignored Updates', 'mainwp' ) . '%n' ) );
1767 \WP_CLI::line( '' );
1768
1769 foreach ( $data as $value ) {
1770 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $value );
1771 }
1772
1773 \WP_CLI::line( '' );
1774 }
1775
1776 /**
1777 * Ignores an update globally.
1778 *
1779 * Command Example: wp mainwp updates --ignore-updates --type=[type] --slug=[slug] --name=[name].
1780 *
1781 * type: plugin|theme
1782 *
1783 * @param array $args Arguments.
1784 * @param array $assoc_args Arguments.
1785 * @param object $website Object containing child site data.
1786 */
1787 public static function callback_updates_ignore_updates( $args = array(), $assoc_args = array(), $website = false ) {
1788
1789 $params = static::get_cli_params( $args, $assoc_args, array( 'type', 'slug', 'name' ) );
1790
1791 $type = $params['type'];
1792 $slug = $params['slug'];
1793 $name = $params['name'];
1794
1795 MainWP_Updates_Handler::ignore_plugins_themes( $type, $slug, $name );
1796
1797 \WP_CLI::line( '' );
1798 \WP_CLI::line( \WP_CLI::colorize( '%g' . $name . esc_html__( ' ignorred successfully.', 'mainwp' ) . '%n' ) );
1799 \WP_CLI::line( '' );
1800 }
1801
1802
1803 /**
1804 * Ignores an update on a child site.
1805 *
1806 * Command Example: wp mainwp updates --ignore-update [<websiteid>] --type=[type] --slug=[slug] --name=[name].
1807 *
1808 * @param array $args Arguments.
1809 * @param array $assoc_args Arguments.
1810 * @param object $website Object containing child site data.
1811 */
1812 public static function callback_updates_ignore_update( $args = array(), $assoc_args = array(), $website = false ) {
1813
1814 $params = static::get_cli_params( $args, $assoc_args, array( 'type', 'slug', 'name' ) );
1815 $type = $params['type'];
1816 $slug = $params['slug'];
1817 $name = $params['name'];
1818
1819 MainWP_Updates_Handler::ignore_plugin_theme( $type, $slug, $name, $website->id );
1820
1821 \WP_CLI::line( '' );
1822 \WP_CLI::line( \WP_CLI::colorize( '%g' . $name . esc_html__( ' ignorred successfully.', 'mainwp' ) . '%n' ) );
1823 \WP_CLI::line( '' );
1824 }
1825
1826 /**
1827 * Unignores an update.
1828 *
1829 * Command Example: wp mainwp updates --unignore-updates --type=[type] --slug=[slug].
1830 *
1831 * @param array $args Arguments.
1832 * @param array $assoc_args Arguments.
1833 * @param object $website Object containing child site data.
1834 */
1835 public static function callback_updates_unignore_updates( $args = array(), $assoc_args = array(), $website = false ) {
1836
1837 $params = static::get_cli_params( $args, $assoc_args, array( 'type', 'slug' ) );
1838 $type = $params['type'];
1839 $slug = $params['slug'];
1840
1841 MainWP_Updates_Handler::unignore_global_plugins_themes( $type, $slug );
1842
1843 \WP_CLI::line( '' );
1844 \WP_CLI::line( \WP_CLI::colorize( '%g' . $slug . esc_html__( ' unignorred successfully.', 'mainwp' ) . '%n' ) );
1845 \WP_CLI::line( '' );
1846 }
1847
1848
1849 /**
1850 * Unitnores an update on a child site.
1851 *
1852 * Command Example: wp mainwp updates --unignore-update [<websiteid>] --type=[type] --slug=[slug].
1853 *
1854 * @param array $args Arguments.
1855 * @param array $assoc_args Arguments.
1856 * @param object $website Object containing child site data.
1857 */
1858 public static function callback_updates_unignore_update( $args = array(), $assoc_args = array(), $website = false ) {
1859
1860 $params = static::get_cli_params( $args, $assoc_args, array( 'type', 'slug' ) );
1861 $type = $params['type'];
1862 $slug = $params['slug'];
1863
1864 MainWP_Updates_Handler::unignore_plugin_theme( $type, $slug, $website->id );
1865
1866 \WP_CLI::line( '' );
1867 \WP_CLI::line( \WP_CLI::colorize( '%g' . $slug . esc_html__( ' unignorred successfully.', 'mainwp' ) . '%n' ) );
1868 \WP_CLI::line( '' );
1869 }
1870
1871 /**
1872 * Syncs all child sites.
1873 *
1874 * Command Example: wp mainwp sites --sync-sites.
1875 *
1876 * @param array $args Arguments.
1877 * @param array $assoc_args Arguments.
1878 */
1879 public static function handle_sync_sites( $args = array(), $assoc_args = false ) {
1880
1881 $sites = static::get_cli_params( $args, $assoc_args, 'sites' );
1882 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
1883
1884 $warnings = 0;
1885 $errors = 0;
1886
1887 \WP_CLI::line( '' );
1888 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Syncing sites. Please wait...', 'mainwp' ) . '%n' ) );
1889 \WP_CLI::line( '' );
1890
1891 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
1892 if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
1893 continue;
1894 }
1895
1896 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' (' . $website->url . ')%n' ) );
1897
1898 try {
1899 if ( MainWP_Sync::sync_site( $website ) ) {
1900 \WP_CLI::success( esc_html__( 'Sync succeeded', 'mainwp' ) );
1901 } else {
1902 \WP_CLI::warning( esc_html__( 'Sync failed', 'mainwp' ) );
1903 ++$warnings;
1904 }
1905 } catch ( \Exception $e ) {
1906 \WP_CLI::error( esc_html__( 'Sync failed: ', 'mainwp' ) . MainWP_Error_Helper::get_console_error_message( $e ) );
1907 ++$errors;
1908 }
1909 \WP_CLI::line( '' );
1910 }
1911 MainWP_DB::free_result( $websites );
1912
1913 if ( $errors > 0 ) {
1914 \WP_CLI::error( esc_html__( 'Sync process completed with errors.', 'mainwp' ) );
1915 } elseif ( $warnings > 0 ) {
1916 \WP_CLI::warning( esc_html__( 'Sync process completed with warnings.', 'mainwp' ) );
1917 } else {
1918 \WP_CLI::success( esc_html__( 'Sync process completed successfully.', 'mainwp' ) );
1919 }
1920 \WP_CLI::line( '' );
1921 }
1922
1923
1924 /**
1925 * Prints process success message.
1926 */
1927 public static function print_process_success() {
1928 \WP_CLI::success( 'Process ran.' );
1929 }
1930
1931 /**
1932 * Prints child sites list.
1933 *
1934 * @param array $websites Array containing child sites.
1935 * @param bool $is_objs Array of objects.
1936 */
1937 public static function print_sites( $websites, $is_objs = false ) {
1938 $data = array();
1939 if ( $is_objs ) {
1940 $fields = array( 'id', 'url', 'name' );
1941 foreach ( $websites as $site_id => $website ) {
1942 $data[ $site_id ] = MainWP_Utility::map_site( $website, $fields, false );
1943 }
1944 } else {
1945 $data = $websites;
1946 }
1947
1948 $idLength = strlen( 'id' );
1949 $nameLength = strlen( 'name' );
1950 $urlLength = strlen( 'url' );
1951
1952 foreach ( $data as $site ) {
1953 if ( $idLength < strlen( $site['id'] ) ) {
1954 $idLength = strlen( $site['id'] );
1955 }
1956 if ( $nameLength < strlen( $site['name'] ) ) {
1957 $nameLength = strlen( $site['name'] );
1958 }
1959 if ( $urlLength < strlen( $site['url'] ) ) {
1960 $urlLength = strlen( $site['url'] );
1961 }
1962 }
1963
1964 \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . 's+', '', '', '', '' ) );
1965 \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $urlLength . 's |', 'id', 'name', 'url', 'version' ) );
1966 \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . 's+', '', '', '', '' ) );
1967
1968 foreach ( $data as $site ) {
1969 \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $urlLength . 's |', $site['id'], $site['name'], $site['url'] ) );
1970 }
1971
1972 \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . 's+', '', '', '', '' ) );
1973 }
1974 }
1975