Passed
Push — 341-feature/implement-link-on-... ( 162bd2...c83084 )
by Maria Daniel Deepak
12:26
created

PostsModule::get_trash_url()   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 0
dl 0
loc 2
ccs 0
cts 0
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
	protected $item_type = 'posts';
26
27
	/**
28
	 * Handle common filters.
29
	 *
30
	 * @param array $request Request array.
31
	 *
32
	 * @return array User options.
33
	 */
34
	protected function parse_common_filters( $request ) {
35
		$options = array();
36
37
		$options['restrict']     = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_restrict', false );
38
		$options['limit_to']     = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_limit_to', 0 ) );
39
		$options['force_delete'] = bd_array_get_bool( $request, 'smbd_' . $this->field_slug . '_force_delete', false );
40
41
		$options['date_op'] = bd_array_get( $request, 'smbd_' . $this->field_slug . '_op' );
42
		$options['days']    = absint( bd_array_get( $request, 'smbd_' . $this->field_slug . '_days' ) );
43
44
		return $options;
45
	}
46
47
	public function filter_js_array( $js_array ) {
48
		$js_array['msg']['deletePostsWarning'] = __( 'Are you sure you want to delete all the posts based on the selected option?', 'bulk-delete' );
49
		$js_array['msg']['selectPostOption']   = __( 'Please select posts from at least one option', 'bulk-delete' );
50
51
		$js_array['validators']['delete_posts_by_category'] = 'validateSelect2';
52
		$js_array['error_msg']['delete_posts_by_category']  = 'selectCategory';
53
		$js_array['msg']['selectCategory']                  = __( 'Please select at least one category', 'bulk-delete' );
54
55
		$js_array['validators']['delete_posts_by_tag']     = 'validateSelect2';
56
		$js_array['error_msg']['delete_posts_by_category'] = 'selectTag';
57
		$js_array['msg']['selectTag']                      = __( 'Please select at least one tag', 'bulk-delete' );
58
59
		$js_array['validators']['delete_posts_by_url'] = 'validateUrl';
60
		$js_array['error_msg']['delete_posts_by_url']  = 'enterUrl';
61
		$js_array['msg']['enterUrl']                   = __( 'Please enter at least one post url', 'bulk-delete' );
62
63
		$js_array['dt_iterators'][] = '_cats';
64
		$js_array['dt_iterators'][] = '_tags';
65
		$js_array['dt_iterators'][] = '_taxs';
66
		$js_array['dt_iterators'][] = '_post_status';
67
68
		return $js_array;
69
	}
70
71 47
	protected function do_delete( $options ) {
72 47
		$query = $this->build_query( $options );
73
74 47
		if ( empty( $query ) ) {
75
			// Short circuit deletion, if nothing needs to be deleted.
76
			return 0;
77
		}
78
79 47
		return $this->delete_posts_from_query( $query, $options );
80
	}
81
82
	/**
83
	 * Build the query using query params and then Delete posts.
84
	 *
85
	 * @param array $query   Params for WP Query.
86
	 * @param array $options Delete Options.
87
	 *
88
	 * @return int Number of posts deleted.
89
	 */
90 56
	protected function delete_posts_from_query( $query, $options ) {
91 56
		$query    = bd_build_query_options( $options, $query );
92 56
		$post_ids = bd_query( $query );
93
94 56
		return $this->delete_posts_by_id( $post_ids, $options['force_delete'] );
95
	}
96
97
	/**
98
	 * Render the "private post" setting fields.
99
	 */
100
	protected function render_private_post_settings() {
101
		bd_render_private_post_settings( $this->field_slug );
102
	}
103
104
	/**
105
	 * Render Category dropdown.
106
	 */
107
	protected function render_category_dropdown() {
108
		$categories = $this->get_categories();
109
		?>
110
111
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>_category[]" data-placeholder="<?php _e( 'Select Categories', 'bulk-delete' ); ?>"
112
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $categories ), 'select2-taxonomy' ) ); ?>"
113
				data-taxonomy="category" multiple>
114
115
			<option value="all">
116
				<?php _e( 'All Categories', 'bulk-delete' ); ?>
117
			</option>
118
119
			<?php foreach ( $categories as $category ) : ?>
120
				<option value="<?php echo absint( $category->cat_ID ); ?>">
121
					<?php echo esc_html( $category->cat_name ), ' (', absint( $category->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
122
				</option>
123
			<?php endforeach; ?>
124
125
		</select>
126
	<?php
127
	}
128
129
	/**
130
	 * Render Tags dropdown.
131
	 */
132
	protected function render_tags_dropdown() {
133
		$tags = $this->get_tags();
134
		?>
135
136
		<select name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" data-placeholder="<?php _e( 'Select Tags', 'bulk-delete' ); ?>"
137
				class="<?php echo sanitize_html_class( $this->enable_ajax_if_needed_to_dropdown_class_name( count( $tags ), 'select2-taxonomy' ) ); ?>"
138
				data-taxonomy="post_tag" multiple>
139
140
			<option value="all">
141
				<?php _e( 'All Tags', 'bulk-delete' ); ?>
142
			</option>
143
144
			<?php foreach ( $tags as $tag ) : ?>
145
				<option value="<?php echo absint( $tag->term_id ); ?>">
146
					<?php echo esc_html( $tag->name ), ' (', absint( $tag->count ), ' ', __( 'Posts', 'bulk-delete' ), ')'; ?>
147
				</option>
148
			<?php endforeach; ?>
149
		</select>
150
	<?php
151
	}
