-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathWC_API.php
98 lines (82 loc) · 2.83 KB
/
WC_API.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
/**
* WooCommerce REST API Class, ie: /wc/v3/ endpoints.
*
* @author Paul Kilmurray <[email protected]>
*
* @see http://wcpos.com
* @package WCPOS\WooCommercePOS
*/
namespace WCPOS\WooCommercePOS;
use WP_Query;
use WCPOS\WooCommercePOS\Services\Settings;
/**
*
*/
class WC_API {
/**
* Indicates if the current request is for WooCommerce products.
*
* @var bool
*/
private $is_woocommerce_rest_api_products_request = false;
/**
* Indicates if the current request is for WooCommerce variations.
*
* @var bool
*/
private $is_woocommerce_rest_api_variations_request = false;
/**
*
*/
public function __construct() {
$pos_only_products = woocommerce_pos_get_settings( 'general', 'pos_only_products' );
if ( $pos_only_products ) {
add_filter( 'rest_pre_dispatch', array( $this, 'set_woocommerce_rest_api_request_flags' ), 10, 3 );
add_filter( 'posts_where', array( $this, 'exclude_pos_only_products_from_api_response' ), 10, 2 );
}
}
/**
*
*/
public function set_woocommerce_rest_api_request_flags( $result, $server, $request ) {
$route = $request->get_route();
if ( strpos( $route, '/wc/v3/products' ) === 0 || strpos( $route, '/wc/v2/products' ) === 0 || strpos( $route, '/wc/v1/products' ) === 0 ) {
$this->is_woocommerce_rest_api_products_request = true;
if ( strpos( $route, '/variations' ) !== false ) {
$this->is_woocommerce_rest_api_variations_request = true;
}
}
return $result;
}
/**
* Hide POS only products from the API response.
*
* @param string $where The WHERE clause of the query.
* @param WP_Query $query The WP_Query instance (passed by reference).
*
* @return string
*/
public function exclude_pos_only_products_from_api_response( $where, $query ) {
global $wpdb;
$settings_instance = Settings::instance();
// Hide POS only variations from the API response.
if ( ! $this->is_woocommerce_rest_api_variations_request ) {
$settings = $settings_instance->get_pos_only_variations_visibility_settings();
if ( isset( $settings['ids'] ) && ! empty( $settings['ids'] ) ) {
$exclude_ids = array_map( 'intval', (array) $settings['ids'] );
$ids_format = implode( ',', array_fill( 0, count( $exclude_ids ), '%d' ) );
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID NOT IN ($ids_format)", $exclude_ids );
}
// Hide POS only products from the API response.
} elseif ( $this->is_woocommerce_rest_api_products_request ) {
$settings = $settings_instance->get_pos_only_product_visibility_settings();
if ( isset( $settings['ids'] ) && ! empty( $settings['ids'] ) ) {
$exclude_ids = array_map( 'intval', (array) $settings['ids'] );
$ids_format = implode( ',', array_fill( 0, count( $exclude_ids ), '%d' ) );
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ID NOT IN ($ids_format)", $exclude_ids );
}
}
return $where;
}
}