'object', 'properties' => array( 'nx_id' => array( 'type' => 'integer', 'description' => 'Only entries for this notification id.', ), 'source' => array( 'type' => 'string', 'description' => 'Limit to one form source.', 'enum' => array( 'popup_notification', 'exit_intent_custom' ), ), 'search' => array( 'type' => 'string', 'description' => 'Match entries whose data or date contains this text.', ), ), ); } public function output_schema() { return array( 'type' => 'object', 'properties' => array( 'filename' => array( 'type' => 'string' ), 'csv_content' => array( 'type' => 'string' ), 'total_entries' => array( 'type' => 'integer' ), 'exported_count' => array( 'type' => 'integer' ), 'truncated' => array( 'type' => 'boolean' ), 'is_pro' => array( 'type' => 'boolean' ), ), ); } public function execute( $input ) { list( $rows, $total ) = $this->query_entries( $input, self::MAX_ROWS, 0 ); if ( empty( $rows ) ) { return new \WP_Error( 'nx_mcp_no_entries', __( 'No entries found to export.', 'notificationx' ), array( 'status' => 404 ) ); } $is_pro = $this->entries_is_pro(); // Header row — same columns and order as the admin CSV export. $headers = array( __( 'No', 'notificationx' ), __( 'Date', 'notificationx' ), __( 'NotificationX Title', 'notificationx' ), ); if ( $is_pro ) { $headers[] = __( 'Name', 'notificationx' ); $headers[] = __( 'Email Address', 'notificationx' ); } $headers[] = __( 'Message', 'notificationx' ); $table = array( $headers ); $counter = 1; foreach ( $rows as $row ) { $entry = $this->map_entry_row( $row, $is_pro ); $date = $entry['created_at']; $ts = strtotime( (string) $date ); if ( $ts ) { $date = gmdate( 'F j, Y', $ts ); } $line = array( $counter++, $date, $entry['notification'], ); if ( $is_pro ) { $line[] = $entry['name']; $line[] = $entry['email']; } $line[] = $entry['message']; $table[] = $line; } // Serialise to CSV — quote every field, escape embedded quotes. $csv = ''; foreach ( $table as $line ) { $csv .= '"' . implode( '","', array_map( function ( $field ) { return str_replace( '"', '""', (string) $field ); }, $line ) ) . '"' . "\n"; } return array( 'filename' => 'notificationx-feedback-entries-' . gmdate( 'Y-m-d-H-i-s' ) . '.csv', 'csv_content' => $csv, 'total_entries' => $total, 'exported_count' => count( $rows ), 'truncated' => $total > count( $rows ), 'is_pro' => $is_pro, ); } }