> $rows Log rows, oldest first. * @param int $now Unix time. * @param int $days Window length in days. * @param int|null $chain Deeper reductions: null keeps the first start, 0 starts again at each one, a number of days keeps the first start only until that many days after it. * @return array{lowest: float|null, start: int|null, start_known: bool, complete: bool} */ public static function compute( array $rows, $now, $days = 30, $chain = null ) { $result = array( 'lowest' => null, 'start' => null, 'start_known' => false, 'complete' => false, ); $rows = array_values( $rows ); $last = count( $rows ) - 1; if ( $last < 0 || null !== $rows[ $last ]['to'] ) { return $result; } $window = max( 1, (int) $days ) * DAY_IN_SECONDS; if ( ! self::is_discounted( $rows[ $last ] ) ) { // The log has not seen the discount yet (for example a sale whose // start WooCommerce has not applied): compare with the last days. $start = (int) $now; $first = $last + 1; } else { // The reduction in force: the rows of the same price before it. $step = $last; while ( $step > 0 && self::is_discounted( $rows[ $step - 1 ] ) && (int) $rows[ $step - 1 ]['to'] === (int) $rows[ $step ]['from'] && round( (float) $rows[ $step - 1 ]['price'], 4 ) === round( (float) $rows[ $step ]['price'], 4 ) ) { --$step; } // With the reductions before it that were not lower. $first = $step; while ( $first > 0 && self::is_discounted( $rows[ $first - 1 ] ) && (int) $rows[ $first - 1 ]['to'] === (int) $rows[ $first ]['from'] && round( (float) $rows[ $first - 1 ]['price'], 4 ) >= round( (float) $rows[ $first ]['price'], 4 ) ) { --$first; } // A limited first start ends that many days after it, whether or // not the price changed again since. if ( null !== $chain && ( 0 === (int) $chain || max( (int) $now, (int) $rows[ $step ]['from'] ) - (int) $rows[ $first ]['from'] > (int) $chain * DAY_IN_SECONDS ) ) { $first = $step; } $start = (int) $rows[ $first ]['from']; if ( 0 === $first ) { // The discount was already running when the log began, so its // real start and the prices before it are unknown. $result['start'] = $start; return $result; } $result['start_known'] = true; } $result['start'] = $start; $window_start = $start - $window; $result['complete'] = (int) $rows[0]['from'] <= $window_start; $lowest = null; for ( $i = 0; $i < $first && $i <= $last; $i++ ) { $row = $rows[ $i ]; $from = (int) $row['from']; $to = null === $row['to'] ? PHP_INT_MAX : (int) $row['to']; if ( $from >= $start || $to <= $window_start ) { continue; } $price = (float) $row['price']; if ( null === $lowest || $price < $lowest ) { $lowest = $price; } } $result['lowest'] = $lowest; return $result; } /** * Whether a log row is a price below its regular price. * * @param array $row Log row. * @return bool */ public static function is_discounted( array $row ) { if ( null === $row['sale'] || '' === $row['sale'] || null === $row['regular'] || '' === $row['regular'] ) { return false; } return round( (float) $row['price'], 4 ) < round( (float) $row['regular'], 4 ); } }