Skip to content
This repository was archived by the owner on Dec 18, 2025. It is now read-only.

Commit 452e7b3

Browse files
authored
Merge pull request #66 from creode/feature/color-choices-trait
Added block trait to facilitate integration of theme color choices in…
2 parents 6b358f4 + 7cb6b99 commit 452e7b3

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

includes/traits/all.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@
1717
require_once plugin_dir_path( __FILE__ ) . 'trait-block-pattern-options.php';
1818
require_once plugin_dir_path( __FILE__ ) . 'trait-menu-integration.php';
1919
require_once plugin_dir_path( __FILE__ ) . 'trait-has-icons.php';
20+
require_once plugin_dir_path( __FILE__ ) . 'trait-has-color-choices.php';
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
<?php
2+
/**
3+
* Trait for providing colour choices based on configured theme colours.
4+
*
5+
* @package Creode Blocks
6+
*/
7+
8+
namespace Creode_Blocks;
9+
10+
use WP_Theme_JSON_Resolver;
11+
12+
/**
13+
* Trait for providing colour choices based on configured theme colours.
14+
*/
15+
trait Trait_Has_Color_Choices {
16+
17+
/**
18+
* Returns an ACF choces array of colors to be used within a radio field.
19+
*
20+
* @return array An ACF choces array of colors.
21+
*/
22+
protected function get_color_choices(): array {
23+
$this->add_preview_admin_css();
24+
$choices = array();
25+
$colors = $this->get_theme_colors();
26+
27+
foreach ( $colors as $color ) {
28+
$choices[ $color['slug'] ] = '<span class="color-choice-preview" style="background-color:' . $color['color'] . ';"></span>' . $color['name'];
29+
}
30+
31+
return $choices;
32+
}
33+
34+
/**
35+
* Retrieves a theme color code by color slug.
36+
*
37+
* @param string $slug The slug of the color.
38+
* @return string The color code or an empty string if color cannot be found.
39+
*/
40+
public function get_color_code_by_slug( string $slug ): string {
41+
$colors = $this->get_theme_colors();
42+
43+
foreach ( $colors as $color ) {
44+
if ( $slug !== $color['slug'] ) {
45+
continue;
46+
}
47+
48+
return $color['color'];
49+
}
50+
51+
return '';
52+
}
53+
54+
/**
55+
* Adds CSS to the admin head to style the choice preview element.
56+
* Will only add this once, regardless of how many times this function is called.
57+
*/
58+
private function add_preview_admin_css() {
59+
if ( apply_filters( 'colour_choice_preview_css_added', false ) ) {
60+
return;
61+
}
62+
63+
add_action(
64+
'admin_head',
65+
function () {
66+
echo '
67+
<style>
68+
.color-choice-preview {
69+
display: inline-block;
70+
width: 12px;
71+
height: 12px;
72+
margin-right: 7px;
73+
border: solid 1px black;
74+
}
75+
</style>
76+
';
77+
}
78+
);
79+
80+
add_filter(
81+
'colour_choice_preview_css_added',
82+
function () {
83+
return true;
84+
}
85+
);
86+
}
87+
88+
/**
89+
* Returns an array of theme color information.
90+
*
91+
* @return array Theme color information.
92+
*/
93+
private function get_theme_colors(): array {
94+
$theme = WP_Theme_JSON_Resolver::get_merged_data()->get_settings();
95+
return $theme['color']['palette']['theme'];
96+
}
97+
}

0 commit comments

Comments
 (0)