1
|
|
|
<?php |
2
|
|
|
namespace testContent; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class to build test data for custom post types. |
6
|
|
|
* |
7
|
|
|
* @package WordPress |
8
|
|
|
* @subpackage Evans |
9
|
|
|
* @author Old Town Media |
10
|
|
|
*/ |
11
|
|
|
class CreatePost{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* metaboxes |
15
|
|
|
* Easy access for the Metaboxes class. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
* @access private |
19
|
|
|
*/ |
20
|
|
|
private $metaboxes; |
21
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Constructor to load in the Metaboxes class. |
25
|
|
|
* |
26
|
|
|
* @see Metaboxes |
27
|
|
|
*/ |
28
|
|
|
public function __construct(){ |
29
|
|
|
|
30
|
|
|
$this->metaboxes = new Metaboxes; |
|
|
|
|
31
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Create test data posts. |
36
|
|
|
* |
37
|
|
|
* This is where the magic begins. We accept a cpt id (slug) and potntially |
38
|
|
|
* a number of posts to create. We then fetch the supports & metaboxes |
39
|
|
|
* for that cpt and feed them into a function to create each post individually. |
40
|
|
|
* |
41
|
|
|
* @access private |
42
|
|
|
* |
43
|
|
|
* @see $this->get_cpt_supports, $this->get_metaboxes, $this->create_test_object |
44
|
|
|
* |
45
|
|
|
* @param string $slug a custom post type ID. |
46
|
|
|
* @param boolean $echo Whether or not to echo. Optional. |
47
|
|
|
* @param int $num Optional. Number of posts to create. |
48
|
|
|
*/ |
49
|
|
|
public function create_post_type_content( $slug, $echo = false, $num = '' ){ |
50
|
|
|
|
51
|
|
|
// If we're missing a custom post type id - don't do anything |
52
|
|
|
if ( empty( $slug ) ){ |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Gather the necessary data to create the posts |
57
|
|
|
$supports = $this->get_cpt_supports( $slug ); |
58
|
|
|
$metaboxes = $this->metaboxes->get_metaboxes( $slug ); |
|
|
|
|
59
|
|
|
|
60
|
|
|
// If we forgot to put in a quantity, make one for us |
61
|
|
|
if ( empty( $num ) ){ |
62
|
|
|
$num = rand( 5, 30 ); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Create test posts |
66
|
|
View Code Duplication |
for( $i = 0; $i < $num; $i++ ){ |
|
|
|
|
67
|
|
|
|
68
|
|
|
$return = $this->create_test_object( $slug, $supports, $metaboxes ); |
69
|
|
|
|
70
|
|
|
if ( $echo === true ){ |
71
|
|
|
echo \json_encode( $return ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Creates the individual test data post. |
81
|
|
|
* |
82
|
|
|
* Create individual posts for testing with. Gathers basic information such |
83
|
|
|
* as title, content, thumbnail, etc. and inserts them with the post. Also |
84
|
|
|
* adds metaboxes if applicable . |
85
|
|
|
* |
86
|
|
|
* @access private |
87
|
|
|
* |
88
|
|
|
* @see TestContent, wp_insert_post, add_post_meta, update_post_meta, $this->random_metabox_content |
89
|
|
|
* |
90
|
|
|
* @param string $slug a custom post type ID. |
91
|
|
|
* @param array $supports Features that the post type supports. |
92
|
|
|
* @param array $supports All CMB2 metaboxes attached to the post type. |
93
|
|
|
*/ |
94
|
|
|
private function create_test_object( $slug, $supports, $metaboxes ){ |
95
|
|
|
$return = ''; |
96
|
|
|
|
97
|
|
|
// Get a random title |
98
|
|
|
$title = TestContent::title(); |
99
|
|
|
|
100
|
|
|
// First, insert our post |
101
|
|
|
$post = array( |
102
|
|
|
'post_name' => sanitize_title( $title ), |
103
|
|
|
'post_status' => 'publish', |
104
|
|
|
'post_type' => $slug, |
105
|
|
|
'ping_status' => 'closed', |
106
|
|
|
'comment_status' => 'closed', |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
// Add title if supported |
110
|
|
|
if ( $supports['title'] === true ){ |
111
|
|
|
$post['post_title'] = $title; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Add main content if supported |
115
|
|
|
if ( $supports['editor'] === true ){ |
116
|
|
|
$post['post_content'] = TestContent::paragraphs(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// Insert then post object |
120
|
|
|
$post_id = wp_insert_post( $post ); |
121
|
|
|
|
122
|
|
|
// Then, set a test content flag on the new post for later deletion |
123
|
|
|
add_post_meta( $post_id, 'evans_test_content', '__test__', true ); |
124
|
|
|
|
125
|
|
|
// Add thumbnail if supported |
126
|
|
|
if ( $supports['thumbnail'] === true || in_array( $slug, array( 'post', 'page' ) ) ){ |
127
|
|
|
update_post_meta( $post_id, '_thumbnail_id', TestContent::image( $post_id ) ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$taxonomies = get_object_taxonomies( $slug ); |
131
|
|
|
|
132
|
|
|
// Assign the post to terms |
133
|
|
|
if ( !empty( $taxonomies ) ){ |
134
|
|
|
$return .= $this->assign_terms( $post_id, $taxonomies ); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// Spin up metaboxes |
138
|
|
|
if ( !empty( $metaboxes ) ){ |
139
|
|
|
foreach ( $metaboxes as $cmb ) : |
140
|
|
|
$return .= $this->metaboxes->random_metabox_content( $post_id, $cmb ); |
|
|
|
|
141
|
|
|
endforeach; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// Check if we have errors and return them or created message |
145
|
|
|
if ( is_wp_error( $return ) ){ |
146
|
|
|
error_log( $return->get_error_message() ); |
|
|
|
|
147
|
|
|
return $return; |
148
|
|
|
} else { |
149
|
|
|
return array( |
150
|
|
|
'type' => 'created', |
151
|
|
|
'object' => 'post', |
152
|
|
|
'pid' => $post_id, |
153
|
|
|
'post_type' => get_post_type( $post_id ), |
154
|
|
|
'link_edit' => admin_url( '/post.php?post='.$post_id.'&action=edit' ), |
155
|
|
|
'link_view' => get_permalink( $post_id ), |
156
|
|
|
); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Assemble supports statements for a particular post type. |
164
|
|
|
* |
165
|
|
|
* @access private |
166
|
|
|
* |
167
|
|
|
* @see post_type_supports |
168
|
|
|
* |
169
|
|
|
* @param string $slug a custom post type ID. |
170
|
|
|
* @return array Array of necessary supports booleans. |
171
|
|
|
*/ |
172
|
|
|
private function get_cpt_supports( $slug ){ |
173
|
|
|
|
174
|
|
|
$supports = array( |
175
|
|
|
'title' => post_type_supports( $slug, 'title' ), |
176
|
|
|
'editor' => post_type_supports( $slug, 'editor' ), |
177
|
|
|
'thumbnail' => post_type_supports( $slug, 'thumbnail' ) |
178
|
|
|
); |
179
|
|
|
|
180
|
|
|
return $supports; |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Assigns taxonomies to the new post. |
187
|
|
|
* |
188
|
|
|
* Loop through every taxonomy type associated with a custom post type & |
189
|
|
|
* assign the post to a random item out of each taxonomy. Taxonomies must |
190
|
|
|
* have at least one term in them for this to work. |
191
|
|
|
* |
192
|
|
|
* @access private |
193
|
|
|
* |
194
|
|
|
* @param int $post_id a custom post type ID. |
195
|
|
|
* @param array $taxonomies taxonomies assigned to this cpt. |
196
|
|
|
* @return object WP Error if there is one. |
197
|
|
|
*/ |
198
|
|
|
private function assign_terms( $post_id, $taxonomies ){ |
199
|
|
|
|
200
|
|
|
// Make sure it's an array & has items |
201
|
|
|
if ( empty( $taxonomies ) || !is_array( $taxonomies ) ){ |
202
|
|
|
return; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
foreach ( $taxonomies as $tax ){ |
206
|
|
|
|
207
|
|
|
// Get the individual terms already existing |
208
|
|
|
$terms = get_terms( $tax, array( 'hide_empty' => false ) ); |
209
|
|
|
$count = count( $terms ) - 1; |
210
|
|
|
|
211
|
|
|
// If there are no terms, skip to the next taxonomy |
212
|
|
|
if ( empty( $terms ) ){ |
213
|
|
|
continue; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
// Get a random index to use |
217
|
|
|
$index = rand( 0, $count ); |
218
|
|
|
|
219
|
|
|
// Initialize our array |
220
|
|
|
$post_data = array( |
221
|
|
|
'ID' => $post_id |
222
|
|
|
); |
223
|
|
|
|
224
|
|
|
// Set the term data to update |
225
|
|
|
$post_data['tax_input'][ $tax ] = array( $terms[$index]->term_id ); |
226
|
|
|
|
227
|
|
|
// Update the post with the taxonomy info |
228
|
|
|
$return = wp_update_post( $post_data ); |
229
|
|
|
|
230
|
|
|
// Return the error if it exists |
231
|
|
|
if ( is_wp_error( $return ) ){ |
232
|
|
|
error_log( $return->get_error_messages() ); |
233
|
|
|
return $return->get_error_messages(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
} |
241
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..