contact-info-customizer.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. /**
  3. * Radcliffe 2 Theme Customizer - Contact Info
  4. *
  5. * @package Radcliffe 2
  6. */
  7. function radcliffe_2_contact_info_customize_register( $wp_customize ) {
  8. /* Add Section for Contact Info */
  9. $wp_customize->add_section( 'radcliffe_2_contact_info', array(
  10. 'title' => esc_html__( 'Contact Info', 'radcliffe-2' ),
  11. 'description' => esc_html__( 'Display your location, hours, and contact information.', 'radcliffe-2' ),
  12. 'priority' => 13,
  13. ) );
  14. /* Display in Header */
  15. $wp_customize->add_setting( 'radcliffe_2_contact_info_location', array(
  16. 'default' => 'header',
  17. 'sanitize_callback' => 'radcliffe_2_contact_info_sanitize_location',
  18. 'transport' => 'postMessage',
  19. ) );
  20. $wp_customize->add_control( 'radcliffe_2_contact_info_location', array(
  21. 'label' => esc_html__( 'Where would you like to display your contact information?', 'radcliffe-2' ),
  22. 'section' => 'radcliffe_2_contact_info',
  23. 'type' => 'radio',
  24. 'choices' => array(
  25. 'header' => esc_html__( 'In the header', 'radcliffe-2' ),
  26. 'footer' => esc_html__( 'In the footer', 'radcliffe-2' ),
  27. ),
  28. ) );
  29. /* Address */
  30. $wp_customize->add_setting( 'site_contact_info[address]', array(
  31. 'type' => 'option',
  32. 'sanitize_callback' => 'radcliffe_2_contact_info_sanitize_address',
  33. 'sanitize_js_callback' => radcliffe_2_generate_contact_compat_callback( 'address' ),
  34. 'transport' => 'postMessage',
  35. ) );
  36. $wp_customize->add_control( 'site_contact_info[address]', array(
  37. 'label' => esc_html__( 'Address', 'radcliffe-2' ),
  38. 'section' => 'radcliffe_2_contact_info',
  39. 'type' => 'textarea',
  40. 'input_attrs' => array(
  41. 'placeholder' => esc_attr__( '123 Main St, Somewhere XY, 98765', 'radcliffe-2' ),
  42. ),
  43. ) );
  44. /* Phone */
  45. $wp_customize->add_setting( 'site_contact_info[phone]', array(
  46. 'type' => 'option',
  47. 'sanitize_callback' => 'radcliffe_2_contact_info_sanitize_phone',
  48. 'sanitize_js_callback' => radcliffe_2_generate_contact_compat_callback( 'phone' ),
  49. 'transport' => 'postMessage',
  50. ) );
  51. $wp_customize->add_control( 'site_contact_info[phone]', array(
  52. 'label' => esc_html__( 'Phone', 'radcliffe-2' ),
  53. 'section' => 'radcliffe_2_contact_info',
  54. 'type' => 'text',
  55. 'input_attrs' => array(
  56. 'placeholder' => esc_attr__( '(555) 123-4567', 'radcliffe-2' ),
  57. ),
  58. ) );
  59. /* Email */
  60. $wp_customize->add_setting( 'site_contact_info[email]', array(
  61. 'type' => 'option',
  62. 'sanitize_callback' => 'radcliffe_2_contact_info_sanitize_email',
  63. 'sanitize_js_callback' => radcliffe_2_generate_contact_compat_callback( 'email' ),
  64. 'transport' => 'postMessage',
  65. ) );
  66. $wp_customize->add_control( 'site_contact_info[email]', array(
  67. 'label' => esc_html__( 'Email', 'radcliffe-2' ),
  68. 'section' => 'radcliffe_2_contact_info',
  69. 'type' => 'email',
  70. 'input_attrs' => array(
  71. 'placeholder' => esc_attr__( 'email@example.com', 'radcliffe-2' ),
  72. ),
  73. ) );
  74. /* Hours */
  75. $wp_customize->add_setting( 'radcliffe_2_contact_info_hours', array(
  76. 'sanitize_callback' => 'sanitize_text_field',
  77. 'transport' => 'postMessage',
  78. ) );
  79. $wp_customize->add_control( 'radcliffe_2_contact_info_hours', array(
  80. 'label' => esc_html__( 'Hours', 'radcliffe-2' ),
  81. 'section' => 'radcliffe_2_contact_info',
  82. 'type' => 'textarea',
  83. 'input_attrs' => array(
  84. 'placeholder' => esc_attr__( 'M-F: 9-5pm - Weekends: 10-4pm', 'radcliffe-2' ),
  85. ),
  86. ) );
  87. }
  88. add_action( 'customize_register', 'radcliffe_2_contact_info_customize_register' );
  89. /**
  90. * For use in `sanitize_js_callback` params to filter in our old,
  91. * theme-mod based options. Using a generator because repetition is boring.
  92. *
  93. * @param string $name The field we're maybe filtering in
  94. * @return function A function for use in `sanitize_js_callback`
  95. */
  96. function radcliffe_2_generate_contact_compat_callback( $name ) {
  97. return function( $value ) use ( $name ) {
  98. // if there's a value, we're fine
  99. if ( ! empty( $value ) ) {
  100. return $value;
  101. }
  102. // pull from the old theme mod value
  103. $option = get_theme_mod( 'radcliffe_2_contact_info_' . $name );
  104. if ( ! empty( $option ) ) {
  105. return $option;
  106. }
  107. // nothing to see here
  108. return $value;
  109. };
  110. }
  111. /**
  112. * Sanitize the checkbox.
  113. *
  114. * @param boolean $input.
  115. * @return boolean
  116. */
  117. function radcliffe_2_contact_info_sanitize_checkbox( $input ) {
  118. return ( 1 == $input ? true : false );
  119. }
  120. /**
  121. * Sanitize the Contact Info address value and remove legacy theme_mod value,
  122. * since we're saving the values in blog options as of r52410.
  123. *
  124. * @param string $value.
  125. * @return string.
  126. */
  127. function radcliffe_2_contact_info_sanitize_address( $value ) {
  128. if ( ! empty( get_theme_mod( 'radcliffe_2_contact_info_address' ) ) ) {
  129. remove_theme_mod( 'radcliffe_2_contact_info_address' );
  130. }
  131. return sanitize_text_field( $value );
  132. }
  133. /**
  134. * Sanitize the Contact Info phone value and remove legacy theme_mod value,
  135. * since we're saving the values in blog options as of r52410.
  136. *
  137. * @param string $value.
  138. * @return string.
  139. */
  140. function radcliffe_2_contact_info_sanitize_phone( $value ) {
  141. if ( ! empty( get_theme_mod( 'radcliffe_2_contact_info_phone' ) ) ) {
  142. remove_theme_mod( 'radcliffe_2_contact_info_phone' );
  143. }
  144. return sanitize_text_field( $value );
  145. }
  146. /**
  147. * Sanitize the Contact Info email value and remove legacy theme_mod value,
  148. * since we're saving the values in blog options as of r52410.
  149. *
  150. * @param string $value.
  151. * @return string.
  152. */
  153. function radcliffe_2_contact_info_sanitize_email( $value ) {
  154. if ( ! empty( get_theme_mod( 'radcliffe_2_contact_info_email' ) ) ) {
  155. remove_theme_mod( 'radcliffe_2_contact_info_email' );
  156. }
  157. return sanitize_email( $value );
  158. }
  159. /**
  160. * Sanitize the Contact Info location value.
  161. *
  162. * @param string $display.
  163. * @return string.
  164. */
  165. function radcliffe_2_contact_info_sanitize_location( $display ) {
  166. if ( ! in_array( $display, array( 'header', 'footer' ) ) ) {
  167. $display = 'header';
  168. }
  169. return $display;
  170. }
  171. /**
  172. * Binds JS handlers to make Theme Customizer preview reload changes asynchronously.
  173. */
  174. function radcliffe_2_contact_info_customize_preview_js() {
  175. wp_enqueue_script( 'radcliffe-2-contact-info-customizer', get_template_directory_uri() . '/contact-info/contact-info-customize-preview.js', array( 'jquery', 'customize-preview' ), '20170907', true );
  176. }
  177. add_action( 'customize_preview_init', 'radcliffe_2_contact_info_customize_preview_js' );