'no_such_report' ); } $format = in_array( (string) $format, self::FORMATS, true ) ? (string) $format : 'csv'; $policy = self::policy( $def ); if ( ! self::may_export( $def, $policy ) ) { return array( 'error' => 'forbidden' ); } $columns = self::columns( $def, $policy ); if ( ! empty( $request['columns'] ) && is_array( $request['columns'] ) ) { $by_key = array_column( $columns, null, 'key' ); $picked = array(); foreach ( $request['columns'] as $key ) { if ( isset( $by_key[ (string) $key ] ) ) { $picked[] = $by_key[ (string) $key ]; } } $columns = $picked ? $picked : $columns; } unset( $request['columns'] ); if ( ! $columns ) { return array( 'error' => 'no_columns' ); } if ( ! class_exists( '\Rooster\Admin\Export' ) && function_exists( 'rooster_admin_builders' ) ) { rooster_admin_builders(); } if ( ! class_exists( '\Rooster\Admin\Export' ) ) { return array( 'error' => 'no_writer' ); } $filename = self::filename( $def, $format, $request ); if ( 'xlsx' === $format ) { return self::stream_xlsx( $def, $request, $columns, $policy, $filename ); } return self::stream_csv( $def, $request, $columns, $policy, $filename ); } public static function policy( $def ) { $raw = (array) $def->get( 'export', array() ); return array_merge( array( 'bom' => true, 'translate' => true, 'columns' => array(), 'cap' => 0, 'capability' => '', 'filename' => '', ), $raw ); } public static function may_export( $def, $policy ) { $required = (string) $policy['capability']; if ( '' === $required ) { $required = $def->capability_for( 'export' ); } if ( '' === $required ) { $required = class_exists( 'AIEO_Report_Roles' ) ? AIEO_Report_Roles::capability_for( 'export' ) : AIEO_CAPABILITY; } if ( ! current_user_can( $required ) ) { return false; } if ( 'pro' === AIEO_Report_Registry::tier( $def->id() ) && class_exists( 'AIEO_DMM_Module_Base' ) && ! AIEO_DMM_Module_Base::is_aieo_pro_user() ) { return false; } return true; } public static function columns( $def, $policy ) { $all = array(); foreach ( $def->columns() as $col ) { $all[ (string) $col['key'] ] = $col; } if ( $policy['columns'] ) { $out = array(); foreach ( (array) $policy['columns'] as $key ) { if ( isset( $all[ (string) $key ] ) ) { $out[] = $all[ (string) $key ]; } } return $out; } return array_values( array_filter( $all, static function ( $col ) { return empty( $col['hidden'] ) && 'actions' !== $col['type']; } ) ); } private static function header( $col, $policy ) { $label = (string) $col['label']; if ( '' === $label ) { return (string) $col['key']; } if ( empty( $policy['translate'] ) ) { return $label; } return __( $label, 'ai-eshop-optimizer' ); } public static function value( $col, $row ) { $raw = AIEO_Table_Columns::value( $row, (string) $col['key'], null ); if ( null === $raw ) { return ''; } if ( is_array( $raw ) ) { return implode( ', ', array_map( 'strval', $raw ) ); } switch ( (string) $col['type'] ) { case 'boolean': return ( ! empty( $raw ) && 'no' !== $raw && '0' !== (string) $raw ) ? '1' : '0'; case 'money': case 'decimal': case 'percent': case 'integer': return is_numeric( $raw ) ? (string) ( 0 + $raw ) : (string) $raw; case 'date': case 'datetime': case 'relative': return self::date_value( $raw, $col ); default: return (string) $raw; } } private static function date_value( $raw, $col ) { $s = trim( (string) $raw ); if ( '' === $s || '0000-00-00' === substr( $s, 0, 10 ) ) { return ''; } $utc = isset( $col['format']['tz'] ) && 'utc' === $col['format']['tz']; $ts = is_numeric( $s ) && (int) $s > 100000 ? (int) $s : strtotime( $utc ? $s . ' UTC' : $s ); if ( false === $ts || null === $ts ) { return $s; } return wp_date( 'Y-m-d H:i:s', $ts ); } public static function cell_type( $col ) { return in_array( (string) $col['type'], array( 'integer', 'decimal', 'money', 'percent', 'duration' ), true ) ? 'number' : 'string'; } public static function spec( $columns, $policy ) { $out = array(); foreach ( $columns as $col ) { $out[ (string) $col['key'] ] = array( 'label' => self::header( $col, $policy ), 'type' => 'number' === self::cell_type( $col ) ? 'number' : 'text', ); } return $out; } private static function stream_csv( $def, $request, $columns, $policy, $filename ) { self::lift_time_limit(); \Rooster\Admin\Export::csv( $filename, self::spec( $columns, $policy ), self::rows( $def, $request, $columns, (int) $policy['cap'] ), array( 'bom' => ! empty( $policy['bom'] ) ) ); exit; } private static function stream_xlsx( $def, $request, $columns, $policy, $filename ) { self::lift_time_limit(); \Rooster\Admin\Export::xlsx( $filename, array( self::sheet( $def, $columns, $policy, self::rows( $def, $request, $columns, (int) $policy['cap'] ) ) ) ); exit; } public static function sheet( $def, $columns, $policy, $rows ) { return array( 'name' => self::header( array( 'key' => $def->id(), 'label' => (string) $def->get( 'label', '' ) ), $policy ), 'columns' => self::spec( $columns, $policy ), 'rows' => $rows, ); } public static function rows( $def, $request, $columns, $cap ) { $cap = max( 0, (int) $cap ); $written = 0; $page = 1; do { $req = array_merge( (array) $request, array( 'page' => $page, 'per_page' => min( self::CHUNK, AIEO_Report_Query::PER_PAGE_MAX ) ) ); $envelope = ( new AIEO_Report_Query( $def, $req ) )->run(); if ( isset( $envelope['error'] ) ) { return; } $rows = isset( $envelope['rows_assoc'] ) ? (array) $envelope['rows_assoc'] : array(); if ( ! $rows ) { return; } foreach ( $rows as $row ) { if ( $cap > 0 && $written >= $cap ) { return; } $line = array(); foreach ( $columns as $col ) { $line[ (string) $col['key'] ] = self::value( $col, $row ); } yield $line; $written++; } $meta = isset( $envelope['meta'] ) ? $envelope['meta'] : array(); $pages = isset( $meta['pages'] ) ? (int) $meta['pages'] : 0; if ( $page >= $pages ) { return; } $page++; } while ( true ); } private static function lift_time_limit() { if ( function_exists( 'set_time_limit' ) && false === strpos( (string) ini_get( 'disable_functions' ), 'set_time_limit' ) ) { @set_time_limit( 0 ); } } public static function filename( $def, $format, $request = array() ) { $policy = self::policy( $def ); $base = '' !== (string) $policy['filename'] ? (string) $policy['filename'] : $def->id(); $cap = max( 0, (int) $policy['cap'] ); if ( $cap > 0 ) { $matching = ( new AIEO_Report_Query( $def, (array) $request ) )->count_filtered(); if ( $matching > $cap ) { $base .= '-first-' . $cap; } } $sig = ''; $applied = isset( $request['params'] ) && is_array( $request['params'] ) ? $request['params'] : array(); foreach ( $applied as $k => $v ) { if ( is_array( $v ) || '' === (string) $v ) { continue; } $v = (string) $v; if ( strlen( $v ) > 24 || ! preg_match( '/^[A-Za-z0-9_\-]+$/', $v ) ) { continue; } $sig .= '-' . $k . '-' . $v; } if ( strlen( $sig ) > 60 ) { $sig = ''; } return sanitize_file_name( $base . $sig . '-' . wp_date( 'Y-m-d-His' ) . '.' . $format ); } }