PageRenderTime 52ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/themes/news/library/classes/widget-categories.php

https://bitbucket.org/lgorence/quickpress
PHP | 281 lines | 199 code | 29 blank | 53 comment | 5 complexity | 7106e3a00255048011779945e5d46ba4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * The Categories widget replaces the default WordPress Categories widget. This version gives total
  4. * control over the output to the user by allowing the input of all the arguments typically seen
  5. * in the wp_list_categories() function.
  6. *
  7. * @package Hybrid
  8. * @subpackage Classes
  9. * @author Justin Tadlock <justin@justintadlock.com>
  10. * @copyright Copyright (c) 2008 - 2012, Justin Tadlock
  11. * @link http://themehybrid.com/hybrid-core
  12. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  13. */
  14. /**
  15. * Categories Widget Class
  16. *
  17. * @since 0.6.0
  18. */
  19. class Hybrid_Widget_Categories extends WP_Widget {
  20. /**
  21. * Set up the widget's unique name, ID, class, description, and other options.
  22. *
  23. * @since 1.2.0
  24. */
  25. function __construct() {
  26. /* Set up the widget options. */
  27. $widget_options = array(
  28. 'classname' => 'categories',
  29. 'description' => esc_html__( 'An advanced widget that gives you total control over the output of your category links.', 'hybrid-core' )
  30. );
  31. /* Set up the widget control options. */
  32. $control_options = array(
  33. 'width' => 800,
  34. 'height' => 350
  35. );
  36. /* Create the widget. */
  37. $this->WP_Widget(
  38. 'hybrid-categories', // $this->id_base
  39. __( 'Categories', 'hybrid-core' ), // $this->name
  40. $widget_options, // $this->widget_options
  41. $control_options // $this->control_options
  42. );
  43. }
  44. /**
  45. * Outputs the widget based on the arguments input through the widget controls.
  46. *
  47. * @since 0.6.0
  48. */
  49. function widget( $sidebar, $instance ) {
  50. extract( $sidebar );
  51. /* Set the $args for wp_list_categories() to the $instance array. */
  52. $args = $instance;
  53. /* Set the $title_li and $echo arguments to false. */
  54. $args['title_li'] = false;
  55. $args['echo'] = false;
  56. /* Output the theme's widget wrapper. */
  57. echo $before_widget;
  58. /* If a title was input by the user, display it. */
  59. if ( !empty( $instance['title'] ) )
  60. echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title;
  61. /* Get the categories list. */
  62. $categories = str_replace( array( "\r", "\n", "\t" ), '', wp_list_categories( $args ) );
  63. /* If 'list' is the user-selected style, wrap the categories in an unordered list. */
  64. if ( 'list' == $args['style'] )
  65. $categories = '<ul class="xoxo categories">' . $categories . '</ul><!-- .xoxo .categories -->';
  66. /* Output the categories list. */
  67. echo $categories;
  68. /* Close the theme's widget wrapper. */
  69. echo $after_widget;
  70. }
  71. /**
  72. * Updates the widget control options for the particular instance of the widget.
  73. *
  74. * @since 0.6.0
  75. */
  76. function update( $new_instance, $old_instance ) {
  77. $instance = $old_instance;
  78. /* Set the instance to the new instance. */
  79. $instance = $new_instance;
  80. /* If new taxonomy is chosen, reset includes and excludes. */
  81. if ( $instance['taxonomy'] !== $old_instance['taxonomy'] && '' !== $old_instance['taxonomy'] ) {
  82. $instance['include'] = array();
  83. $instance['exclude'] = array();
  84. }
  85. $instance['title'] = strip_tags( $new_instance['title'] );
  86. $instance['taxonomy'] = $new_instance['taxonomy'];
  87. $instance['depth'] = strip_tags( $new_instance['depth'] );
  88. $instance['number'] = strip_tags( $new_instance['number'] );
  89. $instance['child_of'] = strip_tags( $new_instance['child_of'] );
  90. $instance['current_category'] = strip_tags( $new_instance['current_category'] );
  91. $instance['feed'] = strip_tags( $new_instance['feed'] );
  92. $instance['feed_image'] = esc_url( $new_instance['feed_image'] );
  93. $instance['search'] = strip_tags( $new_instance['search'] );
  94. $instance['include'] = preg_replace( '/[^0-9,]/', '', $new_instance['include'] );
  95. $instance['exclude'] = preg_replace( '/[^0-9,]/', '', $new_instance['exclude'] );
  96. $instance['exclude_tree'] = preg_replace( '/[^0-9,]/', '', $new_instance['exclude_tree'] );
  97. $instance['hierarchical'] = ( isset( $new_instance['hierarchical'] ) ? 1 : 0 );
  98. $instance['use_desc_for_title'] = ( isset( $new_instance['use_desc_for_title'] ) ? 1 : 0 );
  99. $instance['show_count'] = ( isset( $new_instance['show_count'] ) ? 1 : 0 );
  100. $instance['hide_empty'] = ( isset( $new_instance['hide_empty'] ) ? 1 : 0 );
  101. return $instance;
  102. }
  103. /**
  104. * Displays the widget control options in the Widgets admin screen.
  105. *
  106. * @since 0.6.0
  107. */
  108. function form( $instance ) {
  109. /* Set up the default form values. */
  110. $defaults = array(
  111. 'title' => esc_attr__( 'Categories', 'hybrid-core' ),
  112. 'taxonomy' => 'category',
  113. 'style' => 'list',
  114. 'include' => '',
  115. 'exclude' => '',
  116. 'exclude_tree' => '',
  117. 'child_of' => '',
  118. 'current_category' => '',
  119. 'search' => '',
  120. 'hierarchical' => true,
  121. 'hide_empty' => true,
  122. 'order' => 'ASC',
  123. 'orderby' => 'name',
  124. 'depth' => 0,
  125. 'number' => '',
  126. 'feed' => '',
  127. 'feed_type' => '',
  128. 'feed_image' => '',
  129. 'use_desc_for_title' => false,
  130. 'show_count' => false,
  131. );
  132. /* Merge the user-selected arguments with the defaults. */
  133. $instance = wp_parse_args( (array) $instance, $defaults );
  134. /* <select> element options. */
  135. $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'objects' );
  136. $terms = get_terms( $instance['taxonomy'] );
  137. $style = array( 'list' => esc_attr__( 'List', 'hybrid-core' ), 'none' => esc_attr__( 'None', 'hybrid-core' ) );
  138. $order = array( 'ASC' => esc_attr__( 'Ascending', 'hybrid-core' ), 'DESC' => esc_attr__( 'Descending', 'hybrid-core' ) );
  139. $orderby = array( 'count' => esc_attr__( 'Count', 'hybrid-core' ), 'ID' => esc_attr__( 'ID', 'hybrid-core' ), 'name' => esc_attr__( 'Name', 'hybrid-core' ), 'slug' => esc_attr__( 'Slug', 'hybrid-core' ), 'term_group' => esc_attr__( 'Term Group', 'hybrid-core' ) );
  140. $feed_type = array( '' => '', 'atom' => esc_attr__( 'Atom', 'hybrid-core' ), 'rdf' => esc_attr__( 'RDF', 'hybrid-core' ), 'rss' => esc_attr__( 'RSS', 'hybrid-core' ), 'rss2' => esc_attr__( 'RSS 2.0', 'hybrid-core' ) );
  141. ?>
  142. <div class="hybrid-widget-controls columns-3">
  143. <p>
  144. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'hybrid-core' ); ?></label>
  145. <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" />
  146. </p>
  147. <p>
  148. <label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><code>taxonomy</code></label>
  149. <select class="widefat" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>">
  150. <?php foreach ( $taxonomies as $taxonomy ) { ?>
  151. <option value="<?php echo esc_attr( $taxonomy->name ); ?>" <?php selected( $instance['taxonomy'], $taxonomy->name ); ?>><?php echo esc_html( $taxonomy->labels->singular_name ); ?></option>
  152. <?php } ?>
  153. </select>
  154. </p>
  155. <p>
  156. <label for="<?php echo $this->get_field_id( 'style' ); ?>"><code>style</code></label>
  157. <select class="widefat" id="<?php echo $this->get_field_id( 'style' ); ?>" name="<?php echo $this->get_field_name( 'style' ); ?>">
  158. <?php foreach ( $style as $option_value => $option_label ) { ?>
  159. <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['style'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option>
  160. <?php } ?>
  161. </select>
  162. </p>
  163. <p>
  164. <label for="<?php echo $this->get_field_id( 'order' ); ?>"><code>order</code></label>
  165. <select class="widefat" id="<?php echo $this->get_field_id( 'order' ); ?>" name="<?php echo $this->get_field_name( 'order' ); ?>">
  166. <?php foreach ( $order as $option_value => $option_label ) { ?>
  167. <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option>
  168. <?php } ?>
  169. </select>
  170. </p>
  171. <p>
  172. <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><code>orderby</code></label>
  173. <select class="widefat" id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>">
  174. <?php foreach ( $orderby as $option_value => $option_label ) { ?>
  175. <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['orderby'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option>
  176. <?php } ?>
  177. </select>
  178. </p>
  179. </div>
  180. <div class="hybrid-widget-controls columns-3">
  181. <p>
  182. <label for="<?php echo $this->get_field_id( 'depth' ); ?>"><code>depth</code></label>
  183. <input type="text" class="smallfat code" id="<?php echo $this->get_field_id( 'depth' ); ?>" name="<?php echo $this->get_field_name( 'depth' ); ?>" value="<?php echo esc_attr( $instance['depth'] ); ?>" />
  184. </p>
  185. <p>
  186. <label for="<?php echo $this->get_field_id( 'number' ); ?>"><code>number</code></label>
  187. <input type="text" class="smallfat code" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo esc_attr( $instance['number'] ); ?>" />
  188. </p>
  189. <p>
  190. <label for="<?php echo $this->get_field_id( 'include' ); ?>"><code>include</code></label>
  191. <input type="text" class="smallfat code" id="<?php echo $this->get_field_id( 'include' ); ?>" name="<?php echo $this->get_field_name( 'include' ); ?>" value="<?php echo esc_attr( $instance['include'] ); ?>" />
  192. </p>
  193. <p>
  194. <label for="<?php echo $this->get_field_id( 'exclude' ); ?>"><code>exclude</code></label>
  195. <input type="text" class="smallfat code" id="<?php echo $this->get_field_id( 'exclude' ); ?>" name="<?php echo $this->get_field_name( 'exclude' ); ?>" value="<?php echo esc_attr( $instance['exclude'] ); ?>" />
  196. </p>
  197. <p>
  198. <label for="<?php echo $this->get_field_id( 'exclude_tree' ); ?>"><code>exclude_tree</code></label>
  199. <input type="text" class="smallfat code" id="<?php echo $this->get_field_id( 'exclude_tree' ); ?>" name="<?php echo $this->get_field_name( 'exclude_tree' ); ?>" value="<?php echo esc_attr( $instance['exclude_tree'] ); ?>" />
  200. </p>
  201. <p>
  202. <label for="<?php echo $this->get_field_id( 'child_of' ); ?>"><code>child_of</code></label>
  203. <input type="text" class="smallfat code" id="<?php echo $this->get_field_id( 'child_of' ); ?>" name="<?php echo $this->get_field_name( 'child_of' ); ?>" value="<?php echo esc_attr( $instance['child_of'] ); ?>" />
  204. </p>
  205. <p>
  206. <label for="<?php echo $this->get_field_id( 'current_category' ); ?>"><code>current_category</code></label>
  207. <input type="text" class="smallfat code" id="<?php echo $this->get_field_id( 'current_category' ); ?>" name="<?php echo $this->get_field_name( 'current_category' ); ?>" value="<?php echo esc_attr( $instance['current_category'] ); ?>" />
  208. </p>
  209. <p>
  210. <label for="<?php echo $this->get_field_id( 'search' ); ?>"><code>search</code></label>
  211. <input type="text" class="widefat code" id="<?php echo $this->get_field_id( 'search' ); ?>" name="<?php echo $this->get_field_name( 'search' ); ?>" value="<?php echo esc_attr( $instance['search'] ); ?>" />
  212. </p>
  213. </div>
  214. <div class="hybrid-widget-controls columns-3 column-last">
  215. <p>
  216. <label for="<?php echo $this->get_field_id( 'feed' ); ?>"><code>feed</code></label>
  217. <input type="text" class="widefat code" id="<?php echo $this->get_field_id( 'feed' ); ?>" name="<?php echo $this->get_field_name( 'feed' ); ?>" value="<?php echo esc_attr( $instance['feed'] ); ?>" />
  218. </p>
  219. <p>
  220. <label for="<?php echo $this->get_field_id( 'feed_type' ); ?>"><code>feed_type</code></label>
  221. <select class="widefat" id="<?php echo $this->get_field_id( 'feed_type' ); ?>" name="<?php echo $this->get_field_name( 'feed_type' ); ?>">
  222. <?php foreach ( $feed_type as $option_value => $option_label ) { ?>
  223. <option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( $instance['feed_type'], $option_value ); ?>><?php echo esc_html( $option_label ); ?></option>
  224. <?php } ?>
  225. </select>
  226. </p>
  227. <p>
  228. <label for="<?php echo $this->get_field_id( 'feed_image' ); ?>"><code>feed_image</code></label>
  229. <input type="text" class="widefat code" id="<?php echo $this->get_field_id( 'feed_image' ); ?>" name="<?php echo $this->get_field_name( 'feed_image' ); ?>" value="<?php echo esc_attr( $instance['feed_image'] ); ?>" />
  230. </p>
  231. <p>
  232. <label for="<?php echo $this->get_field_id( 'hierarchical' ); ?>">
  233. <input class="checkbox" type="checkbox" <?php checked( $instance['hierarchical'], true ); ?> id="<?php echo $this->get_field_id( 'hierarchical' ); ?>" name="<?php echo $this->get_field_name( 'hierarchical' ); ?>" /> <?php _e( 'Hierarchical?', 'hybrid-core' ); ?> <code>hierarchical</code></label>
  234. </p>
  235. <p>
  236. <label for="<?php echo $this->get_field_id( 'use_desc_for_title' ); ?>">
  237. <input class="checkbox" type="checkbox" <?php checked( $instance['use_desc_for_title'], true ); ?> id="<?php echo $this->get_field_id( 'use_desc_for_title' ); ?>" name="<?php echo $this->get_field_name( 'use_desc_for_title' ); ?>" /> <?php _e( 'Use description?', 'hybrid-core' ); ?> <code>use_desc_for_title</code></label>
  238. </p>
  239. <p>
  240. <label for="<?php echo $this->get_field_id( 'show_count' ); ?>">
  241. <input class="checkbox" type="checkbox" <?php checked( $instance['show_count'], true ); ?> id="<?php echo $this->get_field_id( 'show_count' ); ?>" name="<?php echo $this->get_field_name( 'show_count' ); ?>" /> <?php _e( 'Show count?', 'hybrid-core' ); ?> <code>show_count</code></label>
  242. </p>
  243. <p>
  244. <label for="<?php echo $this->get_field_id( 'hide_empty' ); ?>">
  245. <input class="checkbox" type="checkbox" <?php checked( $instance['hide_empty'], true ); ?> id="<?php echo $this->get_field_id( 'hide_empty' ); ?>" name="<?php echo $this->get_field_name( 'hide_empty' ); ?>" /> <?php _e( 'Hide empty?', 'hybrid-core' ); ?> <code>hide_empty</code></label>
  246. </p>
  247. </div>
  248. <div style="clear:both;">&nbsp;</div>
  249. <?php
  250. }
  251. }
  252. ?>