PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/rubengrb/ws.vidrialum
PHP | 328 lines | 193 code | 20 blank | 115 comment | 24 complexity | d8787bf8acff22cb5b5dc4a1fd7b3c66 MD5 | raw file
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * WPBakery Visual Composer shortcode attributes fields
  7. *
  8. * @package WPBakeryVisualComposer
  9. *
  10. */
  11. /**
  12. * Edit form fields builder for shortcode attributes.
  13. *
  14. * @since 4.4
  15. */
  16. class Vc_Edit_Form_Fields implements Vc_Render {
  17. /**
  18. * @since 4.4
  19. * @var bool
  20. */
  21. protected $tag = false;
  22. /**
  23. * @since 4.4
  24. * @var array
  25. */
  26. protected $atts = array();
  27. /**
  28. * @since 4.4
  29. * @var array
  30. */
  31. protected $settings = array();
  32. /**
  33. * @since 4.4
  34. * @var bool
  35. */
  36. protected $post_id = false;
  37. /**
  38. * Construct Form fields.
  39. *
  40. * @since 4.4
  41. *
  42. * @param $tag - shortcode tag
  43. * @param $atts - list of attribute assign to the shortcode.
  44. */
  45. public function __construct( $tag, $atts ) {
  46. $this->tag = $tag;
  47. $this->atts = apply_filters( 'vc_edit_form_fields_attributes_' . $this->tag, $atts );
  48. $this->setSettings( WPBMap::getShortCode( $this->tag ) );
  49. }
  50. /**
  51. * Get settings
  52. * @since 4.4
  53. *
  54. * @param $key
  55. *
  56. * @return null
  57. */
  58. public function setting( $key ) {
  59. return isset( $this->settings[ $key ] ) ? $this->settings[ $key ] : null;
  60. }
  61. /**
  62. * Set settings data
  63. * @since 4.4
  64. *
  65. * @param array $settings
  66. */
  67. public function setSettings( array $settings ) {
  68. $this->settings = $settings;
  69. }
  70. /**
  71. * Shortcode Post ID getter.
  72. * If post id isn't set try to get from get_the_ID function.
  73. * @since 4.4
  74. * @return int|bool;
  75. */
  76. public function postId() {
  77. if ( false === $this->post_id ) {
  78. $this->post_id = get_the_ID();
  79. }
  80. return $this->post_id;
  81. }
  82. /**
  83. * Shortcode Post ID setter.
  84. * @since 4.4
  85. *
  86. * @param $post_id - integer value in post_id
  87. */
  88. public function setPostId( $post_id ) {
  89. $this->post_id = (int) $post_id;
  90. }
  91. /**
  92. * Get shortcode attribute value.
  93. *
  94. * This function checks if value isn't set then it uses std or value fields in param settings.
  95. * @since 4.4
  96. *
  97. * @param $param_settings
  98. * @param $value
  99. *
  100. * @return null
  101. */
  102. protected function parseShortcodeAttributeValue( $param_settings, $value ) {
  103. if ( is_null( $value ) ) { // If value doesn't exists
  104. if ( isset( $param_settings['std'] ) ) {
  105. $value = $param_settings['std'];
  106. } elseif ( isset( $param_settings['value'] ) && is_array( $param_settings['value'] ) && ! empty( $param_settings['type'] ) && 'checkbox' !== $param_settings['type']
  107. ) {
  108. $first_key = key( $param_settings['value'] );
  109. $value = $first_key ? $param_settings['value'][ $first_key ] : '';
  110. } elseif ( isset( $param_settings['value'] ) && ! is_array( $param_settings['value'] ) ) {
  111. $value = $param_settings['value'];
  112. }
  113. }
  114. return $value;
  115. }
  116. /**
  117. * Enqueue js scripts for attributes types.
  118. * @since 4.4
  119. * @return string
  120. */
  121. public function enqueueScripts() {
  122. $output = '';
  123. if ( ! WpbakeryShortcodeParams::isEnqueue() ) {
  124. $scripts = apply_filters( 'vc_edit_form_enqueue_script', WpbakeryShortcodeParams::getScripts() );
  125. foreach ( $scripts as $script ) {
  126. $output .= "\n\n" . '<script type="text/javascript" src="' . $script . '"></script>';
  127. }
  128. }
  129. return $output;
  130. }
  131. /**
  132. * Render grouped fields.
  133. * @since 4.4
  134. *
  135. * @param $groups
  136. * @param $groups_content
  137. *
  138. * @return string
  139. */
  140. protected function renderGroupedFields( $groups, $groups_content ) {
  141. $output = '';
  142. if ( sizeof( $groups ) > 1 || ( sizeof( $groups ) >= 1 && empty( $groups_content['_general'] ) ) ) {
  143. $output .= '<div class="vc_panel-tabs" id="vc_edit-form-tabs">';
  144. $output .= '<ul class="vc_general vc_ui-tabs-line" data-vc-ui-element="panel-tabs-controls">';
  145. $key = 0;
  146. foreach ( $groups as $g ) {
  147. $output .= '<li class="vc_edit-form-tab-control" data-tab-index="'
  148. . $key . '"><button data-vc-ui-element-target="#vc_edit-form-tab-'
  149. . $key ++ . '" class="vc_ui-tabs-line-trigger" data-vc-ui-element="panel-tab-control">'
  150. . ( '_general' === $g ? __( 'General', 'js_composer' ) : $g ) . '</button></li>';
  151. }
  152. $output .= '<li class="vc_ui-tabs-line-dropdown-toggle" data-vc-action="dropdown"
  153. data-vc-content=".vc_ui-tabs-line-dropdown" data-vc-ui-element="panel-tabs-line-toggle">
  154. <span class="vc_ui-tabs-line-trigger" data-vc-accordion
  155. data-vc-container=".vc_ui-tabs-line-dropdown-toggle"
  156. data-vc-target=".vc_ui-tabs-line-dropdown"> </span>
  157. <ul class="vc_ui-tabs-line-dropdown" data-vc-ui-element="panel-tabs-line-dropdown">
  158. </ul>
  159. </ul>';
  160. $key = 0;
  161. foreach ( $groups as $g ) {
  162. $output .= '<div id="vc_edit-form-tab-' . $key ++ . '" class="vc_edit-form-tab vc_row vc_ui-flex-row" data-vc-ui-element="panel-edit-element-tab">';
  163. $output .= $groups_content[ $g ];
  164. $output .= '</div>';
  165. }
  166. $output .= '</div>';
  167. } elseif ( ! empty( $groups_content['_general'] ) ) {
  168. $output .= '<div class="vc_edit-form-tab vc_row vc_ui-flex-row vc_active" data-vc-ui-element="panel-edit-element-tab">' . $groups_content['_general'] . '</div>';
  169. }
  170. return $output;
  171. }
  172. /**
  173. * Render fields html and output it.
  174. * @since 4.4
  175. * vc_filter: vc_edit_form_class - filter to override editor_css_classes array
  176. */
  177. public function render() {
  178. $this->loadDefaultParams();
  179. $output = $el_position = '';
  180. $groups_content = $groups = array();
  181. $params = $this->setting( 'params' );
  182. $editor_css_classes = apply_filters( 'vc_edit_form_class',
  183. array(
  184. 'wpb_edit_form_elements',
  185. 'vc_edit_form_elements',
  186. ),
  187. $this->atts,
  188. $params
  189. );
  190. $deprecated = $this->setting( 'deprecated' );
  191. require_once vc_path_dir( 'AUTOLOAD_DIR', 'class-vc-settings-presets.php' );
  192. $list_vendor_presets = Vc_Settings_Preset::listVendorSettingsPresets( $this->tag );
  193. $list_presets = Vc_Settings_Preset::listSettingsPresets( $this->tag );
  194. if ( vc_user_access()
  195. ->part( 'presets' )
  196. ->checkStateAny( true, null )
  197. ->get() || ! empty( $list_presets ) || ! empty( $list_vendor_presets )
  198. ) {
  199. $output .= '<script type="text/javascript">window.vc_presets_show=true;</script>';
  200. } else {
  201. $output .= '<script type="text/javascript">window.vc_presets_show=false;</script>';
  202. }
  203. if ( ! empty( $deprecated ) ) {
  204. $output .= '<div class="vc_row vc_ui-flex-row vc_shortcode-edit-form-deprecated-message"><div class="vc_col-sm-12 wpb_element_wrapper">' .
  205. vc_message_warning( sprintf( __( 'You are using outdated element, it is deprecated since version %s.', 'js_composer' ), $this->setting( 'deprecated' ) ) ) .
  206. '</div></div>';
  207. }
  208. $output .= '<div class="' . implode( ' ', $editor_css_classes ) . '" data-title="' . htmlspecialchars( __( 'Edit', 'js_composer' ) . ' ' . __( $this->setting( 'name' ), 'js_composer' ) ) . '">';
  209. if ( is_array( $params ) ) {
  210. foreach ( $params as $param ) {
  211. $name = isset( $param['param_name'] ) ? $param['param_name'] : null;
  212. if ( ! is_null( $name ) ) {
  213. $value = isset( $this->atts[ $name ] ) ? $this->atts[ $name ] : null;
  214. $value = $this->parseShortcodeAttributeValue( $param, $value );
  215. $group = isset( $param['group'] ) && '' !== $param['group'] ? $param['group'] : '_general';
  216. if ( ! isset( $groups_content[ $group ] ) ) {
  217. $groups[] = $group;
  218. $groups_content[ $group ] = '';
  219. }
  220. $groups_content[ $group ] .= $this->renderField( $param, $value );
  221. }
  222. }
  223. }
  224. $output .= $this->renderGroupedFields( $groups, $groups_content );
  225. $output .= '</div>';
  226. $output .= $this->enqueueScripts();
  227. echo $output;
  228. do_action( 'vc_edit_form_fields_after_render' );
  229. }
  230. /**
  231. * Generate html for shortcode attribute.
  232. *
  233. * Method
  234. * @since 4.4
  235. *
  236. * @param $param
  237. * @param $value
  238. *
  239. * vc_filter: vc_single_param_edit - hook to edit any shortode param
  240. * vc_filter: vc_form_fields_render_field_{shortcode_name}_{param_name}_param_value - hook to edit shortcode param
  241. * value vc_filter: vc_form_fields_render_field_{shortcode_name}_{param_name}_param - hook to edit shortcode
  242. * param attributes vc_filter: vc_single_param_edit_holder_output - hook to edit output of this method
  243. *
  244. * @return mixed|void
  245. */
  246. protected function renderField( $param, $value ) {
  247. $param['vc_single_param_edit_holder_class'] = array(
  248. 'wpb_el_type_' . $param['type'],
  249. 'vc_wrapper-param-type-' . $param['type'],
  250. 'vc_shortcode-param',
  251. 'vc_column',
  252. );
  253. if ( ! empty( $param['param_holder_class'] ) ) {
  254. $param['vc_single_param_edit_holder_class'][] = $param['param_holder_class'];
  255. }
  256. $param = apply_filters( 'vc_single_param_edit', $param, $value );
  257. $output = '<div class="' . implode( ' ', $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 ) ) . '">';
  258. $output .= ( isset( $param['heading'] ) ) ? '<div class="wpb_element_label">' . $param['heading'] . '</div>' : '';
  259. $output .= '<div class="edit_form_line">';
  260. $value = apply_filters( 'vc_form_fields_render_field_' . $this->setting( 'base' ) . '_' . $param['param_name'] . '_param_value',
  261. $value,
  262. $param,
  263. $this->settings,
  264. $this->atts
  265. );
  266. $param = apply_filters( 'vc_form_fields_render_field_' . $this->setting( 'base' ) . '_' . $param['param_name'] . '_param',
  267. $param,
  268. $value,
  269. $this->settings,
  270. $this->atts
  271. );
  272. $output = apply_filters( 'vc_edit_form_fields_render_field_' . $param['type'] . '_before', $output );
  273. $output .= vc_do_shortcode_param_settings_field( $param['type'], $param, $value, $this->setting( 'base' ) );
  274. $output_after = '';
  275. if ( isset( $param['description'] ) ) {
  276. $output_after .= '<span class="vc_description vc_clearfix">' . $param['description'] . '</span>';
  277. }
  278. $output_after .= '</div></div>';
  279. $output .= apply_filters( 'vc_edit_form_fields_render_field_' . $param['type'] . '_after', $output_after );
  280. return apply_filters( 'vc_single_param_edit_holder_output',
  281. $output,
  282. $param,
  283. $value,
  284. $this->settings,
  285. $this->atts
  286. );
  287. }
  288. /**
  289. * Create default shortcode params
  290. *
  291. * List of params stored in global variable $vc_params_list.
  292. * Please check include/params/load.php for default params list.
  293. * @since 4.4
  294. * @return bool
  295. */
  296. public function loadDefaultParams() {
  297. global $vc_params_list;
  298. if ( empty( $vc_params_list ) ) {
  299. return false;
  300. }
  301. $script_url = vc_asset_url( 'js/dist/edit-form.min.js' );
  302. foreach ( $vc_params_list as $param ) {
  303. vc_add_shortcode_param( $param, 'vc_' . $param . '_form_field', $script_url );
  304. }
  305. do_action( 'vc_load_default_params' );
  306. return true;
  307. }
  308. }