customizer.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. /**
  3. * Lodestar Theme Customizer.
  4. *
  5. * @package Lodestar
  6. */
  7. /**
  8. * Add postMessage support for site title and description for the Theme Customizer.
  9. *
  10. * @param WP_Customize_Manager $wp_customize Theme Customizer object.
  11. */
  12. function lodestar_customize_register( $wp_customize ) {
  13. $wp_customize->get_setting( 'blogname' )->transport = 'postMessage';
  14. $wp_customize->get_setting( 'blogdescription' )->transport = 'postMessage';
  15. $wp_customize->get_setting( 'header_textcolor' )->transport = 'postMessage';
  16. /**
  17. * Add the Theme Options section
  18. */
  19. $wp_customize->add_panel( 'lodestar_options_panel', array(
  20. 'title' => esc_html__( 'Theme Options', 'lodestar' ),
  21. 'description' => esc_html__( 'Configure your theme settings', 'lodestar' ),
  22. ) );
  23. // Top of site content
  24. $wp_customize->add_section( 'lodestar_top_of_site', array(
  25. 'title' => esc_html__( 'Header Top Text', 'lodestar' ),
  26. 'panel' => 'lodestar_options_panel',
  27. 'description' => esc_html__( 'Add a short bit of content to the top of your website.', 'lodestar' ),
  28. ) );
  29. $wp_customize->add_setting( 'lodestar_header_top_text_1', array(
  30. 'sanitize_callback' => 'wp_kses_post',
  31. ) );
  32. $wp_customize->add_control( 'lodestar_header_top_text_1', array(
  33. 'section' => 'lodestar_top_of_site',
  34. 'type' => 'textarea',
  35. 'priority' => 1,
  36. 'label' => esc_html__( 'Header Top Text 1', 'lodestar' ),
  37. ) );
  38. $wp_customize->add_setting( 'lodestar_header_top_text_2', array(
  39. 'sanitize_callback' => 'wp_kses_post',
  40. ) );
  41. $wp_customize->add_control( 'lodestar_header_top_text_2', array(
  42. 'section' => 'lodestar_top_of_site',
  43. 'type' => 'textarea',
  44. 'priority' => 1,
  45. 'label' => esc_html__( 'Header Top Text 2', 'lodestar' ),
  46. ) );
  47. // Panel 1
  48. $wp_customize->add_section( 'lodestar_panel1', array(
  49. 'title' => esc_html__( 'Panel 1', 'lodestar' ),
  50. 'active_callback' => 'is_front_page',
  51. 'panel' => 'lodestar_options_panel',
  52. 'description' => esc_html__( 'Add an image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'lodestar' ),
  53. ) );
  54. $wp_customize->add_setting( 'lodestar_panel1', array(
  55. 'default' => false,
  56. 'sanitize_callback' => 'lodestar_sanitize_numeric_value',
  57. ) );
  58. $wp_customize->add_control( 'lodestar_panel1', array(
  59. 'label' => esc_html__( 'Panel Content', 'lodestar' ),
  60. 'section' => 'lodestar_panel1',
  61. 'type' => 'dropdown-pages',
  62. ) );
  63. $wp_customize->add_setting( 'lodestar_panel1_layout', array(
  64. 'default' => 'one-column',
  65. 'sanitize_callback' => 'lodestar_sanitize_layout',
  66. ) );
  67. $wp_customize->add_control( 'lodestar_panel1_layout', array(
  68. 'label' => esc_html__( 'Panel Layout', 'lodestar' ),
  69. 'section' => 'lodestar_panel1',
  70. 'type' => 'radio',
  71. 'choices' => array(
  72. 'one-column' => esc_html__( 'One Column', 'lodestar' ),
  73. 'two-column' => esc_html__( 'Two Column', 'lodestar' ),
  74. ),
  75. ) );
  76. // Panel 2
  77. $wp_customize->add_section( 'lodestar_panel2', array(
  78. 'title' => esc_html__( 'Panel 2', 'lodestar' ),
  79. 'active_callback' => 'is_front_page',
  80. 'panel' => 'lodestar_options_panel',
  81. 'description' => esc_html__( 'Add an image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'lodestar' ),
  82. ) );
  83. $wp_customize->add_setting( 'lodestar_panel2', array(
  84. 'default' => false,
  85. 'sanitize_callback' => 'lodestar_sanitize_numeric_value',
  86. ) );
  87. $wp_customize->add_control( 'lodestar_panel2', array(
  88. 'label' => esc_html__( 'Panel Content', 'lodestar' ),
  89. 'section' => 'lodestar_panel2',
  90. 'type' => 'dropdown-pages',
  91. ) );
  92. $wp_customize->add_setting( 'lodestar_panel2_layout', array(
  93. 'default' => 'one-column',
  94. 'sanitize_callback' => 'lodestar_sanitize_layout',
  95. ) );
  96. $wp_customize->add_control( 'lodestar_panel2_layout', array(
  97. 'label' => esc_html__( 'Panel Layout', 'lodestar' ),
  98. 'section' => 'lodestar_panel2',
  99. 'type' => 'radio',
  100. 'choices' => array(
  101. 'one-column' => esc_html__( 'One Column', 'lodestar' ),
  102. 'two-column' => esc_html__( 'Two Column', 'lodestar' ),
  103. ),
  104. ) );
  105. // Panel 3
  106. $wp_customize->add_section( 'lodestar_panel3', array(
  107. 'title' => esc_html__( 'Panel 3', 'lodestar' ),
  108. 'active_callback' => 'is_front_page',
  109. 'panel' => 'lodestar_options_panel',
  110. 'description' => esc_html__( 'Add an image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'lodestar' ),
  111. ) );
  112. $wp_customize->add_setting( 'lodestar_panel3', array(
  113. 'default' => false,
  114. 'sanitize_callback' => 'lodestar_sanitize_numeric_value',
  115. ) );
  116. $wp_customize->add_control( 'lodestar_panel3', array(
  117. 'label' => esc_html__( 'Panel Content', 'lodestar' ),
  118. 'section' => 'lodestar_panel3',
  119. 'type' => 'dropdown-pages',
  120. ) );
  121. $wp_customize->add_setting( 'lodestar_panel3_layout', array(
  122. 'default' => 'one-column',
  123. 'sanitize_callback' => 'lodestar_sanitize_layout',
  124. ) );
  125. $wp_customize->add_control( 'lodestar_panel3_layout', array(
  126. 'label' => esc_html__( 'Panel Layout', 'lodestar' ),
  127. 'section' => 'lodestar_panel3',
  128. 'type' => 'radio',
  129. 'choices' => array(
  130. 'one-column' => esc_html__( 'One Column', 'lodestar' ),
  131. 'two-column' => esc_html__( 'Two Column', 'lodestar' ),
  132. ),
  133. ) );
  134. // Panel 4
  135. $wp_customize->add_section( 'lodestar_panel4', array(
  136. 'title' => esc_html__( 'Panel 4', 'lodestar' ),
  137. 'active_callback' => 'is_front_page',
  138. 'panel' => 'lodestar_options_panel',
  139. 'description' => esc_html__( 'Add an image to your panel by setting a featured image in the page editor. If you don&rsquo;t select a page, this panel will not be displayed.', 'lodestar' ),
  140. ) );
  141. $wp_customize->add_setting( 'lodestar_panel4', array(
  142. 'default' => false,
  143. 'sanitize_callback' => 'lodestar_sanitize_numeric_value',
  144. ) );
  145. $wp_customize->add_control( 'lodestar_panel4', array(
  146. 'label' => esc_html__( 'Panel Content', 'lodestar' ),
  147. 'section' => 'lodestar_panel4',
  148. 'type' => 'dropdown-pages',
  149. ) );
  150. $wp_customize->add_setting( 'lodestar_panel4_layout', array(
  151. 'default' => 'one-column',
  152. 'sanitize_callback' => 'lodestar_sanitize_layout',
  153. ) );
  154. $wp_customize->add_control( 'lodestar_panel4_layout', array(
  155. 'label' => esc_html__( 'Panel Layout', 'lodestar' ),
  156. 'section' => 'lodestar_panel4',
  157. 'type' => 'radio',
  158. 'choices' => array(
  159. 'one-column' => esc_html__( 'One Column', 'lodestar' ),
  160. 'two-column' => esc_html__( 'Two Column', 'lodestar' ),
  161. ),
  162. ) );
  163. // Footer Image
  164. $wp_customize->add_section( 'lodestar_footer_settings', array(
  165. 'title' => esc_html__( 'Footer Image', 'lodestar' ),
  166. 'panel' => 'lodestar_options_panel',
  167. 'default' => '',
  168. ) );
  169. $wp_customize->add_setting('lodestar_footer_image', array(
  170. 'transport' => 'refresh',
  171. 'sanitize_callback' => 'esc_url_raw',
  172. ) );
  173. $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize,
  174. 'lodestar_footer_image', array(
  175. 'label' => esc_html__( 'Footer Image', 'lodestar' ),
  176. 'section' => 'lodestar_footer_settings',
  177. 'description' => esc_html__( 'Add an image to be displayed at the bottom of the Front Page template, above the footer.', 'lodestar' ),
  178. ) ) );
  179. }
  180. add_action( 'customize_register', 'lodestar_customize_register' );
  181. /**
  182. * Sanitize a numeric value
  183. */
  184. function lodestar_sanitize_numeric_value( $input ) {
  185. if ( is_numeric( $input ) ) {
  186. return intval( $input );
  187. } else {
  188. return 0;
  189. }
  190. }
  191. /**
  192. * Sanitize a radio button
  193. */
  194. function lodestar_sanitize_layout( $input ) {
  195. $valid = array(
  196. 'one-column' => esc_html__( 'One Column', 'lodestar' ),
  197. 'two-column' => esc_html__( 'Two Column', 'lodestar' ),
  198. );
  199. if ( array_key_exists( $input, $valid ) ) {
  200. return $input;
  201. } else {
  202. return '';
  203. }
  204. }
  205. /**
  206. * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
  207. */
  208. function lodestar_customize_preview_js() {
  209. wp_enqueue_script( 'lodestar_customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'customize-preview' ), '20151215', true );
  210. }
  211. add_action( 'customize_preview_init', 'lodestar_customize_preview_js' );
  212. /**
  213. * Some extra JavaScript to improve the user experience in the Customizer for this theme.
  214. */
  215. function lodestar_panels_js() {
  216. wp_enqueue_script( 'lodestar_extra_js', get_template_directory_uri() . '/assets/js/panel-customizer.js', array(), '20151116', true );
  217. }
  218. add_action( 'customize_controls_enqueue_scripts', 'lodestar_panels_js' );