Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wp cli commands to get stats and delete stats #679

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
0b98be8
Don't block PSR-4 style filenames for classes
pattonwebz Jun 24, 2024
183b864
Add a PSR-4 style namespace for autoloader
pattonwebz Jun 24, 2024
3b486bd
Add WP-Cli base setup and commands for get-stats and delete-stats
pattonwebz Jun 24, 2024
0dc00e8
Add WP-Cli base setup and commands for get-stats and delete-stats
pattonwebz Jun 24, 2024
775007d
Use is_subclass_of rather than instance of since we have a classname …
pattonwebz Jun 24, 2024
86fb933
Use is_subclass_of rather than instance of since we have a classname …
pattonwebz Jun 24, 2024
64bdc00
Register the cli commands when bootstrapping the plugin
pattonwebz Jun 24, 2024
9d3bff3
Lowercase autoload key
pattonwebz Jun 24, 2024
dae5484
PHPCS: fix multi-line call by breaking to own lines
pattonwebz Jun 24, 2024
f7d2296
Use spaces in composer.json instead of tabs
pattonwebz Jun 24, 2024
26b8d65
Remove handling for full site stats in get-stats command
pattonwebz Jun 27, 2024
4795cdd
Update get-stats command to not support getting entire site stats and…
pattonwebz Jun 27, 2024
1935f63
Add a get-site-stats command to get the entire site stats for all sca…
pattonwebz Jun 27, 2024
941db72
Add a Mock_WP_CLI class for tests
pattonwebz Jul 10, 2024
23dcd4b
Update the BootstrapCLI class to get WP_CLI with dependency injection
pattonwebz Jul 10, 2024
11a9036
Add tests for the BootstrapCLI class
pattonwebz Jul 10, 2024
ac55f1d
Update the GetStats class to get WP_CLI with dependency injection
pattonwebz Jul 10, 2024
4333584
Add tests for GetStats class
pattonwebz Jul 10, 2024
8af1dbd
Reset the should_throw flag in teardown of BootstrapCLI tests
pattonwebz Jul 10, 2024
35cbc2c
Catch empty stats in another condition
pattonwebz Jul 10, 2024
793e691
Add tests for filtered stat request
pattonwebz Jul 10, 2024
98af861
No need for a dedicated catch block for stats not found, groping it w…
pattonwebz Jul 10, 2024
e4fcc87
Remove bail for non-existent class.
pattonwebz Jul 10, 2024
f238b86
Move mocks and database helpers to dedicated directory in tests folde…
pattonwebz Jul 10, 2024
0e9306d
Allow injecting a WP_CLI-like object to DeleteStats and GetSiteStats
pattonwebz Jul 10, 2024
096c65c
Add tests to cover the delete-stats command
pattonwebz Jul 10, 2024
8830633
Use a nested namespace for test helpers so that test classes don't no…
pattonwebz Jul 10, 2024
8a40907
Use isset to check items_to_return
pattonwebz Jul 10, 2024
951473a
Add cache clear support to the command
pattonwebz Jul 10, 2024
8c9d1e2
Fix missed WP_CLI dependency use
pattonwebz Jul 10, 2024
cbfc1c1
Add tests for get-site-stats command class
pattonwebz Jul 10, 2024
a62ee9e
Remove unneeded function for getting stats - call the method directly…
pattonwebz Jul 10, 2024
38b6068
Return early when stats key not found
pattonwebz Jul 10, 2024
188c4cb
Wrap strings in commands translation functions
pattonwebz Jul 10, 2024
447852a
Make the help text for the commands more useful
pattonwebz Jul 12, 2024
8076a56
Make --clear-cache a true flag and not an associative param
pattonwebz Jul 12, 2024
7ff5681
Update the test to check the correct string after updating translations
pattonwebz Jul 12, 2024
1c305b1
Flags need to be set as `'optional' => true` in CLI synopsys blocks
pattonwebz Jul 12, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@
"admin/",
"includes/classes/",
"includes/deprecated/"
]
],
"psr-4": {
"EqualizeDigital\\AccessibilityChecker\\": "includes/classes/"
}
},
"autoload-dev": {
"classmap": []
"classmap": [],
"psr-4": {
"EqualizeDigital\\AccessibilityChecker\\Tests\\TestHelpers\\": "tests/phpunit/TestHelpers/"
}
},
"scripts": {
"lint": [
Expand All @@ -75,4 +81,4 @@
"@php ./vendor/phpunit/phpunit/phpunit --testdox"
]
}
}
}
107 changes: 107 additions & 0 deletions includes/classes/WPCLI/BootstrapCLI.php
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()
)
);
}
}
}
}
40 changes: 40 additions & 0 deletions includes/classes/WPCLI/Command/CLICommandInterface.php
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 = [] );
}
108 changes: 108 additions & 0 deletions includes/classes/WPCLI/Command/DeleteStats.php
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
)
);
}
}
Loading
Loading