Widget Logic - Version 0.42

Version Description

Download this release

Release Info

Developer alanft
Plugin Icon 128x128 Widget Logic
Version 0.42
Comparing to
See all releases

Code changes from version 0.4 to 0.42

Files changed (3) hide show
  1. readme.txt +16 -17
  2. screenshot-1.png +0 -0
  3. widget_logic.php +45 -35
readme.txt CHANGED
@@ -1,24 +1,29 @@
1
  === Widget Logic ===
2
  Contributors: alanft
3
  Tags: widget, admin, conditional tags, filter
4
- Requires at least: 2.1
5
- Tested up to: 2.5
6
- Stable tag: 0.4
7
 
8
  Widget Logic lets you control when widgets appear. Add WP's conditional tags in the normal widget admin. It also adds a 'widget_content' filter.
9
 
10
  == Description ==
11
- This plugin gives every widget (even widgets lacking controls) an extra control called "Widget logic".
12
 
13
- This text field allows you to specify any WP conditional tags logic to set when the widget appears. Use any standard [Conditional Tags](http://codex.wordpress.org/Conditional_Tags) and even combine them.
14
 
15
  There is also an option to add a wordpress 'widget_content' filter for you to tweak standard widgets to suit your theme.
16
 
17
  = Version HIstory =
 
 
18
  0.4 - Brings WP 2.5 compatibility. I am trying to make it back compatible. If you have trouble using WL with WP 2.1--2.3 let me know the issue.
19
 
20
  0.31 - Last WP 2.3 only version
21
 
 
 
 
22
  == Installation ==
23
 
24
  1. Upload `widget-logic.php` to the `/wp-content/plugins/` directory
@@ -31,17 +36,11 @@ There is also an option to add a wordpress 'widget_content' filter for you to tw
31
 
32
  The logic text on one of your widgets may be invalid PHP.
33
 
34
- = The 'Widget Logic' field is missing or appears elsewhere =
35
-
36
- The plugin uses a tiny bit of javascript* to get the field into position on the admin page. As the widget admin uses JS, I feel this is no imposition. However it might mean that your browser dislikes the DOM tricks that the JS code adopts.
37
-
38
- I've tested it looks OK on Safari, Firefox and even PC IE6. But let me know what browser you are using and I'll see what I can do.
39
-
40
- *As of version 0.4 this is no longer the case.
41
 
42
  == Screenshots ==
43
 
44
- 1. The 'Widget logic' field at work in a widget I use. (Must update the screenshot to a 2.5 version)
45
  2. The 'widget_content' filter option is at the foot of the widget admin page. (Off by default.)
46
 
47
  == Writing Logic Code ==
@@ -54,7 +53,7 @@ Examples:
54
 
55
  * is\_home()
56
  * is\_category(5)
57
- * is\_home() || is\_category(5)
58
  * is\_page('about')
59
  * $x=(1==1)?true:false; return ( !is_home() && $x);
60
 
@@ -62,7 +61,7 @@ Note the use of ';' where there is an explicit 'return'.
62
 
63
  == The 'widget_content' filter ==
64
 
65
- This now needs to be explicitly activated on the widget admin page. To filter widget contents in your themes functions, or in other plugins, use:
66
 
67
  `add_filter('widget_content', 'your_filter_function', [priority], 2);`
68
 
@@ -79,7 +78,7 @@ This adds the widget_id to the foot of every widget:
79
  `function reveal_widget_id($content='', $widget_id='')
80
  { return $content."id=".$widget_id; }`
81
 
82
- I added this filter in order to render all widget titles with the excellent [ttftext plugin](http://templature.com/2007/10/18/ttftitles-wordpress-plugin/) like this:
83
 
84
  `function ttftext_widget_title($content='', $widget_id='')
85
  { preg_match("/<h2[^>]*>([^<]+)/",$content, $matches))
@@ -89,7 +88,7 @@ I added this filter in order to render all widget titles with the excellent [ttf
89
  return $content;
90
  }`
91
 
92
- I add a 'all comments' RSS link to the [Brian's Latest Comments Widget](http://www.4null4.de/142/sidebar-widget-brians-latest-comments/) with this:
93
 
94
  `function blc_add_rss_feed($content='', $widget_id='')
95
  { if ($widget_id=='brians-latest-comments')
1
  === Widget Logic ===
2
  Contributors: alanft
3
  Tags: widget, admin, conditional tags, filter
4
+ Requires at least: 2.5
5
+ Tested up to: 2.5.1
6
+ Stable tag: 0.42
7
 
8
  Widget Logic lets you control when widgets appear. Add WP's conditional tags in the normal widget admin. It also adds a 'widget_content' filter.
9
 
10
  == Description ==
11
+ This plugin gives every widget an extra control field called "Widget logic" that lets you control when the widget appears.
12
 
13
+ The text field lets you use WP's [Conditional Tags](http://codex.wordpress.org/Conditional_Tags), or any general PHP code.
14
 
15
  There is also an option to add a wordpress 'widget_content' filter for you to tweak standard widgets to suit your theme.
16
 
17
  = Version HIstory =
18
+ 0.42 - WP 2.5+ only now. WP's widget admin has changed so much and I was getting tied up in knots trying to make it work with them both.
19
+
20
  0.4 - Brings WP 2.5 compatibility. I am trying to make it back compatible. If you have trouble using WL with WP 2.1--2.3 let me know the issue.
21
 
22
  0.31 - Last WP 2.3 only version
23
 
24
+ = Thanks To =
25
+ Kjetil Flekkoy for reporting and helping to diagnose errors in the 0.4 version
26
+
27
  == Installation ==
28
 
29
  1. Upload `widget-logic.php` to the `/wp-content/plugins/` directory
36
 
37
  The logic text on one of your widgets may be invalid PHP.
38
 
39
+ (Need more FAQs!)
 
 
 
 
 
 
40
 
41
  == Screenshots ==
42
 
43
+ 1. The 'Widget logic' field at work in a widget I use.
44
  2. The 'widget_content' filter option is at the foot of the widget admin page. (Off by default.)
45
 
46
  == Writing Logic Code ==
53
 
54
  * is\_home()
55
  * is\_category(5)
56
+ * is\_home() || is\_category('baked-goods')
57
  * is\_page('about')
58
  * $x=(1==1)?true:false; return ( !is_home() && $x);
59
 
61
 
62
  == The 'widget_content' filter ==
63
 
64
+ Once this is active (by the option tickbox at the foot of the widget admin page) you can modify the text displayed by any widget. In your theme's functions.php file use:
65
 
66
  `add_filter('widget_content', 'your_filter_function', [priority], 2);`
67
 
78
  `function reveal_widget_id($content='', $widget_id='')
79
  { return $content."id=".$widget_id; }`
80
 
81
+ I was motivated to make this filter in order to render all widget titles with the excellent [ttftext plugin](http://templature.com/2007/10/18/ttftitles-wordpress-plugin/) like this:
82
 
83
  `function ttftext_widget_title($content='', $widget_id='')
84
  { preg_match("/<h2[^>]*>([^<]+)/",$content, $matches))
88
  return $content;
89
  }`
90
 
91
+ I add an 'all comments' RSS link to the [Brian's Latest Comments Widget](http://www.4null4.de/142/sidebar-widget-brians-latest-comments/) with this:
92
 
93
  `function blc_add_rss_feed($content='', $widget_id='')
94
  { if ($widget_id=='brians-latest-comments')
screenshot-1.png CHANGED
Binary file
widget_logic.php CHANGED
@@ -4,40 +4,53 @@ Plugin Name: Widget Logic
4
  Plugin URI: http://freakytrigger.co.uk/wordpress-setup/
5
  Description: Control widgets with WP's conditional tags is_home etc
6
  Author: Alan Trewartha
7
- Version: 0.40
8
  Author URI: http://freakytrigger.co.uk/author/alan/
9
  */
10
 
11
 
12
 
13
- // re-wire the registered control functions to go via widget_logic_extra_control
14
  add_action( 'sidebar_admin_setup', 'widget_logic_expand_control');
15
 
16
  function widget_logic_expand_control()
17
- { global $wp_registered_widgets, $wp_registered_widget_controls, $wp_version;
18
 
19
  if(!$wl_options = get_option('widget_logic')) $wl_options = array();
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
- foreach ( $wp_registered_widgets as $id => $widget )
22
- { if (!$wp_registered_widget_controls[$id])
23
- register_widget_control($widget['name'], 'widget_logic_empty_control', 250,61);
24
-
25
- if (is_array($wp_registered_widget_controls[$id]['params'][0]))
26
- {
27
- $wp_registered_widget_controls[$id]['params'][0]['id_for_wl']=$id;
28
- }
29
- else
30
- { array_push($wp_registered_widget_controls[$id]['params'],$id);
31
- $wp_registered_widget_controls[$id]['height']+=40;
 
 
 
 
 
 
32
  }
33
-
34
- $wp_registered_widget_controls[$id]['callback_wl_redirect']=$wp_registered_widget_controls[$id]['callback'];
35
- $wp_registered_widget_controls[$id]['callback']='widget_logic_extra_control';
36
-
37
- if (isset($_POST[$id.'-widget_logic']))
38
- $wl_options[$id]=$_POST[$id.'-widget_logic'];
39
  }
40
-
 
41
  if ( isset($_POST['widget_logic-options-submit']) )
42
  $wl_options['widget_logic-options-filter']=$_POST['widget_logic-options-filter'];
43
 
@@ -66,36 +79,33 @@ function widget_logic_options_filter()
66
  function widget_logic_empty_control() {}
67
 
68
  function widget_logic_extra_control()
69
- { global $wp_registered_widget_controls, $wp_version;
70
  $params=func_get_args();
71
 
 
72
  $id=(is_array($params[0]))?$params[0]['id_for_wl']:array_pop($params);
73
-
74
- if ( ('post' == strtolower($_SERVER['REQUEST_METHOD'])) && version_compare($wp_version, '2.5', ">="))
75
- { foreach ( $wp_registered_widget_controls as $name => $control )
76
- if ( is_callable( $control['callback_wl_redirect'] ) )
77
- call_user_func_array( $control['callback_wl_redirect'], $control['params'] );
78
- }
79
- else
80
- { $callback=$wp_registered_widget_controls[$id]['callback_wl_redirect'];
81
- if (is_callable($callback))
82
- call_user_func_array($callback, $params); // go to the original control function
83
- }
84
 
85
  if(!$wl_options = get_option('widget_logic')) $wl_options = array();
 
 
 
 
86
 
87
- $id_disp=$id;
88
  $value=htmlspecialchars(stripslashes($wl_options[$id]),ENT_QUOTES);
 
 
89
  if (is_array($params[0]) && isset($params[0]['number'])) $number=$params[0]['number'];
90
  if ($number==-1) {$number="%i%"; $value="";}
91
  if (isset($number)) $id_disp=$wp_registered_widget_controls[$id]['id_base'].'-'.$number;
92
 
 
93
  echo "<p><label for='".$id_disp."-widget_logic'>Widget logic <input type='text' name='".$id_disp."-widget_logic' id='".$id_disp."-widget_logic' value='".$value."' /></label></p>";
94
 
95
  }
96
 
97
 
98
- // intercept registered widgets - redirect it and put its ID on the end of the params
99
  // perhaps there is a way to just intercept the ones that are used??
100
  add_action('wp_head', 'widget_logic_redirect_callback');
101
  function widget_logic_redirect_callback()
4
  Plugin URI: http://freakytrigger.co.uk/wordpress-setup/
5
  Description: Control widgets with WP's conditional tags is_home etc
6
  Author: Alan Trewartha
7
+ Version: 0.42
8
  Author URI: http://freakytrigger.co.uk/author/alan/
9
  */
10
 
11
 
12
 
 
13
  add_action( 'sidebar_admin_setup', 'widget_logic_expand_control');
14
 
15
  function widget_logic_expand_control()
16
+ { global $wp_registered_widgets, $wp_registered_widget_controls;
17
 
18
  if(!$wl_options = get_option('widget_logic')) $wl_options = array();
19
+ //print_r($wl_options);
20
+
21
+ // if we're just updating the widgets, just read in the widget logic settings - makes this WP2.5+ only i think
22
+ if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) )
23
+ { foreach ( (array) $_POST['widget-id'] as $widget_number => $widget_id )
24
+ $wl_options[$widget_id]=$_POST[$widget_id.'-widget_logic'];
25
+
26
+ // clean up empty options (in PHP5 use array_intersect_key)
27
+ $regd_plus_new=array_merge(array_keys($wp_registered_widgets),array_values((array) $_POST['widget-id']));
28
+ foreach (array_keys($wl_options) as $key)
29
+ if (!in_array($key, $regd_plus_new))
30
+ unset($wl_options[$key]);
31
+ }
32
 
33
+ else
34
+ // re-wire the registered control functions to go via widget_logic_extra_control
35
+ { foreach ( $wp_registered_widgets as $id => $widget )
36
+ { if (!$wp_registered_widget_controls[$id])
37
+ wp_register_widget_control($id,$widget['name'], 'widget_logic_empty_control');
38
+
39
+ if (!array_key_exists(0,$wp_registered_widget_controls[$id]['params']) || is_array($wp_registered_widget_controls[$id]['params'][0]))
40
+ $wp_registered_widget_controls[$id]['params'][0]['id_for_wl']=$id;
41
+ else
42
+ { // some older widgets put number in to params directly (which messes up the 'templates' in WP2.5)
43
+ array_push($wp_registered_widget_controls[$id]['params'],$id);
44
+ $wp_registered_widget_controls[$id]['height']+=40; // this is really a pre2.5 thing - discard?
45
+ }
46
+
47
+ // do the redirection
48
+ $wp_registered_widget_controls[$id]['callback_wl_redirect']=$wp_registered_widget_controls[$id]['callback'];
49
+ $wp_registered_widget_controls[$id]['callback']='widget_logic_extra_control';
50
  }
 
 
 
 
 
 
51
  }
52
+
53
+ // check the 'widget content' filter option
54
  if ( isset($_POST['widget_logic-options-submit']) )
55
  $wl_options['widget_logic-options-filter']=$_POST['widget_logic-options-filter'];
56
 
79
  function widget_logic_empty_control() {}
80
 
81
  function widget_logic_extra_control()
82
+ { global $wp_registered_widget_controls;
83
  $params=func_get_args();
84
 
85
+ // find the widget id that we have sneaked into the params
86
  $id=(is_array($params[0]))?$params[0]['id_for_wl']:array_pop($params);
87
+ $id_disp=$id;
 
 
 
 
 
 
 
 
 
 
88
 
89
  if(!$wl_options = get_option('widget_logic')) $wl_options = array();
90
+
91
+ $callback=$wp_registered_widget_controls[$id]['callback_wl_redirect'];
92
+ if (is_callable($callback))
93
+ call_user_func_array($callback, $params); // go to the original control function
94
 
 
95
  $value=htmlspecialchars(stripslashes($wl_options[$id]),ENT_QUOTES);
96
+
97
+ // dealing with multiple widgets - get the number. if -1 this is the 'template' for the admin interface
98
  if (is_array($params[0]) && isset($params[0]['number'])) $number=$params[0]['number'];
99
  if ($number==-1) {$number="%i%"; $value="";}
100
  if (isset($number)) $id_disp=$wp_registered_widget_controls[$id]['id_base'].'-'.$number;
101
 
102
+ // output our extra widget logic field
103
  echo "<p><label for='".$id_disp."-widget_logic'>Widget logic <input type='text' name='".$id_disp."-widget_logic' id='".$id_disp."-widget_logic' value='".$value."' /></label></p>";
104
 
105
  }
106
 
107
 
108
+ // intercept registered widgets - redirect them and put each ID on the end of the params
109
  // perhaps there is a way to just intercept the ones that are used??
110
  add_action('wp_head', 'widget_logic_redirect_callback');
111
  function widget_logic_redirect_callback()