You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

167 lines
6.8 KiB

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