Save.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Class NF_Action_Save
  4. */
  5. final class NF_Actions_Save extends NF_Abstracts_Action
  6. {
  7. /**
  8. * @var string
  9. */
  10. protected $_name = 'save';
  11. /**
  12. * @var array
  13. */
  14. protected $_tags = array();
  15. /**
  16. * @var string
  17. */
  18. protected $_timing = 'late';
  19. /**
  20. * @var int
  21. */
  22. protected $_priority = '-1';
  23. /**
  24. * Constructor
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. $this->_nicename = __( 'Store Submission', 'ninja-forms' );
  30. $settings = Ninja_Forms::config( 'ActionSaveSettings' );
  31. $this->_settings = array_merge( $this->_settings, $settings );
  32. }
  33. /*
  34. * PUBLIC METHODS
  35. */
  36. public function save( $action_settings )
  37. {
  38. // Get the form data from the Post variable and send it off for processing.
  39. $form = json_decode( stripslashes( $_POST[ 'form' ] ) );
  40. $this->submission_expiration_processing( $action_settings, $form->id );
  41. }
  42. /**
  43. * Submission Expiration Processing
  44. * Decides if the submission expiration data should be added to the
  45. * submission expiration option or not.
  46. *
  47. * @param $action_settings - array.
  48. * @param $form_id - ( int ) The ID of the Form.
  49. *
  50. * @return void
  51. */
  52. public function submission_expiration_processing( $action_settings, $form_id )
  53. {
  54. /*
  55. * Comma separated value of the form id and action setting.
  56. * Example: 5,90
  57. */
  58. $expiration_value = $form_id . ',' . $action_settings[ 'subs_expire_time' ];
  59. // Check for option value...
  60. $option = get_option( 'nf_sub_expiration', array() );
  61. // If our expiration setting is turned on...
  62. if( 1 == $action_settings[ 'set_subs_to_expire' ] ) {
  63. // Send our data to the compare method to be added to the expiration option
  64. $this->compare_expiration_option( $expiration_value, $option );
  65. } else {
  66. // Otherwise send the data to be removed from the expiration option.
  67. $this->remove_expiration_option( $expiration_value, $option );
  68. }
  69. }
  70. /**
  71. * Compare Expiration Option
  72. * Accepts $expiration_data and checks to see if the values already exist in the array.
  73. * @since 3.3.2
  74. *
  75. * @param array $expiration_value - key/value pair
  76. * $expiration_value[ 'form_id' ] = form_id(int)
  77. * $expiration_value[ 'expire_time' ] = subs_expire_time(int)
  78. * @param array $expiration_option - list of key/value pairs of the expiration options.
  79. *
  80. * @return void
  81. */
  82. public function compare_expiration_option( $expiration_value, $expiration_option )
  83. {
  84. /*
  85. * Breaks a part our options.
  86. * $value[ 0 ] - ( int ) Form ID
  87. * $value[ 1 ] - ( int ) Expiration time in days
  88. */
  89. $values = explode( ',', $expiration_value );
  90. // Find the position of the value we are tyring to update.
  91. $array_position = array_search( ( int ) $values[ 0 ], $expiration_option );
  92. /*
  93. * TODO: Refactor this to only run when needed.
  94. * Remove this value from the array.
  95. */
  96. if( isset( $array_position ) ) {
  97. unset( $expiration_option[ $array_position ] );
  98. }
  99. // Check for our value in the options and then add it if it doesn't exist.
  100. if( ! in_array( $expiration_value, $expiration_option ) ) {
  101. $expiration_option[] = $expiration_value;
  102. }
  103. // Update our option.
  104. update_option( 'nf_sub_expiration', $expiration_option );
  105. }
  106. /**
  107. * Remove Expiration Option
  108. * If the expiration action setting is turned off this helper method
  109. * removes the form id and expiration time from the option.
  110. *
  111. * @param array $expiration_value - key/value pair
  112. * $expiration_value[ 'form_id' ] = form_id(int)
  113. * $expiration_value[ 'expire_time' ] = subs_expire_time(int)
  114. * @param array $expiration_option - list of key/value pairs of the expiration options.
  115. *
  116. * @return void
  117. */
  118. public function remove_expiration_option( $expiration_value, $expiration_option )
  119. {
  120. $values = explode( ',', $expiration_value );
  121. // Find the position of the value we are tyring to update.
  122. $array_position = array_search( ( int ) $values[ 0 ], $expiration_option );
  123. /*
  124. * TODO: Refactor this to only run when needed.
  125. * Remove this value from the array.
  126. */
  127. if( isset( $array_position ) ) {
  128. unset( $expiration_option[ $array_position ] );
  129. }
  130. // Update our option.
  131. update_option( 'nf_sub_expiration', $expiration_option );
  132. }
  133. public function process( $action_settings, $form_id, $data )
  134. {
  135. if( isset( $data['settings']['is_preview'] ) && $data['settings']['is_preview'] ){
  136. return $data;
  137. }
  138. if( ! apply_filters ( 'ninja_forms_save_submission', true, $form_id ) ) return $data;
  139. $sub = Ninja_Forms()->form( $form_id )->sub()->get();
  140. $hidden_field_types = apply_filters( 'nf_sub_hidden_field_types', array() );
  141. // For each field on the form...
  142. foreach( $data['fields'] as $field ){
  143. // If this is a "hidden" field type.
  144. if( in_array( $field[ 'type' ], array_values( $hidden_field_types ) ) ) {
  145. // Do not save it.
  146. $data[ 'actions' ][ 'save' ][ 'hidden' ][] = $field[ 'type' ];
  147. continue;
  148. }
  149. $field[ 'value' ] = apply_filters( 'nf_save_sub_user_value', $field[ 'value' ], $field[ 'id' ] );
  150. $save_all_none = $action_settings[ 'fields-save-toggle' ];
  151. $save_field = true;
  152. // If we were told to save all fields...
  153. if( 'save_all' == $save_all_none ) {
  154. $save_field = true;
  155. // For each exception to that rule...
  156. foreach( $action_settings[ 'exception_fields' ] as $exception_field ) {
  157. // Remove it from the list.
  158. if( $field[ 'key' ] == $exception_field[ 'field'] ) {
  159. $save_field = false;
  160. break;
  161. }
  162. }
  163. } // Otherwise... (We were told to save no fields.)
  164. else if( 'save_none' == $save_all_none ) {
  165. $save_field = false;
  166. // For each exception to that rule...
  167. foreach( $action_settings[ 'exception_fields' ] as
  168. $exception_field ) {
  169. // Add it to the list.
  170. if( $field[ 'key' ] == $exception_field[ 'field'] ) {
  171. $save_field = true;
  172. break;
  173. }
  174. }
  175. }
  176. // If we're supposed to save this field...
  177. if( $save_field ) {
  178. // Do so.
  179. $sub->update_field_value( $field[ 'id' ], $field[ 'value' ] );
  180. } // Otherwise...
  181. else {
  182. // If this field is not a list...
  183. // AND If this field is not a checkbox...
  184. // AND If this field is not a product...
  185. // AND If this field is not a termslist...
  186. if ( false == strpos( $field[ 'type' ], 'list' ) &&
  187. false == strpos( $field[ 'type' ], 'checkbox' ) &&
  188. 'products' !== $field[ 'type' ] &&
  189. 'terms' !== $field[ 'type' ] ) {
  190. // Anonymize it.
  191. $sub->update_field_value( $field[ 'id' ], '(redacted)' );
  192. }
  193. }
  194. }
  195. // If we have extra data...
  196. if( isset( $data[ 'extra' ] ) ) {
  197. // Save that.
  198. $sub->update_extra_values( $data[ 'extra' ] );
  199. }
  200. do_action( 'nf_before_save_sub', $sub->get_id() );
  201. $sub->save();
  202. do_action( 'nf_save_sub', $sub->get_id() );
  203. do_action( 'nf_create_sub', $sub->get_id() );
  204. do_action( 'ninja_forms_save_sub', $sub->get_id() );
  205. $data[ 'actions' ][ 'save' ][ 'sub_id' ] = $sub->get_id();
  206. return $data;
  207. }
  208. }