diff --git a/README.md b/README.md index 5033f1a..498f2e7 100644 --- a/README.md +++ b/README.md @@ -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'); ``` diff --git a/src/Concerns/Palette.php b/src/Concerns/Palette.php index a296b86..3b8ea5b 100644 --- a/src/Concerns/Palette.php +++ b/src/Concerns/Palette.php @@ -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); + } } diff --git a/src/Field.php b/src/Field.php index 8f9209c..e142dc9 100644 --- a/src/Field.php +++ b/src/Field.php @@ -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']); } /**