customizer.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. /**
  3. * _s Theme Customizer.
  4. *
  5. * @package Affinity
  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 affinity_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( 'affinity_theme_options', array(
  20. 'title' => esc_html__( 'Theme Options', 'affinity' ),
  21. ) );
  22. // Panel 1
  23. $wp_customize->add_section( 'affinity_panel1', array(
  24. 'title' => esc_html__( 'Panel 1', 'affinity' ),
  25. 'active_callback' => 'is_front_page',
  26. 'panel' => 'affinity_theme_options',
  27. 'description' => esc_html__( 'Add a background 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.', 'affinity' ),
  28. ) );
  29. $wp_customize->add_setting( 'affinity_panel1', array(
  30. 'default' => false,
  31. 'sanitize_callback' => 'affinity_sanitize_numeric_value',
  32. ) );
  33. $wp_customize->add_control( 'affinity_panel1', array(
  34. 'label' => esc_html__( 'Panel Content', 'affinity' ),
  35. 'section' => 'affinity_panel1',
  36. 'type' => 'dropdown-pages',
  37. ) );
  38. // Panel 2
  39. $wp_customize->add_section( 'affinity_panel2', array(
  40. 'title' => esc_html__( 'Panel 2', 'affinity' ),
  41. 'active_callback' => 'is_front_page',
  42. 'panel' => 'affinity_theme_options',
  43. 'description' => esc_html__( 'Add a background 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.', 'affinity' ),
  44. ) );
  45. $wp_customize->add_setting( 'affinity_panel2', array(
  46. 'default' => false,
  47. 'sanitize_callback' => 'affinity_sanitize_numeric_value',
  48. ) );
  49. $wp_customize->add_control( 'affinity_panel2', array(
  50. 'label' => esc_html__( 'Panel Content', 'affinity' ),
  51. 'section' => 'affinity_panel2',
  52. 'type' => 'dropdown-pages',
  53. ) );
  54. // Panel 3
  55. $wp_customize->add_section( 'affinity_panel3', array(
  56. 'title' => esc_html__( 'Panel 3', 'affinity' ),
  57. 'active_callback' => 'is_front_page',
  58. 'panel' => 'affinity_theme_options',
  59. 'description' => esc_html__( 'Add a background 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.', 'affinity' ),
  60. ) );
  61. $wp_customize->add_setting( 'affinity_panel3', array(
  62. 'default' => false,
  63. 'sanitize_callback' => 'affinity_sanitize_numeric_value',
  64. ) );
  65. $wp_customize->add_control( 'affinity_panel3', array(
  66. 'label' => esc_html__( 'Panel Content', 'affinity' ),
  67. 'section' => 'affinity_panel3',
  68. 'type' => 'dropdown-pages',
  69. ) );
  70. // Panel 4
  71. $wp_customize->add_section( 'affinity_panel4', array(
  72. 'title' => esc_html__( 'Panel 4', 'affinity' ),
  73. 'active_callback' => 'is_front_page',
  74. 'panel' => 'affinity_theme_options',
  75. 'description' => esc_html__( 'Add a background 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.', 'affinity' ),
  76. ) );
  77. $wp_customize->add_setting( 'affinity_panel4', array(
  78. 'default' => false,
  79. 'sanitize_callback' => 'affinity_sanitize_numeric_value',
  80. ) );
  81. $wp_customize->add_control( 'affinity_panel4', array(
  82. 'label' => esc_html__( 'Panel Content', 'affinity' ),
  83. 'section' => 'affinity_panel4',
  84. 'type' => 'dropdown-pages',
  85. ) );
  86. // Panel 5
  87. $wp_customize->add_section( 'affinity_panel5', array(
  88. 'title' => esc_html__( 'Panel 5', 'affinity' ),
  89. 'active_callback' => 'is_front_page',
  90. 'panel' => 'affinity_theme_options',
  91. 'description' => esc_html__( 'Add a background 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.', 'affinity' ),
  92. ) );
  93. $wp_customize->add_setting( 'affinity_panel5', array(
  94. 'default' => false,
  95. 'sanitize_callback' => 'affinity_sanitize_numeric_value',
  96. ) );
  97. $wp_customize->add_control( 'affinity_panel5', array(
  98. 'label' => esc_html__( 'Panel Content', 'affinity' ),
  99. 'section' => 'affinity_panel5',
  100. 'type' => 'dropdown-pages',
  101. ) );
  102. //General Options
  103. $wp_customize->add_section( 'affinity_general_options', array(
  104. 'title' => esc_html__( 'General', 'affinity' ),
  105. 'panel' => 'affinity_theme_options',
  106. 'priority' => 1,
  107. ) );
  108. $wp_customize->add_setting( 'affinity_overlay', array(
  109. 'default' => '0.7',
  110. 'sanitize_callback' => 'affinity_sanitize_overlay',
  111. 'transport' => 'postMessage',
  112. ) );
  113. $wp_customize->add_control( 'affinity_overlay', array(
  114. 'label' => esc_html__( 'Header Image Opacity', 'affinity' ),
  115. 'section' => 'affinity_general_options',
  116. 'type' => 'select',
  117. 'choices' => array(
  118. '0.1' => '10%',
  119. '0.2' => '20%',
  120. '0.3' => '30%',
  121. '0.4' => '40%',
  122. '0.5' => '50%',
  123. '0.6' => '60%',
  124. '0.7' => '70%',
  125. '0.8' => '80%',
  126. '0.9' => '90%',
  127. '1.0' => '100%',
  128. ),
  129. ) );
  130. $wp_customize->add_setting( 'affinity_scrolling', array(
  131. 'default' => 1,
  132. 'sanitize_callback' => 'affinity_sanitize_boolean',
  133. 'transport' => 'postMessage',
  134. ) );
  135. $wp_customize->add_control( 'affinity_scrolling', array(
  136. 'label' => esc_html__( 'Allow Featured Images to scroll in the background on large screens', 'affinity' ),
  137. 'section' => 'affinity_general_options',
  138. 'type' => 'checkbox',
  139. ) );
  140. }
  141. add_action( 'customize_register', 'affinity_customize_register' );
  142. /**
  143. * Sanitize a numeric value
  144. */
  145. function affinity_sanitize_numeric_value( $input ) {
  146. if ( is_numeric( $input ) ) {
  147. return intval( $input );
  148. } else {
  149. return 0;
  150. }
  151. }
  152. /*
  153. * Sanitize a boolean value
  154. */
  155. function affinity_sanitize_boolean( $input ) {
  156. if ( is_bool( $input ) ) {
  157. return $input;
  158. } else {
  159. return true;
  160. }
  161. }
  162. /* Sanitize overlay setting */
  163. function affinity_sanitize_overlay( $input ) {
  164. $choices = array(
  165. '0.1',
  166. '0.2',
  167. '0.3',
  168. '0.4',
  169. '0.5',
  170. '0.6',
  171. '0.7',
  172. '0.8',
  173. '0.9',
  174. '1.0',
  175. );
  176. if ( ! in_array( $input, $choices ) ) {
  177. $input = '0.7';
  178. }
  179. return $input;
  180. }
  181. /**
  182. * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
  183. */
  184. function affinity_customize_preview_js() {
  185. wp_enqueue_script( 'affinity-customizer', get_template_directory_uri() . '/assets/js/customizer.js', array( 'customize-preview' ), '20151215', true );
  186. }
  187. add_action( 'customize_preview_init', 'affinity_customize_preview_js' );
  188. /**
  189. * Allow users to scroll to the section they're editing
  190. */
  191. function affinity_panels_js() {
  192. wp_enqueue_script( 'affinity-customizer-panels', get_template_directory_uri() . '/assets/js/customizer-panels.js', array(), '20160328', true );
  193. }
  194. add_action( 'customize_controls_enqueue_scripts', 'affinity_panels_js' );