Widget Logic - Version 5.8.0

Version Description

Added full support for WP customizer.

In case of a fatal error in logic, the widget will not be displayed.

Download this release

Release Info

Developer wpchefgadget
Plugin Icon 128x128 Widget Logic
Version 5.8.0
Comparing to
See all releases

Code changes from version 5.7.4 to 5.8.0

Files changed (2) hide show
  1. readme.txt +7 -2
  2. widget_logic.php +147 -114
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: wpchefgadget, alanft
3
  Donate link: http://www.justgiving.com/widgetlogic_cancerresearchuk
4
  Tags: widget, admin, conditional tags, filter, context
5
  Requires at least: 3.0
6
- Tested up to: 4.7.4
7
- Stable tag: 5.7.4
8
  License: GPLv2 or later
9
 
10
  Widget Logic lets you control on which pages widgets appear using WP's conditional tags. It also adds a 'widget_content' filter.
@@ -119,6 +119,11 @@ Tighten up your definitions with PHPs 'logical AND' &&, for example:
119
 
120
  == Changelog ==
121
 
 
 
 
 
 
122
  = 5.7.4 =
123
  Fixed the "Warning: Attempt to assign property of non-object" bug.
124
  https://wordpress.org/support/topic/latest-update-seems-break-my-installation/
3
  Donate link: http://www.justgiving.com/widgetlogic_cancerresearchuk
4
  Tags: widget, admin, conditional tags, filter, context
5
  Requires at least: 3.0
6
+ Tested up to: 4.7.5
7
+ Stable tag: 5.8.0
8
  License: GPLv2 or later
9
 
10
  Widget Logic lets you control on which pages widgets appear using WP's conditional tags. It also adds a 'widget_content' filter.
119
 
120
  == Changelog ==
121
 
122
+ = 5.8.0 =
123
+ Added full support for WP customizer.
124
+
125
+ In case of a fatal error in logic, the widget will not be displayed.
126
+
127
  = 5.7.4 =
128
  Fixed the "Warning: Attempt to assign property of non-object" bug.
129
  https://wordpress.org/support/topic/latest-update-seems-break-my-installation/
widget_logic.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Widget Logic
4
  Plugin URI: http://wordpress.org/extend/plugins/widget-logic/
5
  Description: Control widgets with WP's conditional tags is_home etc
6
- Version: 5.7.4
7
  Author: wpchefgadget, alanft
8
 
9
  Text Domain: widget-logic
@@ -28,7 +28,9 @@ function widget_logic_activate()
28
  }
29
 
30
  $plugin_dir = basename(dirname(__FILE__));
31
- global $wl_options;
 
 
32
 
33
  add_action( 'init', 'widget_logic_init' );
34
  function widget_logic_init()
@@ -62,10 +64,14 @@ if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) )
62
 
63
  if (is_admin())
64
  {
65
- add_filter( 'widget_update_callback', 'widget_logic_ajax_update_callback', 10, 4); // widget changes submitted by ajax method
66
- add_action( 'sidebar_admin_setup', 'widget_logic_expand_control'); // before any HTML output save widget changes and add controls to each widget on the widget admin page
67
- add_action( 'sidebar_admin_page', 'widget_logic_options_control'); // add Widget Logic specific options on the widget admin page
68
- add_filter( 'plugin_action_links', 'wl_charity', 10, 2); // add my justgiving page link to the plugin admin page
 
 
 
 
69
  }
70
  else
71
  {
@@ -86,7 +92,13 @@ else
86
  }
87
 
88
 
89
-
 
 
 
 
 
 
90
 
91
 
92
  function widget_logic_sidebars_widgets_filter_add()
@@ -98,17 +110,11 @@ function widget_logic_sidebars_widgets_filter_add()
98
 
99
 
100
  // CALLED VIA 'widget_update_callback' FILTER (ajax update of a widget)
