Widget Logic - Version 5.8.2

Version Description

The code has been adapted to work on the servers with restricted <?=

Fixed support for the wp_register_sidebar_widget widgets.

Some content was prepared for translation.

Download this release

Release Info

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

Code changes from version 5.8.1 to 5.8.2

Files changed (2) hide show
  1. readme.txt +9 -2
  2. widget_logic.php +148 -49
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.5
7
- Stable tag: 5.8.1
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,13 @@ Tighten up your definitions with PHPs 'logical AND' &&, for example:
119
 
120
  == Changelog ==
121
 
 
 
 
 
 
 
 
122
  = 5.8.1 =
123
  Fixed the issue of displaying errors under certain conditions.
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.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
 
120
  == Changelog ==
121
 
122
+ = 5.8.2 =
123
+ The code has been adapted to work on the servers with restricted <?=
124
+
125
+ Fixed support for the wp_register_sidebar_widget widgets.
126
+
127
+ Some content was prepared for translation.
128
+
129
  = 5.8.1 =
130
  Fixed the issue of displaying errors under certain conditions.
131
 
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.1
7
  Author: wpchefgadget, alanft
8
 
9
  Text Domain: widget-logic
@@ -36,7 +36,8 @@ 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
  if ( is_admin() )
41
  {
42
  if ( get_option('widget_logic_version') != WIDGET_LOGIC_VERSION )
@@ -56,7 +57,7 @@ function widget_logic_init()
56
  //enqueue admin/js/updates.js
57
  }
58
  }
59
-
60
  }
61
 
62
  if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) )
@@ -64,7 +65,7 @@ if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) )
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');
@@ -72,6 +73,8 @@ if (is_admin())
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
  {
@@ -96,14 +99,17 @@ 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()
105
  {
106
- add_filter( 'sidebars_widgets', 'widget_logic_filter_sidebars_widgets', 10); // actually remove the widgets from the front end depending on widget logic provided
 
107
  }
108
  // wp-admin/widgets.php explicitly checks current_user_can('edit_theme_options')
109
  // which is enough security, I believe. If you think otherwise please contact me
@@ -252,42 +258,91 @@ function widget_logic_options_control()
252
  <?php
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)
289
  { if ($file == plugin_basename(__FILE__))
290
- array_push($links, '<a href="http://www.justgiving.com/widgetlogic_cancerresearchuk/">Charity Donation</a>');
291
  return $links;
292
  }
293
 
@@ -295,14 +350,71 @@ function wl_charity($links, $file)
295
 
296
  // FRONT END FUNCTIONS...
297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
 
@@ -321,19 +433,7 @@ function widget_logic_filter_sidebars_widgets( $sidebars_widgets )
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]);
@@ -357,9 +457,6 @@ function widget_logic_check_logic( $logic )
357
  if ( stristr( $logic, "return" ) === false )
358
  $logic = "return ( $logic );";
359
 
360
- global $wl_options, $wl_in_customizer;
361
-
362
-
363
  set_error_handler( 'widget_logic_error_handler' );
364
 
365
  try {
@@ -387,29 +484,31 @@ function widget_logic_error_handler( $errno , $errstr )
387
  return true;
388
  }
389
 
390
- // If 'widget_logic-options-filter' is selected the widget_content filter is implemented...
 
 
 
391
 
392
- function widget_logic_customizer_display_callback( $instance, $widget, $args )
393
  {
394
- if ( empty( $instance['widget_logic'] ) )
395
- return $instance;
396
 
 
 
397
  global $wl_options;
398
-
399
  $show_errors = !empty($wl_options['widget_logic-options-show_errors']) && current_user_can('manage_options');
400
 
401
  ob_start();
402
- $show_widget = widget_logic_check_logic( $instance['widget_logic'] );
403
  $error = ob_get_clean();
404
 
405
  if ( $show_errors && $error ) :
406
- ?><script>jQuery(function($){$('#<?=$widget->id?>').append( $('<p class="widget-logic-error">').html(<?=json_encode($error)?>) );})</script><?php
407
  endif;
408
  if ( !$show_widget ):
409
- ?><script>jQuery(function($){$('#<?=$widget->id?>').children().not('.widget-logic-error').css('opacity', '0.2');})</script><?php
410
  endif;
411
-
412
- return $instance;
413
  }
