customizer.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * Twenty Nineteen: Customizer
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Nineteen
  7. * @since 1.0.0
  8. */
  9. /**
  10. * Add postMessage support for site title and description for the Theme Customizer.
  11. *
  12. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  13. */
  14. function twentynineteen_customize_register( $wp_customize ) {
  15. $wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
  16. $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
  17. $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
  18. if ( isset( $wp_customize->selective_refresh ) ) {
  19. $wp_customize->selective_refresh->add_partial(
  20. 'blogname',
  21. array(
  22. 'selector' => '.site-title a',
  23. 'render_callback' => 'twentynineteen_customize_partial_blogname',
  24. )
  25. );
  26. $wp_customize->selective_refresh->add_partial(
  27. 'blogdescription',
  28. array(
  29. 'selector' => '.site-description',
  30. 'render_callback' => 'twentynineteen_customize_partial_blogdescription',
  31. )
  32. );
  33. }
  34. /**
  35. * Primary color.
  36. */
  37. $wp_customize->add_setting(
  38. 'primary_color',
  39. array(
  40. 'default' => 'default',
  41. 'transport' => 'postMessage',
  42. 'sanitize_callback' => 'twentynineteen_sanitize_color_option',
  43. )
  44. );
  45. $wp_customize->add_control(
  46. 'primary_color',
  47. array(
  48. 'type' => 'radio',
  49. 'label' => __( 'Primary Color', 'twentynineteen' ),
  50. 'choices' => array(
  51. 'default' => _x( 'Default', 'primary color', 'twentynineteen' ),
  52. 'custom' => _x( 'Custom', 'primary color', 'twentynineteen' ),
  53. ),
  54. 'section' => 'colors',
  55. 'priority' => 5,
  56. )
  57. );
  58. // Add primary color hue setting and control.
  59. $wp_customize->add_setting(
  60. 'primary_color_hue',
  61. array(
  62. 'default' => 199,
  63. 'transport' => 'postMessage',
  64. 'sanitize_callback' => 'absint',
  65. )
  66. );
  67. $wp_customize->add_control(
  68. new WP_Customize_Color_Control(
  69. $wp_customize,
  70. 'primary_color_hue',
  71. array(
  72. 'description' => __( 'Apply a custom color for buttons, links, featured images, etc.', 'twentynineteen' ),
  73. 'section' => 'colors',
  74. 'mode' => 'hue',
  75. )
  76. )
  77. );
  78. // Add image filter setting and control.
  79. $wp_customize->add_setting(
  80. 'image_filter',
  81. array(
  82. 'default' => 1,
  83. 'sanitize_callback' => 'absint',
  84. 'transport' => 'postMessage',
  85. )
  86. );
  87. $wp_customize->add_control(
  88. 'image_filter',
  89. array(
  90. 'label' => __( 'Apply a filter to featured images using the primary color', 'twentynineteen' ),
  91. 'section' => 'colors',
  92. 'type' => 'checkbox',
  93. )
  94. );
  95. }
  96. add_action( 'customize_register', 'twentynineteen_customize_register' );
  97. /**
  98. * Render the site title for the selective refresh partial.
  99. *
  100. * @return void
  101. */
  102. function twentynineteen_customize_partial_blogname() {
  103. bloginfo( 'name' );
  104. }
  105. /**
  106. * Render the site tagline for the selective refresh partial.
  107. *
  108. * @return void
  109. */
  110. function twentynineteen_customize_partial_blogdescription() {
  111. bloginfo( 'description' );
  112. }
  113. /**
  114. * Bind JS handlers to instantly live-preview changes.
  115. */
  116. function twentynineteen_customize_preview_js() {
  117. wp_enqueue_script( 'twentynineteen-customize-preview', get_theme_file_uri( '/js/customize-preview.js' ), array( 'customize-preview' ), '20181214', true );
  118. }
  119. add_action( 'customize_preview_init', 'twentynineteen_customize_preview_js' );
  120. /**
  121. * Load dynamic logic for the customizer controls area.
  122. */
  123. function twentynineteen_panels_js() {
  124. wp_enqueue_script( 'twentynineteen-customize-controls', get_theme_file_uri( '/js/customize-controls.js' ), array(), '20181214', true );
  125. }
  126. add_action( 'customize_controls_enqueue_scripts', 'twentynineteen_panels_js' );
  127. /**
  128. * Sanitize custom color choice.
  129. *
  130. * @param string $choice Whether image filter is active.
  131. *
  132. * @return string
  133. */
  134. function twentynineteen_sanitize_color_option( $choice ) {
  135. $valid = array(
  136. 'default',
  137. 'custom',
  138. );
  139. if ( in_array( $choice, $valid, true ) ) {
  140. return $choice;
  141. }
  142. return 'default';
  143. }