-
Notifications
You must be signed in to change notification settings - Fork 1
/
logic.php
344 lines (278 loc) · 7.9 KB
/
logic.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
/**
* @author Jason Lemahieu and Kevin Graeme (Cooperative Extension Technology Services)
* @copyright Copyright (c) 2011 - 2015 Jason Lemahieu and Kevin Graeme (Cooperative Extension Technology Services)
* @license http://www.gnu.org/licenses/gpl-2.0.html GPLv2
* @package CETS\Conditional_Widgets
*/
add_filter( 'widget_display_callback', 'conditional_widgets_widget' );
/**
* Determine whether or not this widget should be displayed on this page request
*/
function conditional_widgets_widget( $instance ) {
/* variables we have access to
$instance['cw_home_enable_checkbox']
$instance['cw_select_home_page'] //show == 1, hide == 0, show only on home == 2
// pages
$instance['cw_pages_enable_checkbox']
$instance['cw_select_pages']
$instance['cw_pages_sub_checkbox']
$instance['cw_selected_pages']
$instance['cw_pages_all'] // since 1.8
// custom_taxes - since 1.9
$instance['cw_custom']
$instance['cw_custom'][$type][$tax]['enable']
$instance['cw_custom'][$type][$tax]['select']
$instance['cw_custom'][$type][$tax]['all']
$instance['cw_custom'][$type][$tax]['sub']
$instance['cw_custom'][$type][$tax]['selected_ids']
// utility
$instance['cw_posts_page_hide']
$instance['cw_404_hide']
$instance['cw_search_hide']
$instance['cw_date_archive_hide']
$instance['cw_author_archive_hide']
$instance['cw_tag_archive_hide']
// utility - other
$instance['cw_mobile_hide'] - since 2.2
*/
// First, process 'other' options which take precedence over the following
/* is_mobile */
if ( isset( $instance['cw_mobile_hide'] ) && $instance['cw_mobile_hide'] == 1 ) {
if ( wp_is_mobile() ) {
return false;
}
}
/* is_NOT_mobile */
if ( isset( $instance['cw_desktop_hide'] ) && $instance['cw_desktop_hide'] == 1 ) {
if ( ! wp_is_mobile() ) {
return false;
}
}
global $wp_query;
$qvars = $wp_query->query_vars;
$type_tax_pairs = apply_filters( 'conditional_widgets_type_tax_pairs', array() );
$instance = conditional_widgets_init_instance( $instance );
if ( $instance['cw_home_enable_checkbox'] ) {
//box checked for home page logic takes priority by processing first
switch ( $instance['cw_select_home_page'] ) {
case 0: //hide if front page
if ( is_front_page() ) {
return false;
}
break;
case 1: //show if front page
if ( is_front_page() ) {
return $instance;
}
break;
case 2: //only show on front, hide otherwise
if ( is_front_page() ) {
return $instance;
} else {
return false;
}
break;
}
}
$arr_pages = $instance['cw_selected_pages'];
if ( ! is_array( $arr_pages ) ) {
$arr_pages = array();
}
if ( $instance['cw_pages_enable_checkbox'] && is_page() ) {
// We care about pages and this is a page.
// see if we are using same logic for ALL pages
if ( isset( $instance['cw_pages_all'] ) && $instance['cw_pages_all'] == 1 ) {
if ( $instance['cw_select_pages'] == 1 ) {
// SHOW
return $instance;
} else {
// HIDE
return false;
}
}
$current_page_id = $wp_query->post->ID;
//see if we care about subpages
if ( $instance['cw_pages_sub_checkbox'] == 1 ) {
foreach ( $arr_pages as $page ) {
$args = 'child_of=' . $page;
$newpages = get_pages( $args );
foreach ( $newpages as $newpage ) {
array_push( $arr_pages, $newpage->ID );
}
}
} //if subpages count
$match = false;
if ( in_array( $current_page_id, $arr_pages ) ) {
$match = true;
}
if ( $match ) {
//we matched a page
if ( $instance['cw_select_pages'] == 1 ) {
//and we're showing on matched pages - SHOW
return $instance;
} else {
//and we're hiding on matched pages - so HIDE
return false;
}
} else {
//we did NOT match a page
if ( $instance['cw_select_pages'] == 1 ) {
//and we're telling it to show on matched pages - so HIDE
return false;
} else {
//we didn't match a page, and we told it to hide on those pages - so SHOW
return $instance;
}
}
} //is_page && we care
// is individual post of any type (other than page)
if ( is_single() ) {
$current_post_id = $wp_query->post->ID;
$type = get_post_type();
if ( isset( $instance['cw_custom'][$type] ) ) {
$subdata_type = $instance['cw_custom'][$type];
foreach ( $subdata_type as $tax => $subdata_type_tax ) {
// loop over each taxonomy with CW logic associated with this type
if ( $subdata_type_tax['enable'] != 1 ) {
// we don't care about this post type, so just display.
return $instance;
}
if ( $subdata_type_tax['all'] == 1 ) {
// Don't care about specific terms, just the post type.
if ( $subdata_type_tax['select'] == 1 ) {
return $instance;
} else {
return false;
}
}
// we DO care about specific terms, so see if we match a term or not
$terms_to_match = $subdata_type_tax['selected_ids'];
if ( !is_array( $terms_to_match ) ) {
$terms_to_match = array();
}
if ( $subdata_type_tax['sub'] == 1 ) {
if ( isset( $subdata_type_tax['selected_ids'] ) && is_array( $subdata_type_tax['selected_ids'] ) ) {
foreach ( $subdata_type_tax['selected_ids'] as $term ) {
$kiddos = get_term_children( $term, $tax );
$terms_to_match = array_merge( $terms_to_match, $kiddos );
}
}
}
if ( $terms_to_match ) {
$match = is_object_in_term( $current_post_id, $tax, $terms_to_match );
} else {
$match = false;
}
if ( $match ) {
if ( $subdata_type_tax['select'] == 1 ) {
return $instance;
} else {
return false;
}
} else {
if ( $subdata_type_tax['select'] == 1 ) {
return false;
} else {
return $instance;
}
}
//is_object_in_taxonomy - TODO?
}
} else {
// we have no settings for this post type
return $instance;
}
} // /is_single
if ( is_category() ) {
if ( isset( $instance['cw_custom']['post']['category'] ) ) {
$cat_subdata = $instance['cw_custom']['post']['category'];
} else {
// no post specific logic here
return $instance;
}
if ( $cat_subdata['enable'] != 1 ) {
// we don't care about categories
return $instance;
}
if ( $cat_subdata['all'] == 1 ) {
// Don't care about specific terms, just the post type.
if ( $cat_subdata['select'] == 1 ) {
return $instance;
} else {
return false;
}
}
// we DO care about specific terms, so see if we match a term or not
$terms_to_match = $cat_subdata['selected_ids'];
if ( $cat_subdata['sub'] == 1 ) {
if ( isset( $cat_subdata['selected_ids'] ) && is_array( $cat_subdata['selected_ids'] ) ) {
foreach ( $cat_subdata['selected_ids'] as $term ) {
$kiddos = get_term_children( $term, 'category' );
$terms_to_match = array_merge( $terms_to_match, $kiddos );
}
}
}
if ( ! is_array( $terms_to_match ) ) {
$terms_to_match = array();
}
$cat = $qvars['cat'];
$match = in_array( $cat, $terms_to_match );
if ( $match ) {
if ( $cat_subdata['select'] == 1 ) {
return $instance;
} else {
return false;
}
} else {
if ( $cat_subdata['select'] == 1 ) {
return false;
} else {
return $instance;
}
}
} // /is_category
// TODO
/*
if (is_tax()) {
}
*/
//since 1.4
if ( is_home() ) {
if ( $instance['cw_posts_page_hide'] == 1 ) {
return false;
}
}
// since 1.1
if ( is_404() ) {
if ( $instance['cw_404_hide'] == 1 ) {
return false;
}
} //if 404
// since 1.1
if ( is_search() ) {
if ( $instance['cw_search_hide'] == 1 ) {
return false;
}
} // if search
// since 1.1
if ( is_author() ) {
if ( $instance['cw_author_archive_hide'] == 1 ) {
return false;
}
}
// since 1.1
if ( is_date() ) {
if ( $instance['cw_date_archive_hide'] == 1 ) {
return false;
}
}
//since 1.2
if ( is_tag() ) {
if ( $instance['cw_tag_archive_hide'] == 1 ) {
return false;
}
}
//default to showing
return $instance;
} // /function conditional_widgets_widget()