From 03283ed52049b620746bcb82580fa4665d32b3db Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Tue, 17 Oct 2023 22:03:38 +0600 Subject: [PATCH 1/7] Add function to parse property name and add to body class --- includes/class-sensei.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/includes/class-sensei.php b/includes/class-sensei.php index 0d64fb3abb..4571c58427 100644 --- a/includes/class-sensei.php +++ b/includes/class-sensei.php @@ -735,6 +735,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 @@ -1405,6 +1407,41 @@ public function body_class( $classes ) { return $classes; } + /** + * For course theme, add a body class with the variation. + * + * @param array $classes Body classes. + * + * @intenal + * + * @return array Body classes. + */ + public function maybe_add_course_theme_variation_class( $classes ) { + + $is_course_theme = 'course' === wp_get_theme()->get_template(); + + if ( ! $is_course_theme ) { + return $classes; + } + + if ( ! is_array( $classes ) ) { + $classes = []; + } + + $css_string = wp_get_global_stylesheet( [ 'variables' ] ) ?? ''; + $property_name = '--wp--custom--course-theme-variation'; + $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 From 0660b7079633df1edce1ae5afa212a6d9ab23bca Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Tue, 17 Oct 2023 22:23:49 +0600 Subject: [PATCH 2/7] Add changelog --- changelog/add-body-class-for-course-theme-variation | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/add-body-class-for-course-theme-variation 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 From ff1511b2fe39f47683dd65458b478df4dd94b0c0 Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Fri, 20 Oct 2023 06:52:34 +0600 Subject: [PATCH 3/7] Remove unnecessary check --- includes/class-sensei.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/includes/class-sensei.php b/includes/class-sensei.php index 4571c58427..a538f3f1fe 100644 --- a/includes/class-sensei.php +++ b/includes/class-sensei.php @@ -1424,10 +1424,6 @@ public function maybe_add_course_theme_variation_class( $classes ) { return $classes; } - if ( ! is_array( $classes ) ) { - $classes = []; - } - $css_string = wp_get_global_stylesheet( [ 'variables' ] ) ?? ''; $property_name = '--wp--custom--course-theme-variation'; $pattern = "/$property_name\s*:\s*([^;]+)/"; From 769f18a08cddeccaa7e318903395ef87b64d4827 Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Fri, 20 Oct 2023 06:53:22 +0600 Subject: [PATCH 4/7] Fix typo --- includes/class-sensei.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-sensei.php b/includes/class-sensei.php index a538f3f1fe..5f735bd8d2 100644 --- a/includes/class-sensei.php +++ b/includes/class-sensei.php @@ -1412,7 +1412,7 @@ public function body_class( $classes ) { * * @param array $classes Body classes. * - * @intenal + * @internal * * @return array Body classes. */ From 4bc2370e064916b71a823f9ff74ed4deb30dbca3 Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Fri, 20 Oct 2023 06:54:35 +0600 Subject: [PATCH 5/7] Fix psalm issue --- includes/class-sensei.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-sensei.php b/includes/class-sensei.php index 5f735bd8d2..e0aaed3646 100644 --- a/includes/class-sensei.php +++ b/includes/class-sensei.php @@ -1424,7 +1424,7 @@ public function maybe_add_course_theme_variation_class( $classes ) { return $classes; } - $css_string = wp_get_global_stylesheet( [ 'variables' ] ) ?? ''; + $css_string = wp_get_global_stylesheet( [ 'variables' ] ); $property_name = '--wp--custom--course-theme-variation'; $pattern = "/$property_name\s*:\s*([^;]+)/"; From f45eebc1c2b42b455a543bb4d3ec4a027d460898 Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Fri, 20 Oct 2023 07:22:50 +0600 Subject: [PATCH 6/7] Add additional check to makes sure we detect course theme --- includes/class-sensei.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/includes/class-sensei.php b/includes/class-sensei.php index e0aaed3646..817cf1b86a 100644 --- a/includes/class-sensei.php +++ b/includes/class-sensei.php @@ -1417,8 +1417,10 @@ public function body_class( $classes ) { * @return array Body classes. */ public function maybe_add_course_theme_variation_class( $classes ) { + $theme = wp_get_theme(); - $is_course_theme = 'course' === wp_get_theme()->get_template(); + $is_course_theme = 'course' === strtolower( $theme->get_template() ) || + 'course' === strtolower( $theme['Name'] ?? '' ); if ( ! $is_course_theme ) { return $classes; From a2ba4b9784df66bdb9404387114529dc4ac31810 Mon Sep 17 00:00:00 2001 From: Imran Hossain Date: Fri, 20 Oct 2023 21:56:09 +0600 Subject: [PATCH 7/7] Add comment explaining the Regex --- includes/class-sensei.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/includes/class-sensei.php b/includes/class-sensei.php index 08acad41b2..1c41a5c7d7 100644 --- a/includes/class-sensei.php +++ b/includes/class-sensei.php @@ -1428,11 +1428,20 @@ public function maybe_add_course_theme_variation_class( $classes ) { $css_string = wp_get_global_stylesheet( [ 'variables' ] ); $property_name = '--wp--custom--course-theme-variation'; - $pattern = "/$property_name\s*:\s*([^;]+)/"; + + // 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 + // $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; }