PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0
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 5.0, at class/class-mainwp-wp-cli-handle.php

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