@@ -27,7 +27,7 @@ |
||
27 | 27 | <input type="checkbox" name="plugins[<?php echo esc_attr( $plugin['slug'] ); ?>]" value="<?php echo esc_attr( $plugin['file'] ); ?>" class="custom-control-input" |
28 | 28 | <?php |
29 | 29 | if ( is_plugin_active( $plugin['slug'] ) ) { |
30 | - echo 'checked';} |
|
30 | + echo 'checked';} |
|
31 | 31 | ?> |
32 | 32 | > |
33 | 33 | <label class="custom-control-label" for="ac-setting-updates"></label> |
@@ -7,7 +7,7 @@ discard block |
||
7 | 7 | */ |
8 | 8 | |
9 | 9 | if ( ! defined( 'ABSPATH' ) ) { |
10 | - exit; // Exit if accessed directly |
|
10 | + exit; // Exit if accessed directly |
|
11 | 11 | } |
12 | 12 | |
13 | 13 | /** |
@@ -15,22 +15,22 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class GetPaid_Meta_Box_Invoice_Shipping_Address { |
17 | 17 | |
18 | - /** |
|
19 | - * Output the metabox. |
|
20 | - * |
|
21 | - * @param WP_Post $post |
|
22 | - */ |
|
23 | - public static function output( $post ) { |
|
18 | + /** |
|
19 | + * Output the metabox. |
|
20 | + * |
|
21 | + * @param WP_Post $post |
|
22 | + */ |
|
23 | + public static function output( $post ) { |
|
24 | 24 | |
25 | - // Retrieve shipping address. |
|
26 | - $shipping_address = get_post_meta( $post->ID, 'shipping_address', true ); |
|
25 | + // Retrieve shipping address. |
|
26 | + $shipping_address = get_post_meta( $post->ID, 'shipping_address', true ); |
|
27 | 27 | |
28 | - // Abort if it is invalid. |
|
29 | - if ( ! is_array( $shipping_address ) ) { |
|
30 | - return; |
|
31 | - } |
|
28 | + // Abort if it is invalid. |
|
29 | + if ( ! is_array( $shipping_address ) ) { |
|
30 | + return; |
|
31 | + } |
|
32 | 32 | |
33 | - ?> |
|
33 | + ?> |
|
34 | 34 | |
35 | 35 | <div class="bsui"> |
36 | 36 | |
@@ -68,31 +68,31 @@ discard block |
||
68 | 68 | |
69 | 69 | <?php |
70 | 70 | |
71 | - } |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * Prepares a value. |
|
75 | - * |
|
76 | - * @param array $address |
|
77 | - * @param string $key |
|
78 | - * @return string |
|
79 | - */ |
|
80 | - public static function prepare_for_display( $address, $key ) { |
|
73 | + /** |
|
74 | + * Prepares a value. |
|
75 | + * |
|
76 | + * @param array $address |
|
77 | + * @param string $key |
|
78 | + * @return string |
|
79 | + */ |
|
80 | + public static function prepare_for_display( $address, $key ) { |
|
81 | 81 | |
82 | - // Prepare the value. |
|
83 | - $value = $address[ $key ]; |
|
82 | + // Prepare the value. |
|
83 | + $value = $address[ $key ]; |
|
84 | 84 | |
85 | - if ( $key == 'country' ) { |
|
86 | - $value = wpinv_country_name( $value ); |
|
87 | - } |
|
85 | + if ( $key == 'country' ) { |
|
86 | + $value = wpinv_country_name( $value ); |
|
87 | + } |
|
88 | 88 | |
89 | - if ( $key == 'state' ) { |
|
90 | - $country = isset( $address['country'] ) ? $address['country'] : wpinv_get_default_country(); |
|
91 | - $value = wpinv_state_name( $value, $country ); |
|
92 | - } |
|
89 | + if ( $key == 'state' ) { |
|
90 | + $country = isset( $address['country'] ) ? $address['country'] : wpinv_get_default_country(); |
|
91 | + $value = wpinv_state_name( $value, $country ); |
|
92 | + } |
|
93 | 93 | |
94 | - return esc_html( $value ); |
|
94 | + return esc_html( $value ); |
|
95 | 95 | |
96 | - } |
|
96 | + } |
|
97 | 97 | |
98 | 98 | } |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | */ |
9 | 9 | |
10 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
11 | - exit; // Exit if accessed directly |
|
11 | + exit; // Exit if accessed directly |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | /** |
@@ -17,10 +17,10 @@ discard block |
||
17 | 17 | class GetPaid_Meta_Box_Discount_Details { |
18 | 18 | |
19 | 19 | /** |
20 | - * Output the metabox. |
|
21 | - * |
|
22 | - * @param WP_Post $post |
|
23 | - */ |
|
20 | + * Output the metabox. |
|
21 | + * |
|
22 | + * @param WP_Post $post |
|
23 | + */ |
|
24 | 24 | public static function output( $post ) { |
25 | 25 | |
26 | 26 | // Prepare the discount. |
@@ -396,35 +396,35 @@ discard block |
||
396 | 396 | } |
397 | 397 | |
398 | 398 | /** |
399 | - * Save meta box data. |
|
400 | - * |
|
401 | - * @param int $post_id |
|
402 | - */ |
|
403 | - public static function save( $post_id ) { |
|
399 | + * Save meta box data. |
|
400 | + * |
|
401 | + * @param int $post_id |
|
402 | + */ |
|
403 | + public static function save( $post_id ) { |
|
404 | 404 | |
405 | 405 | // Prepare the discount. |
406 | 406 | $discount = new WPInv_Discount( $post_id ); |
407 | 407 | |
408 | 408 | // Load new data. |
409 | 409 | $discount->set_props( |
410 | - array( |
|
411 | - 'code' => isset( $_POST['wpinv_discount_code'] ) ? wpinv_clean( $_POST['wpinv_discount_code'] ) : null, |
|
412 | - 'amount' => isset( $_POST['wpinv_discount_amount'] ) ? floatval( $_POST['wpinv_discount_amount'] ) : null, |
|
413 | - 'start' => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null, |
|
414 | - 'expiration' => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null, |
|
415 | - 'is_single_use' => ! empty( $_POST['wpinv_discount_single_use'] ), |
|
410 | + array( |
|
411 | + 'code' => isset( $_POST['wpinv_discount_code'] ) ? wpinv_clean( $_POST['wpinv_discount_code'] ) : null, |
|
412 | + 'amount' => isset( $_POST['wpinv_discount_amount'] ) ? floatval( $_POST['wpinv_discount_amount'] ) : null, |
|
413 | + 'start' => isset( $_POST['wpinv_discount_start'] ) ? wpinv_clean( $_POST['wpinv_discount_start'] ) : null, |
|
414 | + 'expiration' => isset( $_POST['wpinv_discount_expiration'] ) ? wpinv_clean( $_POST['wpinv_discount_expiration'] ) : null, |
|
415 | + 'is_single_use' => ! empty( $_POST['wpinv_discount_single_use'] ), |
|
416 | 416 | 'type' => isset( $_POST['wpinv_discount_type'] ) ? wpinv_clean( $_POST['wpinv_discount_type'] ) : null, |
417 | - 'is_recurring' => ! empty( $_POST['wpinv_discount_recurring'] ), |
|
418 | - 'items' => isset( $_POST['wpinv_discount_items'] ) ? wpinv_clean( $_POST['wpinv_discount_items'] ) : array(), |
|
419 | - 'excluded_items' => isset( $_POST['wpinv_discount_excluded_items'] ) ? wpinv_clean( $_POST['wpinv_discount_excluded_items'] ) : array(), |
|
417 | + 'is_recurring' => ! empty( $_POST['wpinv_discount_recurring'] ), |
|
418 | + 'items' => isset( $_POST['wpinv_discount_items'] ) ? wpinv_clean( $_POST['wpinv_discount_items'] ) : array(), |
|
419 | + 'excluded_items' => isset( $_POST['wpinv_discount_excluded_items'] ) ? wpinv_clean( $_POST['wpinv_discount_excluded_items'] ) : array(), |
|
420 | 420 | 'required_items' => isset( $_POST['wpinv_discount_required_items'] ) ? wpinv_clean( $_POST['wpinv_discount_required_items'] ) : array(), |
421 | - 'max_uses' => isset( $_POST['wpinv_discount_max_uses'] ) ? intval( $_POST['wpinv_discount_max_uses'] ) : null, |
|
422 | - 'min_total' => isset( $_POST['wpinv_discount_min_total'] ) ? floatval( $_POST['wpinv_discount_min_total'] ) : null, |
|
423 | - 'max_total' => isset( $_POST['wpinv_discount_max_total'] ) ? floatval( $_POST['wpinv_discount_max_total'] ) : null, |
|
424 | - ) |
|
421 | + 'max_uses' => isset( $_POST['wpinv_discount_max_uses'] ) ? intval( $_POST['wpinv_discount_max_uses'] ) : null, |
|
422 | + 'min_total' => isset( $_POST['wpinv_discount_min_total'] ) ? floatval( $_POST['wpinv_discount_min_total'] ) : null, |
|
423 | + 'max_total' => isset( $_POST['wpinv_discount_max_total'] ) ? floatval( $_POST['wpinv_discount_max_total'] ) : null, |
|
424 | + ) |
|
425 | 425 | ); |
426 | 426 | |
427 | - $discount->save(); |
|
428 | - do_action( 'getpaid_discount_metabox_save', $post_id, $discount ); |
|
429 | - } |
|
427 | + $discount->save(); |
|
428 | + do_action( 'getpaid_discount_metabox_save', $post_id, $discount ); |
|
429 | + } |
|
430 | 430 | } |
@@ -24,14 +24,14 @@ discard block |
||
24 | 24 | } |
25 | 25 | |
26 | 26 | /** |
27 | - * Highlights sub menus. |
|
28 | - */ |
|
29 | - public function set_admin_menu_class() { |
|
30 | - global $current_screen, $parent_file, $submenu_file; |
|
27 | + * Highlights sub menus. |
|
28 | + */ |
|
29 | + public function set_admin_menu_class() { |
|
30 | + global $current_screen, $parent_file, $submenu_file; |
|
31 | 31 | |
32 | 32 | if ( ! empty( $current_screen->id ) && in_array( $current_screen->id, array( 'wpi_discount', 'wpi_payment_form', 'wpi_invoice' ) ) ) { |
33 | - $parent_file = 'wpinv'; |
|
34 | - $submenu_file = 'edit.php?post_type=' . $current_screen->id; |
|
33 | + $parent_file = 'wpinv'; |
|
34 | + $submenu_file = 'edit.php?post_type=' . $current_screen->id; |
|
35 | 35 | } |
36 | 36 | |
37 | 37 | } |
@@ -165,8 +165,8 @@ discard block |
||
165 | 165 | foreach ( wpinv_get_settings_tabs() as $tab_id => $tab_name ) { |
166 | 166 | $tab_url = add_query_arg( |
167 | 167 | array( |
168 | - 'settings-updated' => false, |
|
169 | - 'tab' => $tab_id, |
|
168 | + 'settings-updated' => false, |
|
169 | + 'tab' => $tab_id, |
|
170 | 170 | ), |
171 | 171 | 'admin.php?page=wpinv-settings' |
172 | 172 | ); |
@@ -193,9 +193,9 @@ discard block |
||
193 | 193 | $number++; |
194 | 194 | $tab_url = add_query_arg( |
195 | 195 | array( |
196 | - 'settings-updated' => false, |
|
197 | - 'tab' => $active_tab, |
|
198 | - 'section' => $section_id, |
|
196 | + 'settings-updated' => false, |
|
197 | + 'tab' => $active_tab, |
|
198 | + 'section' => $section_id, |
|
199 | 199 | ), |
200 | 200 | admin_url( 'admin.php?page=wpinv-settings' ) |
201 | 201 | ); |
@@ -21,569 +21,569 @@ |
||
21 | 21 | */ |
22 | 22 | class GetPaid_REST_Controller extends WP_REST_Controller { |
23 | 23 | |
24 | - /** |
|
24 | + /** |
|
25 | 25 | * The namespaces of this controller's route. |
26 | 26 | * |
27 | 27 | * @since 1.0.19 |
28 | 28 | * @var array |
29 | 29 | */ |
30 | - protected $namespaces; |
|
30 | + protected $namespaces; |
|
31 | 31 | |
32 | - /** |
|
32 | + /** |
|
33 | 33 | * The official namespace of this controller's route. |
34 | 34 | * |
35 | 35 | * @since 1.0.19 |
36 | 36 | * @var string |
37 | 37 | */ |
38 | - protected $namespace = 'getpaid/v1'; |
|
38 | + protected $namespace = 'getpaid/v1'; |
|
39 | 39 | |
40 | - /** |
|
40 | + /** |
|
41 | 41 | * Cached results of get_item_schema. |
42 | 42 | * |
43 | 43 | * @since 1.0.19 |
44 | 44 | * @var array |
45 | 45 | */ |
46 | - protected $schema; |
|
46 | + protected $schema; |
|
47 | 47 | |
48 | 48 | /** |
49 | - * Constructor. |
|
50 | - * |
|
51 | - * @since 1.0.19 |
|
52 | - * |
|
53 | - */ |
|
54 | - public function __construct() { |
|
55 | - |
|
56 | - // Offer several namespaces for backwards compatibility. |
|
57 | - $this->namespaces = apply_filters( |
|
58 | - 'getpaid_rest_api_namespaces', |
|
59 | - array( |
|
60 | - 'getpaid/v1', |
|
61 | - 'invoicing/v1', |
|
62 | - 'wpi/v1', |
|
63 | - ) |
|
64 | - ); |
|
65 | - |
|
66 | - // Register REST routes. |
|
49 | + * Constructor. |
|
50 | + * |
|
51 | + * @since 1.0.19 |
|
52 | + * |
|
53 | + */ |
|
54 | + public function __construct() { |
|
55 | + |
|
56 | + // Offer several namespaces for backwards compatibility. |
|
57 | + $this->namespaces = apply_filters( |
|
58 | + 'getpaid_rest_api_namespaces', |
|
59 | + array( |
|
60 | + 'getpaid/v1', |
|
61 | + 'invoicing/v1', |
|
62 | + 'wpi/v1', |
|
63 | + ) |
|
64 | + ); |
|
65 | + |
|
66 | + // Register REST routes. |
|
67 | 67 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
68 | 68 | |
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Registers routes for each namespace. |
|
73 | - * |
|
74 | - * @since 1.0.19 |
|
75 | - * |
|
76 | - */ |
|
77 | - public function register_routes() { |
|
78 | - |
|
79 | - foreach ( $this->namespaces as $namespace ) { |
|
80 | - $this->register_namespace_routes( $namespace ); |
|
81 | - } |
|
82 | - |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Registers routes for a namespace. |
|
87 | - * |
|
88 | - * @since 1.0.19 |
|
89 | - * |
|
90 | - * @param string $namespace |
|
91 | - */ |
|
92 | - public function register_namespace_routes( /** @scrutinizer ignore-unused */ $namespace ) { |
|
93 | - |
|
94 | - getpaid_doing_it_wrong( |
|
95 | - __CLASS__ . '::' . __METHOD__, |
|
96 | - /* translators: %s: register_namespace_routes() */ |
|
97 | - sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), |
|
98 | - '1.0.19' |
|
99 | - ); |
|
100 | - |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Get normalized rest base. |
|
105 | - * |
|
106 | - * @return string |
|
107 | - */ |
|
108 | - protected function get_normalized_rest_base() { |
|
109 | - return preg_replace( '/\(.*\)\//i', '', $this->rest_base ); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Fill batches. |
|
114 | - * |
|
115 | - * @param array array of request items. |
|
116 | - * @return array |
|
117 | - */ |
|
118 | - protected function fill_batch_keys( $items ) { |
|
119 | - |
|
120 | - $items['create'] = empty( $items['create'] ) ? array() : $items['create']; |
|
121 | - $items['update'] = empty( $items['update'] ) ? array() : $items['update']; |
|
122 | - $items['delete'] = empty( $items['delete'] ) ? array() : wp_parse_id_list( $items['delete'] ); |
|
123 | - return $items; |
|
124 | - |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Check batch limit. |
|
129 | - * |
|
130 | - * @param array $items Request items. |
|
131 | - * @return bool|WP_Error |
|
132 | - */ |
|
133 | - protected function check_batch_limit( $items ) { |
|
134 | - $limit = apply_filters( 'getpaid_rest_batch_items_limit', 100, $this->get_normalized_rest_base() ); |
|
135 | - $total = count( $items['create'] ) + count( $items['update'] ) + count( $items['delete'] ); |
|
136 | - |
|
137 | - if ( $total > $limit ) { |
|
138 | - /* translators: %s: items limit */ |
|
139 | - return new WP_Error( 'getpaid_rest_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'invoicing' ), $limit ), array( 'status' => 413 ) ); |
|
140 | - } |
|
141 | - |
|
142 | - return true; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Bulk create items. |
|
147 | - * |
|
148 | - * @param array $items Array of items to create. |
|
149 | - * @param WP_REST_Request $request Full details about the request. |
|
150 | - * @param WP_REST_Server $wp_rest_server |
|
151 | - * @return array() |
|
152 | - */ |
|
153 | - protected function batch_create_items( $items, $request, $wp_rest_server ) { |
|
154 | - |
|
155 | - $query = $request->get_query_params(); |
|
156 | - $create = array(); |
|
157 | - |
|
158 | - foreach ( $items as $item ) { |
|
159 | - $_item = new WP_REST_Request( 'POST' ); |
|
160 | - |
|
161 | - // Default parameters. |
|
162 | - $defaults = array(); |
|
163 | - $schema = $this->get_public_item_schema(); |
|
164 | - foreach ( $schema['properties'] as $arg => $options ) { |
|
165 | - if ( isset( $options['default'] ) ) { |
|
166 | - $defaults[ $arg ] = $options['default']; |
|
167 | - } |
|
168 | - } |
|
169 | - $_item->set_default_params( $defaults ); |
|
170 | - |
|
171 | - // Set request parameters. |
|
172 | - $_item->set_body_params( $item ); |
|
173 | - |
|
174 | - // Set query (GET) parameters. |
|
175 | - $_item->set_query_params( $query ); |
|
176 | - |
|
177 | - // Create the item. |
|
178 | - $_response = $this->create_item( $_item ); |
|
179 | - |
|
180 | - // If an error occured... |
|
181 | - if ( is_wp_error( $_response ) ) { |
|
182 | - |
|
183 | - $create[] = array( |
|
184 | - 'id' => 0, |
|
185 | - 'error' => array( |
|
186 | - 'code' => $_response->get_error_code(), |
|
187 | - 'message' => $_response->get_error_message(), |
|
188 | - 'data' => $_response->get_error_data(), |
|
189 | - ), |
|
190 | - ); |
|
191 | - |
|
192 | - continue; |
|
193 | - } |
|
194 | - |
|
195 | - $create[] = $wp_rest_server->response_to_data( /** @scrutinizer ignore-type */ $_response, false ); |
|
196 | - |
|
197 | - } |
|
198 | - |
|
199 | - return $create; |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Registers routes for each namespace. |
|
73 | + * |
|
74 | + * @since 1.0.19 |
|
75 | + * |
|
76 | + */ |
|
77 | + public function register_routes() { |
|
78 | + |
|
79 | + foreach ( $this->namespaces as $namespace ) { |
|
80 | + $this->register_namespace_routes( $namespace ); |
|
81 | + } |
|
82 | + |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Registers routes for a namespace. |
|
87 | + * |
|
88 | + * @since 1.0.19 |
|
89 | + * |
|
90 | + * @param string $namespace |
|
91 | + */ |
|
92 | + public function register_namespace_routes( /** @scrutinizer ignore-unused */ $namespace ) { |
|
93 | + |
|
94 | + getpaid_doing_it_wrong( |
|
95 | + __CLASS__ . '::' . __METHOD__, |
|
96 | + /* translators: %s: register_namespace_routes() */ |
|
97 | + sprintf( __( "Method '%s' must be overridden." ), __METHOD__ ), |
|
98 | + '1.0.19' |
|
99 | + ); |
|
100 | + |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Get normalized rest base. |
|
105 | + * |
|
106 | + * @return string |
|
107 | + */ |
|
108 | + protected function get_normalized_rest_base() { |
|
109 | + return preg_replace( '/\(.*\)\//i', '', $this->rest_base ); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Fill batches. |
|
114 | + * |
|
115 | + * @param array array of request items. |
|
116 | + * @return array |
|
117 | + */ |
|
118 | + protected function fill_batch_keys( $items ) { |
|
119 | + |
|
120 | + $items['create'] = empty( $items['create'] ) ? array() : $items['create']; |
|
121 | + $items['update'] = empty( $items['update'] ) ? array() : $items['update']; |
|
122 | + $items['delete'] = empty( $items['delete'] ) ? array() : wp_parse_id_list( $items['delete'] ); |
|
123 | + return $items; |
|
124 | + |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Check batch limit. |
|
129 | + * |
|
130 | + * @param array $items Request items. |
|
131 | + * @return bool|WP_Error |
|
132 | + */ |
|
133 | + protected function check_batch_limit( $items ) { |
|
134 | + $limit = apply_filters( 'getpaid_rest_batch_items_limit', 100, $this->get_normalized_rest_base() ); |
|
135 | + $total = count( $items['create'] ) + count( $items['update'] ) + count( $items['delete'] ); |
|
136 | + |
|
137 | + if ( $total > $limit ) { |
|
138 | + /* translators: %s: items limit */ |
|
139 | + return new WP_Error( 'getpaid_rest_request_entity_too_large', sprintf( __( 'Unable to accept more than %s items for this request.', 'invoicing' ), $limit ), array( 'status' => 413 ) ); |
|
140 | + } |
|
141 | + |
|
142 | + return true; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Bulk create items. |
|
147 | + * |
|
148 | + * @param array $items Array of items to create. |
|
149 | + * @param WP_REST_Request $request Full details about the request. |
|
150 | + * @param WP_REST_Server $wp_rest_server |
|
151 | + * @return array() |
|
152 | + */ |
|
153 | + protected function batch_create_items( $items, $request, $wp_rest_server ) { |
|
154 | + |
|
155 | + $query = $request->get_query_params(); |
|
156 | + $create = array(); |
|
157 | + |
|
158 | + foreach ( $items as $item ) { |
|
159 | + $_item = new WP_REST_Request( 'POST' ); |
|
160 | + |
|
161 | + // Default parameters. |
|
162 | + $defaults = array(); |
|
163 | + $schema = $this->get_public_item_schema(); |
|
164 | + foreach ( $schema['properties'] as $arg => $options ) { |
|
165 | + if ( isset( $options['default'] ) ) { |
|
166 | + $defaults[ $arg ] = $options['default']; |
|
167 | + } |
|
168 | + } |
|
169 | + $_item->set_default_params( $defaults ); |
|
170 | + |
|
171 | + // Set request parameters. |
|
172 | + $_item->set_body_params( $item ); |
|
173 | + |
|
174 | + // Set query (GET) parameters. |
|
175 | + $_item->set_query_params( $query ); |
|
176 | + |
|
177 | + // Create the item. |
|
178 | + $_response = $this->create_item( $_item ); |
|
179 | + |
|
180 | + // If an error occured... |
|
181 | + if ( is_wp_error( $_response ) ) { |
|
182 | + |
|
183 | + $create[] = array( |
|
184 | + 'id' => 0, |
|
185 | + 'error' => array( |
|
186 | + 'code' => $_response->get_error_code(), |
|
187 | + 'message' => $_response->get_error_message(), |
|
188 | + 'data' => $_response->get_error_data(), |
|
189 | + ), |
|
190 | + ); |
|
191 | + |
|
192 | + continue; |
|
193 | + } |
|
194 | + |
|
195 | + $create[] = $wp_rest_server->response_to_data( /** @scrutinizer ignore-type */ $_response, false ); |
|
196 | + |
|
197 | + } |
|
198 | + |
|
199 | + return $create; |
|
200 | + |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Bulk update items. |
|
205 | + * |
|
206 | + * @param array $items Array of items to update. |
|
207 | + * @param WP_REST_Request $request Full details about the request. |
|
208 | + * @param WP_REST_Server $wp_rest_server |
|
209 | + * @return array() |
|
210 | + */ |
|
211 | + protected function batch_update_items( $items, $request, $wp_rest_server ) { |
|
212 | + |
|
213 | + $query = $request->get_query_params(); |
|
214 | + $update = array(); |
|
215 | + |
|
216 | + foreach ( $items as $item ) { |
|
217 | + |
|
218 | + // Create a dummy request. |
|
219 | + $_item = new WP_REST_Request( 'PUT' ); |
|
220 | + |
|
221 | + // Add body params. |
|
222 | + $_item->set_body_params( $item ); |
|
223 | + |
|
224 | + // Set query (GET) parameters. |
|
225 | + $_item->set_query_params( $query ); |
|
226 | + |
|
227 | + // Update the item. |
|
228 | + $_response = $this->update_item( $_item ); |
|
229 | + |
|
230 | + // If an error occured... |
|
231 | + if ( is_wp_error( $_response ) ) { |
|
232 | + |
|
233 | + $update[] = array( |
|
234 | + 'id' => $item['id'], |
|
235 | + 'error' => array( |
|
236 | + 'code' => $_response->get_error_code(), |
|
237 | + 'message' => $_response->get_error_message(), |
|
238 | + 'data' => $_response->get_error_data(), |
|
239 | + ), |
|
240 | + ); |
|
241 | + |
|
242 | + continue; |
|
243 | + |
|
244 | + } |
|
245 | + |
|
246 | + $update[] = $wp_rest_server->response_to_data( /** @scrutinizer ignore-type */ $_response, false ); |
|
247 | + |
|
248 | + } |
|
249 | + |
|
250 | + return $update; |
|
251 | + |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Bulk delete items. |
|
256 | + * |
|
257 | + * @param array $items Array of items to delete. |
|
258 | + * @param WP_REST_Server $wp_rest_server |
|
259 | + * @return array() |
|
260 | + */ |
|
261 | + protected function batch_delete_items( $items, $wp_rest_server ) { |
|
262 | + |
|
263 | + $delete = array(); |
|
264 | + |
|
265 | + foreach ( array_filter( $items ) as $id ) { |
|
266 | + |
|
267 | + // Prepare the request. |
|
268 | + $_item = new WP_REST_Request( 'DELETE' ); |
|
269 | + $_item->set_query_params( |
|
270 | + array( |
|
271 | + 'id' => $id, |
|
272 | + 'force' => true, |
|
273 | + ) |
|
274 | + ); |
|
275 | + |
|
276 | + // Delete the item. |
|
277 | + $_response = $this->delete_item( $_item ); |
|
278 | + |
|
279 | + if ( is_wp_error( $_response ) ) { |
|
280 | + |
|
281 | + $delete[] = array( |
|
282 | + 'id' => $id, |
|
283 | + 'error' => array( |
|
284 | + 'code' => $_response->get_error_code(), |
|
285 | + 'message' => $_response->get_error_message(), |
|
286 | + 'data' => $_response->get_error_data(), |
|
287 | + ), |
|
288 | + ); |
|
289 | + |
|
290 | + continue; |
|
291 | + } |
|
200 | 292 | |
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Bulk update items. |
|
205 | - * |
|
206 | - * @param array $items Array of items to update. |
|
207 | - * @param WP_REST_Request $request Full details about the request. |
|
208 | - * @param WP_REST_Server $wp_rest_server |
|
209 | - * @return array() |
|
210 | - */ |
|
211 | - protected function batch_update_items( $items, $request, $wp_rest_server ) { |
|
212 | - |
|
213 | - $query = $request->get_query_params(); |
|
214 | - $update = array(); |
|
215 | - |
|
216 | - foreach ( $items as $item ) { |
|
217 | - |
|
218 | - // Create a dummy request. |
|
219 | - $_item = new WP_REST_Request( 'PUT' ); |
|
220 | - |
|
221 | - // Add body params. |
|
222 | - $_item->set_body_params( $item ); |
|
223 | - |
|
224 | - // Set query (GET) parameters. |
|
225 | - $_item->set_query_params( $query ); |
|
226 | - |
|
227 | - // Update the item. |
|
228 | - $_response = $this->update_item( $_item ); |
|
229 | - |
|
230 | - // If an error occured... |
|
231 | - if ( is_wp_error( $_response ) ) { |
|
232 | - |
|
233 | - $update[] = array( |
|
234 | - 'id' => $item['id'], |
|
235 | - 'error' => array( |
|
236 | - 'code' => $_response->get_error_code(), |
|
237 | - 'message' => $_response->get_error_message(), |
|
238 | - 'data' => $_response->get_error_data(), |
|
239 | - ), |
|
240 | - ); |
|
241 | - |
|
242 | - continue; |
|
243 | - |
|
244 | - } |
|
245 | - |
|
246 | - $update[] = $wp_rest_server->response_to_data( /** @scrutinizer ignore-type */ $_response, false ); |
|
247 | - |
|
248 | - } |
|
249 | - |
|
250 | - return $update; |
|
251 | - |
|
252 | - } |
|
293 | + $delete[] = $wp_rest_server->response_to_data( /** @scrutinizer ignore-type */ $_response, false ); |
|
253 | 294 | |
254 | - /** |
|
255 | - * Bulk delete items. |
|
256 | - * |
|
257 | - * @param array $items Array of items to delete. |
|
258 | - * @param WP_REST_Server $wp_rest_server |
|
259 | - * @return array() |
|
260 | - */ |
|
261 | - protected function batch_delete_items( $items, $wp_rest_server ) { |
|
262 | - |
|
263 | - $delete = array(); |
|
264 | - |
|
265 | - foreach ( array_filter( $items ) as $id ) { |
|
266 | - |
|
267 | - // Prepare the request. |
|
268 | - $_item = new WP_REST_Request( 'DELETE' ); |
|
269 | - $_item->set_query_params( |
|
270 | - array( |
|
271 | - 'id' => $id, |
|
272 | - 'force' => true, |
|
273 | - ) |
|
274 | - ); |
|
275 | - |
|
276 | - // Delete the item. |
|
277 | - $_response = $this->delete_item( $_item ); |
|
278 | - |
|
279 | - if ( is_wp_error( $_response ) ) { |
|
280 | - |
|
281 | - $delete[] = array( |
|
282 | - 'id' => $id, |
|
283 | - 'error' => array( |
|
284 | - 'code' => $_response->get_error_code(), |
|
285 | - 'message' => $_response->get_error_message(), |
|
286 | - 'data' => $_response->get_error_data(), |
|
287 | - ), |
|
288 | - ); |
|
289 | - |
|
290 | - continue; |
|
291 | - } |
|
292 | - |
|
293 | - $delete[] = $wp_rest_server->response_to_data( /** @scrutinizer ignore-type */ $_response, false ); |
|
294 | - |
|
295 | - } |
|
296 | - |
|
297 | - return $delete; |
|
298 | - |
|
299 | - } |
|
300 | - |
|
301 | - /** |
|
302 | - * Bulk create, update and delete items. |
|
303 | - * |
|
304 | - * @param WP_REST_Request $request Full details about the request. |
|
305 | - * @return WP_Error|array. |
|
306 | - */ |
|
307 | - public function batch_items( $request ) { |
|
308 | - global $wp_rest_server; |
|
309 | - |
|
310 | - // Prepare the batch items. |
|
311 | - $items = $this->fill_batch_keys( array_filter( $request->get_params() ) ); |
|
312 | - |
|
313 | - // Ensure that the batch has not exceeded the limit to prevent abuse. |
|
314 | - $limit = $this->check_batch_limit( $items ); |
|
315 | - if ( is_wp_error( $limit ) ) { |
|
316 | - return $limit; |
|
317 | - } |
|
318 | - |
|
319 | - // Process the items. |
|
320 | - return array( |
|
321 | - 'create' => $this->batch_create_items( $items['create'], $request, $wp_rest_server ), |
|
322 | - 'update' => $this->batch_update_items( $items['update'], $request, $wp_rest_server ), |
|
323 | - 'delete' => $this->batch_delete_items( $items['delete'], $wp_rest_server ), |
|
324 | - ); |
|
325 | - |
|
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Add meta query. |
|
330 | - * |
|
331 | - * @since 1.0.19 |
|
332 | - * @param array $args Query args. |
|
333 | - * @param array $meta_query Meta query. |
|
334 | - * @return array |
|
335 | - */ |
|
336 | - protected function add_meta_query( $args, $meta_query ) { |
|
337 | - if ( empty( $args['meta_query'] ) ) { |
|
338 | - $args['meta_query'] = array(); |
|
339 | - } |
|
340 | - |
|
341 | - $args['meta_query'][] = $meta_query; |
|
342 | - |
|
343 | - return $args['meta_query']; |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Get the batch schema, conforming to JSON Schema. |
|
348 | - * |
|
349 | - * @return array |
|
350 | - */ |
|
351 | - public function get_public_batch_schema() { |
|
352 | - |
|
353 | - return array( |
|
354 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
355 | - 'title' => 'batch', |
|
356 | - 'type' => 'object', |
|
357 | - 'properties' => array( |
|
358 | - 'create' => array( |
|
359 | - 'description' => __( 'List of created resources.', 'invoicing' ), |
|
360 | - 'type' => 'array', |
|
361 | - 'context' => array( 'view', 'edit' ), |
|
362 | - 'items' => array( |
|
363 | - 'type' => 'object', |
|
364 | - ), |
|
365 | - ), |
|
366 | - 'update' => array( |
|
367 | - 'description' => __( 'List of updated resources.', 'invoicing' ), |
|
368 | - 'type' => 'array', |
|
369 | - 'context' => array( 'view', 'edit' ), |
|
370 | - 'items' => array( |
|
371 | - 'type' => 'object', |
|
372 | - ), |
|
373 | - ), |
|
374 | - 'delete' => array( |
|
375 | - 'description' => __( 'List of deleted resources.', 'invoicing' ), |
|
376 | - 'type' => 'array', |
|
377 | - 'context' => array( 'view', 'edit' ), |
|
378 | - 'items' => array( |
|
379 | - 'type' => 'integer', |
|
380 | - ), |
|
381 | - ), |
|
382 | - ), |
|
383 | - ); |
|
384 | - |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Returns the value of schema['properties'] |
|
389 | - * |
|
390 | - * i.e Schema fields. |
|
391 | - * |
|
392 | - * @since 1.0.19 |
|
393 | - * @return array |
|
394 | - */ |
|
395 | - protected function get_schema_properties() { |
|
396 | - |
|
397 | - $schema = $this->get_item_schema(); |
|
398 | - $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
399 | - |
|
400 | - // For back-compat, include any field with an empty schema |
|
401 | - // because it won't be present in $this->get_item_schema(). |
|
402 | - foreach ( $this->get_additional_fields() as $field_name => $field_options ) { |
|
403 | - if ( is_null( $field_options['schema'] ) ) { |
|
404 | - $properties[ $field_name ] = $field_options; |
|
405 | - } |
|
406 | - } |
|
407 | - |
|
408 | - return $properties; |
|
409 | - } |
|
410 | - |
|
411 | - /** |
|
412 | - * Filters fields by context. |
|
413 | - * |
|
414 | - * @param array $fields Array of fields |
|
415 | - * @param string|null context view, edit or embed |
|
416 | - * @since 1.0.19 |
|
417 | - * @return array |
|
418 | - */ |
|
419 | - protected function filter_response_fields_by_context( $fields, $context ) { |
|
420 | - |
|
421 | - if ( empty( $context ) ) { |
|
422 | - return $fields; |
|
423 | - } |
|
424 | - |
|
425 | - foreach ( $fields as $name => $options ) { |
|
426 | - if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
427 | - unset( $fields[ $name ] ); |
|
428 | - } |
|
429 | - } |
|
430 | - |
|
431 | - return $fields; |
|
432 | - |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * Filters fields by an array of requested fields. |
|
437 | - * |
|
438 | - * @param array $fields Array of available fields |
|
439 | - * @param array $requested array of requested fields. |
|
440 | - * @since 1.0.19 |
|
441 | - * @return array |
|
442 | - */ |
|
443 | - protected function filter_response_fields_by_array( $fields, $requested ) { |
|
444 | - |
|
445 | - // Trim off any whitespace from the list array. |
|
446 | - $requested = array_map( 'trim', $requested ); |
|
447 | - |
|
448 | - // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
449 | - if ( in_array( 'id', $fields, true ) ) { |
|
450 | - $requested[] = 'id'; |
|
451 | - } |
|
452 | - |
|
453 | - // Get rid of duplicate fields. |
|
454 | - $requested = array_unique( $requested ); |
|
455 | - |
|
456 | - // Return the list of all included fields which are available. |
|
457 | - return array_reduce( |
|
458 | - $requested, |
|
459 | - function( $response_fields, $field ) use ( $fields ) { |
|
460 | - |
|
461 | - if ( in_array( $field, $fields, true ) ) { |
|
462 | - $response_fields[] = $field; |
|
463 | - return $response_fields; |
|
464 | - } |
|
465 | - |
|
466 | - // Check for nested fields if $field is not a direct match. |
|
467 | - $nested_fields = explode( '.', $field ); |
|
468 | - |
|
469 | - // A nested field is included so long as its top-level property is |
|
470 | - // present in the schema. |
|
471 | - if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
472 | - $response_fields[] = $field; |
|
473 | - } |
|
474 | - |
|
475 | - return $response_fields; |
|
476 | - }, |
|
477 | - array() |
|
478 | - ); |
|
479 | - |
|
480 | - } |
|
481 | - |
|
482 | - /** |
|
483 | - * Gets an array of fields to be included on the response. |
|
484 | - * |
|
485 | - * Included fields are based on item schema and `_fields=` request argument. |
|
486 | - * Copied from WordPress 5.3 to support old versions. |
|
487 | - * |
|
488 | - * @since 1.0.19 |
|
489 | - * @param WP_REST_Request $request Full details about the request. |
|
490 | - * @return array Fields to be included in the response. |
|
491 | - */ |
|
492 | - public function get_fields_for_response( $request ) { |
|
493 | - |
|
494 | - // Retrieve fields in the schema. |
|
495 | - $properties = $this->get_schema_properties(); |
|
496 | - |
|
497 | - // Exclude fields that specify a different context than the request context. |
|
498 | - $properties = $this->filter_response_fields_by_context( $properties, $request['context'] ); |
|
499 | - |
|
500 | - // We only need the field keys. |
|
501 | - $fields = array_keys( $properties ); |
|
502 | - |
|
503 | - // Is the user filtering the response fields?? |
|
504 | - if ( empty( $request['_fields'] ) ) { |
|
505 | - return $fields; |
|
506 | - } |
|
507 | - |
|
508 | - return $this->filter_response_fields_by_array( $fields, wpinv_parse_list( $request['_fields'] ) ); |
|
509 | - |
|
510 | - } |
|
511 | - |
|
512 | - /** |
|
513 | - * Limits an object to the requested fields. |
|
514 | - * |
|
515 | - * Included fields are based on the `_fields` request argument. |
|
516 | - * |
|
517 | - * @since 1.0.19 |
|
518 | - * @param array $data Fields to include in the response. |
|
519 | - * @param array $fields Requested fields. |
|
520 | - * @return array Fields to be included in the response. |
|
521 | - */ |
|
522 | - public function limit_object_to_requested_fields( $data, $fields, $prefix = '' ) { |
|
523 | - |
|
524 | - // Is the user filtering the response fields?? |
|
525 | - if ( empty( $fields ) ) { |
|
526 | - return $data; |
|
527 | - } |
|
528 | - |
|
529 | - foreach ( $data as $key => $value ) { |
|
530 | - |
|
531 | - // Numeric arrays. |
|
532 | - if ( is_numeric( $key ) && is_array( $value ) ) { |
|
533 | - $data[ $key ] = $this->limit_object_to_requested_fields( $value, $fields, $prefix ); |
|
534 | - continue; |
|
535 | - } |
|
536 | - |
|
537 | - // Generate a new prefix. |
|
538 | - $new_prefix = empty( $prefix ) ? $key : "$prefix.$key"; |
|
539 | - |
|
540 | - // Check if it was requested. |
|
541 | - if ( ! empty( $key ) && ! $this->is_field_included( $new_prefix, $fields ) ) { |
|
542 | - unset( $data[ $key ] ); |
|
543 | - continue; |
|
544 | - } |
|
545 | - |
|
546 | - if ( $key != 'meta_data' && is_array( $value ) ) { |
|
547 | - $data[ $key ] = $this->limit_object_to_requested_fields( $value, $fields, $new_prefix ); |
|
548 | - } |
|
295 | + } |
|
296 | + |
|
297 | + return $delete; |
|
298 | + |
|
299 | + } |
|
300 | + |
|
301 | + /** |
|
302 | + * Bulk create, update and delete items. |
|
303 | + * |
|
304 | + * @param WP_REST_Request $request Full details about the request. |
|
305 | + * @return WP_Error|array. |
|
306 | + */ |
|
307 | + public function batch_items( $request ) { |
|
308 | + global $wp_rest_server; |
|
309 | + |
|
310 | + // Prepare the batch items. |
|
311 | + $items = $this->fill_batch_keys( array_filter( $request->get_params() ) ); |
|
312 | + |
|
313 | + // Ensure that the batch has not exceeded the limit to prevent abuse. |
|
314 | + $limit = $this->check_batch_limit( $items ); |
|
315 | + if ( is_wp_error( $limit ) ) { |
|
316 | + return $limit; |
|
317 | + } |
|
318 | + |
|
319 | + // Process the items. |
|
320 | + return array( |
|
321 | + 'create' => $this->batch_create_items( $items['create'], $request, $wp_rest_server ), |
|
322 | + 'update' => $this->batch_update_items( $items['update'], $request, $wp_rest_server ), |
|
323 | + 'delete' => $this->batch_delete_items( $items['delete'], $wp_rest_server ), |
|
324 | + ); |
|
325 | + |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Add meta query. |
|
330 | + * |
|
331 | + * @since 1.0.19 |
|
332 | + * @param array $args Query args. |
|
333 | + * @param array $meta_query Meta query. |
|
334 | + * @return array |
|
335 | + */ |
|
336 | + protected function add_meta_query( $args, $meta_query ) { |
|
337 | + if ( empty( $args['meta_query'] ) ) { |
|
338 | + $args['meta_query'] = array(); |
|
339 | + } |
|
340 | + |
|
341 | + $args['meta_query'][] = $meta_query; |
|
342 | + |
|
343 | + return $args['meta_query']; |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Get the batch schema, conforming to JSON Schema. |
|
348 | + * |
|
349 | + * @return array |
|
350 | + */ |
|
351 | + public function get_public_batch_schema() { |
|
352 | + |
|
353 | + return array( |
|
354 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
355 | + 'title' => 'batch', |
|
356 | + 'type' => 'object', |
|
357 | + 'properties' => array( |
|
358 | + 'create' => array( |
|
359 | + 'description' => __( 'List of created resources.', 'invoicing' ), |
|
360 | + 'type' => 'array', |
|
361 | + 'context' => array( 'view', 'edit' ), |
|
362 | + 'items' => array( |
|
363 | + 'type' => 'object', |
|
364 | + ), |
|
365 | + ), |
|
366 | + 'update' => array( |
|
367 | + 'description' => __( 'List of updated resources.', 'invoicing' ), |
|
368 | + 'type' => 'array', |
|
369 | + 'context' => array( 'view', 'edit' ), |
|
370 | + 'items' => array( |
|
371 | + 'type' => 'object', |
|
372 | + ), |
|
373 | + ), |
|
374 | + 'delete' => array( |
|
375 | + 'description' => __( 'List of deleted resources.', 'invoicing' ), |
|
376 | + 'type' => 'array', |
|
377 | + 'context' => array( 'view', 'edit' ), |
|
378 | + 'items' => array( |
|
379 | + 'type' => 'integer', |
|
380 | + ), |
|
381 | + ), |
|
382 | + ), |
|
383 | + ); |
|
384 | + |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Returns the value of schema['properties'] |
|
389 | + * |
|
390 | + * i.e Schema fields. |
|
391 | + * |
|
392 | + * @since 1.0.19 |
|
393 | + * @return array |
|
394 | + */ |
|
395 | + protected function get_schema_properties() { |
|
396 | + |
|
397 | + $schema = $this->get_item_schema(); |
|
398 | + $properties = isset( $schema['properties'] ) ? $schema['properties'] : array(); |
|
399 | + |
|
400 | + // For back-compat, include any field with an empty schema |
|
401 | + // because it won't be present in $this->get_item_schema(). |
|
402 | + foreach ( $this->get_additional_fields() as $field_name => $field_options ) { |
|
403 | + if ( is_null( $field_options['schema'] ) ) { |
|
404 | + $properties[ $field_name ] = $field_options; |
|
405 | + } |
|
406 | + } |
|
407 | + |
|
408 | + return $properties; |
|
409 | + } |
|
410 | + |
|
411 | + /** |
|
412 | + * Filters fields by context. |
|
413 | + * |
|
414 | + * @param array $fields Array of fields |
|
415 | + * @param string|null context view, edit or embed |
|
416 | + * @since 1.0.19 |
|
417 | + * @return array |
|
418 | + */ |
|
419 | + protected function filter_response_fields_by_context( $fields, $context ) { |
|
420 | + |
|
421 | + if ( empty( $context ) ) { |
|
422 | + return $fields; |
|
423 | + } |
|
424 | + |
|
425 | + foreach ( $fields as $name => $options ) { |
|
426 | + if ( ! empty( $options['context'] ) && ! in_array( $context, $options['context'], true ) ) { |
|
427 | + unset( $fields[ $name ] ); |
|
428 | + } |
|
429 | + } |
|
430 | + |
|
431 | + return $fields; |
|
432 | + |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * Filters fields by an array of requested fields. |
|
437 | + * |
|
438 | + * @param array $fields Array of available fields |
|
439 | + * @param array $requested array of requested fields. |
|
440 | + * @since 1.0.19 |
|
441 | + * @return array |
|
442 | + */ |
|
443 | + protected function filter_response_fields_by_array( $fields, $requested ) { |
|
444 | + |
|
445 | + // Trim off any whitespace from the list array. |
|
446 | + $requested = array_map( 'trim', $requested ); |
|
447 | + |
|
448 | + // Always persist 'id', because it can be needed for add_additional_fields_to_object(). |
|
449 | + if ( in_array( 'id', $fields, true ) ) { |
|
450 | + $requested[] = 'id'; |
|
451 | + } |
|
452 | + |
|
453 | + // Get rid of duplicate fields. |
|
454 | + $requested = array_unique( $requested ); |
|
455 | + |
|
456 | + // Return the list of all included fields which are available. |
|
457 | + return array_reduce( |
|
458 | + $requested, |
|
459 | + function( $response_fields, $field ) use ( $fields ) { |
|
460 | + |
|
461 | + if ( in_array( $field, $fields, true ) ) { |
|
462 | + $response_fields[] = $field; |
|
463 | + return $response_fields; |
|
464 | + } |
|
465 | + |
|
466 | + // Check for nested fields if $field is not a direct match. |
|
467 | + $nested_fields = explode( '.', $field ); |
|
468 | + |
|
469 | + // A nested field is included so long as its top-level property is |
|
470 | + // present in the schema. |
|
471 | + if ( in_array( $nested_fields[0], $fields, true ) ) { |
|
472 | + $response_fields[] = $field; |
|
473 | + } |
|
474 | + |
|
475 | + return $response_fields; |
|
476 | + }, |
|
477 | + array() |
|
478 | + ); |
|
479 | + |
|
480 | + } |
|
481 | + |
|
482 | + /** |
|
483 | + * Gets an array of fields to be included on the response. |
|
484 | + * |
|
485 | + * Included fields are based on item schema and `_fields=` request argument. |
|
486 | + * Copied from WordPress 5.3 to support old versions. |
|
487 | + * |
|
488 | + * @since 1.0.19 |
|
489 | + * @param WP_REST_Request $request Full details about the request. |
|
490 | + * @return array Fields to be included in the response. |
|
491 | + */ |
|
492 | + public function get_fields_for_response( $request ) { |
|
493 | + |
|
494 | + // Retrieve fields in the schema. |
|
495 | + $properties = $this->get_schema_properties(); |
|
496 | + |
|
497 | + // Exclude fields that specify a different context than the request context. |
|
498 | + $properties = $this->filter_response_fields_by_context( $properties, $request['context'] ); |
|
499 | + |
|
500 | + // We only need the field keys. |
|
501 | + $fields = array_keys( $properties ); |
|
502 | + |
|
503 | + // Is the user filtering the response fields?? |
|
504 | + if ( empty( $request['_fields'] ) ) { |
|
505 | + return $fields; |
|
506 | + } |
|
507 | + |
|
508 | + return $this->filter_response_fields_by_array( $fields, wpinv_parse_list( $request['_fields'] ) ); |
|
509 | + |
|
510 | + } |
|
511 | + |
|
512 | + /** |
|
513 | + * Limits an object to the requested fields. |
|
514 | + * |
|
515 | + * Included fields are based on the `_fields` request argument. |
|
516 | + * |
|
517 | + * @since 1.0.19 |
|
518 | + * @param array $data Fields to include in the response. |
|
519 | + * @param array $fields Requested fields. |
|
520 | + * @return array Fields to be included in the response. |
|
521 | + */ |
|
522 | + public function limit_object_to_requested_fields( $data, $fields, $prefix = '' ) { |
|
523 | + |
|
524 | + // Is the user filtering the response fields?? |
|
525 | + if ( empty( $fields ) ) { |
|
526 | + return $data; |
|
527 | + } |
|
528 | + |
|
529 | + foreach ( $data as $key => $value ) { |
|
530 | + |
|
531 | + // Numeric arrays. |
|
532 | + if ( is_numeric( $key ) && is_array( $value ) ) { |
|
533 | + $data[ $key ] = $this->limit_object_to_requested_fields( $value, $fields, $prefix ); |
|
534 | + continue; |
|
535 | + } |
|
536 | + |
|
537 | + // Generate a new prefix. |
|
538 | + $new_prefix = empty( $prefix ) ? $key : "$prefix.$key"; |
|
539 | + |
|
540 | + // Check if it was requested. |
|
541 | + if ( ! empty( $key ) && ! $this->is_field_included( $new_prefix, $fields ) ) { |
|
542 | + unset( $data[ $key ] ); |
|
543 | + continue; |
|
544 | + } |
|
545 | + |
|
546 | + if ( $key != 'meta_data' && is_array( $value ) ) { |
|
547 | + $data[ $key ] = $this->limit_object_to_requested_fields( $value, $fields, $new_prefix ); |
|
548 | + } |
|
549 | 549 | } |
550 | 550 | |
551 | - return $data; |
|
552 | - } |
|
553 | - |
|
554 | - /** |
|
555 | - * Given an array of fields to include in a response, some of which may be |
|
556 | - * `nested.fields`, determine whether the provided field should be included |
|
557 | - * in the response body. |
|
558 | - * |
|
559 | - * Copied from WordPress 5.3 to support old versions. |
|
560 | - * |
|
561 | - * @since 1.0.19 |
|
562 | - * |
|
563 | - * @param string $field A field to test for inclusion in the response body. |
|
564 | - * @param array $fields An array of string fields supported by the endpoint. |
|
565 | - * @return bool Whether to include the field or not. |
|
566 | - * @see rest_is_field_included() |
|
567 | - */ |
|
568 | - public function is_field_included( $field, $fields ) { |
|
569 | - if ( in_array( $field, $fields, true ) ) { |
|
570 | - return true; |
|
571 | - } |
|
572 | - |
|
573 | - foreach ( $fields as $accepted_field ) { |
|
574 | - // Check to see if $field is the parent of any item in $fields. |
|
575 | - // A field "parent" should be accepted if "parent.child" is accepted. |
|
576 | - if ( strpos( $accepted_field, "$field." ) === 0 ) { |
|
577 | - return true; |
|
578 | - } |
|
579 | - // Conversely, if "parent" is accepted, all "parent.child" fields |
|
580 | - // should also be accepted. |
|
581 | - if ( strpos( $field, "$accepted_field." ) === 0 ) { |
|
582 | - return true; |
|
583 | - } |
|
584 | - } |
|
585 | - |
|
586 | - return false; |
|
587 | - } |
|
551 | + return $data; |
|
552 | + } |
|
553 | + |
|
554 | + /** |
|
555 | + * Given an array of fields to include in a response, some of which may be |
|
556 | + * `nested.fields`, determine whether the provided field should be included |
|
557 | + * in the response body. |
|
558 | + * |
|
559 | + * Copied from WordPress 5.3 to support old versions. |
|
560 | + * |
|
561 | + * @since 1.0.19 |
|
562 | + * |
|
563 | + * @param string $field A field to test for inclusion in the response body. |
|
564 | + * @param array $fields An array of string fields supported by the endpoint. |
|
565 | + * @return bool Whether to include the field or not. |
|
566 | + * @see rest_is_field_included() |
|
567 | + */ |
|
568 | + public function is_field_included( $field, $fields ) { |
|
569 | + if ( in_array( $field, $fields, true ) ) { |
|
570 | + return true; |
|
571 | + } |
|
572 | + |
|
573 | + foreach ( $fields as $accepted_field ) { |
|
574 | + // Check to see if $field is the parent of any item in $fields. |
|
575 | + // A field "parent" should be accepted if "parent.child" is accepted. |
|
576 | + if ( strpos( $accepted_field, "$field." ) === 0 ) { |
|
577 | + return true; |
|
578 | + } |
|
579 | + // Conversely, if "parent" is accepted, all "parent.child" fields |
|
580 | + // should also be accepted. |
|
581 | + if ( strpos( $field, "$accepted_field." ) === 0 ) { |
|
582 | + return true; |
|
583 | + } |
|
584 | + } |
|
585 | + |
|
586 | + return false; |
|
587 | + } |
|
588 | 588 | |
589 | 589 | } |
@@ -18,50 +18,50 @@ |
||
18 | 18 | */ |
19 | 19 | class GetPaid_REST_Report_Top_Earners_Controller extends GetPaid_REST_Report_Top_Sellers_Controller { |
20 | 20 | |
21 | - /** |
|
22 | - * Route base. |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $rest_base = 'reports/top_earners'; |
|
21 | + /** |
|
22 | + * Route base. |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $rest_base = 'reports/top_earners'; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Get all data needed for this report and store in the class. |
|
30 | - */ |
|
31 | - protected function query_report_data() { |
|
28 | + /** |
|
29 | + * Get all data needed for this report and store in the class. |
|
30 | + */ |
|
31 | + protected function query_report_data() { |
|
32 | 32 | |
33 | - $this->report_data = GetPaid_Reports_Helper::get_invoice_report_data( |
|
34 | - array( |
|
35 | - 'data' => array( |
|
36 | - 'quantity' => array( |
|
37 | - 'type' => 'invoice_item', |
|
38 | - 'function' => 'SUM', |
|
39 | - 'name' => 'invoice_item_qty', |
|
40 | - ), |
|
41 | - 'item_id' => array( |
|
42 | - 'type' => 'invoice_item', |
|
43 | - 'function' => '', |
|
44 | - 'name' => 'invoice_item_id', |
|
45 | - ), |
|
46 | - 'item_name' => array( |
|
47 | - 'type' => 'invoice_item', |
|
48 | - 'function' => '', |
|
49 | - 'name' => 'invoice_item_name', |
|
50 | - ), |
|
51 | - 'price' => array( |
|
52 | - 'type' => 'invoice_item', |
|
53 | - 'function' => 'SUM', |
|
54 | - 'name' => 'invoice_item_price', |
|
55 | - ), |
|
56 | - ), |
|
57 | - 'group_by' => 'invoice_item_id', |
|
58 | - 'order_by' => 'invoice_item_price DESC', |
|
59 | - 'query_type' => 'get_results', |
|
60 | - 'limit' => 10, |
|
61 | - 'filter_range' => $this->report_range, |
|
62 | - ) |
|
63 | - ); |
|
33 | + $this->report_data = GetPaid_Reports_Helper::get_invoice_report_data( |
|
34 | + array( |
|
35 | + 'data' => array( |
|
36 | + 'quantity' => array( |
|
37 | + 'type' => 'invoice_item', |
|
38 | + 'function' => 'SUM', |
|
39 | + 'name' => 'invoice_item_qty', |
|
40 | + ), |
|
41 | + 'item_id' => array( |
|
42 | + 'type' => 'invoice_item', |
|
43 | + 'function' => '', |
|
44 | + 'name' => 'invoice_item_id', |
|
45 | + ), |
|
46 | + 'item_name' => array( |
|
47 | + 'type' => 'invoice_item', |
|
48 | + 'function' => '', |
|
49 | + 'name' => 'invoice_item_name', |
|
50 | + ), |
|
51 | + 'price' => array( |
|
52 | + 'type' => 'invoice_item', |
|
53 | + 'function' => 'SUM', |
|
54 | + 'name' => 'invoice_item_price', |
|
55 | + ), |
|
56 | + ), |
|
57 | + 'group_by' => 'invoice_item_id', |
|
58 | + 'order_by' => 'invoice_item_price DESC', |
|
59 | + 'query_type' => 'get_results', |
|
60 | + 'limit' => 10, |
|
61 | + 'filter_range' => $this->report_range, |
|
62 | + ) |
|
63 | + ); |
|
64 | 64 | |
65 | - } |
|
65 | + } |
|
66 | 66 | |
67 | 67 | } |
@@ -18,179 +18,179 @@ |
||
18 | 18 | */ |
19 | 19 | class GetPaid_REST_Report_Top_Sellers_Controller extends GetPaid_REST_Report_Sales_Controller { |
20 | 20 | |
21 | - /** |
|
22 | - * Route base. |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - protected $rest_base = 'reports/top_sellers'; |
|
27 | - |
|
28 | - /** |
|
29 | - * Get top sellers report. |
|
30 | - * |
|
31 | - * @param WP_REST_Request $request |
|
32 | - * @return array|WP_Error |
|
33 | - */ |
|
34 | - public function get_items( $request ) { |
|
35 | - |
|
36 | - // Prepare items. |
|
37 | - $this->report_range = $this->get_date_range( $request ); |
|
38 | - $report_data = $this->get_report_data(); |
|
39 | - |
|
40 | - $top_sellers = array(); |
|
41 | - |
|
42 | - foreach ( $report_data as $item ) { |
|
43 | - |
|
44 | - $item_obj = new WPInv_Item( $item ); |
|
45 | - $item_name = $item->invoice_item_name; |
|
46 | - $item_qty = floatval( $item->invoice_item_qty ); |
|
47 | - $item_id = absint( $item->invoice_item_id ); |
|
48 | - $price = sanitize_text_field( wpinv_price( $item->invoice_item_price ) ); |
|
49 | - |
|
50 | - $item_obj = new WPInv_Item( $item_id ); |
|
51 | - |
|
52 | - if ( $item_obj->exists() ) { |
|
53 | - $item_name = $item_obj->get_name(); |
|
54 | - } else { |
|
55 | - $item_id = 0; |
|
56 | - } |
|
57 | - |
|
58 | - $top_sellers[] = array( |
|
59 | - 'name' => sanitize_text_field( $item_name ), |
|
60 | - 'item_id' => $item_id, |
|
61 | - 'quantity' => $item_qty, |
|
62 | - 'earnings' => wpinv_round_amount( $item->invoice_item_price ), |
|
63 | - 'earnings_formatted' => sanitize_text_field( wpinv_price( $price ) ), |
|
64 | - ); |
|
65 | - |
|
66 | - } |
|
67 | - |
|
68 | - $data = array(); |
|
69 | - foreach ( $top_sellers as $top_seller ) { |
|
70 | - $item = $this->prepare_item_for_response( (object) $top_seller, $request ); |
|
71 | - $data[] = $this->prepare_response_for_collection( $item ); |
|
72 | - } |
|
73 | - |
|
74 | - return rest_ensure_response( $data ); |
|
75 | - |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * Prepare a report sales object for serialization. |
|
80 | - * |
|
81 | - * @param stdClass $top_seller |
|
82 | - * @param WP_REST_Request $request Request object. |
|
83 | - * @return WP_REST_Response $response Response data. |
|
84 | - */ |
|
85 | - public function prepare_item_for_response( $top_seller, $request ) { |
|
86 | - $data = (array) $top_seller; |
|
87 | - |
|
88 | - $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
89 | - $data = $this->add_additional_fields_to_object( $data, $request ); |
|
90 | - $data = $this->filter_response_by_context( $data, $context ); |
|
91 | - |
|
92 | - // Wrap the data in a response object. |
|
93 | - $response = rest_ensure_response( $data ); |
|
94 | - $links = array( |
|
95 | - 'about' => array( |
|
96 | - 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
97 | - ), |
|
98 | - ); |
|
99 | - |
|
100 | - if ( ! empty( $top_seller->item_id ) ) { |
|
101 | - $links['item'] = array( |
|
102 | - 'href' => rest_url( sprintf( '/%s/items/%s', $this->namespace, $top_seller->item_id ) ), |
|
103 | - 'embeddable' => true, |
|
104 | - ); |
|
105 | - } |
|
106 | - |
|
107 | - $response->add_links( $links ); |
|
108 | - return apply_filters( 'getpaid_rest_prepare_report_' . $this->rest_base, $response, $top_seller, $request ); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * Get all data needed for this report and store in the class. |
|
113 | - */ |
|
114 | - protected function query_report_data() { |
|
115 | - |
|
116 | - $this->report_data = GetPaid_Reports_Helper::get_invoice_report_data( |
|
117 | - array( |
|
118 | - 'data' => array( |
|
119 | - 'quantity' => array( |
|
120 | - 'type' => 'invoice_item', |
|
121 | - 'function' => 'SUM', |
|
122 | - 'name' => 'invoice_item_qty', |
|
123 | - ), |
|
124 | - 'item_id' => array( |
|
125 | - 'type' => 'invoice_item', |
|
126 | - 'function' => '', |
|
127 | - 'name' => 'invoice_item_id', |
|
128 | - ), |
|
129 | - 'item_name' => array( |
|
130 | - 'type' => 'invoice_item', |
|
131 | - 'function' => '', |
|
132 | - 'name' => 'invoice_item_name', |
|
133 | - ), |
|
134 | - 'price' => array( |
|
135 | - 'type' => 'invoice_item', |
|
136 | - 'function' => 'SUM', |
|
137 | - 'name' => 'invoice_item_price', |
|
138 | - ), |
|
139 | - ), |
|
140 | - 'group_by' => 'invoice_item_id', |
|
141 | - 'order_by' => 'invoice_item_qty DESC', |
|
142 | - 'query_type' => 'get_results', |
|
143 | - 'limit' => 10, |
|
144 | - 'filter_range' => $this->report_range, |
|
145 | - ) |
|
146 | - ); |
|
147 | - |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Get the Report's schema, conforming to JSON Schema. |
|
152 | - * |
|
153 | - * @return array |
|
154 | - */ |
|
155 | - public function get_item_schema() { |
|
156 | - $schema = array( |
|
157 | - '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
158 | - 'title' => $this->rest_base, |
|
159 | - 'type' => 'object', |
|
160 | - 'properties' => array( |
|
161 | - 'name' => array( |
|
162 | - 'description' => __( 'Item name.', 'invoicing' ), |
|
163 | - 'type' => 'string', |
|
164 | - 'context' => array( 'view' ), |
|
165 | - 'readonly' => true, |
|
166 | - ), |
|
167 | - 'item_id' => array( |
|
168 | - 'description' => __( 'Item ID.', 'invoicing' ), |
|
169 | - 'type' => 'integer', |
|
170 | - 'context' => array( 'view' ), |
|
171 | - 'readonly' => true, |
|
172 | - ), |
|
173 | - 'quantity' => array( |
|
174 | - 'description' => __( 'Total number of purchases.', 'invoicing' ), |
|
175 | - 'type' => 'number', |
|
176 | - 'context' => array( 'view' ), |
|
177 | - 'readonly' => true, |
|
178 | - ), |
|
179 | - 'earnings' => array( |
|
180 | - 'description' => __( 'Total earnings for the item.', 'invoicing' ), |
|
181 | - 'type' => 'number', |
|
182 | - 'context' => array( 'view' ), |
|
183 | - 'readonly' => true, |
|
184 | - ), |
|
185 | - 'earnings_formatted"' => array( |
|
186 | - 'description' => __( 'Total earnings (formatted) for the item.', 'invoicing' ), |
|
187 | - 'type' => 'string', |
|
188 | - 'context' => array( 'view' ), |
|
189 | - 'readonly' => true, |
|
190 | - ), |
|
191 | - ), |
|
192 | - ); |
|
193 | - |
|
194 | - return $this->add_additional_fields_schema( $schema ); |
|
195 | - } |
|
21 | + /** |
|
22 | + * Route base. |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + protected $rest_base = 'reports/top_sellers'; |
|
27 | + |
|
28 | + /** |
|
29 | + * Get top sellers report. |
|
30 | + * |
|
31 | + * @param WP_REST_Request $request |
|
32 | + * @return array|WP_Error |
|
33 | + */ |
|
34 | + public function get_items( $request ) { |
|
35 | + |
|
36 | + // Prepare items. |
|
37 | + $this->report_range = $this->get_date_range( $request ); |
|
38 | + $report_data = $this->get_report_data(); |
|
39 | + |
|
40 | + $top_sellers = array(); |
|
41 | + |
|
42 | + foreach ( $report_data as $item ) { |
|
43 | + |
|
44 | + $item_obj = new WPInv_Item( $item ); |
|
45 | + $item_name = $item->invoice_item_name; |
|
46 | + $item_qty = floatval( $item->invoice_item_qty ); |
|
47 | + $item_id = absint( $item->invoice_item_id ); |
|
48 | + $price = sanitize_text_field( wpinv_price( $item->invoice_item_price ) ); |
|
49 | + |
|
50 | + $item_obj = new WPInv_Item( $item_id ); |
|
51 | + |
|
52 | + if ( $item_obj->exists() ) { |
|
53 | + $item_name = $item_obj->get_name(); |
|
54 | + } else { |
|
55 | + $item_id = 0; |
|
56 | + } |
|
57 | + |
|
58 | + $top_sellers[] = array( |
|
59 | + 'name' => sanitize_text_field( $item_name ), |
|
60 | + 'item_id' => $item_id, |
|
61 | + 'quantity' => $item_qty, |
|
62 | + 'earnings' => wpinv_round_amount( $item->invoice_item_price ), |
|
63 | + 'earnings_formatted' => sanitize_text_field( wpinv_price( $price ) ), |
|
64 | + ); |
|
65 | + |
|
66 | + } |
|
67 | + |
|
68 | + $data = array(); |
|
69 | + foreach ( $top_sellers as $top_seller ) { |
|
70 | + $item = $this->prepare_item_for_response( (object) $top_seller, $request ); |
|
71 | + $data[] = $this->prepare_response_for_collection( $item ); |
|
72 | + } |
|
73 | + |
|
74 | + return rest_ensure_response( $data ); |
|
75 | + |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * Prepare a report sales object for serialization. |
|
80 | + * |
|
81 | + * @param stdClass $top_seller |
|
82 | + * @param WP_REST_Request $request Request object. |
|
83 | + * @return WP_REST_Response $response Response data. |
|
84 | + */ |
|
85 | + public function prepare_item_for_response( $top_seller, $request ) { |
|
86 | + $data = (array) $top_seller; |
|
87 | + |
|
88 | + $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; |
|
89 | + $data = $this->add_additional_fields_to_object( $data, $request ); |
|
90 | + $data = $this->filter_response_by_context( $data, $context ); |
|
91 | + |
|
92 | + // Wrap the data in a response object. |
|
93 | + $response = rest_ensure_response( $data ); |
|
94 | + $links = array( |
|
95 | + 'about' => array( |
|
96 | + 'href' => rest_url( sprintf( '%s/reports', $this->namespace ) ), |
|
97 | + ), |
|
98 | + ); |
|
99 | + |
|
100 | + if ( ! empty( $top_seller->item_id ) ) { |
|
101 | + $links['item'] = array( |
|
102 | + 'href' => rest_url( sprintf( '/%s/items/%s', $this->namespace, $top_seller->item_id ) ), |
|
103 | + 'embeddable' => true, |
|
104 | + ); |
|
105 | + } |
|
106 | + |
|
107 | + $response->add_links( $links ); |
|
108 | + return apply_filters( 'getpaid_rest_prepare_report_' . $this->rest_base, $response, $top_seller, $request ); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * Get all data needed for this report and store in the class. |
|
113 | + */ |
|
114 | + protected function query_report_data() { |
|
115 | + |
|
116 | + $this->report_data = GetPaid_Reports_Helper::get_invoice_report_data( |
|
117 | + array( |
|
118 | + 'data' => array( |
|
119 | + 'quantity' => array( |
|
120 | + 'type' => 'invoice_item', |
|
121 | + 'function' => 'SUM', |
|
122 | + 'name' => 'invoice_item_qty', |
|
123 | + ), |
|
124 | + 'item_id' => array( |
|
125 | + 'type' => 'invoice_item', |
|
126 | + 'function' => '', |
|
127 | + 'name' => 'invoice_item_id', |
|
128 | + ), |
|
129 | + 'item_name' => array( |
|
130 | + 'type' => 'invoice_item', |
|
131 | + 'function' => '', |
|
132 | + 'name' => 'invoice_item_name', |
|
133 | + ), |
|
134 | + 'price' => array( |
|
135 | + 'type' => 'invoice_item', |
|
136 | + 'function' => 'SUM', |
|
137 | + 'name' => 'invoice_item_price', |
|
138 | + ), |
|
139 | + ), |
|
140 | + 'group_by' => 'invoice_item_id', |
|
141 | + 'order_by' => 'invoice_item_qty DESC', |
|
142 | + 'query_type' => 'get_results', |
|
143 | + 'limit' => 10, |
|
144 | + 'filter_range' => $this->report_range, |
|
145 | + ) |
|
146 | + ); |
|
147 | + |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Get the Report's schema, conforming to JSON Schema. |
|
152 | + * |
|
153 | + * @return array |
|
154 | + */ |
|
155 | + public function get_item_schema() { |
|
156 | + $schema = array( |
|
157 | + '$schema' => 'http://json-schema.org/draft-04/schema#', |
|
158 | + 'title' => $this->rest_base, |
|
159 | + 'type' => 'object', |
|
160 | + 'properties' => array( |
|
161 | + 'name' => array( |
|
162 | + 'description' => __( 'Item name.', 'invoicing' ), |
|
163 | + 'type' => 'string', |
|
164 | + 'context' => array( 'view' ), |
|
165 | + 'readonly' => true, |
|
166 | + ), |
|
167 | + 'item_id' => array( |
|
168 | + 'description' => __( 'Item ID.', 'invoicing' ), |
|
169 | + 'type' => 'integer', |
|
170 | + 'context' => array( 'view' ), |
|
171 | + 'readonly' => true, |
|
172 | + ), |
|
173 | + 'quantity' => array( |
|
174 | + 'description' => __( 'Total number of purchases.', 'invoicing' ), |
|
175 | + 'type' => 'number', |
|
176 | + 'context' => array( 'view' ), |
|
177 | + 'readonly' => true, |
|
178 | + ), |
|
179 | + 'earnings' => array( |
|
180 | + 'description' => __( 'Total earnings for the item.', 'invoicing' ), |
|
181 | + 'type' => 'number', |
|
182 | + 'context' => array( 'view' ), |
|
183 | + 'readonly' => true, |
|
184 | + ), |
|
185 | + 'earnings_formatted"' => array( |
|
186 | + 'description' => __( 'Total earnings (formatted) for the item.', 'invoicing' ), |
|
187 | + 'type' => 'string', |
|
188 | + 'context' => array( 'view' ), |
|
189 | + 'readonly' => true, |
|
190 | + ), |
|
191 | + ), |
|
192 | + ); |
|
193 | + |
|
194 | + return $this->add_additional_fields_schema( $schema ); |
|
195 | + } |
|
196 | 196 | } |
@@ -15,168 +15,168 @@ |
||
15 | 15 | class WPInv_REST_Items_Controller extends GetPaid_REST_Posts_Controller { |
16 | 16 | |
17 | 17 | /** |
18 | - * Post type. |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $post_type = 'wpi_item'; |
|
23 | - |
|
24 | - /** |
|
25 | - * The base of this controller's route. |
|
26 | - * |
|
27 | - * @since 1.0.13 |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - protected $rest_base = 'items'; |
|
31 | - |
|
32 | - /** Contains this controller's class name. |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - public $crud_class = 'WPInv_Item'; |
|
37 | - |
|
38 | - /** |
|
39 | - * Registers the routes for the objects of the controller. |
|
40 | - * |
|
41 | - * @since 1.0.19 |
|
42 | - * |
|
43 | - * @see register_rest_route() |
|
44 | - */ |
|
45 | - public function register_namespace_routes( $namespace ) { |
|
46 | - |
|
47 | - parent::register_namespace_routes( $namespace ); |
|
48 | - |
|
49 | - register_rest_route( |
|
50 | - $this->namespace, |
|
51 | - '/' . $this->rest_base . '/item-types', |
|
52 | - array( |
|
53 | - array( |
|
54 | - 'methods' => WP_REST_Server::READABLE, |
|
55 | - 'callback' => array( $this, 'get_item_types' ), |
|
56 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
57 | - ), |
|
58 | - ) |
|
59 | - ); |
|
60 | - |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Handles rest requests for item types. |
|
65 | - * |
|
66 | - * @since 1.0.13 |
|
67 | - * |
|
68 | - * |
|
69 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
70 | - */ |
|
71 | - public function get_item_types() { |
|
72 | - return rest_ensure_response( wpinv_get_item_types() ); |
|
73 | - } |
|
18 | + * Post type. |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $post_type = 'wpi_item'; |
|
74 | 23 | |
75 | 24 | /** |
76 | - * Retrieves the query params for the items collection. |
|
77 | - * |
|
78 | - * @since 1.0.13 |
|
79 | - * |
|
80 | - * @return array Collection parameters. |
|
81 | - */ |
|
82 | - public function get_collection_params() { |
|
83 | - |
|
84 | - $params = array_merge( |
|
85 | - parent::get_collection_params(), |
|
86 | - array( |
|
87 | - |
|
88 | - // Item types |
|
89 | - 'type' => array( |
|
90 | - 'description' => __( 'Type of items to fetch.', 'invoicing' ), |
|
91 | - 'type' => array( 'array', 'string' ), |
|
92 | - 'default' => 'any', |
|
93 | - 'validate_callback' => 'rest_validate_request_arg', |
|
94 | - 'sanitize_callback' => 'wpinv_parse_list', |
|
95 | - 'items' => array( |
|
96 | - 'enum' => array_merge( array( 'any' ), wpinv_item_types() ), |
|
97 | - 'type' => 'string', |
|
98 | - ), |
|
99 | - ), |
|
100 | - |
|
101 | - ) |
|
102 | - ); |
|
103 | - |
|
104 | - // Filter collection parameters for the items controller. |
|
105 | - return apply_filters( 'getpaid_rest_items_collection_params', $params, $this ); |
|
106 | - |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Determine the allowed query_vars for a get_items() response and |
|
111 | - * prepare for WP_Query. |
|
112 | - * |
|
113 | - * @param array $prepared_args Prepared arguments. |
|
114 | - * @param WP_REST_Request $request Request object. |
|
115 | - * @return array $query_args |
|
116 | - */ |
|
117 | - protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
|
118 | - |
|
119 | - $query_args = parent::prepare_items_query( $prepared_args ); |
|
120 | - |
|
121 | - // Retrieve items by type. |
|
122 | - if ( ! in_array( 'any', $request['type'] ) ) { |
|
123 | - |
|
124 | - if ( empty( $query_args['meta_query'] ) ) { |
|
125 | - $query_args['meta_query'] = array(); |
|
126 | - } |
|
127 | - |
|
128 | - $query_args['meta_query'][] = array( |
|
129 | - 'key' => '_wpinv_type', |
|
130 | - 'value' => implode( ',', $request['type'] ), |
|
131 | - 'compare' => 'IN', |
|
132 | - ); |
|
133 | - |
|
134 | - } |
|
135 | - |
|
136 | - return apply_filters( 'getpaid_rest_items_prepare_items_query', $query_args, $request, $this ); |
|
137 | - |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Retrieves a valid list of post statuses. |
|
142 | - * |
|
143 | - * @since 1.0.15 |
|
144 | - * |
|
145 | - * @return array A list of registered item statuses. |
|
146 | - */ |
|
147 | - public function get_post_statuses() { |
|
148 | - return array( 'draft', 'pending', 'publish' ); |
|
149 | - } |
|
150 | - |
|
151 | - /** |
|
152 | - * Checks if a key should be included in a response. |
|
153 | - * |
|
154 | - * @since 1.0.19 |
|
155 | - * @param WPInv_Item $item Item object. |
|
156 | - * @param string $field_key The key to check for. |
|
157 | - * @return bool |
|
158 | - */ |
|
159 | - public function object_supports_field( $item, $field_key ) { |
|
160 | - |
|
161 | - if ( 'minimum_price' == $field_key && ! $item->user_can_set_their_price() ) { |
|
162 | - return false; |
|
163 | - } |
|
164 | - |
|
165 | - foreach ( wpinv_parse_list( 'initial_price the_initial_price recurring_price the_recurring_price recurring_period recurring_interval recurring_limit is_free_trial trial_period trial_interval first_renewal_date' ) as $key ) { |
|
166 | - |
|
167 | - if ( $key == $field_key && ! $item->is_recurring() ) { |
|
168 | - return false; |
|
169 | - } |
|
25 | + * The base of this controller's route. |
|
26 | + * |
|
27 | + * @since 1.0.13 |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + protected $rest_base = 'items'; |
|
31 | + |
|
32 | + /** Contains this controller's class name. |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + public $crud_class = 'WPInv_Item'; |
|
37 | + |
|
38 | + /** |
|
39 | + * Registers the routes for the objects of the controller. |
|
40 | + * |
|
41 | + * @since 1.0.19 |
|
42 | + * |
|
43 | + * @see register_rest_route() |
|
44 | + */ |
|
45 | + public function register_namespace_routes( $namespace ) { |
|
46 | + |
|
47 | + parent::register_namespace_routes( $namespace ); |
|
48 | + |
|
49 | + register_rest_route( |
|
50 | + $this->namespace, |
|
51 | + '/' . $this->rest_base . '/item-types', |
|
52 | + array( |
|
53 | + array( |
|
54 | + 'methods' => WP_REST_Server::READABLE, |
|
55 | + 'callback' => array( $this, 'get_item_types' ), |
|
56 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
57 | + ), |
|
58 | + ) |
|
59 | + ); |
|
60 | + |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Handles rest requests for item types. |
|
65 | + * |
|
66 | + * @since 1.0.13 |
|
67 | + * |
|
68 | + * |
|
69 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
70 | + */ |
|
71 | + public function get_item_types() { |
|
72 | + return rest_ensure_response( wpinv_get_item_types() ); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Retrieves the query params for the items collection. |
|
77 | + * |
|
78 | + * @since 1.0.13 |
|
79 | + * |
|
80 | + * @return array Collection parameters. |
|
81 | + */ |
|
82 | + public function get_collection_params() { |
|
83 | + |
|
84 | + $params = array_merge( |
|
85 | + parent::get_collection_params(), |
|
86 | + array( |
|
87 | + |
|
88 | + // Item types |
|
89 | + 'type' => array( |
|
90 | + 'description' => __( 'Type of items to fetch.', 'invoicing' ), |
|
91 | + 'type' => array( 'array', 'string' ), |
|
92 | + 'default' => 'any', |
|
93 | + 'validate_callback' => 'rest_validate_request_arg', |
|
94 | + 'sanitize_callback' => 'wpinv_parse_list', |
|
95 | + 'items' => array( |
|
96 | + 'enum' => array_merge( array( 'any' ), wpinv_item_types() ), |
|
97 | + 'type' => 'string', |
|
98 | + ), |
|
99 | + ), |
|
100 | + |
|
101 | + ) |
|
102 | + ); |
|
103 | + |
|
104 | + // Filter collection parameters for the items controller. |
|
105 | + return apply_filters( 'getpaid_rest_items_collection_params', $params, $this ); |
|
106 | + |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Determine the allowed query_vars for a get_items() response and |
|
111 | + * prepare for WP_Query. |
|
112 | + * |
|
113 | + * @param array $prepared_args Prepared arguments. |
|
114 | + * @param WP_REST_Request $request Request object. |
|
115 | + * @return array $query_args |
|
116 | + */ |
|
117 | + protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
|
118 | + |
|
119 | + $query_args = parent::prepare_items_query( $prepared_args ); |
|
120 | + |
|
121 | + // Retrieve items by type. |
|
122 | + if ( ! in_array( 'any', $request['type'] ) ) { |
|
123 | + |
|
124 | + if ( empty( $query_args['meta_query'] ) ) { |
|
125 | + $query_args['meta_query'] = array(); |
|
126 | + } |
|
127 | + |
|
128 | + $query_args['meta_query'][] = array( |
|
129 | + 'key' => '_wpinv_type', |
|
130 | + 'value' => implode( ',', $request['type'] ), |
|
131 | + 'compare' => 'IN', |
|
132 | + ); |
|
133 | + |
|
134 | + } |
|
135 | + |
|
136 | + return apply_filters( 'getpaid_rest_items_prepare_items_query', $query_args, $request, $this ); |
|
137 | + |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Retrieves a valid list of post statuses. |
|
142 | + * |
|
143 | + * @since 1.0.15 |
|
144 | + * |
|
145 | + * @return array A list of registered item statuses. |
|
146 | + */ |
|
147 | + public function get_post_statuses() { |
|
148 | + return array( 'draft', 'pending', 'publish' ); |
|
149 | + } |
|
150 | + |
|
151 | + /** |
|
152 | + * Checks if a key should be included in a response. |
|
153 | + * |
|
154 | + * @since 1.0.19 |
|
155 | + * @param WPInv_Item $item Item object. |
|
156 | + * @param string $field_key The key to check for. |
|
157 | + * @return bool |
|
158 | + */ |
|
159 | + public function object_supports_field( $item, $field_key ) { |
|
160 | + |
|
161 | + if ( 'minimum_price' == $field_key && ! $item->user_can_set_their_price() ) { |
|
162 | + return false; |
|
163 | + } |
|
164 | + |
|
165 | + foreach ( wpinv_parse_list( 'initial_price the_initial_price recurring_price the_recurring_price recurring_period recurring_interval recurring_limit is_free_trial trial_period trial_interval first_renewal_date' ) as $key ) { |
|
166 | + |
|
167 | + if ( $key == $field_key && ! $item->is_recurring() ) { |
|
168 | + return false; |
|
169 | + } |
|
170 | 170 | } |
171 | 171 | |
172 | - foreach ( wpinv_parse_list( 'trial_period trial_interval' ) as $key ) { |
|
172 | + foreach ( wpinv_parse_list( 'trial_period trial_interval' ) as $key ) { |
|
173 | 173 | |
174 | - if ( $key == $field_key && ! $item->has_free_trial() ) { |
|
175 | - return false; |
|
176 | - } |
|
174 | + if ( $key == $field_key && ! $item->has_free_trial() ) { |
|
175 | + return false; |
|
176 | + } |
|
177 | 177 | } |
178 | 178 | |
179 | - return parent::object_supports_field( $item, $field_key ); |
|
180 | - } |
|
179 | + return parent::object_supports_field( $item, $field_key ); |
|
180 | + } |
|
181 | 181 | |
182 | 182 | } |
@@ -15,136 +15,136 @@ |
||
15 | 15 | class WPInv_REST_Discounts_Controller extends GetPaid_REST_Posts_Controller { |
16 | 16 | |
17 | 17 | /** |
18 | - * Post type. |
|
19 | - * |
|
20 | - * @var string |
|
21 | - */ |
|
22 | - protected $post_type = 'wpi_discount'; |
|
23 | - |
|
24 | - /** |
|
25 | - * The base of this controller's route. |
|
26 | - * |
|
27 | - * @since 1.0.13 |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - protected $rest_base = 'discounts'; |
|
31 | - |
|
32 | - /** Contains this controller's class name. |
|
33 | - * |
|
34 | - * @var string |
|
35 | - */ |
|
36 | - public $crud_class = 'WPInv_Discount'; |
|
37 | - |
|
38 | - /** |
|
39 | - * Registers the routes for the objects of the controller. |
|
40 | - * |
|
41 | - * @since 1.0.19 |
|
42 | - * |
|
43 | - * @see register_rest_route() |
|
44 | - */ |
|
45 | - public function register_namespace_routes( $namespace ) { |
|
46 | - |
|
47 | - parent::register_namespace_routes( $namespace ); |
|
48 | - |
|
49 | - register_rest_route( |
|
50 | - $this->namespace, |
|
51 | - '/' . $this->rest_base . '/discount-types', |
|
52 | - array( |
|
53 | - array( |
|
54 | - 'methods' => WP_REST_Server::READABLE, |
|
55 | - 'callback' => array( $this, 'get_discount_types' ), |
|
56 | - 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
57 | - ), |
|
58 | - ) |
|
59 | - ); |
|
60 | - |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Handles rest requests for discount types. |
|
65 | - * |
|
66 | - * @since 1.0.13 |
|
67 | - * |
|
68 | - * |
|
69 | - * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
70 | - */ |
|
71 | - public function get_discount_types() { |
|
72 | - return rest_ensure_response( wpinv_get_discount_types() ); |
|
73 | - } |
|
18 | + * Post type. |
|
19 | + * |
|
20 | + * @var string |
|
21 | + */ |
|
22 | + protected $post_type = 'wpi_discount'; |
|
74 | 23 | |
75 | 24 | /** |
76 | - * Retrieves the query params for the discount collection. |
|
77 | - * |
|
78 | - * @since 1.0.13 |
|
79 | - * |
|
80 | - * @return array Collection parameters. |
|
81 | - */ |
|
82 | - public function get_collection_params() { |
|
83 | - |
|
84 | - $params = array_merge( |
|
85 | - parent::get_collection_params(), |
|
86 | - array( |
|
87 | - |
|
88 | - // Discount types |
|
89 | - 'type' => array( |
|
90 | - 'description' => __( 'Type of discounts to fetch.', 'invoicing' ), |
|
91 | - 'type' => array( 'array', 'string' ), |
|
92 | - 'default' => 'any', |
|
93 | - 'validate_callback' => 'rest_validate_request_arg', |
|
94 | - 'sanitize_callback' => 'wpinv_parse_list', |
|
95 | - 'items' => array( |
|
96 | - 'enum' => array_merge( array( 'any' ), array_keys( wpinv_get_discount_types() ) ), |
|
97 | - 'type' => 'string', |
|
98 | - ), |
|
99 | - ), |
|
100 | - |
|
101 | - ) |
|
102 | - ); |
|
103 | - |
|
104 | - // Filter collection parameters for the discounts controller. |
|
105 | - return apply_filters( 'getpaid_rest_discounts_collection_params', $params, $this ); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Determine the allowed query_vars for a get_items() response and |
|
110 | - * prepare for WP_Query. |
|
111 | - * |
|
112 | - * @param array $prepared_args Prepared arguments. |
|
113 | - * @param WP_REST_Request $request Request object. |
|
114 | - * @return array $query_args |
|
115 | - */ |
|
116 | - protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
|
117 | - |
|
118 | - $query_args = parent::prepare_items_query( $prepared_args ); |
|
119 | - |
|
120 | - // Retrieve items by type. |
|
121 | - if ( ! in_array( 'any', $request['type'] ) ) { |
|
122 | - |
|
123 | - if ( empty( $query_args['meta_query'] ) ) { |
|
124 | - $query_args['meta_query'] = array(); |
|
125 | - } |
|
126 | - |
|
127 | - $query_args['meta_query'][] = array( |
|
128 | - 'key' => '_wpi_discount_type', |
|
129 | - 'value' => implode( ',', $request['type'] ), |
|
130 | - 'compare' => 'IN', |
|
131 | - ); |
|
132 | - |
|
133 | - } |
|
134 | - |
|
135 | - return apply_filters( 'getpaid_rest_discounts_prepare_items_query', $query_args, $request, $this ); |
|
136 | - |
|
137 | - } |
|
138 | - |
|
139 | - /** |
|
140 | - * Retrieves a valid list of post statuses. |
|
141 | - * |
|
142 | - * @since 1.0.15 |
|
143 | - * |
|
144 | - * @return array A list of registered item statuses. |
|
145 | - */ |
|
146 | - public function get_post_statuses() { |
|
147 | - return array( 'publish', 'pending', 'draft', 'expired' ); |
|
148 | - } |
|
25 | + * The base of this controller's route. |
|
26 | + * |
|
27 | + * @since 1.0.13 |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + protected $rest_base = 'discounts'; |
|
31 | + |
|
32 | + /** Contains this controller's class name. |
|
33 | + * |
|
34 | + * @var string |
|
35 | + */ |
|
36 | + public $crud_class = 'WPInv_Discount'; |
|
37 | + |
|
38 | + /** |
|
39 | + * Registers the routes for the objects of the controller. |
|
40 | + * |
|
41 | + * @since 1.0.19 |
|
42 | + * |
|
43 | + * @see register_rest_route() |
|
44 | + */ |
|
45 | + public function register_namespace_routes( $namespace ) { |
|
46 | + |
|
47 | + parent::register_namespace_routes( $namespace ); |
|
48 | + |
|
49 | + register_rest_route( |
|
50 | + $this->namespace, |
|
51 | + '/' . $this->rest_base . '/discount-types', |
|
52 | + array( |
|
53 | + array( |
|
54 | + 'methods' => WP_REST_Server::READABLE, |
|
55 | + 'callback' => array( $this, 'get_discount_types' ), |
|
56 | + 'permission_callback' => array( $this, 'get_items_permissions_check' ), |
|
57 | + ), |
|
58 | + ) |
|
59 | + ); |
|
60 | + |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Handles rest requests for discount types. |
|
65 | + * |
|
66 | + * @since 1.0.13 |
|
67 | + * |
|
68 | + * |
|
69 | + * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. |
|
70 | + */ |
|
71 | + public function get_discount_types() { |
|
72 | + return rest_ensure_response( wpinv_get_discount_types() ); |
|
73 | + } |
|
74 | + |
|
75 | + /** |
|
76 | + * Retrieves the query params for the discount collection. |
|
77 | + * |
|
78 | + * @since 1.0.13 |
|
79 | + * |
|
80 | + * @return array Collection parameters. |
|
81 | + */ |
|
82 | + public function get_collection_params() { |
|
83 | + |
|
84 | + $params = array_merge( |
|
85 | + parent::get_collection_params(), |
|
86 | + array( |
|
87 | + |
|
88 | + // Discount types |
|
89 | + 'type' => array( |
|
90 | + 'description' => __( 'Type of discounts to fetch.', 'invoicing' ), |
|
91 | + 'type' => array( 'array', 'string' ), |
|
92 | + 'default' => 'any', |
|
93 | + 'validate_callback' => 'rest_validate_request_arg', |
|
94 | + 'sanitize_callback' => 'wpinv_parse_list', |
|
95 | + 'items' => array( |
|
96 | + 'enum' => array_merge( array( 'any' ), array_keys( wpinv_get_discount_types() ) ), |
|
97 | + 'type' => 'string', |
|
98 | + ), |
|
99 | + ), |
|
100 | + |
|
101 | + ) |
|
102 | + ); |
|
103 | + |
|
104 | + // Filter collection parameters for the discounts controller. |
|
105 | + return apply_filters( 'getpaid_rest_discounts_collection_params', $params, $this ); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Determine the allowed query_vars for a get_items() response and |
|
110 | + * prepare for WP_Query. |
|
111 | + * |
|
112 | + * @param array $prepared_args Prepared arguments. |
|
113 | + * @param WP_REST_Request $request Request object. |
|
114 | + * @return array $query_args |
|
115 | + */ |
|
116 | + protected function prepare_items_query( $prepared_args = array(), $request = null ) { |
|
117 | + |
|
118 | + $query_args = parent::prepare_items_query( $prepared_args ); |
|
119 | + |
|
120 | + // Retrieve items by type. |
|
121 | + if ( ! in_array( 'any', $request['type'] ) ) { |
|
122 | + |
|
123 | + if ( empty( $query_args['meta_query'] ) ) { |
|
124 | + $query_args['meta_query'] = array(); |
|
125 | + } |
|
126 | + |
|
127 | + $query_args['meta_query'][] = array( |
|
128 | + 'key' => '_wpi_discount_type', |
|
129 | + 'value' => implode( ',', $request['type'] ), |
|
130 | + 'compare' => 'IN', |
|
131 | + ); |
|
132 | + |
|
133 | + } |
|
134 | + |
|
135 | + return apply_filters( 'getpaid_rest_discounts_prepare_items_query', $query_args, $request, $this ); |
|
136 | + |
|
137 | + } |
|
138 | + |
|
139 | + /** |
|
140 | + * Retrieves a valid list of post statuses. |
|
141 | + * |
|
142 | + * @since 1.0.15 |
|
143 | + * |
|
144 | + * @return array A list of registered item statuses. |
|
145 | + */ |
|
146 | + public function get_post_statuses() { |
|
147 | + return array( 'publish', 'pending', 'draft', 'expired' ); |
|
148 | + } |
|
149 | 149 | |
150 | 150 | } |