[wp-trac] Re: [WordPress Trac] #9701: Pull the "Recent Comments"
code out of the widget and into it's own function
WordPress Trac
wp-trac at lists.automattic.com
Sun May 3 21:21:11 GMT 2009
#9701: Pull the "Recent Comments" code out of the widget and into it's own
function
--------------------------+-------------------------------------------------
Reporter: Viper007Bond | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Future Release
Component: Widgets | Version: 2.8
Severity: normal | Keywords: needs-patch
--------------------------+-------------------------------------------------
Comment(by hakre):
well your code is a static function call with hardcoded values. You want
to code that fragment multiple times to mimic every widget? even the ones
you do not know the classnames or widget names from? and what about
configuring $args?
if you take that into account, i doub you would name the code
"extraordinarily complex".
with my suggestion i wanted to ensure that the widget works as it should,
like an instance of a widget, in it's own sidebar environment, fully
configureable and callable by widget-name, not only by class.
this is done sothat it works with _any_ registered widget. and it is
possible to configure the sidebar args. your example uses hardcoded values
therefore. next to this, the standard filter that can decide wether or not
a widget should be displayed has been implemented to better mimic the
stand widget display procedure.
the rest in the attachment is just plugin and test-widget code. so
basically, this one function is how it looks like, and I do not think it
is that complex:
{{{
/** widget template tag
*
* get a configured widgets output without the need of a sidebar and
backend administration.
*
* NOTE: this is development code
*
* @global WP_Widget_Factory $wp_widget_factory
* @global array $wp_registered_sidebars
*
* @param string $widget widget identifier
* @param array $settings optional widget settings
* @param array $args optional sidebar options
* @return void
*/
function the_widget($widget, $settings = array(), $args = array()) {
/* sanitize input */
$widget = (string) $widget;
if (!strlen($widget))
return;
if (!is_array($settings))
return;
if (!is_array($args))
return;
/* globals */
global $wp_widget_factory, $wp_registered_sidebars;
/* pull default (def) sidebar options (args) */
$sidebar_id = register_sidebar( array('name' => uniqid('temp-'))
);
$def = $wp_registered_sidebars[$sidebar_id];
$args = array_merge($def, $args);
/* get widget ($instance) */
$instance = null;
if (isset($wp_widget_factory->widgets[$widget])) {
$instance = $wp_widget_factory->widgets[$widget];
} else {
foreach($wp_widget_factory->widgets as $value)
if (isset($value->name))
if ($value->name == $widget) {
$instance = $value;
break;
}
}
/* display widget */
if (is_a($instance, 'WP_Widget')) {
$settings = apply_filters('widget_display_callback',
$settings, $instance);
if ( false !== $settings )
$instance->widget($args, $settings);
}
unregister_sidebar($sidebar_id);
}
}}}
--
Ticket URL: <http://core.trac.wordpress.org/ticket/9701#comment:8>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list