"Fossies" - the Fresh Open Source Software Archive

Member "wordpress/wp-includes/widgets/class-wp-widget-pages.php" (8 Jul 2020, 5688 Bytes) of package /linux/www/wordpress-5.7-RC1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "class-wp-widget-pages.php" see the Fossies "Dox" file reference documentation.

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