[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/themes/twentytwenty/inc/ -> template-tags.php (source)

   1  <?php
   2  /**
   3   * Custom template tags for this theme.
   4   *
   5   * @package WordPress
   6   * @subpackage Twenty_Twenty
   7   * @since Twenty Twenty 1.0
   8   */
   9  
  10  /**
  11   * Table of Contents:
  12   * Logo & Description
  13   * Comments
  14   * Post Meta
  15   * Menus
  16   * Classes
  17   * Archives
  18   * Miscellaneous
  19   */
  20  
  21  /**
  22   * Logo & Description
  23   */
  24  
  25  /**
  26   * Displays the site logo, either text or image.
  27   *
  28   * @since Twenty Twenty 1.0
  29   *
  30   * @param array $args    Arguments for displaying the site logo either as an image or text.
  31   * @param bool  $display Display or return the HTML.
  32   * @return string Compiled HTML based on our arguments.
  33   */
  34  function twentytwenty_site_logo( $args = array(), $display = true ) {
  35      $logo       = get_custom_logo();
  36      $site_title = get_bloginfo( 'name' );
  37      $contents   = '';
  38      $classname  = '';
  39  
  40      $defaults = array(
  41          'logo'        => '%1$s<span class="screen-reader-text">%2$s</span>',
  42          'logo_class'  => 'site-logo',
  43          'title'       => '<a href="%1$s">%2$s</a>',
  44          'title_class' => 'site-title',
  45          'home_wrap'   => '<h1 class="%1$s">%2$s</h1>',
  46          'single_wrap' => '<div class="%1$s faux-heading">%2$s</div>',
  47          'condition'   => ( is_front_page() || is_home() ) && ! is_page(),
  48      );
  49  
  50      $args = wp_parse_args( $args, $defaults );
  51  
  52      /**
  53       * Filters the arguments for `twentytwenty_site_logo()`.
  54       *
  55       * @since Twenty Twenty 1.0
  56       *
  57       * @param array $args     Parsed arguments.
  58       * @param array $defaults Function's default arguments.
  59       */
  60      $args = apply_filters( 'twentytwenty_site_logo_args', $args, $defaults );
  61  
  62      if ( has_custom_logo() ) {
  63          $contents  = sprintf( $args['logo'], $logo, esc_html( $site_title ) );
  64          $classname = $args['logo_class'];
  65      } else {
  66          $contents  = sprintf( $args['title'], esc_url( get_home_url( null, '/' ) ), esc_html( $site_title ) );
  67          $classname = $args['title_class'];
  68      }
  69  
  70      $wrap = $args['condition'] ? 'home_wrap' : 'single_wrap';
  71  
  72      $html = sprintf( $args[ $wrap ], $classname, $contents );
  73  
  74      /**
  75       * Filters the arguments for `twentytwenty_site_logo()`.
  76       *
  77       * @since Twenty Twenty 1.0
  78       *
  79       * @param string $html      Compiled HTML based on our arguments.
  80       * @param array  $args      Parsed arguments.
  81       * @param string $classname Class name based on current view, home or single.
  82       * @param string $contents  HTML for site title or logo.
  83       */
  84      $html = apply_filters( 'twentytwenty_site_logo', $html, $args, $classname, $contents );
  85  
  86      if ( ! $display ) {
  87          return $html;
  88      }
  89  
  90      echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  91  }
  92  
  93  /**
  94   * Displays the site description.
  95   *
  96   * @since Twenty Twenty 1.0
  97   *
  98   * @param bool $display Display or return the HTML.
  99   * @return string The HTML to display.
 100   */
 101  function twentytwenty_site_description( $display = true ) {
 102      $description = get_bloginfo( 'description' );
 103  
 104      if ( ! $description ) {
 105          return;
 106      }
 107  
 108      $wrapper = '<div class="site-description">%s</div><!-- .site-description -->';
 109  
 110      $html = sprintf( $wrapper, esc_html( $description ) );
 111  
 112      /**
 113       * Filters the HTML for the site description.
 114       *
 115       * @since Twenty Twenty 1.0
 116       *
 117       * @param string $html        The HTML to display.
 118       * @param string $description Site description via `bloginfo()`.
 119       * @param string $wrapper     The format used in case you want to reuse it in a `sprintf()`.
 120       */
 121      $html = apply_filters( 'twentytwenty_site_description', $html, $description, $wrapper );
 122  
 123      if ( ! $display ) {
 124          return $html;
 125      }
 126  
 127      echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
 128  }
 129  
 130  /**
 131   * Comments
 132   */
 133  
 134  /**
 135   * Checks if the specified comment is written by the author of the post commented on.
 136   *
 137   * @since Twenty Twenty 1.0
 138   *
 139   * @param object $comment Comment data.
 140   * @return bool
 141   */
 142  function twentytwenty_is_comment_by_post_author( $comment = null ) {
 143  
 144      if ( is_object( $comment ) && $comment->user_id > 0 ) {
 145  
 146          $user = get_userdata( $comment->user_id );
 147          $post = get_post( $comment->comment_post_ID );
 148  
 149          if ( ! empty( $user ) && ! empty( $post ) ) {
 150  
 151              return $comment->user_id === $post->post_author;
 152  
 153          }
 154      }
 155      return false;
 156  }
 157  
 158  /**
 159   * Filters comment reply link to not JS scroll.
 160   *
 161   * Filter the comment reply link to add a class indicating it should not use JS slow-scroll, as it
 162   * makes it scroll to the wrong position on the page.
 163   *
 164   * @since Twenty Twenty 1.0
 165   *
 166   * @param string $link Link to the top of the page.
 167   * @return string Link to the top of the page.
 168   */
 169  function twentytwenty_filter_comment_reply_link( $link ) {
 170  
 171      $link = str_replace( 'class=\'', 'class=\'do-not-scroll ', $link );
 172      return $link;
 173  }
 174  
 175  add_filter( 'comment_reply_link', 'twentytwenty_filter_comment_reply_link' );
 176  
 177  /**
 178   * Post Meta
 179   */
 180  
 181  /**
 182   * Retrieves and displays the post meta.
 183   *
 184   * If it's a single post, outputs the post meta values specified in the Customizer settings.
 185   *
 186   * @since Twenty Twenty 1.0
 187   *
 188   * @param int    $post_id  The ID of the post for which the post meta should be output.
 189   * @param string $location Which post meta location to output – single or preview.
 190   */
 191  function twentytwenty_the_post_meta( $post_id = null, $location = 'single-top' ) {
 192  
 193      echo twentytwenty_get_post_meta( $post_id, $location ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaped in twentytwenty_get_post_meta().
 194  }
 195  
 196  /**
 197   * Filters the edit post link to add an icon and use the post meta structure.
 198   *
 199   * @since Twenty Twenty 1.0
 200   *
 201   * @param string $link    Anchor tag for the edit link.
 202   * @param int    $post_id Post ID.
 203   * @param string $text    Anchor text.
 204   */
 205  function twentytwenty_edit_post_link( $link, $post_id, $text ) {
 206      if ( is_admin() ) {
 207          return $link;
 208      }
 209  
 210      $edit_url = get_edit_post_link( $post_id );
 211  
 212      if ( ! $edit_url ) {
 213          return;
 214      }
 215  
 216      $text = sprintf(
 217          wp_kses(
 218              /* translators: %s: Post title. Only visible to screen readers. */
 219              __( 'Edit <span class="screen-reader-text">%s</span>', 'twentytwenty' ),
 220              array(
 221                  'span' => array(
 222                      'class' => array(),
 223                  ),
 224              )
 225          ),
 226          get_the_title( $post_id )
 227      );
 228  
 229      return '<div class="post-meta-wrapper post-meta-edit-link-wrapper"><ul class="post-meta"><li class="post-edit meta-wrapper"><span class="meta-icon">' . twentytwenty_get_theme_svg( 'edit' ) . '</span><span class="meta-text"><a href="' . esc_url( $edit_url ) . '">' . $text . '</a></span></li></ul><!-- .post-meta --></div><!-- .post-meta-wrapper -->';
 230  }
 231  
 232  add_filter( 'edit_post_link', 'twentytwenty_edit_post_link', 10, 3 );
 233  
 234  /**
 235   * Retrieves the post meta.
 236   *
 237   * @since Twenty Twenty 1.0
 238   *
 239   * @param int    $post_id  The ID of the post.
 240   * @param string $location The location where the meta is shown.
 241   */
 242  function twentytwenty_get_post_meta( $post_id = null, $location = 'single-top' ) {
 243  
 244      // Require post ID.
 245      if ( ! $post_id ) {
 246          return;
 247      }
 248  
 249      /**
 250       * Filters post types array.
 251       *
 252       * This filter can be used to hide post meta information of post, page or custom post type
 253       * registered by child themes or plugins.
 254       *
 255       * @since Twenty Twenty 1.0
 256       *
 257       * @param array Array of post types.
 258       */
 259      $disallowed_post_types = apply_filters( 'twentytwenty_disallowed_post_types_for_meta_output', array( 'page' ) );
 260  
 261      // Check whether the post type is allowed to output post meta.
 262      if ( in_array( get_post_type( $post_id ), $disallowed_post_types, true ) ) {
 263          return;
 264      }
 265  
 266      $post_meta_wrapper_classes = '';
 267      $post_meta_classes         = '';
 268  
 269      // Get the post meta settings for the location specified.
 270      if ( 'single-top' === $location ) {
 271          /**
 272           * Filters post meta info visibility.
 273           *
 274           * Use this filter to hide post meta information like Author, Post date, Comments, Is sticky status.
 275           *
 276           * @since Twenty Twenty 1.0
 277           *
 278           * @param array $args {
 279           *     @type string $author
 280           *     @type string $post-date
 281           *     @type string $comments
 282           *     @type string $sticky
 283           * }
 284           */
 285          $post_meta = apply_filters(
 286              'twentytwenty_post_meta_location_single_top',
 287              array(
 288                  'author',
 289                  'post-date',
 290                  'comments',
 291                  'sticky',
 292              )
 293          );
 294  
 295          $post_meta_wrapper_classes = ' post-meta-single post-meta-single-top';
 296  
 297      } elseif ( 'single-bottom' === $location ) {
 298  
 299          /**
 300           * Filters post tags visibility.
 301           *
 302           * Use this filter to hide post tags.
 303           *
 304           * @since Twenty Twenty 1.0
 305           *
 306           * @param array $args {
 307           *     @type string $tags
 308           * }
 309           */
 310          $post_meta = apply_filters(
 311              'twentytwenty_post_meta_location_single_bottom',
 312              array(
 313                  'tags',
 314              )
 315          );
 316  
 317          $post_meta_wrapper_classes = ' post-meta-single post-meta-single-bottom';
 318  
 319      }
 320  
 321      // If the post meta setting has the value 'empty', it's explicitly empty and the default post meta shouldn't be output.
 322      if ( $post_meta && ! in_array( 'empty', $post_meta, true ) ) {
 323  
 324          // Make sure we don't output an empty container.
 325          $has_meta = false;
 326  
 327          $the_post = get_post( $post_id );
 328          setup_postdata( $the_post );
 329  
 330          ob_start();
 331  
 332          ?>
 333  
 334          <div class="post-meta-wrapper<?php echo esc_attr( $post_meta_wrapper_classes ); ?>">
 335  
 336              <ul class="post-meta<?php echo esc_attr( $post_meta_classes ); ?>">
 337  
 338                  <?php
 339  
 340                  /**
 341                   * Fires before post meta HTML display.
 342                   *
 343                   * Allow output of additional post meta info to be added by child themes and plugins.
 344                   *
 345                   * @since Twenty Twenty 1.0
 346                   * @since Twenty Twenty 1.1 Added the `$post_meta` and `$location` parameters.
 347                   *
 348                   * @param int    $post_id   Post ID.
 349                   * @param array  $post_meta An array of post meta information.
 350                   * @param string $location  The location where the meta is shown.
 351                   *                          Accepts 'single-top' or 'single-bottom'.
 352                   */
 353                  do_action( 'twentytwenty_start_of_post_meta_list', $post_id, $post_meta, $location );
 354  
 355                  // Author.
 356                  if ( post_type_supports( get_post_type( $post_id ), 'author' ) && in_array( 'author', $post_meta, true ) ) {
 357  
 358                      $has_meta = true;
 359                      ?>
 360                      <li class="post-author meta-wrapper">
 361                          <span class="meta-icon">
 362                              <span class="screen-reader-text">
 363                                  <?php
 364                                  /* translators: Hidden accessibility text. */
 365                                  _e( 'Post author', 'twentytwenty' );
 366                                  ?>
 367                              </span>
 368                              <?php twentytwenty_the_theme_svg( 'user' ); ?>
 369                          </span>
 370                          <span class="meta-text">
 371                              <?php
 372                              printf(
 373                                  /* translators: %s: Author name. */
 374                                  __( 'By %s', 'twentytwenty' ),
 375                                  '<a href="' . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . '">' . esc_html( get_the_author_meta( 'display_name' ) ) . '</a>'
 376                              );
 377                              ?>
 378                          </span>
 379                      </li>
 380                      <?php
 381  
 382                  }
 383  
 384                  // Post date.
 385                  if ( in_array( 'post-date', $post_meta, true ) ) {
 386  
 387                      $has_meta = true;
 388                      ?>
 389                      <li class="post-date meta-wrapper">
 390                          <span class="meta-icon">
 391                              <span class="screen-reader-text">
 392                                  <?php
 393                                  /* translators: Hidden accessibility text. */
 394                                  _e( 'Post date', 'twentytwenty' );
 395                                  ?>
 396                              </span>
 397                              <?php twentytwenty_the_theme_svg( 'calendar' ); ?>
 398                          </span>
 399                          <span class="meta-text">
 400                              <a href="<?php the_permalink(); ?>"><?php the_time( get_option( 'date_format' ) ); ?></a>
 401                          </span>
 402                      </li>
 403                      <?php
 404  
 405                  }
 406  
 407                  // Categories.
 408                  if ( in_array( 'categories', $post_meta, true ) && has_category() ) {
 409  
 410                      $has_meta = true;
 411                      ?>
 412                      <li class="post-categories meta-wrapper">
 413                          <span class="meta-icon">
 414                              <span class="screen-reader-text">
 415                                  <?php
 416                                  /* translators: Hidden accessibility text. */
 417                                  _e( 'Categories', 'twentytwenty' );
 418                                  ?>
 419                              </span>
 420                              <?php twentytwenty_the_theme_svg( 'folder' ); ?>
 421                          </span>
 422                          <span class="meta-text">
 423                              <?php _ex( 'In', 'A string that is output before one or more categories', 'twentytwenty' ); ?> <?php the_category( ', ' ); ?>
 424                          </span>
 425                      </li>
 426                      <?php
 427  
 428                  }
 429  
 430                  // Tags.
 431                  if ( in_array( 'tags', $post_meta, true ) && has_tag() ) {
 432  
 433                      $has_meta = true;
 434                      ?>
 435                      <li class="post-tags meta-wrapper">
 436                          <span class="meta-icon">
 437                              <span class="screen-reader-text">
 438                                  <?php
 439                                  /* translators: Hidden accessibility text. */
 440                                  _e( 'Tags', 'twentytwenty' );
 441                                  ?>
 442                              </span>
 443                              <?php twentytwenty_the_theme_svg( 'tag' ); ?>
 444                          </span>
 445                          <span class="meta-text">
 446                              <?php the_tags( '', ', ', '' ); ?>
 447                          </span>
 448                      </li>
 449                      <?php
 450  
 451                  }
 452  
 453                  // Comments link.
 454                  if ( in_array( 'comments', $post_meta, true ) && ! post_password_required() && ( comments_open() || get_comments_number() ) ) {
 455  
 456                      $has_meta = true;
 457                      ?>
 458                      <li class="post-comment-link meta-wrapper">
 459                          <span class="meta-icon">
 460                              <?php twentytwenty_the_theme_svg( 'comment' ); ?>
 461                          </span>
 462                          <span class="meta-text">
 463                              <?php comments_popup_link(); ?>
 464                          </span>
 465                      </li>
 466                      <?php
 467  
 468                  }
 469  
 470                  // Sticky.
 471                  if ( in_array( 'sticky', $post_meta, true ) && is_sticky() ) {
 472  
 473                      $has_meta = true;
 474                      ?>
 475                      <li class="post-sticky meta-wrapper">
 476                          <span class="meta-icon">
 477                              <?php twentytwenty_the_theme_svg( 'bookmark' ); ?>
 478                          </span>
 479                          <span class="meta-text">
 480                              <?php _e( 'Sticky post', 'twentytwenty' ); ?>
 481                          </span>
 482                      </li>
 483                      <?php
 484  
 485                  }
 486  
 487                  /**
 488                   * Fires after post meta HTML display.
 489                   *
 490                   * Allow output of additional post meta info to be added by child themes and plugins.
 491                   *
 492                   * @since Twenty Twenty 1.0
 493                   * @since Twenty Twenty 1.1 Added the `$post_meta` and `$location` parameters.
 494                   *
 495                   * @param int    $post_id   Post ID.
 496                   * @param array  $post_meta An array of post meta information.
 497                   * @param string $location  The location where the meta is shown.
 498                   *                          Accepts 'single-top' or 'single-bottom'.
 499                   */
 500                  do_action( 'twentytwenty_end_of_post_meta_list', $post_id, $post_meta, $location );
 501  
 502                  ?>
 503  
 504              </ul><!-- .post-meta -->
 505  
 506          </div><!-- .post-meta-wrapper -->
 507  
 508          <?php
 509  
 510          wp_reset_postdata();
 511  
 512          $meta_output = ob_get_clean();
 513  
 514          // If there is meta to output, return it.
 515          if ( $has_meta && $meta_output ) {
 516  
 517              return $meta_output;
 518  
 519          }
 520      }
 521  }
 522  
 523  /**
 524   * Menus
 525   */
 526  
 527  /**
 528   * Filters classes of wp_list_pages items to match menu items.
 529   *
 530   * Filter the class applied to wp_list_pages() items with children to match the menu class, to simplify.
 531   * styling of sub levels in the fallback. Only applied if the match_menu_classes argument is set.
 532   *
 533   * @since Twenty Twenty 1.0
 534   *
 535   * @param string[] $css_class    An array of CSS classes to be applied to each list item.
 536   * @param WP_Post  $page         Page data object.
 537   * @param int      $depth        Depth of page, used for padding.
 538   * @param array    $args         An array of arguments.
 539   * @return array CSS class names.
 540   */
 541  function twentytwenty_filter_wp_list_pages_item_classes( $css_class, $page, $depth, $args ) {
 542  
 543      // Only apply to wp_list_pages() calls with match_menu_classes set to true.
 544      $match_menu_classes = isset( $args['match_menu_classes'] );
 545  
 546      if ( ! $match_menu_classes ) {
 547          return $css_class;
 548      }
 549  
 550      // Add current menu item class.
 551      if ( in_array( 'current_page_item', $css_class, true ) ) {
 552          $css_class[] = 'current-menu-item';
 553      }
 554  
 555      // Add menu item has children class.
 556      if ( in_array( 'page_item_has_children', $css_class, true ) ) {
 557          $css_class[] = 'menu-item-has-children';
 558      }
 559  
 560      return $css_class;
 561  }
 562  
 563  add_filter( 'page_css_class', 'twentytwenty_filter_wp_list_pages_item_classes', 10, 4 );
 564  
 565  /**
 566   * Adds a Sub Nav Toggle to the Expanded Menu and Mobile Menu.
 567   *
 568   * @since Twenty Twenty 1.0
 569   *
 570   * @param stdClass $args  An object of wp_nav_menu() arguments.
 571   * @param WP_Post  $item  Menu item data object.
 572   * @return stdClass An object of wp_nav_menu() arguments.
 573   */
 574  function twentytwenty_add_sub_toggles_to_main_menu( $args, $item ) {
 575  
 576      // Add sub menu toggles to the Expanded Menu with toggles.
 577      if ( isset( $args->show_toggles ) && $args->show_toggles ) {
 578  
 579          // Wrap the menu item link contents in a div, used for positioning.
 580          $args->before = '<div class="ancestor-wrapper">';
 581          $args->after  = '';
 582  
 583          // Add a toggle to items with children.
 584          if ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
 585  
 586              $toggle_target_string = '.menu-modal .menu-item-' . $item->ID . ' > .sub-menu';
 587              $toggle_duration      = twentytwenty_toggle_duration();
 588  
 589              // Add the sub menu toggle.
 590              $args->after .= '<button class="toggle sub-menu-toggle fill-children-current-color" data-toggle-target="' . $toggle_target_string . '" data-toggle-type="slidetoggle" data-toggle-duration="' . absint( $toggle_duration ) . '" aria-expanded="false"><span class="screen-reader-text">' .
 591                  /* translators: Hidden accessibility text. */
 592                  __( 'Show sub menu', 'twentytwenty' ) .
 593              '</span>' . twentytwenty_get_theme_svg( 'chevron-down' ) . '</button>';
 594  
 595          }
 596  
 597          // Close the wrapper.
 598          $args->after .= '</div><!-- .ancestor-wrapper -->';
 599  
 600          // Add sub menu icons to the primary menu without toggles.
 601      } elseif ( 'primary' === $args->theme_location ) {
 602          if ( in_array( 'menu-item-has-children', $item->classes, true ) ) {
 603              $args->after = '<span class="icon"></span>';
 604          } else {
 605              $args->after = '';
 606          }
 607      }
 608  
 609      return $args;
 610  }
 611  
 612  add_filter( 'nav_menu_item_args', 'twentytwenty_add_sub_toggles_to_main_menu', 10, 2 );
 613  
 614  /**
 615   * Displays SVG icons in social links menu.
 616   *
 617   * @since Twenty Twenty 1.0
 618   *
 619   * @param string   $item_output The menu item's starting HTML output.
 620   * @param WP_Post  $item        Menu item data object.
 621   * @param int      $depth       Depth of the menu. Used for padding.
 622   * @param stdClass $args        An object of wp_nav_menu() arguments.
 623   * @return string The menu item output with social icon.
 624   */
 625  function twentytwenty_nav_menu_social_icons( $item_output, $item, $depth, $args ) {
 626      // Change SVG icon inside social links menu if there is supported URL.
 627      if ( 'social' === $args->theme_location ) {
 628          $svg = TwentyTwenty_SVG_Icons::get_social_link_svg( $item->url );
 629          if ( empty( $svg ) ) {
 630              $svg = twentytwenty_get_theme_svg( 'link' );
 631          }
 632          $item_output = str_replace( $args->link_after, '</span>' . $svg, $item_output );
 633      }
 634  
 635      return $item_output;
 636  }
 637  
 638  add_filter( 'walker_nav_menu_start_el', 'twentytwenty_nav_menu_social_icons', 10, 4 );
 639  
 640  /**
 641   * Classes
 642   */
 643  
 644  /**
 645   * Adds 'no-js' class.
 646   *
 647   * If we're missing JavaScript support, the HTML element will have a 'no-js' class.
 648   *
 649   * @since Twenty Twenty 1.0
 650   */
 651  function twentytwenty_no_js_class() {
 652  
 653      ?>
 654      <script>document.documentElement.className = document.documentElement.className.replace( 'no-js', 'js' );</script>
 655      <?php
 656  }
 657  
 658  add_action( 'wp_head', 'twentytwenty_no_js_class' );
 659  
 660  /**
 661   * Adds conditional body classes.
 662   *
 663   * @since Twenty Twenty 1.0
 664   *
 665   * @global WP_Post $post Global post object.
 666   *
 667   * @param array $classes Classes added to the body tag.
 668   * @return array Classes added to the body tag.
 669   */
 670  function twentytwenty_body_classes( $classes ) {
 671  
 672      global $post;
 673      $post_type = isset( $post ) ? $post->post_type : false;
 674  
 675      // Check whether we're singular.
 676      if ( is_singular() ) {
 677          $classes[] = 'singular';
 678      }
 679  
 680      // Check whether the current page should have an overlay header.
 681      if ( is_page_template( array( 'templates/template-cover.php' ) ) ) {
 682          $classes[] = 'overlay-header';
 683      }
 684  
 685      // Check whether the current page has full-width content.
 686      if ( is_page_template( array( 'templates/template-full-width.php' ) ) ) {
 687          $classes[] = 'has-full-width-content';
 688      }
 689  
 690      // Check for enabled search.
 691      if ( true === get_theme_mod( 'enable_header_search', true ) ) {
 692          $classes[] = 'enable-search-modal';
 693      }
 694  
 695      // Check for post thumbnail.
 696      if ( is_singular() && has_post_thumbnail() ) {
 697          $classes[] = 'has-post-thumbnail';
 698      } elseif ( is_singular() ) {
 699          $classes[] = 'missing-post-thumbnail';
 700      }
 701  
 702      // Check whether we're in the customizer preview.
 703      if ( is_customize_preview() ) {
 704          $classes[] = 'customizer-preview';
 705      }
 706  
 707      // Check if posts have single pagination.
 708      if ( is_single() && ( get_next_post() || get_previous_post() ) ) {
 709          $classes[] = 'has-single-pagination';
 710      } else {
 711          $classes[] = 'has-no-pagination';
 712      }
 713  
 714      // Check if we're showing comments.
 715      if ( $post && ( ( 'post' === $post_type || comments_open() || get_comments_number() ) && ! post_password_required() ) ) {
 716          $classes[] = 'showing-comments';
 717      } else {
 718          $classes[] = 'not-showing-comments';
 719      }
 720  
 721      // Check if avatars are visible.
 722      $classes[] = get_option( 'show_avatars' ) ? 'show-avatars' : 'hide-avatars';
 723  
 724      // Slim page template class names (class = name - file suffix).
 725      if ( is_page_template() ) {
 726          $classes[] = basename( get_page_template_slug(), '.php' );
 727      }
 728  
 729      // Check for the elements output in the top part of the footer.
 730      $has_footer_menu = has_nav_menu( 'footer' );
 731      $has_social_menu = has_nav_menu( 'social' );
 732      $has_sidebar_1   = is_active_sidebar( 'sidebar-1' );
 733      $has_sidebar_2   = is_active_sidebar( 'sidebar-2' );
 734  
 735      // Add a class indicating whether those elements are output.
 736      if ( $has_footer_menu || $has_social_menu || $has_sidebar_1 || $has_sidebar_2 ) {
 737          $classes[] = 'footer-top-visible';
 738      } else {
 739          $classes[] = 'footer-top-hidden';
 740      }
 741  
 742      // Get header/footer background color.
 743      $header_footer_background = get_theme_mod( 'header_footer_background_color', '#ffffff' );
 744      $header_footer_background = strtolower( '#' . ltrim( $header_footer_background, '#' ) );
 745  
 746      // Get content background color.
 747      $background_color = get_theme_mod( 'background_color', 'f5efe0' );
 748      $background_color = strtolower( '#' . ltrim( $background_color, '#' ) );
 749  
 750      // Add extra class if main background and header/footer background are the same color.
 751      if ( $background_color === $header_footer_background ) {
 752          $classes[] = 'reduced-spacing';
 753      }
 754  
 755      return $classes;
 756  }
 757  
 758  add_filter( 'body_class', 'twentytwenty_body_classes' );
 759  
 760  /**
 761   * Archives
 762   */
 763  
 764  /**
 765   * Filters the archive title and styles the word before the first colon.
 766   *
 767   * @since Twenty Twenty 1.0
 768   *
 769   * @param string $title Current archive title.
 770   * @return string Current archive title.
 771   */
 772  function twentytwenty_get_the_archive_title( $title ) {
 773  
 774      /**
 775       * Filters the regular expression used to style the word before the first colon.
 776       *
 777       * @since Twenty Twenty 1.0
 778       *
 779       * @param array $regex An array of regular expression pattern and replacement.
 780       */
 781      $regex = apply_filters(
 782          'twentytwenty_get_the_archive_title_regex',
 783          array(
 784              'pattern'     => '/(\A[^\:]+\:)/',
 785              'replacement' => '<span class="color-accent">$1</span>',
 786          )
 787      );
 788  
 789      if ( empty( $regex ) ) {
 790  
 791          return $title;
 792  
 793      }
 794  
 795      return preg_replace( $regex['pattern'], $regex['replacement'], $title );
 796  }
 797  
 798  add_filter( 'get_the_archive_title', 'twentytwenty_get_the_archive_title' );
 799  
 800  /**
 801   * Miscellaneous
 802   */
 803  
 804  /**
 805   * Toggles animation duration in milliseconds.
 806   *
 807   * @since Twenty Twenty 1.0
 808   *
 809   * @return int Duration in milliseconds
 810   */
 811  function twentytwenty_toggle_duration() {
 812      /**
 813       * Filters the animation duration/speed used usually for submenu toggles.
 814       *
 815       * @since Twenty Twenty 1.0
 816       *
 817       * @param int $duration Duration in milliseconds.
 818       */
 819      $duration = apply_filters( 'twentytwenty_toggle_duration', 250 );
 820  
 821      return $duration;
 822  }
 823  
 824  /**
 825   * Gets unique ID.
 826   *
 827   * This is a PHP implementation of Underscore's uniqueId method. A static variable
 828   * contains an integer that is incremented with each call. This number is returned
 829   * with the optional prefix. As such the returned value is not universally unique,
 830   * but it is unique across the life of the PHP process.
 831   *
 832   * @since Twenty Twenty 1.0
 833   *
 834   * @see wp_unique_id() Themes requiring WordPress 5.0.3 and greater should use this instead.
 835   *
 836   * @param string $prefix Prefix for the returned ID.
 837   * @return string Unique ID.
 838   */
 839  function twentytwenty_unique_id( $prefix = '' ) {
 840      static $id_counter = 0;
 841      if ( function_exists( 'wp_unique_id' ) ) {
 842          return wp_unique_id( $prefix );
 843      }
 844      return $prefix . (string) ++$id_counter;
 845  }


Generated : Mon Mar 18 08:20:01 2024 Cross-referenced by PHPXref