]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/widgets/class-wp-widget-pages.php
WordPress 4.7.2-scripts
[autoinstalls/wordpress.git] / wp-includes / widgets / class-wp-widget-pages.php
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 /**
11  * Core class used to implement a Pages widget.
12  *
13  * @since 2.8.0
14  *
15  * @see WP_Widget
16  */
17 class WP_Widget_Pages extends WP_Widget {
18
19         /**
20          * Sets up a new Pages widget instance.
21          *
22          * @since 2.8.0
23          * @access public
24          */
25         public function __construct() {
26                 $widget_ops = array(
27                         'classname' => 'widget_pages',
28                         'description' => __( 'A list of your site&#8217;s Pages.' ),
29                         'customize_selective_refresh' => true,
30                 );
31                 parent::__construct( 'pages', __( 'Pages' ), $widget_ops );
32         }
33
34         /**
35          * Outputs the content for the current Pages widget instance.
36          *
37          * @since 2.8.0
38          * @access public
39          *
40          * @param array $args     Display arguments including 'before_title', 'after_title',
41          *                        'before_widget', and 'after_widget'.
42          * @param array $instance Settings for the current Pages widget instance.
43          */
44         public function widget( $args, $instance ) {
45
46                 /**
47                  * Filters the widget title.
48                  *
49                  * @since 2.6.0
50                  *
51                  * @param string $title    The widget title. Default 'Pages'.
52                  * @param array  $instance An array of the widget's settings.
53                  * @param mixed  $id_base  The widget ID.
54                  */
55                 $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Pages' ) : $instance['title'], $instance, $this->id_base );
56
57                 $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
58                 $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
59
60                 if ( $sortby == 'menu_order' )
61                         $sortby = 'menu_order, post_title';
62
63                 /**
64                  * Filters the arguments for the Pages widget.
65                  *
66                  * @since 2.8.0
67                  *
68                  * @see wp_list_pages()
69                  *
70                  * @param array $args An array of arguments to retrieve the pages list.
71                  */
72                 $out = wp_list_pages( apply_filters( 'widget_pages_args', array(
73                         'title_li'    => '',
74                         'echo'        => 0,
75                         'sort_column' => $sortby,
76                         'exclude'     => $exclude
77                 ) ) );
78
79                 if ( ! empty( $out ) ) {
80                         echo $args['before_widget'];
81                         if ( $title ) {
82                                 echo $args['before_title'] . $title . $args['after_title'];
83                         }
84                 ?>
85                 <ul>
86                         <?php echo $out; ?>
87                 </ul>
88                 <?php
89                         echo $args['after_widget'];
90                 }
91         }
92
93         /**
94          * Handles updating settings for the current Pages widget instance.
95          *
96          * @since 2.8.0
97          * @access public
98          *
99          * @param array $new_instance New settings for this instance as input by the user via
100          *                            WP_Widget::form().
101          * @param array $old_instance Old settings for this instance.
102          * @return array Updated settings to save.
103          */
104         public function update( $new_instance, $old_instance ) {
105                 $instance = $old_instance;
106                 $instance['title'] = sanitize_text_field( $new_instance['title'] );
107                 if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
108                         $instance['sortby'] = $new_instance['sortby'];
109                 } else {
110                         $instance['sortby'] = 'menu_order';
111                 }
112
113                 $instance['exclude'] = sanitize_text_field( $new_instance['exclude'] );
114
115                 return $instance;
116         }
117
118         /**
119          * Outputs the settings form for the Pages widget.
120          *
121          * @since 2.8.0
122          * @access public
123          *
124          * @param array $instance Current settings.
125          */
126         public function form( $instance ) {
127                 //Defaults
128                 $instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
129                 ?>
130                 <p>
131                         <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
132                         <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'] ); ?>" />
133                 </p>
134                 <p>
135                         <label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label>
136                         <select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat">
137                                 <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
138                                 <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
139                                 <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
140                         </select>
141                 </p>
142                 <p>
143                         <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label>
144                         <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" />
145                         <br />
146                         <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
147                 </p>
148                 <?php
149         }
150
151 }