themes-wordpress/course/functions.php
Donna Peplinskie dafa06d928
Course Release 1.3.5 (#7451)
* Update styles of question feedback (#7373)

* Specify the grade style to be applied only in the question header

* Update background color for gold variation

* Update background color for default variation

* Update border color for gold variation

* Tweak CSS formatting

* Tweak color for border of ordering question

---------

Co-authored-by: Donna Peplinskie <donnapep@gmail.com>

* Update version and description

* Add new changelog

* Remove legacy Learning Mode CSS as it's part of Sensei now (#7431)

* Course: Add body class for theme variations (#7432)

* Add custom property with variation name

* Add function to add body class and enqueue proper stylesheets

* Add stylesheets for all variations

* Add version number

* Style the "Awaiting Grade" notice (#7401)

* Style "Awaiting Grade" notice

* Move custom CSS to other areas of theme.json

* Remove typography setting as it needs to be done via CSS

* Do the same for the variations

* Fix notice style getting broken for css parsing issue

---------

Co-authored-by: Imran Hossain <imranh920@gmail.com>

* Bump versions and update change log

---------

Co-authored-by: Imran Hossain <imranh920@gmail.com>
2023-11-01 15:17:51 -03:00

173 lines
4.9 KiB
PHP

<?php
/**
* Course functions and definitions
*
* @link https://developer.wordpress.org/themes/basics/theme-functions/
*
* @package Course
* @since Course 1.0
*/
if ( ! function_exists( 'course_support' ) ) :
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* @since Course 1.0
*
* @return void
*/
function course_support() {
add_theme_support( 'sensei-learning-mode' );
// Enqueue editor styles.
add_editor_style( 'style.css' );
}
endif;
add_action( 'after_setup_theme', 'course_support' );
if ( ! function_exists( 'course_scripts' ) ) :
/**
* Enqueue scripts and styles.
*
* @since Course 1.0
*
* @return void
*/
function course_scripts() {
wp_register_style( 'course-style', get_stylesheet_directory_uri() . '/style.css', array(), wp_get_theme()->get( 'Version' ) );
wp_enqueue_script( 'course-header', get_template_directory_uri() . '/assets/js/header.js', array(), wp_get_theme()->get( 'Version' ), true );
wp_enqueue_style( 'course-style' );
}
endif;
add_action( 'wp_enqueue_scripts', 'course_scripts' );
function course_theme_init() {
register_block_style(
'core/navigation-link',
array(
'name' => 'navigation-link-button',
'label' => __( 'Button', 'course' ),
)
);
}
add_action( 'init', 'course_theme_init' );
/**
* Register Sensei LMS block patterns category.
*/
function course_register_block_patterns_category() {
register_block_pattern_category(
'sensei-lms',
array( 'label' => 'Sensei LMS' )
);
}
add_action( 'init', 'course_register_block_patterns_category' );
add_filter(
'theme_lesson_templates',
'course_theme_filter_single_lesson_template_for_sensei_learning_mode',
11,
3
);
add_filter( 'body_class', 'add_body_class_for_variation' );
add_action( 'course_theme_variation_loaded', 'enqueue_style_for_variation' );
/**
* Filter the list of templates for the single lesson page.
*
* @param array $page_templates Array of page templates.
* @param string $theme The current theme.
* @param WP_Post $post The post being edited, provided for context, or null.
*
* @since Course 1.3.2
*
* @return array Array of page templates.
*/
function course_theme_filter_single_lesson_template_for_sensei_learning_mode( $page_templates, $theme, $post ) {
// In case some other plugin has a post type called lesson.
if ( ! $post || ! class_exists( 'Sensei_Main' ) ) {
return $page_templates;
}
$course_id = Sensei()->lesson->get_course_id( $post->ID );
$is_learning_mode_enabled = Sensei_Course_Theme_Option::has_learning_mode_enabled( $course_id );
if ( $is_learning_mode_enabled ) {
unset( $page_templates['single-lesson'] );
}
return $page_templates;
}
/**
* Add a body class with the variation.
*
* @param array $classes Body classes.
*
* @internal
*
* @return array Body classes.
*/
function add_body_class_for_variation( $classes ) {
$css_string = wp_get_global_stylesheet( array( 'variables' ) );
$property_name = '--wp--custom--course-theme-variation';
// 1. "/": Delimiters that mark the start and end of the regex pattern.
// 2. "$property_name": This part of the pattern matches the specific property name, in our case, '--wp--custom--course-theme-variation', defined in Course theme's JSON files.
// 3. "\s*": Matches zero or more whitespace characters.
// 4. ":": Matches the colon you write to separate the CSS property name and property value.
// 5. "\s*": Matches zero or more whitespace characters after the colon.
// 6. "([^;]+)": This is a capturing group that matches one or more characters that are not a semicolon. It captures the value of the property.
// 7. "/": The closing delimiter of the regex pattern.
// Overall, this regex is designed to extract the value associated with a specific CSS property (defined in $property_name).
$pattern = "/$property_name\s*:\s*([^;]+)/";
$variation_name = 'default';
if ( preg_match( $pattern, $css_string, $matches ) ) {
// $matches[0] contains the full match.
// $matches[1] contains the CSS value for the specified property.
$css_value = trim( $matches[1] );
$classes[] = 'is-' . $css_value;
$variation_name = $css_value;
}
/**
* Action to perform variation specific tasks.
*
* @hook course_theme_variation_loaded Fires after determining which theme variation is loaded.
* @since 1.3.5
*
* @param string $variation_name Name of the variation.
*/
do_action( 'course_theme_variation_loaded', $variation_name );
return $classes;
}
/**
* Enqueue the specific stylesheet for the variation.
*
* @param string $variation_name The current theme variation.
*
* @since Course 1.3.5
*/
function enqueue_style_for_variation( $variation_name ) {
if ( empty( $variation_name ) ) {
return;
}
wp_enqueue_style( 'course-theme-variation-style', get_template_directory_uri() . '/assets/css/' . $variation_name . '.css', array(), wp_get_theme()->get( 'Version' ) );
}