1<?php
2
3namespace WPaaS;
4
5use \WP_CLI;
6
7if ( ! defined( 'ABSPATH' ) ) {
8
9 exit;
10
11}
12
13final class CLI {
14
15 /**
16 * Class constructor.
17 */
18 public function __construct() {
19
20 $commands = [
21 'cache' => '\WPaaS\CLI\Cache',
22 'hmt' => '\WPaaS\CLI\HMT',
23 ];
24
25 foreach ( $commands as $command => $class ) {
26
27 unset( $commands[ $command ] );
28
29 $commands[ Plugin::cli_command( $command, [], false ) ] = $class;
30
31 if ( 'wpaas' !== Plugin::cli_base_command() ) {
32
33 $commands[ "wpaas {$command}" ] = $class;
34
35 }
36
37 }
38
39 // Custom subcommand for a default command
40 $commands['cron event wpaas'] = '\WPaaS\CLI\Cron_Event';
41
42 /**
43 * Filter the default custom WP-CLI commands.
44 *
45 * @since 2.0.0
46 *
47 * @var array
48 */
49 $commands = (array) apply_filters( 'wpaas_cli_commands', $commands );
50
51 $this->register( $commands );
52
53 }
54
55 /**
56 * Register custom WP-CLI commands.
57 *
58 * @param array $commands
59 *
60 * @return array|bool
61 */
62 private function register( array $commands ) {
63
64 if ( ! $commands || ! is_array( $commands ) ) {
65
66 return false;
67
68 }
69
70 $registered = [];
71
72 foreach ( $commands as $command => $class ) {
73
74 if ( ! class_exists( $class ) ) {
75
76 continue;
77
78 }
79
80 WP_CLI::add_command( $command, $class );
81
82 $registered[ $command ] = $class;
83
84 }
85
86 return ( $registered ) ? $registered : false;
87
88 }
89
90}
91