Passed
Pull Request — dev/6.0.0 (#382)
by Rajan
27:52 queued 20:26
created

PostsModule::render_sticky_post_dropdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 22
ccs 0
cts 7
cp 0
rs 9.7
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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
		$js_array['msg']['selectTag']                      = __( 'Please select at least one tag', 'bulk-delete' );
72
73
		$js_array['validators']['delete_posts_by_url'] = 'validateUrl';
74
		$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
		$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
	 *
91
	 * @return array Delete options array
92
	 */
93 74
	protected function build_query_options( $options, $query ) {
94 74
		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 74
	protected function query( $query ) {
105 74
		return bd_query( $query );
106
	}
107
108 65
	protected function do_delete( $options ) {
109 65
		$query = $this->build_query( $options );
110
111 65
		if ( empty( $query ) ) {
112
			// Short circuit deletion, if nothing needs to be deleted.
113
			return 0;
114
		}
115
116 65
		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 74
	protected function delete_posts_from_query( $query, $options ) {
128 74
		$force_delete = isset( $options['force_delete'] ) ? $options['force_delete'] : false;
129 74
		$query        = $this->build_query_options( $options, $query );
130 74
		$post_ids     = $this->query( $query );
131
132 74
		return $this->delete_posts_by_id( $post_ids, $force_delete );
133
	}
134
135
	/**
136
	 * Render the "private post" setting fields.
137
	 */
138
	protected function render_private_post_settings() {
139
		if ( $this->are_private_posts_present() ) {
140
			bd_render_private_post_settings( $this->field_slug );
141
		}
142
	}
143
144
	/**
145
	 * Render Category dropdown.
146
	 */
147
	protected function render_category_dropdown() {
148
		$categories = $this->get_categories();
149
		?>
150
151
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
152
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
153
				data-taxonomy="category" multiple>
154
155
			<option value="all">
156
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
157
			</option>
158
159
			<?php foreach ( $categories as $category ) : ?>
160
				<option value="<?php echo absint( $category->cat_ID ); ?>">
161
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
162
				</option>
163
			<?php endforeach; ?>
164
165
		</select>
166
		<?php
167
	}
168
169
	/**
170
	 * Render Tags dropdown.
171
	 */
172
	protected function render_tags_dropdown() {
173
		$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...
174
		?>
175
176
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"
177
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>"
178
				data-taxonomy="post_tag" multiple>
179
180
			<option value="all">
181
				<?php _e( 'All Tags', 'bulk-delete' ); ?>
182
			</option>
183
184
			<?php foreach ( $tags as $tag ) : ?>
0 ignored issues
show
Bug introduced by
The expression $tags of type null is not traversable.
Loading history...
185
				<option value="<?php echo absint( $tag->term_id ); ?>">
186
					<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
187
				</option>
188
			<?php endforeach; ?>
189
		</select>
190
		<?php
191
	}
192
193
	/**
194
	 * Render Sticky Posts dropdown.
195
	 */
196
	protected function render_sticky_post_dropdown() {
197
		$posts = $this->get_sticky_posts();
198
		?>
199
		<table class="optiontable">
200
			<tr>
201
				<td scope="row">
202
					<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="All">
203
					<label>All</label>
204
				</td>
205
			</tr>
206
			<?php
207
			foreach ( $posts as $post ) :
208
				$user = get_userdata( $post->post_author );
209
				?>
210
			<tr>
211
				<td scope="row">
212
				<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>">
213
				<label><?php echo esc_html( $post->post_title . ' Published by ' . $user->display_name . ' on ' . $post->post_date ); ?></label>
214
				</td>
215
			</tr>
216
			<?php endforeach; ?>
217
		</table>
218
		<?php
219
	}
220
221
	/**
222
	 * Get the list of sticky posts.
223
	 *
224
	 * @return array List of sticky posts.
225
	 */
226
	protected function get_sticky_posts() {
227
		$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) );
228
229
		return $posts;
230
	}
231
232
	/**
233
	 * Get the list of categories.
234
	 *
235
	 * @return array List of categories.
236
	 */
237
	protected function get_categories() {
238
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
239
240
		$categories = get_categories(
241
			array(
242
				'hide_empty' => false,
243
				'number'     => $enhanced_select_threshold,
244
			)
245
		);
246
247
		return $categories;
248
	}
249
250
	/**
251
	 * Are tags present in this WordPress installation?
252
	 *
253
	 * Only one tag is retrieved to check if tags are present for performance reasons.
254
	 *
255
	 * @return bool True if tags are present, False otherwise.
256
	 */
257
	protected function are_tags_present() {
258
		$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...
259
260
		return ( count( $tags ) > 0 );
261
	}
262
263
	/**
264
	 * Are sticky post present in this WordPress?
265
	 *
266
	 * Only one post is retrieved to check if stick post are present for performance reasons.
267
	 *
268
	 * @return bool True if posts are present, False otherwise.
269
	 */
270
	protected function are_sticky_post_present() {
271
		$sticky_post_ids = get_option( 'sticky_posts' );
272
273
		if ( ! is_array( $sticky_post_ids ) ) {
274
			return false;
275
		}
276
277
		return ( count( $sticky_post_ids ) > 0 );
278
	}
279
280
	/**
281
	 * Get the list of tags.
282
	 *
283
	 * @param int $max_count The maximum number of tags to be returned (Optional). Default 0.
284
	 *                       If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned.
285
	 */
286
	protected function get_tags( $max_count = 0 ) {
287
		if ( absint( $max_count ) === 0 ) {
288
			$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...
289
		}
290
	}
291
292
	/**
293
	 * Delete sticky posts.
294
	 *
295
	 * @param bool $force_delete Whether to bypass trash and force deletion.
296
	 *
297
	 * @return int Number of posts deleted.
298
	 */
299
	protected function delete_sticky_posts( $force_delete ) {
300
		$sticky_post_ids = get_option( 'sticky_posts' );
301
302
		if ( ! is_array( $sticky_post_ids ) ) {
303
			return 0;
304
		}
305
306
		return $this->delete_posts_by_id( $sticky_post_ids, $force_delete );
307
	}
308
309
	/**
310
	 * Delete posts by ids.
311
	 *
312
	 * @param int[] $post_ids     List of post ids to delete.
313
	 * @param bool  $force_delete True to force delete posts, False otherwise.
314
	 *
315
	 * @return int Number of posts deleted.
316
	 */
317 83
	protected function delete_posts_by_id( $post_ids, $force_delete ) {
318 83
		foreach ( $post_ids as $post_id ) {
319
			// `$force_delete` parameter to `wp_delete_post` won't work for custom post types.
320
			// See https://core.trac.wordpress.org/ticket/43672
321 81
			if ( $force_delete ) {
322 22
				wp_delete_post( $post_id, true );
323
			} else {
324 81
				wp_trash_post( $post_id );
325
			}
326
		}
327
328 83
		return count( $post_ids );
329
	}
330
}
331