Widget Logic - Version 5.9.0

Version Description

wp_reset_query works better under certain conditions.

Download this release

Release Info

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

Code changes from version 5.8.2 to 5.9.0

Files changed (2) hide show
  1. readme.txt +6 -2
  2. widget_logic.php +48 -50
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.8
7
- Stable tag: 5.8.2
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,10 @@ Tighten up your definitions with PHPs 'logical AND' &&, for example:
119
 
120
  == Changelog ==
121
 
 
 
 
 
122
  = 5.8.2 =
123
  The code has been adapted to work on the servers with restricted <?=
124
 
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.9
7
+ Stable tag: 5.9.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.9.0 =
123
+
124
+ wp_reset_query works better under certain conditions.
125
+
126
  = 5.8.2 =
127
  The code has been adapted to work on the servers with restricted <?=
128
 
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.8.2
7
  Author: wpchefgadget, alanft
8
 
9
  Text Domain: widget-logic
@@ -36,13 +36,13 @@ add_action( 'init', 'widget_logic_init' );
36
  function widget_logic_init()
37
  {
38
  load_plugin_textdomain( 'widget-logic', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
39
-
40
  /*
41
  if ( is_admin() )
42
  {
43
  if ( get_option('widget_logic_version') != WIDGET_LOGIC_VERSION )
44
  widget_logic_activate();
45
-
46
  global $wp_version;
47
  if ( version_compare( $wp_version, '4.2', '>=' ) && !file_exists(WP_PLUGIN_DIR.'/limit-login-attempts-reloaded') && current_user_can('install_plugins') )
48
  {
@@ -67,18 +67,18 @@ if (is_admin())
67
  {
68
  add_filter( 'in_widget_form', 'widget_logic_in_widget_form', 10, 3 );
69
  add_filter( 'widget_update_callback', 'widget_logic_update_callback', 10, 4);
70
-
71
  add_action( 'sidebar_admin_setup', 'widget_logic_expand_control');
72
  // before any HTML output save widget changes and add controls to each widget on the widget admin page
73
  add_action( 'sidebar_admin_page', 'widget_logic_options_control');
74
  // add Widget Logic specific options on the widget admin page
75
  add_filter( 'plugin_action_links', 'wl_charity', 10, 2);// add my justgiving page link to the plugin admin page
76
-
77
  add_action( 'widgets_init', 'widget_logic_add_controls', 999 );
78
  }
79
  else
80
  {
81
- $loadpoint = (string)@$wl_options['widget_logic-options-load_point'];
82
  if ( 'plugins_loaded' == $loadpoint )
83
  widget_logic_sidebars_widgets_filter_add();
84
  else
@@ -99,7 +99,7 @@ function widget_logic_in_customizer()
99
  {
100
  global $wl_in_customizer;
101
  $wl_in_customizer = true;
102
-
103
  //add_filter( 'widget_display_callback', 'widget_logic_customizer_display_callback', 10, 3 );
104
  add_action( 'dynamic_sidebar', 'widget_logic_customizer_dynamic_sidebar_callback' );
105
  }
@@ -120,7 +120,7 @@ function widget_logic_update_callback( $instance, $new_instance, $old_instance,
120
  {
121
  if ( isset( $new_instance['widget_logic'] ) )
122
  $instance['widget_logic'] = $new_instance['widget_logic'];
123
-
124
  return $instance;
125
  }
126
 
@@ -168,7 +168,7 @@ function widget_logic_expand_control()
168
  wp_redirect( admin_url('widgets.php') );
169
  exit;
170
  }
171
-
172
  // UPDATE OTHER WIDGET LOGIC OPTIONS
173
  // must update this to use http://codex.wordpress.org/Settings_API
174
  if ( isset($_POST['widget_logic-options-submit']) )
@@ -261,24 +261,24 @@ function widget_logic_options_control()
261
  function widget_logic_add_controls()
262
  {
263
  global $wp_registered_widget_controls, $wp_registered_widgets, $wp_registered_widget_updates;
264
-
265
  foreach ( $wp_registered_widgets as $id => $widget )
266
  {
267
  if ( preg_match( '/^(.+)-(\d+)$/', $id) )
268
  continue;
269
-
270
  if ( !isset( $wp_registered_widget_controls[ $id ] ) )
271
  {
272
  wp_register_widget_control( $id, $id, 'widget_logic_extra_control', array(), $id, null );
273
  continue;
274
  }
275
-
276
  if ( @$wp_registered_widget_controls[ $id ]['callback'] != 'widget_logic_extra_control' )
277
  {
278
  $wp_registered_widget_controls[$id]['params'][] = $id;
279
  $wp_registered_widget_controls[$id]['params'][] = @$wp_registered_widget_controls[$id]['callback'];
280
  $wp_registered_widget_controls[$id]['callback'] = 'widget_logic_extra_control';
281
-
282
  $wp_registered_widget_updates[$id]['params'][] = $id;
283
  $wp_registered_widget_updates[$id]['params'][] = @$wp_registered_widget_updates[$id]['callback'];
284
  $wp_registered_widget_updates[$id]['callback'] = 'widget_logic_extra_control';
@@ -306,13 +306,13 @@ function widget_logic_extra_control()
306
  {
307
  global $wp_customize;
308
  $args = func_get_args();
309
-
310
  $callback = array_pop( $args );
311
  $widget_id = array_pop( $args );
312
-
313
  if ( is_callable($callback) )
314
  call_user_func_array( $callback, $args );
315
-
316
  if ( isset( $_POST["widget-$widget_id"]['widget_logic'] ) )
317
  {
318
  $logic = stripslashes( $_POST["widget-$widget_id"]['widget_logic'] );
@@ -320,7 +320,7 @@ function widget_logic_extra_control()
320
  }
321
  else
322
  $logic = widget_logic_by_id( $widget_id );
323
-
324
  $input_id = "widget-$widget_id-widget_logic";
325
  $input_name = "widget-{$widget_id}[widget_logic]";
326
  ?>
@@ -330,7 +330,8 @@ function widget_logic_extra_control()
330
  </label>
331
  <?php if ( !empty($wp_customize) && $wp_customize->is_preview() ): ?>
332
  <textarea class="widefat" id="<?php echo $input_id ?>" readonly><?php echo esc_textarea( $logic ) ?></textarea>
333
- <p class="description"><?php printf( esc_html__('This is a "wp register sidebar widget" and is different from regular widgets. Hence it can only be edited from the %s page.', 'widget-logic'), sprintf( '<a href="%s" target="_blank">%s</a>', esc_attr(admin_url('widgets.php')), __('widgets') ) ) ?></p>
 
334
  <?php else: ?>
335
  <textarea class="widefat" name="<?php echo $input_name ?>" id="<?php echo $input_id ?>"><?php echo esc_textarea( $logic ) ?></textarea>
336
  <?php endif ?>
@@ -353,52 +354,52 @@ function wl_charity($links, $file)
353
  function widget_logic_by_id( $widget_id )
354
  {
355
  global $wl_options;
356
-
357
  if ( preg_match( '/^(.+)-(\d+)$/', $widget_id, $m ) )
358
  {
359
  $widget_class = $m[1];
360
  $widget_i = $m[2];
361
-
362
  $info = get_option( 'widget_'.$widget_class );
363
  if ( empty( $info[ $widget_i ] ) )
364
  return '';
365
-
366
  $info = $info[ $widget_i ];
367
  }
368
  else
369
  $info = (array)get_option( 'widget_'.$widget_id, array() );
370
-
371
  if ( isset( $info['widget_logic'] ) )
372
  $logic = $info['widget_logic'];
373
-
374
  elseif ( isset( $wl_options[ $widget_id ] ) )
375
  {
376
  $logic = stripslashes( $wl_options[ $widget_id ] );
377
  widget_logic_save( $widget_id, $logic );
378
-
379
  unset( $wl_options[ $widget_id ] );
380
  update_option( 'widget_logic', $wl_options );
381
  }
382
-
383
  else
384
  $logic = '';
385
-
386
  return $logic;
387
  }
388
 
389
  function widget_logic_save( $widget_id, $logic )
390
  {
391
  global $wl_options;
392
-
393
  if ( preg_match( '/^(.+)-(\d+)$/', $widget_id, $m ) )
394
  {
395
  $widget_class = $m[1];
396
  $widget_i = $m[2];
397
-
398
  $info = get_option( 'widget_'.$widget_class );
399
  if ( !is_array( $info[ $widget_i ] ) )
400
  $info[ $widget_i ] = array();
401
-
402
  $info[ $widget_i ]['widget_logic'] = $logic;
403
  update_option( 'widget_'.$widget_class, $info );
404
  }
@@ -413,18 +414,15 @@ function widget_logic_save( $widget_id, $logic )
413
  // CALLED ON 'sidebars_widgets' FILTER
414
  function widget_logic_filter_sidebars_widgets( $sidebars_widgets )
415
  {
416
- global $wp_reset_query_is_done, $wl_options, $wl_in_customizer;
417
-
418
  if ( $wl_in_customizer )
419
  return $sidebars_widgets;
420
 
421
  // 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
422
- if ( !empty( $wl_options['widget_logic-options-wp_reset_query'] ) && empty( $wp_reset_query_is_done ) )
423
- {
424
  wp_reset_query();
425
- $wp_reset_query_is_done=true;
426
- }
427
-
428
  // loop through every widget in every sidebar (barring 'wp_inactive_widgets') checking WL for each one
429
  foreach($sidebars_widgets as $widget_area => $widget_list)
430
  {
@@ -434,7 +432,7 @@ function widget_logic_filter_sidebars_widgets( $sidebars_widgets )
434
  foreach($widget_list as $pos => $widget_id)
435
  {
436
  $logic = widget_logic_by_id( $widget_id );
437
-
438
  if ( !widget_logic_check_logic( $logic ) )
439
  unset($sidebars_widgets[$widget_area][$pos]);
440
  }
@@ -447,29 +445,29 @@ function widget_logic_check_logic( $logic )
447
  {
448
  $logic = @trim( (string)$logic );
449
  $logic = apply_filters( "widget_logic_eval_override", $logic );
450
-
451
  if ( is_bool( $logic ) )
452
  return $logic;
453
-
454
  if ( $logic === '' )
455
  return true;
456
 
457
  if ( stristr( $logic, "return" ) === false )
458
  $logic = "return ( $logic );";
459
-
460
  set_error_handler( 'widget_logic_error_handler' );
461
-
462
  try {
463
  $show_widget = eval($logic);
464
  }
465
  catch ( Error $e ) {
466
  trigger_error( $e->getMessage(), E_USER_WARNING );
467
-
468
  $show_widget = false;
469
  }
470
-
471
  restore_error_handler();
472
-
473
  return $show_widget;
474
  }
475
 
@@ -477,10 +475,10 @@ function widget_logic_error_handler( $errno , $errstr )
477
  {
478
  global $wl_options;
479
  $show_errors = !empty($wl_options['widget_logic-options-show_errors']) && current_user_can('manage_options');
480
-
481
  if ( $show_errors )
482
  echo 'Invalid Widget Logic: '.$errstr;
483
-
484
  return true;
485
  }
486
 
@@ -493,16 +491,16 @@ function widget_logic_customizer_display( $widget_id )
493
  {
494
  if ( !preg_match( '/^(.+)-(\d+)$/', $widget_id) )
495
  return;
496
-
497
  $logic = widget_logic_by_id( $widget_id );
498
 
499
  global $wl_options;
500
  $show_errors = !empty($wl_options['widget_logic-options-show_errors']) && current_user_can('manage_options');
501
-
502
  ob_start();
503
  $show_widget = widget_logic_check_logic( $logic );
504
  $error = ob_get_clean();
505
-
506
  if ( $show_errors && $error ) :
507
  ?><script>jQuery(function($){$('#<?php echo $widget_id?>').append( $('<p class="widget-logic-error">').html(<?php echo json_encode($error)?>) );})</script><?php
508
  endif;
@@ -524,7 +522,7 @@ function widget_logic_widget_display_callback($params)
524
 
525
  // the redirection comes here
526
  function widget_logic_redirected_callback()
527
- { global $wp_registered_widgets, $wp_reset_query_is_done;
528
 
529
  // replace the original callback data
530
  $params=func_get_args();
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.9.0
7
  Author: wpchefgadget, alanft
8
 
9
  Text Domain: widget-logic
36
  function widget_logic_init()
37
  {
38
  load_plugin_textdomain( 'widget-logic', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
39
+
40
  /*
41
  if ( is_admin() )
42
  {
43
  if ( get_option('widget_logic_version') != WIDGET_LOGIC_VERSION )
44
  widget_logic_activate();
45
+
46
  global $wp_version;
47
  if ( version_compare( $wp_version, '4.2', '>=' ) && !file_exists(WP_PLUGIN_DIR.'/limit-login-attempts-reloaded') && current_user_can('install_plugins') )
48
  {
67
  {
68
  add_filter( 'in_widget_form', 'widget_logic_in_widget_form', 10, 3 );
69
  add_filter( 'widget_update_callback', 'widget_logic_update_callback', 10, 4);
70
+
71
  add_action( 'sidebar_admin_setup', 'widget_logic_expand_control');
72
  // before any HTML output save widget changes and add controls to each widget on the widget admin page
73
  add_action( 'sidebar_admin_page', 'widget_logic_options_control');
74
  // add Widget Logic specific options on the widget admin page
75
  add_filter( 'plugin_action_links', 'wl_charity', 10, 2);// add my justgiving page link to the plugin admin page
76
+
77
  add_action( 'widgets_init', 'widget_logic_add_controls', 999 );
78
  }
79
  else
80
  {
81
+ $loadpoint = isset($wl_options['widget_logic-options-load_point']) ? (string)@$wl_options['widget_logic-options-load_point'] : '';
82
  if ( 'plugins_loaded' == $loadpoint )
83
  widget_logic_sidebars_widgets_filter_add();
84
  else
99
  {
100
  global $wl_in_customizer;
101
  $wl_in_customizer = true;
102
+
103
  //add_filter( 'widget_display_callback', 'widget_logic_customizer_display_callback', 10, 3 );
104
  add_action( 'dynamic_sidebar', 'widget_logic_customizer_dynamic_sidebar_callback' );
105
  }
120
  {
121
  if ( isset( $new_instance['widget_logic'] ) )
122
  $instance['widget_logic'] = $new_instance['widget_logic'];
123
+
124
  return $instance;
125
  }
126
 
168
  wp_redirect( admin_url('widgets.php') );
169
  exit;
170
  }
171
+
172
  // UPDATE OTHER WIDGET LOGIC OPTIONS
173
  // must update this to use http://codex.wordpress.org/Settings_API
174
  if ( isset($_POST['widget_logic-options-submit']) )
261
  function widget_logic_add_controls()
262
  {
263
  global $wp_registered_widget_controls, $wp_registered_widgets, $wp_registered_widget_updates;
264
+
265
  foreach ( $wp_registered_widgets as $id => $widget )
266
  {
267
  if ( preg_match( '/^(.+)-(\d+)$/', $id) )
268
  continue;
269
+
270
  if ( !isset( $wp_registered_widget_controls[ $id ] ) )
271
  {
272
  wp_register_widget_control( $id, $id, 'widget_logic_extra_control', array(), $id, null );
273
  continue;
274
  }
275
+
276
  if ( @$wp_registered_widget_controls[ $id ]['callback'] != 'widget_logic_extra_control' )
277
  {
278
  $wp_registered_widget_controls[$id]['params'][] = $id;
279
  $wp_registered_widget_controls[$id]['params'][] = @$wp_registered_widget_controls[$id]['callback'];
280
  $wp_registered_widget_controls[$id]['callback'] = 'widget_logic_extra_control';
281
+
282
  $wp_registered_widget_updates[$id]['params'][] = $id;
283
  $wp_registered_widget_updates[$id]['params'][] = @$wp_registered_widget_updates[$id]['callback'];
284
  $wp_registered_widget_updates[$id]['callback'] = 'widget_logic_extra_control';
306
  {
307
  global $wp_customize;
308
  $args = func_get_args();
309
+
310
  $callback = array_pop( $args );
311
  $widget_id = array_pop( $args );
312
+
313
  if ( is_callable($callback) )
314
  call_user_func_array( $callback, $args );
315
+
316
  if ( isset( $_POST["widget-$widget_id"]['widget_logic'] ) )
317
  {
318
  $logic = stripslashes( $_POST["widget-$widget_id"]['widget_logic'] );
320
  }
321
  else
322
  $logic = widget_logic_by_id( $widget_id );
323
+
324
  $input_id = "widget-$widget_id-widget_logic";
325
  $input_name = "widget-{$widget_id}[widget_logic]";
326
  ?>
330
  </label>
331
  <?php if ( !empty($wp_customize) && $wp_customize->is_preview() ): ?>
332
  <textarea class="widefat" id="<?php echo $input_id ?>" readonly><?php echo esc_textarea( $logic ) ?></textarea>
333
+ <br>
334
+ <span class="description"><?php printf( esc_html__('This is a "wp register sidebar widget" and is different from regular widgets. Hence it can only be edited from the %s page.', 'widget-logic'), sprintf( '<a href="%s" target="_blank">%s</a>', esc_attr(admin_url('widgets.php')), __('widgets') ) ) ?></span>
335
  <?php else: ?>
336
  <textarea class="widefat" name="<?php echo $input_name ?>" id="<?php echo $input_id ?>"><?php echo esc_textarea( $logic ) ?></textarea>
337
  <?php endif ?>
354
  function widget_logic_by_id( $widget_id )
355
  {
356
  global $wl_options;
357
+
358
  if ( preg_match( '/^(.+)-(\d+)$/', $widget_id, $m ) )
359
  {
360
  $widget_class = $m[1];
361
  $widget_i = $m[2];
362
+
363
  $info = get_option( 'widget_'.$widget_class );
364
  if ( empty( $info[ $widget_i ] ) )
365
  return '';
366
+
367
  $info = $info[ $widget_i ];
368
  }
369
  else
370
  $info = (array)get_option( 'widget_'.$widget_id, array() );
371
+
372
  if ( isset( $info['widget_logic'] ) )
373
  $logic = $info['widget_logic'];
374
+
375
  elseif ( isset( $wl_options[ $widget_id ] ) )
376
  {
377
  $logic = stripslashes( $wl_options[ $widget_id ] );
378
  widget_logic_save( $widget_id, $logic );
379
+
380
  unset( $wl_options[ $widget_id ] );
381
  update_option( 'widget_logic', $wl_options );
382
  }
383
+
384
  else
385
  $logic = '';
386
+
387
  return $logic;
388
  }
389
 
390
  function widget_logic_save( $widget_id, $logic )
391
  {
392
  global $wl_options;
393
+
394
  if ( preg_match( '/^(.+)-(\d+)$/', $widget_id, $m ) )
395
  {
396
  $widget_class = $m[1];
397
  $widget_i = $m[2];
398
+
399
  $info = get_option( 'widget_'.$widget_class );
400
  if ( !is_array( $info[ $widget_i ] ) )
401
  $info[ $widget_i ] = array();
402
+
403
  $info[ $widget_i ]['widget_logic'] = $logic;
404
  update_option( 'widget_'.$widget_class, $info );
405
  }
414
  // CALLED ON 'sidebars_widgets' FILTER
415
  function widget_logic_filter_sidebars_widgets( $sidebars_widgets )
416
  {
417
+ global $wl_options, $wl_in_customizer;
418
+
419
  if ( $wl_in_customizer )
420
  return $sidebars_widgets;
421
 
422
  // 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
423
+ if ( !empty( $wl_options['widget_logic-options-wp_reset_query'] ) )
 
424
  wp_reset_query();
425
+
 
 
426
  // loop through every widget in every sidebar (barring 'wp_inactive_widgets') checking WL for each one
427
  foreach($sidebars_widgets as $widget_area => $widget_list)
428
  {
432
  foreach($widget_list as $pos => $widget_id)
433
  {
434
  $logic = widget_logic_by_id( $widget_id );
435
+
436
  if ( !widget_logic_check_logic( $logic ) )
437
  unset($sidebars_widgets[$widget_area][$pos]);
438
  }
445
  {
446
  $logic = @trim( (string)$logic );
447
  $logic = apply_filters( "widget_logic_eval_override", $logic );
448
+
449
  if ( is_bool( $logic ) )
450
  return $logic;
451
+
452
  if ( $logic === '' )
453
  return true;
454
 
455
  if ( stristr( $logic, "return" ) === false )
456
  $logic = "return ( $logic );";
457
+
458
  set_error_handler( 'widget_logic_error_handler' );
459
+
460
  try {
461
  $show_widget = eval($logic);
462
  }
463
  catch ( Error $e ) {
464
  trigger_error( $e->getMessage(), E_USER_WARNING );
465
+
466
  $show_widget = false;
467
  }
468
+
469
  restore_error_handler();
470
+
471
  return $show_widget;
472
  }
473
 
475
  {
476
  global $wl_options;
477
  $show_errors = !empty($wl_options['widget_logic-options-show_errors']) && current_user_can('manage_options');
478
+
479
  if ( $show_errors )
480
  echo 'Invalid Widget Logic: '.$errstr;
481
+
482
  return true;
483
  }
484
 
491
  {
492
  if ( !preg_match( '/^(.+)-(\d+)$/', $widget_id) )
493
  return;
494
+
495
  $logic = widget_logic_by_id( $widget_id );
496
 
497
  global $wl_options;
498
  $show_errors = !empty($wl_options['widget_logic-options-show_errors']) && current_user_can('manage_options');
499
+
500
  ob_start();
501
  $show_widget = widget_logic_check_logic( $logic );
502
  $error = ob_get_clean();
503
+
504
  if ( $show_errors && $error ) :
505
  ?><script>jQuery(function($){$('#<?php echo $widget_id?>').append( $('<p class="widget-logic-error">').html(<?php echo json_encode($error)?>) );})</script><?php
506
  endif;
522
 
523
  // the redirection comes here
524
  function widget_logic_redirected_callback()
525
+ { global $wp_registered_widgets;
526
 
527
  // replace the original callback data
528
  $params=func_get_args();