101
- function widget_logic_ajax_update_callback($instance, $new_instance, $old_instance, $this_widget)
102
  {
103
- global $wl_options;
104
- $widget_id=$this_widget->id;
105
- if ( isset($_POST[$widget_id.'-widget_logic']))
106
- {
107
- $wl_options[$widget_id]=trim($_POST[$widget_id.'-widget_logic']);
108
- update_option('widget_logic', $wl_options);
109
-
110
- unset( $_POST[$widget_id.'-widget_logic'], $_REQUEST[$widget_id.'-widget_logic'] );
111
- }
112
  return $instance;
113
  }
114
 
@@ -156,36 +162,7 @@ function widget_logic_expand_control()
156
  wp_redirect( admin_url('widgets.php') );
157
  exit;
158
  }
159
-
160
-
161
- // ADD EXTRA WIDGET LOGIC FIELD TO EACH WIDGET CONTROL
162
- // pop the widget id on the params array (as it's not in the main params so not provided to the callback)
163
- foreach ( $wp_registered_widgets as $id => $widget )
164
- { // controll-less widgets need an empty function so the callback function is called.
165
- if (!isset($wp_registered_widget_controls[$id]))
166
- wp_register_widget_control($id,$widget['name'], 'widget_logic_empty_control');
167
- $wp_registered_widget_controls[$id]['callback_wl_redirect']=$wp_registered_widget_controls[$id]['callback'];
168
- $wp_registered_widget_controls[$id]['callback']='widget_logic_extra_control';
169
- array_push($wp_registered_widget_controls[$id]['params'],$id);
170
- }
171
-
172
-
173
- // UPDATE WIDGET LOGIC WIDGET OPTIONS (via accessibility mode?)
174
- if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) )
175
- {
176
- $widgt_ids = (array)@$_POST['widget-id'];
177
- foreach ( $widgt_ids as $widget_number => $widget_id )
178
- if (isset($_POST[$widget_id.'-widget_logic']))
179
- $wl_options[$widget_id]=trim($_POST[$widget_id.'-widget_logic']);
180
-
181
- // clean up empty options (in PHP5 use array_intersect_key)
182
- $regd_plus_new=array_merge(array_keys($wp_registered_widgets),array_values($widgt_ids),
183
- array('widget_logic-options-filter', 'widget_logic-options-wp_reset_query', 'widget_logic-options-load_point', 'widget_logic-options-show_errors'));
184
- foreach (array_keys($wl_options) as $key)
185
- if (!in_array($key, $regd_plus_new))
186
- unset($wl_options[$key]);
187
- }
188
-
189
  // UPDATE OTHER WIDGET LOGIC OPTIONS
190
  // must update this to use http://codex.wordpress.org/Settings_API
191
  if ( isset($_POST['widget_logic-options-submit']) )
@@ -276,36 +253,36 @@ function widget_logic_options_control()
276
  }
277
 
278
  // added to widget functionality in 'widget_logic_expand_control' (above)
279
- function widget_logic_empty_control() {}
280
-
281
-
282
-
283
- // added to widget functionality in 'widget_logic_expand_control' (above)
284
- function widget_logic_extra_control()
285
- { global $wp_registered_widget_controls, $wl_options;
286
-
287
- $params=func_get_args();
288
- $id=array_pop($params);
289
-
290
- // go to the original control function
291
- $callback=$wp_registered_widget_controls[$id]['callback_wl_redirect'];
292
- if (is_callable($callback))
293
- call_user_func_array($callback, $params);
294
-
295
- $value = !empty( $wl_options[$id ] ) ? htmlspecialchars( stripslashes( $wl_options[$id ] ),ENT_QUOTES ) : '';
296
-
297
- // dealing with multiple widgets - get the number. if -1 this is the 'template' for the admin interface
298
- $id_disp=$id;
299
- if (!empty($params) && isset($params[0]['number']))
300
- { $number=$params[0]['number'];
301
- if ($number==-1) {$number="__i__"; $value="";}
302
- $id_disp=$wp_registered_widget_controls[$id]['id_base'].'-'.$number;
303
  }
304
- // output our extra widget logic field
305
- echo "<p><label for='".$id_disp."-widget_logic'>". __('Widget logic:','widget-logic'). " <textarea class='widefat' type='text' name='".$id_disp."-widget_logic' id='".$id_disp."-widget_logic' >".$value."</textarea></label></p>";
306
- }
307
-
 
 
308
 
 
 
 
 
 
 
 
 
 
 
