PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 1.1.6
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v1.1.6
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / classes / Condition_Callbacks.php

Condition_Callbacks.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 1.1.6, at classes/Condition_Callbacks.php

287 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace JP\CC;
4
5
6 /**
7 * Class PUM_Condition_Callbacks
8 */
9 class Condition_Callbacks {
10
11 /**
12 * Checks if this is one of the selected post_type items.
13 *
14 * @param array $condition
15 *
16 * @return bool
17 */
18 public static function post_type( $condition = array() ) {
19 global $post;
20
21 $target = explode( '_', $condition['target'] );
22
23 // Modifier should be the last key.
24 $modifier = array_pop( $target );
25
26 // Post type is the remaining keys combined.
27 $post_type = implode( '_', $target );
28
29 $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
30
31 switch ( $modifier ) {
32 case 'index':
33 if ( is_post_type_archive( $post_type ) ) {
34 return true;
35 }
36 break;
37
38 case 'all':
39 // Checks for valid post type, if $post_type is page, then include the front page as most users simply expect this.
40 if ( self::is_post_type( $post_type ) || ( $post_type == 'page' && is_front_page() ) ) {
41 return true;
42 }
43 break;
44
45 case 'ID':
46 case 'selected':
47 if ( self::is_post_type( $post_type ) && is_singular( $post_type ) && in_array( $post->ID, wp_parse_id_list( $selected ) ) ) {
48 return true;
49 }
50 break;
51
52 case 'children':
53 if ( ! is_post_type_hierarchical( $post_type ) || ! is_singular( $post_type ) ) {
54 return false;
55 }
56
57 // Chosen parents.
58 $selected = wp_parse_id_list( $selected );
59
60 foreach ( $selected as $id ) {
61 if ( $post->post_parent == $id ) {
62 return true;
63 }
64 }
65 break;
66
67 case 'ancestors':
68 if ( ! is_post_type_hierarchical( $post_type ) || ! is_singular( $post_type ) ) {
69 return false;
70 }
71
72 // Ancestors of the current page.
73 $ancestors = get_post_ancestors( $post->ID );
74
75 // Chosen parent/grandparents.
76 $selected = wp_parse_id_list( $selected );
77
78 foreach ( $selected as $id ) {
79 if ( in_array( $id, $ancestors ) ) {
80 return true;
81 }
82 }
83 break;
84
85 case 'template':
86 if ( is_page() && is_page_template( $selected ) ) {
87 return true;
88 }
89 break;
90 }
91
92 return false;
93 }
94
95 /**
96 * Checks if this is one of the selected taxonomy term.
97 *
98 * @param array $condition
99 *
100 * @return bool
101 */
102 public static function taxonomy( $condition = array() ) {
103 $target = explode( '_', $condition['target'] );
104 $settings = isset( $condition['settings'] ) ? $condition['settings'] : array();
105
106 // Remove the tax_ prefix.
107 array_shift( $target );
108
109 // Assign the last key as the modifier _all, _selected
110 $modifier = array_pop( $target );
111
112 // Whatever is left is the taxonomy.
113 $taxonomy = implode( '_', $target );
114
115 if ( $taxonomy == 'category' ) {
116 return self::category( $condition );
117 } elseif ( $taxonomy == 'post_tag' ) {
118 return self::post_tag( $condition );
119 }
120
121 switch ( $modifier ) {
122 case 'all':
123 if ( is_tax( $taxonomy ) ) {
124 return true;
125 }
126 break;
127
128 case 'ID':
129 case 'selected':
130 $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
131
132 if ( is_tax( $taxonomy, wp_parse_id_list( $selected ) ) ) {
133 return true;
134 }
135 break;
136 }
137
138 return false;
139 }
140
141 /**
142 * Checks if this is one of the selected categories.
143 *
144 * @param array $condition
145 *
146 * @return bool
147 */
148 public static function category( $condition = array() ) {
149
150 $target = explode( '_', $condition['target'] );
151 $settings = isset( $condition['settings'] ) ? $condition['settings'] : array();
152
153 // Assign the last key as the modifier _all, _selected
154 $modifier = array_pop( $target );
155
156 switch ( $modifier ) {
157 case 'all':
158 if ( is_category() ) {
159 return true;
160 }
161 break;
162
163 case 'selected':
164 $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
165 if ( is_category( wp_parse_id_list( $selected ) ) ) {
166 return true;
167 }
168 break;
169 }
170
171 return false;
172 }
173
174 /**
175 * Checks if this is one of the selected tags.
176 *
177 * @param array $condition
178 *
179 * @return bool
180 */
181 public static function post_tag( $condition = array() ) {
182
183 $target = explode( '_', $condition['target'] );
184 $settings = isset( $condition['settings'] ) ? $condition['settings'] : array();
185
186 // Assign the last key as the modifier _all, _selected
187 $modifier = array_pop( $target );
188
189 switch ( $modifier ) {
190 case 'all':
191 if ( is_tag() ) {
192 return true;
193 }
194 break;
195
196 case 'selected':
197 $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
198 if ( is_tag( wp_parse_id_list( $selected ) ) ) {
199 return true;
200 }
201 break;
202 }
203
204 return false;
205 }
206
207 /**
208 * Checks if the post_type has the selected categories.
209 *
210 * @param array $condition
211 *
212 * @return bool
213 */
214 public static function post_type_tax( $condition = array() ) {
215 $target = explode( '_w_', $condition['target'] );
216 $settings = isset( $condition['settings'] ) ? $condition['settings'] : array();
217
218 // First key is the post type.
219 $post_type = array_shift( $target );
220
221 // Last Key is the taxonomy
222 $taxonomy = array_pop( $target );
223
224 if ( $taxonomy == 'category' ) {
225 return self::post_type_category( $condition );
226 } elseif ( $taxonomy == 'post_tag' ) {
227 return self::post_type_tag( $condition );
228 }
229
230 $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
231 if ( self::is_post_type( $post_type ) && has_term( wp_parse_id_list( $selected ), $taxonomy ) ) {
232 return true;
233 }
234
235 return false;
236 }
237
238 /**
239 * Checks if the post_type has the selected categories.
240 *
241 * @param array $condition
242 *
243 * @return bool
244 */
245 public static function post_type_category( $condition = array() ) {
246 $target = explode( '_w_', $condition['target'] );
247 $settings = isset( $condition['settings'] ) ? $condition['settings'] : array();
248
249 // First key is the post type.
250 $post_type = array_shift( $target );
251
252 $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
253 if ( self::is_post_type( $post_type ) && has_category( wp_parse_id_list( $selected ) ) ) {
254 return true;
255 }
256
257 return false;
258 }
259
260 /**
261 * Checks is a post_type has the selected tags.
262 *
263 * @param array $condition
264 *
265 * @return bool
266 */
267 public static function post_type_tag( $condition = array() ) {
268 $target = explode( '_w_', $condition['target'] );
269 $settings = isset( $condition['settings'] ) ? $condition['settings'] : array();
270
271 // First key is the post type.
272 $post_type = array_shift( $target );
273
274 $selected = ! empty( $condition['settings']['selected'] ) ? $condition['settings']['selected'] : array();
275 if ( self::is_post_type( $post_type ) && has_tag( wp_parse_id_list( $selected ) ) ) {
276 return true;
277 }
278
279 return false;
280 }
281
282 public static function is_post_type( $post_type ) {
283 global $post;
284 return is_object( $post ) && ( is_singular( $post_type ) || $post->post_type == $post_type );
285 }
286
287 }