No Description

class-gdpr-checkbox.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * GDPR Checkbox field.
  4. *
  5. * @since 1.4.6
  6. */
  7. class WPForms_Field_GDPR_Checkbox extends WPForms_Field {
  8. /**
  9. * Primary class constructor.
  10. *
  11. * @since 1.4.6
  12. */
  13. public function init() {
  14. // Define field type information.
  15. $this->name = esc_html__( 'GDPR Agreement', 'wpforms-lite' );
  16. $this->type = 'gdpr-checkbox';
  17. $this->icon = 'fa-check-square-o';
  18. $this->order = 500;
  19. $this->defaults = array(
  20. 1 => array(
  21. 'label' => esc_html__( 'I consent to having this website store my submitted information so they can respond to my inquiry.', 'wpforms-lite' ),
  22. 'value' => '',
  23. 'image' => '',
  24. 'default' => '',
  25. ),
  26. );
  27. // Set field to default to required.
  28. add_filter( 'wpforms_field_new_required', array( $this, 'field_default_required' ), 10, 2 );
  29. // Define additional field properties.
  30. add_filter( 'wpforms_field_properties_gdpr-checkbox', array( $this, 'field_properties' ), 5, 3 );
  31. }
  32. /**
  33. * Field should default to being required.
  34. *
  35. * @since 1.4.6
  36. *
  37. * @param bool $required Required status, true is required.
  38. * @param array $field Field settings.
  39. *
  40. * @return bool
  41. */
  42. public function field_default_required( $required, $field ) {
  43. if ( $this->type === $field['type'] ) {
  44. return true;
  45. }
  46. return $required;
  47. }
  48. /**
  49. * Define additional field properties.
  50. *
  51. * @since 1.4.6
  52. *
  53. * @param array $properties Field properties.
  54. * @param array $field Field settings.
  55. * @param array $form_data Form data and settings.
  56. *
  57. * @return array
  58. */
  59. public function field_properties( $properties, $field, $form_data ) {
  60. // Define data.
  61. $form_id = absint( $form_data['id'] );
  62. $field_id = absint( $field['id'] );
  63. $choices = ! empty( $field['choices'] ) ? $field['choices'] : [];
  64. // Remove primary input.
  65. unset( $properties['inputs']['primary'] );
  66. // Set input container (ul) properties.
  67. $properties['input_container'] = array(
  68. 'class' => array(),
  69. 'data' => array(),
  70. 'attr' => array(),
  71. 'id' => "wpforms-{$form_id}-field_{$field_id}",
  72. );
  73. // Set input properties.
  74. foreach ( $choices as $key => $choice ) {
  75. $properties['inputs'][ $key ] = array(
  76. 'container' => array(
  77. 'attr' => array(),
  78. 'class' => array( "choice-{$key}" ),
  79. 'data' => array(),
  80. 'id' => '',
  81. ),
  82. 'label' => array(
  83. 'attr' => array(
  84. 'for' => "wpforms-{$form_id}-field_{$field_id}_{$key}",
  85. ),
  86. 'class' => array( 'wpforms-field-label-inline' ),
  87. 'data' => array(),
  88. 'id' => '',
  89. 'text' => $choice['label'],
  90. ),
  91. 'attr' => array(
  92. 'name' => "wpforms[fields][{$field_id}][]",
  93. 'value' => $choice['label'],
  94. ),
  95. 'class' => array(),
  96. 'data' => array(),
  97. 'id' => "wpforms-{$form_id}-field_{$field_id}_{$key}",
  98. 'image' => '',
  99. 'required' => ! empty( $field['required'] ) ? 'required' : '',
  100. 'default' => '',
  101. );
  102. }
  103. // Required class for pagebreak validation.
  104. if ( ! empty( $field['required'] ) ) {
  105. $properties['input_container']['class'][] = 'wpforms-field-required';
  106. }
  107. return $properties;
  108. }
  109. /**
  110. * @inheritdoc
  111. */
  112. public function is_dynamic_population_allowed( $properties, $field ) {
  113. return false;
  114. }
  115. /**
  116. * Field options panel inside the builder.
  117. *
  118. * @since 1.4.6
  119. *
  120. * @param array $field Field settings.
  121. */
  122. public function field_options( $field ) {
  123. // Field is always required.
  124. $this->field_element(
  125. 'text',
  126. $field,
  127. array(
  128. 'type' => 'hidden',
  129. 'slug' => 'required',
  130. 'value' => '1',
  131. )
  132. );
  133. // -------------------------------------------------------------------//
  134. // Basic field options
  135. // -------------------------------------------------------------------//
  136. // Options open markup.
  137. $this->field_option(
  138. 'basic-options',
  139. $field,
  140. array(
  141. 'markup' => 'open',
  142. )
  143. );
  144. // Label.
  145. $this->field_option( 'label', $field );
  146. // Choices.
  147. $this->field_option(
  148. 'choices',
  149. $field,
  150. array(
  151. 'label' => esc_html__( 'Agreement', 'wpforms-lite' ),
  152. )
  153. );
  154. // Description.
  155. $this->field_option( 'description', $field );
  156. // Options close markup.
  157. $this->field_option(
  158. 'basic-options',
  159. $field,
  160. array(
  161. 'markup' => 'close',
  162. )
  163. );
  164. // -------------------------------------------------------------------//
  165. // Advanced field options
  166. // -------------------------------------------------------------------//
  167. // Options open markup.
  168. $this->field_option(
  169. 'advanced-options',
  170. $field,
  171. [
  172. 'markup' => 'open',
  173. ]
  174. );
  175. // Custom CSS classes.
  176. $this->field_option( 'css', $field );
  177. // Hide label.
  178. $this->field_option( 'label_hide', $field );
  179. // Options close markup.
  180. $this->field_option(
  181. 'advanced-options',
  182. $field,
  183. [
  184. 'markup' => 'close',
  185. ]
  186. );
  187. }
  188. /**
  189. * Field preview inside the builder.
  190. *
  191. * @since 1.4.6
  192. *
  193. * @param array $field Field settings.
  194. */
  195. public function field_preview( $field ) {
  196. // Label.
  197. $this->field_preview_option( 'label', $field );
  198. // Choices.
  199. $this->field_preview_option( 'choices', $field );
  200. // Description.
  201. $this->field_preview_option( 'description', $field );
  202. }
  203. /**
  204. * Field display on the form front-end.
  205. *
  206. * @since 1.4.6
  207. *
  208. * @param array $field Field settings.
  209. * @param array $deprecated Deprecated array.
  210. * @param array $form_data Form data and settings.
  211. */
  212. public function field_display( $field, $deprecated, $form_data ) {
  213. // Define data.
  214. $container = $field['properties']['input_container'];
  215. $choices = $field['properties']['inputs'];
  216. printf(
  217. '<ul %s>',
  218. wpforms_html_attributes( $container['id'], $container['class'], $container['data'], $container['attr'] )
  219. );
  220. foreach ( $choices as $key => $choice ) {
  221. $required = '';
  222. if ( ! empty( $choice['required'] ) && ! empty( $field['label_hide'] ) ) {
  223. $required = wpforms_get_field_required_label();
  224. }
  225. printf(
  226. '<li %s>',
  227. wpforms_html_attributes( $choice['container']['id'], $choice['container']['class'], $choice['container']['data'], $choice['container']['attr'] )
  228. );
  229. // Normal display.
  230. printf(
  231. '<input type="checkbox" %s %s %s>',
  232. wpforms_html_attributes( $choice['id'], $choice['class'], $choice['data'], $choice['attr'] ),
  233. esc_attr( $choice['required'] ),
  234. checked( '1', $choice['default'], false )
  235. );
  236. printf(
  237. '<label %s>%s%s</label>',
  238. wpforms_html_attributes( $choice['label']['id'], $choice['label']['class'], $choice['label']['data'], $choice['label']['attr'] ),
  239. wp_kses_post( $choice['label']['text'] ),
  240. $required
  241. ); // WPCS: XSS ok.
  242. echo '</li>';
  243. }
  244. echo '</ul>';
  245. }
  246. /**
  247. * Format and sanitize field.
  248. *
  249. * @since 1.4.6
  250. *
  251. * @param int $field_id Field ID.
  252. * @param array $field_submit Submitted form data.
  253. * @param array $form_data Form data and settings.
  254. */
  255. public function format( $field_id, $field_submit, $form_data ) {
  256. wpforms()->process->fields[ $field_id ] = array(
  257. 'name' => ! empty( $form_data['fields'][ $field_id ]['label'] ) ? sanitize_text_field( $form_data['fields'][ $field_id ]['label'] ) : '',
  258. 'value' => $form_data['fields'][ $field_id ]['choices'][1]['label'],
  259. 'id' => absint( $field_id ),
  260. 'type' => $this->type,
  261. );
  262. }
  263. }
  264. new WPForms_Field_GDPR_Checkbox();