Skip to content

Commit

Permalink
[#103] Forgot to commit this Author code
Browse files Browse the repository at this point in the history
  • Loading branch information
bd-viget committed Mar 22, 2024
1 parent 77bd93a commit 22817ea
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
3 changes: 3 additions & 0 deletions client-mu-plugins/goodbids/src/classes/Auctions/Auction.php
Original file line number Diff line number Diff line change
Expand Up @@ -1362,6 +1362,9 @@ public function increase_bid(): bool {
return false;
}

$bid_author = get_post_field( 'post_author', $bid_product->get_id() );
$bids->set_variation_author( $new_variation, $bid_author );

// Set the Bid variation as a meta of the Auction.
$this->set_bid_variation_id( $new_variation->get_id() );

Expand Down
78 changes: 76 additions & 2 deletions client-mu-plugins/goodbids/src/classes/Auctions/Bids.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ public function __construct() {

// Reduce the current Bid Product stock to 0 when the Auction ends.
$this->disable_bid_product_on_auction_end();

// This ensures all Bid Variations have the same author as the Bid Product.
$this->update_bid_variation_authors();
}

/**
Expand Down Expand Up @@ -270,6 +273,9 @@ function ( $post_id ): void {
return;
}

$bid_author = get_post_field( 'post_author', $bid_product->get_id() );
$this->set_variation_author( $variation, $bid_author );

// Set the Bid variation as a meta of the Auction.
$auction->set_bid_variation_id( $variation->get_id() );
},
Expand Down Expand Up @@ -460,7 +466,7 @@ public function get_variation_id( int $auction_id ): int {

$variation = wc_get_product( $variation_id );

if ( ! $variation ) {
if ( $variation_id && ! $variation ) {
if ( ! $this->reset_variation_id( $auction_id ) ) {
// This is bad.
return $this->get_product_id( $auction_id );
Expand All @@ -487,7 +493,7 @@ private function reset_variation_id( int $auction_id ): bool {
/** @var WC_Product_Variable $product */
$product = $auction->get_bid_product();

if ( ! $product->is_type( 'variable' ) ) {
if ( ! $product || ! $product->is_type( 'variable' ) ) {
return false;
}

Expand Down Expand Up @@ -535,12 +541,35 @@ private function reset_variation_id( int $auction_id ): bool {
return false;
}

$bid_author = get_post_field( 'post_author', $bid_product->get_id() );
$this->set_variation_author( $new_variation, $bid_author );

// Set the Bid variation as a meta of the Auction.
$auction->set_bid_variation_id( $new_variation->get_id() );

return true;
}

/**
* Changes the Variation Author to prevent deletion when the user is deleted.
*
* @param WC_Product_Variation $variation
* @param int $author_id
*
* @return void
*/
public function set_variation_author( WC_Product_Variation $variation, int $author_id = 1 ): void {
$update = [
'ID' => $variation->get_id(),
'post_author' => $author_id,
];

$result = wp_update_post( $update );
if ( is_wp_error( $result ) ) {
Log::error( $result->get_error_message() );
}
}

/**
* Retrieves the Bid product ID for an Auction.
*
Expand Down Expand Up @@ -905,4 +934,49 @@ function ( int $auction_id ) {
);
}

/**
* Update all Bid Variation authors to match the Bid Product author.
*
* TODO: REMOVE ME AFTER 2024-04-30
*
* @since 1.0.0
*
* @return void
*/
private function update_bid_variation_authors(): void {
add_action(
'admin_init',
function () {
$bid_products = wc_get_products(
[
'limit' => -1,
'type' => 'variable',
'category' => self::ITEM_TYPE,
'date_query' => [
'before' => [
'year' => 2024,
'month' => 4,
'day' => 30,
],
],
]
);

foreach ( $bid_products as $bid_product ) {
$author_id = get_post_field( 'post_author', $bid_product->get_id() );
$variations = $bid_product->get_available_variations();

foreach ( $variations as $variation_data ) {
/** @var WC_Product_Variation $variation */
$variation = wc_get_product( $variation_data['variation_id'] );
$variation_author = get_post_field( 'post_author', $variation->get_id() );

if ( $variation_author !== $author_id ) {
$this->set_variation_author( $variation, $author_id );
}
}
}
}
);
}
}

0 comments on commit 22817ea

Please sign in to comment.