PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.9.15
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.9.15
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 / API / Product_Brands_Controller.php

Product_Brands_Controller.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.9.15, at includes/API/Product_Brands_Controller.php

198 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Product_Brands_Controller.
4 *
5 * @package WCPOS\WooCommercePOS
6 */
7
8 namespace WCPOS\WooCommercePOS\API;
9
10 \defined( 'ABSPATH' ) || die;
11
12 if ( ! class_exists( 'WC_REST_Product_Brands_Controller' ) ) {
13 return;
14 }
15
16 use Exception;
17 use WC_REST_Product_Brands_Controller;
18 use WCPOS\WooCommercePOS\Logger;
19 use WP_Error;
20 use WP_REST_Request;
21 use WP_REST_Response;
22
23 /**
24 * Product Brands controller class.
25 *
26 * @NOTE: methods not prefixed with wcpos_ will override WC_REST_Product_Brands_Controller methods
27 */
28 class Product_Brands_Controller extends WC_REST_Product_Brands_Controller {
29 use Traits\Uuid_Handler;
30 use Traits\WCPOS_REST_API;
31
32 /**
33 * Endpoint namespace.
34 *
35 * @var string
36 */
37 protected $namespace = 'wcpos/v1';
38
39 /**
40 * Store the request object for use in lifecycle methods.
41 *
42 * @var WP_REST_Request
43 */
44 protected $wcpos_request;
45
46 /**
47 * Dispatch request to parent controller, or override if needed.
48 *
49 * @param mixed $dispatch_result Dispatch result, will be used if not empty.
50 * @param WP_REST_Request $request Request used to generate the response.
51 * @param string $route Route matched for the request.
52 * @param array $handler Route handler used for the request.
53 */
54 public function wcpos_dispatch_request( $dispatch_result, WP_REST_Request $request, $route, $handler ) {
55 $this->wcpos_request = $request;
56
57 add_filter( 'woocommerce_rest_prepare_product_brand', array( $this, 'wcpos_product_brands_response' ), 10, 3 );
58 add_filter( 'woocommerce_rest_product_brand_query', array( $this, 'wcpos_product_brand_query' ), 10, 2 );
59
60 /*
61 * Check if the request is for all brands and if the 'posts_per_page' is set to -1.
62 * Optimised query for getting all brand IDs.
63 */
64 if ( -1 == $request->get_param( 'posts_per_page' ) && null !== $request->get_param( 'fields' ) ) {
65 return $this->wcpos_get_all_posts( $request );
66 }
67
68 return $dispatch_result;
69 }
70
71 /**
72 * Filter the brand response.
73 *
74 * @param WP_REST_Response $response The response object.
75 * @param object $item The original term object.
76 * @param WP_REST_Request $request Request object.
77 *
78 * @return WP_REST_Response $response The response object.
79 */
80 public function wcpos_product_brands_response( WP_REST_Response $response, object $item, WP_REST_Request $request ): WP_REST_Response {
81 $data = $response->get_data();
82
83 // Make sure the term has a uuid.
84 $data['uuid'] = $this->get_term_uuid( $item );
85
86 // Reset the new response data.
87 $response->set_data( $data );
88
89 return $response;
90 }
91
92 /**
93 * Filter the brand query.
94 *
95 * @param array $args Query arguments.
96 * @param WP_REST_Request $request Request object.
97 */
98 public function wcpos_product_brand_query( array $args, WP_REST_Request $request ): array {
99 // Check for wcpos_include/wcpos_exclude parameter.
100 if ( isset( $request['wcpos_include'] ) || isset( $request['wcpos_exclude'] ) ) {
101 // Add a custom WHERE clause to the query.
102 add_filter( 'terms_clauses', array( $this, 'wcpos_terms_clauses_include_exclude' ), 10, 3 );
103 }
104
105 return $args;
106 }
107
108 /**
109 * Filters the terms query SQL clauses.
110 *
111 * @param string[] $clauses Associative array of the clauses for the query.
112 * @param string[] $taxonomies An array of taxonomy names.
113 * @param array $args An array of term query arguments.
114 *
115 * @return string[] $clauses
116 */
117 public function wcpos_terms_clauses_include_exclude( array $clauses, array $taxonomies, array $args ) {
118 global $wpdb;
119
120 // Handle 'wcpos_include'.
121 if ( ! empty( $this->wcpos_request['wcpos_include'] ) ) {
122 $include_ids = array_map( 'intval', $this->wcpos_request['wcpos_include'] );
123 $ids_format = implode( ',', array_fill( 0, \count( $include_ids ), '%d' ) );
124 $clauses['where'] .= $wpdb->prepare( " AND t.term_id IN ($ids_format) ", $include_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $ids_format is a safe placeholder string.
125 }
126
127 // Handle 'wcpos_exclude'.
128 if ( ! empty( $this->wcpos_request['wcpos_exclude'] ) ) {
129 $exclude_ids = array_map( 'intval', $this->wcpos_request['wcpos_exclude'] );
130 $ids_format = implode( ',', array_fill( 0, \count( $exclude_ids ), '%d' ) );
131 $clauses['where'] .= $wpdb->prepare( " AND t.term_id NOT IN ($ids_format) ", $exclude_ids ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- $ids_format is a safe placeholder string.
132 }
133
134 return $clauses;
135 }
136
137 /**
138 * Returns array of all product brand ids.
139 *
140 * @param WP_REST_Request $request Full details about the request.
141 *
142 * @return WP_Error|WP_REST_Response
143 */
144 public function wcpos_get_all_posts( $request ) {
145 // Start timing execution.
146 $start_time = microtime( true );
147 $modified_after = $request->get_param( 'modified_after' );
148
149 $args = array(
150 'taxonomy' => 'product_brand',
151 'hide_empty' => false,
152 'fields' => 'ids',
153 );
154
155 try {
156 /**
157 * Get all term IDs for the taxonomy.
158 *
159 * @TODO - terms don't have a modified date, it would be good to add a term_meta for last_update
160 * - ideally WooCommerce would provide a modified_after filter for terms
161 * - for now we'll just return empty for modified terms
162 */
163 $results = $modified_after ? array() : get_terms( $args );
164
165 // Format the response.
166 $formatted_results = array_map(
167 function ( $id ) {
168 return array( 'id' => (int) $id );
169 },
170 $results
171 );
172
173 // Get the total number of brands for the given criteria.
174 $total = \count( $formatted_results );
175
176 // Collect execution time and server load.
177 $execution_time = microtime( true ) - $start_time;
178 $execution_time_ms = number_format( $execution_time * 1000, 2 );
179 $server_load = $this->get_server_load();
180
181 $response = rest_ensure_response( $formatted_results );
182 $response->header( 'X-WP-Total', (string) $total );
183 $response->header( 'X-Execution-Time', $execution_time_ms . ' ms' );
184 $response->header( 'X-Server-Load', json_encode( $server_load ) );
185
186 return $response;
187 } catch ( Exception $e ) {
188 Logger::log( 'Error fetching product brand IDs: ' . $e->getMessage() );
189
190 return new \WP_Error(
191 'woocommerce_pos_rest_cannot_fetch',
192 'Error fetching product brand IDs.',
193 array( 'status' => 500 )
194 );
195 }
196 }
197 }
198