| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ability: export form entries (Popup / Exit Intent submissions) as CSV. |
| 4 |
* |
| 5 |
* @package NotificationX\Abilities\Read |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace NotificationX\Abilities\Read; |
| 9 |
|
| 10 |
use NotificationX\Abilities\AbilityBase; |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Exports NotificationX form entries as CSV, byte-for-byte matching the admin |
| 18 |
* "Feedback Entries" export: columns No, Date, Title and Message on Free, with |
| 19 |
* Name and Email Address added when Pro is active. |
| 20 |
* |
| 21 |
* A single response is capped to keep the payload reasonable; the full match |
| 22 |
* count is always reported so a truncated export is never mistaken for a |
| 23 |
* complete one (page through with list-entries, or narrow with nx_id/search). |
| 24 |
* Entries are personal data; this ability is administrator-only. |
| 25 |
*/ |
| 26 |
class ExportEntries extends AbilityBase { |
| 27 |
|
| 28 |
use EntriesReader; |
| 29 |
|
| 30 |
/** |
| 31 |
* Maximum rows returned in one CSV export. |
| 32 |
*/ |
| 33 |
const MAX_ROWS = 1000; |
| 34 |
|
| 35 |
protected $id = 'notificationx/export-entries'; |
| 36 |
protected $label = 'Export form entries (CSV)'; |
| 37 |
protected $description = 'Export NotificationX Popup and Exit Intent form entries as CSV, matching the admin Feedback Entries export (Date, Title, Message on Free; Name and Email added on Pro). Supports nx_id, source and search filters. Note: entries contain personal data; large result sets are capped and the full count is reported.'; |
| 38 |
|
| 39 |
public function input_schema() { |
| 40 |
return array( |
| 41 |
'type' => 'object', |
| 42 |
'properties' => array( |
| 43 |
'nx_id' => array( |
| 44 |
'type' => 'integer', |
| 45 |
'description' => 'Only entries for this notification id.', |
| 46 |
), |
| 47 |
'source' => array( |
| 48 |
'type' => 'string', |
| 49 |
'description' => 'Limit to one form source.', |
| 50 |
'enum' => array( 'popup_notification', 'exit_intent_custom' ), |
| 51 |
), |
| 52 |
'search' => array( |
| 53 |
'type' => 'string', |
| 54 |
'description' => 'Match entries whose data or date contains this text.', |
| 55 |
), |
| 56 |
), |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function output_schema() { |
| 61 |
return array( |
| 62 |
'type' => 'object', |
| 63 |
'properties' => array( |
| 64 |
'filename' => array( 'type' => 'string' ), |
| 65 |
'csv_content' => array( 'type' => 'string' ), |
| 66 |
'total_entries' => array( 'type' => 'integer' ), |
| 67 |
'exported_count' => array( 'type' => 'integer' ), |
| 68 |
'truncated' => array( 'type' => 'boolean' ), |
| 69 |
'is_pro' => array( 'type' => 'boolean' ), |
| 70 |
), |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
public function execute( $input ) { |
| 75 |
list( $rows, $total ) = $this->query_entries( $input, self::MAX_ROWS, 0 ); |
| 76 |
|
| 77 |
if ( empty( $rows ) ) { |
| 78 |
return new \WP_Error( |
| 79 |
'nx_mcp_no_entries', |
| 80 |
__( 'No entries found to export.', 'notificationx' ), |
| 81 |
array( 'status' => 404 ) |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
$is_pro = $this->entries_is_pro(); |
| 86 |
|
| 87 |
// Header row — same columns and order as the admin CSV export. |
| 88 |
$headers = array( |
| 89 |
__( 'No', 'notificationx' ), |
| 90 |
__( 'Date', 'notificationx' ), |
| 91 |
__( 'NotificationX Title', 'notificationx' ), |
| 92 |
); |
| 93 |
if ( $is_pro ) { |
| 94 |
$headers[] = __( 'Name', 'notificationx' ); |
| 95 |
$headers[] = __( 'Email Address', 'notificationx' ); |
| 96 |
} |
| 97 |
$headers[] = __( 'Message', 'notificationx' ); |
| 98 |
|
| 99 |
$table = array( $headers ); |
| 100 |
$counter = 1; |
| 101 |
foreach ( $rows as $row ) { |
| 102 |
$entry = $this->map_entry_row( $row, $is_pro ); |
| 103 |
|
| 104 |
$date = $entry['created_at']; |
| 105 |
$ts = strtotime( (string) $date ); |
| 106 |
if ( $ts ) { |
| 107 |
$date = gmdate( 'F j, Y', $ts ); |
| 108 |
} |
| 109 |
|
| 110 |
$line = array( |
| 111 |
$counter++, |
| 112 |
$date, |
| 113 |
$entry['notification'], |
| 114 |
); |
| 115 |
if ( $is_pro ) { |
| 116 |
$line[] = $entry['name']; |
| 117 |
$line[] = $entry['email']; |
| 118 |
} |
| 119 |
$line[] = $entry['message']; |
| 120 |
|
| 121 |
$table[] = $line; |
| 122 |
} |
| 123 |
|
| 124 |
// Serialise to CSV — quote every field, escape embedded quotes. |
| 125 |
$csv = ''; |
| 126 |
foreach ( $table as $line ) { |
| 127 |
$csv .= '"' . implode( |
| 128 |
'","', |
| 129 |
array_map( |
| 130 |
function ( $field ) { |
| 131 |
return str_replace( '"', '""', (string) $field ); |
| 132 |
}, |
| 133 |
$line |
| 134 |
) |
| 135 |
) . '"' . "\n"; |
| 136 |
} |
| 137 |
|
| 138 |
return array( |
| 139 |
'filename' => 'notificationx-feedback-entries-' . gmdate( 'Y-m-d-H-i-s' ) . '.csv', |
| 140 |
'csv_content' => $csv, |
| 141 |
'total_entries' => $total, |
| 142 |
'exported_count' => count( $rows ), |
| 143 |
'truncated' => $total > count( $rows ), |
| 144 |
'is_pro' => $is_pro, |
| 145 |
); |
| 146 |
} |
| 147 |
} |
| 148 |
|