PluginProbe ʕ •ᴥ•ʔ
Widget Options – Advanced Conditional Visibility for Gutenberg Blocks & Classic Widgets / 3.7.13
Widget Options – Advanced Conditional Visibility for Gutenberg Blocks & Classic Widgets v3.7.13
4.2.5 4.2.4 trunk 3.7.10 3.7.11 3.7.12 3.7.13 3.7.14 3.7.2 3.7.5 3.7.6 3.7.7 3.7.8 3.7.9 3.8 3.8.1 3.8.10 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.8.8 3.8.9 3.8.9.1 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.5.1 4.0.6 4.0.6.1 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.1.3 4.2.0 4.2.1 4.2.2 4.2.3
widget-options / includes / widgets / display.php
widget-options / includes / widgets Last commit date
option-tabs 4 years ago display.php 4 years ago extras.php 4 years ago widgets.php 4 years ago
display.php
508 lines
1 <?php
2 /**
3 * Handles Front-end Display
4 *
5 * @copyright Copyright (c) 2015, Jeffrey Carandang
6 * @since 1.0
7 */
8 // Exit if accessed directly
9 if ( ! defined( 'ABSPATH' ) ) exit;
10
11 /**
12 * Handles widget_display_callback filter
13 *
14 * @since 1.0
15 * @global $widget_options
16 * @return $instance
17 */
18
19 //check if function exists
20 if( !function_exists( 'widgetopts_display_callback' ) ):
21 function widgetopts_display_callback( $instance, $widget, $args ){
22 global $widget_options, $current_user;
23
24 $hidden = false;
25 $opts = ( isset( $instance[ 'extended_widget_opts-'. $widget->id ] ) ) ? $instance[ 'extended_widget_opts-'. $widget->id ] : array();
26 $visibility = array( 'show' => array(), 'hide' => array() );
27
28 //wordpress pages
29 $visibility = isset( $opts['visibility'] ) ? $opts['visibility'] : array();
30 $visibility_opts = isset( $opts['visibility']['options'] ) ? $opts['visibility']['options'] : 'hide';
31
32 $is_misc = ( 'activate' == $widget_options['visibility'] && isset( $widget_options['settings']['visibility'] ) && isset( $widget_options['settings']['visibility']['misc'] ) ) ? true : false;
33 $is_types = ( 'activate' == $widget_options['visibility'] && isset( $widget_options['settings']['visibility'] ) && isset( $widget_options['settings']['visibility']['post_type'] ) ) ? true : false;
34 $is_tax = ( 'activate' == $widget_options['visibility'] && isset( $widget_options['settings']['visibility'] ) && isset( $widget_options['settings']['visibility']['taxonomies'] ) ) ? true : false;
35
36 $isWooPage = false;
37 if ( class_exists( 'WooCommerce' ) ) {
38 $wooPageID = 0;
39
40 $wooPageID = ( is_shop() ) ? get_option( 'woocommerce_shop_page_id' ) : $wooPageID;
41 if ( $wooPageID ) {
42 $isWooPage = true;
43
44 $visibility['pages'] = !empty($visibility['pages']) ? $visibility['pages'] : [];
45 if( $visibility_opts == 'hide' && array_key_exists( $wooPageID , $visibility['pages']) ){
46 $hidden = true; //hide if exists on hidden pages
47 }elseif( $visibility_opts == 'show' && !array_key_exists( $wooPageID , $visibility['pages']) ){
48 $hidden = true; //hide if doesn't exists on visible pages
49 }
50
51 //do return to bypass other conditions
52 $hidden = apply_filters( 'widget_options_visibility_page', $hidden );
53
54 if( $hidden ){
55 return false;
56 }
57 }
58 }
59
60 // Normal Pages
61 if ( !$isWooPage ) {
62 if ( $is_misc && ( ( is_home() && is_front_page() ) || is_front_page() ) ) {
63 if( isset( $visibility['misc']['home'] ) && $visibility_opts == 'hide' ){
64 $hidden = true; //hide if checked on hidden pages
65 }elseif( !isset( $visibility['misc']['home'] ) && $visibility_opts == 'show' ){
66 $hidden = true; //hide if not checked on visible pages
67 }
68
69 //do return to bypass other conditions
70 $hidden = apply_filters( 'widget_options_visibility_home', $hidden );
71 if( $hidden ){
72 return false;
73 }
74 }elseif ( $is_misc && is_home() ) { //filter for blog page
75 if( isset( $visibility['misc']['blog'] ) && $visibility_opts == 'hide' ){
76 $hidden = true; //hide if checked on hidden pages
77 }elseif( !isset( $visibility['misc']['blog'] ) && $visibility_opts == 'show' ){
78 $hidden = true; //hide if not checked on visible pages
79 }
80
81 //do return to bypass other conditions
82 $hidden = apply_filters( 'widget_options_visibility_blog', $hidden );
83 if( $hidden ){
84 return false;
85 }
86
87 }elseif ( $is_tax && is_category() ) {
88 if( !isset( $visibility['categories'] ) ){
89 $visibility['categories'] = array();
90 }
91
92 if( !isset( $visibility['categories']['all_categories'] ) && $visibility_opts == 'hide' && array_key_exists( get_query_var('cat') , $visibility['categories']) ){
93 $hidden = true; //hide if exists on hidden pages
94 }elseif( !isset( $visibility['categories']['all_categories'] ) && $visibility_opts == 'show' && !array_key_exists( get_query_var('cat') , $visibility['categories']) ){
95 $hidden = true; //hide if doesn't exists on visible pages
96 }elseif( isset( $visibility['categories']['all_categories'] ) && $visibility_opts == 'hide' ){
97 $hidden = true; //hide to all categories
98 }elseif( isset( $visibility['categories']['all_categories'] ) && $visibility_opts == 'show' ){
99 $hidden = false; //hide to all categories
100 }
101
102 //do return to bypass other conditions
103 $hidden = apply_filters( 'widget_options_visibility_categories', $hidden );
104 if( $hidden ){
105 return false;
106 }
107 }elseif ( $is_tax && is_tag() ) {
108 if( !isset( $visibility['tags'] ) ){
109 $visibility['tags'] = array();
110 }
111
112 if( ( isset( $visibility['taxonomies']['post_tag'] ) && $visibility_opts == 'hide' ) ||
113 ( !isset( $visibility['taxonomies']['post_tag'] ) && $visibility_opts == 'show' )
114 ){
115 $hidden = true; //hide to all tags
116 }elseif( isset( $visibility['taxonomies']['post_tag'] ) && $visibility_opts == 'show' ){
117 $hidden = false; //hide to all tags
118 }
119
120 //do return to bypass other conditions
121 $hidden = apply_filters( 'widget_options_visibility_tags', $hidden );
122 if( $hidden ){
123 return false;
124 }
125 }elseif ( $is_tax && is_tax() ) {
126 $term = get_queried_object();
127 if( !isset( $visibility['taxonomies'] ) ){
128 $visibility['taxonomies'] = array();
129 }
130
131 if( $visibility_opts == 'hide' && array_key_exists( $term->taxonomy , $visibility['taxonomies']) ){
132 $hidden = true; //hide if exists on hidden pages
133 }elseif( $visibility_opts == 'show' && !array_key_exists( $term->taxonomy , $visibility['taxonomies']) ){
134 $hidden = true; //hide if doesn't exists on visible pages
135 }
136
137 //do return to bypass other conditions
138 $hidden = apply_filters( 'widget_options_visibility_taxonomies', $hidden );
139 if( $hidden ){
140 return false;
141 }
142 }elseif ( $is_misc && is_archive() ) {
143 if( isset( $visibility['misc']['archives'] ) && $visibility_opts == 'hide' ){
144 $hidden = true; //hide if checked on hidden pages
145 }elseif( !isset( $visibility['misc']['archives'] ) && $visibility_opts == 'show' ){
146 $hidden = true; //hide if not checked on visible pages
147 }
148
149 //do return to bypass other conditions
150 $hidden = apply_filters( 'widget_options_visibility_archives', $hidden );
151 if( $hidden ){
152 return false;
153 }
154 }elseif ( $is_misc && is_404() ) {
155 if( isset( $visibility['misc']['404'] ) && $visibility_opts == 'hide' ){
156 $hidden = true; //hide if checked on hidden pages
157 }elseif( !isset( $visibility['misc']['404'] ) && $visibility_opts == 'show' ){
158 $hidden = true; //hide if not checked on visible pages
159 }
160
161 //do return to bypass other conditions
162 $hidden = apply_filters( 'widget_options_visibility_404', $hidden );
163 if( $hidden ){
164 return false;
165 }
166 }elseif ( $is_misc && is_search() ) {
167 if( isset( $visibility['misc']['search'] ) && $visibility_opts == 'hide' ){
168 $hidden = true; //hide if checked on hidden pages
169 }elseif( !isset( $visibility['misc']['search'] ) && $visibility_opts == 'show' ){
170 $hidden = true; //hide if not checked on visible pages
171 }
172
173 //do return to bypass other conditions
174 $hidden = apply_filters( 'widget_options_visibility_search', $hidden );
175 if( $hidden ){
176 return false;
177 }
178 }elseif ( is_single() && !is_page() ) {
179 global $post;
180 $type = $post->post_type;
181
182 if( !isset( $visibility['types'] ) ){
183 $visibility['types'] = array();
184 }
185 if( $visibility_opts == 'hide' && array_key_exists( $type , $visibility['types']) ){
186 $hidden = true; //hide if exists on hidden pages
187 }elseif( $visibility_opts == 'show' && !array_key_exists( $type , $visibility['types']) ){
188 $hidden = true; //hide if doesn't exists on visible pages
189 }
190 // do return to bypass other conditions
191 $hidden = apply_filters( 'widget_options_visibility_types', $hidden );
192 //hide posts assign on category
193 if( !isset( $visibility['categories'] ) ){
194 $visibility['categories'] = array();
195 }
196 if( isset( $visibility['categories']['all_categories'] ) && $visibility_opts == 'hide' ){
197 $hidden = true; //hide to all categories
198 }elseif( isset( $visibility['categories']['all_categories'] ) && $visibility_opts == 'show' ){
199 $hidden = false; //hide to all categories
200 }elseif( !isset( $visibility['categories']['all_categories'] ) && !empty( $visibility['categories'] ) ) {
201 $cats = wp_get_post_categories( get_the_ID() );
202 if( is_array( $cats ) && !empty( $cats ) ){
203 $checked_cats = array_keys( $visibility['categories'] );
204 $intersect = array_intersect( $cats , $checked_cats );
205 if( !empty( $intersect ) && $visibility_opts == 'hide' ){
206 $hidden = true;
207 }elseif( !empty( $intersect ) && $visibility_opts == 'show' ){
208 $hidden = false;
209 }
210 }
211 }
212 // do return to bypass other conditions
213 $hidden = apply_filters( 'widget_options_visibility_post_category', $hidden );
214 if( $hidden ){
215 return false;
216 }
217 // echo $type;
218 }elseif ( $is_types && is_page() ) {
219 global $post;
220 //do post type condition first
221 if( isset( $visibility['types'] ) && isset( $visibility['types']['page'] ) ){
222 if( $visibility_opts == 'hide' && array_key_exists( 'page' , $visibility['types']) ){
223 $hidden = true; //hide if exists on hidden pages
224 }elseif( $visibility_opts == 'show' && !array_key_exists( 'page' , $visibility['types']) ){
225 $hidden = true; //hide if doesn't exists on visible pages
226 }
227 }else{
228 //do per pages condition
229 if( !isset( $visibility['pages'] ) ){
230 $visibility['pages'] = array();
231 }
232 if( $visibility_opts == 'hide' && array_key_exists( $post->ID , $visibility['pages']) ){
233 $hidden = true; //hide if exists on hidden pages
234 }elseif( $visibility_opts == 'show' && !array_key_exists( $post->ID , $visibility['pages']) ){
235 $hidden = true; //hide if doesn't exists on visible pages
236 }
237 }
238 //do return to bypass other conditions
239 $hidden = apply_filters( 'widget_options_visibility_page', $hidden );
240 if( $hidden ){
241 return false;
242 }
243 }
244 }
245 //end wordpress pages
246
247
248 //ACF
249 if( isset( $widget_options['acf'] ) && 'activate' == $widget_options['acf'] ){
250 if( isset( $visibility['acf']['field'] ) && !empty( $visibility['acf']['field'] ) ){
251 $acf = get_field_object( $visibility['acf']['field'] );
252 if( $acf && is_array( $acf ) ){
253 $acf_visibility = ( isset( $visibility['acf'] ) && isset( $visibility['acf']['visibility'] ) ) ? $visibility['acf']['visibility'] : 'hide';
254
255 //handle repeater fields
256 if( isset( $acf['value'] ) ){
257 if( is_array( $acf['value'] ) ){
258 $acf['value'] = implode(', ', array_map(function ( $acf_array_value ) {
259 $acf_implode = implode( ',', array_filter($acf_array_value) );
260 return $acf_implode;
261 }, $acf['value']));
262 }
263 }
264
265 switch ( $visibility['acf']['condition'] ) {
266 case 'equal':
267 if( isset( $acf['value'] ) ){
268 if( 'show' == $acf_visibility && $acf['value'] == $visibility['acf']['value'] ){
269 $hidden = false;
270 }else if( 'show' == $acf_visibility && $acf['value'] != $visibility['acf']['value'] ){
271 $hidden = true;
272 }else if( 'hide' == $acf_visibility && $acf['value'] == $visibility['acf']['value'] ){
273 $hidden = true;
274 }else if( 'hide' == $acf_visibility && $acf['value'] != $visibility['acf']['value'] ){
275 $hidden = false;
276 }
277 }
278 break;
279
280 case 'not_equal':
281 if( isset( $acf['value'] ) ){
282 if( 'show' == $acf_visibility && $acf['value'] == $visibility['acf']['value'] ){
283 $hidden = true;
284 }else if( 'show' == $acf_visibility && $acf['value'] != $visibility['acf']['value'] ){
285 $hidden = false;
286 }else if( 'hide' == $acf_visibility && $acf['value'] == $visibility['acf']['value'] ){
287 $hidden = false;
288 }else if( 'hide' == $acf_visibility && $acf['value'] != $visibility['acf']['value'] ){
289 $hidden = true;
290 }
291 }
292 break;
293
294 case 'contains':
295 if( isset( $acf['value'] ) ){
296 if( 'show' == $acf_visibility && strpos( $acf['value'], $visibility['acf']['value'] ) !== false ){
297 $hidden = false;
298 }else if( 'show' == $acf_visibility && strpos( $acf['value'], $visibility['acf']['value'] ) === false ){
299 $hidden = true;
300 }else if( 'hide' == $acf_visibility && strpos( $acf['value'], $visibility['acf']['value'] ) !== false ){
301 $hidden = true;
302 }else if( 'hide' == $acf_visibility && strpos( $acf['value'], $visibility['acf']['value'] ) === false ){
303 $hidden = false;
304 }
305 }
306 break;
307
308 case 'not_contains':
309 if( isset( $acf['value'] ) ){
310 if( 'show' == $acf_visibility && strpos( $acf['value'], $visibility['acf']['value'] ) !== false ){
311 $hidden = true;
312 }else if( 'show' == $acf_visibility && strpos( $acf['value'], $visibility['acf']['value'] ) === false ){
313 $hidden = false;
314 }else if( 'hide' == $acf_visibility && strpos( $acf['value'], $visibility['acf']['value'] ) !== false ){
315 $hidden = false;
316 }else if( 'hide' == $acf_visibility && strpos( $acf['value'], $visibility['acf']['value'] ) === false ){
317 $hidden = true;
318 }
319 }
320 break;
321
322 case 'empty':
323 if( 'show' == $acf_visibility && empty( $acf['value'] ) ){
324 $hidden = false;
325 }else if( 'show' == $acf_visibility && !empty( $acf['value'] ) ){
326 $hidden = true;
327 }elseif( 'hide' == $acf_visibility && empty( $acf['value'] ) ){
328 $hidden = true;
329 }else if( 'hide' == $acf_visibility && !empty( $acf['value'] ) ){
330 $hidden = false;
331 }
332 break;
333
334 case 'not_empty':
335 if( 'show' == $acf_visibility && empty( $acf['value'] ) ){
336 $hidden = true;
337 }else if( 'show' == $acf_visibility && !empty( $acf['value'] ) ){
338 $hidden = false;
339 }elseif( 'hide' == $acf_visibility && empty( $acf['value'] ) ){
340 $hidden = false;
341 }else if( 'hide' == $acf_visibility && !empty( $acf['value'] ) ){
342 $hidden = true;
343 }
344 break;
345
346 default:
347 # code...
348 break;
349 }
350
351 // //do return to bypass other conditions
352 $hidden = apply_filters( 'widget_options_visibility_acf', $hidden );
353 if( $hidden ){
354 return false;
355 }
356 }
357 }
358 }
359
360 //login state
361 if( isset( $widget_options['state'] ) && 'activate' == $widget_options['state'] && isset( $opts['roles'] ) ){
362 if( isset( $opts['roles']['state'] ) && !empty( $opts['roles']['state'] ) ){
363 //do state action here
364 if( $opts['roles']['state'] == 'out' && is_user_logged_in() ){
365 return false;
366 }else if( $opts['roles']['state'] == 'in' && !is_user_logged_in() ){
367 return false;
368 }
369 }
370 }
371
372 if( 'activate' == $widget_options['logic'] ){
373 // display widget logic
374 if( isset( $opts['class'] ) && isset( $opts['class']['logic'] ) && !empty( $opts['class']['logic'] ) ){
375 $display_logic = stripslashes( trim( $opts['class']['logic'] ) );
376 $display_logic = apply_filters( 'widget_options_logic_override', $display_logic );
377 $display_logic = apply_filters( 'extended_widget_options_logic_override', $display_logic );
378 if ( $display_logic === false ){
379 return false;
380 }
381 if ( $display_logic === true ){
382 return true;
383 }
384 if ( stristr($display_logic,"return")===false ){
385 $display_logic="return (" . $display_logic . ");";
386 }
387 $display_logic = htmlspecialchars_decode($display_logic, ENT_QUOTES);
388 try {
389 if ( !eval( $display_logic ) ){
390 return false;
391 }
392 } catch (ParseError $e) {
393 return false;
394 }
395 }
396 }
397
398 if( 'activate' == $widget_options['hide_title'] ){
399 //hide widget title
400 if( isset( $instance['title'] ) && isset( $opts['class'] ) && isset( $opts['class']['title'] ) && '1' == $opts['class']['title'] ){
401 $instance['title'] = '';
402 }
403 }
404
405 return $instance;
406 }
407 add_filter( 'widget_display_callback', 'widgetopts_display_callback', 50, 3 );
408 endif;
409
410 //Don't show widget title
411 if( !function_exists( 'widgetopts_remove_title' ) ):
412 function widgetopts_remove_title( $widget_title, $instance = array(), $widget_id = '' ){
413 global $widget_options;
414 if ( 'activate' == $widget_options['hide_title'] && is_array( $instance ) && !empty( $instance ) ){
415 foreach ( $instance as $key => $value) {
416 if( substr( $key, 0, 20 ) == 'extended_widget_opts' ){
417 $opts = ( isset( $instance[ $key ] ) ) ? $instance[ $key ] : array();
418
419 if( isset( $opts['class'] ) && isset( $opts['class']['title'] ) && '1' == $opts['class']['title'] ){
420 return;
421 }
422
423 break;
424 }
425 }
426 return $widget_title;
427 }else{
428 return ( $widget_title );
429 }
430 }
431 add_filter( 'widget_title', 'widgetopts_remove_title', 10, 4 );
432 endif;
433
434 /*
435 * Add custom classes on dynamic_sidebar_params filter
436 */
437 if( !function_exists( 'widgetopts_add_classes' ) ):
438 function widgetopts_add_classes( $params ){
439 global $widget_options, $wp_registered_widget_controls;
440 $classe_to_add = '';
441 $id_base = $wp_registered_widget_controls[ $params[0]['widget_id'] ]['id_base'];
442 $instance = get_option( 'widget_' . $id_base );
443
444 $num = substr( $params[0]['widget_id'], -1 );
445 if( isset( $wp_registered_widget_controls[ $params[0]['widget_id'] ]['params'][0]['number'] ) ){
446 $num = $wp_registered_widget_controls[ $params[0]['widget_id'] ]['params'][0]['number'];
447 } elseif( isset($wp_registered_widget_controls[ $params[0]['widget_id'] ]['callback']) && is_array($wp_registered_widget_controls[ $params[0]['widget_id'] ]['callback'])){
448 if (isset($wp_registered_widget_controls[ $params[0]['widget_id'] ]['callback'][0]) && isset( $wp_registered_widget_controls[ $params[0]['widget_id'] ]['callback'][0]->number)) {
449 $num = $wp_registered_widget_controls[ $params[0]['widget_id'] ]['callback'][0]->number;
450 }
451 }
452 if( isset( $instance[ $num ] ) ){
453 $opts = ( isset( $instance[ $num ][ 'extended_widget_opts-'. $params[0]['widget_id'] ] ) ) ? $instance[ $num ][ 'extended_widget_opts-'. $params[0]['widget_id'] ] : array();
454 }else{
455 $opts = array();
456 }
457
458 $custom_class = isset( $opts['class'] ) ? $opts['class'] : '';
459 $widget_id_set = $params[0]['widget_id'];
460
461 if( 'activate' == $widget_options['classes'] && isset( $widget_options['settings']['classes'] ) ){
462 //don't add the IDs when the setting is set to NO
463 if( isset( $widget_options['settings']['classes']['id'] ) ){
464 if( is_array( $custom_class ) && isset( $custom_class['id'] ) && !empty( $custom_class['id'] ) ){
465 $params[0]['before_widget'] = preg_replace( '/id="[^"]*/', "id=\"{$custom_class['id']}", $params[0]['before_widget'], 1 );
466 $widget_id_set = $custom_class['id'];
467 }
468 }
469
470 }
471
472 $get_classes = widgetopts_classes_generator( $opts, $widget_options, $widget_options['settings'] );
473
474 //double check array
475 if( !is_array( $get_classes ) ){
476 $get_classes = array();
477 }
478
479 if( 'activate' == $widget_options['classes'] ){
480 if( isset( $widget_options['settings']['classes']['auto'] ) ){
481 //do nothing
482 }else{
483 //check if widget class exists
484 if ( ( strpos( $params[0]['before_widget'], '"widget ' ) !== false ) ||
485 ( strpos( $params[0]['before_widget'], ' widget ' ) !== false ) ||
486 ( strpos( $params[0]['before_widget'], ' widget"' ) !== false)
487 ) {
488 //do nothing
489 }else{
490 $get_classes[] = 'widget';
491 }
492 }
493 }
494
495 if( !empty( $get_classes ) ){
496 $classes = 'class="'. ( implode( ' ', $get_classes ) ) . ' ';
497 $params[0]['before_widget'] = str_replace('class="', $classes, $params[0]['before_widget']);
498 }
499
500 // $params[0]['before_widget'] = str_replace('class="', ' data-animation="asdf" class="', $params[0]['before_widget']);
501
502 return $params;
503 }
504 add_filter( 'dynamic_sidebar_params', 'widgetopts_add_classes' );
505 endif;
506
507 ?>
508