functions.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. // This theme has one menu location.
  26. register_nav_menus(
  27. array(
  28. 'primary' => __( 'Primary Navigation', 'blockbase' ),
  29. )
  30. );
  31. add_filter(
  32. 'block_editor_settings_all',
  33. function( $settings ) {
  34. $settings['defaultBlockTemplate'] = '<!-- wp:group {"layout":{"inherit":true}} --><div class="wp-block-group"><!-- wp:post-content /--></div><!-- /wp:group -->';
  35. return $settings;
  36. }
  37. );
  38. // Add support for core custom logo.
  39. add_theme_support(
  40. 'custom-logo',
  41. array(
  42. 'height' => 192,
  43. 'width' => 192,
  44. 'flex-width' => true,
  45. 'flex-height' => true,
  46. )
  47. );
  48. }
  49. endif;
  50. add_action( 'after_setup_theme', 'blockbase_support', 9 );
  51. /**
  52. *
  53. * Enqueue scripts and styles.
  54. */
  55. function blockbase_editor_styles() {
  56. // Enqueue editor styles.
  57. add_editor_style(
  58. array(
  59. blockbase_fonts_url(),
  60. )
  61. );
  62. }
  63. add_action( 'admin_init', 'blockbase_editor_styles' );
  64. /**
  65. *
  66. * Enqueue scripts and styles.
  67. */
  68. function blockbase_scripts() {
  69. // Enqueue Google fonts
  70. wp_enqueue_style( 'blockbase-fonts', blockbase_fonts_url(), array(), null );
  71. wp_enqueue_style( 'blockbase-ponyfill', get_template_directory_uri() . '/assets/ponyfill.css', array(), wp_get_theme()->get( 'Version' ) );
  72. }
  73. add_action( 'wp_enqueue_scripts', 'blockbase_scripts' );
  74. /**
  75. * Add Google webfonts
  76. *
  77. * @return $fonts_url
  78. */
  79. function blockbase_fonts_url() {
  80. if ( ! class_exists( 'WP_Theme_JSON_Resolver_Gutenberg' ) ) {
  81. return '';
  82. }
  83. $theme_data = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_settings();
  84. if ( empty( $theme_data ) || empty( $theme_data['typography'] ) || empty( $theme_data['typography']['fontFamilies'] ) ) {
  85. return '';
  86. }
  87. $font_families = [];
  88. if ( ! empty( $theme_data['typography']['fontFamilies']['user'] ) ) {
  89. foreach( $theme_data['typography']['fontFamilies']['user'] as $font ) {
  90. if ( ! empty( $font['google'] ) ) {
  91. $font_families[] = $font['google'];
  92. }
  93. }
  94. } else {
  95. if ( ! empty( $theme_data['typography']['fontFamilies']['theme'] ) ) {
  96. foreach( $theme_data['typography']['fontFamilies']['theme'] as $font ) {
  97. if ( ! empty( $font['google'] ) ) {
  98. $font_families[] = $font['google'];
  99. }
  100. }
  101. }
  102. }
  103. if ( empty( $font_families ) ) {
  104. return '';
  105. }
  106. // Make a single request for the theme or user fonts.
  107. return esc_url_raw( 'https://fonts.googleapis.com/css2?' . implode( '&', array_unique( $font_families ) ) . '&display=swap' );
  108. }
  109. /**
  110. * Restores the Customizer since we still rely on it.
  111. */
  112. function blockbase_restore_customizer() {
  113. remove_action( 'admin_menu', 'gutenberg_remove_legacy_pages' );
  114. }
  115. add_action( 'init', 'blockbase_restore_customizer' );
  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. }
  124. // Force menus to reload
  125. add_action(
  126. 'customize_controls_enqueue_scripts',
  127. static function () {
  128. wp_enqueue_script(
  129. 'wp-customize-nav-menu-refresh',
  130. get_template_directory_uri() . '/inc/customizer/wp-customize-nav-menu-refresh.js',
  131. [ 'customize-nav-menus' ],
  132. wp_get_theme()->get( 'Version' ),
  133. true
  134. );
  135. }
  136. );
  137. /**
  138. * Populate the social links block with the social menu content if it exists
  139. *
  140. */
  141. add_filter( 'render_block', 'blockbase_social_menu_render', 10, 2 );
  142. // We should only change the render of the navigtion block
  143. // to social links in the following conditions.
  144. function blockbase_condition_to_render_social_menu( $block ) {
  145. // The block should be a navigation block.
  146. if ( 'core/navigation' !== $block['blockName'] ) {
  147. return false;
  148. }
  149. // The theme should have a menu defined at the social location.
  150. if ( ! has_nav_menu( 'social' ) ) {
  151. return false;
  152. }
  153. // The block should have a loction defined.
  154. if ( empty( $block['attrs']['__unstableLocation'] ) ) {
  155. return false;
  156. }
  157. // The block's location should be 'social'.
  158. if ( $block['attrs']['__unstableLocation'] !== 'social' ) {
  159. return false;
  160. }
  161. return true;
  162. }
  163. function blockbase_social_menu_render( $block_content, $block ) {
  164. if ( blockbase_condition_to_render_social_menu( $block ) ) {
  165. $nav_menu_locations = get_nav_menu_locations();
  166. $social_menu_id = $nav_menu_locations['social'];
  167. $class_name = 'is-style-logos-only';
  168. if( !empty( $block['attrs']['itemsJustification'] ) && $block['attrs']['itemsJustification'] === 'right' ) {
  169. $class_name .= ' items-justified-right';
  170. }
  171. $block_content = '<!-- wp:social-links {"iconColor":"primary","iconColorValue":"var(--wp--custom--color--primary)","className":"' . $class_name . '"} --><ul class="wp-block-social-links has-icon-color ' . $class_name . '">';
  172. $menu = wp_get_nav_menu_items( $social_menu_id );
  173. foreach ($menu as $menu_item) {
  174. $service_name = preg_replace( '/(-[0-9]+)/', '', $menu_item->post_name );
  175. $block_content .= '<!-- wp:social-link {"url":"' . $menu_item->url . '","service":"' . $service_name . '"} /-->';
  176. }
  177. $block_content .= '</ul>';
  178. return do_blocks( $block_content );
  179. }
  180. return $block_content;
  181. }