Passed
Pull Request — dev/6.1.0 (#661)
by Sudar
08:49 queued 06:03
created

CommentsModule::do_delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Comments;
4
5
use BulkWP\BulkDelete\Core\Base\BaseModule;
6
7
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Module for deleting comments.
11
 *
12
 * @since 6.1.0
13
 */
14
abstract class CommentsModule extends BaseModule {
15
	protected $item_type = 'comments';
16
17
	/**
18
	 * Build query params for WP_Comment_Query by using delete options.
19
	 *
20
	 * Return an empty query array to short-circuit deletion.
21
	 *
22
	 * @param array $options Delete options.
23
	 *
24
	 * @return array Query.
25
	 */
26
	abstract protected function build_query( $options );
27
28
	protected function render_restrict_settings( $item = 'comments' ) {
29
		bd_render_restrict_settings( $this->field_slug, $item );
30
	}
31
32
	/**
33
	 * Handle common filters.
34
	 *
35
	 * @param array $request Request array.
36
	 *
37
	 * @return array User options.
38
	 */
39
	protected function parse_common_filters( $request ) {
40
		$options = array();
41
42
		$options['restrict']     = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_restrict', false );
43
		$options['limit_to']     = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_limit_to', 0 ) );
44
		$options['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false );
45
46
		if ( $options['restrict'] ) {
47
			$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' );
48
			$options['days']    = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) );
49
		}
50
51
		return $options;
52
	}
53
54
	// phpcs:ignore Squiz.Commenting.FunctionComment.Missing
55
	protected function do_delete( $options ) {
56
		$query = $this->build_query( $options );
57
58
		if ( empty( $query ) ) {
59
			// Short circuit deletion, if nothing needs to be deleted.
60
			return 0;
61
		}
62
63
		return $this->delete_comments_from_query( $query, $options );
64
	}
65
66
	/**
67
	 * Query and Delete comments.
68
	 *
69
	 * @access protected
70
	 *
71
	 * @param array $query   Options to query comments.
72
	 * @param array $options Delete options.
73
	 *
74
	 * @return int Number of comments deleted.
75
	 */
76
	protected function delete_comments_from_query( $query, $options ) {
77
		$count    = 0;
78
		$comments = $this->query_comments( $query );
79
80
		if ( ! function_exists( 'wp_delete_comment' ) ) {
81
			require_once ABSPATH . 'wp-admin/includes/comment.php';
82
		}
83
		foreach ( $comments as $comment ) {
84
			$deleted = wp_delete_comment( $comment, $options['force_delete'] );
85
86
			if ( $deleted ) {
87
				$count ++;
88
			}
89
		}
90
91
		return $count;
92
	}
93
94
	/**
95
	 * Query comments using options.
96
	 *
97
	 * @param array $options Query options.
98
	 *
99
	 * @return array List of comment IDs.
100
	 */
101
	protected function query_comments( $options ) {
102
		$defaults = array(
103
			'update_comment_meta_cache' => false,
104
			'fields'                    => 'ids',
105
		);
106
107
		$options = wp_parse_args( $options, $defaults );
108
109
		$wp_comment_query = new \WP_Comment_Query();
110
111
		/**
112
		 * This action before the query happens.
113
		 *
114
		 * @since 6.1.0
115
		 *
116
		 * @param \WP_Comment_Query $wp_comment_query Query object.
117
		 */
118
		do_action( 'bd_before_query', $wp_comment_query );
119
120
		$comments = (array) $wp_comment_query->query( $options );
121
122
		/**
123
		 * This action runs after the query happens.
124
		 *
125
		 * @since 6.1.0
126
		 *
127
		 * @param \WP_Comment_Query $wp_comment_query Query object.
128
		 */
129
		do_action( 'bd_after_query', $wp_comment_query );
130
131
		return $comments;
132
	}
133
134
	/**
135
	 * Get the date query part for WP_Comment_Query.
136
	 *
137
	 * Date query corresponds to comment date.
138
	 *
139
	 * @param array $options Delete options.
140
	 *
141
	 * @return array Date Query.
142
	 */
143
	protected function get_date_query( $options ) {
144
		if ( ! $options['restrict'] ) {
145
			return array();
146
		}
147
148
		if ( $options['days'] <= 0 ) {
149
			return array();
150
		}
151
152
		if ( 'before' === $options['date_op'] || 'after' === $options['date_op'] ) {
153
			return array(
154
				$options['date_op'] => $options['days'] . ' days ago',
155
			);
156
		}
157
158
		return array();
159
	}
160
}
161