Passed
Push — analysis-XZNdyg ( 545f48 )
by Sudar
70:39 queued 51s
created

CommentsModule::do_delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

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