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

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

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