download-all-subs.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. class NF_Download_All_Subs extends NF_Step_Processing {
  3. function __construct() {
  4. $this->action = 'download_all_subs';
  5. parent::__construct();
  6. }
  7. public function loading() {
  8. $form_id = isset( $this->args['form_id'] ) ? absint( $this->args['form_id'] ) : 0;
  9. if ( empty( $form_id ) ) {
  10. return array( 'complete' => true );
  11. }
  12. $sub_count = nf_get_sub_count( $form_id );
  13. if( empty( $this->total_steps ) || $this->total_steps <= 1 ) {
  14. $this->total_steps = round( ( $sub_count / 250 ), 0 ) + 2;
  15. }
  16. $args = array(
  17. 'total_steps' => $this->total_steps,
  18. );
  19. $this->args['filename'] = $this->random_filename( 'all-subs' );
  20. update_user_option( get_current_user_id(), 'nf_download_all_subs_filename', $this->args['filename'] );
  21. $this->redirect = esc_url_raw( add_query_arg( array( 'download_all' => $this->args['filename'] ), $this->args['redirect'] ) );
  22. $this->loaded = true;
  23. return $args;
  24. }
  25. public function step() {
  26. if( ! is_numeric( $this->args[ 'form_id' ] ) ){
  27. wp_die( __( 'Invalid form id', 'ninja-forms' ) );
  28. }
  29. $this->args[ 'filename' ] = wp_kses_post( $this->args[ 'filename' ] );
  30. $exported_subs = get_user_option( get_current_user_id(), 'nf_download_all_subs_ids' );
  31. if ( ! is_array( $exported_subs ) ) {
  32. $exported_subs = array();
  33. }
  34. $previous_name = get_user_option( get_current_user_id(), 'nf_download_all_subs_filename' );
  35. if ( $previous_name ) {
  36. $this->args['filename'] = $previous_name;
  37. }
  38. $args = array(
  39. 'posts_per_page' => 250,
  40. 'paged' => $this->step,
  41. 'post_type' => 'nf_sub',
  42. 'meta_query' => array(
  43. array(
  44. 'key' => '_form_id',
  45. 'value' => $this->args['form_id'],
  46. ),
  47. ),
  48. );
  49. $subs_results = get_posts( $args );
  50. if ( is_array( $subs_results ) && ! empty( $subs_results ) ) {
  51. $upload_dir = wp_upload_dir();
  52. $file_path = trailingslashit( $upload_dir['path'] ) . $this->args['filename'] . '.csv';
  53. $myfile = fopen( $file_path, 'a' ) or die( 'Unable to open file!' );
  54. $x = 0;
  55. $export = '';
  56. foreach ( $subs_results as $sub ) {
  57. $sub_export = Ninja_Forms()->sub( $sub->ID )->export( true );
  58. if ( $x > 0 || $this->step > 1 ) {
  59. $sub_export = substr( $sub_export, strpos( $sub_export, "\n" ) + 1 );
  60. }
  61. if ( ! in_array( $sub->ID, $exported_subs ) ) {
  62. $export .= $sub_export;
  63. $exported_subs[] = $sub->ID;
  64. }
  65. $x++;
  66. }
  67. fwrite( $myfile, $export );
  68. fclose( $myfile );
  69. }
  70. update_user_option( get_current_user_id(), 'nf_download_all_subs_ids', $exported_subs );
  71. }
  72. public function complete() {
  73. delete_user_option( get_current_user_id(), 'nf_download_all_subs_ids' );
  74. delete_user_option( get_current_user_id(), 'nf_download_all_subs_filename' );
  75. }
  76. /**
  77. * Add an integar to the end of our filename to make sure it is unique
  78. *
  79. * @access public
  80. * @since 2.7.6
  81. * @return $filename
  82. */
  83. public function random_filename( $filename ) {
  84. $upload_dir = wp_upload_dir();
  85. $file_path = trailingslashit( $upload_dir['path'] ) . $filename . '.csv';
  86. if ( file_exists ( $file_path ) ) {
  87. for ($x = 0; $x < 999 ; $x++) {
  88. $tmp_name = $filename . '-' . $x;
  89. $tmp_path = trailingslashit( $upload_dir['path'] );
  90. if ( file_exists( $tmp_path . $tmp_name . '.csv' ) ) {
  91. $this->random_filename( $tmp_name );
  92. break;
  93. } else {
  94. $this->filename = $tmp_name;
  95. break;
  96. }
  97. }
  98. }
  99. return $filename;
  100. }
  101. }