-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
912831c
commit 0f1c17b
Showing
21 changed files
with
452 additions
and
537 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
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
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 |
---|---|---|
@@ -1,14 +1,47 @@ | ||
# Block registration | ||
|
||
Use the `register_remote_data_block` function to register your block and associate it with your query and data source. | ||
Use the `register_remote_data_block` function to register your block and associate it with your query and data source. This example: | ||
|
||
1. Creates a data source | ||
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. | ||
|
||
```php | ||
function register_your_custom_block() { | ||
$block_name = 'Your Custom Block'; | ||
$your_data_source = new YourCustomDataSource(); | ||
$your_query = new YourCustomQuery( $your_data_source ); | ||
$data_source = HttpDataSource::from_array( [ | ||
'service_config' => [ | ||
'__version' => 1, | ||
'display_name' => 'Example API', | ||
'endpoint' => 'https://api.example.com/', | ||
], | ||
] ); | ||
|
||
$display_query = HttpQuery::from_array( [ | ||
'display_name' => 'Example Query', | ||
'data_source' => $data_source, | ||
'output_schema' => [ | ||
'type' => [ | ||
'id => [ | ||
'name' => 'ID', | ||
'path' => '$.id', | ||
'type' => 'id', | ||
], | ||
'title' => [ | ||
'name' => 'Title', | ||
'path' => '$.title', | ||
'type' => 'string', | ||
], | ||
], | ||
], | ||
] ); | ||
|
||
register_remote_data_block( $block_name, $your_query ); | ||
register_remote_data_block( [ | ||
'title' => 'My Block', | ||
'queries' => [ | ||
'display' => $display_query, | ||
], | ||
] ); | ||
} | ||
add_action( 'init', 'YourNamespace\\register_your_custom_block', 10, 0 ); | ||
``` |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,35 @@ | ||
# Query runner | ||
|
||
A query runner executes a query and processes the results. The default `QueryRunner` used by the [`HttpQueryContext` class](query.md#HttpQueryContext) is designed to work with most APIs that transact over HTTP and return JSON, but you may want to provide a custom query runner if: | ||
A query runner executes a query and processes the results. The default `QueryRunner` used by the [`HttpQuery` class](query.md) is designed to work with most APIs that transact over HTTP and return JSON, but you may want to provide a custom query runner if: | ||
|
||
- Your API does not respond with JSON or requires custom deserialization logic. | ||
- Your API uses a non-HTTP transport. | ||
- You want to implement custom processing of the response data that is not possible with the provided filters. | ||
|
||
## QueryRunner | ||
## Custom QueryRunner for HTTP queries | ||
|
||
If your API transacts over HTTP and you want to customize the query runner, consider extending the `QueryRunner` class and overriding select methods. | ||
If your API transacts over HTTP and you want to customize the query runner, consider extending the `QueryRunner` class and providing an instance to your query via the `query_runner` option. Here are the methods | ||
|
||
### execute( array $input_variables ): array|WP_Error | ||
### execute( HttpQueryInterface $query, array $input_variables ): array|WP_Error | ||
|
||
The `execute` method executes the query and returns the parsed data. The input variables for the current request are provided as an associative array (`[ $var_name => $value ]`). | ||
|
||
### get_request_details( array $input_variables ): array|WP_Error | ||
### deserialize_response( string $raw_response_data, array $input_variables ): mixed | ||
|
||
By default, the `deserialize_response` assumes a JSON string and deserializes it using `json_decode`. Override this method to provide custom deserialization logic. | ||
|
||
### get_request_details( HttpQueryInterface $query, array $input_variables ): array|WP_Error | ||
|
||
The `get_request_details` method extracts and validates the request details provided by the query. The input variables for the current request are provided as an associative array (`[ $var_name => $value ]`). The return value is an associative array that provides the HTTP method, request options, origin, and URI. | ||
|
||
### get_raw_response_data( array $input_variables ): array|WP_Error | ||
### get_raw_response_data( HttpQueryInterface $query, array $input_variables ): array|WP_Error | ||
|
||
The `get_raw_response_data` method dispatches the HTTP request and assembles the raw (pre-processed) response data. The input variables for the current request are provided as an associative array (`[ $var_name => $value ]`). The return value is an associative array that provides the response metadata and the raw response data. | ||
|
||
### get_response_metadata( array $response_metadata, array $query_results ): array | ||
### get_response_metadata( HttpQueryInterface $query, array $response_metadata, array $query_results ): array | ||
|
||
The `get_response_metadata` method returns the response metadata for the query, which are available as bindings for [field shortcodes](field-shortcodes.md). | ||
|
||
### map_fields( string|array|object|null $response_data, bool $is_collection ): ?array | ||
|
||
The `map_fields` method maps fields from the API response data, adhering to the output schema defined by the query. | ||
|
||
### get_field_value( array|string $field_value, string $default_value = '', string $field_type = 'string' ): string | ||
|
||
The `get_field_value` method computes the field value based on the field type. Overriding this method can be useful if you have custom field types and want to format the value in a specific way (e.g., a custom date format). | ||
## Custom query execution | ||
|
||
## QueryRunnerInterface | ||
|
||
If you want to implement a query runner from scratch, `QueryRunnerInterface` requires only a single method, `execute`: | ||
|
||
### execute( array $input_variables ): array | ||
|
||
The `execute` method executes the query and returns the parsed data. The input variables for the current request are provided as an associative array (`[ $var_name => $value ]`). | ||
If your API uses a non-HTTP transport or you want full control over query execution, you should implement your own query that implements `QueryInterface` and provides a custom `execute` method. |
Oops, something went wrong.