Completed
Branch master (55ca36)
by Andrey
01:42
created

Template_Hierarchy::locate_template()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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;
63
64
			if ( ! empty( $posts ) && isset( $posts[0]->post_mime_type ) )
65
				$this->mime_type = explode( '/', $posts[0]->post_mime_type );
66
67
			add_filter( "{$this->mime_type[0]}_template", array( $this, 'query_template' ) );
68
			add_filter( "{$this->mime_type[1]}_template", array( $this, 'query_template' ) );
69
			add_filter( "{$this->mime_type[0]}{$this->mime_type[1]}_template", array( $this, 'query_template' ) );
70
		}
71
	}
72
73
	/**
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 );
184
					$templates[] = "archive-{$post_type}.twig";
185
				}
186
187
				$templates[] = 'archive.twig';
188
				break;
189
190
			default:
191
				$templates = array( "{$type}.twig" );
192
		}
193
194
		$template = $this->locate_template( $templates );
195
196
		if ( empty( $template ) ) {
197
			$template = $fallback;
198
		}
199
200
		return apply_filters( 'meadow_query_template', $template, $type );
201
	}
202
203
	/**
204
	 * Broken out for easier inheritance to customize lookup logic.
205
	 *
206
	 * @param array|string $templates
207
	 *
208
	 * @return string
209
	 */
210
	public function locate_template( $templates ) {
211
212
		return locate_template( $templates );
213
	}
214
}
215