| 1 |
<?php |
| 2 |
|
| 3 |
namespace BlockEditorColors; |
| 4 |
|
| 5 |
|
| 6 |
class DefaultColorsService { |
| 7 |
|
| 8 |
private $colors = []; |
| 9 |
private $edited_colors = []; |
| 10 |
private $theme_colors = false; |
| 11 |
private $theme_mod_name = 'bec_theme_colors'; |
| 12 |
private static $_instance = null; |
| 13 |
|
| 14 |
public static function getInstance() { |
| 15 |
if ( is_null( self::$_instance ) ) { |
| 16 |
self::$_instance = new self(); |
| 17 |
} |
| 18 |
|
| 19 |
return self::$_instance; |
| 20 |
} |
| 21 |
|
| 22 |
public function __construct() { |
| 23 |
$this->theme_colors = current( (array) get_theme_support( 'editor-color-palette' ) ); |
| 24 |
$this->set_colors(); |
| 25 |
|
| 26 |
add_action( 'admin_post_edit_initial_color', array( $this, 'edit_color' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
public function get_colors() { |
| 30 |
return $this->colors; |
| 31 |
} |
| 32 |
|
| 33 |
public function get_edited_colors() { |
| 34 |
return $this->edited_colors; |
| 35 |
} |
| 36 |
|
| 37 |
private function set_colors() { |
| 38 |
$theme_colors = $this->theme_colors ? $this->theme_colors : $this->get_initial_colors(); |
| 39 |
$edited_theme_colors = []; |
| 40 |
$colors_theme_mod = get_theme_mod( $this->theme_mod_name, array() ); |
| 41 |
|
| 42 |
if ( $theme_colors ) { |
| 43 |
foreach ( $theme_colors as $index => $color ) { |
| 44 |
$theme_colors[ $index ]['default-color'] = $color['color']; |
| 45 |
$edited_color = isset( $colors_theme_mod[ $color['slug'] ] ) ? $colors_theme_mod[ $color['slug'] ] : false; |
| 46 |
if ( $edited_color ) { |
| 47 |
$theme_colors[ $index ]['color'] = $edited_color; |
| 48 |
$edited_theme_colors[] = $theme_colors[ $index ]; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
$this->colors = $theme_colors ? $theme_colors : array(); |
| 54 |
$this->edited_colors = $edited_theme_colors; |
| 55 |
} |
| 56 |
|
| 57 |
public function edit_color() { |
| 58 |
|
| 59 |
if ( ! isset( $_POST['update_initial_color_nonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['update_initial_color_nonce'] ) ), 'update_initial_color' ) ) { |
| 60 |
wp_die( esc_html__( 'Denied', 'block-editor-colors' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
if ( ! isset( $_POST['slug'] ) ) { |
| 64 |
wp_die( esc_html__( 'You must specify Color Slug', 'block-editor-colors' ) ); |
| 65 |
} |
| 66 |
|
| 67 |
$slug = sanitize_title( wp_unslash( $_POST['slug'] ) ); |
| 68 |
|
| 69 |
if ( isset( $_POST['clear'] ) ) { |
| 70 |
$this->reset_color( $slug ); |
| 71 |
wp_redirect( SettingsPage::getAdminUrl() ); |
| 72 |
exit; |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! isset( $_POST['color'] ) || ! isset( $_POST['update'] ) ) { |
| 76 |
wp_die( esc_html__( 'Empty fields', 'block-editor-colors' ) ); |
| 77 |
} |
| 78 |
|
| 79 |
$color = sanitize_hex_color( wp_unslash( $_POST['color'] ) ); |
| 80 |
|
| 81 |
$this->update_color( $slug, $color ); |
| 82 |
|
| 83 |
wp_redirect( SettingsPage::getAdminUrl() ); |
| 84 |
exit; |
| 85 |
|
| 86 |
} |
| 87 |
|
| 88 |
private function update_color( $slug, $color ) { |
| 89 |
|
| 90 |
$colors = $this->get_edited_colors(); |
| 91 |
|
| 92 |
if ( $colors ) { |
| 93 |
$colors = array_column( $colors, 'color', 'slug' ); |
| 94 |
} |
| 95 |
|
| 96 |
$colors[ $slug ] = $color; |
| 97 |
|
| 98 |
set_theme_mod( $this->theme_mod_name, $colors ); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
private function reset_color( $slug ) { |
| 103 |
|
| 104 |
$colors = $this->get_edited_colors(); |
| 105 |
|
| 106 |
if ( $colors ) { |
| 107 |
$colors = array_column( $colors, 'color', 'slug' ); |
| 108 |
} else { |
| 109 |
return; |
| 110 |
} |
| 111 |
|
| 112 |
if ( array_key_exists( $slug, $colors ) ) { |
| 113 |
unset( $colors[ $slug ] ); |
| 114 |
} |
| 115 |
|
| 116 |
set_theme_mod( $this->theme_mod_name, $colors ); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
private function get_initial_colors() { |
| 121 |
return array( |
| 122 |
array( |
| 123 |
'name' => esc_html__( 'Pale pink' ), |
| 124 |
'slug' => 'pale-pink', |
| 125 |
'color' => '#f78da7', |
| 126 |
), |
| 127 |
array( |
| 128 |
'name' => esc_html__( 'Vivid red' ), |
| 129 |
'slug' => 'vivid-red', |
| 130 |
'color' => '#cf2e2e' |
| 131 |
), |
| 132 |
array( |
| 133 |
'name' => esc_html__( 'Luminous vivid orange' ), |
| 134 |
'slug' => 'luminous-vivid-orange', |
| 135 |
'color' => '#ff6900', |
| 136 |
), |
| 137 |
array( |
| 138 |
'name' => esc_html__( 'Luminous vivid amber' ), |
| 139 |
'slug' => 'luminous-vivid-amber', |
| 140 |
'color' => '#fcb900', |
| 141 |
), |
| 142 |
array( |
| 143 |
'name' => esc_html__( 'Light green cyan' ), |
| 144 |
'slug' => 'light-green-cyan', |
| 145 |
'color' => '#7bdcb5', |
| 146 |
), |
| 147 |
array( |
| 148 |
'name' => esc_html__( 'Vivid green cyan' ), |
| 149 |
'slug' => 'vivid-green-cyan', |
| 150 |
'color' => '#00d084', |
| 151 |
), |
| 152 |
array( |
| 153 |
'name' => esc_html__( 'Pale cyan blue' ), |
| 154 |
'slug' => 'pale-cyan-blue', |
| 155 |
'color' => '#8ed1fc', |
| 156 |
), |
| 157 |
array( |
| 158 |
'name' => esc_html__( 'Vivid cyan blue' ), |
| 159 |
'slug' => 'vivid-cyan-blue', |
| 160 |
'color' => '#0693e3', |
| 161 |
), |
| 162 |
array( |
| 163 |
'name' => esc_html__( 'Vivid purple' ), |
| 164 |
'slug' => 'vivid-purple', |
| 165 |
'color' => '#9b51e0', |
| 166 |
), |
| 167 |
array( |
| 168 |
'name' => esc_html__( 'Very light gray' ), |
| 169 |
'slug' => 'very-light-gray', |
| 170 |
'color' => '#eeeeee', |
| 171 |
), |
| 172 |
array( |
| 173 |
'name' => esc_html__( 'Cyan bluish gray' ), |
| 174 |
'slug' => 'cyan-bluish-gray', |
| 175 |
'color' => '#abb8c3', |
| 176 |
), |
| 177 |
array( |
| 178 |
'name' => esc_html__( 'Very dark gray' ), |
| 179 |
'slug' => 'very-dark-gray', |
| 180 |
'color' => '#313131', |
| 181 |
), |
| 182 |
); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
DefaultColorsService::getInstance(); |