Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CMB2_Field often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CMB2_Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class CMB2_Field extends CMB2_Base { |
||
|
|||
18 | |||
19 | /** |
||
20 | * The object properties name. |
||
21 | * @var string |
||
22 | * @since 2.2.3 |
||
23 | */ |
||
24 | protected $properties_name = 'args'; |
||
25 | |||
26 | /** |
||
27 | * Field arguments |
||
28 | * @var mixed |
||
29 | * @since 1.1.0 |
||
30 | */ |
||
31 | public $args = array(); |
||
32 | |||
33 | /** |
||
34 | * Field group object or false (if no group) |
||
35 | * @var mixed |
||
36 | * @since 1.1.0 |
||
37 | */ |
||
38 | public $group = false; |
||
39 | |||
40 | /** |
||
41 | * Field meta value |
||
42 | * @var mixed |
||
43 | * @since 1.1.0 |
||
44 | */ |
||
45 | public $value = null; |
||
46 | |||
47 | /** |
||
48 | * Field meta value |
||
49 | 46 | * @var mixed |
|
50 | * @since 1.1.0 |
||
51 | 46 | */ |
|
52 | public $escaped_value = null; |
||
53 | |||
54 | /** |
||
55 | * Grouped Field's current numeric index during the save process |
||
56 | 46 | * @var mixed |
|
57 | 46 | * @since 2.0.0 |
|
58 | 46 | */ |
|
59 | public $index = 0; |
||
60 | |||
61 | 46 | /** |
|
62 | * Array of field options |
||
63 | * @var array |
||
64 | * @since 2.0.0 |
||
65 | */ |
||
66 | protected $field_options = array(); |
||
67 | |||
68 | /** |
||
69 | * Array of provided field text strings |
||
70 | * @var array |
||
71 | * @since 2.0.0 |
||
72 | */ |
||
73 | protected $strings; |
||
74 | |||
75 | /** |
||
76 | * The field's render context. In most cases, 'edit', but can be 'display'. |
||
77 | * @var string |
||
78 | 46 | * @since 2.2.2 |
|
79 | */ |
||
80 | public $render_context = 'edit'; |
||
81 | |||
82 | /** |
||
83 | * All CMB2_Field callable field arguments. |
||
84 | * Can be used to determine if a field argument is callable. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | public static $callable_fields = array( |
||
89 | 'default', |
||
90 | 'row_classes', |
||
91 | 'options_cb', |
||
92 | 'label_cb', |
||
93 | 'render_row_cb', |
||
94 | 'before_group', |
||
95 | 'before_group_row', |
||
96 | 'before_row', |
||
97 | 46 | 'before', |
|
98 | 'before_field', |
||
99 | 'after_field', |
||
100 | 46 | 'after', |
|
101 | 46 | 'after_row', |
|
102 | 46 | 'after_group_row', |
|
103 | 46 | 'after_group', |
|
104 | ); |
||
105 | |||
106 | /** |
||
107 | * Constructs our field object |
||
108 | * @since 1.1.0 |
||
109 | * @param array $args Field arguments |
||
110 | */ |
||
111 | public function __construct( $args ) { |
||
112 | 38 | ||
113 | 38 | if ( ! empty( $args['group_field'] ) ) { |
|
114 | 38 | $this->group = $args['group_field']; |
|
115 | $this->object_id = $this->group->object_id; |
||
116 | $this->object_type = $this->group->object_type; |
||
117 | $this->cmb_id = $this->group->cmb_id; |
||
118 | } else { |
||
119 | $this->object_id = isset( $args['object_id'] ) && '_' !== $args['object_id'] ? $args['object_id'] : 0; |
||
120 | $this->object_type = isset( $args['object_type'] ) ? $args['object_type'] : 'post'; |
||
121 | |||
122 | if ( isset( $args['cmb_id'] ) ) { |
||
123 | 46 | $this->cmb_id = $args['cmb_id']; |
|
124 | 46 | } |
|
125 | 46 | } |
|
126 | |||
127 | $this->args = $this->_set_field_defaults( $args['field_args'], $args ); |
||
128 | |||
129 | if ( $this->object_id ) { |
||
130 | $this->value = $this->get_data(); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | 46 | * Non-existent methods fallback to checking for field arguments of the same name |
|
136 | 46 | * @since 1.1.0 |
|
137 | 46 | * @param string $name Method name |
|
138 | * @param array $arguments Array of passed-in arguments |
||
139 | * @return mixed Value of field argument |
||
140 | 46 | */ |
|
141 | public function __call( $name, $arguments ) { |
||
142 | if ( 'string' === $name ) { |
||
143 | return call_user_func_array( array( $this, 'get_string' ), $arguments ); |
||
144 | } |
||
145 | |||
146 | $key = isset( $arguments[0] ) ? $arguments[0] : false; |
||
147 | return $this->args( $name, $key ); |
||
148 | } |
||
149 | 37 | ||
150 | 37 | /** |
|
151 | * Retrieves the field id |
||
152 | * @since 1.1.0 |
||
153 | * @param boolean $raw Whether to retrieve pre-modidifed id |
||
154 | * @return string Field id |
||
155 | */ |
||
156 | public function id( $raw = false ) { |
||
157 | $id = $raw ? '_id' : 'id'; |
||
158 | return $this->args( $id ); |
||
159 | } |
||
160 | 46 | ||
161 | 46 | /** |
|
162 | 46 | * Get a field argument |
|
163 | 46 | * @since 1.1.0 |
|
164 | * @param string $key Argument to check |
||
165 | 46 | * @param string $_key Sub argument to check |
|
166 | * @return mixed Argument value or false if non-existent |
||
167 | */ |
||
168 | public function args( $key = '', $_key = '' ) { |
||
169 | $arg = $this->_data( 'args', $key ); |
||
170 | |||
171 | if ( in_array( $key, array( 'default', 'default_cb' ), true ) ) { |
||
172 | |||
173 | $arg = $this->get_default(); |
||
174 | 46 | ||
175 | 46 | } elseif ( $_key ) { |
|
176 | |||
177 | 46 | $arg = isset( $arg[ $_key ] ) ? $arg[ $_key ] : false; |
|
178 | } |
||
179 | |||
180 | return $arg; |
||
181 | 46 | } |
|
182 | |||
183 | 46 | /** |
|
184 | 46 | * Retrieve a portion of a field property |
|
185 | 46 | * @since 1.1.0 |
|
186 | * @param string $var Field property to check |
||
187 | 46 | * @param string $key Field property array key to check |
|
188 | * @return mixed Queried property value or false |
||
189 | */ |
||
190 | public function _data( $var, $key = '' ) { |
||
191 | $vars = $this->{$var}; |
||
192 | 46 | if ( $key ) { |
|
193 | return array_key_exists( $key, $vars ) ? $vars[ $key ] : false; |
||
194 | } |
||
195 | return $vars; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Get Field's value |
||
200 | * @since 1.1.0 |
||
201 | * @param string $key If value is an array, is used to get array key->value |
||
202 | * @return mixed Field value or false if non-existent |
||
203 | */ |
||
204 | public function value( $key = '' ) { |
||
205 | return $this->_data( 'value', $key ); |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Retrieves metadata/option data |
||
210 | * @since 1.0.1 |
||
211 | * @param string $field_id Meta key/Option array key |
||
212 | * @param array $args Override arguments |
||
213 | * @return mixed Meta/Option value |
||
214 | */ |
||
215 | public function get_data( $field_id = '', $args = array() ) { |
||
216 | if ( $field_id ) { |
||
217 | $args['field_id'] = $field_id; |
||
218 | } else if ( $this->group ) { |
||
219 | $args['field_id'] = $this->group->id(); |
||
220 | } |
||
221 | |||
222 | $a = $this->data_args( $args ); |
||
223 | |||
224 | /** |
||
225 | * Filter whether to override getting of meta value. |
||
226 | * Returning a non 'cmb2_field_no_override_val' value |
||
227 | * will effectively short-circuit the value retrieval. |
||
228 | * |
||
229 | * @since 2.0.0 |
||
230 | * |
||
231 | * @param mixed $value The value get_metadata() should |
||
232 | * return - a single metadata value, |
||
233 | * or an array of values. |
||
234 | * |
||
235 | * @param int $object_id Object ID. |
||
236 | * |
||
237 | * @param array $args { |
||
238 | * An array of arguments for retrieving data |
||
239 | * |
||
240 | * @type string $type The current object type |
||
241 | * @type int $id The current object ID |
||
242 | * @type string $field_id The ID of the field being requested |
||
243 | * @type bool $repeat Whether current field is repeatable |
||
244 | * @type bool $single Whether current field is a single database row |
||
245 | * } |
||
246 | * |
||
247 | * @param CMB2_Field object $field This field object |
||
248 | */ |
||
249 | $data = apply_filters( 'cmb2_override_meta_value', 'cmb2_field_no_override_val', $this->object_id, $a, $this ); |
||
250 | |||
251 | /** |
||
252 | * Filter and parameters are documented for 'cmb2_override_meta_value' filter (above). |
||
253 | * |
||
254 | * The dynamic portion of the hook, $field_id, refers to the current |
||
255 | * field id paramater. Returning a non 'cmb2_field_no_override_val' value |
||
256 | * will effectively short-circuit the value retrieval. |
||
257 | * |
||
258 | * @since 2.0.0 |
||
259 | */ |
||
260 | $data = apply_filters( "cmb2_override_{$a['field_id']}_meta_value", $data, $this->object_id, $a, $this ); |
||
261 | |||
262 | // If no override, get value normally |
||
263 | if ( 'cmb2_field_no_override_val' === $data ) { |
||
264 | $data = 'options-page' === $a['type'] |
||
265 | ? cmb2_options( $a['id'] )->get( $a['field_id'] ) |
||
266 | : get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || $a['repeat'] ) ); |
||
267 | } |
||
268 | |||
269 | if ( $this->group ) { |
||
270 | |||
271 | $data = is_array( $data ) && isset( $data[ $this->group->index ][ $this->args( '_id' ) ] ) |
||
272 | ? $data[ $this->group->index ][ $this->args( '_id' ) ] |
||
273 | : false; |
||
274 | } |
||
275 | |||
276 | return $data; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Updates metadata/option data |
||
281 | * @since 1.0.1 |
||
282 | * @param mixed $new_value Value to update data with |
||
283 | * @param bool $single Whether data is an array (add_metadata) |
||
284 | */ |
||
285 | public function update_data( $new_value, $single = true ) { |
||
286 | $a = $this->data_args( array( 'single' => $single ) ); |
||
287 | |||
288 | $a['value'] = $a['repeat'] ? array_values( $new_value ) : $new_value; |
||
289 | |||
290 | /** |
||
291 | * Filter whether to override saving of meta value. |
||
292 | * Returning a non-null value will effectively short-circuit the function. |
||
293 | * |
||
294 | * @since 2.0.0 |
||
295 | * |
||
296 | * @param null|bool $check Whether to allow updating metadata for the given type. |
||
297 | * |
||
298 | * @param array $args { |
||
299 | * Array of data about current field including: |
||
300 | * |
||
301 | * @type string $value The value to set |
||
302 | * @type string $type The current object type |
||
303 | * @type int $id The current object ID |
||
304 | * @type string $field_id The ID of the field being updated |
||
305 | * @type bool $repeat Whether current field is repeatable |
||
306 | * @type bool $single Whether current field is a single database row |
||
307 | * } |
||
308 | * |
||
309 | * @param array $field_args All field arguments |
||
310 | * |
||
311 | * @param CMB2_Field object $field This field object |
||
312 | */ |
||
313 | $override = apply_filters( 'cmb2_override_meta_save', null, $a, $this->args(), $this ); |
||
314 | |||
315 | /** |
||
316 | * Filter and parameters are documented for 'cmb2_override_meta_save' filter (above). |
||
317 | * |
||
318 | * The dynamic portion of the hook, $a['field_id'], refers to the current |
||
319 | * field id paramater. Returning a non-null value |
||
320 | * will effectively short-circuit the function. |
||
321 | * |
||
322 | * @since 2.0.0 |
||
323 | */ |
||
324 | $override = apply_filters( "cmb2_override_{$a['field_id']}_meta_save", $override, $a, $this->args(), $this ); |
||
325 | |||
326 | // If override, return that |
||
327 | if ( null !== $override ) { |
||
328 | return $override; |
||
329 | } |
||
330 | |||
331 | // Options page handling (or temp data store) |
||
332 | if ( 'options-page' === $a['type'] || empty( $a['id'] ) ) { |
||
333 | return cmb2_options( $a['id'] )->update( $a['field_id'], $a['value'], false, $a['single'] ); |
||
334 | } |
||
335 | |||
336 | // Add metadata if not single |
||
337 | if ( ! $a['single'] ) { |
||
338 | return add_metadata( $a['type'], $a['id'], $a['field_id'], $a['value'], false ); |
||
339 | } |
||
340 | |||
341 | // Delete meta if we have an empty array |
||
342 | if ( is_array( $a['value'] ) && empty( $a['value'] ) ) { |
||
343 | return delete_metadata( $a['type'], $a['id'], $a['field_id'], $this->value ); |
||
344 | } |
||
345 | |||
346 | // Update metadata |
||
347 | return update_metadata( $a['type'], $a['id'], $a['field_id'], $a['value'] ); |
||
348 | } |
||
349 | |||
350 | 46 | /** |
|
351 | 46 | * Removes/updates metadata/option data |
|
352 | 46 | * @since 1.0.1 |
|
353 | 46 | * @param string $old Old value |
|
354 | 46 | */ |
|
355 | 46 | public function remove_data( $old = '' ) { |
|
356 | 46 | $a = $this->data_args( array( 'old' => $old ) ); |
|
357 | 46 | ||
358 | 46 | /** |
|
359 | * Filter whether to override removing of meta value. |
||
360 | * Returning a non-null value will effectively short-circuit the function. |
||
361 | * |
||
362 | * @since 2.0.0 |
||
363 | * |
||
364 | * @param null|bool $delete Whether to allow metadata deletion of the given type. |
||
365 | * @param array $args Array of data about current field including: |
||
366 | * 'type' : Current object type |
||
367 | * 'id' : Current object ID |
||
368 | * 'field_id' : Current Field ID |
||
369 | * 'repeat' : Whether current field is repeatable |
||
370 | * 'single' : Whether to save as a |
||
371 | * single meta value |
||
372 | * @param array $field_args All field arguments |
||
373 | * @param CMB2_Field object $field This field object |
||
374 | */ |
||
375 | $override = apply_filters( 'cmb2_override_meta_remove', null, $a, $this->args(), $this ); |
||
376 | |||
377 | /** |
||
378 | * Filter whether to override removing of meta value. |
||
379 | * |
||
380 | * The dynamic portion of the hook, $a['field_id'], refers to the current |
||
381 | * field id paramater. Returning a non-null value |
||
382 | * will effectively short-circuit the function. |
||
383 | * |
||
384 | * @since 2.0.0 |
||
385 | * |
||
386 | * @param null|bool $delete Whether to allow metadata deletion of the given type. |
||
387 | * @param array $args Array of data about current field including: |
||
388 | * 'type' : Current object type |
||
389 | * 'id' : Current object ID |
||
390 | * 'field_id' : Current Field ID |
||
391 | * 'repeat' : Whether current field is repeatable |
||
392 | * 'single' : Whether to save as a |
||
393 | * single meta value |
||
394 | * @param array $field_args All field arguments |
||
395 | * @param CMB2_Field object $field This field object |
||
396 | */ |
||
397 | $override = apply_filters( "cmb2_override_{$a['field_id']}_meta_remove", $override, $a, $this->args(), $this ); |
||
398 | |||
399 | // If no override, remove as usual |
||
400 | if ( null !== $override ) { |
||
401 | return $override; |
||
402 | } |
||
403 | // Option page handling |
||
404 | elseif ( 'options-page' === $a['type'] || empty( $a['id'] ) ) { |
||
405 | return cmb2_options( $a['id'] )->remove( $a['field_id'] ); |
||
406 | } |
||
407 | |||
408 | // Remove metadata |
||
409 | return delete_metadata( $a['type'], $a['id'], $a['field_id'], $old ); |
||
410 | } |
||
411 | |||
412 | /** |
||
413 | * Data variables for get/set data methods |
||
414 | * @since 1.1.0 |
||
415 | * @param array $args Override arguments |
||
416 | * @return array Updated arguments |
||
417 | */ |
||
418 | public function data_args( $args = array() ) { |
||
419 | $args = wp_parse_args( $args, array( |
||
420 | 'type' => $this->object_type, |
||
421 | 'id' => $this->object_id, |
||
422 | 'field_id' => $this->id( true ), |
||
423 | 'repeat' => $this->args( 'repeatable' ), |
||
424 | 'single' => ! $this->args( 'multiple' ), |
||
425 | ) ); |
||
426 | return $args; |
||
427 | } |
||
428 | |||
429 | 39 | /** |
|
430 | 39 | * Checks if field has a registered sanitization callback |
|
431 | 39 | * @since 1.0.1 |
|
432 | 38 | * @param mixed $meta_value Meta value |
|
433 | * @return mixed Possibly sanitized meta value |
||
434 | */ |
||
435 | public function sanitization_cb( $meta_value ) { |
||
436 | 7 | ||
437 | if ( $this->args( 'repeatable' ) && is_array( $meta_value ) ) { |
||
438 | // Remove empties |
||
439 | 7 | $meta_value = array_filter( $meta_value ); |
|
440 | 4 | } |
|
441 | |||
442 | // Check if the field has a registered validation callback |
||
443 | 3 | $cb = $this->maybe_callback( 'sanitization_cb' ); |
|
444 | 2 | if ( false === $cb ) { |
|
445 | // If requesting NO validation, return meta value |
||
446 | 3 | return $meta_value; |
|
447 | } elseif ( $cb ) { |
||
448 | // Ok, callback is good, let's run it. |
||
449 | return call_user_func( $cb, $meta_value, $this->args(), $this ); |
||
450 | } |
||
451 | |||
452 | $sanitizer = new CMB2_Sanitize( $this, $meta_value ); |
||
453 | 37 | ||
454 | /** |
||
455 | 37 | * Filter the value before it is saved. |
|
456 | 37 | * |
|
457 | 37 | * The dynamic portion of the hook name, $this->type(), refers to the field type. |
|
458 | 37 | * |
|
459 | 37 | * Passing a non-null value to the filter will short-circuit saving |
|
460 | * the field value, saving the passed value instead. |
||
461 | * |
||
462 | * @param bool|mixed $override_value Sanitization/Validation override value to return. |
||
463 | * Default false to skip it. |
||
464 | * @param mixed $value The value to be saved to this field. |
||
465 | * @param int $object_id The ID of the object where the value will be saved |
||
466 | * @param array $field_args The current field's arguments |
||
467 | * @param object $sanitizer This `CMB2_Sanitize` object |
||
468 | 1 | */ |
|
469 | $override_value = apply_filters( "cmb2_sanitize_{$this->type()}", null, $sanitizer->value, $this->object_id, $this->args(), $sanitizer ); |
||
470 | 1 | ||
471 | 1 | if ( null !== $override_value ) { |
|
472 | 1 | return $override_value; |
|
473 | 1 | } |
|
474 | 1 | ||
475 | // Sanitization via 'CMB2_Sanitize' |
||
476 | 1 | return $sanitizer->{$this->type()}(); |
|
477 | 1 | } |
|
478 | 1 | ||
479 | 1 | /** |
|
480 | 1 | * Process $_POST data to save this field's value |
|
481 | 1 | * @since 2.0.3 |
|
482 | 1 | * @param array $data_to_save $_POST data to check |
|
483 | * @return array|int|bool Result of save, false on failure |
||
484 | */ |
||
485 | public function save_field_from_data( array $data_to_save ) { |
||
486 | $this->data_to_save = $data_to_save; |
||
487 | |||
488 | $meta_value = isset( $this->data_to_save[ $this->id( true ) ] ) |
||
489 | ? $this->data_to_save[ $this->id( true ) ] |
||
490 | : null; |
||
491 | |||
492 | 37 | return $this->save_field( $meta_value ); |
|
493 | } |
||
494 | 37 | ||
495 | 16 | /** |
|
496 | * Sanitize/store a value to this field |
||
497 | * @since 2.0.0 |
||
498 | 37 | * @param array $meta_value Desired value to sanitize/store |
|
499 | * @return array|int|bool Result of save. false on failure |
||
500 | */ |
||
501 | 37 | public function save_field( $meta_value ) { |
|
502 | |||
503 | $updated = false; |
||
504 | $action = ''; |
||
505 | $new_value = $this->sanitization_cb( $meta_value ); |
||
506 | |||
507 | 37 | if ( ! $this->args( 'save_field' ) ) { |
|
508 | 37 | ||
509 | // Nothing to see here. |
||
510 | $action = 'disabled'; |
||
511 | |||
512 | 37 | } elseif ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) { |
|
513 | |||
514 | 4 | $this->remove_data(); |
|
515 | $count = 0; |
||
516 | |||
517 | if ( ! empty( $new_value ) ) { |
||
518 | 33 | foreach ( $new_value as $add_new ) { |
|
519 | 33 | if ( $this->update_data( $add_new, false ) ) { |
|
520 | $count++; |
||
521 | 33 | } |
|
522 | } |
||
523 | } |
||
524 | |||
525 | $updated = $count ? $count : false; |
||
526 | 33 | $action = 'repeatable'; |
|
527 | |||
528 | } elseif ( ! CMB2_Utils::isempty( $new_value ) && $new_value !== $this->get_data() ) { |
||
529 | 33 | $updated = $this->update_data( $new_value ); |
|
530 | 33 | $action = 'updated'; |
|
531 | } elseif ( CMB2_Utils::isempty( $new_value ) ) { |
||
532 | $updated = $this->remove_data(); |
||
533 | $action = 'removed'; |
||
534 | } |
||
535 | |||
536 | if ( $updated ) { |
||
537 | $this->value = $this->get_data(); |
||
538 | 1 | $this->escaped_value = null; |
|
539 | 1 | } |
|
540 | |||
541 | $field_id = $this->id( true ); |
||
542 | |||
543 | /** |
||
544 | * Hooks after save field action. |
||
545 | * |
||
546 | * @since 2.2.0 |
||
547 | 1 | * |
|
548 | * @param string $field_id the current field id paramater. |
||
549 | * @param bool $updated Whether the metadata update action occurred. |
||
550 | 1 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
|
551 | * @param CMB2_Field object $field This field object |
||
552 | */ |
||
553 | do_action( 'cmb2_save_field', $field_id, $updated, $action, $this ); |
||
554 | 1 | ||
555 | /** |
||
556 | * Hooks after save field action. |
||
557 | * |
||
558 | 1 | * The dynamic portion of the hook, $field_id, refers to the |
|
559 | * current field id paramater. |
||
560 | * |
||
561 | * @since 2.2.0 |
||
562 | * |
||
563 | * @param bool $updated Whether the metadata update action occurred. |
||
564 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
||
565 | * @param CMB2_Field object $field This field object |
||
566 | */ |
||
567 | do_action( "cmb2_save_field_{$field_id}", $updated, $action, $this ); |
||
568 | 5 | ||
569 | 5 | return $updated; |
|
570 | } |
||
571 | |||
572 | /** |
||
573 | * Determine if current type is exempt from escaping |
||
574 | * @since 1.1.0 |
||
575 | * @return bool True if exempt |
||
576 | */ |
||
577 | public function escaping_exception() { |
||
578 | 5 | // These types cannot be escaped |
|
579 | 5 | return in_array( $this->type(), array( |
|
580 | 5 | 'file_list', |
|
581 | 'multicheck', |
||
582 | 5 | 'text_datetime_timestamp_timezone', |
|
583 | ) ); |
||
584 | } |
||
585 | |||
586 | 5 | /** |
|
587 | 5 | * Determine if current type cannot be repeatable |
|
588 | 5 | * @since 1.1.0 |
|
589 | * @param string $type Field type to check |
||
590 | * @return bool True if type cannot be repeatable |
||
591 | */ |
||
592 | public function repeatable_exception( $type ) { |
||
593 | // These types cannot be repeatable. |
||
594 | $internal_fields = array( |
||
595 | 5 | // Use file_list instead |
|
596 | 'file' => 1, |
||
597 | 'radio' => 1, |
||
598 | 5 | 'title' => 1, |
|
599 | // @todo Ajax load wp_editor: http://wordpress.stackexchange.com/questions/51776/how-to-load-wp-editor-through-ajax-jquery |
||
600 | 'wysiwyg' => 1, |
||
601 | 'checkbox' => 1, |
||
602 | 'radio_inline' => 1, |
||
603 | 5 | 'taxonomy_radio' => 1, |
|
604 | 'taxonomy_select' => 1, |
||
605 | 'taxonomy_multicheck' => 1, |
||
606 | ); |
||
607 | 5 | ||
608 | /** |
||
609 | 5 | * Filter field types that are non-repeatable. |
|
610 | * |
||
611 | 5 | * Note that this does *not* allow overriding the default non-repeatable types. |
|
612 | * |
||
613 | * @since 2.1.1 |
||
614 | * |
||
615 | * @param array $fields Array of fields designated as non-repeatable. Note that the field names are *keys*, |
||
616 | * and not values. The value can be anything, because it is meaningless. Example: |
||
617 | * array( 'my_custom_field' => 1 ) |
||
618 | */ |
||
619 | $all_fields = array_merge( apply_filters( 'cmb2_non_repeatable_fields', array() ), $internal_fields ); |
||
620 | 5 | return isset( $all_fields[ $type ] ); |
|
621 | 5 | } |
|
622 | 5 | ||
623 | /** |
||
624 | 5 | * Escape the value before output. Defaults to 'esc_attr()' |
|
625 | * @since 1.0.1 |
||
626 | * @param callable $func Escaping function (if not esc_attr()) |
||
627 | 5 | * @param mixed $meta_value Meta value |
|
628 | * @return mixed Final value |
||
629 | 5 | */ |
|
630 | 5 | public function escaped_value( $func = 'esc_attr', $meta_value = '' ) { |
|
631 | |||
632 | 5 | if ( null !== $this->escaped_value ) { |
|
633 | return $this->escaped_value; |
||
634 | 5 | } |
|
635 | |||
636 | 5 | $meta_value = $meta_value ? $meta_value : $this->value(); |
|
637 | 5 | ||
638 | // Check if the field has a registered escaping callback |
||
639 | if ( $cb = $this->maybe_callback( 'escape_cb' ) ) { |
||
640 | // Ok, callback is good, let's run it. |
||
641 | return call_user_func( $cb, $meta_value, $this->args(), $this ); |
||
642 | } |
||
643 | |||
644 | // Or custom escaping filter can be used |
||
645 | 5 | $esc = apply_filters( "cmb2_types_esc_{$this->type()}", null, $meta_value, $this->args(), $this ); |
|
646 | 5 | if ( null !== $esc ) { |
|
647 | 5 | return $esc; |
|
648 | 5 | } |
|
649 | 5 | ||
650 | if ( false === $cb || $this->escaping_exception() ) { |
||
651 | 5 | // If requesting NO escaping, return meta value |
|
652 | return $this->val_or_default( $meta_value ); |
||
653 | 5 | } |
|
654 | 5 | ||
655 | 5 | // escaping function passed in? |
|
656 | $func = $func ? $func : 'esc_attr'; |
||
657 | 5 | $meta_value = $this->val_or_default( $meta_value ); |
|
658 | 3 | ||
659 | 3 | if ( is_array( $meta_value ) ) { |
|
660 | foreach ( $meta_value as $key => $value ) { |
||
661 | 5 | $meta_value[ $key ] = call_user_func( $func, $value ); |
|
662 | } |
||
663 | } else { |
||
664 | $meta_value = call_user_func( $func, $meta_value ); |
||
665 | } |
||
666 | |||
667 | $this->escaped_value = $meta_value; |
||
668 | return $this->escaped_value; |
||
669 | } |
||
670 | |||
671 | 38 | /** |
|
672 | 38 | * Return non-empty value or field default if value IS empty |
|
673 | * @since 2.0.0 |
||
674 | 2 | * @param mixed $meta_value Field value |
|
675 | 2 | * @return mixed Field value, or default value |
|
676 | */ |
||
677 | public function val_or_default( $meta_value ) { |
||
678 | return ! CMB2_Utils::isempty( $meta_value ) ? $meta_value : $this->get_default(); |
||
679 | 38 | } |
|
680 | 38 | ||
681 | /** |
||
682 | * Offset a time value based on timezone |
||
683 | * @since 1.0.0 |
||
684 | * @return string Offset time string |
||
685 | */ |
||
686 | public function field_timezone_offset() { |
||
687 | return CMB2_Utils::timezone_offset( $this->field_timezone() ); |
||
688 | } |
||
689 | |||
690 | /** |
||
691 | * Return timezone string |
||
692 | * @since 1.0.0 |
||
693 | * @return string Timezone string |
||
694 | */ |
||
695 | public function field_timezone() { |
||
696 | $value = ''; |
||
697 | |||
698 | // Is timezone arg set? |
||
699 | 14 | if ( $this->args( 'timezone' ) ) { |
|
700 | 14 | $value = $this->args( 'timezone' ); |
|
701 | 5 | } |
|
702 | 4 | // Is there another meta key with a timezone stored as its value we should use? |
|
703 | else if ( $this->args( 'timezone_meta_key' ) ) { |
||
704 | $value = $this->get_data( $this->args( 'timezone_meta_key' ) ); |
||
705 | 1 | } |
|
706 | |||
707 | return $value; |
||
708 | 14 | } |
|
709 | |||
710 | 14 | /** |
|
711 | 1 | * Format the timestamp field value based on the field date/time format arg |
|
712 | * @since 2.0.0 |
||
713 | 1 | * @param int $meta_value Timestamp |
|
714 | 1 | * @param string $format Either date_format or time_format |
|
715 | 1 | * @return string Formatted date |
|
716 | 1 | */ |
|
717 | public function format_timestamp( $meta_value, $format = 'date_format' ) { |
||
720 | |||
721 | /** |
||
722 | 11 | * Return a formatted timestamp for a field |
|
723 | * @since 2.0.0 |
||
724 | * @param string $format Either date_format or time_format |
||
725 | * @param string $meta_value Optional meta value to check |
||
726 | * @return string Formatted date |
||
727 | */ |
||
728 | public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) { |
||
729 | $meta_value = $meta_value ? $meta_value : $this->escaped_value(); |
||
730 | 46 | $meta_value = CMB2_Utils::make_valid_time_stamp( $meta_value ); |
|
731 | |||
732 | if ( empty( $meta_value ) ) { |
||
733 | 46 | return ''; |
|
734 | 46 | } |
|
735 | 46 | ||
736 | 46 | return is_array( $meta_value ) |
|
737 | 46 | ? array_map( array( $this, 'format_timestamp' ), $meta_value, $format ) |
|
738 | 46 | : $this->format_timestamp( $meta_value, $format ); |
|
739 | 46 | } |
|
740 | 46 | ||
741 | 46 | /** |
|
742 | 46 | * Get timestamp from text date |
|
743 | 46 | * @since 2.2.0 |
|
744 | 46 | * @param string $value Date value |
|
745 | 46 | * @return mixed Unix timestamp representing the date. |
|
746 | 46 | */ |
|
747 | 46 | public function get_timestamp_from_value( $value ) { |
|
750 | 46 | ||
751 | 46 | /** |
|
752 | 46 | * Get field render callback and Render the field row |
|
753 | 46 | * @since 1.0.0 |
|
754 | 46 | */ |
|
755 | public function render_field() { |
||
763 | |||
764 | 46 | /** |
|
765 | * Default field render callback |
||
766 | * @since 2.1.1 |
||
767 | */ |
||
768 | public function render_field_callback() { |
||
812 | |||
813 | /** |
||
814 | * The default label_cb callback (if not a title field) |
||
815 | * |
||
816 | * @since 2.1.1 |
||
817 | * @return string Label html markup |
||
818 | */ |
||
819 | public function label() { |
||
828 | |||
829 | /** |
||
830 | * Defines the classes for the current CMB2 field row |
||
831 | * |
||
832 | * @since 2.0.0 |
||
833 | * @return string Space concatenated list of classes |
||
834 | */ |
||
835 | public function row_classes() { |
||
885 | |||
886 | |||
887 | |||
888 | /** |
||
889 | * Get field display callback and render the display value in the column. |
||
890 | * @since 2.2.2 |
||
891 | */ |
||
892 | public function render_column() { |
||
900 | |||
901 | /** |
||
902 | * Default callback to outputs field value in a display format. |
||
903 | * @since 2.2.2 |
||
904 | */ |
||
905 | public function display_value_callback() { |
||
948 | |||
949 | /** |
||
950 | * Replaces a hash key - {#} - with the repeatable index |
||
951 | * @since 1.2.0 |
||
952 | * @param string $value Value to update |
||
953 | * @return string Updated value |
||
954 | */ |
||
955 | public function replace_hash( $value ) { |
||
959 | |||
960 | /** |
||
961 | * Retrieve text parameter from field's text array (if it has one), or use fallback text |
||
962 | * For back-compatibility, falls back to checking the options array. |
||
963 | * |
||
964 | * @since 2.2.2 |
||
965 | * @param string $text_key Key in field's text array |
||
966 | * @param string $fallback Fallback text |
||
967 | * @return string Text |
||
968 | */ |
||
969 | public function get_string( $text_key, $fallback ) { |
||
993 | |||
994 | /** |
||
995 | * Retrieve options args. Calls options_cb if it exists. |
||
996 | * @since 2.0.0 |
||
997 | * @param string $key Specific option to retrieve |
||
998 | * @return array Array of options |
||
999 | */ |
||
1000 | public function options( $key = '' ) { |
||
1025 | |||
1026 | /** |
||
1027 | * Store JS dependencies as part of the field args. |
||
1028 | * @since 2.2.0 |
||
1029 | * @param array $dependencies Dependies to register for this field. |
||
1030 | */ |
||
1031 | public function add_js_dependencies( $dependencies = array() ) { |
||
1038 | |||
1039 | /** |
||
1040 | * Get CMB2_Field default value, either from default param or default_cb param. |
||
1041 | * |
||
1042 | * @since 0.2.2 |
||
1043 | * |
||
1044 | * @return mixed Default field value |
||
1045 | */ |
||
1046 | public function get_default() { |
||
1059 | |||
1060 | /** |
||
1061 | * Fills in empty field parameters with defaults |
||
1062 | * @since 1.1.0 |
||
1063 | * @param array $args Metabox field config array |
||
1064 | * @param array Modified field config array. |
||
1065 | */ |
||
1066 | public function _set_field_defaults( $args ) { |
||
1162 | |||
1163 | /** |
||
1164 | * Get default field arguments specific to this CMB2 object. |
||
1165 | * @since 2.2.0 |
||
1166 | * @param array $field_args Metabox field config array. |
||
1167 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1168 | * @return array Array of field arguments. |
||
1169 | */ |
||
1170 | protected function get_default_args( $field_args, $field_group = null ) { |
||
1181 | |||
1182 | /** |
||
1183 | * Returns a cloned version of this field object with, but with |
||
1184 | * modified/overridden field arguments. |
||
1185 | * |
||
1186 | * @since 2.2.2 |
||
1187 | * @param array $field_args Array of field arguments, or entire array of |
||
1188 | * arguments for CMB2_Field |
||
1189 | * |
||
1190 | * @return CMB2_Field The new CMB2_Field instance. |
||
1191 | */ |
||
1192 | public function get_field_clone( $field_args ) { |
||
1195 | |||
1196 | /** |
||
1197 | * Returns the CMB2 instance this field is registered to. |
||
1198 | * |
||
1199 | * @since 2.2.2 |
||
1200 | * |
||
1201 | * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error. |
||
1202 | */ |
||
1203 | public function get_cmb() { |
||
1210 | |||
1211 | /** |
||
1212 | * Converts deprecated field parameters to the current/proper parameter, and throws a deprecation notice. |
||
1213 | * @since 2.2.3 |
||
1214 | * @param array $args Metabox field config array. |
||
1215 | * @param array Modified field config array. |
||
1216 | */ |
||
1217 | protected function convert_deprecated_params( $args ) { |
||
1260 | |||
1261 | } |
||
1262 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.