Skip to content

Commit

Permalink
[Product Block Editor] Add post_password parameter to the Woo product…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
nathanss committed Jul 27, 2023
1 parent 77af817 commit b6b757a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions plugins/woocommerce/changelog/add-post_password_block
@@ -0,0 +1,4 @@
Significance: minor
Type: add

Add post_password for products for REST API V3
Expand Up @@ -203,7 +203,7 @@ public function update( &$product ) {
$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' ),
Expand Down
Expand Up @@ -432,6 +432,11 @@ protected function prepare_object_for_database( $request, $creating = false ) {
$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'] );
Expand Down Expand Up @@ -1140,6 +1145,11 @@ public function get_item_schema() {
'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',
Expand Down Expand Up @@ -1496,6 +1506,10 @@ protected function get_product_data( $product, $context = 'view' ) {
$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 );
Expand Down
Expand Up @@ -649,7 +649,7 @@ public function test_product_schema() {
$response = $this->server->dispatch( $request );
$data = $response->get_data();
$properties = $data['schema']['properties'];
$this->assertEquals( 69, count( $properties ) );
$this->assertEquals( 70, count( $properties ) );
}

/**
Expand Down
Expand Up @@ -149,6 +149,7 @@ public function get_expected_response_fields() {
'grouped_products',
'menu_order',
'meta_data',
'post_password',
);
}

Expand Down

0 comments on commit b6b757a

Please sign in to comment.