diff --git a/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php b/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php index 748ffd5c..704b1cd1 100644 --- a/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php +++ b/client-mu-plugins/goodbids/src/classes/Auctions/Auction.php @@ -1510,4 +1510,15 @@ public function bid_locked(): bool { public function lock_bid(): void { update_post_meta( $this->get_variation_id(), self::BID_LOCKED_META_KEY, get_current_user_id() ); } + + /** + * Unlock the bid in the event checkout fails. + * + * @since 1.0.0 + * + * @return void + */ + public function unlock_bid(): void { + delete_post_meta( $this->get_variation_id(), self::BID_LOCKED_META_KEY ); + } } diff --git a/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php b/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php index a591c9dc..a6b80853 100644 --- a/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php +++ b/client-mu-plugins/goodbids/src/classes/Auctions/Bids.php @@ -143,6 +143,8 @@ function (): void { return; } + nocache_headers(); + $auction_id = get_queried_object_id(); $auction = new Auction( $auction_id ); diff --git a/client-mu-plugins/goodbids/src/classes/Auctions/Rewards.php b/client-mu-plugins/goodbids/src/classes/Auctions/Rewards.php index 5b60ba52..cec2111b 100644 --- a/client-mu-plugins/goodbids/src/classes/Auctions/Rewards.php +++ b/client-mu-plugins/goodbids/src/classes/Auctions/Rewards.php @@ -138,6 +138,8 @@ function (): void { return; } + nocache_headers(); + $auction_id = get_queried_object_id(); $auction = new Auction( $auction_id ); $reward_id = $auction->get_reward_id(); @@ -457,6 +459,11 @@ public function update_price_to_auction_bid( int $auction_id ): void { $winning_bid = $auction->get_last_bid(); + if ( ! $winning_bid ) { + Log::error( 'No winning bid found.', compact( 'auction_id' ) ); + return; + } + if ( floatval( $reward->get_price( 'edit' ) ) === $winning_bid->get_subtotal() ) { return; } diff --git a/client-mu-plugins/goodbids/src/classes/Plugins/MiniOrange.php b/client-mu-plugins/goodbids/src/classes/Plugins/MiniOrange.php index 99a4e3b4..1b853451 100644 --- a/client-mu-plugins/goodbids/src/classes/Plugins/MiniOrange.php +++ b/client-mu-plugins/goodbids/src/classes/Plugins/MiniOrange.php @@ -73,20 +73,26 @@ function ( int $site_id ) { // Source: plugins/miniorange-oauth-oidc-single-sign-on/classes/Premium/Multisite/class-multisitesettings.php // Method: save_multisite_settings - // Encoded Option Name: Look for $Yh->mo_oauth_client_update_option() in the method + // Encoded Option Name: Look for $mo_global->mo_oauth_client_update_option() in the method // Decrypt: json_decode( ( new MOUtils() )->mooauthdecrypt( $value ) ) - // Encrypt: $Yh->mooauthencrypt( json_encode( $value ) ) + // Encrypt: $mo_global->mooauthencrypt( json_encode( $value ) ) // Class: MOUtils // Path: plugins/miniorange-oauth-oidc-single-sign-on/classes/common/class-moutils.php - // Access: global $Yh; + // Access: global $Yh; or similar - global $Yh; + global $Yh, $Uj; // This has potential to change when the MO plugin is updated. + + if ( empty( $Yh ) && empty( $Uj ) ) { + return; + } + + $mo_global = ! empty( $Yh ) ? $Yh : $Uj; $sites_key = 'mo_oauth_c3Vic2l0ZXNzZWxlY3RlZA'; - $sites_val = $Yh->mo_oauth_client_get_option( $sites_key ); - $sites = json_decode( $Yh->mooauthdecrypt( $sites_val ) ); + $sites_val = $mo_global->mo_oauth_client_get_option( $sites_key ); + $sites = json_decode( $mo_global->mooauthdecrypt( $sites_val ) ); if ( ! $sites || ! is_array( $sites ) ) { Log::error( 'Unable to decode the SSO settings.', compact( 'sites_val', 'sites' ) ); @@ -102,7 +108,7 @@ function ( int $site_id ) { // Make sure they don't exceed their limit. $no_of_sites_key = 'noOfSubSites'; - $no_of_sites = intval( $Yh->mo_oauth_client_get_option( $no_of_sites_key ) ); + $no_of_sites = intval( $mo_global->mo_oauth_client_get_option( $no_of_sites_key ) ); if ( count( $sites ) + 1 > $no_of_sites ) { Log::warning( 'You have reached the limit of sub-sites allowed for SSO.', compact( 'sites', 'no_of_sites' ) ); @@ -110,9 +116,9 @@ function ( int $site_id ) { } $sites[] = $site_id; - $updated = $Yh->mooauthencrypt( json_encode( $sites ) ); // phpcs:ignore + $updated = $mo_global->mooauthencrypt( json_encode( $sites ) ); // phpcs:ignore - $Yh->mo_oauth_client_update_option( $sites_key, $updated ); + $mo_global->mo_oauth_client_update_option( $sites_key, $updated ); // :crossed_fingers: } diff --git a/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Checkout.php b/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Checkout.php index a698e7f7..ab21a58c 100644 --- a/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Checkout.php +++ b/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Checkout.php @@ -13,6 +13,7 @@ use GoodBids\Network\Nonprofit; use GoodBids\Plugins\WooCommerce; use GoodBids\Utilities\Log; +use WC_Order; use WP_Block; /** @@ -31,6 +32,9 @@ public function __construct() { // Track Auction info for the order during checkout. $this->store_auction_info_on_checkout(); + // Unlock the bid product when the payment fails. + $this->unlock_bid_on_payment_failure(); + // Validate Bids during checkout. $this->validate_bid(); @@ -92,6 +96,36 @@ function ( int $order_id ) { ); } + /** + * Unlock the bid product when the payment fails. + * + * @since 1.0.1 + * + * @return void + */ + private function unlock_bid_on_payment_failure(): void { + add_action( + 'woocommerce_order_status_failed', + function ( int $order_id ): void { + $info = goodbids()->woocommerce->orders->get_auction_info( $order_id ); + + if ( ! $info ) { + Log::warning( 'Unable to retrieve Auction info from Order Meta.', compact( 'order_id' ) ); + return; + } + + if ( Bids::ITEM_TYPE !== $info['order_type'] ) { + return; + } + + $auction = goodbids()->auctions->get( $info['auction_id'] ); + + $auction->unlock_bid(); + }, + 50 + ); + } + /** * Validate Bids during checkout * @@ -102,11 +136,7 @@ function ( int $order_id ) { private function validate_bid(): void { add_action( 'woocommerce_store_api_checkout_update_order_from_request', - function ( $order, $request = [] ) { - if ( empty( $request ) ) { - return; - } - + function ( WC_Order $order ): void { $info = goodbids()->woocommerce->orders->get_auction_info( $order->get_id() ); if ( ! $info ) { @@ -129,6 +159,7 @@ function ( $order, $request = [] ) { // Make sure Auction has not ended. if ( $auction->has_ended() ) { goodbids()->notices->add_notice( Notices::AUCTION_HAS_ENDED ); + goodbids()->woocommerce->orders->delete( $order->get_id(), true ); return; } @@ -149,16 +180,15 @@ function ( $order, $request = [] ) { // Perform this check last to ensure the bid hasn't already been placed. if ( ! $auction->bid_allowed( $info ) ) { + goodbids()->woocommerce->orders->delete( $order->get_id(), true ); goodbids()->notices->add_notice( Notices::BID_ALREADY_PLACED ); - WC()->cart->empty_cart(); return; } // Lock the Bid. $auction->lock_bid(); }, - 50, - 2 + 50 ); } diff --git a/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Orders.php b/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Orders.php index c22ece61..7b2065b1 100644 --- a/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Orders.php +++ b/client-mu-plugins/goodbids/src/classes/Plugins/WooCommerce/Orders.php @@ -424,4 +424,32 @@ function () { } ); } + + /** + * Delete an Order + * + * @since 1.0.1 + * + * @param int $order_id + * @param bool $force + * + * @return void + */ + public function delete( int $order_id, bool $force = false ): void { + $order = wc_get_order( $order_id ); + + if ( ! $order ) { + return; + } + + $order->delete( $force ); + wp_delete_post( $order->get_id(),$force ); + + if ( $force ) { + global $wpdb; + $wpdb->delete( $wpdb->prefix . 'woocommerce_order_items', [ 'order_id' => $order_id ] ); + $wpdb->delete( $wpdb->prefix . 'wc_orders_meta', [ 'order_id' => $order_id ] ); + $wpdb->delete( $wpdb->prefix . 'wc_orders', [ 'id' => $order_id ] ); + } + } } diff --git a/client-mu-plugins/goodbids/src/classes/Users/Referrals.php b/client-mu-plugins/goodbids/src/classes/Users/Referrals.php index 0a555c0e..e33f9b34 100644 --- a/client-mu-plugins/goodbids/src/classes/Users/Referrals.php +++ b/client-mu-plugins/goodbids/src/classes/Users/Referrals.php @@ -349,7 +349,6 @@ public function convert( int $referrer_id, int $user_id, int $auction_id, int $o $referral_id = $this->get_referral( $referrer_id, $user_id ); if ( is_null( $referral_id ) ) { - Log::warning( 'Referral not found', compact( 'user_id', 'referrer_id' ) ); return false; }