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

Update/documentation/extending last looks #320

Merged
merged 29 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6b0590e
changing example.
smithjw1 Jan 22, 2025
5381a18
adding overrides detail
smithjw1 Jan 22, 2025
614fe36
adding link
smithjw1 Jan 22, 2025
214c601
adding direct playground link
smithjw1 Jan 22, 2025
8c42560
review changes
smithjw1 Jan 22, 2025
0de46f0
grammar and spelling
smithjw1 Jan 22, 2025
9148736
grammar and spelling
smithjw1 Jan 22, 2025
36bf825
grammar and spelling
smithjw1 Jan 22, 2025
792f268
switching title case
smithjw1 Jan 25, 2025
03a8b62
revising block patterns
smithjw1 Jan 25, 2025
949bc66
formatting changes
smithjw1 Jan 25, 2025
96b6942
removing backtic
smithjw1 Jan 25, 2025
ace812a
Update docs/extending/data-source.md
smithjw1 Jan 25, 2025
ac8944f
Merge branch 'update/documentation/extending-last-looks' of github.co…
smithjw1 Jan 25, 2025
e9fafc1
fixing formatting and error
smithjw1 Jan 25, 2025
3b7c2d3
Update docs/extending/overrides.md
smithjw1 Jan 25, 2025
de3f736
Update docs/extending/overrides.md
smithjw1 Jan 25, 2025
73fc803
wording update
smithjw1 Jan 25, 2025
e40cace
Update docs/extending/overrides.md
smithjw1 Jan 25, 2025
4e09a75
Update docs/extending/overrides.md
smithjw1 Jan 25, 2025
512fe66
Update docs/extending/overrides.md
smithjw1 Jan 25, 2025
e001389
Update docs/extending/query-output_schema.md
smithjw1 Jan 25, 2025
7c8a7da
Update docs/extending/query-output_schema.md
smithjw1 Jan 25, 2025
fc79221
Update docs/extending/query.md
smithjw1 Jan 25, 2025
6672b22
Merge branch 'update/documentation/extending-last-looks' of github.co…
smithjw1 Jan 25, 2025
2a0722d
Update data source example with Jacob's suggestion
chriszarate Jan 27, 2025
ce272e0
Formatting updates
chriszarate Jan 27, 2025
a781b4c
Merge remote-tracking branch 'origin/trunk' into update/documentation…
chriszarate Jan 27, 2025
5a5f69a
Merge branch 'trunk' into update/documentation/extending-last-looks
chriszarate Jan 27, 2025
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
24 changes: 20 additions & 4 deletions docs/extending/data-source.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,29 @@ $zipcode_query = HttpQuery::from_array( [
])
```

In this case the `data_source` has a built in `get_endpoint()` method. You could also retrieve the endpoint this way:
In this case the `data_source` has a built in `get_endpoint()` method. Other configuration options can be retireved directly. If you had a Data Source config like:
smithjw1 marked this conversation as resolved.
Show resolved Hide resolved

```php
$comp_data_source = HttpDataSource::from_array( [
smithjw1 marked this conversation as resolved.
Show resolved Hide resolved
'service_config' => [
'__version' => 1,
'display_name' => 'More Complicated Example API',
'endpoint' => 'https://api.complexexample.com/',
'some_identifier' => 'table-2',
chriszarate marked this conversation as resolved.
Show resolved Hide resolved
'request_headers' => [
'Content-Type' => 'application/json',
'X-Api-Key' => MY_API_KEY_CONSTANT,
],
],
] );
```
You could retrieve `some_identifier` directly:

```php
$zipcode_query = HttpQuery::from_array( [
'data_source' => $zipcode_data_source,
'endpoint' => function ( array $input_variables ) use ( $zipcode_data_source ): string {
return $zipcode_data_source->to_array()['service_config']['endpoint'] . $input_variables['zip_code'];
'data_source' => $comp_data_source,
'endpoint' => function ( array $input_variables ) use ( $comp_data_source ): string {
return return $comp_data_source->get_endpoint() . $zipcode_data_source->to_array()['service_config']['some_identifier'] . "/" . $input_variables['search'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mentioned on the other PR, but I don't think we should promote this pattern. It constrains us in future changes and this alternate approach is unnecessary. They can just hold a reference to some_identifier in their own code and pass it in via use(). This should definitely not be idiomatic.

Copy link
Contributor Author

@smithjw1 smithjw1 Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I look at the Airtable Data Source we add a base property to the data source definition, that's was my inspiration.

I think your suggesting that additional configuration should happen outside of service_config, so something like:

$data_source = HttpDataSource::from_array( [
    'service_config' => [
        '__version' => 1,
        'display_name' => 'More Complicated Example API',
        'endpoint' => 'https://api.complexexample.com/',
        'request_headers' => [
            'Content-Type' => 'application/json',
            'X-Api-Key' => MY_API_KEY_CONSTANT,
        ],
    ],
] );

$my_config = [
 'some_identifier' => 'id-123'
]

$zipcode_query = HttpQuery::from_array( [
    'data_source' => $data_source,
    'endpoint' => function ( array $input_variables ) use ( $data_source, $my_config ): string {
        return  return $data_source->get_endpoint() . $my_config['some_identifier'] . "/" . $input_variables['search'];
    },
])

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I applied your suggestion in a781b4c

},
])
```
Expand Down
8 changes: 6 additions & 2 deletions docs/extending/overrides.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Overrides

Overrides provide a way to customize the behavior of remote data blocks on a per-block basis. You can use them to modify the underlying query input variables, adjust the query response, or change the caching behavior. Overrides are defined when you register a remote data block and can be enabled or disabled via the block settings in the editor.
Overrides provide a way to customize the behavior of remote data blocks on a per-block basis. You can use them to modify the underlying query input variables, adjust the query response, or change the caching behavior. Overrides are defined when you register a remote data block and can be enabled or disabled via the block settings in the WordPress editor.

The actual implementation of overrides is left to you. Here is an example of an override that modifies the query input variables based on a query variable (supplied by a rewrite rule):
If you have multiple instnaces of the same remote data block in a piece of content, each instance can have a different number of overrides enabled. The default behavior is no overrides.
smithjw1 marked this conversation as resolved.
Show resolved Hide resolved

The actual implementation of overrides is left to you. Here is an example of an override that modifies the query input variables based on a URL query string variable. You would use this to build a one "single product page" in the WordPress admin that would be able to display any product, based on the product id. The example takes advantage of the [`add_rewrite_rule`](https://developer.wordpress.org/reference/functions/add_rewrite_rule/) method and [`query_vars`](https://developer.wordpress.org/reference/hooks/query_vars/) filter that are built into WordPress.
smithjw1 marked this conversation as resolved.
Show resolved Hide resolved

```php
register_remote_data_block( [
Expand Down Expand Up @@ -39,6 +41,8 @@ add_filter( 'remote_data_blocks_query_input_variables', function ( array $input_
}, 10, 2 );
```

As you can see the `remote_data_blocks_query_input_variables` filter is passed a list of enabled overrides. You need to add logic to identify which filters are enabled and act accordingly.
smithjw1 marked this conversation as resolved.
Show resolved Hide resolved

The `overrides` property in the block registration array enables a panel in the block settings that allows content authors to enable or disable the override:

<img width="276" alt="An overrides panel in a remote data block settings panel" src="https://github.com/user-attachments/assets/e701e621-99f9-4c2e-b34d-cfef352af2ae" />
2 changes: 1 addition & 1 deletion docs/extending/query-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A query runner executes a query and processes the results of a query. The defaul

- 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, which is not possible with the provided filters.
- You want to implement custom processing of the response data, which is not possible with the [provided filters](./hooks.md).

## Custom QueryRunner for HTTP queries

Expand Down
4 changes: 3 additions & 1 deletion docs/quickstart.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Quickstart

The most basic way to see Remote Data Blocks in action is to launch the plugin in WordPress Playground. You can do this by clicking the "Launch in WordPress Playground" badge in the [README](../README.md).
The most basic way to see Remote Data Blocks in action is to launch the plugin in WordPress Playground.
smithjw1 marked this conversation as resolved.
Show resolved Hide resolved

[![Launch in WordPress Playground](https://img.shields.io/badge/Launch%20in%20WordPress%20Playground-DA9A45?style=for-the-badge&logo=wordpress)](https://playground.wordpress.net/?blueprint-url=https://raw.githubusercontent.com/Automattic/remote-data-blocks/trunk/blueprint.json)

However, the more advanced use case is providing your own custom configuration through code. This guide will walk you through the steps to do this.

Expand Down
Loading