customization.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. require_once 'wp-customize-global-styles-setting.php';
  3. class GlobalStylesCustomizer {
  4. private $user_color_palette;
  5. function __construct() {
  6. add_action( 'customize_register', array( $this, 'initialize' ) );
  7. add_action( 'customize_preview_init', array( $this, 'customize_preview_js' ) );
  8. add_action( 'wp_enqueue_scripts', array( $this, 'create_customization_style_element' ) );
  9. add_action( 'customize_save_after', array( $this, 'handle_customize_save_after' ) );
  10. }
  11. function customize_preview_js() {
  12. wp_enqueue_script( 'customizer-preview-color', get_template_directory_uri() . '/inc/customizer-preview.js', array( 'customize-preview' ) );
  13. wp_localize_script( 'customizer-preview-color', 'userColorPalette', $this->user_color_palette );
  14. }
  15. function create_customization_style_element() {
  16. wp_enqueue_style( 'global-styles-customizations', ' ', array( 'global-styles' ) ); // This needs to load after global_styles, hence the dependency
  17. wp_add_inline_style( 'global-styles-customizations', '{}' );
  18. }
  19. function initialize( $wp_customize ) {
  20. $this->user_color_palette = $this->build_user_color_palette();
  21. $this->register_color_controls( $wp_customize, $this->user_color_palette );
  22. }
  23. function build_user_color_palette() {
  24. // Get the merged theme.json.
  25. $theme_json = WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()->get_raw_data();
  26. $theme_color_palette = $theme_json['settings']['color']['palette']['theme'];
  27. $user_color_palette = $theme_json['settings']['color']['palette']['theme'];
  28. // Create a user color palette from user settings, if there are any.
  29. if ( isset( $theme_json['settings']['color']['palette']['user'] ) ) {
  30. $user_color_palette = $theme_json['settings']['color']['palette']['user'];
  31. }
  32. // Combine theme settings with user settings.
  33. foreach ( $user_color_palette as $key => $palette_item ) {
  34. $user_color_palette[ $key ]['default'] = $this->get_theme_default_color_value( $palette_item['slug'], $theme_color_palette );
  35. }
  36. return $user_color_palette;
  37. }
  38. function get_theme_default_color_value( $slug, $palette ) {
  39. foreach ( $palette as $palette_item ) {
  40. if ( $palette_item['slug'] === $slug ) {
  41. return $palette_item['color'];
  42. }
  43. }
  44. return null;
  45. }
  46. function register_color_controls( $wp_customize, $palette ) {
  47. $section_key = 'customize-global-styles';
  48. //Add a Section to the Customizer for these bits
  49. $wp_customize->add_section(
  50. $section_key,
  51. array(
  52. 'capability' => 'edit_theme_options',
  53. 'description' => __( 'Color Customization for Quadrat' ),
  54. 'title' => __( 'Colors' ),
  55. )
  56. );
  57. foreach ( $palette as $palette_item ) {
  58. $this->register_color_control( $wp_customize, $palette_item, $section_key );
  59. }
  60. }
  61. function register_color_control( $wp_customize, $palette_item, $section_key ) {
  62. $setting_key = $section_key . $palette_item['slug'];
  63. $global_styles_setting = new WP_Customize_Global_Styles_Setting(
  64. $wp_customize,
  65. $setting_key,
  66. array(
  67. 'default' => $palette_item['default'],
  68. 'sanitize_callback' => 'sanitize_hex_color',
  69. 'slug' => $palette_item['slug'],
  70. 'user_value' => $palette_item['color'],
  71. )
  72. );
  73. $wp_customize->add_setting( $global_styles_setting );
  74. $wp_customize->add_control(
  75. new WP_Customize_Color_Control(
  76. $wp_customize,
  77. $setting_key,
  78. array(
  79. 'section' => $section_key,
  80. 'label' => $palette_item['name'],
  81. )
  82. )
  83. );
  84. }
  85. function handle_customize_save_after( $manager ) {
  86. //update the palette based on the controls
  87. foreach ( $this->user_color_palette as $key => $palette_item ) {
  88. $setting = $manager->get_setting( 'customize-global-styles' . $palette_item['slug'] );
  89. if ( isset( $setting->new_value ) ) {
  90. $this->user_color_palette[ $key ]['color'] = $setting->new_value;
  91. }
  92. }
  93. // Get the user's theme.json from the CPT.
  94. $user_custom_post_type_id = WP_Theme_JSON_Resolver_Gutenberg::get_user_custom_post_type_id();
  95. $user_theme_json_post = get_post( $user_custom_post_type_id );
  96. $user_theme_json_post_content = json_decode( $user_theme_json_post->post_content );
  97. // Create the color palette inside settings if it doesn't exist.
  98. if ( property_exists( $user_theme_json_post_content, 'settings' ) ) {
  99. if ( property_exists( $user_theme_json_post_content->settings, 'color' ) ) {
  100. $user_theme_json_post_content->settings->color->palette = $this->user_color_palette;
  101. } else {
  102. $user_theme_json_post_content->settings->color = (object) array( 'palette' => $this->user_color_palette );
  103. }
  104. } else {
  105. $user_theme_json_post_content->settings = (object) array( 'color' => (object) array( 'palette' => $this->user_color_palette ) );
  106. }
  107. // Update the theme.json with the new settings.
  108. $user_theme_json_post->post_content = json_encode( $user_theme_json_post_content );
  109. return wp_update_post( $user_theme_json_post );
  110. }
  111. }
  112. new GlobalStylesCustomizer;