diff --git a/changelog/add-body-class-for-course-theme-variation b/changelog/add-body-class-for-course-theme-variation new file mode 100644 index 0000000000..df958faafd --- /dev/null +++ b/changelog/add-body-class-for-course-theme-variation @@ -0,0 +1,4 @@ +Significance: minor +Type: added + +Body class to indicate Course theme variation diff --git a/includes/class-sensei.php b/includes/class-sensei.php index bf119472bd..cf54f0de4a 100644 --- a/includes/class-sensei.php +++ b/includes/class-sensei.php @@ -741,6 +741,8 @@ public function load_hooks() { add_action( 'body_class', array( $this, 'body_class' ) ); + add_filter( 'body_class', array( $this, 'maybe_add_course_theme_variation_class' ) ); + // Check for and activate JetPack LaTeX support add_action( 'plugins_loaded', array( $this, 'jetpack_latex_support' ), 200 ); // Runs after Jetpack has loaded it's modules @@ -1411,6 +1413,48 @@ public function body_class( $classes ) { return $classes; } + /** + * For course theme, add a body class with the variation. + * + * @param array $classes Body classes. + * + * @internal + * + * @return array Body classes. + */ + public function maybe_add_course_theme_variation_class( $classes ) { + $theme = wp_get_theme(); + + $is_course_theme = 'course' === strtolower( $theme->get_template() ) || + 'course' === strtolower( $theme['Name'] ?? '' ); + + if ( ! $is_course_theme ) { + return $classes; + } + + $css_string = wp_get_global_stylesheet( [ '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*([^;]+)/"; + + 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[] = 'course-theme-variation-' . $css_value; + } + + return $classes; + } + /** * Checks that the Jetpack Beautiful Maths module has been activated * to support LaTeX within question titles and answers