Skip to content

Override palette with custom_colors setting #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -58,6 +58,11 @@ $field
->setConfig('default_value', 'green-500')
->setConfig('allowed_colors', ['green-500', 'blue-500'])
->setConfig('exclude_colors', ['green-50', 'green-100'])
->setConfig('custom_colors', [
'name' => 'Magenta',
'slug' => 'magenta',
'color' => '#ff00ff',
]);
->setConfig('return_format', 'slug');
```

21 changes: 20 additions & 1 deletion src/Concerns/Palette.php
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ trait Palette
* @param string $color
* @return string[]
*/
public function palette($color = null)
public function palette($color = null, $custom_colors = [])
{
$colors = [];
$themeJson = [];
@@ -31,6 +31,10 @@ public function palette($color = null)
return $color ?: $colors;
}

if (! empty($custom_colors)) {
$palette = $this->sanitize_custom_colors($custom_colors);
}

foreach ($palette as $value) {
if (empty($value['slug'])) {
continue;
@@ -48,4 +52,19 @@ public function palette($color = null)
$colors[$color] ?? null
) : $colors;
}

public function sanitize_custom_colors($customColors = [])
{
if (! is_array($customColors)) {
return [];
}

return array_map(function ($color) {
if (empty($color['slug'])) {
$color['slug'] = sanitize_title($color['name']);
}

return $color;
}, $customColors);
}
}
41 changes: 38 additions & 3 deletions src/Field.php
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ class Field extends \acf_field
'default_value' => null,
'allowed_colors' => [],
'exclude_colors' => [],
'custom_colors' => [],
'return_format' => 'slug',
];

@@ -59,6 +60,10 @@ public function render_field($field)
});
}

if (! empty($customColors = $field['custom_colors']) && is_array($customColors)) {
$palette = $this->sanitize_custom_colors($customColors);
}

if (empty($palette)) {
echo __('There are no colors available.', 'acf-editor-palette');

@@ -192,6 +197,36 @@ public function render_field_settings($field)
'choices' => $colors,
]);

acf_render_field_setting($field, [
'label' => __('Custom Colors', 'acf-editor-palette'),
'name' => 'custom_colors',
'instructions' => __('Add custom colors to the palette.', 'acf-editor-palette'),
'type' => 'repeater',
'button_label' => __('Add Color', 'acf-editor-palette'),
'sub_fields' => [
[
'label' => __('Name', 'acf-editor-palette'),
'name' => 'name',
'_name' => 'name',
'key' => 'name',
'type' => 'text',
'required' => true,
'instructions' => 'The name of the color.',
'wrapper' => ['width' => '', 'class' => '', 'id' => ''],
],
[
'label' => __('Color', 'acf-editor-palette'),
'name' => 'color',
'_name' => 'color',
'key' => 'color',
'type' => 'color_picker',
'required' => true,
'instructions' => 'The color value.',
'wrapper' => ['width' => '', 'class' => '', 'id' => ''],
],
],
]);

acf_render_field_setting($field, [
'label' => __('Return Format', 'acf-editor-palette'),
'name' => 'return_format',
@@ -222,7 +257,7 @@ public function format_value($value, $post_id, $field)
$format = $field['return_format'] ?? $this->defaults['return_format'];

if (! empty($value) && is_string($value)) {
$value = $this->palette($value);
$value = $this->palette($value, $field['custom_colors']);
}

return $format === 'array' ? $value : ($value[$format] ?? $value);
@@ -243,7 +278,7 @@ public function validate_value($valid, $value, $field, $input)
if (
$valid &&
! empty($value) &&
empty($this->palette($value))
empty($this->palette($value, $field['custom_colors']))
) {
return __('The current color does not exist in the editor palette.', 'acf-editor-palette');
}
@@ -267,7 +302,7 @@ public function update_value($value, $post_id, $field)

$value = is_string($value) ? $value : $value['slug'];

return $this->palette($value);
return $this->palette($value, $field['custom_colors']);
}

/**
Loading