customizer.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php declare( strict_types = 1 ); ?>
  2. <?php
  3. /**
  4. * Seedlet Theme: Customizer
  5. *
  6. * @package Seedlet
  7. * @since 1.0.0
  8. */
  9. if ( ! class_exists( 'Seedlet_Customize' ) ) {
  10. /**
  11. * Customizer Settings.
  12. *
  13. * @since 1.0.0
  14. */
  15. class Seedlet_Customize {
  16. /**
  17. * Constructor. Instantiate the object.
  18. *
  19. * @access public
  20. *
  21. * @since 1.0.0
  22. */
  23. public function __construct() {
  24. add_action( 'customize_register', array( $this, 'register' ) );
  25. }
  26. /**
  27. * Register customizer options.
  28. *
  29. * @access public
  30. *
  31. * @since 1.0.0
  32. *
  33. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  34. *
  35. * @return void
  36. */
  37. public function register( $wp_customize ) {
  38. // Change site-title & description to postMessage.
  39. $wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
  40. $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
  41. // Add partial for blogname.
  42. $wp_customize->selective_refresh->add_partial(
  43. 'blogname',
  44. array(
  45. 'selector' => '.site-title',
  46. 'render_callback' => array( $this, 'partial_blogname' ),
  47. )
  48. );
  49. // Add partial for blogdescription.
  50. $wp_customize->selective_refresh->add_partial(
  51. 'blogdescription',
  52. array(
  53. 'selector' => '.site-description',
  54. 'render_callback' => array( $this, 'partial_blogdescription' ),
  55. )
  56. );
  57. /**
  58. * Add excerpt or full text selector to customizer
  59. */
  60. $wp_customize->add_section(
  61. 'excerpt_settings',
  62. array(
  63. 'title' => esc_html__( 'Excerpt Settings', 'seedlet' ),
  64. 'priority' => 120,
  65. )
  66. );
  67. $wp_customize->add_setting(
  68. 'index_display_excerpt_or_full_post',
  69. array(
  70. 'capability' => 'edit_theme_options',
  71. 'default' => 'full',
  72. 'sanitize_callback' => function( $value ) {
  73. return 'excerpt' === $value || 'full' === $value ? $value : 'excerpt';
  74. },
  75. )
  76. );
  77. $wp_customize->add_setting(
  78. 'archive_display_excerpt_or_full_post',
  79. array(
  80. 'capability' => 'edit_theme_options',
  81. 'default' => 'full',
  82. 'sanitize_callback' => function( $value ) {
  83. return 'excerpt' === $value || 'full' === $value ? $value : 'excerpt';
  84. },
  85. )
  86. );
  87. $wp_customize->add_control(
  88. 'index_display_excerpt_or_full_post',
  89. array(
  90. 'type' => 'radio',
  91. 'section' => 'excerpt_settings',
  92. 'label' => esc_html__( 'On the home page, posts show:', 'seedlet' ),
  93. 'choices' => array(
  94. 'excerpt' => esc_html__( 'Summary', 'seedlet' ),
  95. 'full' => esc_html__( 'Full text', 'seedlet' ),
  96. ),
  97. )
  98. );
  99. $wp_customize->add_control(
  100. 'archive_display_excerpt_or_full_post',
  101. array(
  102. 'type' => 'radio',
  103. 'section' => 'excerpt_settings',
  104. 'label' => esc_html__( 'On archive pages, posts show:', 'seedlet' ),
  105. 'choices' => array(
  106. 'excerpt' => esc_html__( 'Summary', 'seedlet' ),
  107. 'full' => esc_html__( 'Full text', 'seedlet' ),
  108. ),
  109. )
  110. );
  111. // Add "display_title_and_tagline" setting for displaying the site-title & tagline.
  112. $wp_customize->add_setting(
  113. 'display_title_and_tagline',
  114. array(
  115. 'capability' => 'edit_theme_options',
  116. 'default' => true,
  117. 'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
  118. )
  119. );
  120. // Add control for the "display_title_and_tagline" setting.
  121. $wp_customize->add_control(
  122. 'display_title_and_tagline',
  123. array(
  124. 'type' => 'checkbox',
  125. 'section' => 'title_tagline',
  126. 'label' => esc_html__( 'Display Site Title & Tagline', 'seedlet' ),
  127. )
  128. );
  129. // Add setting to hide the site header on the homepage.
  130. $wp_customize->add_setting( 'hide_site_header', array(
  131. 'default' => false,
  132. 'type' => 'theme_mod',
  133. 'transport' => 'refresh',
  134. 'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
  135. ) );
  136. // Add control to hide the site header on the homepage.
  137. $wp_customize->add_control( 'hide_site_header', array(
  138. 'label' => esc_html__( 'Hide the Site Header', 'seedlet' ),
  139. 'description' => esc_html__( 'Check to hide the site header, if your homepage is set to display a static page.', 'seedlet' ),
  140. 'section' => 'static_front_page',
  141. 'priority' => 10,
  142. 'type' => 'checkbox',
  143. 'settings' => 'hide_site_header',
  144. ) );
  145. // Add setting to hide footer elements on the homepage.
  146. $wp_customize->add_setting( 'hide_site_footer', array(
  147. 'default' => false,
  148. 'type' => 'theme_mod',
  149. 'transport' => 'refresh',
  150. 'sanitize_callback' => array( __CLASS__, 'sanitize_checkbox' ),
  151. ) );
  152. // Add control to hide footer elements on the homepage.
  153. $wp_customize->add_control( 'hide_site_footer', array(
  154. 'label' => esc_html__( 'Hide the Site Footer Menu & Widgets', 'seedlet' ),
  155. 'description' => esc_html__( 'Check to hide the site menu & widgets in the footer, if your homepage is set to display a static page.', 'seedlet' ),
  156. 'section' => 'static_front_page',
  157. 'priority' => 10,
  158. 'type' => 'checkbox',
  159. 'settings' => 'hide_site_footer',
  160. ) );
  161. }
  162. /**
  163. * Render the site title for the selective refresh partial.
  164. *
  165. * @access public
  166. *
  167. * @since 1.0.0
  168. *
  169. * @return void
  170. */
  171. public function partial_blogname() {
  172. bloginfo( 'name' );
  173. }
  174. /**
  175. * Render the site tagline for the selective refresh partial.
  176. *
  177. * @access public
  178. *
  179. * @since 1.0.0
  180. *
  181. * @return void
  182. */
  183. public function partial_blogdescription() {
  184. bloginfo( 'description' );
  185. }
  186. /**
  187. * Sanitize boolean for checkbox.
  188. *
  189. * @access public
  190. *
  191. * @since 1.0.0
  192. *
  193. * @param bool $checked Whether or not a box is checked.
  194. *
  195. * @return bool
  196. */
  197. public static function sanitize_checkbox( $checked = null ) {
  198. return (bool) isset( $checked ) && true === $checked;
  199. }
  200. }
  201. new Seedlet_Customize();
  202. }