Widget Logic - Version 0.52

Version Description

Two new features: optional delayed loading of logic (see Configuration under Installation), and the ability to save out and reload all your site's widget logic into a config file

Download this release

Release Info

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

Code changes from version 0.51 to 0.52

Files changed (3) hide show
  1. readme.txt +62 -22
  2. screenshot-2.png +0 -0
  3. widget_logic.php +163 -62
readme.txt CHANGED
@@ -3,15 +3,15 @@ Contributors: alanft
3
  Donate link: http://www.justgiving.com/widgetlogic_cancerresearchuk
4
  Tags: widget, admin, conditional tags, filter, context
5
  Requires at least: 2.8
6
- Tested up to: 3.3
7
- Stable tag: 0.51
8
 
9
  Widget Logic lets you control on which pages widgets appear using WP's conditional tags. It also adds a 'widget_content' filter.
10
 
11
  == Description ==
12
  This plugin gives every widget an extra control field called "Widget logic" that lets you control the pages that the widget will appear on. The text field lets you use WP's [Conditional Tags](http://codex.wordpress.org/Conditional_Tags), or any general PHP code.
13
 
14
- ** PLEASE NOTE ** The widget logic you introduce is EVAL'd directly. Anyone who has access to edit widget appearance will have the right to add any code, including malicious and possibly destructive functions. If you are worried about the security implications, please let me know how you think I could guard against this without compromising the general power and flexibility of Widget Logic too much.
15
 
16
  There is also an option to add a wordpress 'widget_content' filter -- this lets you tweak any widget's HTML to suit your theme without editing plugins and core code.
17
 
@@ -23,7 +23,22 @@ If you like and use Widget Logic you could consider a small donation to Cancer R
23
 
24
  1. Upload `widget-logic.php` to the `/wp-content/plugins/` directory
25
  2. Activate the plugin through the 'Plugins' menu in WordPress
26
- 3. That's it. All the configuring and options are in the usual widget admin interface.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  == Frequently Asked Questions ==
29
 
@@ -40,49 +55,71 @@ The most common sources of problems are:
40
 
41
  In version 0.50 I made some fundamental changes to how Widget Logic works. The result was that the wp_reset_query was less targeted in the code. If lots of people find this problematic I will look at a general solution, but the main workround is to put wp_reset_query() into your code just before calling a dynamic sidebar.
42
 
43
- = Widgets appear when they shouldn't =
44
 
45
- It might be that your theme performs custom queries before calling the sidebar. Try the `wp_reset_query` option.
46
 
47
- Alternatively you may have not defined your logic tightly enough. For example when the sidebar is being processed, in_category('cheese') will be true if the last post on an archive page is in the 'cheese' category.
48
 
49
- Tighten up your definitions with PHPs 'logical AND' &&, for example:
50
 
51
- `is_single() && in_category('cheese')`
52
 
