[Product Block Editor] Add post_password parameter to the Woo product REST api (#39438)

* Add post password to API

* Add changelog

* Fix phpcs issue

* Remove post_password from tests

* Add additional property to test

* Increment number of properties in product schema

* Update the post when post_password changes
This commit is contained in:
Nathan Silveira 2023-07-27 13:02:36 -03:00 committed by GitHub
parent 77af817444
commit b6b757a748
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 2 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: add
Add post_password for products for REST API V3

View File

@ -203,7 +203,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
$changes = $product->get_changes();
// Only update the post when the post data changes.
if ( array_intersect( array( 'description', 'short_description', 'name', 'parent_id', 'reviews_allowed', 'status', 'menu_order', 'date_created', 'date_modified', 'slug' ), array_keys( $changes ) ) ) {
if ( array_intersect( array( 'description', 'short_description', 'name', 'parent_id', 'reviews_allowed', 'status', 'menu_order', 'date_created', 'date_modified', 'slug', 'post_password' ), array_keys( $changes ) ) ) {
$post_data = array(
'post_content' => $product->get_description( 'edit' ),
'post_excerpt' => $product->get_short_description( 'edit' ),

View File

@ -432,6 +432,11 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
$product->set_reviews_allowed( $request['reviews_allowed'] );
}
// Post password.
if ( isset( $request['post_password'] ) ) {
$product->set_post_password( $request['post_password'] );
}
// Virtual.
if ( isset( $request['virtual'] ) ) {
$product->set_virtual( $request['virtual'] );
@ -1140,6 +1145,11 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
'default' => true,
'context' => array( 'view', 'edit' ),
),
'post_password' => array(
'description' => __( 'Post password.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'average_rating' => array(
'description' => __( 'Reviews average rating.', 'woocommerce' ),
'type' => 'string',
@ -1496,6 +1506,10 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
$data['has_options'] = $product->has_options( $context );
}
if ( in_array( 'post_password', $fields, true ) ) {
$data['post_password'] = $product->get_post_password( $context );
}
$post_type_obj = get_post_type_object( $this->post_type );
if ( is_post_type_viewable( $post_type_obj ) && $post_type_obj->public ) {
$permalink_template_requested = in_array( 'permalink_template', $fields, true );

View File

@ -649,7 +649,7 @@ class WC_Tests_API_Product extends WC_REST_Unit_Test_Case {
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 69, count( $properties ) );
$this->assertEquals( 70, count( $properties ) );
}
/**

View File

@ -149,6 +149,7 @@ class WC_REST_Products_Controller_Tests extends WC_REST_Unit_Test_Case {
'grouped_products',
'menu_order',
'meta_data',
'post_password',
);
}