custom-fonts.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * Get the CSS containing font_face values for a given slug.
  4. * WPTT webfont-loader is leveraged to serve the fonts locally
  5. *
  6. * @return string String of CSS
  7. */
  8. function get_style_css( $slug ) {
  9. $font_css = '';
  10. $data = WP_Theme_JSON_Resolver::get_merged_data()->get_raw_data();
  11. $font_families = $data['settings']['typography']['fontFamilies']['theme'];
  12. foreach ( $font_families as $font_family ) {
  13. if ( $font_family['slug'] == $slug ) {
  14. if ( ! array_key_exists( 'fontFace', $font_family ) ) {
  15. continue;
  16. }
  17. foreach ( $font_family['fontFace'] as $font_face ) {
  18. if ( ! array_key_exists( 'src', $font_face ) ) {
  19. continue;
  20. }
  21. foreach ( $font_face['src'] as $font_face_src ) {
  22. $font_css .= wptt_get_webfont_styles( $font_face_src );
  23. }
  24. }
  25. }
  26. }
  27. return $font_css;
  28. }
  29. /**
  30. * Collect fonts set in Global Styles settings.
  31. *
  32. * @return array Font faces from Global Styles settings.
  33. */
  34. function collect_fonts_from_global_styles() {
  35. $global_styles = wp_get_global_styles();
  36. $found_webfonts = array();
  37. // Look for fonts in block presets...
  38. if ( isset( $global_styles['blocks'] ) ) {
  39. foreach ( $global_styles['blocks'] as $setting ) {
  40. $font_slug = extract_font_slug_from_setting( $setting );
  41. if ( $font_slug ) {
  42. $found_webfonts[] = $font_slug;
  43. }
  44. }
  45. }
  46. // Look for fonts in HTML element presets...
  47. if ( isset( $global_styles['elements'] ) ) {
  48. foreach ( $global_styles['elements'] as $setting ) {
  49. $font_slug = extract_font_slug_from_setting( $setting );
  50. if ( $font_slug ) {
  51. $found_webfonts[] = $font_slug;
  52. }
  53. }
  54. }
  55. // Check if a global typography setting was defined.
  56. $font_slug = extract_font_slug_from_setting( $global_styles );
  57. if ( $font_slug ) {
  58. $found_webfonts[] = $font_slug;
  59. }
  60. return array_unique( $found_webfonts );
  61. }
  62. /**
  63. * Extract the font family slug from a settings array.
  64. *
  65. * @param array $setting The settings object.
  66. *
  67. * @return string|null
  68. */
  69. function extract_font_slug_from_setting( $setting ) {
  70. if ( ! isset( $setting['typography']['fontFamily'] ) ) {
  71. return null;
  72. }
  73. $font_family = $setting['typography']['fontFamily'];
  74. // Full string: var(--wp--preset--font-family--slug).
  75. // We do not care about the origin of the font, only its slug.
  76. preg_match( '/font-family--(?P<slug>.+)\)$/', $font_family, $matches );
  77. if ( isset( $matches['slug'] ) ) {
  78. return $matches['slug'];
  79. }
  80. // Full string: var:preset|font-family|slug
  81. // We do not care about the origin of the font, only its slug.
  82. preg_match( '/font-family\|(?P<slug>.+)$/', $font_family, $matches );
  83. if ( isset( $matches['slug'] ) ) {
  84. return $matches['slug'];
  85. }
  86. return $font_family;
  87. }
  88. /**
  89. * Build a list of all font slugs provided by theme from theme.json
  90. *
  91. * @return array Collection of all font slugs defined in the theme.json file
  92. */
  93. function collect_fonts_from_blockbase() {
  94. $font_family_slugs = array();
  95. $global_styles = wp_get_global_styles();
  96. $data = WP_Theme_JSON_Resolver::get_merged_data()->get_raw_data();
  97. $font_families = $data['settings']['typography']['fontFamilies']['theme'];
  98. foreach ( $font_families as $font_family ) {
  99. $font_family_slugs[] = $font_family['slug'];
  100. }
  101. return $font_family_slugs;
  102. }
  103. /**
  104. * Enqeue all of the fonts used in global styles settings.
  105. *
  106. * @return void
  107. */
  108. function enqueue_global_styles_fonts() {
  109. $fonts;
  110. $font_css = '';
  111. if ( is_admin() ) {
  112. $fonts = collect_fonts_from_blockbase();
  113. } else {
  114. $fonts = collect_fonts_from_global_styles();
  115. }
  116. foreach ( $fonts as $font ) {
  117. $font_css .= get_style_css( $font );
  118. }
  119. // Bail out if there are no styles to enqueue.
  120. if ( '' === $font_css ) {
  121. return;
  122. }
  123. // Enqueue the stylesheet.
  124. wp_register_style( 'blockbase_font_faces', '' );
  125. wp_enqueue_style( 'blockbase_font_faces' );
  126. // Add the styles to the stylesheet.
  127. wp_add_inline_style( 'blockbase_font_faces', $font_css );
  128. }
  129. /**
  130. * Enqueue all of the fonts provided by Blockbase for FSE use
  131. */
  132. function enqueue_fse_font_styles( $fonts ) {
  133. $fonts = collect_fonts_from_blockbase();
  134. $font_css = '';
  135. foreach ( $fonts as $font ) {
  136. $font_css .= get_style_css( $font );
  137. }
  138. wp_enqueue_style( 'wp-block-library' );
  139. wp_add_inline_style( 'wp-block-library', $font_css );
  140. }
  141. add_action( 'init', 'enqueue_global_styles_fonts' );
  142. add_action( 'admin_init', 'enqueue_fse_font_styles' );