1
|
|
|
<?php |
2
|
|
|
namespace testContent\Types; |
3
|
|
|
use testContent\Abstracts as Abs; |
4
|
|
|
use testContent\TestContent as TestContent; |
5
|
|
|
use testContent\Delete as Delete; |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class to build test data for terms. |
10
|
|
|
* |
11
|
|
|
* @package WordPress |
12
|
|
|
* @subpackage Evans |
13
|
|
|
* @author Old Town Media |
14
|
|
|
*/ |
15
|
|
|
class Term extends Abs\Type{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* type |
19
|
|
|
* Defines type slug for use elsewhere in the plugin |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
* @access protected |
23
|
|
|
*/ |
24
|
|
|
protected $type = 'term'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Create test data posts. |
28
|
|
|
* |
29
|
|
|
* This is where the magic begins. We accept a cpt id (slug) and potntially |
30
|
|
|
* a number of posts to create. We then fetch the supports & metaboxes |
31
|
|
|
* for that cpt and feed them into a function to create each post individually. |
32
|
|
|
* |
33
|
|
|
* @access private |
34
|
|
|
* |
35
|
|
|
* @see $this->get_cpt_supports, $this->get_metaboxes, $this->create_test_object |
36
|
|
|
* |
37
|
|
|
* @param string $slug a custom post type ID. |
38
|
|
|
* @param int $num Optional. Number of posts to create. |
39
|
|
|
*/ |
40
|
|
|
public function create_objects( $slug, $connection, $num = '' ){ |
41
|
|
|
|
42
|
|
|
// If we're missing a custom post type id - don't do anything |
43
|
|
|
if ( empty( $slug ) ){ |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Set our connection status for the rest of the methods |
48
|
|
|
$this->connected = $connection; |
49
|
|
|
|
50
|
|
|
// If we forgot to put in a quantity, make one for us |
51
|
|
|
if ( empty( $num ) ){ |
52
|
|
|
$num = rand( 5, 30 ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
// Create test terms |
56
|
|
|
for( $i = 0; $i < $num; $i++ ){ |
|
|
|
|
57
|
|
|
|
58
|
|
|
$return = $this->create_test_object( $slug ); |
59
|
|
|
|
60
|
|
|
return $return; |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Creates the individual test data object. |
69
|
|
|
* |
70
|
|
|
* Create individual posts for testing with. Gathers basic information such |
71
|
|
|
* as title, content, thumbnail, etc. and inserts them with the post. Also |
72
|
|
|
* adds metaboxes if applicable . |
73
|
|
|
* |
74
|
|
|
* @access private |
75
|
|
|
* |
76
|
|
|
* @see TestContent, wp_insert_post, add_post_meta, update_post_meta, $this->random_metabox_content |
77
|
|
|
* |
78
|
|
|
* @param string $slug a custom post type ID. |
79
|
|
|
*/ |
80
|
|
|
private function create_test_object( $slug ){ |
81
|
|
|
|
82
|
|
|
// Get a random title |
83
|
|
|
$title = apply_filters( "tc_{$slug}_term_title", TestContent::title() ); |
84
|
|
|
|
85
|
|
|
$return = wp_insert_term( |
86
|
|
|
$title, |
87
|
|
|
$slug, |
88
|
|
|
apply_filters( "tc_{$slug}_term_arguments", array( |
89
|
|
|
'description'=> TestContent::title(), |
90
|
|
|
'slug' => sanitize_title( $title ), |
91
|
|
|
) |
92
|
|
|
) |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
// Then, set a test content flag on the new post for later deletion |
96
|
|
|
add_term_meta( $return['term_id'], 'evans_test_content', '__test__', true ); |
97
|
|
|
|
98
|
|
|
// Check if we have errors and return them or created message |
99
|
|
|
if ( is_wp_error( $return ) ){ |
100
|
|
|
error_log( $return->get_error_message() ); |
101
|
|
|
return $return; |
102
|
|
|
} else { |
103
|
|
|
return array( |
104
|
|
|
'action' => 'created', |
105
|
|
|
'object' => 'term', |
106
|
|
|
'oid' => $return['term_id'], |
107
|
|
|
'type' => $slug, |
108
|
|
|
'link_edit' => admin_url( '/edit-tags.php?action=edit&taxonomy='.$slug.'&tag_ID='.$return['term_id'] ), |
109
|
|
|
'link_view' => get_term_link( $return['term_id'] ) |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Delete all test data, regardless of type, within terms. |
119
|
|
|
* |
120
|
|
|
* @see Delete |
121
|
|
|
*/ |
122
|
|
|
public function delete_all(){ |
123
|
|
|
|
124
|
|
|
$delete = new Delete; |
125
|
|
|
|
126
|
|
|
// Make sure that the current user is logged in & has full permissions. |
127
|
|
|
if ( ! $delete->user_can_delete() ){ |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
// Loop through all taxonomies and remove any data |
132
|
|
|
$taxonomies = get_taxonomies(); |
133
|
|
|
foreach ( $taxonomies as $tax ) : |
134
|
|
|
|
135
|
|
|
$this->delete( $tax ); |
136
|
|
|
|
137
|
|
|
endforeach; |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Delete test data terms. |
144
|
|
|
* |
145
|
|
|
* This function will search for all terms of a particular taxonomy ($slug) |
146
|
|
|
* and delete them all using a particular term_meta flag that we set when creating |
147
|
|
|
* the posts. Validates the user first. |
148
|
|
|
* |
149
|
|
|
* @see WP_Query, wp_delete_post |
150
|
|
|
* |
151
|
|
|
* @param string $slug a custom post type ID. |
152
|
|
|
*/ |
153
|
|
|
public function delete( $slug ){ |
154
|
|
|
|
155
|
|
|
$delete = new Delete; |
156
|
|
|
|
157
|
|
|
// Make sure that the current user is logged in & has full permissions. |
158
|
|
|
if ( !$delete->user_can_delete() ){ |
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// Check that $cptslg has a string. |
163
|
|
|
if ( empty( $slug ) ){ |
164
|
|
|
return; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Query for our terms |
168
|
|
|
$args = array( |
169
|
|
|
'hide_empty' => false, |
170
|
|
|
'meta_query' => array( |
171
|
|
|
array( |
172
|
|
|
'key' => 'evans_test_content', |
173
|
|
|
'value' => '__test__', |
174
|
|
|
'compare' => '=' |
175
|
|
|
) |
176
|
|
|
) |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$terms = get_terms( $slug, $args ); |
180
|
|
|
|
181
|
|
|
if ( !empty( $terms ) ){ |
182
|
|
|
|
183
|
|
|
$events = array(); |
184
|
|
|
|
185
|
|
|
foreach ( $terms as $term ){ |
186
|
|
|
|
187
|
|
|
$events[] = array( |
188
|
|
|
'action' => 'deleted', |
189
|
|
|
'oid' => $term->term_id, |
190
|
|
|
'type' => $slug, |
191
|
|
|
'link' => '' |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
// Delete our term |
195
|
|
|
wp_delete_term( $term->term_id, $slug ); |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$taxonomy = get_taxonomy( $slug ); |
200
|
|
|
|
201
|
|
|
$events[] = array( |
202
|
|
|
'action' => 'general', |
203
|
|
|
'message' => __( 'Deleted', 'otm-test-content' ) . ' ' . $taxonomy->labels->name |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
return $events; |
207
|
|
|
|
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.