Skip to content

Commit

Permalink
[#137] API Key Handshake w/Node (#158)
Browse files Browse the repository at this point in the history
* [#137] Add a check on Dev to suppress error when a plugin is missing

* [#137] Fix get_config bug and updated documentation

* [#137] Slight method adjustment

* [#137] WIP : Custom Rest API Endpoint

* [#137] Generate new API keys by domain on Main Site

* [#137] Remove unused API Constants credential related code.

* [#137] PR Feedback updates

* [#137] Shortened condition, better error message.

* [#137] Prevent credentials from being generated on the main site.

* [#137] Added some Since tags.

* [N/A] Massive Documentation Update

* [#137] Better reference to class.
  • Loading branch information
bd-viget authored Jan 9, 2024
1 parent da22bfc commit 15e4a8b
Show file tree
Hide file tree
Showing 18 changed files with 648 additions and 219 deletions.
193 changes: 1 addition & 192 deletions client-mu-plugins/goodbids/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,195 +4,4 @@ A plugin that powers the GoodBids.org site.

## GoodBids API

This is a quick reference of some of the functions available in the GoodBids API.

Call the API using the `goodbids()` function.

### Core Functions

`goodbids()->get_version()`
Returns the current version of the GoodBids plugin.

`goodbids()->get_config( string $setting )`
Returns the config value for the given setting.

`goodbids()->is_plugin_active( string $plugin )`
Checks if the given plugin (slug) is active. This is not the same as the WordPress `is_plugin_active()` function, all it does is checks if the plugin is in the active_plugins config.

### Network Sites Functions

`goodbids()->sites->get_np_data( string $site_id, string $field_id )`
Returns the custom Nonprofit data for the given network site. If `$field_id` is provided, only that field will be returned, otherwise all fields will be returned.

`goodbids()->sites->get_np_fields( string $context )`
Returns the array of custom fields, based on the given context (create, edit, or both).

### Admin Functions

`goodbids()->admin->render_field( string $key, array $field, string $prefix, array $data )`
Renders an admin field based on the given field array. The `$prefix` and `$data` parameters are optional, but required for some field types.

### Auction Functions

`goodbids()->auctions->get_post_type()`
Returns the Auction post type slug.

`goodbids()->auctions->get_auction_id()`
Returns the current Auction ID (if valid).

`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->has_bid_product( int $auction_id )`
Checks if the Auction has a Bid Product. If `$auction_id` is not provided, the current post ID will be used.

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

`goodbids()->auctions->get_bid_product( int $auction_id )`
Returns the Auction's Bid Product object. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->set_bid_product_id( int $auction_id, int $bid_product_id )`
Associates the Bid Product with the Auction and the Auction with the Bid Product.

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

`goodbids()->auctions->get_reward_product( int $auction_id )`
Returns the Auction's Reward Product object. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_product_type( int $product_id )`
Returns the type of the given product ID. Possible types are: "bids", "rewards", or null.

`goodbids()->auctions->get_estimated_value( int $auction_id )`
Returns the Auction Reward's Estimated Value. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_start_date_time( int $auction_id, string $format )`
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, string $format )`
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.

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

`goodbids()->auctions->get_extensions( int $auction_id )`
Returns the number of times the Auction has been extended. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->is_extension_window( int $auction_id )`
Checks if the Auction is currently inside the extension window. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_bid_extension( int $auction_id )`
Returns the Auction's Bid Extension value (in seconds). 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_starting_bid( int $auction_id )`
Returns the Auction's Starting Bid value. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->calculate_starting_bid( int $auction_id )`
Returns the Auction's Calculated Starting Bid value, which will use the Bid Increment if the Starting Bid is not set. 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.

`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.

`goodbids()->auctions->get_bid_order_ids( int $auction_id, int $limit )`
Returns an array of Bid Order IDs for the given Auction ID. `$limit` can be specified to return a set number of orders.

`goodbids()->auctions->get_bid_orders( int $auction_id, int $limit )`
Returns an array of Bid Orders for the given Auction ID. `$limit` can be specified to return a set number of orders.

`goodbids()->auctions->get_bid_order( int $auction_id, int $limit )`
Returns an array of Bid Order objects for the given Auction ID. `$limit` can be specified to return a set number of orders.

`goodbids()->auctions->get_last_bid( int $auction_id )`
Returns the Bid Order object of the last bid for the given Auction ID.

`goodbids()->auctions->get_last_bidder( int $auction_id )`
Returns the User object that placed the last bid.

`goodbids()->auctions->get_bid_count( int $auction_id )`
Get the number of bids for the given Auction ID.

`goodbids()->auctions->get_status( int $auction_id )`
Returns the status of the Auction. Possible values are: "Upcoming", "Live", and "Closed". If the Auction has not yet been published, it will return "Draft".

### Bids Functions

`goodbids()->auctions->bids->get_auction_id( int $bid_product_id )`
Returns the Auction ID for the given Bid Product ID.

### ACF Block Functions

`goodbids()->acf->blocks()->get_all_blocks()`
Get all custom registered blocks.

`goodbids()->acf->blocks()->get_block( string $block_name )`
Get block array by block name.

`goodbids()->acf->blocks()->block_attr()`
_Use the global `block_attr()` helper function instead._ This will render the block attributes for the current block.

`goodbids()->acf->blocks()->get_block_location( string $block_name, string $return )`
Get the location of a block. Return values can be: "directory" (Default) or "json" (Returns the path to block.json. _Can also be found in the `path` key of the block array._

`goodbids()->acf->blocks()->get_block_locations()`
Get all directories where blocks can be found.

### WooCommerce Functions

`goodbids()->woocommerce->get_order_auction_info( int $order_id )`
Returns an array of information for the given Order ID, including the Auction ID (`auction_id`) and Order Type (`order_type`).

`goodbids()->woocommerce->get_order_auction_id( int $order_id )`
Returns the Auction ID for the given Order ID. If `$order_id` is not provided, the current order ID will be used.

`goodbids()->woocommerce->get_order_type( int $order_id )`
Returns the type of the given order ID. Possible types are: "bids", "rewards", or null.

`goodbids()->woocommerce->is_bid_order( int $order_id )`
Checks if the given order ID is a Bid Order.

`goodbids()->woocommerce->is_reward_order( int $order_id )`
Checks if the given order ID is a Reward Order.

### Auctioneer Functions

`goodbids()->auctioneer->request( string $endpoint, array $data, string $method )`
Makes a request to the Auctioneer API.

`goodbids()->auctioneer->get_last_response()`
Returns the last response from the Auctioneer API.

`goodbids()->auctioneer->is_invalid_response( mixed $response )`
Checks if the API response is valid.

`goodbids()->auctioneer->get_response_json( array $response )`
Returns the API response body data as an associative array.

`goodbids()->auctioneer->get_response_message( array $response )`
Retrieves the formatted response message from the API response body.

`goodbids()->auctioneer->get_response_message_raw( array $response )`
Retrieves the raw response message from the API response body.

### Auctioneer: Auction (Endpoint) Functions

`goodbids()->auctioneer->auctions->start( int $auction_id )`
Signals to the Auctioneer API that the Auction has started.

`goodbids()->auctioneer->auctions->update( int $auction_id, string $context )`
Sends an update to the Auctioneer API. The context provided should identify the purpose of the update and will be pre-pended with `update:` before requesting the payload.

`goodbids()->auctioneer->auctions->end( int $auction_id )`
Signals to the Auctioneer API that the Auction has ended.
This documentation has been moved to the [root docs directory](../../docs/api/index.md).
8 changes: 5 additions & 3 deletions client-mu-plugins/goodbids/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
],
"vip-constants": {
"auctioneer": {
"local": "GOODBIDS_AUCTIONEER_URL_LOCAL",
"develop": "GOODBIDS_AUCTIONEER_URL_DEVELOP",
"production": "GOODBIDS_AUCTIONEER_URL_PRODUCTION"
"urls": {
"local": "GOODBIDS_AUCTIONEER_URL_LOCAL",
"develop": "GOODBIDS_AUCTIONEER_URL_DEVELOP",
"production": "GOODBIDS_AUCTIONEER_URL_PRODUCTION"
}
}
}
}
25 changes: 20 additions & 5 deletions client-mu-plugins/goodbids/src/classes/Auctioneer/Auctioneer.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,22 @@ class Auctioneer {
* @since 1.0.0
*/
public function __construct() {
$environments = goodbids()->get_config( 'vip-constants.auctioneer' );
if ( ! $this->configure_url() ) {
return;
}

$this->init();
}

/**
* Set up the URL to use for the API.
*
* @since 1.0.0
*
* @return bool
*/
private function configure_url(): bool {
$environments = goodbids()->get_config( 'vip-constants.auctioneer.urls' );

if ( ! $environments || empty( $environments[ $this->environment ] ) ) {
add_action(
Expand All @@ -67,12 +82,12 @@ function() {
'<div class="notice notice-error is-dismissible">
<p>%s</p>
</div>',
esc_html__( 'Missing Auctioneer constants config.', 'goodbids' )
esc_html__( 'Missing Auctioneer URL constants config.', 'goodbids' )
);
}
);

return;
return false;
}

$this->url = vip_get_env_var( $environments[ $this->environment ], null );
Expand All @@ -91,10 +106,10 @@ function() {
}
);

return;
return false;
}

$this->init();
return true;
}

/**
Expand Down
32 changes: 27 additions & 5 deletions client-mu-plugins/goodbids/src/classes/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,13 @@ private function load_config(): bool {
}

/**
* Get a config value.
* Get a config value. You can use dot notation to get nested values.
*
* @param string $key Config Key.
*
* @return mixed
*/
public function get_config( string $key ): mixed {
if ( empty( $this->config[ $key ] ) ) {
return null;
}

if ( str_contains( $key, '.' ) ) {
$keys = explode( '.', $key );
$value = $this->config;
Expand Down Expand Up @@ -208,6 +204,17 @@ private function load_dependencies(): void {
new Vite();
}

/**
* Checks if current environment is development.
*
* @since 1.0.0
*
* @return bool
*/
public function is_dev_env(): bool {
return defined( 'VIP_GO_APP_ENVIRONMENT' ) && 'local' === VIP_GO_APP_ENVIRONMENT;
}

/**
* Load 3rd Party Plugins.
*
Expand All @@ -227,6 +234,21 @@ private function load_plugins(): void {
}

foreach ( $plugins as $plugin ) {
if ( $this->is_dev_env() ) {
$plugin_slug = str_contains( $plugin, '/' ) ? $plugin : $plugin . '/' . $plugin . '.php';
$plugin_path = WP_PLUGIN_DIR . '/' . $plugin_slug;
if ( ! file_exists( $plugin_path ) ) {
error_log(
sprintf(
'[GB] Attempted to load plugin %s but the file %s was not found in the plugin directory.',
$plugin,
$plugin_slug
)
);
continue;
}
}

wpcom_vip_load_plugin( $plugin );
}
}
Expand Down
17 changes: 3 additions & 14 deletions client-mu-plugins/goodbids/src/classes/Frontend/Vite.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,6 @@ function () {
add_action( 'acf/input/admin_footer', [ $this, 'acf_color_palette' ] );
}

/**
* Checks if current environment is development.
*
* @since 1.0.0
*
* @return bool
*/
public function is_dev_env(): bool {
return defined( 'VIP_GO_APP_ENVIRONMENT' ) && 'local' === VIP_GO_APP_ENVIRONMENT;
}

/**
* Initialize
*
Expand Down Expand Up @@ -180,8 +169,8 @@ public function get_entry( string $entry = '' ): string|false {
*
* @return string
*/
public function vite( string $entry ) {
if ( $this->is_dev_env() ) {
public function vite( string $entry ): string {
if ( goodbids()->is_dev_env() ) {
$scripts = [
"<script type=\"module\" src=\"{$this->dev_server}/@vite/client\"></script>",
"<script type=\"module\" src=\"{$this->dev_server}/{$entry}\"></script>",
Expand Down Expand Up @@ -479,7 +468,7 @@ public function block_assets( $entry = '' ): void {

$script_url = $this->get_asset_url( $file );

if ( $this->is_dev_env() ) {
if ( goodbids()->is_dev_env() ) {
$vite_client = $this->dev_server . '/@vite/client';
$vite_entry = $this->dev_server . '/' . $entry;

Expand Down
Loading

0 comments on commit 15e4a8b

Please sign in to comment.