Skip to content

Commit

Permalink
fix(field): Fix potential exception when editor palette is empty (Fixes
Browse files Browse the repository at this point in the history
#43) (#44)

enhance(plugin): Move `palette()` to a trait to allow usage outside of the plugin
  • Loading branch information
Log1x authored Nov 4, 2022
1 parent 59f3798 commit 30af4f6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Plugin Name: Advanced Custom Fields: Editor Palette Field
* Plugin URI: https://github.com/log1x/acf-editor-palette
* Description: A Gutenberg-like editor palette color picker field for Advanced Custom Fields.
* Version: 1.1.3
* Version: 1.1.4
* Author: Brandon Nifong
* Author URI: https://github.com/log1x
*/
Expand Down
47 changes: 47 additions & 0 deletions src/Concerns/Palette.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Log1x\AcfEditorPalette\Concerns;

trait Palette
{
/**
* Retrieve the editor color palette.
*
* @param string $color
* @return string[]
*/
public function palette($color = null)
{
$colors = [];
$themeJson = [];

$palette = (array) current(
get_theme_support('editor-color-palette') ?? []
);

if (function_exists('wp_get_global_settings')) {
$themeJson = wp_get_global_settings(['color', 'palette', 'theme']);
}

if (! isset($themeJson['border'])) {
$palette = array_merge($palette, $themeJson);
}

if (empty($palette)) {
return $color ?: $colors;
}

foreach ($palette as $value) {
$colors = array_merge($colors, [
$value['slug'] => array_merge($value, [
'text' => sprintf('has-text-color has-%s-color', $value['slug']),
'background' => sprintf('has-background has-%s-background-color', $value['slug']),
])
]);
}

return ! empty($color) ? (
$colors[$color] ?? null
) : $colors;
}
}
42 changes: 1 addition & 41 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Field extends \acf_field
{
use Concerns\Asset;
use Concerns\Palette;

/**
* The default field values.
Expand Down Expand Up @@ -34,47 +35,6 @@ public function __construct(callable $plugin)
parent::__construct();
}

/**
* Retrieve the editor color palette.
*
* @param string $color
* @return string[]
*/
protected function palette($color = null)
{
$colors = [];
$themeJson = [];

$palette = (array) current(
get_theme_support('editor-color-palette')
);

if (function_exists('wp_get_global_settings')) {
$themeJson = wp_get_global_settings(['color', 'palette', 'theme']);
}

if (! isset($themeJson['border'])) {
$palette = array_merge($palette, $themeJson);
}

if (empty($palette)) {
return $color ?: $colors;
}

foreach ($palette as $value) {
$colors = array_merge($colors, [
$value['slug'] => array_merge($value, [
'text' => sprintf('has-text-color has-%s-color', $value['slug']),
'background' => sprintf('has-background has-%s-background-color', $value['slug']),
])
]);
}

return ! empty($color) ? (
$colors[$color] ?? null
) : $colors;
}

/**
* The rendered field type.
*
Expand Down

0 comments on commit 30af4f6

Please sign in to comment.