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