customizer.php 5.8 KB

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