Nenhuma Descrição

class-textarea.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /**
  3. * Paragraph text field.
  4. *
  5. * @since 1.0.0
  6. */
  7. class WPForms_Field_Textarea extends WPForms_Field {
  8. /**
  9. * Primary class constructor.
  10. *
  11. * @since 1.0.0
  12. */
  13. public function init() {
  14. // Define field type information.
  15. $this->name = esc_html__( 'Paragraph Text', 'wpforms-lite' );
  16. $this->type = 'textarea';
  17. $this->icon = 'fa-paragraph';
  18. $this->order = 50;
  19. add_action( 'wpforms_frontend_js', array( $this, 'frontend_js' ) );
  20. }
  21. /**
  22. * Get the value, that is used to prefill via dynamic or fallback population.
  23. * Based on field data and current properties.
  24. *
  25. * @since 1.6.4
  26. *
  27. * @param string $raw_value Value from a GET param, always a string.
  28. * @param string $input Represent a subfield inside the field. May be empty.
  29. * @param array $properties Field properties.
  30. * @param array $field Current field specific data.
  31. *
  32. * @return array Modified field properties.
  33. */
  34. protected function get_field_populated_single_property_value( $raw_value, $input, $properties, $field ) {
  35. if ( ! is_string( $raw_value ) ) {
  36. return $properties;
  37. }
  38. if (
  39. ! empty( $input ) &&
  40. isset( $properties['inputs'][ $input ] )
  41. ) {
  42. $properties['inputs'][ $input ]['attr']['value'] = wpforms_sanitize_textarea_field( wp_unslash( $raw_value ) );
  43. }
  44. return $properties;
  45. }
  46. /**
  47. * Field options panel inside the builder.
  48. *
  49. * @since 1.0.0
  50. *
  51. * @param array $field Field data and settings.
  52. */
  53. public function field_options( $field ) {
  54. /*
  55. * Basic field options.
  56. */
  57. // Options open markup.
  58. $this->field_option(
  59. 'basic-options',
  60. $field,
  61. array(
  62. 'markup' => 'open',
  63. )
  64. );
  65. // Label.
  66. $this->field_option( 'label', $field );
  67. // Description.
  68. $this->field_option( 'description', $field );
  69. // Required toggle.
  70. $this->field_option( 'required', $field );
  71. // Options close markup.
  72. $this->field_option(
  73. 'basic-options',
  74. $field,
  75. array(
  76. 'markup' => 'close',
  77. )
  78. );
  79. /*
  80. * Advanced field options.
  81. */
  82. // Options open markup.
  83. $args = array(
  84. 'markup' => 'open',
  85. );
  86. $this->field_option( 'advanced-options', $field, $args );
  87. // Size.
  88. $this->field_option( 'size', $field );
  89. // Placeholder.
  90. $this->field_option( 'placeholder', $field );
  91. // Limit length.
  92. $args = array(
  93. 'slug' => 'limit_enabled',
  94. 'content' => $this->field_element(
  95. 'toggle',
  96. $field,
  97. array(
  98. 'slug' => 'limit_enabled',
  99. 'value' => isset( $field['limit_enabled'] ) ? '1' : '0',
  100. 'desc' => esc_html__( 'Limit Length', 'wpforms-lite' ),
  101. 'tooltip' => esc_html__( 'Check this option to limit text length by characters or words count.', 'wpforms-lite' ),
  102. ),
  103. false
  104. ),
  105. );
  106. $this->field_element( 'row', $field, $args );
  107. $count = $this->field_element(
  108. 'text',
  109. $field,
  110. array(
  111. 'type' => 'number',
  112. 'slug' => 'limit_count',
  113. 'attrs' => array(
  114. 'min' => 1,
  115. 'step' => 1,
  116. 'pattern' => '[0-9]',
  117. ),
  118. 'value' => ! empty( $field['limit_count'] ) ? $field['limit_count'] : 1,
  119. ),
  120. false
  121. );
  122. $mode = $this->field_element(
  123. 'select',
  124. $field,
  125. array(
  126. 'slug' => 'limit_mode',
  127. 'value' => ! empty( $field['limit_mode'] ) ? esc_attr( $field['limit_mode'] ) : 'characters',
  128. 'options' => array(
  129. 'characters' => esc_html__( 'Characters', 'wpforms-lite' ),
  130. 'words' => esc_html__( 'Words', 'wpforms-lite' ),
  131. ),
  132. ),
  133. false
  134. );
  135. $args = array(
  136. 'slug' => 'limit_controls',
  137. 'class' => ! isset( $field['limit_enabled'] ) ? 'wpforms-hide' : '',
  138. 'content' => $count . $mode,
  139. );
  140. $this->field_element( 'row', $field, $args );
  141. // Default value.
  142. $this->field_option( 'default_value', $field );
  143. // Custom CSS classes.
  144. $this->field_option( 'css', $field );
  145. // Hide label.
  146. $this->field_option( 'label_hide', $field );
  147. // Options close markup.
  148. $this->field_option(
  149. 'advanced-options',
  150. $field,
  151. [
  152. 'markup' => 'close',
  153. ]
  154. );
  155. }
  156. /**
  157. * Field preview inside the builder.
  158. *
  159. * @since 1.0.0
  160. *
  161. * @param array $field Field data and settings.
  162. */
  163. public function field_preview( $field ) {
  164. // Label.
  165. $this->field_preview_option( 'label', $field );
  166. // Primary input.
  167. $placeholder = ! empty( $field['placeholder'] ) ? $field['placeholder'] : '';
  168. echo '<textarea placeholder="' . esc_attr( $placeholder ) . '" class="primary-input" readonly></textarea>';
  169. // Description.
  170. $this->field_preview_option( 'description', $field );
  171. }
  172. /**
  173. * Field display on the form front-end.
  174. *
  175. * @since 1.0.0
  176. *
  177. * @param array $field Field data and settings.
  178. * @param array $deprecated Deprecated.
  179. * @param array $form_data Form data and settings.
  180. */
  181. public function field_display( $field, $deprecated, $form_data ) {
  182. // Define data.
  183. $primary = $field['properties']['inputs']['primary'];
  184. $value = '';
  185. if ( isset( $primary['attr']['value'] ) ) {
  186. $value = wpforms_sanitize_textarea_field( $primary['attr']['value'] );
  187. unset( $primary['attr']['value'] );
  188. }
  189. if ( isset( $field['limit_enabled'] ) ) {
  190. $limit_count = isset( $field['limit_count'] ) ? absint( $field['limit_count'] ) : 0;
  191. $limit_mode = isset( $field['limit_mode'] ) ? sanitize_key( $field['limit_mode'] ) : 'characters';
  192. $primary['data']['form-id'] = $form_data['id'];
  193. $primary['data']['field-id'] = $field['id'];
  194. if ( 'characters' === $limit_mode ) {
  195. $primary['class'][] = 'wpforms-limit-characters-enabled';
  196. $primary['attr']['maxlength'] = $limit_count;
  197. $primary['data']['text-limit'] = $limit_count;
  198. } else {
  199. $primary['class'][] = 'wpforms-limit-words-enabled';
  200. $primary['data']['text-limit'] = $limit_count;
  201. }
  202. }
  203. // Primary field.
  204. printf(
  205. '<textarea %s %s>%s</textarea>',
  206. wpforms_html_attributes( $primary['id'], $primary['class'], $primary['data'], $primary['attr'] ),
  207. $primary['required'], // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  208. $value // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  209. );
  210. }
  211. /**
  212. * Enqueue frontend limit option js.
  213. *
  214. * @since 1.5.6
  215. *
  216. * @param array $forms Forms on the current page.
  217. */
  218. public function frontend_js( $forms ) {
  219. // Get fields.
  220. $fields = array_map(
  221. function( $form ) {
  222. return empty( $form['fields'] ) ? array() : $form['fields'];
  223. },
  224. (array) $forms
  225. );
  226. // Make fields flat.
  227. $fields = array_reduce(
  228. $fields,
  229. function( $accumulator, $current ) {
  230. return array_merge( $accumulator, $current );
  231. },
  232. array()
  233. );
  234. // Leave only fields with limit.
  235. $fields = array_filter(
  236. $fields,
  237. function( $field ) {
  238. return $field['type'] === $this->type && isset( $field['limit_enabled'] );
  239. }
  240. );
  241. if ( count( $fields ) ) {
  242. $min = \wpforms_get_min_suffix();
  243. wp_enqueue_script( 'wpforms-text-limit', WPFORMS_PLUGIN_URL . "assets/js/text-limit{$min}.js", array(), WPFORMS_VERSION, true );
  244. }
  245. }
  246. /**
  247. * Format and sanitize field.
  248. *
  249. * @since 1.5.6
  250. *
  251. * @param int $field_id Field ID.
  252. * @param mixed $field_submit Field value that was submitted.
  253. * @param array $form_data Form data and settings.
  254. */
  255. public function format( $field_id, $field_submit, $form_data ) {
  256. $field = $form_data['fields'][ $field_id ];
  257. if ( is_array( $field_submit ) ) {
  258. $field_submit = implode( "\r\n", array_filter( $field_submit ) );
  259. }
  260. $name = ! empty( $field['label'] ) ? sanitize_text_field( $field['label'] ) : '';
  261. // Sanitize but keep line breaks.
  262. $value = wpforms_sanitize_textarea_field( $field_submit );
  263. wpforms()->process->fields[ $field_id ] = array(
  264. 'name' => $name,
  265. 'value' => $value,
  266. 'id' => absint( $field_id ),
  267. 'type' => $this->type,
  268. );
  269. }
  270. /**
  271. * Validate field on form submit.
  272. *
  273. * @since 1.6.2
  274. *
  275. * @param int $field_id Field ID.
  276. * @param mixed $field_submit Field value that was submitted.
  277. * @param array $form_data Form data and settings.
  278. */
  279. public function validate( $field_id, $field_submit, $form_data ) {
  280. parent::validate( $field_id, $field_submit, $form_data );
  281. if ( empty( $form_data['fields'][ $field_id ] ) || empty( $form_data['fields'][ $field_id ]['limit_enabled'] ) ) {
  282. return;
  283. }
  284. if ( is_array( $field_submit ) ) {
  285. $field_submit = implode( "\r\n", array_filter( $field_submit ) );
  286. }
  287. $field = $form_data['fields'][ $field_id ];
  288. $limit = absint( $field['limit_count'] );
  289. $mode = ! empty( $field['limit_mode'] ) ? sanitize_key( $field['limit_mode'] ) : 'characters';
  290. $value = wpforms_sanitize_textarea_field( $field_submit );
  291. if ( 'characters' === $mode ) {
  292. if ( mb_strlen( str_replace( "\r\n", "\n", $value ) ) > $limit ) {
  293. /* translators: %s - limit characters number. */
  294. wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = sprintf( _n( 'Text can\'t exceed %d character.', 'Text can\'t exceed %d characters.', $limit, 'wpforms-lite' ), $limit );
  295. return;
  296. }
  297. } else {
  298. if ( wpforms_count_words( $value ) > $limit ) {
  299. /* translators: %s - limit words number. */
  300. wpforms()->process->errors[ $form_data['id'] ][ $field_id ] = sprintf( _n( 'Text can\'t exceed %d word.', 'Text can\'t exceed %d words.', $limit, 'wpforms-lite' ), $limit );
  301. return;
  302. }
  303. }
  304. }
  305. }
  306. new WPForms_Field_Textarea();