[ Index ]

PHP Cross Reference of WordPress

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  	public 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       * @deprecated Twenty Eleven 2.2
  42       */
  43  	public function Twenty_Eleven_Ephemera_Widget() {
  44          self::__construct();
  45      }
  46  
  47      /**
  48       * Outputs the HTML for this widget.
  49       *
  50       * @since Twenty Eleven 1.0
  51       *
  52       * @param array $args     An array of standard parameters for widgets in this theme.
  53       * @param array $instance An array of settings for this widget instance.
  54       */
  55  	public function widget( $args, $instance ) {
  56          $cache = wp_cache_get( 'widget_twentyeleven_ephemera', 'widget' );
  57  
  58          if ( ! is_array( $cache ) ) {
  59              $cache = array();
  60          }
  61  
  62          if ( ! isset( $args['widget_id'] ) ) {
  63              $args['widget_id'] = null;
  64          }
  65  
  66          if ( ! is_customize_preview() && isset( $cache[ $args['widget_id'] ] ) ) {
  67              echo $cache[ $args['widget_id'] ];
  68              return;
  69          }
  70  
  71          ob_start();
  72  
  73          /** This filter is documented in wp-includes/default-widgets.php */
  74          $args['title'] = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Ephemera', 'twentyeleven' ) : $instance['title'], $instance, $this->id_base );
  75  
  76          if ( ! isset( $instance['number'] ) ) {
  77              $instance['number'] = '10';
  78          }
  79  
  80          $args['number'] = absint( $instance['number'] );
  81          if ( ! $args['number'] ) {
  82              $args['number'] = 10;
  83          }
  84  
  85          $ephemera_args = array(
  86              'order'          => 'DESC',
  87              'posts_per_page' => $args['number'],
  88              'no_found_rows'  => true,
  89              'post_status'    => 'publish',
  90              'post__not_in'   => get_option( 'sticky_posts' ),
  91              'tax_query'      => array(
  92                  array(
  93                      'taxonomy' => 'post_format',
  94                      'terms'    => array( 'post-format-aside', 'post-format-link', 'post-format-status', 'post-format-quote' ),
  95                      'field'    => 'slug',
  96                      'operator' => 'IN',
  97                  ),
  98              ),
  99          );
 100          $ephemera      = new WP_Query( $ephemera_args );
 101  
 102          if ( $ephemera->have_posts() ) :
 103              echo $args['before_widget'];
 104              echo $args['before_title'];
 105              echo $args['title'];
 106              echo $args['after_title'];
 107              ?>
 108              <ol>
 109              <?php
 110              while ( $ephemera->have_posts() ) :
 111                  $ephemera->the_post();
 112                  ?>
 113  
 114                  <?php if ( 'link' !== get_post_format() ) : ?>
 115  
 116                  <li class="widget-entry-title">
 117                      <a href="<?php echo esc_url( get_permalink() ); ?>" rel="bookmark"><?php the_title(); ?></a>
 118                      <span class="comments-link">
 119                          <?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' ) ); ?>
 120                      </span>
 121                  </li>
 122  
 123                  <?php else : ?>
 124  
 125                  <li class="widget-entry-title">
 126                      <a href="<?php echo esc_url( twentyeleven_get_first_url() ); ?>" rel="bookmark"><?php the_title(); ?>&nbsp;<span>&rarr;</span></a>
 127                      <span class="comments-link">
 128                          <?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' ) ); ?>
 129                      </span>
 130                  </li>
 131  
 132                  <?php endif; ?>
 133  
 134              <?php endwhile; ?>
 135              </ol>
 136              <?php
 137  
 138              echo $args['after_widget'];
 139  
 140              // Reset the post globals as this query will have stomped on it.
 141              wp_reset_postdata();
 142  
 143              // End check for ephemeral posts.
 144          endif;
 145  
 146          $cache[ $args['widget_id'] ] = ob_get_flush();
 147          if ( ! is_customize_preview() ) {
 148              wp_cache_set( 'widget_twentyeleven_ephemera', $cache, 'widget' );
 149          }
 150  
 151      }
 152  
 153      /**
 154       * Update widget settings.
 155       *
 156       * Deals with the settings when they are saved by the admin. Here is
 157       * where any validation should be dealt with.
 158       *
 159       * @since Twenty Eleven 1.0
 160       */
 161  	public function update( $new_instance, $old_instance ) {
 162          $instance           = $old_instance;
 163          $instance['title']  = strip_tags( $new_instance['title'] );
 164          $instance['number'] = (int) $new_instance['number'];
 165          $this->flush_widget_cache();
 166  
 167          $alloptions = wp_cache_get( 'alloptions', 'options' );
 168          if ( isset( $alloptions['widget_twentyeleven_ephemera'] ) ) {
 169              delete_option( 'widget_twentyeleven_ephemera' );
 170          }
 171  
 172          return $instance;
 173      }
 174  
 175      /**
 176       * Flush widget cache.
 177       *
 178       * @since Twenty Eleven 1.0
 179       */
 180  	public function flush_widget_cache() {
 181          wp_cache_delete( 'widget_twentyeleven_ephemera', 'widget' );
 182      }
 183  
 184      /**
 185       * Set up the widget form.
 186       *
 187       * Displays the form for this widget on the Widgets page of the WP Admin area.
 188       *
 189       * @since Twenty Eleven 1.0
 190       */
 191  	public function form( $instance ) {
 192          $title  = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
 193          $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 10;
 194          ?>
 195              <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyeleven' ); ?></label>
 196              <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>
 197  
 198              <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyeleven' ); ?></label>
 199              <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>
 200          <?php
 201      }
 202  }


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1