Widget Logic - Version 0.4

Version Description

Download this release

Release Info

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

Code changes from version 0.31 to 0.4

Files changed (2) hide show
  1. readme.txt +11 -4
  2. widget_logic.php +70 -46
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
  Contributors: alanft
3
  Tags: widget, admin, conditional tags, filter
4
  Requires at least: 2.1
5
- Tested up to: 2.3.2
6
- Stable tag: 0.31
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
 
@@ -14,6 +14,11 @@ This text field allows you to specify any WP conditional tags logic to set when
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
  == Installation ==
18
 
19
  1. Upload `widget-logic.php` to the `/wp-content/plugins/` directory
@@ -28,13 +33,15 @@ The logic text on one of your widgets may be invalid PHP.
28
 
29
  = The 'Widget Logic' field is missing or appears elsewhere =
30
 
31
- 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.
32
 
33
  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.
34
 
 
 
35
  == Screenshots ==
36
 
37
- 1. The 'Widget logic' field at work in a widget I use.
38
  2. The 'widget_content' filter option is at the foot of the widget admin page. (Off by default.)
39
 
40
  == Writing Logic Code ==
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
 
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
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 ==
widget_logic.php CHANGED
@@ -2,59 +2,54 @@
2
  /*
3
  Plugin Name: Widget Logic
4
  Plugin URI: http://freakytrigger.co.uk/wordpress-setup/
5
- Description: Allows widgets to appear as directed by WP tags is_home etc
6
  Author: Alan Trewartha
7
- Version: 0.31
8
  Author URI: http://freakytrigger.co.uk/author/alan/
9
  */
10
 
11
 
12
 
13
- // before the page is drawn add minimal controls to ALL widgets and make control boxen deeper
14
- // needs to happen before wp_widgets_admin_head
15
- add_action( 'admin_head', 'widget_logic_expand_control', 5);
16
- function widget_logic_expand_control()
17
- { global $wp_registered_widgets, $wp_registered_widget_controls;
18
- foreach ( $wp_registered_widgets as $name => $widget )
19
- { if ( !isset( $wp_registered_widget_controls[$name] ) )
20
- register_widget_control($widget['name'], 'widget_logic_empty_control', 250,61);
21
- $wp_registered_widget_controls[$name]['height']+=40;
22
- }
23
-
24
- }
25
 
26
- function widget_logic_empty_control() {}
27
-
28
- // after the controls drawn, add INPUT by plaing with DOM
29
- add_action('sidebar_admin_page', 'widget_logic_add_control');
30
- function widget_logic_add_control()
31
- { global $wp_registered_widget_controls;
32
 
33
  if(!$wl_options = get_option('widget_logic')) $wl_options = array();
34
 
35
- ?><script>
36
- function insert_control(id,value)
37
- { nc = document.createElement("p");
38
- nc.innerHTML="<label for='" + id + "-widget_logic'>Widget logic <input type='text' name='" + id + "-widget_logic' id='" + id + "-widget_logic' value='" + value + "' /></label>";
39
- document.getElementById(id + "control").getElementsByTagName("div").item(0).appendChild(nc);
 
 
40
  }
41
- <?php
42
- foreach ( $wp_registered_widget_controls as $name => $widget )
43
- { $id=$widget['id'];
 
 
 
 
44
 
45
  if (isset($_POST[$id.'-widget_logic']))
46
- { $wl_options[$id]=$_POST[$id.'-widget_logic'];
47
- update_option('widget_logic', $wl_options);
48
- }
49
- echo "\n\t\tinsert_control('".$id."', '".htmlspecialchars(stripslashes($wl_options[$id]),ENT_QUOTES)."');";
50
  }
51
- echo "\n\t</script>\n\t";
52
-
53
 
54
  if ( isset($_POST['widget_logic-options-submit']) )
55
- { $wl_options['widget_logic-options-filter']=$_POST['widget_logic-options-filter'];
56
- update_option('widget_logic', $wl_options);
57
- }
 
 
 
 
 
 
 
 
58
  ?><div class="wrap">
59
  <form method="POST">
60
  <h2>Widget Logic options</h2>
@@ -65,19 +60,49 @@ function widget_logic_add_control()
65
  </form>
66
  </div>
67
  <?php
 
 
 
 
 
 
 
 
68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  }
71
 
