Skip to content

Commit

Permalink
Auction GUID and End Date/Time fields (#106)
Browse files Browse the repository at this point in the history
* [#81] Set and Get the GUID for Auctions

* [#81] Added Auction End Date/Time field

* [#81] API access method + documentation

* Fix a PHP bug.
  • Loading branch information
bd-viget authored Dec 21, 2023
1 parent eb3a604 commit ce80115
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
6 changes: 6 additions & 0 deletions client-mu-plugins/goodbids/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ Returns the Auction Reward's Estimated Value. If `$auction_id` is not provided,
`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_end_date_time( int $auction_id )`
Returns the Auction's End Date/Time in MySQL format. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->has_started( int $auction_id )`
Checks if the Auction has started. If `$auction_id` is not provided, the current post ID will be used.

Expand All @@ -70,6 +73,9 @@ Returns the Auction's Goal value. If `$auction_id` is not provided, the current
`goodbids()->auctions->get_expected_high_bid( int $auction_id )`
Returns the Auction's Expected High Bid value. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_guid( int $auction_id )`
Returns the Auction's Unique GUID. If `$auction_id` is not provided, the current post ID will be used.

### Bids Functions

`goodbids()->auctions->bids->get_auction_id( int $bid_product_id )`
Expand Down
22 changes: 20 additions & 2 deletions client-mu-plugins/goodbids/acf-json/group_6570c1fa76181.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,25 @@
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"width": "50",
"class": "",
"id": ""
},
"display_format": "m\/d\/Y g:i a",
"return_format": "Y-m-d H:i:s",
"first_day": 0
},
{
"key": "field_65822160b4398",
"label": "Auction End",
"name": "auction_end",
"aria-label": "",
"type": "date_time_picker",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "50",
"class": "",
"id": ""
},
Expand Down Expand Up @@ -179,6 +197,6 @@
"active": true,
"description": "",
"show_in_rest": 0,
"modified": 1702659886,
"modified": 1703027242,
"private": true
}
9 changes: 6 additions & 3 deletions client-mu-plugins/goodbids/blocks/bid-now/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,12 @@ class BidNow {
* @param array $block
*/
public function __construct( array $block ) {
$this->block = $block;
$this->auction_id = goodbids()->auctions->get_auction_id();
$this->bid_product_id = goodbids()->auctions->get_bid_product_id( $this->auction_id );
$this->block = $block;
$this->auction_id = goodbids()->auctions->get_auction_id();

if ( $this->auction_id ) {
$this->bid_product_id = goodbids()->auctions->get_bid_product_id( $this->auction_id );
}
}

/**
Expand Down
70 changes: 70 additions & 0 deletions client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class Auctions {
*/
const TOTAL_RAISED_TRANSIENT = 'gb:total-raised:%d';

/**
* @since 1.0.0
* @var string
*/
const GUID_META_KEY = 'gb_guid';

/**
* @since 1.0.0
* @var Bids
Expand Down Expand Up @@ -82,6 +88,9 @@ public function __construct() {

// Sets a default image
$this->set_default_feature_image();

// Generate a unique ID for each Auction.
$this->generate_guid_on_publish();
}

/**
Expand Down Expand Up @@ -353,6 +362,19 @@ public function get_start_date_time( int $auction_id = null ): string {
return $this->get_setting( 'auction_start', $auction_id );
}

/**
* Get the Auction End Date/Time
*
* @since 1.0.0
*
* @param ?int $auction_id
*
* @return string
*/
public function get_end_date_time( int $auction_id = null ): string {
return $this->get_setting( 'auction_end', $auction_id );
}

/**
* Check if an Auction has started.
*
Expand Down Expand Up @@ -833,4 +855,52 @@ function ( string $html, int $post_id ) {
2
);
}

/**
* Get the Auction GUID
*
* @since 1.0.0
*
* @param ?int $auction_id
*
* @return string
*/
public function get_guid( int $auction_id = null ): string {
if ( null === $auction_id ) {
$auction_id = $this->get_auction_id();
}

return get_post_meta( $auction_id, self::GUID_META_KEY, true );
}

/**
* Create a Bid product when an Auction is created.
*
* @since 1.0.0
*
* @return void
*/
private function generate_guid_on_publish(): void {
add_action(
'wp_after_insert_post',
function ( $post_id ): void {
// 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 ( $this->get_post_type() !== get_post_type( $post_id ) ) {
return;
}

// Bail if the Auction already has a guid.
if ( $this->get_guid( $post_id ) ) {
return;
}

update_post_meta( $post_id, self::GUID_META_KEY, wp_generate_uuid4() );
}
);
}
}

0 comments on commit ce80115

Please sign in to comment.