Make WordPress Core


Ignore:
Timestamp:
02/08/2020 12:59:44 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Twenty Nineteen: Improve code organization in template-functions.php by moving helper functions into their own file.

These functions are moved to inc/helper-functions.php:

  • twentynineteen_can_show_post_thumbnail()
  • twentynineteen_image_filters_enabled()
  • twentynineteen_get_avatar_size()
  • twentynineteen_is_comment_by_post_author()
  • twentynineteen_get_discussion_data()
  • twentynineteen_hsl_hex()

Additionally, twentynineteen_add_dropdown_icons() is moved to inc/icon-functions.php to join twentynineteen_nav_menu_social_icons().

Props akshayar, allancole, grapplerulrich.
Fixes #45984.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentynineteen/inc/template-functions.php

    r47122 r47214  
    9797
    9898/**
    99  * Determines if post thumbnail can be displayed.
    100  */
    101 function twentynineteen_can_show_post_thumbnail() {
    102     return apply_filters( 'twentynineteen_can_show_post_thumbnail', ! post_password_required() && ! is_attachment() && has_post_thumbnail() );
    103 }
    104 
    105 /**
    106  * Returns true if image filters are enabled on the theme options.
    107  */
    108 function twentynineteen_image_filters_enabled() {
    109     return 0 !== get_theme_mod( 'image_filter', 1 );
    110 }
    111 
    112 /**
    11399 * Add custom sizes attribute to responsive image functionality for post thumbnails.
    114100 *
     
    131117}
    132118add_filter( 'wp_get_attachment_image_attributes', 'twentynineteen_post_thumbnail_sizes_attr', 10, 1 );
    133 
    134 /**
    135  * Returns the size for avatars used in the theme.
    136  */
    137 function twentynineteen_get_avatar_size() {
    138     return 60;
    139 }
    140 
    141 /**
    142  * Returns true if comment is by author of the post.
    143  *
    144  * @see get_comment_class()
    145  */
    146 function twentynineteen_is_comment_by_post_author( $comment = null ) {
    147     if ( is_object( $comment ) && $comment->user_id > 0 ) {
    148         $user = get_userdata( $comment->user_id );
    149         $post = get_post( $comment->comment_post_ID );
    150         if ( ! empty( $user ) && ! empty( $post ) ) {
    151             return $comment->user_id === $post->post_author;
    152         }
    153     }
    154     return false;
    155 }
    156 
    157 /**
    158  * Returns information about the current post's discussion, with cache support.
    159  */
    160 function twentynineteen_get_discussion_data() {
    161     static $discussion, $post_id;
    162 
    163     $current_post_id = get_the_ID();
    164     if ( $current_post_id === $post_id ) {
    165         return $discussion; /* If we have discussion information for post ID, return cached object */
    166     } else {
    167         $post_id = $current_post_id;
    168     }
    169 
    170     $comments = get_comments(
    171         array(
    172             'post_id' => $current_post_id,
    173             'orderby' => 'comment_date_gmt',
    174             'order'   => get_option( 'comment_order', 'asc' ), /* Respect comment order from Settings » Discussion. */
    175             'status'  => 'approve',
    176             'number'  => 20, /* Only retrieve the last 20 comments, as the end goal is just 6 unique authors */
    177         )
    178     );
    179 
    180     $authors = array();
    181     foreach ( $comments as $comment ) {
    182         $authors[] = ( (int) $comment->user_id > 0 ) ? (int) $comment->user_id : $comment->comment_author_email;
    183     }
    184 
    185     $authors    = array_unique( $authors );
    186     $discussion = (object) array(
    187         'authors'   => array_slice( $authors, 0, 6 ),           /* Six unique authors commenting on the post. */
    188         'responses' => get_comments_number( $current_post_id ), /* Number of responses. */
    189     );
    190 
    191     return $discussion;
    192 }
    193119
    194120/**
     
    251177
    252178/**
    253  * Add a dropdown icon to top-level menu items.
    254  *
    255  * @param string $output Nav menu item start element.
    256  * @param object $item   Nav menu item.
    257  * @param int    $depth  Depth.
    258  * @param object $args   Nav menu args.
    259  * @return string Nav menu item start element.
    260  * Add a dropdown icon to top-level menu items
    261  */
    262 function twentynineteen_add_dropdown_icons( $output, $item, $depth, $args ) {
    263 
    264     // Only add class to 'top level' items on the 'primary' menu.
    265     if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
    266         return $output;
    267     }
    268 
    269     if ( in_array( 'mobile-parent-nav-menu-item', $item->classes, true ) && isset( $item->original_id ) ) {
    270         // Inject the keyboard_arrow_left SVG inside the parent nav menu item, and let the item link to the parent item.
    271         // @todo Only do this for nested submenus? If on a first-level submenu, then really the link could be "#" since the desire is to remove the target entirely.
    272         $link = sprintf(
    273             '<button class="menu-item-link-return" tabindex="-1">%s',
    274             twentynineteen_get_icon_svg( 'chevron_left', 24 )
    275         );
    276 
    277         // Replace opening <a> with <button>.
    278         $output = preg_replace(
    279             '/<a\s.*?>/',
    280             $link,
    281             $output,
    282             1 // Limit.
    283         );
    284 
    285         // Replace closing </a> with </button>.
    286         $output = preg_replace(
    287             '#</a>#i',
    288             '</button>',
    289             $output,
    290             1 // Limit.
    291         );
    292 
    293     } elseif ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
    294 
    295         // Add SVG icon to parent items.
    296         $icon = twentynineteen_get_icon_svg( 'keyboard_arrow_down', 24 );
    297 
    298         $output .= sprintf(
    299             '<button class="submenu-expand" tabindex="-1">%s</button>',
    300             $icon
    301         );
    302     }
    303 
    304     return $output;
    305 }
    306 add_filter( 'walker_nav_menu_start_el', 'twentynineteen_add_dropdown_icons', 10, 4 );
    307 
    308 /**
    309179 * Create a nav menu item to be displayed on mobile to navigate from submenu back to the parent.
    310180 *
     
    340210}
    341211add_filter( 'wp_nav_menu_objects', 'twentynineteen_add_mobile_parent_nav_menu_items', 10, 2 );
    342 
    343 /**
    344  * Convert HSL to HEX colors
    345  */
    346 function twentynineteen_hsl_hex( $h, $s, $l, $to_hex = true ) {
    347 
    348     $h /= 360;
    349     $s /= 100;
    350     $l /= 100;
    351 
    352     $r = $l;
    353     $g = $l;
    354     $b = $l;
    355     $v = ( $l <= 0.5 ) ? ( $l * ( 1.0 + $s ) ) : ( $l + $s - $l * $s );
    356     if ( $v > 0 ) {
    357         $m;
    358         $sv;
    359         $sextant;
    360         $fract;
    361         $vsf;
    362         $mid1;
    363         $mid2;
    364 
    365         $m       = $l + $l - $v;
    366         $sv      = ( $v - $m ) / $v;
    367         $h      *= 6.0;
    368         $sextant = floor( $h );
    369         $fract   = $h - $sextant;
    370         $vsf     = $v * $sv * $fract;
    371         $mid1    = $m + $vsf;
    372         $mid2    = $v - $vsf;
    373 
    374         switch ( $sextant ) {
    375             case 0:
    376                 $r = $v;
    377                 $g = $mid1;
    378                 $b = $m;
    379                 break;
    380             case 1:
    381                 $r = $mid2;
    382                 $g = $v;
    383                 $b = $m;
    384                 break;
    385             case 2:
    386                 $r = $m;
    387                 $g = $v;
    388                 $b = $mid1;
    389                 break;
    390             case 3:
    391                 $r = $m;
    392                 $g = $mid2;
    393                 $b = $v;
    394                 break;
    395             case 4:
    396                 $r = $mid1;
    397                 $g = $m;
    398                 $b = $v;
    399                 break;
    400             case 5:
    401                 $r = $v;
    402                 $g = $m;
    403                 $b = $mid2;
    404                 break;
    405         }
    406     }
    407     $r = round( $r * 255, 0 );
    408     $g = round( $g * 255, 0 );
    409     $b = round( $b * 255, 0 );
    410 
    411     if ( $to_hex ) {
    412 
    413         $r = ( $r < 15 ) ? '0' . dechex( $r ) : dechex( $r );
    414         $g = ( $g < 15 ) ? '0' . dechex( $g ) : dechex( $g );
    415         $b = ( $b < 15 ) ? '0' . dechex( $b ) : dechex( $b );
    416 
    417         return "#$r$g$b";
    418 
    419     }
    420 
    421     return "rgb($r, $g, $b)";
    422 }
Note: See TracChangeset for help on using the changeset viewer.