Completed
Push — master ( 9148b5...ea58fd )
by Andrey
09:02
created

Template_Hierarchy::query_template()   D

Complexity

Conditions 23
Paths 52

Size

Total Lines 127
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 127
rs 4.6303
cc 23
eloc 73
nc 52
nop 1

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
namespace Rarst\Meadow;
3
4
/**
5
 * Augment native template hierarchy with non-PHP template processing.
6
 *
7
 * @deprecated 0.2:1.0 Deprecated in favor of better hierarchy filter in WP 4.7+.
8
 */
9
class Template_Hierarchy {
10
11
	/** @var string[] $template_types Template type names to be used for dynamic hooks. */
12
	public $template_types = array(
13
		'embed',
14
		'404',
15
		'search',
16
		'taxonomy',
17
		'frontpage',
18
		'home',
19
		'attachment',
20
		'single',
21
		'page',
22
		'singular',
23
		'category',
24
		'tag',
25
		'author',
26
		'date',
27
		'archive',
28
		'commentspopup',
29
		'paged',
30
		'index',
31
	);
32
33
	protected $mime_type;
34
35
	public function enable() {
36
37
		add_action( 'template_redirect', array( $this, 'template_redirect' ) );
38
39
		foreach ( $this->template_types as $type ) {
40
			add_filter( "{$type}_template", array( $this, 'query_template' ) );
41
		}
42
	}
43
44
	public function disable() {
45
46
		remove_action( 'template_redirect', array( $this, 'template_redirect' ) );
47
48
		foreach ( $this->template_types as $type ) {
49
			remove_filter( "{$type}_template", array( $this, 'query_template' ) );
50
		}
51
52
		if ( ! empty($this->mime_type) ) {
53
			remove_filter( "{$this->mime_type[0]}_template", array( $this, 'query_template' ) );
54
			remove_filter( "{$this->mime_type[1]}_template", array( $this, 'query_template' ) );
55
			remove_filter( "{$this->mime_type[0]}{$this->mime_type[1]}_template", array( $this, 'query_template' ) );
56
		}
57
	}
58
59
	public function template_redirect() {
60
61
		if ( is_attachment() ) {
62
			global $posts;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
63
64
			if ( ! empty( $posts ) && isset( $posts[0]->post_mime_type ) ) {
65
				$this->mime_type = explode( '/', $posts[0]->post_mime_type );
66
			}
67
68
			add_filter( "{$this->mime_type[0]}_template", array( $this, 'query_template' ) );
69
			add_filter( "{$this->mime_type[1]}_template", array( $this, 'query_template' ) );
70
			add_filter( "{$this->mime_type[0]}{$this->mime_type[1]}_template", array( $this, 'query_template' ) );
71
		}
72
	}
73
74
	/**
75
	 * @param string $fallback
76
	 *
77
	 * @return string
78
	 */
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':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
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();
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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
207
	/**
208
	 * Broken out for easier inheritance to customize lookup logic.
209
	 *
210
	 * @param array|string $templates
211
	 *
212
	 * @return string
213
	 */
214
	public function locate_template( $templates ) {
215
216
		return locate_template( $templates );
217
	}
218
}
219