Sin descripción

functions.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. /**
  3. * Output fields to be used on panels (settings etc).
  4. *
  5. * @since 1.0.0
  6. *
  7. * @param string $option
  8. * @param string $panel
  9. * @param string $field
  10. * @param array $form_data
  11. * @param string $label
  12. * @param array $args
  13. * @param bool $echo
  14. *
  15. * @return string
  16. */
  17. function wpforms_panel_field( $option, $panel, $field, $form_data, $label, $args = array(), $echo = true ) {
  18. // Required params.
  19. if ( empty( $option ) || empty( $panel ) || empty( $field ) ) {
  20. return '';
  21. }
  22. // Setup basic vars.
  23. $panel = esc_attr( $panel );
  24. $field = esc_attr( $field );
  25. $panel_id = sanitize_html_class( $panel );
  26. $parent = ! empty( $args['parent'] ) ? esc_attr( $args['parent'] ) : '';
  27. $subsection = ! empty( $args['subsection'] ) ? esc_attr( $args['subsection'] ) : '';
  28. $label = ! empty( $label ) ? esc_html( $label ) : '';
  29. $class = ! empty( $args['class'] ) ? wpforms_sanitize_classes( $args['class'] ) : '';
  30. $input_class = ! empty( $args['input_class'] ) ? wpforms_sanitize_classes( $args['input_class'] ) : '';
  31. $default = isset( $args['default'] ) ? $args['default'] : '';
  32. $placeholder = ! empty( $args['placeholder'] ) ? esc_attr( $args['placeholder'] ) : '';
  33. $data_attr = '';
  34. $output = '';
  35. $smarttags_toggle = '';
  36. $input_id = sprintf( 'wpforms-panel-field-%s-%s', sanitize_html_class( $panel_id ), sanitize_html_class( $field ) );
  37. if ( ! empty( $args['input_id'] ) ) {
  38. $input_id = esc_attr( $args['input_id'] );
  39. }
  40. if ( ! empty( $args['smarttags'] ) ) {
  41. $type = ! empty( $args['smarttags']['type'] ) ? esc_attr( $args['smarttags']['type'] ) : 'fields';
  42. $fields = ! empty( $args['smarttags']['fields'] ) ? esc_attr( $args['smarttags']['fields'] ) : '';
  43. $smarttags_toggle = sprintf(
  44. '<a href="#" class="toggle-smart-tag-display toggle-unfoldable-cont" data-type="%s" data-fields="%s">
  45. <i class="fa fa-tags"></i><span>%s</span>
  46. </a>',
  47. esc_attr( $type ),
  48. esc_attr( $fields ),
  49. esc_html__( 'Show Smart Tags', 'wpforms-lite' )
  50. );
  51. }
  52. if ( ! empty( $args['pro_badge'] ) ) {
  53. $label .= '<span class="wpforms-field-option-education-pro-badge">pro</span>';
  54. }
  55. // Check if we should store values in a parent array.
  56. if ( ! empty( $parent ) ) {
  57. if ( ! empty( $subsection ) ) {
  58. $field_name = sprintf( '%s[%s][%s][%s]', $parent, $panel, $subsection, $field );
  59. $value = isset( $form_data[ $parent ][ $panel ][ $subsection ][ $field ] ) ? $form_data[ $parent ][ $panel ][ $subsection ][ $field ] : $default;
  60. $input_id = sprintf( 'wpforms-panel-field-%s-%s-%s', sanitize_html_class( $panel_id ), sanitize_html_class( $subsection ), sanitize_html_class( $field ) );
  61. $panel_id = sanitize_html_class( $panel . '-' . $subsection );
  62. } else {
  63. $field_name = sprintf( '%s[%s][%s]', $parent, $panel, $field );
  64. $value = isset( $form_data[ $parent ][ $panel ][ $field ] ) ? $form_data[ $parent ][ $panel ][ $field ] : $default;
  65. }
  66. } else {
  67. $field_name = sprintf( '%s[%s]', $panel, $field );
  68. $value = isset( $form_data[ $panel ][ $field ] ) ? $form_data[ $panel ][ $field ] : $default;
  69. }
  70. if ( isset( $args['field_name'] ) ) {
  71. $field_name = $args['field_name'];
  72. }
  73. if ( isset( $args['value'] ) ) {
  74. $value = $args['value'];
  75. }
  76. // Check for data attributes.
  77. if ( ! empty( $args['data'] ) ) {
  78. foreach ( $args['data'] as $key => $val ) {
  79. if ( is_array( $val ) ) {
  80. $val = wp_json_encode( $val );
  81. }
  82. $data_attr .= ' data-' . $key . '=\'' . $val . '\'';
  83. }
  84. }
  85. // Check for readonly inputs.
  86. if ( ! empty( $args['readonly' ] ) ) {
  87. $data_attr .= 'readonly';
  88. }
  89. // Determine what field type to output.
  90. switch ( $option ) {
  91. // Text input.
  92. case 'text':
  93. $output = sprintf(
  94. '<input type="%s" id="%s" name="%s" value="%s" placeholder="%s" class="%s" %s>',
  95. ! empty( $args['type'] ) ? esc_attr( $args['type'] ) : 'text',
  96. $input_id,
  97. $field_name,
  98. esc_attr( $value ),
  99. $placeholder,
  100. $input_class,
  101. $data_attr
  102. );
  103. break;
  104. // Textarea.
  105. case 'textarea':
  106. $output = sprintf(
  107. '<textarea id="%s" name="%s" rows="%d" placeholder="%s" class="%s" %s>%s</textarea>',
  108. $input_id,
  109. $field_name,
  110. ! empty( $args['rows'] ) ? (int) $args['rows'] : '3',
  111. $placeholder,
  112. $input_class,
  113. $data_attr,
  114. esc_textarea( $value )
  115. );
  116. break;
  117. // TinyMCE.
  118. case 'tinymce':
  119. $id = str_replace( '-', '_', $input_id );
  120. $args['tinymce']['textarea_name'] = $field_name;
  121. $args['tinymce']['teeny'] = true;
  122. $args['tinymce'] = wp_parse_args( $args['tinymce'], array(
  123. 'media_buttons' => false,
  124. 'teeny' => true,
  125. ) );
  126. ob_start();
  127. wp_editor( $value, $id, $args['tinymce'] );
  128. $output = ob_get_clean();
  129. break;
  130. // Checkbox.
  131. case 'checkbox':
  132. $output = sprintf(
  133. '<input type="checkbox" id="%s" name="%s" value="1" class="%s" %s %s>',
  134. $input_id,
  135. $field_name,
  136. $input_class,
  137. checked( '1', $value, false ),
  138. $data_attr
  139. );
  140. $output .= sprintf(
  141. '<label for="%s" class="inline">%s',
  142. $input_id,
  143. $label
  144. );
  145. if ( ! empty( $args['tooltip'] ) ) {
  146. $output .= sprintf( '<i class="fa fa-question-circle-o wpforms-help-tooltip" title="%s"></i>', esc_attr( $args['tooltip'] ) );
  147. }
  148. $output .= '</label>';
  149. break;
  150. // Toggle.
  151. case 'toggle':
  152. $toggle_args = $args;
  153. $toggle_args['input-class'] = $input_class;
  154. $output = wpforms_panel_field_toggle_control( $toggle_args, $input_id, $field_name, $label, $value, $data_attr );
  155. break;
  156. // Radio.
  157. case 'radio':
  158. $options = $args['options'];
  159. $radio_counter = 1;
  160. $output = '';
  161. foreach ( $options as $key => $item ) {
  162. if ( empty( $item['label'] ) ) {
  163. continue;
  164. }
  165. $item_value = ! empty( $item['value'] ) ? $item['value'] : $key;
  166. $output .= '<span class="row">';
  167. if ( ! empty( $item['pre_label'] ) ) {
  168. $output .= '<label>' . $item['pre_label'];
  169. }
  170. $output .= sprintf(
  171. '<input type="radio" id="%s-%d" name="%s" value="%s" class="%s" %s %s>',
  172. $input_id,
  173. $radio_counter,
  174. $field_name,
  175. $item_value,
  176. $input_class,
  177. checked( $item_value, $value, false ),
  178. $data_attr
  179. );
  180. if ( empty( $item['pre_label'] ) ) {
  181. $output .= sprintf(
  182. '<label for="%s-%d" class="inline">%s',
  183. $input_id,
  184. $radio_counter,
  185. $item['label']
  186. );
  187. } else {
  188. $output .= '<span class="wpforms-panel-field-radio-label">' . $item['label'] . '</span>';
  189. }
  190. if ( ! empty( $item['tooltip'] ) ) {
  191. $output .= sprintf( '<i class="fa fa-question-circle-o wpforms-help-tooltip" title="%s"></i>', esc_attr( $item['tooltip'] ) );
  192. }
  193. $output .= '</label></span>';
  194. $radio_counter ++;
  195. }
  196. if ( ! empty( $output ) ) {
  197. $output = '<div class="wpforms-panel-field-radio-container">' . $output . '</div>';
  198. }
  199. break;
  200. // Select.
  201. case 'select':
  202. if ( empty( $args['options'] ) && empty( $args['field_map'] ) ) {
  203. return '';
  204. }
  205. if ( ! empty( $args['field_map'] ) ) {
  206. $options = array();
  207. $available_fields = wpforms_get_form_fields( $form_data, $args['field_map'] );
  208. if ( ! empty( $available_fields ) ) {
  209. foreach ( $available_fields as $id => $available_field ) {
  210. $options[ $id ] = ! empty( $available_field['label'] )
  211. ? esc_attr( $available_field['label'] )
  212. : sprintf( /* translators: %d - field ID. */
  213. esc_html__( 'Field #%d', 'wpforms-lite' ),
  214. absint( $id )
  215. );
  216. }
  217. }
  218. $input_class .= ' wpforms-field-map-select';
  219. $data_attr .= ' data-field-map-allowed="' . implode( ' ', $args['field_map'] ) . '"';
  220. if ( ! empty( $placeholder ) ) {
  221. $data_attr .= ' data-field-map-placeholder="' . esc_attr( $placeholder ) . '"';
  222. }
  223. } else {
  224. $options = $args['options'];
  225. }
  226. $output = sprintf(
  227. '<select id="%s" name="%s" class="%s" %s>',
  228. $input_id,
  229. $field_name,
  230. $input_class,
  231. $data_attr
  232. );
  233. if ( ! empty( $placeholder ) ) {
  234. $output .= '<option value="">' . $placeholder . '</option>';
  235. }
  236. foreach ( $options as $key => $item ) {
  237. $output .= sprintf( '<option value="%s" %s>%s</option>', esc_attr( $key ), selected( $key, $value, false ), $item );
  238. }
  239. $output .= '</select>';
  240. break;
  241. }
  242. // Put the pieces together.
  243. $field_open = sprintf(
  244. '<div id="%s-wrap" class="wpforms-panel-field %s %s">',
  245. $input_id,
  246. $class,
  247. 'wpforms-panel-field-' . sanitize_html_class( $option )
  248. );
  249. $field_open .= ! empty( $args['before'] ) ? $args['before'] : '';
  250. if ( $option !== 'toggle' && $option !== 'checkbox' && ! empty( $label ) ) {
  251. $field_label = sprintf(
  252. '<label for="%s">%s',
  253. $input_id,
  254. $label
  255. );
  256. if ( ! empty( $args['tooltip'] ) ) {
  257. $field_label .= sprintf( '<i class="fa fa-question-circle-o wpforms-help-tooltip" title="%s"></i>', esc_attr( $args['tooltip'] ) );
  258. }
  259. if ( ! empty( $args['after_tooltip'] ) ) {
  260. $field_label .= $args['after_tooltip'];
  261. }
  262. if ( $smarttags_toggle && ! ( $option === 'textarea' && ! empty( $args['tinymce'] ) ) ) {
  263. $field_label .= $smarttags_toggle;
  264. }
  265. $field_label .= '</label>';
  266. if ( ! empty( $args['after_label'] ) ) {
  267. $field_label .= $args['after_label'];
  268. }
  269. } else {
  270. $field_label = '';
  271. }
  272. $field_close = '';
  273. if ( $smarttags_toggle && $option === 'textarea' && ! empty( $args['tinymce'] ) ) {
  274. $field_close .= $smarttags_toggle;
  275. }
  276. $field_close .= ! empty( $args['after'] ) ? $args['after'] : '';
  277. $field_close .= '</div>';
  278. $output = $field_open . $field_label . $output . $field_close;
  279. // Wash our hands.
  280. if ( $echo ) {
  281. echo $output;
  282. } else {
  283. return $output;
  284. }
  285. }
  286. /**
  287. * Create toggle control.
  288. *
  289. * It's like a regular checkbox but with a modern visual appearance.
  290. *
  291. * @since 1.6.8
  292. *
  293. * @param array $args Arguments array.
  294. *
  295. * @type bool $status If `true`, control will display the current status next to the toggle.
  296. * @type string $status-on Status `On` text. By default `On`.
  297. * @type string $status-off Status `Off` text. By default `Off`.
  298. * @type bool $label-hide If `true` then label will not display.
  299. * @type string $tooltip Tooltip text.
  300. * @type string $input-class CSS class for the hidden `<input type=checkbox>`.
  301. * @type string $control-class CSS class for the wrapper `<span>`.
  302. *
  303. * @param string $input_id Input ID.
  304. * @param string $field_name Field name.
  305. * @param string $label Label text. Can contain HTML in order to display additional badges.
  306. * @param mixed $value Value.
  307. * @param string $data_attr Attributes.
  308. *
  309. * @return string
  310. */
  311. function wpforms_panel_field_toggle_control( $args, $input_id, $field_name, $label, $value, $data_attr ) {
  312. $checked = checked( true, (bool) $value, false );
  313. $status = '';
  314. if ( ! empty( $args['status'] ) ) {
  315. $status_on = ! empty( $args['status-on'] ) ? $args['status-on'] : esc_html__( 'On', 'wpforms-lite' );
  316. $status_off = ! empty( $args['status-off'] ) ? $args['status-off'] : esc_html__( 'Off', 'wpforms-lite' );
  317. $status = sprintf(
  318. '<label
  319. for="%s"
  320. class="wpforms-toggle-control-status"
  321. data-on="%s"
  322. data-off="%s">
  323. %s
  324. </label>',
  325. esc_attr( $input_id ),
  326. esc_attr( $status_on ),
  327. esc_attr( $status_off ),
  328. esc_html( ! empty( $args['value'] ) ? $status_on : $status_off )
  329. );
  330. }
  331. $label_html = empty( $args['label-hide'] ) && ! empty( $label ) ?
  332. sprintf(
  333. '<label for="%s" class="wpforms-toggle-control-label">%s</label>',
  334. esc_attr( $input_id ),
  335. $label
  336. ) : '';
  337. $label_html .= isset( $args['tooltip'] ) ?
  338. sprintf(
  339. '<i class="fa fa-question-circle-o wpforms-help-tooltip" title="%s"></i>',
  340. esc_attr( $args['tooltip'] )
  341. ) : '';
  342. $label_left = ! empty( $args['label-left'] ) ? $label_html . $status : '';
  343. $label_right = empty( $args['label-left'] ) ? $status . $label_html : '';
  344. $title = isset( $args['title'] ) ? ' title="' . esc_attr( $args['title'] ) . '"' : '';
  345. $control_class = ! empty( $args['control-class'] ) ? $args['control-class'] : '';
  346. $input_class = ! empty( $args['input-class'] ) ? $args['input-class'] : '';
  347. return sprintf(
  348. '<span class="wpforms-toggle-control %8$s" %9$s>
  349. %1$s
  350. <input type="checkbox" id="%2$s" name="%3$s" class="%7$s" value="1" %4$s %5$s>
  351. <label class="wpforms-toggle-control-icon" for="%2$s"></label>
  352. %6$s
  353. </span>',
  354. $label_left,
  355. esc_attr( $input_id ),
  356. esc_attr( $field_name ),
  357. $checked,
  358. $data_attr,
  359. $label_right,
  360. wpforms_sanitize_classes( $input_class ),
  361. wpforms_sanitize_classes( $control_class ),
  362. $title
  363. );
  364. }
  365. /**
  366. * Get settings block state, whether it's opened or closed.
  367. *
  368. * @since 1.4.8
  369. *
  370. * @param int $form_id
  371. * @param int $block_id
  372. * @param string $block_type
  373. *
  374. * @return string
  375. */
  376. function wpforms_builder_settings_block_get_state( $form_id, $block_id, $block_type ) {
  377. $form_id = absint( $form_id );
  378. $block_id = absint( $block_id );
  379. $block_type = sanitize_key( $block_type );
  380. $state = 'opened';
  381. $all_states = get_user_meta( get_current_user_id(), 'wpforms_builder_settings_collapsable_block_states', true );
  382. if ( empty( $all_states ) ) {
  383. return $state;
  384. }
  385. if (
  386. is_array( $all_states ) &&
  387. ! empty( $all_states[ $form_id ][ $block_type ][ $block_id ] ) &&
  388. 'closed' === $all_states[ $form_id ][ $block_type ][ $block_id ]
  389. ) {
  390. $state = 'closed';
  391. }
  392. // Backward compatibility for notifications.
  393. if ( 'notification' === $block_type && 'closed' !== $state ) {
  394. $notification_states = get_user_meta( get_current_user_id(), 'wpforms_builder_notification_states', true );
  395. }
  396. if (
  397. ! empty( $notification_states[ $form_id ][ $block_id ] ) &&
  398. 'closed' === $notification_states[ $form_id ][ $block_id ]
  399. ) {
  400. $state = 'closed';
  401. }
  402. if ( 'notification' === $block_type ) {
  403. // Backward compatibility for notifications.
  404. return apply_filters( 'wpforms_builder_notification_get_state', $state, $form_id, $block_id );
  405. }
  406. return apply_filters( 'wpforms_builder_settings_block_get_state', $state, $form_id, $block_id, $block_type );
  407. }
  408. /**
  409. * Get the list of allowed tags, used in pair with wp_kses() function.
  410. * This allows getting rid of all potentially harmful HTML tags and attributes.
  411. *
  412. * @since 1.5.9
  413. *
  414. * @return array Allowed Tags.
  415. */
  416. function wpforms_builder_preview_get_allowed_tags() {
  417. static $allowed_tags;
  418. if ( ! empty( $allowed_tags ) ) {
  419. return $allowed_tags;
  420. }
  421. $atts = [ 'align', 'class', 'type', 'id', 'for', 'style', 'src', 'rel', 'href', 'target', 'value', 'width', 'height' ];
  422. $tags = [ 'label', 'iframe', 'style', 'button', 'strong', 'small', 'table', 'span', 'abbr', 'code', 'pre', 'div', 'img', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ol', 'ul', 'li', 'em', 'hr', 'br', 'th', 'tr', 'td', 'p', 'a', 'b', 'i' ];
  423. $allowed_atts = array_fill_keys( $atts, [] );
  424. $allowed_tags = array_fill_keys( $tags, $allowed_atts );
  425. return $allowed_tags;
  426. }
  427. /**
  428. * Output builder panel fields group wrapper.
  429. *
  430. * @since 1.6.6
  431. *
  432. * @param string $inner Inner HTML to wrap.
  433. * @param array $args Array of arguments.
  434. * @param bool $echo Flag to display.
  435. *
  436. * @return string
  437. */
  438. function wpforms_panel_fields_group( $inner, $args = [], $echo = true ) {
  439. $group = ! empty( $args['group'] ) ? $args['group'] : '';
  440. $unfoldable = ! empty( $args['unfoldable'] );
  441. $opened = ! empty( $_COOKIE[ 'wpforms_fields_group_' . $group ] ) && $_COOKIE[ 'wpforms_fields_group_' . $group ] === 'true' ? ' opened' : '';
  442. $output = sprintf(
  443. '<div class="wpforms-panel-fields-group%1$s"%2$s>',
  444. $unfoldable ? ' unfoldable' . $opened : '',
  445. $unfoldable ? ' data-group="' . $group . '"' : ''
  446. );
  447. if ( ! empty( $args['borders'] ) && in_array( 'top', $args['borders'], true ) ) {
  448. $output .= '<div class="wpforms-panel-fields-group-border-top"></div>';
  449. }
  450. if ( ! empty( $args['title'] ) ) {
  451. $chevron = $unfoldable ? '<i class="fa fa-chevron-circle-right"></i>' : '';
  452. $output .= '<div class="wpforms-panel-fields-group-title">' . esc_html( $args['title'] ) . $chevron . '</div>';
  453. }
  454. if ( ! empty( $args['description'] ) ) {
  455. $output .= '<div class="wpforms-panel-fields-group-description">' . wp_kses_post( $args['description'] ) . '</div>';
  456. }
  457. $output .= sprintf(
  458. '<div class="wpforms-panel-fields-group-inner"%s>' . $inner . '</div>',
  459. empty( $opened ) && $unfoldable ? ' style="display: none;"' : ''
  460. );
  461. if ( ! empty( $args['borders'] ) && in_array( 'bottom', $args['borders'], true ) ) {
  462. $output .= '<div class="wpforms-panel-fields-group-border-bottom"></div>';
  463. }
  464. $output .= '</div>';
  465. if ( ! $echo ) {
  466. return $output;
  467. }
  468. echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  469. }