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 |
||
| 8 | class Taxonomy extends Component |
||
| 9 | { |
||
| 10 | const CUSTOM_KEYS = [ |
||
| 11 | 'menu_name', 'plural', 'single', |
||
| 12 | ]; |
||
| 13 | |||
| 14 | const TAXONOMY_DEFAULTS = [ |
||
| 15 | 'hierarchical' => true, |
||
| 16 | 'labels' => [], |
||
| 17 | 'menu_name' => '', |
||
| 18 | 'plural' => '', |
||
| 19 | 'post_types' => [], |
||
| 20 | 'public' => true, |
||
| 21 | 'rewrite' => true, |
||
| 22 | 'show_admin_column' => true, |
||
| 23 | 'show_ui' => true, |
||
| 24 | 'single' => '', |
||
| 25 | ]; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | public $taxonomies = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * {@inheritdoc} |
||
| 34 | */ |
||
| 35 | public function init() |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @return null|WP_Query |
||
| 46 | * @filter parse_query |
||
| 47 | */ |
||
| 48 | public function filterByTaxonomy( WP_Query $query ) |
||
| 49 | { |
||
| 50 | if( !is_admin() || $this->app->screen()->base != 'edit' )return; |
||
| 51 | $vars = &$query->query_vars; |
||
| 52 | foreach( array_keys( $this->taxonomies ) as $taxonomy ) { |
||
| 53 | if( !isset( $vars[$taxonomy] ))return; |
||
| 54 | if( $term = get_term_by( 'id', $vars[$taxonomy], $taxonomy )) { |
||
| 55 | $vars[$taxonomy] = $term->slug; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | return $query; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return void |
||
| 63 | * @action restrict_manage_posts |
||
| 64 | */ |
||
| 65 | public function printFilters() |
||
| 66 | { |
||
| 67 | global $wp_query; |
||
| 68 | foreach( $this->taxonomies as $taxonomy => $args ) { |
||
| 69 | if( !in_array( get_current_screen()->post_type, $args['post_types'] ))continue; |
||
| 70 | $selected = isset( $wp_query->query[$taxonomy] ) |
||
| 71 | ? $wp_query->query[$taxonomy] |
||
| 72 | : false; |
||
| 73 | wp_dropdown_categories([ |
||
| 74 | 'hide_if_empty' => true, |
||
| 75 | 'name' => $taxonomy, |
||
| 76 | 'orderby' => 'name', |
||
| 77 | 'selected' => $selected, |
||
| 78 | 'show_option_all' => $args['labels']['all_items'], |
||
| 79 | 'taxonomy' => $taxonomy, |
||
| 80 | ]); |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @return void |
||
| 86 | * @action register |
||
| 87 | */ |
||
| 88 | public function register() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | protected function normalize() |
||
| 102 | { |
||
| 103 | View Code Duplication | foreach( $this->app->config['taxonomies'] as $taxonomy => $args ) { |
|
| 104 | $this->taxonomies[$taxonomy] = apply_filters( 'pollux/taxonomy/args', |
||
| 105 | $this->normalizeThis( $args, static::TAXONOMY_DEFAULTS, $taxonomy ) |
||
| 106 | ); |
||
| 107 | } |
||
| 108 | $this->taxonomies = array_diff_key( |
||
| 109 | $this->taxonomies, |
||
| 110 | get_taxonomies( ['_builtin' => true] ) |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param mixed $labels |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | protected function normalizeLabels( $labels, array $args ) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $menuname |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | protected function normalizeMenuName( $menuname, array $args ) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param mixed $types |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | protected function normalizePostTypes( $types ) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | protected function setLabels( array $args ) |
||
| 162 | } |
||
| 163 |