1 || '' !== trim( (string) ( $request['search'] ?? '' ) ) ) { return; } $rows = isset( $envelope['rows_assoc'] ) ? (array) $envelope['rows_assoc'] : ( isset( $envelope['rows'] ) ? (array) $envelope['rows'] : array() ); $total = isset( $envelope['meta']['total'] ) ? (int) $envelope['meta']['total'] : count( $rows ); if ( $total < 1 || empty( $rows ) ) { return; } $totals = array(); foreach ( (array) ( $envelope['totals'] ?? array() ) as $k => $v ) { if ( is_scalar( $v ) && '' !== (string) $v ) { $totals[ sanitize_key( (string) $k ) ] = is_numeric( $v ) ? $v + 0 : (string) $v; } if ( count( $totals ) >= self::KEEP_TOTALS ) { break; } } $id = sanitize_key( (string) $page_id ); $all = self::rows(); $all[ $id ] = array( 'at' => time(), 'report' => sanitize_key( (string) $report_id ), 'total' => $total, 'totals' => $totals, ); if ( count( $all ) > self::MAX ) { uasort( $all, static function ( $a, $b ) { return (int) ( $a['at'] ?? 0 ) <=> (int) ( $b['at'] ?? 0 ); } ); $all = array_slice( $all, count( $all ) - self::MAX, null, true ); } update_option( self::OPTION, $all, false ); } public static function forget( $page_id ) { $all = self::rows(); $id = sanitize_key( (string) $page_id ); if ( ! isset( $all[ $id ] ) ) { return; } unset( $all[ $id ] ); if ( empty( $all ) ) { delete_option( self::OPTION ); } else { update_option( self::OPTION, $all, false ); } } public static function sentence( $page_id ) { $r = self::get( $page_id ); if ( null === $r ) { return ''; } $bits = array(); $bits[] = sprintf( _n( '%s row', '%s rows', (int) $r['total'], 'ai-eshop-optimizer' ), number_format_i18n( (int) $r['total'] ) ); $labels = self::column_labels( (string) $r['report'] ); foreach ( (array) $r['totals'] as $k => $v ) { $label = isset( $labels[ $k ] ) ? $labels[ $k ] : ucfirst( str_replace( '_', ' ', (string) $k ) ); $value = is_numeric( $v ) ? number_format_i18n( (float) $v, ( floor( (float) $v ) == (float) $v ) ? 0 : 2 ) : (string) $v; $bits[] = $label . ' ' . $value; } return sprintf( __( 'On %1$s this said: %2$s', 'ai-eshop-optimizer' ), wp_date( get_option( 'date_format' ), (int) $r['at'] ), implode( ' ยท ', $bits ) ); } private static function column_labels( $report_id ) { if ( '' === $report_id || ! class_exists( 'AIEO_Report_Registry' ) || ! method_exists( 'AIEO_Report_Registry', 'get' ) ) { return array(); } $def = AIEO_Report_Registry::get( $report_id ); if ( ! $def || ! method_exists( $def, 'columns' ) ) { return array(); } $out = array(); foreach ( (array) $def->columns() as $key => $col ) { $label = is_array( $col ) && isset( $col['label'] ) ? (string) $col['label'] : ''; if ( '' !== $label ) { $out[ sanitize_key( (string) $key ) ] = __( $label, 'ai-eshop-optimizer' ); } } return $out; } }