309
 
310
  // CALLED ON 'plugin_action_links' ACTION
311
  function wl_charity($links, $file)
@@ -321,9 +298,13 @@ function wl_charity($links, $file)
321
 
322
 
323
  // CALLED ON 'sidebars_widgets' FILTER
324
- function widget_logic_filter_sidebars_widgets($sidebars_widgets)
325
  {
326
- global $wp_reset_query_is_done, $wl_options;
 
 
 
 
327
 
328
  // reset any database queries done now that we're about to make decisions based on the context given in the WP query for the page
329
  if ( !empty( $wl_options['widget_logic-options-wp_reset_query'] ) && empty( $wp_reset_query_is_done ) )
@@ -331,60 +312,112 @@ function widget_logic_filter_sidebars_widgets($sidebars_widgets)
331
  wp_reset_query();
332
  $wp_reset_query_is_done=true;
333
  }
334
-
335
  // loop through every widget in every sidebar (barring 'wp_inactive_widgets') checking WL for each one
336
  foreach($sidebars_widgets as $widget_area => $widget_list)
337
- { if ($widget_area=='wp_inactive_widgets' || empty($widget_list)) continue;
 
 
338
 
339
  foreach($widget_list as $pos => $widget_id)
340
- { if (empty($wl_options[$widget_id])) continue;
341
- $wl_value=stripslashes(trim($wl_options[$widget_id]));
342
- if (empty($wl_value)) continue;
343
-
344
- $wl_value=apply_filters( "widget_logic_eval_override", $wl_value );
345
- if ($wl_value===false)
346
- { unset($sidebars_widgets[$widget_area][$pos]);
347
  continue;
348
- }
349
- if ($wl_value===true) continue;
350
-
351
- if (stristr($wl_value,"return")===false)
352
- $wl_value="return (" . $wl_value . ");";
353
 
354
- $show_errors = !empty($wl_options['widget_logic-options-show_errors']) && current_user_can('manage_options');
 
355
 
356
- if ( $show_errors )
357
- $save = ini_get('display_errors');
358
- try {
359
- if ( $show_errors )
360
- ini_set( 'display_errors', 'On' );
361
-
362
- if (!eval($wl_value))
363
- unset($sidebars_widgets[$widget_area][$pos]);
364
-
365
- if ( $show_errors )
366
- ini_set( 'display_errors', $save );
367
- }
368
- catch ( Error $e ) {
369
- if ( current_user_can('manage_options') && !empty($wl_options['widget_logic-options-show_errors']) )
370
- trigger_error( 'Invalid Widget Logic: '.$e->getMessage(), E_USER_WARNING );
371
-
372
- if ( $show_errors )
373
- ini_set( 'display_errors', $save );
374
-
375
  continue;
376
- }
377
-
 
 
 
 
 
378
  }
379
  }
380
  return $sidebars_widgets;
381
  }
382
 
383
 
 
 
 
 
 
 
 
 
 
 
384
 
385
- // If 'widget_logic-options-filter' is selected the widget_content filter is implemented...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
386
 
 
 
 
 
 
 
 
 
387
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
388
 
389
  // CALLED ON 'dynamic_sidebar_params' FILTER - this is called during 'dynamic_sidebar' just before each callback is run
390
  // swap out the original call back and replace it with our own
3
  Plugin Name: Widget Logic
4
  Plugin URI: http://wordpress.org/extend/plugins/widget-logic/
5
  Description: Control widgets with WP's conditional tags is_home etc
6
+ Version: 5.8.0
7
  Author: wpchefgadget, alanft
8
 
9
  Text Domain: widget-logic
28
  }
29
 
30
  $plugin_dir = basename(dirname(__FILE__));
31
+ global $wl_options, $wl_in_customizer;
32
+
33
+ $wl_in_customizer = false;
34
 
35
  add_action( 'init', 'widget_logic_init' );
36
  function widget_logic_init()
64
 
65
  if (is_admin())
