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