[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/widgets/ -> class-wp-widget-pages.php (source)

   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          $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 ( 'menu_order' === $sortby ) {
  60              $sortby = 'menu_order, post_title';
  61          }
  62  
  63          $out = wp_list_pages(
  64              /**
  65               * Filters the arguments for the Pages widget.
  66               *
  67               * @since 2.8.0
  68               * @since 4.9.0 Added the `$instance` parameter.
  69               *
  70               * @see wp_list_pages()
  71               *
  72               * @param array $args     An array of arguments to retrieve the pages list.
  73               * @param array $instance Array of settings for the current widget.
  74               */
  75              apply_filters(
  76                  'widget_pages_args',
  77                  array(
  78                      'title_li'    => '',
  79                      'echo'        => 0,
  80                      'sort_column' => $sortby,
  81                      'exclude'     => $exclude,
  82                  ),
  83                  $instance
  84              )
  85          );
  86  
  87          if ( ! empty( $out ) ) {
  88              echo $args['before_widget'];
  89              if ( $title ) {
  90                  echo $args['before_title'] . $title . $args['after_title'];
  91              }
  92              ?>
  93          <ul>
  94              <?php echo $out; ?>
  95          </ul>
  96              <?php
  97              echo $args['after_widget'];
  98          }
  99      }
 100  
 101      /**
 102       * Handles updating settings for the current Pages widget instance.
 103       *
 104       * @since 2.8.0
 105       *
 106       * @param array $new_instance New settings for this instance as input by the user via
 107       *                            WP_Widget::form().
 108       * @param array $old_instance Old settings for this instance.
 109       * @return array Updated settings to save.
 110       */
 111  	public function update( $new_instance, $old_instance ) {
 112          $instance          = $old_instance;
 113          $instance['title'] = sanitize_text_field( $new_instance['title'] );
 114          if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) ) {
 115              $instance['sortby'] = $new_instance['sortby'];
 116          } else {
 117              $instance['sortby'] = 'menu_order';
 118          }
 119  
 120          $instance['exclude'] = sanitize_text_field( $new_instance['exclude'] );
 121  
 122          return $instance;
 123      }
 124  
 125      /**
 126       * Outputs the settings form for the Pages widget.
 127       *
 128       * @since 2.8.0
 129       *
 130       * @param array $instance Current settings.
 131       */
 132  	public function form( $instance ) {
 133          //Defaults
 134          $instance = wp_parse_args(
 135              (array) $instance,
 136              array(
 137                  'sortby'  => 'post_title',
 138                  'title'   => '',
 139                  'exclude' => '',
 140              )
 141          );
 142          ?>
 143          <p>
 144              <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:' ); ?></label>
 145              <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'] ); ?>" />
 146          </p>
 147          <p>
 148              <label for="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>"><?php _e( 'Sort by:' ); ?></label>
 149              <select name="<?php echo esc_attr( $this->get_field_name( 'sortby' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'sortby' ) ); ?>" class="widefat">
 150                  <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e( 'Page title' ); ?></option>
 151                  <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e( 'Page order' ); ?></option>
 152                  <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
 153              </select>
 154          </p>
 155          <p>
 156              <label for="<?php echo esc_attr( $this->get_field_id( 'exclude' ) ); ?>"><?php _e( 'Exclude:' ); ?></label>
 157              <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" />
 158              <br />
 159              <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
 160          </p>
 161          <?php
 162      }
 163  
 164  }


Generated: Wed Dec 11 01:00:03 2019 Cross-referenced by PHPXref 0.7.1