|
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 */ |
|
24 public function __construct() { |
|
25 $widget_ops = array( |
|
26 'classname' => 'widget_pages', |
|
27 'description' => __( 'A list of your site’s Pages.' ), |
|
28 'customize_selective_refresh' => true, |
|
29 ); |
|
30 parent::__construct( 'pages', __( 'Pages' ), $widget_ops ); |
|
31 } |
|
32 |
|
33 /** |
|
34 * Outputs the content for the current Pages widget instance. |
|
35 * |
|
36 * @since 2.8.0 |
|
37 * |
|
38 * @param array $args Display arguments including 'before_title', 'after_title', |
|
39 * 'before_widget', and 'after_widget'. |
|
40 * @param array $instance Settings for the current Pages widget instance. |
|
41 */ |
|
42 public function widget( $args, $instance ) { |
|
43 $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Pages' ); |
|
44 |
|
45 /** |
|
46 * Filters the widget title. |
|
47 * |
|
48 * @since 2.6.0 |
|
49 * |
|
50 * @param string $title The widget title. Default 'Pages'. |
|
51 * @param array $instance Array of settings for the current widget. |
|
52 * @param mixed $id_base The widget ID. |
|
53 */ |
|
54 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
|
55 |
|
56 $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby']; |
|
57 $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude']; |
|
58 |
|
59 if ( $sortby == 'menu_order' ) |
|
60 $sortby = 'menu_order, post_title'; |
|
61 |
|
62 /** |
|
63 * Filters the arguments for the Pages widget. |
|
64 * |
|
65 * @since 2.8.0 |
|
66 * @since 4.9.0 Added the `$instance` parameter. |
|
67 * |
|
68 * @see wp_list_pages() |
|
69 * |
|
70 * @param array $args An array of arguments to retrieve the pages list. |
|
71 * @param array $instance Array of settings for the current widget. |
|
72 */ |
|
73 $out = wp_list_pages( apply_filters( 'widget_pages_args', array( |
|
74 'title_li' => '', |
|
75 'echo' => 0, |
|
76 'sort_column' => $sortby, |
|
77 'exclude' => $exclude |
|
78 ), $instance ) ); |
|
79 |
|
80 if ( ! empty( $out ) ) { |
|
81 echo $args['before_widget']; |
|
82 if ( $title ) { |
|
83 echo $args['before_title'] . $title . $args['after_title']; |
|
84 } |
|
85 ?> |
|
86 <ul> |
|
87 <?php echo $out; ?> |
|
88 </ul> |
|
89 <?php |
|
90 echo $args['after_widget']; |
|
91 } |
|
92 } |
|
93 |
|
94 /** |
|
95 * Handles updating settings for the current Pages widget instance. |
|
96 * |
|
97 * @since 2.8.0 |
|
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 * |
|
123 * @param array $instance Current settings. |
|
124 */ |
|
125 public function form( $instance ) { |
|
126 //Defaults |
|
127 $instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') ); |
|
128 ?> |
|
129 <p> |
|
130 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label> |
|
131 <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'] ); ?>" /> |
|
132 </p> |
|
133 <p> |
|
134 <label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label> |
|
135 <select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat"> |
|
136 <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option> |
|
137 <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option> |
|
138 <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option> |
|
139 </select> |
|
140 </p> |
|
141 <p> |
|
142 <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label> |
|
143 <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" /> |
|
144 <br /> |
|
145 <small><?php _e( 'Page IDs, separated by commas.' ); ?></small> |
|
146 </p> |
|
147 <?php |
|
148 } |
|
149 |
|
150 } |