PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.27
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.27
1.10.27 1.10.26 1.10.25 1.10.24 1.10.23 1.10.22 1.10.21 1.10.20 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 All 171 releases
woocommerce-pos / includes / WC_API.php

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

115 lines 4.0 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 <[email protected]>
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 // `wp wc product list` dispatches these same routes. A WP-CLI command is the
41 // merchant operating the shop, like wp-admin, so it lists every product.
42 $is_cli = \defined( 'WP_CLI' ) && WP_CLI;
43
44 if ( $pos_only_products && ! $is_cli ) {
45 add_filter( 'rest_pre_dispatch', array( $this, 'set_woocommerce_rest_api_request_flags' ), 10, 3 );
46 add_filter( 'posts_where', array( $this, 'exclude_pos_only_products_from_api_response' ), 10, 2 );
47 }
48 }
49
50 /**
51 * Set WooCommerce REST API request flags.
52 *
53 * @param mixed $result The dispatch result.
54 * @param \WP_REST_Server $server The server instance.
55 * @param \WP_REST_Request $request The request object.
56 */
57 public function set_woocommerce_rest_api_request_flags( $result, $server, $request ) {
58 // WordPress matches REST routes case-insensitively, so a mixed-case path
59 // still dispatches to the products controller — flag detection must not
60 // be skippable by upper-casing the route.
61 $route = strtolower( $request->get_route() );
62
63 // The flags describe THIS dispatch. A `/batch/v1` request dispatches its
64 // sub-requests one after another through this same filter, so a products
65 // sub-request must not leave the flags set for the one that follows it.
66 $this->is_woocommerce_rest_api_products_request = false;
67 $this->is_woocommerce_rest_api_variations_request = false;
68
69 if ( 0 === strpos( $route, '/wc/v3/products' ) || 0 === strpos( $route, '/wc/v2/products' ) || 0 === strpos( $route, '/wc/v1/products' ) ) {
70 $this->is_woocommerce_rest_api_products_request = true;
71
72 if ( false !== strpos( $route, '/variations' ) ) {
73 $this->is_woocommerce_rest_api_variations_request = true;
74 }
75 }
76
77 return $result;
78 }
79
80 /**
81 * Hide POS only products from the API response.
82 *
83 * @param string $where The WHERE clause of the query.
84 * @param WP_Query $query The WP_Query instance (passed by reference).
85 *
86 * @return string
87 */
88 public function exclude_pos_only_products_from_api_response( $where, $query ) {
89 global $wpdb;
90 $settings_instance = Settings::instance();
91
92 // Hide POS only variations from the API response.
93 if ( $this->is_woocommerce_rest_api_variations_request ) {
94 $settings = $settings_instance->get_pos_only_variations_visibility_settings();
95
96 if ( isset( $settings['ids'] ) && ! empty( $settings['ids'] ) ) {
97 $exclude_ids = array_map( 'intval', (array) $settings['ids'] );
98 $ids_format = implode( ',', array_fill( 0, \count( $exclude_ids ), '%d' ) );
99 $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.
100 }
101 } elseif ( $this->is_woocommerce_rest_api_products_request ) {
102 // Hide POS only products from the API response.
103 $settings = $settings_instance->get_pos_only_product_visibility_settings();
104
105 if ( isset( $settings['ids'] ) && ! empty( $settings['ids'] ) ) {
106 $exclude_ids = array_map( 'intval', (array) $settings['ids'] );
107 $ids_format = implode( ',', array_fill( 0, \count( $exclude_ids ), '%d' ) );
108 $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.
109 }
110 }
111
112 return $where;
113 }
114 }
115