53
- Another source of confusion is the difference between the [Main Page and the front page](http://codex.wordpress.org/Conditional_Tags#The_Main_Page). If you have set your front page to be a static page, use is_front_page() instead of is_page(x).
 
 
54
 
55
  = How do I get a widget to appear both on a category page and on single posts within that category? =
56
- Again, take care with your conditional tags. There is both an `in_category` and `is_category` tag. One is used to tell if the 'current' post is IN a category, and the other is used to tell if the page showing IS for that category (same goes for tags etc). What you want is the case when:
57
 
58
  `(this page IS category X) OR (this is a single post AND this post is IN category X)`
59
  which in proper PHP is:
60
 
61
  `is_category(X) || (is_single() && in_category(X))`
62
 
63
- See also: 'Writing Logic Code' in the [Other Notes](../other_notes/) section.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
  == Screenshots ==
66
 
67
  1. The 'Widget logic' field at work in standard widgets.
68
- 2. The `widget_content` filter and `wp_reset_query` options are at the foot of the widget admin page. (Both are off by default.)
69
 
70
  == Writing Logic Code ==
71
 
72
- The text in the 'Widget logic' field can be full PHP code and should return 'true' when you need the widget to appear. Make good use of [WP's own conditional tags](http://codex.wordpress.org/Conditional_Tags).
73
-
74
- If there is no 'return' in the text, an implicit 'return' is added to the start and a ';' is added on the end.
75
 
76
- Try variations on the examples given below. Use `!` (NOT) in front of a conditional tag to reverse the logic, eg !is_home() to show a widget on any page except the home page.
 
77
 
78
- Use `||` (OR), `&&` (AND) to make more complex conditions. There are lots of great code examples on the WP forums, and on WP sites across the net. But the WP Codex is also full of good examples to adapt, such as [Test if post is in a descendent category](http://codex.wordpress.org/Template_Tags/in_category#Testing_if_a_post_is_in_a_descendant_category).
 
 
79
 
80
- Remember -- the code runs even if the widget doesn't appear. (Even if it never appears!)
81
 
82
- Examples:
83
 
84
  * `is_home()` -- just the main blog page
85
  * `!is_page('about')` -- everywhere EXCEPT this specific WP 'page'
 
86
  * `is_category(array(5,9,10,11))` -- category page of one of the given category IDs
87
  * `is_single() && in_category('baked-goods')` -- single post that's in the category with this slug
88
  * `current_user_can('level_10')` -- admin only widget
@@ -107,7 +144,7 @@ The second parameter ($widget_id) can be used to target specific widgets if need
107
 
108
  A [Wordpress filter function](http://codex.wordpress.org/Plugin_API#Filters) 'takes as input the unmodified data, and returns modified data' which means that widget_content filters are provided with the raw HTML output by the widget, and you are then free to return something else entirely:
109
 
110
- _Example filters_
111
 
112
  `add_filter('widget_content', 'basic_widget_content_filter');
113
  function basic_widget_content_filter($content='')
@@ -137,6 +174,9 @@ function make_alternating_widget_styles($content='')
137
 
138
  == Changelog ==
139
 
 
 
 
140
  = 0.51 =
141
  One important bug fix (fairly major and fairly stupid of me too)
142
 
@@ -149,7 +189,7 @@ introduced in WP2.8 by the look of it). The upshot is that is_active_sidebar sho
149
  Widget callbacks only get intercepted if the 'widget_content' filter is activated, and much more briefly. (A widget's 'callback' is rewired within the 'dynamic_sidebar' function just before the widget is called, by the 'dynamic_sidebar_param' filter, and is restored when the callback function is invoked.)
150
 
151
  = 0.48 =
152
- Kill some poor coding practices that throws debug notices - thanks to John James Jacoby. This is currently the 'last stable release'
153
 
154
  = 0.47 =
155
  FINALLY tracked down the elusive 'wp_reset_query' option resetting bug.
3
  Donate link: http://www.justgiving.com/widgetlogic_cancerresearchuk
4
  Tags: widget, admin, conditional tags, filter, context
5
  Requires at least: 2.8
6
+ Tested up to: 3.3.2
7
+ Stable tag: 0.52
8
 
9
  Widget Logic lets you control on which pages widgets appear using WP's conditional tags. It also adds a 'widget_content' filter.
10
 
11
  == Description ==
12
  This plugin gives every widget an extra control field called "Widget logic" that lets you control the pages that the widget will appear on. The text field lets you use WP's [Conditional Tags](http://codex.wordpress.org/Conditional_Tags), or any general PHP code.
13
 
14
+ PLEASE NOTE The widget logic you introduce is EVAL'd directly. Anyone who has access to edit widget appearance will have the right to add any code, including malicious and possibly destructive functions. If you are worried about the security implications, please let me know how you think I could guard against this without compromising the general power and flexibility of Widget Logic too much.
15
 
16
  There is also an option to add a wordpress 'widget_content' filter -- this lets you tweak any widget's HTML to suit your theme without editing plugins and core code.
17
 
23
 
24
  1. Upload `widget-logic.php` to the `/wp-content/plugins/` directory
25
  2. Activate the plugin through the 'Plugins' menu in WordPress
26
+ 3. That's it. The configuring and options are in the usual widget admin interface.
27
+
28
+ = Configuration =
29
+
30
+ Aside from logic against your widgets, there are three options added to the foot of the widget admin page (see screenshots).
31
+
32
+ * Add 'widget_content' filter -- This allows you to modify the text output in all widgets. You need to know how to write a WP filter, though some basics are covered in [Other Notes](../other_notes/).
33
+
34
+ * Use 'wp_reset_query' fix -- Many features of WP, as well as the many themes and plugins out there, can mess with the conditional tags, such that is_home is NOT true on the home page. This can often be fixed with a quick wp_reset_query() statement just before the widgets are called, and this option puts that in for you rather than having to resort to code editing
35
+
36
+ * Load logic -- This option allows you to set the point in the page load at which your widget logic starts to be checked. Pre v.50 it was when the 'wp_head' trigger happened, ie during the creation of the HTML's HEAD block. Many themes didn't call wp_head, which was a problem. From v.50 it happens, by default, as early as possible, which is as soon as the plugin loads. You can now specify these 'late load' points (in chronological order):
37
+ * after the theme loads (after_setup_theme trigger)
38
+ * when all PHP loaded (wp_loaded trigger)
39
+ * during page header (wp_head trigger)
40
+
41
+ You may need to delay the load if your logic depends on functions defined, eg in the theme functions.php file. Conversely you may want the load early so that the widget count is calculated correctly, eg to show an alternative layour or content when a sidebar has no widgets.
42
 
43
  == Frequently Asked Questions ==
44
 
55
 
56
  In version 0.50 I made some fundamental changes to how Widget Logic works. The result was that the wp_reset_query was less targeted in the code. If lots of people find this problematic I will look at a general solution, but the main workround is to put wp_reset_query() into your code just before calling a dynamic sidebar.
57
 
58
+ = What's this stuff in my sidebar when there are no widgets? =
59
 
60
+ Since v .50 the widget logic code runs such that when dynamic_sidebar is called in a theme's code it will 'return false' if no widgets are present. In such cases many themes are coded to put in some default sidebar text in place of widgets, which is what you are seeing.
61
 
62
+ Your options, if you want this default sidebar content gone, are to either edit the theme, or as a work around, add an empty text widget (no title, no content) to the end of the sidebar's widget list.
63
 
64
+ = How do I get widget X on just my 'home' page? (Or on every page except that.) =
65
 
66
+ There is some confusion between the [Main Page and the front page](http://codex.wordpress.org/Conditional_Tags#The_Main_Page). If you want a widget on your 'front page' whether that is a static page or a set of posts, use is_front_page(). If it is a page using is_page(x) does not work. If your 'front page' is a page and not a series of posts, you can still use is_home() to get widgets on that main posts page (as defined in Admin > Settings > Reading).
67
 
68
+ = Logic using is_page() doesn't work =
69
+
70
+ If your theme calls the sidebar after the loop you should find that the wp_reset_query option fixes things. This problem is explained on the [is_page codex page](http://codex.wordpress.org/Function_Reference/is_page#Cannot_Be_Used_Inside_The_Loop).
71
 
72
  = How do I get a widget to appear both on a category page and on single posts within that category? =
73
+ Take care with your conditional tags. There is both an `in_category` and `is_category` tag. One is used to tell if the 'current' post is IN a category, and the other is used to tell if the page showing IS for that category (same goes for tags etc). What you want is the case when:
74
 
75
  `(this page IS category X) OR (this is a single post AND this post is IN category X)`
76
  which in proper PHP is:
77
 
78
  `is_category(X) || (is_single() && in_category(X))`
79
 
80
+
81
+ = How do I get a widget to appear when X, Y and Z? =
82
+ Have a go at it yourself first. Check out the 'Writing Logic Code' section under [Other Notes](../other_notes/).
83
+
84
+ = Why is Widget Logic so unfriendly, you have to be a code demon to use it? =
85
+ This is sort of deliberate. I originally wrote it to be as flexible as possible with the thought of writing a drag'n'drop UI at some point. I never got round to it, because (I'm lazy and) I couldn't make it both look nice and let you fall back to 'pure code' (for the possibilities harder to cater for in a UI).
86
+
87
+ The plugin [Widget Context](http://wordpress.org/extend/plugins/widget-context/) presents a nice UI and has a neat 'URL matching' function too.
88
+
89
+ = Widgets appear when they shouldn't =
90
+
91
+ It might be that your theme performs custom queries before calling the sidebar. Try the `wp_reset_query` option.
92
+
93
+ Alternatively you may have not defined your logic tightly enough. For example when the sidebar is being processed, in_category('cheese') will be true if the last post on an archive page is in the 'cheese' category.
94
+
95
+ Tighten up your definitions with PHPs 'logical AND' &&, for example:
96
+
97
+ `is_single() && in_category('cheese')`
98
+
99
 
100
  == Screenshots ==
101
 
102
  1. The 'Widget logic' field at work in standard widgets.
103
+ 2. The `widget_content` filter, `wp_reset_query` option and 'load logic point' options are at the foot of the widget admin page. You can also export and import your site's WL options for safe-keeping.
104
 
105
  == Writing Logic Code ==
106
 
107
+ The text in the 'Widget logic' field can be full PHP code and should return 'true' when you need the widget to appear. If there is no 'return' in the text, an implicit 'return' is added to the start and a ';' is added on the end. (This is just to make single statements like is_home() more convenient.)
 
 
108
 
109
+ = The Basics =
110
+ Make good use of [WP's own conditional tags](http://codex.wordpress.org/Conditional_Tags). You can vary and combine code using:
111
 
112
+ * `!` (NOT) to **reverse** the logic, eg `!is_home()` is TRUE when this is NOT the home page.
113
+ * `||` (OR) to **combine** conditions. `X OR Y` is TRUE when either X is true or Y is true.
114
+ * `&&` (AND) to make conditions **more specific**. `X AND Y` is TRUE when both X is true and Y is true.
115
 
116
+ There are lots of great code examples on the WP forums, and on WP sites across the net. But the WP Codex is also full of good examples to adapt, such as [Test if post is in a descendent category](http://codex.wordpress.org/Template_Tags/in_category#Testing_if_a_post_is_in_a_descendant_category).
117
 
118
+ = Examples =
119
 
120
  * `is_home()` -- just the main blog page
121
  * `!is_page('about')` -- everywhere EXCEPT this specific WP 'page'
122
+ * `!is_user_logged_in()` -- shown when a user is not logged in
123
  * `is_category(array(5,9,10,11))` -- category page of one of the given category IDs
124
  * `is_single() && in_category('baked-goods')` -- single post that's in the category with this slug
125
  * `current_user_can('level_10')` -- admin only widget
144
 
145
  A [Wordpress filter function](http://codex.wordpress.org/Plugin_API#Filters) 'takes as input the unmodified data, and returns modified data' which means that widget_content filters are provided with the raw HTML output by the widget, and you are then free to return something else entirely:
146
 
147
+ = Example filters =
148
 
149
  `add_filter('widget_content', 'basic_widget_content_filter');
150
  function basic_widget_content_filter($content='')
174
 
175
  == Changelog ==
176
 
177
+ = 0.52 =
178
+ Two new features: optional delayed loading of logic (see Configuration under [Installation](../installation/)), and the ability to save out and reload all your site's widget logic into a config file
179
+
180
  = 0.51 =
181
  One important bug fix (fairly major and fairly stupid of me too)
182
 
189
  Widget callbacks only get intercepted if the 'widget_content' filter is activated, and much more briefly. (A widget's 'callback' is rewired within the 'dynamic_sidebar' function just before the widget is called, by the 'dynamic_sidebar_param' filter, and is restored when the callback function is invoked.)
190
 
191
  = 0.48 =
192
+ Kill some poor coding practices that throws debug notices - thanks to John James Jacoby.
193
 
194
  = 0.47 =
195
  FINALLY tracked down the elusive 'wp_reset_query' option resetting bug.
screenshot-2.png CHANGED
Binary file
widget_logic.php CHANGED
@@ -4,72 +4,209 @@ 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.51
8
  Author URI: http://freakytrigger.co.uk/author/alan/
9
  */
10
 
11
  global $wl_options;
 
 
 
 
 
 
12
  if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) ) $wl_options = array();
13
 
14
  if (is_admin())
15
- { add_action( 'sidebar_admin_setup', 'widget_logic_expand_control'); // save widget changes and add controls to each widget on the widget admin page
16
- add_action( 'sidebar_admin_page', 'widget_logic_options_filter'); // add extra Widget Logic specific options on the widget admin page
17
- add_filter( 'widget_update_callback', 'widget_logic_widget_update_callback', 10, 3); // save individual widget changes submitted by ajax method
18
- add_filter( 'plugin_action_links', 'wl_charity', 10, 2); // add my justgiving page
 
19
  }
20
  else
21
  {
22
- add_filter( 'sidebars_widgets', 'widget_logic_filter_sidebars_widgets', 10); // actually remove the widgets from the front end depending on widget logic provided
23
-
 
 
 
 
 
 
24
  if ( isset($wl_options['widget_logic-options-filter']) && $wl_options['widget_logic-options-filter'] == 'checked' )
25
  add_filter( 'dynamic_sidebar_params', 'widget_logic_widget_display_callback', 10); // redirect the widget callback so the output can be buffered and filtered
26
  }
27
 
 
 
 
 
 
 
28
 
29
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  // CALLED VIA 'sidebar_admin_setup' ACTION
 
32
  function widget_logic_expand_control()
33
  { global $wp_registered_widgets, $wp_registered_widget_controls, $wl_options;
34
 
35
- // if we're updating the widgets, read in the widget logic settings (makes this WP2.5+ only?)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) )
37
  { foreach ( (array) $_POST['widget-id'] as $widget_number => $widget_id )
38
  if (isset($_POST[$widget_id.'-widget_logic']))
39
  $wl_options[$widget_id]=$_POST[$widget_id.'-widget_logic'];
40
 
41
  // clean up empty options (in PHP5 use array_intersect_key)
42
- $regd_plus_new=array_merge(array_keys($wp_registered_widgets),array_values((array) $_POST['widget-id']),array('widget_logic-options-filter', 'widget_logic-options-wp_reset_query'));
 
43
  foreach (array_keys($wl_options) as $key)
44
  if (!in_array($key, $regd_plus_new))
45
  unset($wl_options[$key]);
46
  }
47
-
48
- // check the 'widget content' filter option
 
49
  if ( isset($_POST['widget_logic-options-submit']) )
50
  { $wl_options['widget_logic-options-filter']=$_POST['widget_logic-options-filter'];
51
  $wl_options['widget_logic-options-wp_reset_query']=$_POST['widget_logic-options-wp_reset_query'];
 
52
  }
53
 
54
- // before updating widget logic options (which gets EVALd)
55
- // we could test current_user_can('edit_theme_options')
56
- // although we shouldn't be able to get here without that capability anyway?
57
  update_option('widget_logic', $wl_options);
58
 
59
- // intercept the widget controls to add in our extra text field
60
- // pop the widget id on the params array (as it's not in the main params so not provided to the callback)
61
- foreach ( $wp_registered_widgets as $id => $widget )
62
- { // controll-less widgets need an empty function so the callback function is called.
63
- if (!$wp_registered_widget_controls[$id])
64
- wp_register_widget_control($id,$widget['name'], 'widget_logic_empty_control');
65
- $wp_registered_widget_controls[$id]['callback_wl_redirect']=$wp_registered_widget_controls[$id]['callback'];
66
- $wp_registered_widget_controls[$id]['callback']='widget_logic_extra_control';
67
- array_push($wp_registered_widget_controls[$id]['params'],$id);
68
- }
69
  }
70
 
71
 
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  // added to widget functionality in 'widget_logic_expand_control' (above)
74
  function widget_logic_empty_control() {}
75
 
@@ -96,42 +233,7 @@ function widget_logic_extra_control()
96
  if (isset($number)) $id_disp=$wp_registered_widget_controls[$id]['id_base'].'-'.$number;
97
 
98
  // output our extra widget logic field
99
- 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>";
100
- }
101
-
102
-
103
-
104
- // CALLED VIA 'sidebar_admin_page' ACTION
105
- function widget_logic_options_filter()
106
- { global $wp_registered_widget_controls, $wl_options;
107
- ?><div class="wrap">
108
- <form method="POST">
109
- <h2>Widget Logic options</h2>
110
- <p style="line-height: 30px;">
111
-
112
- <label for="widget_logic-options-filter" title="Adds a new WP filter you can use in your own code. Not needed for main Widget Logic functionality.">Use 'widget_content' filter
113
- <input id="widget_logic-options-filter" name="widget_logic-options-filter" type="checkbox" value="checked" class="checkbox" <?php echo $wl_options['widget_logic-options-filter'] ?> /></label>
114
- &nbsp;&nbsp;
115
- <label for="widget_logic-options-wp_reset_query" title="Resets a theme's custom queries before your Widget Logic is checked.">Use 'wp_reset_query' fix
116
- <input id="widget_logic-options-wp_reset_query" name="widget_logic-options-wp_reset_query" type="checkbox" value="checked" class="checkbox" <?php echo $wl_options['widget_logic-options-wp_reset_query'] ?> /></label>
117
-
118
- <span class="submit"><input type="submit" name="widget_logic-options-submit" id="widget_logic-options-submit" value="Save" /></span></p>
119
- </form>
120
- </div>
121
- <?php
122
- }
123
-
124
-
125
-
126
- // CALLED VIA 'widget_update_callback' ACTION (ajax update of a widget)
127
- function widget_logic_widget_update_callback($instance, $new_instance, $this_widget)
128
- { global $wl_options;
129
- $widget_id=$this_widget->id;
130
- if ( isset($_POST[$widget_id.'-widget_logic']))
131
- { $wl_options[$widget_id]=$_POST[$widget_id.'-widget_logic'];
132
- update_option('widget_logic', $wl_options);
133
- }
134
- return $instance;
135
  }
136
 
137
 
@@ -154,8 +256,7 @@ function widget_logic_filter_sidebars_widgets($sidebars_widgets)
154
  { global $wp_reset_query_is_done, $wl_options;
155
 
156
  // 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
157
- // add && empty( $wp_reset_query_is_done ) to the next line to only exec the reset once per page
158
- if ( !empty( $wl_options['widget_logic-options-wp_reset_query'] ) && ( $wl_options['widget_logic-options-wp_reset_query'] == 'checked' ))
159
  { wp_reset_query(); $wp_reset_query_is_done=true; }
160
 
161
  // loop through every widget in every sidebar (barring 'wp_inactive_widgets') checking WL for each one
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.52
8
  Author URI: http://freakytrigger.co.uk/author/alan/
9
  */
10
 
11
  global $wl_options;
12
+ $wl_load_points=array( 'plugins_loaded' => 'when plugin starts (default)',
13
+ 'after_setup_theme'=> 'after theme loads',
14
+ 'wp_loaded' => 'when all PHP loaded',
15
+ 'wp_head'=> 'during page header'
16
+ );
17
+
18
  if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) ) $wl_options = array();
19
 
20
  if (is_admin())
21
+ {
22
+ add_filter( 'widget_update_callback', 'widget_logic_ajax_update_callback', 10, 3); // widget changes submitted by ajax method
23
+ 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
24
+ add_action( 'sidebar_admin_page', 'widget_logic_options_control'); // add Widget Logic specific options on the widget admin page
25
+ add_filter( 'plugin_action_links', 'wl_charity', 10, 2); // add my justgiving page link to the plugin admin page
26
  }
27
  else
28
  {
29
+ if ( isset($wl_options['widget_logic-options-load_point']) &&
30
+ ($wl_options['widget_logic-options-load_point']!='plugins_loaded') &&
31
+ array_key_exists($wl_options['widget_logic-options-load_point'],$wl_load_points )
32
+ )
33
+ add_action ($wl_options['widget_logic-options-load_point'],'widget_logic_sidebars_widgets_filter_add');
34
+ else
35
+ widget_logic_sidebars_widgets_filter_add();
36
+
37
  if ( isset($wl_options['widget_logic-options-filter']) && $wl_options['widget_logic-options-filter'] == 'checked' )
38
  add_filter( 'dynamic_sidebar_params', 'widget_logic_widget_display_callback', 10); // redirect the widget callback so the output can be buffered and filtered
39
  }
40
 
41
+ function widget_logic_sidebars_widgets_filter_add()
42
+ {
43
+ add_filter( 'sidebars_widgets', 'widget_logic_filter_sidebars_widgets', 10); // actually remove the widgets from the front end depending on widget logic provided
44
+ }
45
+ // wp-admin/widgets.php explicitly checks current_user_can('edit_theme_options')
46
+ // which is enough security, I believe. If you think otherwise please contact me
47
 
48
 
49
+ // CALLED VIA 'widget_update_callback' FILTER (ajax update of a widget)
50
+ function widget_logic_ajax_update_callback($instance, $new_instance, $this_widget)
51
+ { global $wl_options;
52
+ $widget_id=$this_widget->id;
53
+ if ( isset($_POST[$widget_id.'-widget_logic']))
54
+ { $wl_options[$widget_id]=$_POST[$widget_id.'-widget_logic'];
55
+ update_option('widget_logic', $wl_options);
56
+ }
57
+ return $instance;
58
+ }
59
+
60
 
61
  // CALLED VIA 'sidebar_admin_setup' ACTION
62
+ // adds in the admin control per widget, but also processes import/export
63
  function widget_logic_expand_control()
64
  { global $wp_registered_widgets, $wp_registered_widget_controls, $wl_options;
65
 
66
+
67
+ // EXPORT ALL OPTIONS
68
+ if (isset($_GET['wl-options-export']))
69
+ {
70
+ header("Content-Disposition: attachment; filename=widget_logic_options.txt");
71
+ header('Content-Type: text/plain; charset=utf-8');
72
+
73
+ echo "[START=WIDGET LOGIC OPTIONS]\n";
74
+ foreach ($wl_options as $id => $text)
75
+ echo "$id\t".json_encode($text)."\n";
76
+ echo "[STOP=WIDGET LOGIC OPTIONS]";
77
+ exit;
78
+ }
79
+
80
+
81
+ // IMPORT ALL OPTIONS
82
+ if ( isset($_POST['wl-options-import']))
83
+ { if ($_FILES['wl-options-import-file']['tmp_name'])
84
+ { $import=split("\n",file_get_contents($_FILES['wl-options-import-file']['tmp_name'], false));
85
+ if (array_shift($import)=="[START=WIDGET LOGIC OPTIONS]" && array_pop($import)=="[STOP=WIDGET LOGIC OPTIONS]")
86
+ { foreach ($import as $import_option)
87
+ { list($key, $value)=split("\t",$import_option);
88
+ $wl_options[$key]=json_decode($value);
89
+ }
90
+ $wl_options['msg']="OK – options file imported";
91
+ }
92
+ else
93
+ { $wl_options['msg']="Invalid options file";
94
+ }
95
+
96
+ }
97
+ else
98
+ $wl_options['msg']="No options file provided";
99
+
100
+ update_option('widget_logic', $wl_options);
101
+ wp_redirect( admin_url('widgets.php') );
102
+ exit;
103
+ }
104
+
105
+
106
+ // ADD EXTRA WIDGET LOGIC FIELD TO EACH WIDGET CONTROL
107
+ // pop the widget id on the params array (as it's not in the main params so not provided to the callback)
108
+ foreach ( $wp_registered_widgets as $id => $widget )
109
+ { // controll-less widgets need an empty function so the callback function is called.
110
+ if (!$wp_registered_widget_controls[$id])
111
+ wp_register_widget_control($id,$widget['name'], 'widget_logic_empty_control');
112
+ $wp_registered_widget_controls[$id]['callback_wl_redirect']=$wp_registered_widget_controls[$id]['callback'];
113
+ $wp_registered_widget_controls[$id]['callback']='widget_logic_extra_control';
114
+ array_push($wp_registered_widget_controls[$id]['params'],$id);
115
+ }
116
+
117
+
118
+ // UPDATE WIDGET LOGIC WIDGET OPTIONS (via accessibility mode?)
119
  if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) )