66
  {
67
+ add_filter( 'in_widget_form', 'widget_logic_extra_control', 10, 3 );
68
+ add_filter( 'widget_update_callback', 'widget_logic_update_callback', 10, 4);
69
+
70
+ add_action( 'sidebar_admin_setup', 'widget_logic_expand_control');
71
+ // before any HTML output save widget changes and add controls to each widget on the widget admin page
72
+ add_action( 'sidebar_admin_page', 'widget_logic_options_control');
73
+ // add Widget Logic specific options on the widget admin page
74
+ add_filter( 'plugin_action_links', 'wl_charity', 10, 2);// add my justgiving page link to the plugin admin page
75
  }
76
  else
77
  {
92
  }
93
 
94
 
95
+ function widget_logic_in_customizer()
96
+ {
97
+ global $wl_in_customizer;
98
+ $wl_in_customizer = true;
99
+ add_filter( 'widget_display_callback', 'widget_logic_customizer_display_callback', 10, 3 );
100
+ }
101
+ add_action( 'customize_preview_init', 'widget_logic_in_customizer' );
102
 
103
 
104
  function widget_logic_sidebars_widgets_filter_add()
110
 
111
 
112
  // CALLED VIA 'widget_update_callback' FILTER (ajax update of a widget)
113
+ function widget_logic_update_callback( $instance, $new_instance, $old_instance, $this_widget )
114
  {
115
+ if ( isset( $new_instance['widget_logic'] ) )
116
+ $instance['widget_logic'] = $new_instance['widget_logic'];
117
+
 
 
 
 
 
 
118
  return $instance;
119
  }
120
 
162
  wp_redirect( admin_url('widgets.php') );
163
  exit;
164
  }
165
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
166
  // UPDATE OTHER WIDGET LOGIC OPTIONS
167
  // must update this to use http://codex.wordpress.org/Settings_API
168
  if ( isset($_POST['widget_logic-options-submit']) )
253
  }
254
 
255
  // added to widget functionality in 'widget_logic_expand_control' (above)
256
+ function widget_logic_extra_control( $widget, $return, $instance )
257
+ {
258
+ global $wl_options;
259
+
260
+ if ( isset( $instance['widget_logic'] ) )
261
+ {
262
+ $logic = $instance['widget_logic'];
263
+ if ( isset( $wl_options[ $widget->id ] ) )
264
+ {
265
+ unset( $wl_options[ $widget->id ] );
266
+ update_option('widget_logic', $wl_options);
267
+ }
 
 
 
 
 
 
 
 
 
 
 
 
268
  }
269
+ elseif ( !empty( $wl_options[ $widget->id ] ) )
270
+ {
271
+ $logic = stripslashes( $wl_options[ $widget->id ] );
272
+ }
273
+ else
274
+ $logic = '';
275
 
276
+ ?>
277
+ <p>
278
+ <label for="<?php echo $widget->get_field_id('widget_logic'); ?>">
279
+ <?php esc_html_e('Widget logic:','widget-logic') ?>
280
+ </label>
281
+ <textarea class="widefat" name="<?php echo $widget->get_field_name('widget_logic'); ?>" id="<?php echo $widget->get_field_id('widget_logic'); ?>"><?= esc_textarea( $logic ) ?></textarea>
282
+ </p>
283
+ <?php
284
+ return;
285
+ }
286
 
287
  // CALLED ON 'plugin_action_links' ACTION
288
  function wl_charity($links, $file)
298
 
299
 
300
  // CALLED ON 'sidebars_widgets' FILTER
301
+ function widget_logic_filter_sidebars_widgets( $sidebars_widgets )
302
  {
303
+ global $wp_reset_query_is_done, $wl_options, $wl_in_customizer;
304
+
305
+
306
+ if ( $wl_in_customizer )
307
+ return $sidebars_widgets;
308
 
309
  // reset any database queries done now that we're about to make decisions based on the context given in the WP query for the page
310
  if ( !empty( $wl_options['widget_logic-options-wp_reset_query'] ) && empty( $wp_reset_query_is_done ) )
312
  wp_reset_query();
313
  $wp_reset_query_is_done=true;
314
  }
315
+
316
  // loop through every widget in every sidebar (barring 'wp_inactive_widgets') checking WL for each one
