PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.3.16
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.3.16
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 / SettingsApi.php

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

317 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 * Settings 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\Settings;
15 use WP_REST_Controller;
16 use WP_REST_Server;
17
18 /**
19 * Class SettingsApi
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 SettingsApi 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 register_rest_route(
37 Api::NAMESPACE_NAME . '/' . Api::VERSION,
38 '/' . Api::SETTINGS_ROUTE_NAME . '/',
39 array(
40 array(
41 'methods' => WP_REST_Server::READABLE,
42 'callback' => array(
43 $this,
44 'get_item',
45 ),
46 'permission_callback' => array(
47 $this,
48 'permissions_check',
49 ),
50 'args' => $this->get_collection_params(),
51 ),
52 array(
53 'methods' => WP_REST_Server::EDITABLE,
54 'callback' => array(
55 $this,
56 'update_item',
57 ),
58 'permission_callback' => array(
59 $this,
60 'permissions_check',
61 ),
62 'args' => $this->get_collection_params(),
63 ),
64 'schema' => array(
65 $this,
66 'get_item_schema',
67 ),
68 )
69 );
70 }
71
72 /**
73 * Checks if a given request has access to read contacts.
74 *
75 * @param \WP_REST_Request $request Full data about the request.
76 * @return bool
77 */
78 public function permissions_check( $request ) {// phpcs:ignore
79 return current_user_can( 'manage_options' ) || current_user_can( 'manage_woocommerce' ); // phpcs:ignore WordPress.WP.Capabilities.Unknown
80 }
81
82 /**
83 * Retrieves a list of address items.
84 *
85 * @param \WP_REST_Request $request Full data about the request.
86 * @return \WP_REST_Response|\WP_Error
87 * @throws \Exception If the settings is invalid.
88 */
89 public function get_item( $request ) {
90 $settings = Settings::get();
91
92 if ( is_wp_error( $settings ) ) {
93 return $settings;
94 }
95
96 $response = $this->prepare_item_for_response( $settings, $request );
97 $data = $this->prepare_response_for_collection( $response );
98
99 $total = 0;
100
101 if ( is_array( $settings ) ) {
102 $total = count( $settings );
103 }
104
105 $response = rest_ensure_response( $data );
106
107 $response->header( 'X-WP-Total', (int) $total );
108 $response->header( 'X-WP-TotalPages', (int) $total );
109
110 return $response;
111 }
112
113 /**
114 * Updates one item from the collection.
115 *
116 * @param \WP_REST_Request $request Full data about the request.
117 * @return \WP_Error|\WP_REST_Response
118 */
119 public function update_item( $request ) {
120 $prepared_settings = (array) $this->prepare_item_for_database( $request );
121
122 $updated = Settings::save( $prepared_settings );
123
124 if ( ! $updated ) {
125 return new \WP_Error(
126 'rest_not_updated',
127 __( 'Sorry, settings could not be updated.', 'disco' ),
128 array( 'status' => 400 )
129 );
130 }
131
132 $settings = Settings::get();
133
134 if ( is_wp_error( $settings ) ) {
135 return $settings;
136 }
137
138 $response = $this->prepare_item_for_response( $settings, $request );
139
140 return rest_ensure_response( $response );
141 }
142
143 /**
144 * Prepares the item for the REST response.
145 *
146 * @param array|string $item WordPress' representation of the item.
147 * @param \WP_REST_Request $request Request object.
148 * @return \WP_Error|\WP_REST_Response
149 */
150 public function prepare_item_for_response( $item, $request ) { // phpcs:ignore
151 $data = array();
152
153 $fields = $this->get_fields_for_response( $request );
154
155 if ( is_array( $fields ) && is_array( $item ) && in_array( 'show_strike_through', $fields, true ) ) {
156 $data['show_strike_through'] = $item['show_strike_through'] ?? array();
157 }
158
159 if ( is_array( $fields ) && is_array( $item ) && in_array( 'on_sale_badge', $fields, true ) ) {
160 $data['on_sale_badge'] = $item['on_sale_badge'] ?? '';
161 }
162
163 if ( is_array( $fields ) && is_array( $item ) && in_array( 'product_price_type', $fields, true ) ) {
164 $data['product_price_type'] = $item['product_price_type'];
165 }
166
167 if ( is_array( $fields ) && is_array( $item ) && in_array( 'min_max_discount_amount', $fields, true ) ) {
168 $data['min_max_discount_amount'] = $item['min_max_discount_amount'];
169 }
170
171 if ( is_array( $fields ) && is_array( $item ) && in_array( 'discount_priority_type', $fields, true ) ) {
172 $data['discount_priority_type'] = $item['discount_priority_type'] ?? '';
173 }
174
175 $context = 'view';
176
177 if ( is_string( $request['context'] ) ) {
178 $context = $request['context'];
179 }
180
181 $data = $this->filter_response_by_context( $data, $context );
182
183 return rest_ensure_response( $data );
184 }
185
186 /**
187 * Retrieves the contact schema, conforming to JSON Schema.
188 *
189 * @return array
190 */
191 public function get_item_schema() { // phpcs:ignore
192 if ( $this->schema ) {
193 return $this->add_additional_fields_schema( $this->schema );
194 }
195
196 $schema = array(
197 '$schema' => 'http://json-schema.org/draft-04/schema#',
198 'title' => 'contact',
199 'type' => 'object',
200 'properties' => array(
201 'product_price_type' => array(
202 'description' => __( 'Product Price Type.', 'disco' ),
203 'type' => 'string',
204 'context' => array(
205 'view',
206 'edit',
207 ),
208 ),
209 'min_max_discount_amount' => array(
210 'description' => __( 'Minimum, Maximum or Average Discount Amount.', 'disco' ),
211 'type' => 'string',
212 'context' => array(
213 'view',
214 'edit',
215 ),
216 ),
217 'discount_priority_type' => array(
218 'description' => __( 'Discount Priority Type.', 'disco' ),
219 'type' => 'string',
220 'enum' => array( 'both', 'woocommerce_coupon', 'disco_campaign' ),
221 'default' => 'both',
222 'context' => array(
223 'view',
224 'edit',
225 ),
226 ),
227 'on_sale_badge' => array(
228 'description' => __( 'On Sale Badge Text.', 'disco' ),
229 'type' => 'string',
230 'context' => array(
231 'view',
232 'edit',
233 ),
234 ),
235 'show_strike_through' => array(
236 'description' => __( 'Show Strike Through Price.', 'disco' ),
237 'type' => 'array',
238 'context' => array(
239 'view',
240 'edit',
241 ),
242 ),
243 ),
244 );
245
246 $this->schema = $schema;
247
248 return $this->add_additional_fields_schema( $this->schema );
249 }
250
251 /**
252 * Retrieves the query params for collections.
253 *
254 * @return array
255 */
256 public function get_collection_params() {
257 $params = parent::get_collection_params();
258
259 unset( $params['search'], $params['per_page'], $params['page'] );
260
261 return $params;
262 }
263
264 /**
265 * Get the setting, if the key is valid.
266 *
267 * @param string $key Supplied ID.
268 * @return array|string|\WP_Error
269 */
270 protected function get_settings( $key ) {
271 $setting = Settings::get( $key );
272
273 if ( ! $setting ) {
274 return new \WP_Error(
275 'rest_setting_invalid_key',
276 __( 'Invalid Setting Key.', 'disco' ),// phpcs:ignore
277 array( 'status' => 404 )
278 );
279 }
280
281 return $setting;
282 }
283
284 /**
285 * Prepares one item for create or update operation.
286 *
287 * @param \WP_REST_Request $request Request object.
288 * @return object
289 */
290 protected function prepare_item_for_database( $request ) {
291 $prepared = array();
292
293 if ( isset( $request['show_strike_through'] ) ) {
294 $prepared['show_strike_through'] = $request['show_strike_through'];
295 }
296
297 if ( isset( $request['on_sale_badge'] ) ) {
298 $prepared['on_sale_badge'] = $request['on_sale_badge'];
299 }
300
301 if ( isset( $request['discount_priority_type'] ) ) {
302 $prepared['discount_priority_type'] = $request['discount_priority_type'];
303 }
304
305 if ( isset( $request['product_price_type'] ) ) {
306 $prepared['product_price_type'] = $request['product_price_type'];
307 }
308
309 if ( isset( $request['min_max_discount_amount'] ) ) {
310 $prepared['min_max_discount_amount'] = $request['min_max_discount_amount'];
311 }
312
313 return (object) $prepared;
314 }
315
316 }
317