Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 51 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| CLI | |
0.00% |
0 / 51 |
|
0.00% |
0 / 9 |
462 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| register_commands | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| get_status | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| get_key | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| set_key | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| activate | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| deactivate | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| get_product_details | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| get_check_updates | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * CLI commands to view/set the licence key and status, to activate/deactivate the licence, and to refresh |
| 4 | * product information from the update server. |
| 5 | * |
| 6 | * No need for a plugin update command, since updates should work as normal through `wp plugin update`. |
| 7 | * |
| 8 | * `wp plugin list --fields=name,version,update_version,update_package` |
| 9 | * `wp transient delete update_plugins --network` |
| 10 | * |
| 11 | * @package brianhenryie/bh-wp-plugin-updater |
| 12 | */ |
| 13 | |
| 14 | namespace BrianHenryIE\WP_Plugin_Updater\WP_Includes; |
| 15 | |
| 16 | use BrianHenryIE\WP_Plugin_Updater\API_Interface; |
| 17 | use BrianHenryIE\WP_Plugin_Updater\Exception\Licence_Key_Not_Set_Exception; |
| 18 | use BrianHenryIE\WP_Plugin_Updater\Exception\Plugin_Updater_Exception_Abstract; |
| 19 | use BrianHenryIE\WP_Plugin_Updater\Settings_Interface; |
| 20 | use Exception; |
| 21 | use Psr\Log\LoggerAwareTrait; |
| 22 | use Psr\Log\LoggerInterface; |
| 23 | use WP_CLI; |
| 24 | use WP_CLI\Formatter; |
| 25 | |
| 26 | /** |
| 27 | * `wp {$cli_base} updater get-status` |
| 28 | */ |
| 29 | class CLI { |
| 30 | use LoggerAwareTrait; |
| 31 | |
| 32 | /** |
| 33 | * Constructor. |
| 34 | * |
| 35 | * @param API_Interface $api The main API class where the functionality is implemented. |
| 36 | * @param Settings_Interface $settings The plugin settings. |
| 37 | * @param LoggerInterface $logger A PSR logger. |
| 38 | */ |
| 39 | public function __construct( |
| 40 | protected API_Interface $api, |
| 41 | protected Settings_Interface $settings, |
| 42 | LoggerInterface $logger |
| 43 | ) { |
| 44 | $this->setLogger( $logger ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Register the WP-CLI commands. |
| 49 | * |
| 50 | * If the CLI base is not set in {@see Settings_Interface::get_cli_base()} no commands will be registered. |
| 51 | */ |
| 52 | public function register_commands(): void { |
| 53 | |
| 54 | $cli_base = $this->settings->get_cli_base(); |
| 55 | |
| 56 | if ( is_null( $cli_base ) ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | try { |
| 61 | WP_CLI::add_command( "{$cli_base} updater get-status", array( $this, 'get_status' ) ); |
| 62 | WP_CLI::add_command( "{$cli_base} updater set-key", array( $this, 'set_key' ) ); |
| 63 | WP_CLI::add_command( "{$cli_base} updater get-key", array( $this, 'get_key' ) ); |
| 64 | WP_CLI::add_command( "{$cli_base} updater activate", array( $this, 'activate' ) ); |
| 65 | WP_CLI::add_command( "{$cli_base} updater deactivate", array( $this, 'deactivate' ) ); |
| 66 | WP_CLI::add_command( "{$cli_base} updater product-information", array( $this, 'get_product_details' ) ); |
| 67 | WP_CLI::add_command( "{$cli_base} updater check-updates", array( $this, 'get_check_updates' ) ); |
| 68 | } catch ( Exception $e ) { |
| 69 | $this->logger->error( |
| 70 | 'Failed to register WP CLI commands: ' . $e->getMessage(), |
| 71 | array( 'exception' => $e ) |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Get the updater status |
| 78 | * |
| 79 | * [--format=<format>] |
| 80 | * The serialization format for the value. |
| 81 | * --- |
| 82 | * default: table |
| 83 | * options: |
| 84 | * - table |
| 85 | * - json |
| 86 | * --- |
| 87 | * |
| 88 | * ## EXAMPLES |
| 89 | * |
| 90 | * # Get the updater status for the plugin. |
| 91 | * $ wp plugin-slug updater get-status |
| 92 | * +-------------+---------+---------------------------+---------------------------+ |
| 93 | * | licence_key | status | expires | last_updated | |
| 94 | * +-------------+---------+---------------------------+---------------------------+ |
| 95 | * | {my-key} | invalid | 2024-06-11T00:22:31+00:00 | 2024-06-01T00:12:29+00:00 | |
| 96 | * +-------------+---------+---------------------------+---------------------------+ |
| 97 | * |
| 98 | * # Get the updater status for the plugin from the updater server. |
| 99 | * $ wp plugin-slug updater get-status --refresh |
| 100 | * +-------------+---------+---------------------------+---------------------------+ |
| 101 | * | licence_key | status | expires | last_updated | |
| 102 | * +-------------+---------+---------------------------+---------------------------+ |
| 103 | * | {my-key} | invalid | 2024-06-11T00:22:31+00:00 | 2024-06-11T00:22:31+00:00 | |
| 104 | * +-------------+---------+---------------------------+---------------------------+ |
| 105 | * |
| 106 | * @param string[] $args The unlabelled command line arguments. |
| 107 | * @param array<string,string> $assoc_args The labelled command line arguments. |
| 108 | * |
| 109 | * @see API_Interface::get_licence_details() |
| 110 | */ |
| 111 | public function get_status( array $args, array $assoc_args ): void { |
| 112 | |
| 113 | try { |
| 114 | $result = $this->api->get_licence_details( |
| 115 | \WP_CLI\Utils\get_flag_value( $assoc_args, 'refresh', false ) |
| 116 | ); |
| 117 | } catch ( Licence_Key_Not_Set_Exception $e ) { |
| 118 | WP_CLI::error( $e->getMessage() . ' Use `wp ' . $this->settings->get_cli_base() . ' updater set-key {my-key}`.' ); |
| 119 | } catch ( Plugin_Updater_Exception_Abstract $e ) { |
| 120 | WP_CLI::error( $e->getMessage() ); |
| 121 | } |
| 122 | |
| 123 | $formatter = new Formatter( $assoc_args, array_keys( $result->__serialize() ) ); |
| 124 | $formatter->display_items( array( $result->__serialize() ) ); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Get the updater key |
| 129 | * |
| 130 | * ## EXAMPLES |
| 131 | * |
| 132 | * # Get the updater key the plugin has been configured with. |
| 133 | * $ wp plugin-slug updater get-key |
| 134 | * Success: 876235557140adb9b8c47b28488cda8481d98495 |
| 135 | * |
| 136 | * @see API_Interface::get_licence_details() |
| 137 | * |
| 138 | * @param string[] $args The unlabelled command line arguments. |
| 139 | * @param array<string,string> $assoc_args The labelled command line arguments. |
| 140 | */ |
| 141 | public function get_key( array $args, array $assoc_args ): void { |
| 142 | |
| 143 | $result = $this->api->get_licence_details( false ); |
| 144 | |
| 145 | WP_CLI::success( $result->get_licence_key() ?? 'No key set' ); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Set the updater licence key or access token. |
| 150 | * |
| 151 | * Sets the licence key the plugin should use. Conditionally activates it. |
| 152 | * |
| 153 | * A licence key cannot be validated until it is activated. I.e. an invalid licence key may be accepted. |
| 154 | * |
| 155 | * TODO: A licence key of an invalid format will be rejected. |
| 156 | * |
| 157 | * ## OPTIONS |
| 158 | * |
| 159 | * <key> |
| 160 | * : Alphanumeric licence key or access token. |
| 161 | * |
| 162 | * ## EXAMPLES |
| 163 | * |
| 164 | * # Set the key the plugin should use. |
| 165 | * $ wp plugin-slug updater set-key 876235557140adb9b8c47b28488cda8481d98495 |
| 166 | * Success: active |
| 167 | * |
| 168 | * # Set the key the plugin should use and activate it. |
| 169 | * $ wp plugin-slug updater set-key 876235557140adb9b8c47b28488cda8481d98495 --activate |
| 170 | * Success: active |
| 171 | * |
| 172 | * # Set an invalid licence key |
| 173 | * $ wp plugin-slug updater set-key a1s2invalidp0o9 |
| 174 | * TODO |
| 175 | * |
| 176 | * @param string[] $args The unlabelled command line arguments. |
| 177 | * @param array<string,string> $assoc_args The labelled command line arguments. |
| 178 | * |
| 179 | * @see API_Interface::activate_licence() |
| 180 | */ |
| 181 | public function set_key( array $args, array $assoc_args ): void { |
| 182 | |
| 183 | try { |
| 184 | $result = $this->api->set_license_key( $args[0] ); |
| 185 | |
| 186 | if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'activate', false ) ) { |
| 187 | $result = $this->api->activate_licence(); |
| 188 | } |
| 189 | } catch ( Plugin_Updater_Exception_Abstract $e ) { |
| 190 | WP_CLI::error( $e->getMessage() ); |
| 191 | } |
| 192 | |
| 193 | WP_CLI::success( "Licence key set to: {$result->get_licence_key()}" ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Activate the already configured licence. |
| 198 | * |
| 199 | * ## EXAMPLES |
| 200 | * |
| 201 | * # Activate this domain to use the configured licence key for Zelle plugin updates. |
| 202 | * $ wp zelle updater activate |
| 203 | * TODO |
| 204 | * |
| 205 | * @param string[] $args The unlabelled command line arguments. |
| 206 | * @param array<string,string> $assoc_args The labelled command line arguments. |
| 207 | * |
| 208 | * @see API_Interface::activate_licence() |
| 209 | */ |
| 210 | public function activate( array $args, array $assoc_args ): void { |
| 211 | |
| 212 | try { |
| 213 | $result = $this->api->activate_licence(); |
| 214 | } catch ( Licence_Key_Not_Set_Exception $e ) { |
| 215 | WP_CLI::error( $e->getMessage() . ' Use `wp ' . $this->settings->get_cli_base() . ' updater set-key {my-key}`.' ); |
| 216 | } catch ( Plugin_Updater_Exception_Abstract $e ) { |
| 217 | WP_CLI::error( $e->getMessage() ); |
| 218 | } |
| 219 | |
| 220 | // TODO: |
| 221 | WP_CLI::success( $result->get_status() ); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Deactivate the licence. |
| 226 | * |
| 227 | * Deactivates the configured key. |
| 228 | * |
| 229 | * ## EXAMPLES |
| 230 | * |
| 231 | * # Deactivate the licence key the Zelle plugin is using. |
| 232 | * $ wp zelle updater deactivate |
| 233 | * TODO |
| 234 | * |
| 235 | * @param string[] $args The unlabelled command line arguments. |
| 236 | * @param array<string,string> $assoc_args The labelled command line arguments. |
| 237 | * |
| 238 | * @see API_Interface::deactivate_licence() |
| 239 | */ |
| 240 | public function deactivate( array $args, array $assoc_args ): void { |
| 241 | |
| 242 | try { |
| 243 | $result = $this->api->deactivate_licence(); |
| 244 | } catch ( Licence_Key_Not_Set_Exception $e ) { |
| 245 | WP_CLI::error( $e->getMessage() . ' Use `wp ' . $this->settings->get_cli_base() . ' updater set-key {my-key}`.' ); |
| 246 | } catch ( Plugin_Updater_Exception_Abstract $e ) { |
| 247 | WP_CLI::error( $e->getMessage() ); |
| 248 | } |
| 249 | |
| 250 | // TODO: |
| 251 | WP_CLI::success( $result->get_status() ); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Refresh the product information from the plugin update server. |
| 256 | * |
| 257 | * ## EXAMPLES |
| 258 | * |
| 259 | * # Refresh the product information from the plugin update server. |
| 260 | * $ wp plugin-slug product-information --refresh |
| 261 | * Success: {json} TODO: display as table |
| 262 | * |
| 263 | * @param string[] $args The unlabelled command line arguments. |
| 264 | * @param array<string,string> $assoc_args The labelled command line arguments. |
| 265 | * |
| 266 | * @see API_Interface::get_plugin_information() |
| 267 | */ |
| 268 | public function get_product_details( array $args, array $assoc_args ): void { |
| 269 | |
| 270 | $result = $this->api->get_plugin_information( |
| 271 | \WP_CLI\Utils\get_flag_value( $assoc_args, 'refresh', false ) |
| 272 | ); |
| 273 | |
| 274 | WP_CLI::success( wp_json_encode( $result, JSON_PRETTY_PRINT ) ?: '' ); |
| 275 | } |
| 276 | |
| 277 | public function get_check_updates( array $args, array $assoc_args ): void { |
| 278 | |
| 279 | $result = $this->api->get_check_update( true ); |
| 280 | |
| 281 | WP_CLI::success( wp_json_encode( $result, JSON_PRETTY_PRINT ) ?: '' ); |
| 282 | } |
| 283 | } |