Passed
Push — 677-feature/add-wp-cli-support ( d28ac0...f0b540 )
by
unknown
05:05
created

DeletePostsCommand::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 8
ccs 0
cts 8
cp 0
crap 12
rs 10
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\CLI\Commands;
4
5
use BulkWP\BulkDelete\Core\Base\BaseCommand;
6
use BulkWP\BulkDelete\Core\Posts\Modules\DeletePostsByStatusModule;
7
8
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
9
10
/**
11
 * Delete Posts CLI Command.
12
 *
13
 * @since 6.1.0
14
 */
15
class DeletePostsCommand extends BaseCommand {
16
	/**
17
	 * Get the command.
18
	 *
19
	 * @return string Command name.
20
	 */
21
	public static function get_command() {
22
		return 'posts';
23
	}
24
25
	/**
26
	 * Validate Delete Options.
27
	 *
28
	 * @param array $options          Delete Options.
29
	 * @param array $mandatory_fields Mandatory fields list.
30
	 *
31
	 * @return boolean True for success and False for failure.
32
	 */
33
	public function validate( $options, $mandatory_fields ) {
34
		foreach ( $mandatory_fields as $field ) {
35
			if ( empty( $options[ $field ] ) ) {
36
				\WP_CLI::error( $field . ' can not be empty.' );
0 ignored issues
show
Bug introduced by
The type WP_CLI was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
				return false;
38
			}
39
		}
40
		return true;
41
	}
42
43
	/**
44
	 * Get default options.
45
	 *
46
	 * @return array $defaults
47
	 */
48
	public function get_defaults() {
49
		$defaults                   = array();
50
		$defaults['restrict']       = false;
51
		$defaults['limit_to']       = 0;
52
		$defaults['exclude_sticky'] = false;
53
		$defaults['force_delete']   = false;
54
		return $defaults;
55
	}
56
57
	/**
58
	 * Delete post by status.
59
	 *
60
	 * @param array $args       Arguments to be supplied.
61
	 * @param array $assoc_args Associative arguments to be supplied.
62
	 * @return void
63
	 * ## OPTIONS
64
	 *
65
	 * [--post_status=<post_status>]
66
	 * : Post with the entered post status will be deleted.
67
	 * ---
68
	 * options:
69
	 *   - draft
70
	 *   - publish
71
	 *   - private
72
	 *   - any custom post status
73
	 * ---
74
	 *
75
	 * [--limit_to=<limit_to>]
76
	 * : Limits the number of posts to be deleted.
77
	 *
78
	 * [--restrict=<restrict>]
79
	 * : Restricts posts deletion with post date filter.
80
	 * ---
81
	 * default: false
82
	 * options:
83
	 *   - true
84
	 *   - false
85
	 * ---
86
	 *
87
	 * [--force_delete=<force_delete>]
88
	 * : True for permanent deletion and false for moving to trash.
89
	 * ---
90
	 * default: false
91
	 * options:
92
	 *   - true
93
	 *   - false
94
	 * ---
95
	 *
96
	 * @subcommand by-status
97
	 */
98
	public function by_status( $args, $assoc_args ) {
99
		$options          = $this->get_defaults();
100
		$mandatory_fields = array( 'post_status' );
101
		$this->validate( $assoc_args, $mandatory_fields );
102
		$status_module          = new DeletePostsByStatusModule();
103
		$options['post_status'] = $assoc_args['post_status'];
104
105
		if ( array_key_exists( 'limit_to', $assoc_args ) ) {
106
			$options['limit_to'] = $assoc_args['limit_to'];
107
		}
108
109
		if ( array_key_exists( 'restrict', $assoc_args ) ) {
110
			$options['restrict'] = $assoc_args['restrict'];
111
		}
112
113
		if ( array_key_exists( 'force_delete', $assoc_args ) ) {
114
			$options['force_delete'] = $assoc_args['force_delete'];
115
		}
116
117
		$count = $status_module->delete( $options );
118
		\WP_CLI::success( 'Deleted ' . $count . ' posts with ' . $options['post_status'] . ' status' );
119
	}
120
}
121