subs.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. /**
  3. * Submissions.
  4. * This class handles creating and exporting submissions.
  5. *
  6. * @package Ninja Forms
  7. * @subpackage Classes/Submissions
  8. * @copyright Copyright (c) 2014, WPNINJAS
  9. * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
  10. * @since 2.7
  11. */
  12. class NF_Subs {
  13. /**
  14. * Get things started
  15. *
  16. * @access public
  17. * @since 2.7
  18. * @return void/
  19. */
  20. public function __construct() {
  21. }
  22. /**
  23. * Create a submission.
  24. *
  25. * @access public
  26. * @since 2.7
  27. * @return int $sub_id
  28. */
  29. public function create( $form_id = '' ) {
  30. // Create Submission
  31. $post = array(
  32. 'post_status' => 'publish',
  33. 'post_type' => 'nf_sub'
  34. );
  35. $sub_id = wp_insert_post( $post );
  36. // Add our form ID to the submission
  37. Ninja_Forms()->sub( $sub_id )->update_form_id( $form_id );
  38. // Get the current sequential ID
  39. $last_sub = Ninja_Forms()->form( $form_id )->get_setting( 'last_sub', true );
  40. $seq_num = ! empty ( $last_sub ) ? $last_sub + 1 : 1;
  41. $seq_num = apply_filters( 'nf_sub_seq_num', $seq_num, $form_id );
  42. // Add the sequential ID to the post meta
  43. Ninja_Forms()->sub( $sub_id )->update_seq_num( $seq_num );
  44. // Update our form data with the new "last seq id."
  45. Ninja_Forms()->form( $form_id )->update_setting( 'last_sub', $seq_num );
  46. // Update our sub count
  47. Ninja_Forms()->form( $form_id )->sub_count = $seq_num - 1;
  48. return $sub_id;
  49. }
  50. /**
  51. * Get submissions based on specific critera.
  52. *
  53. * @since 2.7
  54. * @param array $args
  55. * @return array $sub_ids
  56. */
  57. public function get( $args = array() ) {
  58. $query_args = array(
  59. 'post_type' => 'nf_sub',
  60. 'posts_per_page' => -1,
  61. 'date_query' => array(
  62. 'inclusive' => true,
  63. ),
  64. );
  65. if( isset( $args['form_id'] ) ) {
  66. $query_args['meta_query'][] = array(
  67. 'key' => '_form_id',
  68. 'value' => $args['form_id'],
  69. );
  70. }
  71. if( isset( $args['seq_num'] ) ) {
  72. $query_args['meta_query'][] = array(
  73. 'key' => '_seq_num',
  74. 'value' => $args['seq_num'],
  75. );
  76. }
  77. if( isset( $args['user_id'] ) ) {
  78. $query_args['author'] = $args['user_id'];
  79. }
  80. if( isset( $args['action'])){
  81. $query_args['meta_query'][] = array(
  82. 'key' => '_action',
  83. 'value' => $args['action'],
  84. );
  85. }
  86. if ( isset ( $args['meta'] ) ) {
  87. foreach ( $args['meta'] as $key => $value ) {
  88. $query_args['meta_query'][] = array(
  89. 'key' => $key,
  90. 'value' => $value,
  91. );
  92. }
  93. }
  94. if ( isset ( $args['fields'] ) ) {
  95. foreach ( $args['fields'] as $field_id => $value ) {
  96. $query_args['meta_query'][] = array(
  97. 'key' => '_field_' . $field_id,
  98. 'value' => $value,
  99. );
  100. }
  101. }
  102. if( isset( $args['begin_date'] ) AND $args['begin_date'] != '') {
  103. $query_args['date_query']['after'] = nf_get_begin_date( $args['begin_date'] )->format("Y-m-d G:i:s");
  104. }
  105. if( isset( $args['end_date'] ) AND $args['end_date'] != '' ) {
  106. $query_args['date_query']['before'] = nf_get_end_date( $args['end_date'] )->format("Y-m-d G:i:s");
  107. }
  108. $subs = new WP_Query( $query_args );;
  109. $sub_objects = array();
  110. if ( is_array( $subs->posts ) && ! empty( $subs->posts ) ) {
  111. foreach ( $subs->posts as $sub ) {
  112. $sub_objects[] = Ninja_Forms()->sub( $sub->ID );
  113. }
  114. }
  115. wp_reset_postdata();
  116. return $sub_objects;
  117. }
  118. /**
  119. * Export submissions.
  120. *
  121. * @access public
  122. * @param array $sub_ids
  123. * @param bool @return
  124. * @since 2.7
  125. * @return void
  126. */
  127. public function export( $sub_ids = '', $return = false ){
  128. global $ninja_forms_fields;
  129. // Bail if we haven't been sent any IDs.
  130. if ( empty( $sub_ids ) )
  131. return false;
  132. if ( ! is_array( $sub_ids ) )
  133. $sub_ids = array( $sub_ids );
  134. $plugin_settings = nf_get_settings();
  135. $date_format = $plugin_settings['date_format'];
  136. $label_array = array();
  137. // Get our Form ID.
  138. $form_id = Ninja_Forms()->sub( $sub_ids[0] )->form_id;
  139. // Get our list of fields.
  140. $fields = Ninja_Forms()->form( $form_id )->fields;
  141. // Add our sequential number.
  142. $label_array[0]['_seq_num'] = __( '#', 'ninja-forms' );
  143. // Add our "Date" label.
  144. $label_array[0]['_date_submitted'] = __( 'Date Submitted', 'ninja-forms' );
  145. $label_array = apply_filters( 'nf_subs_csv_label_array_before_fields', $label_array, $sub_ids );
  146. foreach ( $fields as $field_id => $field ) {
  147. // Get our field type
  148. $field_type = $field['type'];
  149. // Check to see if our field type has been set as a "process_field".
  150. if ( isset ( $ninja_forms_fields[ $field_type ] ) ) {
  151. $reg_field = $ninja_forms_fields[ $field_type ];
  152. $process_field = $reg_field['process_field'];
  153. } else {
  154. $process_field = false;
  155. }
  156. // If this field's "process_field" is set to true, then add its label to the array.
  157. if ( $process_field ) {
  158. if ( isset ( $field['data']['admin_label'] ) && $field['data']['admin_label'] != '' ) {
  159. $label = $field['data']['admin_label'];
  160. } else if( isset ( $field['data']['label'] ) ) {
  161. $label = $field['data']['label'];
  162. }else{
  163. $label = '';
  164. }
  165. $label_array[0][ $field_id ] = apply_filters( 'nf_subs_csv_field_label', $label, $field_id );
  166. }
  167. }
  168. $label_array = ninja_forms_stripslashes_deep( $label_array );
  169. $label_array = apply_filters( 'nf_subs_csv_label_array', $label_array, $sub_ids );
  170. $value_array = array();
  171. $x = 0;
  172. // Loop through our submissions and create a new row for each one.
  173. foreach ( $sub_ids as $sub_id ) {
  174. foreach ( $label_array[0] as $field_id => $label ) {
  175. // Make sure we aren't working with our date field, which will always have a field id of 0.
  176. if ( $field_id !== 0 ) {
  177. // Check to see if our field_id is numeric. If it isn't, then we're working with meta, not a field.
  178. if ( is_numeric( $field_id ) ) {
  179. // We're working with a field, grab the value.
  180. $user_value = Ninja_Forms()->sub( $sub_id )->get_field( $field_id );
  181. } else if ( '_date_submitted' == $field_id ) {
  182. // Get the date of our submission.
  183. $date = strtotime( Ninja_Forms()->sub( $sub_id )->date_submitted );
  184. // The first item is our date field.
  185. $user_value = date( $date_format, $date );
  186. } else if ( '_seq_num' == $field_id ) {
  187. $user_value = Ninja_Forms()->sub( $sub_id )->get_seq_num();
  188. } else {
  189. // We're working with a piece of meta, grabe the value.
  190. $user_value = Ninja_Forms()->sub( $sub_id )->get_meta( $field_id );
  191. }
  192. // Run our value through the appropriate filters before we flatten any arrays.
  193. $user_value = apply_filters( 'nf_subs_export_pre_value', $user_value, $field_id );
  194. // Implode any arrays we might have.
  195. if ( is_array( $user_value ) ) {
  196. $user_value = implode( ',', $user_value );
  197. }
  198. // Add an ' to the beginning = sign to prevent any CSV/Excel security issues.
  199. if ( strpos( $user_value, '=' ) === 0 ) {
  200. $user_value = "'" . $user_value;
  201. }
  202. // Run our final value through the appropriate filters and assign it to the array.
  203. $value_array[ $x ][ $field_id ] = htmlspecialchars_decode( apply_filters( 'nf_subs_csv_field_value', $user_value, $field_id ), ENT_QUOTES );
  204. }
  205. }
  206. $x++;
  207. }
  208. $value_array = ninja_forms_stripslashes_deep( $value_array );
  209. $value_array = apply_filters( 'nf_subs_csv_value_array', $value_array, $sub_ids );
  210. $array = array( $label_array, $value_array );
  211. $today = date( $date_format, current_time( 'timestamp' ) );
  212. $filename = apply_filters( 'nf_subs_csv_filename', 'nf_subs_' . $today );
  213. $filename = $filename . ".csv";
  214. if( $return ){
  215. return str_putcsv( $array,
  216. apply_filters( 'nf_sub_csv_delimiter', ',' ),
  217. apply_filters( 'nf_sub_csv_enclosure', '"' ),
  218. apply_filters( 'nf_sub_csv_terminator', "\n" )
  219. );
  220. }else{
  221. header( 'Content-type: application/csv');
  222. header( 'Content-Disposition: attachment; filename="'.$filename .'"' );
  223. header( 'Pragma: no-cache');
  224. header( 'Expires: 0' );
  225. echo apply_filters( 'nf_sub_csv_bom',"\xEF\xBB\xBF" ) ; // Byte Order Mark
  226. echo str_putcsv( $array,
  227. apply_filters( 'nf_sub_csv_delimiter', ',' ),
  228. apply_filters( 'nf_sub_csv_enclosure', '"' ),
  229. apply_filters( 'nf_sub_csv_terminator', "\n" )
  230. );
  231. die();
  232. }
  233. }
  234. }