1
|
|
|
<?php |
2
|
|
|
namespace DummyPress; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class for handling CMB data |
6
|
|
|
* |
7
|
|
|
* @package WordPress |
8
|
|
|
* @subpackage Evans |
9
|
|
|
* @author Mike Selander |
10
|
|
|
*/ |
11
|
|
|
class MetaboxValues { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Assigns the proper testing data to a custom metabox. |
15
|
|
|
* |
16
|
|
|
* Swaps through the possible types of CMB2 supported fields and |
17
|
|
|
* insert the appropriate data based on type & id. |
18
|
|
|
* Some types are not yet supported due to low frequency of use. |
19
|
|
|
* |
20
|
|
|
* @see TestContent, add_post_meta |
21
|
|
|
* |
22
|
|
|
* @param int $post_id Single post ID. |
23
|
|
|
* @param array $cmb custom metabox array from CMB2. |
24
|
|
|
*/ |
25
|
|
|
public function get_values( $post_id, $cmb, $connected ) { |
|
|
|
|
26
|
|
|
$value = ''; |
27
|
|
|
|
28
|
|
|
// First check that our post ID & cmb array aren't empty |
29
|
|
|
if ( empty( $cmb ) || empty( $post_id ) || ! is_user_logged_in() ) { |
30
|
|
|
return; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
// Fetch the appropriate type of data and return |
34
|
|
|
switch( $cmb['type'] ) { |
35
|
|
|
|
36
|
|
|
case 'text': |
37
|
|
|
case 'text_small': |
38
|
|
|
case 'text_medium': |
|
|
|
|
39
|
|
|
|
40
|
|
|
$value = $this->text( $cmb ); |
41
|
|
|
|
42
|
|
|
break; |
43
|
|
|
|
44
|
|
|
case 'text_url': |
|
|
|
|
45
|
|
|
|
46
|
|
|
$value = $this->url( $cmb ); |
47
|
|
|
|
48
|
|
|
break; |
49
|
|
|
|
50
|
|
|
case 'text_email' : |
|
|
|
|
51
|
|
|
case 'email': |
|
|
|
|
52
|
|
|
|
53
|
|
|
$value = $this->email( $cmb ); |
54
|
|
|
|
55
|
|
|
break; |
56
|
|
|
|
57
|
|
|
case 'number' : |
|
|
|
|
58
|
|
|
case 'text_money': |
|
|
|
|
59
|
|
|
|
60
|
|
|
$value = $this->number( $cmb ); |
61
|
|
|
|
62
|
|
|
break; |
63
|
|
|
|
64
|
|
|
case 'text_time': |
65
|
|
|
case 'time': |
|
|
|
|
66
|
|
|
|
67
|
|
|
$value = $this->time( $cmb ); |
68
|
|
|
|
69
|
|
|
break; |
70
|
|
|
|
71
|
|
|
case 'select_timezone': |
|
|
|
|
72
|
|
|
|
73
|
|
|
$value = $this->timezone( $cmb ); |
74
|
|
|
|
75
|
|
|
break; |
76
|
|
|
|
77
|
|
|
case 'text_date': |
78
|
|
|
case 'date': |
|
|
|
|
79
|
|
|
|
80
|
|
|
$value = $this->date( $cmb ); |
81
|
|
|
|
82
|
|
|
break; |
83
|
|
|
|
84
|
|
|
case 'text_date_timestamp': |
85
|
|
|
case 'text_datetime_timestamp': |
86
|
|
|
case 'date_unix': |
87
|
|
|
case 'datetime_unix': |
|
|
|
|
88
|
|
|
|
89
|
|
|
$value = $this->timestamp( $cmb ); |
90
|
|
|
|
91
|
|
|
break; |
92
|
|
|
|
93
|
|
|
// case 'text_datetime_timestamp_timezone': break; |
|
|
|
|
94
|
|
|
|
95
|
|
|
case 'test_colorpicker': |
|
|
|
|
96
|
|
|
|
97
|
|
|
$value = $this->color( $cmb ); |
98
|
|
|
|
99
|
|
|
break; |
100
|
|
|
|
101
|
|
|
case 'textarea': |
102
|
|
|
case 'textarea_small': |
103
|
|
|
case 'textarea_code': |
|
|
|
|
104
|
|
|
|
105
|
|
|
$value = $this->textarea( $cmb ); |
106
|
|
|
|
107
|
|
|
break; |
108
|
|
|
|
109
|
|
|
case 'select': |
110
|
|
|
case 'radio_inline': |
111
|
|
|
case 'radio': |
|
|
|
|
112
|
|
|
|
113
|
|
|
$value = $this->radio( $cmb ); |
114
|
|
|
|
115
|
|
|
break; |
116
|
|
|
|
117
|
|
|
// case 'taxonomy_radio': break; |
|
|
|
|
118
|
|
|
// case 'taxonomy_select': break; |
|
|
|
|
119
|
|
|
// case 'taxonomy_multicheck': break; |
|
|
|
|
120
|
|
|
|
121
|
|
|
case 'checkbox': |
|
|
|
|
122
|
|
|
|
123
|
|
|
if ( isset( $cmb['source'] ) && 'acf' === $cmb['source'] ) { |
124
|
|
|
$value = $this->multicheck( $cmb ); |
125
|
|
|
} else { |
126
|
|
|
$value = $this->checkbox( $cmb ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
break; |
130
|
|
|
|
131
|
|
|
case 'multicheck': |
|
|
|
|
132
|
|
|
|
133
|
|
|
$value = $this->multicheck( $cmb ); |
134
|
|
|
|
135
|
|
|
break; |
136
|
|
|
|
137
|
|
|
case 'wysiwyg': |
|
|
|
|
138
|
|
|
|
139
|
|
|
$value = $this->wysiwyg( $cmb ); |
140
|
|
|
|
141
|
|
|
break; |
142
|
|
|
|
143
|
|
|
case 'file': |
144
|
|
|
case 'image': |
|
|
|
|
145
|
|
|
|
146
|
|
|
$value = $this->file( $cmb, $post_id, $connected ); |
147
|
|
|
|
148
|
|
|
break; |
149
|
|
|
|
150
|
|
|
// case 'file_list': break; |
|
|
|
|
151
|
|
|
|
152
|
|
|
case 'oembed': |
|
|
|
|
153
|
|
|
|
154
|
|
|
$value = $this->oembed( $cmb ); |
155
|
|
|
|
156
|
|
|
break; |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// Value must exist to attempt to insert |
161
|
|
|
if ( ! empty( $value ) && ! is_wp_error( $value ) ) { |
162
|
|
|
|
163
|
|
|
$this->update_meta( $post_id, $value, $cmb ); |
164
|
|
|
|
165
|
|
|
// If we're dealing with a WP Error object, just return the message for debugging |
166
|
|
|
} elseif ( is_wp_error( $value ) ) { |
167
|
|
|
|
168
|
|
|
return $value->get_error_message(); |
169
|
|
|
|
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
} // end get_values |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Pulls a text string for CMB field. |
177
|
|
|
* |
178
|
|
|
* @see TestContent |
179
|
|
|
* |
180
|
|
|
* @param array $cmb Metabox data |
181
|
|
|
* @return string cmb value |
182
|
|
|
*/ |
183
|
|
|
private function text( $cmb ) { |
184
|
|
|
|
185
|
|
|
// If phone is in the id, fetch a phone # |
186
|
|
|
if ( stripos( $cmb['id'], 'phone' ) ) { |
187
|
|
|
$value = TestContent::phone(); |
188
|
|
|
|
189
|
|
|
// If email is in the id, fetch an email address |
190
|
|
|
} elseif ( stripos( $cmb['id'], 'email' ) ) { |
191
|
|
|
$value = TestContent::email(); |
192
|
|
|
|
193
|
|
|
// If time is in the id, fetch a time string |
194
|
|
|
} elseif ( stripos( $cmb['id'], 'time' ) ) { |
195
|
|
|
$value = TestContent::time(); |
196
|
|
|
|
197
|
|
|
// If company|organization is in the ID, fetch a company name |
198
|
|
|
} elseif ( stripos( $cmb['id'], 'company' ) || stripos( $cmb['id'], 'organization' ) ) { |
199
|
|
|
$value = TestContent::organization(); |
200
|
|
|
|
201
|
|
|
// Otherwise, just a random text string |
202
|
|
|
} else { |
203
|
|
|
$value = TestContent::title( rand( 10, 50 ) ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
View Code Duplication |
if ( 'acf' === $cmb['source'] && ! empty( $cmb['extras']->chars ) ) { |
|
|
|
|
207
|
|
|
$value = substr( $value, 0, $cmb['extras']->chars ); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
return $value; |
211
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Pulls a URL value CMB field. |
217
|
|
|
* |
218
|
|
|
* @see TestContent |
219
|
|
|
* |
220
|
|
|
* @param array $cmb Metabox data |
221
|
|
|
* @return string cmb value |
222
|
|
|
*/ |
223
|
|
|
private function url( $cmb ) { |
|
|
|
|
224
|
|
|
|
225
|
|
|
return TestContent::link(); |
226
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Pulls an email address for CMB field. |
232
|
|
|
* |
233
|
|
|
* @see TestContent |
234
|
|
|
* |
235
|
|
|
* @param array $cmb Metabox data |
236
|
|
|
* @return string cmb value |
237
|
|
|
*/ |
238
|
|
|
private function email( $cmb ) { |
|
|
|
|
239
|
|
|
|
240
|
|
|
return TestContent::email(); |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Pulls a random valnumberue for CMB field. |
247
|
|
|
* |
248
|
|
|
* @param array $cmb Metabox data |
249
|
|
|
* @return int cmb value |
250
|
|
|
*/ |
251
|
|
|
private function number( $cmb ) { |
252
|
|
|
|
253
|
|
|
$min = 1; |
254
|
|
|
$max = 10000000; |
255
|
|
|
|
256
|
|
View Code Duplication |
if ( 'acf' == $cmb['source'] && ! empty( $cmb['extras']->min ) ) { |
|
|
|
|
257
|
|
|
$min = $cmb['extras']->min; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
View Code Duplication |
if ( 'acf' == $cmb['source'] && ! empty( $cmb['extras']->max ) ) { |
|
|
|
|
261
|
|
|
$max = $cmb['extras']->max; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return rand( $min, $max ); |
265
|
|
|
|
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Pulls a time of day for CMB field. |
271
|
|
|
* |
272
|
|
|
* @see TestContent |
273
|
|
|
* |
274
|
|
|
* @param array $cmb Metabox data |
275
|
|
|
* @return string cmb value |
276
|
|
|
*/ |
277
|
|
|
private function time( $cmb ) { |
|
|
|
|
278
|
|
|
|
279
|
|
|
return TestContent::time(); |
280
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Pulls a timezone for CMB field. |
286
|
|
|
* |
287
|
|
|
* @see TestContent |
288
|
|
|
* |
289
|
|
|
* @param array $cmb Metabox data |
290
|
|
|
* @return string cmb value |
291
|
|
|
*/ |
292
|
|
|
private function timezone( $cmb ) { |
|
|
|
|
293
|
|
|
|
294
|
|
|
return TestContent::timezone(); |
295
|
|
|
|
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* Pulls a date for CMB field. |
301
|
|
|
* |
302
|
|
|
* @see TestContent |
303
|
|
|
* |
304
|
|
|
* @param array $cmb Metabox data |
305
|
|
|
* @return string cmb value |
306
|
|
|
*/ |
307
|
|
|
private function date( $cmb ) { |
|
|
|
|
308
|
|
|
|
309
|
|
|
return TestContent::date( 'm/d/Y' ); |
310
|
|
|
|
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Pulls a timestamp for CMB field. |
316
|
|
|
* |
317
|
|
|
* @see TestContent |
318
|
|
|
* |
319
|
|
|
* @param array $cmb Metabox data |
320
|
|
|
* @return string cmb value |
321
|
|
|
*/ |
322
|
|
|
private function timestamp( $cmb ) { |
|
|
|
|
323
|
|
|
|
324
|
|
|
return TestContent::date( 'U' ); |
325
|
|
|
|
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Pulls a random hexadecimal color code for CMB field. |
331
|
|
|
* |
332
|
|
|
* @param array $cmb Metabox data |
333
|
|
|
* @return string cmb value |
334
|
|
|
*/ |
335
|
|
|
private function color( $cmb ) { |
|
|
|
|
336
|
|
|
|
337
|
|
|
return '#' . str_pad( dechex( mt_rand( 0, 0xFFFFFF ) ), 6, '0', STR_PAD_LEFT ); |
338
|
|
|
|
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* Pulls a long text string for CMB field. |
344
|
|
|
* |
345
|
|
|
* @see TestContent |
346
|
|
|
* |
347
|
|
|
* @param array $cmb Metabox data |
348
|
|
|
* @return string cmb value |
349
|
|
|
*/ |
350
|
|
|
private function textarea( $cmb ) { |
351
|
|
|
|
352
|
|
|
$value = TestContent::plain_text(); |
353
|
|
|
|
354
|
|
View Code Duplication |
if ( 'acf' == $cmb['source'] && ! empty( $cmb['extras']->chars ) ) { |
|
|
|
|
355
|
|
|
$value = substr( $value, 0, $cmb['extras']->chars ); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
return $value; |
359
|
|
|
|
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Pulls a random radio field value for CMB field. |
365
|
|
|
* |
366
|
|
|
* @see TestContent |
367
|
|
|
* |
368
|
|
|
* @param array $cmb Metabox data |
369
|
|
|
* @return string cmb value |
370
|
|
|
*/ |
371
|
|
|
private function radio( $cmb ) { |
372
|
|
|
|
373
|
|
|
// Grab a random item out of the array and return the key |
374
|
|
|
$new_val = array_slice( $cmb['options'], rand( 0, count( $cmb['options'] ) ), 1 ); |
375
|
|
|
$value = key( $new_val ); |
376
|
|
|
|
377
|
|
|
return $value; |
378
|
|
|
|
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Pulls a random checkbox field value for CMB field. |
384
|
|
|
* |
385
|
|
|
* @see TestContent |
386
|
|
|
* |
387
|
|
|
* @param array $cmb Metabox data |
388
|
|
|
* @return string cmb value |
389
|
|
|
*/ |
390
|
|
|
private function checkbox( $cmb ) { |
|
|
|
|
391
|
|
|
$value = ''; |
392
|
|
|
|
393
|
|
|
// 50/50 odds of being turned on |
394
|
|
|
if ( rand( 0, 1 ) == 1 ) { |
395
|
|
|
$value = 'on'; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return $value; |
399
|
|
|
|
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Pulls a random multicheck field value for CMB field. |
405
|
|
|
* |
406
|
|
|
* @see TestContent |
407
|
|
|
* |
408
|
|
|
* @param array $cmb Metabox data |
409
|
|
|
* @return array cmb value |
410
|
|
|
*/ |
411
|
|
|
private function multicheck( $cmb ) { |
412
|
|
|
|
413
|
|
|
$new_option = array(); |
414
|
|
|
|
415
|
|
|
// Loop through each of our options |
416
|
|
|
foreach ( $cmb['options'] as $key => $value ) { |
417
|
|
|
|
418
|
|
|
// 50/50 chance of being included |
419
|
|
|
if ( rand( 0, 1 ) ) { |
420
|
|
|
$new_option[] = $key; |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
return $new_option; |
426
|
|
|
|
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Pulls an HTML paragraph string for CMB field. |
432
|
|
|
* |
433
|
|
|
* @see TestContent |
434
|
|
|
* |
435
|
|
|
* @param array $cmb Metabox data |
436
|
|
|
* @return string cmb value |
437
|
|
|
*/ |
438
|
|
|
private function wysiwyg( $cmb ) { |
|
|
|
|
439
|
|
|
|
440
|
|
|
return TestContent::paragraphs(); |
441
|
|
|
|
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Pulls an image URL for CMB field. |
447
|
|
|
* |
448
|
|
|
* @see TestContent |
449
|
|
|
* |
450
|
|
|
* @param array $cmb Metabox data |
451
|
|
|
* @param int $post_id Post ID |
452
|
|
|
* @param bool $connected Whether we're connected to the Internets or not |
453
|
|
|
* @return mixed string|object cmb value or WP_Error object |
454
|
|
|
*/ |
455
|
|
|
private function file( $cmb, $post_id, $connected ) { |
|
|
|
|
456
|
|
|
$value = ''; |
457
|
|
|
|
458
|
|
|
if ( true === $connected ) { |
459
|
|
|
$value = TestContent::image( $post_id ); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
return $value; |
463
|
|
|
|
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Pulls an Oembed URL for CMB field. |
469
|
|
|
* |
470
|
|
|
* @see TestContent |
471
|
|
|
* |
472
|
|
|
* @param array $cmb Metabox data |
473
|
|
|
* @return string cmb value |
474
|
|
|
*/ |
475
|
|
|
private function oembed( $cmb ) { |
|
|
|
|
476
|
|
|
|
477
|
|
|
return TestContent::oembed(); |
478
|
|
|
|
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Update the metabox with new data. |
484
|
|
|
* |
485
|
|
|
* @access private |
486
|
|
|
* |
487
|
|
|
* @see add_post_meta |
488
|
|
|
* |
489
|
|
|
* @param int $post_id Post ID. |
490
|
|
|
* @param string $value Value to add into the database. |
491
|
|
|
* @param array $cmb SMB data. |
492
|
|
|
*/ |
493
|
|
|
private function update_meta( $post_id, $value, $cmb ) { |
494
|
|
|
|
495
|
|
|
$type = $cmb['type']; |
496
|
|
|
$id = $cmb['id']; |
497
|
|
|
$value = apply_filters( "tc_{$type}_metabox", $value ); // Filter by metabox type |
498
|
|
|
$value = apply_filters( "tc_{$id}_metabox", $value ); // Filter by metabox ID |
499
|
|
|
|
500
|
|
|
// Files must be treated separately - they use the attachment ID |
501
|
|
|
// & url of media for separate cmb values. (only in cmb1 & cmb2 though) |
502
|
|
|
if ( 'file'!== $cmb['type'] || ( 'file' === $cmb['type'] && 'cmb_hm' === $cmb['source'] ) ) { |
503
|
|
|
add_post_meta( $post_id, $cmb['id'], $value, true ); |
504
|
|
|
} else { |
505
|
|
|
add_post_meta( $post_id, $cmb['id'].'_id', $value, true ); |
506
|
|
|
add_post_meta( $post_id, $cmb['id'], wp_get_attachment_url( $value ), true ); |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
// Add extra, redundant meta. Because, why not have two rows for the price of one? |
510
|
|
|
if ( isset( $cmb['source'] ) && 'acf' === $cmb['source'] ) { |
511
|
|
|
add_post_meta( $post_id, '_' . $cmb['id'], $cmb['key'], true ); |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
} |
517
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.