PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.2.18
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.2.18
1.4.15 1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 All 178 releases
disco / rest / SearchApi.php

SearchApi.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.2.18, at rest/SearchApi.php

316 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Search API
5 *
6 * @package Disco
7 * @subpackage \Rest
8 * @since 1.0.0
9 * @category Rest
10 */
11
12 namespace Disco\Rest;
13
14 use Disco\App\Utility\Search;
15 use WP_REST_Controller;
16 use WP_REST_Server;
17
18 /**
19 * Class SearchApi
20 *
21 * @package Disco
22 * @subpackage \Rest
23 * @author Ohidul Islam <wahid0003@gmail.com>
24 * @link https://webappick.com
25 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
26 * @category Rest
27 */
28 class SearchApi extends WP_REST_Controller {
29
30 /**
31 * Registers the routes for the objects of the controller.
32 *
33 * @return void
34 */
35 public function register_routes() {
36 $base = Api::NAMESPACE_NAME . '/' . Api::VERSION . '/' . Api::SEARCH_ROUTE_NAME;
37 $endpoints = array(
38 '/product/' => 'get_products',
39 '/category/' => 'get_categories',
40 '/tag/' => 'get_tags',
41 '/attribute/' => 'get_attributes',
42 '/coupon/' => 'get_coupons',
43 '/customer/' => 'get_customers',
44 '/state/' => 'get_states',
45 '/country/' => 'get_countries',
46 '/user_name/' => 'get_user_name',
47 '/user_email/' => 'get_user_email',
48 );
49
50 foreach ( $endpoints as $endpoint => $callback ) {
51 register_rest_route(
52 $base,
53 $endpoint,
54 array(
55 'methods' => WP_REST_Server::READABLE,
56 'callback' => array( $this, $callback ),
57 'permission_callback' => array( $this, 'permissions_check' ),
58 'args' => $this->get_collection_params(),
59 'schema' => array( $this, 'get_item_schema' ),
60 )
61 );
62 }
63 }
64
65 /**
66 * Search a list of items.
67 *
68 * @param \WP_REST_Request $request Full data about the request.
69 * @param string $search_function Search function name.
70 * @return \WP_REST_Response|\WP_Error
71 */
72 private function search_helper( $request, $search_function ) {
73 $search = '';
74
75 if ( is_string( $request['search'] ) ) {
76 $search = $request['search'];
77 }
78
79 $search = $this->validate_search_term( $search );
80 $data = array();
81 $results = Search::$search_function( $search );
82
83 if ( empty( $results ) ) {
84 return new \WP_Error( 'rest_search_invalid_search_term', __( 'Sorry, No items found with the search term.', 'disco' ), array( 'status' => 404 ) );
85 }
86
87 foreach ( $results as $result ) {
88 $response = $this->prepare_item_for_response( $result, $request );
89 $data[] = $this->prepare_response_for_collection( $response );
90 }
91
92 $response = rest_ensure_response( $data );
93 $total = count( $results );
94 $response->header( 'X-WP-Total', (int) $total );
95 $response->header( 'X-WP-TotalPages', (int) $total );
96
97 return $response;
98 }
99
100 /**
101 * Checks if a given request has access to read campaigns.
102 *
103 * @param \WP_REST_Request $request Full data about the request.
104 * @return bool
105 */
106 public function permissions_check( $request ) {// phpcs:ignore
107 return current_user_can( 'manage_options' );
108 }
109
110 /**
111 * Search a list of product items.
112 *
113 * @param \WP_REST_Request $request Full data about the request.
114 * @return \WP_REST_Response|\WP_Error
115 * @throws \Exception If invalid search term.
116 */
117 public function get_products( $request ) {
118 return $this->search_helper( $request, 'products' );
119 }
120
121 /**
122 * Search a list of category items.
123 *
124 * @param \WP_REST_Request $request Full data about the request.
125 * @return \WP_REST_Response|\WP_Error
126 */
127 public function get_categories( $request ) {
128 return $this->search_helper( $request, 'categories' );
129 }
130
131 /**
132 * Search a list of tag items.
133 *
134 * @param \WP_REST_Request $request Full data about the request.
135 * @return \WP_REST_Response|\WP_Error
136 */
137 public function get_tags( $request ) {
138 return $this->search_helper( $request, 'tags' );
139 }
140
141 /**
142 * Search a list of attribute items.
143 *
144 * @param \WP_REST_Request $request Full data about the request.
145 * @return \WP_REST_Response|\WP_Error
146 */
147 public function get_attributes( $request ) {
148 return $this->search_helper( $request, 'attributes' );
149 }
150
151 /**
152 * Search a list of coupon items.
153 *
154 * @param \WP_REST_Request $request Full data about the request.
155 * @return \WP_REST_Response|\WP_Error
156 */
157 public function get_coupons( $request ) {
158 return $this->search_helper( $request, 'coupons' );
159 }
160
161 /**
162 * Search a list of customer items.
163 *
164 * @param \WP_REST_Request $request Full data about the request.
165 * @return \WP_REST_Response|\WP_Error
166 */
167 public function get_customers( $request ) {
168 return $this->search_helper( $request, 'customers' );
169 }
170
171 /**
172 * Search a list of customer items.
173 *
174 * @param \WP_REST_Request $request Full data about the request.
175 * @return \WP_REST_Response|\WP_Error
176 */
177 public function get_states( $request ) {
178 return $this->search_helper( $request, 'states' );
179 }
180
181 /**
182 * Search a list of customer items.
183 *
184 * @param \WP_REST_Request $request Full data about the request.
185 * @return \WP_REST_Response|\WP_Error
186 */
187 public function get_countries( $request ) {
188 return $this->search_helper( $request, 'countries' );
189 }
190
191 /**
192 * Search a list of user items.
193 *
194 * @param \WP_REST_Request $request Full data about the request.
195 * @return \WP_REST_Response|\WP_Error
196 */
197 public function get_user_name( $request ) {
198 return $this->search_helper( $request, 'user_name' );
199 }
200
201 /**
202 * Search a list of user items.
203 *
204 * @param \WP_REST_Request $request Full data about the request.
205 * @return \WP_REST_Response|\WP_Error
206 */
207 public function get_user_email( $request ) {
208 return $this->search_helper( $request, 'user_email' );
209 }
210
211 /**
212 * Prepares the item for the REST response.
213 *
214 * @param mixed $item WordPress's representation of the item.
215 * @param \WP_REST_Request $request Request object.
216 * @return \WP_Error|\WP_REST_Response
217 */
218 public function prepare_item_for_response( $item, $request ) {// phpcs:ignore
219 $data = array();
220
221 foreach ( array( 'id', 'name', 'sku', 'image' ) as $field ) {
222 if ( is_array( $item ) && array_key_exists( $field, $item ) ) {
223 $data[ $field ] = $item[ $field ];
224 } else {
225 $data[ $field ] = null;
226 }
227 }
228
229 return rest_ensure_response( $data );
230 }
231
232 /**
233 * Retrieves the campaign schema, conforming to JSON Schema.
234 *
235 * @return array
236 */
237 public function get_item_schema() {
238 if ( $this->schema ) {
239 return $this->add_additional_fields_schema( $this->schema );
240 }
241
242 $schema = array(
243 '$schema' => 'http://json-schema.org/draft-04/schema#',
244 'title' => 'search',
245 'type' => 'object',
246 'properties' => array(
247 'id' => array(
248 'description' => __( 'Unique identifier for the object.', 'disco' ),
249 'type' => 'integer',
250 'context' => array( 'view' ),
251 'readonly' => true,
252 ),
253 'name' => array(
254 'description' => __( 'Name of the object.', 'disco' ),
255 'type' => 'string',
256 'context' => array( 'view' ),
257 'readonly' => true,
258 'arg_options' => array(
259 'sanitize_callback' => 'sanitize_text_field',
260 ),
261 ),
262 'sku' => array(
263 'description' => __( 'SKU of the object.', 'disco' ),
264 'type' => 'string',
265 'context' => array( 'view' ),
266 'readonly' => true,
267 'arg_options' => array(
268 'sanitize_callback' => 'sanitize_text_field',
269 ),
270 ),
271 'image' => array(
272 'description' => __( 'Image url of the object.', 'disco' ),
273 'type' => 'string',
274 'context' => array( 'view' ),
275 'readonly' => true,
276 'arg_options' => array(
277 'sanitize_callback' => 'sanitize_text_field',
278 ),
279 ),
280 ),
281 );
282
283 $this->schema = $schema;
284
285 return $this->add_additional_fields_schema( $this->schema );
286 }
287
288 /**
289 * Retrieves the query params for collections.
290 *
291 * @return array
292 */
293 public function get_collection_params() {
294 $params = parent::get_collection_params();
295
296 unset( $params['page'], $params['per_page'] );
297
298 return $params;
299 }
300
301 /**
302 * Validate empty search term.
303 *
304 * @param string $search Search Term.
305 * @return string
306 */
307 private function validate_search_term( $search ) {
308 if ( empty( $search ) ) {
309 return '';
310 }
311
312 return sanitize_text_field( $search );
313 }
314
315 }
316