PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.9.16
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.9.16
1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 1.10.1 1.10.0 1.9.17 1.9.15 1.9.16 1.9.14 All 163 releases
woocommerce-pos / includes / WC_API.php

WC_API.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.16, at includes/WC_API.php

102 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = woocommerce_pos_get_settings( 'general', 'pos_only_products' );
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 $route = $request->get_route();
55
56 if ( 0 === strpos( $route, '/wc/v3/products' ) || 0 === strpos( $route, '/wc/v2/products' ) || 0 === strpos( $route, '/wc/v1/products' ) ) {
57 $this->is_woocommerce_rest_api_products_request = true;
58
59 if ( false !== strpos( $route, '/variations' ) ) {
60 $this->is_woocommerce_rest_api_variations_request = true;
61 }
62 }
63
64 return $result;
65 }
66
67 /**
68 * Hide POS only products from the API response.
69 *
70 * @param string $where The WHERE clause of the query.
71 * @param WP_Query $query The WP_Query instance (passed by reference).
72 *
73 * @return string
74 */
75 public function exclude_pos_only_products_from_api_response( $where, $query ) {
76 global $wpdb;
77 $settings_instance = Settings::instance();
78
79 // Hide POS only variations from the API response.
80 if ( $this->is_woocommerce_rest_api_variations_request ) {
81 $settings = $settings_instance->get_pos_only_variations_visibility_settings();
82
83 if ( isset( $settings['ids'] ) && ! empty( $settings['ids'] ) ) {
84 $exclude_ids = array_map( 'intval', (array) $settings['ids'] );
85 $ids_format = implode( ',', array_fill( 0, \count( $exclude_ids ), '%d' ) );
86 $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.
87 }
88 } elseif ( $this->is_woocommerce_rest_api_products_request ) {
89 // Hide POS only products from the API response.
90 $settings = $settings_instance->get_pos_only_product_visibility_settings();
91
92 if ( isset( $settings['ids'] ) && ! empty( $settings['ids'] ) ) {
93 $exclude_ids = array_map( 'intval', (array) $settings['ids'] );
94 $ids_format = implode( ',', array_fill( 0, \count( $exclude_ids ), '%d' ) );
95 $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.
96 }
97 }
98
99 return $where;
100 }
101 }
102