Skip to content

Improve examples, introduce templates, and default to array config #569

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

Merged
merged 9 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build
example/templates/airtable-map-block/src
node_modules
vendor
*.php
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ coverage
# Build output (only at root)
/build/

# Private examples
/examples/private

# Testing
/artifacts/
/test-results/
Expand Down
2 changes: 1 addition & 1 deletion docs/concepts/inline-bindings.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Inline bindings

One of the current limitations of the [block bindings API](./block-bindings.md) is that it is restricted to a small number of core blocks and attributes. For example, currently, you cannot bind to the content of a table block or a custom block. You also cannot bind to a _subset_ of a block's content.
One of the current limitations of the [block bindings API](block-bindings.md) is that it is restricted to a small number of core blocks and attributes. For example, currently, you cannot bind to the content of a table block or a custom block. You also cannot bind to a _subset_ of a block's content.

As a partial workaround, this plugin provides a way to use remote data in some places where block bindings are not supported. This feature is named "inline bindings" and it is available in any block that uses [rich text](https://developer.wordpress.org/block-editor/reference-guides/richtext/), such as tables, lists, and some custom blocks. Look for the inline binding button in the rich text formatting toolbar:

Expand Down
26 changes: 13 additions & 13 deletions docs/extending/block-registration.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Block registration

Use the `register_remote_data_block` function to register your block and associate it with your query and data source. This example:
Use the `register_remote_data_block` function to register your remote data block and associate it with your query and data source. This example:

1. Creates a data source.
1. Creates a [data source](data-source.md).
2. Associates the data source with a query.
3. Defines the output schema of a query, which tells the plugin how to map the query response to blocks.
4. Registers a remote data block.
Expand All @@ -18,12 +18,12 @@ We are assuming `https://api.example.com/` returns JSON that has a shape like:

```php
function register_your_custom_block() {
$data_source = HttpDataSource::from_array( [
$data_source = [
'display_name' => 'Example API',
'endpoint' => 'https://api.example.com/',
] );
];

$render_query = HttpQuery::from_array( [
$render_query = [
'display_name' => 'Example Query',
'data_source' => $data_source,
'output_schema' => [
Expand All @@ -40,7 +40,7 @@ function register_your_custom_block() {
],
],
],
] );
];

register_remote_data_block( [
'title' => 'My Block',
Expand All @@ -49,7 +49,7 @@ function register_your_custom_block() {
],
] );
}
add_action( 'init', 'YourNamespace\\register_your_custom_block', 10, 0 );
add_action( 'init', 'register_your_custom_block', 10, 0 );
```

## Configuration options
Expand Down Expand Up @@ -91,10 +91,10 @@ Example:

#### Search queries

Search queries must return a collection and must accept one input variable with the special type `ui:search_input`. The [Art Institute of Chicago](https://github.com/Automattic/remote-data-blocks/blob/trunk/example/rest-api/art-institute/README.md) example looks like this:
Search queries must return a collection and must accept an input variable with the special type `ui:search_input`. The [Art Institute of Chicago](https://github.com/Automattic/remote-data-blocks/blob/trunk/example/rest-api/art-institute/README.md) example looks like this:

```php
$search_art_query = HttpQuery::from_array([
$search_art_query = [
'data_source' => $aic_data_source,
'endpoint' => function ( array $input_variables ) use ( $aic_data_source ): string {
$query = $input_variables['search'];
Expand Down Expand Up @@ -122,19 +122,19 @@ $search_art_query = HttpQuery::from_array([
],
],
],
]);
];
```

Here you can see the `search` input variable has a special type of `ui:search_input` and is used in the endpoint method to populate a query string. You can read more about [queries](./query.md) and how to construct them. End users enter the search term to find the specific item.

![Screenshot showing the search inputin the WordPress Editor](https://raw.githubusercontent.com/Automattic/remote-data-blocks/trunk/docs/extending/search-input.png)
![Screenshot showing the search input in the WordPress Editor](https://raw.githubusercontent.com/Automattic/remote-data-blocks/trunk/docs/extending/search-input.png)

**Note:** The same search box appears for `list` query types. For this type, the form is only filtering the results returned by the initial list query. For `search` queries, an additional query is made for every search.

### `overrides`: array (optional)

[Overrides](./overrides.md) are used to customize the behavior of the block on a per-block basis.
[Overrides](overrides.md) are used to customize the behavior of the block on a per-block basis.

### `patterns`: array (optional)

[Block patterns](./block-patterns.md) allow you to customize the display of your remote data.
[Block patterns](block-patterns.md) allow you to customize the display of your remote data.
4 changes: 3 additions & 1 deletion docs/extending/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ Once you've defined your data source and queries, you can [register a remote dat
- [Hooks (actions and filters)](hooks.md)
- [Query runner](query-runner.md)

## Examples
## Examples and Templates

The [examples](https://github.com/Automattic/remote-data-blocks/blob/trunk/example/README.md) provide detailed code samples of interacting with the plugin various methods of extending the plugin.

For quick development, use the [templates](https://github.com/Automattic/remote-data-blocks/blob/trunk/example/templates/README.md) and [AI prompts](../ai-prompts.md) to scaffold new integrations rapidly.

## Create a local development environment

This repository includes tools for quickly starting a [local development environment](../local-development.md).
2 changes: 1 addition & 1 deletion docs/extending/query-input-schema.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Query `input_schema` property
# HttpQuery `input_schema` property

The `input_schema` property defines the input variables expected by the query. The property should be an associative array of input variable definitions. The keys of the array are machine-friendly input variable names, and the values are associative arrays with the following structure:

Expand Down
2 changes: 1 addition & 1 deletion docs/extending/query-output-schema.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Query `output_schema` property
# HttpQuery `output_schema` property

A query's `output_schema` defines how an API response should be transformed and provided to a remote data block. A typical goal is to transform the API response into a flat array of fields that can be bound to blocks, while omitting values that are not needed. Output can be nested, but nested values cannot be bound to blocks.

Expand Down
12 changes: 8 additions & 4 deletions docs/extending/query.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A common approach is to define a data source on the settings screen and then com

## HttpQuery

Most HTTP-powered APIs can be queried using an `HttpQuery`. Here's an example of a query for US ZIP code data. This examples assumes you have configured the data source in the UI, and have the UUID.
Most HTTP-powered APIs can be queried using `HttpQuery`. Queries are instantiated with configuration options, described below.

```php
if ( ! defined( 'REMOTE_DATA_BLOCKS_EXAMPLE_ZIP_CODE_DATA_SOURCE_UUID' ) ) {
Expand All @@ -19,7 +19,7 @@ if ( ! $data_source instanceof HttpDataSource ) {
return;
}

$query = HttpQuery::from_array( [
$query = [
'display_name' => 'Get location by Zip code',
'data_source' => $data_source,
'endpoint' => function( array $input_variables ) use ( $data_source ): string {
Expand Down Expand Up @@ -51,7 +51,7 @@ $query = HttpQuery::from_array( [
],
],
],
] );
];
```

- The `endpoint` property is a callback function that constructs the query endpoint. In this case, the endpoint is constructed by appending the `zip_code` input variable to the data source endpoint.
Expand Down Expand Up @@ -84,7 +84,7 @@ The `endpoint` property defines the query endpoint. It can be a string or a call

### input_schema: array

The `input_schema` property defines the input variables expected by the query. Further information and examples are provided in the [`input_schema` documentation](./query-input-schema.md).
The `input_schema` property defines the input variables expected by the query, which can be used to formulate the endpoint, the request headers, or the request body. Further specification and examples are provided in the [`input_schema` documentation](./query-input-schema.md).

### output_schema: array (required)

Expand Down Expand Up @@ -221,3 +221,7 @@ If you need to pre-process the response in some way before the output variables
### query_runner: QueryRunnerInterface

Use the `query_runner` property to provide a custom [query runner](./query-runner.md) for the query. If omitted, the query will use the default query runner, which works well with most HTTP-powered APIs.

## GraphqlQuery

## GraphqlMutation
Loading
Loading