@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * @subpackage Evans |
9 | 9 | * @author Old Town Media |
10 | 10 | */ |
11 | -class Metaboxes{ |
|
11 | +class Metaboxes { |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * Decide which cmb library to try and loop to get our metaboxes. |
@@ -22,31 +22,31 @@ discard block |
||
22 | 22 | * @param string $slug Post Type slug ID. |
23 | 23 | * @return array Fields to fill in for our post object. |
24 | 24 | */ |
25 | - public function get_metaboxes( $slug ){ |
|
25 | + public function get_metaboxes($slug) { |
|
26 | 26 | $cmb2_fields = $cmb_fields = $acf_fields = array(); |
27 | 27 | |
28 | 28 | // CMB2 |
29 | - if ( class_exists( 'CMB2', false ) ) { |
|
30 | - $cmb2_fields = $this->get_cmb2_metaboxes( $slug ); |
|
29 | + if (class_exists('CMB2', false)) { |
|
30 | + $cmb2_fields = $this->get_cmb2_metaboxes($slug); |
|
31 | 31 | } |
32 | 32 | |
33 | 33 | // Custom Metaboxes and Fields (CMB1) |
34 | - if ( class_exists( 'cmb_Meta_Box', false ) ) { |
|
35 | - $cmb_fields = $this->get_cmb1_metaboxes( $slug ); |
|
34 | + if (class_exists('cmb_Meta_Box', false)) { |
|
35 | + $cmb_fields = $this->get_cmb1_metaboxes($slug); |
|
36 | 36 | } |
37 | 37 | |
38 | 38 | // Advanced Custom Fields (ACF Free) |
39 | - if ( class_exists( 'acf', false ) ) { |
|
40 | - $acf_fields = $this->get_acf_free_metaboxes( $slug ); |
|
39 | + if (class_exists('acf', false)) { |
|
40 | + $acf_fields = $this->get_acf_free_metaboxes($slug); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | // Return our array |
44 | - return array_merge( $cmb2_fields, $cmb_fields, $acf_fields ); |
|
44 | + return array_merge($cmb2_fields, $cmb_fields, $acf_fields); |
|
45 | 45 | |
46 | 46 | } |
47 | 47 | |
48 | 48 | |
49 | - private function get_acf_free_metaboxes( $slug ){ |
|
49 | + private function get_acf_free_metaboxes($slug) { |
|
50 | 50 | |
51 | 51 | $fields = array(); |
52 | 52 | |
@@ -54,21 +54,21 @@ discard block |
||
54 | 54 | $fieldsets = $this->get_all_acf_field_groups(); |
55 | 55 | |
56 | 56 | // Return empty array if there are no fieldsets at all |
57 | - if ( empty( $fieldsets ) ){ |
|
57 | + if (empty($fieldsets)) { |
|
58 | 58 | return $fields; |
59 | 59 | } |
60 | 60 | |
61 | 61 | // Loop through each fieldset for possible matches |
62 | - foreach ( $fieldsets as $fieldset ){ |
|
62 | + foreach ($fieldsets as $fieldset) { |
|
63 | 63 | |
64 | - if ( $this->is_acf_field_in_post_type( $slug, $fieldset ) ){ |
|
64 | + if ($this->is_acf_field_in_post_type($slug, $fieldset)) { |
|
65 | 65 | |
66 | 66 | // If this is the first group of fields, simply set the value |
67 | 67 | // Else, merge this group with the previous one |
68 | - if ( empty( $fields ) ){ |
|
68 | + if (empty($fields)) { |
|
69 | 69 | $fields = $fieldset->fields; |
70 | 70 | } else { |
71 | - $fields = array_merge( $fields, $fieldset->fields ); |
|
71 | + $fields = array_merge($fields, $fieldset->fields); |
|
72 | 72 | } |
73 | 73 | |
74 | 74 | } |
@@ -80,16 +80,16 @@ discard block |
||
80 | 80 | } |
81 | 81 | |
82 | 82 | |
83 | - private function is_acf_field_in_post_type( $slug, $fieldset ){ |
|
83 | + private function is_acf_field_in_post_type($slug, $fieldset) { |
|
84 | 84 | |
85 | 85 | // Make sure we have something to parse |
86 | - if ( empty( $fieldset ) ){ |
|
86 | + if (empty($fieldset)) { |
|
87 | 87 | return false; |
88 | 88 | } |
89 | 89 | |
90 | 90 | // Loop through the rules to check for post type matches |
91 | - foreach ( $fieldset->rules as $rule ){ |
|
92 | - if ( $rule['param'] === 'post_type' && $rule['value'] === $slug ){ |
|
91 | + foreach ($fieldset->rules as $rule) { |
|
92 | + if ($rule['param'] === 'post_type' && $rule['value'] === $slug) { |
|
93 | 93 | return true; |
94 | 94 | } |
95 | 95 | } |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | } |
101 | 101 | |
102 | 102 | |
103 | - private function get_all_acf_field_groups(){ |
|
103 | + private function get_all_acf_field_groups() { |
|
104 | 104 | $info = $rules = $fields = array(); |
105 | 105 | |
106 | 106 | $args = array( |
@@ -108,20 +108,20 @@ discard block |
||
108 | 108 | 'posts_per_page'=> 500 |
109 | 109 | ); |
110 | 110 | |
111 | - $objects = new \WP_Query( $args ); |
|
111 | + $objects = new \WP_Query($args); |
|
112 | 112 | |
113 | - if ( $objects->have_posts() ) : |
|
114 | - while ( $objects->have_posts() ) : $objects->the_post(); |
|
113 | + if ($objects->have_posts()) : |
|
114 | + while ($objects->have_posts()) : $objects->the_post(); |
|
115 | 115 | |
116 | - $data = get_metadata( 'post', get_the_id() ); |
|
116 | + $data = get_metadata('post', get_the_id()); |
|
117 | 117 | |
118 | - foreach ( $data['rule'] as $rule ){ |
|
119 | - $rules[] = unserialize( $rule ); |
|
118 | + foreach ($data['rule'] as $rule) { |
|
119 | + $rules[] = unserialize($rule); |
|
120 | 120 | } |
121 | 121 | |
122 | - foreach ( $data as $key => $value ){ |
|
123 | - if ( substr( $key, 0, 6 ) == 'field_' ) : |
|
124 | - $field_detail = unserialize( $value[0] ); |
|
122 | + foreach ($data as $key => $value) { |
|
123 | + if (substr($key, 0, 6) == 'field_') : |
|
124 | + $field_detail = unserialize($value[0]); |
|
125 | 125 | $fields[] = array( |
126 | 126 | 'key' => $field_detail['key'], |
127 | 127 | 'type' => $field_detail['type'], |
@@ -159,55 +159,55 @@ discard block |
||
159 | 159 | * @param string $slug a custom post type ID. |
160 | 160 | * @return array Array of fields. |
161 | 161 | */ |
162 | - private function get_cmb2_metaboxes( $slug ){ |
|
162 | + private function get_cmb2_metaboxes($slug) { |
|
163 | 163 | |
164 | 164 | $fields = array(); |
165 | 165 | |
166 | 166 | // Get all metaboxes from CMB2 library |
167 | - $all_metaboxes = apply_filters( 'cmb2_meta_boxes', array() ); |
|
167 | + $all_metaboxes = apply_filters('cmb2_meta_boxes', array()); |
|
168 | 168 | |
169 | 169 | // Loop through all possible sets of metaboxes added the old way |
170 | - foreach ( $all_metaboxes as $metabox_array ){ |
|
170 | + foreach ($all_metaboxes as $metabox_array) { |
|
171 | 171 | |
172 | 172 | // If the custom post type ID matches this set of fields, set & stop |
173 | - if ( in_array( $slug, $metabox_array['object_types'] ) ) { |
|
173 | + if (in_array($slug, $metabox_array['object_types'])) { |
|
174 | 174 | |
175 | 175 | // If this is the first group of fields, simply set the value |
176 | 176 | // Else, merge this group with the previous one |
177 | - if ( empty( $fields ) ){ |
|
177 | + if (empty($fields)) { |
|
178 | 178 | $fields = $metabox_array['fields']; |
179 | 179 | } else { |
180 | - $fields = array_merge( $fields, $metabox_array['fields'] ); |
|
180 | + $fields = array_merge($fields, $metabox_array['fields']); |
|
181 | 181 | } |
182 | 182 | } |
183 | 183 | |
184 | 184 | } |
185 | 185 | |
186 | 186 | // Loop through all metaboxes added the new way |
187 | - foreach ( \CMB2_Boxes::get_all() as $cmb ) { |
|
187 | + foreach (\CMB2_Boxes::get_all() as $cmb) { |
|
188 | 188 | |
189 | 189 | // Create the default |
190 | 190 | $match = false; |
191 | 191 | |
192 | 192 | // Establish correct cmb types |
193 | - if ( is_string( $cmb->meta_box['object_types'] ) ){ |
|
194 | - if ( $cmb->meta_box['object_types'] == $slug ){ |
|
193 | + if (is_string($cmb->meta_box['object_types'])) { |
|
194 | + if ($cmb->meta_box['object_types'] == $slug) { |
|
195 | 195 | $match = true; |
196 | 196 | } |
197 | 197 | } else { |
198 | - if ( in_array( $slug, $cmb->meta_box['object_types'] ) ){ |
|
198 | + if (in_array($slug, $cmb->meta_box['object_types'])) { |
|
199 | 199 | $match = true; |
200 | 200 | } |
201 | 201 | } |
202 | 202 | |
203 | - if ( $match !== true ){ |
|
203 | + if ($match !== true) { |
|
204 | 204 | continue; |
205 | 205 | } |
206 | 206 | |
207 | - if ( empty( $fields ) ){ |
|
207 | + if (empty($fields)) { |
|
208 | 208 | $fields = $cmb->meta_box['fields']; |
209 | 209 | } else { |
210 | - $fields = array_merge( $fields, $cmb->meta_box['fields'] ); |
|
210 | + $fields = array_merge($fields, $cmb->meta_box['fields']); |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | } |
@@ -231,25 +231,25 @@ discard block |
||
231 | 231 | * @param string $slug a custom post type ID. |
232 | 232 | * @return array Array of fields. |
233 | 233 | */ |
234 | - private function get_cmb1_metaboxes( $slug ){ |
|
234 | + private function get_cmb1_metaboxes($slug) { |
|
235 | 235 | |
236 | 236 | $fields = array(); |
237 | 237 | |
238 | 238 | // Get all metaboxes from CMB2 library |
239 | - $all_metaboxes = apply_filters( 'cmb_meta_boxes', array() ); |
|
239 | + $all_metaboxes = apply_filters('cmb_meta_boxes', array()); |
|
240 | 240 | |
241 | 241 | // Loop through all possible sets of metaboxes |
242 | - foreach ( $all_metaboxes as $metabox_array ){ |
|
242 | + foreach ($all_metaboxes as $metabox_array) { |
|
243 | 243 | |
244 | 244 | // If the custom post type ID matches this set of fields, set & stop |
245 | - if ( in_array( $slug, $metabox_array['pages'] ) ) { |
|
245 | + if (in_array($slug, $metabox_array['pages'])) { |
|
246 | 246 | |
247 | 247 | // If this is the first group of fields, simply set the value |
248 | 248 | // Else, merge this group with the previous one |
249 | - if ( empty( $fields ) ){ |
|
249 | + if (empty($fields)) { |
|
250 | 250 | $fields = $metabox_array['fields']; |
251 | 251 | } else { |
252 | - $fields = array_merge( $fields, $metabox_array['fields'] ); |
|
252 | + $fields = array_merge($fields, $metabox_array['fields']); |
|
253 | 253 | } |
254 | 254 | } |
255 | 255 | |
@@ -272,36 +272,36 @@ discard block |
||
272 | 272 | * @param int $post_id Single post ID. |
273 | 273 | * @param array $cmb custom metabox array from CMB2. |
274 | 274 | */ |
275 | - public function random_metabox_content( $post_id, $cmb, $connected ){ |
|
275 | + public function random_metabox_content($post_id, $cmb, $connected) { |
|
276 | 276 | $value = ''; |
277 | 277 | |
278 | 278 | // First check that our post ID & cmb array aren't empty |
279 | - if ( empty( $cmb ) || empty( $post_id ) ){ |
|
279 | + if (empty($cmb) || empty($post_id)) { |
|
280 | 280 | return; |
281 | 281 | } |
282 | 282 | |
283 | 283 | // Fetch the appropriate type of data and return |
284 | - switch( $cmb['type'] ){ |
|
284 | + switch ($cmb['type']) { |
|
285 | 285 | |
286 | 286 | case 'text': |
287 | 287 | case 'text_small': |
288 | 288 | case 'text_medium': |
289 | 289 | |
290 | 290 | // If phone is in the id, fetch a phone # |
291 | - if ( stripos( $cmb['id'], 'phone' ) ){ |
|
291 | + if (stripos($cmb['id'], 'phone')) { |
|
292 | 292 | $value = TestContent::phone(); |
293 | 293 | |
294 | 294 | // If email is in the id, fetch an email address |
295 | - } elseif ( stripos( $cmb['id'], 'email' ) ){ |
|
295 | + } elseif (stripos($cmb['id'], 'email')) { |
|
296 | 296 | $value = TestContent::email(); |
297 | 297 | |
298 | 298 | // If time is in the id, fetch a time string |
299 | - } elseif ( stripos( $cmb['id'], 'time' ) ){ |
|
299 | + } elseif (stripos($cmb['id'], 'time')) { |
|
300 | 300 | $value = TestContent::time(); |
301 | 301 | |
302 | 302 | // Otherwise, just a random text string |
303 | 303 | } else { |
304 | - $value = TestContent::title( rand( 10, 50 ) ); |
|
304 | + $value = TestContent::title(rand(10, 50)); |
|
305 | 305 | } |
306 | 306 | |
307 | 307 | break; |
@@ -332,14 +332,14 @@ discard block |
||
332 | 332 | |
333 | 333 | case 'text_date': |
334 | 334 | |
335 | - $value = TestContent::date( 'm/d/Y' ); |
|
335 | + $value = TestContent::date('m/d/Y'); |
|
336 | 336 | |
337 | 337 | break; |
338 | 338 | |
339 | 339 | case 'text_date_timestamp': |
340 | 340 | case 'text_datetime_timestamp': |
341 | 341 | |
342 | - $value = TestContent::date( 'U' ); |
|
342 | + $value = TestContent::date('U'); |
|
343 | 343 | |
344 | 344 | break; |
345 | 345 | |
@@ -347,13 +347,13 @@ discard block |
||
347 | 347 | |
348 | 348 | case 'text_money': |
349 | 349 | |
350 | - $value = rand( 0, 100000 ); |
|
350 | + $value = rand(0, 100000); |
|
351 | 351 | |
352 | 352 | break; |
353 | 353 | |
354 | 354 | case 'test_colorpicker': |
355 | 355 | |
356 | - $value = '#' . str_pad( dechex( mt_rand( 0, 0xFFFFFF ) ), 6, '0', STR_PAD_LEFT ); |
|
356 | + $value = '#'.str_pad(dechex(mt_rand(0, 0xFFFFFF)), 6, '0', STR_PAD_LEFT); |
|
357 | 357 | |
358 | 358 | break; |
359 | 359 | |
@@ -370,8 +370,8 @@ discard block |
||
370 | 370 | case 'radio': |
371 | 371 | |
372 | 372 | // Grab a random item out of the array and return the key |
373 | - $new_val = array_slice( $cmb['options'], rand( 0, count( $cmb['options'] ) ), 1 ); |
|
374 | - $value = key( $new_val ); |
|
373 | + $new_val = array_slice($cmb['options'], rand(0, count($cmb['options'])), 1); |
|
374 | + $value = key($new_val); |
|
375 | 375 | |
376 | 376 | break; |
377 | 377 | |
@@ -382,7 +382,7 @@ discard block |
||
382 | 382 | case 'checkbox': |
383 | 383 | |
384 | 384 | // 50/50 odds of being turned on |
385 | - if ( rand( 0, 1 ) == 1 ){ |
|
385 | + if (rand(0, 1) == 1) { |
|
386 | 386 | $value = 'on'; |
387 | 387 | } |
388 | 388 | |
@@ -393,10 +393,10 @@ discard block |
||
393 | 393 | $new_option = array(); |
394 | 394 | |
395 | 395 | // Loop through each of our options |
396 | - foreach ( $cmb['options'] as $key => $value ){ |
|
396 | + foreach ($cmb['options'] as $key => $value) { |
|
397 | 397 | |
398 | 398 | // 50/50 chance of being included |
399 | - if ( rand( 0, 1 ) ){ |
|
399 | + if (rand(0, 1)) { |
|
400 | 400 | $new_option[] = $key; |
401 | 401 | } |
402 | 402 | |
@@ -414,8 +414,8 @@ discard block |
||
414 | 414 | |
415 | 415 | case 'file': |
416 | 416 | |
417 | - if ( true == $connected ){ |
|
418 | - $value = TestContent::image( $post_id ); |
|
417 | + if (true == $connected) { |
|
418 | + $value = TestContent::image($post_id); |
|
419 | 419 | } |
420 | 420 | |
421 | 421 | break; |
@@ -431,29 +431,29 @@ discard block |
||
431 | 431 | } |
432 | 432 | |
433 | 433 | // Value must exist to attempt to insert |
434 | - if ( !empty( $value ) && !is_wp_error( $value ) ){ |
|
434 | + if (!empty($value) && !is_wp_error($value)) { |
|
435 | 435 | |
436 | 436 | $type = $cmb['type']; |
437 | - $id = $cmb['id']; |
|
438 | - $value = apply_filters( "tc_{$type}_metabox", $value ); // Filter by metabox type |
|
439 | - $value = apply_filters( "tc_{$id}_metabox", $value ); // Filter by metabox ID |
|
437 | + $id = $cmb['id']; |
|
438 | + $value = apply_filters("tc_{$type}_metabox", $value); // Filter by metabox type |
|
439 | + $value = apply_filters("tc_{$id}_metabox", $value); // Filter by metabox ID |
|
440 | 440 | |
441 | 441 | // Files must be treated separately - they use the attachment ID |
442 | 442 | // & url of media for separate cmb values |
443 | - if ( $cmb['type'] != 'file' ){ |
|
444 | - add_post_meta( $post_id, $cmb['id'], $value, true ); |
|
443 | + if ($cmb['type'] != 'file') { |
|
444 | + add_post_meta($post_id, $cmb['id'], $value, true); |
|
445 | 445 | } else { |
446 | - add_post_meta( $post_id, $cmb['id'].'_id', $value, true ); |
|
447 | - add_post_meta( $post_id, $cmb['id'], wp_get_attachment_url( $value ), true ); |
|
446 | + add_post_meta($post_id, $cmb['id'].'_id', $value, true); |
|
447 | + add_post_meta($post_id, $cmb['id'], wp_get_attachment_url($value), true); |
|
448 | 448 | } |
449 | 449 | |
450 | - if ( $cmb['source'] === 'acf' ){ |
|
451 | - add_post_meta( $post_id, '_' . $cmb['id'], $cmb['key'], true ); |
|
450 | + if ($cmb['source'] === 'acf') { |
|
451 | + add_post_meta($post_id, '_'.$cmb['id'], $cmb['key'], true); |
|
452 | 452 | } |
453 | 453 | |
454 | 454 | // If we're dealing with a WP Error object, just return the message for debugging |
455 | - } elseif ( is_wp_error( $value ) ){ |
|
456 | - error_log( $value->get_error_message() ); |
|
455 | + } elseif (is_wp_error($value)) { |
|
456 | + error_log($value->get_error_message()); |
|
457 | 457 | return $value->get_error_message(); |
458 | 458 | } |
459 | 459 |