Skip to content
This repository has been archived by the owner on Feb 17, 2025. It is now read-only.

Blockbase: Add support for custom palettes customization #4098

Merged
merged 23 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
933aa2c
first pass at color palette controls without functionality
MaggieCabrera Jun 25, 2021
284b68e
made palettes look like color annotations plugin
MaggieCabrera Jun 25, 2021
2f66ce8
make color selection functional
scruffian Jun 25, 2021
47c697c
get palettes from theme.json
scruffian Jun 25, 2021
50ac20e
rename file
scruffian Jun 25, 2021
dd15473
rename colors preview file
scruffian Jun 25, 2021
d55abbd
rename files
scruffian Jun 25, 2021
753e1ef
more file renaming for clarity
scruffian Jun 25, 2021
ec413a7
simplify settings
scruffian Jun 25, 2021
049c68f
rebuilt mayland and seedlet with alternative palettes
MaggieCabrera Jun 25, 2021
8bf752c
renamed default template, dynamic theme name
MaggieCabrera Jun 25, 2021
e73bf28
remove space
scruffian Jun 25, 2021
ebf38dc
Added default Varia/Seedlet palettes to blockbase
MaggieCabrera Jun 25, 2021
a61c6ac
check if the custom palette array is empty
MaggieCabrera Jun 25, 2021
e0e9522
Only render color palette picker controls if there are alternatives t…
pbking Jun 25, 2021
5808c73
Only render color palette picker controls if there are alternatives t…
pbking Jun 25, 2021
555c9b1
Removed commented code
pbking Jun 25, 2021
ddc7b0b
change palettes to an array
scruffian Jun 28, 2021
df3dcb0
chagne props comment
scruffian Jun 28, 2021
842dc5d
removed unnecesary credit
MaggieCabrera Jun 28, 2021
28f033b
undo last commit, brought back the credits
MaggieCabrera Jun 28, 2021
2a80ac9
readd the settings file
scruffian Jun 28, 2021
5d328b8
rebuild children
scruffian Jun 28, 2021
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
3 changes: 2 additions & 1 deletion blockbase/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ function blockbase_fonts_url() {
/**
* Customize Global Styles
*/
require get_template_directory() . '/inc/customization.php';
require get_template_directory() . '/inc/wp-customize-colors.php';
require get_template_directory() . '/inc/wp-customize-color-palettes.php';
48 changes: 48 additions & 0 deletions blockbase/inc/color-customization.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
.customize-control-color-palette .color-palette-group {
display: grid;
flex-wrap: wrap;
grid-template-columns: 1fr 1fr 1fr;
column-gap: 15px;
row-gap: 15px;
}

.customize-control-color-palette .color-palette-group input {
display: none;
}

.customize-control-color-palette .color-palette-group .color-option {
padding: 0;
}

.customize-control-color-palette input:checked + .color-option .custom-color-palette {
border-color: #2271b1;
}

.custom-color-palette {
position: relative;
display: flex;
border-radius: 2px;
border: 1px solid rgba(0,0,0,0.1);
height: 50px;
width: 72px;
overflow: hidden;
}

.color-palette-label {
background: #000;
color: #fff;
font-size: 10px;
opacity: 0.7;
position: absolute;
bottom: 5px;
right: 0;
left: 0;
text-indent: 5px;
line-height: 1.5;
padding-bottom: 1px;
}

.color-stripe {
flex: 1 1 auto;
border: 1px solid rgba(0,0,0,0.1);
}
11 changes: 11 additions & 0 deletions blockbase/inc/color-palettes-controls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
wp.customize( 'color_palette', ( value ) => {
value.bind( ( newValue ) => {
const newPalette = colorPalettes[ newValue ].colors;
Object.keys( newPalette ).forEach( function ( slug ) {
const color = newPalette[ slug ];
wp.customize
.control( 'customize-global-styles' + slug )
.setting.set( color );
} );
} );
} );
84 changes: 84 additions & 0 deletions blockbase/inc/wp-customize-color-palettes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

require_once 'wp-customize-palette-control.php';

class GlobalStylesColorPalettes {
private $palettes = array();

function __construct() {
add_action( 'customize_register', array( $this, 'color_palette_control' ) );
add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_preview_js' ) );
}

function customize_preview_js() {
wp_enqueue_script( 'customizer-color-palettes', get_template_directory_uri() . '/inc/color-palettes-controls.js', array( 'customize-controls' ) );
wp_localize_script( 'customizer-color-palettes', 'colorPalettes', $this->palettes );
}

function build_palettes( $theme_json ) {

$default_palette = $theme_json['settings']['color']['palette']['theme'];

$default_palette_setting = array();
foreach ( $default_palette as $default_color ) {
$default_palette_setting[ $default_color['slug'] ] = $default_color['color'];
}

$this->palettes['default-palette'] = array(
'label' => 'Default',
'colors' => $default_palette_setting,
);

$custom_palettes = $theme_json['settings']['custom']['colorPalettes'];
Copy link
Contributor

@pbking pbking Jun 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm getting an error here w/ Quadrat due to there being no palette's currently defined in theme.json
Likely the Quadrat palette options weren't included in the change (yet).
But seems reasonable that a BB child might not ship with any customizations so probably a relevant use-case.

Notice: Undefined index: colorPalettes in /var/www/html/wp-content/themes/themes/blockbase/inc/wp-customize-color-palettes.php on line 33 Warning: Invalid argument supplied for foreach() in /var/www/html/wp-content/themes/themes/blockbase/inc/wp-customize-color-palettes.php on line 34

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that fixed disappeared on a conflict, let me get it back

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be fine now

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed this small change to only render these controls if there are alternatives to default to show. Also addressed that error that seemed to stick around despite the recent change.

if ( ! empty( $custom_palettes ) ) {
foreach ( $custom_palettes as $custom_palette ) {
$custom_palette_setting = array();
foreach ( $custom_palette['colors'] as $color_slug => $color ) {
$custom_palette_setting[ $color_slug ] = $color;
}

$this->palettes[ $custom_palette['slug'] ] = array(
'label' => $custom_palette['label'],
'colors' => $custom_palette_setting,
);
}
}
}

function color_palette_control( $wp_customize ) {

$theme_json = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_raw_data();

if ( ! isset( $theme_json['settings']['custom']['colorPalettes'] ) ) {
return;
}

$this->build_palettes( $theme_json );

$wp_customize->add_setting(
'color_palette',
array(
'default' => 'default-palette',
'capability' => 'edit_theme_options',
'transport' => 'postMessage', // We need this to stop the page refreshing.
)
);

$wp_customize->add_control(
new Color_Palette_Control(
$wp_customize,
'color_palette',
array(
'label' => __( 'Color Scheme', 'blockbase' ),
'description' => __( 'Choose a color scheme for your website.', 'blockbase' ),
'section' => 'customize-global-styles',
'choices' => $this->palettes,
'settings' => 'color_palette',
)
)
);

}
}

new GlobalStylesColorPalettes;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

require_once 'wp-customize-global-styles-setting.php';
require_once 'wp-customize-color-settings.php';

class GlobalStylesCustomizer {
class GlobalStylesColorCustomizer {

private $user_color_palette;

Expand All @@ -14,7 +14,7 @@ function __construct() {
}

function customize_preview_js() {
wp_enqueue_script( 'customizer-preview-color', get_template_directory_uri() . '/inc/customizer-preview.js', array( 'customize-preview' ) );
wp_enqueue_script( 'customizer-preview-color', get_template_directory_uri() . '/inc/customize-colors-preview.js', array( 'customize-preview' ) );
wp_localize_script( 'customizer-preview-color', 'userColorPalette', $this->user_color_palette );
}

Expand Down Expand Up @@ -61,13 +61,15 @@ function register_color_controls( $wp_customize, $palette ) {

$section_key = 'customize-global-styles';

$theme = wp_get_theme();

//Add a Section to the Customizer for these bits
$wp_customize->add_section(
$section_key,
array(
'capability' => 'edit_theme_options',
'description' => __( 'Color Customization for Quadrat' ),
'title' => __( 'Colors' ),
'description' => sprintf( __( 'Color Customization for %1$s', 'blockbase' ), $theme->name ),
'title' => __( 'Colors', 'blockbase' ),
)
);

Expand All @@ -85,7 +87,6 @@ function register_color_control( $wp_customize, $palette_item, $section_key ) {
array(
'default' => $palette_item['default'],
'sanitize_callback' => 'sanitize_hex_color',
'slug' => $palette_item['slug'],
'user_value' => $palette_item['color'],
)
);
Expand All @@ -107,8 +108,8 @@ function handle_customize_save_after( $manager ) {
//update the palette based on the controls
foreach ( $this->user_color_palette as $key => $palette_item ) {
$setting = $manager->get_setting( 'customize-global-styles' . $palette_item['slug'] );
if ( isset( $setting->new_value ) ) {
$this->user_color_palette[ $key ]['color'] = $setting->new_value;
if ( null !== $setting->post_value() ) {
$this->user_color_palette[ $key ]['color'] = $setting->post_value();
}
}

Expand All @@ -129,9 +130,9 @@ function handle_customize_save_after( $manager ) {
}

// Update the theme.json with the new settings.
$user_theme_json_post->post_content = json_encode( $user_theme_json_post_content );
$user_theme_json_post->post_content = json_encode( $user_theme_json_post_content );
return wp_update_post( $user_theme_json_post );
}
}

new GlobalStylesCustomizer;
new GlobalStylesColorCustomizer;
44 changes: 44 additions & 0 deletions blockbase/inc/wp-customize-palette-control.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Color Palette Customizer Control
*
* Based on https://github.com/HardeepAsrani/o2
*/

if ( class_exists( 'WP_Customize_Control' ) && ! class_exists( 'Color_Palette_Control' ) ) {
class Color_Palette_Control extends WP_Customize_Control {

public $type = 'color-palette';

public function enqueue() {
wp_enqueue_style( 'color-customization', get_template_directory_uri() . '/inc/color-customization.css' );
}

public function render_content() {
?>
<label>
<?php if ( ! empty( $this->label ) ) : ?>
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
<?php endif;
if ( ! empty( $this->description ) ) : ?>
<span class="description customize-control-description"><?php echo esc_html( $this->description ); ?></span>
<?php endif; ?>
<div class="color-palette-group">
<?php foreach ( $this->choices as $value => $label ) : ?>
<input name="color_palette_<?php echo esc_attr( $this->id ); ?>" id="color_palette_<?php echo esc_attr( $this->id ); ?>_<?php echo esc_attr( $value ); ?>" type="radio" value="<?php echo esc_attr( $value ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> >
<label for="color_palette_<?php echo esc_attr( $this->id ); ?>_<?php echo esc_attr( $value ); ?>" class="color-option">
<div class="custom-color-palette">
<span class="color-palette-label"><?php echo esc_attr( $label['label'] ); ?></span>
<?php foreach ( $label['colors'] as $slug => $color ) : ?>
<div class="color-stripe" style="background-color: <?php echo esc_attr( $color ); ?>">&nbsp;</div>
<?php endforeach; ?>
</tr>
</div>
</label>
</input>
<?php endforeach; ?>
</div>
</label>
<?php }
}
}
2 changes: 1 addition & 1 deletion blockbase/rebuild-child-themes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ declare -a ChildThemes=( "mayland-blocks" "seedlet-blocks" "quadrat" )
for child in ${ChildThemes[@]}; do
cd '../'${child}
echo 'Rebulding '${child}
npm i
npm run build
done

35 changes: 35 additions & 0 deletions blockbase/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,41 @@
"secondary": "var(--wp--preset--color--secondary)",
"selection": "var(--wp--preset--color--selection)"
},
"colorPalettes": [
{
"label": "Featured",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is a mechanism now for labels for presets to get translated, but is there a way yet to mark that a custom variable needs to be translated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we do it like this. Since we haven't done this for any of the strings that we have (such as the template parts) I think this merits its own separate PR

"slug": "palette-1",
"colors": {
"primary": "#C8133E",
"secondary": "#4E2F4B",
"foreground": "#1D1E1E",
"background": "#FFFFFF",
"selection": "#F9F9F9"
}
},
{
"label": "Featured",
"slug": "palette-2",
"colors": {
"primary": "#35845D",
"secondary": "#233252",
"foreground": "#242527",
"background": "#EEF4F7",
"selection": "#F9F9F9"
}
},
{
"label": "Featured",
"slug": "palette-3",
"colors": {
"primary": "#9FD3E8",
"secondary": "#FBE6AA",
"foreground": "#FFFFFF",
"background": "#1F2527",
"selection": "#364043"
}
}
],
"form": {
"padding": "calc( 0.5 * var(--wp--custom--margin--horizontal) )",
"border": {
Expand Down
35 changes: 35 additions & 0 deletions mayland-blocks/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,41 @@
"secondary": "var(--wp--preset--color--secondary)",
"selection": "var(--wp--preset--color--selection)"
},
"colorPalettes": [
{
"label": "Featured",
"slug": "palette-1",
"colors": {
"primary": "#C8133E",
"secondary": "#4E2F4B",
"foreground": "#1D1E1E",
"background": "#FFFFFF",
"selection": "#F9F9F9"
}
},
{
"label": "Featured",
"slug": "palette-2",
"colors": {
"primary": "#35845D",
"secondary": "#233252",
"foreground": "#242527",
"background": "#EEF4F7",
"selection": "#F9F9F9"
}
},
{
"label": "Featured",
"slug": "palette-3",
"colors": {
"primary": "#9FD3E8",
"secondary": "#FBE6AA",
"foreground": "#FFFFFF",
"background": "#1F2527",
"selection": "#364043"
}
}
],
"form": {
"padding": "calc( 0.5 * var(--wp--custom--margin--horizontal) )",
"border": {
Expand Down
5 changes: 2 additions & 3 deletions quadrat/assets/theme.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading