class-wp-widget-pages.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. );
  28. parent::__construct( 'pages', __( 'Pages' ), $widget_ops );
  29. }
  30. /**
  31. * Outputs the content for the current Pages widget instance.
  32. *
  33. * @since 2.8.0
  34. *
  35. * @param array $args Display arguments including 'before_title', 'after_title',
  36. * 'before_widget', and 'after_widget'.
  37. * @param array $instance Settings for the current Pages widget instance.
  38. */
  39. public function widget( $args, $instance ) {
  40. $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Pages' );
  41. /**
  42. * Filters the widget title.
  43. *
  44. * @since 2.6.0
  45. *
  46. * @param string $title The widget title. Default 'Pages'.
  47. * @param array $instance Array of settings for the current widget.
  48. * @param mixed $id_base The widget ID.
  49. */
  50. $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
  51. $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
  52. $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
  53. if ( $sortby == 'menu_order' )
  54. $sortby = 'menu_order, post_title';
  55. /**
  56. * Filters the arguments for the Pages widget.
  57. *
  58. * @since 2.8.0
  59. * @since 4.9.0 Added the `$instance` parameter.
  60. *
  61. * @see wp_list_pages()
  62. *
  63. * @param array $args An array of arguments to retrieve the pages list.
  64. * @param array $instance Array of settings for the current widget.
  65. */
  66. $out = wp_list_pages( apply_filters( 'widget_pages_args', array(
  67. 'title_li' => '',
  68. 'echo' => 0,
  69. 'sort_column' => $sortby,
  70. 'exclude' => $exclude
  71. ), $instance ) );
  72. if ( ! empty( $out ) ) {
  73. echo $args['before_widget'];
  74. if ( $title ) {
  75. echo $args['before_title'] . $title . $args['after_title'];
  76. }
  77. ?>
  78. <ul>
  79. <?php echo $out; ?>
  80. </ul>
  81. <?php
  82. echo $args['after_widget'];
  83. }
  84. }
  85. /**
  86. * Handles updating settings for the current Pages widget instance.
  87. *
  88. * @since 2.8.0
  89. *
  90. * @param array $new_instance New settings for this instance as input by the user via
  91. * WP_Widget::form().
  92. * @param array $old_instance Old settings for this instance.
  93. * @return array Updated settings to save.
  94. */
  95. public function update( $new_instance, $old_instance ) {
  96. $instance = $old_instance;
  97. $instance['title'] = sanitize_text_field( $new_instance['title'] );
  98. if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
  99. $instance['sortby'] = $new_instance['sortby'];
  100. } else {
  101. $instance['sortby'] = 'menu_order';
  102. }
  103. $instance['exclude'] = sanitize_text_field( $new_instance['exclude'] );
  104. return $instance;
  105. }
  106. /**
  107. * Outputs the settings form for the Pages widget.
  108. *
  109. * @since 2.8.0
  110. *
  111. * @param array $instance Current settings.
  112. */
  113. public function form( $instance ) {
  114. //Defaults
  115. $instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
  116. ?>
  117. <p>
  118. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
  119. <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'] ); ?>" />
  120. </p>
  121. <p>
  122. <label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label>
  123. <select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat">
  124. <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
  125. <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
  126. <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
  127. </select>
  128. </p>
  129. <p>
  130. <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label>
  131. <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" />
  132. <br />
  133. <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
  134. </p>
  135. <?php
  136. }
  137. }