| 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 |
'post_status' => 'any', |
| 59 |
'numberposts' => -1 |
| 60 |
); |
| 61 |
|
| 62 |
$posts = get_posts( $args ); |
| 63 |
$colors = []; |
| 64 |
$disabled_colors = []; |
| 65 |
|
| 66 |
foreach ( $posts as $post ) { |
| 67 |
if ( $post->post_status === 'publish' ) { |
| 68 |
$colors[ $post->ID ] = [ |
| 69 |
'name' => $post->post_title, |
| 70 |
'slug' => get_post_meta( $post->ID, 'slug', true ), |
| 71 |
'color' => get_post_meta( $post->ID, 'color', true ), |
| 72 |
]; |
| 73 |
} else { |
| 74 |
$disabled_colors[ $post->ID ] = [ |
| 75 |
'name' => $post->post_title, |
| 76 |
'slug' => get_post_meta( $post->ID, 'slug', true ), |
| 77 |
'color' => get_post_meta( $post->ID, 'color', true ), |
| 78 |
]; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
$this->custom_colors = $colors; |
| 83 |
$this->disabled_custom_colors = $disabled_colors; |
| 84 |
} |
| 85 |
|
| 86 |
public function get_colors( $disabled = false ) { |
| 87 |
if ( $disabled ) { |
| 88 |
return $this->disabled_custom_colors; |
| 89 |
} |
| 90 |
|
| 91 |
return $this->custom_colors; |
| 92 |
} |
| 93 |
|
| 94 |
public function add_color() { |
| 95 |
|
| 96 |
if ( ! isset( $_POST['create_custom_color_nonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['create_custom_color_nonce'] ) ), 'create_custom_color' ) ) { |
| 97 |
wp_die( esc_html__( 'Denied', 'block-editor-colors' ) ); |
| 98 |
} |
| 99 |
|
| 100 |
if ( ! isset( $_POST['new_name'] ) || ! isset( $_POST['new_slug'] ) || ! isset( $_POST['new_color'] ) ) { |
| 101 |
wp_die( esc_html__( 'Empty fields', 'block-editor-colors' ) ); |
| 102 |
} |
| 103 |
|
| 104 |
$name = sanitize_text_field( wp_unslash( $_POST['new_name'] ) ); |
| 105 |
$slug = sanitize_title( wp_unslash( $_POST['new_slug'] ) ); |
| 106 |
$color = sanitize_hex_color( wp_unslash( $_POST['new_color'] ) ); |
| 107 |
|
| 108 |
$this->update_color( $name, $color, $slug ); |
| 109 |
|
| 110 |
wp_redirect( SettingsPage::getAdminUrl() ); |
| 111 |
exit; |
| 112 |
} |
| 113 |
|
| 114 |
public function edit_color() { |
| 115 |
|
| 116 |
if ( ! isset( $_POST['update_custom_color_nonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['update_custom_color_nonce'] ) ), 'update_custom_color' ) ) { |
| 117 |
wp_die( esc_html__( 'Denied', 'block-editor-colors' ) ); |
| 118 |
} |
| 119 |
|
| 120 |
if ( ! isset( $_POST['color_id'] ) ) { |
| 121 |
wp_die( esc_html__( 'You must specify Color ID', 'block-editor-colors' ) ); |
| 122 |
} |
| 123 |
|
| 124 |
$id = absint( $_POST['color_id'] ); |
| 125 |
|
| 126 |
if ( isset( $_POST['disable'] ) ) { |
| 127 |
$this->disable_color( $id ); |
| 128 |
wp_redirect( SettingsPage::getAdminUrl() ); |
| 129 |
exit; |
| 130 |
} |
| 131 |
|
| 132 |
if ( ! isset( $_POST['name'] ) || ! isset( $_POST['color'] ) || ! isset( $_POST['update'] ) ) { |
| 133 |
wp_die( esc_html__( 'Empty fields', 'block-editor-colors' ) ); |
| 134 |
} |
| 135 |
|
| 136 |
$name = sanitize_text_field( wp_unslash( $_POST['name'] ) ); |
| 137 |
$color = sanitize_hex_color( wp_unslash( $_POST['color'] ) ); |
| 138 |
|
| 139 |
$this->update_color( $name, $color, false, $id ); |
| 140 |
|
| 141 |
wp_redirect( SettingsPage::getAdminUrl() ); |
| 142 |
exit; |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
private function disable_color( $id ) { |
| 147 |
wp_update_post( array( |
| 148 |
'ID' => $id, |
| 149 |
'post_type' => $this->color_cpt_slug, |
| 150 |
'post_status' => 'draft', |
| 151 |
) ); |
| 152 |
} |
| 153 |
|
| 154 |
private function enable_color( $id ) { |
| 155 |
wp_update_post( array( |
| 156 |
'ID' => $id, |
| 157 |
'post_type' => $this->color_cpt_slug, |
| 158 |
'post_status' => 'publish', |
| 159 |
) ); |
| 160 |
} |
| 161 |
|
| 162 |
private function delete_color( $id ) { |
| 163 |
wp_delete_post( $id ); |
| 164 |
} |
| 165 |
|
| 166 |
private function update_color( $name, $color, $slug = false, $id = false ) { |
| 167 |
|
| 168 |
$post_data = array( |
| 169 |
'post_type' => $this->color_cpt_slug, |
| 170 |
'post_title' => $name, |
| 171 |
'meta_input' => array( |
| 172 |
'color' => $color, |
| 173 |
), |
| 174 |
'post_status' => 'publish', |
| 175 |
); |
| 176 |
|
| 177 |
if ( $slug ) { |
| 178 |
$post_data['meta_input']['slug'] = $slug; |
| 179 |
} |
| 180 |
|
| 181 |
if ( $id ) { |
| 182 |
$post_data['ID'] = $id; |
| 183 |
} |
| 184 |
|
| 185 |
wp_insert_post( $post_data ); |
| 186 |
} |
| 187 |
|
| 188 |
public function edit_inactive_color() { |
| 189 |
if ( ! isset( $_POST['update_inactive_color_nonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['update_inactive_color_nonce'] ) ), 'update_inactive_color' ) ) { |
| 190 |
wp_die( esc_html__( 'Denied', 'block-editor-colors' ) ); |
| 191 |
} |
| 192 |
|
| 193 |
if ( ! isset( $_POST['color_id'] ) ) { |
| 194 |
wp_die( esc_html__( 'You must specify Color ID', 'block-editor-colors' ) ); |
| 195 |
} |
| 196 |
|
| 197 |
$id = absint( $_POST['color_id'] ); |
| 198 |
|
| 199 |
if ( isset( $_POST['delete'] ) ) { |
| 200 |
$this->delete_color( $id ); |
| 201 |
} |
| 202 |
|
| 203 |
if ( isset( $_POST['restore'] ) ) { |
| 204 |
$this->enable_color( $id ); |
| 205 |
} |
| 206 |
|
| 207 |
wp_redirect( SettingsPage::getAdminUrl() ); |
| 208 |
exit; |
| 209 |
} |
| 210 |
|
| 211 |
public function update_color_order() { |
| 212 |
check_ajax_referer( 'block_editor_colors_nonce', 'nonce' ); |
| 213 |
|
| 214 |
$colors = $this->recursive_sanitize_array( wp_unslash( $_POST['colors'] ) ); // phpcs:ignore |
| 215 |
|
| 216 |
foreach ( $colors as $order => $color_id ) { |
| 217 |
$updated = wp_update_post( [ |
| 218 |
'ID' => $color_id, |
| 219 |
'menu_order' => $order |
| 220 |
] ); |
| 221 |
|
| 222 |
if ( ! $updated ) { |
| 223 |
wp_send_json_error(); |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
wp_send_json_success(); |
| 228 |
wp_die(); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Recursive sanitation for an array |
| 233 |
* |
| 234 |
* @since 1.2.1 |
| 235 |
* |
| 236 |
* @param $array |
| 237 |
* |
| 238 |
* @return mixed |
| 239 |
*/ |
| 240 |
function recursive_sanitize_array( $array ) { |
| 241 |
|
| 242 |
foreach ( $array as $key => &$value ) { |
| 243 |
if ( is_array( $value ) ) { |
| 244 |
$value = $this->recursive_sanitize_array( $value ); |
| 245 |
} |
| 246 |
else { |
| 247 |
$value = sanitize_text_field( $value ); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
return $array; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
CustomColorsService::getInstance(); |