PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/furrutia1991/imosa_web
PHP | 317 lines | 176 code | 20 blank | 121 comment | 19 complexity | b10a93e16d02a7af1a62407c827912d0 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::getUserShortCode( $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 ( isset( $param_settings['value'] ) && is_array( $param_settings['value'] ) && ! empty( $param_settings['type'] ) && $param_settings['type'] !== 'checkbox'
  104. ) {
  105. $first_key = key( $param_settings['value'] );
  106. $value = $first_key ? $param_settings['value'][ $first_key ] : '';
  107. } elseif ( isset( $param_settings['value'] ) && ! is_array( $param_settings['value'] ) ) {
  108. $value = $param_settings['value'];
  109. }
  110. }
  111. return $value;
  112. }
  113. /**
  114. * Enqueue js scripts for attributes types.
  115. * @since 4.4
  116. * @return string
  117. */
  118. public function enqueueScripts() {
  119. $output = '';
  120. if ( ! WpbakeryShortcodeParams::isEnqueue() ) {
  121. $scripts = apply_filters( 'vc_edit_form_enqueue_script', WpbakeryShortcodeParams::getScripts() );
  122. foreach ( $scripts as $script ) {
  123. $output .= "\n\n" . '<script type="text/javascript" src="' . $script . '"></script>';
  124. }
  125. }
  126. return $output;
  127. }
  128. /**
  129. * Render grouped fields.
  130. * @since 4.4
  131. *
  132. * @param $groups
  133. * @param $groups_content
  134. *
  135. * @return string
  136. */
  137. protected function renderGroupedFields( $groups, $groups_content ) {
  138. $output = '';
  139. if ( sizeof( $groups ) > 1 || ( sizeof( $groups ) >= 1 && empty( $groups_content['_general'] ) ) ) {
  140. $output .= '<div class="vc_panel-tabs" id="vc_edit-form-tabs">';
  141. $output .= '<ul class="vc_general vc_ui-tabs-line" data-vc-ui-element="panel-tabs-controls">';
  142. $key = 0;
  143. foreach ( $groups as $g ) {
  144. $output .= '<li class="vc_edit-form-tab-control" data-tab-index="'
  145. . $key . '"><button data-vc-ui-element-target="#vc_edit-form-tab-'
  146. . $key ++ . '" class="vc_ui-tabs-line-trigger" data-vc-ui-element="panel-tab-control">'
  147. . ( $g === '_general' ? __( 'General', 'js_composer' ) : $g ) . '</button></li>';
  148. }
  149. $output .= '<li class="vc_ui-tabs-line-dropdown-toggle" data-vc-action="dropdown"
  150. data-vc-content=".vc_ui-tabs-line-dropdown" data-vc-ui-element="panel-tabs-line-toggle">
  151. <span class="vc_ui-tabs-line-trigger" data-vc-accordion
  152. data-vc-container=".vc_ui-tabs-line-dropdown-toggle"
  153. data-vc-target=".vc_ui-tabs-line-dropdown"> </span>
  154. <ul class="vc_ui-tabs-line-dropdown" data-vc-ui-element="panel-tabs-line-dropdown">
  155. </ul>
  156. </ul>';
  157. /*$output .= '<ul class="vc_edit-form-tabs-menu">';
  158. $key = 0;
  159. foreach ( $groups as $g ) {
  160. $output .= '<li class="vc_edit-form-tab-control" data-tab-index="' . $key . '"><a href="#vc_edit-form-tab-' . $key ++ . '" class="vc_edit-form-link">' . ( $g === '_general' ? __( 'General', 'js_composer' ) : $g ) . '</a></li>';
  161. }
  162. $output .= '</ul>';*/
  163. $key = 0;
  164. foreach ( $groups as $g ) {
  165. $output .= '<div id="vc_edit-form-tab-' . $key ++ . '" class="vc_edit-form-tab vc_row" data-vc-ui-element="panel-edit-element-tab">';
  166. $output .= $groups_content[ $g ];
  167. $output .= '</div>';
  168. }
  169. $output .= '</div>';
  170. } elseif ( ! empty( $groups_content['_general'] ) ) {
  171. $output .= '<div class="vc_edit-form-tab vc_row vc_active" data-vc-ui-element="panel-edit-element-tab">' . $groups_content['_general'] . '</div>';
  172. }
  173. return $output;
  174. }
  175. /**
  176. * Render fields html and output it.
  177. * @since 4.4
  178. * vc_filter: vc_edit_form_class - filter to override editor_css_classes array
  179. */
  180. public function render() {
  181. $this->loadDefaultParams();
  182. $output = $el_position = '';
  183. $groups_content = $groups = array();
  184. $params = $this->setting( 'params' );
  185. $editor_css_classes = apply_filters( 'vc_edit_form_class',
  186. array(
  187. 'wpb_edit_form_elements',
  188. 'vc_edit_form_elements'
  189. ),
  190. $this->atts,
  191. $params );
  192. $deprecated = $this->setting( 'deprecated' );
  193. if ( ! empty( $deprecated ) ) {
  194. $output .= '<div class="vc_row vc_shortcode-edit-form-deprecated-message"><div class="vc_col-sm-12 wpb_element_wrapper">' .
  195. vc_message_warning( sprintf( __( 'You are using outdated element, it is deprecated since version %s.', 'js_composer' ), $this->setting( 'deprecated' ) ) ) .
  196. '</div></div>';
  197. }
  198. $output .= '<div class="' . implode( ' ',
  199. $editor_css_classes ) . '" data-title="' . htmlspecialchars( __( 'Edit',
  200. 'js_composer' ) . ' ' . __( $this->setting( 'name' ), "js_composer" ) ) . '">';
  201. if ( is_array( $params ) ) {
  202. foreach ( $params as $param ) {
  203. $name = isset( $param['param_name'] ) ? $param['param_name'] : null;
  204. if ( ! is_null( $name ) ) {
  205. $value = isset( $this->atts[ $name ] ) ? $this->atts[ $name ] : null;
  206. $value = $this->parseShortcodeAttributeValue( $param, $value );
  207. $group = isset( $param['group'] ) && '' !== $param['group'] ? $param['group'] : '_general';
  208. if ( ! isset( $groups_content[ $group ] ) ) {
  209. $groups[] = $group;
  210. $groups_content[ $group ] = '';
  211. }
  212. $groups_content[ $group ] .= $this->renderField( $param, $value );
  213. }
  214. }
  215. }
  216. $output .= $this->renderGroupedFields( $groups, $groups_content );
  217. $output .= '</div>';
  218. $output .= $this->enqueueScripts();
  219. echo $output;
  220. do_action( 'vc_edit_form_fields_after_render' );
  221. }
  222. /**
  223. * Generate html for shortcode attribute.
  224. *
  225. * Method
  226. * @since 4.4
  227. *
  228. * @param $param
  229. * @param $value
  230. *
  231. * vc_filter: vc_single_param_edit - hook to edit any shortode param
  232. * vc_filter: vc_form_fields_render_field_{shortcode_name}_{param_name}_param_value - hook to edit shortcode param
  233. * value vc_filter: vc_form_fields_render_field_{shortcode_name}_{param_name}_param - hook to edit shortcode
  234. * param attributes vc_filter: vc_single_param_edit_holder_output - hook to edit output of this method
  235. *
  236. * @return mixed|void
  237. */
  238. protected function renderField( $param, $value ) {
  239. $param['vc_single_param_edit_holder_class'] = array(
  240. 'wpb_el_type_' . $param['type'],
  241. 'vc_wrapper-param-type-' . $param['type'],
  242. 'vc_shortcode-param'
  243. );
  244. if ( ! empty( $param['param_holder_class'] ) ) {
  245. $param['vc_single_param_edit_holder_class'][] = $param['param_holder_class'];
  246. }
  247. $param = apply_filters( 'vc_single_param_edit', $param, $value );
  248. $output = '<div class="' . implode( ' ',
  249. $param['vc_single_param_edit_holder_class'] ) . '" data-vc-ui-element="panel-shortcode-param" data-vc-shortcode-param-name="' . esc_attr( $param['param_name'] ) . '" data-param_type="' . esc_attr( $param['type'] ) . '" data-param_settings="' . esc_attr( json_encode( $param ) ) . '">';
  250. $output .= ( isset( $param['heading'] ) ) ? '<div class="wpb_element_label">' . $param['heading'] . '</div>' : '';
  251. $output .= '<div class="edit_form_line">';
  252. $value = apply_filters( 'vc_form_fields_render_field_' . $this->setting( 'base' ) . '_' . $param['param_name'] . '_param_value',
  253. $value,
  254. $param,
  255. $this->settings,
  256. $this->atts );
  257. $param = apply_filters( 'vc_form_fields_render_field_' . $this->setting( 'base' ) . '_' . $param['param_name'] . '_param',
  258. $param,
  259. $value,
  260. $this->settings,
  261. $this->atts );
  262. $output = apply_filters( 'vc_edit_form_fields_render_field_' . $param['type'] . '_before', $output );
  263. $output .= vc_do_shortcode_param_settings_field( $param['type'], $param, $value, $this->setting( 'base' ) );
  264. $output_after = '';
  265. if ( isset( $param['description'] ) ) {
  266. $output_after .= '<span class="vc_description vc_clearfix">' . $param['description'] . '</span>';
  267. }
  268. $output_after .= '</div></div>';
  269. $output .= apply_filters( 'vc_edit_form_fields_render_field_' . $param['type'] . '_after', $output_after );
  270. return apply_filters( 'vc_single_param_edit_holder_output',
  271. $output,
  272. $param,
  273. $value,
  274. $this->settings,
  275. $this->atts );
  276. }
  277. /**
  278. * Create default shortcode params
  279. *
  280. * List of params stored in global variable $vc_params_list.
  281. * Please check include/params/load.php for default params list.
  282. * @since 4.4
  283. * @return bool
  284. */
  285. public function loadDefaultParams() {
  286. global $vc_params_list;
  287. if ( empty( $vc_params_list ) ) {
  288. return false;
  289. }
  290. $script_url = vc_asset_url( 'js/params/all.js' );
  291. foreach ( $vc_params_list as $param ) {
  292. vc_add_shortcode_param( $param, 'vc_' . $param . '_form_field', $script_url );
  293. }
  294. do_action( 'vc_load_default_params' );
  295. return true;
  296. }
  297. }