functions.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. // Load Jetpack packages included with theme (Jetpack plugin is not required).
  3. // Run `composer update --no-dev` to update packages and commit the changes.
  4. require_once 'vendor/autoload.php';
  5. if ( ! function_exists( 'blockbase_support' ) ) :
  6. function blockbase_support() {
  7. // Alignwide and alignfull classes in the block editor.
  8. add_theme_support( 'align-wide' );
  9. // Add support for experimental link color control.
  10. add_theme_support( 'experimental-link-color' );
  11. // Add support for responsive embedded content.
  12. // https://github.com/WordPress/gutenberg/issues/26901
  13. add_theme_support( 'responsive-embeds' );
  14. // Add support for editor styles.
  15. add_theme_support( 'editor-styles' );
  16. // Add support for post thumbnails.
  17. add_theme_support( 'post-thumbnails' );
  18. // Experimental support for adding blocks inside nav menus
  19. add_theme_support( 'block-nav-menus' );
  20. // Enqueue editor styles.
  21. add_editor_style(
  22. array(
  23. '/assets/ponyfill.css',
  24. )
  25. );
  26. // Register two nav menus
  27. register_nav_menus(
  28. array(
  29. 'primary' => __( 'Primary Navigation', 'blockbase' ),
  30. 'social' => __( 'Social Navigation', 'blockbase' )
  31. )
  32. );
  33. add_filter(
  34. 'block_editor_settings_all',
  35. function( $settings ) {
  36. $settings['defaultBlockTemplate'] = '<!-- wp:group {"layout":{"inherit":true}} --><div class="wp-block-group"><!-- wp:post-content /--></div><!-- /wp:group -->';
  37. return $settings;
  38. }
  39. );
  40. // Add support for core custom logo.
  41. add_theme_support(
  42. 'custom-logo',
  43. array(
  44. 'height' => 192,
  45. 'width' => 192,
  46. 'flex-width' => true,
  47. 'flex-height' => true,
  48. )
  49. );
  50. }
  51. endif;
  52. add_action( 'after_setup_theme', 'blockbase_support', 9 );
  53. /**
  54. *
  55. * Enqueue scripts and styles.
  56. */
  57. function blockbase_editor_styles() {
  58. // Enqueue editor styles.
  59. add_editor_style(
  60. array(
  61. blockbase_fonts_url(),
  62. )
  63. );
  64. // Add the child theme CSS if it exists.
  65. if ( file_exists( get_stylesheet_directory() . '/assets/theme.css' ) ) {
  66. add_editor_style(
  67. '/assets/theme.css'
  68. );
  69. }
  70. }
  71. add_action( 'admin_init', 'blockbase_editor_styles' );
  72. /**
  73. *
  74. * Enqueue scripts and styles.
  75. */
  76. function blockbase_scripts() {
  77. // Enqueue Google fonts
  78. wp_enqueue_style( 'blockbase-fonts', blockbase_fonts_url(), array(), null );
  79. wp_enqueue_style( 'blockbase-ponyfill', get_template_directory_uri() . '/assets/ponyfill.css', array(), wp_get_theme()->get( 'Version' ) );
  80. // Add the child theme CSS if it exists.
  81. if ( file_exists( get_stylesheet_directory() . '/assets/theme.css' ) ) {
  82. wp_enqueue_style( 'blockbase-child-styles', get_stylesheet_directory_uri() . '/assets/theme.css', array('blockbase-ponyfill'), wp_get_theme()->get( 'Version' ) );
  83. }
  84. }
  85. add_action( 'wp_enqueue_scripts', 'blockbase_scripts' );
  86. /**
  87. * Customize Global Styles
  88. */
  89. if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
  90. require get_template_directory() . '/inc/customizer/wp-customize-colors.php';
  91. require get_template_directory() . '/inc/customizer/wp-customize-color-palettes.php';
  92. require get_template_directory() . '/inc/social-navigation.php';
  93. }
  94. // Font settings migration and deprecation message. Fonts now set in Global Styles.
  95. require get_template_directory() . '/inc/customizer/wp-customize-fonts.php';
  96. // Force menus to reload
  97. add_action(
  98. 'customize_controls_enqueue_scripts',
  99. static function () {
  100. wp_enqueue_script(
  101. 'wp-customize-nav-menu-refresh',
  102. get_template_directory_uri() . '/inc/customizer/wp-customize-nav-menu-refresh.js',
  103. [ 'customize-nav-menus' ],
  104. wp_get_theme()->get( 'Version' ),
  105. true
  106. );
  107. }
  108. );
  109. /**
  110. * Disable the fallback for the core/navigation block.
  111. */
  112. function blockbase_core_navigation_render_fallback() {
  113. return null;
  114. }
  115. add_filter( 'block_core_navigation_render_fallback', 'blockbase_core_navigation_render_fallback' );
  116. /**
  117. * Block Patterns.
  118. */
  119. require get_template_directory() . '/inc/block-patterns.php';
  120. // Add the child theme patterns if they exist.
  121. if ( file_exists( get_stylesheet_directory() . '/inc/block-patterns.php' ) ) {
  122. require_once get_stylesheet_directory() . '/inc/block-patterns.php';
  123. }
  124. // ---------- Custom Fonts ---------- //
  125. /**
  126. * Font families used in block settings.
  127. *
  128. * @var array
  129. */
  130. $blockbase_block_font_families = array();
  131. /**
  132. * Generate url used to load font-face css from Google.
  133. *
  134. * @return string
  135. */
  136. function blockbase_fonts_url() {
  137. global $blockbase_block_font_families;
  138. $font_families = [];
  139. $global_styles_fonts = \Automattic\Jetpack\Fonts\Introspectors\Global_Styles::collect_fonts_from_global_styles();
  140. $fonts_to_load = array_merge( $blockbase_block_font_families, $global_styles_fonts );
  141. $fonts_to_load = array_unique( $fonts_to_load );
  142. $font_settings = blockbase_get_font_settings();
  143. foreach( $fonts_to_load as $font_slug ) {
  144. if ( isset( $font_settings[ $font_slug ]['google'] ) ) {
  145. $font_families[] = $font_settings[ $font_slug ]['google'];
  146. }
  147. }
  148. if ( empty( $font_families ) ) {
  149. return '';
  150. }
  151. // Make a single request for the theme or user fonts.
  152. return esc_url_raw( 'https://fonts.googleapis.com/css2?' . implode( '&', array_unique( $font_families ) ) . '&display=swap' );
  153. }
  154. /**
  155. * Get an array of the fonts from theme.json, with font slug as the key.
  156. *
  157. * @return array
  158. */
  159. function blockbase_get_font_settings() {
  160. $font_settings = wp_get_global_settings( array( 'typography', 'fontFamilies' ) );
  161. $remapped_font_settings = array();
  162. foreach( $font_settings['theme'] as $font ) {
  163. $remapped_font_settings[ $font['slug'] ] = $font;
  164. }
  165. return $remapped_font_settings;
  166. }
  167. /**
  168. * Gather fonts set in block settings.
  169. *
  170. * @filter pre_render_block
  171. *
  172. * @param string|null $content The pre-rendered content. Default null.
  173. * @param array $parsed_block The block being rendered.
  174. * @return string|null
  175. */
  176. function blockbase_enqueue_block_fonts( $content, $parsed_block ) {
  177. global $blockbase_block_font_families;
  178. if ( ! is_admin() && isset( $parsed_block['attrs']['fontFamily'] ) ) {
  179. $block_font_family = $parsed_block['attrs']['fontFamily'];
  180. $blockbase_block_font_families[] = $block_font_family;
  181. }
  182. return $content;
  183. }
  184. add_filter( 'pre_render_block', 'blockbase_enqueue_block_fonts', 20, 2 );
  185. /**
  186. * Disable the Jetpack Google Fonts module, since Blockbase provids it's own font loading.
  187. *
  188. * Prevents duplicate fonts in font family settings.
  189. *
  190. * @return void
  191. */
  192. function blockbase_disable_jetpack_google_fonts() {
  193. if ( method_exists( 'Jetpack', 'is_module_active' ) && Jetpack::is_module_active( 'google-fonts' ) ) {
  194. Jetpack::deactivate_module( 'google-fonts' );
  195. }
  196. }
  197. add_action( 'init', 'blockbase_disable_jetpack_google_fonts', 0 );