Passed
Push — 729-fix/add-delete-comments-by... ( 71abb9 )
by
unknown
09:37
created

CommentsModule::delete_comments_by_id()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 6
nop 2
dl 0
loc 16
ccs 0
cts 12
cp 0
crap 20
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
		$comments = $this->query_comments( $query );
78
79
		$deleted_comments_count = $this->delete_comments_by_id( $comments, $options['force_delete'] );
80
81
		return $deleted_comments_count;
82
	}
83
84
	/**
85
	 * Query comments using options.
86
	 *
87
	 * @param array $options Query options.
88
	 *
89
	 * @return array List of comment IDs.
90
	 */
91
	protected function query_comments( $options ) {
92
		$defaults = array(
93
			'update_comment_meta_cache' => false,
94
			'fields'                    => 'ids',
95
		);
96
97
		$options = wp_parse_args( $options, $defaults );
98
99
		$wp_comment_query = new \WP_Comment_Query();
100
101
		/**
102
		 * This action before the query happens.
103
		 *
104
		 * @since 6.1.0
105
		 *
106
		 * @param \WP_Comment_Query $wp_comment_query Query object.
107
		 */
108
		do_action( 'bd_before_query', $wp_comment_query );
109
110
		$comments = (array) $wp_comment_query->query( $options );
111
112
		/**
113
		 * This action runs after the query happens.
114
		 *
115
		 * @since 6.1.0
116
		 *
117
		 * @param \WP_Comment_Query $wp_comment_query Query object.
118
		 */
119
		do_action( 'bd_after_query', $wp_comment_query );
120
121
		return $comments;
122
	}
123
124
	/**
125
	 * Delete comments by ids.
126
	 *
127
	 * @param int[] $comment_ids  List of comment ids to delete.
128
	 * @param bool  $force_delete True to force delete comments, False otherwise.
129
	 *
130
	 * @return int Number of comments deleted.
131
	 */
132
	protected function delete_comments_by_id( $comment_ids, $force_delete ) {
133
		$count = 0;
134
135
		if ( ! function_exists( 'wp_delete_comment' ) ) {
136
			require_once ABSPATH . 'wp-admin/includes/comment.php';
137
		}
138
139
		foreach ( $comment_ids as $comment_id ) {
140
			$deleted = wp_delete_comment( $comment_id, $force_delete );
141
142
			if ( $deleted ) {
143
				$count ++;
144
			}
145
		}
146
147
		return $count;
148
	}
149
150
	/**
151
	 * Get the date query part for WP_Comment_Query.
152
	 *
153
	 * Date query corresponds to comment date.
154
	 *
155
	 * @param array $options Delete options.
156
	 *
157
	 * @return array Date Query.
158
	 */
159
	protected function get_date_query( $options ) {
160
		if ( ! $options['restrict'] ) {
161
			return array();
162
		}
163
164
		if ( $options['days'] <= 0 ) {
165
			return array();
166
		}
167
168
		if ( 'before' === $options['date_op'] || 'after' === $options['date_op'] ) {
169
			return array(
170
				$options['date_op'] => $options['days'] . ' days ago',
171
			);
172
		}
173
174
		return array();
175
	}
176
}
177