[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-content/themes/twentyeleven/inc/ -> widgets.php (source)

   1  <?php
   2  /**
   3   * Widget For displaying post format posts
   4   *
   5   * Handles displaying Aside, Link, Status, and Quote Posts available with Twenty Eleven.
   6   *
   7   * @link https://developer.wordpress.org/themes/functionality/widgets/#developing-widgets
   8   *
   9   * @package WordPress
  10   * @subpackage Twenty_Eleven
  11   * @since Twenty Eleven 1.0
  12   */
  13  class Twenty_Eleven_Ephemera_Widget extends WP_Widget {
  14  
  15      /**
  16       * PHP5 constructor.
  17       *
  18       * @since Twenty Eleven 2.2
  19       */
  20  	function __construct() {
  21          parent::__construct(
  22              'widget_twentyeleven_ephemera',
  23              __( 'Twenty Eleven Ephemera', 'twentyeleven' ),
  24              array(
  25                  'classname'                   => 'widget_twentyeleven_ephemera',
  26                  'description'                 => __( 'Use this widget to list your recent Aside, Status, Quote, and Link posts', 'twentyeleven' ),
  27                  'customize_selective_refresh' => true,
  28              )
  29          );
  30          $this->alt_option_name = 'widget_twentyeleven_ephemera';
  31  
  32          add_action( 'save_post', array( &$this, 'flush_widget_cache' ) );
  33          add_action( 'deleted_post', array( &$this, 'flush_widget_cache' ) );
  34          add_action( 'switch_theme', array( &$this, 'flush_widget_cache' ) );
  35      }
  36  
  37      /**
  38       * PHP4 constructor.
  39       *
  40       * @since Twenty Eleven 1.0
  41       */
  42  	function Twenty_Eleven_Ephemera_Widget() {
  43          self::__construct();
  44      }
  45  
  46      /**
  47       * Outputs the HTML for this widget.
  48       *
  49       * @since Twenty Eleven 1.0
  50       *
  51       * @param array $args     An array of standard parameters for widgets in this theme.
  52       * @param array $instance An array of settings for this widget instance.
  53       */
  54  	function widget( $args, $instance ) {
  55          $cache = wp_cache_get( 'widget_twentyeleven_ephemera', 'widget' );
  56  
  57          if ( ! is_array( $cache ) ) {
  58              $cache = array();
  59          }
  60  
  61          if ( ! isset( $args['widget_id'] ) ) {
  62              $args['widget_id'] = null;
  63          }
  64  
  65          if ( ! is_customize_preview() && isset( $cache[ $args['widget_id'] ] ) ) {
  66              echo $cache[ $args['widget_id'] ];
  67              return;
  68          }
  69  
  70          ob_start();
  71  
  72          /** This filter is documented in wp-includes/default-widgets.php */
  73          $args['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Ephemera', 'twentyeleven' ) : $instance['title'], $instance, $this->id_base );
  74  
  75          if ( ! isset( $instance['number'] ) ) {
  76              $instance['number'] = '10';
  77          }
  78  
  79          $args['number'] = absint( $instance['number'] );
  80          if ( ! $args['number'] ) {
  81              $args['number'] = 10;
  82          }
  83  
  84          $ephemera_args = array(
  85              'order'          => 'DESC',
  86              'posts_per_page' => $args['number'],
  87              'no_found_rows'  => true,
  88              'post_status'    => 'publish',
  89              'post__not_in'   => get_option( 'sticky_posts' ),
  90              'tax_query'      => array(
  91                  array(
  92                      'taxonomy' => 'post_format',
  93                      'terms'    => array( 'post-format-aside', 'post-format-link', 'post-format-status', 'post-format-quote' ),
  94                      'field'    => 'slug',
  95                      'operator' => 'IN',
  96                  ),
  97              ),
  98          );
  99          $ephemera      = new WP_Query( $ephemera_args );
 100  
 101          if ( $ephemera->have_posts() ) :
 102              echo $args['before_widget'];
 103              echo $args['before_title'];
 104              echo $args['title'];
 105              echo $args['after_title'];
 106              ?>
 107              <ol>
 108              <?php
 109              while ( $ephemera->have_posts() ) :
 110                  $ephemera->the_post();
 111                  ?>
 112  
 113                  <?php if ( 'link' != get_post_format() ) : ?>
 114  
 115                  <li class="widget-entry-title">
 116                      <a href="<?php echo esc_url( get_permalink() ); ?>" rel="bookmark"><?php the_title(); ?></a>
 117                      <span class="comments-link">
 118                          <?php comments_popup_link( __( '0 <span class="reply">comments &rarr;</span>', 'twentyeleven' ), __( '1 <span class="reply">comment &rarr;</span>', 'twentyeleven' ), __( '% <span class="reply">comments &rarr;</span>', 'twentyeleven' ) ); ?>
 119                      </span>
 120                  </li>
 121  
 122                  <?php else : ?>
 123  
 124                  <li class="widget-entry-title">
 125                      <a href="<?php echo esc_url( twentyeleven_get_first_url() ); ?>" rel="bookmark"><?php the_title(); ?>&nbsp;<span>&rarr;</span></a>
 126                      <span class="comments-link">
 127                          <?php comments_popup_link( __( '0 <span class="reply">comments &rarr;</span>', 'twentyeleven' ), __( '1 <span class="reply">comment &rarr;</span>', 'twentyeleven' ), __( '% <span class="reply">comments &rarr;</span>', 'twentyeleven' ) ); ?>
 128                      </span>
 129                  </li>
 130  
 131                  <?php endif; ?>
 132  
 133              <?php endwhile; ?>
 134              </ol>
 135              <?php
 136  
 137              echo $args['after_widget'];
 138  
 139              // Reset the post globals as this query will have stomped on it
 140              wp_reset_postdata();
 141  
 142              // end check for ephemeral posts
 143          endif;
 144  
 145          $cache[ $args['widget_id'] ] = ob_get_flush();
 146          if ( ! is_customize_preview() ) {
 147              wp_cache_set( 'widget_twentyeleven_ephemera', $cache, 'widget' );
 148          }
 149  
 150      }
 151  
 152      /**
 153       * Update widget settings.
 154       *
 155       * Deals with the settings when they are saved by the admin. Here is
 156       * where any validation should be dealt with.
 157       *
 158       * @since Twenty Eleven 1.0
 159       */
 160  	function update( $new_instance, $old_instance ) {
 161          $instance           = $old_instance;
 162          $instance['title']  = strip_tags( $new_instance['title'] );
 163          $instance['number'] = (int) $new_instance['number'];
 164          $this->flush_widget_cache();
 165  
 166          $alloptions = wp_cache_get( 'alloptions', 'options' );
 167          if ( isset( $alloptions['widget_twentyeleven_ephemera'] ) ) {
 168              delete_option( 'widget_twentyeleven_ephemera' );
 169          }
 170  
 171          return $instance;
 172      }
 173  
 174      /**
 175       * Flush widget cache.
 176       *
 177       * @since Twenty Eleven 1.0
 178       */
 179  	function flush_widget_cache() {
 180          wp_cache_delete( 'widget_twentyeleven_ephemera', 'widget' );
 181      }
 182  
 183      /**
 184       * Set up the widget form.
 185       *
 186       * Displays the form for this widget on the Widgets page of the WP Admin area.
 187       *
 188       * @since Twenty Eleven 1.0
 189       */
 190  	function form( $instance ) {
 191          $title  = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
 192          $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 10;
 193          ?>
 194              <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyeleven' ); ?></label>
 195              <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( $title ); ?>" /></p>
 196  
 197              <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyeleven' ); ?></label>
 198              <input id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo esc_attr( $number ); ?>" size="3" /></p>
 199          <?php
 200      }
 201  }


Generated: Tue Aug 20 08:20:01 2019 Cross-referenced by PHPXref 0.7