Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up HTTP examples #302

Merged
merged 12 commits into from
Jan 19, 2025
4 changes: 3 additions & 1 deletion example/rest-api/art-institute/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Example: "Art Institute of Chicago" block

This example registers a remote data block representing an artwork from the [Art Institute of Chicago's public API](http://api.artic.edu/docs/).
This example plugin registers a remote data block representing an artwork from the [Art Institute of Chicago's public API](http://api.artic.edu/docs/). The source is defined in the plugin code directly, as opposed to the in settings screen like the Zip Code example.

To enable it, simply copy the art-institute.php file to your plugins folder and activate it. This will expose a new custom block called `Art Institute of Chicago`
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
<?php declare(strict_types = 1);

/**
* Plugin Name: Art Institute RDB Example
* Description: Creates a custom block to be used with Remote Data Blocks in order to retrieve artwork from the Art Institute of Chicago.
* Author: WPVIP
* Author URI: https://remotedatablocks.com/
* Text Domain: remote-data-blocks
* Version: 1.0.0
* Requires Plugins: remote-data-blocks
*/

namespace RemoteDataBlocks\Example\ArtInstituteOfChicago;

use RemoteDataBlocks\Config\DataSource\HttpDataSource;
use RemoteDataBlocks\Config\Query\HttpQuery;
use function add_query_arg;

function register_aic_block(): void {
$aic_data_source = HttpDataSource::from_array( [
$aic_data_source = HttpDataSource::from_array([
'service_config' => [
'__version' => 1,
'display_name' => 'Art Institute of Chicago',
Expand All @@ -16,9 +26,9 @@ function register_aic_block(): void {
'Content-Type' => 'application/json',
],
],
] );
]);

$get_art_query = HttpQuery::from_array( [
$get_art_query = HttpQuery::from_array([
'data_source' => $aic_data_source,
'endpoint' => function ( array $input_variables ) use ( $aic_data_source ): string {
return sprintf( '%s/%s', $aic_data_source->get_endpoint(), $input_variables['id'] ?? '' );
Expand Down Expand Up @@ -54,9 +64,9 @@ function register_aic_block(): void {
],
],
],
] );
]);

$search_art_query = HttpQuery::from_array( [
$search_art_query = HttpQuery::from_array([
'data_source' => $aic_data_source,
'endpoint' => function ( array $input_variables ) use ( $aic_data_source ): string {
$query = $input_variables['search_terms'];
Expand Down Expand Up @@ -84,9 +94,9 @@ function register_aic_block(): void {
],
],
],
] );
]);

register_remote_data_block( [
register_remote_data_block([
'title' => 'Art Institute of Chicago',
'render_query' => [
'query' => $get_art_query,
Expand All @@ -97,6 +107,6 @@ function register_aic_block(): void {
'type' => 'search',
],
],
] );
]);
}
add_action( 'init', __NAMESPACE__ . '\\register_aic_block' );
4 changes: 3 additions & 1 deletion example/rest-api/zip-code/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Example: "Zip Code" block

This example registers a remote data block representing a Zip code. The block fetches data from the [Zippopotam.us API](http://www.zippopotam.us/).
This example plugin registers a remote data block to represent the city and state from a US ZIP code. The block fetches data from the [Zippopotam.us API](http://www.zippopotam.us/).

When working with REST APIs that do not have a first-class integration (like Airtable, Google Sheets, Shopify, et al.), a common approach is to define a data source on the settings screen and then commit a custom query in code to fetch and process the data.

This example illustrates this approach, and assumes you have configured the data source in the UI and have provided the UUID via the `EXAMPLE_ZIP_CODE_DATA_SOURCE_UUID` constant.

To enable it, simply copy the zipcode.php file to your plugins folder, replace the UUID, and activate it. This will expose a new custom block called `Zip Code'
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
<?php declare(strict_types = 1);

/**
* Plugin Name: Zip Code RDB Example
* Description: Creates a custom block to be used with Remote Data Blocks in order to retrieve the city and state from a US zip code.
* Author: WPVIP
* Author URI: https://remotedatablocks.com/
* Text Domain: remote-data-blocks
* Version: 1.0.0
* Requires Plugins: remote-data-blocks
*/

namespace RemoteDataBlocks\Example\ZipCode;

use RemoteDataBlocks\Config\DataSource\HttpDataSource;
use RemoteDataBlocks\Config\Query\HttpQuery;

define( 'EXAMPLE_ZIP_CODE_DATA_SOURCE_UUID', 'xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxxx' );

function register_zipcode_block(): void {
if ( ! defined( 'EXAMPLE_ZIP_CODE_DATA_SOURCE_UUID' ) ) {
if ( !defined( 'EXAMPLE_ZIP_CODE_DATA_SOURCE_UUID' ) ) {
return;
}

$uuid = constant( EXAMPLE_ZIP_CODE_DATA_SOURCE_UUID );
$zipcode_data_source = HttpDataSource::from_uuid( $uuid );
$zipcode_data_source = HttpDataSource::from_uuid( EXAMPLE_ZIP_CODE_DATA_SOURCE_UUID );

if ( ! $zipcode_data_source instanceof HttpDataSource ) {
if ( !$zipcode_data_source instanceof HttpDataSource ) {
return;
}

$zipcode_query = HttpQuery::from_array( [
$zipcode_query = HttpQuery::from_array([
'data_source' => $zipcode_data_source,
'endpoint' => function ( array $input_variables ) use ( $zipcode_data_source ): string {
return $zipcode_data_source->get_endpoint() . $input_variables['zip_code'];
Expand Down Expand Up @@ -48,13 +59,13 @@ function register_zipcode_block(): void {
],
],
],
] );
]);

register_remote_data_block( [
register_remote_data_block([
'title' => 'Zip Code',
'render_query' => [
'query' => $zipcode_query,
],
] );
]);
}
add_action( 'init', __NAMESPACE__ . '\\register_zipcode_block' );
2 changes: 2 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@
<!-- Exclude PSR12.Files.FileHeader.IncorrectOrder for specific files -->
<rule ref="PSR12.Files.FileHeader.IncorrectOrder">
<exclude-pattern>remote-data-blocks.php</exclude-pattern>
<exclude-pattern>zip-code.php</exclude-pattern>
<exclude-pattern>art-institute.php</exclude-pattern>
<exclude-pattern>functions.php</exclude-pattern>
</rule>

Expand Down
Loading