120
  { foreach ( (array) $_POST['widget-id'] as $widget_number => $widget_id )
121
  if (isset($_POST[$widget_id.'-widget_logic']))
122
  $wl_options[$widget_id]=$_POST[$widget_id.'-widget_logic'];
123
 
124
  // clean up empty options (in PHP5 use array_intersect_key)
125
+ $regd_plus_new=array_merge(array_keys($wp_registered_widgets),array_values((array) $_POST['widget-id']),
126
+ array('widget_logic-options-filter', 'widget_logic-options-wp_reset_query', 'widget_logic-options-load_point'));
127
  foreach (array_keys($wl_options) as $key)
128
  if (!in_array($key, $regd_plus_new))
129
  unset($wl_options[$key]);
130
  }
131
+
132
+ // UPDATE OTHER WIDGET LOGIC OPTIONS
133
+ // must update this to use http://codex.wordpress.org/Settings_API
134
  if ( isset($_POST['widget_logic-options-submit']) )
135
  { $wl_options['widget_logic-options-filter']=$_POST['widget_logic-options-filter'];
136
  $wl_options['widget_logic-options-wp_reset_query']=$_POST['widget_logic-options-wp_reset_query'];
137
+ $wl_options['widget_logic-options-load_point']=$_POST['widget_logic-options-load_point'];
138
  }
