NF_Subs

Submissions.

Defined (1)

The class is defined in the following location(s).

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