317
  foreach($sidebars_widgets as $widget_area => $widget_list)
318
+ {
319
+ if ($widget_area=='wp_inactive_widgets' || empty($widget_list))
320
+ continue;
321
 
322
  foreach($widget_list as $pos => $widget_id)
323
+ {
324
+ if ( !preg_match( '/^(.+)-(\d+)$/', $widget_id, $m ) )
 
 
 
 
 
325
  continue;
 
 
 
 
 
326
 
327
+ $widget_class = $m[1];
328
+ $widget_i = $m[2];
329
 
330
+ $info = get_option( 'widget_'.$widget_class );
331
+ if ( empty( $info[ $widget_i ] ) )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
332
  continue;
333
+
334
+ $logic = @$info[ $widget_i ]['widget_logic'];
335
+ if ( !isset( $logic ) && isset( $wl_options[ $widget_id ] ) )
336
+ $logic = stripslashes( $wl_options[ $widget_id ] );
337
+
338
+ if ( !widget_logic_check_logic( $logic ) )
339
+ unset($sidebars_widgets[$widget_area][$pos]);
340
  }
341
  }
342
  return $sidebars_widgets;
343
  }
344
 
345
 
346
+ function widget_logic_check_logic( $logic )
347
+ {
348
+ $logic = @trim( (string)$logic );
349
+ $logic = apply_filters( "widget_logic_eval_override", $logic );
350
+
351
+ if ( is_bool( $logic ) )
352
+ return $logic;
353
+
354
+ if ( $logic === '' )
355
+ return true;
356
 
357
+ if ( stristr( $logic, "return" ) === false )
358
+ $logic = "return ( $logic );";
359
+
360
+ global $wl_options, $wl_in_customizer;
361
+
362
+ $show_errors = !empty($wl_options['widget_logic-options-show_errors']) && current_user_can('manage_options');
363
+
364
+ set_error_handler( 'widget_logic_error_handler' );
365
+
366
+ if ( $show_errors )
367
+ $save = ini_get('display_errors');
368
+ try {
369
+ if ( $show_errors )
370
+ ini_set( 'display_errors', 'On' );
371
+
372
+ $show_widget = eval($logic);
373
+
374
+ if ( $show_errors )
375
+ ini_set( 'display_errors', $save );
376
+ }
377
+ catch ( Error $e ) {
378
+ trigger_error( $e->getMessage(), E_USER_WARNING );
379
+ if ( $show_errors )
380
+ ini_set( 'display_errors', $save );
381
+
382
+ $show_widget = false;
383
+ }
384
+
385
+ restore_error_handler();
386
+
387
+ return $show_widget;
388
+ }
389
 
390
+ function widget_logic_error_handler( $errno , $errstr )
391
+ {
392
+ echo 'Invalid Widget Logic: '.$errstr;
393
+
394
+ return true;
395
+ }
396
+
397
+ // If 'widget_logic-options-filter' is selected the widget_content filter is implemented...
398
 
399
+ function widget_logic_customizer_display_callback( $instance, $widget, $args )
400
+ {
401
+ if ( empty( $instance['widget_logic'] ) )
402
+ return $instance;
403
+
404
+ global $wl_options;
405
+
406
+ $show_errors = !empty($wl_options['widget_logic-options-show_errors']) && current_user_can('manage_options');
407
+
408
+ ob_start();
409
+ $show_widget = widget_logic_check_logic( $instance['widget_logic'] );
410
+ $error = ob_get_clean();
411
+
412
+ if ( $show_errors && $error ) :
413
+ ?><script>jQuery(function($){$('#<?=$widget->id?>').append( $('<p class="widget-logic-error">').html(<?=json_encode($error)?>) );})</script><?php
414
+ endif;
415
+ if ( !$show_widget ):
416
+ ?><script>jQuery(function($){$('#<?=$widget->id?>').children().not('.widget-logic-error').css('opacity', '0.2');})</script><?php
417
+ endif;
418
+
419
+ return $instance;
420
+ }
421
 
422
  // CALLED ON 'dynamic_sidebar_params' FILTER - this is called during 'dynamic_sidebar' just before each callback is run
423
  // swap out the original call back and replace it with our own