PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lgorence/quickpress
PHP | 300 lines | 215 code | 31 blank | 54 comment | 3 complexity | 501940ecd8522006d31c241c1e451bde MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * The Tags widget replaces the default WordPress Tag Cloud 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_tag_cloud() 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. * Tags Widget Class
  16. *
  17. * @since 0.6.0
  18. */
  19. class Hybrid_Widget_Tags 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' => 'tags',
  29. 'description' => esc_html__( 'An advanced widget that gives you total control over the output of your tags.', '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-tags', // $this->id_base
  39. __( 'Tags', '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_tag_cloud() to the $instance array. */
  52. $args = $instance;
  53. /* Make sure empty callbacks aren't passed for custom functions. */
  54. $args['topic_count_text_callback'] = !empty( $args['topic_count_text_callback'] ) ? $args['topic_count_text_callback'] : 'default_topic_count_text';
  55. $args['topic_count_scale_callback'] = !empty( $args['topic_count_scale_callback'] ) ? $args['topic_count_scale_callback'] : 'default_topic_count_scale';
  56. /* If the separator is empty, set it to the default new line. */
  57. $args['separator'] = !empty( $args['separator'] ) ? $args['separator'] : "\n";
  58. /* Overwrite the echo argument. */
  59. $args['echo'] = false;
  60. /* Output the theme's $before_widget wrapper. */
  61. echo $before_widget;
  62. /* If a title was input by the user, display it. */
  63. if ( !empty( $instance['title'] ) )
  64. echo $before_title . apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ) . $after_title;
  65. /* Get the tag cloud. */
  66. $tags = str_replace( array( "\r", "\n", "\t" ), ' ', wp_tag_cloud( $args ) );
  67. /* If $format should be flat, wrap it in the <p> element. */
  68. if ( 'flat' == $instance['format'] ) {
  69. $classes = array( 'term-cloud' );
  70. foreach ( $instance['taxonomy'] as $tax )
  71. $classes[] = sanitize_html_class( "{$tax}-cloud" );
  72. $tags = '<p class="' . join( $classes, ' ' ) . '">' . $tags . '</p>';
  73. }
  74. /* Output the tag cloud. */
  75. echo $tags;
  76. /* Close the theme's widget wrapper. */
  77. echo $after_widget;
  78. }
  79. /**
  80. * Updates the widget control options for the particular instance of the widget.
  81. *
  82. * @since 0.6.0
  83. */
  84. function update( $new_instance, $old_instance ) {
  85. $instance = $old_instance;
  86. /* Set the instance to the new instance. */
  87. $instance = $new_instance;
  88. $instance['title'] = strip_tags( $new_instance['title'] );
  89. $instance['smallest'] = strip_tags( $new_instance['smallest'] );
  90. $instance['largest'] = strip_tags( $new_instance['largest'] );
  91. $instance['number'] = strip_tags( $new_instance['number'] );
  92. $instance['separator'] = strip_tags( $new_instance['separator'] );
  93. $instance['name__like'] = strip_tags( $new_instance['name__like'] );
  94. $instance['search'] = strip_tags( $new_instance['search'] );
  95. $instance['child_of'] = strip_tags( $new_instance['child_of'] );
  96. $instance['parent'] = strip_tags( $new_instance['parent'] );
  97. $instance['topic_count_text_callback'] = strip_tags( $new_instance['topic_count_text_callback'] );
  98. $instance['topic_count_scale_callback'] = strip_tags( $new_instance['topic_count_scale_callback'] );
  99. $instance['include'] = preg_replace( '/[^0-9,]/', '', $new_instance['include'] );
  100. $instance['exclude'] = preg_replace( '/[^0-9,]/', '', $new_instance['exclude'] );
  101. $instance['unit'] = $new_instance['unit'];
  102. $instance['format'] = $new_instance['format'];
  103. $instance['orderby'] = $new_instance['orderby'];
  104. $instance['order'] = $new_instance['order'];
  105. $instance['taxonomy'] = $new_instance['taxonomy'];
  106. $instance['link'] = $new_instance['link'];
  107. $instance['pad_counts'] = ( isset( $new_instance['pad_counts'] ) ? 1 : 0 );
  108. $instance['hide_empty'] = ( isset( $new_instance['hide_empty'] ) ? 1 : 0 );
  109. return $instance;
  110. }
  111. /**
  112. * Displays the widget control options in the Widgets admin screen.
  113. *
  114. * @since 0.6.0
  115. */
  116. function form( $instance ) {
  117. /* Set up the default form values. */
  118. $defaults = array(
  119. 'title' => esc_attr__( 'Tags', 'hybrid-core' ),
  120. 'order' => 'ASC',
  121. 'orderby' => 'name',
  122. 'format' => 'flat',
  123. 'include' => '',
  124. 'exclude' => '',
  125. 'unit' => 'pt',
  126. 'smallest' => 8,
  127. 'largest' => 22,
  128. 'link' => 'view',
  129. 'number' => 45,
  130. 'separator' => ' ',
  131. 'child_of' => '',
  132. 'parent' => '',
  133. 'taxonomy' => array( 'post_tag' ),
  134. 'hide_empty' => 1,
  135. 'pad_counts' => false,
  136. 'search' => '',
  137. 'name__like' => '',
  138. 'topic_count_text_callback' => 'default_topic_count_text',
  139. 'topic_count_scale_callback' => 'default_topic_count_scale',
  140. );
  141. /* Merge the user-selected arguments with the defaults. */
  142. $instance = wp_parse_args( (array) $instance, $defaults );
  143. /* <select> element options. */
  144. $taxonomies = get_taxonomies( array( 'show_tagcloud' => true ), 'objects' );
  145. $link = array( 'view' => esc_attr__( 'View', 'hybrid-core' ), 'edit' => esc_attr__( 'Edit', 'hybrid-core' ) );
  146. $format = array( 'flat' => esc_attr__( 'Flat', 'hybrid-core' ), 'list' => esc_attr__( 'List', 'hybrid-core' ) );
  147. $order = array( 'ASC' => esc_attr__( 'Ascending', 'hybrid-core' ), 'DESC' => esc_attr__( 'Descending', 'hybrid-core' ), 'RAND' => esc_attr__( 'Random', 'hybrid-core' ) );
  148. $orderby = array( 'count' => esc_attr__( 'Count', 'hybrid-core' ), 'name' => esc_attr__( 'Name', 'hybrid-core' ) );
  149. $unit = array( 'pt' => 'pt', 'px' => 'px', 'em' => 'em', '%' => '%' );
  150. ?>
  151. <div class="hybrid-widget-controls columns-3">
  152. <p>
  153. <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'hybrid-core' ); ?></label>
  154. <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'] ); ?>" />
  155. </p>
  156. <p>
  157. <label for="<?php echo $this->get_field_id( 'taxonomy' ); ?>"><code>taxonomy</code></label>
  158. <select class="widefat" id="<?php echo $this->get_field_id( 'taxonomy' ); ?>" name="<?php echo $this->get_field_name( 'taxonomy' ); ?>[]" size="4" multiple="multiple">
  159. <?php foreach ( $taxonomies as $taxonomy ) { ?>
  160. <option value="<?php echo $taxonomy->name; ?>" <?php selected( in_array( $taxonomy->name, (array)$instance['taxonomy'] ) ); ?>><?php echo $taxonomy->labels->singular_name; ?></option>
  161. <?php } ?>
  162. </select>
  163. </p>
  164. <p>
  165. <label for="<?php echo $this->get_field_id( 'format' ); ?>"><code>format</code></label>
  166. <select class="widefat" id="<?php echo $this->get_field_id( 'format' ); ?>" name="<?php echo $this->get_field_name( 'format' ); ?>">
  167. <?php foreach ( $format as $option_value => $option_label ) { ?>
  168. <option value="<?php echo $option_value; ?>" <?php selected( $instance['format'], $option_value ); ?>><?php echo $option_label; ?></option>
  169. <?php } ?>
  170. </select>
  171. </p>
  172. <p>
  173. <label for="<?php echo $this->get_field_id( 'order' ); ?>"><code>order</code></label>
  174. <select class="widefat" id="<?php echo $this->get_field_id( 'order' ); ?>" name="<?php echo $this->get_field_name( 'order' ); ?>">
  175. <?php foreach ( $order as $option_value => $option_label ) { ?>
  176. <option value="<?php echo $option_value; ?>" <?php selected( $instance['order'], $option_value ); ?>><?php echo $option_label; ?></option>
  177. <?php } ?>
  178. </select>
  179. </p>
  180. <p>
  181. <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><code>orderby</code></label>
  182. <select class="widefat" id="<?php echo $this->get_field_id( 'orderby' ); ?>" name="<?php echo $this->get_field_name( 'orderby' ); ?>">
  183. <?php foreach ( $orderby as $option_value => $option_label ) { ?>
  184. <option value="<?php echo $option_value; ?>" <?php selected( $instance['orderby'], $option_value ); ?>><?php echo $option_label; ?></option>
  185. <?php } ?>
  186. </select>
  187. </p>
  188. </div>
  189. <div class="hybrid-widget-controls columns-3">
  190. <p>
  191. <label for="<?php echo $this->get_field_id( 'include' ); ?>"><code>include</code></label>
  192. <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'] ); ?>" />
  193. </p>
  194. <p>
  195. <label for="<?php echo $this->get_field_id( 'exclude' ); ?>"><code>exclude</code></label>
  196. <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'] ); ?>" />
  197. </p>
  198. <p>
  199. <label for="<?php echo $this->get_field_id( 'number' ); ?>"><code>number</code></label>
  200. <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'] ); ?>" />
  201. </p>
  202. <p>
  203. <label for="<?php echo $this->get_field_id( 'largest' ); ?>"><code>largest</code></label>
  204. <input type="text" class="smallfat code" id="<?php echo $this->get_field_id( 'largest' ); ?>" name="<?php echo $this->get_field_name( 'largest' ); ?>" value="<?php echo esc_attr( $instance['largest'] ); ?>" />
  205. </p>
  206. <p>
  207. <label for="<?php echo $this->get_field_id( 'smallest' ); ?>"><code>smallest</code></label>
  208. <input type="text" class="smallfat code" id="<?php echo $this->get_field_id( 'smallest' ); ?>" name="<?php echo $this->get_field_name( 'smallest' ); ?>" value="<?php echo esc_attr( $instance['smallest'] ); ?>" />
  209. </p>
  210. <p>
  211. <label for="<?php echo $this->get_field_id( 'unit' ); ?>"><code>unit</code></label>
  212. <select class="smallfat" id="<?php echo $this->get_field_id( 'unit' ); ?>" name="<?php echo $this->get_field_name( 'unit' ); ?>">
  213. <?php foreach ( $unit as $option_value => $option_label ) { ?>
  214. <option value="<?php echo $option_value; ?>" <?php selected( $instance['unit'], $option_value ); ?>><?php echo $option_label; ?></option>
  215. <?php } ?>
  216. </select>
  217. </p>
  218. <p>
  219. <label for="<?php echo $this->get_field_id( 'separator' ); ?>"><code>separator</code></label>
  220. <input type="text" class="smallfat code" id="<?php echo $this->get_field_id( 'separator' ); ?>" name="<?php echo $this->get_field_name( 'separator' ); ?>" value="<?php echo esc_attr( $instance['separator'] ); ?>" />
  221. </p>
  222. <p>
  223. <label for="<?php echo $this->get_field_id( 'child_of' ); ?>"><code>child_of</code></label>
  224. <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'] ); ?>" />
  225. </p>
  226. <p>
  227. <label for="<?php echo $this->get_field_id( 'parent' ); ?>"><code>parent</code></label>
  228. <input type="text" class="smallfat code" id="<?php echo $this->get_field_id( 'parent' ); ?>" name="<?php echo $this->get_field_name( 'parent' ); ?>" value="<?php echo esc_attr( $instance['parent'] ); ?>" />
  229. </p>
  230. <p>
  231. <label for="<?php echo $this->get_field_id( 'link' ); ?>"><code>link</code></label>
  232. <select class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>">
  233. <?php foreach ( $link as $option_value => $option_label ) { ?>
  234. <option value="<?php echo $option_value; ?>" <?php selected( $instance['link'], $option_value ); ?>><?php echo $option_label; ?></option>
  235. <?php } ?>
  236. </select>
  237. </p>
  238. </div>
  239. <div class="hybrid-widget-controls columns-3 column-last">
  240. <p>
  241. <label for="<?php echo $this->get_field_id( 'search' ); ?>"><code>search</code></label>
  242. <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'] ); ?>" />
  243. </p>
  244. <p>
  245. <label for="<?php echo $this->get_field_id( 'name__like' ); ?>"><code>name__like</code></label>
  246. <input type="text" class="widefat code" id="<?php echo $this->get_field_id( 'name__like' ); ?>" name="<?php echo $this->get_field_name( 'name__like' ); ?>" value="<?php echo esc_attr( $instance['name__like'] ); ?>" />
  247. </p>
  248. <p>
  249. <label for="<?php echo $this->get_field_id( 'topic_count_text_callback' ); ?>"><code>topic_count_text_callback</code></label>
  250. <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'topic_count_text_callback' ); ?>" name="<?php echo $this->get_field_name( 'topic_count_text_callback' ); ?>" value="<?php echo esc_attr( $instance['topic_count_text_callback'] ); ?>" />
  251. </p>
  252. <p>
  253. <label for="<?php echo $this->get_field_id( 'topic_count_scale_callback' ); ?>"><code>topic_count_scale_callback</code></label>
  254. <input type="text" class="widefat" id="<?php echo $this->get_field_id( 'topic_count_scale_callback' ); ?>" name="<?php echo $this->get_field_name( 'topic_count_scale_callback' ); ?>" value="<?php echo esc_attr( $instance['topic_count_scale_callback'] ); ?>" />
  255. </p>
  256. <p>
  257. <label for="<?php echo $this->get_field_id( 'pad_counts' ); ?>">
  258. <input class="checkbox" type="checkbox" <?php checked( $instance['pad_counts'], true ); ?> id="<?php echo $this->get_field_id( 'pad_counts' ); ?>" name="<?php echo $this->get_field_name( 'pad_counts' ); ?>" /> <?php _e( 'Pad counts?', 'hybrid-core' ); ?> <code>pad_counts</code></label>
  259. </p>
  260. <p>
  261. <label for="<?php echo $this->get_field_id( 'hide_empty' ); ?>">
  262. <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>
  263. </p>
  264. </div>
  265. <div style="clear:both;">&nbsp;</div>
  266. <?php
  267. }
  268. }
  269. ?>