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

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