functions.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * Skatepark functions and definitions
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package Skatepark
  8. * @since Skatepark 1.0
  9. */
  10. /**
  11. * Add class to body if post/page has a featured image.
  12. */
  13. function skatepark_add_featured_image_class( $classes ) {
  14. global $post;
  15. if ( isset( $post->ID ) && get_the_post_thumbnail( $post->ID ) ) {
  16. $classes[] = 'has-featured-image';
  17. }
  18. return $classes;
  19. }
  20. add_filter( 'body_class', 'skatepark_add_featured_image_class' );
  21. if ( ! function_exists( 'skatepark_support' ) ) :
  22. /**
  23. * Sets up theme defaults and registers support for various WordPress features.
  24. *
  25. * @since Skatepark 1.0
  26. *
  27. * @return void
  28. */
  29. function skatepark_support() {
  30. // Add support for block styles.
  31. add_theme_support( 'wp-block-styles' );
  32. // Enqueue editor styles.
  33. add_editor_style( 'style.css' );
  34. }
  35. endif;
  36. add_action( 'after_setup_theme', 'skatepark_support' );
  37. if ( ! function_exists( 'skatepark_styles' ) ) :
  38. /**
  39. * Enqueue styles.
  40. *
  41. * @since Skatepark 1.0
  42. *
  43. * @return void
  44. */
  45. function skatepark_styles() {
  46. // Register theme stylesheet.
  47. wp_register_style(
  48. 'skatepark-style',
  49. get_template_directory_uri() . '/assets/theme.css',
  50. array(),
  51. wp_get_theme()->get( 'Version' )
  52. );
  53. // Add styles inline.
  54. wp_add_inline_style( 'skatepark-style', skatepark_get_font_face_styles() );
  55. // Enqueue theme stylesheet.
  56. wp_enqueue_style( 'skatepark-style' );
  57. }
  58. endif;
  59. add_action( 'wp_enqueue_scripts', 'skatepark_styles' );
  60. if ( ! function_exists( 'skatepark_editor_styles' ) ) :
  61. /**
  62. * Enqueue editor styles.
  63. *
  64. * @since Skatepark 1.0
  65. *
  66. * @return void
  67. */
  68. function skatepark_editor_styles() {
  69. // Add styles inline.
  70. wp_add_inline_style( 'wp-block-library', skatepark_get_font_face_styles() );
  71. add_editor_style(
  72. get_template_directory_uri() . '/assets/theme.css'
  73. );
  74. }
  75. endif;
  76. add_action( 'admin_init', 'skatepark_editor_styles' );
  77. if ( ! function_exists( 'skatepark_get_font_face_styles' ) ) :
  78. /**
  79. * Get font face styles.
  80. * Called by functions skatepark_styles() and skatepark_editor_styles() above.
  81. *
  82. * @since Skatepark 1.0
  83. *
  84. * @return string
  85. */
  86. function skatepark_get_font_face_styles() {
  87. return "
  88. @font-face{
  89. font-family: 'Red Hat Display';
  90. font-weight: 400;
  91. font-style: normal;
  92. font-stretch: normal;
  93. font-display: swap;
  94. src: url('" . get_theme_file_uri( 'assets/fonts/red-hat-display-regular.woff2' ) . "') format('woff2');
  95. }
  96. @font-face{
  97. font-family: 'Red Hat Display';
  98. font-weight: 500;
  99. font-style: normal;
  100. font-stretch: normal;
  101. font-display: swap;
  102. src: url('" . get_theme_file_uri( 'assets/fonts/red-hat-display-500.woff2' ) . "') format('woff2');
  103. }
  104. @font-face{
  105. font-family: 'Red Hat Display';
  106. font-weight: 700;
  107. font-style: normal;
  108. font-stretch: normal;
  109. font-display: swap;
  110. src: url('" . get_theme_file_uri( 'assets/fonts/red-hat-display-700.woff2' ) . "') format('woff2');
  111. }
  112. @font-face{
  113. font-family: 'Red Hat Display';
  114. font-weight: 900;
  115. font-style: normal;
  116. font-stretch: normal;
  117. font-display: swap;
  118. src: url('" . get_theme_file_uri( 'assets/fonts/red-hat-display-900.woff2' ) . "') format('woff2');
  119. }
  120. @font-face{
  121. font-family: 'Red Hat Display';
  122. font-weight: 400;
  123. font-style: italic;
  124. font-stretch: normal;
  125. font-display: swap;
  126. src: url('" . get_theme_file_uri( 'assets/fonts/red-hat-display-italic.woff2' ) . "') format('woff2');
  127. }
  128. @font-face{
  129. font-family: 'Red Hat Display';
  130. font-weight: 500;
  131. font-style: italic;
  132. font-stretch: normal;
  133. font-display: swap;
  134. src: url('" . get_theme_file_uri( 'assets/fonts/red-hat-display-500italic.woff2' ) . "') format('woff2');
  135. }
  136. @font-face{
  137. font-family: 'Red Hat Display';
  138. font-weight: 700;
  139. font-style: italic;
  140. font-stretch: normal;
  141. font-display: swap;
  142. src: url('" . get_theme_file_uri( 'assets/fonts/red-hat-display-700italic.woff2' ) . "') format('woff2');
  143. }
  144. @font-face{
  145. font-family: 'Red Hat Display';
  146. font-weight: 900;
  147. font-style: italic;
  148. font-stretch: normal;
  149. font-display: swap;
  150. src: url('" . get_theme_file_uri( 'assets/fonts/red-hat-display-900italic.woff2' ) . "') format('woff2');
  151. }
  152. ";
  153. }
  154. endif;
  155. if ( ! function_exists( 'skatepark_preload_webfonts' ) ) :
  156. /**
  157. * Preloads the main web font to improve performance.
  158. *
  159. * Only the main web font (font-weight: 400,700, font-style: normal) is preloaded here since that font is always relevant.
  160. * The other fonts are only needed if the user changed style or weight of the fonts,
  161. * and therefore preloading it would in most cases regress performance when that font would otherwise not be loaded
  162. * at all.
  163. *
  164. * @since Skatepark 1.0
  165. *
  166. * @return void
  167. */
  168. function skatepark_preload_webfonts() {
  169. ?>
  170. <link rel="preload" href="<?php echo esc_url( get_theme_file_uri( 'assets/fonts/red-hat-display-regular.woff2' ) ); ?>" as="font" type="font/woff2" crossorigin>
  171. <link rel="preload" href="<?php echo esc_url( get_theme_file_uri( 'assets/fonts/red-hat-display-700.woff2' ) ); ?>" as="font" type="font/woff2" crossorigin>
  172. <?php
  173. }
  174. endif;
  175. add_action( 'wp_head', 'skatepark_preload_webfonts' );
  176. /**
  177. * Registers block patterns and categories.
  178. *
  179. * @since Skatepark 1.0
  180. *
  181. * @return void
  182. */
  183. function skatepark_register_block_pattern_categories() {
  184. //Needed until https://github.com/WordPress/gutenberg/issues/39500 is fixed.
  185. $block_pattern_categories = array(
  186. 'featured' => array( 'label' => __( 'Featured', 'skatepark' ) ),
  187. 'columns' => array( 'label' => __( 'Columns', 'skatepark' ) ),
  188. 'images' => array( 'label' => __( 'Images', 'skatepark' ) ),
  189. 'text' => array( 'label' => __( 'Text', 'skatepark' ) ),
  190. 'query' => array( 'label' => __( 'Query', 'skatepark' ) ),
  191. );
  192. /**
  193. * Filters the theme block pattern categories.
  194. *
  195. * @since Skatepark 1.0
  196. *
  197. * @param array[] $block_pattern_categories {
  198. * An associative array of block pattern categories, keyed by category name.
  199. *
  200. * @type array[] $properties {
  201. * An array of block category properties.
  202. *
  203. * @type string $label A human-readable label for the pattern category.
  204. * }
  205. * }
  206. */
  207. $block_pattern_categories = apply_filters( 'skatepark_block_pattern_categories', $block_pattern_categories );
  208. foreach ( $block_pattern_categories as $name => $properties ) {
  209. if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( $name ) ) {
  210. register_block_pattern_category( $name, $properties );
  211. }
  212. }
  213. }
  214. add_action( 'init', 'skatepark_register_block_pattern_categories', 9 );