diff --git a/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php b/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php index 64ae61fa..316503ac 100644 --- a/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php +++ b/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php @@ -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() ); diff --git a/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php b/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php index 7878f5c6..a591c9dc 100644 --- a/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php +++ b/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php @@ -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(); } /** @@ -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() ); }, @@ -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 ); @@ -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; } @@ -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. * @@ -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 ); + } + } + } + } + ); + } }