Passed
Pull Request — dev/6.1.0 (#678)
by
unknown
09:20 queued 06:38
created

DeletePostsCommand::by_status()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 2
b 0
f 0
nc 12
nop 2
dl 0
loc 26
ccs 0
cts 22
cp 0
crap 30
rs 9.3888
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 bool True for success and False for failure.
32
	 */
33
	private 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
38
				return false;
39
			}
40
		}
41
42
		return true;
43
	}
44
45
	/**
46
	 * Get default options.
47
	 *
48
	 * @return array $defaults
49
	 */
50
	private function get_defaults() {
51
		$defaults                   = array();
52
		$defaults['restrict']       = false;
53
		$defaults['limit_to']       = 0;
54
		$defaults['exclude_sticky'] = false;
55
		$defaults['force_delete']   = false;
56
57
		return $defaults;
58
	}
59
60
	/**
61
	 * Delete post by status.
62
	 *
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
	 * @param array $args       Arguments to be supplied.
99
	 * @param array $assoc_args Associative arguments to be supplied.
100
	 *
101
	 * @return void
102
	 */
103
	public function by_status( $args, $assoc_args ) {
104
		$options          = $this->get_defaults();
105
		$mandatory_fields = array( 'post_status' );
106
		$this->validate( $assoc_args, $mandatory_fields );
107
		$status_module          = new DeletePostsByStatusModule();
108
		$options['post_status'] = $assoc_args['post_status'];
109
110
		if ( array_key_exists( 'limit_to', $assoc_args ) ) {
111
			$options['limit_to'] = $assoc_args['limit_to'];
112
		}
113
114
		if ( array_key_exists( 'restrict', $assoc_args ) ) {
115
			$options['restrict'] = $assoc_args['restrict'];
116
			if ( $options['restrict'] ) {
117
				$this->validate( $assoc_args, array( 'date_op', 'days' ) );
118
				$options['date_op'] = $assoc_args['date_op'];
119
				$options['days']    = $assoc_args['days'];
120
			}
121
		}
122
123
		if ( array_key_exists( 'force_delete', $assoc_args ) ) {
124
			$options['force_delete'] = $assoc_args['force_delete'];
125
		}
126
127
		$count = $status_module->delete( $options );
128
		\WP_CLI::success( 'Deleted ' . $count . ' posts with ' . $options['post_status'] . ' status' );
129
	}
130
}
131