-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
plugin.php
139 lines (121 loc) · 3.35 KB
/
plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* 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.2.0
* Author: Brandon Nifong
* Author URI: https://github.com/log1x
*/
namespace Log1x\AcfEditorPalette;
add_filter('after_setup_theme', new class
{
/**
* The field label.
*
* @var string
*/
public $label = 'Editor Palette';
/**
* The field name.
*
* @var string
*/
public $name = 'editor_palette';
/**
* The field category.
*
* @var string
*/
public $category = 'basic';
/**
* The field asset URI.
*
* @var string
*/
public $uri;
/**
* The field asset path.
*
* @var string
*/
public $path = 'public/';
/**
* Invoke the plugin.
*
* @return void
*/
public function __invoke()
{
if (! class_exists('\ACF')) {
return;
}
$this->uri = plugin_dir_url(__FILE__) . $this->path;
$this->path = plugin_dir_path(__FILE__) . $this->path;
if (file_exists($composer = __DIR__ . '/vendor/autoload.php')) {
require_once $composer;
}
$this->register();
$this->registerAdminColumns();
}
/**
* Register the field type with ACF.
*
* @return void
*/
protected function register()
{
foreach (['acf/include_field_types', 'acf/register_fields'] as $hook) {
add_filter($hook, function () {
return new Field($this);
});
}
if (function_exists('register_graphql_acf_field_type')) {
add_action('wpgraphql/acf/registry_init', function () {
register_graphql_acf_field_type($this->name, [
'graphql_type' => 'string',
'resolve' => function ($root, $args, $context, $info, $field_config) {
$value = $field_config->resolve_field($root, $args, $context, $info);
if (is_null($value)) {
return null;
}
return $value['slug'];
},
]);
});
}
}
/**
* Register the field type with ACP.
*
* @return void
*/
protected function registerAdminColumns()
{
if (! defined('ACP_FILE')) {
return;
}
add_filter('ac/column/value', function ($value, $id, $column) {
if (
! is_a($column, '\ACA\ACF\Column') ||
$column->get_field_type() !== $this->name ||
empty($color = get_field($column->get_meta_key())) ||
! is_array($color)
) {
return $value;
}
return sprintf(
'<div
aria-label="%s"
style="background-color: %s;
width: 24px;
height: 24px;
border: 1px solid #ccd0d4;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);"
></div>',
$color['name'],
$color['color']
);
}, 10, 3);
}
});