class-twenty-twenty-one-customize.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * Customizer settings for this theme.
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Twenty_One
  7. * @since Twenty Twenty-One 1.0
  8. */
  9. if ( ! class_exists( 'Twenty_Twenty_One_Customize' ) ) {
  10. /**
  11. * Customizer Settings.
  12. *
  13. * @since Twenty Twenty-One 1.0
  14. */
  15. class Twenty_Twenty_One_Customize {
  16. /**
  17. * Constructor. Instantiate the object.
  18. *
  19. * @since Twenty Twenty-One 1.0
  20. */
  21. public function __construct() {
  22. add_action( 'customize_register', array( $this, 'register' ) );
  23. }
  24. /**
  25. * Register customizer options.
  26. *
  27. * @since Twenty Twenty-One 1.0
  28. *
  29. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  30. * @return void
  31. */
  32. public function register( $wp_customize ) {
  33. // Change site-title & description to postMessage.
  34. $wp_customize->get_setting( 'blogname' )->transport = 'postMessage'; // @phpstan-ignore-line. Assume that this setting exists.
  35. $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage'; // @phpstan-ignore-line. Assume that this setting exists.
  36. // Add partial for blogname.
  37. $wp_customize->selective_refresh->add_partial(
  38. 'blogname',
  39. array(
  40. 'selector' => '.site-title',
  41. 'render_callback' => array( $this, 'partial_blogname' ),
  42. )
  43. );
  44. // Add partial for blogdescription.
  45. $wp_customize->selective_refresh->add_partial(
  46. 'blogdescription',
  47. array(
  48. 'selector' => '.site-description',
  49. 'render_callback' => array( $this, 'partial_blogdescription' ),
  50. )
  51. );
  52. // Add "display_title_and_tagline" setting for displaying the site-title & tagline.
  53. $wp_customize->add_setting(
  54. 'display_title_and_tagline',
  55. array(
  56. 'capability' => 'edit_theme_options',
  57. 'default' => true,
  58. 'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
  59. )
  60. );
  61. // Add control for the "display_title_and_tagline" setting.
  62. $wp_customize->add_control(
  63. 'display_title_and_tagline',
  64. array(
  65. 'type' => 'checkbox',
  66. 'section' => 'title_tagline',
  67. 'label' => esc_html__( 'Display Site Title & Tagline', 'twentytwentyone' ),
  68. )
  69. );
  70. /**
  71. * Add excerpt or full text selector to customizer
  72. */
  73. $wp_customize->add_section(
  74. 'excerpt_settings',
  75. array(
  76. 'title' => esc_html__( 'Excerpt Settings', 'twentytwentyone' ),
  77. 'priority' => 120,
  78. )
  79. );
  80. $wp_customize->add_setting(
  81. 'display_excerpt_or_full_post',
  82. array(
  83. 'capability' => 'edit_theme_options',
  84. 'default' => 'excerpt',
  85. 'sanitize_callback' => static function( $value ) {
  86. return 'excerpt' === $value || 'full' === $value ? $value : 'excerpt';
  87. },
  88. )
  89. );
  90. $wp_customize->add_control(
  91. 'display_excerpt_or_full_post',
  92. array(
  93. 'type' => 'radio',
  94. 'section' => 'excerpt_settings',
  95. 'label' => esc_html__( 'On Archive Pages, posts show:', 'twentytwentyone' ),
  96. 'choices' => array(
  97. 'excerpt' => esc_html__( 'Summary', 'twentytwentyone' ),
  98. 'full' => esc_html__( 'Full text', 'twentytwentyone' ),
  99. ),
  100. )
  101. );
  102. // Background color.
  103. // Include the custom control class.
  104. include_once get_theme_file_path( 'classes/class-twenty-twenty-one-customize-color-control.php' ); // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound
  105. // Register the custom control.
  106. $wp_customize->register_control_type( 'Twenty_Twenty_One_Customize_Color_Control' );
  107. // Get the palette from theme-supports.
  108. $palette = get_theme_support( 'editor-color-palette' );
  109. // Build the colors array from theme-support.
  110. $colors = array();
  111. if ( isset( $palette[0] ) && is_array( $palette[0] ) ) {
  112. foreach ( $palette[0] as $palette_color ) {
  113. $colors[] = $palette_color['color'];
  114. }
  115. }
  116. // Add the control. Overrides the default background-color control.
  117. $wp_customize->add_control(
  118. new Twenty_Twenty_One_Customize_Color_Control(
  119. $wp_customize,
  120. 'background_color',
  121. array(
  122. 'label' => esc_html_x( 'Background color', 'Customizer control', 'twentytwentyone' ),
  123. 'section' => 'colors',
  124. 'palette' => $colors,
  125. )
  126. )
  127. );
  128. }
  129. /**
  130. * Sanitize boolean for checkbox.
  131. *
  132. * @since Twenty Twenty-One 1.0
  133. *
  134. * @param bool $checked Whether or not a box is checked.
  135. * @return bool
  136. */
  137. public static function sanitize_checkbox( $checked = null ) {
  138. return (bool) isset( $checked ) && true === $checked;
  139. }
  140. /**
  141. * Render the site title for the selective refresh partial.
  142. *
  143. * @since Twenty Twenty-One 1.0
  144. *
  145. * @return void
  146. */
  147. public function partial_blogname() {
  148. bloginfo( 'name' );
  149. }
  150. /**
  151. * Render the site tagline for the selective refresh partial.
  152. *
  153. * @since Twenty Twenty-One 1.0
  154. *
  155. * @return void
  156. */
  157. public function partial_blogdescription() {
  158. bloginfo( 'description' );
  159. }
  160. }
  161. }