[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-content/themes/twentytwentyone/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_Twenty_One
   7   * @since Twenty Twenty-One 1.0
   8   */
   9  
  10  /**
  11   * Adds custom classes to the array of body classes.
  12   *
  13   * @since Twenty Twenty-One 1.0
  14   *
  15   * @param array $classes Classes for the body element.
  16   * @return array
  17   */
  18  function twenty_twenty_one_body_classes( $classes ) {
  19  
  20      // Helps detect if JS is enabled or not.
  21      $classes[] = 'no-js';
  22  
  23      // Adds `singular` to singular pages, and `hfeed` to all other pages.
  24      $classes[] = is_singular() ? 'singular' : 'hfeed';
  25  
  26      // Add a body class if main navigation is active.
  27      if ( has_nav_menu( 'primary' ) ) {
  28          $classes[] = 'has-main-navigation';
  29      }
  30  
  31      // Add a body class if there are no footer widgets.
  32      if ( ! is_active_sidebar( 'sidebar-1' ) ) {
  33          $classes[] = 'no-widgets';
  34      }
  35  
  36      return $classes;
  37  }
  38  add_filter( 'body_class', 'twenty_twenty_one_body_classes' );
  39  
  40  /**
  41   * Adds custom class to the array of posts classes.
  42   *
  43   * @since Twenty Twenty-One 1.0
  44   *
  45   * @param array $classes An array of CSS classes.
  46   * @return array
  47   */
  48  function twenty_twenty_one_post_classes( $classes ) {
  49      $classes[] = 'entry';
  50  
  51      return $classes;
  52  }
  53  add_filter( 'post_class', 'twenty_twenty_one_post_classes', 10, 3 );
  54  
  55  /**
  56   * Add a pingback url auto-discovery header for single posts, pages, or attachments.
  57   *
  58   * @since Twenty Twenty-One 1.0
  59   *
  60   * @return void
  61   */
  62  function twenty_twenty_one_pingback_header() {
  63      if ( is_singular() && pings_open() ) {
  64          echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
  65      }
  66  }
  67  add_action( 'wp_head', 'twenty_twenty_one_pingback_header' );
  68  
  69  /**
  70   * Remove the `no-js` class from body if JS is supported.
  71   *
  72   * @since Twenty Twenty-One 1.0
  73   *
  74   * @return void
  75   */
  76  function twenty_twenty_one_supports_js() {
  77      echo '<script>document.body.classList.remove("no-js");</script>';
  78  }
  79  add_action( 'wp_footer', 'twenty_twenty_one_supports_js' );
  80  
  81  /**
  82   * Changes comment form default fields.
  83   *
  84   * @since Twenty Twenty-One 1.0
  85   *
  86   * @param array $defaults The form defaults.
  87   * @return array
  88   */
  89  function twenty_twenty_one_comment_form_defaults( $defaults ) {
  90  
  91      // Adjust height of comment form.
  92      $defaults['comment_field'] = preg_replace( '/rows="\d+"/', 'rows="5"', $defaults['comment_field'] );
  93  
  94      return $defaults;
  95  }
  96  add_filter( 'comment_form_defaults', 'twenty_twenty_one_comment_form_defaults' );
  97  
  98  /**
  99   * Determines if post thumbnail can be displayed.
 100   *
 101   * @since Twenty Twenty-One 1.0
 102   *
 103   * @return bool
 104   */
 105  function twenty_twenty_one_can_show_post_thumbnail() {
 106      /**
 107       * Filters whether post thumbnail can be displayed.
 108       *
 109       * @since Twenty Twenty-One 1.0
 110       *
 111       * @param bool $show_post_thumbnail Whether to show post thumbnail.
 112       */
 113      return apply_filters(
 114          'twenty_twenty_one_can_show_post_thumbnail',
 115          ! post_password_required() && ! is_attachment() && has_post_thumbnail()
 116      );
 117  }
 118  
 119  /**
 120   * Returns the size for avatars used in the theme.
 121   *
 122   * @since Twenty Twenty-One 1.0
 123   *
 124   * @return int
 125   */
 126  function twenty_twenty_one_get_avatar_size() {
 127      return 60;
 128  }
 129  
 130  /**
 131   * Creates continue reading text.
 132   *
 133   * @since Twenty Twenty-One 1.0
 134   */
 135  function twenty_twenty_one_continue_reading_text() {
 136      $continue_reading = sprintf(
 137          /* translators: %s: Post title. Only visible to screen readers. */
 138          esc_html__( 'Continue reading %s', 'twentytwentyone' ),
 139          the_title( '<span class="screen-reader-text">', '</span>', false )
 140      );
 141  
 142      return $continue_reading;
 143  }
 144  
 145  /**
 146   * Creates the continue reading link for excerpt.
 147   *
 148   * @since Twenty Twenty-One 1.0
 149   */
 150  function twenty_twenty_one_continue_reading_link_excerpt() {
 151      if ( ! is_admin() ) {
 152          return '&hellip; <a class="more-link" href="' . esc_url( get_permalink() ) . '">' . twenty_twenty_one_continue_reading_text() . '</a>';
 153      }
 154  }
 155  
 156  // Filter the excerpt more link.
 157  add_filter( 'excerpt_more', 'twenty_twenty_one_continue_reading_link_excerpt' );
 158  
 159  /**
 160   * Creates the continue reading link.
 161   *
 162   * @since Twenty Twenty-One 1.0
 163   */
 164  function twenty_twenty_one_continue_reading_link() {
 165      if ( ! is_admin() ) {
 166          return '<div class="more-link-container"><a class="more-link" href="' . esc_url( get_permalink() ) . '#more-' . esc_attr( get_the_ID() ) . '">' . twenty_twenty_one_continue_reading_text() . '</a></div>';
 167      }
 168  }
 169  
 170  // Filter the content more link.
 171  add_filter( 'the_content_more_link', 'twenty_twenty_one_continue_reading_link' );
 172  
 173  if ( ! function_exists( 'twenty_twenty_one_post_title' ) ) {
 174      /**
 175       * Adds a title to posts and pages that are missing titles.
 176       *
 177       * @since Twenty Twenty-One 1.0
 178       *
 179       * @param string $title The title.
 180       * @return string
 181       */
 182  	function twenty_twenty_one_post_title( $title ) {
 183          return '' === $title ? esc_html_x( 'Untitled', 'Added to posts and pages that are missing titles', 'twentytwentyone' ) : $title;
 184      }
 185  }
 186  add_filter( 'the_title', 'twenty_twenty_one_post_title' );
 187  
 188  /**
 189   * Gets the SVG code for a given icon.
 190   *
 191   * @since Twenty Twenty-One 1.0
 192   *
 193   * @param string $group The icon group.
 194   * @param string $icon  The icon.
 195   * @param int    $size  The icon size in pixels.
 196   * @return string
 197   */
 198  function twenty_twenty_one_get_icon_svg( $group, $icon, $size = 24 ) {
 199      return Twenty_Twenty_One_SVG_Icons::get_svg( $group, $icon, $size );
 200  }
 201  
 202  /**
 203   * Changes the default navigation arrows to svg icons
 204   *
 205   * @since Twenty Twenty-One 1.0
 206   *
 207   * @param string $calendar_output The generated HTML of the calendar.
 208   * @return string
 209   */
 210  function twenty_twenty_one_change_calendar_nav_arrows( $calendar_output ) {
 211      $calendar_output = str_replace( '&laquo; ', is_rtl() ? twenty_twenty_one_get_icon_svg( 'ui', 'arrow_right' ) : twenty_twenty_one_get_icon_svg( 'ui', 'arrow_left' ), $calendar_output );
 212      $calendar_output = str_replace( ' &raquo;', is_rtl() ? twenty_twenty_one_get_icon_svg( 'ui', 'arrow_left' ) : twenty_twenty_one_get_icon_svg( 'ui', 'arrow_right' ), $calendar_output );
 213      return $calendar_output;
 214  }
 215  add_filter( 'get_calendar', 'twenty_twenty_one_change_calendar_nav_arrows' );
 216  
 217  /**
 218   * Get custom CSS.
 219   *
 220   * Return CSS for non-latin language, if available, or null
 221   *
 222   * @since Twenty Twenty-One 1.0
 223   *
 224   * @param string $type Whether to return CSS for the "front-end", "block-editor", or "classic-editor".
 225   * @return string
 226   */
 227  function twenty_twenty_one_get_non_latin_css( $type = 'front-end' ) {
 228  
 229      // Fetch site locale.
 230      $locale = get_bloginfo( 'language' );
 231  
 232      /**
 233       * Filters the fallback fonts for non-latin languages.
 234       *
 235       * @since Twenty Twenty-One 1.0
 236       *
 237       * @param array $font_family An array of locales and font families.
 238       */
 239      $font_family = apply_filters(
 240          'twenty_twenty_one_get_localized_font_family_types',
 241          array(
 242  
 243              // Arabic.
 244              'ar'    => array( 'Tahoma', 'Arial', 'sans-serif' ),
 245              'ary'   => array( 'Tahoma', 'Arial', 'sans-serif' ),
 246              'azb'   => array( 'Tahoma', 'Arial', 'sans-serif' ),
 247              'ckb'   => array( 'Tahoma', 'Arial', 'sans-serif' ),
 248              'fa-IR' => array( 'Tahoma', 'Arial', 'sans-serif' ),
 249              'haz'   => array( 'Tahoma', 'Arial', 'sans-serif' ),
 250              'ps'    => array( 'Tahoma', 'Arial', 'sans-serif' ),
 251  
 252              // Chinese Simplified (China) - Noto Sans SC.
 253              'zh-CN' => array( '\'PingFang SC\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ),
 254  
 255              // Chinese Traditional (Taiwan) - Noto Sans TC.
 256              'zh-TW' => array( '\'PingFang TC\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ),
 257  
 258              // Chinese (Hong Kong) - Noto Sans HK.
 259              'zh-HK' => array( '\'PingFang HK\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ),
 260  
 261              // Cyrillic.
 262              'bel'   => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 263              'bg-BG' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 264              'kk'    => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 265              'mk-MK' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 266              'mn'    => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 267              'ru-RU' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 268              'sah'   => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 269              'sr-RS' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 270              'tt-RU' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 271              'uk'    => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
 272  
 273              // Devanagari.
 274              'bn-BD' => array( 'Arial', 'sans-serif' ),
 275              'hi-IN' => array( 'Arial', 'sans-serif' ),
 276              'mr'    => array( 'Arial', 'sans-serif' ),
 277              'ne-NP' => array( 'Arial', 'sans-serif' ),
 278  
 279              // Greek.
 280              'el'    => array( '\'Helvetica Neue\', Helvetica, Arial, sans-serif' ),
 281  
 282              // Gujarati.
 283              'gu'    => array( 'Arial', 'sans-serif' ),
 284  
 285              // Hebrew.
 286              'he-IL' => array( '\'Arial Hebrew\'', 'Arial', 'sans-serif' ),
 287  
 288              // Japanese.
 289              'ja'    => array( 'sans-serif' ),
 290  
 291              // Korean.
 292              'ko-KR' => array( '\'Apple SD Gothic Neo\'', '\'Malgun Gothic\'', '\'Nanum Gothic\'', 'Dotum', 'sans-serif' ),
 293  
 294              // Thai.
 295              'th'    => array( '\'Sukhumvit Set\'', '\'Helvetica Neue\'', 'Helvetica', 'Arial', 'sans-serif' ),
 296  
 297              // Vietnamese.
 298              'vi'    => array( '\'Libre Franklin\'', 'sans-serif' ),
 299  
 300          )
 301      );
 302  
 303      // Return if the selected language has no fallback fonts.
 304      if ( empty( $font_family[ $locale ] ) ) {
 305          return '';
 306      }
 307  
 308      /**
 309       * Filters the elements to apply fallback fonts to.
 310       *
 311       * @since Twenty Twenty-One 1.0
 312       *
 313       * @param array $elements An array of elements for "front-end", "block-editor", or "classic-editor".
 314       */
 315      $elements = apply_filters(
 316          'twenty_twenty_one_get_localized_font_family_elements',
 317          array(
 318              'front-end'      => array( 'body', 'input', 'textarea', 'button', '.button', '.faux-button', '.wp-block-button__link', '.wp-block-file__button', '.has-drop-cap:not(:focus)::first-letter', '.entry-content .wp-block-archives', '.entry-content .wp-block-categories', '.entry-content .wp-block-cover-image', '.entry-content .wp-block-latest-comments', '.entry-content .wp-block-latest-posts', '.entry-content .wp-block-pullquote', '.entry-content .wp-block-quote.is-large', '.entry-content .wp-block-quote.is-style-large', '.entry-content .wp-block-archives *', '.entry-content .wp-block-categories *', '.entry-content .wp-block-latest-posts *', '.entry-content .wp-block-latest-comments *', '.entry-content p', '.entry-content ol', '.entry-content ul', '.entry-content dl', '.entry-content dt', '.entry-content cite', '.entry-content figcaption', '.entry-content .wp-caption-text', '.comment-content p', '.comment-content ol', '.comment-content ul', '.comment-content dl', '.comment-content dt', '.comment-content cite', '.comment-content figcaption', '.comment-content .wp-caption-text', '.widget_text p', '.widget_text ol', '.widget_text ul', '.widget_text dl', '.widget_text dt', '.widget-content .rssSummary', '.widget-content cite', '.widget-content figcaption', '.widget-content .wp-caption-text' ),
 319              'block-editor'   => array( '.editor-styles-wrapper > *', '.editor-styles-wrapper p', '.editor-styles-wrapper ol', '.editor-styles-wrapper ul', '.editor-styles-wrapper dl', '.editor-styles-wrapper dt', '.editor-post-title__block .editor-post-title__input', '.editor-styles-wrapper .wp-block h1', '.editor-styles-wrapper .wp-block h2', '.editor-styles-wrapper .wp-block h3', '.editor-styles-wrapper .wp-block h4', '.editor-styles-wrapper .wp-block h5', '.editor-styles-wrapper .wp-block h6', '.editor-styles-wrapper .has-drop-cap:not(:focus)::first-letter', '.editor-styles-wrapper cite', '.editor-styles-wrapper figcaption', '.editor-styles-wrapper .wp-caption-text' ),
 320              'classic-editor' => array( 'body#tinymce.wp-editor', 'body#tinymce.wp-editor p', 'body#tinymce.wp-editor ol', 'body#tinymce.wp-editor ul', 'body#tinymce.wp-editor dl', 'body#tinymce.wp-editor dt', 'body#tinymce.wp-editor figcaption', 'body#tinymce.wp-editor .wp-caption-text', 'body#tinymce.wp-editor .wp-caption-dd', 'body#tinymce.wp-editor cite', 'body#tinymce.wp-editor table' ),
 321          )
 322      );
 323  
 324      // Return if the specified type doesn't exist.
 325      if ( empty( $elements[ $type ] ) ) {
 326          return '';
 327      }
 328  
 329      // Include file if function doesn't exist.
 330      if ( ! function_exists( 'twenty_twenty_one_generate_css' ) ) {
 331          require_once get_theme_file_path( 'inc/custom-css.php' ); // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound
 332      }
 333  
 334      // Return the specified styles.
 335      return twenty_twenty_one_generate_css( // @phpstan-ignore-line.
 336          implode( ',', $elements[ $type ] ),
 337          'font-family',
 338          implode( ',', $font_family[ $locale ] ),
 339          null,
 340          null,
 341          false
 342      );
 343  }
 344  
 345  /**
 346   * Print the first instance of a block in the content, and then break away.
 347   *
 348   * @since Twenty Twenty-One 1.0
 349   *
 350   * @param string      $block_name The full block type name, or a partial match.
 351   *                                Example: `core/image`, `core-embed/*`.
 352   * @param string|null $content    The content to search in. Use null for get_the_content().
 353   * @param int         $instances  How many instances of the block will be printed (max). Default  1.
 354   * @return bool Returns true if a block was located & printed, otherwise false.
 355   */
 356  function twenty_twenty_one_print_first_instance_of_block( $block_name, $content = null, $instances = 1 ) {
 357      $instances_count = 0;
 358      $blocks_content  = '';
 359  
 360      if ( ! $content ) {
 361          $content = get_the_content();
 362      }
 363  
 364      // Parse blocks in the content.
 365      $blocks = parse_blocks( $content );
 366  
 367      // Loop blocks.
 368      foreach ( $blocks as $block ) {
 369  
 370          // Confidence check.
 371          if ( ! isset( $block['blockName'] ) ) {
 372              continue;
 373          }
 374  
 375          // Check if this the block matches the $block_name.
 376          $is_matching_block = false;
 377  
 378          // If the block ends with *, try to match the first portion.
 379          if ( '*' === $block_name[-1] ) {
 380              $is_matching_block = 0 === strpos( $block['blockName'], rtrim( $block_name, '*' ) );
 381          } else {
 382              $is_matching_block = $block_name === $block['blockName'];
 383          }
 384  
 385          if ( $is_matching_block ) {
 386              // Increment count.
 387              ++$instances_count;
 388  
 389              // Add the block HTML.
 390              $blocks_content .= render_block( $block );
 391  
 392              // Break the loop if the $instances count was reached.
 393              if ( $instances_count >= $instances ) {
 394                  break;
 395              }
 396          }
 397      }
 398  
 399      if ( $blocks_content ) {
 400          /** This filter is documented in wp-includes/post-template.php */
 401          echo apply_filters( 'the_content', $blocks_content ); // phpcs:ignore WordPress.Security.EscapeOutput
 402          return true;
 403      }
 404  
 405      return false;
 406  }
 407  
 408  /**
 409   * Retrieve protected post password form content.
 410   *
 411   * @since Twenty Twenty-One 1.0
 412   * @since Twenty Twenty-One 1.4 Corrected parameter name for `$output`,
 413   *                              added the `$post` parameter.
 414   *
 415   * @param string      $output The password form HTML output.
 416   * @param int|WP_Post $post   Optional. Post ID or WP_Post object. Default is global $post.
 417   * @return string HTML content for password form for password protected post.
 418   */
 419  function twenty_twenty_one_password_form( $output, $post = 0 ) {
 420      $post   = get_post( $post );
 421      $label  = 'pwbox-' . ( empty( $post->ID ) ? wp_rand() : $post->ID );
 422      $output = '<p class="post-password-message">' . esc_html__( 'This content is password protected. Please enter a password to view.', 'twentytwentyone' ) . '</p>
 423      <form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" class="post-password-form" method="post">
 424      <label class="post-password-form__label" for="' . esc_attr( $label ) . '">' . esc_html_x( 'Password', 'Post password form', 'twentytwentyone' ) . '</label><input class="post-password-form__input" name="post_password" id="' . esc_attr( $label ) . '" type="password" spellcheck="false" size="20" /><input type="submit" class="post-password-form__submit" name="' . esc_attr_x( 'Submit', 'Post password form', 'twentytwentyone' ) . '" value="' . esc_attr_x( 'Enter', 'Post password form', 'twentytwentyone' ) . '" /></form>
 425      ';
 426      return $output;
 427  }
 428  add_filter( 'the_password_form', 'twenty_twenty_one_password_form', 10, 2 );
 429  
 430  /**
 431   * Filters the list of attachment image attributes.
 432   *
 433   * @since Twenty Twenty-One 1.0
 434   *
 435   * @param string[]     $attr       Array of attribute values for the image markup, keyed by attribute name.
 436   *                                 See wp_get_attachment_image().
 437   * @param WP_Post      $attachment Image attachment post.
 438   * @param string|int[] $size       Requested image size. Can be any registered image size name, or
 439   *                                 an array of width and height values in pixels (in that order).
 440   * @return string[] The filtered attributes for the image markup.
 441   */
 442  function twenty_twenty_one_get_attachment_image_attributes( $attr, $attachment, $size ) {
 443  
 444      if ( is_admin() ) {
 445          return $attr;
 446      }
 447  
 448      if ( isset( $attr['class'] ) && false !== strpos( $attr['class'], 'custom-logo' ) ) {
 449          return $attr;
 450      }
 451  
 452      $width  = false;
 453      $height = false;
 454  
 455      if ( is_array( $size ) ) {
 456          $width  = (int) $size[0];
 457          $height = (int) $size[1];
 458      } elseif ( $attachment && is_object( $attachment ) && $attachment->ID ) {
 459          $meta = wp_get_attachment_metadata( $attachment->ID );
 460          if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) {
 461              $width  = (int) $meta['width'];
 462              $height = (int) $meta['height'];
 463          }
 464      }
 465  
 466      if ( $width && $height ) {
 467  
 468          // Add style.
 469          $attr['style'] = isset( $attr['style'] ) ? $attr['style'] : '';
 470          $attr['style'] = 'width:100%;height:' . round( 100 * $height / $width, 2 ) . '%;max-width:' . $width . 'px;' . $attr['style'];
 471      }
 472  
 473      return $attr;
 474  }
 475  add_filter( 'wp_get_attachment_image_attributes', 'twenty_twenty_one_get_attachment_image_attributes', 10, 3 );


Generated : Thu Apr 25 08:20:02 2024 Cross-referenced by PHPXref