Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
NFD_WPCLI
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 8
420
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validate_required_string
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 add_command
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
42
 register_commands
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
30
 warning
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 error
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 success
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 is_executing_wp_cli
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace NewfoldLabs\WP\Module\Performance;
4
5use WP_CLI;
6
7/**
8 * Manages all "nfd" WP-CLI commands.
9 */
10class NFD_WPCLI {
11
12    /**
13     * Holds registered commands.
14     *
15     * Array structure:
16     * array(
17     *     'namespace' => array(
18     *         'command' => 'handler',
19     *         // ...
20     *     ),
21     *     // ...
22     * )
23     *
24     * @var array
25     */
26    private static $commands = array();
27
28    /**
29     * Constructor. Hooks into cli_init.
30     */
31    public function __construct() {
32        add_action( 'cli_init', array( $this, 'register_commands' ) );
33    }
34
35    /**
36     * Validates that a given value is a non-empty string.
37     *
38     * @param mixed  $value The value to validate.
39     * @param string $field The field name (used for error messages).
40     *
41     * @return true|\WP_Error True if valid, or WP_Error on failure.
42     */
43    private static function validate_required_string( $value, $field ) {
44        if ( ! is_string( $value ) || empty( $value ) ) {
45            return new \WP_Error(
46                'nfd_cli_error',
47                sprintf(
48                    /* translators: %s is the name of the field that is invalid (e.g., 'namespace', 'command', or 'handler'). */
49                    __( 'Newfold CLI: Invalid %s provided to NFD_WPCLI::add_command().', 'wp-module-performance' ),
50                    $field
51                )
52            );
53        }
54        return true;
55    }
56
57    /**
58     * Registers a WP-CLI command under "wp nfd".
59     *
60     * @param string $cmd_namespace The command namespace (e.g., "performance").
61     * @param string $command       The command string.
62     * @param string $handler       The handler class that implements the command.
63     *
64     * @return true|\WP_Error True on success, or WP_Error on failure.
65     */
66    public static function add_command( $cmd_namespace, $command, $handler ) {
67        $error = self::validate_required_string( $cmd_namespace, 'namespace' );
68        if ( is_wp_error( $error ) ) {
69            return $error;
70        }
71
72        $error = self::validate_required_string( $command, 'command' );
73        if ( is_wp_error( $error ) ) {
74            return $error;
75        }
76
77        $error = self::validate_required_string( $handler, 'handler' );
78        if ( is_wp_error( $error ) ) {
79            return $error;
80        }
81
82        if ( ! class_exists( $handler ) ) {
83            return new \WP_Error(
84                'nfd_cli_error',
85                sprintf(
86                    /* translators: %s is the name of the handler class. */
87                    __( 'Newfold CLI: The handler class %s does not exist.', 'wp-module-performance' ),
88                    $handler
89                )
90            );
91        }
92
93        if ( ! isset( self::$commands[ $cmd_namespace ] ) ) {
94            self::$commands[ $cmd_namespace ] = array();
95        }
96
97        self::$commands[ $cmd_namespace ][ $command ] = $handler;
98
99        return true;
100    }
101
102    /**
103     * Hooks into WP-CLI and registers all commands.
104     *
105     * @return void
106     */
107    public function register_commands() {
108        if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) {
109            return;
110        }
111
112        foreach ( self::$commands as $cmd_namespace => $commands ) {
113            foreach ( $commands as $command => $handler ) {
114                WP_CLI::add_command( "nfd {$cmd_namespace} {$command}", $handler );
115            }
116        }
117    }
118
119    /**
120     * Outputs a warning message with "Newfold CLI:" prefix.
121     *
122     * @param string $message Message to output.
123     *
124     * @return void
125     */
126    public static function warning( $message ) {
127        WP_CLI::warning( 'Newfold CLI: ' . $message );
128    }
129
130    /**
131     * Outputs an error message with "Newfold CLI:" prefix.
132     *
133     * @param string $message Message to output.
134     *
135     * @return void
136     */
137    public static function error( $message ) {
138        WP_CLI::error( 'Newfold CLI: ' . $message );
139    }
140
141    /**
142     * Outputs a success message with "Newfold CLI:" prefix.
143     *
144     * @param string $message Message to output.
145     *
146     * @return void
147     */
148    public static function success( $message ) {
149        WP_CLI::success( 'Newfold CLI: ' . $message );
150    }
151
152    /**
153     * Checks if the current execution context is WP-CLI.
154     *
155     * @return bool True if running inside WP-CLI, false otherwise.
156     */
157    public static function is_executing_wp_cli() {
158        return defined( 'WP_CLI' ) && WP_CLI;
159    }
160}