[ Index ]

PHP Cross Reference of WordPress

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


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