1
|
|
|
<?php |
2
|
|
|
namespace testContent; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class for handling CMB data |
6
|
|
|
* |
7
|
|
|
* @package WordPress |
8
|
|
|
* @subpackage Evans |
9
|
|
|
* @author Old Town Media |
10
|
|
|
*/ |
11
|
|
|
class Metaboxes{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Decide which cmb library to try and loop to get our metaboxes. |
15
|
|
|
* |
16
|
|
|
* Due to supporting multiple CMB libraries, we need to check which library |
17
|
|
|
* is used on our site and then run the appropriate function. Currently |
18
|
|
|
* supported libraries are CMB2 & Custom Metaboxes and Fields. |
19
|
|
|
* |
20
|
|
|
* @see get_cmb2_metaboxes, get_cmb1_metaboxes |
21
|
|
|
* |
22
|
|
|
* @param string $slug Post Type slug ID. |
23
|
|
|
* @return array Fields to fill in for our post object. |
24
|
|
|
*/ |
25
|
|
|
public function get_metaboxes( $slug ){ |
26
|
|
|
|
27
|
|
|
$fields = array(); |
28
|
|
|
|
29
|
|
|
// CMB2 |
30
|
|
|
if ( class_exists( 'CMB2', false ) ) { |
31
|
|
|
$fields = $this->get_cmb2_metaboxes( $slug ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
// Custom Metaboxes and Fields (CMB1) |
35
|
|
|
if ( class_exists( 'cmb_Meta_Box', false ) ) { |
36
|
|
|
$fields = $this->get_cmb1_metaboxes( $slug ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Return our array |
40
|
|
|
return $fields; |
41
|
|
|
|
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Gets all CMB2 custom metaboxes associated with a post type. |
47
|
|
|
* |
48
|
|
|
* Loops through all custom metabox fields registered with CMB2 and |
49
|
|
|
* looks through them for matches on the given post type ID. Returns a single |
50
|
|
|
* array of all boxes associated with the post type. |
51
|
|
|
* |
52
|
|
|
* @access private |
53
|
|
|
* |
54
|
|
|
* @see cmb2_meta_boxes |
55
|
|
|
* |
56
|
|
|
* @param string $slug a custom post type ID. |
57
|
|
|
* @return array Array of fields. |
58
|
|
|
*/ |
59
|
|
|
private function get_cmb2_metaboxes( $slug ){ |
60
|
|
|
|
61
|
|
|
$fields = array(); |
62
|
|
|
|
63
|
|
|
// Get all metaboxes from CMB2 library |
64
|
|
|
$all_metaboxes = apply_filters( 'cmb2_meta_boxes', array() ); |
65
|
|
|
|
66
|
|
|
// Loop through all possible sets of metaboxes added the old way |
67
|
|
|
foreach ( $all_metaboxes as $metabox_array ){ |
68
|
|
|
|
69
|
|
|
// If the custom post type ID matches this set of fields, set & stop |
70
|
|
|
if ( in_array( $slug, $metabox_array['object_types'] ) ) { |
71
|
|
|
|
72
|
|
|
// If this is the first group of fields, simply set the value |
73
|
|
|
// Else, merge this group with the previous one |
74
|
|
|
if ( empty( $fields ) ){ |
75
|
|
|
$fields = $metabox_array['fields']; |
76
|
|
|
} else { |
77
|
|
|
$fields = array_merge( $fields, $metabox_array['fields'] ); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Loop through all metaboxes added the new way |
84
|
|
|
foreach ( \CMB2_Boxes::get_all() as $cmb ) { |
85
|
|
|
|
86
|
|
|
// Create the default |
87
|
|
|
$match = false; |
88
|
|
|
|
89
|
|
|
// Establish correct cmb types |
90
|
|
|
if ( is_string( $cmb->meta_box['object_types'] ) ){ |
91
|
|
|
if ( $cmb->meta_box['object_types'] == $slug ){ |
92
|
|
|
$match = true; |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
if ( in_array( $slug, $cmb->meta_box['object_types'] ) ){ |
96
|
|
|
$match = true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ( $match !== true ){ |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ( empty( $fields ) ){ |
105
|
|
|
$fields = $cmb->meta_box['fields']; |
106
|
|
|
} else { |
107
|
|
|
$fields = array_merge( $fields, $cmb->meta_box['fields'] ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $fields; |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Gets all CMB1 custom metaboxes associated with a post type. |
119
|
|
|
* |
120
|
|
|
* Loops through all custom metabox fields registered with CMB2 and |
121
|
|
|
* looks through them for matches on the given post type ID. Returns a single |
122
|
|
|
* array of all boxes associated with the post type. |
123
|
|
|
* |
124
|
|
|
* @access private |
125
|
|
|
* |
126
|
|
|
* @see cmb_meta_boxes |
127
|
|
|
* |
128
|
|
|
* @param string $slug a custom post type ID. |
129
|
|
|
* @return array Array of fields. |
130
|
|
|
*/ |
131
|
|
|
private function get_cmb1_metaboxes( $slug ){ |
132
|
|
|
|
133
|
|
|
$fields = array(); |
134
|
|
|
|
135
|
|
|
// Get all metaboxes from CMB2 library |
136
|
|
|
$all_metaboxes = apply_filters( 'cmb_meta_boxes', array() ); |
137
|
|
|
|
138
|
|
|
// Loop through all possible sets of metaboxes |
139
|
|
|
foreach ( $all_metaboxes as $metabox_array ){ |
140
|
|
|
|
141
|
|
|
// If the custom post type ID matches this set of fields, set & stop |
142
|
|
|
if ( in_array( $slug, $metabox_array['pages'] ) ) { |
143
|
|
|
|
144
|
|
|
// If this is the first group of fields, simply set the value |
145
|
|
|
// Else, merge this group with the previous one |
146
|
|
|
if ( empty( $fields ) ){ |
147
|
|
|
$fields = $metabox_array['fields']; |
148
|
|
|
} else { |
149
|
|
|
$fields = array_merge( $fields, $metabox_array['fields'] ); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $fields; |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Assigns the proper testing data to a custom metabox. |
162
|
|
|
* |
163
|
|
|
* Swaps through the possible types of CMB2 supported fields and |
164
|
|
|
* insert the appropriate data based on type & id. |
165
|
|
|
* Some types are not yet supported due to low frequency of use. |
166
|
|
|
* |
167
|
|
|
* @see TestContent, add_post_meta |
168
|
|
|
* |
169
|
|
|
* @param int $post_id Single post ID. |
170
|
|
|
* @param array $cmb custom metabox array from CMB2. |
171
|
|
|
*/ |
172
|
|
|
public function random_metabox_content( $post_id, $cmb ){ |
173
|
|
|
$value = ''; |
174
|
|
|
|
175
|
|
|
// First check that our post ID & cmb array aren't empty |
176
|
|
|
if ( empty( $cmb ) || empty( $post_id ) ){ |
177
|
|
|
return; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
// Fetch the appropriate type of data and return |
181
|
|
|
switch( $cmb['type'] ){ |
182
|
|
|
|
183
|
|
|
case 'text': |
184
|
|
|
case 'text_small': |
185
|
|
|
case 'text_medium': |
186
|
|
|
|
187
|
|
|
// If phone is in the id, fetch a phone # |
188
|
|
|
if ( stripos( $cmb['id'], 'phone' ) ){ |
189
|
|
|
$value = TestContent::phone(); |
190
|
|
|
|
191
|
|
|
// If email is in the id, fetch an email address |
192
|
|
|
} elseif ( stripos( $cmb['id'], 'email' ) ){ |
193
|
|
|
$value = TestContent::email(); |
194
|
|
|
|
195
|
|
|
// If time is in the id, fetch a time string |
196
|
|
|
} elseif ( stripos( $cmb['id'], 'time' ) ){ |
197
|
|
|
$value = TestContent::time(); |
198
|
|
|
|
199
|
|
|
// Otherwise, just a random text string |
200
|
|
|
} else { |
201
|
|
|
$value = TestContent::title( rand( 10, 50 ) ); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
break; |
205
|
|
|
|
206
|
|
|
case 'text_url': |
207
|
|
|
|
208
|
|
|
$value = TestContent::link(); |
209
|
|
|
|
210
|
|
|
break; |
211
|
|
|
|
212
|
|
|
case 'text_email': |
213
|
|
|
|
214
|
|
|
$value = TestContent::email(); |
215
|
|
|
|
216
|
|
|
break; |
217
|
|
|
|
218
|
|
|
case 'text_time': |
219
|
|
|
|
220
|
|
|
$value = TestContent::time(); |
221
|
|
|
|
222
|
|
|
break; |
223
|
|
|
|
224
|
|
|
case 'select_timezone': |
225
|
|
|
|
226
|
|
|
$value = TestContent::timezone(); |
227
|
|
|
|
228
|
|
|
break; |
229
|
|
|
|
230
|
|
|
case 'text_date': |
231
|
|
|
|
232
|
|
|
$value = TestContent::date( 'm/d/Y' ); |
233
|
|
|
|
234
|
|
|
break; |
235
|
|
|
|
236
|
|
|
case 'text_date_timestamp': |
237
|
|
|
case 'text_datetime_timestamp': |
238
|
|
|
|
239
|
|
|
$value = TestContent::date( 'U' ); |
240
|
|
|
|
241
|
|
|
break; |
242
|
|
|
|
243
|
|
|
// case 'text_datetime_timestamp_timezone': break; |
|
|
|
|
244
|
|
|
|
245
|
|
|
case 'text_money': |
246
|
|
|
|
247
|
|
|
$value = rand( 0, 100000 ); |
248
|
|
|
|
249
|
|
|
break; |
250
|
|
|
|
251
|
|
|
case 'test_colorpicker': |
252
|
|
|
|
253
|
|
|
$value = '#' . str_pad( dechex( mt_rand( 0, 0xFFFFFF ) ), 6, '0', STR_PAD_LEFT ); |
254
|
|
|
|
255
|
|
|
break; |
256
|
|
|
|
257
|
|
|
case 'textarea': |
258
|
|
|
case 'textarea_small': |
259
|
|
|
case 'textarea_code': |
260
|
|
|
|
261
|
|
|
$value = TestContent::plain_text(); |
262
|
|
|
|
263
|
|
|
break; |
264
|
|
|
|
265
|
|
|
case 'select': |
266
|
|
|
case 'radio_inline': |
267
|
|
|
case 'radio': |
268
|
|
|
|
269
|
|
|
// Grab a random item out of the array and return the key |
270
|
|
|
$new_val = array_slice( $cmb['options'], rand( 0, count( $cmb['options'] ) ), 1 ); |
271
|
|
|
$value = key( $new_val ); |
272
|
|
|
|
273
|
|
|
break; |
274
|
|
|
|
275
|
|
|
// case 'taxonomy_radio': break; |
|
|
|
|
276
|
|
|
// case 'taxonomy_select': break; |
|
|
|
|
277
|
|
|
// case 'taxonomy_multicheck': break; |
|
|
|
|
278
|
|
|
|
279
|
|
|
case 'checkbox': |
280
|
|
|
|
281
|
|
|
// 50/50 odds of being turned on |
282
|
|
|
if ( rand( 0, 1 ) == 1 ){ |
283
|
|
|
$value = 'on'; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
break; |
287
|
|
|
|
288
|
|
|
case 'multicheck': |
289
|
|
|
|
290
|
|
|
$new_option = array(); |
291
|
|
|
|
292
|
|
|
// Loop through each of our options |
293
|
|
|
foreach ( $cmb['options'] as $key => $value ){ |
294
|
|
|
|
295
|
|
|
// 50/50 chance of being included |
296
|
|
|
if ( rand( 0, 1 ) ){ |
297
|
|
|
$new_option[] = $key; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
$value = $new_option; |
303
|
|
|
|
304
|
|
|
break; |
305
|
|
|
|
306
|
|
|
case 'wysiwyg': |
307
|
|
|
|
308
|
|
|
$value = TestContent::paragraphs(); |
309
|
|
|
|
310
|
|
|
break; |
311
|
|
|
|
312
|
|
|
case 'file': |
313
|
|
|
|
314
|
|
|
$value = TestContent::image( $post_id ); |
315
|
|
|
|
316
|
|
|
break; |
317
|
|
|
|
318
|
|
|
// case 'file_list': break; |
|
|
|
|
319
|
|
|
|
320
|
|
|
case 'oembed': |
321
|
|
|
|
322
|
|
|
$value = TestContent::oembed(); |
323
|
|
|
|
324
|
|
|
break; |
325
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
// Value must exist to attempt to insert |
329
|
|
|
if ( !empty( $value ) && !is_wp_error( $value ) ){ |
330
|
|
|
|
331
|
|
|
// Files must be treated separately - they use the attachment ID |
332
|
|
|
// & url of media for separate cmb values |
333
|
|
|
if ( $cmb['type'] != 'file' ){ |
334
|
|
|
add_post_meta( $post_id, $cmb['id'], $value, true ); |
335
|
|
|
} else { |
336
|
|
|
add_post_meta( $post_id, $cmb['id'].'_id', $value, true ); |
337
|
|
|
add_post_meta( $post_id, $cmb['id'], wp_get_attachment_url( $value ), true ); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
// If we're dealing with a WP Error object, just return the message for debugging |
341
|
|
|
} elseif ( is_wp_error( $value ) ){ |
342
|
|
|
error_log( $value->get_error_message() ); |
343
|
|
|
return $value->get_error_message(); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
} // end random_metabox_content |
347
|
|
|
|
348
|
|
|
} |
349
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.