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_hookup 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_hookup, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class CMB2_hookup extends CMB2_Hookup_Base { |
||
|
|||
15 | |||
16 | /** |
||
17 | * Only allow JS registration once |
||
18 | * @var bool |
||
19 | * @since 2.0.7 |
||
20 | */ |
||
21 | protected static $js_registration_done = false; |
||
22 | |||
23 | /** |
||
24 | * Only allow CSS registration once |
||
25 | * @var bool |
||
26 | * @since 2.0.7 |
||
27 | */ |
||
28 | protected static $css_registration_done = false; |
||
29 | |||
30 | /** |
||
31 | * CMB taxonomies array for term meta |
||
32 | * @var array |
||
33 | * @since 2.2.0 |
||
34 | */ |
||
35 | protected $taxonomies = array(); |
||
36 | |||
37 | /** |
||
38 | * Custom field columns. |
||
39 | * @var array |
||
40 | * @since 2.2.2 |
||
41 | */ |
||
42 | protected $columns = array(); |
||
43 | |||
44 | /** |
||
45 | * Constructor |
||
46 | * @since 2.0.0 |
||
47 | * @param CMB2 $cmb The CMB2 object to hookup |
||
48 | */ |
||
49 | public function __construct( CMB2 $cmb ) { |
||
50 | $this->cmb = $cmb; |
||
51 | $this->object_type = $this->cmb->mb_object_type(); |
||
52 | |||
53 | $this->universal_hooks(); |
||
54 | |||
55 | if ( is_admin() ) { |
||
56 | |||
57 | switch ( $this->object_type ) { |
||
58 | case 'post': |
||
59 | return $this->post_hooks(); |
||
60 | case 'comment': |
||
61 | return $this->comment_hooks(); |
||
62 | case 'user': |
||
63 | return $this->user_hooks(); |
||
64 | case 'term': |
||
65 | return $this->term_hooks(); |
||
66 | } |
||
67 | |||
68 | } |
||
69 | } |
||
70 | |||
71 | public function universal_hooks() { |
||
72 | foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) { |
||
73 | add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 ); |
||
74 | } |
||
75 | |||
76 | if ( is_admin() ) { |
||
77 | // register our scripts and styles for cmb |
||
78 | $this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 ); |
||
79 | $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); |
||
80 | |||
81 | $this->maybe_enqueue_column_display_styles(); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | public function post_hooks() { |
||
98 | |||
99 | public function comment_hooks() { |
||
108 | |||
109 | public function user_hooks() { |
||
125 | |||
126 | public function term_hooks() { |
||
165 | |||
166 | /** |
||
167 | * Registers styles for CMB2 |
||
168 | * @since 2.0.7 |
||
169 | */ |
||
170 | 1 | protected static function register_styles() { |
|
187 | |||
188 | /** |
||
189 | * Registers scripts for CMB2 |
||
190 | * @since 2.0.7 |
||
191 | */ |
||
192 | 1 | protected static function register_js() { |
|
202 | |||
203 | /** |
||
204 | * Registers scripts and styles for CMB2 |
||
205 | * @since 1.0.0 |
||
206 | */ |
||
207 | public static function register_scripts() { |
||
211 | |||
212 | /** |
||
213 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
214 | * @since 1.0.0 |
||
215 | */ |
||
216 | public function do_scripts( $hook ) { |
||
240 | |||
241 | /** |
||
242 | * Register the CMB2 field column headers. |
||
243 | * @since 2.2.2 |
||
244 | */ |
||
245 | public function register_column_headers( $columns ) { |
||
272 | |||
273 | /** |
||
274 | * The CMB2 field column display output. |
||
275 | * @since 2.2.2 |
||
276 | */ |
||
277 | public function column_display( $column_name, $object_id ) { |
||
289 | |||
290 | /** |
||
291 | * Returns the column display. |
||
292 | * @since 2.2.2 |
||
293 | */ |
||
294 | public function return_column_display( $empty, $custom_column, $object_id ) { |
||
301 | |||
302 | /** |
||
303 | * Add metaboxes (to 'post' or 'comment' object types) |
||
304 | * @since 1.0.0 |
||
305 | */ |
||
306 | public function add_metaboxes() { |
||
307 | |||
308 | if ( ! $this->show_on() ) { |
||
309 | return; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * To keep from registering an actual post-screen metabox, |
||
314 | * omit the 'title' attribute from the metabox registration array. |
||
315 | * |
||
316 | * (WordPress will not display metaboxes without titles anyway) |
||
317 | * |
||
318 | * This is a good solution if you want to output your metaboxes |
||
319 | * Somewhere else in the post-screen |
||
320 | */ |
||
321 | if ( ! $this->cmb->prop( 'title' ) ) { |
||
322 | return; |
||
323 | } |
||
324 | |||
325 | foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) { |
||
326 | if ( $this->cmb->prop( 'closed' ) ) { |
||
327 | add_filter( "postbox_classes_{$post_type}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) ); |
||
328 | } |
||
329 | |||
330 | if ( count( $this->cmb->tax_metaboxes_to_remove ) ) { |
||
331 | $this->remove_default_tax_metaboxes( $post_type ); |
||
332 | } |
||
333 | |||
334 | add_meta_box( $this->cmb->cmb_id, $this->cmb->prop( 'title' ), array( $this, 'metabox_callback' ), $post_type, $this->cmb->prop( 'context' ), $this->cmb->prop( 'priority' ) ); |
||
335 | } |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Remove the specified default taxonomy metaboxes for a post-type. |
||
340 | * @since 2.2.3 |
||
341 | * @param string $post_type Post type to remove the metabox for. |
||
342 | */ |
||
343 | protected function remove_default_tax_metaboxes( $post_type ) { |
||
344 | foreach ( $this->cmb->tax_metaboxes_to_remove as $taxonomy ) { |
||
345 | if ( ! taxonomy_exists( $taxonomy ) ) { |
||
346 | continue; |
||
347 | } |
||
348 | |||
349 | $mb_id = is_taxonomy_hierarchical( $taxonomy ) ? "{$taxonomy}div" : "tagsdiv-{$taxonomy}"; |
||
350 | remove_meta_box( $mb_id, $post_type, 'side' ); |
||
351 | } |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Add 'closed' class to metabox |
||
356 | * @since 2.0.0 |
||
357 | * @param array $classes Array of classes |
||
358 | * @return array Modified array of classes |
||
359 | */ |
||
360 | public function close_metabox_class( $classes ) { |
||
361 | $classes[] = 'closed'; |
||
362 | return $classes; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Display metaboxes for a post or comment object |
||
367 | * @since 1.0.0 |
||
368 | */ |
||
369 | public function metabox_callback() { |
||
373 | |||
374 | /** |
||
375 | * Display metaboxes for new user page |
||
376 | * @since 1.0.0 |
||
377 | */ |
||
378 | public function user_new_metabox( $section ) { |
||
379 | if ( $section == $this->cmb->prop( 'new_user_section' ) ) { |
||
380 | $object_id = $this->cmb->object_id(); |
||
381 | $this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id ); |
||
382 | $this->user_metabox(); |
||
383 | } |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Display metaboxes for a user object |
||
388 | * @since 1.0.0 |
||
389 | */ |
||
390 | public function user_metabox() { |
||
391 | $this->show_form_for_type( 'user' ); |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * Display metaboxes for a taxonomy term object |
||
396 | * @since 2.2.0 |
||
397 | */ |
||
398 | public function term_metabox() { |
||
399 | $this->show_form_for_type( 'term' ); |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Display metaboxes for an object type |
||
404 | * @since 2.2.0 |
||
405 | * @param string $type Object type |
||
406 | * @return void |
||
407 | */ |
||
408 | public function show_form_for_type( $type ) { |
||
409 | if ( $type != $this->cmb->mb_object_type() ) { |
||
410 | return; |
||
411 | } |
||
412 | |||
413 | if ( ! $this->show_on() ) { |
||
414 | return; |
||
415 | } |
||
416 | |||
417 | if ( $this->cmb->prop( 'cmb_styles' ) ) { |
||
418 | self::enqueue_cmb_css(); |
||
419 | } |
||
420 | if ( $this->cmb->prop( 'enqueue_js' ) ) { |
||
421 | self::enqueue_cmb_js(); |
||
422 | } |
||
423 | |||
424 | $this->cmb->show_form( 0, $type ); |
||
425 | } |
||
426 | |||
427 | /** |
||
428 | * Determines if metabox should be shown in current context |
||
429 | * @since 2.0.0 |
||
430 | * @return bool Whether metabox should be added/shown |
||
431 | */ |
||
432 | public function show_on() { |
||
433 | // If metabox is requesting to be conditionally shown |
||
434 | $show = $this->cmb->should_show(); |
||
435 | |||
436 | /** |
||
437 | * Filter to determine if metabox should show. Default is true |
||
438 | * |
||
439 | * @param array $show Default is true, show the metabox |
||
440 | * @param mixed $meta_box_args Array of the metabox arguments |
||
441 | * @param mixed $cmb The CMB2 instance |
||
442 | */ |
||
443 | $show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb ); |
||
444 | |||
445 | return $show; |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Get the CMB priority property set to numeric hook priority. |
||
450 | * @since 2.2.0 |
||
451 | * @param integer $default Default display hook priority. |
||
452 | * @return integer Hook priority. |
||
453 | */ |
||
454 | public function get_priority( $default = 10 ) { |
||
455 | $priority = $this->cmb->prop( 'priority' ); |
||
456 | |||
457 | if ( ! is_numeric( $priority ) ) { |
||
458 | switch ( $priority ) { |
||
459 | |||
460 | case 'high': |
||
461 | $priority = 5; |
||
462 | break; |
||
463 | |||
464 | case 'low': |
||
465 | $priority = 20; |
||
466 | break; |
||
467 | |||
468 | default: |
||
469 | $priority = $default; |
||
470 | break; |
||
471 | } |
||
472 | } |
||
473 | |||
474 | return $priority; |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * Save data from post metabox |
||
479 | * @since 1.0.0 |
||
480 | * @param int $post_id Post ID |
||
481 | * @param mixed $post Post object |
||
482 | * @return null |
||
483 | */ |
||
484 | public function save_post( $post_id, $post = false ) { |
||
485 | |||
486 | $post_type = $post ? $post->post_type : get_post_type( $post_id ); |
||
487 | |||
488 | $do_not_pass_go = ( |
||
489 | ! $this->can_save( $post_type ) |
||
490 | // check user editing permissions |
||
491 | || ( 'page' == $post_type && ! current_user_can( 'edit_page', $post_id ) ) |
||
492 | || ! current_user_can( 'edit_post', $post_id ) |
||
493 | ); |
||
494 | |||
495 | if ( $do_not_pass_go ) { |
||
496 | // do not collect $200 |
||
497 | return; |
||
498 | } |
||
499 | |||
500 | // take a trip to reading railroad – if you pass go collect $200 |
||
501 | $this->cmb->save_fields( $post_id, 'post', $_POST ); |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * Save data from comment metabox |
||
506 | * @since 2.0.9 |
||
507 | * @param int $comment_id Comment ID |
||
508 | * @return null |
||
509 | */ |
||
510 | public function save_comment( $comment_id ) { |
||
511 | |||
512 | $can_edit = current_user_can( 'moderate_comments', $comment_id ); |
||
513 | |||
514 | if ( $this->can_save( get_comment_type( $comment_id ) ) && $can_edit ) { |
||
515 | $this->cmb->save_fields( $comment_id, 'comment', $_POST ); |
||
516 | } |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * Save data from user fields |
||
521 | * @since 1.0.x |
||
522 | * @param int $user_id User ID |
||
523 | * @return null |
||
524 | */ |
||
525 | public function save_user( $user_id ) { |
||
526 | // check permissions |
||
527 | if ( $this->can_save( 'user' ) ) { |
||
528 | $this->cmb->save_fields( $user_id, 'user', $_POST ); |
||
529 | } |
||
530 | } |
||
531 | |||
532 | /** |
||
533 | * Save data from term fields |
||
534 | * @since 2.2.0 |
||
535 | * @param int $term_id Term ID |
||
536 | * @param int $tt_id Term Taxonomy ID |
||
537 | * @param string $taxonomy Taxonomy |
||
538 | * @return null |
||
539 | */ |
||
540 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
541 | $taxonomy = $taxonomy ? $taxonomy : $tt_id; |
||
542 | |||
543 | // check permissions |
||
544 | if ( $this->taxonomy_can_save( $taxonomy ) && $this->can_save( 'term' ) ) { |
||
545 | $this->cmb->save_fields( $term_id, 'term', $_POST ); |
||
546 | } |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * Delete term meta when a term is deleted. |
||
551 | * @since 2.2.0 |
||
552 | * @param int $term_id Term ID |
||
553 | * @param int $tt_id Term Taxonomy ID |
||
554 | * @param string $taxonomy Taxonomy |
||
555 | * @return null |
||
556 | */ |
||
557 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
558 | if ( $this->taxonomy_can_save( $taxonomy ) ) { |
||
559 | |||
560 | foreach ( $this->cmb->prop( 'fields' ) as $field ) { |
||
561 | $data_to_delete[ $field['id'] ] = ''; |
||
562 | } |
||
563 | |||
564 | $this->cmb->save_fields( $term_id, 'term', $data_to_delete ); |
||
565 | } |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * Determines if the current object is able to be saved |
||
570 | * @since 2.0.9 |
||
571 | * @param string $type Current post_type or comment_type |
||
572 | * @return bool Whether object can be saved |
||
573 | */ |
||
574 | public function can_save( $type = '' ) { |
||
575 | return ( |
||
576 | $this->cmb->prop( 'save_fields' ) |
||
577 | // check nonce |
||
578 | && isset( $_POST[ $this->cmb->nonce() ] ) |
||
579 | && wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() ) |
||
580 | // check if autosave |
||
581 | && ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
||
582 | // get the metabox types & compare it to this type |
||
583 | && ( $type && in_array( $type, $this->cmb->prop( 'object_types' ) ) ) |
||
584 | ); |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
589 | * @since 2.2.0 |
||
590 | * @param string $taxonomy Taxonomy of term being modified. |
||
591 | * @return bool Whether taxonomy is editable. |
||
592 | */ |
||
593 | public function taxonomy_can_save( $taxonomy ) { |
||
594 | if ( empty( $this->taxonomies ) || ! in_array( $taxonomy, $this->taxonomies ) ) { |
||
595 | return false; |
||
596 | } |
||
597 | |||
598 | $taxonomy_object = get_taxonomy( $taxonomy ); |
||
599 | // Can the user edit this term? |
||
600 | if ( ! isset( $taxonomy_object->cap ) || ! current_user_can( $taxonomy_object->cap->edit_terms ) ) { |
||
601 | return false; |
||
602 | } |
||
603 | |||
604 | return true; |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc). |
||
609 | * @since 2.2.2.1 |
||
610 | */ |
||
611 | protected function maybe_enqueue_column_display_styles() { |
||
612 | global $pagenow; |
||
613 | if ( |
||
614 | $pagenow |
||
615 | && $this->cmb->has_columns |
||
616 | && $this->cmb->prop( 'cmb_styles' ) |
||
617 | && in_array( $pagenow, array( 'edit.php', 'users.php', 'edit-comments.php', 'edit-tags.php' ), 1 ) |
||
618 | ) { |
||
619 | self::enqueue_cmb_css( 'cmb2-display-styles' ); |
||
620 | } |
||
621 | } |
||
622 | |||
623 | /** |
||
624 | * Includes CMB2 styles |
||
625 | * @since 2.0.0 |
||
626 | */ |
||
627 | 1 | public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { |
|
640 | |||
641 | /** |
||
642 | * Includes CMB2 JS |
||
643 | * @since 2.0.0 |
||
644 | */ |
||
645 | 1 | public static function enqueue_cmb_js() { |
|
646 | 1 | if ( ! apply_filters( 'cmb2_enqueue_js', true ) ) { |
|
647 | return false; |
||
648 | } |
||
649 | |||
650 | 1 | self::register_js(); |
|
651 | 1 | return true; |
|
652 | } |
||
653 | |||
654 | } |
||
655 |
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.