class-wp-widget-pages.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Widget API: WP_Widget_Pages class
  4. *
  5. * @package WordPress
  6. * @subpackage Widgets
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement a Pages widget.
  11. *
  12. * @since 2.8.0
  13. *
  14. * @see WP_Widget
  15. */
  16. class WP_Widget_Pages extends WP_Widget {
  17. /**
  18. * Sets up a new Pages widget instance.
  19. *
  20. * @since 2.8.0
  21. */
  22. public function __construct() {
  23. $widget_ops = array(
  24. 'classname' => 'widget_pages',
  25. 'description' => __( 'A list of your site&#8217;s Pages.' ),
  26. 'customize_selective_refresh' => true,
  27. 'show_instance_in_rest' => true,
  28. );
  29. parent::__construct( 'pages', __( 'Pages' ), $widget_ops );
  30. }
  31. /**
  32. * Outputs the content for the current Pages widget instance.
  33. *
  34. * @since 2.8.0
  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 Pages widget instance.
  39. */
  40. public function widget( $args, $instance ) {
  41. $default_title = __( 'Pages' );
  42. $title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title;
  43. /**
  44. * Filters the widget title.
  45. *
  46. * @since 2.6.0
  47. *
  48. * @param string $title The widget title. Default 'Pages'.
  49. * @param array $instance Array of settings for the current widget.
  50. * @param mixed $id_base The widget ID.
  51. */
  52. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  53. $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
  54. $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
  55. if ( 'menu_order' === $sortby ) {
  56. $sortby = 'menu_order, post_title';
  57. }
  58. $output = wp_list_pages(
  59. /**
  60. * Filters the arguments for the Pages widget.
  61. *
  62. * @since 2.8.0
  63. * @since 4.9.0 Added the `$instance` parameter.
  64. *
  65. * @see wp_list_pages()
  66. *
  67. * @param array $args An array of arguments to retrieve the pages list.
  68. * @param array $instance Array of settings for the current widget.
  69. */
  70. apply_filters(
  71. 'widget_pages_args',
  72. array(
  73. 'title_li' => '',
  74. 'echo' => 0,
  75. 'sort_column' => $sortby,
  76. 'exclude' => $exclude,
  77. ),
  78. $instance
  79. )
  80. );
  81. if ( ! empty( $output ) ) {
  82. echo $args['before_widget'];
  83. if ( $title ) {
  84. echo $args['before_title'] . $title . $args['after_title'];
  85. }
  86. $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
  87. /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
  88. $format = apply_filters( 'navigation_widgets_format', $format );
  89. if ( 'html5' === $format ) {
  90. // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
  91. $title = trim( strip_tags( $title ) );
  92. $aria_label = $title ? $title : $default_title;
  93. echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
  94. }
  95. ?>
  96. <ul>
  97. <?php echo $output; ?>
  98. </ul>
  99. <?php
  100. if ( 'html5' === $format ) {
  101. echo '</nav>';
  102. }
  103. echo $args['after_widget'];
  104. }
  105. }
  106. /**
  107. * Handles updating settings for the current Pages widget instance.
  108. *
  109. * @since 2.8.0
  110. *
  111. * @param array $new_instance New settings for this instance as input by the user via
  112. * WP_Widget::form().
  113. * @param array $old_instance Old settings for this instance.
  114. * @return array Updated settings to save.
  115. */
  116. public function update( $new_instance, $old_instance ) {
  117. $instance = $old_instance;
  118. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  119. if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ), true ) ) {
  120. $instance['sortby'] = $new_instance['sortby'];
  121. } else {
  122. $instance['sortby'] = 'menu_order';
  123. }
  124. $instance['exclude'] = sanitize_text_field( $new_instance['exclude'] );
  125. return $instance;
  126. }
  127. /**
  128. * Outputs the settings form for the Pages widget.
  129. *
  130. * @since 2.8.0
  131. *
  132. * @param array $instance Current settings.
  133. */
  134. public function form( $instance ) {
  135. // Defaults.
  136. $instance = wp_parse_args(
  137. (array) $instance,
  138. array(
  139. 'sortby' => 'post_title',
  140. 'title' => '',
  141. 'exclude' => '',
  142. )
  143. );
  144. ?>
  145. <p>
  146. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
  147. <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" />
  148. </p>
  149. <p>
  150. <label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label>
  151. <select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat">
  152. <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e( 'Page title' ); ?></option>
  153. <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e( 'Page order' ); ?></option>
  154. <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
  155. </select>
  156. </p>
  157. <p>
  158. <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label>
  159. <input type="text" value="<?php echo esc_attr( $instance['exclude'] ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'exclude' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>" class="widefat" />
  160. <br />
  161. <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
  162. </p>
  163. <?php
  164. }
  165. }