Conditions | 23 |
Paths | 52 |
Total Lines | 127 |
Code Lines | 73 |
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 |
||
79 | public function query_template( $fallback ) { |
||
80 | |||
81 | $type = substr( current_filter(), 0, - 9 ); // trim '_template' from end |
||
82 | $templates = array(); |
||
83 | |||
84 | switch ( $type ) { |
||
85 | case 'embed': |
||
86 | |||
87 | $object = get_queried_object(); |
||
88 | |||
89 | if ( ! empty( $object->post_type ) ) { |
||
90 | |||
91 | $post_format = get_post_format( $object ); |
||
92 | |||
93 | if ( $post_format ) { |
||
94 | $templates[] = "embed-{$object->post_type}-{$post_format}.twig"; |
||
95 | } |
||
96 | |||
97 | $templates[] = "embed-{$object->post_type}.twig"; |
||
98 | } |
||
99 | |||
100 | $templates[] = 'embed.twig'; |
||
101 | |||
102 | break; |
||
103 | |||
104 | case 'taxonomy': |
||
105 | $term = get_queried_object(); |
||
106 | |||
107 | if ( $term ) { |
||
108 | $taxonomy = $term->taxonomy; |
||
109 | $templates[] = "taxonomy-{$taxonomy}-{$term->slug}.twig"; |
||
110 | $templates[] = "taxonomy-{$taxonomy}.twig"; |
||
111 | } |
||
112 | |||
113 | $templates[] = 'taxonomy.twig'; |
||
114 | break; |
||
115 | |||
116 | case 'frontpage': |
||
117 | $templates = array( 'front-page.twig' ); |
||
118 | break; |
||
119 | |||
120 | case 'home': |
||
121 | $templates = array( 'home.twig', 'index.twig' ); |
||
122 | break; |
||
123 | |||
124 | case 'single': |
||
125 | $object = get_queried_object(); |
||
126 | |||
127 | if ( $object ) { |
||
128 | $templates[] = "single-{$object->post_type}.twig"; |
||
129 | } |
||
130 | |||
131 | $templates[] = 'single.twig'; |
||
132 | break; |
||
133 | |||
134 | case 'page': |
||
135 | $page_id = get_queried_object_id(); |
||
136 | // $template = get_page_template_slug(); |
||
137 | $pagename = get_query_var( 'pagename' ); |
||
138 | |||
139 | if ( ! $pagename && $page_id ) { |
||
140 | // If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object |
||
141 | $post = get_queried_object(); |
||
142 | $pagename = $post->post_name; |
||
143 | } |
||
144 | |||
145 | // TODO page templates |
||
146 | // if ( $template && 0 === validate_file( $template ) ) |
||
147 | // $templates[] = $template; |
||
148 | |||
149 | if ( $pagename ) { |
||
150 | $templates[] = "page-{$pagename}.twig"; |
||
151 | } |
||
152 | |||
153 | if ( $page_id ) { |
||
154 | $templates[] = "page-{$page_id}.twig"; |
||
155 | } |
||
156 | |||
157 | $templates[] = 'page.twig'; |
||
158 | break; |
||
159 | |||
160 | case 'category': |
||
161 | case 'tag': |
||
162 | $term = get_queried_object(); |
||
163 | |||
164 | if ( $term ) { |
||
165 | $templates[] = "{$type}-{$term->slug}.twig"; |
||
166 | $templates[] = "{$type}-{$term->term_id}.twig"; |
||
167 | } |
||
168 | |||
169 | $templates[] = "{$type}.twig"; |
||
170 | break; |
||
171 | |||
172 | case 'author': |
||
173 | $author = get_queried_object(); |
||
174 | |||
175 | if ( $author ) { |
||
176 | $templates[] = "author-{$author->user_nicename}.twig"; |
||
177 | $templates[] = "author-{$author->ID}.twig"; |
||
178 | } |
||
179 | |||
180 | $templates[] = 'author.twig'; |
||
181 | break; |
||
182 | |||
183 | case 'archive': |
||
184 | $post_types = array_filter( (array) get_query_var( 'post_type' ) ); |
||
185 | |||
186 | if ( \count( $post_types ) === 1 ) { |
||
187 | $post_type = reset( $post_types ); |
||
188 | $templates[] = "archive-{$post_type}.twig"; |
||
189 | } |
||
190 | |||
191 | $templates[] = 'archive.twig'; |
||
192 | break; |
||
193 | |||
194 | default: |
||
195 | $templates = array( "{$type}.twig" ); |
||
196 | } |
||
197 | |||
198 | $template = $this->locate_template( $templates ); |
||
199 | |||
200 | if ( empty( $template ) ) { |
||
201 | $template = $fallback; |
||
202 | } |
||
203 | |||
204 | return apply_filters( 'meadow_query_template', $template, $type ); |
||
205 | } |
||
206 | |||
219 |