Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
8a68ba7
Initial commit for WP CLI support.
esudalaimadan Oct 18, 2019
d28ac03
Update WP Cli format
sudar Oct 22, 2019
f0b5405
WP CLI command for by status module.
esudalaimadan Oct 23, 2019
1838dba
Fix long description of by-status command
esudalaimadan Oct 23, 2019
81ed9cc
Apply fixes from StyleCI
esudalaimadan Oct 24, 2019
a6bca42
Merge pull request #684 from sudar/analysis-XlG33O
esudalaimadan Oct 24, 2019
1a74d20
Reuse validation logic from UI
sudar Oct 25, 2019
d30ec93
Handle arguments
sudar Oct 25, 2019
64c504f
Add DeletePostsByCommentsModule to WP CLI.
esudalaimadan Oct 25, 2019
b6034fe
Merge branch 'dev/6.1.0' into 677-feature/add-wp-cli-support
esudalaimadan Oct 29, 2019
17888df
Add DeletePostsByPostType module support to WP CLI.
esudalaimadan Oct 29, 2019
e1ac40d
Merge branch '677-feature/add-wp-cli-support' of github.com:sudar/bul…
esudalaimadan Oct 29, 2019
a902d3d
Apply fixes from StyleCI
esudalaimadan Oct 29, 2019
2c69ccd
Merge pull request #686 from sudar/analysis-z3Kd2P
esudalaimadan Oct 29, 2019
edf7f9a
Add revision, sticky post and taxonomy modules.
esudalaimadan Oct 31, 2019
2f7e8f7
Merge branch 'dev/6.1.0' into 677-feature/add-wp-cli-support
esudalaimadan Nov 1, 2019
edc6262
Add delete users by user meta module to wp cli
esudalaimadan Nov 1, 2019
8c49e57
Merge branch '677-feature/add-wp-cli-support' of github.com:sudar/bul…
esudalaimadan Nov 1, 2019
6de45ba
Add User role module to WP CLI.
esudalaimadan Nov 4, 2019
2d7fc57
Add delete terms by name module to WP CLI.
esudalaimadan Nov 4, 2019
dce4dc2
Add delete terms by post count module to WP CLI.
esudalaimadan Nov 5, 2019
4f28408
Add delete pages by status module to WP CLI.
esudalaimadan Nov 5, 2019
aee401e
Pages command file.
esudalaimadan Nov 6, 2019
74856e4
Add delete comments module to WP CLI
esudalaimadan Nov 6, 2019
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
1 change: 0 additions & 1 deletion bulk-delete.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
* Version 6.1.0 of the Bulk Delete plugin dropped support for PHP < 5.2.
* If you are still struck with PHP < 5.6 and can't update, then use v6.0.2 of the plugin.
* But note that some add-ons may not work.
*
* @since 6.0.0 Dropped support for PHP 5.2
* @since 6.1.0 Dropped support for < PHP 5.6
*/
Expand Down
23 changes: 23 additions & 0 deletions include/Core/Base/BaseCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace BulkWP\BulkDelete\Core\Base;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
* Base class for a Bulk Delete WP CLI command.
*
* @since 6.1.0
*/
abstract class BaseCommand extends \WP_CLI_Command {
/**
* Get the command.
*
* This method is commented since PHP doesn't allow static abstract methods in PHP 5.6 or below.
* Refer to https://stackoverflow.com/a/31235907/24949 for the full explanation.
*
* Once WordPress increases the minimum PHP to 7.0 or above this method will be uncommented.
*
* @return string Command name.
*/
// abstract public static function get_command();
}
129 changes: 121 additions & 8 deletions include/Core/Base/BaseModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,18 +265,53 @@ protected function append_to_js_array( $js_array ) {
* @param array $request Request array.
*/
public function process( $request ) {
$msg = $this->process_request( $request );

add_settings_error(
$this->page_slug,
$this->action,
$msg,
'updated'
);
}

/**
* Process CLI Request.
*
* @since 6.1.0
*
* @param array $request Request input.
*
* @return string Message to be shown to the user.
*/
public function process_cli_request( $request ) {
$request = $this->prepare_cli_input( $request );

return $this->process_request( $request );
}

/**
* Process the user request.
*
* @since 6.1.0
*
* @param array $request Request array. For UI it will be `$_POST`. For CLI it will be the `$assoc_arg`.
*
* @return string Message to be shown to the user.
*/
protected function process_request( $request ) {
$options = $this->parse_common_filters( $request );
$options = $this->convert_user_input_to_options( $request, $options );
$cron_options = $this->parse_cron_filters( $request );

/**
* Filter the processed delete options.
*
* @since 6.0.0
*
* @param array $options Processed options.
* @param array $request Request array.
* @param \BulkWP\BulkDelete\Core\Base\BaseModule The delete module.
*
* @since 6.0.0
*/
$options = apply_filters( 'bd_processed_delete_options', $options, $request, $this );

Expand All @@ -287,12 +322,7 @@ public function process( $request ) {
$msg = sprintf( $this->get_success_message( $items_deleted ), $items_deleted );
}

add_settings_error(
$this->page_slug,
$this->action,
$msg,
'updated'
);
return $msg;
}

/**
Expand Down Expand Up @@ -459,4 +489,87 @@ public function get_name() {
public function get_page_slug() {
return $this->page_slug;
}

/**
* Get the prefix that is added to the user input fields in the UI.
*
* @return string UI input prefix.
*
* @since 6.1.0
*/
protected function get_ui_input_prefix() {
return 'smbd_' . $this->field_slug . '_';
}

/**
* Get the map of non standard keys used in user input.
*
* Before v6.1.0, each module has non-standard names for some input keys.
* This became a problem when WP CLI was introduced.
* This function provides a way for each module to specify a map of these non-standard keys that it expects.
* Eventually the non-standard keys will be rectified and after that this function will no longer be needed.
*
* @return array UI map.
*
* @since 6.1.0
*/
protected function get_non_standard_input_key_map() {
return array();
}

/**
* Prepare the input for a module.
*
* @since 6.1.0
*
* @param array $input Input array.
*
* @return array Prepared input.
*/
protected function prepare_cli_input( $input ) {
$input = $this->prefix_input( $input, $this->get_ui_input_prefix() );
$input = $this->handle_non_standard_keys( $input, $this->get_non_standard_input_key_map() );

return $input;
}

/**
* Prefix user request array keys.
*
* @since 6.1.0
*
* @param array $input User input.
* @param string $prefix Prefix string.
*
* @return array Prefixed user input.
*/
protected function prefix_input( $input, $prefix ) {
$prefixed_input = array();

foreach ( $input as $key => $value ) {
$prefixed_input[ $prefix . $key ] = $value;
}

return $prefixed_input;
}

/**
* Handle non standard keys that are used in the module.
*
* @since 6.1.0
*
* @param array $input User input.
* @param array $map Map of non standard and standard keys.
*
* @return array Processed input.
*/
protected function handle_non_standard_keys( $input, $map ) {
foreach ( $map as $non_standard_key => $standard_key ) {
if ( isset( $input[ $standard_key ] ) ) {
$input[ $non_standard_key ] = $input[ $standard_key ];
}
}

return $input;
}
}
49 changes: 45 additions & 4 deletions include/Core/BulkDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use BulkWP\BulkDelete\Core\Addon\Upseller;
use BulkWP\BulkDelete\Core\Base\BasePage;
use BulkWP\BulkDelete\Core\CLI\CLILoader;
use BulkWP\BulkDelete\Core\Comments\DeleteCommentsPage;
use BulkWP\BulkDelete\Core\Comments\Modules\DeleteCommentsByAuthorModule;
use BulkWP\BulkDelete\Core\Comments\Modules\DeleteCommentsByIPModule;
Expand Down Expand Up @@ -89,6 +90,15 @@ final class BulkDelete {
*/
private $upseller;

/**
* Loads CLI commands.
*
* @since 6.1.0
*
* @var BulkWP\BulkDelete\Core\CLI\CLILoader
*/
private $cli_loader;

/**
* Bulk Delete Autoloader.
*
Expand Down Expand Up @@ -161,7 +171,7 @@ public function load() {
return;
}

if ( ! $this->is_admin_or_cron() ) {
if ( ! $this->is_plugin_needed() ) {
return;
}

Expand All @@ -179,6 +189,10 @@ public function load() {
*/
do_action( 'bd_loaded', $this->get_plugin_file() );

if ( $this->is_wp_cli() ) {
$this->load_cli();
}

$this->load_primary_pages();
}

