functions.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. // Declare that there are no <title> tags and allow WordPress to provide them
  16. add_theme_support( 'title-tag' );
  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. if ( ! class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
  92. return '';
  93. }
  94. $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_settings();
  95. if ( empty( $theme_data ) || empty( $theme_data['typography'] ) || empty( $theme_data['typography']['fontFamilies'] ) ) {
  96. return '';
  97. }
  98. $font_families = [];
  99. if ( ! empty( $theme_data['typography']['fontFamilies']['custom'] ) ) {
  100. foreach( $theme_data['typography']['fontFamilies']['custom'] as $font ) {
  101. if ( ! empty( $font['google'] ) ) {
  102. $font_families[] = $font['google'];
  103. }
  104. }
  105. // NOTE: This should be removed once Gutenberg 12.1 lands stably in all environments
  106. } else if ( ! empty( $theme_data['typography']['fontFamilies']['user'] ) ) {
  107. foreach( $theme_data['typography']['fontFamilies']['user'] as $font ) {
  108. if ( ! empty( $font['google'] ) ) {
  109. $font_families[] = $font['google'];
  110. }
  111. }
  112. // End Gutenberg < 12.1 compatibility patch
  113. } else {
  114. if ( ! empty( $theme_data['typography']['fontFamilies']['theme'] ) ) {
  115. foreach( $theme_data['typography']['fontFamilies']['theme'] as $font ) {
  116. if ( ! empty( $font['google'] ) ) {
  117. $font_families[] = $font['google'];
  118. }
  119. }
  120. }
  121. }
  122. if ( empty( $font_families ) ) {
  123. return '';
  124. }
  125. // Make a single request for the theme or user fonts.
  126. return esc_url_raw( 'https://fonts.googleapis.com/css2?' . implode( '&', array_unique( $font_families ) ) . '&display=swap' );
  127. }
  128. /**
  129. * Customize Global Styles
  130. */
  131. if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
  132. require get_template_directory() . '/inc/customizer/wp-customize-colors.php';
  133. require get_template_directory() . '/inc/customizer/wp-customize-color-palettes.php';
  134. require get_template_directory() . '/inc/customizer/wp-customize-fonts.php';
  135. require get_template_directory() . '/inc/social-navigation.php';
  136. }
  137. // Force menus to reload
  138. add_action(
  139. 'customize_controls_enqueue_scripts',
  140. static function () {
  141. wp_enqueue_script(
  142. 'wp-customize-nav-menu-refresh',
  143. get_template_directory_uri() . '/inc/customizer/wp-customize-nav-menu-refresh.js',
  144. [ 'customize-nav-menus' ],
  145. wp_get_theme()->get( 'Version' ),
  146. true
  147. );
  148. }
  149. );
  150. /**
  151. * Disable the fallback for the core/navigation block.
  152. */
  153. function blockbase_core_navigation_render_fallback() {
  154. return null;
  155. }
  156. add_filter( 'block_core_navigation_render_fallback', 'blockbase_core_navigation_render_fallback' );
  157. /**
  158. * Block Patterns.
  159. */
  160. require get_template_directory() . '/inc/block-patterns.php';
  161. // Add the child theme patterns if they exist.
  162. if ( file_exists( get_stylesheet_directory() . '/inc/block-patterns.php' ) ) {
  163. require_once get_stylesheet_directory() . '/inc/block-patterns.php';
  164. }