Skip to content

Instantly share code, notes, and snippets.

@hpaul
Created July 10, 2012 20:37
Show Gist options
  • Select an option

  • Save hpaul/3086093 to your computer and use it in GitHub Desktop.

Select an option

Save hpaul/3086093 to your computer and use it in GitHub Desktop.
Widget class
<?php
/**
* Widget class library
*
* @author Paul Hrimiuc <[email protected]>
*/
class Widget {
/**
* All sidebar registered.
*
* @var array
*/
public static $sidebars = array();
/**
* All registered widgets. Every sidebar will
* have an array in this var.
*
* @var array
*/
public static $widgets = array();
/**
* Register a sidebar
*
* This check if the sidebar is already registered.
* If don't exist create a $id_name sidebar with
* basic args and register widget basic name.
*
* <code>
* // Register a sidebar
* Widget::register_sidebar('index', 'Sidebar Index', array('class' => 'index'));
* </code>
*
* @param string $id_name
* @param string $name
* @param array $args
* @return bolean
*/
public static function register_sidebar($id_name, $name, $args = array())
{
//Check if sidebar is registered and add it to sidebar array
if( ! isset(self::$sidebars[$id_name]) )
{
$side_args = array(
'description' => '',
'class' => '',
'id' => $id_name,
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="title">',
'after_title' => '</h2>',
);
$args = $args + $side_args;
self::$sidebars[$id_name] = array(
'name' => $name,
'args' => $args,
);
//Need to define the sidebar in widgets var
//Else if sidebar is called and there are
//no widgets, class will throw an error
self::$widgets[$id_name] = array();
return TRUE;
}
return FALSE;
}
/**
* Register a widget based on a sidebar
*
* This check if the widget is already registered.
* If don't exist create a widget based on title or
* custom name. The content can be a Anonymous function too.
*
* <code>
* // Register latest-posts widget on index sidebar
* Widget::register('Latest posts', 'index', 'nested content for latest posts');
* </code>
*
* @param string $title
* @param string $sidebar_name
* @param closure/string $instance
* @param int $order
* @param string $id_base //custom widget name
* @return bolean
*/
public static function register($title, $sidebar_name, $instance, $order = false, $id_base = false, $widget_options = array())
{
$id_base = empty($id_base) ? Str::slug($title) : strtolower($id_base);
if( isset(self::$sidebars[$sidebar_name]) )
{
$order = empty($order) ? count(self::$widgets[$sidebar_name]) + 1 : $order;
if( ! isset(self::$widgets[$sidebar_name][$id_base]) )
{
self::$widgets[$sidebar_name][$id_base] = array(
'order' => $order,
'title' => $title,
'content' => $instance,
'options' => $widget_options,
);
return TRUE;
}
}
return FALSE;
}
/**
* Show sidebar widgets
*
* Will return the nestest string with all widget in sidebar.
*
* <code>
* // Echo the sidebar widgets content
* echo Widget::sidebar('index');
* </code>
*
* @param string $id_name
* @return bolean
*/
public static function sidebar($id_name)
{
if( isset(self::$sidebars[$id_name]) )
{
$sidebar = self::$sidebars[$id_name];
$sort = function($field)
{
$code = "return strnatcmp(\$a['$field'], \$b['$field']);";
return create_function('$a,$b', $code);
};
usort(self::$widgets[$id_name], $sort('order'));
$output = '<div id="sidebar-'.$sidebar['args']['id'].'" class="'.$sidebar['args']['class'].'">';
foreach(self::$widgets[$id_name] as $widget)
{
$output .= $sidebar['args']['before_widget'];
$output .= $sidebar['args']['before_title'];
$output .= $widget['title'];
$output .= $sidebar['args']['after_title'];
if( $widget['content'] instanceof Closure)
{
$output .= call_user_func($widget['content']);
}
else
{
$output .= $widget['content'];
}
$output .= $sidebar['args']['after_widget'];
}
$output .= '</div>';
return $output;
}
else
{
return FALSE;
}
}
public static function order_widget($sidebar, $widget, $order)
{
if( isset(self::$sidebars[$sidebar]) )
{
if( isset(self::$widgets[$sidebar][$widget]) )
{
self::$widgets[$sidebar][$widget]['order'] = $order;
return TRUE;
}
}
return FALSE;
}
public static function mass_order($sidebar, $order)
{
if( isset(self::$sidebars[$sidebar]) )
{
$order = json_decode($order);
$i = 0;
foreach(self::$widgets[$sidebar] as $widget)
{
$widget['order'] = $order[$i++];
}
return TRUE;
}
return FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment