Skip to content

Commit

Permalink
#18 Bid Products (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
bd-viget authored Dec 11, 2023
1 parent 903cb99 commit c0a8163
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 3 deletions.
16 changes: 16 additions & 0 deletions client-mu-plugins/goodbids/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

6 changes: 4 additions & 2 deletions client-mu-plugins/goodbids/group_6570c1fa76181.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
"post_status": [
"publish"
],
"taxonomy": "",
"taxonomy": [
"product_cat:prizes"
],
"return_format": "object",
"multiple": 0,
"allow_null": 0,
Expand Down Expand Up @@ -111,6 +113,6 @@
"active": true,
"description": "",
"show_in_rest": 0,
"modified": 1702068952,
"modified": 1702079254,
"private": true
}
1 change: 1 addition & 0 deletions client-mu-plugins/goodbids/src/classes/admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Admin {
* @since 1.0.0
*/
public function __construct() {
// Initialize Submodules.
$this->assets = new Assets();
}

Expand Down
153 changes: 153 additions & 0 deletions client-mu-plugins/goodbids/src/classes/auctions/Auctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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.
*
Expand All @@ -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 ) );
}
}
117 changes: 117 additions & 0 deletions client-mu-plugins/goodbids/src/classes/auctions/Bids.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* Bids Functionality
*
* @since 1.0.0
* @package GoodBids
*/

namespace GoodBids\Auctions;

/**
* Class for Bids
*
* @since 1.0.0
*/
class Bids {

/**
* @since 1.0.0
* @var string
*/
const AUCTION_BID_META_KEY = 'gb_bid_product_id';

/**
* Initialize Bids
*
* @since 1.0.0
*/
public function __construct() {
// Create a Bid product when an Auction is created.
$this->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;
}
}
15 changes: 14 additions & 1 deletion docs/hooks/filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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;
}
);
```

0 comments on commit c0a8163

Please sign in to comment.