[ Index ]

PHP Cross Reference of WordPress

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'] ) ? $instance['format'] : '';
  75  
  76          if ( ! $format || ! in_array( $format, $this->formats, true ) ) {
  77              $format = 'aside';
  78          }
  79  
  80          switch ( $format ) {
  81              case 'image':
  82                  $format_string      = __( 'Images', 'twentyfourteen' );
  83                  $format_string_more = __( 'More images', 'twentyfourteen' );
  84                  break;
  85              case 'video':
  86                  $format_string      = __( 'Videos', 'twentyfourteen' );
  87                  $format_string_more = __( 'More videos', 'twentyfourteen' );
  88                  break;
  89              case 'audio':
  90                  $format_string      = __( 'Audio', 'twentyfourteen' );
  91                  $format_string_more = __( 'More audio', 'twentyfourteen' );
  92                  break;
  93              case 'quote':
  94                  $format_string      = __( 'Quotes', 'twentyfourteen' );
  95                  $format_string_more = __( 'More quotes', 'twentyfourteen' );
  96                  break;
  97              case 'link':
  98                  $format_string      = __( 'Links', 'twentyfourteen' );
  99                  $format_string_more = __( 'More links', 'twentyfourteen' );
 100                  break;
 101              case 'gallery':
 102                  $format_string      = __( 'Galleries', 'twentyfourteen' );
 103                  $format_string_more = __( 'More galleries', 'twentyfourteen' );
 104                  break;
 105              case 'aside':
 106              default:
 107                  $format_string      = __( 'Asides', 'twentyfourteen' );
 108                  $format_string_more = __( 'More asides', 'twentyfourteen' );
 109                  break;
 110          }
 111  
 112          $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : 2;
 113          $title  = ! empty( $instance['title'] ) ? $instance['title'] : $format_string;
 114          $title  = apply_filters( 'widget_title', $title, $instance, $this->id_base );
 115  
 116          $ephemera = new WP_Query(
 117              array(
 118                  'order'          => 'DESC',
 119                  'posts_per_page' => $number,
 120                  'no_found_rows'  => true,
 121                  'post_status'    => 'publish',
 122                  'post__not_in'   => get_option( 'sticky_posts' ),
 123                  'tax_query'      => array(
 124                      array(
 125                          'taxonomy' => 'post_format',
 126                          'terms'    => array( "post-format-$format" ),
 127                          'field'    => 'slug',
 128                          'operator' => 'IN',
 129                      ),
 130                  ),
 131              )
 132          );
 133  
 134          if ( $ephemera->have_posts() ) :
 135              $tmp_content_width        = $GLOBALS['content_width'];
 136              $GLOBALS['content_width'] = 306;
 137  
 138              echo $args['before_widget'];
 139              ?>
 140              <h1 class="widget-title <?php echo esc_attr( $format ); ?>">
 141                  <a class="entry-format" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>"><?php echo esc_html( $title ); ?></a>
 142              </h1>
 143              <ol>
 144  
 145                  <?php
 146                  while ( $ephemera->have_posts() ) :
 147                      $ephemera->the_post();
 148                      $tmp_more        = $GLOBALS['more'];
 149                      $GLOBALS['more'] = 0;
 150                      ?>
 151                  <li>
 152                  <article <?php post_class(); ?>>
 153                  <div class="entry-content">
 154                      <?php
 155                      if ( has_post_format( 'gallery' ) ) :
 156  
 157                          if ( post_password_required() ) :
 158                              the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) );
 159                              else :
 160                                  $images = array();
 161  
 162                                  $galleries = get_post_galleries( get_the_ID(), false );
 163                                  if ( isset( $galleries[0]['ids'] ) ) {
 164                                      $images = explode( ',', $galleries[0]['ids'] );
 165                                  }
 166  
 167                                  if ( ! $images ) :
 168                                      $images = get_posts(
 169                                          array(
 170                                              'fields'      => 'ids',
 171                                              'numberposts' => -1,
 172                                              'order'       => 'ASC',
 173                                              'orderby'     => 'menu_order',
 174                                              'post_mime_type' => 'image',
 175                                              'post_parent' => get_the_ID(),
 176                                              'post_type'   => 'attachment',
 177                                          )
 178                                      );
 179                                  endif;
 180  
 181                                  $total_images = count( $images );
 182  
 183                                  if ( has_post_thumbnail() ) :
 184                                      $post_thumbnail = get_the_post_thumbnail();
 185                                      elseif ( $total_images > 0 ) :
 186                                          $image          = reset( $images );
 187                                          $post_thumbnail = wp_get_attachment_image( $image, 'post-thumbnail' );
 188                                      endif;
 189  
 190                                      if ( ! empty( $post_thumbnail ) ) :
 191                                          ?>
 192                          <a href="<?php the_permalink(); ?>"><?php echo $post_thumbnail; ?></a>
 193                          <?php endif; ?>
 194                          <p class="wp-caption-text">
 195                                  <?php
 196                                  printf(
 197                                      /* translators: 1: Post permalink, 2: Number of images in the gallery. */
 198                                      _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' ),
 199                                      esc_url( get_permalink() ),
 200                                      number_format_i18n( $total_images )
 201                                  );
 202                                  ?>
 203                          </p>
 204                                  <?php
 205                          endif;
 206  
 207                              else :
 208                                  the_content( __( 'Continue reading <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ) );
 209                              endif;
 210                              ?>
 211                      </div><!-- .entry-content -->
 212  
 213                      <header class="entry-header">
 214                          <div class="entry-meta">
 215                          <?php
 216                          if ( ! has_post_format( 'link' ) ) :
 217                              the_title( '<h1 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h1>' );
 218                              endif;
 219  
 220                              printf(
 221                                  '<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>',
 222                                  esc_url( get_permalink() ),
 223                                  esc_attr( get_the_date( 'c' ) ),
 224                                  esc_html( get_the_date() ),
 225                                  esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
 226                                  get_the_author()
 227                              );
 228  
 229                          if ( ! post_password_required() && ( comments_open() || get_comments_number() ) ) :
 230                              ?>
 231                              <span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'twentyfourteen' ), __( '1 Comment', 'twentyfourteen' ), __( '% Comments', 'twentyfourteen' ) ); ?></span>
 232                              <?php endif; ?>
 233                          </div><!-- .entry-meta -->
 234                      </header><!-- .entry-header -->
 235                  </article><!-- #post-<?php the_ID(); ?> -->
 236                  </li>
 237                  <?php endwhile; ?>
 238  
 239              </ol>
 240              <a class="post-format-archive-link" href="<?php echo esc_url( get_post_format_link( $format ) ); ?>">
 241                  <?php
 242                      /* translators: Used with More archives link. */
 243                      printf( __( '%s <span class="meta-nav">&rarr;</span>', 'twentyfourteen' ), $format_string_more );
 244                  ?>
 245              </a>
 246              <?php
 247  
 248              echo $args['after_widget'];
 249  
 250              // Reset the post globals as this query will have stomped on it.
 251              wp_reset_postdata();
 252  
 253              $GLOBALS['more']          = $tmp_more;
 254              $GLOBALS['content_width'] = $tmp_content_width;
 255  
 256          endif; // End check for ephemeral posts.
 257      }
 258  
 259      /**
 260       * Deal with the settings when they are saved by the admin.
 261       *
 262       * Here is where any validation should happen.
 263       *
 264       * @since Twenty Fourteen 1.0
 265       *
 266       * @param array $new_instance New widget instance.
 267       * @param array $instance     Original widget instance.
 268       * @return array Updated widget instance.
 269       */
 270  	function update( $new_instance, $instance ) {
 271          $instance['title']  = strip_tags( $new_instance['title'] );
 272          $instance['number'] = empty( $new_instance['number'] ) ? 2 : absint( $new_instance['number'] );
 273  
 274          if ( in_array( $new_instance['format'], $this->formats, true ) ) {
 275              $instance['format'] = $new_instance['format'];
 276          }
 277  
 278          return $instance;
 279      }
 280  
 281      /**
 282       * Display the form for this widget on the Widgets page of the Admin area.
 283       *
 284       * @since Twenty Fourteen 1.0
 285       *
 286       * @param array $instance
 287       */
 288  	function form( $instance ) {
 289          $title  = ! empty( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
 290          $number = ! empty( $instance['number'] ) ? absint( $instance['number'] ) : 2;
 291          $format = isset( $instance['format'] ) ? $instance['format'] : '';
 292  
 293          if ( ! $format || ! in_array( $format, $this->formats, true ) ) {
 294              $format = 'aside';
 295          }
 296          ?>
 297              <p><label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php _e( 'Title:', 'twentyfourteen' ); ?></label>
 298              <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>
 299  
 300              <p><label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php _e( 'Number of posts to show:', 'twentyfourteen' ); ?></label>
 301              <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>
 302  
 303              <p><label for="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>"><?php _e( 'Post format to show:', 'twentyfourteen' ); ?></label>
 304              <select id="<?php echo esc_attr( $this->get_field_id( 'format' ) ); ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'format' ) ); ?>">
 305                  <?php foreach ( $this->formats as $slug ) : ?>
 306                  <option value="<?php echo esc_attr( $slug ); ?>"<?php selected( $format, $slug ); ?>><?php echo esc_html( get_post_format_string( $slug ) ); ?></option>
 307                  <?php endforeach; ?>
 308              </select>
 309          <?php
 310      }
 311  }


Generated: Tue Jun 2 01:00:03 2020 Cross-referenced by PHPXref 0.7.1