[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-content/themes/twentynineteen/inc/ -> template-functions.php (source)

   1  <?php
   2  /**
   3   * Functions which enhance the theme by hooking into WordPress
   4   *
   5   * @package WordPress
   6   * @subpackage Twenty_Nineteen
   7   * @since Twenty Nineteen 1.0
   8   */
   9  
  10  /**
  11   * Adds custom classes to the array of body classes.
  12   *
  13   * @param array $classes Classes for the body element.
  14   * @return array
  15   */
  16  function twentynineteen_body_classes( $classes ) {
  17  
  18      if ( is_singular() ) {
  19          // Adds `singular` to singular pages.
  20          $classes[] = 'singular';
  21      } else {
  22          // Adds `hfeed` to non singular pages.
  23          $classes[] = 'hfeed';
  24      }
  25  
  26      // Adds a class if image filters are enabled.
  27      if ( twentynineteen_image_filters_enabled() ) {
  28          $classes[] = 'image-filters-enabled';
  29      }
  30  
  31      return $classes;
  32  }
  33  add_filter( 'body_class', 'twentynineteen_body_classes' );
  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  /**
  47   * Add a pingback url auto-discovery header for single posts, pages, or attachments.
  48   */
  49  function twentynineteen_pingback_header() {
  50      if ( is_singular() && pings_open() ) {
  51          echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  52      }
  53  }
  54  add_action( 'wp_head', 'twentynineteen_pingback_header' );
  55  
  56  /**
  57   * Changes comment form default fields.
  58   */
  59  function twentynineteen_comment_form_defaults( $defaults ) {
  60      $comment_field = $defaults['comment_field'];
  61  
  62      // Adjust height of comment form.
  63      $defaults['comment_field'] = preg_replace( '/rows="\d+"/', 'rows="5"', $comment_field );
  64  
  65      return $defaults;
  66  }
  67  add_filter( 'comment_form_defaults', 'twentynineteen_comment_form_defaults' );
  68  
  69  /**
  70   * Filters the default archive titles.
  71   */
  72  function twentynineteen_get_the_archive_title() {
  73      if ( is_category() ) {
  74          $title = __( 'Category Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  75      } elseif ( is_tag() ) {
  76          $title = __( 'Tag Archives: ', 'twentynineteen' ) . '<span class="page-description">' . single_term_title( '', false ) . '</span>';
  77      } elseif ( is_author() ) {
  78          $title = __( 'Author Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_author_meta( 'display_name' ) . '</span>';
  79      } elseif ( is_year() ) {
  80          $title = __( 'Yearly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'Y', 'yearly archives date format', 'twentynineteen' ) ) . '</span>';
  81      } elseif ( is_month() ) {
  82          $title = __( 'Monthly Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date( _x( 'F Y', 'monthly archives date format', 'twentynineteen' ) ) . '</span>';
  83      } elseif ( is_day() ) {
  84          $title = __( 'Daily Archives: ', 'twentynineteen' ) . '<span class="page-description">' . get_the_date() . '</span>';
  85      } elseif ( is_post_type_archive() ) {
  86          $title = __( 'Post Type Archives: ', 'twentynineteen' ) . '<span class="page-description">' . post_type_archive_title( '', false ) . '</span>';
  87      } elseif ( is_tax() ) {
  88          $tax = get_taxonomy( get_queried_object()->taxonomy );
  89          /* translators: %s: Taxonomy singular name. */
  90          $title = sprintf( esc_html__( '%s Archives:', 'twentynineteen' ), $tax->labels->singular_name );
  91      } else {
  92          $title = __( 'Archives:', 'twentynineteen' );
  93      }
  94      return $title;
  95  }
  96  add_filter( 'get_the_archive_title', 'twentynineteen_get_the_archive_title' );
  97  
  98  /**
  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  /**
 113   * Add custom sizes attribute to responsive image functionality for post thumbnails.
 114   *
 115   * @origin Twenty Nineteen 1.0
 116   *
 117   * @param array $attr  Attributes for the image markup.
 118   * @return string Value for use in post thumbnail 'sizes' attribute.
 119   */
 120  function twentynineteen_post_thumbnail_sizes_attr( $attr ) {
 121  
 122      if ( is_admin() ) {
 123          return $attr;
 124      }
 125  
 126      if ( ! is_singular() ) {
 127          $attr['sizes'] = '(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw';
 128      }
 129  
 130      return $attr;
 131  }
 132  add_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  }
 193  
 194  /**
 195   * Add an extra menu to our nav for our priority+ navigation to use
 196   *
 197   * @param object $nav_menu  Nav menu.
 198   * @param object $args      Nav menu args.
 199   * @return string More link for hidden menu items.
 200   */
 201  function twentynineteen_add_ellipses_to_nav( $nav_menu, $args ) {
 202  
 203      if ( 'menu-1' === $args->theme_location ) :
 204  
 205          $nav_menu .= '
 206              <div class="main-menu-more">
 207                  <ul class="main-menu">
 208                      <li class="menu-item menu-item-has-children">
 209                          <button class="submenu-expand main-menu-more-toggle is-empty" tabindex="-1"
 210                              aria-label="' . esc_attr__( 'More', 'twentynineteen' ) . '" aria-haspopup="true" aria-expanded="false">' .
 211                              twentynineteen_get_icon_svg( 'arrow_drop_down_ellipsis' ) . '
 212                          </button>
 213                          <ul class="sub-menu hidden-links">
 214                              <li class="mobile-parent-nav-menu-item">
 215                                  <button class="menu-item-link-return">' .
 216                                      twentynineteen_get_icon_svg( 'chevron_left' ) .
 217                                      esc_html__( 'Back', 'twentynineteen' ) . '
 218                                  </button>
 219                              </li>
 220                          </ul>
 221                      </li>
 222                  </ul>
 223              </div>';
 224  
 225      endif;
 226  
 227      return $nav_menu;
 228  }
 229  add_filter( 'wp_nav_menu', 'twentynineteen_add_ellipses_to_nav', 10, 2 );
 230  
 231  /**
 232   * WCAG 2.0 Attributes for Dropdown Menus
 233   *
 234   * Adjustments to menu attributes tot support WCAG 2.0 recommendations
 235   * for flyout and dropdown menus.
 236   *
 237   * @ref https://www.w3.org/WAI/tutorials/menus/flyout/
 238   */
 239  function twentynineteen_nav_menu_link_attributes( $atts, $item, $args, $depth ) {
 240  
 241      // Add [aria-haspopup] and [aria-expanded] to menu items that have children
 242      $item_has_children = in_array( 'menu-item-has-children', $item->classes );
 243      if ( $item_has_children ) {
 244          $atts['aria-haspopup'] = 'true';
 245          $atts['aria-expanded'] = 'false';
 246      }
 247  
 248      return $atts;
 249  }
 250  add_filter( 'nav_menu_link_attributes', 'twentynineteen_nav_menu_link_attributes', 10, 4 );
 251  
 252  /**
 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  /**
 309   * Create a nav menu item to be displayed on mobile to navigate from submenu back to the parent.
 310   *
 311   * This duplicates each parent nav menu item and makes it the first child of itself.
 312   *
 313   * @param array  $sorted_menu_items Sorted nav menu items.
 314   * @param object $args              Nav menu args.
 315   * @return array Amended nav menu items.
 316   */
 317  function twentynineteen_add_mobile_parent_nav_menu_items( $sorted_menu_items, $args ) {
 318      static $pseudo_id = 0;
 319      if ( ! isset( $args->theme_location ) || 'menu-1' !== $args->theme_location ) {
 320          return $sorted_menu_items;
 321      }
 322  
 323      $amended_menu_items = array();
 324      foreach ( $sorted_menu_items as $nav_menu_item ) {
 325          $amended_menu_items[] = $nav_menu_item;
 326          if ( in_array( 'menu-item-has-children', $nav_menu_item->classes, true ) ) {
 327              $parent_menu_item                   = clone $nav_menu_item;
 328              $parent_menu_item->original_id      = $nav_menu_item->ID;
 329              $parent_menu_item->ID               = --$pseudo_id;
 330              $parent_menu_item->db_id            = $parent_menu_item->ID;
 331              $parent_menu_item->object_id        = $parent_menu_item->ID;
 332              $parent_menu_item->classes          = array( 'mobile-parent-nav-menu-item' );
 333              $parent_menu_item->menu_item_parent = $nav_menu_item->ID;
 334  
 335              $amended_menu_items[] = $parent_menu_item;
 336          }
 337      }
 338  
 339      return $amended_menu_items;
 340  }
 341  add_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  }


Generated: Tue Dec 10 01:00:04 2019 Cross-referenced by PHPXref 0.7.1