wp-customize-color-palettes.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. require_once ( __DIR__ . '/wp-customize-color-palette-control.php' );
  3. class GlobalStylesColorPalettes {
  4. private $palettes = array();
  5. private $section_key = 'customize-global-styles-colors';
  6. function __construct() {
  7. add_action( 'customize_register', array( $this, 'color_palette_control' ) );
  8. add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_preview_js' ) );
  9. }
  10. function customize_preview_js() {
  11. wp_enqueue_script( 'customizer-color-palettes', get_template_directory_uri() . '/inc/customizer/wp-customize-color-palettes.js', array( 'customize-controls' ) );
  12. wp_localize_script( 'customizer-color-palettes', 'colorPalettes', $this->palettes );
  13. wp_add_inline_script( 'customizer-color-palettes', 'var userColorSectionKey="' . $this->section_key . '";', 'before' );
  14. }
  15. function build_palettes( $theme_json ) {
  16. $default_palette = $theme_json['settings']['color']['palette']['theme'];
  17. $default_palette_setting = array();
  18. foreach ( $default_palette as $default_color ) {
  19. $default_palette_setting[ $default_color['slug'] ] = $default_color['color'];
  20. }
  21. $this->palettes['default-palette'] = array(
  22. 'label' => 'Default',
  23. 'colors' => $default_palette_setting,
  24. );
  25. $custom_palettes = $theme_json['settings']['custom']['colorPalettes'];
  26. if ( ! empty( $custom_palettes ) ) {
  27. foreach ( $custom_palettes as $custom_palette ) {
  28. $custom_palette_setting = array();
  29. foreach ( $custom_palette['colors'] as $color_slug => $color ) {
  30. //the alternative palettes need to have the same color mapping as the default one
  31. if ( isset( $default_palette_setting[ $color_slug ] ) ) {
  32. $custom_palette_setting[ $color_slug ] = $color;
  33. }
  34. }
  35. $this->palettes[ $custom_palette['slug'] ] = array(
  36. 'label' => $custom_palette['label'],
  37. 'colors' => $custom_palette_setting,
  38. );
  39. }
  40. }
  41. }
  42. function color_palette_control( $wp_customize ) {
  43. $theme_json = WP_Theme_JSON_Resolver_Gutenberg::get_theme_data()->get_raw_data();
  44. if ( ! isset( $theme_json['settings']['custom']['colorPalettes'] ) ) {
  45. return;
  46. }
  47. $this->build_palettes( $theme_json );
  48. $wp_customize->add_setting(
  49. 'color_palette',
  50. array(
  51. 'default' => 'default-palette',
  52. 'capability' => 'edit_theme_options',
  53. 'sanitize_callback' => array( __CLASS__, 'sanitize_color_palette' ),
  54. 'transport' => 'postMessage', // We need this to stop the page refreshing.
  55. )
  56. );
  57. $wp_customize->add_control(
  58. new Color_Palette_Control(
  59. $wp_customize,
  60. 'color_palette',
  61. array(
  62. 'label' => __( 'Color Scheme', 'blockbase' ),
  63. 'description' => __( 'Choose a color scheme for your website.', 'blockbase' ),
  64. 'section' => $this->section_key,
  65. 'choices' => $this->palettes,
  66. 'settings' => 'color_palette',
  67. )
  68. )
  69. );
  70. }
  71. static function sanitize_color_palette( $palette ) {
  72. return sanitize_title( $palette );
  73. }
  74. }
  75. new GlobalStylesColorPalettes;