PluginProbe
Block Editor Colors / 1.2.4
Block Editor Colors v1.2.4
1.2.7 1.2.4 1.2.5 1.2.6 trunk 1.2.0 1.2.1 1.2.2 1.2.3
block-editor-colors / includes / CustomColorsService.php

CustomColorsService.php in Block Editor Colors 1.2.4, at includes/CustomColorsService.php

263 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BlockEditorColors;
4
5
6 class CustomColorsService {
7
8 private $color_cpt_slug = 'block_editor_color';
9 private $custom_colors = [];
10 private $disabled_custom_colors = [];
11 private static $_instance = null;
12
13 public function __construct() {
14 $this->set_color_cpt();
15 $this->set_colors();
16
17 add_action( 'admin_post_add_custom_color', array( $this, 'add_color' ) );
18 add_action( 'admin_post_edit_custom_color', array( $this, 'edit_color' ) );
19 add_action( 'admin_post_edit_inactive_color', array( $this, 'edit_inactive_color' ) );
20 if ( wp_doing_ajax() ) {
21 add_action( 'wp_ajax_bec_update_color_order', array( $this, 'update_color_order' ) );
22 }
23 }
24
25 public static function getInstance() {
26 if ( is_null( self::$_instance ) ) {
27 self::$_instance = new self();
28 }
29
30 return self::$_instance;
31 }
32
33 public function set_color_cpt() {
34 register_post_type( $this->color_cpt_slug, array(
35 'label' => esc_html__( 'Editor Color', 'block-editor-colors' ),
36 'labels' => array(
37 'name' => esc_html__( 'Editor Colors', 'block-editor-colors' ),
38 'singular_name' => esc_html__( 'Editor Color', 'block-editor-colors' ),
39 'add_new' => esc_html__( 'Add Editor Color', 'block-editor-colors' ),
40 'add_new_item' => esc_html__( 'Add Editor Color', 'block-editor-colors' ),
41 'edit_item' => esc_html__( 'Edit Editor Color', 'block-editor-colors' ),
42 'new_item' => esc_html__( 'New Editor Color', 'block-editor-colors' ),
43 'view_item' => esc_html__( 'View Editor Color', 'block-editor-colors' ),
44 'search_items' => esc_html__( 'Find Editor Color', 'block-editor-colors' ),
45 'not_found' => esc_html__( 'Editor Color Not Found', 'block-editor-colors' ),
46 'menu_name' => esc_html__( 'Editor Colors', 'block-editor-colors' ),
47 ),
48 'public' => false,
49 ) );
50 }
51
52 private function set_colors() {
53
54 $args = array(
55 'post_type' => $this->color_cpt_slug,
56 'orderby' => 'menu_order',
57 'order' => 'ASC',
58 'nopaging' => true
59 );
60
61 $query = new \WP_Query( $args );
62 $colors = [];
63 $disabled_colors = [];
64
65 if ( $query->have_posts() ) {
66 while ( $query->have_posts() ) {
67 $query->the_post();
68
69 if ( get_post_status() === 'publish' ) {
70 $colors[ get_the_ID() ] = [
71 'name' => get_the_title(),
72 'slug' => get_post_meta( get_the_ID(), 'slug', true ),
73 'color' => get_post_meta( get_the_ID(), 'color', true ),
74 ];
75 } else {
76 $disabled_colors[ get_the_ID() ] = [
77 'name' => get_the_title(),
78 'slug' => get_post_meta( get_the_ID(), 'slug', true ),
79 'color' => get_post_meta( get_the_ID(), 'color', true ),
80 ];
81 }
82
83 }
84 }
85
86 wp_reset_postdata();
87
88 $this->custom_colors = $colors;
89 $this->disabled_custom_colors = $disabled_colors;
90 }
91
92 public function get_colors( $disabled = false ) {
93 if ( $disabled ) {
94 return $this->disabled_custom_colors;
95 }
96
97 return $this->custom_colors;
98 }
99
100 public function add_color() {
101
102 if ( ! isset( $_POST['create_custom_color_nonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['create_custom_color_nonce'] ) ), 'create_custom_color' ) ) {
103 wp_die( esc_html__( 'Denied', 'block-editor-colors' ) );
104 }
105
106 if ( ! isset( $_POST['new_name'] ) || ! isset( $_POST['new_slug'] ) || ! isset( $_POST['new_color'] ) ) {
107 wp_die( esc_html__( 'Empty fields', 'block-editor-colors' ) );
108 }
109
110 $name = sanitize_text_field( wp_unslash( $_POST['new_name'] ) );
111 $slug = sanitize_title( wp_unslash( $_POST['new_slug'] ) );
112 $color = sanitize_hex_color( wp_unslash( $_POST['new_color'] ) );
113
114 $this->update_color( $name, $color, $slug );
115
116 wp_redirect( SettingsPage::getAdminUrl() );
117 exit;
118 }
119
120 public function edit_color() {
121
122 if ( ! isset( $_POST['update_custom_color_nonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['update_custom_color_nonce'] ) ), 'update_custom_color' ) ) {
123 wp_die( esc_html__( 'Denied', 'block-editor-colors' ) );
124 }
125
126 if ( ! isset( $_POST['color_id'] ) ) {
127 wp_die( esc_html__( 'You must specify Color ID', 'block-editor-colors' ) );
128 }
129
130 $id = absint( $_POST['color_id'] );
131
132 if ( isset( $_POST['disable'] ) ) {
133 $this->disable_color( $id );
134 wp_redirect( SettingsPage::getAdminUrl() );
135 exit;
136 }
137
138 if ( ! isset( $_POST['name'] ) || ! isset( $_POST['color'] ) || ! isset( $_POST['update'] ) ) {
139 wp_die( esc_html__( 'Empty fields', 'block-editor-colors' ) );
140 }
141
142 $name = sanitize_text_field( wp_unslash( $_POST['name'] ) );
143 $color = sanitize_hex_color( wp_unslash( $_POST['color'] ) );
144
145 $this->update_color( $name, $color, false, $id );
146
147 wp_redirect( SettingsPage::getAdminUrl() );
148 exit;
149
150 }
151
152 private function disable_color( $id ) {
153 wp_update_post( array(
154 'ID' => $id,
155 'post_type' => $this->color_cpt_slug,
156 'post_status' => 'draft',
157 ) );
158 }
159
160 private function enable_color( $id ) {
161 wp_update_post( array(
162 'ID' => $id,
163 'post_type' => $this->color_cpt_slug,
164 'post_status' => 'publish',
165 ) );
166 }
167
168 private function delete_color( $id ) {
169 wp_delete_post( $id );
170 }
171
172 private function update_color( $name, $color, $slug = false, $id = false ) {
173
174 $post_data = array(
175 'post_type' => $this->color_cpt_slug,
176 'post_title' => $name,
177 'meta_input' => array(
178 'color' => $color,
179 ),
180 'post_status' => 'publish',
181 );
182
183 if ( $slug ) {
184 $post_data['meta_input']['slug'] = $slug;
185 }
186
187 if ( $id ) {
188 $post_data['ID'] = $id;
189 }
190
191 wp_insert_post( $post_data );
192 }
193
194 public function edit_inactive_color() {
195 if ( ! isset( $_POST['update_inactive_color_nonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['update_inactive_color_nonce'] ) ), 'update_inactive_color' ) ) {
196 wp_die( esc_html__( 'Denied', 'block-editor-colors' ) );
197 }
198
199 if ( ! isset( $_POST['color_id'] ) ) {
200 wp_die( esc_html__( 'You must specify Color ID', 'block-editor-colors' ) );
201 }
202
203 $id = absint( $_POST['color_id'] );
204
205 if ( isset( $_POST['delete'] ) ) {
206 $this->delete_color( $id );
207 }
208
209 if ( isset( $_POST['restore'] ) ) {
210 $this->enable_color( $id );
211 }
212
213 wp_redirect( SettingsPage::getAdminUrl() );
214 exit;
215 }
216
217 public function update_color_order() {
218 check_ajax_referer( 'block_editor_colors_nonce', 'nonce' );
219
220 $colors = $this->recursive_sanitize_array( wp_unslash( $_POST['colors'] ) ); // phpcs:ignore
221
222 foreach ( $colors as $order => $color_id ) {
223 $updated = wp_update_post( [
224 'ID' => $color_id,
225 'menu_order' => $order
226 ] );
227
228 if ( ! $updated ) {
229 wp_send_json_error();
230 }
231 }
232
233 wp_send_json_success();
234 wp_die();
235 }
236
237 /**
238 * Recursive sanitation for an array
239 *
240 * @since 1.2.1
241 *
242 * @param $array
243 *
244 * @return mixed
245 */
246 function recursive_sanitize_array( $array ) {
247
248 foreach ( $array as $key => &$value ) {
249 if ( is_array( $value ) ) {
250 $value = $this->recursive_sanitize_array( $value );
251 }
252 else {
253 $value = sanitize_text_field( $value );
254 }
255 }
256
257 return $array;
258 }
259
260
261 }
262
263 CustomColorsService::getInstance();