PageRenderTime 59ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/default-widgets.php

https://github.com/shaiquddin/WordPress
PHP | 1197 lines | 871 code | 196 blank | 130 comment | 125 complexity | 681c58611dc7c42690bb83e751109823 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * Default Widgets
  4. *
  5. * @package WordPress
  6. * @subpackage Widgets
  7. */
  8. /**
  9. * Pages widget class
  10. *
  11. * @since 2.8.0
  12. */
  13. class WP_Widget_Pages extends WP_Widget {
  14. function __construct() {
  15. $widget_ops = array('classname' => 'widget_pages', 'description' => __( 'Your site&#8217;s WordPress Pages') );
  16. parent::__construct('pages', __('Pages'), $widget_ops);
  17. }
  18. function widget( $args, $instance ) {
  19. extract( $args );
  20. $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title'], $instance, $this->id_base);
  21. $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
  22. $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
  23. if ( $sortby == 'menu_order' )
  24. $sortby = 'menu_order, post_title';
  25. $out = wp_list_pages( apply_filters('widget_pages_args', array('title_li' => '', 'echo' => 0, 'sort_column' => $sortby, 'exclude' => $exclude) ) );
  26. if ( !empty( $out ) ) {
  27. echo $before_widget;
  28. if ( $title)
  29. echo $before_title . $title . $after_title;
  30. ?>
  31. <ul>
  32. <?php echo $out; ?>
  33. </ul>
  34. <?php
  35. echo $after_widget;
  36. }
  37. }
  38. function update( $new_instance, $old_instance ) {
  39. $instance = $old_instance;
  40. $instance['title'] = strip_tags($new_instance['title']);
  41. if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
  42. $instance['sortby'] = $new_instance['sortby'];
  43. } else {
  44. $instance['sortby'] = 'menu_order';
  45. }
  46. $instance['exclude'] = strip_tags( $new_instance['exclude'] );
  47. return $instance;
  48. }
  49. function form( $instance ) {
  50. //Defaults
  51. $instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
  52. $title = esc_attr( $instance['title'] );
  53. $exclude = esc_attr( $instance['exclude'] );
  54. ?>
  55. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  56. <p>
  57. <label for="<?php echo $this->get_field_id('sortby'); ?>"><?php _e( 'Sort by:' ); ?></label>
  58. <select name="<?php echo $this->get_field_name('sortby'); ?>" id="<?php echo $this->get_field_id('sortby'); ?>" class="widefat">
  59. <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
  60. <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
  61. <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
  62. </select>
  63. </p>
  64. <p>
  65. <label for="<?php echo $this->get_field_id('exclude'); ?>"><?php _e( 'Exclude:' ); ?></label> <input type="text" value="<?php echo $exclude; ?>" name="<?php echo $this->get_field_name('exclude'); ?>" id="<?php echo $this->get_field_id('exclude'); ?>" class="widefat" />
  66. <br />
  67. <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
  68. </p>
  69. <?php
  70. }
  71. }
  72. /**
  73. * Links widget class
  74. *
  75. * @since 2.8.0
  76. */
  77. class WP_Widget_Links extends WP_Widget {
  78. function __construct() {
  79. $widget_ops = array('description' => __( "Your blogroll" ) );
  80. parent::__construct('links', __('Links'), $widget_ops);
  81. }
  82. function widget( $args, $instance ) {
  83. extract($args, EXTR_SKIP);
  84. $show_description = isset($instance['description']) ? $instance['description'] : false;
  85. $show_name = isset($instance['name']) ? $instance['name'] : false;
  86. $show_rating = isset($instance['rating']) ? $instance['rating'] : false;
  87. $show_images = isset($instance['images']) ? $instance['images'] : true;
  88. $category = isset($instance['category']) ? $instance['category'] : false;
  89. $orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'name';
  90. $order = $orderby == 'rating' ? 'DESC' : 'ASC';
  91. $limit = isset( $instance['limit'] ) ? $instance['limit'] : -1;
  92. $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
  93. wp_list_bookmarks(apply_filters('widget_links_args', array(
  94. 'title_before' => $before_title, 'title_after' => $after_title,
  95. 'category_before' => $before_widget, 'category_after' => $after_widget,
  96. 'show_images' => $show_images, 'show_description' => $show_description,
  97. 'show_name' => $show_name, 'show_rating' => $show_rating,
  98. 'category' => $category, 'class' => 'linkcat widget',
  99. 'orderby' => $orderby, 'order' => $order,
  100. 'limit' => $limit,
  101. )));
  102. }
  103. function update( $new_instance, $old_instance ) {
  104. $new_instance = (array) $new_instance;
  105. $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0 );
  106. foreach ( $instance as $field => $val ) {
  107. if ( isset($new_instance[$field]) )
  108. $instance[$field] = 1;
  109. }
  110. $instance['orderby'] = 'name';
  111. if ( in_array( $new_instance['orderby'], array( 'name', 'rating', 'id', 'rand' ) ) )
  112. $instance['orderby'] = $new_instance['orderby'];
  113. $instance['category'] = intval( $new_instance['category'] );
  114. $instance['limit'] = ! empty( $new_instance['limit'] ) ? intval( $new_instance['limit'] ) : -1;
  115. return $instance;
  116. }
  117. function form( $instance ) {
  118. //Defaults
  119. $instance = wp_parse_args( (array) $instance, array( 'images' => true, 'name' => true, 'description' => false, 'rating' => false, 'category' => false, 'orderby' => 'name', 'limit' => -1 ) );
  120. $link_cats = get_terms( 'link_category' );
  121. if ( ! $limit = intval( $instance['limit'] ) )
  122. $limit = -1;
  123. ?>
  124. <p>
  125. <label for="<?php echo $this->get_field_id('category'); ?>"><?php _e( 'Select Link Category:' ); ?></label>
  126. <select class="widefat" id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>">
  127. <option value=""><?php _ex('All Links', 'links widget'); ?></option>
  128. <?php
  129. foreach ( $link_cats as $link_cat ) {
  130. echo '<option value="' . intval( $link_cat->term_id ) . '"'
  131. . selected( $instance['category'], $link_cat->term_id, false )
  132. . '>' . $link_cat->name . "</option>\n";
  133. }
  134. ?>
  135. </select>
  136. <label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e( 'Sort by:' ); ?></label>
  137. <select name="<?php echo $this->get_field_name('orderby'); ?>" id="<?php echo $this->get_field_id('orderby'); ?>" class="widefat">
  138. <option value="name"<?php selected( $instance['orderby'], 'name' ); ?>><?php _e( 'Link title' ); ?></option>
  139. <option value="rating"<?php selected( $instance['orderby'], 'rating' ); ?>><?php _e( 'Link rating' ); ?></option>
  140. <option value="id"<?php selected( $instance['orderby'], 'id' ); ?>><?php _e( 'Link ID' ); ?></option>
  141. <option value="rand"<?php selected( $instance['orderby'], 'rand' ); ?>><?php _e( 'Random' ); ?></option>
  142. </select>
  143. </p>
  144. <p>
  145. <input class="checkbox" type="checkbox" <?php checked($instance['images'], true) ?> id="<?php echo $this->get_field_id('images'); ?>" name="<?php echo $this->get_field_name('images'); ?>" />
  146. <label for="<?php echo $this->get_field_id('images'); ?>"><?php _e('Show Link Image'); ?></label><br />
  147. <input class="checkbox" type="checkbox" <?php checked($instance['name'], true) ?> id="<?php echo $this->get_field_id('name'); ?>" name="<?php echo $this->get_field_name('name'); ?>" />
  148. <label for="<?php echo $this->get_field_id('name'); ?>"><?php _e('Show Link Name'); ?></label><br />
  149. <input class="checkbox" type="checkbox" <?php checked($instance['description'], true) ?> id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" />
  150. <label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Show Link Description'); ?></label><br />
  151. <input class="checkbox" type="checkbox" <?php checked($instance['rating'], true) ?> id="<?php echo $this->get_field_id('rating'); ?>" name="<?php echo $this->get_field_name('rating'); ?>" />
  152. <label for="<?php echo $this->get_field_id('rating'); ?>"><?php _e('Show Link Rating'); ?></label>
  153. </p>
  154. <p>
  155. <label for="<?php echo $this->get_field_id('limit'); ?>"><?php _e( 'Number of links to show:' ); ?></label>
  156. <input id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" type="text" value="<?php echo $limit == -1 ? '' : intval( $limit ); ?>" size="3" />
  157. </p>
  158. <?php
  159. }
  160. }
  161. /**
  162. * Search widget class
  163. *
  164. * @since 2.8.0
  165. */
  166. class WP_Widget_Search extends WP_Widget {
  167. function __construct() {
  168. $widget_ops = array('classname' => 'widget_search', 'description' => __( "A search form for your site") );
  169. parent::__construct('search', __('Search'), $widget_ops);
  170. }
  171. function widget( $args, $instance ) {
  172. extract($args);
  173. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
  174. echo $before_widget;
  175. if ( $title )
  176. echo $before_title . $title . $after_title;
  177. // Use current theme search form if it exists
  178. get_search_form();
  179. echo $after_widget;
  180. }
  181. function form( $instance ) {
  182. $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
  183. $title = $instance['title'];
  184. ?>
  185. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></label></p>
  186. <?php
  187. }
  188. function update( $new_instance, $old_instance ) {
  189. $instance = $old_instance;
  190. $new_instance = wp_parse_args((array) $new_instance, array( 'title' => ''));
  191. $instance['title'] = strip_tags($new_instance['title']);
  192. return $instance;
  193. }
  194. }
  195. /**
  196. * Archives widget class
  197. *
  198. * @since 2.8.0
  199. */
  200. class WP_Widget_Archives extends WP_Widget {
  201. function __construct() {
  202. $widget_ops = array('classname' => 'widget_archive', 'description' => __( 'A monthly archive of your site&#8217;s posts') );
  203. parent::__construct('archives', __('Archives'), $widget_ops);
  204. }
  205. function widget( $args, $instance ) {
  206. extract($args);
  207. $c = ! empty( $instance['count'] ) ? '1' : '0';
  208. $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
  209. $title = apply_filters('widget_title', empty($instance['title']) ? __('Archives') : $instance['title'], $instance, $this->id_base);
  210. echo $before_widget;
  211. if ( $title )
  212. echo $before_title . $title . $after_title;
  213. if ( $d ) {
  214. ?>
  215. <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo esc_attr(__('Select Month')); ?></option> <?php wp_get_archives(apply_filters('widget_archives_dropdown_args', array('type' => 'monthly', 'format' => 'option', 'show_post_count' => $c))); ?> </select>
  216. <?php
  217. } else {
  218. ?>
  219. <ul>
  220. <?php wp_get_archives(apply_filters('widget_archives_args', array('type' => 'monthly', 'show_post_count' => $c))); ?>
  221. </ul>
  222. <?php
  223. }
  224. echo $after_widget;
  225. }
  226. function update( $new_instance, $old_instance ) {
  227. $instance = $old_instance;
  228. $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
  229. $instance['title'] = strip_tags($new_instance['title']);
  230. $instance['count'] = $new_instance['count'] ? 1 : 0;
  231. $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
  232. return $instance;
  233. }
  234. function form( $instance ) {
  235. $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
  236. $title = strip_tags($instance['title']);
  237. $count = $instance['count'] ? 'checked="checked"' : '';
  238. $dropdown = $instance['dropdown'] ? 'checked="checked"' : '';
  239. ?>
  240. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
  241. <p>
  242. <input class="checkbox" type="checkbox" <?php echo $dropdown; ?> id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>" /> <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e('Display as dropdown'); ?></label>
  243. <br/>
  244. <input class="checkbox" type="checkbox" <?php echo $count; ?> id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>" /> <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e('Show post counts'); ?></label>
  245. </p>
  246. <?php
  247. }
  248. }
  249. /**
  250. * Meta widget class
  251. *
  252. * Displays log in/out, RSS feed links, etc.
  253. *
  254. * @since 2.8.0
  255. */
  256. class WP_Widget_Meta extends WP_Widget {
  257. function __construct() {
  258. $widget_ops = array('classname' => 'widget_meta', 'description' => __( "Log in/out, admin, feed and WordPress links") );
  259. parent::__construct('meta', __('Meta'), $widget_ops);
  260. }
  261. function widget( $args, $instance ) {
  262. extract($args);
  263. $title = apply_filters('widget_title', empty($instance['title']) ? __('Meta') : $instance['title'], $instance, $this->id_base);
  264. echo $before_widget;
  265. if ( $title )
  266. echo $before_title . $title . $after_title;
  267. ?>
  268. <ul>
  269. <?php wp_register(); ?>
  270. <li><?php wp_loginout(); ?></li>
  271. <li><a href="<?php bloginfo('rss2_url'); ?>" title="<?php echo esc_attr(__('Syndicate this site using RSS 2.0')); ?>"><?php _e('Entries <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
  272. <li><a href="<?php bloginfo('comments_rss2_url'); ?>" title="<?php echo esc_attr(__('The latest comments to all posts in RSS')); ?>"><?php _e('Comments <abbr title="Really Simple Syndication">RSS</abbr>'); ?></a></li>
  273. <li><a href="<?php esc_attr_e( 'http://wordpress.org/' ); ?>" title="<?php echo esc_attr(__('Powered by WordPress, state-of-the-art semantic personal publishing platform.')); ?>"><?php
  274. /* translators: meta widget link text */
  275. _e( 'WordPress.org' );
  276. ?></a></li>
  277. <?php wp_meta(); ?>
  278. </ul>
  279. <?php
  280. echo $after_widget;
  281. }
  282. function update( $new_instance, $old_instance ) {
  283. $instance = $old_instance;
  284. $instance['title'] = strip_tags($new_instance['title']);
  285. return $instance;
  286. }
  287. function form( $instance ) {
  288. $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
  289. $title = strip_tags($instance['title']);
  290. ?>
  291. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
  292. <?php
  293. }
  294. }
  295. /**
  296. * Calendar widget class
  297. *
  298. * @since 2.8.0
  299. */
  300. class WP_Widget_Calendar extends WP_Widget {
  301. function __construct() {
  302. $widget_ops = array('classname' => 'widget_calendar', 'description' => __( 'A calendar of your site&#8217;s posts') );
  303. parent::__construct('calendar', __('Calendar'), $widget_ops);
  304. }
  305. function widget( $args, $instance ) {
  306. extract($args);
  307. $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base);
  308. echo $before_widget;
  309. if ( $title )
  310. echo $before_title . $title . $after_title;
  311. echo '<div id="calendar_wrap">';
  312. get_calendar();
  313. echo '</div>';
  314. echo $after_widget;
  315. }
  316. function update( $new_instance, $old_instance ) {
  317. $instance = $old_instance;
  318. $instance['title'] = strip_tags($new_instance['title']);
  319. return $instance;
  320. }
  321. function form( $instance ) {
  322. $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
  323. $title = strip_tags($instance['title']);
  324. ?>
  325. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  326. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
  327. <?php
  328. }
  329. }
  330. /**
  331. * Text widget class
  332. *
  333. * @since 2.8.0
  334. */
  335. class WP_Widget_Text extends WP_Widget {
  336. function __construct() {
  337. $widget_ops = array('classname' => 'widget_text', 'description' => __('Arbitrary text or HTML'));
  338. $control_ops = array('width' => 400, 'height' => 350);
  339. parent::__construct('text', __('Text'), $widget_ops, $control_ops);
  340. }
  341. function widget( $args, $instance ) {
  342. extract($args);
  343. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
  344. $text = apply_filters( 'widget_text', empty( $instance['text'] ) ? '' : $instance['text'], $instance );
  345. echo $before_widget;
  346. if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?>
  347. <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div>
  348. <?php
  349. echo $after_widget;
  350. }
  351. function update( $new_instance, $old_instance ) {
  352. $instance = $old_instance;
  353. $instance['title'] = strip_tags($new_instance['title']);
  354. if ( current_user_can('unfiltered_html') )
  355. $instance['text'] = $new_instance['text'];
  356. else
  357. $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed
  358. $instance['filter'] = isset($new_instance['filter']);
  359. return $instance;
  360. }
  361. function form( $instance ) {
  362. $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
  363. $title = strip_tags($instance['title']);
  364. $text = esc_textarea($instance['text']);
  365. ?>
  366. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  367. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
  368. <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea>
  369. <p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox" <?php checked(isset($instance['filter']) ? $instance['filter'] : 0); ?> />&nbsp;<label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
  370. <?php
  371. }
  372. }
  373. /**
  374. * Categories widget class
  375. *
  376. * @since 2.8.0
  377. */
  378. class WP_Widget_Categories extends WP_Widget {
  379. function __construct() {
  380. $widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories" ) );
  381. parent::__construct('categories', __('Categories'), $widget_ops);
  382. }
  383. function widget( $args, $instance ) {
  384. extract( $args );
  385. $title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Categories' ) : $instance['title'], $instance, $this->id_base);
  386. $c = ! empty( $instance['count'] ) ? '1' : '0';
  387. $h = ! empty( $instance['hierarchical'] ) ? '1' : '0';
  388. $d = ! empty( $instance['dropdown'] ) ? '1' : '0';
  389. echo $before_widget;
  390. if ( $title )
  391. echo $before_title . $title . $after_title;
  392. $cat_args = array('orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);
  393. if ( $d ) {
  394. $cat_args['show_option_none'] = __('Select Category');
  395. wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));
  396. ?>
  397. <script type='text/javascript'>
  398. /* <![CDATA[ */
  399. var dropdown = document.getElementById("cat");
  400. function onCatChange() {
  401. if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
  402. location.href = "<?php echo home_url(); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
  403. }
  404. }
  405. dropdown.onchange = onCatChange;
  406. /* ]]> */
  407. </script>
  408. <?php
  409. } else {
  410. ?>
  411. <ul>
  412. <?php
  413. $cat_args['title_li'] = '';
  414. wp_list_categories(apply_filters('widget_categories_args', $cat_args));
  415. ?>
  416. </ul>
  417. <?php
  418. }
  419. echo $after_widget;
  420. }
  421. function update( $new_instance, $old_instance ) {
  422. $instance = $old_instance;
  423. $instance['title'] = strip_tags($new_instance['title']);
  424. $instance['count'] = !empty($new_instance['count']) ? 1 : 0;
  425. $instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
  426. $instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
  427. return $instance;
  428. }
  429. function form( $instance ) {
  430. //Defaults
  431. $instance = wp_parse_args( (array) $instance, array( 'title' => '') );
  432. $title = esc_attr( $instance['title'] );
  433. $count = isset($instance['count']) ? (bool) $instance['count'] :false;
  434. $hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
  435. $dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
  436. ?>
  437. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></label>
  438. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  439. <p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
  440. <label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />
  441. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
  442. <label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />
  443. <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
  444. <label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>
  445. <?php
  446. }
  447. }
  448. /**
  449. * Recent_Posts widget class
  450. *
  451. * @since 2.8.0
  452. */
  453. class WP_Widget_Recent_Posts extends WP_Widget {
  454. function __construct() {
  455. $widget_ops = array('classname' => 'widget_recent_entries', 'description' => __( "The most recent posts on your site") );
  456. parent::__construct('recent-posts', __('Recent Posts'), $widget_ops);
  457. $this->alt_option_name = 'widget_recent_entries';
  458. add_action( 'save_post', array(&$this, 'flush_widget_cache') );
  459. add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
  460. add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
  461. }
  462. function widget($args, $instance) {
  463. $cache = wp_cache_get('widget_recent_posts', 'widget');
  464. if ( !is_array($cache) )
  465. $cache = array();
  466. if ( ! isset( $args['widget_id'] ) )
  467. $args['widget_id'] = $this->id;
  468. if ( isset( $cache[ $args['widget_id'] ] ) ) {
  469. echo $cache[ $args['widget_id'] ];
  470. return;
  471. }
  472. ob_start();
  473. extract($args);
  474. $title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Posts') : $instance['title'], $instance, $this->id_base);
  475. if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
  476. $number = 10;
  477. $show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
  478. $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true ) ) );
  479. if ($r->have_posts()) :
  480. ?>
  481. <?php echo $before_widget; ?>
  482. <?php if ( $title ) echo $before_title . $title . $after_title; ?>
  483. <ul>
  484. <?php while ( $r->have_posts() ) : $r->the_post(); ?>
  485. <li>
  486. <a href="<?php the_permalink() ?>" title="<?php echo esc_attr( get_the_title() ? get_the_title() : get_the_ID() ); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a>
  487. <?php if ( $show_date ) : ?>
  488. <span class="post-date"><?php echo get_the_date(); ?></span>
  489. <?php endif; ?>
  490. </li>
  491. <?php endwhile; ?>
  492. </ul>
  493. <?php echo $after_widget; ?>
  494. <?php
  495. // Reset the global $the_post as this query will have stomped on it
  496. wp_reset_postdata();
  497. endif;
  498. $cache[$args['widget_id']] = ob_get_flush();
  499. wp_cache_set('widget_recent_posts', $cache, 'widget');
  500. }
  501. function update( $new_instance, $old_instance ) {
  502. $instance = $old_instance;
  503. $instance['title'] = strip_tags($new_instance['title']);
  504. $instance['number'] = (int) $new_instance['number'];
  505. $instance['show_date'] = (bool) $new_instance['show_date'];
  506. $this->flush_widget_cache();
  507. $alloptions = wp_cache_get( 'alloptions', 'options' );
  508. if ( isset($alloptions['widget_recent_entries']) )
  509. delete_option('widget_recent_entries');
  510. return $instance;
  511. }
  512. function flush_widget_cache() {
  513. wp_cache_delete('widget_recent_posts', 'widget');
  514. }
  515. function form( $instance ) {
  516. $title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
  517. $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 5;
  518. $show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
  519. ?>
  520. <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
  521. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>" /></p>
  522. <p><label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number of posts to show:' ); ?></label>
  523. <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
  524. <p><input class="checkbox" type="checkbox" <?php checked( $show_date ); ?> id="<?php echo $this->get_field_id( 'show_date' ); ?>" name="<?php echo $this->get_field_name( 'show_date' ); ?>" />
  525. <label for="<?php echo $this->get_field_id( 'show_date' ); ?>"><?php _e( 'Display post date?' ); ?></label></p>
  526. <?php
  527. }
  528. }
  529. /**
  530. * Recent_Comments widget class
  531. *
  532. * @since 2.8.0
  533. */
  534. class WP_Widget_Recent_Comments extends WP_Widget {
  535. function __construct() {
  536. $widget_ops = array('classname' => 'widget_recent_comments', 'description' => __( 'The most recent comments' ) );
  537. parent::__construct('recent-comments', __('Recent Comments'), $widget_ops);
  538. $this->alt_option_name = 'widget_recent_comments';
  539. if ( is_active_widget(false, false, $this->id_base) )
  540. add_action( 'wp_head', array(&$this, 'recent_comments_style') );
  541. add_action( 'comment_post', array(&$this, 'flush_widget_cache') );
  542. add_action( 'transition_comment_status', array(&$this, 'flush_widget_cache') );
  543. }
  544. function recent_comments_style() {
  545. if ( ! current_theme_supports( 'widgets' ) // Temp hack #14876
  546. || ! apply_filters( 'show_recent_comments_widget_style', true, $this->id_base ) )
  547. return;
  548. ?>
  549. <style type="text/css">.recentcomments a{display:inline !important;padding:0 !important;margin:0 !important;}</style>
  550. <?php
  551. }
  552. function flush_widget_cache() {
  553. wp_cache_delete('widget_recent_comments', 'widget');
  554. }
  555. function widget( $args, $instance ) {
  556. global $comments, $comment;
  557. $cache = wp_cache_get('widget_recent_comments', 'widget');
  558. if ( ! is_array( $cache ) )
  559. $cache = array();
  560. if ( ! isset( $args['widget_id'] ) )
  561. $args['widget_id'] = $this->id;
  562. if ( isset( $cache[ $args['widget_id'] ] ) ) {
  563. echo $cache[ $args['widget_id'] ];
  564. return;
  565. }
  566. extract($args, EXTR_SKIP);
  567. $output = '';
  568. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Recent Comments' ) : $instance['title'], $instance, $this->id_base );
  569. if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
  570. $number = 5;
  571. $comments = get_comments( apply_filters( 'widget_comments_args', array( 'number' => $number, 'status' => 'approve', 'post_status' => 'publish' ) ) );
  572. $output .= $before_widget;
  573. if ( $title )
  574. $output .= $before_title . $title . $after_title;
  575. $output .= '<ul id="recentcomments">';
  576. if ( $comments ) {
  577. foreach ( (array) $comments as $comment) {
  578. $output .= '<li class="recentcomments">' . /* translators: comments widget: 1: comment author, 2: post link */ sprintf(_x('%1$s on %2$s', 'widgets'), get_comment_author_link(), '<a href="' . esc_url( get_comment_link($comment->comment_ID) ) . '">' . get_the_title($comment->comment_post_ID) . '</a>') . '</li>';
  579. }
  580. }
  581. $output .= '</ul>';
  582. $output .= $after_widget;
  583. echo $output;
  584. $cache[$args['widget_id']] = $output;
  585. wp_cache_set('widget_recent_comments', $cache, 'widget');
  586. }
  587. function update( $new_instance, $old_instance ) {
  588. $instance = $old_instance;
  589. $instance['title'] = strip_tags($new_instance['title']);
  590. $instance['number'] = absint( $new_instance['number'] );
  591. $this->flush_widget_cache();
  592. $alloptions = wp_cache_get( 'alloptions', 'options' );
  593. if ( isset($alloptions['widget_recent_comments']) )
  594. delete_option('widget_recent_comments');
  595. return $instance;
  596. }
  597. function form( $instance ) {
  598. $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  599. $number = isset($instance['number']) ? absint($instance['number']) : 5;
  600. ?>
  601. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  602. <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p>
  603. <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
  604. <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p>
  605. <?php
  606. }
  607. }
  608. /**
  609. * RSS widget class
  610. *
  611. * @since 2.8.0
  612. */
  613. class WP_Widget_RSS extends WP_Widget {
  614. function __construct() {
  615. $widget_ops = array( 'description' => __('Entries from any RSS or Atom feed') );
  616. $control_ops = array( 'width' => 400, 'height' => 200 );
  617. parent::__construct( 'rss', __('RSS'), $widget_ops, $control_ops );
  618. }
  619. function widget($args, $instance) {
  620. if ( isset($instance['error']) && $instance['error'] )
  621. return;
  622. extract($args, EXTR_SKIP);
  623. $url = ! empty( $instance['url'] ) ? $instance['url'] : '';
  624. while ( stristr($url, 'http') != $url )
  625. $url = substr($url, 1);
  626. if ( empty($url) )
  627. return;
  628. // self-url destruction sequence
  629. if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ) ) )
  630. return;
  631. $rss = fetch_feed($url);
  632. $title = $instance['title'];
  633. $desc = '';
  634. $link = '';
  635. if ( ! is_wp_error($rss) ) {
  636. $desc = esc_attr(strip_tags(@html_entity_decode($rss->get_description(), ENT_QUOTES, get_option('blog_charset'))));
  637. if ( empty($title) )
  638. $title = esc_html(strip_tags($rss->get_title()));
  639. $link = esc_url(strip_tags($rss->get_permalink()));
  640. while ( stristr($link, 'http') != $link )
  641. $link = substr($link, 1);
  642. }
  643. if ( empty($title) )
  644. $title = empty($desc) ? __('Unknown Feed') : $desc;
  645. $title = apply_filters('widget_title', $title, $instance, $this->id_base);
  646. $url = esc_url(strip_tags($url));
  647. $icon = includes_url('images/rss.png');
  648. if ( $title )
  649. $title = "<a class='rsswidget' href='$url' title='" . esc_attr__( 'Syndicate this content' ) ."'><img style='border:0' width='14' height='14' src='$icon' alt='RSS' /></a> <a class='rsswidget' href='$link' title='$desc'>$title</a>";
  650. echo $before_widget;
  651. if ( $title )
  652. echo $before_title . $title . $after_title;
  653. wp_widget_rss_output( $rss, $instance );
  654. echo $after_widget;
  655. if ( ! is_wp_error($rss) )
  656. $rss->__destruct();
  657. unset($rss);
  658. }
  659. function update($new_instance, $old_instance) {
  660. $testurl = ( isset( $new_instance['url'] ) && ( !isset( $old_instance['url'] ) || ( $new_instance['url'] != $old_instance['url'] ) ) );
  661. return wp_widget_rss_process( $new_instance, $testurl );
  662. }
  663. function form($instance) {
  664. if ( empty($instance) )
  665. $instance = array( 'title' => '', 'url' => '', 'items' => 10, 'error' => false, 'show_summary' => 0, 'show_author' => 0, 'show_date' => 0 );
  666. $instance['number'] = $this->number;
  667. wp_widget_rss_form( $instance );
  668. }
  669. }
  670. /**
  671. * Display the RSS entries in a list.
  672. *
  673. * @since 2.5.0
  674. *
  675. * @param string|array|object $rss RSS url.
  676. * @param array $args Widget arguments.
  677. */
  678. function wp_widget_rss_output( $rss, $args = array() ) {
  679. if ( is_string( $rss ) ) {
  680. $rss = fetch_feed($rss);
  681. } elseif ( is_array($rss) && isset($rss['url']) ) {
  682. $args = $rss;
  683. $rss = fetch_feed($rss['url']);
  684. } elseif ( !is_object($rss) ) {
  685. return;
  686. }
  687. if ( is_wp_error($rss) ) {
  688. if ( is_admin() || current_user_can('manage_options') )
  689. echo '<p>' . sprintf( __('<strong>RSS Error</strong>: %s'), $rss->get_error_message() ) . '</p>';
  690. return;
  691. }
  692. $default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0 );
  693. $args = wp_parse_args( $args, $default_args );
  694. extract( $args, EXTR_SKIP );
  695. $items = (int) $items;
  696. if ( $items < 1 || 20 < $items )
  697. $items = 10;
  698. $show_summary = (int) $show_summary;
  699. $show_author = (int) $show_author;
  700. $show_date = (int) $show_date;
  701. if ( !$rss->get_item_quantity() ) {
  702. echo '<ul><li>' . __( 'An error has occurred; the feed is probably down. Try again later.' ) . '</li></ul>';
  703. $rss->__destruct();
  704. unset($rss);
  705. return;
  706. }
  707. echo '<ul>';
  708. foreach ( $rss->get_items(0, $items) as $item ) {
  709. $link = $item->get_link();
  710. while ( stristr($link, 'http') != $link )
  711. $link = substr($link, 1);
  712. $link = esc_url(strip_tags($link));
  713. $title = esc_attr(strip_tags($item->get_title()));
  714. if ( empty($title) )
  715. $title = __('Untitled');
  716. $desc = str_replace( array("\n", "\r"), ' ', esc_attr( strip_tags( @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option('blog_charset') ) ) ) );
  717. $desc = wp_html_excerpt( $desc, 360 );
  718. // Append ellipsis. Change existing [...] to [&hellip;].
  719. if ( '[...]' == substr( $desc, -5 ) )
  720. $desc = substr( $desc, 0, -5 ) . '[&hellip;]';
  721. elseif ( '[&hellip;]' != substr( $desc, -10 ) )
  722. $desc .= ' [&hellip;]';
  723. $desc = esc_html( $desc );
  724. if ( $show_summary ) {
  725. $summary = "<div class='rssSummary'>$desc</div>";
  726. } else {
  727. $summary = '';
  728. }
  729. $date = '';
  730. if ( $show_date ) {
  731. $date = $item->get_date( 'U' );
  732. if ( $date ) {
  733. $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
  734. }
  735. }
  736. $author = '';
  737. if ( $show_author ) {
  738. $author = $item->get_author();
  739. if ( is_object($author) ) {
  740. $author = $author->get_name();
  741. $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
  742. }
  743. }
  744. if ( $link == '' ) {
  745. echo "<li>$title{$date}{$summary}{$author}</li>";
  746. } else {
  747. echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
  748. }
  749. }
  750. echo '</ul>';
  751. $rss->__destruct();
  752. unset($rss);
  753. }
  754. /**
  755. * Display RSS widget options form.
  756. *
  757. * The options for what fields are displayed for the RSS form are all booleans
  758. * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
  759. * 'show_date'.
  760. *
  761. * @since 2.5.0
  762. *
  763. * @param array|string $args Values for input fields.
  764. * @param array $inputs Override default display options.
  765. */
  766. function wp_widget_rss_form( $args, $inputs = null ) {
  767. $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
  768. $inputs = wp_parse_args( $inputs, $default_inputs );
  769. extract( $args );
  770. extract( $inputs, EXTR_SKIP);
  771. $number = esc_attr( $number );
  772. $title = esc_attr( $title );
  773. $url = esc_url( $url );
  774. $items = (int) $items;
  775. if ( $items < 1 || 20 < $items )
  776. $items = 10;
  777. $show_summary = (int) $show_summary;
  778. $show_author = (int) $show_author;
  779. $show_date = (int) $show_date;
  780. if ( !empty($error) )
  781. echo '<p class="widget-error"><strong>' . sprintf( __('RSS Error: %s'), $error) . '</strong></p>';
  782. if ( $inputs['url'] ) :
  783. ?>
  784. <p><label for="rss-url-<?php echo $number; ?>"><?php _e('Enter the RSS feed URL here:'); ?></label>
  785. <input class="widefat" id="rss-url-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][url]" type="text" value="<?php echo $url; ?>" /></p>
  786. <?php endif; if ( $inputs['title'] ) : ?>
  787. <p><label for="rss-title-<?php echo $number; ?>"><?php _e('Give the feed a title (optional):'); ?></label>
  788. <input class="widefat" id="rss-title-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" /></p>
  789. <?php endif; if ( $inputs['items'] ) : ?>
  790. <p><label for="rss-items-<?php echo $number; ?>"><?php _e('How many items would you like to display?'); ?></label>
  791. <select id="rss-items-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][items]">
  792. <?php
  793. for ( $i = 1; $i <= 20; ++$i )
  794. echo "<option value='$i' " . selected( $items, $i, false ) . ">$i</option>";
  795. ?>
  796. </select></p>
  797. <?php endif; if ( $inputs['show_summary'] ) : ?>
  798. <p><input id="rss-show-summary-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_summary]" type="checkbox" value="1" <?php if ( $show_summary ) echo 'checked="checked"'; ?>/>
  799. <label for="rss-show-summary-<?php echo $number; ?>"><?php _e('Display item content?'); ?></label></p>
  800. <?php endif; if ( $inputs['show_author'] ) : ?>
  801. <p><input id="rss-show-author-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_author]" type="checkbox" value="1" <?php if ( $show_author ) echo 'checked="checked"'; ?>/>
  802. <label for="rss-show-author-<?php echo $number; ?>"><?php _e('Display item author if available?'); ?></label></p>
  803. <?php endif; if ( $inputs['show_date'] ) : ?>
  804. <p><input id="rss-show-date-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][show_date]" type="checkbox" value="1" <?php if ( $show_date ) echo 'checked="checked"'; ?>/>
  805. <label for="rss-show-date-<?php echo $number; ?>"><?php _e('Display item date?'); ?></label></p>
  806. <?php
  807. endif;
  808. foreach ( array_keys($default_inputs) as $input ) :
  809. if ( 'hidden' === $inputs[$input] ) :
  810. $id = str_replace( '_', '-', $input );
  811. ?>
  812. <input type="hidden" id="rss-<?php echo $id; ?>-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][<?php echo $input; ?>]" value="<?php echo $$input; ?>" />
  813. <?php
  814. endif;
  815. endforeach;
  816. }
  817. /**
  818. * Process RSS feed widget data and optionally retrieve feed items.
  819. *
  820. * The feed widget can not have more than 20 items or it will reset back to the
  821. * default, which is 10.
  822. *
  823. * The resulting array has the feed title, feed url, feed link (from channel),
  824. * feed items, error (if any), and whether to show summary, author, and date.
  825. * All respectively in the order of the array elements.
  826. *
  827. * @since 2.5.0
  828. *
  829. * @param array $widget_rss RSS widget feed data. Expects unescaped data.
  830. * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
  831. * @return array
  832. */
  833. function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
  834. $items = (int) $widget_rss['items'];
  835. if ( $items < 1 || 20 < $items )
  836. $items = 10;
  837. $url = esc_url_raw(strip_tags( $widget_rss['url'] ));
  838. $title = trim(strip_tags( $widget_rss['title'] ));
  839. $show_summary = isset($widget_rss['show_summary']) ? (int) $widget_rss['show_summary'] : 0;
  840. $show_author = isset($widget_rss['show_author']) ? (int) $widget_rss['show_author'] :0;
  841. $show_date = isset($widget_rss['show_date']) ? (int) $widget_rss['show_date'] : 0;
  842. if ( $check_feed ) {
  843. $rss = fetch_feed($url);
  844. $error = false;
  845. $link = '';
  846. if ( is_wp_error($rss) ) {
  847. $error = $rss->get_error_message();
  848. } else {
  849. $link = esc_url(strip_tags($rss->get_permalink()));
  850. while ( stristr($link, 'http') != $link )
  851. $link = substr($link, 1);
  852. $rss->__destruct();
  853. unset($rss);
  854. }
  855. }
  856. return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
  857. }
  858. /**
  859. * Tag cloud widget class
  860. *
  861. * @since 2.8.0
  862. */
  863. class WP_Widget_Tag_Cloud extends WP_Widget {
  864. function __construct() {
  865. $widget_ops = array( 'description' => __( "Your most used tags in cloud format") );
  866. parent::__construct('tag_cloud', __('Tag Cloud'), $widget_ops);
  867. }
  868. function widget( $args, $instance ) {
  869. extract($args);
  870. $current_taxonomy = $this->_get_current_taxonomy($instance);
  871. if ( !empty($instance['title']) ) {
  872. $title = $instance['title'];
  873. } else {
  874. if ( 'post_tag' == $current_taxonomy ) {
  875. $title = __('Tags');
  876. } else {
  877. $tax = get_taxonomy($current_taxonomy);
  878. $title = $tax->labels->name;
  879. }
  880. }
  881. $title = apply_filters('widget_title', $title, $instance, $this->id_base);
  882. echo $before_widget;
  883. if ( $title )
  884. echo $before_title . $title . $after_title;
  885. echo '<div class="tagcloud">';
  886. wp_tag_cloud( apply_filters('widget_tag_cloud_args', array('taxonomy' => $current_taxonomy) ) );
  887. echo "</div>\n";
  888. echo $after_widget;
  889. }
  890. function update( $new_instance, $old_instance ) {
  891. $instance['title'] = strip_tags(stripslashes($new_instance['title']));
  892. $instance['taxonomy'] = stripslashes($new_instance['taxonomy']);
  893. return $instance;
  894. }
  895. function form( $instance ) {
  896. $current_taxonomy = $this->_get_current_taxonomy($instance);
  897. ?>
  898. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
  899. <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php if (isset ( $instance['title'])) {echo esc_attr( $instance['title'] );} ?>" /></p>
  900. <p><label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Taxonomy:') ?></label>
  901. <select class="widefat" id="<?php echo $this->get_field_id('taxonomy'); ?>" name="<?php echo $this->get_field_name('taxonomy'); ?>">
  902. <?php foreach ( get_taxonomies() as $taxonomy ) :
  903. $tax = get_taxonomy($taxonomy);
  904. if ( !$tax->show_tagcloud || empty($tax->labels->name) )
  905. continue;
  906. ?>
  907. <option value="<?php echo esc_attr($taxonomy) ?>" <?php selected($taxonomy, $current_taxonomy) ?>><?php echo $tax->labels->name; ?></option>
  908. <?php endforeach; ?>
  909. </select></p><?php
  910. }
  911. function _get_current_taxonomy($instance) {
  912. if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
  913. return $instance['taxonomy'];
  914. return 'post_tag';
  915. }
  916. }
  917. /**
  918. * Navigation Menu widget class
  919. *
  920. * @since 3.0.0
  921. */
  922. class WP_Nav_Menu_Widget extends WP_Widget {
  923. function __construct() {
  924. $widget_ops = array( 'description' => __('Use this widget to add one of your custom menus as a widget.') );
  925. parent::__construct( 'nav_menu', __('Custom Menu'), $widget_ops );
  926. }
  927. function widget($args, $instance) {
  928. // Get menu
  929. $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
  930. if ( !$nav_menu )
  931. return;
  932. $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
  933. echo $args['before_widget'];
  934. if ( !empty($instance['title']) )
  935. echo $args['before_title'] . $instance['title'] . $args['after_title'];
  936. wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ) );
  937. echo $args['after_widget'];
  938. }
  939. function update( $new_instance, $old_instance ) {
  940. $instance['title'] = strip_tags( stripslashes($new_instance['title']) );
  941. $instance['nav_menu'] = (int) $new_instance['nav_menu'];
  942. return $instance;
  943. }
  944. function form( $instance ) {
  945. $title = isset( $instance['title'] ) ? $instance['title'] : '';
  946. $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
  947. // Get menus
  948. $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
  949. // If no menus exists, direct the user to go and create some.
  950. if ( !$menus ) {
  951. echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>';
  952. return;
  953. }
  954. ?>
  955. <p>
  956. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
  957. <input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
  958. </p>
  959. <p>
  960. <label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
  961. <select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
  962. <?php
  963. foreach ( $menus as $menu ) {
  964. echo '<option value="' . $menu->term_id . '"'
  965. . selected( $nav_menu, $menu->term_id, false )
  966. . '>'. $menu->name . '</option>';
  967. }
  968. ?>
  969. </select>
  970. </p>
  971. <?php
  972. }
  973. }
  974. /**
  975. * Register all of the default WordPress widgets on startup.
  976. *
  977. * Calls 'widgets_init' action after all of the WordPress widgets have been
  978. * registered.
  979. *
  980. * @since 2.2.0
  981. */
  982. function wp_widgets_init() {
  983. if ( !is_blog_installed() )
  984. return;
  985. register_widget('WP_Widget_Pages');
  986. register_widget('WP_Widget_Calendar');
  987. register_widget('WP_Widget_Archives');
  988. if ( get_option( 'link_manager_enabled' ) )
  989. register_widget('WP_Widget_Links');
  990. register_widget('WP_Widget_Meta');
  991. register_widget('WP_Widget_Search');
  992. register_widget('WP_Widget_Text');
  993. register_widget('WP_Widget_Categories');
  994. register_widget('WP_Widget_Recent_Posts');
  995. register_widget('WP_Widget_Recent_Comments');
  996. register_widget('WP_Widget_RSS');
  997. register_widget('WP_Widget_Tag_Cloud');
  998. register_widget('WP_Nav_Menu_Widget');
  999. do_action('widgets_init');
  1000. }
  1001. add_action('init', 'wp_widgets_init', 1);