themes-wordpress/spearhead/functions.php

348 lines
9.3 KiB
PHP
Raw Permalink Normal View History

2020-08-04 21:44:03 +00:00
<?php
/**
* Child Theme Functions and definitions
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package WordPress
* @subpackage Spearhead
2020-08-04 21:44:03 +00:00
* @since 1.0.0
*/
if ( ! function_exists( 'spearhead_setup' ) ) :
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which
* runs before the init hook. The init hook is too late for some features, such
* as indicating support for post thumbnails.
*/
function spearhead_setup() {
// Add support for editor styles.
2020-10-05 09:56:34 +00:00
add_theme_support( 'editor-styles' );
2020-08-04 21:44:03 +00:00
// Enqueue editor styles.
add_editor_style(
array(
spearhead_fonts_url(),
'variables.css',
'style.css',
)
);
2020-08-04 21:44:03 +00:00
// Add child theme editor font sizes to match Sass-map variables in `_config-child-theme-deep.scss`.
add_theme_support(
'editor-font-sizes',
array(
array(
'name' => __( 'Tiny', 'spearhead' ),
'shortName' => __( 'XS', 'spearhead' ),
'size' => 14,
'slug' => 'xs',
),
array(
'name' => __( 'Small', 'spearhead' ),
'shortName' => __( 'S', 'spearhead' ),
'size' => 16,
'slug' => 'small',
),
array(
'name' => __( 'Medium', 'spearhead' ),
'shortName' => __( 'M', 'spearhead' ),
'size' => 20,
'slug' => 'medium',
),
array(
'name' => __( 'Large', 'spearhead' ),
'shortName' => __( 'L', 'spearhead' ),
'size' => 24,
'slug' => 'large',
),
array(
'name' => __( 'XL', 'spearhead' ),
'shortName' => __( 'XL', 'spearhead' ),
'size' => 36,
'slug' => 'xl',
),
array(
'name' => __( 'Huge', 'spearhead' ),
'shortName' => __( 'XXL', 'spearhead' ),
'size' => 48,
'slug' => 'huge',
),
)
);
// Add child theme editor color pallete to match Sass-map variables in `_config-child-theme-deep.scss`.
add_theme_support(
'editor-color-palette',
array(
array(
'name' => __( 'Primary', 'spearhead' ),
'slug' => 'primary',
'color' => '#DB0042',
),
array(
'name' => __( 'Foreground', 'spearhead' ),
'slug' => 'foreground',
2020-08-04 21:44:03 +00:00
'color' => '#000000',
),
array(
'name' => __( 'Background', 'spearhead' ),
'slug' => 'background',
'color' => '#FFFFFF',
2020-08-04 21:44:03 +00:00
),
)
);
2021-02-15 11:31:33 +00:00
remove_filter( 'excerpt_more', 'seedlet_continue_reading_link' );
remove_filter( 'the_content_more_link', 'seedlet_continue_reading_link' );
2020-08-04 21:44:03 +00:00
}
endif;
add_action( 'after_setup_theme', 'spearhead_setup', 12 );
2021-02-15 11:31:33 +00:00
/**
* Filter the colors for Speahead
*/
function spearhead_colors() {
return array(
array( '--global--color-background', '#FFFFFF', __( 'Background Color', 'seedlet' ) ),
array( '--global--color-foreground', '#000000', __( 'Foreground Color', 'seedlet' ) ),
array( '--global--color-primary', '#DB0042', __( 'Primary Color', 'seedlet' ) ),
);
}
add_filter( 'seedlet_colors', 'spearhead_colors' );
2020-08-04 21:44:03 +00:00
/**
* Filter the content_width in pixels, based on the child-theme's design and stylesheet.
*/
function spearhead_content_width() {
2020-10-22 12:10:54 +00:00
return 782;
2020-08-04 21:44:03 +00:00
}
add_filter( 'seedlet_content_width', 'spearhead_content_width' );
/**
* Enqueue scripts and styles.
*/
function spearhead_scripts() {
2020-10-08 11:12:15 +00:00
// dequeue parent styles
wp_dequeue_style( 'seedlet-style-navigation' );
2020-08-04 21:44:03 +00:00
// enqueue Google fonts, if necessary
wp_enqueue_style( 'spearhead-fonts', spearhead_fonts_url(), array(), null );
2020-08-04 21:44:03 +00:00
// Child theme variables
wp_dequeue_style( 'seedlet-custom-color-overrides' );
2020-08-04 21:44:03 +00:00
wp_enqueue_style( 'spearhead-variables-style', get_stylesheet_directory_uri() . '/variables.css', array(), wp_get_theme()->get( 'Version' ) );
if ( false === get_theme_mod( 'color_darkmode_disable', false ) ) {
wp_enqueue_style( 'spearhead-variables-dark-style', get_stylesheet_directory_uri() . '/variables-dark.css', array(), wp_get_theme()->get( 'Version' ) );
}
wp_enqueue_style( 'seedlet-custom-color-overrides' );
2020-08-04 21:44:03 +00:00
// enqueue child styles
wp_enqueue_style( 'spearhead-style', get_stylesheet_uri(), array(), wp_get_theme()->get( 'Version' ) );
2020-10-08 11:12:15 +00:00
wp_enqueue_style( 'spearhead-navigation', get_stylesheet_directory_uri() . '/navigation.css', array(), wp_get_theme()->get( 'Version' ) );
2020-08-04 21:44:03 +00:00
// enqueue child RTL styles
wp_style_add_data( 'spearhead-style', 'rtl', 'replace' );
2020-10-08 11:12:15 +00:00
wp_style_add_data( 'spearhead-navigation', 'rtl', 'replace' );
2020-08-04 21:44:03 +00:00
}
add_action( 'wp_enqueue_scripts', 'spearhead_scripts', 11 );
/**
* Enqueue Custom Cover Block Styles and Scripts
*/
function spearhead_block_extends() {
// Block Tweaks
wp_enqueue_script(
'spearhead-block-extends',
2020-08-04 21:44:03 +00:00
get_stylesheet_directory_uri() . '/assets/js/extend-blocks.js',
2020-10-05 09:54:13 +00:00
array( 'wp-blocks', 'wp-edit-post' ) // wp-edit-post is added to avoid a race condition when trying to unregister a style variation
2020-08-04 21:44:03 +00:00
);
}
add_action( 'enqueue_block_assets', 'spearhead_block_extends' );
/**
* Add Google webfonts
*
2020-12-01 18:20:36 +00:00
* @return value
2020-08-04 21:44:03 +00:00
*/
2020-12-01 18:20:36 +00:00
function spearhead_fonts_url() {
$fonts_url = '';
2020-08-04 21:44:03 +00:00
$font_families = array();
$font_families[] = 'family=Libre+Franklin:ital,wght@0,400;0,500;0,700;1,400;1,500;1,700';
$font_families[] = 'family=IBM+Plex+Mono:wght@400;700';
2020-08-04 21:44:03 +00:00
$font_families[] = 'display=swap';
// Make a single request for the theme fonts.
$fonts_url = 'https://fonts.googleapis.com/css2?' . implode( '&', $font_families );
2020-10-05 09:54:13 +00:00
return $fonts_url;
2020-08-04 21:44:03 +00:00
}
/**
* Load extras
*/
2020-12-01 18:20:36 +00:00
function seedlet_entry_meta_header() {
// Hide author, post date, category and tag text for pages.
if ( 'post' === get_post_type() ) {
// Posted on
seedlet_posted_on();
}
}
2020-10-05 09:54:13 +00:00
// require get_stylesheet_directory() . '/inc/custom-header.php';
/**
* Block Patterns.
*/
require get_stylesheet_directory() . '/inc/block-patterns.php';
2020-10-09 12:43:52 +00:00
add_filter(
'body_class',
function( $classes ) {
$stickies = get_option( 'sticky_posts' );
if ( is_countable( $stickies ) && count( $stickies ) && is_home() ) {
2020-10-09 12:43:52 +00:00
return array_merge( $classes, array( 'has-sticky-post' ) );
}
return $classes;
}
);
/**
* Create the continue reading link.
*/
function spearhead_continue_reading_link( $more ) {
if ( ! is_admin() ) {
$more_link = spearhead_more_link();
return '<p>' . $more_link . '</p>';
}
}
/**
* Create a more link for use in both "Read more" and excerpt contexts.
*/
function spearhead_more_link() {
$more_text = sprintf(
/* translators: %s: Name of current post. */
wp_kses( __( 'More', 'spearhead' ), array( 'span' => array( 'class' => array() ) ) ),
the_title( '<span class="screen-reader-text">"', '"</span>', false )
);
return '<a class="more-link" href="' . esc_url( get_permalink() ) . '">' . $more_text . ' ' . seedlet_get_icon_svg( 'dropdown' ) . '</a>';
}
/**
* Use this instead of the default WordPress ellipsis which is [].
*/
2020-12-07 18:21:48 +00:00
function spearhead_excerpt_more( $more ) {
2020-12-03 22:04:58 +00:00
if ( is_admin() ) {
return $more;
}
return '&hellip;';
}
function spearhead_the_excerpt( $excerpt ) {
2020-10-29 10:31:03 +00:00
$audio_block = '';
if ( has_block( 'audio' ) ) {
$post = get_post();
$blocks = parse_blocks( $post->post_content );
foreach ( $blocks as $block ) {
if ( 'core/audio' === $block['blockName'] ) {
2020-10-29 11:19:28 +00:00
$audio_block .= '<div class="excerpt-audio-block">' . wp_kses_post( $block['innerHTML'] ) . '</div>';
2020-10-29 10:31:03 +00:00
break;
}
}
}
// For cases where the post excerpt is empty
// (but the post might have content)
if ( 0 === strlen( $excerpt ) ) {
2020-10-29 10:31:03 +00:00
return $excerpt . $audio_block;
}
2020-10-29 10:31:03 +00:00
return $excerpt . '<span class="excerpt-more-link">' . spearhead_more_link() . '</span>' . $audio_block;
}
/**
* Overwrite Seedlet's post navigation template tag.
*/
if ( ! function_exists( 'seedlet_the_post_navigation' ) ) :
function seedlet_the_post_navigation() {
return null;
}
endif;
// Filter the excerpt more link
add_filter( 'excerpt_more', 'spearhead_excerpt_more' );
// Filter the content more link
add_filter( 'the_content_more_link', 'spearhead_continue_reading_link' );
// Filter the excerpt
add_filter( 'get_the_excerpt', 'spearhead_the_excerpt' );
2020-10-14 10:08:57 +00:00
/*
* Post footer meta
*/
2020-10-13 15:38:47 +00:00
if ( ! function_exists( 'seedlet_entry_meta_footer' ) ) :
/**
* Prints HTML with meta information for the categories, tags and comments.
*/
2020-10-13 15:38:47 +00:00
function seedlet_entry_meta_footer() {
2020-10-13 15:38:47 +00:00
seedlet_posted_on();
// Edit post link.
edit_post_link(
sprintf(
wp_kses(
/* translators: %s: Name of current post. Only visible to screen readers. */
__( 'Edit <span class="screen-reader-text">%s</span>', 'spearhead' ),
array(
'span' => array(
'class' => array(),
),
)
),
get_the_title()
),
'<span class="edit-link">',
'</span>'
);
}
endif;
2020-10-13 15:38:47 +00:00
if ( ! function_exists( 'seedlet_posted_on' ) ) :
/**
* Prints HTML with meta information for the current post-date/time.
*/
2020-10-13 15:38:47 +00:00
function seedlet_posted_on() {
$time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
$time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
}
$time_string = sprintf(
$time_string,
esc_attr( get_the_date( DATE_W3C ) ),
esc_html( get_the_date( 'M d Y' ) ),
esc_attr( get_the_modified_date( DATE_W3C ) ),
esc_html( get_the_modified_date() )
);
printf(
'<span class="posted-on"><a href="%1$s" rel="bookmark">%2$s</a></span>',
esc_url( get_permalink() ),
$time_string
);
}
endif;