Skip to content
Open
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file, per [the Ke
### Added

- Read-only Abilities API abilities `content-connect/get-relationships`, `content-connect/get-post-relationships`, and `content-connect/get-related-items`, exposed over REST and MCP, registered on WordPress 6.9+ only (props [@s3rgiosan](https://github.com/s3rgiosan)).
- Read-only ability `content-connect/get-related-query-block` that returns Query Loop markup listing a post's related posts, so agents display related content with a live query instead of a hand-picked list (props [@fabiankaegy](https://github.com/fabiankaegy)).

## [2.0.0] - TBD

Expand Down
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

## Overview

WP Content Connect defines and manages post-to-post and post-to-user relationships. Relationships are edited in both the **Block Editor** (a relationships panel in the document sidebar) and the **Classic Editor** (relationship meta boxes), and are exposed through a REST API and a set of PHP helper functions. See [REST API](#rest-api), [Helper Functions](#helper-functions), and [Customizing the Block Editor UI](#customizing-the-block-editor-ui).
WP Content Connect defines and manages post-to-post and post-to-user relationships. Relationships are edited in both the **Block Editor** (a relationships panel in the document sidebar) and the **Classic Editor** (relationship meta boxes), and are exposed through a REST API and a set of PHP helper functions. See [REST API](#rest-api), [Abilities](#abilities), [Helper Functions](#helper-functions), and [Customizing the Block Editor UI](#customizing-the-block-editor-ui).

## Installation and Usage

Expand Down Expand Up @@ -501,6 +501,25 @@ Post REST responses for post types that support REST also gain `content-connect:

> The legacy `POST /content-connect/v1/search` endpoint is **deprecated** as of 2.0.0. Use the v2 endpoints above.

## Abilities

On WordPress 6.9 and later, Content Connect registers read-only abilities in the `content-connect` category of the [Abilities API](https://developer.wordpress.org/apis/abilities/). They run over REST under `wp-abilities/v1` and are public to MCP clients. Every ability requires `edit_posts`, and the post-scoped ones also require `edit_post` on the post. On WordPress 6.8 nothing is registered.

- `content-connect/get-relationships` — Registered relationship definitions, filterable by type, key, post type, or from/to side.
- `content-connect/get-post-relationships` — The relationships that apply to a post. `context: embed` includes the related posts and users.
- `content-connect/get-related-items` — One page of the posts or users related to a post through one relationship, with `total` and `total_pages`.
- `content-connect/get-related-query-block` — Query Loop (`core/query`) markup that lists the posts related to a post through one post-to-post relationship.

### Displaying related content

Related posts on the site should come from a Query Loop filtered by the relationship. The read abilities return related IDs so an agent can inspect a relationship. Copying those IDs into a hand-picked list, such as a post picker block or a list of links, makes a snapshot that goes stale when the relationship changes. An agent adding related posts to a post should:

1. Call `content-connect/get-post-relationships` for the post to find the relationship's `rel_key`.
2. Call `content-connect/get-related-query-block` with the `post_id` and `rel_key`, and insert the returned `markup` into that post's content.
3. Change the Query Loop's inner blocks to fit the design, and leave its `query` attribute as returned.

A hand-picked list is the right choice only when an editor wants a curated selection that is not the relationship itself.

## Helper Functions

Content Connect ships namespaced helper functions in `TenUp\ContentConnect\Helpers` for reading relationship data without touching the database directly:
Expand Down
173 changes: 168 additions & 5 deletions includes/Abilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function register_category() {
self::CATEGORY,
array(
'label' => __( 'Content Connect', 'wp-content-connect' ),
'description' => __( 'Post-to-post and post-to-user relationships.', 'wp-content-connect' ),
'description' => __( 'Post-to-post and post-to-user relationships. These abilities read relationship data. To display related posts on the site, use a Query Loop (core/query) block filtered by the relationship, which stays in sync when relationships change. content-connect/get-related-query-block returns the markup for one.', 'wp-content-connect' ),
)
);
}
Expand Down Expand Up @@ -101,7 +101,7 @@ public function register_abilities() {
'content-connect/get-post-relationships',
array(
'label' => __( 'Get Post Relationships', 'wp-content-connect' ),
'description' => __( 'Lists the relationships that apply to a post, optionally including the related posts and users for each one.', 'wp-content-connect' ),
'description' => __( 'Lists the relationships that apply to a post, optionally including the related posts and users for each one. To display related posts on the site, use the markup from content-connect/get-related-query-block instead of copying the related IDs into content.', 'wp-content-connect' ),
'category' => self::CATEGORY,
'input_schema' => self::get_input_schema( self::get_post_input_args( ( new PostRelationshipsRoute() )->get_collection_params() ) ),
'output_schema' => array(
Expand All @@ -124,7 +124,7 @@ public function register_abilities() {
'content-connect/get-related-items',
array(
'label' => __( 'Get Related Items', 'wp-content-connect' ),
'description' => __( 'Lists the published posts or the users related to a post through one relationship, with pagination and ordering.', 'wp-content-connect' ),
'description' => __( 'Lists the published posts or the users related to a post through one relationship, with pagination and ordering. Use it to inspect a relationship, not to build content: to display related posts on the site, use the Query Loop (core/query) markup from content-connect/get-related-query-block. A Query Loop updates when relationships change; IDs copied into a hand-picked list do not.', 'wp-content-connect' ),
'category' => self::CATEGORY,
'input_schema' => self::get_input_schema( self::get_post_input_args( $related_args ) ),
'output_schema' => array(
Expand All @@ -143,6 +143,58 @@ public function register_abilities() {
'meta' => self::get_read_only_meta(),
)
);

wp_register_ability(
'content-connect/get-related-query-block',
array(
'label' => __( 'Get Related Query Block', 'wp-content-connect' ),
'description' => __( 'Returns the markup for a Query Loop (core/query) block that lists the posts related to a post through one post-to-post relationship. Insert the markup into that post\'s content to display its related posts; the list stays in sync when relationships change. The inner blocks are a starting point and can be changed.', 'wp-content-connect' ),
'category' => self::CATEGORY,
'input_schema' => array(
'type' => 'object',
'properties' => array(
'post_id' => array(
'type' => 'integer',
'minimum' => 1,
'description' => __( 'The post whose related posts the Query Loop lists. The markup is meant for this post\'s content.', 'wp-content-connect' ),
),
'rel_key' => array(
'type' => 'string',
'description' => __( 'The key of a post-to-post relationship that applies to the post, as returned by content-connect/get-post-relationships.', 'wp-content-connect' ),
),
'post_type' => array(
'type' => 'string',
'description' => __( 'The related post type to list. Defaults to the first related post type of the relationship.', 'wp-content-connect' ),
),
'per_page' => array(
'type' => 'integer',
'minimum' => 1,
'maximum' => 100,
'default' => 3,
'description' => __( 'The number of posts to show.', 'wp-content-connect' ),
),
),
'required' => array( 'post_id', 'rel_key' ),
'additionalProperties' => false,
),
'output_schema' => array(
'type' => 'object',
'properties' => array(
'markup' => array(
'type' => 'string',
'description' => __( 'Serialized block markup, ready to insert into post content.', 'wp-content-connect' ),
),
'attributes' => array(
'type' => 'object',
'description' => __( 'The core/query block attributes used in the markup.', 'wp-content-connect' ),
),
),
),
'execute_callback' => array( $this, 'execute_get_related_query_block' ),
'permission_callback' => array( $this, 'can_edit_input_post' ),
'meta' => self::get_read_only_meta(),
)
);
}

/**
Expand Down Expand Up @@ -258,6 +310,111 @@ public function execute_get_related_items( $input = null ) {
);
}

/**
* Builds Query Loop markup that lists the posts related to a post.
*
* The block attributes match what the block editor's Query Loop extension
* stores, so the block opens in the editor unchanged and renders through
* the `query_loop_block_query_vars` integration.
*
* @since 2.1.0
*
* @param array|null $input Ability input.
* @return array|\WP_Error
*/
public function execute_get_related_query_block( $input = null ) {

$params = (array) $input;
$post_id = (int) ( $params['post_id'] ?? 0 );
$rel_key = (string) ( $params['rel_key'] ?? '' );

$response = self::dispatch(
sprintf( '/content-connect/v2/post/%d/relationships', $post_id ),
array( 'rel_type' => 'post-to-post' )
);

if ( is_wp_error( $response ) ) {
return $response;
}

$relationships = wp_list_filter( (array) $response->get_data(), array( 'rel_key' => $rel_key ) );
$relationship = reset( $relationships );

if ( empty( $relationship ) ) {
return new \WP_Error(
'rest_relationship_not_found',
__( 'No post-to-post relationship with this key applies to the post.', 'wp-content-connect' ),
array( 'status' => 404 )
);
}

$post_types = (array) $relationship['post_type'];
$post_type = (string) ( $params['post_type'] ?? reset( $post_types ) );

if ( ! in_array( $post_type, $post_types, true ) ) {
return new \WP_Error(
'rest_invalid_param',
/* translators: %s: Comma-separated list of post types. */
sprintf( __( 'The post type must be one of: %s.', 'wp-content-connect' ), implode( ', ', $post_types ) ),
array( 'status' => 400 )
);
}

$attributes = array(
'query' => array(
'perPage' => (int) ( $params['per_page'] ?? 3 ),
'pages' => 0,
'offset' => 0,
'postType' => $post_type,
'order' => 'desc',
'orderBy' => 'date',
'author' => '',
'search' => '',
'exclude' => array(),
'sticky' => '',
'inherit' => false,
'orderByRelationship' => ! empty( $relationship['sortable'] ),
'relationshipQuery' => array(
array(
'name' => $relationship['rel_name'],
'related_to_post' => $post_id,
),
),
),
'showRelated' => true,
'relationshipKey' => $rel_key,
);

$block = array(
'blockName' => 'core/query',
'attrs' => $attributes,
'innerBlocks' => array(
array(
'blockName' => 'core/post-template',
'attrs' => array(),
'innerBlocks' => array(
array(
'blockName' => 'core/post-title',
'attrs' => array( 'isLink' => true ),
'innerBlocks' => array(),
'innerHTML' => '',
'innerContent' => array(),
),
),
'innerHTML' => "\n\n",
'innerContent' => array( "\n", null, "\n" ),
),
),
'innerHTML' => '<div class="wp-block-query"></div>',
'innerContent' => array( '<div class="wp-block-query">', null, '</div>' ),
);

return array(
'markup' => serialize_block( $block ),
'attributes' => $attributes,
);
}

/**
* Builds the shared metadata for read-only abilities.
*
Expand Down Expand Up @@ -411,7 +568,10 @@ private static function get_relationship_definition_schema() {
'properties' => array(
'rel_key' => array( 'type' => 'string' ),
'rel_type' => array( 'type' => 'string' ),
'rel_name' => array( 'type' => 'string' ),
'rel_name' => array(
'type' => 'string',
'description' => __( 'The relationship name, used as `name` in a relationship query.', 'wp-content-connect' ),
),
'object_type' => array( 'type' => 'string' ),
'labels' => $side['labels'],
'sortable' => $side['sortable'],
Expand Down Expand Up @@ -455,7 +615,10 @@ private static function get_post_relationship_schema() {
array(
'rel_key' => array( 'type' => 'string' ),
'rel_type' => array( 'type' => 'string' ),
'rel_name' => array( 'type' => 'string' ),
'rel_name' => array(
'type' => 'string',
'description' => __( 'The relationship name, used as `name` in a relationship query.', 'wp-content-connect' ),
),
'object_type' => array( 'type' => 'string' ),
'post_type' => array(
'type' => 'array',
Expand Down
107 changes: 107 additions & 0 deletions tests/php/AbilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class AbilitiesTest extends ContentConnectTestCase {
'content-connect/get-relationships',
'content-connect/get-post-relationships',
'content-connect/get-related-items',
'content-connect/get-related-query-block',
);

/**
Expand Down Expand Up @@ -375,4 +376,110 @@ public function test_get_related_items_unknown_relationship() {
$this->assertWPError( $result );
$this->assertSame( 'rest_relationship_not_found', $result->get_error_code() );
}

/**
* Tests that get-related-query-block returns Query Loop markup that renders the related posts.
*
* @return void
*/
public function test_get_related_query_block_renders_related_posts() {
$registry = get_registry();
$registry->define_post_to_post( 'post', 'post', 'ability-query-block' );
$rel_key = $registry->get_relationship_key( 'post', 'post', 'ability-query-block' );

$source = $this->factory()->post->create( array( 'post_title' => 'Query Block Source' ) );
$related = $this->factory()->post->create( array( 'post_title' => 'Query Block Related' ) );
$unrelated = $this->factory()->post->create( array( 'post_title' => 'Query Block Unrelated' ) );

$registry->get_post_to_post_relationship_by_key( $rel_key )->add_relationship( $source, $related );

$result = wp_get_ability( 'content-connect/get-related-query-block' )->execute(
array(
'post_id' => $source,
'rel_key' => $rel_key,
)
);

$this->assertIsArray( $result );
$this->assertTrue( $result['attributes']['showRelated'] );
$this->assertSame( $rel_key, $result['attributes']['relationshipKey'] );
$this->assertSame( 'post', $result['attributes']['query']['postType'] );
$this->assertSame( 3, $result['attributes']['query']['perPage'] );
$this->assertSame(
array(
array(
'name' => 'ability-query-block',
'related_to_post' => $source,
),
),
$result['attributes']['query']['relationshipQuery']
);

$blocks = parse_blocks( $result['markup'] );

$this->assertSame( 'core/query', $blocks[0]['blockName'] );
$this->assertSame( $result['attributes'], $blocks[0]['attrs'] );

$html = do_blocks( $result['markup'] );

$this->assertStringContainsString( 'Query Block Related', $html );
$this->assertStringNotContainsString( 'Query Block Unrelated', $html );
}

/**
* Tests that get-related-query-block rejects a relationship that does not apply to the post.
*
* @return void
*/
public function test_get_related_query_block_unknown_relationship() {
$result = wp_get_ability( 'content-connect/get-related-query-block' )->execute(
array(
'post_id' => 1,
'rel_key' => 'does-not-exist',
)
);

$this->assertWPError( $result );
$this->assertSame( 'rest_relationship_not_found', $result->get_error_code() );
}

/**
* Tests that get-related-query-block rejects a post type the relationship does not relate to.
*
* @return void
*/
public function test_get_related_query_block_rejects_unrelated_post_type() {
$registry = get_registry();
$registry->define_post_to_post( 'post', 'car', 'ability-query-block-type' );
$rel_key = $registry->get_relationship_key( 'post', 'car', 'ability-query-block-type' );

$result = wp_get_ability( 'content-connect/get-related-query-block' )->execute(
array(
'post_id' => 1,
'rel_key' => $rel_key,
'post_type' => 'page',
)
);

$this->assertWPError( $result );
$this->assertSame( 'rest_invalid_param', $result->get_error_code() );
}

/**
* Tests that get-related-query-block is denied to users who cannot edit the post.
*
* @return void
*/
public function test_get_related_query_block_requires_edit_post() {
wp_set_current_user( 0 );

$result = wp_get_ability( 'content-connect/get-related-query-block' )->execute(
array(
'post_id' => 1,
'rel_key' => 'any',
)
);

$this->assertWPError( $result );
}
}
Loading