PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
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 1.3.45 All 177 releases
disco / rest / DropDownApi.php

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

197 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * DropDown 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\DropDown\DiscountDropDown;
15 use Disco\App\DropDown\DropDown;
16 use Disco\App\DropDown\StoreDropDown;
17 use WP_REST_Controller;
18 use WP_REST_Server;
19
20 /**
21 * Class SearchApi
22 *
23 * @package Disco
24 * @subpackage \Rest
25 * @author Ohidul Islam <wahid0003@gmail.com>
26 * @link https://webappick.com
27 * @license https://opensource.org/licenses/gpl-license.php GNU Public License
28 * @category Rest
29 */
30 class DropDownApi extends WP_REST_Controller {
31
32 /**
33 * Registers the routes for the objects of the controller.
34 *
35 * @return void
36 */
37 public function register_routes() {
38 register_rest_route(
39 Api::NAMESPACE_NAME . '/' . Api::VERSION,
40 '/' . Api::DROPDOWN_ROUTE_NAME . '/',
41 array(
42 array(
43 'methods' => WP_REST_Server::READABLE,
44 'callback' => array( $this, 'get_items' ),
45 'permission_callback' => array( $this, 'permissions_check' ),
46 'args' => array(
47 'context' => $this->get_context_param( array( 'default' => 'view' ) ),
48 ),
49 ),
50 'schema' => array( $this, 'get_item_schema' ),
51 )
52 );
53 }
54
55 /**
56 * Checks if a given request has access to read campaigns.
57 *
58 * @param \WP_REST_Request $request Full data about the request.
59 * @return bool
60 */
61 public function permissions_check( $request ) { // phpcs:ignore
62 return current_user_can( 'manage_options' ) || current_user_can( 'manage_woocommerce' ); // phpcs:ignore WordPress.WP.Capabilities.Unknown
63 }
64
65 /**
66 * Search a list of product items.
67 *
68 * @param \WP_REST_Request $request Full data about the request.
69 * @return \WP_REST_Response|\WP_Error
70 * @throws \Exception If the search term is not set.
71 */
72 public function get_items( $request ) {
73 $search = '';
74
75 if ( is_string( $request['search'] ) ) {
76 $search = sanitize_text_field( $request['search'] );
77 }
78
79 $dropdown_methods = array(
80 'discount_intents' => DiscountDropDown::discount_intents(),
81 'discount_types' => DiscountDropDown::discount_types(),
82 'discount_based_on' => DiscountDropDown::discount_based_on(),
83 'bogo_types' => DiscountDropDown::bogo_types(),
84 'discount_methods' => DiscountDropDown::discount_methods(),
85 'products' => DiscountDropDown::products(),
86 'conditions' => DropDown::conditions(),
87 'filters' => DropDown::filters(),
88 'order_status' => StoreDropDown::order_status(),
89 'user_roles' => StoreDropDown::user_roles(),
90 'payment_methods' => StoreDropDown::payment_methods(),
91 'countries' => StoreDropDown::countries(),
92 );
93
94 // If the search term is not set, return an error.
95 if ( empty( $request['search'] ) || !array_key_exists( $search, $dropdown_methods ) ) {
96 return new \WP_Error(
97 'rest_dropdown_invalid_search_term',
98 /* translators: %s: List of valid dropdown values. */
99 esc_html( sprintf( __( 'Sorry, Invalid dropdown list. Search term should be any one of these values [ %s ].', 'disco' ), implode( ', ', array_keys( $dropdown_methods ) ) ) ),
100 array( 'status' => 404 )
101 );
102 }
103
104 $result = array(
105 'name' => $search,
106 'values' => $dropdown_methods[ $search ],
107 );
108
109 $response = $this->prepare_item_for_response( $result, $request );
110 $data = $this->prepare_response_for_collection( $response );
111
112 $total = count( $result['values'] );
113 $response = rest_ensure_response( $data );
114
115 $response->header( 'X-WP-Total', (int) $total );
116 $response->header( 'X-WP-TotalPages', (int) $total );
117
118 return $response;
119 }
120
121 /**
122 * Prepares the item for the REST response.
123 *
124 * @param array $item WordPress's representation of the item.
125 * @param \WP_REST_Request $request Request object.
126 * @return \WP_Error|\WP_REST_Response
127 */
128 public function prepare_item_for_response( $item, $request ) {
129 $data = array();
130 $fields = $this->get_fields_for_response( $request );
131
132 if ( in_array( 'name', $fields, true ) && isset( $item['name'] ) ) {
133 $data['name'] = $item['name'];
134 }
135
136 if ( in_array( 'values', $fields, true ) && isset( $item['values'] ) ) {
137 $data['values'] = $item['values'];
138 }
139
140 $data = $this->filter_response_by_context( $data, 'view' );
141
142 return rest_ensure_response( $data );
143 }
144
145 /**
146 * Retrieves the campaign schema, conforming to JSON Schema.
147 *
148 * @return array
149 */
150 public function get_item_schema() {
151 if ( $this->schema ) {
152 return $this->add_additional_fields_schema( $this->schema );
153 }
154
155 $schema = array(
156 '$schema' => 'http://json-schema.org/draft-04/schema#',
157 'title' => 'dropdown',
158 'type' => 'object',
159 'properties' => array(
160 'name' => array(
161 'description' => __( 'Name for the object.', 'disco' ),
162 'type' => 'string',
163 'context' => array( 'view' ),
164 'readonly' => true,
165 ),
166 'values' => array(
167 'description' => __( 'Values of the object.', 'disco' ),
168 'type' => 'array',
169 'context' => array( 'view' ),
170 'readonly' => true,
171 'arg_options' => array(
172 'sanitize_callback' => 'sanitize_text_field',
173 ),
174 ),
175 ),
176 );
177
178 $this->schema = $schema;
179
180 return $this->add_additional_fields_schema( $this->schema );
181 }
182
183 /**
184 * Retrieves the query params for collections.
185 *
186 * @return array
187 */
188 public function get_collection_params() {
189 $params = parent::get_collection_params();
190
191 unset( $params['page'], $params['per_page'] );
192
193 return $params;
194 }
195
196 }
197