Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add body class for theme variations #7432

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions course/assets/css/blue.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
* Styles specific to blue variation
*/
3 changes: 3 additions & 0 deletions course/assets/css/dark.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
* Styles specific to dark variation
*/
3 changes: 3 additions & 0 deletions course/assets/css/default.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
* Styles specific to default variation
*/
3 changes: 3 additions & 0 deletions course/assets/css/gold.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
* Styles specific to gold variation
*/
80 changes: 66 additions & 14 deletions course/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,8 @@ function course_support() {
*/
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', [], wp_get_theme()->get( 'Version' ), true );
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' );

/**
* Temporary Hook to skip the learning mode style when the Sensei LMS is able to provide it.
* It is only used to continue loading the deprecated styles if a old sensei version is installed.
*/
$use_deprecated_style = apply_filters( 'course_learning_mode_load_styles', true );

if ( class_exists( 'Sensei_Main' ) && $use_deprecated_style ) {
wp_register_style( 'course-sensei-learning-mode', get_stylesheet_directory_uri() . '/learning-mode.css', array(), wp_get_theme()->get( 'Version' ) );
wp_enqueue_style( 'course-sensei-learning-mode' );
}

}

endif;
Expand All @@ -79,7 +67,7 @@ function course_theme_init() {
function course_register_block_patterns_category() {
register_block_pattern_category(
'sensei-lms',
[ 'label' => 'Sensei LMS' ]
array( 'label' => 'Sensei LMS' )
);
}

Expand All @@ -92,6 +80,10 @@ function course_register_block_patterns_category() {
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.
*
Expand Down Expand Up @@ -119,3 +111,63 @@ function course_theme_filter_single_lesson_template_for_sensei_learning_mode( $p
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' ) );
}
212 changes: 0 additions & 212 deletions course/learning-mode.css

This file was deleted.

3 changes: 2 additions & 1 deletion course/styles/blue.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"normal": "clamp(1rem, 0.911rem + 0.238vw, 1.125rem)",
"button": "clamp(1rem, 0.911rem + 0.238vw, 1.125rem)"
}
}
},
"courseThemeVariation": "blue"
},
"typography": {
"fluid": true,
Expand Down
3 changes: 2 additions & 1 deletion course/styles/dark.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"button": "clamp(0.875rem, 0.875rem + ((1vw - 0.48rem) * 0.481), 1.125rem)",
"fixed": "1.125rem"
}
}
},
"courseThemeVariation": "dark"
},
"typography": {
"fluid": true,
Expand Down
3 changes: 2 additions & 1 deletion course/styles/gold.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
"normal": "1.125rem",
"button": "1rem"
}
}
},
"courseThemeVariation": "gold"
},
"typography": {
"fluid": true,
Expand Down
1 change: 1 addition & 0 deletions course/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@
"commentGapSmall" : "10px",
"commentGapMedium" : "1.25rem",
"courseNewsletterGap": "clamp(2.5rem, -0.292rem + 9.306vw, 6.688rem)",
"courseThemeVariation": "default",
"button": {
"radius": "0.5rem",
"typography": {
Expand Down
Loading