WP_Widget_Links

Core class used to implement a Links widget.

Defined (1)

The class is defined in the following location(s).

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