class-vamtam-customize-switch-control.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. class Vamtam_Customize_Switch_Control extends Vamtam_Customize_Control {
  3. public $type = 'vamtam-switch';
  4. /**
  5. * Render the control's content.
  6. */
  7. protected function render_content() {
  8. if ( empty( $this->choices ) ) {
  9. $this->choices = array(
  10. '1' => esc_html__( 'On', 'vamtam-consulting' ),
  11. '0' => esc_html__( 'Off', 'vamtam-consulting' ),
  12. );
  13. }
  14. $setting_value = $this->value();
  15. if ( $setting_value === 'false' ) {
  16. $setting_value = '0';
  17. } elseif ( $setting_value === 'true' ) {
  18. $setting_value = '1';
  19. }
  20. $name = '_customize-switch-' . $this->id;
  21. if ( ! empty( $this->label ) ) : ?>
  22. <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
  23. <?php endif;
  24. if ( ! empty( $this->description ) ) : ?>
  25. <span class="description customize-control-description"><?php echo esc_html( $this->description ) ?></span>
  26. <?php endif;
  27. foreach ( $this->choices as $value => $label ) :
  28. ?>
  29. <label>
  30. <input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $setting_value, $value ); ?> />
  31. <?php echo esc_html( $label ); ?>
  32. </label>
  33. <?php
  34. endforeach;
  35. }
  36. public static function sanitize_callback( $value ) {
  37. if ( 'true' === $value ) {
  38. return '1';
  39. } elseif ( 'false' === $value ) {
  40. return '0';
  41. }
  42. return (string) $value;
  43. }
  44. }