Completed
Push — master ( 58fb4c...ef04cf )
by Mike
02:29
created

Delete::delete_posts()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 62
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 6
eloc 31
c 1
b 0
f 1
nc 6
nop 2
dl 0
loc 62
rs 8.6652

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace testContent;
3
4
/**
5
 * Class to handle deletion of test data for the plugin.
6
 *
7
 * @package    WordPress
8
 * @subpackage Evans
9
 * @author     Old Town Media
10
 */
11
class Delete{
12
13
	/**
14
	 * Delete all test content created ever.
15
	 *
16
	 * @access private
17
	 */
18
	public function delete_all_test_data(){
19
20
		if ( !$this->user_can_delete() ){
21
			return;
22
		}
23
24
		// Loop through all post types and remove any test data
25
		$post_types = get_post_types( array( 'public' => true ), 'objects' );
26
		foreach ( $post_types as $post_type ) :
27
28
		    $this->delete_posts( $post_type->name );
29
30
		endforeach;
31
32
		// Loop through all taxonomies and remove any data
33
		$taxonomies = get_taxonomies();
34
		foreach ( $taxonomies as $tax ) :
35
36
		    $this->delete_terms( $tax );
37
38
		endforeach;
39
40
	}
41
42
43
	/**
44
	 * Delete test data posts.
45
	 *
46
	 * This function will search for all posts of a particular post type ($slug)
47
	 * and delete them all using a particular cmb flag that we set when creating
48
	 * the posts. Validates the user first.
49
	 *
50
	 * @see WP_Query, wp_delete_post
51
	 *
52
	 * @param string $slug a custom post type ID.
53
	 * @param boolean $echo Whether or not to echo the result
54
	 */
55
	public function delete_posts( $slug, $echo = false ){
56
57
		// Make sure that the current user is logged in & has full permissions.
58
		if ( !$this->user_can_delete() ){
59
			return;
60
		}
61
62
		// Check that $cptslg has a string.
63
		if ( empty( $slug ) ){
64
			return;
65
		}
66
67
		// Find our test data by the unique flag we set when we created the data
68
		$query = array(
69
			'post_type' 		=> $slug,
70
			'posts_per_page'	=> 500,
71
			'meta_query' 		=> array(
72
				array(
73
					'key'     => 'evans_test_content',
74
					'value'   => '__test__',
75
					'compare' => '=',
76
				),
77
			),
78
		);
79
80
		$objects = new \WP_Query( $query );
81
82
		if ( $objects->have_posts() ){
83
84
			$events = array();
85
86
			while ( $objects->have_posts() ) : $objects->the_post();
87
88
				// Find any media associated with the test post and delete it as well
89
				$this->delete_associated_media( get_the_id() );
90
91
				if ( $echo === true ){
92
					$events[] = array(
93
						'type'		=> 'deleted',
94
						'pid'		=> get_the_id(),
95
						'post_type'	=> get_post_type( get_the_id() ),
96
						'link'		=> ''
97
					);
98
				}
99
100
				// Force delete the post
101
				wp_delete_post( get_the_id(), true );
102
103
			endwhile;
104
105
			$obj = get_post_type_object( $slug );
106
107
			$events[] = array(
108
				'type'		=> 'general',
109
				'message'	=> __( 'Deleted', 'otm-test-content' ) . ' ' . $obj->labels->all_items
110
			);
111
112
			echo \json_encode( $events );
113
114
		}
115
116
	}
117
118
119
	/**
120
	 * Find and delete attachments associated with a post ID.
121
	 *
122
	 * This function finds each attachment that is associated with a post ID
123
	 * and deletes it completely from the site. This is to prevent leftover
124
	 * random images from sitting on the site forever.
125
	 *
126
	 * @access private
127
	 *
128
	 * @see get_attached_media, wp_delete_attachment
129
	 *
130
	 * @param int $pid a custom post type ID.
131
	 */
132
	private function delete_associated_media( $pid ){
133
134
		// Make sure that the current user is logged in & has full permissions.
135
		if ( !$this->user_can_delete() ){
136
			return;
137
		}
138
139
		// Make sure $pid is, in fact, an ID
140
		if ( !is_int( $pid ) ){
141
			return;
142
		}
143
144
		// Get our images
145
		$media = get_attached_media( 'image', $pid );
146
147
		if ( !empty( $media ) ){
148
149
			// Loop through the media & delete each one
150
			foreach ( $media as $attachment ){
151
				wp_delete_attachment( $attachment->ID, true );
152
			}
153
154
		}
155
156
	}
157
158
159
	/**
160
	 * Delete test data terms.
161
	 *
162
	 * This function will search for all terms of a particular taxonomy ($slug)
163
	 * and delete them all using a particular term_meta flag that we set when creating
164
	 * the posts. Validates the user first.
165
	 *
166
	 * @see WP_Query, wp_delete_post
167
	 *
168
	 * @param string $slug a custom post type ID.
169
	 * @param boolean $echo Whether or not to echo the result
170
	 */
171
	public function delete_terms( $slug, $echo = false ){
172
173
		// Make sure that the current user is logged in & has full permissions.
174
		if ( !$this->user_can_delete() ){
175
			return;
176
		}
177
178
		// Check that $cptslg has a string.
179
		if ( empty( $slug ) ){
180
			return;
181
		}
182
183
		// Query for our terms
184
		$args = array(
185
		    'hide_empty' => false,
186
		    'meta_query' => array(
187
		        array(
188
		           'key'       => 'evans_test_content',
189
		           'value'     => '__test__',
190
		           'compare'   => '='
191
		        )
192
		    )
193
		);
194
195
		$terms = get_terms( $slug, $args );
196
197
		if ( !empty( $terms ) ){
198
199
			$events = array();
200
201
			foreach ( $terms as $term ){
202
203
				if ( $echo === true ){
204
					$events[] = array(
205
						'type'		=> 'deleted',
206
						'pid'		=> $term->term_id,
207
						'post_type'	=> $slug,
208
						'link'		=> ''
209
					);
210
				}
211
212
				// Delete our term
213
				wp_delete_term( $term->term_id, $slug );
214
215
			}
216
217
			$taxonomy = get_taxonomy( $slug );
218
219
			$events[] = array(
220
				'type'		=> 'general',
221
				'message'	=> __( 'Deleted', 'otm-test-content' ) . ' ' . $taxonomy->labels->name
222
			);
223
224
			echo \json_encode( $events );
225
226
		}
227
228
	}
229
230
231
	/**
232
	 * Run some checks to make sure that our user is allowed to delete data.
233
	 *
234
	 * @see is_user_logged_in, current_user_can
235
	 */
236
	private function user_can_delete(){
237
238
		// User must be logged in
239
		if ( !is_user_logged_in() ){
240
			return false;
241
		}
242
243
		// User must have editor priveledges, at a minimum
244
		if ( !current_user_can( 'delete_others_posts' ) ){
245
			return false;
246
		}
247
248
		// We passed all the checks, hooray!
249
		return true;
250
251
	}
252
253
254
}
255