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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file, per [the Ke

## [Unreleased] - TBD

### 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)).
Comment thread
s3rgiosan marked this conversation as resolved.
Resolved
- Ability descriptions that point agents to a Query Loop (`core/query`) block filtered by the relationship for displaying related posts (props [@fabiankaegy](https://github.com/fabiankaegy) via [#147](https://github.com/10up/wp-content-connect/pull/147)).

## [2.0.0] - TBD

### Breaking Changes
Expand Down
112 changes: 62 additions & 50 deletions includes/API/V2/Post/Route/RelatedEntities.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,56 +27,7 @@ public function register_routes() {
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => array(
'post_status' => array(
'description' => __( 'Limit result set to posts assigned one or more statuses.', 'wp-content-connect' ),
'type' => 'array',
'default' => 'publish',
'sanitize_callback' => function ( $value ) {
if ( is_array( $value ) ) {
return array_map( 'sanitize_text_field', $value );
}
return sanitize_text_field( $value );
},
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => array_merge( array_keys( get_post_stati() ), array( 'any' ) ),
'type' => 'string',
),
),
'page' => array(
'description' => __( 'Current page of the collection.', 'wp-content-connect' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
),
'per_page' => array(
'description' => __( 'Maximum number of items to be returned in result set.', 'wp-content-connect' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
),
'order' => array(
'description' => __( 'Order sort attribute ascending or descending.', 'wp-content-connect' ),
'type' => 'string',
'default' => 'asc',
'enum' => array( 'asc', 'desc' ),
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
),
'orderby' => array(
'description' => __( 'Sort collection by relationship or object attribute.', 'wp-content-connect' ),
'type' => 'string',
'default' => 'relationship',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => array( $this, 'validate_orderby_request_arg' ),
),
),
'args' => $this->get_collection_params(),
),
array(
'methods' => \WP_REST_Server::CREATABLE,
Expand Down Expand Up @@ -125,6 +76,67 @@ public function register_routes() {
);
}

/**
* Retrieves the query params for the related entities collection.
*
* @since 2.1.0
*
* @return array
*/
public function get_collection_params() {
return array(
'post_status' => array(
'description' => __( 'Limit result set to posts assigned one or more statuses.', 'wp-content-connect' ),
'type' => 'array',
'default' => 'publish',
'sanitize_callback' => function ( $value ) {
if ( is_array( $value ) ) {
return array_map( 'sanitize_text_field', $value );
}
return sanitize_text_field( $value );
},
'validate_callback' => 'rest_validate_request_arg',
'items' => array(
'enum' => array_merge( array_keys( get_post_stati() ), array( 'any' ) ),
'type' => 'string',
),
),
'page' => array(
'description' => __( 'Current page of the collection.', 'wp-content-connect' ),
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
'minimum' => 1,
),
'per_page' => array(
'description' => __( 'Maximum number of items to be returned in result set.', 'wp-content-connect' ),
'type' => 'integer',
'default' => 10,
'minimum' => 1,
'maximum' => 100,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
),
'order' => array(
'description' => __( 'Order sort attribute ascending or descending. Ignored when ordering by relationship.', 'wp-content-connect' ),
'type' => 'string',
'default' => 'asc',
'enum' => array( 'asc', 'desc' ),
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
),
'orderby' => array(
'description' => __( 'Sort collection by relationship or object attribute. "relationship" keeps the saved sort order. "title" applies to post-to-post relationships and "name" to post-to-user relationships.', 'wp-content-connect' ),
'type' => 'string',
'default' => 'relationship',
'enum' => array( 'relationship', 'date', 'id', 'title', 'name' ),
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => array( $this, 'validate_orderby_request_arg' ),
),
);
}

/**
* Retrieves a collection of related entities for a post.
*
Expand Down
67 changes: 40 additions & 27 deletions includes/API/V2/Post/Route/Relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,11 @@ class Relationships extends AbstractPostRoute {
*/
public function register_routes() {

$params = $this->get_route_params();

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)/relationships',
array(
'args' => array(
'id' => $params['id'],
'rel_type' => array(
'description' => __( 'The relationship type to filter relationships by.', 'wp-content-connect' ),
'type' => 'string',
'default' => 'any',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
'enum' => array( 'any', 'post-to-post', 'post-to-user' ),
),
'post_type' => array(
'description' => __( 'The post type to filter relationships by.', 'wp-content-connect' ),
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
),
'context' => array(
'description' => __( 'Scope under which the request is made; determines fields present in response.', 'wp-content-connect' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
'enum' => array( 'view', 'embed' ),
),
),
'args' => $this->get_collection_params(),
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
Expand All @@ -58,6 +32,45 @@ public function register_routes() {
);
}

/**
* Retrieves the query params for the post relationships collection.
*
* @since 2.1.0
*
* @return array
*/
public function get_collection_params() {

$params = $this->get_route_params();

return array(
'id' => $params['id'],
'rel_type' => array(
'description' => __( 'The relationship type to filter relationships by.', 'wp-content-connect' ),
'type' => 'string',
'default' => 'any',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
'enum' => array( 'any', 'post-to-post', 'post-to-user' ),
),
'post_type' => array(
'description' => __( 'The post type to filter post-to-post relationships by.', 'wp-content-connect' ),
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
),
'context' => array(
'description' => __( 'Scope under which the request is made; determines fields present in response. "embed" includes the related posts and users for each relationship.', 'wp-content-connect' ),
'type' => 'string',
'default' => 'view',
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
'enum' => array( 'view', 'embed' ),
),
);
}

/**
* Retrieves a collection of relationships for a post.
*
Expand Down
61 changes: 36 additions & 25 deletions includes/API/V2/Route/Relationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,47 @@ public function register_routes() {
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => array(
'rel_type' => array(
'description' => __( 'The relationship type to filter relationships by.', 'wp-content-connect' ),
'type' => 'string',
'default' => 'post-to-post',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
'enum' => array( 'post-to-post', 'post-to-user' ),
),
'filter_by' => array(
'description' => __( 'The criteria to filter relationships by.', 'wp-content-connect' ),
'type' => 'string',
'default' => 'any',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => array( $this, 'validate_filter_by' ),
'enum' => array( 'key', 'post_type', 'from', 'to', 'any' ),
),
'filter_value' => array(
'description' => __( 'The value to use with the selected filter.', 'wp-content-connect' ),
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
),
),
'args' => $this->get_collection_params(),
),
),
);
}

/**
* Retrieves the query params for the relationships collection.
*
* @since 2.1.0
*
* @return array
*/
public function get_collection_params() {
return array(
'rel_type' => array(
'description' => __( 'The relationship type to filter relationships by.', 'wp-content-connect' ),
'type' => 'string',
'default' => 'post-to-post',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
'enum' => array( 'post-to-post', 'post-to-user' ),
),
'filter_by' => array(
'description' => __( 'The criteria to filter relationships by. "from" and "to" apply to post-to-post relationships only.', 'wp-content-connect' ),
'type' => 'string',
'default' => 'any',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => array( $this, 'validate_filter_by' ),
'enum' => array( 'key', 'post_type', 'from', 'to', 'any' ),
),
'filter_value' => array(
'description' => __( 'The value to use with the selected filter, such as a relationship key or post type.', 'wp-content-connect' ),
'type' => 'string',
'default' => '',
'sanitize_callback' => 'sanitize_text_field',
'validate_callback' => 'rest_validate_request_arg',
),
);
}

/**
* Retrieves a collection of relationships by type.
*
Expand Down
Loading
Loading