-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #679 from equalizedigital/william/674/add-wp-cli-c…
…ommands-to-get-stats-and-delete-stats Add wp cli commands to get stats and delete stats
- Loading branch information
Showing
14 changed files
with
1,213 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
/** | ||
* Bootstrap the CLI commands for the Accessibility Checker plugin. | ||
* | ||
* @since 1.15.0 | ||
* | ||
* @package Accessibility_Checker | ||
*/ | ||
|
||
namespace EqualizeDigital\AccessibilityChecker\WPCLI; | ||
|
||
use EqualizeDigital\AccessibilityChecker\WPCLI\Command\CLICommandInterface; | ||
use EqualizeDigital\AccessibilityChecker\WPCLI\Command\DeleteStats; | ||
use EqualizeDigital\AccessibilityChecker\WPCLI\Command\GetSiteStats; | ||
use EqualizeDigital\AccessibilityChecker\WPCLI\Command\GetStats; | ||
use Exception; | ||
use WP_CLI; | ||
|
||
/** | ||
* Handles the registration of WP-CLI commands for the Accessibility Checker plugin. | ||
* | ||
* @since 1.15.0 | ||
*/ | ||
class BootstrapCLI { | ||
|
||
/** | ||
* The WP-CLI instance. | ||
* | ||
* This allows injecting a mock WP-CLI instance for testing. | ||
* | ||
* @since 1.15.0 | ||
* | ||
* @var WP_CLI | ||
*/ | ||
private $wp_cli; | ||
|
||
/** | ||
* The boot method on this class will use this array to register custom WP-CLI commands. | ||
* | ||
* @since 1.15.0 | ||
* | ||
* @var CLICommandInterface[] | ||
*/ | ||
protected array $commands = [ | ||
DeleteStats::class, | ||
GetSiteStats::class, | ||
GetStats::class, | ||
]; | ||
|
||
/** | ||
* Set up the internal wp_cli property. | ||
* | ||
* @since 1.15.0 | ||
* | ||
* @param WP_CLI|null $wp_cli The WP-CLI instance. | ||
*/ | ||
public function __construct( $wp_cli = null ) { | ||
$this->wp_cli = $wp_cli ? $wp_cli : new WP_CLI(); | ||
} | ||
|
||
/** | ||
* Register the WP-CLI commands by looping through the commands array and adding each command. | ||
* | ||
* @since 1.15.0 | ||
* | ||
* @return void | ||
*/ | ||
public function register() { | ||
// Bail if not running in WP_CLI. | ||
if ( ! defined( 'WP_CLI' ) || ! WP_CLI ) { | ||
return; | ||
} | ||
|
||
/** | ||
* Filter the list of classes that hold the commands to be registered. | ||
* | ||
* @since 1.15.0 | ||
* | ||
* @param CLICommandInterface[] $commands array of classes to register as commands. | ||
*/ | ||
$commands = apply_filters( 'edac_filter_command_classes', $this->commands ); | ||
|
||
foreach ( $commands as $command ) { | ||
// All commands must follow the interface. | ||
if ( ! is_subclass_of( $command, CLICommandInterface::class, true ) ) { | ||
continue; | ||
} | ||
|
||
try { | ||
$this->wp_cli::add_command( | ||
$command::get_name(), | ||
$command, | ||
$command::get_args() | ||
); | ||
} catch ( Exception $e ) { | ||
$this->wp_cli::warning( | ||
sprintf( | ||
// translators: 1: a php classname, 2: an error message that was thrown about why this failed to register. | ||
esc_html__( 'Failed to register command %1$s because %2$s', 'accessibility-checker' ), | ||
$command, | ||
$e->getMessage() | ||
) | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php /* phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- WP_CLI doesn't follow WP method name rules */ | ||
/** | ||
* Interface CLICommandInterface | ||
* | ||
* @since 1.15.0 | ||
* | ||
* @package Accessibility_Checker | ||
*/ | ||
|
||
namespace EqualizeDigital\AccessibilityChecker\WPCLI\Command; | ||
|
||
/** | ||
* Interface defining the methods required for a WP-CLI command to be bootstrapped by this plugin. | ||
*/ | ||
interface CLICommandInterface { | ||
|
||
/** | ||
* Get the name of the command | ||
* | ||
* @return string | ||
*/ | ||
public static function get_name(): string; | ||
|
||
/** | ||
* Get the arguments for the command | ||
* | ||
* @return array | ||
*/ | ||
public static function get_args(): array; | ||
|
||
/** | ||
* Run the command | ||
* | ||
* @param array $options Positional args passed to the command. | ||
* @param array $arguments Associative args passed to the command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function __invoke( array $options = [], array $arguments = [] ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
/** | ||
* Delete stats for a post. | ||
* | ||
* @since 1.15.0 | ||
* | ||
* @package Accessibility_Checker | ||
*/ | ||
|
||
namespace EqualizeDigital\AccessibilityChecker\WPCLI\Command; | ||
|
||
use EDAC\Admin\Purge_Post_Data; | ||
use WP_CLI; | ||
use WP_CLI\ExitException; | ||
|
||
/** | ||
* Deletes stats for a given post ID. | ||
* | ||
* @package PattonWebz\AccessibilityCheckerCLI\Command | ||
*/ | ||
class DeleteStats implements CLICommandInterface { | ||
|
||
/** | ||
* The WP-CLI instance. | ||
* | ||
* This lets a mock be passed in for testing. | ||
* | ||
* @var mixed|WP_CLI | ||
*/ | ||
private $wp_cli; | ||
|
||
/** | ||
* GetStats constructor. | ||
* | ||
* @param mixed|WP_CLI $wp_cli The WP-CLI instance. | ||
*/ | ||
public function __construct( $wp_cli = null ) { | ||
$this->wp_cli = $wp_cli ?? new WP_CLI(); | ||
} | ||
|
||
/** | ||
* Get the name of the command | ||
* | ||
* @return string | ||
*/ | ||
public static function get_name(): string { | ||
return 'accessibility-checker delete-stats'; | ||
} | ||
|
||
/** | ||
* Get the arguments for the command | ||
* | ||
* @return array | ||
*/ | ||
public static function get_args(): array { | ||
return [ | ||
'synopsis' => [ | ||
[ | ||
'type' => 'positional', | ||
'name' => 'post_id', | ||
'description' => esc_html__( 'The ID of the post to delete stats for.', 'accessibility-checker' ), | ||
'optional' => true, | ||
'default' => 0, | ||
'repeating' => false, | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* Delete the accessibility-checker stats for a given post ID. | ||
* | ||
* @param array $options This is the positional argument, the post ID in this case. | ||
* @param array $arguments This is the associative argument, not used in this command but kept for consistency with cli commands using this pattern. | ||
* | ||
* @return void | ||
* @throws ExitException If the post ID is not provided, does not exist, or the class we need isn't available. | ||
*/ | ||
public function __invoke( array $options = [], array $arguments = [] ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | ||
$post_id = $options[0] ?? 0; | ||
|
||
if ( 0 === $post_id ) { | ||
$this->wp_cli::error( esc_html__( 'No Post ID provided.', 'accessibility-checker' ) ); | ||
} | ||
|
||
$post_exists = (bool) get_post( $post_id ); | ||
|
||
if ( ! $post_exists ) { | ||
$this->wp_cli::error( | ||
sprintf( | ||
// translators: 1: a post ID. | ||
esc_html__( 'Post ID %1$s does not exist.', 'accessibility-checker' ), | ||
$post_id | ||
) | ||
); | ||
return; | ||
} | ||
|
||
Purge_Post_Data::delete_post( $post_id ); | ||
$this->wp_cli::success( | ||
sprintf( | ||
// translators: 1: a post ID. | ||
esc_html__( 'Stats of %1$s deleted.', 'accessibility-checker' ), | ||
$post_id | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.