functions.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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']['user'] ) ) {
  100. foreach( $theme_data['typography']['fontFamilies']['user'] as $font ) {
  101. if ( ! empty( $font['google'] ) ) {
  102. $font_families[] = $font['google'];
  103. }
  104. }
  105. } else {
  106. if ( ! empty( $theme_data['typography']['fontFamilies']['theme'] ) ) {
  107. foreach( $theme_data['typography']['fontFamilies']['theme'] as $font ) {
  108. if ( ! empty( $font['google'] ) ) {
  109. $font_families[] = $font['google'];
  110. }
  111. }
  112. }
  113. }
  114. if ( empty( $font_families ) ) {
  115. return '';
  116. }
  117. // Make a single request for the theme or user fonts.
  118. return esc_url_raw( 'https://fonts.googleapis.com/css2?' . implode( '&', array_unique( $font_families ) ) . '&display=swap' );
  119. }
  120. /**
  121. * Restores the Customizer since we still rely on it.
  122. */
  123. function blockbase_restore_customizer() {
  124. remove_action( 'admin_menu', 'gutenberg_remove_legacy_pages' );
  125. }
  126. add_action( 'init', 'blockbase_restore_customizer' );
  127. /**
  128. * Customize Global Styles
  129. */
  130. if ( class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
  131. require get_template_directory() . '/inc/customizer/wp-customize-colors.php';
  132. require get_template_directory() . '/inc/customizer/wp-customize-color-palettes.php';
  133. require get_template_directory() . '/inc/customizer/wp-customize-fonts.php';
  134. }
  135. require get_template_directory() . '/inc/social-navigation.php';
  136. // Force menus to reload
  137. add_action(
  138. 'customize_controls_enqueue_scripts',
  139. static function () {
  140. wp_enqueue_script(
  141. 'wp-customize-nav-menu-refresh',
  142. get_template_directory_uri() . '/inc/customizer/wp-customize-nav-menu-refresh.js',
  143. [ 'customize-nav-menus' ],
  144. wp_get_theme()->get( 'Version' ),
  145. true
  146. );
  147. }
  148. );
  149. /**
  150. * Block Styles.
  151. */
  152. require get_template_directory() . '/inc/block-styles.php';
  153. /**
  154. * Block Patterns.
  155. */
  156. require get_template_directory() . '/inc/block-patterns.php';