[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Custom Widget for displaying specific post formats
   4   *
   5   * Displays posts from Aside, Quote, Video, Audio, Image, Gallery, and Link formats.
   6   *
   7   * @link https://developer.wordpress.org/themes/functionality/widgets/#developing-widgets
   8   *
   9   * @package WordPress
  10   * @subpackage Twenty_Fourteen
  11   * @since Twenty Fourteen 1.0
  12   */
  13  
  14  class Twenty_Fourteen_Ephemera_Widget extends WP_Widget {
  15  
  16      /**
  17       * The supported post formats.
  18       *
  19       * @since Twenty Fourteen 1.0
  20       *
  21       * @var array
  22       */
  23      private $formats = array( 'aside', 'image', 'video', 'audio', 'quote', 'link', 'gallery' );
  24  
  25      /**
  26       * Constructor.
  27       *
  28       * @since Twenty Fourteen 1.0
  29       *
  30       * @return Twenty_Fourteen_Ephemera_Widget
  31       */
  32  	public function __construct() {
  33          parent::__construct(
  34              'widget_twentyfourteen_ephemera',
  35              __( 'Twenty Fourteen Ephemera', 'twentyfourteen' ),
  36              array(
  37                  'classname'                   => 'widget_twentyfourteen_ephemera',
  38                  'description'                 => __( 'Use this widget to list your recent Aside, Quote, Video, Audio, Image, Gallery, and Link posts.', 'twentyfourteen' ),
  39                  'customize_selective_refresh' => true,
  40              )
  41          );
  42  
  43          if ( is_active_widget( false, false, $this->id_base ) || is_customize_preview() ) {
  44              add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
  45          }
  46      }
  47  
  48      /**
  49       * Enqueue scripts.
  50       *
  51       * @since Twenty Fourteen 1.7
  52       */
  53  	public function enqueue_scripts() {
  54          /** This filter is documented in wp-includes/media.php */
  55          $audio_library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' );
  56          /** This filter is documented in wp-includes/media.php */
  57          $video_library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' );
  58          if ( in_array( 'mediaelement', array( $video_library, $audio_library ), true ) ) {
  59              wp_enqueue_style( 'wp-mediaelement' );
  60              wp_enqueue_script( 'mediaelement-vimeo' );
  61              wp_enqueue_script( 'wp-mediaelement' );
  62          }
  63      }
  64  
  65      /**
  66       * Output the HTML for this widget.
  67       *
  68       * @since Twenty Fourteen 1.0
  69       *
  70       * @param array $args     An array of standard parameters for widgets in this theme.
  71       * @param array $instance An array of settings for this widget instance.
  72       */
  73  	public function widget( $args, $instance ) {
  74          $format = isset( $instance['format'] ) && in_array( $instance['format'], $this->formats ) ? $instance['format'] : 'aside';
  75  
  76          switch ( $format ) {
  77              case 'image':
  78                  $format_string      = __( 'Images', 'twentyfourteen' );
  79                  $format_string_more = __( 'More images', 'twentyfourteen' );
  80                  break;
  81              case 'video':
  82                  $format_string      = __( 'Videos', 'twentyfourteen' );
  83                  $format_string_more = __( 'More videos', 'twentyfourteen' );
  84                  break;
  85              case 'audio':
  86                  $format_string      = __( 'Audio', 'twentyfourteen' );
  87                  $format_string_more = __( 'More audio', 'twentyfourteen' );
  88                  break;
  89              case 'quote':
  90                  $format_string      = __( 'Quotes', 'twentyfourteen' );
  91                  $format_string_more = __( 'More quotes', 'twentyfourteen' );
  92                  break;
  93              case 'link':
  94                  $format_string      = __( 'Links', 'twentyfourteen' );
  95                  $format_string_more = __( 'More links', 'twentyfourteen' );
  96                  break;
  97              case 'gallery':
  98                  $format_string      = __( 'Galleries', 'twentyfourteen' );
  99                  $format_string_more = __( 'More galleries', 'twentyfourteen' );
 100                  break;
 101              case 'aside':
 102              default:
 103                  $format_string      = __( 'Asides', 'twentyfourteen' );
 104                  $format_string_more = __( 'More asides', 'twentyfourteen' );
 105                  break;
 106          }
 107  
 108          $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] );
 109          $title  = apply_filters( 'widget_title', empty( $instance['title'] ) ? $format_string : $instance['title'], $instance, $this->id_base );
 110  
 111          $ephemera = new WP_Query(
 112              array(
 113                  'order'          => 'DESC',
 114                  'posts_per_page' => $number,
 115                  'no_found_rows'  => true,
 116                  'post_status'    => 'publish',
 117                  'post__not_in'   => get_option( 'sticky_posts' ),
 118                  'tax_query'      => array(
 119                      array(
 120                          'taxonomy' => 'post_format',
 121                          'terms'    => array( "post-format-$format" ),
 122                          'field'    => 'slug',
 123                          'operator' => 'IN',
 124                      ),
 125                  ),
 126              )
 127          );
 128  
 129          if ( $ephemera->have_posts() ) :
 130              $tmp_content_width        = $GLOBALS['content_width'];
 131              $GLOBALS['content_width'] = 306;
 132  
 133              echo $args['before_widget'];
 134              ?>
 135              <h1 class="widget-title <?php echo esc_attr( $format ); ?>">
 136                  <a class="entry-format" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"><?php echo esc_html( $title ); ?></a>
 137              </h1>
 138              <ol>
 139  
 140                  <?php
 141                  while ( $ephemera->have_posts() ) :
 142                      $ephemera->the_post();
 143                      $tmp_more        = $GLOBALS['more'];
 144                      $GLOBALS['more'] = 0;
 145                      ?>
 146                  <li>
 147                  <article <?php post_class(); ?>>
 148                  <div class="entry-content">
 149                      <?php
 150                      if ( has_post_format( 'gallery' ) ) :
 151  
 152                          if ( post_password_required() ) :
 153                              the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) );
 154                              else :
 155                                  $images = array();
 156  
 157                                  $galleries = get_post_galleries( get_the_ID(), false );
 158                                  if ( isset( $galleries[0]['ids'] ) ) {
 159                                      $images = explode( ',', $galleries[0]['ids'] );
 160                                  }
 161  
 162                                  if ( ! $images ) :
 163                                      $images = get_posts(
 164                                          array(
 165                                              'fields'      => 'ids',
 166                                              'numberposts' => -1,
 167                                              'order'       => 'ASC',
 168                                              'orderby'     => 'menu_order',
 169                                              'post_mime_type' => 'image',
 170                                              'post_parent' => get_the_ID(),
 171                                              'post_type'   => 'attachment',
 172                                          )
 173                                      );
 174                                  endif;
 175  
 176                                  $total_images = count( $images );
 177  
 178                                  if ( has_post_thumbnail() ) :
 179                                      $post_thumbnail = get_the_post_thumbnail();
 180                                      elseif ( $total_images > 0 ) :
 181                                          $image          = reset( $images );
 182                                          $post_thumbnail = wp_get_attachment_image( $image, 'post-thumbnail' );
 183                                      endif;
 184  
 185                                      if ( ! empty( $post_thumbnail ) ) :
 186                                          ?>
 187                          <a href="<?php the_permalink(); ?>"><?php echo $post_thumbnail; ?></a>
 188                          <?php endif; ?>
 189                          <p class="wp-caption-text">
 190                                  <?php
 191                                  printf(
 192                                      /* translators: 1: Post permalink, 2: Number of images in the gallery. */
 193                                      _n( 'This gallery contains <a href="%1$s" rel="bookmark">%2$s photo</a>.', 'This gallery contains <a href="%1$s" rel="bookmark">%2$s photos</a>.', $total_images, 'twentyfourteen' ),
 194                                      esc_url( get_permalink() ),
 195                                      number_format_i18n( $total_images )
 196                                  );
 197                                  ?>
 198                          </p>
 199                                  <?php
 200                          endif;
 201  
 202                              else :
 203                                  the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) );
 204                              endif;
 205                              ?>
 206                      </div><!-- .entry-content -->
 207  
 208                      <header class="entry-header">
 209                          <div class="entry-meta">
 210                          <?php
 211                          if ( ! has_post_format( 'link' ) ) :
 212                              the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' );
 213                              endif;
 214  
 215                              printf(
 216                                  '<span class="entry-date"><a href="%1$s" rel="bookmark"><time class="entry-date" datetime="%2$s">%3$s</time></a></span> <span class="byline"><span class="author vcard"><a class="url fn n" href="%4$s" rel="author">%5$s</a></span></span>',
 217                                  esc_url( get_permalink() ),
 218                                  esc_attr( get_the_date( 'c' ) ),
 219                                  esc_html( get_the_date() ),
 220                                  esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
 221                                  get_the_author()
 222                              );
 223  
 224                          if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) :
 225                              ?>
 226                              <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span>
 227                              <?php endif; ?>
 228                          </div><!-- .entry-meta -->
 229                      </header><!-- .entry-header -->
 230                  </article><!-- #post-<?php the_ID(); ?> -->
 231                  </li>
 232                  <?php endwhile; ?>
 233  
 234              </ol>
 235              <a class="post-format-archive-link" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>">
 236                  <?php
 237                      /* translators: Used with More archives link. */
 238                      printf( __( '%s <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ), $format_string_more );
 239                  ?>
 240              </a>
 241              <?php
 242  
 243              echo $args['after_widget'];
 244  
 245              // Reset the post globals as this query will have stomped on it.
 246              wp_reset_postdata();
 247  
 248              $GLOBALS['more']          = $tmp_more;
 249              $GLOBALS['content_width'] = $tmp_content_width;
 250  
 251          endif; // End check for ephemeral posts.
 252      }
 253  
 254      /**
 255       * Deal with the settings when they are saved by the admin.
 256       *
 257       * Here is where any validation should happen.
 258       *
 259       * @since Twenty Fourteen 1.0
 260       *
 261       * @param array $new_instance New widget instance.
 262       * @param array $instance     Original widget instance.
 263       * @return array Updated widget instance.
 264       */
 265  	function update( $new_instance, $instance ) {
 266          $instance['title']  = strip_tags( $new_instance['title'] );
 267          $instance['number'] = empty( $new_instance['number'] ) ? 2 : absint( $new_instance['number'] );
 268          if ( in_array( $new_instance['format'], $this->formats ) ) {
 269              $instance['format'] = $new_instance['format'];
 270          }
 271  
 272          return $instance;
 273      }
 274  
 275      /**
 276       * Display the form for this widget on the Widgets page of the Admin area.
 277       *
 278       * @since Twenty Fourteen 1.0
 279       *
 280       * @param array $instance
 281       */
 282  	function form( $instance ) {
 283          $title  = empty( $instance['title'] ) ? '' : esc_attr( $instance['title'] );
 284          $number = empty( $instance['number'] ) ? 2 : absint( $instance['number'] );
 285          $format = isset( $instance['format'] ) && in_array( $instance['format'], $this->formats ) ? $instance['format'] : 'aside';
 286          ?>
 287              <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyfourteen' ); ?></label>
 288              <input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>"></p>
 289  
 290              <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyfourteen' ); ?></label>
 291              <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>
 292  
 293              <p><label for="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>"><?php _e( 'Post format to show:', 'twentyfourteen' ); ?></label>
 294              <select id="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'format' ) ); ?>">
 295                  <?php foreach ( $this->formats as $slug ) : ?>
 296                  <option value="<?php echo esc_attr( $slug ); ?>"<?php selected( $format, $slug ); ?>><?php echo esc_html( get_post_format_string( $slug ) ); ?></option>
 297                  <?php endforeach; ?>
 298              </select>
 299          <?php
 300      }
 301  }


Generated: Thu Oct 17 08:20:02 2019 Cross-referenced by PHPXref 0.7