CYS - Move the `private/patterns` endpoint to woocommerce admin API (#50400)

* CYS - Move the ai/store-title endpoint to woocommerce admin api

* Add middleware and callback

* Add changefile(s) from automation for the following project(s): woocommerce

* Fix lint error

* CYS - Move the ai/business-description endpoint to woocommerce admin API

* CYS - Move the ai/store-info endpoint to woocommerce admin API

* Update endpoint

* CYS - Move the ai/images endpoint to woocommerce admin API

* CYS - Move the `ai/patterns` endpoint to woocommerce admin API

* CYS - Move the `ai/product` endpoint to woocommerce admin API

* CYS - Move the `ai/products` endpoint to woocommerce admin API

* CYS - Move the `private/patterns` endpoint to woocommerce admin API

* Remove unnecessary variable

* Replace endpoint

* Add changefile(s) from automation for the following project(s): woocommerce

* Fix lint errors

* Fix some feedback

* Refactor route to return an error not an exception and add tests

* Fix lint errors

---------

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Alba Rincón 2024-08-23 13:27:57 +02:00 committed by GitHub
parent eef4749d97
commit 091141218e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 225 additions and 109 deletions

View File

@ -49,7 +49,7 @@ export const OptInSubscribe = () => {
await apiFetch< {
success: boolean;
} >( {
path: `/wc/private/patterns`,
path: '/wc-admin/patterns',
method: 'POST',
} );
};

View File

@ -264,7 +264,7 @@ export const SidebarNavigationScreenHomepagePTK = ( {
<Button
onClick={ async () => {
await apiFetch( {
path: `/wc/private/patterns`,
path: `/wc-admin/patterns`,
method: 'POST',
} );

View File

@ -168,7 +168,7 @@ export const installPatterns = async () => {
const { success } = await apiFetch< {
success: boolean;
} >( {
path: '/wc/private/patterns',
path: '/wc-admin/patterns',
method: 'POST',
} );

View File

@ -106,7 +106,7 @@ const fetchIsFontLibraryAvailable = async () => {
const fetchIsPTKPatternsAPIAvailable = async () => {
try {
await apiFetch( {
path: '/wc/private/patterns',
path: '/wc-admin/patterns',
method: 'GET',
} );

View File

@ -0,0 +1,4 @@
Significance: minor
Type: dev
CYS - Move the "private/patterns" endpoint to woocommerce admin API.

View File

@ -93,6 +93,7 @@ class Init {
'Automattic\WooCommerce\Admin\API\AI\Patterns',
'Automattic\WooCommerce\Admin\API\AI\Product',
'Automattic\WooCommerce\Admin\API\AI\Products',
'Automattic\WooCommerce\Admin\API\Patterns',
);
}

View File

@ -0,0 +1,93 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Admin\API;
use Automattic\WooCommerce\Blocks\BlockPatterns;
use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\Patterns\PTKClient;
use Automattic\WooCommerce\Blocks\Patterns\PTKPatternsStore;
use WP_Error;
use WP_REST_Response;
defined( 'ABSPATH' ) || exit;
/**
* Patterns controller
*
* @internal
*/
class Patterns {
/**
* Register routes.
*/
public function register_routes() {
register_rest_route(
'wc-admin',
'/patterns',
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_pattern' ),
'permission_callback' => function () {
return is_user_logged_in();
},
),
array(
'methods' => \WP_REST_Server::CREATABLE,
'callback' => array( $this, 'update_patterns' ),
'permission_callback' => function () {
return is_user_logged_in();
},
),
)
);
}
/**
* Fetch a single pattern from the PTK to ensure the API is available.
*
* @return WP_Error|WP_REST_Response
*/
public function get_pattern() {
$ptk_client = Package::container()->get( PTKClient::class );
$response = $ptk_client->fetch_patterns(
array(
'per_page' => 1,
)
);
if ( is_wp_error( $response ) ) {
return $response;
}
return rest_ensure_response(
array(
'success' => true,
)
);
}
/**
* Fetch the patterns from the PTK and update the transient.
*
* @return WP_REST_Response
*/
public function update_patterns() {
$ptk_patterns_store = Package::container()->get( PTKPatternsStore::class );
$ptk_patterns_store->fetch_patterns();
$block_patterns = Package::container()->get( BlockPatterns::class );
$block_patterns->register_ptk_patterns();
return rest_ensure_response(
array(
'success' => true,
)
);
}
}

View File

@ -36,7 +36,7 @@ class RoutesController {
public function __construct( SchemaController $schema_controller ) {
$this->schema_controller = $schema_controller;
$this->routes = [
'v1' => [
'v1' => [
Routes\V1\Batch::IDENTIFIER => Routes\V1\Batch::class,
Routes\V1\Cart::IDENTIFIER => Routes\V1\Cart::class,
Routes\V1\CartAddItem::IDENTIFIER => Routes\V1\CartAddItem::class,
@ -66,9 +66,6 @@ class RoutesController {
Routes\V1\ProductsById::IDENTIFIER => Routes\V1\ProductsById::class,
Routes\V1\ProductsBySlug::IDENTIFIER => Routes\V1\ProductsBySlug::class,
],
'private' => [
Routes\V1\Patterns::IDENTIFIER => Routes\V1\Patterns::class,
],
];
}

View File

@ -54,7 +54,6 @@ class SchemaController {
Schemas\V1\ProductCategorySchema::IDENTIFIER => Schemas\V1\ProductCategorySchema::class,
Schemas\V1\ProductCollectionDataSchema::IDENTIFIER => Schemas\V1\ProductCollectionDataSchema::class,
Schemas\V1\ProductReviewSchema::IDENTIFIER => Schemas\V1\ProductReviewSchema::class,
Schemas\V1\PatternsSchema::IDENTIFIER => Schemas\V1\PatternsSchema::class,
],
];
}

View File

@ -1,43 +0,0 @@
<?php
namespace Automattic\WooCommerce\StoreApi\Schemas\V1;
/**
* OrderSchema class.
*/
class PatternsSchema extends AbstractSchema {
/**
* The schema item name.
*
* @var string
*/
protected $title = 'patterns';
/**
* The schema item identifier.
*
* @var string
*/
const IDENTIFIER = 'patterns';
/**
* Patterns schema properties.
*
* @return array
*/
public function get_properties() {
return [];
}
/**
* Get the Patterns response.
*
* @param array $item Item to get response for.
*
* @return array
*/
public function get_item_response( $item ) {
return [
'success' => true,
];
}
}

View File

@ -254,7 +254,7 @@ test.describe( 'Homepage tracking banner', () => {
} ) => {
await setOption( request, baseURL, 'woocommerce_allow_tracking', 'no' );
await page.route( '**/wp-json/wc/private/patterns*', ( route ) => {
await page.route( '**/wp-json/wc-admin/patterns*', ( route ) => {
route.fulfill( {
status: 500,
} );

View File

@ -0,0 +1,121 @@
<?php
declare( strict_types = 1 );
namespace Automattic\WooCommerce\Tests\Admin\API;
use Automattic\WooCommerce\Blocks\Patterns\PTKPatternsStore;
use WC_REST_Unit_Test_Case;
/**
* Patterns Controller Tests.
*/
class PatternsTest extends WC_REST_Unit_Test_Case {
/**
* Set up user for tests.
*/
public function setUp(): void {
parent::setUp();
$user = $this->factory->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $user );
}
/**
* Test the post endpoint when tracking is not allowed.
*
* @return void
*/
public function test_post_endpoint_when_tracking_is_not_allowed() {
update_option( 'woocommerce_allow_tracking', 'no' );
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'POST', '/wc-admin/patterns' ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( true, $data['success'] );
$patterns = get_transient( PTKPatternsStore::TRANSIENT_NAME );
$this->assertFalse( $patterns );
}
/**
* Test the post endpoint when tracking is allowed.
*
* @return void
*/
public function test_post_endpoint_when_tracking_is_allowed() {
update_option( 'woocommerce_allow_tracking', 'yes' );
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'POST', '/wc-admin/patterns' ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( true, $data['success'] );
$patterns = get_transient( PTKPatternsStore::TRANSIENT_NAME );
$this->assertNotFalse( $patterns );
}
/**
* Test the get endpoint when tracking is not allowed.
*
* @return void
*/
public function test_get_endpoint_when_tracking_is_not_allowed() {
update_option( 'woocommerce_allow_tracking', 'no' );
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc-admin/patterns' ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( true, $data['success'] );
$patterns = get_transient( PTKPatternsStore::TRANSIENT_NAME );
$this->assertFalse( $patterns );
}
/**
* Test the get endpoint when tracking is allowed.
*
* @return void
*/
public function test_get_endpoint_when_tracking_is_allowed() {
update_option( 'woocommerce_allow_tracking', 'yes' );
rest_get_server()->dispatch( new \WP_REST_Request( 'POST', '/wc-admin/patterns' ) );
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc-admin/patterns' ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( true, $data['success'] );
$patterns = get_transient( PTKPatternsStore::TRANSIENT_NAME );
$this->assertNotFalse( $patterns );
}
/**
* Test the get endpoint when the PTK client fails.
*
* @return void
*/
public function test_get_endpoint_when_ptk_client_fails() {
update_option( 'woocommerce_allow_tracking', 'yes' );
$error = function () {
return new \WP_Error( 'error', 'An error message' );
};
add_filter( 'pre_http_request', $error, 10, 3 );
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'GET', '/wc-admin/patterns' ) );
$data = $response->get_data();
$this->assertEquals( 500, $response->get_status() );
$this->assertEquals( 'patterns_toolkit_api_error', $data['code'] );
$this->assertEquals( 'Failed to connect with the Patterns Toolkit API: try again later.', $data['message'] );
$patterns = get_transient( PTKPatternsStore::TRANSIENT_NAME );
$this->assertFalse( $patterns );
}
}

View File

@ -1,56 +0,0 @@
<?php
namespace Automattic\WooCommerce\Tests\Blocks\StoreApi\Routes;
use Automattic\WooCommerce\Blocks\Patterns\PTKPatternsStore;
/**
* Patterns Controller Tests.
*/
class Patterns extends ControllerTestCase {
/**
* Set up user for tests.
*/
public function setUp(): void {
parent::setUp();
$user = $this->factory->user->create( array( 'role' => 'administrator' ) );
wp_set_current_user( $user );
}
/**
* Test the post endpoint when tracking is not allowed.
*
* @return void
*/
public function test_post_endpoint_when_tracking_is_not_allowed() {
update_option( 'woocommerce_allow_tracking', 'no' );
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'POST', '/wc/private/patterns' ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( true, $data['success'] );
$patterns = get_transient( PTKPatternsStore::TRANSIENT_NAME );
$this->assertFalse( $patterns );
}
/**
* Test the post endpoint when tracking is allowed.
*
* @return void
*/
public function test_post_endpoint_when_tracking_is_allowed() {
update_option( 'woocommerce_allow_tracking', 'yes' );
$response = rest_get_server()->dispatch( new \WP_REST_Request( 'POST', '/wc/private/patterns' ) );
$data = $response->get_data();
$this->assertEquals( 200, $response->get_status() );
$this->assertEquals( true, $data['success'] );
$patterns = get_transient( PTKPatternsStore::TRANSIENT_NAME );
$this->assertNotFalse( $patterns );
}
}