functions.php 8.6 KB

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