Make WordPress Core


Ignore:
Timestamp:
12/14/2018 02:32:33 AM (6 years ago)
Author:
pento
Message:

Default Themes: Import Twenty Nineteen from the 5.0 branch.

Merges [43808,43821,43842,43860,43892,43904,43909,43926-43929,43956,43961-43963] from the 5.0 branch to trunk.

Props allancole, karmatosed, kjellr, yingling017, mrasharirfan, milana_cap, fabiankaegy, westonruter, jorbin, netweb, b-07, khleomix, audrasjb, nielslange, mmaumio, richsalvucci, littlebigthing, dimadin, joyously, anevins, peterwilsoncc, dannycooper, iCaleb, siriokun, technosiren, travel_girl, azchughtai, ianbelanger, nadim1992, ismailelkorchi, nativeinside, chetan200891, grapplerulrich, ocean90, joshfeck, frankew, AbdulWahab610, mendezcode, eliorivero, melchoyce, joen, laurelfulford, mdawaffe, kraftbj, dsmart, nao, mayukojpn, enodekciw, ketuchetan, atanasangelovdev, poena, sharaz, artisticasad, mukesh27, burhandodhy, crunnells, aprakasa, themeroots, imonly_ik, tlxo, youthkee, brentswisher, smyoon315, mrahmadawais, desideveloper, Kau-Boy, mor10, mikeyarce, dingo_bastard, xkon, twoabove.

Fixes #45424.

