diff --git a/app/PriorPrice/HistoryStorage.php b/app/PriorPrice/HistoryStorage.php index 7756641..fb7d8c2 100644 --- a/app/PriorPrice/HistoryStorage.php +++ b/app/PriorPrice/HistoryStorage.php @@ -32,11 +32,13 @@ public function get_minimal( int $product_id, int $days = 30 ) : float { $history = $this->get_history( $product_id ); + $this_ = $this; + // Get only $days last items. $the_last = array_filter( $history, - static function( $timestamp ) use ( $days ) { - return $timestamp >= ( time() - ( $days * DAY_IN_SECONDS ) ); + static function( $timestamp ) use ( $days, $this_ ) { + return $timestamp >= ( $this_->get_time_with_offset() - ( $days * DAY_IN_SECONDS ) ); }, ARRAY_FILTER_USE_KEY ); @@ -106,7 +108,7 @@ public function add_price( int $product_id, float $price, bool $on_change_only ) return 0; } - $history[ time() + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) ] = $price; + $history[ $this->get_time_with_offset() ] = $price; return $this->save_history( $product_id, $history ); } @@ -161,7 +163,7 @@ public function get_history( int $product_id ) : array { private function fill_empty_history( int $product_id, array $history ) : array { if ( empty( $history ) ) { - $history[ time() ] = get_post_meta( $product_id, '_price', true ); + $history[ $this->get_time_with_offset() ] = get_post_meta( $product_id, '_price', true ); $this->save_history( $product_id, $history ); } @@ -206,4 +208,16 @@ static function( $carry, $item ) { } ); } + + /** + * Get time with offset. + * + * @since 1.6.1 + * + * @return int + */ + private function get_time_with_offset() : int { + + return time() + ( (int) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ); + } } diff --git a/tests/phpunit/HistoryStorageTest.php b/tests/phpunit/HistoryStorageTest.php index 8c248f4..b2679d0 100644 --- a/tests/phpunit/HistoryStorageTest.php +++ b/tests/phpunit/HistoryStorageTest.php @@ -33,6 +33,11 @@ public function test_get_minimal( $history, $expected_minimal ) { 'return' => $history ] ); + \WP_Mock::userFunction( 'get_option', [ + 'args' => [ 'gmt_offset' ], + 'return' => 0 + ] ); + $minimal = $subject->get_minimal( $product_id, 30 );