functions.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 colors for Speahead
  100. */
  101. function spearhead_colors() {
  102. return array(
  103. array( '--global--color-background', '#FFFFFF', __( 'Background Color', 'seedlet' ) ),
  104. array( '--global--color-foreground', '#000000', __( 'Foreground Color', 'seedlet' ) ),
  105. array( '--global--color-primary', '#DB0042', __( 'Primary Color', 'seedlet' ) ),
  106. );
  107. }
  108. add_filter( 'seedlet_colors', 'spearhead_colors' );
  109. /**
  110. * Filter the content_width in pixels, based on the child-theme's design and stylesheet.
  111. */
  112. function spearhead_content_width() {
  113. return 782;
  114. }
  115. add_filter( 'seedlet_content_width', 'spearhead_content_width' );
  116. /**
  117. * Enqueue scripts and styles.
  118. */
  119. function spearhead_scripts() {
  120. // dequeue parent styles
  121. wp_dequeue_style( 'seedlet-style-navigation' );
  122. // enqueue Google fonts, if necessary
  123. wp_enqueue_style( 'spearhead-fonts', spearhead_fonts_url(), array(), null );
  124. // Child theme variables
  125. wp_dequeue_style( 'seedlet-custom-color-overrides' );
  126. wp_enqueue_style( 'spearhead-variables-style', get_stylesheet_directory_uri() . '/variables.css', array(), wp_get_theme()->get( 'Version' ) );
  127. if ( false === get_theme_mod( 'color_darkmode_disable', false ) ) {
  128. wp_enqueue_style( 'spearhead-variables-dark-style', get_stylesheet_directory_uri() . '/variables-dark.css', array(), wp_get_theme()->get( 'Version' ) );
  129. }
  130. wp_enqueue_style( 'seedlet-custom-color-overrides' );
  131. // enqueue child styles
  132. wp_enqueue_style( 'spearhead-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
  133. wp_enqueue_style( 'spearhead-navigation', get_stylesheet_directory_uri() . '/navigation.css', array(), wp_get_theme()->get( 'Version' ) );
  134. // enqueue child RTL styles
  135. wp_style_add_data( 'spearhead-style', 'rtl', 'replace' );
  136. wp_style_add_data( 'spearhead-navigation', 'rtl', 'replace' );
  137. }
  138. add_action( 'wp_enqueue_scripts', 'spearhead_scripts', 11 );
  139. /**
  140. * Enqueue Custom Cover Block Styles and Scripts
  141. */
  142. function spearhead_block_extends() {
  143. // Block Tweaks
  144. wp_enqueue_script(
  145. 'spearhead-block-extends',
  146. get_stylesheet_directory_uri() . '/assets/js/extend-blocks.js',
  147. array( 'wp-blocks', 'wp-edit-post' ) // wp-edit-post is added to avoid a race condition when trying to unregister a style variation
  148. );
  149. }
  150. add_action( 'enqueue_block_assets', 'spearhead_block_extends' );
  151. /**
  152. * Add Google webfonts
  153. *
  154. * @return value
  155. */
  156. function spearhead_fonts_url() {
  157. $fonts_url = '';
  158. $font_families = array();
  159. $font_families[] = 'family=Libre+Franklin:ital,wght@0,400;0,500;0,700;1,400;1,500;1,700';
  160. $font_families[] = 'family=IBM+Plex+Mono:wght@400;700';
  161. $font_families[] = 'display=swap';
  162. // Make a single request for the theme fonts.
  163. $fonts_url = 'https://fonts.googleapis.com/css2?' . implode( '&', $font_families );
  164. return $fonts_url;
  165. }
  166. /**
  167. * Load extras
  168. */
  169. function seedlet_entry_meta_header() {
  170. // Hide author, post date, category and tag text for pages.
  171. if ( 'post' === get_post_type() ) {
  172. // Posted on
  173. seedlet_posted_on();
  174. }
  175. }
  176. // require get_stylesheet_directory() . '/inc/custom-header.php';
  177. /**
  178. * Block Patterns.
  179. */
  180. require get_stylesheet_directory() . '/inc/block-patterns.php';
  181. add_filter(
  182. 'body_class',
  183. function( $classes ) {
  184. $stickies = get_option( 'sticky_posts' );
  185. if ( count( $stickies ) && is_home() ) {
  186. return array_merge( $classes, array( 'has-sticky-post' ) );
  187. }
  188. return $classes;
  189. }
  190. );
  191. /**
  192. * Create the continue reading link.
  193. */
  194. function spearhead_continue_reading_link( $more ) {
  195. if ( ! is_admin() ) {
  196. $more_link = spearhead_more_link();
  197. return '<p>' . $more_link . '</p>';
  198. }
  199. }
  200. /**
  201. * Create a more link for use in both "Read more" and excerpt contexts.
  202. */
  203. function spearhead_more_link() {
  204. $more_text = sprintf(
  205. /* translators: %s: Name of current post. */
  206. wp_kses( __( 'More', 'spearhead' ), array( 'span' => array( 'class' => array() ) ) ),
  207. the_title( '<span class="screen-reader-text">"', '"</span>', false )
  208. );
  209. return '<a class="more-link" href="' . esc_url( get_permalink() ) . '">' . $more_text . ' ' . seedlet_get_icon_svg( 'dropdown' ) . '</a>';
  210. }
  211. /**
  212. * Use this instead of the default WordPress ellipsis which is […].
  213. */
  214. function spearhead_excerpt_more( $more ) {
  215. if ( is_admin() ) {
  216. return $more;
  217. }
  218. return '&hellip;';
  219. }
  220. function spearhead_the_excerpt( $excerpt ) {
  221. $audio_block = '';
  222. if ( has_block( 'audio' ) ) {
  223. $post = get_post();
  224. $blocks = parse_blocks( $post->post_content );
  225. foreach ( $blocks as $block ) {
  226. if ( 'core/audio' === $block['blockName'] ) {
  227. $audio_block .= '<div class="excerpt-audio-block">' . wp_kses_post( $block['innerHTML'] ) . '</div>';
  228. break;
  229. }
  230. }
  231. }
  232. // For cases where the post excerpt is empty
  233. // (but the post might have content)
  234. if ( 0 === strlen( $excerpt ) ) {
  235. return $excerpt . $audio_block;
  236. }
  237. return $excerpt . '<span class="excerpt-more-link">' . spearhead_more_link() . '</span>' . $audio_block;
  238. }
  239. /**
  240. * Overwrite Seedlet's post navigation template tag.
  241. */
  242. if ( ! function_exists( 'seedlet_the_post_navigation' ) ) :
  243. function seedlet_the_post_navigation() {
  244. return null;
  245. }
  246. endif;
  247. // Filter the excerpt more link
  248. add_filter( 'excerpt_more', 'spearhead_excerpt_more' );
  249. // Filter the content more link
  250. add_filter( 'the_content_more_link', 'spearhead_continue_reading_link' );
  251. // Filter the excerpt
  252. add_filter( 'get_the_excerpt', 'spearhead_the_excerpt' );
  253. /*
  254. * Post footer meta
  255. */
  256. if ( ! function_exists( 'seedlet_entry_meta_footer' ) ) :
  257. /**
  258. * Prints HTML with meta information for the categories, tags and comments.
  259. */
  260. function seedlet_entry_meta_footer() {
  261. seedlet_posted_on();
  262. // Edit post link.
  263. edit_post_link(
  264. sprintf(
  265. wp_kses(
  266. /* translators: %s: Name of current post. Only visible to screen readers. */
  267. __( 'Edit <span class="screen-reader-text">%s</span>', 'spearhead' ),
  268. array(
  269. 'span' => array(
  270. 'class' => array(),
  271. ),
  272. )
  273. ),
  274. get_the_title()
  275. ),
  276. '<span class="edit-link">',
  277. '</span>'
  278. );
  279. }
  280. endif;
  281. if ( ! function_exists( 'seedlet_posted_on' ) ) :
  282. /**
  283. * Prints HTML with meta information for the current post-date/time.
  284. */
  285. function seedlet_posted_on() {
  286. $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
  287. if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
  288. $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
  289. }
  290. $time_string = sprintf(
  291. $time_string,
  292. esc_attr( get_the_date( DATE_W3C ) ),
  293. esc_html( get_the_date( 'M d Y' ) ),
  294. esc_attr( get_the_modified_date( DATE_W3C ) ),
  295. esc_html( get_the_modified_date() )
  296. );
  297. printf(
  298. '<span class="posted-on"><a href="%1$s" rel="bookmark">%2$s</a></span>',
  299. esc_url( get_permalink() ),
  300. $time_string
  301. );
  302. }
  303. endif;