PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.0.3
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.0.3
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.0.3, at rest/SearchApi.php

294 lines 7.4 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 );
47
48 foreach ( $endpoints as $endpoint => $callback ) {
49 register_rest_route(
50 $base,
51 $endpoint,
52 array(
53 'methods' => WP_REST_Server::READABLE,
54 'callback' => array( $this, $callback ),
55 'permission_callback' => array( $this, 'permissions_check' ),
56 'args' => $this->get_collection_params(),
57 'schema' => array( $this, 'get_item_schema' ),
58 )
59 );
60 }
61 }
62
63 /**
64 * Search a list of items.
65 *
66 * @param \WP_REST_Request $request Full data about the request.
67 * @param string $search_function Search function name.
68 * @return \WP_REST_Response|\WP_Error
69 */
70 private function search_helper( $request, $search_function ) {
71 $search = '';
72
73 if ( is_string( $request['search'] ) ) {
74 $search = $request['search'];
75 }
76
77 $search = $this->validate_search_term( $search );
78 $data = array();
79 $results = Search::$search_function( $search );
80
81 if ( empty( $results ) ) {
82 return new \WP_Error( 'rest_search_invalid_search_term', __( 'Sorry, No items found with the search term.', 'disco' ), array( 'status' => 404 ) );
83 }
84
85 foreach ( $results as $result ) {
86 $response = $this->prepare_item_for_response( $result, $request );
87 $data[] = $this->prepare_response_for_collection( $response );
88 }
89
90 $response = rest_ensure_response( $data );
91 $total = count( $results );
92 $response->header( 'X-WP-Total', (int) $total );
93 $response->header( 'X-WP-TotalPages', (int) $total );
94
95 return $response;
96 }
97
98 /**
99 * Checks if a given request has access to read campaigns.
100 *
101 * @param \WP_REST_Request $request Full data about the request.
102 * @return bool
103 */
104 public function permissions_check( $request ) {// phpcs:ignore
105 return current_user_can( 'manage_options' );
106 }
107
108 /**
109 * Search a list of product items.
110 *
111 * @param \WP_REST_Request $request Full data about the request.
112 * @return \WP_REST_Response|\WP_Error
113 * @throws \Exception If invalid search term.
114 */
115 public function get_products( $request ) {
116 return $this->search_helper( $request, 'products' );
117 }
118
119 /**
120 * Search a list of category items.
121 *
122 * @param \WP_REST_Request $request Full data about the request.
123 * @return \WP_REST_Response|\WP_Error
124 */
125 public function get_categories( $request ) {
126 return $this->search_helper( $request, 'categories' );
127 }
128
129 /**
130 * Search a list of tag items.
131 *
132 * @param \WP_REST_Request $request Full data about the request.
133 * @return \WP_REST_Response|\WP_Error
134 */
135 public function get_tags( $request ) {
136 return $this->search_helper( $request, 'tags' );
137 }
138
139 /**
140 * Search a list of attribute items.
141 *
142 * @param \WP_REST_Request $request Full data about the request.
143 * @return \WP_REST_Response|\WP_Error
144 */
145 public function get_attributes( $request ) {
146 return $this->search_helper( $request, 'attributes' );
147 }
148
149 /**
150 * Search a list of coupon items.
151 *
152 * @param \WP_REST_Request $request Full data about the request.
153 * @return \WP_REST_Response|\WP_Error
154 */
155 public function get_coupons( $request ) {
156 return $this->search_helper( $request, 'coupons' );
157 }
158
159 /**
160 * Search a list of customer items.
161 *
162 * @param \WP_REST_Request $request Full data about the request.
163 * @return \WP_REST_Response|\WP_Error
164 */
165 public function get_customers( $request ) {
166 return $this->search_helper( $request, 'customers' );
167 }
168
169 /**
170 * Search a list of customer items.
171 *
172 * @param \WP_REST_Request $request Full data about the request.
173 * @return \WP_REST_Response|\WP_Error
174 */
175 public function get_states( $request ) {
176 return $this->search_helper( $request, 'states' );
177 }
178
179 /**
180 * Search a list of customer items.
181 *
182 * @param \WP_REST_Request $request Full data about the request.
183 * @return \WP_REST_Response|\WP_Error
184 */
185 public function get_countries( $request ) {
186 return $this->search_helper( $request, 'countries' );
187 }
188
189 /**
190 * Prepares the item for the REST response.
191 *
192 * @param mixed $item WordPress's representation of the item.
193 * @param \WP_REST_Request $request Request object.
194 * @return \WP_Error|\WP_REST_Response
195 */
196 public function prepare_item_for_response( $item, $request ) {// phpcs:ignore
197 $data = array();
198
199 foreach ( array( 'id', 'name', 'sku', 'image' ) as $field ) {
200 if ( is_array( $item ) && array_key_exists( $field, $item ) ) {
201 $data[ $field ] = $item[ $field ];
202 } else {
203 $data[ $field ] = null;
204 }
205 }
206
207 return rest_ensure_response( $data );
208 }
209
210 /**
211 * Retrieves the campaign schema, conforming to JSON Schema.
212 *
213 * @return array
214 */
215 public function get_item_schema() {
216 if ( $this->schema ) {
217 return $this->add_additional_fields_schema( $this->schema );
218 }
219
220 $schema = array(
221 '$schema' => 'http://json-schema.org/draft-04/schema#',
222 'title' => 'search',
223 'type' => 'object',
224 'properties' => array(
225 'id' => array(
226 'description' => __( 'Unique identifier for the object.', 'disco' ),
227 'type' => 'integer',
228 'context' => array( 'view' ),
229 'readonly' => true,
230 ),
231 'name' => array(
232 'description' => __( 'Name of the object.', 'disco' ),
233 'type' => 'string',
234 'context' => array( 'view' ),
235 'readonly' => true,
236 'arg_options' => array(
237 'sanitize_callback' => 'sanitize_text_field',
238 ),
239 ),
240 'sku' => array(
241 'description' => __( 'SKU of the object.', 'disco' ),
242 'type' => 'string',
243 'context' => array( 'view' ),
244 'readonly' => true,
245 'arg_options' => array(
246 'sanitize_callback' => 'sanitize_text_field',
247 ),
248 ),
249 'image' => array(
250 'description' => __( 'Image url of the object.', 'disco' ),
251 'type' => 'string',
252 'context' => array( 'view' ),
253 'readonly' => true,
254 'arg_options' => array(
255 'sanitize_callback' => 'sanitize_text_field',
256 ),
257 ),
258 ),
259 );
260
261 $this->schema = $schema;
262
263 return $this->add_additional_fields_schema( $this->schema );
264 }
265
266 /**
267 * Retrieves the query params for collections.
268 *
269 * @return array
270 */
271 public function get_collection_params() {
272 $params = parent::get_collection_params();
273
274 unset( $params['page'], $params['per_page'] );
275
276 return $params;
277 }
278
279 /**
280 * Validate empty search term.
281 *
282 * @param string $search Search Term.
283 * @return string
284 */
285 private function validate_search_term( $search ) {
286 if ( empty( $search ) ) {
287 return '';
288 }
289
290 return sanitize_text_field( $search );
291 }
292
293 }
294