prefix . 'ninetytwo_notify_subscribers'; nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=92-notify-subscribers-' . gmdate( 'Y-m-d' ) . '.csv' ); // php://output is a stream, not a file on disk, so WP_Filesystem (which only // handles real files) doesn't apply here; direct fopen/fputcsv/fclose is the // standard way to stream a CSV download in a WordPress plugin. // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen $output = fopen( 'php://output', 'w' ); fputcsv( $output, array( 'ID', 'Email', 'Product Name', 'Product URL', 'Status', 'Subscribed At', 'Last Notified' ) ); $per_page = 500; $page = 0; $row_count = 0; do { $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT id, email, product_name, product_url, status, created_at, notified_at FROM %i ORDER BY id LIMIT %d OFFSET %d', $table_name, $per_page, $page * $per_page ), ARRAY_A ); $row_count = count( $rows ); foreach ( $rows as $row ) { fputcsv( $output, array( $row['id'], ninetytwo_notify_csv_safe_value( $row['email'] ), ninetytwo_notify_csv_safe_value( $row['product_name'] ), ninetytwo_notify_csv_safe_value( $row['product_url'] ), $row['status'] ? 'Active' : 'Unsubscribed', $row['created_at'], $row['notified_at'] ? $row['notified_at'] : '', ) ); } ++$page; } while ( $row_count === $per_page ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose fclose( $output ); exit; } add_action( 'admin_post_ninetytwo_notify_export_subscribers', 'ninetytwo_notify_export_subscribers_csv' );