@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * @subpackage Evans |
9 | 9 | * @author Old Town Media |
10 | 10 | */ |
11 | -class CreatePost{ |
|
11 | +class CreatePost { |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * Create test data posts. |
@@ -25,29 +25,29 @@ discard block |
||
25 | 25 | * @param boolean $echo Whether or not to echo. Optional. |
26 | 26 | * @param int $num Optional. Number of posts to create. |
27 | 27 | */ |
28 | - public function create_post_type_content( $slug, $echo = false, $num = '' ){ |
|
28 | + public function create_post_type_content($slug, $echo = false, $num = '') { |
|
29 | 29 | |
30 | 30 | // If we're missing a custom post type id - don't do anything |
31 | - if ( empty( $slug ) ){ |
|
31 | + if (empty($slug)) { |
|
32 | 32 | return; |
33 | 33 | } |
34 | 34 | |
35 | 35 | // Gather the necessary data to create the posts |
36 | - $supports = $this->get_cpt_supports( $slug ); |
|
37 | - $metaboxes = $this->get_metaboxes( $slug ); |
|
36 | + $supports = $this->get_cpt_supports($slug); |
|
37 | + $metaboxes = $this->get_metaboxes($slug); |
|
38 | 38 | |
39 | 39 | // If we forgot to put in a quantity, make one for us |
40 | - if ( empty( $num ) ){ |
|
41 | - $num = rand( 5, 30 ); |
|
40 | + if (empty($num)) { |
|
41 | + $num = rand(5, 30); |
|
42 | 42 | } |
43 | 43 | |
44 | 44 | // Create test posts |
45 | - for( $i = 0; $i < $num; $i++ ){ |
|
45 | + for ($i = 0; $i < $num; $i++) { |
|
46 | 46 | |
47 | - $return = $this->create_test_object( $slug, $supports, $metaboxes ); |
|
47 | + $return = $this->create_test_object($slug, $supports, $metaboxes); |
|
48 | 48 | |
49 | - if ( $echo === true ){ |
|
50 | - echo \json_encode( $return ); |
|
49 | + if ($echo === true) { |
|
50 | + echo \json_encode($return); |
|
51 | 51 | } |
52 | 52 | |
53 | 53 | } |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | * @param array $supports Features that the post type supports. |
71 | 71 | * @param array $supports All CMB2 metaboxes attached to the post type. |
72 | 72 | */ |
73 | - private function create_test_object( $slug, $supports, $metaboxes ){ |
|
73 | + private function create_test_object($slug, $supports, $metaboxes) { |
|
74 | 74 | $return = ''; |
75 | 75 | |
76 | 76 | // Get a random title |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | |
79 | 79 | // First, insert our post |
80 | 80 | $post = array( |
81 | - 'post_name' => sanitize_title( $title ), |
|
81 | + 'post_name' => sanitize_title($title), |
|
82 | 82 | 'post_status' => 'publish', |
83 | 83 | 'post_type' => $slug, |
84 | 84 | 'ping_status' => 'closed', |
@@ -86,51 +86,51 @@ discard block |
||
86 | 86 | ); |
87 | 87 | |
88 | 88 | // Add title if supported |
89 | - if ( $supports['title'] === true ){ |
|
89 | + if ($supports['title'] === true) { |
|
90 | 90 | $post['post_title'] = $title; |
91 | 91 | } |
92 | 92 | |
93 | 93 | // Add main content if supported |
94 | - if ( $supports['editor'] === true ){ |
|
94 | + if ($supports['editor'] === true) { |
|
95 | 95 | $post['post_content'] = TestContent::paragraphs(); |
96 | 96 | } |
97 | 97 | |
98 | 98 | // Insert then post object |
99 | - $post_id = wp_insert_post( $post ); |
|
99 | + $post_id = wp_insert_post($post); |
|
100 | 100 | |
101 | 101 | // Then, set a test content flag on the new post for later deletion |
102 | - add_post_meta( $post_id, 'evans_test_content', '__test__', true ); |
|
102 | + add_post_meta($post_id, 'evans_test_content', '__test__', true); |
|
103 | 103 | |
104 | 104 | // Add thumbnail if supported |
105 | - if ( $supports['thumbnail'] === true || in_array( $slug, array( 'post', 'page' ) ) ){ |
|
106 | - update_post_meta( $post_id, '_thumbnail_id', TestContent::image( $post_id ) ); |
|
105 | + if ($supports['thumbnail'] === true || in_array($slug, array('post', 'page'))) { |
|
106 | + update_post_meta($post_id, '_thumbnail_id', TestContent::image($post_id)); |
|
107 | 107 | } |
108 | 108 | |
109 | - $taxonomies = get_object_taxonomies( $slug ); |
|
109 | + $taxonomies = get_object_taxonomies($slug); |
|
110 | 110 | |
111 | 111 | // Assign the post to terms |
112 | - if ( !empty( $taxonomies ) ){ |
|
113 | - $return .= $this->assign_terms( $post_id, $taxonomies ); |
|
112 | + if (!empty($taxonomies)) { |
|
113 | + $return .= $this->assign_terms($post_id, $taxonomies); |
|
114 | 114 | } |
115 | 115 | |
116 | 116 | // Spin up metaboxes |
117 | - if ( !empty( $metaboxes ) ){ |
|
118 | - foreach ( $metaboxes as $cmb ) : |
|
119 | - $return .= $this->random_metabox_content( $post_id, $cmb ); |
|
117 | + if (!empty($metaboxes)) { |
|
118 | + foreach ($metaboxes as $cmb) : |
|
119 | + $return .= $this->random_metabox_content($post_id, $cmb); |
|
120 | 120 | endforeach; |
121 | 121 | } |
122 | 122 | |
123 | 123 | // Check if we have errors and return them or created message |
124 | - if ( is_wp_error( $return ) ){ |
|
124 | + if (is_wp_error($return)) { |
|
125 | 125 | return $return; |
126 | 126 | } else { |
127 | 127 | return array( |
128 | 128 | 'type' => 'created', |
129 | 129 | 'object' => 'post', |
130 | 130 | 'pid' => $post_id, |
131 | - 'post_type' => get_post_type( $post_id ), |
|
132 | - 'link_edit' => admin_url( '/post.php?post='.$post_id.'&action=edit' ), |
|
133 | - 'link_view' => get_permalink( $post_id ), |
|
131 | + 'post_type' => get_post_type($post_id), |
|
132 | + 'link_edit' => admin_url('/post.php?post=' . $post_id . '&action=edit'), |
|
133 | + 'link_view' => get_permalink($post_id), |
|
134 | 134 | ); |
135 | 135 | } |
136 | 136 | |
@@ -147,12 +147,12 @@ discard block |
||
147 | 147 | * @param string $slug a custom post type ID. |
148 | 148 | * @return array Array of necessary supports booleans. |
149 | 149 | */ |
150 | - private function get_cpt_supports( $slug ){ |
|
150 | + private function get_cpt_supports($slug) { |
|
151 | 151 | |
152 | 152 | $supports = array( |
153 | - 'title' => post_type_supports( $slug, 'title' ), |
|
154 | - 'editor' => post_type_supports( $slug, 'editor' ), |
|
155 | - 'thumbnail' => post_type_supports( $slug, 'thumbnail' ) |
|
153 | + 'title' => post_type_supports($slug, 'title'), |
|
154 | + 'editor' => post_type_supports($slug, 'editor'), |
|
155 | + 'thumbnail' => post_type_supports($slug, 'thumbnail') |
|
156 | 156 | ); |
157 | 157 | |
158 | 158 | return $supports; |
@@ -173,26 +173,26 @@ discard block |
||
173 | 173 | * @param array $taxonomies taxonomies assigned to this cpt. |
174 | 174 | * @return object WP Error if there is one. |
175 | 175 | */ |
176 | - private function assign_terms( $post_id, $taxonomies ){ |
|
176 | + private function assign_terms($post_id, $taxonomies) { |
|
177 | 177 | |
178 | 178 | // Make sure it's an array & has items |
179 | - if ( empty( $taxonomies ) || !is_array( $taxonomies ) ){ |
|
179 | + if (empty($taxonomies) || !is_array($taxonomies)) { |
|
180 | 180 | return; |
181 | 181 | } |
182 | 182 | |
183 | - foreach ( $taxonomies as $tax ){ |
|
183 | + foreach ($taxonomies as $tax) { |
|
184 | 184 | |
185 | 185 | // Get the individual terms already existing |
186 | - $terms = get_terms( $tax, array( 'hide_empty' => false ) ); |
|
187 | - $count = count( $terms ) - 1; |
|
186 | + $terms = get_terms($tax, array('hide_empty' => false)); |
|
187 | + $count = count($terms) - 1; |
|
188 | 188 | |
189 | 189 | // If there are no terms, skip to the next taxonomy |
190 | - if ( empty( $terms ) ){ |
|
190 | + if (empty($terms)) { |
|
191 | 191 | continue; |
192 | 192 | } |
193 | 193 | |
194 | 194 | // Get a random index to use |
195 | - $index = rand( 0, $count ); |
|
195 | + $index = rand(0, $count); |
|
196 | 196 | |
197 | 197 | // Initialize our array |
198 | 198 | $post_data = array( |
@@ -200,13 +200,13 @@ discard block |
||
200 | 200 | ); |
201 | 201 | |
202 | 202 | // Set the term data to update |
203 | - $post_data['tax_input'][ $tax ] = array( $terms[$index]->term_id ); |
|
203 | + $post_data['tax_input'][$tax] = array($terms[$index]->term_id); |
|
204 | 204 | |
205 | 205 | // Update the post with the taxonomy info |
206 | - $return = wp_update_post( $post_data ); |
|
206 | + $return = wp_update_post($post_data); |
|
207 | 207 | |
208 | 208 | // Return the error if it exists |
209 | - if ( is_wp_error( $return ) ){ |
|
209 | + if (is_wp_error($return)) { |
|
210 | 210 | return $return->get_error_messages(); |
211 | 211 | } |
212 | 212 | |
@@ -229,18 +229,18 @@ discard block |
||
229 | 229 | * @param string $slug Post Type slug ID. |
230 | 230 | * @return array Fields to fill in for our post object. |
231 | 231 | */ |
232 | - private function get_metaboxes( $slug ){ |
|
232 | + private function get_metaboxes($slug) { |
|
233 | 233 | |
234 | 234 | $fields = array(); |
235 | 235 | |
236 | 236 | // CMB2 |
237 | - if ( class_exists( 'CMB2', false ) ) { |
|
238 | - $fields = $this->get_cmb2_metaboxes( $slug ); |
|
237 | + if (class_exists('CMB2', false)) { |
|
238 | + $fields = $this->get_cmb2_metaboxes($slug); |
|
239 | 239 | } |
240 | 240 | |
241 | 241 | // Custom Metaboxes and Fields (CMB1) |
242 | - if ( class_exists( 'cmb_Meta_Box', false ) ) { |
|
243 | - $fields = $this->get_cmb1_metaboxes( $slug ); |
|
242 | + if (class_exists('cmb_Meta_Box', false)) { |
|
243 | + $fields = $this->get_cmb1_metaboxes($slug); |
|
244 | 244 | } |
245 | 245 | |
246 | 246 | // Return our array |
@@ -263,52 +263,52 @@ discard block |
||
263 | 263 | * @param string $slug a custom post type ID. |
264 | 264 | * @return array Array of fields. |
265 | 265 | */ |
266 | - private function get_cmb2_metaboxes( $slug ){ |
|
266 | + private function get_cmb2_metaboxes($slug) { |
|
267 | 267 | |
268 | 268 | $fields = array(); |
269 | 269 | |
270 | 270 | // Get all metaboxes from CMB2 library |
271 | - $all_metaboxes = apply_filters( 'cmb2_meta_boxes', array() ); |
|
271 | + $all_metaboxes = apply_filters('cmb2_meta_boxes', array()); |
|
272 | 272 | |
273 | 273 | // Loop through all possible sets of metaboxes added the old way |
274 | - foreach ( $all_metaboxes as $metabox_array ){ |
|
274 | + foreach ($all_metaboxes as $metabox_array) { |
|
275 | 275 | |
276 | 276 | // If the custom post type ID matches this set of fields, set & stop |
277 | - if ( in_array( $slug, $metabox_array['object_types'] ) ) { |
|
277 | + if (in_array($slug, $metabox_array['object_types'])) { |
|
278 | 278 | |
279 | 279 | // If this is the first group of fields, simply set the value |
280 | 280 | // Else, merge this group with the previous one |
281 | - if ( empty( $fields ) ){ |
|
281 | + if (empty($fields)) { |
|
282 | 282 | $fields = $metabox_array['fields']; |
283 | 283 | } else { |
284 | - $fields = array_merge( $fields, $metabox_array['fields'] ); |
|
284 | + $fields = array_merge($fields, $metabox_array['fields']); |
|
285 | 285 | } |
286 | 286 | } |
287 | 287 | |
288 | 288 | } |
289 | 289 | |
290 | 290 | // Loop through all metaboxes added the new way |
291 | - foreach ( \CMB2_Boxes::get_all() as $cmb ) { |
|
291 | + foreach (\CMB2_Boxes::get_all() as $cmb) { |
|
292 | 292 | |
293 | 293 | // Establish correct cmb types |
294 | - if ( is_string( $cmb->meta_box['object_types'] ) ){ |
|
295 | - if ( $cmb->meta_box['object_types'] == $slug ){ |
|
294 | + if (is_string($cmb->meta_box['object_types'])) { |
|
295 | + if ($cmb->meta_box['object_types'] == $slug) { |
|
296 | 296 | $match = true; |
297 | 297 | } |
298 | 298 | } else { |
299 | - if ( in_array( $slug, $cmb->meta_box['object_types'] ) ){ |
|
299 | + if (in_array($slug, $cmb->meta_box['object_types'])) { |
|
300 | 300 | $match = true; |
301 | 301 | } |
302 | 302 | } |
303 | 303 | |
304 | - if ( $match !== true ){ |
|
304 | + if ($match !== true) { |
|
305 | 305 | continue; |
306 | 306 | } |
307 | 307 | |
308 | - if ( empty( $fields ) ){ |
|
308 | + if (empty($fields)) { |
|
309 | 309 | $fields = $cmb->meta_box['fields']; |
310 | 310 | } else { |
311 | - $fields = array_merge( $fields, $cmb->meta_box['fields'] ); |
|
311 | + $fields = array_merge($fields, $cmb->meta_box['fields']); |
|
312 | 312 | } |
313 | 313 | |
314 | 314 | } |
@@ -332,25 +332,25 @@ discard block |
||
332 | 332 | * @param string $slug a custom post type ID. |
333 | 333 | * @return array Array of fields. |
334 | 334 | */ |
335 | - private function get_cmb1_metaboxes( $slug ){ |
|
335 | + private function get_cmb1_metaboxes($slug) { |
|
336 | 336 | |
337 | 337 | $fields = array(); |
338 | 338 | |
339 | 339 | // Get all metaboxes from CMB2 library |
340 | - $all_metaboxes = apply_filters( 'cmb_meta_boxes', array() ); |
|
340 | + $all_metaboxes = apply_filters('cmb_meta_boxes', array()); |
|
341 | 341 | |
342 | 342 | // Loop through all possible sets of metaboxes |
343 | - foreach ( $all_metaboxes as $metabox_array ){ |
|
343 | + foreach ($all_metaboxes as $metabox_array) { |
|
344 | 344 | |
345 | 345 | // If the custom post type ID matches this set of fields, set & stop |
346 | - if ( in_array( $slug, $metabox_array['pages'] ) ) { |
|
346 | + if (in_array($slug, $metabox_array['pages'])) { |
|
347 | 347 | |
348 | 348 | // If this is the first group of fields, simply set the value |
349 | 349 | // Else, merge this group with the previous one |
350 | - if ( empty( $fields ) ){ |
|
350 | + if (empty($fields)) { |
|
351 | 351 | $fields = $metabox_array['fields']; |
352 | 352 | } else { |
353 | - $fields = array_merge( $fields, $metabox_array['fields'] ); |
|
353 | + $fields = array_merge($fields, $metabox_array['fields']); |
|
354 | 354 | } |
355 | 355 | } |
356 | 356 | |
@@ -375,36 +375,36 @@ discard block |
||
375 | 375 | * @param int $post_id Single post ID. |
376 | 376 | * @param array $cmb custom metabox array from CMB2. |
377 | 377 | */ |
378 | - private function random_metabox_content( $post_id, $cmb ){ |
|
378 | + private function random_metabox_content($post_id, $cmb) { |
|
379 | 379 | $value = ''; |
380 | 380 | |
381 | 381 | // First check that our post ID & cmb array aren't empty |
382 | - if ( empty( $cmb ) || empty( $post_id ) ){ |
|
382 | + if (empty($cmb) || empty($post_id)) { |
|
383 | 383 | return; |
384 | 384 | } |
385 | 385 | |
386 | 386 | // Fetch the appropriate type of data and return |
387 | - switch( $cmb['type'] ){ |
|
387 | + switch ($cmb['type']) { |
|
388 | 388 | |
389 | 389 | case 'text': |
390 | 390 | case 'text_small': |
391 | 391 | case 'text_medium': |
392 | 392 | |
393 | 393 | // If phone is in the id, fetch a phone # |
394 | - if ( stripos( $cmb['id'], 'phone' ) ){ |
|
394 | + if (stripos($cmb['id'], 'phone')) { |
|
395 | 395 | $value = TestContent::phone(); |
396 | 396 | |
397 | 397 | // If email is in the id, fetch an email address |
398 | - } elseif ( stripos( $cmb['id'], 'email' ) ){ |
|
398 | + } elseif (stripos($cmb['id'], 'email')) { |
|
399 | 399 | $value = TestContent::email(); |
400 | 400 | |
401 | 401 | // If time is in the id, fetch a time string |
402 | - } elseif ( stripos( $cmb['id'], 'time' ) ){ |
|
402 | + } elseif (stripos($cmb['id'], 'time')) { |
|
403 | 403 | $value = TestContent::time(); |
404 | 404 | |
405 | 405 | // Otherwise, just a random text string |
406 | 406 | } else { |
407 | - $value = TestContent::title( rand( 10, 50 ) ); |
|
407 | + $value = TestContent::title(rand(10, 50)); |
|
408 | 408 | } |
409 | 409 | |
410 | 410 | break; |
@@ -431,14 +431,14 @@ discard block |
||
431 | 431 | |
432 | 432 | case 'text_date': |
433 | 433 | |
434 | - $value = TestContent::date( 'm/d/Y' ); |
|
434 | + $value = TestContent::date('m/d/Y'); |
|
435 | 435 | |
436 | 436 | break; |
437 | 437 | |
438 | 438 | case 'text_date_timestamp': |
439 | 439 | case 'text_datetime_timestamp': |
440 | 440 | |
441 | - $value = TestContent::date( 'U' ); |
|
441 | + $value = TestContent::date('U'); |
|
442 | 442 | |
443 | 443 | break; |
444 | 444 | |
@@ -446,13 +446,13 @@ discard block |
||
446 | 446 | |
447 | 447 | case 'text_money': |
448 | 448 | |
449 | - $value = rand( 0, 100000 ); |
|
449 | + $value = rand(0, 100000); |
|
450 | 450 | |
451 | 451 | break; |
452 | 452 | |
453 | 453 | case 'test_colorpicker': |
454 | 454 | |
455 | - $value = '#' . str_pad( dechex( mt_rand( 0, 0xFFFFFF ) ), 6, '0', STR_PAD_LEFT ); |
|
455 | + $value = '#' . str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT); |
|
456 | 456 | |
457 | 457 | break; |
458 | 458 | |
@@ -469,8 +469,8 @@ discard block |
||
469 | 469 | case 'radio': |
470 | 470 | |
471 | 471 | // Grab a random item out of the array and return the key |
472 | - $new_val = array_slice( $cmb['options'], rand( 0, count( $cmb['options'] ) ), 1 ); |
|
473 | - $value = key( $new_val ); |
|
472 | + $new_val = array_slice($cmb['options'], rand(0, count($cmb['options'])), 1); |
|
473 | + $value = key($new_val); |
|
474 | 474 | |
475 | 475 | break; |
476 | 476 | |
@@ -481,7 +481,7 @@ discard block |
||
481 | 481 | case 'checkbox': |
482 | 482 | |
483 | 483 | // 50/50 odds of being turned on |
484 | - if ( rand( 0, 1 ) == 1 ){ |
|
484 | + if (rand(0, 1) == 1) { |
|
485 | 485 | $value = 'on'; |
486 | 486 | } |
487 | 487 | |
@@ -492,10 +492,10 @@ discard block |
||
492 | 492 | $new_option = array(); |
493 | 493 | |
494 | 494 | // Loop through each of our options |
495 | - foreach ( $cmb['options'] as $key => $value ){ |
|
495 | + foreach ($cmb['options'] as $key => $value) { |
|
496 | 496 | |
497 | 497 | // 50/50 chance of being included |
498 | - if ( rand( 0, 1 ) ){ |
|
498 | + if (rand(0, 1)) { |
|
499 | 499 | $new_option[] = $key; |
500 | 500 | } |
501 | 501 | |
@@ -513,7 +513,7 @@ discard block |
||
513 | 513 | |
514 | 514 | case 'file': |
515 | 515 | |
516 | - $value = TestContent::image( $post_id ); |
|
516 | + $value = TestContent::image($post_id); |
|
517 | 517 | |
518 | 518 | break; |
519 | 519 | |
@@ -528,19 +528,19 @@ discard block |
||
528 | 528 | } |
529 | 529 | |
530 | 530 | // Value must exist to attempt to insert |
531 | - if ( !empty( $value ) && !is_wp_error( $value ) ){ |
|
531 | + if (!empty($value) && !is_wp_error($value)) { |
|
532 | 532 | |
533 | 533 | // Files must be treated separately - they use the attachment ID |
534 | 534 | // & url of media for separate cmb values |
535 | - if ( $cmb['type'] != 'file' ){ |
|
536 | - add_post_meta( $post_id, $cmb['id'], $value, true ); |
|
535 | + if ($cmb['type'] != 'file') { |
|
536 | + add_post_meta($post_id, $cmb['id'], $value, true); |
|
537 | 537 | } else { |
538 | - add_post_meta( $post_id, $cmb['id'].'_id', $value, true ); |
|
539 | - add_post_meta( $post_id, $cmb['id'], wp_get_attachment_url( $value ), true ); |
|
538 | + add_post_meta($post_id, $cmb['id'] . '_id', $value, true); |
|
539 | + add_post_meta($post_id, $cmb['id'], wp_get_attachment_url($value), true); |
|
540 | 540 | } |
541 | 541 | |
542 | 542 | // If we're dealing with a WP Error object, just return the message for debugging |
543 | - } elseif ( is_wp_error( $value ) ){ |
|
543 | + } elseif (is_wp_error($value)) { |
|
544 | 544 | return $value->get_error_message(); |
545 | 545 | } |
546 | 546 |