Commit 4ec8e341 authored by lechuck's avatar lechuck Committed by lechuck

Upgrade widget logic to 0.52

parent 260b1b9e
......@@ -2,28 +2,43 @@
Contributors: alanft
Donate link: http://www.justgiving.com/widgetlogic_cancerresearchuk
Tags: widget, admin, conditional tags, filter, context
Requires at least: 2.5
Tested up to: 3.1
Stable tag: 0.48
Requires at least: 2.8
Tested up to: 3.3.2
Stable tag: 0.52
Widget Logic lets you control on which pages widgets appear. It uses any of WP's conditional tags. It also adds a 'widget_content' filter.
Widget Logic lets you control on which pages widgets appear using WP's conditional tags. It also adds a 'widget_content' filter.
== Description ==
This plugin gives every widget an extra control field called "Widget logic" that lets you control the pages that the widget will appear on.
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.
The text field lets you use WP's [Conditional Tags](http://codex.wordpress.org/Conditional_Tags), or any general PHP code.
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.
There is also an option to add a wordpress 'widget_content' filter -- this lets you tweak standard widgets to suit your theme without editing plugins and core code.
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.
= Donations =
If you like and use Widget Logic you could consider a small donation to Cancer Research UK. I have a [JustGiving.com donation link](http://www.justgiving.com/widgetlogic_cancerresearchuk) above with the aim of raising 500 UKP in the next 12 months.
If you like and use Widget Logic you could consider a small donation to Cancer Research UK. I have a [JustGiving.com donation link](http://www.justgiving.com/widgetlogic_cancerresearchuk). As of December 2011 we have raised 440 UKP. I'm going to aim to have upped that to 750 UKP by the end of 2012.
== Installation ==
1. Upload `widget-logic.php` to the `/wp-content/plugins/` directory
2. Activate the plugin through the 'Plugins' menu in WordPress
3. That's it. All the configuring is in the usual widget admin interface.
3. That's it. The configuring and options are in the usual widget admin interface.
= Configuration =
Aside from logic against your widgets, there are three options added to the foot of the widget admin page (see screenshots).
* 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/).
* 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
* 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):
* after the theme loads (after_setup_theme trigger)
* when all PHP loaded (wp_loaded trigger)
* during page header (wp_head trigger)
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.
== Frequently Asked Questions ==
......@@ -34,52 +49,77 @@ Try switching to the WP default theme - if the problem goes away, there is somet
The most common sources of problems are:
* The logic text on one of your widgets is invalid PHP
* Your theme doesn't call wp_head()
* Your theme performs custom queries before calling the dynamic sidebar -- if so, try ticking the `wp_reset_query` option.
= Widgets appear when they shouldn't =
= Use 'wp_reset_query' fix option isn't working properly any more =
It might be that your theme performs custom queries before calling the sidebar. Try the `wp_reset_query` option.
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.
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.
= What's this stuff in my sidebar when there are no widgets? =
Tighten up your definitions with PHPs 'logical AND' &&, for example:
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.
`is_single() && in_category('cheese')`
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.
= How do I get widget X on just my 'home' page? (Or on every page except that.) =
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).
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).
= Logic using is_page() doesn't work =
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).
= How do I get a widget to appear both on a category page and on single posts within that category? =
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:
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:
`(this page IS category X) OR (this is a single post AND this post is IN category X)`
which in proper PHP is:
`is_category(X) || (is_single() && in_category(X))`
See also: 'Writing Logic Code' in the [Other Notes](../other_notes/) section.
= How do I get a widget to appear when X, Y and Z? =
Have a go at it yourself first. Check out the 'Writing Logic Code' section under [Other Notes](../other_notes/).
= Why is Widget Logic so unfriendly, you have to be a code demon to use it? =
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).
The plugin [Widget Context](http://wordpress.org/extend/plugins/widget-context/) presents a nice UI and has a neat 'URL matching' function too.
= Widgets appear when they shouldn't =
It might be that your theme performs custom queries before calling the sidebar. Try the `wp_reset_query` option.
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.
Tighten up your definitions with PHPs 'logical AND' &&, for example:
`is_single() && in_category('cheese')`
== Screenshots ==
1. The 'Widget logic' field at work in standard widgets.
2. The `widget_content` filter and `wp_reset_query` options are at the foot of the widget admin page. (Both are off by default.)
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.
== Writing Logic Code ==
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).
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.)
If there is no 'return' in the text, an implicit 'return' is added to the start and a ';' is added on the end.
= The Basics =
Make good use of [WP's own conditional tags](http://codex.wordpress.org/Conditional_Tags). You can vary and combine code using:
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.
* `!` (NOT) to **reverse** the logic, eg `!is_home()` is TRUE when this is NOT the home page.
* `||` (OR) to **combine** conditions. `X OR Y` is TRUE when either X is true or Y is true.
* `&&` (AND) to make conditions **more specific**. `X AND Y` is TRUE when both X is true and Y is true.
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).
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).
Remember -- the code runs even if the widget doesn't appear. (Even if it never appears!)
Examples:
= Examples =
* `is_home()` -- just the main blog page
* `!is_page('about')` -- everywhere EXCEPT this specific WP 'page'
* `!is_user_logged_in()` -- shown when a user is not logged in
* `is_category(array(5,9,10,11))` -- category page of one of the given category IDs
* `is_single() && in_category('baked-goods')` -- single post that's in the category with this slug
* `current_user_can('level_10')` -- admin only widget
......@@ -102,7 +142,14 @@ where `[priority]` is the optional priority parameter for the [add_filter](http:
The second parameter ($widget_id) can be used to target specific widgets if needed.
_Example filters_
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:
= Example filters =
`add_filter('widget_content', 'basic_widget_content_filter');
function basic_widget_content_filter($content='')
{ return $content."<PRE>THIS APPEARS AFTER EVERY WIDGET</PRE>";
}`
I was motivated to make this filter in order to render all widget titles with the excellent [ttftitles plugin](http://templature.com/2007/10/18/ttftitles-wordpress-plugin/) like this:
......@@ -127,6 +174,20 @@ function make_alternating_widget_styles($content='')
== Changelog ==
= 0.52 =
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
= 0.51 =
One important bug fix (fairly major and fairly stupid of me too)
= 0.50 =
For the first time since this started on WP 2.3, I've rewritten how the core widget logic function works, so there may be 'bumps ahead'.
It now uses the 'sidebars_widgets' filter (as it should have done when that was
introduced in WP2.8 by the look of it). The upshot is that is_active_sidebar should behave properly.
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.)
= 0.48 =
Kill some poor coding practices that throws debug notices - thanks to John James Jacoby.
......
......@@ -4,171 +4,313 @@ Plugin Name: Widget Logic
Plugin URI: http://freakytrigger.co.uk/wordpress-setup/
Description: Control widgets with WP's conditional tags is_home etc
Author: Alan Trewartha
Version: 0.48
Version: 0.52
Author URI: http://freakytrigger.co.uk/author/alan/
*/
global $wl_options;
$wl_load_points=array( 'plugins_loaded' => 'when plugin starts (default)',
'after_setup_theme'=> 'after theme loads',
'wp_loaded' => 'when all PHP loaded',
'wp_head'=> 'during page header'
);
add_filter( 'widget_update_callback', 'widget_logic_widget_update_callback', 10, 3);
add_filter( "plugin_action_links", "wl_charity", 10, 2);
if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) ) $wl_options = array();
function wl_charity($links, $file) {
if ($file == plugin_basename(__FILE__))
array_push($links, '<a href="http://www.justgiving.com/widgetlogic_cancerresearchuk/">Charity Donation</a>');
return $links;
if (is_admin())
{
add_filter( 'widget_update_callback', 'widget_logic_ajax_update_callback', 10, 3); // widget changes submitted by ajax method
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
add_action( 'sidebar_admin_page', 'widget_logic_options_control'); // add Widget Logic specific options on the widget admin page
add_filter( 'plugin_action_links', 'wl_charity', 10, 2); // add my justgiving page link to the plugin admin page
}
else
{
if ( isset($wl_options['widget_logic-options-load_point']) &&
($wl_options['widget_logic-options-load_point']!='plugins_loaded') &&
array_key_exists($wl_options['widget_logic-options-load_point'],$wl_load_points )
)
add_action ($wl_options['widget_logic-options-load_point'],'widget_logic_sidebars_widgets_filter_add');
else
widget_logic_sidebars_widgets_filter_add();
if ( isset($wl_options['widget_logic-options-filter']) && $wl_options['widget_logic-options-filter'] == 'checked' )
add_filter( 'dynamic_sidebar_params', 'widget_logic_widget_display_callback', 10); // redirect the widget callback so the output can be buffered and filtered
}
// new in 2.8 - ajaxy update of a single widget
function widget_logic_widget_update_callback($instance, $new_instance, $this_widget)
{ $widget_id=$this_widget->id;
function widget_logic_sidebars_widgets_filter_add()
{
add_filter( 'sidebars_widgets', 'widget_logic_filter_sidebars_widgets', 10); // actually remove the widgets from the front end depending on widget logic provided
}
// wp-admin/widgets.php explicitly checks current_user_can('edit_theme_options')
// which is enough security, I believe. If you think otherwise please contact me
// CALLED VIA 'widget_update_callback' FILTER (ajax update of a widget)
function widget_logic_ajax_update_callback($instance, $new_instance, $this_widget)
{ global $wl_options;
$widget_id=$this_widget->id;
if ( isset($_POST[$widget_id.'-widget_logic']))
{ if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) ) $wl_options = array();
$wl_options[$widget_id]=$_POST[$widget_id.'-widget_logic'];
{ $wl_options[$widget_id]=$_POST[$widget_id.'-widget_logic'];
update_option('widget_logic', $wl_options);
}
return $instance;
}
add_action( 'sidebar_admin_setup', 'widget_logic_expand_control');
// CALLED VIA 'sidebar_admin_setup' ACTION
// adds in the admin control per widget, but also processes import/export
function widget_logic_expand_control()
{ global $wp_registered_widgets, $wp_registered_widget_controls;
{ global $wp_registered_widgets, $wp_registered_widget_controls, $wl_options;
// EXPORT ALL OPTIONS
if (isset($_GET['wl-options-export']))
{
header("Content-Disposition: attachment; filename=widget_logic_options.txt");
header('Content-Type: text/plain; charset=utf-8');
echo "[START=WIDGET LOGIC OPTIONS]\n";
foreach ($wl_options as $id => $text)
echo "$id\t".json_encode($text)."\n";
echo "[STOP=WIDGET LOGIC OPTIONS]";
exit;
}
// IMPORT ALL OPTIONS
if ( isset($_POST['wl-options-import']))
{ if ($_FILES['wl-options-import-file']['tmp_name'])
{ $import=split("\n",file_get_contents($_FILES['wl-options-import-file']['tmp_name'], false));
if (array_shift($import)=="[START=WIDGET LOGIC OPTIONS]" && array_pop($import)=="[STOP=WIDGET LOGIC OPTIONS]")
{ foreach ($import as $import_option)
{ list($key, $value)=split("\t",$import_option);
$wl_options[$key]=json_decode($value);
}
$wl_options['msg']="OK – options file imported";
}
else
{ $wl_options['msg']="Invalid options file";
}
}
else
$wl_options['msg']="No options file provided";
update_option('widget_logic', $wl_options);
wp_redirect( admin_url('widgets.php') );
exit;
}
if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) ) $wl_options = array();
// if we're just updating the widgets, just read in the widget logic settings - makes this WP2.5+ only i think
// ADD EXTRA WIDGET LOGIC FIELD TO EACH WIDGET CONTROL
// pop the widget id on the params array (as it's not in the main params so not provided to the callback)
foreach ( $wp_registered_widgets as $id => $widget )
{ // controll-less widgets need an empty function so the callback function is called.
if (!$wp_registered_widget_controls[$id])
wp_register_widget_control($id,$widget['name'], 'widget_logic_empty_control');
$wp_registered_widget_controls[$id]['callback_wl_redirect']=$wp_registered_widget_controls[$id]['callback'];
$wp_registered_widget_controls[$id]['callback']='widget_logic_extra_control';
array_push($wp_registered_widget_controls[$id]['params'],$id);
}
// UPDATE WIDGET LOGIC WIDGET OPTIONS (via accessibility mode?)
if ( 'post' == strtolower($_SERVER['REQUEST_METHOD']) )
{ foreach ( (array) $_POST['widget-id'] as $widget_number => $widget_id )
if (isset($_POST[$widget_id.'-widget_logic']))
$wl_options[$widget_id]=$_POST[$widget_id.'-widget_logic'];
// clean up empty options (in PHP5 use array_intersect_key)
$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'));
$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', 'widget_logic-options-load_point'));
foreach (array_keys($wl_options) as $key)
if (!in_array($key, $regd_plus_new))
unset($wl_options[$key]);
}
foreach ( $wp_registered_widgets as $id => $widget )
{ if (!$wp_registered_widget_controls[$id])
wp_register_widget_control($id,$widget['name'], 'widget_logic_empty_control');
if (!array_key_exists(0,$wp_registered_widget_controls[$id]['params']) || is_array($wp_registered_widget_controls[$id]['params'][0]))
$wp_registered_widget_controls[$id]['params'][0]['id_for_wl']=$id;
else
{ // some older widgets put number in to params directly (which messes up the 'templates' in WP2.5)
array_push($wp_registered_widget_controls[$id]['params'],$id);
$wp_registered_widget_controls[$id]['height']+=40; // this is really a pre2.5 thing - discard?
}
// do the redirection
$wp_registered_widget_controls[$id]['callback_wl_redirect']=$wp_registered_widget_controls[$id]['callback'];
$wp_registered_widget_controls[$id]['callback']='widget_logic_extra_control';
}
// check the 'widget content' filter option
// UPDATE OTHER WIDGET LOGIC OPTIONS
// must update this to use http://codex.wordpress.org/Settings_API
if ( isset($_POST['widget_logic-options-submit']) )
{ $wl_options['widget_logic-options-filter']=$_POST['widget_logic-options-filter'];
$wl_options['widget_logic-options-wp_reset_query']=$_POST['widget_logic-options-wp_reset_query'];
$wl_options['widget_logic-options-load_point']=$_POST['widget_logic-options-load_point'];
}
update_option('widget_logic', $wl_options);
}
add_action( 'sidebar_admin_page', 'widget_logic_options_filter');
function widget_logic_options_filter()
{
if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) ) $wl_options = array();
// CALLED VIA 'sidebar_admin_page' ACTION
// output extra HTML
// to update using http://codex.wordpress.org/Settings_API asap
function widget_logic_options_control()
{ global $wp_registered_widget_controls, $wl_options, $wl_load_points;
if ( isset($wl_options['msg']))
{ if (substr($wl_options['msg'],0,2)=="OK")
echo '<div id="message" class="updated">';
else
echo '<div id="message" class="error">';
echo '<p>Widget Logic – '.$wl_options['msg'].'</p></div>';
unset($wl_options['msg']);
update_option('widget_logic', $wl_options);
}
?><div class="wrap">
<form method="POST">
<h2>Widget Logic options</h2>
<p style="line-height: 30px;">
<h2>Widget Logic options</h2>
<form method="POST" style="float:left; width:45%">
<ul>
<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.">
<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'] ?>/>
Add 'widget_content' filter
</label>
</li>
<li><label for="widget_logic-options-wp_reset_query" title="Resets a theme's custom queries before your Widget Logic is checked">
<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'] ?> />
Use 'wp_reset_query' fix
</label>
</li>
<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
<select id="widget_logic-options-load_point" name="widget_logic-options-load_point" ><?php
foreach($wl_load_points as $action => $action_desc)
{ echo "<option value='".$action."'";
if ($action==$wl_options['widget_logic-options-load_point'])
echo " selected ";
echo ">".$action_desc."</option>"; //
}
?>
</select>
</label>
</li>
</ul>
<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
<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>
&nbsp;&nbsp;
<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
<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>
<?php submit_button( __( 'Save WL options' ), 'button-primary', 'widget_logic-options-submit', false ); ?>
<span class="submit"><input type="submit" name="widget_logic-options-submit" id="widget_logic-options-submit" value="Save" /></span></p>
</form>
<form method="POST" enctype="multipart/form-data" style="float:left; width:45%">
<a class="submit button" href="?wl-options-export" title="Save all WL options to a plain text config file">Export options</a><p>
<?php submit_button( __( 'Import options' ), 'button', 'wl-options-import', false,
array( 'title'=>'Load all WL options from a plain text config file'
) ); ?>
<input type="file" name="wl-options-import-file" id="wl-options-import-file" title="Select file for importing" /></p>
</form>
</div>
<?php
}
// added to widget functionality in 'widget_logic_expand_control' (above)
function widget_logic_empty_control() {}
// added to widget functionality in 'widget_logic_expand_control' (above)
function widget_logic_extra_control()
{ global $wp_registered_widget_controls;
$params=func_get_args();
{ global $wp_registered_widget_controls, $wl_options;
// find the widget id that we have sneaked into the params
$id=(is_array($params[0]))?$params[0]['id_for_wl']:array_pop($params);
$id_disp=$id;
$params=func_get_args();
$id=array_pop($params);
if((!$wl_options = get_option('widget_logic')) || !is_array($wl_options) ) $wl_options = array();
// go to the original control function
$callback=$wp_registered_widget_controls[$id]['callback_wl_redirect'];
if (is_callable($callback))
call_user_func_array($callback, $params); // go to the original control function
call_user_func_array($callback, $params);
$value = !empty( $wl_options[$id ] ) ? htmlspecialchars( stripslashes( $wl_options[$id ] ),ENT_QUOTES ) : '';
// dealing with multiple widgets - get the number. if -1 this is the 'template' for the admin interface
if (is_array($params[0]) && isset($params[0]['number'])) $number=$params[0]['number'];
$number=$params[0]['number'];
if ($number==-1) {$number="%i%"; $value="";}
$id_disp=$id;
if (isset($number)) $id_disp=$wp_registered_widget_controls[$id]['id_base'].'-'.$number;
// output our extra widget logic field
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>";
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>";
}
// CALLED ON 'plugin_action_links' ACTION
function wl_charity($links, $file)
{ if ($file == plugin_basename(__FILE__))
array_push($links, '<a href="http://www.justgiving.com/widgetlogic_cancerresearchuk/">Charity Donation</a>');
return $links;
}
// intercept registered widgets - redirect them and put each ID on the end of the params
// perhaps there is a way to just intercept the ones that are used??
add_action('wp_head', 'widget_logic_redirect_callback');
function widget_logic_redirect_callback()
{ global $wp_registered_widgets;
foreach ( $wp_registered_widgets as $id => $widget )
{ if ( empty( $wp_registered_widgets[$id]['callback_wl_redirect'] ) )
{ array_push($wp_registered_widgets[$id]['params'],$id);
$wp_registered_widgets[$id]['callback_wl_redirect']=$wp_registered_widgets[$id]['callback'];
$wp_registered_widgets[$id]['callback']='widget_logic_redirected_callback';
// FRONT END FUNCTIONS...
// CALLED ON 'sidebars_widgets' FILTER
function widget_logic_filter_sidebars_widgets($sidebars_widgets)
{ global $wp_reset_query_is_done, $wl_options;
// 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
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 ) )
{ wp_reset_query(); $wp_reset_query_is_done=true; }
// loop through every widget in every sidebar (barring 'wp_inactive_widgets') checking WL for each one
foreach($sidebars_widgets as $widget_area => $widget_list)
{ if ($widget_area=='wp_inactive_widgets' || empty($widget_list)) continue;
foreach($widget_list as $pos => $widget_id)
{
$wl_value=(!empty($wl_options[$widget_id]))? stripslashes($wl_options[$widget_id]) : "true";
$wl_value =(stristr($wl_value,"return"))? $wl_value: "return (" . $wl_value . ");";
if (!eval($wl_value))
unset($sidebars_widgets[$widget_area][$pos]);
}
}
return $sidebars_widgets;
}
// If 'widget_logic-options-filter' is selected the widget_content filter is implemented...
// CALLED ON 'dynamic_sidebar_params' FILTER - this is called during 'dynamic_sidebar' just before each callback is run
// swap out the original call back and replace it with our own
function widget_logic_widget_display_callback($params)
{ global $wp_registered_widgets;
$id=$params[0]['widget_id'];
$wp_registered_widgets[$id]['callback_wl_redirect']=$wp_registered_widgets[$id]['callback'];
$wp_registered_widgets[$id]['callback']='widget_logic_redirected_callback';
return $params;
}
// the redirection comes here
function widget_logic_redirected_callback()
{ global $wp_registered_widgets, $wp_reset_query_is_done;
$params=func_get_args(); // get all the passed params
$id=array_pop($params); // take off the widget ID
$callback=$wp_registered_widgets[$id]['callback_wl_redirect']; // find the real callback
$wl_options = get_option('widget_logic'); // do we want the widget?
$wl_value = ( !empty( $wl_options[$id] ) ) ? stripslashes( $wl_options[$id] ) : "true";
$wl_value = ( stristr( $wl_value, "return" ) ) ? $wl_value : "return (" . $wl_value . ");";
// before we execute the condtional code, perhaps we want to wp_reset_query...
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 ) )
{ wp_reset_query(); $wp_reset_query_is_done=true; }
// replace the original callback data
$params=func_get_args();
$id=$params[0]['widget_id'];
$callback=$wp_registered_widgets[$id]['callback_wl_redirect'];
$wp_registered_widgets[$id]['callback']=$callback;
$wl_value=(eval($wl_value) && is_callable($callback));
if ( $wl_value )
{ if ( !empty( $wl_options['widget_logic-options-filter'] ) && ( $wl_options['widget_logic-options-filter'] != 'checked' ) )
call_user_func_array($callback, $params); // if so callback with original params!
else
{ ob_start();
call_user_func_array($callback, $params); // if so callback with original params!
$widget_content = ob_get_contents();
ob_end_clean();
echo apply_filters( 'widget_content', $widget_content, $id);
}
// run the callback but capture and filter the output using PHP output buffering
if ( is_callable($callback) )
{ ob_start();
call_user_func_array($callback, $params);
$widget_content = ob_get_contents();
ob_end_clean();
echo apply_filters( 'widget_content', $widget_content, $id);
}
}
?>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment