From 679c457931a83ac13236408882749897bdd5e740 Mon Sep 17 00:00:00 2001 From: Dmitry Merkushin Date: Tue, 2 May 2023 22:24:26 +0400 Subject: [PATCH] Add body class for the theme variation (#7034) --- course/functions.php | 49 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/course/functions.php b/course/functions.php index a63fa01df0..0589ff48af 100644 --- a/course/functions.php +++ b/course/functions.php @@ -75,6 +75,8 @@ function course_scripts() { add_action( 'wp_enqueue_scripts', 'course_scripts' ); function course_theme_init() { + add_option( 'course_theme_variation', 'default' ); + register_block_style( 'core/navigation-link', array( @@ -96,4 +98,49 @@ function course_register_block_patterns_category() { ); } -add_action( 'init', 'course_register_block_patterns_category' ); \ No newline at end of file +add_action( 'init', 'course_register_block_patterns_category' ); + +/** + * Determine the theme variation and save in option. + * + * @param int $post_id Post ID. + * @param WP_Post $post Post object. + * @param bool $update Whether this is an existing post being updated or not. + */ +function course_save_global_styles( $post_id, $post, $update ) { + if ( 'wp_global_styles' !== $post->post_type ) { + return; + } + + $global_styles = json_decode( $post->post_content, true ); + $global['settings'] = isset( $global_styles['settings'] ) ? $global_styles['settings'] : array(); + $global['styles'] = isset( $global_styles['styles'] ) ? $global_styles['styles'] : array(); + $variations = WP_Theme_JSON_Resolver::get_style_variations(); + $current_variation = 'default'; + foreach ( $variations as $variation ) { + if ( $variation['settings'] === $global['settings'] && $variation['styles'] === $global['styles'] ) { + $current_variation = sanitize_title( $variation['title'] ); + } + } + update_option( 'course_theme_variation', $current_variation ); +} + +add_action( 'save_post', 'course_save_global_styles', 10, 3 ); + +/** + * Add the theme variation to the body class. + * + * @param array $classes Array of body classes. + */ +function course_add_variation_body_class( $classes ) { + $current_variation = get_option( 'course_theme_variation' ); + if ( ! $current_variation ) { + return $classes; + } + + $classes[] = 'is-' . $current_variation; + + return $classes; +} + +add_action( 'body_class', 'course_add_variation_body_class' );