[ XREF Home ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Makes a custom Widget for displaying Aside, Link, Status, and Quote Posts available with Twenty Eleven
   4   *
   5   * Learn more: http://codex.wordpress.org/Widgets_API#Developing_Widgets
   6   *
   7   * @package WordPress
   8   * @subpackage Twenty_Eleven
   9   * @since Twenty Eleven 1.0
  10   */
  11  class Twenty_Eleven_Ephemera_Widget extends WP_Widget {
  12  
  13      /**
  14       * Constructor
  15       *
  16       * @return void
  17       **/
  18  	function Twenty_Eleven_Ephemera_Widget() {
  19          $widget_ops = array( 'classname' => 'widget_twentyeleven_ephemera', 'description' => __( 'Use this widget to list your recent Aside, Status, Quote, and Link posts', 'twentyeleven' ) );
  20          $this->WP_Widget( 'widget_twentyeleven_ephemera', __( 'Twenty Eleven Ephemera', 'twentyeleven' ), $widget_ops );
  21          $this->alt_option_name = 'widget_twentyeleven_ephemera';
  22  
  23          add_action( 'save_post', array(&$this, 'flush_widget_cache' ) );
  24          add_action( 'deleted_post', array(&$this, 'flush_widget_cache' ) );
  25          add_action( 'switch_theme', array(&$this, 'flush_widget_cache' ) );
  26      }
  27  
  28      /**
  29       * Outputs the HTML for this widget.
  30       *
  31       * @param array An array of standard parameters for widgets in this theme
  32       * @param array An array of settings for this widget instance
  33       * @return void Echoes it's output
  34       **/
  35  	function widget( $args, $instance ) {
  36          $cache = wp_cache_get( 'widget_twentyeleven_ephemera', 'widget' );
  37  
  38          if ( !is_array( $cache ) )
  39              $cache = array();
  40  
  41          if ( ! isset( $args['widget_id'] ) )
  42              $args['widget_id'] = null;
  43  
  44          if ( isset( $cache[$args['widget_id']] ) ) {
  45              echo $cache[$args['widget_id']];
  46              return;
  47          }
  48  
  49          ob_start();
  50          extract( $args, EXTR_SKIP );
  51  
  52          $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Ephemera', 'twentyeleven' ) : $instance['title'], $instance, $this->id_base);
  53  
  54          if ( ! isset( $instance['number'] ) )
  55              $instance['number'] = '10';
  56  
  57          if ( ! $number = absint( $instance['number'] ) )
  58               $number = 10;
  59  
  60          $ephemera_args = array(
  61              'order' => 'DESC',
  62              'posts_per_page' => $number,
  63              'nopaging' => 0,
  64              'post_status' => 'publish',
  65              'post__not_in' => get_option( 'sticky_posts' ),
  66              'tax_query' => array(
  67                  array(
  68                      'taxonomy' => 'post_format',
  69                      'terms' => array( 'post-format-aside', 'post-format-link', 'post-format-status', 'post-format-quote' ),
  70                      'field' => 'slug',
  71                      'operator' => 'IN',
  72                  ),
  73              ),
  74          );
  75          $ephemera = new WP_Query();
  76          $ephemera->query( $ephemera_args );
  77  
  78          if ( $ephemera->have_posts() ) :
  79  
  80          echo $before_widget;
  81          echo $before_title;
  82          echo $title; // Can set this with a widget option, or omit altogether
  83          echo $after_title;
  84  
  85          ?>
  86          <ol>
  87          <?php while ( $ephemera->have_posts() ) : $ephemera->the_post(); ?>
  88  
  89              <?php if ( 'link' != get_post_format() ) : ?>
  90  
  91              <li class="widget-entry-title">
  92                  <a href="<?php echo esc_url( get_permalink() ); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
  93                  <span class="comments-link">
  94                      <?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' ) ); ?>
  95                  </span>
  96              </li>
  97  
  98              <?php else : ?>
  99  
 100              <li class="widget-entry-title">
 101                  <?php
 102                      // Grab first link from the post content. If none found, use the post permalink as fallback.
 103                      $link_url = twentyeleven_url_grabber();
 104  
 105                      if ( empty( $link_url ) )
 106                          $link_url = get_permalink();
 107                  ?>
 108                  <a href="<?php echo esc_url( $link_url ); ?>" title="<?php printf( esc_attr__( 'Link to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?>&nbsp;<span>&rarr;</span></a>
 109                  <span class="comments-link">
 110                      <?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' ) ); ?>
 111                  </span>
 112              </li>
 113  
 114              <?php endif; ?>
 115  
 116          <?php endwhile; ?>
 117          </ol>
 118          <?php
 119  
 120          echo $after_widget;
 121  
 122          // Reset the post globals as this query will have stomped on it
 123          wp_reset_postdata();
 124  
 125          // end check for ephemeral posts
 126          endif;
 127  
 128          $cache[$args['widget_id']] = ob_get_flush();
 129          wp_cache_set( 'widget_twentyeleven_ephemera', $cache, 'widget' );
 130      }
 131  
 132      /**
 133       * Deals with the settings when they are saved by the admin. Here is
 134       * where any validation should be dealt with.
 135       **/
 136  	function update( $new_instance, $old_instance ) {
 137          $instance = $old_instance;
 138          $instance['title'] = strip_tags( $new_instance['title'] );
 139          $instance['number'] = (int) $new_instance['number'];
 140          $this->flush_widget_cache();
 141  
 142          $alloptions = wp_cache_get( 'alloptions', 'options' );
 143          if ( isset( $alloptions['widget_twentyeleven_ephemera'] ) )
 144              delete_option( 'widget_twentyeleven_ephemera' );
 145  
 146          return $instance;
 147      }
 148  
 149  	function flush_widget_cache() {
 150          wp_cache_delete( 'widget_twentyeleven_ephemera', 'widget' );
 151      }
 152  
 153      /**
 154       * Displays the form for this widget on the Widgets page of the WP Admin area.
 155       **/
 156  	function form( $instance ) {
 157          $title = isset( $instance['title']) ? esc_attr( $instance['title'] ) : '';
 158          $number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 10;
 159  ?>
 160              <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyeleven' ); ?></label>
 161              <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>
 162  
 163              <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyeleven' ); ?></label>
 164              <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>
 165          <?php
 166      }
 167  }


Generated: Wed Jun 1 08:30:02 2011 Cross-referenced by PHPXref 0.7
Provided by Yoast and awesome WordPress Hosting