Expand Down Expand Up @@ -222,6 +236,16 @@ private function load_dependencies() {
$this->upseller->load();
}

/**
* Load CLI commands.
*
* @since 6.1.0
*/
private function load_cli() {
$this->cli_loader = new CLILoader();
$this->cli_loader->load();
}

/**
* Loads the plugin's actions and hooks.
*
Expand Down Expand Up @@ -721,11 +745,28 @@ public function get_page( $page_slug ) {
}

/**
* Is the current request an admin or cron request?
* Is the plugin needed for the current request?
*
* Plugin is needed for the following requests
* - Admin requests
* - Cron requests
* - WP Cli requests
*
* @return bool True, if yes, False otherwise.
*/
private function is_admin_or_cron() {
return is_admin() || defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] );
private function is_plugin_needed() {
return is_admin() || defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) || $this->is_wp_cli();
}

/**
* Is this a WP CLI request?
*
* @since 6.1.0
*
* @return true if WP CLI. FALSE otherwise.
*
*/
private function is_wp_cli() {
return defined( 'WP_CLI' ) && WP_CLI;
}
}
73 changes: 73 additions & 0 deletions include/Core/CLI/CLILoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace BulkWP\BulkDelete\Core\CLI;

use BulkWP\BulkDelete\Core\CLI\Commands\DeleteCommentsCommand;
use BulkWP\BulkDelete\Core\CLI\Commands\DeletePagesCommand;
use BulkWP\BulkDelete\Core\CLI\Commands\DeletePostsCommand;
use BulkWP\BulkDelete\Core\CLI\Commands\DeleteTermsCommand;
use BulkWP\BulkDelete\Core\CLI\Commands\DeleteUsersCommand;
use BulkWP\BulkDelete\Core\CLI\Commands\SystemInfoCommand;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
* Loads all CLI command.
*
* @since 6.1.0
*/
class CLILoader {
/**
* Base Command.
*
* @var string
*/
protected $base_command = 'bulk-delete';

/**
* Load CLI command.
*
* This will be called after `bd_loaded` hook.
*/
public function load() {
$commands = $this->get_commands();

foreach ( $commands as $command ) {
$this->register_command( $command );
}
}

/**
* List of Bulk Delete WP CLI Commands.
*
* @return \BulkWP\BulkDelete\Core\Base\BaseCommand[]
*/
protected function get_commands() {
$commands = array(
SystemInfoCommand::class,
DeleteCommentsCommand::class,
DeletePagesCommand::class,
DeletePostsCommand::class,
DeleteTermsCommand::class,
DeleteUsersCommand::class,
);

/**
* Filters the CLI command map.
*
* @since 6.1.0
*
* @param \BulkWP\BulkDelete\Core\Base\BaseCommand[] List of commands.
*/
return apply_filters( 'bd_cli_commands', $commands );
}

/**
* Register a command.
*
* @param \BulkWP\BulkDelete\Core\Base\BaseCommand $command Command to register.
*/
protected function register_command( $command ) {
\WP_CLI::add_command( $this->base_command . ' ' . $command::get_command(), $command );
}
}
Loading