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 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class CMB2 extends CMB2_Base { |
||
|
|||
18 | |||
19 | /** |
||
20 | * The object properties name. |
||
21 | * @var string |
||
22 | * @since 2.2.3 |
||
23 | */ |
||
24 | protected $properties_name = 'meta_box'; |
||
25 | |||
26 | /** |
||
27 | * Metabox Config array |
||
28 | * @var array |
||
29 | * @since 0.9.0 |
||
30 | */ |
||
31 | protected $meta_box = array(); |
||
32 | |||
33 | /** |
||
34 | * Type of object registered for metabox. (e.g., post, user, or comment) |
||
35 | * @var string |
||
36 | * @since 1.0.0 |
||
37 | */ |
||
38 | protected $mb_object_type = null; |
||
39 | |||
40 | /** |
||
41 | * List of fields that are changed/updated on save |
||
42 | * @var array |
||
43 | * @since 1.1.0 |
||
44 | */ |
||
45 | protected $updated = array(); |
||
46 | |||
47 | /** |
||
48 | * Metabox Defaults |
||
49 | * @var array |
||
50 | * @since 1.0.1 |
||
51 | */ |
||
52 | protected $mb_defaults = array( |
||
53 | 'id' => '', |
||
54 | 'title' => '', |
||
55 | 'object_types' => array(), // Post type |
||
56 | 'context' => 'normal', |
||
57 | 'priority' => 'high', |
||
58 | 'show_names' => true, // Show field names on the left |
||
59 | 'show_on_cb' => null, // Callback to determine if metabox should display. |
||
60 | 'show_on' => array(), // Post IDs or page templates to display this metabox. overrides 'show_on_cb' |
||
61 | 'cmb_styles' => true, // Include CMB2 stylesheet |
||
62 | 'enqueue_js' => true, // Include CMB2 JS |
||
63 | 'fields' => array(), |
||
64 | 'hookup' => true, |
||
65 | 'save_fields' => true, // Will not save during hookup if false |
||
66 | 'closed' => false, // Default to metabox being closed? |
||
67 | 'taxonomies' => array(), |
||
68 | 'new_user_section' => 'add-new-user', // or 'add-existing-user' |
||
69 | 'new_term_section' => true, |
||
70 | 'show_in_rest' => false, |
||
71 | ); |
||
72 | |||
73 | /** |
||
74 | * Metabox field objects |
||
75 | * @var array |
||
76 | * @since 2.0.3 |
||
77 | */ |
||
78 | protected $fields = array(); |
||
79 | |||
80 | /** |
||
81 | * An array of hidden fields to output at the end of the form |
||
82 | * @var array |
||
83 | * @since 2.0.0 |
||
84 | */ |
||
85 | protected $hidden_fields = array(); |
||
86 | |||
87 | /** |
||
88 | * Array of key => value data for saving. Likely $_POST data. |
||
89 | * @var string |
||
90 | * @since 2.0.0 |
||
91 | */ |
||
92 | protected $generated_nonce = ''; |
||
93 | |||
94 | /** |
||
95 | * Whether there are fields to be shown in columns. Set in CMB2::add_field(). |
||
96 | * @var bool |
||
97 | * @since 2.2.2 |
||
98 | */ |
||
99 | protected $has_columns = false; |
||
100 | |||
101 | /** |
||
102 | * If taxonomy field is requesting to remove_default, we store the taxonomy here. |
||
103 | * @var array |
||
104 | * @since 2.2.3 |
||
105 | */ |
||
106 | protected $tax_metaboxes_to_remove = array(); |
||
107 | |||
108 | /** |
||
109 | * Get started |
||
110 | * @since 0.4.0 |
||
111 | * @param array $config Metabox config array |
||
112 | * @param integer $object_id Optional object id |
||
113 | */ |
||
114 | 46 | public function __construct( $config, $object_id = 0 ) { |
|
115 | |||
116 | 46 | if ( empty( $config['id'] ) ) { |
|
117 | 1 | wp_die( esc_html__( 'Metabox configuration is required to have an ID parameter.', 'cmb2' ) ); |
|
118 | } |
||
119 | |||
120 | 46 | $this->meta_box = wp_parse_args( $config, $this->mb_defaults ); |
|
121 | 46 | $this->meta_box['fields'] = array(); |
|
122 | |||
123 | 46 | $this->object_id( $object_id ); |
|
124 | 46 | $this->mb_object_type(); |
|
125 | 46 | $this->cmb_id = $config['id']; |
|
126 | |||
127 | 46 | if ( ! empty( $config['fields'] ) && is_array( $config['fields'] ) ) { |
|
128 | 43 | $this->add_fields( $config['fields'] ); |
|
129 | 43 | } |
|
130 | |||
131 | 46 | CMB2_Boxes::add( $this ); |
|
132 | |||
133 | /** |
||
134 | * Hook during initiation of CMB2 object |
||
135 | * |
||
136 | * The dynamic portion of the hook name, $this->cmb_id, is this meta_box id. |
||
137 | * |
||
138 | * @param array $cmb This CMB2 object |
||
139 | */ |
||
140 | 46 | do_action( "cmb2_init_{$this->cmb_id}", $this ); |
|
141 | 46 | } |
|
142 | |||
143 | /** |
||
144 | * Loops through and displays fields |
||
145 | * @since 1.0.0 |
||
146 | * @param int $object_id Object ID |
||
147 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
148 | */ |
||
149 | 1 | public function show_form( $object_id = 0, $object_type = '' ) { |
|
150 | 1 | $this->render_form_open( $object_id, $object_type ); |
|
151 | |||
152 | 1 | foreach ( $this->prop( 'fields' ) as $field_args ) { |
|
153 | 1 | $this->render_field( $field_args ); |
|
154 | 1 | } |
|
155 | |||
156 | 1 | $this->render_form_close( $object_id, $object_type ); |
|
157 | 1 | } |
|
158 | |||
159 | /** |
||
160 | * Outputs the opening form markup and runs corresponding hooks: |
||
161 | * 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}" |
||
162 | * @since 2.2.0 |
||
163 | * @param integer $object_id Object ID |
||
164 | * @param string $object_type Object type |
||
165 | * @return void |
||
166 | */ |
||
167 | 1 | public function render_form_open( $object_id = 0, $object_type = '' ) { |
|
168 | 1 | $object_type = $this->object_type( $object_type ); |
|
169 | 1 | $object_id = $this->object_id( $object_id ); |
|
170 | |||
171 | 1 | echo "\n<!-- Begin CMB2 Fields -->\n"; |
|
172 | |||
173 | 1 | $this->nonce_field(); |
|
174 | |||
175 | /** |
||
176 | * Hook before form table begins |
||
177 | * |
||
178 | * @param array $cmb_id The current box ID |
||
179 | * @param int $object_id The ID of the current object |
||
180 | * @param string $object_type The type of object you are working with. |
||
181 | * Usually `post` (this applies to all post-types). |
||
182 | * Could also be `comment`, `user` or `options-page`. |
||
183 | * @param array $cmb This CMB2 object |
||
184 | */ |
||
185 | 1 | do_action( 'cmb2_before_form', $this->cmb_id, $object_id, $object_type, $this ); |
|
186 | |||
187 | /** |
||
188 | * Hook before form table begins |
||
189 | * |
||
190 | * The first dynamic portion of the hook name, $object_type, is the type of object |
||
191 | * you are working with. Usually `post` (this applies to all post-types). |
||
192 | * Could also be `comment`, `user` or `options-page`. |
||
193 | * |
||
194 | * The second dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
||
195 | * |
||
196 | * @param array $cmb_id The current box ID |
||
197 | * @param int $object_id The ID of the current object |
||
198 | * @param array $cmb This CMB2 object |
||
199 | */ |
||
200 | 1 | do_action( "cmb2_before_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); |
|
201 | |||
202 | 1 | echo '<div class="', $this->box_classes(), '"><div id="cmb2-metabox-', sanitize_html_class( $this->cmb_id ), '" class="cmb2-metabox cmb-field-list">'; |
|
203 | |||
204 | 1 | } |
|
205 | |||
206 | /** |
||
207 | * Defines the classes for the CMB2 form/wrap. |
||
208 | * |
||
209 | * @since 2.0.0 |
||
210 | * @return string Space concatenated list of classes |
||
211 | */ |
||
212 | 1 | public function box_classes() { |
|
213 | |||
214 | 1 | $classes = array( 'cmb2-wrap', 'form-table' ); |
|
215 | |||
216 | // Use the callback to fetch classes. |
||
217 | 1 | View Code Duplication | if ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) { |
218 | 1 | $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); |
|
219 | 1 | $classes = array_merge( $classes, $added_classes ); |
|
220 | 1 | } |
|
221 | |||
222 | 1 | View Code Duplication | if ( $added_classes = $this->prop( 'classes' ) ) { |
223 | 1 | $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); |
|
224 | 1 | $classes = array_merge( $classes, $added_classes ); |
|
225 | 1 | } |
|
226 | |||
227 | /** |
||
228 | * Globally filter box wrap classes |
||
229 | * |
||
230 | * @since 2.2.2 |
||
231 | * |
||
232 | * @param string $classes Array of classes for the cmb2-wrap. |
||
233 | * @param CMB2 $cmb This CMB2 object. |
||
234 | */ |
||
235 | 1 | $classes = apply_filters( 'cmb2_wrap_classes', $classes, $this ); |
|
236 | |||
237 | // Clean up. |
||
238 | 1 | $classes = array_map( 'strip_tags', array_filter( $classes ) ); |
|
239 | |||
240 | // Make a string. |
||
241 | 1 | return implode( ' ', $classes ); |
|
242 | } |
||
243 | |||
244 | /** |
||
245 | * Outputs the closing form markup and runs corresponding hooks: |
||
246 | * 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}" |
||
247 | * @since 2.2.0 |
||
248 | * @param integer $object_id Object ID |
||
249 | * @param string $object_type Object type |
||
250 | * @return void |
||
251 | */ |
||
252 | 1 | public function render_form_close( $object_id = 0, $object_type = '' ) { |
|
289 | |||
290 | /** |
||
291 | * Renders a field based on the field type |
||
292 | * @since 2.2.0 |
||
293 | * @param array $field_args A field configuration array. |
||
294 | * @return mixed CMB2_Field object if successful. |
||
295 | */ |
||
296 | 1 | public function render_field( $field_args ) { |
|
297 | 1 | $field_args['context'] = $this->prop( 'context' ); |
|
298 | |||
299 | 1 | if ( 'group' == $field_args['type'] ) { |
|
300 | |||
301 | if ( ! isset( $field_args['show_names'] ) ) { |
||
302 | $field_args['show_names'] = $this->prop( 'show_names' ); |
||
303 | } |
||
304 | $field = $this->render_group( $field_args ); |
||
305 | |||
306 | 1 | } elseif ( 'hidden' == $field_args['type'] && $this->get_field( $field_args )->should_show() ) { |
|
307 | // Save rendering for after the metabox |
||
308 | $field = $this->add_hidden_field( $field_args ); |
||
309 | |||
310 | } else { |
||
311 | |||
312 | 1 | $field_args['show_names'] = $this->prop( 'show_names' ); |
|
313 | |||
314 | // Render default fields |
||
315 | 1 | $field = $this->get_field( $field_args )->render_field(); |
|
316 | } |
||
317 | |||
318 | 1 | return $field; |
|
319 | } |
||
320 | |||
321 | /** |
||
322 | * Render a repeatable group. |
||
323 | * @param array $args Array of field arguments for a group field parent. |
||
324 | * @return CMB2_Field|null Group field object. |
||
325 | */ |
||
326 | 2 | public function render_group( $args ) { |
|
327 | |||
328 | 2 | if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
|
329 | return; |
||
330 | } |
||
331 | |||
332 | 2 | $field_group = $this->get_field( $args ); |
|
333 | |||
334 | // If field is requesting to be conditionally shown |
||
335 | 2 | if ( ! $field_group || ! $field_group->should_show() ) { |
|
336 | return; |
||
337 | } |
||
338 | |||
339 | 2 | $desc = $field_group->args( 'description' ); |
|
340 | 2 | $label = $field_group->args( 'name' ); |
|
341 | 2 | $group_val = (array) $field_group->value(); |
|
342 | 2 | $remove_disabled = count( $group_val ) <= 1 ? 'disabled="disabled" ' : ''; |
|
343 | 2 | $field_group->index = 0; |
|
344 | |||
345 | 2 | $field_group->peform_param_callback( 'before_group' ); |
|
346 | |||
347 | 2 | echo '<div class="cmb-row cmb-repeat-group-wrap ', $field_group->row_classes(), '" data-fieldtype="group"><div class="cmb-td"><div data-groupid="', $field_group->id(), '" id="', $field_group->id(), '_repeat" ', $this->group_wrap_attributes( $field_group ), '>'; |
|
348 | |||
349 | 2 | if ( $desc || $label ) { |
|
350 | 2 | $class = $desc ? ' cmb-group-description' : ''; |
|
351 | 2 | echo '<div class="cmb-row', $class, '"><div class="cmb-th">'; |
|
352 | 2 | if ( $label ) { |
|
353 | 2 | echo '<h2 class="cmb-group-name">', $label, '</h2>'; |
|
354 | 2 | } |
|
355 | 2 | if ( $desc ) { |
|
356 | 1 | echo '<p class="cmb2-metabox-description">', $desc, '</p>'; |
|
357 | 1 | } |
|
358 | 2 | echo '</div></div>'; |
|
359 | 2 | } |
|
360 | |||
361 | 2 | if ( ! empty( $group_val ) ) { |
|
362 | foreach ( $group_val as $group_key => $field_id ) { |
||
363 | $this->render_group_row( $field_group, $remove_disabled ); |
||
364 | $field_group->index++; |
||
365 | } |
||
366 | } else { |
||
367 | 2 | $this->render_group_row( $field_group, $remove_disabled ); |
|
368 | } |
||
369 | |||
370 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
371 | 1 | echo '<div class="cmb-row"><div class="cmb-td"><p class="cmb-add-row"><button type="button" data-selector="', $field_group->id(), '_repeat" data-grouptitle="', $field_group->options( 'group_title' ), '" class="cmb-add-group-row button">', $field_group->options( 'add_button' ), '</button></p></div></div>'; |
|
372 | 1 | } |
|
373 | |||
374 | 2 | echo '</div></div></div>'; |
|
375 | |||
376 | 2 | $field_group->peform_param_callback( 'after_group' ); |
|
377 | |||
378 | 2 | return $field_group; |
|
379 | } |
||
380 | |||
381 | /** |
||
382 | * Get the group wrap attributes, which are passed through a filter. |
||
383 | * @since 2.2.3 |
||
384 | * @param CMB2_Field $field_group The group CMB2_Field object. |
||
385 | * @return string The attributes string. |
||
386 | */ |
||
387 | 2 | public function group_wrap_attributes( $field_group ) { |
|
388 | 2 | $classes = 'cmb-nested cmb-field-list cmb-repeatable-group'; |
|
389 | 2 | $classes .= $field_group->options( 'sortable' ) ? ' sortable' : ' non-sortable'; |
|
390 | 2 | $classes .= $field_group->args( 'repeatable' ) ? ' repeatable' : ' non-repeatable'; |
|
391 | |||
392 | $group_wrap_attributes = array( |
||
393 | 2 | 'class' => $classes, |
|
394 | 2 | 'style' => 'width:100%;', |
|
395 | 2 | ); |
|
396 | |||
397 | /** |
||
398 | * Allow for adding additional HTML attributes to a group wrapper. |
||
399 | * |
||
400 | * The attributes will be an array of key => value pairs for each attribute. |
||
401 | * |
||
402 | * @since 2.2.2 |
||
403 | * |
||
404 | * @param string $group_wrap_attributes Current attributes array. |
||
405 | * |
||
406 | * @param CMB2_Field $field_group The group CMB2_Field object. |
||
407 | */ |
||
408 | 2 | $group_wrap_attributes = apply_filters( 'cmb2_group_wrap_attributes', $group_wrap_attributes, $field_group ); |
|
409 | |||
410 | 2 | return CMB2_Utils::concat_attrs( $group_wrap_attributes ); |
|
411 | } |
||
412 | |||
413 | /** |
||
414 | * Render a repeatable group row |
||
415 | * @since 1.0.2 |
||
416 | * @param CMB2_Field $field_group CMB2_Field group field object |
||
417 | * @param string $remove_disabled Attribute string to disable the remove button |
||
418 | */ |
||
419 | 2 | public function render_group_row( $field_group, $remove_disabled ) { |
|
420 | |||
421 | 2 | $field_group->peform_param_callback( 'before_group_row' ); |
|
422 | 2 | $closed_class = $field_group->options( 'closed' ) ? ' closed' : ''; |
|
423 | |||
424 | echo ' |
||
425 | 2 | <div class="postbox cmb-row cmb-repeatable-grouping', $closed_class, '" data-iterator="', $field_group->index, '">'; |
|
426 | |||
427 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
428 | 1 | echo '<button type="button" ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="dashicons-before dashicons-no-alt cmb-remove-group-row"></button>'; |
|
429 | 1 | } |
|
430 | |||
431 | echo ' |
||
432 | 2 | <div class="cmbhandle" title="' , esc_attr__( 'Click to toggle', 'cmb2' ), '"><br></div> |
|
433 | 2 | <h3 class="cmb-group-title cmbhandle-title"><span>', $field_group->replace_hash( $field_group->options( 'group_title' ) ), '</span></h3> |
|
434 | |||
435 | <div class="inside cmb-td cmb-nested cmb-field-list">'; |
||
436 | // Loop and render repeatable group fields |
||
437 | 2 | foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) { |
|
438 | 2 | if ( 'hidden' == $field_args['type'] ) { |
|
439 | |||
440 | // Save rendering for after the metabox |
||
441 | $this->add_hidden_field( $field_args, $field_group ); |
||
442 | |||
443 | } else { |
||
444 | |||
445 | 2 | $field_args['show_names'] = $field_group->args( 'show_names' ); |
|
446 | 2 | $field_args['context'] = $field_group->args( 'context' ); |
|
447 | |||
448 | 2 | $field = $this->get_field( $field_args, $field_group )->render_field(); |
|
449 | } |
||
450 | 2 | } |
|
451 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
452 | echo ' |
||
453 | <div class="cmb-row cmb-remove-field-row"> |
||
454 | <div class="cmb-remove-row"> |
||
455 | 1 | <button type="button" ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="button cmb-remove-group-row alignright">', $field_group->options( 'remove_button' ), '</button> |
|
456 | </div> |
||
457 | </div> |
||
458 | '; |
||
459 | 1 | } |
|
460 | echo ' |
||
461 | </div> |
||
462 | </div> |
||
463 | 2 | '; |
|
464 | |||
465 | 2 | $field_group->peform_param_callback( 'after_group_row' ); |
|
466 | 2 | } |
|
467 | |||
468 | /** |
||
469 | * Add a hidden field to the list of hidden fields to be rendered later |
||
470 | * @since 2.0.0 |
||
471 | * @param array $field_args Array of field arguments to be passed to CMB2_Field |
||
472 | */ |
||
473 | public function add_hidden_field( $field_args, $field_group = null ) { |
||
474 | if ( isset( $field_args['field_args'] ) ) { |
||
475 | // For back-compatibility. |
||
476 | $field = new CMB2_Field( $field_args ); |
||
477 | } else { |
||
478 | $field = $this->get_new_field( $field_args, $field_group ); |
||
479 | } |
||
480 | |||
481 | $type = new CMB2_Types( $field ); |
||
482 | |||
483 | if ( $field_group ) { |
||
484 | $type->iterator = $field_group->index; |
||
485 | } |
||
486 | |||
487 | $this->hidden_fields[] = $type; |
||
488 | |||
489 | return $field; |
||
490 | } |
||
491 | |||
492 | /** |
||
493 | * Loop through and output hidden fields |
||
494 | * @since 2.0.0 |
||
495 | */ |
||
496 | 1 | public function render_hidden_fields() { |
|
503 | |||
504 | /** |
||
505 | * Returns array of sanitized field values (without saving them) |
||
506 | * @since 2.0.3 |
||
507 | * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data). |
||
508 | */ |
||
509 | 2 | public function get_sanitized_values( array $data_to_sanitize ) { |
|
533 | |||
534 | /** |
||
535 | * Loops through and saves field data |
||
536 | * @since 1.0.0 |
||
537 | * @param int $object_id Object ID |
||
538 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
539 | * @param array $data_to_save Array of key => value data for saving. Likely $_POST data. |
||
540 | */ |
||
541 | 1 | public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) { |
|
542 | |||
543 | // Fall-back to $_POST data |
||
544 | 1 | $this->data_to_save = ! empty( $data_to_save ) ? $data_to_save : $_POST; |
|
545 | 1 | $object_id = $this->object_id( $object_id ); |
|
546 | 1 | $object_type = $this->object_type( $object_type ); |
|
547 | |||
548 | 1 | $this->process_fields(); |
|
549 | |||
550 | // If options page, save the updated options |
||
551 | 1 | if ( 'options-page' == $object_type ) { |
|
552 | 1 | cmb2_options( $object_id )->set(); |
|
553 | 1 | } |
|
554 | |||
555 | 1 | $this->after_save(); |
|
556 | 1 | } |
|
557 | |||
558 | /** |
||
559 | * Process and save form fields |
||
560 | * @since 2.0.0 |
||
561 | */ |
||
562 | 3 | public function process_fields() { |
|
563 | |||
564 | 3 | $this->pre_process(); |
|
565 | |||
566 | // Remove the show_on properties so saving works |
||
567 | 3 | $this->prop( 'show_on', array() ); |
|
568 | |||
569 | // save field ids of those that are updated |
||
570 | 3 | $this->updated = array(); |
|
571 | |||
572 | 3 | foreach ( $this->prop( 'fields' ) as $field_args ) { |
|
573 | 3 | $this->process_field( $field_args ); |
|
574 | 3 | } |
|
575 | 3 | } |
|
576 | |||
577 | /** |
||
578 | * Process and save a field |
||
579 | * @since 2.0.0 |
||
580 | * @param array $field_args Array of field arguments |
||
581 | */ |
||
582 | 3 | public function process_field( $field_args ) { |
|
583 | |||
584 | 3 | switch ( $field_args['type'] ) { |
|
585 | |||
586 | 3 | case 'group': |
|
587 | 1 | if ( $this->save_group( $field_args ) ) { |
|
588 | 1 | $this->updated[] = $field_args['id']; |
|
589 | 1 | } |
|
590 | |||
591 | 1 | break; |
|
592 | |||
593 | 2 | case 'title': |
|
594 | // Don't process title fields |
||
595 | break; |
||
596 | |||
597 | 2 | default: |
|
598 | |||
599 | 2 | $field = $this->get_new_field( $field_args ); |
|
600 | |||
601 | 2 | if ( $field->save_field_from_data( $this->data_to_save ) ) { |
|
602 | 2 | $this->updated[] = $field->id(); |
|
603 | 2 | } |
|
604 | |||
605 | 2 | break; |
|
606 | 3 | } |
|
607 | |||
608 | 3 | } |
|
609 | |||
610 | 3 | public function pre_process() { |
|
611 | /** |
||
612 | * Fires before fields have been processed/saved. |
||
613 | * |
||
614 | * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
||
615 | * |
||
616 | * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
||
617 | * Usually `post` (this applies to all post-types). |
||
618 | * Could also be `comment`, `user` or `options-page`. |
||
619 | * |
||
620 | * @param array $cmb This CMB2 object |
||
621 | * @param int $object_id The ID of the current object |
||
622 | */ |
||
623 | 3 | do_action( "cmb2_{$this->object_type()}_process_fields_{$this->cmb_id}", $this, $this->object_id() ); |
|
624 | 3 | } |
|
625 | |||
626 | 1 | public function after_save() { |
|
627 | 1 | $object_type = $this->object_type(); |
|
628 | 1 | $object_id = $this->object_id(); |
|
629 | |||
630 | /** |
||
631 | * Fires after all fields have been saved. |
||
632 | * |
||
633 | * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
||
634 | * Usually `post` (this applies to all post-types). |
||
635 | * Could also be `comment`, `user` or `options-page`. |
||
636 | * |
||
637 | * @param int $object_id The ID of the current object |
||
638 | * @param array $cmb_id The current box ID |
||
639 | * @param string $updated Array of field ids that were updated. |
||
640 | * Will only include field ids that had values change. |
||
641 | * @param array $cmb This CMB2 object |
||
642 | */ |
||
643 | 1 | do_action( "cmb2_save_{$object_type}_fields", $object_id, $this->cmb_id, $this->updated, $this ); |
|
644 | |||
645 | /** |
||
646 | * Fires after all fields have been saved. |
||
647 | * |
||
648 | * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
||
649 | * |
||
650 | * The dynamic portion of the hook name, $object_type, refers to the metabox/form's object type |
||
651 | * Usually `post` (this applies to all post-types). |
||
652 | * Could also be `comment`, `user` or `options-page`. |
||
653 | * |
||
654 | * @param int $object_id The ID of the current object |
||
655 | * @param string $updated Array of field ids that were updated. |
||
656 | * Will only include field ids that had values change. |
||
657 | * @param array $cmb This CMB2 object |
||
658 | */ |
||
659 | 1 | do_action( "cmb2_save_{$object_type}_fields_{$this->cmb_id}", $object_id, $this->updated, $this ); |
|
660 | 1 | } |
|
661 | |||
662 | /** |
||
663 | * Save a repeatable group |
||
664 | * @since 1.x.x |
||
665 | * @param array $args Field arguments array |
||
666 | * @return mixed Return of CMB2_Field::update_data() |
||
667 | */ |
||
668 | 1 | public function save_group( $args ) { |
|
669 | 1 | if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
|
670 | return; |
||
671 | } |
||
672 | |||
673 | 1 | return $this->save_group_field( $this->get_new_field( $args ) ); |
|
674 | } |
||
675 | |||
676 | /** |
||
677 | * Save a repeatable group |
||
678 | * @since 1.x.x |
||
679 | * @param array $field_group CMB2_Field group field object |
||
680 | * @return mixed Return of CMB2_Field::update_data() |
||
681 | */ |
||
682 | 1 | public function save_group_field( $field_group ) { |
|
683 | 1 | $base_id = $field_group->id(); |
|
684 | |||
685 | 1 | if ( ! isset( $this->data_to_save[ $base_id ] ) ) { |
|
686 | return; |
||
687 | } |
||
688 | |||
689 | 1 | $old = $field_group->get_data(); |
|
690 | // Check if group field has sanitization_cb |
||
691 | 1 | $group_vals = $field_group->sanitization_cb( $this->data_to_save[ $base_id ] ); |
|
692 | 1 | $saved = array(); |
|
693 | |||
694 | 1 | $field_group->index = 0; |
|
695 | 1 | $field_group->data_to_save = $this->data_to_save; |
|
696 | |||
697 | 1 | foreach ( array_values( $field_group->fields() ) as $field_args ) { |
|
698 | |||
699 | 1 | $field = $this->get_new_field( $field_args, $field_group ); |
|
700 | 1 | $sub_id = $field->id( true ); |
|
701 | |||
702 | 1 | foreach ( (array) $group_vals as $field_group->index => $post_vals ) { |
|
703 | |||
704 | // Get value |
||
705 | 1 | $new_val = isset( $group_vals[ $field_group->index ][ $sub_id ] ) |
|
706 | 1 | ? $group_vals[ $field_group->index ][ $sub_id ] |
|
707 | 1 | : false; |
|
708 | |||
709 | // Sanitize |
||
710 | 1 | $new_val = $field->sanitization_cb( $new_val ); |
|
711 | |||
712 | 1 | if ( is_array( $new_val ) && $field->args( 'has_supporting_data' ) ) { |
|
713 | 1 | if ( $field->args( 'repeatable' ) ) { |
|
714 | 1 | $_new_val = array(); |
|
715 | 1 | foreach ( $new_val as $group_index => $grouped_data ) { |
|
716 | // Add the supporting data to the $saved array stack |
||
717 | 1 | $saved[ $field_group->index ][ $grouped_data['supporting_field_id'] ][] = $grouped_data['supporting_field_value']; |
|
718 | // Reset var to the actual value |
||
719 | 1 | $_new_val[ $group_index ] = $grouped_data['value']; |
|
720 | 1 | } |
|
721 | 1 | $new_val = $_new_val; |
|
722 | 1 | } else { |
|
723 | // Add the supporting data to the $saved array stack |
||
724 | 1 | $saved[ $field_group->index ][ $new_val['supporting_field_id'] ] = $new_val['supporting_field_value']; |
|
725 | // Reset var to the actual value |
||
726 | 1 | $new_val = $new_val['value']; |
|
727 | } |
||
728 | 1 | } |
|
729 | |||
730 | // Get old value |
||
731 | 1 | $old_val = is_array( $old ) && isset( $old[ $field_group->index ][ $sub_id ] ) |
|
732 | 1 | ? $old[ $field_group->index ][ $sub_id ] |
|
733 | 1 | : false; |
|
734 | |||
735 | 1 | $is_updated = ( ! CMB2_Utils::isempty( $new_val ) && $new_val !== $old_val ); |
|
736 | 1 | $is_removed = ( CMB2_Utils::isempty( $new_val ) && ! CMB2_Utils::isempty( $old_val ) ); |
|
737 | |||
738 | // Compare values and add to `$updated` array |
||
739 | 1 | if ( $is_updated || $is_removed ) { |
|
740 | 1 | $this->updated[] = $base_id . '::' . $field_group->index . '::' . $sub_id; |
|
741 | 1 | } |
|
742 | |||
743 | // Add to `$saved` array |
||
744 | 1 | $saved[ $field_group->index ][ $sub_id ] = $new_val; |
|
745 | |||
746 | 1 | } |
|
747 | |||
748 | 1 | $saved[ $field_group->index ] = CMB2_Utils::filter_empty( $saved[ $field_group->index ] ); |
|
749 | 1 | } |
|
750 | |||
751 | 1 | $saved = CMB2_Utils::filter_empty( $saved ); |
|
752 | |||
753 | 1 | return $field_group->update_data( $saved, true ); |
|
754 | } |
||
755 | |||
756 | /** |
||
757 | * Get object id from global space if no id is provided |
||
758 | * @since 1.0.0 |
||
759 | * @param integer $object_id Object ID |
||
760 | * @return integer $object_id Object ID |
||
761 | */ |
||
762 | 50 | public function object_id( $object_id = 0 ) { |
|
801 | |||
802 | /** |
||
803 | * Sets the $object_type based on metabox settings |
||
804 | * @since 1.0.0 |
||
805 | * @return string Object type |
||
806 | */ |
||
807 | 46 | public function mb_object_type() { |
|
808 | 46 | if ( null !== $this->mb_object_type ) { |
|
809 | 12 | return $this->mb_object_type; |
|
810 | } |
||
811 | |||
812 | 46 | if ( $this->is_options_page_mb() ) { |
|
813 | 38 | $this->mb_object_type = 'options-page'; |
|
814 | 38 | return $this->mb_object_type; |
|
815 | } |
||
816 | |||
817 | 45 | $registered_types = $this->box_types(); |
|
818 | |||
819 | 45 | $type = false; |
|
820 | |||
821 | // if it's an array of one, extract it |
||
822 | 45 | if ( 1 === count( $registered_types ) ) { |
|
823 | 45 | $last = end( $registered_types ); |
|
824 | 45 | if ( is_string( $last ) ) { |
|
825 | 45 | $type = $last; |
|
826 | 45 | } |
|
827 | 45 | } elseif ( in_array( $this->current_object_type(), $registered_types, true ) ) { |
|
828 | $type = $page_type; |
||
829 | } |
||
830 | |||
831 | // Get our object type |
||
832 | switch ( $type ) { |
||
833 | |||
834 | 45 | case 'user': |
|
835 | 45 | case 'comment': |
|
836 | 45 | case 'term': |
|
837 | 1 | $this->mb_object_type = $type; |
|
838 | 1 | break; |
|
839 | |||
840 | 45 | default: |
|
841 | 45 | $this->mb_object_type = 'post'; |
|
842 | 45 | break; |
|
843 | 45 | } |
|
844 | |||
845 | 45 | return $this->mb_object_type; |
|
846 | } |
||
847 | |||
848 | /** |
||
849 | * Gets the box 'object_types' array based on box settings. |
||
850 | * @since 2.2.3 |
||
851 | * @return array Object types |
||
852 | */ |
||
853 | 45 | public function box_types() { |
|
856 | |||
857 | /** |
||
858 | * Determines if metabox is for an options page |
||
859 | * @since 1.0.1 |
||
860 | * @return boolean True/False |
||
861 | */ |
||
862 | 46 | public function is_options_page_mb() { |
|
865 | |||
866 | /** |
||
867 | * Returns the object type |
||
868 | * @since 1.0.0 |
||
869 | * @return string Object type |
||
870 | */ |
||
871 | 50 | public function object_type( $object_type = '' ) { |
|
885 | |||
886 | /** |
||
887 | * Get the object type for the current page, based on the $pagenow global. |
||
888 | * @since 2.2.2 |
||
889 | * @return string Page object type name. |
||
890 | */ |
||
891 | 1 | View Code Duplication | public function current_object_type() { |
892 | global $pagenow; |
||
893 | 1 | $type = 'post'; |
|
894 | |||
895 | if ( in_array( $pagenow, array( 'user-edit.php', 'profile.php', 'user-new.php' ), true ) ) { |
||
896 | $type = 'user'; |
||
897 | } |
||
898 | |||
899 | if ( in_array( $pagenow, array( 'edit-comments.php', 'comment.php' ), true ) ) { |
||
900 | $type = 'comment'; |
||
901 | } |
||
902 | |||
903 | if ( in_array( $pagenow, array( 'edit-tags.php', 'term.php' ), true ) ) { |
||
904 | $type = 'term'; |
||
905 | } |
||
906 | |||
907 | return $type; |
||
908 | } |
||
909 | |||
910 | /** |
||
911 | * Set metabox property. |
||
912 | * @since 2.2.2 |
||
913 | * @param string $property Metabox config property to retrieve |
||
914 | * @param mixed $value Value to set if no value found |
||
915 | * @return mixed Metabox config property value or false |
||
916 | */ |
||
917 | 1 | public function set_prop( $property, $value ) { |
|
922 | |||
923 | /** |
||
924 | * Get metabox property and optionally set a fallback |
||
925 | * @since 2.0.0 |
||
926 | * @param string $property Metabox config property to retrieve |
||
927 | * @param mixed $fallback Fallback value to set if no value found |
||
928 | * @return mixed Metabox config property value or false |
||
929 | */ |
||
930 | 46 | public function prop( $property, $fallback = null ) { |
|
937 | |||
938 | /** |
||
939 | * Get a field object |
||
940 | * @since 2.0.3 |
||
941 | * @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object |
||
942 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
943 | * @return CMB2_Field|false CMB2_Field object (or false) |
||
944 | */ |
||
945 | 15 | public function get_field( $field, $field_group = null ) { |
|
946 | 15 | if ( is_a( $field, 'CMB2_Field' ) ) { |
|
947 | return $field; |
||
948 | } |
||
949 | |||
950 | 15 | $field_id = is_string( $field ) ? $field : $field['id']; |
|
951 | |||
952 | 15 | $parent_field_id = ! empty( $field_group ) ? $field_group->id() : ''; |
|
953 | 15 | $ids = $this->get_field_ids( $field_id, $parent_field_id, true ); |
|
954 | |||
955 | 15 | if ( ! $ids ) { |
|
956 | return false; |
||
957 | } |
||
958 | |||
959 | 15 | list( $field_id, $sub_field_id ) = $ids; |
|
960 | |||
961 | 15 | $index = implode( '', $ids ) . ( $field_group ? $field_group->index : '' ); |
|
962 | 15 | if ( array_key_exists( $index, $this->fields ) ) { |
|
963 | 4 | return $this->fields[ $index ]; |
|
964 | } |
||
965 | |||
966 | 13 | $this->fields[ $index ] = new CMB2_Field( $this->get_field_args( $field_id, $field, $sub_field_id, $field_group ) ); |
|
967 | |||
968 | 13 | return $this->fields[ $index ]; |
|
969 | } |
||
970 | |||
971 | /** |
||
972 | * Handles determining which type of arguments to pass to CMB2_Field |
||
973 | * @since 2.0.7 |
||
974 | * @param mixed $field_id Field (or group field) ID |
||
975 | * @param mixed $field_args Array of field arguments |
||
976 | * @param mixed $sub_field_id Sub field ID (if field_group exists) |
||
977 | * @param mixed $field_group If a sub-field, will be the parent group CMB2_Field object |
||
978 | * @return array Array of CMB2_Field arguments |
||
979 | */ |
||
980 | 13 | public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) { |
|
997 | |||
998 | /** |
||
999 | * Get default field arguments specific to this CMB2 object. |
||
1000 | * @since 2.2.0 |
||
1001 | * @param array $field_args Metabox field config array. |
||
1002 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1003 | * @return array Array of field arguments. |
||
1004 | */ |
||
1005 | 17 | View Code Duplication | protected function get_default_args( $field_args, $field_group = null ) { |
1022 | |||
1023 | /** |
||
1024 | * Get a new field object specific to this CMB2 object. |
||
1025 | * @since 2.2.0 |
||
1026 | * @param array $field_args Metabox field config array. |
||
1027 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1028 | * @return CMB2_Field CMB2_Field object |
||
1029 | */ |
||
1030 | 5 | protected function get_new_field( $field_args, $field_group = null ) { |
|
1033 | |||
1034 | /** |
||
1035 | * When fields are added in the old-school way, intitate them as they should be |
||
1036 | * @since 2.1.0 |
||
1037 | * @param array $fields Array of fields to add |
||
1038 | * @param mixed $parent_field_id Parent field id or null |
||
1039 | */ |
||
1040 | 43 | protected function add_fields( $fields, $parent_field_id = null ) { |
|
1058 | |||
1059 | /** |
||
1060 | * Add a field to the metabox |
||
1061 | * @since 2.0.0 |
||
1062 | * @param array $field Metabox field config array |
||
1063 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
1064 | * @return mixed Field id or false |
||
1065 | */ |
||
1066 | 45 | public function add_field( array $field, $position = 0 ) { |
|
1092 | |||
1093 | /** |
||
1094 | * Defines a field's column if requesting to be show in admin columns. |
||
1095 | * @since 2.2.3 |
||
1096 | * @param array $field Metabox field config array. |
||
1097 | * @return array Modified metabox field config array. |
||
1098 | */ |
||
1099 | protected function define_field_column( array $field ) { |
||
1111 | |||
1112 | /** |
||
1113 | * Add a field to a group |
||
1114 | * @since 2.0.0 |
||
1115 | * @param string $parent_field_id The field id of the group field to add the field |
||
1116 | * @param array $field Metabox field config array |
||
1117 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
1118 | * @return mixed Array of parent/field ids or false |
||
1119 | */ |
||
1120 | 3 | public function add_group_field( $parent_field_id, array $field, $position = 0 ) { |
|
1143 | |||
1144 | /** |
||
1145 | * Add a field array to a fields array in desired position |
||
1146 | * @since 2.0.2 |
||
1147 | * @param array $field Metabox field config array |
||
1148 | * @param array &$fields Array (passed by reference) to append the field (array) to |
||
1149 | * @param integer $position Optionally specify a position in the array to be inserted |
||
1150 | */ |
||
1151 | 45 | protected function _add_field_to_array( $field, &$fields, $position = 0 ) { |
|
1158 | |||
1159 | /** |
||
1160 | * Remove a field from the metabox |
||
1161 | * @since 2.0.0 |
||
1162 | * @param string $field_id The field id of the field to remove |
||
1163 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
1164 | * @return bool True if field was removed |
||
1165 | */ |
||
1166 | 2 | public function remove_field( $field_id, $parent_field_id = '' ) { |
|
1190 | |||
1191 | /** |
||
1192 | * Update or add a property to a field |
||
1193 | * @since 2.0.0 |
||
1194 | * @param string $field_id Field id |
||
1195 | * @param string $property Field property to set/update |
||
1196 | * @param mixed $value Value to set the field property |
||
1197 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
1198 | * @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0) |
||
1199 | */ |
||
1200 | 4 | public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) { |
|
1217 | |||
1218 | /** |
||
1219 | * Check if field ids match a field and return the index/field id |
||
1220 | * @since 2.0.2 |
||
1221 | * @param string $field_id Field id |
||
1222 | * @param string $parent_field_id (optional) Parent field id |
||
1223 | * @return mixed Array of field/parent ids, or false |
||
1224 | */ |
||
1225 | 19 | public function get_field_ids( $field_id, $parent_field_id = '' ) { |
|
1252 | |||
1253 | /** |
||
1254 | * When using the old array filter, it is unlikely field array indexes will be the field id |
||
1255 | * @since 2.0.2 |
||
1256 | * @param string $field_id The field id |
||
1257 | * @param array $fields Array of fields to search |
||
1258 | * @return mixed Field index or false |
||
1259 | */ |
||
1260 | 2 | public function search_old_school_array( $field_id, $fields ) { |
|
1265 | |||
1266 | /** |
||
1267 | * Handles metabox property callbacks, and passes this $cmb object as property. |
||
1268 | * @since 2.2.3 |
||
1269 | * @param callable $cb The callback method/function/closure |
||
1270 | * @return mixed Return of the callback function. |
||
1271 | */ |
||
1272 | 1 | protected function do_callback( $cb ) { |
|
1275 | |||
1276 | /** |
||
1277 | * Generate a unique nonce field for each registered meta_box |
||
1278 | * @since 2.0.0 |
||
1279 | * @return string unique nonce hidden input |
||
1280 | */ |
||
1281 | 1 | public function nonce_field() { |
|
1284 | |||
1285 | /** |
||
1286 | * Generate a unique nonce for each registered meta_box |
||
1287 | * @since 2.0.0 |
||
1288 | * @return string unique nonce string |
||
1289 | */ |
||
1290 | 1 | public function nonce() { |
|
1297 | |||
1298 | /** |
||
1299 | * Magic getter for our object. |
||
1300 | * @param string $field |
||
1301 | * @throws Exception Throws an exception if the field is invalid. |
||
1302 | * @return mixed |
||
1303 | */ |
||
1304 | 46 | public function __get( $field ) { |
|
1314 | |||
1315 | } |
||
1316 |
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.