-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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. * VS Code settings and extensions recommendations * Update Auction Meta Box, Determine Auction Status * Just some PR feedback that got left off. * [#26] Data validation, restructure, & Status Column * [#26] Added Time Until info * Styling tweaks * [#26] Update Auction meta when auction has started. * [#26] Cron hook documentation * [#26] Last minute tweaks. * [#26] Better method name. * [#26] Removed a space * [#26] Swap API for $this * [#26] More last second fixes. * [N/A] Modified Auction Metrics block. * [N/A] Last change to Pattern * [#82] Add conditional trigger wrapper to Auction Start cron job. * [#82] Bad merge * [#82] Auctioneer class * [#82] WIP: Trigger Start of Auction to Auctioneer * [#82] Catch DateTime Exception * [#82] Some Cleanup and Documentation * [#82] Endpoint adjustment, stripped down payload, removed msg check. * [#82] Change to Auction Start endpoint * [#82] Added 1 last check for the guid * [#82] Move constants to config.json --------- Co-authored-by: Nathan Schmidt <[email protected]>
- Loading branch information
1 parent
b97d92f
commit 259cece
Showing
6 changed files
with
409 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,5 +59,6 @@ vendor | |
# PhpStorm | ||
.idea/ | ||
|
||
# Credentials | ||
# Sensitive data | ||
auth.json | ||
client-mu-plugins/vip-env-vars.local.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
241 changes: 241 additions & 0 deletions
241
client-mu-plugins/goodbids/src/classes/Auctioneer/Auctioneer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
<?php | ||
/** | ||
* The Auctioneer functionality | ||
* | ||
* @since 1.0.0 | ||
* @package GoodBids | ||
*/ | ||
|
||
namespace GoodBids\Auctioneer; | ||
|
||
/** | ||
* Class for Auctioneer, our Node.js server | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
class Auctioneer { | ||
|
||
/** | ||
* Defines the environment to use. | ||
* | ||
* @since 1.0.0 | ||
* @var string | ||
*/ | ||
private string $environment = 'develop'; | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @var ?string | ||
*/ | ||
private ?string $url = null; | ||
|
||
/** | ||
* @since 1.0.0 | ||
* @var bool | ||
*/ | ||
private bool $initialized = false; | ||
|
||
/** | ||
* Initialize Auctioneer | ||
* | ||
* @since 1.0.0 | ||
*/ | ||
public function __construct() { | ||
$environments = goodbids()->get_config( 'vip-constants.auctioneer' ); | ||
|
||
if ( ! $environments || empty( $environments[ $this->environment ] ) ) { | ||
add_action( | ||
'admin_notices', | ||
function() { | ||
printf( | ||
'<div class="notice notice-error is-dismissible"> | ||
<p>%s</p> | ||
</div>', | ||
esc_html__( 'Missing Auctioneer constants config.', 'goodbids' ) | ||
); | ||
} | ||
); | ||
|
||
return; | ||
} | ||
|
||
$this->url = vip_get_env_var( $environments[ $this->environment ], null ); | ||
|
||
// Abort if missing environment variable. | ||
if ( ! $this->url ) { | ||
add_action( | ||
'admin_notices', | ||
function() { | ||
printf( | ||
'<div class="notice notice-error is-dismissible"> | ||
<p>%s</p> | ||
</div>', | ||
esc_html__( 'Missing Auctioneer environment variables.', 'goodbids' ) | ||
); | ||
} | ||
); | ||
|
||
return; | ||
} | ||
|
||
$this->init(); | ||
} | ||
|
||
/** | ||
* Run initialization tasks | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @return void | ||
*/ | ||
private function init(): void { | ||
$this->initialized = true; | ||
} | ||
|
||
/** | ||
* Make a request to an API endpoint | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @param string $endpoint | ||
* @param array $params | ||
* @param string $method | ||
* | ||
* @return ?array | ||
*/ | ||
private function request( string $endpoint, array $params = [], string $method = 'GET' ): ?array { | ||
$url = trailingslashit( $this->url ) . $endpoint; | ||
$response = wp_remote_request( | ||
$url, | ||
[ | ||
'method' => $method, | ||
'headers' => [ | ||
'Content-Type' => 'application/json', | ||
], | ||
'body' => wp_json_encode( $params ), | ||
] | ||
); | ||
|
||
if ( ! is_array( $response ) ) { | ||
return null; | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Trigger an Auction Start event for an Auction | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @param int $auction_id | ||
* | ||
* @return array|bool | ||
*/ | ||
public function auction_start( int $auction_id ): array|bool { | ||
$guid = goodbids()->auctions->get_guid( $auction_id ); | ||
|
||
if ( ! $guid ) { | ||
// TODO: Log error. | ||
return false; | ||
} | ||
|
||
$endpoint = sprintf( 'auctions/%s/start', $guid ); | ||
$bid_product = goodbids()->auctions->get_bid_product( $auction_id ); | ||
|
||
// TODO: Refactor using individual Response classes. | ||
$payload = [ | ||
'id' => $guid, | ||
'startTime' => goodbids()->auctions->get_start_date_time( $auction_id, 'c' ), | ||
'endTime' => goodbids()->auctions->get_end_date_time( $auction_id, 'c' ), | ||
'currentBid' => floatval( $bid_product?->get_price( 'edit' ) ), | ||
'requestTime' => current_datetime()->format( 'c' ), | ||
]; | ||
|
||
$response = $this->request( $endpoint, $payload, 'POST' ); | ||
|
||
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { | ||
// TODO: Log Response | ||
error_log( '[GB] ' . wp_strip_all_tags( $this->get_response_message( $response ) ) ); | ||
return $response; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Get the message from the response array. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @param array $response | ||
* | ||
* @return string | ||
*/ | ||
public function get_response_message( array $response ): string { | ||
$data = $this->get_response_json( $response ); | ||
$msg_raw = $this->get_response_message_raw( $response ); | ||
|
||
if ( ! $msg_raw ) { | ||
return ''; | ||
} | ||
|
||
$message = sprintf( | ||
'<p><strong>%s:</strong></p>', | ||
esc_html( $msg_raw ) | ||
); | ||
|
||
if ( ! empty( $data['details'] ) ) { | ||
$message .= sprintf( | ||
'<ul><li>%s</li></ul>', | ||
is_array( $data['details'] ) ? wp_kses_post( implode( '</li><li>', $data['details'] ) ) : esc_html( $data['details'] ) | ||
); | ||
} | ||
|
||
return $message; | ||
} | ||
|
||
/** | ||
* Get the raw value for the message from the response array. | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @param array $response | ||
* | ||
* @return string | ||
*/ | ||
public function get_response_message_raw( array $response ): string { | ||
$data = $this->get_response_json( $response ); | ||
|
||
if ( empty( $data['message'] ) ) { | ||
return ''; | ||
} | ||
|
||
return $data['message']; | ||
} | ||
|
||
/** | ||
* Get the Response JSON as an array | ||
* | ||
* @since 1.0.0 | ||
* | ||
* @param array $response | ||
* | ||
* @return ?array | ||
*/ | ||
public function get_response_json( array $response ): ?array { | ||
$body = wp_remote_retrieve_body( $response ); | ||
|
||
if ( ! $body ) { | ||
return null; | ||
} | ||
|
||
$data = json_decode( $body, true ); | ||
|
||
if ( ! $data ) { | ||
return null; | ||
} | ||
|
||
return $data; | ||
} | ||
} |
Oops, something went wrong.