functions.php 5.6 KB

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