| 1 |
<?php |
| 2 |
|
| 3 |
namespace BlockEditorColors; |
| 4 |
|
| 5 |
class SettingsPage { |
| 6 |
|
| 7 |
private static $menu_slug = 'editor-colors'; |
| 8 |
private static $capability = 'manage_options'; |
| 9 |
private $custom_colors_service = null; |
| 10 |
private $default_colors_service = null; |
| 11 |
private $options_service = null; |
| 12 |
|
| 13 |
public function __construct() { |
| 14 |
add_options_page( |
| 15 |
esc_html__( 'Editor Colors', 'block-editor-colors' ), |
| 16 |
esc_html__( 'Editor Colors', 'block-editor-colors' ), |
| 17 |
self::$capability, |
| 18 |
self::$menu_slug, |
| 19 |
array( $this, 'render_page' ) |
| 20 |
); |
| 21 |
$this->custom_colors_service = CustomColorsService::getInstance(); |
| 22 |
$this->default_colors_service = DefaultColorsService::getInstance(); |
| 23 |
$this->options_service = OptionsService::getInstance(); |
| 24 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_page_scripts' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
public static function getSettingPageSlug() { |
| 28 |
return self::$menu_slug; |
| 29 |
} |
| 30 |
|
| 31 |
public static function getAdminUrl() { |
| 32 |
return admin_url( '/options-general.php?page=' . self::getSettingPageSlug() ); |
| 33 |
} |
| 34 |
|
| 35 |
public function render_page() { |
| 36 |
?> |
| 37 |
<div class="wrap bec-wrapper"> |
| 38 |
<h2><?php esc_html_e( 'Block Editor Colors', 'block-editor-colors' ); ?></h2> |
| 39 |
<p><?php esc_html_e( 'Change block editor colors that are registered with a theme or create your own colors. They appear in the color palette of the block editor.', 'block-editor-colors' ); ?></p> |
| 40 |
|
| 41 |
<hr> |
| 42 |
|
| 43 |
<?php |
| 44 |
$this->render_custom_colors(); |
| 45 |
?> |
| 46 |
|
| 47 |
<hr> |
| 48 |
|
| 49 |
<?php |
| 50 |
$this->render_initial_colors(); |
| 51 |
?> |
| 52 |
|
| 53 |
<hr> |
| 54 |
|
| 55 |
<?php |
| 56 |
$this->render_general_settings(); |
| 57 |
?> |
| 58 |
|
| 59 |
<hr> |
| 60 |
|
| 61 |
<?php |
| 62 |
$this->render_disabled_custom_colors(); |
| 63 |
?> |
| 64 |
</div> |
| 65 |
<?php |
| 66 |
} |
| 67 |
|
| 68 |
public function render_initial_colors() { |
| 69 |
|
| 70 |
$initial_colors = $this->default_colors_service->get_colors(); |
| 71 |
?> |
| 72 |
<h3><?php esc_html_e( 'Default Colors', 'block-editor-colors' ); ?></h3> |
| 73 |
<p><?php esc_html_e( 'The colors of the active theme. They change when you switch a theme.', 'block-editor-colors' ); ?></p> |
| 74 |
|
| 75 |
<?php |
| 76 |
if ( ! $initial_colors ) { |
| 77 |
?> |
| 78 |
<strong><?php esc_html_e( 'Looks like your theme does not have any registered colors', 'block-editor-colors' ); ?> </strong> |
| 79 |
<?php |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
?> |
| 84 |
<div class="bec-color-tiles" id="bec-default-colors"> |
| 85 |
<?php |
| 86 |
foreach ( $initial_colors as $color ): |
| 87 |
?> |
| 88 |
<form class="bec-color-tile" |
| 89 |
action="<?php echo esc_url( admin_url( 'admin-post.php' ) ) ?>" method="post" autocomplete="off"> |
| 90 |
|
| 91 |
<?php wp_nonce_field( 'update_initial_color', 'update_initial_color_nonce' ); ?> |
| 92 |
|
| 93 |
<input name="slug" type="hidden" value="<?php echo esc_attr( $color['slug'] ); ?>"> |
| 94 |
<input name="action" type="hidden" value="edit_initial_color"> |
| 95 |
|
| 96 |
<table class="bec-color-table"> |
| 97 |
<tr> |
| 98 |
<td class="bec-color-cell"> |
| 99 |
<div class="default-color" |
| 100 |
style="height: 40px; border: 2px solid black; background: <?php echo esc_attr( $color['default-color'] ); ?>"></div> |
| 101 |
</td> |
| 102 |
<td class="bec-color-cell"> |
| 103 |
<div class="bec-color-preview" |
| 104 |
style="background: <?php echo esc_attr( $color['color'] ); ?>"></div> |
| 105 |
</td> |
| 106 |
</tr> |
| 107 |
<tr> |
| 108 |
<td> |
| 109 |
<?php esc_html_e( 'Color: ', 'block-editor-colors' ); ?> |
| 110 |
</td> |
| 111 |
<td> |
| 112 |
<input type="text" name="color" |
| 113 |
class="bec-color-field" |
| 114 |
value="<?php echo esc_attr( $color['color'] ); ?>" required> |
| 115 |
</td> |
| 116 |
</tr> |
| 117 |
<tr> |
| 118 |
<td> |
| 119 |
<?php esc_html_e( 'Name: ', 'block-editor-colors' ); ?> |
| 120 |
</td> |
| 121 |
<td> |
| 122 |
<input type="text" value="<?php echo esc_attr( $color['name'] ); ?>" |
| 123 |
placeholder="<?php esc_html_e( 'Color Name', 'block-editor-colors' ); ?>" |
| 124 |
required disabled> |
| 125 |
</td> |
| 126 |
</tr> |
| 127 |
<tr> |
| 128 |
<td> |
| 129 |
<?php esc_html_e( 'Slug: ', 'block-editor-colors' ); ?> |
| 130 |
</td> |
| 131 |
<td> |
| 132 |
<input type="text" |
| 133 |
value="<?php echo esc_attr( $color['slug'] ); ?>" |
| 134 |
placeholder="<?php esc_html_e( 'color-name', 'block-editor-colors' ); ?>" |
| 135 |
required disabled> |
| 136 |
</td> |
| 137 |
</tr> |
| 138 |
<tr> |
| 139 |
<td class="bec-color-submit-cell" colspan="2"> |
| 140 |
<button type="submit" name="update" |
| 141 |
class="button button-primary"><?php esc_html_e( 'Update', 'block-editor-colors' ); ?></button> |
| 142 |
|
| 143 |
<button type="submit" name="clear" |
| 144 |
class="button button-secondary"><?php esc_html_e( 'Reset', 'block-editor-colors' ); ?></button> |
| 145 |
</td> |
| 146 |
</tr> |
| 147 |
</table> |
| 148 |
</form> |
| 149 |
<?php |
| 150 |
endforeach; |
| 151 |
?> |
| 152 |
</div> |
| 153 |
<?php |
| 154 |
} |
| 155 |
|
| 156 |
public function render_custom_colors() { |
| 157 |
$colors = $this->custom_colors_service->get_colors(); |
| 158 |
?> |
| 159 |
<h3><?php esc_html_e( 'Custom Colors', 'block-editor-colors' ); ?></h3> |
| 160 |
<p><?php esc_html_e( 'Create new colors using the form below to extend the color palette of the block editor. You will not lose these colors when you change a theme. These colors can be deactivated or transfered via XML.', 'block-editor-colors' ); ?></p> |
| 161 |
<p><?php esc_html_e( 'Name - will be displayed as the color name in the block editor.', 'block-editor-colors' ); ?><br/> |
| 162 |
<?php esc_html_e( 'Slug - will be used to generate CSS classes for a color.', 'block-editor-colors' ); ?> |
| 163 |
<strong><?php esc_html_e( 'Only Latin lowercase letters, numbers, hyphens and underscores are allowed. The slug must be unique.', 'block-editor-colors' ); ?></strong> |
| 164 |
<?php esc_html_e( '("name12" - bad slug use "name-12" instead)', 'block-editor-colors' ); ?> |
| 165 |
</p> |
| 166 |
<div class="bec-color-tiles" id="bec-custom-colors"> |
| 167 |
<?php |
| 168 |
foreach ( $colors as $id => $color ): |
| 169 |
?> |
| 170 |
<form class="bec-color-tile" |
| 171 |
action="<?php echo esc_url( admin_url( 'admin-post.php' ) ) ?>" method="post"> |
| 172 |
|
| 173 |
<?php wp_nonce_field( 'update_custom_color', 'update_custom_color_nonce' ); ?> |
| 174 |
|
| 175 |
<input name="color_id" type="hidden" value="<?php echo esc_attr( $id ); ?>"> |
| 176 |
<input name="action" type="hidden" value="edit_custom_color"> |
| 177 |
|
| 178 |
<table class="bec-color-table"> |
| 179 |
<tr> |
| 180 |
<td colspan="2" class="bec-color-cell"> |
| 181 |
<div class="bec-color-preview" |
| 182 |
style="background: <?php echo esc_attr( $color['color'] ); ?>"></div> |
| 183 |
</td> |
| 184 |
</tr> |
| 185 |
<tr> |
| 186 |
<td> |
| 187 |
<?php esc_html_e( 'Color: ', 'block-editor-colors' ); ?> |
| 188 |
</td> |
| 189 |
<td> |
| 190 |
<input type="text" name="color" |
| 191 |
class="bec-color-field" |
| 192 |
value="<?php echo esc_attr( $color['color'] ); ?>" required> |
| 193 |
</td> |
| 194 |
</tr> |
| 195 |
<tr> |
| 196 |
<td> |
| 197 |
<?php esc_html_e( 'Name: ', 'block-editor-colors' ); ?> |
| 198 |
</td> |
| 199 |
<td> |
| 200 |
<input type="text" name="name" value="<?php echo esc_attr( $color['name'] ); ?>" |
| 201 |
placeholder="<?php esc_html_e( 'Color Name', 'block-editor-colors' ); ?>" |
| 202 |
required> |
| 203 |
</td> |
| 204 |
</tr> |
| 205 |
<tr> |
| 206 |
<td> |
| 207 |
<?php esc_html_e( 'Slug: ', 'block-editor-colors' ); ?> |
| 208 |
</td> |
| 209 |
<td> |
| 210 |
<input type="text" name="slug" |
| 211 |
value="<?php echo esc_attr( $color['slug'] ); ?>" |
| 212 |
placeholder="<?php esc_html_e( 'color-name', 'block-editor-colors' ); ?>" |
| 213 |
disabled> |
| 214 |
</td> |
| 215 |
</tr> |
| 216 |
<tr> |
| 217 |
<td class="bec-color-submit" colspan="2"> |
| 218 |
<button type="submit" name="update" |
| 219 |
class="button button-primary"><?php esc_html_e( 'Update', 'block-editor-colors' ); ?></button> |
| 220 |
<button type="submit" name="disable" |
| 221 |
class="button button-secondary"><?php esc_html_e( 'Disable', 'block-editor-colors' ); ?></button> |
| 222 |
</td> |
| 223 |
</tr> |
| 224 |
</table> |
| 225 |
<div class="status-icon move-icon" title="<?php esc_html_e('Drag the card to change the order of colors.'); ?>"><span class="dashicons dashicons-move"></span></div> |
| 226 |
<div class="status-icon moving-icon" title="<?php esc_html_e('Updating.'); ?>"><span class="dashicons dashicons-update"></span></div> |
| 227 |
<div class="status-icon update-error-icon" title="<?php esc_html_e('Something went wrong during the color update! Please try again.'); ?>"><span class="dashicons dashicons-warning"></span></div> |
| 228 |
</form> |
| 229 |
<?php |
| 230 |
endforeach; |
| 231 |
|
| 232 |
$this->render_custom_color_creator(); |
| 233 |
|
| 234 |
?> |
| 235 |
</div> |
| 236 |
<?php |
| 237 |
} |
| 238 |
|
| 239 |
public function render_custom_color_creator() { |
| 240 |
?> |
| 241 |
<form class="bec-color-tile bec-color-creator" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ) ?>" |
| 242 |
method="post"> |
| 243 |
|
| 244 |
<?php wp_nonce_field( 'create_custom_color', 'create_custom_color_nonce' ); ?> |
| 245 |
|
| 246 |
<input type="hidden" name="action" value="add_custom_color"> |
| 247 |
|
| 248 |
<table class="bec-color-table"> |
| 249 |
<tr> |
| 250 |
<td colspan="2" class="bec-color-cell"> |
| 251 |
<div class="bec-color-preview" |
| 252 |
style="background: #ffff"></div> |
| 253 |
</td> |
| 254 |
</tr> |
| 255 |
<tr> |
| 256 |
<td> |
| 257 |
<?php esc_html_e( 'Color: ', 'block-editor-colors' ); ?> |
| 258 |
</td> |
| 259 |
<td> |
| 260 |
<input type="text" name="new_color" class="bec-color-field" value="#ffffff" |
| 261 |
required> |
| 262 |
</td> |
| 263 |
</tr> |
| 264 |
<tr> |
| 265 |
<td> |
| 266 |
<?php esc_html_e( 'Name: ', 'block-editor-colors' ); ?> |
| 267 |
</td> |
| 268 |
<td> |
| 269 |
<input type="text" name="new_name" value="" |
| 270 |
placeholder="<?php esc_html_e( 'Color Name', 'block-editor-colors' ); ?>" required> |
| 271 |
</td> |
| 272 |
</tr> |
| 273 |
<tr> |
| 274 |
<td> |
| 275 |
<?php esc_html_e( 'Slug: ', 'block-editor-colors' ); ?> |
| 276 |
</td> |
| 277 |
<td> |
| 278 |
<input type="text" name="new_slug" value="" |
| 279 |
title="<?php esc_html_e( 'Only Latin lowercase letters, numbers, hyphens and underscores are allowed. The slug must be unique.', 'block-editor-colors' ); ?>" |
| 280 |
placeholder="<?php esc_html_e( 'color-name', 'block-editor-colors' ); ?>" required> |
| 281 |
</td> |
| 282 |
</tr> |
| 283 |
<tr> |
| 284 |
<td colspan="2" class="custom-color-submit"> |
| 285 |
<button type="submit" name="submit" id="submit" class="button button-primary"> |
| 286 |
<?php esc_html_e( 'Add Color', 'block-editor-colors' ); ?> |
| 287 |
</button> |
| 288 |
</td> |
| 289 |
</tr> |
| 290 |
</table> |
| 291 |
</form> |
| 292 |
<?php |
| 293 |
} |
| 294 |
|
| 295 |
public function render_general_settings() { |
| 296 |
?> |
| 297 |
<h3><?php esc_html_e( 'General Settings', 'block-editor-colors' ); ?></h3> |
| 298 |
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ) ?>" method="post"> |
| 299 |
|
| 300 |
<?php wp_nonce_field( 'update_general_options', 'update_general_options_nonce' ); ?> |
| 301 |
<input name="action" type="hidden" value="update_general_options"> |
| 302 |
|
| 303 |
<table style="max-width: 900px"> |
| 304 |
<tr> |
| 305 |
<td style="width: 100px"> |
| 306 |
<?php esc_html_e( 'CSS class prefix:', 'block-editor-colors' ); ?> |
| 307 |
</td> |
| 308 |
<td> |
| 309 |
<input type="text" |
| 310 |
value="<?php echo esc_attr( $this->options_service->get_style_classes_prefix() ); ?>" |
| 311 |
name="<?php echo esc_attr( $this->options_service->get_class_prefix_option_name() ); ?>"> |
| 312 |
</td> |
| 313 |
<td> |
| 314 |
<?php esc_html_e( 'This prefix will be used in style generation, and will be added before the color classes.', 'block-editor-colors' ); ?> |
| 315 |
<br/> |
| 316 |
<i><?php esc_html_e( 'For example: .entry-content', 'block-editor-colors' ); ?></i> |
| 317 |
</td> |
| 318 |
</tr> |
| 319 |
</table> |
| 320 |
<p> |
| 321 |
<button type="submit" name="save" |
| 322 |
class="button button-primary"><?php esc_html_e( 'Update', 'block-editor-colors' ); ?></button> |
| 323 |
</p> |
| 324 |
</form> |
| 325 |
<?php |
| 326 |
} |
| 327 |
|
| 328 |
public function render_disabled_custom_colors() { |
| 329 |
$colors = $this->custom_colors_service->get_colors( true ); |
| 330 |
if ( ! $colors ) { |
| 331 |
return; |
| 332 |
} |
| 333 |
?> |
| 334 |
<h4><?php esc_html_e( 'Inactive Custom Colors', 'block-editor-colors' ); ?></h4> |
| 335 |
<div class="bec-inactive-colors"> |
| 336 |
<?php |
| 337 |
foreach ( $colors as $id => $color ): |
| 338 |
?> |
| 339 |
<form class="bec-inactive-color" |
| 340 |
action="<?php echo esc_url( admin_url( 'admin-post.php' ) ) ?>" method="post"> |
| 341 |
|
| 342 |
<?php wp_nonce_field( 'update_inactive_color', 'update_inactive_color_nonce' ); ?> |
| 343 |
|
| 344 |
<input name="color_id" type="hidden" value="<?php echo esc_attr( $id ); ?>"> |
| 345 |
<input name="action" type="hidden" value="edit_inactive_color"> |
| 346 |
|
| 347 |
<table> |
| 348 |
<tr> |
| 349 |
<td rowspan="5" style="width: 40px; background: <?php echo esc_attr( $color['color'] ); ?>"> |
| 350 |
</td> |
| 351 |
<td></td> |
| 352 |
<td></td> |
| 353 |
</tr> |
| 354 |
<tr> |
| 355 |
<td></td> |
| 356 |
<td> |
| 357 |
<?php esc_html_e( 'Name: ', 'block-editor-colors' ); ?> |
| 358 |
</td> |
| 359 |
<td> |
| 360 |
<?php echo esc_html( $color['name'] ); ?> |
| 361 |
</td> |
| 362 |
</tr> |
| 363 |
<tr> |
| 364 |
<td></td> |
| 365 |
<td> |
| 366 |
<?php esc_html_e( 'Slug: ', 'block-editor-colors' ); ?> |
| 367 |
</td> |
| 368 |
<td> |
| 369 |
<?php echo esc_html( $color['slug'] ); ?> |
| 370 |
</td> |
| 371 |
</tr> |
| 372 |
|
| 373 |
<tr> |
| 374 |
<td></td> |
| 375 |
<td> |
| 376 |
<?php esc_html_e( 'Color: ', 'block-editor-colors' ); ?> |
| 377 |
</td> |
| 378 |
<td> |
| 379 |
<?php echo esc_html( $color['color'] ); ?> |
| 380 |
</td> |
| 381 |
</tr> |
| 382 |
<tr> |
| 383 |
<td></td> |
| 384 |
<td class="bec-color-submit-cell" colspan="2"> |
| 385 |
<button type="submit" name="restore" |
| 386 |
class="bec-link-button"><?php esc_html_e( 'Restore', 'block-editor-colors' ); ?></button> |
| 387 |
| |
| 388 |
<button type="submit" name="delete" |
| 389 |
class="bec-link-button red"><?php esc_html_e( 'Delete', 'block-editor-colors' ); ?></button> |
| 390 |
</td> |
| 391 |
</tr> |
| 392 |
</table> |
| 393 |
</form> |
| 394 |
<?php |
| 395 |
endforeach; |
| 396 |
?> |
| 397 |
</div> |
| 398 |
<?php |
| 399 |
} |
| 400 |
|
| 401 |
public function enqueue_page_scripts( $hook ) { |
| 402 |
if ( 'settings_page_' . self::getSettingPageSlug() !== $hook ) { |
| 403 |
return; |
| 404 |
} |
| 405 |
wp_enqueue_style( 'wp-color-picker' ); |
| 406 |
wp_enqueue_script( 'cec-admin-js', plugins_url( '/assets/admin.js', BEC_PLUGIN_FILE ), array( |
| 407 |
'jquery', |
| 408 |
'wp-color-picker', |
| 409 |
'jquery-ui-sortable' |
| 410 |
), BEC_PLUGIN_VERSION ); |
| 411 |
|
| 412 |
wp_localize_script( 'cec-admin-js', 'BlockEditorColors', |
| 413 |
array( |
| 414 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 415 |
'nonce' => wp_create_nonce('block_editor_colors_nonce') |
| 416 |
) |
| 417 |
); |
| 418 |
|
| 419 |
wp_enqueue_style( 'cec-admin-style', plugins_url( '/assets/style.css', BEC_PLUGIN_FILE ), array(), BEC_PLUGIN_VERSION ); |
| 420 |
} |
| 421 |
} |