139
 
140
+
 
 
141
  update_option('widget_logic', $wl_options);
142
 
 
 
 
 
 
 
 
 
 
 
143
  }
144
 
145
 
146
 
147
+
148
+ // CALLED VIA 'sidebar_admin_page' ACTION
149
+ // output extra HTML
150
+ // to update using http://codex.wordpress.org/Settings_API asap
151
+ function widget_logic_options_control()
152
+ { global $wp_registered_widget_controls, $wl_options, $wl_load_points;
153
+
154
+ if ( isset($wl_options['msg']))
155
+ { if (substr($wl_options['msg'],0,2)=="OK")
156
+ echo '<div id="message" class="updated">';
157
+ else
158
+ echo '<div id="message" class="error">';
159
+ echo '<p>Widget Logic – '.$wl_options['msg'].'</p></div>';
160
+ unset($wl_options['msg']);
161
+ update_option('widget_logic', $wl_options);
162
+ }
163
+
164
+
165
+ ?><div class="wrap">
166
+
167
+ <h2>Widget Logic options</h2>
168
+ <form method="POST" style="float:left; width:45%">
169
+ <ul>
170
+ <li><label for="widget_logic-options-filter" title="Adds a new WP filter you can use in your own code. Not needed for main Widget Logic functionality.">
171
+ <input id="widget_logic-options-filter" name="widget_logic-options-filter" type="checkbox" value="checked" class="checkbox" <?php echo $wl_options['widget_logic-options-filter'] ?>/>
172
+ Add 'widget_content' filter
173
+ </label>
174
+ </li>
175
+ <li><label for="widget_logic-options-wp_reset_query" title="Resets a theme's custom queries before your Widget Logic is checked">
176
+ <input id="widget_logic-options-wp_reset_query" name="widget_logic-options-wp_reset_query" type="checkbox" value="checked" class="checkbox" <?php echo $wl_options['widget_logic-options-wp_reset_query'] ?> />
177
+ Use 'wp_reset_query' fix
178
+ </label>
179
+ </li>
180
+ <li><label for="widget_logic-options-load_point" title="Delays widget logic code being evaluated til various points in the WP loading process">Load logic
181
+ <select id="widget_logic-options-load_point" name="widget_logic-options-load_point" ><?php
182
+ foreach($wl_load_points as $action => $action_desc)
183
+ { echo "<option value='".$action."'";
184
+ if ($action==$wl_options['widget_logic-options-load_point'])
185
+ echo " selected ";
186
+ echo ">".$action_desc."</option>"; //
187
+ }
188
+ ?>
189
+ </select>
190
+ </label>
191
+ </li>
192
+ </ul>
193
+
194
+ <?php submit_button( __( 'Save WL options' ), 'button-primary', 'widget_logic-options-submit', false ); ?>
195
+
196
+ </form>
197
+ <form method="POST" enctype="multipart/form-data" style="float:left; width:45%">
198
+ <a class="submit button" href="?wl-options-export" title="Save all WL options to a plain text config file">Export options</a><p>
199
+ <?php submit_button( __( 'Import options' ), 'button', 'wl-options-import', false,
200
+ array( 'title'=>'Load all WL options from a plain text config file'
201
+ ) ); ?>
202
+ <input type="file" name="wl-options-import-file" id="wl-options-import-file" title="Select file for importing" /></p>
203
+ </form>
204
+
205
+ </div>
206
+
207
+ <?php
208
+ }
209
+
210
  // added to widget functionality in 'widget_logic_expand_control' (above)
211
  function widget_logic_empty_control() {}
212
 
233
  if (isset($number)) $id_disp=$wp_registered_widget_controls[$id]['id_base'].'-'.$number;
234
 
235
  // output our extra widget logic field
236
+ echo "<p><label for='".$id_disp."-widget_logic'>Widget logic <textarea class='widefat' type='text' name='".$id_disp."-widget_logic' id='".$id_disp."-widget_logic' >".$value."</textarea></label></p>";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  }
238
 
239
 
256
  { global $wp_reset_query_is_done, $wl_options;
257
 
258
  // 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
259
+ if ( !empty( $wl_options['widget_logic-options-wp_reset_query'] ) && ( $wl_options['widget_logic-options-wp_reset_query'] == 'checked' ) && empty( $wp_reset_query_is_done ) )
 
260
  { wp_reset_query(); $wp_reset_query_is_done=true; }
261
 
262
  // loop through every widget in every sidebar (barring 'wp_inactive_widgets') checking WL for each one