Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (7 years ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentysixteen/inc/template-tags.php

    r41756 r42343  
    1111
    1212if ( ! function_exists( 'twentysixteen_entry_meta' ) ) :
    13 /**
    14  * Prints HTML with meta information for the categories, tags.
    15  *
    16  * Create your own twentysixteen_entry_meta() function to override in a child theme.
    17  *
    18  * @since Twenty Sixteen 1.0
    19  */
    20 function twentysixteen_entry_meta() {
    21     if ( 'post' === get_post_type() ) {
    22         $author_avatar_size = apply_filters( 'twentysixteen_author_avatar_size', 49 );
    23         printf( '<span class="byline"><span class="author vcard">%1$s<span class="screen-reader-text">%2$s </span> <a class="url fn n" href="%3$s">%4$s</a></span></span>',
    24             get_avatar( get_the_author_meta( 'user_email' ), $author_avatar_size ),
    25             _x( 'Author', 'Used before post author name.', 'twentysixteen' ),
    26             esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    27             get_the_author()
     13    /**
     14     * Prints HTML with meta information for the categories, tags.
     15     *
     16     * Create your own twentysixteen_entry_meta() function to override in a child theme.
     17     *
     18     * @since Twenty Sixteen 1.0
     19     */
     20    function twentysixteen_entry_meta() {
     21        if ( 'post' === get_post_type() ) {
     22            $author_avatar_size = apply_filters( 'twentysixteen_author_avatar_size', 49 );
     23            printf(
     24                '<span class="byline"><span class="author vcard">%1$s<span class="screen-reader-text">%2$s </span> <a class="url fn n" href="%3$s">%4$s</a></span></span>',
     25                get_avatar( get_the_author_meta( 'user_email' ), $author_avatar_size ),
     26                _x( 'Author', 'Used before post author name.', 'twentysixteen' ),
     27                esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
     28                get_the_author()
     29            );
     30        }
     31
     32        if ( in_array( get_post_type(), array( 'post', 'attachment' ) ) ) {
     33            twentysixteen_entry_date();
     34        }
     35
     36        $format = get_post_format();
     37        if ( current_theme_supports( 'post-formats', $format ) ) {
     38            printf(
     39                '<span class="entry-format">%1$s<a href="%2$s">%3$s</a></span>',
     40                sprintf( '<span class="screen-reader-text">%s </span>', _x( 'Format', 'Used before post format.', 'twentysixteen' ) ),
     41                esc_url( get_post_format_link( $format ) ),
     42                get_post_format_string( $format )
     43            );
     44        }
     45
     46        if ( 'post' === get_post_type() ) {
     47            twentysixteen_entry_taxonomies();
     48        }
     49
     50        if ( ! is_singular() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
     51            echo '<span class="comments-link">';
     52            comments_popup_link( sprintf( __( 'Leave a comment<span class="screen-reader-text"> on %s</span>', 'twentysixteen' ), get_the_title() ) );
     53            echo '</span>';
     54        }
     55    }
     56endif;
     57
     58if ( ! function_exists( 'twentysixteen_entry_date' ) ) :
     59    /**
     60     * Prints HTML with date information for current post.
     61     *
     62     * Create your own twentysixteen_entry_date() function to override in a child theme.
     63     *
     64     * @since Twenty Sixteen 1.0
     65     */
     66    function twentysixteen_entry_date() {
     67        $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
     68
     69        if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
     70            $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
     71        }
     72
     73        $time_string = sprintf(
     74            $time_string,
     75            esc_attr( get_the_date( 'c' ) ),
     76            get_the_date(),
     77            esc_attr( get_the_modified_date( 'c' ) ),
     78            get_the_modified_date()
    2879        );
    29     }
    30 
    31     if ( in_array( get_post_type(), array( 'post', 'attachment' ) ) ) {
    32         twentysixteen_entry_date();
    33     }
    34 
    35     $format = get_post_format();
    36     if ( current_theme_supports( 'post-formats', $format ) ) {
    37         printf( '<span class="entry-format">%1$s<a href="%2$s">%3$s</a></span>',
    38             sprintf( '<span class="screen-reader-text">%s </span>', _x( 'Format', 'Used before post format.', 'twentysixteen' ) ),
    39             esc_url( get_post_format_link( $format ) ),
    40             get_post_format_string( $format )
     80
     81        printf(
     82            '<span class="posted-on"><span class="screen-reader-text">%1$s </span><a href="%2$s" rel="bookmark">%3$s</a></span>',
     83            _x( 'Posted on', 'Used before publish date.', 'twentysixteen' ),
     84            esc_url( get_permalink() ),
     85            $time_string
    4186        );
    4287    }
    43 
    44     if ( 'post' === get_post_type() ) {
    45         twentysixteen_entry_taxonomies();
    46     }
    47 
    48     if ( ! is_singular() && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
    49         echo '<span class="comments-link">';
    50         comments_popup_link( sprintf( __( 'Leave a comment<span class="screen-reader-text"> on %s</span>', 'twentysixteen' ), get_the_title() ) );
    51         echo '</span>';
    52     }
    53 }
    54 endif;
    55 
    56 if ( ! function_exists( 'twentysixteen_entry_date' ) ) :
    57 /**
    58  * Prints HTML with date information for current post.
    59  *
    60  * Create your own twentysixteen_entry_date() function to override in a child theme.
    61  *
    62  * @since Twenty Sixteen 1.0
    63  */
    64 function twentysixteen_entry_date() {
    65     $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
    66 
    67     if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    68         $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
    69     }
    70 
    71     $time_string = sprintf( $time_string,
    72         esc_attr( get_the_date( 'c' ) ),
    73         get_the_date(),
    74         esc_attr( get_the_modified_date( 'c' ) ),
    75         get_the_modified_date()
    76     );
    77 
    78     printf( '<span class="posted-on"><span class="screen-reader-text">%1$s </span><a href="%2$s" rel="bookmark">%3$s</a></span>',
    79         _x( 'Posted on', 'Used before publish date.', 'twentysixteen' ),
    80         esc_url( get_permalink() ),
    81         $time_string
    82     );
    83 }
    8488endif;
    8589
    8690if ( ! function_exists( 'twentysixteen_entry_taxonomies' ) ) :
    87 /**
    88  * Prints HTML with category and tags for current post.
    89  *
    90  * Create your own twentysixteen_entry_taxonomies() function to override in a child theme.
    91  *
    92  * @since Twenty Sixteen 1.0
    93  */
    94 function twentysixteen_entry_taxonomies() {
    95     $categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentysixteen' ) );
    96     if ( $categories_list && twentysixteen_categorized_blog() ) {
    97         printf( '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    98             _x( 'Categories', 'Used before category names.', 'twentysixteen' ),
    99             $categories_list
    100         );
    101     }
    102 
    103     $tags_list = get_the_tag_list( '', _x( ', ', 'Used between list items, there is a space after the comma.', 'twentysixteen' ) );
    104     if ( $tags_list && ! is_wp_error( $tags_list ) ) {
    105         printf( '<span class="tags-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
    106             _x( 'Tags', 'Used before tag names.', 'twentysixteen' ),
    107             $tags_list
    108         );
    109     }
    110 }
     91    /**
     92     * Prints HTML with category and tags for current post.
     93     *
     94     * Create your own twentysixteen_entry_taxonomies() function to override in a child theme.
     95     *
     96     * @since Twenty Sixteen 1.0
     97     */
     98    function twentysixteen_entry_taxonomies() {
     99        $categories_list = get_the_category_list( _x( ', ', 'Used between list items, there is a space after the comma.', 'twentysixteen' ) );
     100        if ( $categories_list && twentysixteen_categorized_blog() ) {
     101            printf(
     102                '<span class="cat-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
     103                _x( 'Categories', 'Used before category names.', 'twentysixteen' ),
     104                $categories_list
     105            );
     106        }
     107
     108        $tags_list = get_the_tag_list( '', _x( ', ', 'Used between list items, there is a space after the comma.', 'twentysixteen' ) );
     109        if ( $tags_list && ! is_wp_error( $tags_list ) ) {
     110            printf(
     111                '<span class="tags-links"><span class="screen-reader-text">%1$s </span>%2$s</span>',
     112                _x( 'Tags', 'Used before tag names.', 'twentysixteen' ),
     113                $tags_list
     114            );
     115        }
     116    }
    111117endif;
    112118
    113119if ( ! function_exists( 'twentysixteen_post_thumbnail' ) ) :
    114 /**
    115  * Displays an optional post thumbnail.
    116  *
    117  * Wraps the post thumbnail in an anchor element on index views, or a div
    118  * element when on single views.
    119  *
    120  * Create your own twentysixteen_post_thumbnail() function to override in a child theme.
    121  *
    122  * @since Twenty Sixteen 1.0
    123  */
    124 function twentysixteen_post_thumbnail() {
    125     if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
    126         return;
    127     }
    128 
    129     if ( is_singular() ) :
    130     ?>
    131 
    132     <div class="post-thumbnail">
     120    /**
     121    * Displays an optional post thumbnail.
     122    *
     123    * Wraps the post thumbnail in an anchor element on index views, or a div
     124    * element when on single views.
     125    *
     126    * Create your own twentysixteen_post_thumbnail() function to override in a child theme.
     127    *
     128    * @since Twenty Sixteen 1.0
     129    */
     130    function twentysixteen_post_thumbnail() {
     131        if ( post_password_required() || is_attachment() || ! has_post_thumbnail() ) {
     132            return;
     133        }
     134
     135        if ( is_singular() ) :
     136        ?>
     137
     138        <div class="post-thumbnail">
    133139        <?php the_post_thumbnail(); ?>
    134140    </div><!-- .post-thumbnail -->
     
    140146    </a>
    141147
    142     <?php endif; // End is_singular()
    143 }
     148    <?php
     149    endif; // End is_singular()
     150    }
    144151endif;
    145152
     
    159166        $class = esc_attr( $class );
    160167
    161         if ( has_excerpt() || is_search() ) : ?>
     168        if ( has_excerpt() || is_search() ) :
     169        ?>
    162170            <div class="<?php echo $class; ?>">
    163171                <?php the_excerpt(); ?>
    164172            </div><!-- .<?php echo $class; ?> -->
    165         <?php endif;
     173        <?php
     174        endif;
    166175    }
    167176endif;
    168177
    169178if ( ! function_exists( 'twentysixteen_excerpt_more' ) && ! is_admin() ) :
    170 /**
    171  * Replaces "[...]" (appended to automatically generated excerpts) with ... and
    172  * a 'Continue reading' link.
    173  *
    174  * Create your own twentysixteen_excerpt_more() function to override in a child theme.
    175  *
    176  * @since Twenty Sixteen 1.0
    177  *
    178  * @return string 'Continue reading' link prepended with an ellipsis.
    179  */
    180 function twentysixteen_excerpt_more() {
    181     $link = sprintf( '<a href="%1$s" class="more-link">%2$s</a>',
    182         esc_url( get_permalink( get_the_ID() ) ),
    183         /* translators: %s: Name of current post */
    184         sprintf( __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentysixteen' ), get_the_title( get_the_ID() ) )
    185     );
    186     return ' &hellip; ' . $link;
    187 }
    188 add_filter( 'excerpt_more', 'twentysixteen_excerpt_more' );
     179    /**
     180     * Replaces "[...]" (appended to automatically generated excerpts) with ... and
     181     * a 'Continue reading' link.
     182     *
     183     * Create your own twentysixteen_excerpt_more() function to override in a child theme.
     184     *
     185     * @since Twenty Sixteen 1.0
     186     *
     187     * @return string 'Continue reading' link prepended with an ellipsis.
     188     */
     189    function twentysixteen_excerpt_more() {
     190        $link = sprintf(
     191            '<a href="%1$s" class="more-link">%2$s</a>',
     192            esc_url( get_permalink( get_the_ID() ) ),
     193            /* translators: %s: Name of current post */
     194            sprintf( __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentysixteen' ), get_the_title( get_the_ID() ) )
     195        );
     196        return ' &hellip; ' . $link;
     197    }
     198    add_filter( 'excerpt_more', 'twentysixteen_excerpt_more' );
    189199endif;
    190200
    191201if ( ! function_exists( 'twentysixteen_categorized_blog' ) ) :
    192 /**
    193  * Determines whether blog/site has more than one category.
    194  *
    195  * Create your own twentysixteen_categorized_blog() function to override in a child theme.
    196  *
    197  * @since Twenty Sixteen 1.0
    198  *
    199  * @return bool True if there is more than one category, false otherwise.
    200  */
    201 function twentysixteen_categorized_blog() {
    202     if ( false === ( $all_the_cool_cats = get_transient( 'twentysixteen_categories' ) ) ) {
    203         // Create an array of all the categories that are attached to posts.
    204         $all_the_cool_cats = get_categories( array(
    205             'fields'     => 'ids',
    206             // We only need to know if there is more than one category.
    207             'number'     => 2,
    208         ) );
    209 
    210         // Count the number of categories that are attached to the posts.
    211         $all_the_cool_cats = count( $all_the_cool_cats );
    212 
    213         set_transient( 'twentysixteen_categories', $all_the_cool_cats );
    214     }
    215 
    216     if ( $all_the_cool_cats > 1 || is_preview() ) {
    217         // This blog has more than 1 category so twentysixteen_categorized_blog should return true.
    218         return true;
    219     } else {
    220         // This blog has only 1 category so twentysixteen_categorized_blog should return false.
    221         return false;
    222     }
    223 }
     202    /**
     203     * Determines whether blog/site has more than one category.
     204     *
     205     * Create your own twentysixteen_categorized_blog() function to override in a child theme.
     206     *
     207     * @since Twenty Sixteen 1.0
     208     *
     209     * @return bool True if there is more than one category, false otherwise.
     210     */
     211    function twentysixteen_categorized_blog() {
     212        if ( false === ( $all_the_cool_cats = get_transient( 'twentysixteen_categories' ) ) ) {
     213            // Create an array of all the categories that are attached to posts.
     214            $all_the_cool_cats = get_categories(
     215                array(
     216                    'fields' => 'ids',
     217                    // We only need to know if there is more than one category.
     218                    'number' => 2,
     219                )
     220            );
     221
     222            // Count the number of categories that are attached to the posts.
     223            $all_the_cool_cats = count( $all_the_cool_cats );
     224
     225            set_transient( 'twentysixteen_categories', $all_the_cool_cats );
     226        }
     227
     228        if ( $all_the_cool_cats > 1 || is_preview() ) {
     229            // This blog has more than 1 category so twentysixteen_categorized_blog should return true.
     230            return true;
     231        } else {
     232            // This blog has only 1 category so twentysixteen_categorized_blog should return false.
     233            return false;
     234        }
     235    }
    224236endif;
    225237
     
    237249}
    238250add_action( 'edit_category', 'twentysixteen_category_transient_flusher' );
    239 add_action( 'save_post',     'twentysixteen_category_transient_flusher' );
     251add_action( 'save_post', 'twentysixteen_category_transient_flusher' );
    240252
    241253if ( ! function_exists( 'twentysixteen_the_custom_logo' ) ) :
    242 /**
    243  * Displays the optional custom logo.
    244  *
    245  * Does nothing if the custom logo is not available.
    246  *
    247  * @since Twenty Sixteen 1.2
    248  */
    249 function twentysixteen_the_custom_logo() {
    250     if ( function_exists( 'the_custom_logo' ) ) {
    251         the_custom_logo();
    252     }
    253 }
    254 endif;
     254    /**
     255    * Displays the optional custom logo.
     256    *
     257    * Does nothing if the custom logo is not available.
     258    *
     259    * @since Twenty Sixteen 1.2
     260    */
     261    function twentysixteen_the_custom_logo() {
     262        if ( function_exists( 'the_custom_logo' ) ) {
     263            the_custom_logo();
     264        }
     265    }
     266endif;
Note: See TracChangeset for help on using the changeset viewer.