- Timestamp:
- 10/30/2018 02:13:07 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/5.0/src/wp-content/themes/twentynineteen/inc/template-functions.php
r43808 r43842 5 5 * @package WordPress 6 6 * @subpackage Twenty_Nineteen 7 * @since 1.0.0 7 8 */ 8 9 … … 33 34 34 35 /** 36 * Adds custom class to the array of posts classes. 37 */ 38 function twentynineteen_post_classes( $classes, $class, $post_id ) { 39 $classes[] = 'entry'; 40 41 return $classes; 42 } 43 add_filter( 'post_class', 'twentynineteen_post_classes', 10, 3 ); 44 45 46 /** 35 47 * Add a pingback url auto-discovery header for single posts, pages, or attachments. 36 48 */ … … 60 72 function twentynineteen_get_the_archive_title() { 61 73 if ( is_category() ) { 62 $title = esc_html__( 'Category Archives: ', 'twentynineteen' );74 $title = esc_html__( 'Category Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>'; 63 75 } elseif ( is_tag() ) { 64 $title = esc_html__( 'Tag Archives: ', 'twentynineteen' );76 $title = esc_html__( 'Tag Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>'; 65 77 } elseif ( is_author() ) { 66 $title = esc_html__( 'Author Archives: ', 'twentynineteen' );78 $title = esc_html__( 'Author Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_author_meta( 'display_name' ) . '</span>'; 67 79 } elseif ( is_year() ) { 68 $title = esc_html__( 'Yearly Archives: ', 'twentynineteen' );80 $title = esc_html__( 'Yearly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'Y', 'yearly archives date format', 'twentynineteen' ) ) . '</span>'; 69 81 } elseif ( is_month() ) { 70 $title = esc_html__( 'Monthly Archives: ', 'twentynineteen' );82 $title = esc_html__( 'Monthly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'F Y', 'monthly archives date format', 'twentynineteen' ) ) . '</span>'; 71 83 } elseif ( is_day() ) { 72 $title = esc_html__( 'Daily Archives: ', 'twentynineteen' );84 $title = esc_html__( 'Daily Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date() . '</span>'; 73 85 } elseif ( is_post_type_archive() ) { 74 $title = esc_html__( 'Post Type Archives: ', 'twentynineteen' );86 $title = esc_html__( 'Post Type Archives: ', 'twentynineteen' ) . '<span class="page-description">' . post_type_archive_title( '', false ) . '</span>'; 75 87 } elseif ( is_tax() ) { 76 88 $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 ); 79 91 } else { 80 92 $title = esc_html__( 'Archives:', 'twentynineteen' ); … … 85 97 86 98 /** 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 /**110 99 * Determines if post thumbnail can be displayed. 111 100 */ 112 101 function 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() ); 114 103 } 115 104 … … 118 107 */ 119 108 function twentynineteen_image_filters_enabled() { 109 if ( 'inactive' === get_theme_mod( 'image_filter' ) ) { 110 return false; 111 } 120 112 return true; 121 113 } … … 155 147 $authors = array(); 156 148 $commenters = array(); 157 $user_id = is_user_logged_in() ? get_current_user_id() : -1;149 $user_id = -1; // is_user_logged_in() ? get_current_user_id() : -1; 158 150 $comments = get_comments( 159 151 array( … … 205 197 206 198 /** 199 * Add a dropdown icon to top-level menu items. 200 * 201 * @param string $output Nav menu item start element. 202 * @param object $item Nav menu item. 203 * @param int $depth Depth. 204 * @param object $args Nav menu args. 205 * @return string Nav menu item start element. 207 206 * Add a dropdown icon to top-level menu items 208 207 */ … … 210 209 211 210 // 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 ); 216 } 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 ); 221 } 211 if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) { 212 return $output; 213 } 214 215 if ( in_array( 'mobile-parent-nav-menu-item', $item->classes, true ) && isset( $item->original_id ) ) { 216 // Inject the keyboard_arrow_left SVG inside the parent nav menu item, and let the item link to the parent item. 217 // @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. 218 $link = sprintf( 219 '<a class="menu-item-link-return" id="%1$s" href="%2$s" onclick="%3$s" tabindex="-1">%4$s', 220 esc_attr( "menu-item-link-return-{$item->original_id}" ), 221 esc_attr( "#menu-item-link-{$item->original_id}" ), 222 esc_attr( 'event.preventDefault();' ), 223 twentynineteen_get_icon_svg( 'chevron_left', 24 ) 224 ); 225 226 $output = preg_replace( 227 '/<a\s.*?>/', 228 $link, 229 $output, 230 1 // Limit. 231 ); 232 } elseif ( in_array( 'menu-item-has-children', $item->classes, true ) ) { 233 // Add an ID to the link element itself to facilitate navigation from submenu back to parent. 234 $output = preg_replace( '/(?<=<a\s)/', sprintf( ' id="%s" ', esc_attr( "menu-item-link-{$item->ID}" ) ), $output ); 235 236 // Add SVG icon to parent items. 237 if ( 0 === $depth ) { 238 $icon = twentynineteen_get_icon_svg( 'keyboard_arrow_down', 24 ); 239 } else { 240 $icon = twentynineteen_get_icon_svg( 'chevron_right', 24 ); 241 } 242 243 // @todo We might as well just go back to using the SVG element if the link approach is not suitable for no-JS environments. 244 $link = sprintf( 245 '<a class="mobile-submenu-expand" href="%s" onclick="%s" tabindex="-1">%s</a>', 246 esc_attr( "#menu-item-link-return-{$item->ID}" ), 247 esc_attr( 'event.preventDefault();' ), 248 $icon 249 ); 250 251 $output .= $link; 252 $output .= "<span class='desktop-submenu-expand'>$icon</span>"; 222 253 } 223 254 … … 225 256 } 226 257 add_filter( 'walker_nav_menu_start_el', 'twentynineteen_add_dropdown_icons', 10, 4 ); 258 259 /** 260 * Create a nav menu item to be displayed on mobile to navigate from submenu back to the parent. 261 * 262 * This duplicates each parent nav menu item and makes it the first child of itself. 263 * 264 * @param array $sorted_menu_items Sorted nav menu items. 265 * @param object $args Nav menu args. 266 * @return array Amended nav menu items. 267 */ 268 function twentynineteen_add_mobile_parent_nav_menu_items( $sorted_menu_items, $args ) { 269 static $pseudo_id = 0; 270 if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) { 271 return $sorted_menu_items; 272 } 273 274 $amended_menu_items = array(); 275 foreach ( $sorted_menu_items as $nav_menu_item ) { 276 $amended_menu_items[] = $nav_menu_item; 277 if ( in_array( 'menu-item-has-children', $nav_menu_item->classes, true ) ) { 278 $parent_menu_item = clone $nav_menu_item; 279 $parent_menu_item->original_id = $nav_menu_item->ID; 280 $parent_menu_item->ID = --$pseudo_id; 281 $parent_menu_item->db_id = $parent_menu_item->ID; 282 $parent_menu_item->object_id = $parent_menu_item->ID; 283 $parent_menu_item->classes = array( 'mobile-parent-nav-menu-item' ); 284 $parent_menu_item->menu_item_parent = $nav_menu_item->ID; 285 286 $amended_menu_items[] = $parent_menu_item; 287 } 288 } 289 290 return $amended_menu_items; 291 } 292 add_filter( 'wp_nav_menu_objects', 'twentynineteen_add_mobile_parent_nav_menu_items', 10, 2 ); 293 294 /** 295 * Convert HSL to HEX colors 296 */ 297 function twentynineteen_hsl_hex( $h, $s, $l, $to_hex = true ) { 298 299 $h /= 360; 300 $s /= 100; 301 $l /= 100; 302 303 $r = $l; 304 $g = $l; 305 $b = $l; 306 $v = ( $l <= 0.5 ) ? ( $l * ( 1.0 + $s ) ) : ( $l + $s - $l * $s ); 307 if ( $v > 0 ) { 308 $m; 309 $sv; 310 $sextant; 311 $fract; 312 $vsf; 313 $mid1; 314 $mid2; 315 316 $m = $l + $l - $v; 317 $sv = ( $v - $m ) / $v; 318 $h *= 6.0; 319 $sextant = floor( $h ); 320 $fract = $h - $sextant; 321 $vsf = $v * $sv * $fract; 322 $mid1 = $m + $vsf; 323 $mid2 = $v - $vsf; 324 325 switch ( $sextant ) { 326 case 0: 327 $r = $v; 328 $g = $mid1; 329 $b = $m; 330 break; 331 case 1: 332 $r = $mid2; 333 $g = $v; 334 $b = $m; 335 break; 336 case 2: 337 $r = $m; 338 $g = $v; 339 $b = $mid1; 340 break; 341 case 3: 342 $r = $m; 343 $g = $mid2; 344 $b = $v; 345 break; 346 case 4: 347 $r = $mid1; 348 $g = $m; 349 $b = $v; 350 break; 351 case 5: 352 $r = $v; 353 $g = $m; 354 $b = $mid2; 355 break; 356 } 357 } 358 $r = round( $r * 255, 0 ); 359 $g = round( $g * 255, 0 ); 360 $b = round( $b * 255, 0 ); 361 362 if ( $to_hex ) { 363 364 $r = ( $r < 15 ) ? '0' . dechex( $r ) : dechex( $r ); 365 $g = ( $g < 15 ) ? '0' . dechex( $g ) : dechex( $g ); 366 $b = ( $b < 15 ) ? '0' . dechex( $b ) : dechex( $b ); 367 368 return "#$r$g$b"; 369 370 } else { 371 372 return "rgb($r, $g, $b)"; 373 } 374 }
Note: See TracChangeset
for help on using the changeset viewer.