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

Make theme variation body class available in editor screens #7465

Merged
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
17 changes: 14 additions & 3 deletions course/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ function course_register_block_patterns_category() {

add_filter( 'body_class', 'add_body_class_for_variation' );

add_filter( 'admin_body_class', 'add_body_class_for_variation' );

add_action( 'course_theme_variation_loaded', 'enqueue_style_for_variation' );

/**
Expand Down Expand Up @@ -113,16 +115,19 @@ function course_theme_filter_single_lesson_template_for_sensei_learning_mode( $p

/**
* Add a body class with the variation.
* body_class hook passes classes as array whereas
* admin_body_class passes them as string. So we handle both.
*
* @param array $classes Body classes.
* @param array|string $classes Body classes.
*
* @internal
*
* @return array Body classes.
* @return array|string Body classes.
*/
function add_body_class_for_variation( $classes ) {
$css_string = wp_get_global_stylesheet( array( 'variables' ) );
$property_name = '--wp--custom--course-theme-variation';
$is_array = is_array( $classes );

// 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.
Expand All @@ -140,8 +145,14 @@ function add_body_class_for_variation( $classes ) {
// $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;
$class_value = 'is-' . $css_value;

if ( $is_array ) {
$classes[] = $class_value;
} else {
$classes .= ' ' . $class_value;
}
}

/**
Expand Down
Loading