Location:
trunk
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk

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

    r43808 r44149  
    55 * @package WordPress
    66 * @subpackage Twenty_Nineteen
     7 * @since 1.0.0
    78 */
    89
     
    3334
    3435/**
     36 * Adds custom class to the array of posts classes.
     37 */
     38function twentynineteen_post_classes( $classes, $class, $post_id ) {
     39    $classes[] = 'entry';
     40
     41    return $classes;
     42}
     43add_filter( 'post_class', 'twentynineteen_post_classes', 10, 3 );
     44
     45
     46/**
    3547 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
    3648 */
     
    6072function twentynineteen_get_the_archive_title() {
    6173    if ( is_category() ) {
    62         $title = esc_html__( 'Category Archives:', 'twentynineteen' );
     74        $title = __( 'Category Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
    6375    } elseif ( is_tag() ) {
    64         $title = esc_html__( 'Tag Archives:', 'twentynineteen' );
     76        $title = __( 'Tag Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
    6577    } elseif ( is_author() ) {
    66         $title = esc_html__( 'Author Archives:', 'twentynineteen' );
     78        $title = __( 'Author Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_author_meta( 'display_name' ) . '</span>';
    6779    } elseif ( is_year() ) {
    68         $title = esc_html__( 'Yearly Archives:', 'twentynineteen' );
     80        $title = __( 'Yearly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'Y', 'yearly archives date format', 'twentynineteen' ) ) . '</span>';
    6981    } elseif ( is_month() ) {
    70         $title = esc_html__( 'Monthly Archives:', 'twentynineteen' );
     82        $title = __( 'Monthly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'F Y', 'monthly archives date format', 'twentynineteen' ) ) . '</span>';
    7183    } elseif ( is_day() ) {
    72         $title = esc_html__( 'Daily Archives:', 'twentynineteen' );
     84        $title = __( 'Daily Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date() . '</span>';
    7385    } elseif ( is_post_type_archive() ) {
    74         $title = esc_html__( 'Post Type Archives:', 'twentynineteen' );
     86        $title = __( 'Post Type Archives: ', 'twentynineteen' ) . '<span class="page-description">' . post_type_archive_title( '', false ) . '</span>';
    7587    } elseif ( is_tax() ) {
    7688        $tax = get_taxonomy( get_queried_object()->taxonomy );
    77         /* translators: 1: Taxonomy singular name */
    78         $title = sprintf( __( '%s Archives: ' ), $tax->labels->singular_name );
     89        /* translators: %s: Taxonomy singular name */
     90        $title = sprintf( esc_html__( '%s Archives:', 'twentynineteen' ), $tax->labels->singular_name );
    7991    } else {
    80         $title = esc_html__( 'Archives:', 'twentynineteen' );
     92        $title = __( 'Archives:', 'twentynineteen' );
    8193    }
    8294    return $title;
     
    8597
    8698/**
    87  * Filters the default archive descriptions.
    88  */
    89 function twentynineteen_get_the_archive_description() {
    90     if ( is_category() || is_tag() || is_tax() ) {
    91         $description = single_term_title( '', false );
    92     } elseif ( is_author() ) {
    93         $description = get_the_author_meta( 'display_name' );
    94     } elseif ( is_post_type_archive() ) {
    95         $description = post_type_archive_title( '', false );
    96     } elseif ( is_year() ) {
    97         $description = get_the_date( _x( 'Y', 'yearly archives date format', 'twentynineteen' ) );
    98     } elseif ( is_month() ) {
    99         $description = get_the_date( _x( 'F Y', 'monthly archives date format', 'twentynineteen' ) );
    100     } elseif ( is_day() ) {
    101         $description = get_the_date();
    102     } else {
    103         $description = null;
    104     }
    105     return $description;
    106 }
    107 add_filter( 'get_the_archive_description', 'twentynineteen_get_the_archive_description' );
    108 
    109 /**
    11099 * Determines if post thumbnail can be displayed.
    111100 */
    112101function twentynineteen_can_show_post_thumbnail() {
    113     return ! post_password_required() && ! is_attachment() && has_post_thumbnail();
     102    return apply_filters( 'twentynineteen_can_show_post_thumbnail', ! post_password_required() && ! is_attachment() && has_post_thumbnail() );
    114103}
    115104
     
    118107 */
    119108function twentynineteen_image_filters_enabled() {
    120     return true;
    121 }
     109    if ( get_theme_mod( 'image_filter', 1 ) ) {
     110        return true;
     111    }
     112    return false;
     113}
     114
     115/**
     116 * Add custom sizes attribute to responsive image functionality for post thumbnails.
     117 *
     118 * @origin Twenty Nineteen 1.0
     119 *
     120 * @param array $attr  Attributes for the image markup.
     121 * @return string Value for use in post thumbnail 'sizes' attribute.
     122 */
     123function twentynineteen_post_thumbnail_sizes_attr( $attr ) {
     124
     125    if ( is_admin() ) {
     126        return $attr;
     127    }
     128
     129    if ( ! is_singular() ) {
     130        $attr['sizes'] = '(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw';
     131    }
     132
     133    return $attr;
     134}
     135add_filter( 'wp_get_attachment_image_attributes', 'twentynineteen_post_thumbnail_sizes_attr', 10, 1 );
    122136
    123137/**
     
    149163function twentynineteen_get_discussion_data() {
    150164    static $discussion, $post_id;
     165
    151166    $current_post_id = get_the_ID();
    152     if ( $current_post_id === $post_id ) { /* If we have discussion information for post ID, return cached object */
    153         return $discussion;
    154     }
    155     $authors    = array();
    156     $commenters = array();
    157     $user_id    = is_user_logged_in() ? get_current_user_id() : -1;
    158     $comments   = get_comments(
     167    if ( $current_post_id === $post_id ) {
     168        return $discussion; /* If we have discussion information for post ID, return cached object */
     169    } else {
     170        $post_id = $current_post_id;
     171    }
     172
     173    $comments = get_comments(
    159174        array(
    160175            'post_id' => $current_post_id,
     
    162177            'order'   => get_option( 'comment_order', 'asc' ), /* Respect comment order from Settings » Discussion. */
    163178            'status'  => 'approve',
     179            'number'  => 20, /* Only retrieve the last 20 comments, as the end goal is just 6 unique authors */
    164180        )
    165181    );
     182
     183    $authors = array();
    166184    foreach ( $comments as $comment ) {
    167         $comment_user_id = (int) $comment->user_id;
    168         if ( $comment_user_id !== $user_id ) {
    169             $authors[]    = ( $comment_user_id > 0 ) ? $comment_user_id : $comment->comment_author_email;
    170             $commenters[] = $comment->comment_author_email;
    171         }
    172     }
     185        $authors[] = ( (int) $comment->user_id > 0 ) ? (int) $comment->user_id : $comment->comment_author_email;
     186    }
     187
    173188    $authors    = array_unique( $authors );
    174     $responses  = count( $commenters );
    175     $commenters = array_unique( $commenters );
    176     $post_id    = $current_post_id;
    177189    $discussion = (object) array(
    178         'authors'    => array_slice( $authors, 0, 6 ), /* Unique authors commenting on post (a subset of), excluding current user. */
    179         'commenters' => count( $commenters ),          /* Number of commenters involved in discussion, excluding current user. */
    180         'responses'  => $responses,                    /* Number of responses, excluding responses from current user. */
     190        'authors'   => array_slice( $authors, 0, 6 ),           /* Six unique authors commenting on the post. */
     191        'responses' => get_comments_number( $current_post_id ), /* Number of responses. */
    181192    );
     193
    182194    return $discussion;
    183195}
     196
     197/**
     198 * Add an extra menu to our nav for our priority+ navigation to use
     199 *
     200 * @param object $nav_menu  Nav menu.
     201 * @param object $args      Nav menu args.
     202 * @return string More link for hidden menu items.
     203 */
     204function twentynineteen_add_ellipses_to_nav( $nav_menu, $args ) {
     205
     206    if ( 'menu-1' === $args->theme_location ) :
     207
     208        $nav_menu .= '<div class="main-menu-more">';
     209        $nav_menu .= '<ul class="main-menu" tabindex="0">';
     210        $nav_menu .= '<li class="menu-item menu-item-has-children">';
     211        $nav_menu .= '<a href="#" class="screen-reader-text" aria-label="More" aria-haspopup="true" aria-expanded="false">' . esc_html__( 'More', 'twentynineteen' ) . '</a>';
     212        $nav_menu .= '<span class="submenu-expand main-menu-more-toggle is-empty" tabindex="-1">';
     213        $nav_menu .= twentynineteen_get_icon_svg( 'arrow_drop_down_ellipsis' );
     214        $nav_menu .= '</span>';
     215        $nav_menu .= '<ul class="sub-menu hidden-links">';
     216        $nav_menu .= '<li id="menu-item--1" class="mobile-parent-nav-menu-item menu-item--1">';
     217        $nav_menu .= '<span class="menu-item-link-return">';
     218        $nav_menu .= twentynineteen_get_icon_svg( 'chevron_left' );
     219        $nav_menu .= esc_html__( 'Back', 'twentynineteen' );
     220        $nav_menu .= '</span>';
     221        $nav_menu .= '</li>';
     222        $nav_menu .= '</ul>';
     223        $nav_menu .= '</li>';
     224        $nav_menu .= '</ul>';
     225        $nav_menu .= '</div>';
     226
     227    endif;
     228
     229    return $nav_menu;
     230}
     231add_filter( 'wp_nav_menu', 'twentynineteen_add_ellipses_to_nav', 10, 2 );
    184232
    185233/**
     
    205253
    206254/**
     255 * Add a dropdown icon to top-level menu items.
     256 *
     257 * @param string $output Nav menu item start element.
     258 * @param object $item   Nav menu item.
     259 * @param int    $depth  Depth.
     260 * @param object $args   Nav menu args.
     261 * @return string Nav menu item start element.
    207262 * Add a dropdown icon to top-level menu items
    208263 */
     
    210265
    211266    // Only add class to 'top level' items on the 'primary' menu.
    212     if ( 'menu-1' == $args->theme_location && 0 === $depth ) {
    213 
    214         if ( in_array( 'menu-item-has-children', $item->classes ) ) {
    215             $output .= twentynineteen_get_icon_svg( 'arrow_drop_down_circle', 16 );
     267    if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
     268        return $output;
     269    }
     270
     271    if ( in_array( 'mobile-parent-nav-menu-item', $item->classes, true ) && isset( $item->original_id ) ) {
     272        // Inject the keyboard_arrow_left SVG inside the parent nav menu item, and let the item link to the parent item.
     273        // @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.
     274        $link = sprintf(
     275            '<span class="menu-item-link-return" tabindex="-1">%s',
     276            twentynineteen_get_icon_svg( 'chevron_left', 24 )
     277        );
     278
     279        // replace opening <a> with <span>
     280        $output = preg_replace(
     281            '/<a\s.*?>/',
     282            $link,
     283            $output,
     284            1 // Limit.
     285        );
     286
     287        // replace closing </a> with </span>
     288        $output = preg_replace(
     289            '#</a>#i',
     290            '</span>',
     291            $output,
     292            1 // Limit.
     293        );
     294
     295    } elseif ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
     296
     297        // Add SVG icon to parent items.
     298        $icon = twentynineteen_get_icon_svg( 'keyboard_arrow_down', 24 );
     299
     300        $output .= sprintf(
     301            '<span class="submenu-expand" tabindex="-1">%s</span>',
     302            $icon
     303        );
     304    }
     305
     306    return $output;
     307}
     308add_filter( 'walker_nav_menu_start_el', 'twentynineteen_add_dropdown_icons', 10, 4 );
     309
     310/**
     311 * Create a nav menu item to be displayed on mobile to navigate from submenu back to the parent.
     312 *
     313 * This duplicates each parent nav menu item and makes it the first child of itself.
     314 *
     315 * @param array  $sorted_menu_items Sorted nav menu items.
     316 * @param object $args              Nav menu args.
     317 * @return array Amended nav menu items.
     318 */
     319function twentynineteen_add_mobile_parent_nav_menu_items( $sorted_menu_items, $args ) {
     320    static $pseudo_id = 0;
     321    if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
     322        return $sorted_menu_items;
     323    }
     324
     325    $amended_menu_items = array();
     326    foreach ( $sorted_menu_items as $nav_menu_item ) {
     327        $amended_menu_items[] = $nav_menu_item;
     328        if ( in_array( 'menu-item-has-children', $nav_menu_item->classes, true ) ) {
     329            $parent_menu_item                   = clone $nav_menu_item;
     330            $parent_menu_item->original_id      = $nav_menu_item->ID;
     331            $parent_menu_item->ID               = --$pseudo_id;
     332            $parent_menu_item->db_id            = $parent_menu_item->ID;
     333            $parent_menu_item->object_id        = $parent_menu_item->ID;
     334            $parent_menu_item->classes          = array( 'mobile-parent-nav-menu-item' );
     335            $parent_menu_item->menu_item_parent = $nav_menu_item->ID;
     336
     337            $amended_menu_items[] = $parent_menu_item;
    216338        }
    217     } else if ( 'menu-1' == $args->theme_location && $depth >= 1 ) {
    218 
    219         if ( in_array( 'menu-item-has-children', $item->classes ) ) {
    220             $output .= twentynineteen_get_icon_svg( 'keyboard_arrow_right', 24 );
     339    }
     340
     341    return $amended_menu_items;
     342}
     343add_filter( 'wp_nav_menu_objects', 'twentynineteen_add_mobile_parent_nav_menu_items', 10, 2 );
     344
     345/**
     346 * Convert HSL to HEX colors
     347 */
     348function twentynineteen_hsl_hex( $h, $s, $l, $to_hex = true ) {
     349
     350    $h /= 360;
     351    $s /= 100;
     352    $l /= 100;
     353
     354    $r = $l;
     355    $g = $l;
     356    $b = $l;
     357    $v = ( $l <= 0.5 ) ? ( $l * ( 1.0 + $s ) ) : ( $l + $s - $l * $s );
     358    if ( $v > 0 ) {
     359        $m;
     360        $sv;
     361        $sextant;
     362        $fract;
     363        $vsf;
     364        $mid1;
     365        $mid2;
     366
     367        $m = $l + $l - $v;
     368        $sv = ( $v - $m ) / $v;
     369        $h *= 6.0;
     370        $sextant = floor( $h );
     371        $fract = $h - $sextant;
     372        $vsf = $v * $sv * $fract;
     373        $mid1 = $m + $vsf;
     374        $mid2 = $v - $vsf;
     375
     376        switch ( $sextant ) {
     377            case 0:
     378                $r = $v;
     379                $g = $mid1;
     380                $b = $m;
     381                break;
     382            case 1:
     383                $r = $mid2;
     384                $g = $v;
     385                $b = $m;
     386                break;
     387            case 2:
     388                $r = $m;
     389                $g = $v;
     390                $b = $mid1;
     391                break;
     392            case 3:
     393                $r = $m;
     394                $g = $mid2;
     395                $b = $v;
     396                break;
     397            case 4:
     398                $r = $mid1;
     399                $g = $m;
     400                $b = $v;
     401                break;
     402            case 5:
     403                $r = $v;
     404                $g = $m;
     405                $b = $mid2;
     406                break;
    221407        }
    222408    }
    223 
    224     return $output;
    225 }
    226 add_filter( 'walker_nav_menu_start_el', 'twentynineteen_add_dropdown_icons', 10, 4 );
     409    $r = round( $r * 255, 0 );
     410    $g = round( $g * 255, 0 );
     411    $b = round( $b * 255, 0 );
     412
     413    if ( $to_hex ) {
     414
     415        $r = ( $r < 15 ) ? '0' . dechex( $r ) : dechex( $r );
     416        $g = ( $g < 15 ) ? '0' . dechex( $g ) : dechex( $g );
     417        $b = ( $b < 15 ) ? '0' . dechex( $b ) : dechex( $b );
     418
     419        return "#$r$g$b";
     420
     421    } else {
     422
     423        return "rgb($r, $g, $b)";
     424    }
     425}
Note: See TracChangeset for help on using the changeset viewer.