class-twentytwenty-non-latin-languages.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Non-latin language handling.
  4. *
  5. * Handle non-latin language styles.
  6. *
  7. * @package WordPress
  8. * @subpackage Twenty_Twenty
  9. * @since Twenty Twenty 1.0
  10. */
  11. if ( ! class_exists( 'TwentyTwenty_Non_Latin_Languages' ) ) {
  12. /**
  13. * Language handling.
  14. */
  15. class TwentyTwenty_Non_Latin_Languages {
  16. /**
  17. * Get custom CSS.
  18. *
  19. * Return CSS for non-latin language, if available, or null
  20. *
  21. * @param string $type Whether to return CSS for the "front-end", "block-editor" or "classic-editor".
  22. *
  23. * @return void
  24. */
  25. public static function get_non_latin_css( $type = 'front-end' ) {
  26. // Fetch site locale.
  27. $locale = get_bloginfo( 'language' );
  28. // Define fallback fonts for non-latin languages.
  29. $font_family = apply_filters(
  30. 'twentytwenty_get_localized_font_family_types',
  31. array(
  32. // Arabic.
  33. 'ar' => array( 'Tahoma', 'Arial', 'sans-serif' ),
  34. 'ary' => array( 'Tahoma', 'Arial', 'sans-serif' ),
  35. 'azb' => array( 'Tahoma', 'Arial', 'sans-serif' ),
  36. 'ckb' => array( 'Tahoma', 'Arial', 'sans-serif' ),
  37. 'fa-IR' => array( 'Tahoma', 'Arial', 'sans-serif' ),
  38. 'haz' => array( 'Tahoma', 'Arial', 'sans-serif' ),
  39. 'ps' => array( 'Tahoma', 'Arial', 'sans-serif' ),
  40. // Chinese Simplified (China) - Noto Sans SC.
  41. 'zh-CN' => array( '\'PingFang SC\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ),
  42. // Chinese Traditional (Taiwan) - Noto Sans TC.
  43. 'zh-TW' => array( '\'PingFang TC\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ),
  44. // Chinese (Hong Kong) - Noto Sans HK.
  45. 'zh-HK' => array( '\'PingFang HK\'', '\'Helvetica Neue\'', '\'Microsoft YaHei New\'', '\'STHeiti Light\'', 'sans-serif' ),
  46. // Cyrillic.
  47. 'bel' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
  48. 'bg-BG' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
  49. 'kk' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
  50. 'mk-MK' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
  51. 'mn' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
  52. 'ru-RU' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
  53. 'sah' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
  54. 'sr-RS' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
  55. 'tt-RU' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
  56. 'uk' => array( '\'Helvetica Neue\'', 'Helvetica', '\'Segoe UI\'', 'Arial', 'sans-serif' ),
  57. // Devanagari.
  58. 'bn-BD' => array( 'Arial', 'sans-serif' ),
  59. 'hi-IN' => array( 'Arial', 'sans-serif' ),
  60. 'mr' => array( 'Arial', 'sans-serif' ),
  61. 'ne-NP' => array( 'Arial', 'sans-serif' ),
  62. // Greek.
  63. 'el' => array( '\'Helvetica Neue\', Helvetica, Arial, sans-serif' ),
  64. // Gujarati.
  65. 'gu' => array( 'Arial', 'sans-serif' ),
  66. // Hebrew.
  67. 'he-IL' => array( '\'Arial Hebrew\'', 'Arial', 'sans-serif' ),
  68. // Japanese.
  69. 'ja' => array( 'sans-serif' ),
  70. // Korean.
  71. 'ko-KR' => array( '\'Apple SD Gothic Neo\'', '\'Malgun Gothic\'', '\'Nanum Gothic\'', 'Dotum', 'sans-serif' ),
  72. // Thai.
  73. 'th' => array( '\'Sukhumvit Set\'', '\'Helvetica Neue\'', 'Helvetica', 'Arial', 'sans-serif' ),
  74. // Vietnamese.
  75. 'vi' => array( '\'Libre Franklin\'', 'sans-serif' ),
  76. )
  77. );
  78. // Return if the selected language has no fallback fonts.
  79. if ( empty( $font_family[ $locale ] ) ) {
  80. return;
  81. }
  82. // Define elements to apply fallback fonts to.
  83. $elements = apply_filters(
  84. 'twentytwenty_get_localized_font_family_elements',
  85. array(
  86. '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' ),
  87. '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' ),
  88. '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' ),
  89. )
  90. );
  91. // Return if the specified type doesn't exist.
  92. if ( empty( $elements[ $type ] ) ) {
  93. return;
  94. }
  95. // Return the specified styles.
  96. return twentytwenty_generate_css( implode( ',', $elements[ $type ] ), 'font-family', implode( ',', $font_family[ $locale ] ), null, null, false );
  97. }
  98. }
  99. }