From 62806648e070a2fdca054c145ac88e8c1c450bbd Mon Sep 17 00:00:00 2001 From: Brian DiChiara Date: Wed, 27 Dec 2023 13:01:41 -0600 Subject: [PATCH] [#62] Trash/Restore/Delete Auction's Associated Bid Product --- .../goodbids/src/classes/Auctions/Bids.php | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php b/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php index 067583cb4..363e623c7 100644 --- a/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php +++ b/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php @@ -38,6 +38,15 @@ public function __construct() { // Bump Auction Bid Product Price when an Order is completed. $this->update_bid_product_on_order_complete(); + + // Trash the Bid product when an Auction is trashed. + $this->trash_bid_product_on_auction_trash(); + + // Restore the Bid product when an Auction is restored from trash. + $this->restore_bid_product_on_auction_restore(); + + // Delete the Bid product when an Auction is deleted. + $this->delete_bid_product_on_auction_delete(); } /** @@ -180,4 +189,88 @@ function ( int $order_id, int $auction_id ) { 2 ); } + + /** + * Trash the Bid product when an Auction is trashed. + * + * @since 1.0.0 + * @return void + */ + private function trash_bid_product_on_auction_trash(): void { + add_action( + 'trashed_post', + function ( int $post_id ): void { + // Bail if not an Auction. + if ( goodbids()->auctions->get_post_type() !== get_post_type( $post_id ) ) { + return; + } + + $bid_product_id = goodbids()->auctions->get_bid_product_id( $post_id ); + + // Bail if the Auction doesn't have a Bid product. + if ( ! $bid_product_id ) { + return; + } + + // Trash the Bid product. + wp_trash_post( $bid_product_id ); + } + ); + } + + /** + * Restore the Bid product when an Auction is restored from trash. + * + * @since 1.0.0 + * @return void + */ + private function restore_bid_product_on_auction_restore(): void { + add_action( + 'untrashed_post', + function ( int $post_id ): void { + // Bail if not an Auction. + if ( goodbids()->auctions->get_post_type() !== get_post_type( $post_id ) ) { + return; + } + + $bid_product_id = goodbids()->auctions->get_bid_product_id( $post_id ); + + // Bail if the Auction doesn't have a Bid product. + if ( ! $bid_product_id ) { + return; + } + + // Restore the Bid product. + wp_untrash_post( $bid_product_id ); + } + ); + } + + /** + * Delete the Bid product when an Auction is deleted. + * + * @since 1.0.0 + * @return void + */ + private function delete_bid_product_on_auction_delete(): void { + add_action( + 'before_delete_post', + function ( int $post_id ): void { + // Bail if not an Auction. + if ( goodbids()->auctions->get_post_type() !== get_post_type( $post_id ) ) { + return; + } + + $bid_product_id = goodbids()->auctions->get_bid_product_id( $post_id ); + + // Bail if the Auction doesn't have a Bid product. + if ( ! $bid_product_id ) { + return; + } + + // Delete the Bid product. + wp_delete_post( $bid_product_id, true ); + } + ); + } }