functions.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. /**
  3. * Child Theme Functions and definitions
  4. *
  5. * @link https://developer.wordpress.org/themes/basics/theme-functions/
  6. *
  7. * @package WordPress
  8. * @subpackage Spearhead
  9. * @since 1.0.0
  10. */
  11. if ( ! function_exists( 'spearhead_setup' ) ) :
  12. /**
  13. * Sets up theme defaults and registers support for various WordPress features.
  14. *
  15. * Note that this function is hooked into the after_setup_theme hook, which
  16. * runs before the init hook. The init hook is too late for some features, such
  17. * as indicating support for post thumbnails.
  18. */
  19. function spearhead_setup() {
  20. // Add support for editor styles.
  21. add_theme_support( 'editor-styles' );
  22. // Enqueue editor styles.
  23. add_editor_style(
  24. array(
  25. spearhead_fonts_url(),
  26. 'variables.css',
  27. 'style.css',
  28. )
  29. );
  30. // Add child theme editor font sizes to match Sass-map variables in `_config-child-theme-deep.scss`.
  31. add_theme_support(
  32. 'editor-font-sizes',
  33. array(
  34. array(
  35. 'name' => __( 'Tiny', 'spearhead' ),
  36. 'shortName' => __( 'XS', 'spearhead' ),
  37. 'size' => 14,
  38. 'slug' => 'xs',
  39. ),
  40. array(
  41. 'name' => __( 'Small', 'spearhead' ),
  42. 'shortName' => __( 'S', 'spearhead' ),
  43. 'size' => 16,
  44. 'slug' => 'small',
  45. ),
  46. array(
  47. 'name' => __( 'Medium', 'spearhead' ),
  48. 'shortName' => __( 'M', 'spearhead' ),
  49. 'size' => 20,
  50. 'slug' => 'medium',
  51. ),
  52. array(
  53. 'name' => __( 'Large', 'spearhead' ),
  54. 'shortName' => __( 'L', 'spearhead' ),
  55. 'size' => 24,
  56. 'slug' => 'large',
  57. ),
  58. array(
  59. 'name' => __( 'XL', 'spearhead' ),
  60. 'shortName' => __( 'XL', 'spearhead' ),
  61. 'size' => 36,
  62. 'slug' => 'xl',
  63. ),
  64. array(
  65. 'name' => __( 'Huge', 'spearhead' ),
  66. 'shortName' => __( 'XXL', 'spearhead' ),
  67. 'size' => 48,
  68. 'slug' => 'huge',
  69. ),
  70. )
  71. );
  72. // Add child theme editor color pallete to match Sass-map variables in `_config-child-theme-deep.scss`.
  73. add_theme_support(
  74. 'editor-color-palette',
  75. array(
  76. array(
  77. 'name' => __( 'Primary', 'spearhead' ),
  78. 'slug' => 'primary',
  79. 'color' => '#DB0042',
  80. ),
  81. array(
  82. 'name' => __( 'Foreground', 'spearhead' ),
  83. 'slug' => 'foreground',
  84. 'color' => '#000000',
  85. ),
  86. array(
  87. 'name' => __( 'Background', 'spearhead' ),
  88. 'slug' => 'background',
  89. 'color' => '#FFFFFF',
  90. ),
  91. )
  92. );
  93. remove_filter( 'excerpt_more', 'seedlet_continue_reading_link' );
  94. remove_filter( 'the_content_more_link', 'seedlet_continue_reading_link' );
  95. }
  96. endif;
  97. add_action( 'after_setup_theme', 'spearhead_setup', 12 );
  98. /**
  99. * Filter the content_width in pixels, based on the child-theme's design and stylesheet.
  100. */
  101. function spearhead_content_width() {
  102. return 782;
  103. }
  104. add_filter( 'seedlet_content_width', 'spearhead_content_width' );
  105. /**
  106. * Enqueue scripts and styles.
  107. */
  108. function spearhead_scripts() {
  109. // dequeue parent styles
  110. wp_dequeue_style( 'seedlet-style-navigation' );
  111. // enqueue Google fonts, if necessary
  112. wp_enqueue_style( 'spearhead-fonts', spearhead_fonts_url(), array(), null );
  113. // Child theme variables
  114. wp_dequeue_style( 'seedlet-custom-color-overrides' );
  115. wp_enqueue_style( 'spearhead-variables-style', get_stylesheet_directory_uri() . '/variables.css', array(), wp_get_theme()->get( 'Version' ) );
  116. wp_enqueue_style( 'seedlet-custom-color-overrides' );
  117. // enqueue child styles
  118. wp_enqueue_style( 'spearhead-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
  119. wp_enqueue_style( 'spearhead-navigation', get_stylesheet_directory_uri() . '/navigation.css', array(), wp_get_theme()->get( 'Version' ) );
  120. // enqueue child RTL styles
  121. wp_style_add_data( 'spearhead-style', 'rtl', 'replace' );
  122. wp_style_add_data( 'spearhead-navigation', 'rtl', 'replace' );
  123. }
  124. add_action( 'wp_enqueue_scripts', 'spearhead_scripts', 11 );
  125. /**
  126. * Enqueue Custom Cover Block Styles and Scripts
  127. */
  128. function spearhead_block_extends() {
  129. // Block Tweaks
  130. wp_enqueue_script(
  131. 'spearhead-block-extends',
  132. get_stylesheet_directory_uri() . '/assets/js/extend-blocks.js',
  133. array( 'wp-blocks', 'wp-edit-post' ) // wp-edit-post is added to avoid a race condition when trying to unregister a style variation
  134. );
  135. }
  136. add_action( 'enqueue_block_assets', 'spearhead_block_extends' );
  137. /**
  138. * Add Google webfonts
  139. *
  140. * @return value
  141. */
  142. function spearhead_fonts_url() {
  143. $fonts_url = '';
  144. $font_families = array();
  145. $font_families[] = 'family=Libre+Franklin:ital,wght@0,400;0,500;0,700;1,400;1,500;1,700';
  146. $font_families[] = 'family=IBM+Plex+Mono:wght@400;700';
  147. $font_families[] = 'display=swap';
  148. // Make a single request for the theme fonts.
  149. $fonts_url = 'https://fonts.googleapis.com/css2?' . implode( '&', $font_families );
  150. return $fonts_url;
  151. }
  152. /**
  153. * Load extras
  154. */
  155. function seedlet_entry_meta_header() {
  156. // Hide author, post date, category and tag text for pages.
  157. if ( 'post' === get_post_type() ) {
  158. // Posted on
  159. seedlet_posted_on();
  160. }
  161. }
  162. // require get_stylesheet_directory() . '/inc/custom-header.php';
  163. /**
  164. * Block Patterns.
  165. */
  166. require get_stylesheet_directory() . '/inc/block-patterns.php';
  167. add_filter(
  168. 'body_class',
  169. function( $classes ) {
  170. $stickies = get_option( 'sticky_posts' );
  171. if ( count( $stickies ) && is_home() ) {
  172. return array_merge( $classes, array( 'has-sticky-post' ) );
  173. }
  174. return $classes;
  175. }
  176. );
  177. /**
  178. * Create the continue reading link.
  179. */
  180. function spearhead_continue_reading_link( $more ) {
  181. if ( ! is_admin() ) {
  182. $more_link = spearhead_more_link();
  183. return '<p>' . $more_link . '</p>';
  184. }
  185. }
  186. /**
  187. * Create a more link for use in both "Read more" and excerpt contexts.
  188. */
  189. function spearhead_more_link() {
  190. $more_text = sprintf(
  191. /* translators: %s: Name of current post. */
  192. wp_kses( __( 'More', 'spearhead' ), array( 'span' => array( 'class' => array() ) ) ),
  193. the_title( '<span class="screen-reader-text">"', '"</span>', false )
  194. );
  195. return '<a class="more-link" href="' . esc_url( get_permalink() ) . '">' . $more_text . ' ' . seedlet_get_icon_svg( 'dropdown' ) . '</a>';
  196. }
  197. /**
  198. * Use this instead of the default WordPress ellipsis which is […].
  199. */
  200. function spearhead_excerpt_more( $more ) {
  201. if ( is_admin() ) {
  202. return $more;
  203. }
  204. return '&hellip;';
  205. }
  206. function spearhead_the_excerpt( $excerpt ) {
  207. $audio_block = '';
  208. if ( has_block( 'audio' ) ) {
  209. $post = get_post();
  210. $blocks = parse_blocks( $post->post_content );
  211. foreach ( $blocks as $block ) {
  212. if ( 'core/audio' === $block['blockName'] ) {
  213. $audio_block .= '<div class="excerpt-audio-block">' . wp_kses_post( $block['innerHTML'] ) . '</div>';
  214. break;
  215. }
  216. }
  217. }
  218. // For cases where the post excerpt is empty
  219. // (but the post might have content)
  220. if ( 0 === strlen( $excerpt ) ) {
  221. return $excerpt . $audio_block;
  222. }
  223. return $excerpt . '<span class="excerpt-more-link">' . spearhead_more_link() . '</span>' . $audio_block;
  224. }
  225. /**
  226. * Overwrite Seedlet's post navigation template tag.
  227. */
  228. if ( ! function_exists( 'seedlet_the_post_navigation' ) ) :
  229. function seedlet_the_post_navigation() {
  230. return null;
  231. }
  232. endif;
  233. // Filter the excerpt more link
  234. add_filter( 'excerpt_more', 'spearhead_excerpt_more' );
  235. // Filter the content more link
  236. add_filter( 'the_content_more_link', 'spearhead_continue_reading_link' );
  237. // Filter the excerpt
  238. add_filter( 'get_the_excerpt', 'spearhead_the_excerpt' );
  239. /*
  240. * Post footer meta
  241. */
  242. if ( ! function_exists( 'seedlet_entry_meta_footer' ) ) :
  243. /**
  244. * Prints HTML with meta information for the categories, tags and comments.
  245. */
  246. function seedlet_entry_meta_footer() {
  247. seedlet_posted_on();
  248. // Edit post link.
  249. edit_post_link(
  250. sprintf(
  251. wp_kses(
  252. /* translators: %s: Name of current post. Only visible to screen readers. */
  253. __( 'Edit <span class="screen-reader-text">%s</span>', 'spearhead' ),
  254. array(
  255. 'span' => array(
  256. 'class' => array(),
  257. ),
  258. )
  259. ),
  260. get_the_title()
  261. ),
  262. '<span class="edit-link">',
  263. '</span>'
  264. );
  265. }
  266. endif;
  267. if ( ! function_exists( 'seedlet_posted_on' ) ) :
  268. /**
  269. * Prints HTML with meta information for the current post-date/time.
  270. */
  271. function seedlet_posted_on() {
  272. $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
  273. if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
  274. $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
  275. }
  276. $time_string = sprintf(
  277. $time_string,
  278. esc_attr( get_the_date( DATE_W3C ) ),
  279. esc_html( get_the_date( 'M d Y' ) ),
  280. esc_attr( get_the_modified_date( DATE_W3C ) ),
  281. esc_html( get_the_modified_date() )
  282. );
  283. printf(
  284. '<span class="posted-on"><a href="%1$s" rel="bookmark">%2$s</a></span>',
  285. esc_url( get_permalink() ),
  286. $time_string
  287. );
  288. }
  289. endif;