/wp-content/plugins/js_composer/include/classes/editors/class-vc-edit-form-fields.php

https://gitlab.com/oxidigitaluser/liguelista · PHP · 285 lines · 153 code · 18 blank · 114 comment · 16 complexity · 2bc5cb288c8a1137b7596d3eacfeea75 MD5 · raw file

  1. <?php
  2. /**
  3. * WPBakery Visual Composer shortcode attributes fields
  4. *
  5. * @package WPBakeryVisualComposer
  6. *
  7. */
  8. /**
  9. * Edit form fields builder for shortcode attributes.
  10. *
  11. * @since 4.4
  12. */
  13. class Vc_Edit_Form_Fields implements Vc_Render {
  14. /**
  15. * @since 4.4
  16. * @var bool
  17. */
  18. protected $tag = false;
  19. /**
  20. * @since 4.4
  21. * @var array
  22. */
  23. protected $atts = array();
  24. /**
  25. * @since 4.4
  26. * @var array
  27. */
  28. protected $settings = array();
  29. /**
  30. * @since 4.4
  31. * @var bool
  32. */
  33. protected $post_id = false;
  34. /**
  35. * Construct Form fields.
  36. *
  37. * @since 4.4
  38. *
  39. * @param $tag - shortcode tag
  40. * @param $atts - list of attribute assign to the shortcode.
  41. */
  42. public function __construct( $tag, $atts ) {
  43. $this->tag = $tag;
  44. $this->atts = apply_filters( 'vc_edit_form_fields_attributes_' . $this->tag, $atts );
  45. $this->setSettings( WPBMap::getShortCode( $this->tag ) );
  46. }
  47. /**
  48. * Get settings
  49. * @since 4.4
  50. *
  51. * @param $key
  52. *
  53. * @return null
  54. */
  55. public function setting( $key ) {
  56. return isset( $this->settings[$key] ) ? $this->settings[$key] : null;
  57. }
  58. /**
  59. * Set settings data
  60. * @since 4.4
  61. *
  62. * @param array $settings
  63. */
  64. public function setSettings( array $settings ) {
  65. $this->settings = $settings;
  66. }
  67. /**
  68. * Shortcode Post ID getter.
  69. * If post id isn't set try to get from get_the_ID function.
  70. * @since 4.4
  71. * @return int|bool;
  72. */
  73. public function postId() {
  74. if ( $this->post_id === false ) {
  75. $this->post_id = get_the_ID();
  76. }
  77. return $this->post_id;
  78. }
  79. /**
  80. * Shortcode Post ID setter.
  81. * @since 4.4
  82. *
  83. * @param $post_id - integer value in post_id
  84. */
  85. public function setPostId( $post_id ) {
  86. $this->post_id = (int) $post_id;
  87. }
  88. /**
  89. * Get shortcode attribute value.
  90. *
  91. * This function checks if value isn't set then it uses std or value fields in param settings.
  92. * @since 4.4
  93. *
  94. * @param $param_settings
  95. * @param $value
  96. *
  97. * @return null
  98. */
  99. protected function parseShortcodeAttributeValue( $param_settings, $value ) {
  100. if ( is_null( $value ) ) { // If value doesn't exists
  101. if ( isset( $param_settings['std'] ) ) {
  102. $value = $param_settings['std'];
  103. } elseif (
  104. isset( $param_settings['value'] ) && is_array( $param_settings['value'] )
  105. && !empty( $param_settings['type'] ) && $param_settings['type'] != 'checkbox'
  106. ) {
  107. $first_key = key( $param_settings['value'] );
  108. $value = $first_key ? $param_settings['value'][$first_key] : '';
  109. } elseif ( isset( $param_settings['value'] ) && !is_array( $param_settings['value'] ) ) {
  110. $value = $param_settings['value'];
  111. }
  112. }
  113. return $value;
  114. }
  115. /**
  116. * Enqueue js scripts for attributes types.
  117. * @since 4.4
  118. * @return string
  119. */
  120. public function enqueueScripts() {
  121. $output = '';
  122. if ( !WpbakeryShortcodeParams::isEnqueue() ) {
  123. $scripts = apply_filters( 'vc_edit_form_enqueue_script', WpbakeryShortcodeParams::getScripts() );
  124. foreach ($scripts as $script ) {
  125. $output .= "\n\n" . '<script type="text/javascript" src="' . $script . '"></script>';
  126. }
  127. }
  128. return $output;
  129. }
  130. /**
  131. * Render grouped fields.
  132. * @since 4.4
  133. *
  134. * @param $groups
  135. * @param $groups_content
  136. *
  137. * @return string
  138. */
  139. protected function renderGroupedFields( $groups, $groups_content ) {
  140. $output = '';
  141. if ( sizeof( $groups ) > 1 ) {
  142. $output .= '<div class="vc_panel-tabs" id="vc_edit-form-tabs"><ul class="vc_edit-form-tabs-menu">';
  143. $key = 0;
  144. foreach ( $groups as $g ) {
  145. $output .= '<li class="vc_edit-form-tab-control" data-tab-index="'
  146. . $key . '"><a href="#vc_edit-form-tab-' . $key ++
  147. . '" class="vc_edit-form-link">'
  148. . ( $g === '_general' ? __( 'General', 'js_composer' ) : $g ) . '</a></li>';
  149. }
  150. $output .= '</ul>';
  151. $key = 0;
  152. foreach ( $groups as $g ) {
  153. $output .= '<div id="vc_edit-form-tab-' . $key ++ . '" class="vc_edit-form-tab">';
  154. $output .= $groups_content[$g];
  155. $output .= '</div>';
  156. }
  157. $output .= '</div>';
  158. } elseif ( !empty( $groups_content['_general'] ) ) {
  159. $output .= $groups_content['_general'];
  160. }
  161. return $output;
  162. }
  163. /**
  164. * Render fields html and output it.
  165. * @since 4.4
  166. * vc_filter: vc_edit_form_class - filter to override editor_css_classes array
  167. */
  168. public function render() {
  169. $this->loadDefaultParams();
  170. $output = $el_position = '';
  171. $groups_content = $groups = array();
  172. $params = $this->setting( 'params' );
  173. $editor_css_classes = apply_filters( 'vc_edit_form_class', array(
  174. 'wpb_edit_form_elements',
  175. 'vc_edit_form_elements'
  176. ), $this->atts, $params );
  177. $output .= '<div class="'
  178. . implode( ' ', $editor_css_classes )
  179. . '" data-title="' . htmlspecialchars( __( 'Edit', 'js_composer' )
  180. . ' ' . __( $this->setting( 'name' ), "js_composer" ) ) . '">';
  181. foreach ( $params as $param ) {
  182. $name = isset( $param['param_name'] ) ? $param['param_name'] : null;
  183. if ( !is_null( $name ) ) {
  184. $value = isset( $this->atts[$name] ) ? $this->atts[$name] : null;
  185. $value = $this->parseShortcodeAttributeValue( $param, $value );
  186. $group = isset( $param['group'] ) && '' !== $param['group'] ? $param['group'] : '_general';
  187. if ( !isset( $groups_content[$group] ) ) {
  188. $groups[] = $group;
  189. $groups_content[$group] = '';
  190. }
  191. $groups_content[$group] .= $this->renderField( $param, $value );
  192. }
  193. }
  194. $output .= $this->renderGroupedFields( $groups, $groups_content );
  195. $output .= '</div>';
  196. $output .= $this->enqueueScripts();
  197. echo $output;
  198. }
  199. /**
  200. * Generate html for shortcode attribute.
  201. *
  202. * Method
  203. * @since 4.4
  204. *
  205. * @param $param
  206. * @param $value
  207. *
  208. * vc_filter: vc_single_param_edit - hook to edit any shortode param
  209. * vc_filter: vc_form_fields_render_field_{shortcode_name}_{param_name}_param_value - hook to edit shortcode param value
  210. * vc_filter: vc_form_fields_render_field_{shortcode_name}_{param_name}_param - hook to edit shortcode param attributes
  211. * vc_filter: vc_single_param_edit_holder_output - hook to edit output of this method
  212. * @return mixed|void
  213. */
  214. protected function renderField( $param, $value ) {
  215. $param['vc_single_param_edit_holder_class'] = array(
  216. 'wpb_el_type_' . $param['type'],
  217. 'vc_shortcode-param'
  218. );
  219. if ( !empty( $param['param_holder_class'] ) ) {
  220. $param['vc_single_param_edit_holder_class'][] = $param['param_holder_class'];
  221. }
  222. $param = apply_filters( 'vc_single_param_edit', $param, $value );
  223. $output = '<div class="' . implode( ' ', $param['vc_single_param_edit_holder_class'] ) . '" data-param_name="' . $param['param_name'] . '" data-param_type="' . $param['type'] . '">';
  224. $output .= ( isset( $param['heading'] ) )
  225. ? '<div class="wpb_element_label">' . __( $param['heading'], "js_composer" ) . '</div>' : '';
  226. $output .= '<div class="edit_form_line">';
  227. $value = apply_filters(
  228. 'vc_form_fields_render_field_' . $this->setting( 'base' ) . '_' . $param['param_name'] . '_param_value'
  229. , $value
  230. , $param
  231. , $this->settings
  232. , $this->atts );
  233. $param = apply_filters(
  234. 'vc_form_fields_render_field_' . $this->setting( 'base' ) . '_' . $param['param_name'] . '_param'
  235. , $param
  236. , $value
  237. , $this->settings
  238. , $this->atts );
  239. $output .= vc_do_shortcode_param_settings_field( $param['type'], $param, $value, $this->setting( 'base' ) );
  240. if ( isset( $param['description'] ) ) {
  241. $output .= '<span class="vc_description vc_clearfix">' . __( $param['description'], "js_composer" ) . '</span>';
  242. }
  243. $output .= '</div>';
  244. $output .= '</div>';
  245. return apply_filters( 'vc_single_param_edit_holder_output', $output, $param, $value, $this->settings, $this->atts );
  246. }
  247. /**
  248. * Create default shortcode params
  249. *
  250. * List of params stored in global variable $vc_params_list.
  251. * Please check include/params/load.php for default params list.
  252. * @since 4.4
  253. * @return bool
  254. */
  255. public function loadDefaultParams() {
  256. global $vc_params_list;
  257. if ( empty( $vc_params_list ) ) {
  258. return false;
  259. }
  260. $script_url = vc_asset_url( 'js/params/all.js' );
  261. foreach ( $vc_params_list as $param ) {
  262. vc_add_shortcode_param( $param, 'vc_' . $param . '_form_field', $script_url );
  263. }
  264. do_action( 'vc_load_default_params' );
  265. return true;
  266. }
  267. }