save-sub.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. function nf_save_sub(){
  3. global $ninja_forms_processing, $ninja_forms_fields;
  4. // save forms by default
  5. $save = true;
  6. // check if there's some legacy save settings saved in the database
  7. if ( 0 === $ninja_forms_processing->get_form_setting('save_subs') ) {
  8. $save = false;
  9. }
  10. $save = apply_filters ( 'ninja_forms_save_submission', $save, $ninja_forms_processing->get_form_ID() );
  11. if( $save ){
  12. $action = $ninja_forms_processing->get_action();
  13. $user_id = $ninja_forms_processing->get_user_ID();
  14. $sub_id = $ninja_forms_processing->get_form_setting( 'sub_id' );
  15. $form_id = $ninja_forms_processing->get_form_ID();
  16. $field_data = $ninja_forms_processing->get_all_fields();
  17. // If we don't have a submission ID already, create a submission post.
  18. if ( empty( $sub_id ) ) {
  19. $sub_id = Ninja_Forms()->subs()->create( $form_id );
  20. Ninja_Forms()->sub( $sub_id )->update_user_id( $user_id );
  21. do_action( 'nf_create_sub', $sub_id );
  22. // Update our legacy $ninja_forms_processing with the new sub_id
  23. $ninja_forms_processing->update_form_setting( 'sub_id', $sub_id );
  24. }
  25. do_action( 'nf_before_save_sub', $sub_id );
  26. Ninja_Forms()->sub( $sub_id )->update_action( $action );
  27. if ( is_array ( $field_data ) && ! empty ( $field_data ) ) {
  28. // Loop through our submitted data and add the values found there.
  29. // Maintain backwards compatibility with older extensions that use the ninja_forms_save_sub_args filter.
  30. $data = array();
  31. //
  32. foreach ( $field_data as $field_id => $user_value ) {
  33. $field_row = $ninja_forms_processing->get_field_settings( $field_id );
  34. $field_type = $field_row['type'];
  35. if ( isset ( $ninja_forms_fields[$field_type]['save_sub'] ) ) {
  36. $save_sub = $ninja_forms_fields[$field_type]['save_sub'];
  37. if( $save_sub ){
  38. $user_value = apply_filters( 'nf_save_sub_user_value', $user_value, $field_id );
  39. if( is_array( $user_value ) ){
  40. $user_value = ninja_forms_esc_html_deep( $user_value );
  41. }else{
  42. $user_value = esc_html( $user_value );
  43. }
  44. // Add our submitted field value.
  45. Ninja_Forms()->sub( $sub_id )->add_field( $field_id, $user_value );
  46. // Maintain backwards compatibility with older extensions that use the ninja_forms_save_sub_args filter.
  47. $data[] = array( 'field_id' => $field_id, 'user_value' => $user_value );
  48. //
  49. }
  50. }
  51. }
  52. }
  53. // Maintain backwards compatibility with older extensions that still use the ninja_forms_save_sub_args filter.
  54. $args = apply_filters( 'ninja_forms_save_sub_args', array(
  55. 'sub_id' => $sub_id,
  56. 'form_id' => $form_id,
  57. 'data' => serialize( $data ),
  58. ) );
  59. ninja_forms_update_sub( $args );
  60. //
  61. do_action( 'nf_save_sub', $sub_id );
  62. }
  63. }
  64. add_action('ninja_forms_post_process', 'nf_save_sub');