* @copyright Copyright (c) 2008 - 2014, Justin Tadlock * @link http://themehybrid.com/hybrid-core * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html */ /** * Categories Widget Class * * @since 0.6.0 */ class Hybrid_Widget_Categories extends WP_Widget { /** * Default arguments for the widget settings. * * @since 2.0.0 * @access public * @var array */ public $defaults = array(); /** * Set up the widget's unique name, ID, class, description, and other options. * * @since 1.2.0 * @access public * @return void */ function __construct() { /* Set up the widget options. */ $widget_options = array( 'classname' => 'widget-categories widget_categories', 'description' => esc_html__( 'An advanced widget that gives you total control over the output of your category links.', 'hybrid-core' ) ); /* Set up the widget control options. */ $control_options = array( 'width' => 800, 'height' => 350 ); /* Create the widget. */ $this->WP_Widget( 'hybrid-categories', __( 'Categories', 'hybrid-core' ), $widget_options, $control_options ); /* Set up the defaults. */ $this->defaults = array( 'title' => esc_attr__( 'Categories', 'hybrid-core' ), 'taxonomy' => 'category', 'style' => 'list', 'include' => '', 'exclude' => '', 'exclude_tree' => '', 'child_of' => '', 'current_category' => '', 'search' => '', 'hierarchical' => true, 'hide_empty' => true, 'order' => 'ASC', 'orderby' => 'name', 'depth' => 0, 'number' => '', 'feed' => '', 'feed_type' => '', 'feed_image' => '', 'use_desc_for_title' => false, 'show_count' => false, ); } /** * Outputs the widget based on the arguments input through the widget controls. * * @since 0.6.0 * @access public * @param array $sidebar * @param array $instance * @return void */ function widget( $sidebar, $instance ) { /* Set the $args for wp_list_categories() to the $instance array. */ $args = wp_parse_args( $instance, $this->defaults ); /* Set the $title_li and $echo arguments to false. */ $args['title_li'] = false; $args['echo'] = false; /* Output the sidebar's $before_widget wrapper. */ echo $sidebar['before_widget']; /* If a title was input by the user, display it. */ if ( !empty( $args['title'] ) ) echo $sidebar['before_title'] . apply_filters( 'widget_title', $args['title'], $instance, $this->id_base ) . $sidebar['after_title']; /* Get the categories list. */ $categories = str_replace( array( "\r", "\n", "\t" ), '', wp_list_categories( $args ) ); /* If 'list' is the user-selected style, wrap the categories in an unordered list. */ if ( 'list' == $args['style'] ) $categories = ''; /* If no style is given, wrap in a

tag for formatting. */ else $categories = '

' . $categories . '

'; /* Output the categories list. */ echo $categories; /* Close the sidebar's widget wrapper. */ echo $sidebar['after_widget']; } /** * The update callback for the widget control options. This method is used to sanitize and/or * validate the options before saving them into the database. * * @since 0.6.0 * @access public * @param array $new_instance * @param array $old_instance * @return array */ function update( $new_instance, $old_instance ) { /* If new taxonomy is chosen, reset includes and excludes. */ if ( $new_instance['taxonomy'] !== $old_instance['taxonomy'] ) $new_instance['include'] = $new_instance['exclude'] = ''; /* Sanitize key. */ $instance['taxonomy'] = sanitize_key( $new_instance['taxonomy'] ); /* Strip tags. */ $instance['title'] = strip_tags( $new_instance['title'] ); $instance['search'] = strip_tags( $new_instance['search'] ); $instance['feed'] = strip_tags( $new_instance['feed'] ); /* Whitelist options. */ $order = array( 'ASC', 'DESC' ); $orderby = array( 'count', 'ID', 'name', 'slug', 'term_group' ); $style = array( 'list', 'none' ); $feed_type = array( '', 'atom', 'rdf', 'rss', 'rss2' ); $instance['order'] = in_array( $new_instance['order'], $order ) ? $new_instance['order'] : 'ASC'; $instance['orderby'] = in_array( $new_instance['orderby'], $orderby ) ? $new_instance['orderby'] : 'name'; $instance['style'] = in_array( $new_instance['style'], $style ) ? $new_instance['style'] : 'list'; $instance['feed_type'] = in_array( $new_instance['feed_type'], $feed_type ) ? $new_instance['feed_type'] : ''; /* Integers. */ $instance['number'] = intval( $new_instance['number'] ); $instance['depth'] = absint( $new_instance['depth'] ); $instance['child_of'] = absint( $new_instance['child_of'] ); $instance['current_category'] = absint( $new_instance['current_category'] ); /* Only allow integers and commas. */ $instance['include'] = preg_replace( '/[^0-9,]/', '', $new_instance['include'] ); $instance['exclude'] = preg_replace( '/[^0-9,]/', '', $new_instance['exclude'] ); $instance['exclude_tree'] = preg_replace( '/[^0-9,]/', '', $new_instance['exclude_tree'] ); /* URLs. */ $instance['feed_image'] = esc_url_raw( $new_instance['feed_image'] ); /* Checkboxes. */ $instance['hierarchical'] = isset( $new_instance['hierarchical'] ) ? 1 : 0; $instance['use_desc_for_title'] = isset( $new_instance['use_desc_for_title'] ) ? 1 : 0; $instance['show_count'] = isset( $new_instance['show_count'] ) ? 1 : 0; $instance['hide_empty'] = isset( $new_instance['hide_empty'] ) ? 1 : 0; /* Return sanitized options. */ return $instance; } /** * Displays the widget control options in the Widgets admin screen. * * @since 0.6.0 * @access public * @param array $instance * @param void */ function form( $instance ) { /* Merge the user-selected arguments with the defaults. */ $instance = wp_parse_args( (array) $instance, $this->defaults ); /*