<?php
/**
 * Handles widgets and widget areas
 *
 * WP widgets must be unregistered.  Hybrid widgets must be registered 
 * in their place. All widget areas are loaded and registered with WP.
 * Use register_sidebar() to create custom widget areas.
 * @link http://codex.wordpress.org/WordPress_Widgets_Api/register_sidebar
 *
 * @package Hybrid
 * @subpackage Widgets
 */

/**
 * Unregister WP widgets
 * @since 0.3.2
 */
add_action( 'widgets_init', 'hybrid_unregister_widgets' );

/**
 * Register Hybrid Widgets
 * @since 0.6
 */
add_action( 'widgets_init', 'hybrid_register_widgets' );

/**
 * Disables widget areas
 * @since 0.5
 */
add_filter( 'sidebars_widgets', 'remove_sidebars' );

/**
 * Register aside widget areas
 * @since 0.5
 */
register_sidebar( array( 'name' => __('Primary', 'hybrid'), 'id' => 'primary', 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
register_sidebar( array( 'name' => __('Secondary', 'hybrid'), 'id' => 'secondary', 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
register_sidebar( array( 'name' => __('Subsidiary', 'hybrid'), 'id' => 'subsidiary', 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );

/**
 * Register utility widget areas
 * @since 0.5
 */
register_sidebar( array( 'name' => __('Utility: Before Content', 'hybrid'), 'id' => 'utility-before-content', 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
register_sidebar( array( 'name' => __('Utility: After Content', 'hybrid'), 'id' => 'utility-after-content', 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
register_sidebar( array( 'name' => __('Utility: After Single', 'hybrid'), 'id' => 'utility-after-single', 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
register_sidebar( array( 'name' => __('Utility: After Page', 'hybrid'), 'id' => 'utility-after-page', 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
register_sidebar( array( 'name' => __('Widgets Template', 'hybrid'), 'id' => 'utility-widgets-template', 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );
register_sidebar( array( 'name' => __('404 Template', 'hybrid'), 'id' => 'utility-404', 'before_widget' => '<div id="%1$s" class="widget %2$s widget-%2$s"><div class="widget-inside">', 'after_widget' => '</div></div>', 'before_title' => '<h3 class="widget-title">', 'after_title' => '</h3>' ) );

/**
 * Register Hybrid's extra widgets. Each widget is meant to 
 * replace or extend the current default WordPress widgets.
 * @uses register_widget() Registers individual widgets.
 * @link http://codex.wordpress.org/WordPress_Widgets_Api
 *
 * @since 0.6
 */
function hybrid_register_widgets() {

	/* Load each widget file. */
	require_once( HYBRID_WIDGETS . '/archives.php' );
	require_once( HYBRID_WIDGETS . '/authors.php' );
	require_once( HYBRID_WIDGETS . '/bookmarks.php' );
	require_once( HYBRID_WIDGETS . '/calendar.php' );
	require_once( HYBRID_WIDGETS . '/categories.php' );
	require_once( HYBRID_WIDGETS . '/pages.php' );
	require_once( HYBRID_WIDGETS . '/search.php' );
	require_once( HYBRID_WIDGETS . '/tags.php' );

	/* Register each widget. */
	register_widget( 'Hybrid_Widget_Archives' );
	register_widget( 'Hybrid_Widget_Authors' );
	register_widget( 'Hybrid_Widget_Bookmarks' );
	register_widget( 'Hybrid_Widget_Calendar' );
	register_widget( 'Hybrid_Widget_Categories' );
	register_widget( 'Hybrid_Widget_Pages' );
	register_widget( 'Hybrid_Widget_Search' );
	register_widget( 'Hybrid_Widget_Tags' );
}

/**
 * Unregister default WordPress widgets we don't need.
 * The theme adds its own versions of these widgets.
 * @uses unregister_widget() Removes individual widgets.
 * @link http://codex.wordpress.org/WordPress_Widgets_Api
 *
 * @since 0.3.2
 */
function hybrid_unregister_widgets() {
	unregister_widget( 'WP_Widget_Pages' );
	unregister_widget( 'WP_Widget_Calendar' );
	unregister_widget( 'WP_Widget_Archives' );
	unregister_widget( 'WP_Widget_Links' );
	unregister_widget( 'WP_Widget_Categories' );
	unregister_widget( 'WP_Widget_Recent_Posts' );
	unregister_widget( 'WP_Widget_Search' );
	unregister_widget( 'WP_Widget_Tag_Cloud' );
}

/**
 * Loads the Primary widget area. Users can overwrite 'aside-primary.php'.
 * @uses locate_template() Checks for template in child and parent theme.
 *
 * @since 0.2.2
 */
function hybrid_get_primary() {
	locate_template( array( 'aside-primary.php' ), true );
}

/**
 * Loads the Secondary widget area. Users can overwrite 'aside-secondary.php'.
 * @uses locate_template() Checks for template in child and parent theme.
 *
 * @since 0.2.2
 */
function hybrid_get_secondary() {
	locate_template( array( 'aside-secondary.php' ), true );
}

/**
 * Loads the Subsidiary widget area. Users can overwrite 'aside-subsidiary.php'.
 * @uses locate_template() Checks for template in child and parent theme.
 *
 * @since 0.3.1
 */
function hybrid_get_subsidiary() {
	locate_template( array( 'aside-subsidiary.php' ), true );
}

/**
 * Loads the Utility: Before Content widget area. Users can overwrite 
 * 'utility-before-content.php' in child themes.
 * @uses locate_template() Checks for template in child and parent theme.
 *
 * @since 0.4
 */
function hybrid_get_utility_before_content() {
	locate_template( array( 'utility-before-content.php' ), true );
}

/**
 * Loads the Utility: After Content widget area. Users can overwrite 
 * 'utility-after-content.php' in child themes.
 * @uses locate_template() Checks for template in child and parent theme.
 *
 * @since 0.4
 */
function hybrid_get_utility_after_content() {
	locate_template( array( 'utility-after-content.php' ), true );
}

/**
 * Loads the Utility: After Single widget area. Users can overwrite 
 * 'utility-after-single.php' in child themes.
 * @uses locate_template() Checks for template in child and parent theme.
 *
 * @since 0.4
 */
function hybrid_get_utility_after_single() {
	locate_template( array( 'utility-after-single.php' ), true );
}

/**
 * Loads the Utility: After Page widget area. Users can overwrite 
 * 'utility-after-page.php' in child themes.
 * @uses locate_template() Checks for template in child and parent theme.
 *
 * @since 0.4
 */
function hybrid_get_utility_after_page() {
	locate_template( array( 'utility-after-page.php' ), true );
}

/**
 * Check for widgets in widget-ready areas. Allows user to completely
 * collapse widget-ready areas by not adding widgets to particular widget areas.
 * Checks widget areas by name and/or ID.
 *
 * Realizing that WordPress 2.8 introduced is_active_sidebar(), we're still using this
 * function because the WP function doesn't perform correctly.  See ticket #10136.
 * @link http://core.trac.wordpress.org/ticket/10136
 *
 * Major props to Chaos Kaizer and Ian Stewart.
 * @link http://wordpress.org/support/topic/190184#post-808787
 * @link http://blog.kaizeku.com
 * @link http://themeshaper.com/collapsing-wordpress-widget-ready-areas-sidebars
 *
 * @deprecated 0.6.2
 *
 * @since 0.2
 * @param string|int $index name|ID of widget area.
 * @return bool
 */
function is_sidebar_active( $index = 1 ) {
	$sidebars_widgets = wp_get_sidebars_widgets();

	$index = ( is_int( $index ) ) ? "sidebar-$index" : sanitize_title( $index );

	if ( isset( $sidebars_widgets[$index] ) && !empty( $sidebars_widgets[$index] ) )
		return true;
  
	return false;
}

/**
 * Removes all widget areas on the No Widgets page template.
 * @uses sidebars_widgets Filter to remove all widget areas
 *
 * We're only going to run it on the No Widgets template. Users that 
 * need additional templates without widgets should create a simliar 
 * function in their child theme.
 *
 * We're retaining the 'sidebar' terminology until such time that the 
 * WordPress community decides on a better term.
 *
 * @since 0.5
 */
function remove_sidebars( $sidebars_widgets ) {
	if ( is_page_template( 'no-widgets.php' ) )
		$sidebars_widgets = array( false );
	return $sidebars_widgets;
}

?>