Passed
Pull Request — dev/6.0.0 (#382)
by Rajan
12:16
created

PostsModule::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
namespace BulkWP\BulkDelete\Core\Posts;
3
4
use BulkWP\BulkDelete\Core\Base\BaseModule;
5
6 1
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
7
8
/**
9
 * Module for deleting posts.
10
 *
11
 * @since 6.0.0
12
 */
13
abstract class PostsModule extends BaseModule {
14
	/**
15
	 * Build query params for WP_Query by using delete options.
16
	 *
17
	 * Return an empty query array to short-circuit deletion.
18
	 *
19
	 * @param array $options Delete options.
20
	 *
21
	 * @return array Query.
22
	 */
23
	abstract protected function build_query( $options );
24
25
	/**
26
	 * Item Type. Possible values 'posts', 'pages', 'users' etc.
27
	 *
28
	 * @var string
29
	 */
30
	protected $item_type = 'posts';
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
		$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' );
47
		$options['days']    = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) );
48
49
		return $options;
50
	}
51
52
	/**
53
	 * Filter JS Array and add pro hooks.
54
	 *
55
	 * @since 5.5
56
	 *
57
	 * @param array $js_array JavaScript Array.
58
	 *
59
	 * @return array Modified JavaScript Array
60
	 */
61
	public function filter_js_array( $js_array ) {
62
		$js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' );
63
		$js_array['msg']['selectPostOption']   = __( 'Please select posts from at least one option', 'bulk-delete' );
64
65
		$js_array['validators']['delete_posts_by_category'] = 'validateSelect2';
66
		$js_array['error_msg']['delete_posts_by_category']  = 'selectCategory';
67
		$js_array['msg']['selectCategory']                  = __( 'Please select at least one category', 'bulk-delete' );
68
69
		$js_array['validators']['delete_posts_by_tag']     = 'validateSelect2';
70
		$js_array['error_msg']['delete_posts_by_category'] = 'selectTag';
71 56
		$js_array['msg']['selectTag']                      = __( 'Please select at least one tag', 'bulk-delete' );
72 56
73
		$js_array['validators']['delete_posts_by_url'] = 'validateUrl';
74 56
		$js_array['error_msg']['delete_posts_by_url']  = 'enterUrl';
75
		$js_array['msg']['enterUrl']                   = __( 'Please enter at least one post url', 'bulk-delete' );
76
77
		$js_array['dt_iterators'][] = '_cats';
78
		$js_array['dt_iterators'][] = '_tags';
79 56
		$js_array['dt_iterators'][] = '_taxs';
80
		$js_array['dt_iterators'][] = '_post_status';
81
82
		return $js_array;
83
	}
84
85
	/**
86
	 * Helper function to build the query params.
87
	 *
88
	 * @param array $options Delete Options.
89
	 * @param array $query   Params for WP Query.
90 65
	 *
91 65
	 * @return array Delete options array
92 65
	 */
93
	protected function build_query_options( $options, $query ) {
94 65
		return bd_build_query_options( $options, $query );
95
	}
96
97
	/**
98
	 * Helper function for bd_query which runs query.
99
	 *
100
	 * @param array $query Params for WP Query.
101
	 *
102
	 * @return array Deleted Post IDs array
103
	 */
104
	protected function query( $query ) {
105
		return bd_query( $query );
106
	}
107
108
	protected function do_delete( $options ) {
109
		$query = $this->build_query( $options );
110
111
		if ( empty( $query ) ) {
112
			// Short circuit deletion, if nothing needs to be deleted.
113
			return 0;
114
		}
115
116
		return $this->delete_posts_from_query( $query, $options );
117
	}
118
119
	/**
120
	 * Build the query using query params and then Delete posts.
121
	 *
122
	 * @param array $query   Params for WP Query.
123
	 * @param array $options Delete Options.
124
	 *
125
	 * @return int Number of posts deleted.
126
	 */
127
	protected function delete_posts_from_query( $query, $options ) {
128
		$force_delete = isset( $options['force_delete'] ) ? $options['force_delete'] : false;
129
		$query    = $this->build_query_options( $options, $query );
130
		$post_ids = $this->query( $query );
131 74
		return $this->delete_posts_by_id( $post_ids, $force_delete );
132 74
	}
133
134
	/**
135 72
	 * Render the "private post" setting fields.
136 22
	 */
137
	protected function render_private_post_settings() {
138 72
		if ( $this->are_private_posts_present() ) {
139
			bd_render_private_post_settings( $this->field_slug );
140
		}
141
	}
142 74
143
	/**
144
	 * Render Category dropdown.
145
	 */
146
	protected function render_category_dropdown() {
147
		$categories = $this->get_categories();
148
		?>
149
150
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
151
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
152
				data-taxonomy="category" multiple>
153
154
			<option value="all">
155
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
156
			</option>
157
158
			<?php foreach ( $categories as $category ) : ?>
159
				<option value="<?php echo absint( $category->cat_ID ); ?>">
160
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
161
				</option>
162
			<?php endforeach; ?>
163
164
		</select>
165
		<?php
166
	}
167
168
	/**
169
	 * Render Tags dropdown.
170
	 */
171
	protected function render_tags_dropdown() {
172
		$tags = $this->get_tags();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $tags is correct as $this->get_tags() targeting BulkWP\BulkDelete\Core\P...PostsModule::get_tags() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
173
		?>
174
175
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"
176
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>"
177
				data-taxonomy="post_tag" multiple>
178
179
			<option value="all">
180
				<?php _e( 'All Tags', 'bulk-delete' ); ?>
181
			</option>
182
183
			<?php foreach ( $tags as $tag ) : ?>
0 ignored issues
show
Bug introduced by
The expression $tags of type null is not traversable.
Loading history...
184
				<option value="<?php echo absint( $tag->term_id ); ?>">
185
					<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
186
				</option>
187
			<?php endforeach; ?>
188
		</select>
189
		<?php
190
	}
191
192
	/**
193
	 * Render Sticky Posts dropdown.
194
	 */
195
	protected function render_sticky_post_dropdown() {
196
		$posts = $this->get_sticky_posts();
197
		?>
198
		<table class="optiontable">
199
			<tr>
200
				<td scope="row">
201
					<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="All">
202
					<label>All</label>
203
				</td>
204
			</tr>
205
			<?php
206
			foreach ( $posts as $post ) :
207
				$user = get_userdata( $post->post_author );
208
				?>
209
			<tr>
210
				<td scope="row">
211
				<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>">
212
				<label><?php echo esc_html( $post->post_title . ' Published by ' . $user->display_name . ' on ' . $post->post_date ); ?></label>
213
				</td>
214
			</tr>
215
			<?php endforeach; ?>
216
		</table>
217
		<?php
218
	}
219
220
	/**
221
	 * Get the list of sticky posts.
222
	 *
223
	 * @return array List of sticky posts.
224
	 */
225
	protected function get_sticky_posts() {
226
		$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) );
227
228
		return $posts;
229
	}
230
231
	/**
232
	 * Get the list of categories.
233
	 *
234
	 * @return array List of categories.
235
	 */
236
	protected function get_categories() {
237
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
238
239
		$categories = get_categories(
240
			array(
241
				'hide_empty' => false,
242
				'number'     => $enhanced_select_threshold,
243
			)
244
		);
245
246
		return $categories;
247
	}
248
249
	/**
250
	 * Are tags present in this WordPress installation?
251
	 *
252
	 * Only one tag is retrieved to check if tags are present for performance reasons.
253
	 *
254
	 * @return bool True if tags are present, False otherwise.
255
	 */
256
	protected function are_tags_present() {
257
		$tags = $this->get_tags( 1 );
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $tags is correct as $this->get_tags(1) targeting BulkWP\BulkDelete\Core\P...PostsModule::get_tags() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
258
259
		return ( count( $tags ) > 0 );
260
	}
261
262
	/**
263
	 * Are sticky post present in this WordPress?
264
	 *
265
	 * Only one post is retrieved to check if stick post are present for performance reasons.
266
	 *
267
	 * @return bool True if posts are present, False otherwise.
268
	 */
269
	protected function are_sticky_post_present() {
270
		$sticky_post_ids = get_option( 'sticky_posts' );
271
272
		if ( ! is_array( $sticky_post_ids ) ) {
273
			return false;
274
		}
275
276
		return ( count( $sticky_post_ids ) > 0 );
277
	}
278
279
	/**
280
	 * Get the list of tags.
281
	 *
282
	 * @param int $max_count The maximum number of tags to be returned (Optional). Default 0.
283
	 *                       If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned.
284
	 */
285
	protected function get_tags( $max_count = 0 ) {
286
		if ( absint( $max_count ) === 0 ) {
287
			$max_count = $this->get_enhanced_select_threshold();
0 ignored issues
show
Unused Code introduced by
The assignment to $max_count is dead and can be removed.
Loading history...
288
		}
289
	}
290
291
	/**
292
	 * Delete sticky posts.
293
	 *
294
	 * @param bool $force_delete Whether to bypass trash and force deletion.
295
	 *
296
	 * @return int Number of posts deleted.
297
	 */
298
	protected function delete_sticky_posts( $force_delete ) {
299
		$sticky_post_ids = get_option( 'sticky_posts' );
300
301
		if ( ! is_array( $sticky_post_ids ) ) {
302
			return 0;
303
		}
304
305
		return $this->delete_posts_by_id( $sticky_post_ids, $force_delete );
306
	}
307
308
	/**
309
	 * Delete posts by ids.
310
	 *
311
	 * @param int[] $post_ids     List of post ids to delete.
312
	 * @param bool  $force_delete True to force delete posts, False otherwise.
313
	 *
314
	 * @return int Number of posts deleted.
315
	 */
316
	protected function delete_posts_by_id( $post_ids, $force_delete ) {
317
		foreach ( $post_ids as $post_id ) {
318
			// `$force_delete` parameter to `wp_delete_post` won't work for custom post types.
319
			// See https://core.trac.wordpress.org/ticket/43672
320
			if ( $force_delete ) {
321
				wp_delete_post( $post_id, true );
322
			} else {
323
				wp_trash_post( $post_id );
324
			}
325
		}
326
327
		return count( $post_ids );
328
	}
329
}
330