Completed
Push — master ( ea58fd...fed7ac )
by Andrey
01:28
created

Template_Hierarchy   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 33
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 210
rs 9.3999
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
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':
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
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