plugin updates
This commit is contained in:
@@ -180,15 +180,15 @@ abstract class WC_REST_Controller extends WP_REST_Controller {
|
||||
$limit = apply_filters( 'woocommerce_rest_batch_items_limit', 100, $this->get_normalized_rest_base() );
|
||||
$total = 0;
|
||||
|
||||
if ( ! empty( $items['create'] ) ) {
|
||||
if ( ! empty( $items['create'] ) && is_countable( $items['create'] ) ) {
|
||||
$total += count( $items['create'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $items['update'] ) ) {
|
||||
if ( ! empty( $items['update'] ) && is_countable( $items['update'] ) ) {
|
||||
$total += count( $items['update'] );
|
||||
}
|
||||
|
||||
if ( ! empty( $items['delete'] ) ) {
|
||||
if ( ! empty( $items['delete'] ) && is_countable( $items['delete'] ) ) {
|
||||
$total += count( $items['delete'] );
|
||||
}
|
||||
|
||||
|
||||
@@ -111,6 +111,7 @@ class WC_REST_Data_Currencies_Controller extends WC_REST_Data_Controller {
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$currencies = get_woocommerce_currencies();
|
||||
$data = array();
|
||||
foreach ( array_keys( $currencies ) as $code ) {
|
||||
$currency = $this->get_currency( $code, $request );
|
||||
$response = $this->prepare_item_for_response( $currency, $request );
|
||||
|
||||
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
/**
|
||||
* REST API Layout Templates controller
|
||||
*
|
||||
* Handles requests to /layout-templates.
|
||||
*
|
||||
* @package WooCommerce\RestApi
|
||||
* @since 8.6.0
|
||||
*/
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
use Automattic\WooCommerce\LayoutTemplates\LayoutTemplateRegistry;
|
||||
|
||||
/**
|
||||
* REST API Layout Templates controller class.
|
||||
*/
|
||||
class WC_REST_Layout_Templates_Controller extends WC_REST_Controller {
|
||||
|
||||
/**
|
||||
* Endpoint namespace.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $namespace = 'wc/v3';
|
||||
|
||||
/**
|
||||
* Route base.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'layout-templates';
|
||||
|
||||
/**
|
||||
* Register the routes for template layouts.
|
||||
*/
|
||||
public function register_routes() {
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base,
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_items' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => array(
|
||||
'area' => array(
|
||||
'description' => __( 'Area to get templates for.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
'default' => '',
|
||||
),
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/(?P<id>\w[\w\s\-]*)',
|
||||
array(
|
||||
'args' => array(
|
||||
'id' => array(
|
||||
'description' => __( 'Unique identifier for the resource.', 'woocommerce' ),
|
||||
'type' => 'string',
|
||||
),
|
||||
),
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_item' ),
|
||||
'permission_callback' => array( $this, 'get_item_permissions_check' ),
|
||||
'args' => array(),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read template layouts.
|
||||
*
|
||||
* @param WP_REST_Request $request The request.
|
||||
*/
|
||||
public function get_items_permissions_check( $request ): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a given request has access to read a template layout.
|
||||
*
|
||||
* @param WP_REST_Request $request The request.
|
||||
*/
|
||||
public function get_item_permissions_check( $request ): bool {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle request for template layouts.
|
||||
*
|
||||
* @param WP_REST_Request $request The request.
|
||||
*/
|
||||
public function get_items( $request ) {
|
||||
$layout_templates = $this->get_layout_templates(
|
||||
array(
|
||||
'area' => $request['area'],
|
||||
)
|
||||
);
|
||||
|
||||
$response = rest_ensure_response( $layout_templates );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle request for a single template layout.
|
||||
*
|
||||
* @param WP_REST_Request $request The request.
|
||||
*/
|
||||
public function get_item( $request ) {
|
||||
$layout_templates = $this->get_layout_templates(
|
||||
array(
|
||||
'id' => $request['id'],
|
||||
)
|
||||
);
|
||||
|
||||
if ( count( $layout_templates ) !== 1 ) {
|
||||
return new WP_Error( 'woocommerce_rest_layout_template_invalid_id', __( 'Invalid layout template ID.', 'woocommerce' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$response = rest_ensure_response( current( $layout_templates ) );
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get layout templates.
|
||||
*
|
||||
* @param array $query_params Query params.
|
||||
*/
|
||||
private function get_layout_templates( array $query_params ): array {
|
||||
$layout_template_registry = wc_get_container()->get( LayoutTemplateRegistry::class );
|
||||
|
||||
return array_map(
|
||||
function( $layout_template ) {
|
||||
return $layout_template->to_json();
|
||||
},
|
||||
$layout_template_registry->instantiate_layout_templates( $query_params )
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -735,6 +735,7 @@ class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
|
||||
* @return array|WP_Error $prepared_review
|
||||
*/
|
||||
protected function prepare_item_for_database( $request ) {
|
||||
$prepared_review = array();
|
||||
if ( isset( $request['id'] ) ) {
|
||||
$prepared_review['comment_ID'] = (int) $request['id'];
|
||||
}
|
||||
|
||||
@@ -36,6 +36,34 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
||||
*/
|
||||
private $search_sku_in_product_lookup_table = '';
|
||||
|
||||
/**
|
||||
* Suggested product ids.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $suggested_products_ids = array();
|
||||
|
||||
/**
|
||||
* Register the routes for products.
|
||||
*/
|
||||
public function register_routes() {
|
||||
parent::register_routes();
|
||||
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base . '/suggested-products',
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_suggested_products' ),
|
||||
'permission_callback' => array( $this, 'get_items_permissions_check' ),
|
||||
'args' => $this->get_suggested_products_collection_params(),
|
||||
),
|
||||
'schema' => array( $this, 'get_public_item_schema' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the images for a product or product variation.
|
||||
*
|
||||
@@ -235,6 +263,15 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
||||
$args['meta_key'] = $ordering_args['meta_key']; // WPCS: slow query ok.
|
||||
}
|
||||
|
||||
/*
|
||||
* When the suggested products ids is not empty,
|
||||
* filter the query to return only the suggested products,
|
||||
* overwriting the post__in parameter.
|
||||
*/
|
||||
if ( ! empty( $this->suggested_products_ids ) ) {
|
||||
$args['post__in'] = $this->suggested_products_ids;
|
||||
}
|
||||
|
||||
return $args;
|
||||
}
|
||||
|
||||
@@ -1483,6 +1520,72 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add new options for the suggested-products endpoint.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_suggested_products_collection_params() {
|
||||
$params = parent::get_collection_params();
|
||||
|
||||
$params['categories'] = array(
|
||||
'description' => __( 'Limit result set to specific product categorie ids.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
);
|
||||
|
||||
$params['tags'] = array(
|
||||
'description' => __( 'Limit result set to specific product tag ids.', 'woocommerce' ),
|
||||
'type' => 'array',
|
||||
'items' => array(
|
||||
'type' => 'integer',
|
||||
),
|
||||
'default' => array(),
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
'sanitize_callback' => 'wp_parse_id_list',
|
||||
);
|
||||
|
||||
$params['limit'] = array(
|
||||
'description' => __( 'Limit result set to specific amount of suggested products.', 'woocommerce' ),
|
||||
'type' => 'integer',
|
||||
'default' => 5,
|
||||
'validate_callback' => 'rest_validate_request_arg',
|
||||
'sanitize_callback' => 'absint',
|
||||
);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the downloads for a product.
|
||||
*
|
||||
* @param WC_Product $product Product instance.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_downloads( $product ) {
|
||||
$downloads = array();
|
||||
|
||||
$context = isset( $this->request ) && isset( $this->request['context'] ) ? $this->request['context'] : 'view';
|
||||
|
||||
if ( $product->is_downloadable() || 'edit' === $context ) {
|
||||
foreach ( $product->get_downloads() as $file_id => $file ) {
|
||||
$downloads[] = array(
|
||||
'id' => $file_id, // MD5 hash.
|
||||
'name' => $file['name'],
|
||||
'file' => $file['file'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $downloads;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get product data.
|
||||
*
|
||||
@@ -1538,4 +1641,36 @@ class WC_REST_Products_Controller extends WC_REST_Products_V2_Controller {
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the suggested products.
|
||||
*
|
||||
* @param WP_REST_Request $request Request object.
|
||||
* @return object
|
||||
*/
|
||||
public function get_suggested_products( $request ) {
|
||||
$categories = $request->get_param( 'categories' );
|
||||
$tags = $request->get_param( 'tags' );
|
||||
$exclude_ids = $request->get_param( 'exclude' );
|
||||
$limit = $request->get_param( 'limit' ) ? $request->get_param( 'limit' ) : 5;
|
||||
|
||||
$data_store = WC_Data_Store::load( 'product' );
|
||||
$this->suggested_products_ids = $data_store->get_related_products(
|
||||
$categories,
|
||||
$tags,
|
||||
$exclude_ids,
|
||||
$limit,
|
||||
null // No need to pass the product ID.
|
||||
);
|
||||
|
||||
// When no suggested products are found, return an empty array.
|
||||
if ( empty( $this->suggested_products_ids ) ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
// Ensure to respect the limit, since the data store may return more than the limit.
|
||||
$this->suggested_products_ids = array_slice( $this->suggested_products_ids, 0, $limit );
|
||||
|
||||
return parent::get_items( $request );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user