Нет описания

class-process.php 32KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007
  1. <?php
  2. /**
  3. * Process and validate form entries.
  4. *
  5. * @since 1.0.0
  6. */
  7. class WPForms_Process {
  8. /**
  9. * Store errors.
  10. *
  11. * @since 1.0.0
  12. *
  13. * @var array
  14. */
  15. public $errors;
  16. /**
  17. * Confirmation message.
  18. *
  19. * @var string
  20. */
  21. public $confirmation_message;
  22. /**
  23. * Current confirmation.
  24. *
  25. * @since 1.6.9
  26. *
  27. * @var array
  28. */
  29. private $confirmation;
  30. /**
  31. * Store formatted fields.
  32. *
  33. * @since 1.0.0
  34. *
  35. * @var array
  36. */
  37. public $fields;
  38. /**
  39. * Store the ID of a successful entry.
  40. *
  41. * @since 1.2.3
  42. *
  43. * @var int
  44. */
  45. public $entry_id = 0;
  46. /**
  47. * Form data and settings.
  48. *
  49. * @since 1.4.5
  50. *
  51. * @var array
  52. */
  53. public $form_data;
  54. /**
  55. * If a valid return has was processed.
  56. *
  57. * @since 1.4.5
  58. *
  59. * @var bool
  60. */
  61. public $valid_hash = false;
  62. /**
  63. * Primary class constructor.
  64. *
  65. * @since 1.0.0
  66. */
  67. public function __construct() {
  68. add_action( 'wp', array( $this, 'listen' ) );
  69. add_action( 'wp_ajax_wpforms_submit', array( $this, 'ajax_submit' ) );
  70. add_action( 'wp_ajax_nopriv_wpforms_submit', array( $this, 'ajax_submit' ) );
  71. }
  72. /**
  73. * Listen to see if this is a return callback or a posted form entry.
  74. *
  75. * @since 1.0.0
  76. */
  77. public function listen() {
  78. // Catch the post_max_size overflow.
  79. if ( $this->post_max_size_overflow() ) {
  80. return;
  81. }
  82. // phpcs:disable WordPress.Security.NonceVerification
  83. if ( ! empty( $_GET['wpforms_return'] ) ) {
  84. // Additional redirect trigger for addons.
  85. $this->entry_confirmation_redirect( '', sanitize_text_field( wp_unslash( $_GET['wpforms_return'] ) ) );
  86. }
  87. $form_id = ! empty( $_POST['wpforms']['id'] ) ? absint( $_POST['wpforms']['id'] ) : 0;
  88. if ( ! $form_id ) {
  89. return;
  90. }
  91. // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
  92. $this->process( wp_unslash( $_POST['wpforms'] ) );
  93. // phpcs:enable WordPress.Security.NonceVerification
  94. if ( ! wpforms_is_amp() ) {
  95. return;
  96. }
  97. // Send 400 Bad Request when there are errors.
  98. if ( empty( $this->errors[ $form_id ] ) ) {
  99. wp_send_json(
  100. [
  101. 'message' => $this->get_confirmation_message( $this->form_data, $this->fields, $this->entry_id ),
  102. ],
  103. 200
  104. );
  105. return;
  106. }
  107. $message = $this->errors[ $form_id ]['header'];
  108. if ( ! empty( $this->errors[ $form_id ]['footer'] ) ) {
  109. $message .= ' ' . $this->errors[ $form_id ]['footer'];
  110. }
  111. wp_send_json(
  112. [
  113. 'message' => $message,
  114. ],
  115. 400
  116. );
  117. }
  118. /**
  119. * Process the form entry.
  120. *
  121. * @since 1.0.0
  122. * @since 1.6.4 Added hCaptcha support.
  123. *
  124. * @param array $entry Form submission raw data ($_POST).
  125. */
  126. public function process( $entry ) {
  127. $this->errors = array();
  128. $this->fields = array();
  129. $form_id = absint( $entry['id'] );
  130. $form = wpforms()->form->get( $form_id );
  131. // Validate form is real and active (published).
  132. if ( ! $form || 'publish' !== $form->post_status ) {
  133. $this->errors[ $form_id ]['header'] = esc_html__( 'Invalid form.', 'wpforms-lite' );
  134. return;
  135. }
  136. // Formatted form data for hooks.
  137. $this->form_data = apply_filters( 'wpforms_process_before_form_data', wpforms_decode( $form->post_content ), $entry );
  138. // Pre-process/validate hooks and filter.
  139. // Data is not validated or cleaned yet so use with caution.
  140. $entry = apply_filters( 'wpforms_process_before_filter', $entry, $this->form_data );
  141. do_action( 'wpforms_process_before', $entry, $this->form_data );
  142. do_action( "wpforms_process_before_{$form_id}", $entry, $this->form_data );
  143. // Validate fields.
  144. foreach ( $this->form_data['fields'] as $field_properties ) {
  145. $field_id = $field_properties['id'];
  146. $field_type = $field_properties['type'];
  147. $field_submit = isset( $entry['fields'][ $field_id ] ) ? $entry['fields'][ $field_id ] : '';
  148. do_action( "wpforms_process_validate_{$field_type}", $field_id, $field_submit, $this->form_data );
  149. }
  150. // CAPTCHA check.
  151. $captcha_settings = wpforms_get_captcha_settings();
  152. $bypass_captcha = apply_filters( 'wpforms_process_bypass_captcha', false, $entry, $this->form_data );
  153. if (
  154. ! empty( $captcha_settings['provider'] ) &&
  155. $captcha_settings['provider'] !== 'none' &&
  156. ! empty( $captcha_settings['site_key'] ) &&
  157. ! empty( $captcha_settings['secret_key'] ) &&
  158. isset( $this->form_data['settings']['recaptcha'] ) &&
  159. (int) $this->form_data['settings']['recaptcha'] === 1 &&
  160. empty( $bypass_captcha ) &&
  161. ! isset( $_POST['__amp_form_verify'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing -- No need to check CAPTCHA until form is submitted.
  162. &&
  163. ( ( $captcha_settings['provider'] === 'recaptcha' && $captcha_settings['recaptcha_type'] === 'v3' ) || ! wpforms_is_amp() ) // AMP requires Google reCAPTCHA v3.
  164. ) {
  165. if ( $captcha_settings['provider'] === 'hcaptcha' ) {
  166. $verify_url_raw = 'https://hcaptcha.com/siteverify';
  167. $captcha_provider = esc_html__( 'hCaptcha', 'wpforms-lite' );
  168. $post_key = 'h-captcha-response';
  169. } else {
  170. $verify_url_raw = 'https://www.google.com/recaptcha/api/siteverify';
  171. $captcha_provider = esc_html__( 'Google reCAPTCHA', 'wpforms-lite' );
  172. $post_key = 'g-recaptcha-response';
  173. }
  174. /* translators: %s - The CAPTCHA provider name. */
  175. $error = wpforms_setting( "{$captcha_settings['provider']}-fail-msg", sprintf( esc_html__( '%s verification failed, please try again later.', 'wpforms-lite' ), $captcha_provider ) );
  176. $token = ! empty( $_POST[ $post_key ] ) ? $_POST[ $post_key ] : false; // phpcs:ignore
  177. $is_recaptcha_v3 = $captcha_settings['provider'] === 'recaptcha' && $captcha_settings['recaptcha_type'] === 'v3';
  178. if ( $is_recaptcha_v3 ) {
  179. $token = ! empty( $_POST['wpforms']['recaptcha'] ) ? $_POST['wpforms']['recaptcha'] : false; // phpcs:ignore
  180. }
  181. $verify_query_arg = [
  182. 'secret' => $captcha_settings['secret_key'],
  183. 'response' => $token,
  184. 'remoteip' => wpforms_get_ip(),
  185. ];
  186. /*
  187. * hCaptcha uses user IP to better detect bots and their attacks on a form.
  188. * Majority of our users have GDPR disabled.
  189. * So we remove this data from the request only when it's not needed, depending on wpforms_is_collecting_ip_allowed($this->form_data) check.
  190. */
  191. if ( ! wpforms_is_collecting_ip_allowed( $this->form_data ) ) {
  192. unset( $verify_query_arg['remoteip'] );
  193. }
  194. $verify_url = add_query_arg( $verify_query_arg, $verify_url_raw );
  195. /**
  196. * Filter the CAPTCHA verify URL.
  197. *
  198. * @since 1.6.4
  199. *
  200. * @param string $verify_url The full CAPTCHA verify URL.
  201. * @param string $verify_url_raw The CAPTCHA verify URL without query.
  202. * @param string $verify_query_arg The query arguments for verify URL.
  203. */
  204. $verify_url = apply_filters( 'wpforms_process_captcha_verify_url', $verify_url, $verify_url_raw, $verify_query_arg );
  205. // API call.
  206. $response = json_decode( wp_remote_retrieve_body( wp_remote_get( $verify_url ) ) );
  207. if (
  208. empty( $response->success ) ||
  209. ( $is_recaptcha_v3 && $response->score <= wpforms_setting( 'recaptcha-v3-threshold', '0.4' ) )
  210. ) {
  211. if ( $is_recaptcha_v3 ) {
  212. if ( isset( $response->score ) ) {
  213. $error .= ' (' . esc_html( $response->score ) . ')';
  214. }
  215. $this->errors[ $form_id ]['footer'] = $error;
  216. } else {
  217. $this->errors[ $form_id ]['recaptcha'] = $error;
  218. }
  219. }
  220. }
  221. // Check if combined upload size exceeds allowed maximum.
  222. $this->validate_combined_upload_size( $form );
  223. // Initial error check.
  224. // Don't proceed if there are any errors thus far. We provide a filter
  225. // so that other features, such as conditional logic, have the ability
  226. // to adjust blocking errors.
  227. $errors = apply_filters( 'wpforms_process_initial_errors', $this->errors, $this->form_data );
  228. if ( isset( $_POST['__amp_form_verify'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
  229. if ( empty( $errors[ $form_id ] ) ) {
  230. wp_send_json( array(), 200 );
  231. } else {
  232. $verify_errors = array();
  233. foreach ( $errors[ $form_id ] as $field_id => $error_fields ) {
  234. $field = $this->form_data['fields'][ $field_id ];
  235. $field_properties = wpforms()->frontend->get_field_properties( $field, $this->form_data );
  236. if ( is_string( $error_fields ) ) {
  237. if ( 'checkbox' === $field['type'] || 'radio' === $field['type'] || 'select' === $field['type'] ) {
  238. $first = current( $field_properties['inputs'] );
  239. $name = $first['attr']['name'];
  240. } elseif ( isset( $field_properties['inputs']['primary']['attr']['name'] ) ) {
  241. $name = $field_properties['inputs']['primary']['attr']['name'];
  242. }
  243. $verify_errors[] = array(
  244. 'name' => $name,
  245. 'message' => $error_fields,
  246. );
  247. } else {
  248. foreach ( $error_fields as $error_field => $error_message ) {
  249. if ( isset( $field_properties['inputs'][ $error_field ]['attr']['name'] ) ) {
  250. $name = $field_properties['inputs'][ $error_field ]['attr']['name'];
  251. }
  252. $verify_errors[] = array(
  253. 'name' => $name,
  254. 'message' => $error_message,
  255. );
  256. }
  257. }
  258. }
  259. wp_send_json(
  260. array(
  261. 'verifyErrors' => $verify_errors,
  262. ),
  263. 400
  264. );
  265. }
  266. return;
  267. }
  268. if ( ! empty( $errors[ $form_id ] ) ) {
  269. if ( empty( $errors[ $form_id ]['header'] ) ) {
  270. $errors[ $form_id ]['header'] = esc_html__( 'Form has not been submitted, please see the errors below.', 'wpforms-lite' );
  271. }
  272. $this->errors = $errors;
  273. return;
  274. }
  275. // If a logged-in user fails the nonce check, we want to log the entry, disable the errors and fail silently.
  276. // Please note that logs may be disabled and in this case nothing will be logged or reported.
  277. if (
  278. is_user_logged_in() &&
  279. ( empty( $entry['nonce'] ) || ! wp_verify_nonce( $entry['nonce'], "wpforms::form_{$form_id}" ) )
  280. ) {
  281. // Logs XSS attempt depending on log levels set.
  282. wpforms_log(
  283. 'Cross-site scripting attempt ' . uniqid( '', true ),
  284. [ true, $entry ],
  285. [
  286. 'type' => [ 'security' ],
  287. 'form_id' => $this->form_data['id'],
  288. ]
  289. );
  290. // Fail silently.
  291. return;
  292. }
  293. $honeypot = wpforms()->get( 'honeypot' )->validate( $this->form_data, $this->fields, $entry );
  294. // If we trigger the honey pot, we want to log the entry, disable the errors, and fail silently.
  295. if ( $honeypot ) {
  296. // Logs spam entry depending on log levels set.
  297. wpforms_log(
  298. 'Spam Entry ' . uniqid(),
  299. array( $honeypot, $entry ),
  300. array(
  301. 'type' => array( 'spam' ),
  302. 'form_id' => $this->form_data['id'],
  303. )
  304. );
  305. // Fail silently.
  306. return;
  307. }
  308. $antispam = wpforms()->get( 'token' )->validate( $this->form_data, $this->fields, $entry );
  309. // If spam - return early.
  310. // For antispam, we want to make sure that we have a value, we are not using AMP, and the value is an error string.
  311. if ( $antispam && ! wpforms_is_amp() && is_string( $antispam ) ) {
  312. if ( $antispam ) {
  313. $this->errors[ $form_id ]['header'] = $antispam;
  314. }
  315. // Logs spam entry depending on log levels set.
  316. wpforms_log(
  317. esc_html__( 'Spam Entry ' ) . uniqid(),
  318. array( $antispam, $entry ),
  319. array(
  320. 'type' => array( 'spam' ),
  321. 'form_id' => $this->form_data['id'],
  322. )
  323. );
  324. return;
  325. }
  326. // Pass the form created date into the form data.
  327. $this->form_data['created'] = $form->post_date;
  328. // Format fields.
  329. foreach ( (array) $this->form_data['fields'] as $field_properties ) {
  330. $field_id = $field_properties['id'];
  331. $field_type = $field_properties['type'];
  332. $field_submit = isset( $entry['fields'][ $field_id ] ) ? $entry['fields'][ $field_id ] : '';
  333. do_action( "wpforms_process_format_{$field_type}", $field_id, $field_submit, $this->form_data );
  334. }
  335. // This hook is for internal purposes and should not be leveraged.
  336. do_action( 'wpforms_process_format_after', $this->form_data );
  337. // Process hooks/filter - this is where most addons should hook
  338. // because at this point we have completed all field validation and
  339. // formatted the data.
  340. $this->fields = apply_filters( 'wpforms_process_filter', $this->fields, $entry, $this->form_data );
  341. do_action( 'wpforms_process', $this->fields, $entry, $this->form_data );
  342. do_action( "wpforms_process_{$form_id}", $this->fields, $entry, $this->form_data );
  343. $this->fields = apply_filters( 'wpforms_process_after_filter', $this->fields, $entry, $this->form_data );
  344. // One last error check - don't proceed if there are any errors.
  345. if ( ! empty( $this->errors[ $form_id ] ) ) {
  346. if ( empty( $this->errors[ $form_id ]['header'] ) ) {
  347. $this->errors[ $form_id ]['header'] = esc_html__( 'Form has not been submitted, please see the errors below.', 'wpforms-lite' );
  348. }
  349. return;
  350. }
  351. // Success - add entry to database.
  352. $this->entry_id = $this->entry_save( $this->fields, $entry, $this->form_data['id'], $this->form_data );
  353. // Fire the logic to send notification emails.
  354. $this->entry_email( $this->fields, $entry, $this->form_data, $this->entry_id, 'entry' );
  355. // Pass completed and formatted fields in POST.
  356. $_POST['wpforms']['complete'] = $this->fields;
  357. // Pass entry ID in POST.
  358. $_POST['wpforms']['entry_id'] = $this->entry_id;
  359. // Logs entry depending on log levels set.
  360. wpforms_log(
  361. $this->entry_id ? "Entry {$this->entry_id}" : 'Entry',
  362. $this->fields,
  363. array(
  364. 'type' => array( 'entry' ),
  365. 'parent' => $this->entry_id,
  366. 'form_id' => $this->form_data['id'],
  367. )
  368. );
  369. // Post-process hooks.
  370. do_action( 'wpforms_process_complete', $this->fields, $entry, $this->form_data, $this->entry_id );
  371. do_action( "wpforms_process_complete_{$form_id}", $this->fields, $entry, $this->form_data, $this->entry_id );
  372. $this->entry_confirmation_redirect( $this->form_data );
  373. }
  374. /**
  375. * Check if combined upload size exceeds allowed maximum.
  376. *
  377. * @since 1.6.0
  378. *
  379. * @param \WP_Post $form Form post object.
  380. */
  381. public function validate_combined_upload_size( $form ) {
  382. $form_id = (int) $form->ID;
  383. $upload_fields = wpforms_get_form_fields( $form, array( 'file-upload' ) );
  384. if ( ! empty( $upload_fields ) && ! empty( $_FILES ) ) {
  385. // Get $_FILES keys generated by WPForms only.
  386. $files_keys = preg_filter( '/^/', 'wpforms_' . $form_id . '_', array_keys( $upload_fields ) );
  387. // Filter uploads without errors. Individual errors are handled by WPForms_Field_File_Upload class.
  388. $files = wp_list_filter( wp_array_slice_assoc( $_FILES, $files_keys ), array( 'error' => 0 ) );
  389. $files_size = array_sum( wp_list_pluck( $files, 'size' ) );
  390. $files_size_max = wpforms_max_upload( true );
  391. if ( $files_size > $files_size_max ) {
  392. // Add new header error preserving previous ones.
  393. $this->errors[ $form_id ]['header'] = ! empty( $this->errors[ $form_id ]['header'] ) ? $this->errors[ $form_id ]['header'] . '<br>' : '';
  394. $this->errors[ $form_id ]['header'] .= esc_html__( 'Uploaded files combined size exceeds allowed maximum.', 'wpforms-lite' );
  395. }
  396. }
  397. }
  398. /**
  399. * Validate the form return hash.
  400. *
  401. * @since 1.0.0
  402. *
  403. * @param string $hash Base64-encoded hash of form and entry IDs.
  404. *
  405. * @return array|false False for invalid or form id.
  406. */
  407. public function validate_return_hash( $hash = '' ) {
  408. $query_args = base64_decode( $hash );
  409. parse_str( $query_args, $output );
  410. // Verify hash matches.
  411. if ( wp_hash( $output['form_id'] . ',' . $output['entry_id'] ) !== $output['hash'] ) {
  412. return false;
  413. }
  414. // Get lead and verify it is attached to the form we received with it.
  415. $entry = wpforms()->entry->get( $output['entry_id'], [ 'cap' => false ] );
  416. if ( empty( $entry->form_id ) ) {
  417. return false;
  418. }
  419. if ( $output['form_id'] !== $entry->form_id ) {
  420. return false;
  421. }
  422. return array(
  423. 'form_id' => absint( $output['form_id'] ),
  424. 'entry_id' => absint( $output['form_id'] ),
  425. 'fields' => null !== $entry && isset( $entry->fields ) ? $entry->fields : array(),
  426. );
  427. }
  428. /**
  429. * Check if the confirmation data are valid.
  430. *
  431. * @since 1.6.4
  432. *
  433. * @param array $data The confirmation data.
  434. *
  435. * @return bool
  436. */
  437. protected function is_valid_confirmation( $data ) {
  438. if ( empty( $data['type'] ) ) {
  439. return false;
  440. }
  441. // Confirmation type: redirect, page or message.
  442. $type = $data['type'];
  443. return isset( $data[ $type ] ) && ! wpforms_is_empty_string( $data[ $type ] );
  444. }
  445. /**
  446. * Redirect user to a page or URL specified in the form confirmation settings.
  447. *
  448. * @since 1.0.0
  449. *
  450. * @param array $form_data Form data and settings.
  451. * @param string $hash Base64-encoded hash of form and entry IDs.
  452. */
  453. public function entry_confirmation_redirect( $form_data = array(), $hash = '' ) {
  454. // Maybe process return hash.
  455. if ( ! empty( $hash ) ) {
  456. $hash_data = $this->validate_return_hash( $hash );
  457. if ( ! $hash_data || ! is_array( $hash_data ) ) {
  458. return;
  459. }
  460. $this->valid_hash = true;
  461. $this->entry_id = absint( $hash_data['entry_id'] );
  462. $this->fields = json_decode( $hash_data['fields'], true );
  463. $this->form_data = wpforms()->form->get(
  464. absint( $hash_data['form_id'] ),
  465. array(
  466. 'content_only' => true,
  467. )
  468. );
  469. } else {
  470. $this->form_data = $form_data;
  471. }
  472. // Backward compatibility.
  473. if ( empty( $this->form_data['settings']['confirmations'] ) ) {
  474. $this->form_data['settings']['confirmations'][1]['type'] = ! empty( $this->form_data['settings']['confirmation_type'] ) ? $this->form_data['settings']['confirmation_type'] : 'message';
  475. $this->form_data['settings']['confirmations'][1]['message'] = ! empty( $this->form_data['settings']['confirmation_message'] ) ? $this->form_data['settings']['confirmation_message'] : esc_html__( 'Thanks for contacting us! We will be in touch with you shortly.', 'wpforms-lite' );
  476. $this->form_data['settings']['confirmations'][1]['message_scroll'] = ! empty( $this->form_data['settings']['confirmation_message_scroll'] ) ? $this->form_data['settings']['confirmation_message_scroll'] : 1;
  477. $this->form_data['settings']['confirmations'][1]['page'] = ! empty( $this->form_data['settings']['confirmation_page'] ) ? $this->form_data['settings']['confirmation_page'] : '';
  478. $this->form_data['settings']['confirmations'][1]['redirect'] = ! empty( $this->form_data['settings']['confirmation_redirect'] ) ? $this->form_data['settings']['confirmation_redirect'] : '';
  479. }
  480. if ( empty( $this->form_data['settings']['confirmations'] ) || ! is_array( $this->form_data['settings']['confirmations'] ) ) {
  481. return;
  482. }
  483. $confirmations = $this->form_data['settings']['confirmations'];
  484. // Reverse sort confirmations by id to process newer ones first.
  485. krsort( $confirmations );
  486. $default_confirmation_key = min( array_keys( $confirmations ) );
  487. foreach ( $confirmations as $confirmation_id => $confirmation ) {
  488. // Last confirmation should execute in any case.
  489. if ( $default_confirmation_key === $confirmation_id ) {
  490. break;
  491. }
  492. if ( ! $this->is_valid_confirmation( $confirmation ) ) {
  493. continue;
  494. }
  495. $process_confirmation = apply_filters( 'wpforms_entry_confirmation_process', true, $this->fields, $form_data, $confirmation_id );
  496. if ( $process_confirmation ) {
  497. break;
  498. }
  499. }
  500. $url = '';
  501. // Redirect if needed, to either a page or URL, after form processing.
  502. if ( ! empty( $confirmations[ $confirmation_id ]['type'] ) && 'message' !== $confirmations[ $confirmation_id ]['type'] ) {
  503. if ( 'redirect' === $confirmations[ $confirmation_id ]['type'] ) {
  504. add_filter( 'wpforms_field_smart_tag_value', 'rawurlencode' );
  505. $url = wpforms_process_smart_tags( $confirmations[ $confirmation_id ]['redirect'], $this->form_data, $this->fields, $this->entry_id );
  506. }
  507. if ( 'page' === $confirmations[ $confirmation_id ]['type'] ) {
  508. $url = get_permalink( (int) $confirmations[ $confirmation_id ]['page'] );
  509. }
  510. }
  511. if ( ! empty( $url ) ) {
  512. $url = apply_filters( 'wpforms_process_redirect_url', $url, $this->form_data['id'], $this->fields, $this->form_data, $this->entry_id );
  513. if ( wpforms_is_amp() ) {
  514. /** This filter is documented in wp-includes/pluggable.php */
  515. $url = apply_filters( 'wp_redirect', $url, 302 );
  516. $url = wp_sanitize_redirect( $url );
  517. header( sprintf( 'AMP-Redirect-To: %s', $url ) );
  518. header( 'Access-Control-Expose-Headers: AMP-Redirect-To', false );
  519. wp_send_json(
  520. array(
  521. 'message' => __( 'Redirecting…', 'wpforms-lite' ),
  522. 'redirecting' => true,
  523. ),
  524. 200
  525. );
  526. } else {
  527. wp_redirect( esc_url_raw( $url ) ); // phpcs:ignore
  528. }
  529. do_action( 'wpforms_process_redirect', $this->form_data['id'] );
  530. do_action( "wpforms_process_redirect_{$this->form_data['id']}", $this->form_data['id'] );
  531. exit;
  532. }
  533. // Pass a message to a frontend if no redirection happened.
  534. if ( ! empty( $confirmations[ $confirmation_id ]['type'] ) && 'message' === $confirmations[ $confirmation_id ]['type'] ) {
  535. $this->confirmation = $confirmations[ $confirmation_id ];
  536. $this->confirmation_message = $confirmations[ $confirmation_id ]['message'];
  537. if ( ! empty( $confirmations[ $confirmation_id ]['message_scroll'] ) ) {
  538. wpforms()->frontend->confirmation_message_scroll = true;
  539. }
  540. }
  541. }
  542. /**
  543. * Get confirmation message.
  544. *
  545. * @since 1.5.3
  546. *
  547. * @param array $form_data Form data and settings.
  548. * @param array $fields Sanitized field data.
  549. * @param int $entry_id Entry id.
  550. *
  551. * @return string Confirmation message.
  552. */
  553. public function get_confirmation_message( $form_data, $fields, $entry_id ) {
  554. if ( empty( $this->confirmation_message ) ) {
  555. return '';
  556. }
  557. $confirmation_message = wpforms_process_smart_tags( $this->confirmation_message, $form_data, $fields, $entry_id );
  558. $confirmation_message = apply_filters( 'wpforms_frontend_confirmation_message', wpautop( $confirmation_message ), $form_data, $fields, $entry_id );
  559. return $confirmation_message;
  560. }
  561. /**
  562. * Get current confirmation.
  563. *
  564. * @since 1.6.9
  565. *
  566. * @return array
  567. */
  568. public function get_current_confirmation() {
  569. return ! empty( $this->confirmation ) ? $this->confirmation : [];
  570. }
  571. /**
  572. * Catch the post_max_size overflow.
  573. *
  574. * @since 1.5.2
  575. *
  576. * @return bool
  577. */
  578. public function post_max_size_overflow() {
  579. // phpcs:disable WordPress.Security.NonceVerification
  580. if ( empty( $_SERVER['CONTENT_LENGTH'] ) || empty( $_GET['wpforms_form_id'] ) ) {
  581. return false;
  582. }
  583. $form_id = (int) $_GET['wpforms_form_id'];
  584. $total_size = (int) $_SERVER['CONTENT_LENGTH'];
  585. $post_max_size = wpforms_size_to_bytes( ini_get( 'post_max_size' ) );
  586. if ( ! ( $total_size > $post_max_size && empty( $_POST ) && $form_id > 0 ) ) {
  587. return false;
  588. }
  589. // phpcs:enable WordPress.Security.NonceVerification
  590. $error_msg = esc_html__( 'Form has not been submitted, please see the errors below.', 'wpforms-lite' );
  591. $error_msg .= '<br>' . sprintf( /* translators: %1$.3f - the total size of the selected files in megabytes, %2$.3f - allowed file upload limit in megabytes.*/
  592. esc_html__( 'The total size of the selected files %1$.3f Mb exceeds the allowed limit %2$.3f Mb.', 'wpforms-lite' ),
  593. esc_html( $total_size / 1048576 ),
  594. esc_html( $post_max_size / 1048576 )
  595. );
  596. $this->errors[ $form_id ]['header'] = $error_msg;
  597. return true;
  598. }
  599. /**
  600. * Send entry email notifications.
  601. *
  602. * @since 1.0.0
  603. *
  604. * @param array $fields List of fields.
  605. * @param array $entry Submitted form entry.
  606. * @param array $form_data Form data and settings.
  607. * @param int $entry_id Saved entry id.
  608. * @param string $context In which context this email is sent.
  609. */
  610. public function entry_email( $fields, $entry, $form_data, $entry_id, $context = '' ) {
  611. // Check that the form was configured for email notifications.
  612. if ( empty( $form_data['settings']['notification_enable'] ) ) {
  613. return;
  614. }
  615. // Provide the opportunity to override via a filter.
  616. if ( ! apply_filters( 'wpforms_entry_email', true, $fields, $entry, $form_data ) ) {
  617. return;
  618. }
  619. // Make sure we have and entry id.
  620. if ( empty( $this->entry_id ) ) {
  621. $this->entry_id = (int) $entry_id;
  622. }
  623. $fields = apply_filters( 'wpforms_entry_email_data', $fields, $entry, $form_data );
  624. // Backwards compatibility for notifications before v1.4.3.
  625. if ( empty( $form_data['settings']['notifications'] ) ) {
  626. $notifications[1] = array(
  627. 'email' => $form_data['settings']['notification_email'],
  628. 'subject' => $form_data['settings']['notification_subject'],
  629. 'sender_name' => $form_data['settings']['notification_fromname'],
  630. 'sender_address' => $form_data['settings']['notification_fromaddress'],
  631. 'replyto' => $form_data['settings']['notification_replyto'],
  632. 'message' => '{all_fields}',
  633. );
  634. } else {
  635. $notifications = $form_data['settings']['notifications'];
  636. }
  637. foreach ( $notifications as $notification_id => $notification ) :
  638. if ( empty( $notification['email'] ) ) {
  639. continue;
  640. }
  641. $process_email = apply_filters( 'wpforms_entry_email_process', true, $fields, $form_data, $notification_id, $context );
  642. if ( ! $process_email ) {
  643. continue;
  644. }
  645. $email = array();
  646. // Setup email properties.
  647. /* translators: %s - form name. */
  648. $email['subject'] = ! empty( $notification['subject'] ) ? $notification['subject'] : sprintf( esc_html__( 'New %s Entry', 'wpforms-lite' ), $form_data['settings']['form_title'] );
  649. $email['address'] = explode( ',', wpforms_process_smart_tags( $notification['email'], $form_data, $fields, $this->entry_id ) );
  650. $email['address'] = array_map( 'sanitize_email', $email['address'] );
  651. $email['sender_address'] = ! empty( $notification['sender_address'] ) ? $notification['sender_address'] : get_option( 'admin_email' );
  652. $email['sender_name'] = ! empty( $notification['sender_name'] ) ? $notification['sender_name'] : get_bloginfo( 'name' );
  653. $email['replyto'] = ! empty( $notification['replyto'] ) ? $notification['replyto'] : false;
  654. $email['message'] = ! empty( $notification['message'] ) ? $notification['message'] : '{all_fields}';
  655. $email = apply_filters( 'wpforms_entry_email_atts', $email, $fields, $entry, $form_data, $notification_id );
  656. // Create new email.
  657. $emails = new WPForms_WP_Emails();
  658. $emails->__set( 'form_data', $form_data );
  659. $emails->__set( 'fields', $fields );
  660. $emails->__set( 'notification_id', $notification_id );
  661. $emails->__set( 'entry_id', $this->entry_id );
  662. $emails->__set( 'from_name', $email['sender_name'] );
  663. $emails->__set( 'from_address', $email['sender_address'] );
  664. $emails->__set( 'reply_to', $email['replyto'] );
  665. // Maybe include CC.
  666. if ( ! empty( $notification['carboncopy'] ) && wpforms_setting( 'email-carbon-copy', false ) ) {
  667. $emails->__set( 'cc', $notification['carboncopy'] );
  668. }
  669. $emails = apply_filters( 'wpforms_entry_email_before_send', $emails );
  670. // Go.
  671. foreach ( $email['address'] as $address ) {
  672. $emails->send( trim( $address ), $email['subject'], $email['message'] );
  673. }
  674. endforeach;
  675. }
  676. /**
  677. * Save entry to database.
  678. *
  679. * @since 1.0.0
  680. *
  681. * @param array $fields List of form fields.
  682. * @param array $entry User submitted data.
  683. * @param int $form_id Form ID.
  684. * @param array $form_data Prepared form settings.
  685. *
  686. * @return int
  687. */
  688. public function entry_save( $fields, $entry, $form_id, $form_data = array() ) {
  689. do_action( 'wpforms_process_entry_save', $fields, $entry, $form_id, $form_data );
  690. return $this->entry_id;
  691. }
  692. /**
  693. * Process AJAX form submit.
  694. *
  695. * @since 1.5.3
  696. */
  697. public function ajax_submit() {
  698. // phpcs:disable WordPress.Security.NonceVerification.Missing
  699. $form_id = isset( $_POST['wpforms']['id'] ) ? absint( $_POST['wpforms']['id'] ) : 0;
  700. if ( empty( $form_id ) ) {
  701. wp_send_json_error();
  702. }
  703. if ( isset( $_POST['wpforms']['post_id'] ) ) {
  704. // We don't have a global $post when processing ajax requests.
  705. // Therefore, it's needed to set a global $post manually for compatibility with functions used in smart tag processing.
  706. global $post;
  707. // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
  708. $post = WP_Post::get_instance( absint( $_POST['wpforms']['post_id'] ) );
  709. }
  710. // phpcs:enable WordPress.Security.NonceVerification.Missing
  711. add_filter( 'wp_redirect', [ $this, 'ajax_process_redirect' ], 999 );
  712. do_action( 'wpforms_ajax_submit_before_processing', $form_id );
  713. // If redirect happens in listen(), ajax_process_redirect() gets executed because of the filter on `wp_redirect`.
  714. // The code, that is below listen(), runs only if no redirect happened.
  715. $this->listen();
  716. $form_data = $this->form_data;
  717. if ( empty( $form_data ) ) {
  718. $form_data = wpforms()->form->get( $form_id, [ 'content_only' => true ] );
  719. $form_data = apply_filters( 'wpforms_frontend_form_data', $form_data );
  720. }
  721. if ( ! empty( $this->errors[ $form_id ] ) ) {
  722. $this->ajax_process_errors( $form_id, $form_data );
  723. wp_send_json_error();
  724. }
  725. ob_start();
  726. wpforms()->frontend->confirmation( $form_data );
  727. $response = apply_filters( 'wpforms_ajax_submit_success_response', [ 'confirmation' => ob_get_clean() ], $form_id, $form_data );
  728. do_action( 'wpforms_ajax_submit_completed', $form_id, $response );
  729. wp_send_json_success( $response );
  730. }
  731. /**
  732. * Process AJAX errors.
  733. *
  734. * @since 1.5.3
  735. * @todo This should be re-used/combined for AMP verify-xhr requests.
  736. *
  737. * @param int $form_id Form ID.
  738. * @param array $form_data Form data and settings.
  739. */
  740. protected function ajax_process_errors( $form_id, $form_data ) {
  741. $errors = isset( $this->errors[ $form_id ] ) ? $this->errors[ $form_id ] : array();
  742. $errors = apply_filters( 'wpforms_ajax_submit_errors', $errors, $form_id, $form_data );
  743. if ( empty( $errors ) ) {
  744. wp_send_json_error();
  745. }
  746. // General errors are errors that cannot be populated with jQuery Validate plugin.
  747. $general_errors = array_intersect_key( $errors, array_flip( array( 'header', 'footer', 'recaptcha' ) ) );
  748. foreach ( $general_errors as $key => $error ) {
  749. ob_start();
  750. wpforms()->frontend->form_error( $key, $error );
  751. $general_errors[ $key ] = ob_get_clean();
  752. }
  753. $fields = isset( $form_data['fields'] ) ? $form_data['fields'] : array();
  754. // Get registered fields errors only.
  755. $field_errors = array_intersect_key( $errors, $fields );
  756. // Transform field ids to field names for jQuery Validate plugin.
  757. foreach ( $field_errors as $key => $error ) {
  758. $name = $this->ajax_error_field_name( $fields[ $key ], $form_data, $error );
  759. if ( $name ) {
  760. $field_errors[ $name ] = $error;
  761. }
  762. unset( $field_errors[ $key ] );
  763. }
  764. $response = array();
  765. if ( $general_errors ) {
  766. $response['errors']['general'] = $general_errors;
  767. }
  768. if ( $field_errors ) {
  769. $response['errors']['field'] = $field_errors;
  770. }
  771. $response = apply_filters( 'wpforms_ajax_submit_errors_response', $response, $form_id, $form_data );
  772. do_action( 'wpforms_ajax_submit_completed', $form_id, $response );
  773. wp_send_json_error( $response );
  774. }
  775. /**
  776. * Get field name for ajax error message.
  777. *
  778. * @since 1.6.3
  779. *
  780. * @param array $field Field settings.
  781. * @param array $form_data Form data and settings.
  782. * @param string $error Error message.
  783. *
  784. * @return string
  785. */
  786. private function ajax_error_field_name( $field, $form_data, $error ) {
  787. $props = wpforms()->frontend->get_field_properties( $field, $form_data );
  788. return apply_filters( 'wpforms_process_ajax_error_field_name', '', $field, $props, $error );
  789. }
  790. /**
  791. * Process AJAX redirect.
  792. *
  793. * @since 1.5.3
  794. *
  795. * @param string $url Redirect URL.
  796. */
  797. public function ajax_process_redirect( $url ) {
  798. // phpcs:ignore WordPress.Security.NonceVerification.Missing
  799. $form_id = isset( $_POST['wpforms']['id'] ) ? absint( $_POST['wpforms']['id'] ) : 0;
  800. if ( empty( $form_id ) ) {
  801. wp_send_json_error();
  802. }
  803. $response = array(
  804. 'form_id' => $form_id,
  805. 'redirect_url' => $url,
  806. );
  807. $response = apply_filters( 'wpforms_ajax_submit_redirect', $response, $form_id, $url );
  808. do_action( 'wpforms_ajax_submit_completed', $form_id, $response );
  809. wp_send_json_success( $response );
  810. }
  811. }