class-wp-widget-links.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * Widget API: WP_Widget_Links class
  4. *
  5. * @package WordPress
  6. * @subpackage Widgets
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement a Links widget.
  11. *
  12. * @since 2.8.0
  13. *
  14. * @see WP_Widget
  15. */
  16. class WP_Widget_Links extends WP_Widget {
  17. /**
  18. * Sets up a new Links widget instance.
  19. *
  20. * @since 2.8.0
  21. */
  22. public function __construct() {
  23. $widget_ops = array(
  24. 'description' => __( 'Your blogroll' ),
  25. 'customize_selective_refresh' => true,
  26. );
  27. parent::__construct( 'links', __( 'Links' ), $widget_ops );
  28. }
  29. /**
  30. * Outputs the content for the current Links widget instance.
  31. *
  32. * @since 2.8.0
  33. *
  34. * @param array $args Display arguments including 'before_title', 'after_title',
  35. * 'before_widget', and 'after_widget'.
  36. * @param array $instance Settings for the current Links widget instance.
  37. */
  38. public function widget( $args, $instance ) {
  39. $show_description = isset( $instance['description'] ) ? $instance['description'] : false;
  40. $show_name = isset( $instance['name'] ) ? $instance['name'] : false;
  41. $show_rating = isset( $instance['rating'] ) ? $instance['rating'] : false;
  42. $show_images = isset( $instance['images'] ) ? $instance['images'] : true;
  43. $category = isset( $instance['category'] ) ? $instance['category'] : false;
  44. $orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'name';
  45. $order = 'rating' === $orderby ? 'DESC' : 'ASC';
  46. $limit = isset( $instance['limit'] ) ? $instance['limit'] : -1;
  47. $before_widget = preg_replace( '/ id="[^"]*"/', ' id="%id"', $args['before_widget'] );
  48. $widget_links_args = array(
  49. 'title_before' => $args['before_title'],
  50. 'title_after' => $args['after_title'],
  51. 'category_before' => $before_widget,
  52. 'category_after' => $args['after_widget'],
  53. 'show_images' => $show_images,
  54. 'show_description' => $show_description,
  55. 'show_name' => $show_name,
  56. 'show_rating' => $show_rating,
  57. 'category' => $category,
  58. 'class' => 'linkcat widget',
  59. 'orderby' => $orderby,
  60. 'order' => $order,
  61. 'limit' => $limit,
  62. );
  63. /**
  64. * Filters the arguments for the Links widget.
  65. *
  66. * @since 2.6.0
  67. * @since 4.4.0 Added the `$instance` parameter.
  68. *
  69. * @see wp_list_bookmarks()
  70. *
  71. * @param array $widget_links_args An array of arguments to retrieve the links list.
  72. * @param array $instance The settings for the particular instance of the widget.
  73. */
  74. wp_list_bookmarks( apply_filters( 'widget_links_args', $widget_links_args, $instance ) );
  75. }
  76. /**
  77. * Handles updating settings for the current Links widget instance.
  78. *
  79. * @since 2.8.0
  80. *
  81. * @param array $new_instance New settings for this instance as input by the user via
  82. * WP_Widget::form().
  83. * @param array $old_instance Old settings for this instance.
  84. * @return array Updated settings to save.
  85. */
  86. public function update( $new_instance, $old_instance ) {
  87. $new_instance = (array) $new_instance;
  88. $instance = array(
  89. 'images' => 0,
  90. 'name' => 0,
  91. 'description' => 0,
  92. 'rating' => 0,
  93. );
  94. foreach ( $instance as $field => $val ) {
  95. if ( isset( $new_instance[ $field ] ) ) {
  96. $instance[ $field ] = 1;
  97. }
  98. }
  99. $instance['orderby'] = 'name';
  100. if ( in_array( $new_instance['orderby'], array( 'name', 'rating', 'id', 'rand' ), true ) ) {
  101. $instance['orderby'] = $new_instance['orderby'];
  102. }
  103. $instance['category'] = (int) $new_instance['category'];
  104. $instance['limit'] = ! empty( $new_instance['limit'] ) ? (int) $new_instance['limit'] : -1;
  105. return $instance;
  106. }
  107. /**
  108. * Outputs the settings form for the Links widget.
  109. *
  110. * @since 2.8.0
  111. *
  112. * @param array $instance Current settings.
  113. */
  114. public function form( $instance ) {
  115. // Defaults.
  116. $instance = wp_parse_args(
  117. (array) $instance,
  118. array(
  119. 'images' => true,
  120. 'name' => true,
  121. 'description' => false,
  122. 'rating' => false,
  123. 'category' => false,
  124. 'orderby' => 'name',
  125. 'limit' => -1,
  126. )
  127. );
  128. $link_cats = get_terms( array( 'taxonomy' => 'link_category' ) );
  129. $limit = (int) $instance['limit'];
  130. if ( ! $limit ) {
  131. $limit = -1;
  132. }
  133. ?>
  134. <p>
  135. <label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Select Link Category:' ); ?></label>
  136. <select class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>">
  137. <option value=""><?php _ex( 'All Links', 'links widget' ); ?></option>
  138. <?php foreach ( $link_cats as $link_cat ) : ?>
  139. <option value="<?php echo (int) $link_cat->term_id; ?>" <?php selected( $instance['category'], $link_cat->term_id ); ?>>
  140. <?php echo esc_html( $link_cat->name ); ?>
  141. </option>
  142. <?php endforeach; ?>
  143. </select>
  144. <label for="<?php echo $this->get_field_id( 'orderby' ); ?>"><?php _e( 'Sort by:' ); ?></label>
  145. <select name="<?php echo $this->get_field_name( 'orderby' ); ?>" id="<?php echo $this->get_field_id( 'orderby' ); ?>" class="widefat">
  146. <option value="name"<?php selected( $instance['orderby'], 'name' ); ?>><?php _e( 'Link title' ); ?></option>
  147. <option value="rating"<?php selected( $instance['orderby'], 'rating' ); ?>><?php _e( 'Link rating' ); ?></option>
  148. <option value="id"<?php selected( $instance['orderby'], 'id' ); ?>><?php _e( 'Link ID' ); ?></option>
  149. <option value="rand"<?php selected( $instance['orderby'], 'rand' ); ?>><?php _ex( 'Random', 'Links widget' ); ?></option>
  150. </select>
  151. </p>
  152. <p>
  153. <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' ); ?>" />
  154. <label for="<?php echo $this->get_field_id( 'images' ); ?>"><?php _e( 'Show Link Image' ); ?></label>
  155. <br />
  156. <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' ); ?>" />
  157. <label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'Show Link Name' ); ?></label>
  158. <br />
  159. <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' ); ?>" />
  160. <label for="<?php echo $this->get_field_id( 'description' ); ?>"><?php _e( 'Show Link Description' ); ?></label>
  161. <br />
  162. <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' ); ?>" />
  163. <label for="<?php echo $this->get_field_id( 'rating' ); ?>"><?php _e( 'Show Link Rating' ); ?></label>
  164. </p>
  165. <p>
  166. <label for="<?php echo $this->get_field_id( 'limit' ); ?>"><?php _e( 'Number of links to show:' ); ?></label>
  167. <input id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" type="text" value="<?php echo ( -1 !== $limit ) ? (int) $limit : ''; ?>" size="3" />
  168. </p>
  169. <?php
  170. }
  171. }