vc-custom-heading.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * Class WPBakeryShortCode_Vc_Custom_heading
  7. * @since 4.3
  8. */
  9. class WPBakeryShortCode_Vc_Custom_Heading extends WPBakeryShortCode {
  10. /**
  11. * Defines fields names for google_fonts, font_container and etc
  12. * @since 4.4
  13. * @var array
  14. */
  15. protected $fields = array(
  16. 'google_fonts' => 'google_fonts',
  17. 'font_container' => 'font_container',
  18. 'el_class' => 'el_class',
  19. 'css' => 'css',
  20. 'text' => 'text',
  21. );
  22. /**
  23. * Used to get field name in vc_map function for google_fonts, font_container and etc..
  24. *
  25. * @param $key
  26. *
  27. * @return bool
  28. * @since 4.4
  29. */
  30. protected function getField( $key ) {
  31. return isset( $this->fields[ $key ] ) ? $this->fields[ $key ] : false;
  32. }
  33. /**
  34. * Get param value by providing key
  35. *
  36. * @param $key
  37. *
  38. * @return array|bool
  39. * @throws \Exception
  40. * @since 4.4
  41. */
  42. protected function getParamData( $key ) {
  43. return WPBMap::getParam( $this->shortcode, $this->getField( $key ) );
  44. }
  45. /**
  46. * Parses shortcode attributes and set defaults based on vc_map function relative to shortcode and fields names
  47. *
  48. * @param $atts
  49. *
  50. * @return array
  51. * @throws \Exception
  52. * @since 4.3
  53. */
  54. public function getAttributes( $atts ) {
  55. /**
  56. * Shortcode attributes
  57. * @var $text
  58. * @var $google_fonts
  59. * @var $font_container
  60. * @var $el_class
  61. * @var $link
  62. * @var $css
  63. */
  64. $atts = vc_map_get_attributes( $this->getShortcode(), $atts );
  65. extract( $atts );
  66. /**
  67. * Get default values from VC_MAP.
  68. */
  69. $google_fonts_field = $this->getParamData( 'google_fonts' );
  70. $font_container_field = $this->getParamData( 'font_container' );
  71. $el_class = $this->getExtraClass( $el_class );
  72. $font_container_obj = new Vc_Font_Container();
  73. $google_fonts_obj = new Vc_Google_Fonts();
  74. $font_container_field_settings = isset( $font_container_field['settings'], $font_container_field['settings']['fields'] ) ? $font_container_field['settings']['fields'] : array();
  75. $google_fonts_field_settings = isset( $google_fonts_field['settings'], $google_fonts_field['settings']['fields'] ) ? $google_fonts_field['settings']['fields'] : array();
  76. $font_container_data = $font_container_obj->_vc_font_container_parse_attributes( $font_container_field_settings, $font_container );
  77. $google_fonts_data = strlen( $google_fonts ) > 0 ? $google_fonts_obj->_vc_google_fonts_parse_attributes( $google_fonts_field_settings, $google_fonts ) : '';
  78. return array(
  79. 'text' => isset( $text ) ? $text : '',
  80. 'google_fonts' => $google_fonts,
  81. 'font_container' => $font_container,
  82. 'el_class' => $el_class,
  83. 'css' => isset( $css ) ? $css : '',
  84. 'link' => ( 0 === strpos( $link, '|' ) ) ? false : $link,
  85. 'font_container_data' => $font_container_data,
  86. 'google_fonts_data' => $google_fonts_data,
  87. );
  88. }
  89. /**
  90. * Parses google_fonts_data and font_container_data to get needed css styles to markup
  91. *
  92. * @param $el_class
  93. * @param $css
  94. * @param $google_fonts_data
  95. * @param $font_container_data
  96. * @param $atts
  97. *
  98. * @return array
  99. * @since 4.3
  100. */
  101. public function getStyles( $el_class, $css, $google_fonts_data, $font_container_data, $atts ) {
  102. $styles = array();
  103. if ( ! empty( $font_container_data ) && isset( $font_container_data['values'] ) ) {
  104. foreach ( $font_container_data['values'] as $key => $value ) {
  105. if ( 'tag' !== $key && strlen( $value ) ) {
  106. if ( preg_match( '/description/', $key ) ) {
  107. continue;
  108. }
  109. if ( 'font_size' === $key || 'line_height' === $key ) {
  110. $value = preg_replace( '/\s+/', '', $value );
  111. }
  112. if ( 'font_size' === $key ) {
  113. $pattern = '/^(\d*(?:\.\d+)?)\s*(px|\%|in|cm|mm|em|rem|ex|pt|pc|vw|vh|vmin|vmax)?$/';
  114. // allowed metrics: http://www.w3schools.com/cssref/css_units.asp
  115. preg_match( $pattern, $value, $matches );
  116. $value = isset( $matches[1] ) ? (float) $matches[1] : (float) $value;
  117. $unit = isset( $matches[2] ) ? $matches[2] : 'px';
  118. $value = $value . $unit;
  119. }
  120. if ( strlen( $value ) > 0 ) {
  121. $styles[] = str_replace( '_', '-', $key ) . ': ' . $value;
  122. }
  123. }
  124. }
  125. }
  126. if ( ( ! isset( $atts['use_theme_fonts'] ) || 'yes' !== $atts['use_theme_fonts'] ) && ! empty( $google_fonts_data ) && isset( $google_fonts_data['values'], $google_fonts_data['values']['font_family'], $google_fonts_data['values']['font_style'] ) ) {
  127. $google_fonts_family = explode( ':', $google_fonts_data['values']['font_family'] );
  128. $styles[] = 'font-family:' . $google_fonts_family[0];
  129. $google_fonts_styles = explode( ':', $google_fonts_data['values']['font_style'] );
  130. $styles[] = 'font-weight:' . $google_fonts_styles[1];
  131. $styles[] = 'font-style:' . $google_fonts_styles[2];
  132. }
  133. /**
  134. * Filter 'VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG' to change vc_custom_heading class
  135. *
  136. * @param string - filter_name
  137. * @param string - element_class
  138. * @param string - shortcode_name
  139. * @param array - shortcode_attributes
  140. *
  141. * @since 4.3
  142. */
  143. $css_class = apply_filters( VC_SHORTCODE_CUSTOM_CSS_FILTER_TAG, 'vc_custom_heading ' . $el_class . vc_shortcode_custom_css_class( $css, ' ' ), $this->settings['base'], $atts );
  144. return array(
  145. 'css_class' => trim( preg_replace( '/\s+/', ' ', $css_class ) ),
  146. 'styles' => $styles,
  147. );
  148. }
  149. }