| Conditions | 21 |
| Paths | 48 |
| Total Lines | 109 |
| Code Lines | 67 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 74 | * @param string $fallback |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function query_template( $fallback ) { |
||
| 79 | |||
| 80 | $type = substr( current_filter(), 0, - 9 ); // trim '_template' from end |
||
| 81 | $templates = array(); |
||
| 82 | |||
| 83 | switch ( $type ) { |
||
| 84 | case 'embed': |
||
| 85 | |||
| 86 | $object = get_queried_object(); |
||
| 87 | |||
| 88 | if ( ! empty( $object->post_type ) ) { |
||
| 89 | |||
| 90 | $post_format = get_post_format( $object ); |
||
| 91 | |||
| 92 | if ( $post_format ) { |
||
| 93 | $templates[] = "embed-{$object->post_type}-{$post_format}.twig"; |
||
| 94 | } |
||
| 95 | |||
| 96 | $templates[] = "embed-{$object->post_type}.twig"; |
||
| 97 | } |
||
| 98 | |||
| 99 | $templates[] = 'embed.twig'; |
||
| 100 | |||
| 101 | break; |
||
| 102 | |||
| 103 | case 'taxonomy': |
||
| 104 | $term = get_queried_object(); |
||
| 105 | |||
| 106 | if ( $term ) { |
||
| 107 | $taxonomy = $term->taxonomy; |
||
| 108 | $templates[] = "taxonomy-{$taxonomy}-{$term->slug}.twig"; |
||
| 109 | $templates[] = "taxonomy-{$taxonomy}.twig"; |
||
| 110 | } |
||
| 111 | |||
| 112 | $templates[] = 'taxonomy.twig'; |
||
| 113 | break; |
||
| 114 | |||
| 115 | case 'frontpage': |
||
| 116 | $templates = array( 'front-page.twig' ); |
||
| 117 | break; |
||
| 118 | |||
| 119 | case 'home': |
||
| 120 | $templates = array( 'home.twig', 'index.twig' ); |
||
| 121 | break; |
||
| 122 | |||
| 123 | case 'single': |
||
| 124 | $object = get_queried_object(); |
||
| 125 | |||
| 126 | if ( $object ) |
||
| 127 | $templates[] = "single-{$object->post_type}.twig"; |
||
| 128 | |||
| 129 | $templates[] = 'single.twig'; |
||
| 130 | break; |
||
| 131 | |||
| 132 | case 'page': |
||
| 133 | $page_id = get_queried_object_id(); |
||
| 134 | // $template = get_page_template_slug(); |
||
| 135 | $pagename = get_query_var( 'pagename' ); |
||
| 136 | |||
| 137 | if ( ! $pagename && $page_id ) { |
||
| 138 | // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object |
||
| 139 | $post = get_queried_object(); |
||
| 140 | $pagename = $post->post_name; |
||
| 141 | } |
||
| 142 | |||
| 143 | // TODO page templates |
||
| 144 | // if ( $template && 0 === validate_file( $template ) ) |
||
| 145 | // $templates[] = $template; |
||
| 146 | |||
| 147 | if ( $pagename ) |
||
| 148 | $templates[] = "page-{$pagename}.twig"; |
||
| 149 | |||
| 150 | if ( $page_id ) |
||
| 151 | $templates[] = "page-{$page_id}.twig"; |
||
| 152 | |||
| 153 | $templates[] = 'page.twig'; |
||
| 154 | break; |
||
| 155 | |||
| 156 | case 'category': |
||
| 157 | case 'tag': |
||
| 158 | $term = get_queried_object(); |
||
| 159 | |||
| 160 | if ( $term ) { |
||
| 161 | $templates[] = "{$type}-{$term->slug}.twig"; |
||
| 162 | $templates[] = "{$type}-{$term->term_id}.twig"; |
||
| 163 | } |
||
| 164 | |||
| 165 | $templates[] = "{$type}.twig"; |
||
| 166 | break; |
||
| 167 | |||
| 168 | case 'author': |
||
| 169 | $author = get_queried_object(); |
||
| 170 | |||
| 171 | if ( $author ) { |
||
| 172 | $templates[] = "author-{$author->user_nicename}.twig"; |
||
| 173 | $templates[] = "author-{$author->ID}.twig"; |
||
| 174 | } |
||
| 175 | |||
| 176 | $templates[] = 'author.twig'; |
||
| 177 | break; |
||
| 178 | |||
| 179 | case 'archive': |
||
| 180 | $post_types = array_filter( (array) get_query_var( 'post_type' ) ); |
||
| 181 | |||
| 182 | if ( count( $post_types ) == 1 ) { |
||
| 183 | $post_type = reset( $post_types ); |
||
| 215 |