Skip to content
Draft
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
10 changes: 8 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ jobs:
# - Uploads code coverage report to Codecov.io (if coverage is enabled).
# - Uploads HTML coverage report as an artifact (if coverage is enabled).
phpunit:
name: Test PHP ${{ matrix.php }} WP ${{ matrix.wp }}${{ matrix.coverage && ' with coverage' || '' }}
name: Test PHP ${{ matrix.php }} WP ${{ matrix.wp }}${{ matrix.coverage && ' with coverage' || '' }}${{ matrix.rest_backend && ' against the REST API' || '' }}
runs-on: ubuntu-24.04
if: ${{ github.repository == 'WordPress/ai' || github.event_name == 'pull_request' }}
strategy:
Expand All @@ -176,10 +176,16 @@ jobs:
php: ['8.4', '8.3', '8.2', '8.1', '8.0', '7.4']
wp: ['7.0', latest, trunk]
coverage: [false]
rest_backend: [false]
include:
- php: '8.4'
wp: latest
coverage: true
# The core read abilities ship a second execute implementation that calls the
# REST API. One run covers it, since it shares the suite with the default one.
- php: '8.3'
wp: latest
rest_backend: true
env:
WP_ENV_PHP_VERSION: ${{ matrix.php }}
WP_ENV_CORE: ${{ matrix.wp == 'trunk' && 'WordPress/WordPress' || format( 'https://wordpress.org/wordpress-{0}.zip', matrix.wp ) }}
Expand Down Expand Up @@ -244,7 +250,7 @@ jobs:
- name: Run PHPUnit tests${{ matrix.coverage && ' with coverage report' || '' }}
id: phpunit
run: |
npm run test:php
npm run ${{ matrix.rest_backend && 'test:php:rest' || 'test:php' }}

- name: Upload code coverage report
continue-on-error: true
Expand Down
17 changes: 15 additions & 2 deletions includes/Abilities/Content/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use WP_Error;
use WP_Post;
use WP_Query;
use WordPress\AI\Abilities\Rest\Rest_Backend;

// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
Expand Down Expand Up @@ -522,6 +523,9 @@ public function execute_read_content( $input = array() ) {
$fields = $this->normalize_fields( $input );
$requires_edit = $this->has_explicit_edit_fields( $input );

// Plugin: the alternative implementation reads the same posts through the REST API.
$rest = Rest_Backend::is_enabled() ? new Content_Rest() : null;

// Single-post mode (by ID).
if ( ! empty( $input['id'] ) ) {
$post = get_post( $this->input_int( $input['id'] ) );
Expand All @@ -533,7 +537,9 @@ public function execute_read_content( $input = array() ) {
return $this->not_found_error();
}

return $this->to_output_post( $this->format_post( $post, $fields ) );
return null !== $rest
? $rest->get_post( $post, $fields )
: $this->to_output_post( $this->format_post( $post, $fields ) );
}

// Single-post mode (by slug) and query mode.
Expand All @@ -549,7 +555,9 @@ public function execute_read_content( $input = array() ) {
return $this->not_found_error();
}

return $this->to_output_post( $this->format_post( $post, $fields ) );
return null !== $rest
? $rest->get_post( $post, $fields )
: $this->to_output_post( $this->format_post( $post, $fields ) );
}

/*
Expand Down Expand Up @@ -646,6 +654,11 @@ public function execute_read_content( $input = array() ) {
$query_args['post_parent'] = $parent;
}

// Plugin: the alternative implementation runs the same query through the REST API.
if ( null !== $rest ) {
return $rest->query_posts( $post_type, $query_args, $fields );
}

$query = new WP_Query( $query_args );
$total = $this->get_query_total( $query, $query_args, $page );
$total_pages = $total > 0 ? (int) ceil( $total / $per_page ) : 0;
Expand Down
Loading
Loading