From c0a8163a1bba9abf56dbbd34e23a913bd2a2a188 Mon Sep 17 00:00:00 2001 From: Brian DiChiara <122309362+bd-viget@users.noreply.github.com> Date: Mon, 11 Dec 2023 11:34:09 -0600 Subject: [PATCH] #18 Bid Products (#61) --- client-mu-plugins/goodbids/README.md | 16 ++ .../goodbids/group_6570c1fa76181.json | 6 +- .../goodbids/src/classes/admin/Admin.php | 1 + .../src/classes/auctions/Auctions.php | 153 ++++++++++++++++++ .../goodbids/src/classes/auctions/Bids.php | 117 ++++++++++++++ docs/hooks/filters.md | 15 +- 6 files changed, 305 insertions(+), 3 deletions(-) create mode 100644 client-mu-plugins/goodbids/src/classes/auctions/Bids.php diff --git a/client-mu-plugins/goodbids/README.md b/client-mu-plugins/goodbids/README.md index 2252675bc..531dbfbc7 100644 --- a/client-mu-plugins/goodbids/README.md +++ b/client-mu-plugins/goodbids/README.md @@ -36,3 +36,19 @@ Renders an admin field based on the given field array. The `$prefix` and `$data` `goodbids()->auctions->get_post_type()` Returns the auction post type slug. + +`goodbids()->auctions->get_setting( string $setting, int $auction_id )` +Returns a setting value for an auction. If `$auction_id` is not provided, the current post ID will be used. + +`goodbids()->auctions->get_prize_product_id( int $auction_id )` +Returns the Auction's Prize Product ID. If `$auction_id` is not provided, the current post ID will be used. + +`goodbids()->auctions->get_start_date_time( int $auction_id )` +Returns the Auction's Start Date/Time in MySQL format. If `$auction_id` is not provided, the current post ID will be used. + +`goodbids()->auctions->get_bid_increment( int $auction_id )` +Returns the Auction's Bid Increment value. If `$auction_id` is not provided, the current post ID will be used. + +`goodbids()->auctions->get_goal( int $auction_id )` +Returns the Auction's Goal value. If `$auction_id` is not provided, the current post ID will be used. + diff --git a/client-mu-plugins/goodbids/group_6570c1fa76181.json b/client-mu-plugins/goodbids/group_6570c1fa76181.json index 6c84738c7..5e0b12f72 100644 --- a/client-mu-plugins/goodbids/group_6570c1fa76181.json +++ b/client-mu-plugins/goodbids/group_6570c1fa76181.json @@ -22,7 +22,9 @@ "post_status": [ "publish" ], - "taxonomy": "", + "taxonomy": [ + "product_cat:prizes" + ], "return_format": "object", "multiple": 0, "allow_null": 0, @@ -111,6 +113,6 @@ "active": true, "description": "", "show_in_rest": 0, - "modified": 1702068952, + "modified": 1702079254, "private": true } diff --git a/client-mu-plugins/goodbids/src/classes/admin/Admin.php b/client-mu-plugins/goodbids/src/classes/admin/Admin.php index 007d5ee16..2ecba2c09 100644 --- a/client-mu-plugins/goodbids/src/classes/admin/Admin.php +++ b/client-mu-plugins/goodbids/src/classes/admin/Admin.php @@ -27,6 +27,7 @@ class Admin { * @since 1.0.0 */ public function __construct() { + // Initialize Submodules. $this->assets = new Assets(); } diff --git a/client-mu-plugins/goodbids/src/classes/auctions/Auctions.php b/client-mu-plugins/goodbids/src/classes/auctions/Auctions.php index 06e1e2d41..bf54b3c92 100644 --- a/client-mu-plugins/goodbids/src/classes/auctions/Auctions.php +++ b/client-mu-plugins/goodbids/src/classes/auctions/Auctions.php @@ -33,13 +33,26 @@ class Auctions { */ const SINGULAR_SLUG = 'auction'; + /** + * @since 1.0.0 + * @var Bids + */ + public Bids $bids; + /** * Initialize Auctions * * @since 1.0.0 */ public function __construct() { + // Initialize Submodules. + $this->bids = new Bids(); + + // Register Post Type. $this->register_post_type(); + + // Init Prizes Category. + $this->init_prizes_category(); } /** @@ -118,6 +131,22 @@ function () { ); } + /** + * Initialize Prizes Category + * + * @since 1.0.0 + * + * @return void + */ + private function init_prizes_category() : void { + add_action( + 'init', + function () { + $this->get_prizes_category_id(); + } + ); + } + /** * Returns the Auction post type slug. * @@ -129,4 +158,128 @@ public function get_post_type() : string { return self::POST_TYPE; } + /** + * Retrieve the Prizes category ID, or create it if it doesn't exist. + * + * @since 1.0.0 + * + * @return int + */ + public function get_prizes_category_id() : int { + $prizes_category = get_term_by( 'slug', 'prizes', 'product_cat' ); + if ( ! $prizes_category ) { + $prizes_category = wp_insert_term( 'Prizes', 'product_cat' ); + } + return $prizes_category->term_id; + } + + /** + * Check if an auction has a Bid product. + * + * @since 1.0.0 + * + * @param int $auction_id + * + * @return bool + */ + public function has_bid_product( int $auction_id ) : bool { + return boolval( $this->get_bid_product_id( $auction_id ) ); + } + + /** + * Retrieves the Bid product ID for an Auction. + * + * @since 1.0.0 + * + * @param int $auction_id + * + * @return int + */ + public function get_bid_product_id( int $auction_id ) : int { + return intval( get_post_meta( $auction_id, Bids::AUCTION_BID_META_KEY, true ) ); + } + + /** + * Sets the Bid product ID for an Auction. + * + * @since 1.0.0 + * + * @param int $auction_id + * @param int $bid_product_id + * + * @return void + */ + public function set_bid_product_id( int $auction_id, int $bid_product_id ) : void { + update_post_meta( $auction_id, Bids::AUCTION_BID_META_KEY, $bid_product_id ); + } + + /** + * Retrieves a setting for an Auction. + * + * @since 1.0.0 + * + * @param string $meta_key + * @param int|null $auction_id + * + * @return mixed + */ + public function get_setting( string $meta_key, int $auction_id = null ) : mixed { + if ( ! $auction_id ) { + $auction_id = get_the_ID(); + } + + return get_field( $meta_key, $auction_id ); + } + + /** + * Get the Auction Prize Product ID. + * + * @since 1.0.0 + * + * @param int|null $auction_id + * + * @return int + */ + public function get_prize_product_id( int $auction_id = null ) : int { + return intval( $this->get_setting( 'auction_product', $auction_id ) ); + } + + /** + * Get the Auction Start Date/Time + * + * @since 1.0.0 + * + * @param int|null $auction_id + * + * @return string + */ + public function get_start_date_time( int $auction_id = null ) : string { + return $this->get_setting( 'auction_start', $auction_id ); + } + + /** + * Get the Auction Bid Increment amount + * + * @since 1.0.0 + * + * @param int|null $auction_id + * + * @return int + */ + public function get_bid_increment( int $auction_id = null ) : int { + return intval( $this->get_setting( 'bid_increment', $auction_id ) ); + } + + /** + * Get the Auction Goal Amount + * + * @since 1.0.0 + * + * @param int|null $auction_id + * + * @return int + */ + public function get_goal( int $auction_id = null ) : int { + return intval( $this->get_setting( 'auction_goal', $auction_id ) ); + } } diff --git a/client-mu-plugins/goodbids/src/classes/auctions/Bids.php b/client-mu-plugins/goodbids/src/classes/auctions/Bids.php new file mode 100644 index 000000000..35ca447fd --- /dev/null +++ b/client-mu-plugins/goodbids/src/classes/auctions/Bids.php @@ -0,0 +1,117 @@ +create_bid_product_for_auction(); + } + + /** + * Create a Bid product when an Auction is created. + * + * @since 1.0.0 + * + * @return void + */ + private function create_bid_product_for_auction() : void { + add_action( + 'wp_after_insert_post', + function ( $post_id ) { + // Bail if this is a revision. + if ( wp_is_post_revision( $post_id ) || 'publish' !== get_post_status( $post_id ) ) { + return; + } + + // Bail if not an Auction. + if ( Auctions::POST_TYPE !== get_post_type( $post_id ) ) { + return; + } + + // Bail if the Auction already has a Bid product. + if ( goodbids()->auctions->has_bid_product( (int) $post_id ) ) { + return; + } + + $bid_price = goodbids()->auctions->get_bid_increment( (int) $post_id ); + + // Make sure this meta value has been saved. + if ( ! $bid_price ) { + return; + } + + $bid_title = sprintf( + '%s %s (%s: %s)', + __( 'Bid for', 'goodbids' ), + get_the_title( $post_id ), + __( 'ID', 'goodbids' ), + $post_id + ); + + // Create a new Bid product. + $bid_product = new \WC_Product_Simple(); + $bid_product->set_name( $bid_title ); + $bid_product->set_slug( sanitize_title( $bid_title ) ); + $bid_product->set_regular_price( $bid_price ); + $bid_product->set_category_ids( [ $this->get_bids_category_id() ] ); + $bid_product->set_status( 'publish' ); + + try { + $bid_product->set_sku( 'BID-' . $post_id ); + } catch ( \WC_Data_Exception $e ) { + // Do nothing. + } + + $bid_product = apply_filters( 'goodbids_bid_product_create', $bid_product, $post_id ); + + if ( ! $bid_product->save() ) { + // TODO: Log error. + return; + } + + // Set the Bid product as a meta of the Auction. + goodbids()->auctions->set_bid_product_id( (int) $post_id, $bid_product->get_id() ); + }, + 10 + ); + } + + /** + * Retrieve the Bids category ID, or create it if it doesn't exist. + * + * @since 1.0.0 + * + * @return int + */ + public function get_bids_category_id() : int { + $bids_category = get_term_by( 'slug', 'bids', 'product_cat' ); + if ( ! $bids_category ) { + $bids_category = wp_insert_term( 'Bids', 'product_cat' ); + } + return $bids_category->term_id; + } +} diff --git a/docs/hooks/filters.md b/docs/hooks/filters.md index 74f30c839..ee2fd0746 100644 --- a/docs/hooks/filters.md +++ b/docs/hooks/filters.md @@ -2,7 +2,7 @@ ### goodbids_nonprofit_custom_fields -Add custom fields to the Nonprofit meta data during new site creation. +Modify Nonprofit custom fields. ```php add_filter( @@ -20,3 +20,16 @@ add_filter( } ); ``` +### goodbids_bid_product_create + +Modify the new bid product associated with a product. + +```php +add_filter( + 'goodbids_bid_product', + function( WC_Product_Simple $bid_product, int $post_id ) : WC_Product_Simple { + $bid_product->set_regular_price( 2 ); + return $bid_product; + } +); +```