152
153
	/**
154
	 * Render Sticky Posts dropdown.
155
	 */
156
	protected function render_sticky_post_dropdown() {
157
		$posts = $this->get_sticky_posts();
158
		?>
159
		<table class="optiontable">
160
			<tr>
161
				<td scope="row">
162
					<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="All">
163
					<label>All</label>
164
				</td>
165
			</tr>
166
			<?php foreach ( $posts as $post ) :
167
			$user = get_userdata( $post->post_author );
168
			?>
169
			<tr>
170
				<td scope="row">
171
				<input type="checkbox" class="smbd_sticky_post_options" name="smbd_<?php echo esc_attr( $this->field_slug ); ?>[]" value="<?php echo absint( $post->ID ); ?>">
172
				<label><?php echo esc_html( $post->post_title . ' Published by ' . $user->display_name . ' on ' . $post->post_date ); ?></label>
173
				</td>
174
			</tr>
175
			<?php endforeach; ?>
176
		</table>
177
	<?php
178
	}
179
180
	/**
181
	 * Get the list of sticky posts.
182
	 *
183
	 * @return array List of sticky posts.
184
	 */
185
	protected function get_sticky_posts(){
186
		$posts = get_posts( array( 'post__in' => get_option( 'sticky_posts' ) ) );
187
188
		return $posts;
189
	}
190
191
	/**
192
	 * Get the list of categories.
193
	 *
194
	 * @return array List of categories.
195
	 */
196
	protected function get_categories() {
197
		$enhanced_select_threshold = $this->get_enhanced_select_threshold();
198
199
		$categories = get_categories(
200
			array(
201
				'hide_empty' => false,
202
				'number'     => $enhanced_select_threshold,
203
			)
204
		);
205
206
		return $categories;
207
	}
208
209
	/**
210
	 * Are tags present in this WordPress installation?
211
	 *
212
	 * Only one tag is retrieved to check if tags are present for performance reasons.
213
	 *
214
	 * @return bool True if tags are present, False otherwise.
215
	 */
216
	protected function are_tags_present() {
217
		$tags = $this->get_tags( 1 );
218
219
		return ( count( $tags ) > 0 );
220
	}
221
222
	/**
223
	 * Are sticky post present in this WordPress?
224
	 *
225
	 * Only one post is retrieved to check if stick post are present for performance reasons.
226
	 *
227
	 * @return bool True if posts are present, False otherwise.
228
	 */
229
	protected function are_sticky_post_present() {
230
		$sticky_post_ids = get_option( 'sticky_posts' );
231
232
		if ( ! is_array( $sticky_post_ids ) ) {
233
			return false;
234
		}
235
236
		return ( count( $sticky_post_ids ) > 0 );
237
	}
238
239
	/**
240
	 * Get the list of tags.
241
	 *
242
	 * @param int $max_count The maximum number of tags to be returned (Optional). Default 0.
243
	 *                       If 0 then the maximum number of tags specified in `get_enhanced_select_threshold` will be returned.
244
	 *
245
	 * @return array List of tags.
246
	 */
247
	protected function get_tags( $max_count = 0 ) {
248
		if ( absint( $max_count ) === 0 ) {
249
			$max_count = $this->get_enhanced_select_threshold();
250
		}
251
252
		$tags = get_tags(
253
			array(
254
				'hide_empty' => false,
255
				'number'     => $max_count,
256
			)
257
		);
258
259
		return $tags;
260
	}
261
262
	/**
263
	 * Delete sticky posts.
264
	 *
265
	 * @param bool $force_delete Whether to bypass trash and force deletion.
266
	 *
267
	 * @return int Number of posts deleted.
268
	 */
269
	protected function delete_sticky_posts( $force_delete ) {
270
		$sticky_post_ids = get_option( 'sticky_posts' );
271
272
		if ( ! is_array( $sticky_post_ids ) ) {
273
			return 0;
274
		}
275
276
		return $this->delete_posts_by_id( $sticky_post_ids, $force_delete );
277
	}
278
279
	/**
280
	 * Delete posts by ids.
281
	 *
282
	 * @param int[] $post_ids     List of post ids to delete.
283
	 * @param bool  $force_delete True to force delete posts, False otherwise.
284
	 *
285
	 * @return int Number of posts deleted.
286
	 */
287 65
	protected function delete_posts_by_id( $post_ids, $force_delete ) {
288 65
		foreach ( $post_ids as $post_id ) {
289
			// `$force_delete` parameter to `wp_delete_post` won't work for custom post types.
290
			// See https://core.trac.wordpress.org/ticket/43672
291 63
			if ( $force_delete ) {
292 17
				wp_delete_post( $post_id, true );
293
			} else {
294 63
				wp_trash_post( $post_id );
295
			}
296
		}
297
298 65
		return count( $post_ids );
299
	}
300
301
	/**
302
	 * {@inheritDoc}
303
	 */
304
	protected function get_trash_url() {
305
		return admin_url( 'edit.php?post_status=trash&post_type=post' );
306
	}
307
}
308