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 | * |
||
22 | * @var string |
||
23 | * @since 2.2.3 |
||
24 | */ |
||
25 | protected $properties_name = 'args'; |
||
26 | |||
27 | /** |
||
28 | * Field arguments |
||
29 | * |
||
30 | * @var mixed |
||
31 | * @since 1.1.0 |
||
32 | */ |
||
33 | public $args = array(); |
||
34 | |||
35 | /** |
||
36 | * Field group object or false (if no group) |
||
37 | * |
||
38 | * @var mixed |
||
39 | * @since 1.1.0 |
||
40 | */ |
||
41 | public $group = false; |
||
42 | |||
43 | /** |
||
44 | * Field meta value |
||
45 | * |
||
46 | * @var mixed |
||
47 | * @since 1.1.0 |
||
48 | */ |
||
49 | 46 | public $value = null; |
|
50 | |||
51 | 46 | /** |
|
52 | * Field meta value |
||
53 | * |
||
54 | * @var mixed |
||
55 | * @since 1.1.0 |
||
56 | 46 | */ |
|
57 | 46 | public $escaped_value = null; |
|
58 | 46 | ||
59 | /** |
||
60 | * Grouped Field's current numeric index during the save process |
||
61 | 46 | * |
|
62 | * @var mixed |
||
63 | * @since 2.0.0 |
||
64 | */ |
||
65 | public $index = 0; |
||
66 | |||
67 | /** |
||
68 | * Array of field options |
||
69 | * |
||
70 | * @var array |
||
71 | * @since 2.0.0 |
||
72 | */ |
||
73 | protected $field_options = array(); |
||
74 | |||
75 | /** |
||
76 | * Array of provided field text strings |
||
77 | * |
||
78 | 46 | * @var array |
|
79 | * @since 2.0.0 |
||
80 | */ |
||
81 | protected $strings; |
||
82 | |||
83 | /** |
||
84 | * The field's render context. In most cases, 'edit', but can be 'display'. |
||
85 | * |
||
86 | * @var string |
||
87 | * @since 2.2.2 |
||
88 | */ |
||
89 | public $render_context = 'edit'; |
||
90 | |||
91 | /** |
||
92 | * All CMB2_Field callable field arguments. |
||
93 | * Can be used to determine if a field argument is callable. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | 46 | public static $callable_fields = array( |
|
98 | 'default_cb', |
||
99 | 'classes_cb', |
||
100 | 46 | 'options_cb', |
|
101 | 46 | 'text_cb', |
|
102 | 46 | 'label_cb', |
|
103 | 46 | 'render_row_cb', |
|
104 | 'display_cb', |
||
105 | 'before_group', |
||
106 | 'before_group_row', |
||
107 | 'before_row', |
||
108 | 'before', |
||
109 | 'before_field', |
||
110 | 'after_field', |
||
111 | 'after', |
||
112 | 38 | 'after_row', |
|
113 | 38 | 'after_group_row', |
|
114 | 38 | 'after_group', |
|
115 | ); |
||
116 | |||
117 | /** |
||
118 | * Constructs our field object |
||
119 | * |
||
120 | * @since 1.1.0 |
||
121 | * @param array $args Field arguments |
||
122 | */ |
||
123 | 46 | public function __construct( $args ) { |
|
124 | 46 | ||
125 | 46 | if ( ! empty( $args['group_field'] ) ) { |
|
126 | $this->group = $args['group_field']; |
||
127 | $this->object_id = $this->group->object_id; |
||
128 | $this->object_type = $this->group->object_type; |
||
129 | $this->cmb_id = $this->group->cmb_id; |
||
130 | } else { |
||
131 | $this->object_id = isset( $args['object_id'] ) && '_' !== $args['object_id'] ? $args['object_id'] : 0; |
||
132 | $this->object_type = isset( $args['object_type'] ) ? $args['object_type'] : 'post'; |
||
133 | |||
134 | if ( isset( $args['cmb_id'] ) ) { |
||
135 | 46 | $this->cmb_id = $args['cmb_id']; |
|
136 | 46 | } |
|
137 | 46 | } |
|
138 | |||
139 | $this->args = $this->_set_field_defaults( $args['field_args'], $args ); |
||
140 | 46 | ||
141 | if ( $this->object_id ) { |
||
142 | $this->value = $this->get_data(); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Non-existent methods fallback to checking for field arguments of the same name |
||
148 | * |
||
149 | 37 | * @since 1.1.0 |
|
150 | 37 | * @param string $name Method name |
|
151 | * @param array $arguments Array of passed-in arguments |
||
152 | * @return mixed Value of field argument |
||
153 | */ |
||
154 | public function __call( $name, $arguments ) { |
||
155 | if ( 'string' === $name ) { |
||
156 | return call_user_func_array( array( $this, 'get_string' ), $arguments ); |
||
157 | } |
||
158 | |||
159 | $key = isset( $arguments[0] ) ? $arguments[0] : false; |
||
160 | 46 | return $this->args( $name, $key ); |
|
161 | 46 | } |
|
162 | 46 | ||
163 | 46 | /** |
|
164 | * Retrieves the field id |
||
165 | 46 | * |
|
166 | * @since 1.1.0 |
||
167 | * @param boolean $raw Whether to retrieve pre-modidifed id |
||
168 | * @return string Field id |
||
169 | */ |
||
170 | public function id( $raw = false ) { |
||
171 | $id = $raw ? '_id' : 'id'; |
||
172 | return $this->args( $id ); |
||
173 | } |
||
174 | 46 | ||
175 | 46 | /** |
|
176 | * Get a field argument |
||
177 | 46 | * |
|
178 | * @since 1.1.0 |
||
179 | * @param string $key Argument to check |
||
180 | * @param string $_key Sub argument to check |
||
181 | 46 | * @return mixed Argument value or false if non-existent |
|
182 | */ |
||
183 | 46 | public function args( $key = '', $_key = '' ) { |
|
184 | 46 | $arg = $this->_data( 'args', $key ); |
|
185 | 46 | ||
186 | if ( in_array( $key, array( 'default', 'default_cb' ), true ) ) { |
||
187 | 46 | ||
188 | $arg = $this->get_default(); |
||
189 | |||
190 | } elseif ( $_key ) { |
||
191 | |||
192 | 46 | $arg = isset( $arg[ $_key ] ) ? $arg[ $_key ] : false; |
|
193 | } |
||
194 | |||
195 | return $arg; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Retrieve a portion of a field property |
||
200 | * |
||
201 | * @since 1.1.0 |
||
202 | * @param string $var Field property to check |
||
203 | * @param string $key Field property array key to check |
||
204 | * @return mixed Queried property value or false |
||
205 | */ |
||
206 | public function _data( $var, $key = '' ) { |
||
207 | $vars = $this->{$var}; |
||
208 | if ( $key ) { |
||
209 | return array_key_exists( $key, $vars ) ? $vars[ $key ] : false; |
||
210 | } |
||
211 | return $vars; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get Field's value |
||
216 | * |
||
217 | * @since 1.1.0 |
||
218 | * @param string $key If value is an array, is used to get array key->value |
||
219 | * @return mixed Field value or false if non-existent |
||
220 | */ |
||
221 | public function value( $key = '' ) { |
||
222 | return $this->_data( 'value', $key ); |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * Retrieves metadata/option data |
||
227 | * |
||
228 | * @since 1.0.1 |
||
229 | * @param string $field_id Meta key/Option array key |
||
230 | * @param array $args Override arguments |
||
231 | * @return mixed Meta/Option value |
||
232 | */ |
||
233 | public function get_data( $field_id = '', $args = array() ) { |
||
234 | if ( $field_id ) { |
||
235 | $args['field_id'] = $field_id; |
||
236 | } elseif ( $this->group ) { |
||
237 | $args['field_id'] = $this->group->id(); |
||
238 | } |
||
239 | |||
240 | $a = $this->data_args( $args ); |
||
241 | |||
242 | /** |
||
243 | * Filter whether to override getting of meta value. |
||
244 | * Returning a non 'cmb2_field_no_override_val' value |
||
245 | * will effectively short-circuit the value retrieval. |
||
246 | * |
||
247 | * @since 2.0.0 |
||
248 | * |
||
249 | * @param mixed $value The value get_metadata() should |
||
250 | * return - a single metadata value, |
||
251 | * or an array of values. |
||
252 | * |
||
253 | * @param int $object_id Object ID. |
||
254 | * |
||
255 | * @param array $args { |
||
256 | * An array of arguments for retrieving data |
||
257 | * |
||
258 | * @type string $type The current object type |
||
259 | * @type int $id The current object ID |
||
260 | * @type string $field_id The ID of the field being requested |
||
261 | * @type bool $repeat Whether current field is repeatable |
||
262 | * @type bool $single Whether current field is a single database row |
||
263 | * } |
||
264 | * |
||
265 | * @param CMB2_Field object $field This field object |
||
266 | */ |
||
267 | $data = apply_filters( 'cmb2_override_meta_value', 'cmb2_field_no_override_val', $this->object_id, $a, $this ); |
||
268 | |||
269 | /** |
||
270 | * Filter and parameters are documented for 'cmb2_override_meta_value' filter (above). |
||
271 | * |
||
272 | * The dynamic portion of the hook, $field_id, refers to the current |
||
273 | * field id paramater. Returning a non 'cmb2_field_no_override_val' value |
||
274 | * will effectively short-circuit the value retrieval. |
||
275 | * |
||
276 | * @since 2.0.0 |
||
277 | */ |
||
278 | $data = apply_filters( "cmb2_override_{$a['field_id']}_meta_value", $data, $this->object_id, $a, $this ); |
||
279 | |||
280 | // If no override, get value normally |
||
281 | if ( 'cmb2_field_no_override_val' === $data ) { |
||
282 | $data = 'options-page' === $a['type'] |
||
283 | ? cmb2_options( $a['id'] )->get( $a['field_id'] ) |
||
284 | : get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || $a['repeat'] ) ); |
||
285 | } |
||
286 | |||
287 | if ( $this->group ) { |
||
288 | |||
289 | $data = is_array( $data ) && isset( $data[ $this->group->index ][ $this->args( '_id' ) ] ) |
||
290 | ? $data[ $this->group->index ][ $this->args( '_id' ) ] |
||
291 | : false; |
||
292 | } |
||
293 | |||
294 | return $data; |
||
295 | } |
||
296 | |||
297 | /** |
||
298 | * Updates metadata/option data |
||
299 | * |
||
300 | * @since 1.0.1 |
||
301 | * @param mixed $new_value Value to update data with |
||
302 | * @param bool $single Whether data is an array (add_metadata) |
||
303 | */ |
||
304 | public function update_data( $new_value, $single = true ) { |
||
305 | $a = $this->data_args( array( 'single' => $single ) ); |
||
306 | |||
307 | $a['value'] = $a['repeat'] ? array_values( $new_value ) : $new_value; |
||
308 | |||
309 | /** |
||
310 | * Filter whether to override saving of meta value. |
||
311 | * Returning a non-null value will effectively short-circuit the function. |
||
312 | * |
||
313 | * @since 2.0.0 |
||
314 | * |
||
315 | * @param null|bool $check Whether to allow updating metadata for the given type. |
||
316 | * |
||
317 | * @param array $args { |
||
318 | * Array of data about current field including: |
||
319 | * |
||
320 | * @type string $value The value to set |
||
321 | * @type string $type The current object type |
||
322 | * @type int $id The current object ID |
||
323 | * @type string $field_id The ID of the field being updated |
||
324 | * @type bool $repeat Whether current field is repeatable |
||
325 | * @type bool $single Whether current field is a single database row |
||
326 | * } |
||
327 | * |
||
328 | * @param array $field_args All field arguments |
||
329 | * |
||
330 | * @param CMB2_Field object $field This field object |
||
331 | */ |
||
332 | $override = apply_filters( 'cmb2_override_meta_save', null, $a, $this->args(), $this ); |
||
333 | |||
334 | /** |
||
335 | * Filter and parameters are documented for 'cmb2_override_meta_save' filter (above). |
||
336 | * |
||
337 | * The dynamic portion of the hook, $a['field_id'], refers to the current |
||
338 | * field id paramater. Returning a non-null value |
||
339 | * will effectively short-circuit the function. |
||
340 | * |
||
341 | * @since 2.0.0 |
||
342 | */ |
||
343 | $override = apply_filters( "cmb2_override_{$a['field_id']}_meta_save", $override, $a, $this->args(), $this ); |
||
344 | |||
345 | // If override, return that |
||
346 | if ( null !== $override ) { |
||
347 | return $override; |
||
348 | } |
||
349 | |||
350 | 46 | // Options page handling (or temp data store) |
|
351 | 46 | if ( 'options-page' === $a['type'] || empty( $a['id'] ) ) { |
|
352 | 46 | return cmb2_options( $a['id'] )->update( $a['field_id'], $a['value'], false, $a['single'] ); |
|
353 | 46 | } |
|
354 | 46 | ||
355 | 46 | // Add metadata if not single |
|
356 | 46 | if ( ! $a['single'] ) { |
|
357 | 46 | return add_metadata( $a['type'], $a['id'], $a['field_id'], $a['value'], false ); |
|
358 | 46 | } |
|
359 | |||
360 | // Delete meta if we have an empty array |
||
361 | if ( is_array( $a['value'] ) && empty( $a['value'] ) ) { |
||
362 | return delete_metadata( $a['type'], $a['id'], $a['field_id'], $this->value ); |
||
363 | } |
||
364 | |||
365 | // Update metadata |
||
366 | return update_metadata( $a['type'], $a['id'], $a['field_id'], $a['value'] ); |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Removes/updates metadata/option data |
||
371 | * |
||
372 | * @since 1.0.1 |
||
373 | * @param string $old Old value |
||
374 | */ |
||
375 | public function remove_data( $old = '' ) { |
||
376 | $a = $this->data_args( array( 'old' => $old ) ); |
||
377 | |||
378 | /** |
||
379 | * Filter whether to override removing of meta value. |
||
380 | * Returning a non-null value will effectively short-circuit the function. |
||
381 | * |
||
382 | * @since 2.0.0 |
||
383 | * |
||
384 | * @param null|bool $delete Whether to allow metadata deletion of the given type. |
||
385 | * @param array $args Array of data about current field including: |
||
386 | * 'type' : Current object type |
||
387 | * 'id' : Current object ID |
||
388 | * 'field_id' : Current Field ID |
||
389 | * 'repeat' : Whether current field is repeatable |
||
390 | * 'single' : Whether to save as a |
||
391 | * single meta value |
||
392 | * @param array $field_args All field arguments |
||
393 | * @param CMB2_Field object $field This field object |
||
394 | */ |
||
395 | $override = apply_filters( 'cmb2_override_meta_remove', null, $a, $this->args(), $this ); |
||
396 | |||
397 | /** |
||
398 | * Filter whether to override removing of meta value. |
||
399 | * |
||
400 | * The dynamic portion of the hook, $a['field_id'], refers to the current |
||
401 | * field id paramater. Returning a non-null value |
||
402 | * will effectively short-circuit the function. |
||
403 | * |
||
404 | * @since 2.0.0 |
||
405 | * |
||
406 | * @param null|bool $delete Whether to allow metadata deletion of the given type. |
||
407 | * @param array $args Array of data about current field including: |
||
408 | * 'type' : Current object type |
||
409 | * 'id' : Current object ID |
||
410 | * 'field_id' : Current Field ID |
||
411 | * 'repeat' : Whether current field is repeatable |
||
412 | * 'single' : Whether to save as a |
||
413 | * single meta value |
||
414 | * @param array $field_args All field arguments |
||
415 | * @param CMB2_Field object $field This field object |
||
416 | */ |
||
417 | $override = apply_filters( "cmb2_override_{$a['field_id']}_meta_remove", $override, $a, $this->args(), $this ); |
||
418 | |||
419 | // If no override, remove as usual |
||
420 | if ( null !== $override ) { |
||
421 | return $override; |
||
422 | } // Option page handling |
||
423 | elseif ( 'options-page' === $a['type'] || empty( $a['id'] ) ) { |
||
424 | return cmb2_options( $a['id'] )->remove( $a['field_id'] ); |
||
425 | } |
||
426 | |||
427 | // Remove metadata |
||
428 | return delete_metadata( $a['type'], $a['id'], $a['field_id'], $old ); |
||
429 | 39 | } |
|
430 | 39 | ||
431 | 39 | /** |
|
432 | 38 | * Data variables for get/set data methods |
|
433 | * |
||
434 | * @since 1.1.0 |
||
435 | * @param array $args Override arguments |
||
436 | 7 | * @return array Updated arguments |
|
437 | */ |
||
438 | public function data_args( $args = array() ) { |
||
448 | |||
449 | /** |
||
450 | * Checks if field has a registered sanitization callback |
||
451 | * |
||
452 | * @since 1.0.1 |
||
453 | 37 | * @param mixed $meta_value Meta value |
|
454 | * @return mixed Possibly sanitized meta value |
||
455 | 37 | */ |
|
456 | 37 | public function sanitization_cb( $meta_value ) { |
|
457 | 37 | ||
458 | 37 | if ( $this->args( 'repeatable' ) && is_array( $meta_value ) ) { |
|
459 | 37 | // Remove empties |
|
460 | $meta_value = array_filter( $meta_value ); |
||
461 | } |
||
462 | |||
463 | // Check if the field has a registered validation callback |
||
464 | $cb = $this->maybe_callback( 'sanitization_cb' ); |
||
465 | if ( false === $cb ) { |
||
466 | // If requesting NO validation, return meta value |
||
467 | return $meta_value; |
||
468 | 1 | } elseif ( $cb ) { |
|
469 | // Ok, callback is good, let's run it. |
||
470 | 1 | return call_user_func( $cb, $meta_value, $this->args(), $this ); |
|
471 | 1 | } |
|
472 | 1 | ||
473 | 1 | $sanitizer = new CMB2_Sanitize( $this, $meta_value ); |
|
474 | 1 | ||
475 | /** |
||
476 | 1 | * Filter the value before it is saved. |
|
477 | 1 | * |
|
478 | 1 | * The dynamic portion of the hook name, $this->type(), refers to the field type. |
|
479 | 1 | * |
|
480 | 1 | * Passing a non-null value to the filter will short-circuit saving |
|
481 | 1 | * the field value, saving the passed value instead. |
|
482 | 1 | * |
|
483 | * @param bool|mixed $override_value Sanitization/Validation override value to return. |
||
484 | * Default false to skip it. |
||
485 | * @param mixed $value The value to be saved to this field. |
||
486 | * @param int $object_id The ID of the object where the value will be saved |
||
487 | * @param array $field_args The current field's arguments |
||
488 | * @param object $sanitizer This `CMB2_Sanitize` object |
||
489 | */ |
||
490 | $override_value = apply_filters( "cmb2_sanitize_{$this->type()}", null, $sanitizer->value, $this->object_id, $this->args(), $sanitizer ); |
||
491 | |||
492 | 37 | if ( null !== $override_value ) { |
|
493 | return $override_value; |
||
494 | 37 | } |
|
495 | 16 | ||
496 | // Sanitization via 'CMB2_Sanitize' |
||
497 | return $sanitizer->{$this->type()}(); |
||
498 | 37 | } |
|
499 | |||
500 | /** |
||
501 | 37 | * Process $_POST data to save this field's value |
|
502 | * |
||
503 | * @since 2.0.3 |
||
504 | * @param array $data_to_save $_POST data to check |
||
505 | * @return array|int|bool Result of save, false on failure |
||
506 | */ |
||
507 | 37 | public function save_field_from_data( array $data_to_save ) { |
|
508 | 37 | $this->data_to_save = $data_to_save; |
|
509 | |||
510 | $meta_value = isset( $this->data_to_save[ $this->id( true ) ] ) |
||
511 | ? $this->data_to_save[ $this->id( true ) ] |
||
512 | 37 | : null; |
|
513 | |||
514 | 4 | return $this->save_field( $meta_value ); |
|
515 | } |
||
516 | |||
517 | /** |
||
518 | 33 | * Sanitize/store a value to this field |
|
519 | 33 | * |
|
520 | * @since 2.0.0 |
||
521 | 33 | * @param array $meta_value Desired value to sanitize/store |
|
522 | * @return array|int|bool Result of save. false on failure |
||
523 | */ |
||
524 | public function save_field( $meta_value ) { |
||
525 | |||
526 | 33 | $updated = false; |
|
527 | $action = ''; |
||
528 | $new_value = $this->sanitization_cb( $meta_value ); |
||
529 | 33 | ||
530 | 33 | if ( ! $this->args( 'save_field' ) ) { |
|
531 | |||
532 | // Nothing to see here. |
||
533 | $action = 'disabled'; |
||
534 | |||
535 | } elseif ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) { |
||
536 | |||
537 | $this->remove_data(); |
||
538 | 1 | $count = 0; |
|
539 | 1 | ||
540 | if ( ! empty( $new_value ) ) { |
||
541 | foreach ( $new_value as $add_new ) { |
||
542 | if ( $this->update_data( $add_new, false ) ) { |
||
543 | $count++; |
||
544 | } |
||
545 | } |
||
546 | } |
||
547 | 1 | ||
548 | $updated = $count ? $count : false; |
||
549 | $action = 'repeatable'; |
||
550 | 1 | ||
551 | } elseif ( ! CMB2_Utils::isempty( $new_value ) && $new_value !== $this->get_data() ) { |
||
552 | $updated = $this->update_data( $new_value ); |
||
553 | $action = 'updated'; |
||
554 | 1 | } elseif ( CMB2_Utils::isempty( $new_value ) ) { |
|
555 | $updated = $this->remove_data(); |
||
556 | $action = 'removed'; |
||
557 | } |
||
558 | 1 | ||
559 | if ( $updated ) { |
||
560 | $this->value = $this->get_data(); |
||
561 | $this->escaped_value = null; |
||
562 | } |
||
563 | |||
564 | $field_id = $this->id( true ); |
||
565 | |||
566 | /** |
||
567 | * Hooks after save field action. |
||
568 | 5 | * |
|
569 | 5 | * @since 2.2.0 |
|
570 | * |
||
571 | * @param string $field_id the current field id paramater. |
||
572 | * @param bool $updated Whether the metadata update action occurred. |
||
573 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
||
574 | * @param CMB2_Field object $field This field object |
||
575 | */ |
||
576 | do_action( 'cmb2_save_field', $field_id, $updated, $action, $this ); |
||
577 | |||
578 | 5 | /** |
|
579 | 5 | * Hooks after save field action. |
|
580 | 5 | * |
|
581 | * The dynamic portion of the hook, $field_id, refers to the |
||
582 | 5 | * current field id paramater. |
|
583 | * |
||
584 | * @since 2.2.0 |
||
585 | * |
||
586 | 5 | * @param bool $updated Whether the metadata update action occurred. |
|
587 | 5 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
|
588 | 5 | * @param CMB2_Field object $field This field object |
|
589 | */ |
||
590 | do_action( "cmb2_save_field_{$field_id}", $updated, $action, $this ); |
||
591 | |||
592 | return $updated; |
||
593 | } |
||
594 | |||
595 | 5 | /** |
|
596 | * Determine if current type is exempt from escaping |
||
597 | * |
||
598 | 5 | * @since 1.1.0 |
|
599 | * @return bool True if exempt |
||
600 | */ |
||
601 | public function escaping_exception() { |
||
602 | // These types cannot be escaped |
||
603 | 5 | return in_array( $this->type(), array( |
|
604 | 'file_list', |
||
605 | 'multicheck', |
||
606 | 'text_datetime_timestamp_timezone', |
||
607 | 5 | ) ); |
|
608 | } |
||
609 | 5 | ||
610 | /** |
||
611 | 5 | * Determine if current type cannot be repeatable |
|
612 | * |
||
613 | * @since 1.1.0 |
||
614 | * @param string $type Field type to check |
||
615 | * @return bool True if type cannot be repeatable |
||
616 | */ |
||
617 | public function repeatable_exception( $type ) { |
||
618 | // These types cannot be repeatable. |
||
619 | $internal_fields = array( |
||
620 | 5 | // Use file_list instead |
|
621 | 5 | 'file' => 1, |
|
622 | 5 | 'radio' => 1, |
|
623 | 'title' => 1, |
||
624 | 5 | 'wysiwyg' => 1, |
|
625 | 'checkbox' => 1, |
||
626 | 'radio_inline' => 1, |
||
627 | 5 | 'taxonomy_radio' => 1, |
|
628 | 'taxonomy_select' => 1, |
||
629 | 5 | 'taxonomy_multicheck' => 1, |
|
630 | 5 | ); |
|
631 | |||
632 | 5 | /** |
|
633 | * Filter field types that are non-repeatable. |
||
634 | 5 | * |
|
635 | * Note that this does *not* allow overriding the default non-repeatable types. |
||
636 | 5 | * |
|
637 | 5 | * @since 2.1.1 |
|
638 | * |
||
639 | * @param array $fields Array of fields designated as non-repeatable. Note that the field names are *keys*, |
||
640 | * and not values. The value can be anything, because it is meaningless. Example: |
||
641 | * array( 'my_custom_field' => 1 ) |
||
642 | */ |
||
643 | $all_fields = array_merge( apply_filters( 'cmb2_non_repeatable_fields', array() ), $internal_fields ); |
||
644 | return isset( $all_fields[ $type ] ); |
||
645 | 5 | } |
|
646 | 5 | ||
647 | 5 | /** |
|
648 | 5 | * Escape the value before output. Defaults to 'esc_attr()' |
|
649 | 5 | * |
|
650 | * @since 1.0.1 |
||
651 | 5 | * @param callable $func Escaping function (if not esc_attr()) |
|
652 | * @param mixed $meta_value Meta value |
||
653 | 5 | * @return mixed Final value |
|
654 | 5 | */ |
|
655 | 5 | public function escaped_value( $func = 'esc_attr', $meta_value = '' ) { |
|
656 | |||
657 | 5 | if ( null !== $this->escaped_value ) { |
|
658 | 3 | return $this->escaped_value; |
|
659 | 3 | } |
|
660 | |||
661 | 5 | $meta_value = $meta_value ? $meta_value : $this->value(); |
|
662 | |||
663 | // Check if the field has a registered escaping callback |
||
664 | if ( $cb = $this->maybe_callback( 'escape_cb' ) ) { |
||
665 | // Ok, callback is good, let's run it. |
||
666 | return call_user_func( $cb, $meta_value, $this->args(), $this ); |
||
667 | } |
||
668 | |||
669 | // Or custom escaping filter can be used |
||
670 | $esc = apply_filters( "cmb2_types_esc_{$this->type()}", null, $meta_value, $this->args(), $this ); |
||
671 | 38 | if ( null !== $esc ) { |
|
672 | 38 | return $esc; |
|
673 | } |
||
674 | 2 | ||
675 | 2 | if ( false === $cb || $this->escaping_exception() ) { |
|
676 | // If requesting NO escaping, return meta value |
||
677 | return $this->val_or_default( $meta_value ); |
||
678 | } |
||
679 | 38 | ||
680 | 38 | // escaping function passed in? |
|
681 | $func = $func ? $func : 'esc_attr'; |
||
682 | $meta_value = $this->val_or_default( $meta_value ); |
||
683 | |||
684 | if ( is_array( $meta_value ) ) { |
||
685 | foreach ( $meta_value as $key => $value ) { |
||
686 | $meta_value[ $key ] = call_user_func( $func, $value ); |
||
687 | } |
||
688 | } else { |
||
689 | $meta_value = call_user_func( $func, $meta_value ); |
||
690 | } |
||
691 | |||
692 | $this->escaped_value = $meta_value; |
||
693 | return $this->escaped_value; |
||
694 | } |
||
695 | |||
696 | /** |
||
697 | * Return non-empty value or field default if value IS empty |
||
698 | * |
||
699 | 14 | * @since 2.0.0 |
|
700 | 14 | * @param mixed $meta_value Field value |
|
701 | 5 | * @return mixed Field value, or default value |
|
702 | 4 | */ |
|
703 | public function val_or_default( $meta_value ) { |
||
704 | return ! CMB2_Utils::isempty( $meta_value ) ? $meta_value : $this->get_default(); |
||
705 | 1 | } |
|
706 | |||
707 | /** |
||
708 | 14 | * Offset a time value based on timezone |
|
709 | * |
||
710 | 14 | * @since 1.0.0 |
|
711 | 1 | * @return string Offset time string |
|
712 | */ |
||
713 | 1 | public function field_timezone_offset() { |
|
714 | 1 | return CMB2_Utils::timezone_offset( $this->field_timezone() ); |
|
715 | 1 | } |
|
716 | 1 | ||
717 | /** |
||
718 | 14 | * Return timezone string |
|
719 | 4 | * |
|
720 | * @since 1.0.0 |
||
721 | * @return string Timezone string |
||
722 | 11 | */ |
|
723 | public function field_timezone() { |
||
724 | $value = ''; |
||
725 | |||
726 | // Is timezone arg set? |
||
727 | if ( $this->args( 'timezone' ) ) { |
||
728 | $value = $this->args( 'timezone' ); |
||
729 | } // Is there another meta key with a timezone stored as its value we should use? |
||
730 | 46 | elseif ( $this->args( 'timezone_meta_key' ) ) { |
|
731 | $value = $this->get_data( $this->args( 'timezone_meta_key' ) ); |
||
732 | } |
||
733 | 46 | ||
734 | 46 | return $value; |
|
735 | 46 | } |
|
736 | 46 | ||
737 | 46 | /** |
|
738 | 46 | * Format the timestamp field value based on the field date/time format arg |
|
739 | 46 | * |
|
740 | 46 | * @since 2.0.0 |
|
741 | 46 | * @param int $meta_value Timestamp |
|
742 | 46 | * @param string $format Either date_format or time_format |
|
743 | 46 | * @return string Formatted date |
|
744 | 46 | */ |
|
745 | 46 | public function format_timestamp( $meta_value, $format = 'date_format' ) { |
|
746 | 46 | return date( stripslashes( $this->args( $format ) ), $meta_value ); |
|
747 | 46 | } |
|
748 | 46 | ||
749 | 46 | /** |
|
750 | 46 | * Return a formatted timestamp for a field |
|
751 | 46 | * |
|
752 | 46 | * @since 2.0.0 |
|
753 | 46 | * @param string $format Either date_format or time_format |
|
754 | 46 | * @param string $meta_value Optional meta value to check |
|
755 | * @return string Formatted date |
||
756 | */ |
||
757 | public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) { |
||
758 | 46 | $meta_value = $meta_value ? $meta_value : $this->escaped_value(); |
|
759 | $meta_value = CMB2_Utils::make_valid_time_stamp( $meta_value ); |
||
760 | 46 | ||
761 | 46 | if ( empty( $meta_value ) ) { |
|
762 | return ''; |
||
763 | } |
||
764 | 46 | ||
765 | return is_array( $meta_value ) |
||
766 | ? array_map( array( $this, 'format_timestamp' ), $meta_value, $format ) |
||
767 | : $this->format_timestamp( $meta_value, $format ); |
||
768 | } |
||
769 | 46 | ||
770 | /** |
||
771 | * Get timestamp from text date |
||
772 | 46 | * |
|
773 | * @since 2.2.0 |
||
774 | 46 | * @param string $value Date value |
|
775 | 46 | * @return mixed Unix timestamp representing the date. |
|
776 | */ |
||
777 | 46 | public function get_timestamp_from_value( $value ) { |
|
780 | |||
781 | /** |
||
782 | * Get field render callback and Render the field row |
||
783 | 46 | * |
|
784 | 1 | * @since 1.0.0 |
|
785 | 1 | */ |
|
786 | 1 | public function render_field() { |
|
787 | $this->render_context = 'edit'; |
||
788 | 46 | ||
789 | $this->peform_param_callback( 'render_row_cb' ); |
||
790 | 46 | ||
791 | // For chaining |
||
792 | 2 | return $this; |
|
793 | 2 | } |
|
794 | |||
795 | 2 | /** |
|
796 | * Default field render callback |
||
797 | 46 | * |
|
798 | * @since 2.1.1 |
||
799 | */ |
||
800 | public function render_field_callback() { |
||
801 | |||
844 | |||
845 | /** |
||
846 | * The default label_cb callback (if not a title field) |
||
847 | * |
||
848 | * @since 2.1.1 |
||
849 | * @return string Label html markup |
||
850 | */ |
||
851 | public function label() { |
||
860 | |||
861 | /** |
||
862 | * Defines the classes for the current CMB2 field row |
||
863 | * |
||
864 | * @since 2.0.0 |
||
865 | * @return string Space concatenated list of classes |
||
866 | */ |
||
867 | public function row_classes() { |
||
918 | |||
919 | |||
920 | |||
921 | /** |
||
922 | * Get field display callback and render the display value in the column. |
||
923 | * |
||
924 | * @since 2.2.2 |
||
925 | */ |
||
926 | public function render_column() { |
||
934 | |||
935 | /** |
||
936 | * Default callback to outputs field value in a display format. |
||
937 | * |
||
938 | * @since 2.2.2 |
||
939 | */ |
||
940 | public function display_value_callback() { |
||
983 | |||
984 | /** |
||
985 | * Replaces a hash key - {#} - with the repeatable index |
||
986 | * |
||
987 | * @since 1.2.0 |
||
988 | * @param string $value Value to update |
||
989 | * @return string Updated value |
||
990 | */ |
||
991 | public function replace_hash( $value ) { |
||
995 | |||
996 | /** |
||
997 | * Retrieve text parameter from field's text array (if it has one), or use fallback text |
||
998 | * For back-compatibility, falls back to checking the options array. |
||
999 | * |
||
1000 | * @since 2.2.2 |
||
1001 | * @param string $text_key Key in field's text array |
||
1002 | * @param string $fallback Fallback text |
||
1003 | * @return string Text |
||
1004 | */ |
||
1005 | public function get_string( $text_key, $fallback ) { |
||
1029 | |||
1030 | /** |
||
1031 | * Retrieve options args. Calls options_cb if it exists. |
||
1032 | * |
||
1033 | * @since 2.0.0 |
||
1034 | * @param string $key Specific option to retrieve |
||
1035 | * @return array Array of options |
||
1036 | */ |
||
1037 | public function options( $key = '' ) { |
||
1062 | |||
1063 | /** |
||
1064 | * Store JS dependencies as part of the field args. |
||
1065 | * |
||
1066 | * @since 2.2.0 |
||
1067 | * @param array $dependencies Dependies to register for this field. |
||
1068 | */ |
||
1069 | public function add_js_dependencies( $dependencies = array() ) { |
||
1076 | |||
1077 | /** |
||
1078 | * Get CMB2_Field default value, either from default param or default_cb param. |
||
1079 | * |
||
1080 | * @since 0.2.2 |
||
1081 | * |
||
1082 | * @return mixed Default field value |
||
1083 | */ |
||
1084 | public function get_default() { |
||
1097 | |||
1098 | /** |
||
1099 | * Fills in empty field parameters with defaults |
||
1100 | * |
||
1101 | * @since 1.1.0 |
||
1102 | * @param array $args Metabox field config array |
||
1103 | * @param array Modified field config array. |
||
1104 | */ |
||
1105 | public function _set_field_defaults( $args ) { |
||
1200 | |||
1201 | /** |
||
1202 | * Get default field arguments specific to this CMB2 object. |
||
1203 | * |
||
1204 | * @since 2.2.0 |
||
1205 | * @param array $field_args Metabox field config array. |
||
1206 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1207 | * @return array Array of field arguments. |
||
1208 | */ |
||
1209 | protected function get_default_args( $field_args, $field_group = null ) { |
||
1220 | |||
1221 | /** |
||
1222 | * Returns a cloned version of this field object with, but with |
||
1223 | * modified/overridden field arguments. |
||
1224 | * |
||
1225 | * @since 2.2.2 |
||
1226 | * @param array $field_args Array of field arguments, or entire array of |
||
1227 | * arguments for CMB2_Field |
||
1228 | * |
||
1229 | * @return CMB2_Field The new CMB2_Field instance. |
||
1230 | */ |
||
1231 | public function get_field_clone( $field_args ) { |
||
1234 | |||
1235 | /** |
||
1236 | * Returns the CMB2 instance this field is registered to. |
||
1237 | * |
||
1238 | * @since 2.2.2 |
||
1239 | * |
||
1240 | * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error. |
||
1241 | */ |
||
1242 | public function get_cmb() { |
||
1249 | |||
1250 | /** |
||
1251 | * Converts deprecated field parameters to the current/proper parameter, and throws a deprecation notice. |
||
1252 | * |
||
1253 | * @since 2.2.3 |
||
1254 | * @param array $args Metabox field config array. |
||
1255 | * @param array Modified field config array. |
||
1256 | */ |
||
1257 | protected function convert_deprecated_params( $args ) { |
||
1298 | |||
1299 | } |
||
1300 |
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.