| 1 |
<?php |
| 2 |
/** |
| 3 |
* WooCommerce REST API Class, ie: /wc/v3/ endpoints. |
| 4 |
* |
| 5 |
* @author Paul Kilmurray <paul@kilbot.com> |
| 6 |
* |
| 7 |
* @see http://wcpos.com |
| 8 |
* @package WCPOS\WooCommercePOS |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace WCPOS\WooCommercePOS; |
| 12 |
|
| 13 |
use WCPOS\WooCommercePOS\Services\Settings; |
| 14 |
use WP_Query; |
| 15 |
|
| 16 |
/** |
| 17 |
* WC_API class. |
| 18 |
*/ |
| 19 |
class WC_API { |
| 20 |
/** |
| 21 |
* Indicates if the current request is for WooCommerce products. |
| 22 |
* |
| 23 |
* @var bool |
| 24 |
*/ |
| 25 |
private $is_woocommerce_rest_api_products_request = false; |
| 26 |
|
| 27 |
/** |
| 28 |
* Indicates if the current request is for WooCommerce variations. |
| 29 |
* |
| 30 |
* @var bool |
| 31 |
*/ |
| 32 |
private $is_woocommerce_rest_api_variations_request = false; |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor. |
| 36 |
*/ |
| 37 |
public function __construct() { |
| 38 |
$pos_only_products = Settings::instance()->pos_only_products_enabled(); |
| 39 |
|
| 40 |
if ( $pos_only_products ) { |
| 41 |
add_filter( 'rest_pre_dispatch', array( $this, 'set_woocommerce_rest_api_request_flags' ), 10, 3 ); |
| 42 |
add_filter( 'posts_where', array( $this, 'exclude_pos_only_products_from_api_response' ), 10, 2 ); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Set WooCommerce REST API request flags. |
| 48 |
* |
| 49 |
* @param mixed $result The dispatch result. |
| 50 |
* @param \WP_REST_Server $server The server instance. |
| 51 |
* @param \WP_REST_Request $request The request object. |
| 52 |
*/ |
| 53 |
public function set_woocommerce_rest_api_request_flags( $result, $server, $request ) { |
| 54 |
// WordPress matches REST routes case-insensitively, so a mixed-case path |
| 55 |
// still dispatches to the products controller — flag detection must not |
| 56 |
// be skippable by upper-casing the route. |
| 57 |
$route = strtolower( $request->get_route() ); |
| 58 |
|
| 59 |
if ( 0 === strpos( $route, '/wc/v3/products' ) || 0 === strpos( $route, '/wc/v2/products' ) || 0 === strpos( $route, '/wc/v1/products' ) ) { |
| 60 |
$this->is_woocommerce_rest_api_products_request = true; |
| 61 |
|
| 62 |
if ( false !== strpos( $route, '/variations' ) ) { |
| 63 |
$this->is_woocommerce_rest_api_variations_request = true; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
return $result; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Hide POS only products from the API response. |
| 72 |
* |
| 73 |
* @param string $where The WHERE clause of the query. |
| 74 |
* @param WP_Query $query The WP_Query instance (passed by reference). |
| 75 |
* |
| 76 |
* @return string |
| 77 |
*/ |
| 78 |
public function exclude_pos_only_products_from_api_response( $where, $query ) { |
| 79 |
global $wpdb; |
| 80 |
$settings_instance = Settings::instance(); |
| 81 |
|
| 82 |
// Hide POS only variations from the API response. |
| 83 |
if ( $this->is_woocommerce_rest_api_variations_request ) { |
| 84 |
$settings = $settings_instance->get_pos_only_variations_visibility_settings(); |
| 85 |
|
| 86 |
if ( isset( $settings['ids'] ) && ! empty( $settings['ids'] ) ) { |
| 87 |
$exclude_ids = array_map( 'intval', (array) $settings['ids'] ); |
| 88 |
$ids_format = implode( ',', array_fill( 0, \count( $exclude_ids ), '%d' ) ); |
| 89 |
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID NOT IN ($ids_format)", $exclude_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name and format are safe. |
| 90 |
} |
| 91 |
} elseif ( $this->is_woocommerce_rest_api_products_request ) { |
| 92 |
// Hide POS only products from the API response. |
| 93 |
$settings = $settings_instance->get_pos_only_product_visibility_settings(); |
| 94 |
|
| 95 |
if ( isset( $settings['ids'] ) && ! empty( $settings['ids'] ) ) { |
| 96 |
$exclude_ids = array_map( 'intval', (array) $settings['ids'] ); |
| 97 |
$ids_format = implode( ',', array_fill( 0, \count( $exclude_ids ), '%d' ) ); |
| 98 |
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID NOT IN ($ids_format)", $exclude_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name and format are safe. |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return $where; |
| 103 |
} |
| 104 |
} |
| 105 |
|