72
 
73
- // intercept EVERY registered widget - redirect it and put its ID on the end of the params
74
  // perhaps there is a way to just intercept the ones that are used??
75
  add_action('wp_head', 'widget_logic_redirect_callback');
76
  function widget_logic_redirect_callback()
77
  { global $wp_registered_widgets;
78
  foreach ( $wp_registered_widgets as $id => $widget )
79
  { array_push($wp_registered_widgets[$id]['params'],$id);
80
- $wp_registered_widgets[$id]['callback_redirect']=$wp_registered_widgets[$id]['callback'];
81
  $wp_registered_widgets[$id]['callback']='widget_logic_redirected_callback';
82
  }
83
  }
@@ -85,21 +110,21 @@ function widget_logic_redirect_callback()
85
  // the redirection comes here
86
  function widget_logic_redirected_callback()
87
  { global $wp_registered_widgets;
88
- $params=func_get_args(); // get all the passed params
89
- $id=array_pop($params); // take off the widget ID
90
- $callback=$wp_registered_widgets[$id]['callback_redirect']; // find the real callback
91
 
92
- $wl_options = get_option('widget_logic'); // do we want the widget?
93
  $wl_value=($wl_options[$id])?stripslashes($wl_options[$id]):"true";
94
  $wl_value=(stristr($wl_value, "return"))?$wl_value:"return ".$wl_value.";";
95
 
96
  $wl_value=(eval($wl_value) && is_callable($callback));
97
  if ( $wl_value )
98
  { if ($wl_options['widget_logic-options-filter']!='checked')
99
- call_user_func_array($callback, $params); // if so callback with original params!
100
  else
101
  { ob_start();
102
- call_user_func_array($callback, $params); // if so callback with original params!
103
  $widget_content = ob_get_contents();
104
  ob_end_clean();
105
  echo apply_filters( 'widget_content', $widget_content, $id);
@@ -107,5 +132,4 @@ function widget_logic_redirected_callback()
107
  }
108
  }
109
 
110
-
111
  ?>
2
  /*
3
  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
+
44
+ update_option('widget_logic', $wl_options);
45
+ }
46
+
47
+
48
+ add_action( 'sidebar_admin_page', 'widget_logic_options_filter');
49
+
50
+ function widget_logic_options_filter()
51
+ {
52
+ if(!$wl_options = get_option('widget_logic')) $wl_options = array();
53
  ?><div class="wrap">
54
  <form method="POST">
55
  <h2>Widget Logic options</h2>
60
  </form>
61
  </div>
62
  <?php
63
+ }
64
+
65
+
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()
102
  { global $wp_registered_widgets;
103
  foreach ( $wp_registered_widgets as $id => $widget )
104
  { array_push($wp_registered_widgets[$id]['params'],$id);
105
+ $wp_registered_widgets[$id]['callback_wl_redirect']=$wp_registered_widgets[$id]['callback'];
106
  $wp_registered_widgets[$id]['callback']='widget_logic_redirected_callback';
107
  }
108
  }
110
  // the redirection comes here
111
  function widget_logic_redirected_callback()
112
  { global $wp_registered_widgets;
113
+ $params=func_get_args(); // get all the passed params
114
+ $id=array_pop($params); // take off the widget ID
115
+ $callback=$wp_registered_widgets[$id]['callback_wl_redirect']; // find the real callback
116
 
117
+ $wl_options = get_option('widget_logic'); // do we want the widget?
118
  $wl_value=($wl_options[$id])?stripslashes($wl_options[$id]):"true";
119
  $wl_value=(stristr($wl_value, "return"))?$wl_value:"return ".$wl_value.";";
120
 
121
  $wl_value=(eval($wl_value) && is_callable($callback));
122
  if ( $wl_value )
123
  { if ($wl_options['widget_logic-options-filter']!='checked')
124
+ call_user_func_array($callback, $params); // if so callback with original params!
125
  else
126
  { ob_start();
127
+ call_user_func_array($callback, $params); // if so callback with original params!
128
  $widget_content = ob_get_contents();
129
  ob_end_clean();
130
  echo apply_filters( 'widget_content', $widget_content, $id);
132
  }
133
  }
134
 
 
135
  ?>