functions.php 5.2 KB

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