PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/df_home/static/test/portalbkd/wp-content/plugins/js_composer/include/params/font_container/font_container.php

https://gitlab.com/darmawan.fatria/df-skp-2014
PHP | 298 lines | 214 code | 17 blank | 67 comment | 34 complexity | f5411bbfd9ad662e27a6e98e3fb2441d MD5 | raw file
  1. <?php
  2. /**
  3. * Class Vc_Font_Container
  4. * @since 4.3
  5. * vc_map examples:
  6. * array(
  7. * 'type' => 'font_container',
  8. * 'param_name' => 'font_container',
  9. * 'value'=>'',
  10. * 'settings'=>array(
  11. * 'fields'=>array(
  12. *
  13. * 'tag'=>'h2', // default value h2
  14. * 'text_align',
  15. * 'font_size',
  16. * 'line_height',
  17. * 'color',
  18. * //'font_style_italic'
  19. * //'font_style_bold'
  20. * //'font_family'
  21. *
  22. * 'tag_description' => __('Select element tag.','js_composer'),
  23. * 'text_align_description' => __('Select text alignment.','js_composer'),
  24. * 'font_size_description' => __('Enter font size.','js_composer'),
  25. * 'line_height_description' => __('Enter line height.','js_composer'),
  26. * 'color_description' => __('Select color for your element.','js_composer'),
  27. * //'font_style_description' => __('Put your description here','js_composer'),
  28. * //'font_family_description' => __('Put your description here','js_composer'),
  29. * ),
  30. * ),
  31. * // 'description' => __( '', 'js_composer' ), // description for field group
  32. * ),
  33. * Ordering of fields, font_family, tag, text_align and etc. will be Same as ordering in array!
  34. * To provide default value to field use 'key' => 'value'
  35. */
  36. class Vc_Font_Container {
  37. /**
  38. * @param $settings
  39. * @param $value
  40. *
  41. * @return string
  42. */
  43. public function render( $settings, $value ) {
  44. $fields = array();
  45. $values = array();
  46. extract( $this->_vc_font_container_parse_attributes( $settings['settings']['fields'], $value ) );
  47. $data = array();
  48. $output = '';
  49. if ( ! empty( $fields ) ) {
  50. if ( isset( $fields['tag'] ) ) {
  51. $data['tag'] = '
  52. <div class="vc_row-fluid vc_column">
  53. <div class="wpb_element_label">' . __( 'Element tag', 'js_composer' ) . '</div>
  54. <div class="vc_font_container_form_field-tag-container">
  55. <select class="vc_font_container_form_field-tag-select">';
  56. $tags = $this->_vc_font_container_get_allowed_tags();
  57. foreach ( $tags as $tag ) {
  58. $data['tag'] .= '<option value="' . $tag . '" class="' . $tag . '" ' . ( $values['tag'] == $tag ? 'selected="selected"' : '' ) . '>' . __( $tag, 'js_composer' ) . '</option>';
  59. }
  60. $data['tag'] .= '
  61. </select>
  62. </div>';
  63. if ( isset( $fields['tag_description'] ) && strlen( $fields['tag_description'] ) > 0 ) {
  64. $data['tag'] .= '
  65. <span class="vc_description clear">' . $fields['tag_description'] . '</span>
  66. ';
  67. }
  68. $data['tag'] .= '</div>';
  69. }
  70. if ( isset( $fields['font_size'] ) ) {
  71. $data['font_size'] = '
  72. <div class="vc_row-fluid vc_column">
  73. <div class="wpb_element_label">' . __( 'Font size', 'js_composer' ) . '</div>
  74. <div class="vc_font_container_form_field-font_size-container">
  75. <input class="vc_font_container_form_field-font_size-input" type="text" value="' . $values['font_size'] . '" />
  76. </div>';
  77. if ( isset( $fields['font_size_description'] ) && strlen( $fields['font_size_description'] ) > 0 ) {
  78. $data['font_size'] .= '
  79. <span class="vc_description clear">' . $fields['font_size_description'] . '</span>
  80. ';
  81. }
  82. $data['font_size'] .= '</div>';
  83. }
  84. if ( isset( $fields['text_align'] ) ) {
  85. $data['text_align'] = '
  86. <div class="vc_row-fluid vc_column">
  87. <div class="wpb_element_label">' . __( 'Text align', 'js_composer' ) . '</div>
  88. <div class="vc_font_container_form_field-text_align-container">
  89. <select class="vc_font_container_form_field-text_align-select">
  90. <option value="left" class="left" ' . ( $values['text_align'] == 'left' ? 'selected="selected"' : '' ) . '>' . __( 'left', 'js_composer' ) . '</option>
  91. <option value="right" class="right" ' . ( $values['text_align'] == 'right' ? 'selected="selected"' : '' ) . '>' . __( 'right', 'js_composer' ) . '</option>
  92. <option value="center" class="center" ' . ( $values['text_align'] == 'center' ? 'selected="selected"' : '' ) . '>' . __( 'center', 'js_composer' ) . '</option>
  93. <option value="justify" class="justify" ' . ( $values['text_align'] == 'justify' ? 'selected="selected"' : '' ) . '>' . __( 'justify', 'js_composer' ) . '</option>
  94. </select>
  95. </div>';
  96. if ( isset( $fields['text_align_description'] ) && strlen( $fields['text_align_description'] ) > 0 ) {
  97. $data['text_align'] .= '
  98. <span class="vc_description clear">' . $fields['text_align_description'] . '</span>
  99. ';
  100. }
  101. $data['text_align'] .= '</div>';
  102. }
  103. if ( isset( $fields['line_height'] ) ) {
  104. $data['line_height'] = '
  105. <div class="vc_row-fluid vc_column">
  106. <div class="wpb_element_label">' . __( 'Line height', 'js_composer' ) . '</div>
  107. <div class="vc_font_container_form_field-line_height-container">
  108. <input class="vc_font_container_form_field-line_height-input" type="text" value="' . $values['line_height'] . '" />
  109. </div>';
  110. if ( isset( $fields['line_height_description'] ) && strlen( $fields['line_height_description'] ) > 0 ) {
  111. $data['line_height'] .= '
  112. <span class="vc_description clear">' . $fields['line_height_description'] . '</span>
  113. ';
  114. }
  115. $data['line_height'] .= '</div>';
  116. }
  117. if ( isset( $fields['color'] ) ) {
  118. $data['color'] = '
  119. <div class="vc_row-fluid vc_column">
  120. <div class="wpb_element_label">' . __( 'Text color', 'js_composer' ) . '</div>
  121. <div class="vc_font_container_form_field-color-container">
  122. <div class="color-group">
  123. <input type="text" value="' . $values['color'] . '" class="vc_font_container_form_field-color-input vc_color-control" />
  124. </div>
  125. </div>';
  126. if ( isset( $fields['color_description'] ) && strlen( $fields['color_description'] ) > 0 ) {
  127. $data['color'] .= '
  128. <span class="vc_description clear">' . $fields['color_description'] . '</span>
  129. ';
  130. }
  131. $data['color'] .= '</div>';
  132. }
  133. if ( isset( $fields['font_family'] ) ) {
  134. $data['font_family'] = '
  135. <div class="vc_row-fluid vc_column">
  136. <div class="wpb_element_label">' . __( 'Font Family', 'js_composer' ) . '</div>
  137. <div class="vc_font_container_form_field-font_family-container">
  138. <select class="vc_font_container_form_field-font_family-select">';
  139. $fonts = $this->_vc_font_container_get_web_safe_fonts();
  140. foreach ( $fonts as $font_name => $font_data ) {
  141. $data['font_family'] .= '<option value="' . $font_name . '" class="' . vc_build_safe_css_class( $font_name ) . '" ' . ( strtolower( $values['font_family'] ) == strtolower( $font_name ) ? 'selected="selected"' : '' ) . ' data[font_family]="' . urlencode( $font_data ) . '">' . __( $font_name, 'js_composer' ) . '</option>';
  142. }
  143. $data['font_family'] .= '
  144. </select>
  145. </div>';
  146. if ( isset( $fields['font_family_description'] ) && strlen( $fields['font_family_description'] ) > 0 ) {
  147. $data['font_family'] .= '
  148. <span class="vc_description clear">' . $fields['font_family_description'] . '</span>
  149. ';
  150. }
  151. $data['font_family'] .= '</div>';
  152. }
  153. if ( isset( $fields['font_style'] ) ) {
  154. $data['font_style'] = '
  155. <div class="vc_row-fluid vc_column">
  156. <div class="wpb_element_label">' . __( 'Font style', 'js_composer' ) . '</div>
  157. <div class="vc_font_container_form_field-font_style-container">
  158. <label>
  159. <input type="checkbox" class="vc_font_container_form_field-font_style-checkbox italic" value="italic" ' . ( $values['font_style_italic'] == "1" ? 'checked="checked"' : '' ) . '><span class="vc_font_container_form_field-font_style-label italic">' . __( 'italic', 'js_composer' ) . '</span>
  160. </label>
  161. <br />
  162. <label>
  163. <input type="checkbox" class="vc_font_container_form_field-font_style-checkbox bold" value="bold" ' . ( $values['font_style_bold'] == "1" ? 'checked="checked"' : '' ) . '><span class="vc_font_container_form_field-font_style-label bold">' . __( 'bold', 'js_composer' ) . '</span>
  164. </label>
  165. </div>';
  166. if ( isset( $fields['font_style_description'] ) && strlen( $fields['font_style_description'] ) > 0 ) {
  167. $data['font_style'] .= '
  168. <span class="vc_description clear">' . $fields['font_style_description'] . '</span>
  169. ';
  170. }
  171. $data['font_style'] .= '</div>';
  172. }
  173. // combine all in output, make sure you follow ordering
  174. foreach ( $fields as $key => $field ) {
  175. if ( isset( $data[ $key ] ) ) {
  176. $output .= $data[ $key ];
  177. }
  178. }
  179. }
  180. $output .= '<input name="' . $settings['param_name'] . '" class="wpb_vc_param_value ' . $settings['param_name'] . ' ' . $settings['type'] . '_field" type="hidden" value="' . $value . '" />';
  181. return $output;
  182. }
  183. /**
  184. * If field 'font_family' is used this is list of fonts available
  185. * To modify this list, you should use add_filter('vc_font_container_get_fonts_filter','your_custom_function');
  186. * vc_filter: vc_font_container_get_fonts_filter - to modify list of fonts
  187. * @return array list of fonts
  188. */
  189. public function _vc_font_container_get_web_safe_fonts() {
  190. // this is "Web Safe FONTS" from w3c: http://www.w3schools.com/cssref/css_websafe_fonts.asp
  191. $web_fonts = array(
  192. 'Georgia' => 'Georgia, serif',
  193. 'Palatino Linotype' => '"Palatino Linotype", "Book Antiqua", Palatino, serif',
  194. 'Book Antiqua' => '"Book Antiqua", Palatino, serif',
  195. 'Palatino' => 'Palatino, serif',
  196. 'Times New Roman' => '"Times New Roman", Times, serif',
  197. 'Arial' => 'Arial, Helvetica, sans-serif',
  198. 'Arial Black' => '"Arial Black", Gadget, sans-serif',
  199. 'Helvetica' => 'Helvetica, sans-serif',
  200. 'Comic Sans MS' => '"Comic Sans MS", cursive, sans-serif',
  201. 'Impact' => 'Impact, Charcoal, sans-serif',
  202. 'Charcoal' => 'Charcoal, sans-serif',
  203. 'Lucida Sans Unicode' => '"Lucida Sans Unicode", "Lucida Grande", sans-serif',
  204. 'Lucida Grande' => '"Lucida Grande", sans-serif',
  205. 'Tahoma' => 'Tahoma, Geneva, sans-serif',
  206. 'Geneva' => 'Geneva, sans-serif',
  207. 'Trebuchet MS' => '"Trebuchet MS", Helvetica, sans-serif',
  208. 'Verdana' => '"Trebuchet MS", Helvetica, sans-serif',
  209. 'Courier New' => '"Courier New", Courier, monospace',
  210. 'Lucida Console' => '"Lucida Console", Monaco, monospace',
  211. 'Monaco' => 'Monaco, monospace'
  212. );
  213. return apply_filters( 'vc_font_container_get_fonts_filter', $web_fonts );
  214. }
  215. /**
  216. * If 'tag' field used this is list of allowed tags
  217. * To modify this list, you should use add_filter('vc_font_container_get_allowed_tags','your_custom_function');
  218. * vc_filter: vc_font_container_get_allowed_tags - to modify list of allowed tags by default
  219. * @return array list of allowed tags
  220. */
  221. public function _vc_font_container_get_allowed_tags() {
  222. $allowed_tags = array(
  223. 'h2',
  224. 'h3',
  225. 'h4',
  226. 'h5',
  227. 'h6',
  228. 'p',
  229. //'span', // @note this is not inline element, so you can't use this for text-align
  230. 'div',
  231. );
  232. return apply_filters( 'vc_font_container_get_allowed_tags', $allowed_tags );
  233. }
  234. /**
  235. * @param $attr
  236. * @param $value
  237. *
  238. * @return array
  239. */
  240. public function _vc_font_container_parse_attributes( $attr, $value ) {
  241. $fields = array();
  242. if ( isset( $attr ) ) {
  243. foreach ( $attr as $key => $val ) {
  244. if ( is_numeric( $key ) ) {
  245. $fields[ $val ] = "";
  246. } else {
  247. $fields[ $key ] = $val;
  248. }
  249. }
  250. }
  251. $values = vc_parse_multi_attribute( $value, array(
  252. 'tag' => isset( $fields['tag'] ) ? $fields['tag'] : 'h2',
  253. 'font_size' => isset( $fields['font_size'] ) ? $fields['font_size'] : '',
  254. 'font_style_italic' => isset( $fields['font_style_italic'] ) ? $fields['font_style_italic'] : '',
  255. 'font_style_bold' => isset( $fields['font_style_bold'] ) ? $fields['font_style_bold'] : '',
  256. 'font_family' => isset( $fields['font_family'] ) ? $fields['font_family'] : '',
  257. 'color' => isset( $fields['color'] ) ? $fields['color'] : '',
  258. 'line_height' => isset( $fields['line_height'] ) ? $fields['line_height'] : '',
  259. 'text_align' => isset( $fields['text_align'] ) ? $fields['text_align'] : 'left',
  260. 'tag_description' => isset( $fields['tag_description'] ) ? $fields['tag_description'] : '',
  261. 'font_size_description' => isset( $fields['font_size_description'] ) ? $fields['font_size_description'] : '',
  262. 'font_style_description' => isset( $fields['font_style_description'] ) ? $fields['font_style_description'] : '',
  263. 'font_family_description' => isset( $fields['font_family_description'] ) ? $fields['font_family_description'] : '',
  264. 'color_description' => isset( $fields['color_description'] ) ? $fields['color_description'] : 'left',
  265. 'line_height_description' => isset( $fields['line_height_description'] ) ? $fields['line_height_description'] : '',
  266. 'text_align_description' => isset( $fields['text_align_description'] ) ? $fields['text_align_description'] : ''
  267. )
  268. );
  269. return array( 'fields' => $fields, 'values' => $values );
  270. }
  271. }
  272. /**
  273. * @param $settings
  274. * @param $value
  275. *
  276. * @return mixed|void
  277. */
  278. function vc_font_container_form_field( $settings, $value ) {
  279. $font_container = new Vc_Font_Container();
  280. return apply_filters( 'vc_font_container_render_filter', $font_container->render( $settings, $value ) );
  281. }