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

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

1,952 lines 82.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; // 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 = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
798
799 }
800 $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
801 if ( is_array( $ignored_plugins ) ) {
802 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
803
804 }
805 }
806
807 if ( is_array( $theme_upgrades ) && ! empty( $theme_upgrades ) ) {
808 $ignored_themes = json_decode( $website->ignored_themes, true );
809 if ( is_array( $ignored_themes ) ) {
810 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
811 }
812
813 $ignored_themes = json_decode( $userExtension->ignored_themes, true );
814 if ( is_array( $ignored_themes ) ) {
815 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
816 }
817 }
818
819 \WP_CLI::line( '' );
820 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Available Updates', 'mainwp' ) . '%n' ) );
821 \WP_CLI::line( '' );
822
823 if ( ! empty( $wp_upgrades ) ) {
824 \WP_CLI::line( '' );
825 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'WordPress Core', 'mainwp' ) . '%n' ) );
826 \WP_CLI::line( '' );
827
828 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $wp_upgrades['current'] );
829 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $wp_upgrades['new'] );
830 }
831
832 if ( ! empty( $plugin_upgrades ) ) {
833 \WP_CLI::line( '' );
834 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Plugin Updates', 'mainwp' ) . '%n' ) );
835 \WP_CLI::line( '' );
836
837 foreach ( $plugin_upgrades as $plugin_upgrade ) {
838 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $plugin_upgrade['Name'] );
839 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $plugin_upgrade['Version'] );
840 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $plugin_upgrade['update']['new_version'] );
841 \WP_CLI::line( '' );
842 }
843 }
844
845 if ( ! empty( $theme_upgrades ) ) {
846 \WP_CLI::line( '' );
847 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Theme Updates', 'mainwp' ) . '%n' ) );
848 \WP_CLI::line( '' );
849
850 foreach ( $theme_upgrades as $theme_upgrade ) {
851 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $theme_upgrade['Name'] );
852 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $theme_upgrade['Version'] );
853 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $theme_upgrade['update']['new_version'] );
854 \WP_CLI::line( '' );
855 }
856 }
857
858 if ( ! empty( $translation_upgrades ) ) {
859 \WP_CLI::line( '' );
860 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Translation Updates', 'mainwp' ) . '%n' ) );
861 \WP_CLI::line( '' );
862
863 foreach ( $translation_upgrades as $translation_upgrade ) {
864 if ( ! is_array( $translation_upgrade ) ) {
865 $translation_upgrade = array();
866 }
867 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . ( isset( $translation_upgrade['Name'] ) ? $translation_upgrade['Name'] : '' ) );
868 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . ( isset( $translation_upgrade['Version'] ) ? $translation_upgrade['Version'] : '' ) );
869 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . ( isset( $translation_upgrade['update']['new_version'] ) ? $translation_upgrade['update']['new_version'] : '' ) );
870 \WP_CLI::line( '' );
871 }
872 }
873 }
874
875 /**
876 * Returns the number of available updates for a child site.
877 *
878 * Command Example: wp mainwp site --site-available-updates-count [<websiteid>].
879 *
880 * @param array $args Arguments.
881 * @param array $assoc_args Arguments.
882 * @param object $website Object containing child site data.
883 */
884 public static function callback_site_site_available_updates_count( $args = array(), $assoc_args = array(), $website = false ) { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR - complexity.
885 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
886 $theme_upgrades = json_decode( $website->theme_upgrades, true );
887
888 if ( $website->is_ignorePluginUpdates ) {
889 $plugin_upgrades = array();
890 }
891 if ( $website->is_ignoreThemeUpdates ) {
892 $theme_upgrades = array();
893 }
894
895 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
896 if ( is_array( $plugin_upgrades ) && ! empty( $plugin_upgrades ) ) {
897 $ignored_plugins = json_decode( $website->ignored_plugins, true );
898 if ( is_array( $ignored_plugins ) ) {
899 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
900
901 }
902 $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
903 if ( is_array( $ignored_plugins ) ) {
904 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
905
906 }
907 }
908
909 if ( is_array( $theme_upgrades ) && ! empty( $theme_upgrades ) ) {
910 $ignored_themes = json_decode( $website->ignored_themes, true );
911 if ( is_array( $ignored_themes ) ) {
912 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
913 }
914
915 $ignored_themes = json_decode( $userExtension->ignored_themes, true );
916 if ( is_array( $ignored_themes ) ) {
917 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
918 }
919 }
920
921 $translations = json_decode( $website->translation_upgrades, true );
922 $wp = MainWP_DB::instance()->get_website_option( $website, 'wp_upgrades' );
923 $wp = ! empty( $wp ) ? json_decode( $wp, true ) : array();
924
925 if ( ! empty( $wp ) ) {
926 $wp = 1;
927 } else {
928 $wp = 0;
929 }
930 $total = array_merge( $plugin_upgrades, $theme_upgrades, $translations );
931 $data = array(
932 'total' => count( $total ) + $wp,
933 'wp' => $wp,
934 'plugins' => count( $plugin_upgrades ),
935 'themes' => count( $theme_upgrades ),
936 'translations' => count( $translations ),
937 );
938
939 \WP_CLI::line( '' );
940 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Updates', 'mainwp' ) . '%n' ) );
941 \WP_CLI::line( '' );
942
943 \WP_CLI::line( \WP_CLI::colorize( '%gWordPress:%n ' ) . $data['wp'] );
944 \WP_CLI::line( \WP_CLI::colorize( '%gPlugins:%n ' ) . $data['plugins'] );
945 \WP_CLI::line( \WP_CLI::colorize( '%gThemes:%n ' ) . $data['themes'] );
946 \WP_CLI::line( \WP_CLI::colorize( '%gTranslations:%n ' ) . $data['translations'] );
947 \WP_CLI::line( '' );
948 \WP_CLI::line( \WP_CLI::colorize( '%gTotal:%n ' ) . $data['total'] );
949 \WP_CLI::line( '' );
950 }
951
952 /**
953 * Lists all abandoned plugins on a child site.
954 *
955 * Command Example: wp mainwp site --site-abandoned-plugins [<websiteid>].
956 *
957 * @param array $args Arguments.
958 * @param array $assoc_args Arguments.
959 * @param object $website Object containing child site data.
960 */
961 public static function callback_site_site_abandoned_plugins( $args = array(), $assoc_args = array(), $website = false ) {
962 $plugins = MainWP_DB::instance()->get_website_option( $website, 'plugins_outdate_info' );
963 $plugins = ! empty( $plugins ) ? json_decode( $plugins, true ) : array();
964
965 \WP_CLI::line( '' );
966 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Abandoned Plugins', 'mainwp' ) . '%n' ) );
967 \WP_CLI::line( '' );
968
969 foreach ( $plugins as $plugin ) {
970 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $plugin['Name'] );
971 \WP_CLI::line( \WP_CLI::colorize( '%gURI:%n ' ) . $plugin['PluginURI'] );
972 \WP_CLI::line( \WP_CLI::colorize( '%gVersion:%n ' ) . $plugin['Version'] );
973 \WP_CLI::line( \WP_CLI::colorize( '%gLatest Update:%n ' ) . date( 'F j, Y', $plugin['last_updated'] ) ); // phpcs:ignore -- local time.
974 \WP_CLI::line( '' );
975 }
976 }
977
978 /**
979 * Returns the number of abaindoned plugins on a child site.
980 *
981 * Command Example: wp mainwp site --site-abandoned-plugins-count [<websiteid>].
982 *
983 * @param array $args Arguments.
984 * @param array $assoc_args Arguments.
985 * @param object $website Object containing child site data.
986 */
987 public static function callback_site_site_abandoned_plugins_count( $args = array(), $assoc_args = array(), $website = false ) {
988 $plugins = MainWP_DB::instance()->get_website_option( $website, 'plugins_outdate_info' );
989 $plugins = ! empty( $plugins ) ? json_decode( $plugins, true ) : array();
990
991 \WP_CLI::line( '' );
992 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . ' ' . esc_html__( 'Abandoned plugins: ', 'mainwp' ) . '%n' . count( $plugins ) ) );
993 \WP_CLI::line( '' );
994 }
995
996 /**
997 * Lists all abandoned themes on a child site.
998 *
999 * Command Example: wp mainwp site --site-abandoned-themes [<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_abandoned_themes( $args = array(), $assoc_args = array(), $website = false ) {
1006 $themes = MainWP_DB::instance()->get_website_option( $website, 'themes_outdate_info' );
1007 $themes = ! empty( $themes ) ? json_decode( $themes, true ) : array();
1008
1009 \WP_CLI::line( '' );
1010 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Abandoned Themes', 'mainwp' ) . '%n' ) );
1011 \WP_CLI::line( '' );
1012
1013 foreach ( $themes as $theme ) {
1014 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $theme['Name'] );
1015 \WP_CLI::line( \WP_CLI::colorize( '%gVersion:%n ' ) . $theme['Version'] );
1016 \WP_CLI::line( \WP_CLI::colorize( '%gLatest Update:%n ' ) . date( 'F j, Y', $theme['last_updated'] ) ); // phpcs:ignore -- local time.
1017 \WP_CLI::line( '' );
1018 }
1019 }
1020
1021 /**
1022 * Returns the number of abandoned themes on a child site.
1023 *
1024 * Command Example: wp mainwp site --site-abandoned-themes-count [<websiteid>].
1025 *
1026 * @param array $args Arguments.
1027 * @param array $assoc_args Arguments.
1028 * @param object $website Object containing child site data.
1029 */
1030 public static function callback_site_site_abandoned_themes_count( $args = array(), $assoc_args = array(), $website = false ) {
1031 $themes = MainWP_DB::instance()->get_website_option( $website, 'themes_outdate_info' );
1032 $themes = ! empty( $themes ) ? json_decode( $themes, true ) : array();
1033
1034 \WP_CLI::line( '' );
1035 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . ' ' . esc_html__( 'Abandoned themes: ', 'mainwp' ) . '%n' . count( $themes ) ) );
1036 \WP_CLI::line( '' );
1037 }
1038
1039 /**
1040 * Returns child site HTTP status.
1041 *
1042 * Command Example: wp mainwp site --site-http-status [<websiteid>].
1043 *
1044 * @param array $args Arguments.
1045 * @param array $assoc_args Arguments.
1046 * @param object $website Object containing child site data.
1047 */
1048 public static function callback_site_site_http_status( $args = array(), $assoc_args = array(), $website = false ) {
1049 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1050 MainWP_Monitoring_Handler::handle_check_website( $website );
1051 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Process ran successfully on ', 'mainwp' ) . $website->name . '%n' ) );
1052 }
1053
1054 /**
1055 * Returns child site Health score.
1056 *
1057 * Command Example: wp mainwp site --site-health-score [<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_site_health_score( $args = array(), $assoc_args = array(), $website = false ) {
1064 $website = MainWP_DB::instance()->get_website_by_id( $website->id, false, array( 'health_site_status' ) );
1065 $health_status = isset( $website->health_site_status ) ? json_decode( $website->health_site_status, true ) : array();
1066 $hstatus = MainWP_Utility::get_site_health( $health_status );
1067 $hval = $hstatus['val'];
1068 $critical = $hstatus['critical'];
1069 if ( 80 <= $hval && empty( $critical ) ) {
1070 $health_score = 'Good';
1071 } else {
1072 $health_score = 'Should be improved';
1073 }
1074
1075 \WP_CLI::line( '' );
1076 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . ' Health: %n ' ) . $health_score );
1077 \WP_CLI::line( '' );
1078 }
1079
1080 /**
1081 * Lists child site security issues.
1082 *
1083 * Command Example: wp mainwp site --site-security-issues [<websiteid>].
1084 *
1085 * @param array $args Arguments.
1086 * @param array $assoc_args Arguments.
1087 * @param object $website Object containing child site data.
1088 */
1089 public static function callback_site_site_security_issues( $args = array(), $assoc_args = array(), $website = false ) {
1090 $data = MainWP_Connect::fetch_url_authed( $website, 'security' );
1091
1092 \WP_CLI::line( '' );
1093 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Security Issues', 'mainwp' ) . '%n' ) );
1094 \WP_CLI::line( '' );
1095
1096 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Directories listing prevented:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['listing'] ? 'NO' : 'YES' ) );
1097 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'WordPress version hidden:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['wp_version'] ? 'NO' : 'YES' ) );
1098 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Really Simple Discovery meta tag removed:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['rsd'] ? 'NO' : 'YES' ) );
1099 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Windows Live Writer meta tag removed:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['wlw'] ? 'NO' : 'YES' ) );
1100 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Database error reporting disabled:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['db_reporting'] ? 'NO' : 'YES' ) );
1101 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'PHP error reporting disabled:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['php_reporting'] ? 'NO' : 'YES' ) );
1102 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Version information removed from URLs:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['versions'] ? 'NO' : 'YES' ) );
1103 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Registered version information removed from URLs:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['registered_versions'] ? 'NO' : 'YES' ) );
1104 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'readme.html removed from WordPress root:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['readme'] ? 'NO' : 'YES' ) );
1105 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Administrator username is not "admin":', 'mainwp' ) . '%n ' ) . ( 'N' === $data['admin'] ? 'NO' : 'YES' ) );
1106 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'WordPress is not up to date:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['wp_uptodate'] ? 'NO' : 'YES' ) );
1107 \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' ) );
1108 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'SSL protocol is not in place:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['sslprotocol'] ? 'NO' : 'YES' ) );
1109 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'WP Config debugging is enabled:', 'mainwp' ) . '%n ' ) . ( 'N' === $data['debug_disabled'] ? 'NO' : 'YES' ) );
1110 }
1111
1112 /**
1113 * Adds child site.
1114 *
1115 * Command Example: wp mainwp site --add-site [<websiteid>].
1116 *
1117 * @param array $args Arguments.
1118 * @param array $assoc_args Arguments.
1119 * @param object $website Object containing child site data.
1120 */
1121 public static function callback_site_add_site( $args = array(), $assoc_args = array(), $website = false ) {
1122 $fields = static::get_cli_params( $args, $assoc_args, 'add-site' );
1123 $data = MainWP_Manage_Sites_Handler::rest_api_add_site( $fields );
1124 if ( is_array( $data ) && ! empty( $data['siteid'] ) ) {
1125 \WP_CLI::success( ' -> Add site result: ' . print_r( $data, true ) ); // phpcs:ignore -- for cli result.
1126 } else {
1127 \WP_CLI::error( ' -> Add site result: ' . print_r( $data, true ) ); // phpcs:ignore -- for cli result.
1128 }
1129 }
1130
1131 /**
1132 * Edits child site.
1133 *
1134 * Command Example: wp mainwp site --edit-site [<websiteid>].
1135 *
1136 * @param array $args Arguments.
1137 * @param array $assoc_args Arguments.
1138 * @param object $website Object containing child site data.
1139 */
1140 public static function callback_site_edit_site( $args = array(), $assoc_args = array(), $website = false ) {
1141 $fields = static::get_cli_params( $args, $assoc_args, 'edit-site' );
1142 $data = MainWP_DB_Common::instance()->rest_api_update_website( $website->id, $fields );
1143 $website = MainWP_DB::instance()->get_website_by_id( $website->id );
1144 \WP_CLI::line( ' -> ' . $website->name . ' (' . $website->url . ')' );
1145 \WP_CLI::line( ' -> Edit site result: ' . ( $data ? 'successed' : 'failed' ) );
1146 }
1147
1148 /**
1149 * Syncs child site.
1150 *
1151 * Command Example: wp mainwp site --sync-site [<websiteid>].
1152 *
1153 * @param array $args Arguments.
1154 * @param array $assoc_args Arguments.
1155 * @param object $website Object containing child site data.
1156 */
1157 public static function callback_site_sync_site( $args = array(), $assoc_args = array(), $website = false ) {
1158 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1159 $error = false;
1160 try {
1161 MainWP_Sync::sync_site( $website );
1162 } catch ( \Exception $e ) {
1163 $error = $e->getMessage();
1164 }
1165
1166 if ( empty( $error ) ) {
1167 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' synced successfully.', 'mainwp' ) . '%n' ) );
1168 } else {
1169 \WP_CLI::line( \WP_CLI::colorize( '%r' . esc_html__( 'Process failed with error:', 'mainwp' ) . '%n' ) );
1170 \WP_CLI::line( $error );
1171 }
1172 }
1173
1174 /**
1175 * Reconnects child site.
1176 *
1177 * Command Example: wp mainwp site --reconnect-site [<websiteid>].
1178 *
1179 * @param array $args Arguments.
1180 * @param array $assoc_args Arguments.
1181 * @param object $website Object containing child site data.
1182 */
1183 public static function callback_site_reconnect_site( $args = array(), $assoc_args = array(), $website = false ) {
1184 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1185 $error = false;
1186 try {
1187 MainWP_Manage_Sites_View::m_reconnect_site( $website );
1188 } catch ( \Exception $e ) {
1189 $error = $e->getMessage();
1190 }
1191
1192 if ( empty( $error ) ) {
1193 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' reconnected successfully.', 'mainwp' ) . '%n' ) );
1194 } else {
1195 \WP_CLI::line( \WP_CLI::colorize( '%r' . esc_html__( 'Process failed with error:', 'mainwp' ) . '%n' ) );
1196 \WP_CLI::line( $error );
1197 }
1198 }
1199
1200 /**
1201 * Disconnects child site.
1202 *
1203 * Command Example: wp mainwp site --disconnect-site [<websiteid>].
1204 *
1205 * @param array $args Arguments.
1206 * @param array $assoc_args Arguments.
1207 * @param object $website Object containing child site data.
1208 */
1209 public static function callback_site_disconnect_site( $args = array(), $assoc_args = array(), $website = false ) {
1210 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1211
1212 $error = false;
1213 try {
1214 MainWP_Connect::fetch_url_authed( $website, 'disconnect' );
1215 } catch ( \Exception $e ) {
1216 $error = $e->getMessage();
1217 }
1218
1219 if ( empty( $error ) ) {
1220 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' disconnected successfully.', 'mainwp' ) . '%n' ) );
1221 } else {
1222 \WP_CLI::line( \WP_CLI::colorize( '%r' . esc_html__( 'Process failed with error:', 'mainwp' ) . '%n' ) );
1223 \WP_CLI::line( $error );
1224 }
1225 }
1226
1227 /**
1228 * Removes child site from the MainWP Dashboard.
1229 *
1230 * Command Example: wp mainwp site --remove-site [<websiteid>].
1231 *
1232 * @param array $args Arguments.
1233 * @param array $assoc_args Arguments.
1234 * @param object $website Object containing child site data.
1235 */
1236 public static function callback_site_remove_site( $args = array(), $assoc_args = array(), $website = false ) {
1237 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1238 MainWP_Manage_Sites_Handler::remove_website( $website->id );
1239 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'Site removed successfully.', 'mainwp' ) . '%n' ) );
1240 }
1241
1242 /**
1243 * Updates WP Core on a child site.
1244 *
1245 * Command Example: wp mainwp site --site-update-wordpress [<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_wordpress( $args = array(), $assoc_args = array(), $website = false ) {
1252 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1253 try {
1254 MainWP_Updates_Handler::upgrade_website( $website );
1255 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' updated successfully.', 'mainwp' ) . '%n' ) );
1256 } catch ( \Exception $e ) {
1257 \WP_CLI::error( 'Updates failed: ' . MainWP_Error_Helper::get_console_error_message( $e ) );
1258 if ( $e->getMesage() === 'WPERROR' ) {
1259 \WP_CLI::debug( 'Error: ' . MainWP_Utility::value_to_string( $e->get_message_extra(), 1 ) );
1260 }
1261 }
1262 }
1263
1264 /**
1265 * Updates all plugins on a child site.
1266 *
1267 * Command Example: wp mainwp site --site-update-plugins [<websiteid>].
1268 *
1269 * @param array $args Arguments.
1270 * @param array $assoc_args Arguments.
1271 * @param object $website Object containing child site data.
1272 */
1273 public static function callback_site_site_update_plugins( $args = array(), $assoc_args = array(), $website = false ) {
1274 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1275
1276 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
1277
1278 if ( $website->is_ignorePluginUpdates ) {
1279 $plugin_upgrades = array();
1280 }
1281
1282 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
1283 if ( is_array( $plugin_upgrades ) && ! empty( $plugin_upgrades ) ) {
1284 $ignored_plugins = json_decode( $website->ignored_plugins, true );
1285 if ( is_array( $ignored_plugins ) ) {
1286 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
1287
1288 }
1289 $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
1290 if ( is_array( $ignored_plugins ) ) {
1291 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
1292
1293 }
1294 }
1295
1296 $slugs = array();
1297 foreach ( $plugin_upgrades as $slug => $plugin ) {
1298 $slugs[] = $slug;
1299 }
1300 $list = urldecode( implode( ',', $slugs ) );
1301
1302 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' plugins updated successfully.', 'mainwp' ) . '%n' ) );
1303
1304 try {
1305 MainWP_Connect::fetch_url_authed(
1306 $website,
1307 'upgradeplugintheme',
1308 array(
1309 'type' => 'plugin',
1310 'list' => $list,
1311 )
1312 );
1313 } catch ( \Exception $e ) {
1314 MainWP_Error_Helper::get_console_error_message( $e );
1315 }
1316 }
1317
1318 /**
1319 * Updates all themes on a child site.
1320 *
1321 * Command Example: wp mainwp site --site-update-themes [<websiteid>].
1322 *
1323 * @param array $args Arguments.
1324 * @param array $assoc_args Arguments.
1325 * @param object $website Object containing child site data.
1326 */
1327 public static function callback_site_site_update_themes( $args = array(), $assoc_args = array(), $website = false ) {
1328 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1329 $theme_upgrades = json_decode( $website->theme_upgrades, true );
1330 $slugs = array();
1331 foreach ( $theme_upgrades as $slug => $theme ) {
1332 $slugs[] = $slug;
1333 }
1334 $list = urldecode( implode( ',', $slugs ) );
1335
1336 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' themes updated successfully.', 'mainwp' ) . '%n' ) );
1337
1338 try {
1339 MainWP_Connect::fetch_url_authed(
1340 $website,
1341 'upgradeplugintheme',
1342 array(
1343 'type' => 'theme',
1344 'list' => $list,
1345 )
1346 );
1347 } catch ( \Exception $e ) {
1348 MainWP_Error_Helper::get_console_error_message( $e );
1349 }
1350 }
1351
1352 /**
1353 * Updates translations on a child site.
1354 *
1355 * Command Example: wp mainwp site --site-update-translations [<websiteid>].
1356 *
1357 * @param array $args Arguments.
1358 * @param array $assoc_args Arguments.
1359 * @param object $website Object containing child site data.
1360 */
1361 public static function callback_site_site_update_translations( $args = array(), $assoc_args = array(), $website = false ) {
1362 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1363
1364 $translation_upgrades = json_decode( $website->translation_upgrades, true );
1365 $slugs = array();
1366 foreach ( $translation_upgrades as $translation_upgrade ) {
1367 $slugs[] = $translation_upgrade['slug'];
1368 }
1369 $list = urldecode( implode( ',', $slugs ) );
1370
1371 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' translations updated successfully.', 'mainwp' ) . '%n' ) );
1372
1373 try {
1374 MainWP_Connect::fetch_url_authed(
1375 $website,
1376 'upgradetranslation',
1377 array(
1378 'type' => 'translation',
1379 'list' => $list,
1380 )
1381 );
1382 } catch ( \Exception $e ) {
1383 MainWP_Error_Helper::get_console_error_message( $e );
1384 }
1385 }
1386
1387 /**
1388 * Updates single item on a child site.
1389 *
1390 * Command Example: wp mainwp site --site-update-item [<websiteid>] --type=[type] --slug=[slug].
1391 *
1392 * @param array $args Arguments.
1393 * @param array $assoc_args Arguments.
1394 * @param object $website Object containing child site data.
1395 */
1396 public static function callback_site_site_update_item( $args = array(), $assoc_args = array(), $website = false ) {
1397
1398 $params = static::get_cli_params( $args, $assoc_args, array( 'type', 'slug' ) );
1399
1400 $type = $params['type'];
1401 $slug = $params['slug'];
1402
1403 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1404
1405 try {
1406 MainWP_Connect::fetch_url_authed(
1407 $website,
1408 'upgradeplugintheme',
1409 array(
1410 'type' => $type,
1411 'list' => urldecode( $slug ),
1412 )
1413 );
1414 } catch ( \Exception $e ) {
1415 MainWP_Error_Helper::get_console_error_message( $e );
1416 }
1417
1418 \WP_CLI::line( \WP_CLI::colorize( '%g' . $website->name . esc_html__( ' item updated successfully.', 'mainwp' ) . '%n' ) );
1419 }
1420
1421 /**
1422 * Manages plgins on a child site.
1423 *
1424 * Command Example: wp mainwp site --site-manage-plugin [<websiteid>] --action=[action] --plugin=[plugin].
1425 *
1426 * Action: activate|deactivate|delete
1427 * Plugin: plugin slugs separated by ||.
1428 *
1429 * @param array $args Arguments.
1430 * @param array $assoc_args Arguments.
1431 * @param object $website Object containing child site data.
1432 */
1433 public static function callback_site_site_manage_plugin( $args = array(), $assoc_args = array(), $website = false ) {
1434 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1435
1436 $params = static::get_cli_params( $args, $assoc_args, array( 'plugin', 'action' ) );
1437 $plugin = $params['plugin'];
1438 $action = $params['action'];
1439
1440 \WP_CLI::line( \WP_CLI::colorize( '%g' . $plugin . ' ' . $action . 'd' . esc_html__( ' successfully.', 'mainwp' ) . '%n' ) );
1441
1442 try {
1443 MainWP_Connect::fetch_url_authed(
1444 $website,
1445 'plugin_action',
1446 array(
1447 'action' => $action,
1448 'plugin' => $plugin,
1449 )
1450 );
1451 } catch ( \Exception $e ) {
1452 // ok.
1453 }
1454 }
1455
1456 /**
1457 * Manages themes on a child site.
1458 *
1459 * Command Example: wp mainwp site --site-manage-theme [<websiteid>] --action=[action] --theme=[theme].
1460 *
1461 * action: activate|delete.
1462 * theme: theme name.
1463 *
1464 * @param array $args Arguments.
1465 * @param array $assoc_args Arguments.
1466 * @param object $website Object containing child site data.
1467 */
1468 public static function callback_site_site_manage_theme( $args = array(), $assoc_args = array(), $website = false ) {
1469 \WP_CLI::line( esc_html__( 'Please wait... ', 'mainwp' ) );
1470
1471 $params = static::get_cli_params( $args, $assoc_args, array( 'theme', 'action' ) );
1472 $theme = $params['theme'];
1473 $action = $params['action'];
1474
1475 \WP_CLI::line( \WP_CLI::colorize( '%g' . $theme . ' ' . $action . 'd' . esc_html__( ' successfully.', 'mainwp' ) . '%n' ) );
1476
1477 try {
1478 MainWP_Connect::fetch_url_authed(
1479 $website,
1480 'theme_action',
1481 array(
1482 'action' => $action,
1483 'theme' => $theme,
1484 )
1485 );
1486 } catch ( \Exception $e ) {
1487 // ok.
1488 }
1489 }
1490
1491 /**
1492 * Checks child site for HTTP Status.
1493 *
1494 * Command Example: wp mainwp site --check-site-http-status [<websiteid>].
1495 *
1496 * @param array $args Arguments.
1497 * @param array $assoc_args Arguments.
1498 * @param object $website Object containing child site data.
1499 */
1500 public static function callback_site_check_site_http_status( $args = array(), $assoc_args = array(), $website = false ) {
1501 \WP_CLI::line( '' );
1502 \WP_CLI::line( esc_html__( 'Checking ', 'mainwp' ) . $website->name . ' (' . $website->url . '). ' . esc_html__( 'Please wait... ', 'mainwp' ) );
1503 \WP_CLI::line( '' );
1504 $data = MainWP_Monitoring_Handler::handle_check_website( $website );
1505 \WP_CLI::line( \WP_CLI::colorize( '%g' . esc_html__( 'HTTP Status: ', 'mainwp' ) . '%n' . $data['httpCode'] . ' (' . $data['httpCodeString'] . ')' ) );
1506 \WP_CLI::line( '' );
1507 }
1508
1509 /**
1510 * Lists all available update for all sites.
1511 *
1512 * Command Example: wp mainwp updates --available-updates [<websiteid>].
1513 *
1514 * @param array $args Arguments.
1515 * @param array $assoc_args Arguments.
1516 */
1517 public static function callback_updates_available_updates( $args = array(), $assoc_args = array() ) { // phpcs:ignore -- NOSONAR - complex.
1518
1519 \WP_CLI::line( '' );
1520 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Updates', 'mainwp' ) . '%n' ) );
1521 \WP_CLI::line( '' );
1522
1523 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
1524 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
1525 $wp_upgrades = MainWP_DB::instance()->get_website_option( $website, 'wp_upgrades' );
1526 $wp_upgrades = ! empty( $wp_upgrades ) ? json_decode( $wp_upgrades, true ) : array();
1527
1528 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
1529 $theme_upgrades = json_decode( $website->theme_upgrades, true );
1530 $translation_upgrades = json_decode( $website->translation_upgrades, true );
1531
1532 if ( $website->is_ignorePluginUpdates ) {
1533 $plugin_upgrades = array();
1534 }
1535 if ( $website->is_ignoreThemeUpdates ) {
1536 $theme_upgrades = array();
1537 }
1538
1539 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
1540 if ( is_array( $plugin_upgrades ) && ! empty( $plugin_upgrades ) ) {
1541 $ignored_plugins = json_decode( $website->ignored_plugins, true );
1542 if ( is_array( $ignored_plugins ) ) {
1543 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
1544
1545 }
1546 $ignored_plugins = json_decode( $userExtension->ignored_plugins, true );
1547 if ( is_array( $ignored_plugins ) ) {
1548 $plugin_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $plugin_upgrades, $ignored_plugins );
1549
1550 }
1551 }
1552
1553 if ( is_array( $theme_upgrades ) && ! empty( $theme_upgrades ) ) {
1554 $ignored_themes = json_decode( $website->ignored_themes, true );
1555 if ( is_array( $ignored_themes ) ) {
1556 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
1557 }
1558
1559 $ignored_themes = json_decode( $userExtension->ignored_themes, true );
1560 if ( is_array( $ignored_themes ) ) {
1561 $theme_upgrades = MainWP_Common_Functions::instance()->get_not_ignored_updates_themesplugins( $theme_upgrades, $ignored_themes );
1562 }
1563 }
1564
1565 \WP_CLI::line( \WP_CLI::colorize( '%B' . $website->name . ' (' . $website->url . ')%n' ) );
1566 if ( ! empty( $wp_upgrades ) ) {
1567 \WP_CLI::line( '' );
1568 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'WordPress Core', 'mainwp' ) . '%n' ) );
1569 \WP_CLI::line( '' );
1570
1571 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $wp_upgrades['current'] );
1572 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $wp_upgrades['new'] );
1573 }
1574
1575 if ( ! empty( $plugin_upgrades ) ) {
1576 \WP_CLI::line( '' );
1577 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Plugin Updates', 'mainwp' ) . '%n' ) );
1578 \WP_CLI::line( '' );
1579
1580 foreach ( $plugin_upgrades as $plugin_upgrade ) {
1581 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $plugin_upgrade['Name'] );
1582 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $plugin_upgrade['Version'] );
1583 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $plugin_upgrade['update']['new_version'] );
1584 \WP_CLI::line( '' );
1585 }
1586 }
1587
1588 if ( ! empty( $theme_upgrades ) ) {
1589 \WP_CLI::line( '' );
1590 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Theme Updates', 'mainwp' ) . '%n' ) );
1591 \WP_CLI::line( '' );
1592
1593 foreach ( $theme_upgrades as $theme_upgrade ) {
1594 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $theme_upgrade['Name'] );
1595 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $theme_upgrade['Version'] );
1596 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $theme_upgrade['update']['new_version'] );
1597 \WP_CLI::line( '' );
1598 }
1599 }
1600
1601 if ( ! empty( $translation_upgrades ) ) {
1602 \WP_CLI::line( '' );
1603 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Available Translation Updates', 'mainwp' ) . '%n' ) );
1604 \WP_CLI::line( '' );
1605
1606 foreach ( $translation_upgrades as $translation_upgrade ) {
1607 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $translation_upgrade['Name'] );
1608 \WP_CLI::line( \WP_CLI::colorize( '%gDetected:%n ' ) . $translation_upgrade['Version'] );
1609 \WP_CLI::line( \WP_CLI::colorize( '%gLatest:%n ' ) . $translation_upgrade['update']['new_version'] );
1610 \WP_CLI::line( '' );
1611 }
1612 }
1613 }
1614 MainWP_DB::free_result( $websites );
1615 }
1616
1617 /**
1618 * Lists all ignored plugins for a child site.
1619 *
1620 * Command Example: wp mainwp updates --ignored-plugins-updates [<websiteid>].
1621 *
1622 * @param array $args Arguments.
1623 * @param array $assoc_args Arguments.
1624 * @param object|bool $website Website object.
1625 */
1626 public static function callback_updates_ignored_plugins_updates( $args = array(), $assoc_args = array(), $website = false ) { //phpcs:ignore -- NOSONAR - complex.
1627 $ignored_all = false;
1628 if ( $website ) {
1629 \WP_CLI::line( '' );
1630 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Ignored Updates', 'mainwp' ) . '%n' ) );
1631 \WP_CLI::line( '' );
1632
1633 if ( $website->is_ignorePluginUpdates ) {
1634 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'All ignored', 'mainwp' ) . '%n' ) );
1635 $ignored_all = true;
1636 }
1637 }
1638
1639 if ( ! $ignored_all ) {
1640 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
1641 $data = json_decode( $userExtension->ignored_plugins, true );
1642 foreach ( $data as $value ) {
1643 $name = is_array( $value ) && ! empty( $value['Name'] ) ? $value['Name'] : '';
1644 $name = empty( $name ) && is_string( $value ) ? $value : $name;
1645
1646 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $name );
1647 }
1648 if ( $website ) {
1649 $ignored_plugins = json_decode( $website->ignored_plugins, true );
1650 if ( is_array( $ignored_plugins ) ) {
1651 foreach ( $ignored_plugins as $value ) {
1652 $name = is_array( $value ) && ! empty( $value['Name'] ) ? $value['Name'] : '';
1653 $name = empty( $name ) && is_string( $value ) ? $value : $name;
1654 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $name );
1655 }
1656 }
1657 }
1658 }
1659
1660 \WP_CLI::line( '' );
1661 }
1662
1663 /**
1664 * Lists all per site ignored plugin updates for a child site.
1665 *
1666 * Command Example: wp mainwp updates --site-ignored-plugins-updates [<websiteid>].
1667 *
1668 * @param array $args Arguments.
1669 * @param array $assoc_args Arguments.
1670 * @param object $website Object containing child site data.
1671 */
1672 public static function callback_updates_site_ignored_plugins_updates( $args = array(), $assoc_args = array(), $website = false ) {
1673 $data = json_decode( $website->ignored_plugins, true );
1674
1675 \WP_CLI::line( '' );
1676 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Ignored Updates', 'mainwp' ) . '%n' ) );
1677 \WP_CLI::line( '' );
1678
1679 foreach ( $data as $value ) {
1680 $name = is_array( $value ) && ! empty( $value['Name'] ) ? $value['Name'] : '';
1681 $name = empty( $name ) && is_string( $value ) ? $value : $name;
1682 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $name );
1683 }
1684
1685 \WP_CLI::line( '' );
1686 }
1687
1688 /**
1689 * Lists all ignored theme updates for a child site.
1690 *
1691 * Command Example: wp mainwp updates --ignored-themes-updates [<websiteid>].
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_ignored_themes_updates( $args = array(), $assoc_args = array(), $website = false ) {
1698
1699 $ignored_all = false;
1700 if ( $website ) {
1701 if ( $website->is_ignoreThemeUpdates ) {
1702 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'All ignored', 'mainwp' ) . '%n' ) );
1703 $ignored_all = true;
1704 }
1705 \WP_CLI::line( '' );
1706 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Ignored Updates', 'mainwp' ) . '%n' ) );
1707 \WP_CLI::line( '' );
1708 }
1709
1710 if ( ! $ignored_all ) {
1711 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
1712 $data = json_decode( $userExtension->ignored_themes, true );
1713 foreach ( $data as $value ) {
1714 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $value );
1715 }
1716 if ( $website ) {
1717 $ignored_themes = json_decode( $website->ignored_themes, true );
1718 if ( is_array( $ignored_themes ) ) {
1719 foreach ( $ignored_themes as $name ) {
1720 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $name );
1721 }
1722 }
1723 }
1724 }
1725
1726 \WP_CLI::line( '' );
1727 }
1728
1729 /**
1730 * Lists all per site ignored theme updates for a child site.
1731 *
1732 * Command Example: wp mainwp updates --site-ignored-themes-updates [<websiteid>].
1733 *
1734 * @param array $args Arguments.
1735 * @param array $assoc_args Arguments.
1736 * @param object $website Object containing child site data.
1737 */
1738 public static function callback_updates_site_ignored_themes_updates( $args = array(), $assoc_args = array(), $website = false ) {
1739
1740 $data = json_decode( $website->ignored_themes, true );
1741
1742 \WP_CLI::line( '' );
1743 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' ' . esc_html__( 'Ignored Updates', 'mainwp' ) . '%n' ) );
1744 \WP_CLI::line( '' );
1745
1746 foreach ( $data as $value ) {
1747 \WP_CLI::line( \WP_CLI::colorize( '%gName:%n ' ) . $value );
1748 }
1749
1750 \WP_CLI::line( '' );
1751 }
1752
1753 /**
1754 * Ignores an update globally.
1755 *
1756 * Command Example: wp mainwp updates --ignore-updates --type=[type] --slug=[slug] --name=[name].
1757 *
1758 * type: plugin|theme
1759 *
1760 * @param array $args Arguments.
1761 * @param array $assoc_args Arguments.
1762 * @param object $website Object containing child site data.
1763 */
1764 public static function callback_updates_ignore_updates( $args = array(), $assoc_args = array(), $website = false ) {
1765
1766 $params = static::get_cli_params( $args, $assoc_args, array( 'type', 'slug', 'name' ) );
1767
1768 $type = $params['type'];
1769 $slug = $params['slug'];
1770 $name = $params['name'];
1771
1772 MainWP_Updates_Handler::ignore_plugins_themes( $type, $slug, $name );
1773
1774 \WP_CLI::line( '' );
1775 \WP_CLI::line( \WP_CLI::colorize( '%g' . $name . esc_html__( ' ignorred successfully.', 'mainwp' ) . '%n' ) );
1776 \WP_CLI::line( '' );
1777 }
1778
1779
1780 /**
1781 * Ignores an update on a child site.
1782 *
1783 * Command Example: wp mainwp updates --ignore-update [<websiteid>] --type=[type] --slug=[slug] --name=[name].
1784 *
1785 * @param array $args Arguments.
1786 * @param array $assoc_args Arguments.
1787 * @param object $website Object containing child site data.
1788 */
1789 public static function callback_updates_ignore_update( $args = array(), $assoc_args = array(), $website = false ) {
1790
1791 $params = static::get_cli_params( $args, $assoc_args, array( 'type', 'slug', 'name' ) );
1792 $type = $params['type'];
1793 $slug = $params['slug'];
1794 $name = $params['name'];
1795
1796 MainWP_Updates_Handler::ignore_plugin_theme( $type, $slug, $name, $website->id );
1797
1798 \WP_CLI::line( '' );
1799 \WP_CLI::line( \WP_CLI::colorize( '%g' . $name . esc_html__( ' ignorred successfully.', 'mainwp' ) . '%n' ) );
1800 \WP_CLI::line( '' );
1801 }
1802
1803 /**
1804 * Unignores an update.
1805 *
1806 * Command Example: wp mainwp updates --unignore-updates --type=[type] --slug=[slug].
1807 *
1808 * @param array $args Arguments.
1809 * @param array $assoc_args Arguments.
1810 * @param object $website Object containing child site data.
1811 */
1812 public static function callback_updates_unignore_updates( $args = array(), $assoc_args = array(), $website = false ) {
1813
1814 $params = static::get_cli_params( $args, $assoc_args, array( 'type', 'slug' ) );
1815 $type = $params['type'];
1816 $slug = $params['slug'];
1817
1818 MainWP_Updates_Handler::unignore_global_plugins_themes( $type, $slug );
1819
1820 \WP_CLI::line( '' );
1821 \WP_CLI::line( \WP_CLI::colorize( '%g' . $slug . esc_html__( ' unignorred successfully.', 'mainwp' ) . '%n' ) );
1822 \WP_CLI::line( '' );
1823 }
1824
1825
1826 /**
1827 * Unitnores an update on a child site.
1828 *
1829 * Command Example: wp mainwp updates --unignore-update [<websiteid>] --type=[type] --slug=[slug].
1830 *
1831 * @param array $args Arguments.
1832 * @param array $assoc_args Arguments.
1833 * @param object $website Object containing child site data.
1834 */
1835 public static function callback_updates_unignore_update( $args = array(), $assoc_args = array(), $website = false ) {
1836
1837 $params = static::get_cli_params( $args, $assoc_args, array( 'type', 'slug' ) );
1838 $type = $params['type'];
1839 $slug = $params['slug'];
1840
1841 MainWP_Updates_Handler::unignore_plugin_theme( $type, $slug, $website->id );
1842
1843 \WP_CLI::line( '' );
1844 \WP_CLI::line( \WP_CLI::colorize( '%g' . $slug . esc_html__( ' unignorred successfully.', 'mainwp' ) . '%n' ) );
1845 \WP_CLI::line( '' );
1846 }
1847
1848 /**
1849 * Syncs all child sites.
1850 *
1851 * Command Example: wp mainwp sites --sync-sites.
1852 *
1853 * @param array $args Arguments.
1854 * @param array $assoc_args Arguments.
1855 */
1856 public static function handle_sync_sites( $args = array(), $assoc_args = false ) {
1857
1858 $sites = static::get_cli_params( $args, $assoc_args, 'sites' );
1859 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
1860
1861 $warnings = 0;
1862 $errors = 0;
1863
1864 \WP_CLI::line( '' );
1865 \WP_CLI::line( \WP_CLI::colorize( '%9' . esc_html__( 'Syncing sites. Please wait...', 'mainwp' ) . '%n' ) );
1866 \WP_CLI::line( '' );
1867
1868 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
1869 if ( ! empty( $sites ) && ( ! in_array( $website->id, $sites, true ) ) ) {
1870 continue;
1871 }
1872
1873 \WP_CLI::line( \WP_CLI::colorize( '%9' . $website->name . ' (' . $website->url . ')%n' ) );
1874
1875 try {
1876 if ( MainWP_Sync::sync_site( $website ) ) {
1877 \WP_CLI::success( esc_html__( 'Sync succeeded', 'mainwp' ) );
1878 } else {
1879 \WP_CLI::warning( esc_html__( 'Sync failed', 'mainwp' ) );
1880 ++$warnings;
1881 }
1882 } catch ( \Exception $e ) {
1883 \WP_CLI::error( esc_html__( 'Sync failed: ', 'mainwp' ) . MainWP_Error_Helper::get_console_error_message( $e ) );
1884 ++$errors;
1885 }
1886 \WP_CLI::line( '' );
1887 }
1888 MainWP_DB::free_result( $websites );
1889
1890 if ( $errors > 0 ) {
1891 \WP_CLI::error( esc_html__( 'Sync process completed with errors.', 'mainwp' ) );
1892 } elseif ( $warnings > 0 ) {
1893 \WP_CLI::warning( esc_html__( 'Sync process completed with warnings.', 'mainwp' ) );
1894 } else {
1895 \WP_CLI::success( esc_html__( 'Sync process completed successfully.', 'mainwp' ) );
1896 }
1897 \WP_CLI::line( '' );
1898 }
1899
1900
1901 /**
1902 * Prints process success message.
1903 */
1904 public static function print_process_success() {
1905 \WP_CLI::success( 'Process ran.' );
1906 }
1907
1908 /**
1909 * Prints child sites list.
1910 *
1911 * @param array $websites Array containing child sites.
1912 * @param bool $is_objs Array of objects.
1913 */
1914 public static function print_sites( $websites, $is_objs = false ) {
1915 $data = array();
1916 if ( $is_objs ) {
1917 $fields = array( 'id', 'url', 'name' );
1918 foreach ( $websites as $site_id => $website ) {
1919 $data[ $site_id ] = MainWP_Utility::map_site( $website, $fields, false );
1920 }
1921 } else {
1922 $data = $websites;
1923 }
1924
1925 $idLength = strlen( 'id' );
1926 $nameLength = strlen( 'name' );
1927 $urlLength = strlen( 'url' );
1928
1929 foreach ( $data as $site ) {
1930 if ( $idLength < strlen( $site['id'] ) ) {
1931 $idLength = strlen( $site['id'] );
1932 }
1933 if ( $nameLength < strlen( $site['name'] ) ) {
1934 $nameLength = strlen( $site['name'] );
1935 }
1936 if ( $urlLength < strlen( $site['url'] ) ) {
1937 $urlLength = strlen( $site['url'] );
1938 }
1939 }
1940
1941 \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . 's+', '', '', '', '' ) );
1942 \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $urlLength . 's |', 'id', 'name', 'url', 'version' ) );
1943 \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . 's+', '', '', '', '' ) );
1944
1945 foreach ( $data as $site ) {
1946 \WP_CLI::line( sprintf( '| %-' . $idLength . 's | %-' . $nameLength . 's | %-' . $urlLength . 's |', $site['id'], $site['name'], $site['url'] ) );
1947 }
1948
1949 \WP_CLI::line( sprintf( "+%'--" . ( $idLength + 2 ) . "s+%'--" . ( $nameLength + 2 ) . "s+%'--" . ( $urlLength + 2 ) . 's+', '', '', '', '' ) );
1950 }
1951 }
1952