2018-12-12 13:35:56 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* REST API Products Controller
|
|
|
|
*
|
|
|
|
* Handles requests to /products/*
|
|
|
|
*
|
|
|
|
* @package WooCommerce Admin/API
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Products controller.
|
|
|
|
*
|
|
|
|
* @package WooCommerce Admin/API
|
|
|
|
* @extends WC_REST_Products_Controller
|
|
|
|
*/
|
|
|
|
class WC_Admin_REST_Products_Controller extends WC_REST_Products_Controller {
|
|
|
|
|
2019-01-18 02:52:58 +00:00
|
|
|
/**
|
|
|
|
* Endpoint namespace.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $namespace = 'wc/v4';
|
|
|
|
|
2018-12-12 13:35:56 +00:00
|
|
|
/**
|
|
|
|
* Adds properties that can be embed via ?_embed=1.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_item_schema() {
|
|
|
|
$schema = parent::get_item_schema();
|
|
|
|
|
|
|
|
$properties_to_embed = array(
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'slug',
|
|
|
|
'permalink',
|
|
|
|
'images',
|
|
|
|
'description',
|
|
|
|
'short_description',
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $properties_to_embed as $property ) {
|
|
|
|
$schema['properties'][ $property ]['context'][] = 'embed';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $schema;
|
|
|
|
}
|
|
|
|
|
2019-02-27 08:49:21 +00:00
|
|
|
/**
|
|
|
|
* Get the query params for collections.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_collection_params() {
|
2019-03-18 03:31:02 +00:00
|
|
|
$params = parent::get_collection_params();
|
|
|
|
$params['search'] = array(
|
|
|
|
'description' => __( 'Search by similar product name or sku.', 'woocommerce-admin' ),
|
2019-02-27 08:49:21 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'validate_callback' => 'rest_validate_request_arg',
|
|
|
|
);
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2019-03-18 03:31:02 +00:00
|
|
|
|
2019-02-27 08:49:21 +00:00
|
|
|
/**
|
2019-03-18 03:31:02 +00:00
|
|
|
* Add product name and sku filtering to the WC API.
|
2019-02-27 08:49:21 +00:00
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request Request data.
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function prepare_objects_query( $request ) {
|
|
|
|
$args = parent::prepare_objects_query( $request );
|
|
|
|
|
2019-03-18 03:31:02 +00:00
|
|
|
if ( ! empty( $request['search'] ) ) {
|
|
|
|
$args['search'] = $request['search'];
|
|
|
|
unset( $args['s'] );
|
2019-02-27 08:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $args;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a collection of posts and add the post title filter option to WP_Query.
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request Full details about the request.
|
|
|
|
* @return WP_Error|WP_REST_Response
|
|
|
|
*/
|
|
|
|
public function get_items( $request ) {
|
2019-03-18 03:31:02 +00:00
|
|
|
add_filter( 'posts_where', array( __CLASS__, 'add_wp_query_product_search_filter' ), 10, 2 );
|
|
|
|
add_filter( 'posts_join', array( __CLASS__, 'add_wp_query_product_search_join' ), 10, 2 );
|
2019-02-27 08:49:21 +00:00
|
|
|
$response = parent::get_items( $request );
|
2019-03-18 03:31:02 +00:00
|
|
|
remove_filter( 'posts_where', array( __CLASS__, 'add_wp_query_product_search_filter' ), 10 );
|
|
|
|
remove_filter( 'posts_join', array( __CLASS__, 'add_wp_query_product_search_join' ), 10 );
|
2019-02-27 08:49:21 +00:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-03-18 03:31:02 +00:00
|
|
|
* Allow searching by product name or sku in WP Query
|
2019-02-27 08:49:21 +00:00
|
|
|
*
|
|
|
|
* @param string $where Where clause used to search posts.
|
|
|
|
* @param object $wp_query WP_Query object.
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-03-18 03:31:02 +00:00
|
|
|
public static function add_wp_query_product_search_filter( $where, $wp_query ) {
|
2019-02-27 08:49:21 +00:00
|
|
|
global $wpdb;
|
|
|
|
|
2019-03-18 03:31:02 +00:00
|
|
|
$search = trim( $wp_query->get( 'search' ) );
|
|
|
|
if ( $search ) {
|
|
|
|
$search = $wpdb->esc_like( $search );
|
|
|
|
$search = "'%" . $search . "%'";
|
|
|
|
$where .= ' AND (' . $wpdb->posts . '.post_title LIKE ' . $search;
|
|
|
|
$where .= wc_product_sku_enabled() ? ' OR ps_post_meta.meta_key = "_sku" AND ps_post_meta.meta_value LIKE ' . $search . ')' : ')';
|
2019-02-27 08:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $where;
|
|
|
|
}
|
|
|
|
|
2019-03-18 03:31:02 +00:00
|
|
|
/**
|
|
|
|
* Join posts meta table when product search is present and meta query is not present.
|
|
|
|
*
|
|
|
|
* @param string $join Join clause used to search posts.
|
|
|
|
* @param object $wp_query WP_Query object.
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function add_wp_query_product_search_join( $join, $wp_query ) {
|
|
|
|
global $wpdb;
|
|
|
|
|
|
|
|
$search = trim( $wp_query->get( 'search' ) );
|
|
|
|
if ( $search && wc_product_sku_enabled() ) {
|
|
|
|
$join .= ' INNER JOIN ' . $wpdb->postmeta . ' AS ps_post_meta ON ps_post_meta.post_id = ' . $wpdb->posts . '.ID';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $join;
|
|
|
|
}
|
|
|
|
|
2018-12-12 13:35:56 +00:00
|
|
|
}
|