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.
 
 
 
 
 

152 lines
4.7 KiB

  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. * @access public
  22. */
  23. public function __construct() {
  24. $widget_ops = array(
  25. 'classname' => 'widget_pages',
  26. 'description' => __( 'A list of your site&#8217;s Pages.' ),
  27. 'customize_selective_refresh' => 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. * @access public
  36. *
  37. * @param array $args Display arguments including 'before_title', 'after_title',
  38. * 'before_widget', and 'after_widget'.
  39. * @param array $instance Settings for the current Pages widget instance.
  40. */
  41. public function widget( $args, $instance ) {
  42. /**
  43. * Filters the widget title.
  44. *
  45. * @since 2.6.0
  46. *
  47. * @param string $title The widget title. Default 'Pages'.
  48. * @param array $instance An array of the widget's settings.
  49. * @param mixed $id_base The widget ID.
  50. */
  51. $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title'], $instance, $this->id_base );
  52. $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
  53. $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
  54. if ( $sortby == 'menu_order' )
  55. $sortby = 'menu_order, post_title';
  56. /**
  57. * Filters the arguments for the Pages widget.
  58. *
  59. * @since 2.8.0
  60. *
  61. * @see wp_list_pages()
  62. *
  63. * @param array $args An array of arguments to retrieve the pages list.
  64. */
  65. $out = wp_list_pages( apply_filters( 'widget_pages_args', array(
  66. 'title_li' => '',
  67. 'echo' => 0,
  68. 'sort_column' => $sortby,
  69. 'exclude' => $exclude
  70. ) ) );
  71. if ( ! empty( $out ) ) {
  72. echo $args['before_widget'];
  73. if ( $title ) {
  74. echo $args['before_title'] . $title . $args['after_title'];
  75. }
  76. ?>
  77. <ul>
  78. <?php echo $out; ?>
  79. </ul>
  80. <?php
  81. echo $args['after_widget'];
  82. }
  83. }
  84. /**
  85. * Handles updating settings for the current Pages widget instance.
  86. *
  87. * @since 2.8.0
  88. * @access public
  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. * @access public
  111. *
  112. * @param array $instance Current settings.
  113. */
  114. public function form( $instance ) {
  115. //Defaults
  116. $instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
  117. ?>
  118. <p>
  119. <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
  120. <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'] ); ?>" />
  121. </p>
  122. <p>
  123. <label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label>
  124. <select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat">
  125. <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
  126. <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
  127. <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
  128. </select>
  129. </p>
  130. <p>
  131. <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label>
  132. <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" />
  133. <br />
  134. <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
  135. </p>
  136. <?php
  137. }
  138. }