Completed
Push — master ( a7cd2a...eabd6c )
by Stephen
38:42
created

WP_Widget_Pages::form()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 24
rs 8.9713
cc 1
eloc 15
nc 1
nop 1
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('classname' => 'widget_pages', 'description' => __( 'A list of your site&#8217;s Pages.') );
27
		parent::__construct('pages', __('Pages'), $widget_ops);
28
	}
29
30
	/**
31
	 * Outputs the content for the current Pages widget instance.
32
	 *
33
	 * @since 2.8.0
34
	 * @access public
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
42
		/**
43
		 * Filter 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
53
		$sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
54
		$exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
55
56
		if ( $sortby == 'menu_order' )
57
			$sortby = 'menu_order, post_title';
58
59
		/**
60
		 * Filter the arguments for the Pages widget.
61
		 *
62
		 * @since 2.8.0
63
		 *
64
		 * @see wp_list_pages()
65
		 *
66
		 * @param array $args An array of arguments to retrieve the pages list.
67
		 */
68
		$out = wp_list_pages( apply_filters( 'widget_pages_args', array(
69
			'title_li'    => '',
70
			'echo'        => 0,
71
			'sort_column' => $sortby,
72
			'exclude'     => $exclude
73
		) ) );
74
75
		if ( ! empty( $out ) ) {
76
			echo $args['before_widget'];
77
			if ( $title ) {
78
				echo $args['before_title'] . $title . $args['after_title'];
79
			}
80
		?>
81
		<ul>
82
			<?php echo $out; ?>
83
		</ul>
84
		<?php
85
			echo $args['after_widget'];
86
		}
87
	}
88
89
	/**
90
	 * Handles updating settings for the current Pages widget instance.
91
	 *
92
	 * @since 2.8.0
93
	 * @access public
94
	 *
95
	 * @param array $new_instance New settings for this instance as input by the user via
96
	 *                            WP_Widget::form().
97
	 * @param array $old_instance Old settings for this instance.
98
	 * @return array Updated settings to save.
99
	 */
100
	public function update( $new_instance, $old_instance ) {
101
		$instance = $old_instance;
102
		$instance['title'] = sanitize_text_field( $new_instance['title'] );
103 View Code Duplication
		if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
104
			$instance['sortby'] = $new_instance['sortby'];
105
		} else {
106
			$instance['sortby'] = 'menu_order';
107
		}
108
109
		$instance['exclude'] = sanitize_text_field( $new_instance['exclude'] );
110
111
		return $instance;
112
	}
113
114
	/**
115
	 * Outputs the settings form for the Pages widget.
116
	 *
117
	 * @since 2.8.0
118
	 * @access public
119
	 *
120
	 * @param array $instance Current settings.
121
	 */
122
	public function form( $instance ) {
123
		//Defaults
124
		$instance = wp_parse_args( (array) $instance, array( 'sortby' => 'post_title', 'title' => '', 'exclude' => '') );
125
		?>
126
		<p>
127
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
128
			<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'] ); ?>" />
129
		</p>
130
		<p>
131
			<label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label>
132
			<select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat">
133
				<option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e('Page title'); ?></option>
134
				<option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e('Page order'); ?></option>
135
				<option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
136
			</select>
137
		</p>
138
		<p>
139
			<label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label>
140
			<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" />
141
			<br />
142
			<small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
143
		</p>
144
		<?php
145
	}
146
147
}
148