self::ACTION ), admin_url( 'admin-post.php' ) ), self::ACTION ); } /** * The download: the capability for the record, the nonce, then the stream. * * @return void */ public static function download() { if ( ! current_user_can( Security::capability( 'records' ) ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to download the record.', 'agevow-age-verification-age-gate' ), 403 ); } check_admin_referer( self::ACTION ); $filters = array(); foreach ( array( 'from', 'to', 'result', 'method', 'source' ) as $key ) { $filters[ $key ] = isset( $_GET[ $key ] ) && is_string( $_GET[ $key ] ) ? sanitize_text_field( wp_unslash( $_GET[ $key ] ) ) : ''; } $filters = self::filters( $filters ); // admin-post.php has already sent WordPress's no-cache headers. header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="' . self::filename( $filters ) . '"' ); header( 'X-Content-Type-Options: nosniff' ); $out = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- the response stream itself, never a file on disk. if ( false === $out ) { wp_die( esc_html__( 'The download could not be started.', 'agevow-age-verification-age-gate' ), 500 ); } self::write( $out, $filters ); fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- php://output is a stream, not a file; WP_Filesystem cannot stream. exit; } /** * The filters, each held to its shape: a real day or '', a word of its list or ''. * * @param array $in from, to, result, method, source. * @return array */ public static function filters( array $in ) { $pick = function ( $key, array $allowed ) use ( $in ) { return isset( $in[ $key ] ) && is_string( $in[ $key ] ) && in_array( $in[ $key ], $allowed, true ) ? $in[ $key ] : ''; }; return array( 'from' => isset( $in['from'] ) && Dates::is_day( $in['from'] ) ? $in['from'] : '', 'to' => isset( $in['to'] ) && Dates::is_day( $in['to'] ) ? $in['to'] : '', 'result' => $pick( 'result', Checks::RESULTS ), 'method' => $pick( 'method', Checks::METHODS ), 'source' => $pick( 'source', Checks::SOURCES ), ); } /** * The file's name: agevow-checks-2026-10-01-to-2026-10-31.csv. * * @param array $filters filters(). * @return string */ public static function filename( array $filters ) { $range = '' !== $filters['from'] || '' !== $filters['to'] ? ( '' !== $filters['from'] ? $filters['from'] : 'start' ) . '-to-' . ( '' !== $filters['to'] ? $filters['to'] : Dates::today() ) : 'all-' . Dates::today(); return 'agevow-checks-' . $range . '.csv'; } /** * Write the header and every row to a stream. * * @param resource $out Stream. * @param array $filters filters(). * @return int Rows written (the header not counted). */ public static function write( $out, array $filters ) { fwrite( $out, "\xEF\xBB\xBF" ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- the byte-order mark that tells spreadsheets the file is UTF-8, written to php://output. fputcsv( $out, array_map( array( self::class, 'csv_cell' ), self::header() ), ',', '"', '\\' ); $n = 0; $after = 0; do { $rows = self::rows( $filters, $after, self::BATCH ); $got = count( $rows ); foreach ( $rows as $row ) { fputcsv( $out, array_map( array( self::class, 'csv_cell' ), self::cells( $row ) ), ',', '"', '\\' ); $after = (int) $row['id']; ++$n; } } while ( self::BATCH === $got ); return $n; } /** * The column names. * * @return string[] */ public static function header() { return array( __( 'Check', 'agevow-age-verification-age-gate' ), __( 'Order', 'agevow-age-verification-age-gate' ), __( 'Date (site time)', 'agevow-age-verification-age-gate' ), __( 'Date (UTC)', 'agevow-age-verification-age-gate' ), __( 'Where', 'agevow-age-verification-age-gate' ), __( 'How', 'agevow-age-verification-age-gate' ), __( 'Minimum age', 'agevow-age-verification-age-gate' ), __( 'Result', 'agevow-age-verification-age-gate' ), __( 'Plugin version', 'agevow-age-verification-age-gate' ), ); } /** * A batch of checks matching the filters, oldest first, after a row id. * * @param array $filters filters(). * @param int $after The last id written. * @param int $limit How many. * @return array> */ public static function rows( array $filters, $after, $limit ) { global $wpdb; list( $from, $to ) = Dates::utc_range( $filters['from'], $filters['to'] ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- this plugin's own table, read in batches by id for a download. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, order_id, checked_at, source, method, min_age, result, ref, version FROM %i WHERE id > %d AND checked_at >= %s AND checked_at <= %s AND ( %s = '' OR result = %s ) AND ( %s = '' OR method = %s ) AND ( %s = '' OR source = %s ) ORDER BY id ASC LIMIT %d", Schema::table( 'checks' ), (int) $after, $from, $to, $filters['result'], $filters['result'], $filters['method'], $filters['method'], $filters['source'], $filters['source'], max( 1, (int) $limit ) ), ARRAY_A ); return array_map( array( Checks::class, 'decode' ), (array) $rows ); } /** * One check as the CSV's cells. * * @param array $row Checks::decode(). * @return array */ public static function cells( array $row ) { return array( (string) $row['id'], null === $row['order_id'] ? '' : (string) $row['order_id'], Record::local( $row['checked_at'] ), (string) $row['checked_at'], Record::where( $row ), Record::method_word( $row['method'] ), (string) $row['min_age'], Record::result_word( $row['result'] ), (string) $row['version'], ); } /** * A cell made safe for a spreadsheet: a value starting with =, +, -, @ (or a tab or * carriage return, which some spreadsheets skip before reading one) gets a leading * apostrophe, so it is shown as text and never run as a formula. * * @param mixed $value Value. * @return string */ public static function csv_cell( $value ) { $value = is_scalar( $value ) ? (string) $value : ''; if ( '' !== $value && false !== strpos( "=+-@\t\r", $value[0] ) ) { return "'" . $value; } return $value; } }