class-vamtam-customize-typography-control.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. Typograohy control
  4. @see Kirki/typography
  5. */
  6. class Vamtam_Customize_Typography_Control extends Vamtam_Customize_Control {
  7. public $type = 'vamtam-typography';
  8. /**
  9. * Enqueue control related scripts/styles.
  10. *
  11. * @access public
  12. */
  13. public function enqueue() {
  14. wp_enqueue_script(
  15. 'customizer-control-vamtam-typography-js',
  16. VAMTAM_CUSTOMIZER_LIB_URL . 'assets/js/typography' . ( WP_DEBUG ? '' : '.min' ) . '.js',
  17. array( 'jquery-core', 'customize-base', 'wp-color-picker' ),
  18. Vamtam_Customizer::$version,
  19. true
  20. );
  21. wp_enqueue_style(
  22. 'customizer-control-vamtam-typography',
  23. VAMTAM_CUSTOMIZER_LIB_URL . 'assets/css/typography.css',
  24. array( 'wp-color-picker' ),
  25. Vamtam_Customizer::$version
  26. );
  27. wp_localize_script( 'customize-base', 'VAMTAM_ALL_FONTS', $GLOBALS['vamtam_fonts'] );
  28. }
  29. /**
  30. * Refresh the parameters passed to the JavaScript via JSON.
  31. *
  32. * @access public
  33. */
  34. public function to_json() {
  35. parent::to_json();
  36. $this->add_values_backwards_compatibility();
  37. $defaults = array(
  38. 'font-family' => false,
  39. 'font-size' => 0,
  40. 'variant' => false,
  41. 'line-height' => 0,
  42. 'color' => '#000000',
  43. );
  44. $this->json['default'] = wp_parse_args( $this->json['default'], $defaults );
  45. $this->json['show_variants'] = true;
  46. $this->json['l10n'] = array(
  47. 'font-family' => esc_html__( 'Font Family', 'vamtam-consulting' ),
  48. 'select-font-family' => esc_html__( 'Select Font Family', 'vamtam-consulting' ),
  49. 'variant' => esc_html__( 'Variant', 'vamtam-consulting' ),
  50. 'font-size' => esc_html__( 'Font Size', 'vamtam-consulting' ),
  51. 'line-height' => esc_html__( 'Line Height', 'vamtam-consulting' ),
  52. 'color' => esc_html__( 'Color', 'vamtam-consulting' ),
  53. );
  54. $this->json['unit'] = 'px';
  55. }
  56. /**
  57. * An Underscore (JS) template for this control's content (but not its container).
  58. *
  59. * Class variables for this control class are available in the `data` JS object;
  60. * export custom variables by overriding {@see Kirki_Customize_Control::to_json()}.
  61. *
  62. * @see WP_Customize_Control::print_template()
  63. *
  64. * @access protected
  65. */
  66. protected function content_template() {
  67. ?>
  68. <# if ( data.label ) { #>
  69. <span class="customize-control-title">{{{ data.label }}}</span>
  70. <# } #>
  71. <# if ( data.description ) { #>
  72. <span class="description customize-control-description">{{{ data.description }}}</span>
  73. <# } #>
  74. <div class="wrapper">
  75. <# if ( '' == data.value['font-family'] ) { data.value['font-family'] = data.default['font-family']; } #>
  76. <div class="font-family">
  77. <h5>{{ data.l10n['font-family'] }}</h5>
  78. <select id="vamtam-typography-font-family-{{{ data.id }}}" placeholder="{{ data.l10n['select-font-family'] }}">
  79. <# for ( var font in VAMTAM_ALL_FONTS ) { #>
  80. <option value="{{ VAMTAM_ALL_FONTS[font].family }}">{{ font }}</option>
  81. <# } #>
  82. </select>
  83. </div>
  84. <div class="sizes">
  85. <div class="font-size">
  86. <h5>{{ data.l10n['font-size'] }}</h5>
  87. <input type="number" value="{{ parseInt( data.value['font-size'], 10 ) }}" min="0" />{{data.unit}}
  88. </div>
  89. <div class="line-height">
  90. <h5>{{ data.l10n['line-height'] }}</h5>
  91. <input type="number" value="{{ parseInt( data.value['line-height'], 10 ) }}" min="0" />{{data.unit}}
  92. </div>
  93. </div>
  94. <div class="variant-color">
  95. <div class="color">
  96. <h5>{{ data.l10n['color'] }}</h5>
  97. <input type="text" data-palette="{{ data.palette }}" data-default-color="{{ data.default['color'] }}" value="{{ data.value['color'] }}" class="vamtam-color-picker" />
  98. </div>
  99. <# if ( true === data.show_variants || false !== data.default.variant ) { #>
  100. <div class="variant vamtam-variant-wrapper">
  101. <h5>{{ data.l10n['variant'] }}</h5>
  102. <select class="variant" id="vamtam-typography-variant-{{{ data.id }}}"></select>
  103. </div>
  104. <# } #>
  105. </div>
  106. </div>
  107. <?php
  108. }
  109. /**
  110. * Adds backwards-compatibility for values.
  111. * Converts font-weight to variant
  112. * Adds units to letter-spacing
  113. *
  114. * @access protected
  115. */
  116. protected function add_values_backwards_compatibility() {
  117. $value = $this->value();
  118. $old_values = array(
  119. 'font-family' => '',
  120. 'font-size' => '',
  121. 'variant' => ( isset( $value['font-weight'] ) ) ? $value['font-weight'] : 'regular',
  122. 'line-height' => '',
  123. 'letter-spacing' => '',
  124. 'color' => '',
  125. );
  126. // Font-weight is now variant.
  127. // All values are the same with the exception of 400 (becomes regular).
  128. if ( '400' == $old_values['variant'] ) {
  129. $old_values['variant'] = 'regular';
  130. }
  131. // Letter spacing was in px, now it requires units.
  132. if ( isset( $value['letter-spacing'] ) && is_numeric( $value['letter-spacing'] ) && $value['letter-spacing'] ) {
  133. $value['letter-spacing'] .= 'px';
  134. }
  135. $this->json['value'] = wp_parse_args( $value, $old_values );
  136. // Cleanup.
  137. if ( isset( $this->json['value']['font-weight'] ) ) {
  138. unset( $this->json['value']['font-weight'] );
  139. }
  140. }
  141. /**
  142. * Don't render any content for this control from PHP.
  143. */
  144. public function render_content() {}
  145. }