forked from Log1x/acf-editor-palette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
144 lines (126 loc) · 3.83 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
140
141
142
143
144
<?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.1
* 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();
}
/**
* 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')) {
$this->registerGraphQl();
}
}
/**
* Register the field type with WPGraphQL.
*
* @return void
*/
protected function registerGraphQl()
{
add_action('graphql_register_types', function () {
register_graphql_object_type('EditorPaletteColor', [
'description' => __('Editor Palette Color Object', 'acf-editor-palette'),
'fields' => [
'color' => [
'type' => 'String',
'description' => __('The color value', 'acf-editor-palette'),
],
'name' => [
'type' => 'String',
'description' => __('The color name', 'acf-editor-palette'),
],
'slug' => [
'type' => 'String',
'description' => __('The color slug', 'acf-editor-palette'),
],
'text' => [
'type' => 'String',
'description' => __('The text color class', 'acf-editor-palette'),
],
'background' => [
'type' => 'String',
'description' => __('The background color class', 'acf-editor-palette'),
],
],
]);
});
add_action('wpgraphql/acf/registry_init', function () {
register_graphql_acf_field_type($this->name, [
'graphql_type' => 'EditorPaletteColor',
'resolve' => function ($root, $args, $context, $info, $field_config) {
$id = isset($root['node']) && isset($root['node']->ID) ? $root['node']->ID : null;
$field = $info->fieldName ?? null;
if (! $id || ! $field) {
return null;
}
$field = preg_replace('/[A-Z]/', '_$0', $field);
$field = strtolower($field);
return get_field($field, $id);
},
]);
});
}
});