-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-meta-box-colors.php
169 lines (143 loc) · 4.53 KB
/
wp-meta-box-colors.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
Plugin Name: WP Meta Box Colors
Plugin URI: https://horttcore.de
Description: Add color picker to posts
Version: 2.0
Author: Ralf Hortt
Author URI: https://horttcore.de
License: GPL2
*/
if (!class_exists('WP_Meta_Box_Colors')) {
class WP_Meta_Box_Colors
{
public function __construct()
{
add_action('admin_print_styles-post.php', [$this, 'enqueue_colorpicker']);
add_action('add_meta_boxes', [$this, 'add_meta_boxes']);
add_action('save_post', [$this, 'save_post']);
add_action('register_meta', [$this, 'register_meta']);
}
// END __construct
/**
* Register meta box.
*
* @return void
*
* @author Ralf Hortt <[email protected]>
**/
public function add_meta_boxes()
{
$post_types = get_post_types_by_support('colors');
foreach ($post_types as $post_type) {
add_meta_box('page-colors', __('Colors'), [$this, 'meta_box'], $post_type, 'side');
}
}
// END add_meta_boxes
/**
* Get registered colors.
*
* @param string $post_type Post type
*
* @return void
*/
public function get_colors($post_type = '')
{
$colors = apply_filters('wp-meta-box-colors-fields', []);
if (!$post_type) {
return $colors;
}
return apply_filters("wp-meta-box-colors-fields-$post_type", $colors);
}
// END get_colors
/**
* Enqueue colorpicker.
*
* @return void
*/
public function enqueue_colorpicker()
{
$screen = get_current_screen();
if ('post' != $screen->base) {
return;
}
if (!post_type_supports($screen->post_type, 'colors')) {
return;
}
wp_enqueue_style('wp-color-picker');
wp_enqueue_script('wp-color-picker');
}
// END enqueue_colorpicker
/**
* Meta box.
*
* @param WP_Post $post Post object
*
* @return void
*/
public function meta_box($post)
{
$colors = get_post_meta($post->ID, '_colors', true);
$fields = $this->get_colors($post->post_type); ?>
<table class="form-table">
<?php foreach ($fields as $field => $label) { ?>
<tr>
<td><label for="wp-meta-box-colors-<?php echo esc_attr($field) ?>"><?php echo $label ?></label></td>
<td>
<input type="text" name="wp-meta-box-colors[<?php echo esc_attr($field) ?>]" value="<?php echo esc_attr(get_post_meta($post->ID, "wp-meta-box-colors-$field", true)) ?>" id="wp-meta-box-colors-<?php echo esc_attr($field) ?>">
</td>
</tr>
<?php } ?>
</table>
<script>
jQuery(document).ready(function(){
jQuery('input[name^="wp-meta-box-colors["]').wpColorPicker();
});
</script>
<?php
wp_nonce_field('save-wp-meta-box-colors', 'wp-meta-box-colors-nonce');
}
// END meta_box
/**
* Register meta field.
*
* @return void
*/
public function register_meta()
{
$post_types = get_post_types_by_support('colors');
foreach ($post_types as $post_type) {
foreach ($this->get_colors($post_type) as $key => $value) {
register_meta($post_type, $key, [
'type' => 'string',
'show_in_rest' => true,
]);
}
}
}
/**
* Save post meta.
*
* @param int $post_id Post ID
*
* @author Ralf Hortt
**/
public function save_post($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (!isset($_POST['wp-meta-box-colors-nonce']) || !wp_verify_nonce($_POST['wp-meta-box-colors-nonce'], 'save-wp-meta-box-colors')) {
return;
}
foreach ($this->get_colors() as $key => $value) {
if (!isset($_POST['wp-meta-box-colors'][$key])) {
continue;
}
update_post_meta($post_id, "wp-meta-box-colors-$key", sanitize_text_field($_POST['wp-meta-box-colors'][$key]));
}
}
// END save_post
} // END class WP_Meta_Box_Colors
new WP_Meta_Box_Colors();
}