Skip to content

Commit

Permalink
Update generatepress-bb-color-palettes.php
Browse files Browse the repository at this point in the history
Updated active theme check and class naming
  • Loading branch information
gbissland authored Oct 18, 2024
1 parent d41707a commit 2b290bf
Showing 1 changed file with 9 additions and 31 deletions.
40 changes: 9 additions & 31 deletions generatepress-bb-color-palettes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: GenPress global colour palette for Beaver Builder colour picker
* Plugin URI: https://github.com/weavedigitalstudio/GeneratePress-BB-Color-Palettes
* Description: A custom plugin to add Beaver Builder color picker compatibility for the GeneratePress Global Color Palette.
* Version: 0.1.0
* Version: 0.1.1
* Primary Branch: main
* GitHub Plugin URI: weavedigitalstudio/GeneratePress-BB-Color-Palettes
* Author: Weave Digital Studio
Expand All @@ -20,74 +20,52 @@
*
* @return bool True if GeneratePress or a child theme of GeneratePress is active, false otherwise.
*/
function is_generatepress_active() {
function generatepress_is_active() {
$theme = wp_get_theme();

// Check if the current theme is GeneratePress or if it is a child theme of GeneratePress.
if ( 'GeneratePress' === $theme->get( 'Name' ) || 'generatepress' === $theme->get_template() ) {
return true;
}

return false;
return ( 'GeneratePress' === $theme->get( 'Name' ) || 'generatepress' === $theme->get_template() );
}

/**
* Generates custom CSS for global color variables compatible with Beaver Builder.
*
* This function generates CSS variables for the GeneratePress global colors with the prefix
* `--wp--preset--color--` and returns the CSS as a string, allowing Beaver Builder
* to recognize and utilise GeneratePress global colors.
*
* @param array $global_colors Array of global color data (slug and color values).
* @return string Generated CSS for the global colors.
*/
function generate_custom_global_colors_css( $global_colors ) {
// Start the custom CSS with a comment and root selector
$custom_css = '/* Beaver Builder Color Compatibility with GeneratePress */' . "\n";
$custom_css .= ':root {';

// Check if there are any global colors to process
if ( ! empty( $global_colors ) ) {
$css_variables = array();
foreach ( (array) $global_colors as $key => $data ) {
// Add each color as a CSS variable with the custom prefix for Beaver Builder
foreach ( (array) $global_colors as $data ) {
$css_variables[] = '--wp--preset--color--' . $data['slug'] . ':' . $data['color'] . ';';
}
$custom_css .= implode( "\n", $css_variables );
}

// Close the root selector
$custom_css .= '}';

// Return the generated CSS string
return $custom_css;
}

/**
* Enqueue custom inline styles for Beaver Builder compatibility.
*
* This function retrieves the global colors from GeneratePress,
* generates custom CSS using the generate_custom_global_colors_css function,
* and enqueues the CSS as inline styles, making the colors available in Beaver Builder.
*/
function generate_enqueue_custom_inline_styles() {
// Check if GeneratePress is active
if ( ! is_generatepress_active() ) {
if ( ! generatepress_is_active() ) {
return; // Exit early if GeneratePress is not the active theme
}

// Get the global colors defined by GeneratePress
$global_colors = generate_get_global_colors();
if ( ! function_exists( 'generate_get_global_colors' ) ) {
return; // Handle the error gracefully
}

// Generate the custom global colors CSS for Beaver Builder
$global_colors = generate_get_global_colors();
$custom_global_colors_css = generate_custom_global_colors_css( $global_colors );

// Check if the 'generate-style' handle exists
if ( wp_style_is( 'generate-style', 'enqueued' ) ) {
// Add the custom CSS as inline styles, attached to the 'generate-style' handle
wp_add_inline_style( 'generate-style', $custom_global_colors_css );
}
}

// Hook the function to enqueue custom styles after the theme styles are loaded
add_action( 'wp_enqueue_scripts', 'generate_enqueue_custom_inline_styles', 20 );

0 comments on commit 2b290bf

Please sign in to comment.