Stencil_Environment::format_hook()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
/**
3
 * Environment class
4
 *
5
 * Use Filters and Hooks via this class. Prefixes are automatically applied to calls.
6
 *
7
 * @package Stencil
8
 */
9
10
/**
11
 * Class Environment
12
 */
13
class Stencil_Environment {
14
15
	/**
16
	 * Prefixed used for filters
17
	 *
18
	 * @const string
19
	 */
20
	const FILTER_PREFIX = 'stencil:';
21
22
	/**
23
	 * Prefix used for hooks
24
	 *
25
	 * @const string
26
	 */
27
	const HOOK_PREFIX = 'stencil.';
28
29
	/**
30
	 * Unified filter method with globalised prefix
31
	 *
32
	 * @param string $variable , ...
33
	 *
34
	 * @return mixed
35
	 */
36
	public static function filter( $variable ) {
37
		$arguments    = func_get_args();
38
		$arguments[0] = self::format_filter( $variable );
39
40
		return call_user_func_array( 'apply_filters', $arguments );
41
	}
42
43
	/**
44
	 * Unified hook method with globalised prefix
45
	 *
46
	 * @param string $hook , ...
47
	 */
48
	public static function trigger( $hook ) {
49
		$arguments    = func_get_args();
50
		$arguments[0] = self::format_hook( $hook );
51
52
		call_user_func_array( 'do_action', $arguments );
53
	}
54
55
	/**
56
	 * Provide unified filter format
57
	 *
58
	 * @param string $filter Filter to use to build.
59
	 *
60
	 * @return string
61
	 */
62
	public static function format_filter( $filter ) {
63
		if ( strpos( $filter, self::FILTER_PREFIX ) !== 0 ) {
64
			$filter = sprintf( self::FILTER_PREFIX . '%s', $filter );
65
		}
66
67
		return $filter;
68
	}
69
70
	/**
71
	 * Provide unified hook format
72
	 *
73
	 * @param string $hook Hook to format.
74
	 *
75
	 * @return string
76
	 */
77
	public static function format_hook( $hook ) {
78
		if ( strpos( $hook, self::HOOK_PREFIX ) !== 0 ) {
79
			$hook = sprintf( self::HOOK_PREFIX . '%s', $hook );
80
		}
81
82
		return $hook;
83
	}
84
85
	/**
86
	 * Get the identifier for the active page
87
	 *
88
	 * @return null|string
89
	 */
90
	public static function get_page() {
91
		$page = null;
92
93
		/**
94
		 * See: https://codex.wordpress.org/Conditional_Tags
95
		 */
96
97
		switch ( true ) {
98
			case ( is_404() ):
99
				$page = 'error';
100
				break;
101
102
			case ( is_search() ):
103
				$page = 'search';
104
				break;
105
106
			/**
107
			 * Case is_home() return true when on the posts list page,
108
			 * This is usually the page that shows the latest 10 posts.
109
			 */
110
			case ( is_home() ):
111
				$page = 'home';
112
				break;
113
114
			/**
115
			 * Case is_front_page() returns true if the user is on the page or page of posts that is set to the front page
116
			 * on Settings->Reading->Front page displays
117
			 */
118
			case ( is_front_page() ):
119
				$page = 'front_page';
120
				break;
121
122
			case ( is_post_type_archive() ):
123
				$page = 'post-type-archive';
124
				break;
125
126
			case ( is_page_template() ):
127
				$page = 'custom';
128
				break;
129
130
			case ( is_single() ):
131
				$page = 'single';
132
				break;
133
134
			case ( is_page() ):
135
				$page = 'page';
136
				break;
137
138
			case ( is_attachment() ):
139
				$page = 'attachment';
140
				break;
141
142
			case ( is_comments_popup() ):
143
				$page = 'comments-popup';
144
				break;
145
146
			case ( is_tax() ):
147
				$page = 'taxonomy';
148
				break;
149
150
			case ( is_tag() ):
151
				$page = 'tag';
152
				break;
153
154
			case ( is_date() ):
155
				$page = 'date';
156
				break;
157
158
			case ( is_category() ):
159
				$page = 'category';
160
				break;
161
162
			case ( is_author() ):
163
				$page = 'author';
164
				break;
165
166
			case ( is_archive() ):
167
				$page = 'archive';
168
				break;
169
		}
170
171
		/**
172
		 * Filter: override for page selection
173
		 */
174
		$page = self::filter( 'page-' . $page, $page );
175
		$page = self::filter( 'page', $page );
176
177
		return $page;
178
	}
179
}
180