PageRenderTime 48ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/default-widgets.php

https://bitbucket.org/abnopanda/wordpress
PHP | 1201 lines | 873 code | 197 blank | 131 comment | 125 complexity | 414a65874953f32cc0111ded785bd301 MD5 | raw file
  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. // Prime cache for associated posts. (Prime post term cache if we need it for permalinks.)
  578. $post_ids = array_unique( wp_list_pluck( $comments, 'comment_post_ID' ) );
  579. _prime_post_caches( $post_ids, strpos( get_option( 'permalink_structure' ), '%category%' ), false );
  580. foreach ( (array) $comments as $comment) {
  581. $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>';
  582. }
  583. }
  584. $output .= '</ul>';
  585. $output .= $after_widget;
  586. echo $output;
  587. $cache[$args['widget_id']] = $output;
  588. wp_cache_set('widget_recent_comments', $cache, 'widget');
  589. }
  590. function update( $new_instance, $old_instance ) {
  591. $instance = $old_instance;
  592. $instance['title'] = strip_tags($new_instance['title']);
  593. $instance['number'] = absint( $new_instance['number'] );
  594. $this->flush_widget_cache();
  595. $alloptions = wp_cache_get( 'alloptions', 'options' );
  596. if ( isset($alloptions['widget_recent_comments']) )
  597. delete_option('widget_recent_comments');
  598. return $instance;
  599. }
  600. function form( $instance ) {
  601. $title = isset($instance['title']) ? esc_attr($instance['title']) : '';
  602. $number = isset($instance['number']) ? absint($instance['number']) : 5;
  603. ?>
  604. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
  605. <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>
  606. <p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('Number of comments to show:'); ?></label>
  607. <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>
  608. <?php
  609. }
  610. }
  611. /**
  612. * RSS widget class
  613. *
  614. * @since 2.8.0
  615. */
  616. class WP_Widget_RSS extends WP_Widget {
  617. function __construct() {
  618. $widget_ops = array( 'description' => __('Entries from any RSS or Atom feed') );
  619. $control_ops = array( 'width' => 400, 'height' => 200 );
  620. parent::__construct( 'rss', __('RSS'), $widget_ops, $control_ops );
  621. }
  622. function widget($args, $instance) {
  623. if ( isset($instance['error']) && $instance['error'] )
  624. return;
  625. extract($args, EXTR_SKIP);
  626. $url = ! empty( $instance['url'] ) ? $instance['url'] : '';
  627. while ( stristr($url, 'http') != $url )
  628. $url = substr($url, 1);
  629. if ( empty($url) )
  630. return;
  631. // self-url destruction sequence
  632. if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ) ) )
  633. return;
  634. $rss = fetch_feed($url);
  635. $title = $instance['title'];
  636. $desc = '';
  637. $link = '';
  638. if ( ! is_wp_error($rss) ) {
  639. $desc = esc_attr(strip_tags(@html_entity_decode($rss->get_description(), ENT_QUOTES, get_option('blog_charset'))));
  640. if ( empty($title) )
  641. $title = esc_html(strip_tags($rss->get_title()));
  642. $link = esc_url(strip_tags($rss->get_permalink()));
  643. while ( stristr($link, 'http') != $link )
  644. $link = substr($link, 1);
  645. }
  646. if ( empty($title) )
  647. $title = empty($desc) ? __('Unknown Feed') : $desc;
  648. $title = apply_filters('widget_title', $title, $instance, $this->id_base);
  649. $url = esc_url(strip_tags($url));
  650. $icon = includes_url('images/rss.png');
  651. if ( $title )
  652. $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>";
  653. echo $before_widget;
  654. if ( $title )
  655. echo $before_title . $title . $after_title;
  656. wp_widget_rss_output( $rss, $instance );
  657. echo $after_widget;
  658. if ( ! is_wp_error($rss) )
  659. $rss->__destruct();
  660. unset($rss);
  661. }
  662. function update($new_instance, $old_instance) {
  663. $testurl = ( isset( $new_instance['url'] ) && ( !isset( $old_instance['url'] ) || ( $new_instance['url'] != $old_instance['url'] ) ) );
  664. return wp_widget_rss_process( $new_instance, $testurl );
  665. }
  666. function form($instance) {
  667. if ( empty($instance) )
  668. $instance = array( 'title' => '', 'url' => '', 'items' => 10, 'error' => false, 'show_summary' => 0, 'show_author' => 0, 'show_date' => 0 );
  669. $instance['number'] = $this->number;
  670. wp_widget_rss_form( $instance );
  671. }
  672. }
  673. /**
  674. * Display the RSS entries in a list.
  675. *
  676. * @since 2.5.0
  677. *
  678. * @param string|array|object $rss RSS url.
  679. * @param array $args Widget arguments.
  680. */
  681. function wp_widget_rss_output( $rss, $args = array() ) {
  682. if ( is_string( $rss ) ) {
  683. $rss = fetch_feed($rss);
  684. } elseif ( is_array($rss) && isset($rss['url']) ) {
  685. $args = $rss;
  686. $rss = fetch_feed($rss['url']);
  687. } elseif ( !is_object($rss) ) {
  688. return;
  689. }
  690. if ( is_wp_error($rss) ) {
  691. if ( is_admin() || current_user_can('manage_options') )
  692. echo '<p>' . sprintf( __('<strong>RSS Error</strong>: %s'), $rss->get_error_message() ) . '</p>';
  693. return;
  694. }
  695. $default_args = array( 'show_author' => 0, 'show_date' => 0, 'show_summary' => 0 );
  696. $args = wp_parse_args( $args, $default_args );
  697. extract( $args, EXTR_SKIP );
  698. $items = (int) $items;
  699. if ( $items < 1 || 20 < $items )
  700. $items = 10;
  701. $show_summary = (int) $show_summary;
  702. $show_author = (int) $show_author;
  703. $show_date = (int) $show_date;
  704. if ( !$rss->get_item_quantity() ) {
  705. echo '<ul><li>' . __( 'An error has occurred, which probably means the feed is down. Try again later.' ) . '</li></ul>';
  706. $rss->__destruct();
  707. unset($rss);
  708. return;
  709. }
  710. echo '<ul>';
  711. foreach ( $rss->get_items(0, $items) as $item ) {
  712. $link = $item->get_link();
  713. while ( stristr($link, 'http') != $link )
  714. $link = substr($link, 1);
  715. $link = esc_url(strip_tags($link));
  716. $title = esc_attr(strip_tags($item->get_title()));
  717. if ( empty($title) )
  718. $title = __('Untitled');
  719. $desc = str_replace( array("\n", "\r"), ' ', esc_attr( strip_tags( @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option('blog_charset') ) ) ) );
  720. $desc = wp_html_excerpt( $desc, 360 );
  721. // Append ellipsis. Change existing [...] to [&hellip;].
  722. if ( '[...]' == substr( $desc, -5 ) )
  723. $desc = substr( $desc, 0, -5 ) . '[&hellip;]';
  724. elseif ( '[&hellip;]' != substr( $desc, -10 ) )
  725. $desc .= ' [&hellip;]';
  726. $desc = esc_html( $desc );
  727. if ( $show_summary ) {
  728. $summary = "<div class='rssSummary'>$desc</div>";
  729. } else {
  730. $summary = '';
  731. }
  732. $date = '';
  733. if ( $show_date ) {
  734. $date = $item->get_date( 'U' );
  735. if ( $date ) {
  736. $date = ' <span class="rss-date">' . date_i18n( get_option( 'date_format' ), $date ) . '</span>';
  737. }
  738. }
  739. $author = '';
  740. if ( $show_author ) {
  741. $author = $item->get_author();
  742. if ( is_object($author) ) {
  743. $author = $author->get_name();
  744. $author = ' <cite>' . esc_html( strip_tags( $author ) ) . '</cite>';
  745. }
  746. }
  747. if ( $link == '' ) {
  748. echo "<li>$title{$date}{$summary}{$author}</li>";
  749. } else {
  750. echo "<li><a class='rsswidget' href='$link' title='$desc'>$title</a>{$date}{$summary}{$author}</li>";
  751. }
  752. }
  753. echo '</ul>';
  754. $rss->__destruct();
  755. unset($rss);
  756. }
  757. /**
  758. * Display RSS widget options form.
  759. *
  760. * The options for what fields are displayed for the RSS form are all booleans
  761. * and are as follows: 'url', 'title', 'items', 'show_summary', 'show_author',
  762. * 'show_date'.
  763. *
  764. * @since 2.5.0
  765. *
  766. * @param array|string $args Values for input fields.
  767. * @param array $inputs Override default display options.
  768. */
  769. function wp_widget_rss_form( $args, $inputs = null ) {
  770. $default_inputs = array( 'url' => true, 'title' => true, 'items' => true, 'show_summary' => true, 'show_author' => true, 'show_date' => true );
  771. $inputs = wp_parse_args( $inputs, $default_inputs );
  772. extract( $args );
  773. extract( $inputs, EXTR_SKIP);
  774. $number = esc_attr( $number );
  775. $title = esc_attr( $title );
  776. $url = esc_url( $url );
  777. $items = (int) $items;
  778. if ( $items < 1 || 20 < $items )
  779. $items = 10;
  780. $show_summary = (int) $show_summary;
  781. $show_author = (int) $show_author;
  782. $show_date = (int) $show_date;
  783. if ( !empty($error) )
  784. echo '<p class="widget-error"><strong>' . sprintf( __('RSS Error: %s'), $error) . '</strong></p>';
  785. if ( $inputs['url'] ) :
  786. ?>
  787. <p><label for="rss-url-<?php echo $number; ?>"><?php _e('Enter the RSS feed URL here:'); ?></label>
  788. <input class="widefat" id="rss-url-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][url]" type="text" value="<?php echo $url; ?>" /></p>
  789. <?php endif; if ( $inputs['title'] ) : ?>
  790. <p><label for="rss-title-<?php echo $number; ?>"><?php _e('Give the feed a title (optional):'); ?></label>
  791. <input class="widefat" id="rss-title-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][title]" type="text" value="<?php echo $title; ?>" /></p>
  792. <?php endif; if ( $inputs['items'] ) : ?>
  793. <p><label for="rss-items-<?php echo $number; ?>"><?php _e('How many items would you like to display?'); ?></label>
  794. <select id="rss-items-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][items]">
  795. <?php
  796. for ( $i = 1; $i <= 20; ++$i )
  797. echo "<option value='$i' " . selected( $items, $i, false ) . ">$i</option>";
  798. ?>
  799. </select></p>
  800. <?php endif; if ( $inputs['show_summary'] ) : ?>
  801. <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"'; ?>/>
  802. <label for="rss-show-summary-<?php echo $number; ?>"><?php _e('Display item content?'); ?></label></p>
  803. <?php endif; if ( $inputs['show_author'] ) : ?>
  804. <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"'; ?>/>
  805. <label for="rss-show-author-<?php echo $number; ?>"><?php _e('Display item author if available?'); ?></label></p>
  806. <?php endif; if ( $inputs['show_date'] ) : ?>
  807. <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"'; ?>/>
  808. <label for="rss-show-date-<?php echo $number; ?>"><?php _e('Display item date?'); ?></label></p>
  809. <?php
  810. endif;
  811. foreach ( array_keys($default_inputs) as $input ) :
  812. if ( 'hidden' === $inputs[$input] ) :
  813. $id = str_replace( '_', '-', $input );
  814. ?>
  815. <input type="hidden" id="rss-<?php echo $id; ?>-<?php echo $number; ?>" name="widget-rss[<?php echo $number; ?>][<?php echo $input; ?>]" value="<?php echo $$input; ?>" />
  816. <?php
  817. endif;
  818. endforeach;
  819. }
  820. /**
  821. * Process RSS feed widget data and optionally retrieve feed items.
  822. *
  823. * The feed widget can not have more than 20 items or it will reset back to the
  824. * default, which is 10.
  825. *
  826. * The resulting array has the feed title, feed url, feed link (from channel),
  827. * feed items, error (if any), and whether to show summary, author, and date.
  828. * All respectively in the order of the array elements.
  829. *
  830. * @since 2.5.0
  831. *
  832. * @param array $widget_rss RSS widget feed data. Expects unescaped data.
  833. * @param bool $check_feed Optional, default is true. Whether to check feed for errors.
  834. * @return array
  835. */
  836. function wp_widget_rss_process( $widget_rss, $check_feed = true ) {
  837. $items = (int) $widget_rss['items'];
  838. if ( $items < 1 || 20 < $items )
  839. $items = 10;
  840. $url = esc_url_raw(strip_tags( $widget_rss['url'] ));
  841. $title = trim(strip_tags( $widget_rss['title'] ));
  842. $show_summary = isset($widget_rss['show_summary']) ? (int) $widget_rss['show_summary'] : 0;
  843. $show_author = isset($widget_rss['show_author']) ? (int) $widget_rss['show_author'] :0;
  844. $show_date = isset($widget_rss['show_date']) ? (int) $widget_rss['show_date'] : 0;
  845. if ( $check_feed ) {
  846. $rss = fetch_feed($url);
  847. $error = false;
  848. $link = '';
  849. if ( is_wp_error($rss) ) {
  850. $error = $rss->get_error_message();
  851. } else {
  852. $link = esc_url(strip_tags($rss->get_permalink()));
  853. while ( stristr($link, 'http') != $link )
  854. $link = substr($link, 1);
  855. $rss->__destruct();
  856. unset($rss);
  857. }
  858. }
  859. return compact( 'title', 'url', 'link', 'items', 'error', 'show_summary', 'show_author', 'show_date' );
  860. }
  861. /**
  862. * Tag cloud widget class
  863. *
  864. * @since 2.8.0
  865. */
  866. class WP_Widget_Tag_Cloud extends WP_Widget {
  867. function __construct() {
  868. $widget_ops = array( 'description' => __( "Your most used tags in cloud format") );
  869. parent::__construct('tag_cloud', __('Tag Cloud'), $widget_ops);
  870. }
  871. function widget( $args, $instance ) {
  872. extract($args);
  873. $current_taxonomy = $this->_get_current_taxonomy($instance);
  874. if ( !empty($instance['title']) ) {
  875. $title = $instance['title'];
  876. } else {
  877. if ( 'post_tag' == $current_taxonomy ) {
  878. $title = __('Tags');
  879. } else {
  880. $tax = get_taxonomy($current_taxonomy);
  881. $title = $tax->labels->name;
  882. }
  883. }
  884. $title = apply_filters('widget_title', $title, $instance, $this->id_base);
  885. echo $before_widget;
  886. if ( $title )
  887. echo $before_title . $title . $after_title;
  888. echo '<div class="tagcloud">';
  889. wp_tag_cloud( apply_filters('widget_tag_cloud_args', array('taxonomy' => $current_taxonomy) ) );
  890. echo "</div>\n";
  891. echo $after_widget;
  892. }
  893. function update( $new_instance, $old_instance ) {
  894. $instance['title'] = strip_tags(stripslashes($new_instance['title']));
  895. $instance['taxonomy'] = stripslashes($new_instance['taxonomy']);
  896. return $instance;
  897. }
  898. function form( $instance ) {
  899. $current_taxonomy = $this->_get_current_taxonomy($instance);
  900. ?>
  901. <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
  902. <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>
  903. <p><label for="<?php echo $this->get_field_id('taxonomy'); ?>"><?php _e('Taxonomy:') ?></label>
  904. <select class="widefat" id="<?php echo $this->get_field_id('taxonomy'); ?>" name="<?php echo $this->get_field_name('taxonomy'); ?>">
  905. <?php foreach ( get_taxonomies() as $taxonomy ) :
  906. $tax = get_taxonomy($taxonomy);
  907. if ( !$tax->show_tagcloud || empty($tax->labels->name) )
  908. continue;
  909. ?>
  910. <option value="<?php echo esc_attr($taxonomy) ?>" <?php selected($taxonomy, $current_taxonomy) ?>><?php echo $tax->labels->name; ?></option>
  911. <?php endforeach; ?>
  912. </select></p><?php
  913. }
  914. function _get_current_taxonomy($instance) {
  915. if ( !empty($instance['taxonomy']) && taxonomy_exists($instance['taxonomy']) )
  916. return $instance['taxonomy'];
  917. return 'post_tag';
  918. }
  919. }
  920. /**
  921. * Navigation Menu widget class
  922. *
  923. * @since 3.0.0
  924. */
  925. class WP_Nav_Menu_Widget extends WP_Widget {
  926. function __construct() {
  927. $widget_ops = array( 'description' => __('Use this widget to add one of your custom menus as a widget.') );
  928. parent::__construct( 'nav_menu', __('Custom Menu'), $widget_ops );
  929. }
  930. function widget($args, $instance) {
  931. // Get menu
  932. $nav_menu = ! empty( $instance['nav_menu'] ) ? wp_get_nav_menu_object( $instance['nav_menu'] ) : false;
  933. if ( !$nav_menu )
  934. return;
  935. $instance['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
  936. echo $args['before_widget'];
  937. if ( !empty($instance['title']) )
  938. echo $args['before_title'] . $instance['title'] . $args['after_title'];
  939. wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu ) );
  940. echo $args['after_widget'];
  941. }
  942. function update( $new_instance, $old_instance ) {
  943. $instance['title'] = strip_tags( stripslashes($new_instance['title']) );
  944. $instance['nav_menu'] = (int) $new_instance['nav_menu'];
  945. return $instance;
  946. }
  947. function form( $instance ) {
  948. $title = isset( $instance['title'] ) ? $instance['title'] : '';
  949. $nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
  950. // Get menus
  951. $menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );
  952. // If no menus exists, direct the user to go and create some.
  953. if ( !$menus ) {
  954. echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>';
  955. return;
  956. }
  957. ?>
  958. <p>
  959. <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
  960. <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; ?>" />
  961. </p>
  962. <p>
  963. <label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
  964. <select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
  965. <?php
  966. foreach ( $menus as $menu ) {
  967. echo '<option value="' . $menu->term_id . '"'
  968. . selected( $nav_menu, $menu->term_id, false )
  969. . '>'. $menu->name . '</option>';
  970. }
  971. ?>
  972. </select>
  973. </p>
  974. <?php
  975. }
  976. }
  977. /**
  978. * Register all of the default WordPress widgets on startup.
  979. *
  980. * Calls 'widgets_init' action after all of the WordPress widgets have been
  981. * registered.
  982. *
  983. * @since 2.2.0
  984. */
  985. function wp_widgets_init() {
  986. if ( !is_blog_installed() )
  987. return;
  988. register_widget('WP_Widget_Pages');
  989. register_widget('WP_Widget_Calendar');
  990. register_widget('WP_Widget_Archives');
  991. if ( get_option( 'link_manager_enabled' ) )
  992. register_widget('WP_Widget_Links');
  993. register_widget('WP_Widget_Meta');
  994. register_widget('WP_Widget_Search');
  995. register_widget('WP_Widget_Text');
  996. register_widget('WP_Widget_Categories');
  997. register_widget('WP_Widget_Recent_Posts');
  998. register_widget('WP_Widget_Recent_Comments');
  999. register_widget('WP_Widget_RSS');
  1000. register_widget('WP_Widget_Tag_Cloud');
  1001. register_widget('WP_Nav_Menu_Widget');
  1002. do_action('widgets_init');
  1003. }
  1004. add_action('init', 'wp_widgets_init', 1);