plugin updates

This commit is contained in:
Tony Volpe
2024-09-05 11:04:01 -04:00
parent ed6b060261
commit 50cd64dd3d
925 changed files with 16918 additions and 13003 deletions

View File

@@ -144,6 +144,11 @@ class WC_REST_Product_Reviews_V1_Controller extends WC_REST_Controller {
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
$review = $this->get_review( (int) $request['id'], (int) $request['product_id'] );
if ( is_wp_error( $review ) ) {
return $review;
}
if ( ! wc_rest_check_product_reviews_permissions( 'read', (int) $request['id'] ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
@@ -172,6 +177,11 @@ class WC_REST_Product_Reviews_V1_Controller extends WC_REST_Controller {
* @return WP_Error|boolean
*/
public function update_item_permissions_check( $request ) {
$review = $this->get_review( (int) $request['id'], (int) $request['product_id'] );
if ( is_wp_error( $review ) ) {
return $review;
}
if ( ! wc_rest_check_product_reviews_permissions( 'edit', (int) $request['id'] ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
@@ -186,6 +196,11 @@ class WC_REST_Product_Reviews_V1_Controller extends WC_REST_Controller {
* @return WP_Error|boolean
*/
public function delete_item_permissions_check( $request ) {
$review = $this->get_review( (int) $request['id'], (int) $request['product_id'] );
if ( is_wp_error( $review ) ) {
return $review;
}
if ( ! wc_rest_check_product_reviews_permissions( 'delete', (int) $request['id'] ) ) {
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you cannot delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
@@ -218,6 +233,28 @@ class WC_REST_Product_Reviews_V1_Controller extends WC_REST_Controller {
return rest_ensure_response( $data );
}
/**
* Fetch a single product review from the database.
*
* @param int $id Review ID.
* @param int $product_id Product ID.
*
* @since 9.2.0
* @return \WP_Comment
*/
protected function get_review( int $id, int $product_id ) {
if ( 0 >= $product_id || 'product' !== get_post_type( $product_id ) ) {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
$review = 0 <= $id ? get_comment( $id ) : null;
if ( empty( $review ) || empty( $review->comment_ID ) || 'review' !== get_comment_type( $id ) || empty( $review->comment_post_ID ) || (int) $review->comment_post_ID !== $product_id ) {
return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid product review ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
return $review;
}
/**
* Get a single product review.
*
@@ -225,17 +262,9 @@ class WC_REST_Product_Reviews_V1_Controller extends WC_REST_Controller {
* @return WP_Error|WP_REST_Response
*/
public function get_item( $request ) {
$id = (int) $request['id'];
$product_id = (int) $request['product_id'];
if ( 'product' !== get_post_type( $product_id ) ) {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
$review = get_comment( $id );
if ( empty( $id ) || empty( $review ) || intval( $review->comment_post_ID ) !== $product_id ) {
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
$review = $this->get_review( (int) $request['id'], (int) $request['product_id'] );
if ( is_wp_error( $review ) ) {
return $review;
}
$delivery = $this->prepare_item_for_response( $review, $request );
@@ -309,14 +338,9 @@ class WC_REST_Product_Reviews_V1_Controller extends WC_REST_Controller {
$product_review_id = (int) $request['id'];
$product_id = (int) $request['product_id'];
if ( 'product' !== get_post_type( $product_id ) ) {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
$review = get_comment( $product_review_id );
if ( empty( $product_review_id ) || empty( $review ) || intval( $review->comment_post_ID ) !== $product_id ) {
return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid resource ID.', 'woocommerce' ), array( 'status' => 404 ) );
$review = $this->get_review( $product_review_id, $product_id );
if ( is_wp_error( $review ) ) {
return $review;
}
$prepared_review = $this->prepare_item_for_database( $request );
@@ -358,15 +382,11 @@ class WC_REST_Product_Reviews_V1_Controller extends WC_REST_Controller {
public function delete_item( $request ) {
$product_id = (int) $request['product_id'];
$product_review_id = (int) $request['id'];
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
$product_review = $this->get_review( $product_review_id, $product_id );
$force = isset( $request['force'] ) ? (bool) $request['force'] : false;
if ( 'product' !== get_post_type( $product_id ) ) {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
$product_review = get_comment( $product_review_id );
if ( empty( $product_review_id ) || empty( $product_review->comment_ID ) || empty( $product_review->comment_post_ID ) ) {
return new WP_Error( 'woocommerce_rest_product_review_invalid_id', __( 'Invalid product review ID.', 'woocommerce' ), array( 'status' => 404 ) );
if ( is_wp_error( $product_review ) ) {
return $product_review;
}
/**

View File

@@ -213,7 +213,11 @@ class WC_REST_Tax_Classes_V1_Controller extends WC_REST_Controller {
}
$tax_class = WC_Tax::get_tax_class_by( 'slug', sanitize_title( $request['slug'] ) );
$deleted = WC_Tax::delete_tax_class_by( 'slug', sanitize_title( $request['slug'] ) );
if ( ! $tax_class ) {
return new WP_Error( 'woocommerce_rest_tax_class_invalid_slug', __( 'Invalid slug.', 'woocommerce' ), array( 'status' => 404 ) );
}
$deleted = WC_Tax::delete_tax_class_by( 'slug', sanitize_title( $request['slug'] ) );
if ( ! $deleted ) {
return new WP_Error( 'woocommerce_rest_invalid_id', __( 'Invalid resource id.', 'woocommerce' ), array( 'status' => 400 ) );

View File

@@ -561,7 +561,7 @@ class WC_REST_Webhooks_V1_Controller extends WC_REST_Controller {
$webhook = wc_get_webhook( $id );
if ( empty( $webhook ) || is_null( $webhook ) ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 404 ) );
}
$data = array(

View File

@@ -10,6 +10,8 @@
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Internal\Utilities\Types;
/**
* REST API Order Refunds controller class.
*
@@ -316,11 +318,28 @@ class WC_REST_Order_Refunds_V2_Controller extends WC_REST_Orders_V2_Controller {
* The dynamic portion of the hook name, `$this->post_type`,
* refers to the object type slug.
*
* @since 4.5.0
*
* @param WC_Data $coupon Object object.
* @param WP_REST_Request $request Request object.
* @param bool $creating If is creating a new object.
*/
return apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $refund, $request, $creating );
$refund = apply_filters( "woocommerce_rest_pre_insert_{$this->post_type}_object", $refund, $request, $creating );
// If the filtered result is not a WC_Data instance and is not a WP_Error then something went wrong, but we
// still need to honor the declared return type.
return Types::ensure_instance_of(
$refund,
WC_Data::class,
function ( $thing ) {
return is_wp_error( $thing )
? $thing
: new WP_Error(
'woocommerce_rest_cannot_verify_refund_created',
__( 'An unexpected error occurred while generating the refund.', 'woocommerce' )
);
}
);
}
/**

View File

@@ -468,7 +468,7 @@ class WC_REST_Orders_V2_Controller extends WC_REST_CRUD_Controller {
}
// Format the order status.
$data['status'] = 'wc-' === substr( $data['status'], 0, 3 ) ? substr( $data['status'], 3 ) : $data['status'];
$data['status'] = OrderUtil::remove_status_prefix( $data['status'] );
// Format line items.
foreach ( $format_line_items as $key ) {

View File

@@ -137,10 +137,48 @@ class WC_REST_Product_Variations_V2_Controller extends WC_REST_Products_V2_Contr
*
* @since 3.0.0
* @param int $id Object ID.
* @return WC_Data
* @return WC_Data|null
*/
protected function get_object( $id ) {
return wc_get_product( $id );
$object = wc_get_product( $id );
return ( $object && 0 !== $object->get_parent_id() ) ? $object : null;
}
/**
* Checks that a variation belongs to the specified parent product.
*
* @param int $variation_id Variation ID.
* @param int $parent_id Parent product ID to check against.
* @return bool TRUE if variation and parent product exist. FALSE otherwise.
*
* @since 9.2.0
*/
protected function check_variation_parent( int $variation_id, int $parent_id ): bool {
$variation = $this->get_object( $variation_id );
if ( ! $variation || $parent_id !== $variation->get_parent_id() ) {
return false;
}
$parent = wc_get_product( $variation->get_parent_id() );
if ( ! $parent ) {
return false;
}
return true;
}
/**
* Check if a given request has access to read an item.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
if ( ! $this->check_variation_parent( (int) $request['id'], (int) $request['product_id'] ) ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
return parent::get_item_permissions_check( $request );
}
/**
@@ -150,18 +188,31 @@ class WC_REST_Product_Variations_V2_Controller extends WC_REST_Products_V2_Contr
* @return WP_Error|boolean
*/
public function update_item_permissions_check( $request ) {
if ( ! $this->check_variation_parent( (int) $request['id'], (int) $request['product_id'] ) ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
$object = $this->get_object( (int) $request['id'] );
if ( $object && 0 !== $object->get_id() && ! wc_rest_check_post_permissions( $this->post_type, 'edit', $object->get_id() ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you are not allowed to edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
// Check if variation belongs to the correct parent product.
if ( $object && 0 !== $object->get_parent_id() && absint( $request['product_id'] ) !== $object->get_parent_id() ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Parent product does not match current variation.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
return true;
}
/**
* Check if a given request has access to delete an item.
*
* @param WP_REST_Request $request Full details about the request.
* @return bool|WP_Error
*/
public function delete_item_permissions_check( $request ) {
if ( ! $this->check_variation_parent( (int) $request['id'], (int) $request['product_id'] ) ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'Invalid ID.', 'woocommerce' ), array( 'status' => 404 ) );
}
return true;
return parent::delete_item_permissions_check( $request );
}
/**

View File

@@ -486,7 +486,7 @@ class WC_REST_System_Status_V2_Controller extends WC_REST_Controller {
'readonly' => true,
'properties' => array(
'api_enabled' => array(
'description' => __( 'REST API enabled?', 'woocommerce' ),
'description' => __( 'Legacy REST API enabled?', 'woocommerce' ),
'type' => 'boolean',
'context' => array( 'view' ),
'readonly' => true,

View File

@@ -97,13 +97,15 @@ class WC_REST_Tax_Classes_V2_Controller extends WC_REST_Tax_Classes_V1_Controlle
$tax_class = WC_Tax::get_tax_class_by( 'slug', sanitize_title( $request['slug'] ) );
}
$data = array();
if ( $tax_class ) {
$class = $this->prepare_item_for_response( $tax_class, $request );
$class = $this->prepare_response_for_collection( $class );
$data[] = $class;
if ( ! $tax_class ) {
return new WP_Error( 'woocommerce_rest_tax_class_invalid_slug', __( 'Invalid slug.', 'woocommerce' ), array( 'status' => 404 ) );
}
$data = array();
$class = $this->prepare_item_for_response( $tax_class, $request );
$class = $this->prepare_response_for_collection( $class );
$data[] = $class;
return rest_ensure_response( $data );
}
}

View File

@@ -36,7 +36,7 @@ class WC_REST_Webhooks_V2_Controller extends WC_REST_Webhooks_V1_Controller {
$webhook = wc_get_webhook( $id );
if ( empty( $webhook ) || is_null( $webhook ) ) {
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 400 ) );
return new WP_Error( "woocommerce_rest_{$this->post_type}_invalid_id", __( 'ID is invalid.', 'woocommerce' ), array( 'status' => 404 ) );
}
$data = array(

View File

@@ -166,7 +166,21 @@ abstract class WC_REST_CRUD_Controller extends WC_REST_Posts_Controller {
return $object;
}
$object->save();
try {
$object->save();
} catch ( Exception $e ) {
$error = "woocommerce_rest_{$this->post_type}_not_created";
wc_get_logger()->error(
$e->getMessage(),
array(
'source' => 'woocommerce-rest-api',
'error' => $error,
'code' => 400,
)
);
return new WP_Error( $error, $e->getMessage(), array( 'status' => 400 ) );
}
return $this->get_object( $object->get_id() );
} catch ( WC_Data_Exception $e ) {

View File

@@ -0,0 +1,162 @@
<?php
/**
* REST API CustomFields controller
*
* Handles requests to the /products/custom-fields endpoint.
*
* @package WooCommerce\RestApi
* @since 9.2.0
*/
use Automattic\WooCommerce\Utilities\I18nUtil;
defined( 'ABSPATH' ) || exit;
/**
* REST API Product Custom Fields controller class.
*
* @package WooCommerce\RestApi
* @extends WC_REST_Controller
*/
class WC_REST_Product_Custom_Fields_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.
*
* @var string
*/
protected $namespace = 'wc/v3';
/**
* Route base.
*
* @var string
*/
protected $rest_base = 'products/custom-fields';
/**
* Post type.
*
* @var string
*/
protected $post_type = 'product';
/**
* Register the routes for products.
*/
public function register_routes() {
register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/names',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_item_names' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => $this->get_collection_params(),
),
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
/**
* Get a collection of custom field names.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function get_item_names( $request ) {
global $wpdb;
$search = trim( $request['search'] );
$order = strtoupper( $request['order'] ) === 'DESC' ? 'DESC' : 'ASC';
$page = (int) $request['page'];
$limit = (int) $request['per_page'];
$offset = ( $page - 1 ) * $limit;
$base_query = $wpdb->prepare(
"SELECT DISTINCT post_metas.meta_key
FROM {$wpdb->postmeta} post_metas LEFT JOIN {$wpdb->posts} posts ON post_metas.post_id = posts.id
WHERE posts.post_type = %s AND post_metas.meta_key NOT LIKE %s AND post_metas.meta_key LIKE %s",
$this->post_type,
$wpdb->esc_like( '_' ) . '%',
'%' . $wpdb->esc_like( $search ) . '%'
);
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $base_query has been prepared already and $order is a static value.
$query = $wpdb->prepare(
"$base_query ORDER BY post_metas.meta_key $order LIMIT %d, %d",
$offset,
$limit
);
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $base_query has been prepared already.
$total_query = "SELECT COUNT(1) FROM ($base_query) AS total";
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- $query has been prepared already.
$query_result = $wpdb->get_results( $query );
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared -- $total_query has been prepared already.
$total_items = $wpdb->get_var( $total_query );
$custom_field_names = array();
foreach ( $query_result as $custom_field_name ) {
$custom_field_names[] = $custom_field_name->meta_key;
}
$response = rest_ensure_response( $custom_field_names );
$response->header( 'X-WP-Total', (int) $total_items );
$max_pages = ceil( $total_items / $limit );
$response->header( 'X-WP-TotalPages', (int) $max_pages );
$base = add_query_arg( $request->get_query_params(), rest_url( '/' . $this->namespace . '/' . $this->rest_base . '/names' ) );
if ( $page > 1 ) {
$prev_page = $page - 1;
if ( $prev_page > $max_pages ) {
$prev_page = $max_pages;
}
$prev_link = add_query_arg( 'page', $prev_page, $base );
$response->link_header( 'prev', $prev_link );
}
if ( $max_pages > $page ) {
$next_page = $page + 1;
$next_link = add_query_arg( 'page', $next_page, $base );
$response->link_header( 'next', $next_link );
}
return $response;
}
/**
* Check if a given request has access to read items.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|boolean
*/
public function get_items_permissions_check( $request ) {
if ( ! wc_rest_check_post_permissions( $this->post_type, 'read' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot list resources.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Add new options for 'order' to the collection params.
*
* @return array
*/
public function get_collection_params() {
$params = parent::get_collection_params();
$params['order'] = array(
'description' => __( 'Order sort items ascending or descending.', 'woocommerce' ),
'type' => 'string',
'default' => 'asc',
'enum' => array( 'asc', 'desc' ),
'validate_callback' => 'rest_validate_request_arg',
);
return $params;
}
}

View File

@@ -149,6 +149,11 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
* @return WP_Error|boolean
*/
public function get_item_permissions_check( $request ) {
$review = $this->get_review( (int) $request['id'] );
if ( is_wp_error( $review ) ) {
return $review;
}
if ( ! wc_rest_check_product_reviews_permissions( 'read', (int) $request['id'] ) ) {
return new WP_Error( 'woocommerce_rest_cannot_view', __( 'Sorry, you cannot view this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
@@ -177,6 +182,11 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
* @return WP_Error|boolean
*/
public function update_item_permissions_check( $request ) {
$review = $this->get_review( (int) $request['id'] );
if ( is_wp_error( $review ) ) {
return $review;
}
if ( ! wc_rest_check_product_reviews_permissions( 'edit', (int) $request['id'] ) ) {
return new WP_Error( 'woocommerce_rest_cannot_edit', __( 'Sorry, you cannot edit this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
@@ -191,6 +201,11 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
* @return WP_Error|boolean
*/
public function delete_item_permissions_check( $request ) {
$review = $this->get_review( (int) $request['id'] );
if ( is_wp_error( $review ) ) {
return $review;
}
if ( ! wc_rest_check_product_reviews_permissions( 'delete', (int) $request['id'] ) ) {
return new WP_Error( 'woocommerce_rest_cannot_delete', __( 'Sorry, you cannot delete this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
@@ -1057,13 +1072,11 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
}
$review = get_comment( $id );
if ( empty( $review ) ) {
if ( empty( $review ) || 'review' !== get_comment_type( $id ) ) {
return $error;
}
if ( ! empty( $review->comment_post_ID ) ) {
$post = get_post( (int) $review->comment_post_ID );
if ( 'product' !== get_post_type( (int) $review->comment_post_ID ) ) {
return new WP_Error( 'woocommerce_rest_product_invalid_id', __( 'Invalid product ID.', 'woocommerce' ), array( 'status' => 404 ) );
}

View File

@@ -108,6 +108,7 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
'description' => wc_format_content( $object->get_description() ),
'permalink' => $object->get_permalink(),
'sku' => $object->get_sku(),
'global_unique_id' => $object->get_global_unique_id(),
'price' => $object->get_price(),
'regular_price' => $object->get_regular_price(),
'sale_price' => $object->get_sale_price(),
@@ -193,6 +194,11 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
$variation->set_sku( wc_clean( $request['sku'] ) );
}
// Unique ID.
if ( isset( $request['global_unique_id'] ) ) {
$variation->set_global_unique_id( wc_clean( $request['global_unique_id'] ) );
}
// Thumbnail.
if ( isset( $request['image'] ) ) {
if ( is_array( $request['image'] ) ) {
@@ -535,7 +541,12 @@ class WC_REST_Product_Variations_Controller extends WC_REST_Product_Variations_V
'readonly' => true,
),
'sku' => array(
'description' => __( 'Unique identifier.', 'woocommerce' ),
'description' => __( 'Stock Keeping Unit.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'global_unique_id' => array(
'description' => __( 'GTIN, UPC, EAN or ISBN.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),

View File

@@ -561,6 +561,11 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
$product->set_sku( wc_clean( $request['sku'] ) );
}
// Unique ID.
if ( isset( $request['global_unique_id'] ) ) {
$product->set_global_unique_id( wc_clean( $request['global_unique_id'] ) );
}
// Attributes.
if ( isset( $request['attributes'] ) ) {
$attributes = array();
@@ -987,7 +992,12 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
'context' => array( 'view', 'edit' ),
),
'sku' => array(
'description' => __( 'Unique identifier.', 'woocommerce' ),
'description' => __( 'Stock Keeping Unit.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
'global_unique_id' => array(
'description' => __( 'GTIN, UPC, EAN or ISBN.', 'woocommerce' ),
'type' => 'string',
'context' => array( 'view', 'edit' ),
),
@@ -1662,6 +1672,10 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
$data['post_password'] = $product->get_post_password( $context );
}
if ( in_array( 'global_unique_id', $fields, true ) ) {
$data['global_unique_id'] = $product->get_global_unique_id( $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

@@ -28,7 +28,7 @@ class Server {
/**
* Hook into WordPress ready to init the REST API as needed.
*/
public function init() {
public function init() { // phpcs:ignore WooCommerce.Functions.InternalInjectionMethod -- Not an injection method.
add_action( 'rest_api_init', array( $this, 'register_rest_routes' ), 10 );
\WC_REST_System_Status_V2_Controller::register_cache_clean();
@@ -57,13 +57,19 @@ class Server {
* @return array List of Namespaces and Main controller classes.
*/
protected function get_rest_namespaces() {
/**
* Filter the list of REST API controllers to load.
*
* @since 4.5.0
* @param array $controllers List of $namespace => $controllers to load.
*/
return apply_filters(
'woocommerce_rest_api_get_rest_namespaces',
array(
'wc/v1' => $this->get_v1_controllers(),
'wc/v2' => $this->get_v2_controllers(),
'wc/v3' => $this->get_v3_controllers(),
'wc-telemetry' => $this->get_telemetry_controllers(),
'wc/v1' => wc_rest_should_load_namespace( 'wc/v1' ) ? $this->get_v1_controllers() : array(),
'wc/v2' => wc_rest_should_load_namespace( 'wc/v2' ) ? $this->get_v2_controllers() : array(),
'wc/v3' => wc_rest_should_load_namespace( 'wc/v3' ) ? $this->get_v3_controllers() : array(),
'wc-telemetry' => wc_rest_should_load_namespace( 'wc-telemetry' ) ? $this->get_telemetry_controllers() : array(),
)
);
}
@@ -157,6 +163,7 @@ class Server {
'product-attribute-terms' => 'WC_REST_Product_Attribute_Terms_Controller',
'product-attributes' => 'WC_REST_Product_Attributes_Controller',
'product-categories' => 'WC_REST_Product_Categories_Controller',
'product-custom-fields' => 'WC_REST_Product_Custom_Fields_Controller',
'product-reviews' => 'WC_REST_Product_Reviews_Controller',
'product-shipping-classes' => 'WC_REST_Product_Shipping_Classes_Controller',
'product-tags' => 'WC_REST_Product_Tags_Controller',