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:
| 1 | <?php |
||
| 20 | class Post_Types { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Hook in WordPress init to register custom content. |
||
| 24 | * |
||
| 25 | * @since 3.0.0 |
||
| 26 | */ |
||
| 27 | public function __construct() { |
||
| 28 | // Register custom taxonomies. |
||
| 29 | add_action( 'init', array( __CLASS__, 'register_taxonomies' ), 5 ); |
||
| 30 | // Register custom post types. |
||
| 31 | add_action( 'init', array( __CLASS__, 'register_post_types' ), 5 ); |
||
| 32 | // Filter the calendar feed post content to display a calendar view. |
||
| 33 | add_filter( 'the_content', array( $this, 'filter_post_content' ), 100 ); |
||
| 34 | // Delete calendar transients and notices upon post deletion. |
||
| 35 | add_action( 'before_delete_post', array( $this, 'upon_deletion' ), 10, 1 ); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Register custom taxonomies. |
||
| 40 | * |
||
| 41 | * @since 3.0.0 |
||
| 42 | */ |
||
| 43 | public static function register_taxonomies() { |
||
| 44 | |||
| 45 | do_action( 'simcal_register_taxonomies' ); |
||
| 46 | |||
| 47 | View Code Duplication | if ( ! taxonomy_exists( 'calendar_feed' ) ) { |
|
|
|
|||
| 48 | |||
| 49 | // Feed Type. |
||
| 50 | $labels = array( |
||
| 51 | 'name' => __( 'Events Source Types', 'google-calendar-events' ), |
||
| 52 | 'singular_name' => __( 'Events Source Type', 'google-calendar-events' ), |
||
| 53 | 'menu_name' => __( 'Events Source Type', 'google-calendar-events' ), |
||
| 54 | 'all_items' => __( 'All Events Source Types', 'google-calendar-events' ), |
||
| 55 | 'parent_item' => __( 'Parent Events Source Type', 'google-calendar-events' ), |
||
| 56 | 'parent_item_colon' => __( 'Parent Events Source Type:', 'google-calendar-events' ), |
||
| 57 | 'new_item_name' => __( 'New Events Source Type', 'google-calendar-events' ), |
||
| 58 | 'add_new_item' => __( 'Add New Events Source Type', 'google-calendar-events' ), |
||
| 59 | 'edit_item' => __( 'Edit Events Source Type', 'google-calendar-events' ), |
||
| 60 | 'update_item' => __( 'Update Events Source Type', 'google-calendar-events' ), |
||
| 61 | 'view_item' => __( 'View Events Source Type', 'google-calendar-events' ), |
||
| 62 | 'separate_items_with_commas' => __( 'Separate events source types with commas', 'google-calendar-events' ), |
||
| 63 | 'add_or_remove_items' => __( 'Add or remove events source types', 'google-calendar-events' ), |
||
| 64 | 'choose_from_most_used' => __( 'Choose from the most used', 'google-calendar-events' ), |
||
| 65 | 'popular_items' => __( 'Popular events source types', 'google-calendar-events' ), |
||
| 66 | 'search_items' => __( 'Search Events Source Types', 'google-calendar-events' ), |
||
| 67 | 'not_found' => __( 'Not Found', 'google-calendar-events' ), |
||
| 68 | ); |
||
| 69 | |||
| 70 | $args = array( |
||
| 71 | 'hierarchical' => true, |
||
| 72 | 'labels' => $labels, |
||
| 73 | 'public' => false, |
||
| 74 | 'show_admin_column' => false, |
||
| 75 | 'show_in_nav_menus' => false, |
||
| 76 | 'show_tagcloud' => false, |
||
| 77 | 'show_ui' => false, |
||
| 78 | ); |
||
| 79 | register_taxonomy( 'calendar_feed', array( 'calendar' ), $args ); |
||
| 80 | |||
| 81 | } |
||
| 82 | |||
| 83 | View Code Duplication | if ( ! taxonomy_exists( 'calendar_type' ) ) { |
|
| 84 | |||
| 85 | // Calendar Type. |
||
| 86 | $labels = array( |
||
| 87 | 'name' => __( 'Calendar Types', 'google-calendar-events' ), |
||
| 88 | 'singular_name' => __( 'Calendar Type', 'google-calendar-events' ), |
||
| 89 | 'menu_name' => __( 'Calendar Type', 'google-calendar-events' ), |
||
| 90 | 'all_items' => __( 'All Calendar Types', 'google-calendar-events' ), |
||
| 91 | 'parent_item' => __( 'Parent Calendar Type', 'google-calendar-events' ), |
||
| 92 | 'parent_item_colon' => __( 'Parent Calendar Type:', 'google-calendar-events' ), |
||
| 93 | 'new_item_name' => __( 'New Calendar Type', 'google-calendar-events' ), |
||
| 94 | 'add_new_item' => __( 'Add New Calendar Type', 'google-calendar-events' ), |
||
| 95 | 'edit_item' => __( 'Edit Calendar Type', 'google-calendar-events' ), |
||
| 96 | 'update_item' => __( 'Update Calendar Type', 'google-calendar-events' ), |
||
| 97 | 'view_item' => __( 'View Calendar Type', 'google-calendar-events' ), |
||
| 98 | 'separate_items_with_commas' => __( 'Separate calendar types with commas', 'google-calendar-events' ), |
||
| 99 | 'add_or_remove_items' => __( 'Add or remove calendar types', 'google-calendar-events' ), |
||
| 100 | 'choose_from_most_used' => __( 'Choose from the most used', 'google-calendar-events' ), |
||
| 101 | 'popular_items' => __( 'Popular calendar types', 'google-calendar-events' ), |
||
| 102 | 'search_items' => __( 'Search Calendar Types', 'google-calendar-events' ), |
||
| 103 | 'not_found' => __( 'Not Found', 'google-calendar-events' ), |
||
| 104 | ); |
||
| 105 | |||
| 106 | $args = array( |
||
| 107 | 'hierarchical' => true, |
||
| 108 | 'labels' => $labels, |
||
| 109 | 'public' => false, |
||
| 110 | 'show_admin_column' => false, |
||
| 111 | 'show_in_nav_menus' => false, |
||
| 112 | 'show_tagcloud' => false, |
||
| 113 | 'show_ui' => false, |
||
| 114 | ); |
||
| 115 | register_taxonomy( 'calendar_type', array( 'calendar' ), $args ); |
||
| 116 | |||
| 117 | } |
||
| 118 | |||
| 119 | View Code Duplication | if ( ! taxonomy_exists( 'calendar_category' ) ) { |
|
| 120 | |||
| 121 | // Feed Category. |
||
| 122 | $labels = array( |
||
| 123 | 'name' => __( 'Categories', 'google-calendar-events' ), |
||
| 124 | 'singular_name' => __( 'Category', 'google-calendar-events' ), |
||
| 125 | 'menu_name' => __( 'Categories', 'google-calendar-events' ), |
||
| 126 | 'all_items' => __( 'All Categories', 'google-calendar-events' ), |
||
| 127 | 'parent_item' => __( 'Parent Category', 'google-calendar-events' ), |
||
| 128 | 'parent_item_colon' => __( 'Parent Category:', 'google-calendar-events' ), |
||
| 129 | 'new_item_name' => __( 'New Category', 'google-calendar-events' ), |
||
| 130 | 'add_new_item' => __( 'Add New Category', 'google-calendar-events' ), |
||
| 131 | 'edit_item' => __( 'Edit Category', 'google-calendar-events' ), |
||
| 132 | 'update_item' => __( 'Update Category', 'google-calendar-events' ), |
||
| 133 | 'view_item' => __( 'View Category', 'google-calendar-events' ), |
||
| 134 | 'separate_items_with_commas' => __( 'Separate categories with commas', 'google-calendar-events' ), |
||
| 135 | 'add_or_remove_items' => __( 'Add or remove categories', 'google-calendar-events' ), |
||
| 136 | 'choose_from_most_used' => __( 'Choose from the most used', 'google-calendar-events' ), |
||
| 137 | 'popular_items' => __( 'Popular Categories', 'google-calendar-events' ), |
||
| 138 | 'search_items' => __( 'Search Categories', 'google-calendar-events' ), |
||
| 139 | 'not_found' => __( 'Not Found', 'google-calendar-events' ), |
||
| 140 | ); |
||
| 141 | |||
| 142 | $args = array( |
||
| 143 | 'hierarchical' => true, |
||
| 144 | 'labels' => $labels, |
||
| 145 | 'public' => true, |
||
| 146 | 'show_admin_column' => true, |
||
| 147 | 'show_in_nav_menus' => true, |
||
| 148 | 'show_tagcloud' => false, |
||
| 149 | 'show_ui' => true, |
||
| 150 | ); |
||
| 151 | |||
| 152 | register_taxonomy( 'calendar_category', array( 'calendar' ), $args ); |
||
| 153 | } |
||
| 154 | |||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Register custom post types. |
||
| 159 | * |
||
| 160 | * @since 3.0.0 |
||
| 161 | */ |
||
| 162 | public static function register_post_types() { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Filter post content to output a calendar. |
||
| 228 | * |
||
| 229 | * @since 3.0.0 |
||
| 230 | * |
||
| 231 | * @param string $the_content |
||
| 232 | * |
||
| 233 | * @return string |
||
| 234 | */ |
||
| 235 | public function filter_post_content( $the_content ) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Upon posts deletion. |
||
| 296 | * |
||
| 297 | * Delete transients and notices when a calendar is deleted. |
||
| 298 | * |
||
| 299 | * @since 3.0.0 |
||
| 300 | * |
||
| 301 | * @param $post_id |
||
| 302 | */ |
||
| 303 | public function upon_deletion( $post_id ) { |
||
| 319 | |||
| 320 | } |
||
| 321 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.