Skip to content
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
5 changes: 0 additions & 5 deletions .changeset/six-boats-exercise.md

This file was deleted.

7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# WPGraphQL Content Blocks

## 4.8.3

### Patch Changes

- bf77481: Updated plugin test suite and readme for WordPress 6.8
- bb3631c: Adds WPGraphQL version compatibility headers and checks

## 4.8.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@wpengine/wp-graphql-content-blocks",
"private": true,
"version": "4.8.2",
"version": "4.8.3",
"engines": {
"node": ">=16.0.0"
},
Expand Down
163 changes: 7 additions & 156 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: blakewpe, chriswiegman, joefusco, matthewguywright, TeresaGobble,
Tags: faustjs, faust, headless, decoupled, gutenberg
Requires at least: 5.7
Tested up to: 6.8
Stable tag: 4.8.2
Stable tag: 4.8.3
Requires PHP: 7.4
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Expand All @@ -26,6 +26,12 @@ Extends WPGraphQL to support querying (Gutenberg) Blocks as data.

== Changelog ==

= 4.8.3 =

### Patch Changes

- bf77481: Updated plugin test suite and readme for WordPress 6.8

= 4.8.2 =

### Patch Changes
Expand All @@ -38,159 +44,4 @@ Extends WPGraphQL to support querying (Gutenberg) Blocks as data.

- 84a65bb: bug: Fixes fatal error when you de-activate WPGraphQL

= 4.8.0 =

### Minor Changes

- 742f18a: # Querying Object-Type Block Attributes in WPGraphQL

## Overview

With this update, you can now query object-type block attributes with each property individually, provided that the **typed structure** is defined in the class `typed_object_attributes` property or through a **WordPress filter**.

## How It Works

The `typed_object_attributes` is a **filterable** array that defines the expected **typed structure** for object-type block attributes.

- The **keys** in `typed_object_attributes` correspond to **object attribute names** in the block.
- Each value is an **associative array**, where:
- The key represents the **property name** inside the object.
- The value defines the **WPGraphQL type** (e.g., `string`, `integer`, `object`, etc.).
- If a block attribute has a specified **typed structure**, only the properties listed within it will be processed.

## Defining Typed Object Attributes

Typed object attributes can be **defined in two ways**:

### 1. In a Child Class (`typed_object_attributes` property)

Developers can extend the `Block` class and specify **typed properties** directly:

```php
class CustomMovieBlock extends Block {
/**
* {@inheritDoc}
*
* @var array<string, array<string, "array"|"boolean"|"number"|"integer"|"object"|"rich-text"|"string">>
*/
protected array $typed_object_attributes = [
'film' => [
'id' => 'integer',
'title' => 'string',
'director' => 'string',
'soundtrack' => 'object',
],
'soundtrack' => [
'title' => 'string',
'artist' => 'string'
],
];
}
```

### 2. Via WordPress Filter

You can also define **typed structures dynamically** using a WordPress filter.

```php
add_filter(
'wpgraphql_content_blocks_object_typing_my-custom-plugin_movie-block',
function () {
return [
'film' => [
'id' => 'integer',
'title' => 'string',
'director' => 'string',
'soundtrack' => 'object',
],
'soundtrack' => [
'title' => 'string',
'artist' => 'string'
],
];
}
);
```

## Filter Naming Convention

To apply custom typing via a filter, use the following format:

```
wpgraphql_content_blocks_object_typing_{block-name}
```

- Replace `/` in the block name with `-`.
- Example:
- **Block name**: `my-custom-plugin/movie-block`
- **Filter name**: `wpgraphql_content_blocks_object_typing_my-custom-plugin_movie-block`

## Example:

### Example `block.json` Definition

If the block has attributes defined as **objects**, like this:

```json
"attributes": {
"film": {
"type": "object",
"default": {
"id": 1,
"title": "The Matrix",
"director": "Director Name"
}
},
"soundtrack": {
"type": "object",
"default": {
"title": "The Matrix Revolutions...",
"artist": "Artist Name"
}
}
}
```

This means:

- The `film` attribute contains `id`, `title`, `director`.
- The `soundtrack` attribute contains `title` and `artist`.

## WPGraphQL Query Example

Once the typed object attributes are **defined**, you can query them **individually** in WPGraphQL.

```graphql
fragment Movie on MyCustomPluginMovieBlock {
attributes {
film {
id
title
director
soundtrack {
title
}
}
soundtrack {
title
artist
}
}
}

query GetAllPostsWhichSupportBlockEditor {
posts {
edges {
node {
editorBlocks {
__typename
name
...Movie
}
}
}
}
}
```

[View the full changelog](https://github.com/wpengine/wp-graphql-content-blocks/blob/main/CHANGELOG.md)
4 changes: 2 additions & 2 deletions wp-graphql-content-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-graphql-content-blocks
* Domain Path: /languages
* Version: 4.8.2
* Version: 4.8.3
* Requires PHP: 7.4
* Requires at least: 5.7
* Requires Plugins: wp-graphql
Expand Down Expand Up @@ -47,7 +47,7 @@ function wpgraphql_content_blocks_constants(): void {
}

if ( ! defined( 'WPGRAPHQL_CONTENT_BLOCKS_VERSION' ) ) {
define( 'WPGRAPHQL_CONTENT_BLOCKS_VERSION', '4.8.2' );
define( 'WPGRAPHQL_CONTENT_BLOCKS_VERSION', '4.8.3' );
}

if ( ! defined( 'WPGRAPHQL_CONTENT_BLOCKS_PLUGIN_URL' ) ) {
Expand Down
Loading