Skip to content

Commit

Permalink
[#910] Auction Index Bug (Attempt 1) (#912)
Browse files Browse the repository at this point in the history
* [#910] Consistent data, filtering, and sorting for All Auctions

* [#910] Nothing major, just cleanup

* [#910] Fix PHP Error
  • Loading branch information
bd-viget authored Apr 17, 2024
1 parent c2c9bcf commit ab62dc9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 67 deletions.
83 changes: 29 additions & 54 deletions client-mu-plugins/goodbids/blocks/all-auctions/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,37 +106,17 @@ private function get_offset(): int {
* @return array
*/
public function get_all_auctions(): array {
// if on the main site, get all auctions from all site
if ( is_main_site() ) {
return goodbids()->sites->get_all_open_auctions();
return goodbids()->sites->get_all_auctions();
}

// default to get all auctions from the current site
return collect( ( goodbids()->auctions->get_all() )->posts )
->map(
fn ( int $post_id ) => [
'post_id' => $post_id,
'site_id' => get_current_blog_id(),
]
)
->filter(
fn ( array $auction_data ) => goodbids()->sites->swap(
function () use ( $auction_data ) {
$auction = goodbids()->auctions->get( $auction_data['post_id'] );
return ! $auction->has_ended();
},
$auction_data['site_id']
return collect( goodbids()->auctions->get_all()->posts )
->map(
fn ( int $post_id ) => [
'post_id' => $post_id,
'site_id' => get_current_blog_id(),
]
)
)
->sortByDesc(
function ( array $auction_data ): array {
$auction = goodbids()->auctions->get( $auction_data['post_id'] );
return [
'bid_count' => $auction->get_bid_count(),
'total_raised' => $auction->get_total_raised(),
];
}
)
->all();
}

Expand Down Expand Up @@ -173,7 +153,7 @@ function () use ( $auction_data ) {
fn ( array $auction_data ) => goodbids()->sites->swap(
function () use ( $auction_data ) {
$auction = goodbids()->auctions->get( $auction_data['post_id'] );
return $auction->has_started() && ! $auction->has_ended();
return Auction::STATUS_LIVE === $auction->get_status();
},
$auction_data['site_id']
)
Expand Down Expand Up @@ -240,45 +220,40 @@ public function get_sort_options(): array {
* @return array
*/
private function sort_auctions_by( string $sort, array $auctions = [] ): array {

$sort_method = match ( $sort ) {
'newest' => 'get_start_date_time',
'starting' => 'calculate_starting_bid',
'ending' => 'get_end_date_time',
'low_bid' => 'bid_variation_price',
default => false,
default => 'get_bid_count',
};

if ( $sort_method ) {
$auctions = collect( $auctions )
->sortBy(
fn( array $auction_data ) => goodbids()->sites->swap(
function () use ( $auction_data, $sort_method ) {
$auction = goodbids()->auctions->get( $auction_data['post_id'] );
$auctions = collect( $auctions )
->sortBy(
fn( array $auction_data ) => goodbids()->sites->swap(
function () use ( $auction_data, $sort_method ) {
$auction = goodbids()->auctions->get( $auction_data['post_id'] );

if ( method_exists( $auction, $sort_method ) ) {
return $auction->$sort_method();
}
if ( method_exists( $auction, $sort_method ) ) {
return $auction->$sort_method();
}

if ( 'bid_variation_price' === $sort_method ) {
return goodbids()->bids->get_variation( $auction->get_id() )?->get_price( 'edit' );
}
if ( 'bid_variation_price' === $sort_method ) {
return goodbids()->bids->get_variation( $auction->get_id() )?->get_price( 'edit' );
}

return null;
},
$auction_data['site_id']
)
);

// if sorting by start date, reverse the auctions
if ( $sort_method === 'get_start_date_time' ) {
$auctions = $auctions->reverse();
}
return null;
},
$auction_data['site_id']
)
);

return $auctions->all();
// if sorting by start date, reverse the auctions
if ( in_array( $sort_method, [ 'get_start_date_time', 'get_bid_count' ], true ) ) {
$auctions = $auctions->reverse();
}

return $auctions;
return $auctions->toArray();
}

/**
Expand Down
8 changes: 4 additions & 4 deletions client-mu-plugins/goodbids/src/classes/Auctions/Auction.php
Original file line number Diff line number Diff line change
Expand Up @@ -1293,20 +1293,20 @@ public function extend(): bool {
}

try {
$close = current_datetime()->add( new DateInterval( 'PT' . $extension . 'S' ) );
$close_time = $close->format( 'Y-m-d H:i:s' );
$extended_close = current_datetime()->add( new DateInterval( 'PT' . $extension . 'S' ) );
$extended_close_time = $extended_close->format( 'Y-m-d H:i:s' );
} catch ( Exception $e ) {
Log::error( $e->getMessage(), compact( 'extension' ) );
return false;
}

// Be sure to extend, not shorten.
if ( $close_time < $this->get_end_date_time() ) {
if ( $extended_close_time < $this->get_end_date_time() ) {
return false;
}

// Update the Auction Close Date/Time
update_post_meta( $this->get_id(), self::AUCTION_CLOSE_META_KEY, $close_time );
update_post_meta( $this->get_id(), self::AUCTION_CLOSE_META_KEY, $extended_close_time );

// Update Extensions
$extensions = $this->get_extensions();
Expand Down
19 changes: 10 additions & 9 deletions client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -398,17 +398,17 @@ public function get_unclaimed_reward_auction_emails(): array {

/**
* Get any live auctions that are within a given range of their extension time of closing.
*
*
* @since 1.0.0
*
*
* @param Date $threshold_start
* @param Date $threshold_end
*
*
*
*
* @return array
*/
public function get_auctions_ending_soon($threshold_start, $threshold_end): array {
$query_args =
$query_args =
[
'meta_query'=>[
[
Expand All @@ -435,9 +435,9 @@ public function get_auctions_ending_soon($threshold_start, $threshold_end): arra

/**
* Send emails for any auctions ending in a certain window of time
*
*
* @since 1.0.0
*
*
* @return array
*/
public function get_auctions_ending_soon_emails(): array {
Expand Down Expand Up @@ -588,13 +588,14 @@ public function get_bid_order_ids( ?int $auction_id = null, int $limit = -1, ?in
* Use Auction close date/time when asked for Auction End Date/Time.
*
* @since 1.0.0
*
* @return void
*/
private function override_end_date_time(): void {
add_filter(
'goodbids_auction_setting',
function ( $value, $meta_key, $auction_id ) {
if ( 'auction_end' !== $meta_key ) {
function ( mixed $value, string $meta_key, ?int $auction_id ): mixed {
if ( 'auction_end' !== $meta_key || ! $auction_id ) {
return $value;
}

Expand Down

0 comments on commit ab62dc9

Please sign in to comment.