Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#62] Trash/Restore/Delete Auction's Associated Bid Product #122

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions client-mu-plugins/goodbids/src/classes/Auctions/Bids.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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 );
}
);
}
}