[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
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 'show_instance_in_rest' => 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 * 39 * @param array $args Display arguments including 'before_title', 'after_title', 40 * 'before_widget', and 'after_widget'. 41 * @param array $instance Settings for the current Pages widget instance. 42 */ 43 public function widget( $args, $instance ) { 44 $default_title = __( 'Pages' ); 45 $title = ! empty( $instance['title'] ) ? $instance['title'] : $default_title; 46 47 /** 48 * Filters the widget title. 49 * 50 * @since 2.6.0 51 * 52 * @param string $title The widget title. Default 'Pages'. 53 * @param array $instance Array of settings for the current widget. 54 * @param mixed $id_base The widget ID. 55 */ 56 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); 57 58 $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby']; 59 $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude']; 60 61 if ( 'menu_order' === $sortby ) { 62 $sortby = 'menu_order, post_title'; 63 } 64 65 $output = wp_list_pages( 66 /** 67 * Filters the arguments for the Pages widget. 68 * 69 * @since 2.8.0 70 * @since 4.9.0 Added the `$instance` parameter. 71 * 72 * @see wp_list_pages() 73 * 74 * @param array $args An array of arguments to retrieve the pages list. 75 * @param array $instance Array of settings for the current widget. 76 */ 77 apply_filters( 78 'widget_pages_args', 79 array( 80 'title_li' => '', 81 'echo' => 0, 82 'sort_column' => $sortby, 83 'exclude' => $exclude, 84 ), 85 $instance 86 ) 87 ); 88 89 if ( ! empty( $output ) ) { 90 echo $args['before_widget']; 91 if ( $title ) { 92 echo $args['before_title'] . $title . $args['after_title']; 93 } 94 95 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; 96 97 /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */ 98 $format = apply_filters( 'navigation_widgets_format', $format ); 99 100 if ( 'html5' === $format ) { 101 // The title may be filtered: Strip out HTML and make sure the aria-label is never empty. 102 $title = trim( strip_tags( $title ) ); 103 $aria_label = $title ? $title : $default_title; 104 echo '<nav aria-label="' . esc_attr( $aria_label ) . '">'; 105 } 106 ?> 107 108 <ul> 109 <?php echo $output; ?> 110 </ul> 111 112 <?php 113 if ( 'html5' === $format ) { 114 echo '</nav>'; 115 } 116 117 echo $args['after_widget']; 118 } 119 } 120 121 /** 122 * Handles updating settings for the current Pages widget instance. 123 * 124 * @since 2.8.0 125 * 126 * @param array $new_instance New settings for this instance as input by the user via 127 * WP_Widget::form(). 128 * @param array $old_instance Old settings for this instance. 129 * @return array Updated settings to save. 130 */ 131 public function update( $new_instance, $old_instance ) { 132 $instance = $old_instance; 133 $instance['title'] = sanitize_text_field( $new_instance['title'] ); 134 if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ), true ) ) { 135 $instance['sortby'] = $new_instance['sortby']; 136 } else { 137 $instance['sortby'] = 'menu_order'; 138 } 139 140 $instance['exclude'] = sanitize_text_field( $new_instance['exclude'] ); 141 142 return $instance; 143 } 144 145 /** 146 * Outputs the settings form for the Pages widget. 147 * 148 * @since 2.8.0 149 * 150 * @param array $instance Current settings. 151 */ 152 public function form( $instance ) { 153 // Defaults. 154 $instance = wp_parse_args( 155 (array) $instance, 156 array( 157 'sortby' => 'post_title', 158 'title' => '', 159 'exclude' => '', 160 ) 161 ); 162 ?> 163 <p> 164 <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label> 165 <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'] ); ?>" /> 166 </p> 167 168 <p> 169 <label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label> 170 <select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat"> 171 <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e( 'Page title' ); ?></option> 172 <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e( 'Page order' ); ?></option> 173 <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option> 174 </select> 175 </p> 176 177 <p> 178 <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label> 179 <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" /> 180 <br /> 181 <small><?php _e( 'Page IDs, separated by commas.' ); ?></small> 182 </p> 183 <?php 184 } 185 186 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Nov 21 01:00:03 2024 | Cross-referenced by PHPXref 0.7.1 |