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 |
||
15 | class CMB2 { |
||
|
|||
16 | |||
17 | /** |
||
18 | * Current CMB2 instance ID |
||
19 | * @var string |
||
20 | * @since 2.0.0 |
||
21 | */ |
||
22 | protected $cmb_id = ''; |
||
23 | |||
24 | /** |
||
25 | * Metabox Config array |
||
26 | * @var array |
||
27 | * @since 0.9.0 |
||
28 | */ |
||
29 | protected $meta_box = array(); |
||
30 | |||
31 | /** |
||
32 | * Object ID for metabox meta retrieving/saving |
||
33 | * @var mixed |
||
34 | * @since 1.0.0 |
||
35 | */ |
||
36 | protected $object_id = 0; |
||
37 | |||
38 | /** |
||
39 | * Type of object being saved. (e.g., post, user, or comment) |
||
40 | * @var string |
||
41 | * @since 1.0.0 |
||
42 | */ |
||
43 | protected $object_type = 'post'; |
||
44 | |||
45 | /** |
||
46 | * Type of object registered for metabox. (e.g., post, user, or comment) |
||
47 | * @var string |
||
48 | * @since 1.0.0 |
||
49 | */ |
||
50 | protected $mb_object_type = null; |
||
51 | |||
52 | /** |
||
53 | * List of fields that are changed/updated on save |
||
54 | * @var array |
||
55 | * @since 1.1.0 |
||
56 | */ |
||
57 | protected $updated = array(); |
||
58 | |||
59 | /** |
||
60 | * Metabox Defaults |
||
61 | * @var array |
||
62 | * @since 1.0.1 |
||
63 | */ |
||
64 | protected $mb_defaults = array( |
||
65 | 'id' => '', |
||
66 | 'title' => '', |
||
67 | 'type' => '', |
||
68 | 'object_types' => array(), // Post type |
||
69 | 'context' => 'normal', |
||
70 | 'priority' => 'high', |
||
71 | 'show_names' => true, // Show field names on the left |
||
72 | 'show_on_cb' => null, // Callback to determine if metabox should display. |
||
73 | 'show_on' => array(), // Post IDs or page templates to display this metabox. overrides 'show_on_cb' |
||
74 | 'cmb_styles' => true, // Include CMB2 stylesheet |
||
75 | 'enqueue_js' => true, // Include CMB2 JS |
||
76 | 'fields' => array(), |
||
77 | 'hookup' => true, |
||
78 | 'save_fields' => true, // Will not save during hookup if false |
||
79 | 'closed' => false, // Default to metabox being closed? |
||
80 | 'taxonomies' => array(), |
||
81 | 'new_user_section' => 'add-new-user', // or 'add-existing-user' |
||
82 | 'new_term_section' => true, |
||
83 | 'show_in_rest' => false, |
||
84 | ); |
||
85 | |||
86 | /** |
||
87 | * Metabox field objects |
||
88 | * @var array |
||
89 | * @since 2.0.3 |
||
90 | */ |
||
91 | protected $fields = array(); |
||
92 | |||
93 | /** |
||
94 | * An array of hidden fields to output at the end of the form |
||
95 | * @var array |
||
96 | * @since 2.0.0 |
||
97 | */ |
||
98 | protected $hidden_fields = array(); |
||
99 | |||
100 | /** |
||
101 | * Array of key => value data for saving. Likely $_POST data. |
||
102 | * @var array |
||
103 | * @since 2.0.0 |
||
104 | */ |
||
105 | public $data_to_save = array(); |
||
106 | |||
107 | /** |
||
108 | * Array of key => value data for saving. Likely $_POST data. |
||
109 | * @var string |
||
110 | * @since 2.0.0 |
||
111 | */ |
||
112 | protected $generated_nonce = ''; |
||
113 | |||
114 | /** |
||
115 | * Get started |
||
116 | * @since 0.4.0 |
||
117 | * @param array $meta_box Metabox config array |
||
118 | * @param integer $object_id Optional object id |
||
119 | */ |
||
120 | 44 | public function __construct( $meta_box, $object_id = 0 ) { |
|
121 | |||
122 | 44 | if ( empty( $meta_box['id'] ) ) { |
|
123 | 1 | wp_die( __( 'Metabox configuration is required to have an ID parameter', 'cmb2' ) ); |
|
124 | } |
||
125 | |||
126 | 44 | $this->meta_box = wp_parse_args( $meta_box, $this->mb_defaults ); |
|
127 | 44 | $this->meta_box['fields'] = array(); |
|
128 | |||
129 | 44 | $this->object_id( $object_id ); |
|
130 | 44 | $this->mb_object_type(); |
|
131 | 44 | $this->cmb_id = $meta_box['id']; |
|
132 | |||
133 | 44 | if ( ! empty( $meta_box['fields'] ) && is_array( $meta_box['fields'] ) ) { |
|
134 | 41 | $this->add_fields( $meta_box['fields'] ); |
|
135 | 41 | } |
|
136 | |||
137 | 44 | CMB2_Boxes::add( $this ); |
|
138 | |||
139 | /** |
||
140 | * Hook during initiation of CMB2 object |
||
141 | * |
||
142 | * The dynamic portion of the hook name, $this->cmb_id, is this meta_box id. |
||
143 | * |
||
144 | * @param array $cmb This CMB2 object |
||
145 | */ |
||
146 | 44 | do_action( "cmb2_init_{$this->cmb_id}", $this ); |
|
147 | 44 | } |
|
148 | |||
149 | /** |
||
150 | * Loops through and displays fields |
||
151 | * @since 1.0.0 |
||
152 | * @param int $object_id Object ID |
||
153 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
154 | */ |
||
155 | 1 | public function show_form( $object_id = 0, $object_type = '' ) { |
|
156 | 1 | $this->render_form_open( $object_id, $object_type ); |
|
157 | |||
158 | 1 | foreach ( $this->prop( 'fields' ) as $field_args ) { |
|
159 | 1 | $this->render_field( $field_args ); |
|
160 | 1 | } |
|
161 | |||
162 | 1 | $this->render_form_close( $object_id, $object_type ); |
|
163 | 1 | } |
|
164 | |||
165 | /** |
||
166 | * Outputs the opening form markup and runs corresponding hooks: |
||
167 | * 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}" |
||
168 | * @since 2.2.0 |
||
169 | * @param integer $object_id Object ID |
||
170 | * @param string $object_type Object type |
||
171 | * @return void |
||
172 | */ |
||
173 | 1 | View Code Duplication | public function render_form_open( $object_id = 0, $object_type = '' ) { |
174 | 1 | $object_type = $this->object_type( $object_type ); |
|
175 | 1 | $object_id = $this->object_id( $object_id ); |
|
176 | |||
177 | 1 | echo "\n<!-- Begin CMB2 Fields -->\n"; |
|
178 | |||
179 | 1 | $this->nonce_field(); |
|
180 | |||
181 | /** |
||
182 | * Hook before form table begins |
||
183 | * |
||
184 | * @param array $cmb_id The current box ID |
||
185 | * @param int $object_id The ID of the current object |
||
186 | * @param string $object_type The type of object you are working with. |
||
187 | * Usually `post` (this applies to all post-types). |
||
188 | * Could also be `comment`, `user` or `options-page`. |
||
189 | * @param array $cmb This CMB2 object |
||
190 | */ |
||
191 | 1 | do_action( 'cmb2_before_form', $this->cmb_id, $object_id, $object_type, $this ); |
|
192 | |||
193 | /** |
||
194 | * Hook before form table begins |
||
195 | * |
||
196 | * The first dynamic portion of the hook name, $object_type, is the type of object |
||
197 | * you are working with. Usually `post` (this applies to all post-types). |
||
198 | * Could also be `comment`, `user` or `options-page`. |
||
199 | * |
||
200 | * The second dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
||
201 | * |
||
202 | * @param array $cmb_id The current box ID |
||
203 | * @param int $object_id The ID of the current object |
||
204 | * @param array $cmb This CMB2 object |
||
205 | */ |
||
206 | 1 | do_action( "cmb2_before_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); |
|
207 | |||
208 | 1 | echo '<div class="cmb2-wrap form-table"><div id="cmb2-metabox-', sanitize_html_class( $this->cmb_id ), '" class="cmb2-metabox cmb-field-list">'; |
|
209 | |||
210 | 1 | } |
|
211 | |||
212 | /** |
||
213 | * Outputs the closing form markup and runs corresponding hooks: |
||
214 | * 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}" |
||
215 | * @since 2.2.0 |
||
216 | * @param integer $object_id Object ID |
||
217 | * @param string $object_type Object type |
||
218 | * @return void |
||
219 | */ |
||
220 | 1 | View Code Duplication | public function render_form_close( $object_id = 0, $object_type = '' ) { |
221 | 1 | $object_type = $this->object_type( $object_type ); |
|
222 | 1 | $object_id = $this->object_id( $object_id ); |
|
223 | |||
224 | 1 | echo '</div></div>'; |
|
225 | |||
226 | 1 | $this->render_hidden_fields(); |
|
227 | |||
228 | /** |
||
229 | * Hook after form form has been rendered |
||
230 | * |
||
231 | * @param array $cmb_id The current box ID |
||
232 | * @param int $object_id The ID of the current object |
||
233 | * @param string $object_type The type of object you are working with. |
||
234 | * Usually `post` (this applies to all post-types). |
||
235 | * Could also be `comment`, `user` or `options-page`. |
||
236 | * @param array $cmb This CMB2 object |
||
237 | */ |
||
238 | 1 | do_action( 'cmb2_after_form', $this->cmb_id, $object_id, $object_type, $this ); |
|
239 | |||
240 | /** |
||
241 | * Hook after form form has been rendered |
||
242 | * |
||
243 | * The dynamic portion of the hook name, $this->cmb_id, is the meta_box id. |
||
244 | * |
||
245 | * The first dynamic portion of the hook name, $object_type, is the type of object |
||
246 | * you are working with. Usually `post` (this applies to all post-types). |
||
247 | * Could also be `comment`, `user` or `options-page`. |
||
248 | * |
||
249 | * @param int $object_id The ID of the current object |
||
250 | * @param array $cmb This CMB2 object |
||
251 | */ |
||
252 | 1 | do_action( "cmb2_after_{$object_type}_form_{$this->cmb_id}", $object_id, $this ); |
|
253 | |||
254 | 1 | echo "\n<!-- End CMB2 Fields -->\n"; |
|
255 | |||
256 | 1 | } |
|
257 | |||
258 | /** |
||
259 | * Renders a field based on the field type |
||
260 | * @since 2.2.0 |
||
261 | * @param array $field_args A field configuration array. |
||
262 | * @return mixed CMB2_Field object if successful. |
||
263 | */ |
||
264 | 1 | public function render_field( $field_args ) { |
|
265 | 1 | $field_args['context'] = $this->prop( 'context' ); |
|
266 | |||
267 | 1 | if ( 'group' == $field_args['type'] ) { |
|
268 | |||
269 | if ( ! isset( $field_args['show_names'] ) ) { |
||
270 | $field_args['show_names'] = $this->prop( 'show_names' ); |
||
271 | } |
||
272 | $field = $this->render_group( $field_args ); |
||
273 | |||
274 | 1 | } elseif ( 'hidden' == $field_args['type'] && $this->get_field( $field_args )->should_show() ) { |
|
275 | // Save rendering for after the metabox |
||
276 | $field = $this->add_hidden_field( $field_args ); |
||
277 | |||
278 | } else { |
||
279 | |||
280 | 1 | $field_args['show_names'] = $this->prop( 'show_names' ); |
|
281 | |||
282 | // Render default fields |
||
283 | 1 | $field = $this->get_field( $field_args )->render_field(); |
|
284 | } |
||
285 | |||
286 | 1 | return $field; |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * Render a repeatable group. |
||
291 | * @param array $args Array of field arguments for a group field parent. |
||
292 | * @return CMB2_Field|null Group field object. |
||
293 | */ |
||
294 | 2 | public function render_group( $args ) { |
|
295 | |||
296 | 2 | if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
|
297 | return; |
||
298 | } |
||
299 | |||
300 | 2 | $field_group = $this->get_field( $args ); |
|
301 | |||
302 | // If field is requesting to be conditionally shown |
||
303 | 2 | if ( ! $field_group || ! $field_group->should_show() ) { |
|
304 | return; |
||
305 | } |
||
306 | |||
307 | 2 | $desc = $field_group->args( 'description' ); |
|
308 | 2 | $label = $field_group->args( 'name' ); |
|
309 | 2 | $sortable = $field_group->options( 'sortable' ) ? ' sortable' : ' non-sortable'; |
|
310 | 2 | $repeat_class = $field_group->args( 'repeatable' ) ? ' repeatable' : ' non-repeatable'; |
|
311 | 2 | $group_val = (array) $field_group->value(); |
|
312 | 2 | $nrows = count( $group_val ); |
|
313 | 2 | $remove_disabled = $nrows <= 1 ? 'disabled="disabled" ' : ''; |
|
314 | 2 | $field_group->index = 0; |
|
315 | |||
316 | 2 | $field_group->peform_param_callback( 'before_group' ); |
|
317 | |||
318 | 2 | echo '<div class="cmb-row cmb-repeat-group-wrap ', $field_group->row_classes(), '" data-fieldtype="group"><div class="cmb-td"><div id="', $field_group->id(), '_repeat" class="cmb-nested cmb-field-list cmb-repeatable-group', $sortable, $repeat_class, '" style="width:100%;">'; |
|
319 | |||
320 | 2 | if ( $desc || $label ) { |
|
321 | 2 | $class = $desc ? ' cmb-group-description' : ''; |
|
322 | 2 | echo '<div class="cmb-row', $class, '"><div class="cmb-th">'; |
|
323 | 2 | if ( $label ) { |
|
324 | 2 | echo '<h2 class="cmb-group-name">', $label, '</h2>'; |
|
325 | 2 | } |
|
326 | 2 | if ( $desc ) { |
|
327 | 1 | echo '<p class="cmb2-metabox-description">', $desc, '</p>'; |
|
328 | 1 | } |
|
329 | 2 | echo '</div></div>'; |
|
330 | 2 | } |
|
331 | |||
332 | 2 | if ( ! empty( $group_val ) ) { |
|
333 | |||
334 | foreach ( $group_val as $group_key => $field_id ) { |
||
335 | $this->render_group_row( $field_group, $remove_disabled ); |
||
336 | $field_group->index++; |
||
337 | } |
||
338 | } else { |
||
339 | 2 | $this->render_group_row( $field_group, $remove_disabled ); |
|
340 | } |
||
341 | |||
342 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
343 | 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>'; |
|
344 | 1 | } |
|
345 | |||
346 | 2 | echo '</div></div></div>'; |
|
347 | |||
348 | 2 | $field_group->peform_param_callback( 'after_group' ); |
|
349 | |||
350 | 2 | return $field_group; |
|
351 | } |
||
352 | |||
353 | /** |
||
354 | * Render a repeatable group row |
||
355 | * @since 1.0.2 |
||
356 | * @param CMB2_Field $field_group CMB2_Field group field object |
||
357 | * @param string $remove_disabled Attribute string to disable the remove button |
||
358 | */ |
||
359 | 2 | public function render_group_row( $field_group, $remove_disabled ) { |
|
360 | |||
361 | 2 | $field_group->peform_param_callback( 'before_group_row' ); |
|
362 | 2 | $closed_class = $field_group->options( 'closed' ) ? ' closed' : ''; |
|
363 | |||
364 | echo ' |
||
365 | 2 | <div class="postbox cmb-row cmb-repeatable-grouping', $closed_class, '" data-iterator="', $field_group->index, '">'; |
|
366 | |||
367 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
368 | 1 | echo '<button type="button" ', $remove_disabled, 'data-selector="', $field_group->id(), '_repeat" class="dashicons-before dashicons-no-alt cmb-remove-group-row"></button>'; |
|
369 | 1 | } |
|
370 | |||
371 | echo ' |
||
372 | 2 | <div class="cmbhandle" title="' , __( 'Click to toggle', 'cmb2' ), '"><br></div> |
|
373 | 2 | <h3 class="cmb-group-title cmbhandle-title"><span>', $field_group->replace_hash( $field_group->options( 'group_title' ) ), '</span></h3> |
|
374 | |||
375 | <div class="inside cmb-td cmb-nested cmb-field-list">'; |
||
376 | // Loop and render repeatable group fields |
||
377 | 2 | foreach ( array_values( $field_group->args( 'fields' ) ) as $field_args ) { |
|
378 | 2 | if ( 'hidden' == $field_args['type'] ) { |
|
379 | |||
380 | // Save rendering for after the metabox |
||
381 | $this->add_hidden_field( $field_args, $field_group ); |
||
382 | |||
383 | } else { |
||
384 | |||
385 | 2 | $field_args['show_names'] = $field_group->args( 'show_names' ); |
|
386 | 2 | $field_args['context'] = $field_group->args( 'context' ); |
|
387 | |||
388 | 2 | $field = $this->get_field( $field_args, $field_group )->render_field(); |
|
389 | } |
||
390 | 2 | } |
|
391 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
392 | echo ' |
||
393 | <div class="cmb-row cmb-remove-field-row"> |
||
394 | <div class="cmb-remove-row"> |
||
395 | 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> |
|
396 | </div> |
||
397 | </div> |
||
398 | '; |
||
399 | 1 | } |
|
400 | echo ' |
||
401 | </div> |
||
402 | </div> |
||
403 | 2 | '; |
|
404 | |||
405 | 2 | $field_group->peform_param_callback( 'after_group_row' ); |
|
406 | 2 | } |
|
407 | |||
408 | /** |
||
409 | * Add a hidden field to the list of hidden fields to be rendered later |
||
410 | * @since 2.0.0 |
||
411 | * @param array $field_args Array of field arguments to be passed to CMB2_Field |
||
412 | */ |
||
413 | public function add_hidden_field( $field_args, $field_group = null ) { |
||
414 | if ( isset( $field_args['field_args'] ) ) { |
||
415 | // For back-compatibility. |
||
416 | $field = new CMB2_Field( $field_args ); |
||
417 | } else { |
||
418 | $field = $this->get_new_field( $field_args, $field_group ); |
||
419 | } |
||
420 | |||
421 | $this->hidden_fields[] = new CMB2_Types( $field ); |
||
422 | |||
423 | return $field; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | * Loop through and output hidden fields |
||
428 | * @since 2.0.0 |
||
429 | */ |
||
430 | 1 | public function render_hidden_fields() { |
|
437 | |||
438 | /** |
||
439 | * Returns array of sanitized field values (without saving them) |
||
440 | * @since 2.0.3 |
||
441 | * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data). |
||
442 | */ |
||
443 | 2 | public function get_sanitized_values( array $data_to_sanitize ) { |
|
467 | |||
468 | /** |
||
469 | * Loops through and saves field data |
||
470 | * @since 1.0.0 |
||
471 | * @param int $object_id Object ID |
||
472 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
473 | * @param array $data_to_save Array of key => value data for saving. Likely $_POST data. |
||
474 | */ |
||
475 | 1 | public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) { |
|
491 | |||
492 | /** |
||
493 | * Process and save form fields |
||
494 | * @since 2.0.0 |
||
495 | */ |
||
496 | 3 | public function process_fields() { |
|
510 | |||
511 | /** |
||
512 | * Process and save a field |
||
513 | * @since 2.0.0 |
||
514 | * @param array $field_args Array of field arguments |
||
515 | */ |
||
516 | 3 | public function process_field( $field_args ) { |
|
543 | |||
544 | 3 | public function pre_process() { |
|
559 | |||
560 | 1 | public function after_save() { |
|
595 | |||
596 | /** |
||
597 | * Save a repeatable group |
||
598 | * @since 1.x.x |
||
599 | * @param array $args Field arguments array |
||
600 | * @return mixed Return of CMB2_Field::update_data() |
||
601 | */ |
||
602 | 1 | public function save_group( $args ) { |
|
609 | |||
610 | /** |
||
611 | * Save a repeatable group |
||
612 | * @since 1.x.x |
||
613 | * @param array $field_group CMB2_Field group field object |
||
614 | * @return mixed Return of CMB2_Field::update_data() |
||
615 | */ |
||
616 | 1 | public function save_group_field( $field_group ) { |
|
686 | |||
687 | /** |
||
688 | * Get object id from global space if no id is provided |
||
689 | * @since 1.0.0 |
||
690 | * @param integer $object_id Object ID |
||
691 | * @return integer $object_id Object ID |
||
692 | */ |
||
693 | 48 | public function object_id( $object_id = 0 ) { |
|
732 | |||
733 | /** |
||
734 | * Sets the $object_type based on metabox settings |
||
735 | * @since 1.0.0 |
||
736 | * @return string Object type |
||
737 | */ |
||
738 | 44 | public function mb_object_type() { |
|
792 | |||
793 | /** |
||
794 | * Determines if metabox is for an options page |
||
795 | * @since 1.0.1 |
||
796 | * @return boolean True/False |
||
797 | */ |
||
798 | 44 | public function is_options_page_mb() { |
|
801 | |||
802 | /** |
||
803 | * Returns the object type |
||
804 | * @since 1.0.0 |
||
805 | * @return string Object type |
||
806 | */ |
||
807 | 48 | public function object_type( $object_type = '' ) { |
|
821 | |||
822 | /** |
||
823 | * Get the object type for the current page, based on the $pagenow global. |
||
824 | * @since 2.2.2 |
||
825 | * @return string Page object type name. |
||
826 | */ |
||
827 | public function current_object_type() { |
||
845 | |||
846 | /** |
||
847 | * Set metabox property. |
||
848 | * @since 2.2.0 |
||
849 | * @param string $property Metabox config property to retrieve |
||
850 | * @param mixed $value Value to set if no value found |
||
851 | * @return mixed Metabox config property value or false |
||
852 | */ |
||
853 | 1 | public function set_prop( $property, $value ) { |
|
858 | |||
859 | /** |
||
860 | * Get metabox property and optionally set a fallback |
||
861 | * @since 2.0.0 |
||
862 | * @param string $property Metabox config property to retrieve |
||
863 | * @param mixed $fallback Fallback value to set if no value found |
||
864 | * @return mixed Metabox config property value or false |
||
865 | */ |
||
866 | 44 | public function prop( $property, $fallback = null ) { |
|
873 | |||
874 | /** |
||
875 | * Get a field object |
||
876 | * @since 2.0.3 |
||
877 | * @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object |
||
878 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
879 | * @return CMB2_Field|false CMB2_Field object (or false) |
||
880 | */ |
||
881 | 15 | public function get_field( $field, $field_group = null ) { |
|
906 | |||
907 | /** |
||
908 | * Handles determining which type of arguments to pass to CMB2_Field |
||
909 | * @since 2.0.7 |
||
910 | * @param mixed $field_id Field (or group field) ID |
||
911 | * @param mixed $field_args Array of field arguments |
||
912 | * @param mixed $sub_field_id Sub field ID (if field_group exists) |
||
913 | * @param mixed $field_group If a sub-field, will be the parent group CMB2_Field object |
||
914 | * @return array Array of CMB2_Field arguments |
||
915 | */ |
||
916 | 13 | public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) { |
|
933 | |||
934 | /** |
||
935 | * Get default field arguments specific to this CMB2 object. |
||
936 | * @since 2.2.0 |
||
937 | * @param array $field_args Metabox field config array. |
||
938 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
939 | * @return array Array of field arguments. |
||
940 | */ |
||
941 | 17 | protected function get_default_args( $field_args, $field_group = null ) { |
|
958 | |||
959 | /** |
||
960 | * Get a new field object specific to this CMB2 object. |
||
961 | * @since 2.2.0 |
||
962 | * @param array $field_args Metabox field config array. |
||
963 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
964 | * @return CMB2_Field CMB2_Field object |
||
965 | */ |
||
966 | 5 | protected function get_new_field( $field_args, $field_group = null ) { |
|
969 | |||
970 | /** |
||
971 | * When fields are added in the old-school way, intitate them as they should be |
||
972 | * @since 2.1.0 |
||
973 | * @param array $fields Array of fields to add |
||
974 | * @param mixed $parent_field_id Parent field id or null |
||
975 | */ |
||
976 | 41 | protected function add_fields( $fields, $parent_field_id = null ) { |
|
994 | |||
995 | /** |
||
996 | * Add a field to the metabox |
||
997 | * @since 2.0.0 |
||
998 | * @param array $field Metabox field config array |
||
999 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
1000 | * @return mixed Field id or false |
||
1001 | */ |
||
1002 | 43 | public function add_field( array $field, $position = 0 ) { |
|
1020 | |||
1021 | /** |
||
1022 | * Add a field to a group |
||
1023 | * @since 2.0.0 |
||
1024 | * @param string $parent_field_id The field id of the group field to add the field |
||
1025 | * @param array $field Metabox field config array |
||
1026 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
1027 | * @return mixed Array of parent/field ids or false |
||
1028 | */ |
||
1029 | 3 | public function add_group_field( $parent_field_id, array $field, $position = 0 ) { |
|
1052 | |||
1053 | /** |
||
1054 | * Add a field array to a fields array in desired position |
||
1055 | * @since 2.0.2 |
||
1056 | * @param array $field Metabox field config array |
||
1057 | * @param array &$fields Array (passed by reference) to append the field (array) to |
||
1058 | * @param integer $position Optionally specify a position in the array to be inserted |
||
1059 | */ |
||
1060 | 43 | protected function _add_field_to_array( $field, &$fields, $position = 0 ) { |
|
1067 | |||
1068 | /** |
||
1069 | * Remove a field from the metabox |
||
1070 | * @since 2.0.0 |
||
1071 | * @param string $field_id The field id of the field to remove |
||
1072 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
1073 | * @return bool True if field was removed |
||
1074 | */ |
||
1075 | 2 | public function remove_field( $field_id, $parent_field_id = '' ) { |
|
1099 | |||
1100 | /** |
||
1101 | * Update or add a property to a field |
||
1102 | * @since 2.0.0 |
||
1103 | * @param string $field_id Field id |
||
1104 | * @param string $property Field property to set/update |
||
1105 | * @param mixed $value Value to set the field property |
||
1106 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
1107 | * @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0) |
||
1108 | */ |
||
1109 | 4 | public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) { |
|
1126 | |||
1127 | /** |
||
1128 | * Check if field ids match a field and return the index/field id |
||
1129 | * @since 2.0.2 |
||
1130 | * @param string $field_id Field id |
||
1131 | * @param string $parent_field_id (optional) Parent field id |
||
1132 | * @return mixed Array of field/parent ids, or false |
||
1133 | */ |
||
1134 | 19 | public function get_field_ids( $field_id, $parent_field_id = '' ) { |
|
1161 | |||
1162 | /** |
||
1163 | * When using the old array filter, it is unlikely field array indexes will be the field id |
||
1164 | * @since 2.0.2 |
||
1165 | * @param string $field_id The field id |
||
1166 | * @param array $fields Array of fields to search |
||
1167 | * @return mixed Field index or false |
||
1168 | */ |
||
1169 | 2 | public function search_old_school_array( $field_id, $fields ) { |
|
1174 | |||
1175 | /** |
||
1176 | * Determine whether this cmb object should show, based on the 'show_on_cb' callback. |
||
1177 | * |
||
1178 | * @since 2.0.9 |
||
1179 | * |
||
1180 | * @return bool Whether this cmb should be shown. |
||
1181 | */ |
||
1182 | View Code Duplication | public function should_show() { |
|
1193 | |||
1194 | /** |
||
1195 | * Generate a unique nonce field for each registered meta_box |
||
1196 | * @since 2.0.0 |
||
1197 | * @return string unique nonce hidden input |
||
1198 | */ |
||
1199 | 1 | public function nonce_field() { |
|
1202 | |||
1203 | /** |
||
1204 | * Generate a unique nonce for each registered meta_box |
||
1205 | * @since 2.0.0 |
||
1206 | * @return string unique nonce string |
||
1207 | */ |
||
1208 | 1 | public function nonce() { |
|
1215 | |||
1216 | /** |
||
1217 | * Magic getter for our object. |
||
1218 | * @param string $field |
||
1219 | * @throws Exception Throws an exception if the field is invalid. |
||
1220 | * @return mixed |
||
1221 | */ |
||
1222 | 44 | public function __get( $field ) { |
|
1234 | |||
1235 | } |
||
1236 |
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.