414
 
415
  // CALLED ON 'dynamic_sidebar_params' FILTER - this is called during 'dynamic_sidebar' just before each callback is run
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
  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 )
57
  //enqueue admin/js/updates.js
58
  }
59
  }
60
+ */
61
  }
62
 
63
  if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) )
65
 
66
  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');
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
  {
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
  }
106
  add_action( 'customize_preview_init', 'widget_logic_in_customizer' );
107
 
108
 
109
  function widget_logic_sidebars_widgets_filter_add()
110
  {
111
+ // actually remove the widgets from the front end depending on widget logic provided
112
+ add_filter( 'sidebars_widgets', 'widget_logic_filter_sidebars_widgets', 10);
113
  }
114
  // wp-admin/widgets.php explicitly checks current_user_can('edit_theme_options')
115
  // which is enough security, I believe. If you think otherwise please contact me
258
  <?php
259
  }
260
 
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';
285
  }
286
  }
287
+ }
288
+
289
+ function widget_logic_in_widget_form( $widget, $return, $instance )
290
+ {
291
+ $logic = isset( $instance['widget_logic'] ) ? $instance['widget_logic'] : widget_logic_by_id( $widget->id );
 
292
 
293
  ?>
294
  <p>
295
  <label for="<?php echo $widget->get_field_id('widget_logic'); ?>">
296
  <?php esc_html_e('Widget logic:','widget-logic') ?>
297
  </label>
298
+ <textarea class="widefat" name="<?php echo $widget->get_field_name('widget_logic'); ?>" id="<?php echo $widget->get_field_id('widget_logic'); ?>"><?php echo esc_textarea( $logic ) ?></textarea>
299
  </p>
300
  <?php
301
  return;
302
  }
303
 
304
+ // added to widget functionality in 'widget_logic_expand_control' (above)
305
+ 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'] );
319
+ widget_logic_save( $widget_id, $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
+ ?>
327
+ <p>
328
+ <label for="<?php echo $input_id ?>">
329
+ <?php esc_html_e('Widget logic:','widget-logic') ?>
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 ?>
337
+ </p>
338
+ <?php
339
+ return true;
340
+ }
341
+
342
  // CALLED ON 'plugin_action_links' ACTION
343
  function wl_charity($links, $file)
344
  { if ($file == plugin_basename(__FILE__))
345
+ array_push($links, '<a href="http://www.justgiving.com/widgetlogic_cancerresearchuk/">'.esc_html__('Charity Donation', 'widget-logic').'</a>');
346
  return $links;
347
  }
348
 
350
 
351
  // FRONT END FUNCTIONS...
352
 
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
+ }
405
+ else
406
+ {
407
+ $info = (array)get_option( 'widget_'.$widget_id, array() );
408
+ $info['widget_logic'] = $logic;
409
+ update_option( 'widget_'.$widget_id, $info );
410
+ }
411
+ }
412
 
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
 
433
 
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]);
457
  if ( stristr( $logic, "return" ) === false )
458
  $logic = "return ( $logic );";
459
 
 
 
 
460
  set_error_handler( 'widget_logic_error_handler' );
461
 
462
  try {
484
  return true;
485
  }
486
 
487
+ function widget_logic_customizer_dynamic_sidebar_callback( $widget )
488
+ {
489
+ widget_logic_customizer_display( $widget['id'] );
490
+ }
491
 
492
+ 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;
509
  if ( !$show_widget ):
510
+ ?><script>jQuery(function($){$('#<?php echo $widget_id?>').children().not('.widget-logic-error').css('opacity', '0.2');})</script><?php
511
  endif;
 
 
512
  }
513
 
514
  // CALLED ON 'dynamic_sidebar_params' FILTER - this is called during 'dynamic_sidebar' just before each callback is run