Automattic\WooCommerce\Internal\PushNotifications\DataStores
StepLogTableReader::read
Returns every row written under a source between two times, as far back as the bounded scan reached.
Rows are turned into what the caller wants as each one is read, and the scan stops once it holds enough, so a short page does not walk the whole row budget. has_more says whether it stopped with older windows unread.
Method of the class: StepLogTableReader{}
No Hooks.
Returns
array{rows:. array<int, array{timestamp: int, level: string, message: string, context: array|null, raw: string|null}>, covered_from: int, has_more: bool}
Usage
$StepLogTableReader = new StepLogTableReader(); $StepLogTableReader->read( $source, $from, $to, $limit, ?callable $expand ): array;
- $source(string) (required)
- The log source, e.g.
push-notifications-store-order. - $from(int) (required)
- Earliest timestamp to include, inclusive.
- $to(int) (required)
- Latest timestamp to include, inclusive.
- $limit(int)
- The fewest rows to collect before stopping.
Default:PHP_INT_MAX - ?callable $expand
- .
Default:null
Changelog
| Since 11.3.0 | Introduced. |
StepLogTableReader::read() StepLogTableReader::read code WC 11.3.0
public function read( string $source, int $from, int $to, int $limit = PHP_INT_MAX, ?callable $expand = null ): array {
global $wpdb;
$table = $wpdb->prefix . 'woocommerce_log';
$max_id = (int) $wpdb->get_var( "SELECT MAX(log_id) FROM {$table}" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$rows = array();
$upper = $max_id;
$examined = 0;
$covered_from = $from;
$has_more = false;
while ( $upper > 0 && $examined < self::MAX_ROWS_EXAMINED ) {
$lower = max( 0, $upper - self::WINDOW_SIZE );
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT log_id, timestamp, level, message, context FROM {$table} WHERE log_id > %d AND log_id <= %d AND source = %s ORDER BY log_id ASC", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$lower,
$upper,
$source
),
ARRAY_A
);
foreach ( (array) $results as $result ) {
$row = self::build_row( $result );
if ( $row['timestamp'] < $from || $row['timestamp'] > $to ) {
continue;
}
if ( null === $expand ) {
$rows[] = $row;
continue;
}
foreach ( $expand( $row ) as $expanded ) {
$rows[] = $expanded;
}
}
$examined += $upper - $lower;
$oldest = $this->get_window_start_timestamp( $table, $lower );
$upper = $lower;
/*
* Windows are walked newest first, so everything left is older than
* this window's oldest row. That row's second can carry on into the
* next window, so stop only once a full page is newer than it.
*/
if ( count( array_filter( $rows, fn( array $row ) => null === $oldest || $row['timestamp'] > $oldest ) ) >= $limit ) {
$covered_from = null === $oldest ? $from : $oldest;
$has_more = $upper > 0 && $covered_from > $from;
break;
}
if ( null === $oldest ) {
continue;
}
if ( $oldest <= $from ) {
$covered_from = $from;
break;
}
$covered_from = $oldest;
}
if ( 0 === $upper ) {
$covered_from = $from;
}
usort( $rows, fn( array $a, array $b ) => $a['timestamp'] <=> $b['timestamp'] );
return array(
'rows' => $rows,
'covered_from' => $covered_from,
'has_more' => $has_more,
);
}