@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | /** |
@@ -10,30 +10,30 @@ discard block |
||
10 | 10 | class WPInv_Item extends GetPaid_Data { |
11 | 11 | |
12 | 12 | /** |
13 | - * Which data store to load. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
13 | + * Which data store to load. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | 17 | protected $data_store_name = 'item'; |
18 | 18 | |
19 | 19 | /** |
20 | - * This is the name of this object type. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $object_type = 'item'; |
|
20 | + * This is the name of this object type. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $object_type = 'item'; |
|
25 | 25 | |
26 | 26 | /** |
27 | - * Item Data array. This is the core item data exposed in APIs. |
|
28 | - * |
|
29 | - * @since 1.0.19 |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $data = array( |
|
33 | - 'parent_id' => 0, |
|
34 | - 'status' => 'draft', |
|
35 | - 'version' => '', |
|
36 | - 'date_created' => null, |
|
27 | + * Item Data array. This is the core item data exposed in APIs. |
|
28 | + * |
|
29 | + * @since 1.0.19 |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $data = array( |
|
33 | + 'parent_id' => 0, |
|
34 | + 'status' => 'draft', |
|
35 | + 'version' => '', |
|
36 | + 'date_created' => null, |
|
37 | 37 | 'date_modified' => null, |
38 | 38 | 'name' => '', |
39 | 39 | 'description' => '', |
@@ -58,13 +58,13 @@ discard block |
||
58 | 58 | ); |
59 | 59 | |
60 | 60 | /** |
61 | - * Stores meta in cache for future reads. |
|
62 | - * |
|
63 | - * A group must be set to to enable caching. |
|
64 | - * |
|
65 | - * @var string |
|
66 | - */ |
|
67 | - protected $cache_group = 'getpaid_items'; |
|
61 | + * Stores meta in cache for future reads. |
|
62 | + * |
|
63 | + * A group must be set to to enable caching. |
|
64 | + * |
|
65 | + * @var string |
|
66 | + */ |
|
67 | + protected $cache_group = 'getpaid_items'; |
|
68 | 68 | |
69 | 69 | /** |
70 | 70 | * Stores a reference to the original WP_Post object |
@@ -74,37 +74,37 @@ discard block |
||
74 | 74 | protected $post = null; |
75 | 75 | |
76 | 76 | /** |
77 | - * Get the item if ID is passed, otherwise the item is new and empty. |
|
78 | - * |
|
79 | - * @param int|object|WPInv_Item|WP_Post $item Item to read. |
|
80 | - */ |
|
81 | - public function __construct( $item = 0 ) { |
|
82 | - parent::__construct( $item ); |
|
83 | - |
|
84 | - if ( ! empty( $item ) && is_numeric( $item ) && 'wpi_item' == get_post_type( $item ) ) { |
|
85 | - $this->set_id( $item ); |
|
86 | - } elseif ( $item instanceof self ) { |
|
87 | - $this->set_id( $item->get_id() ); |
|
88 | - } elseif ( ! empty( $item->ID ) ) { |
|
89 | - $this->set_id( $item->ID ); |
|
90 | - } elseif ( is_scalar( $item ) && $item_id = self::get_item_id_by_field( $item, 'custom_id' ) ) { |
|
91 | - $this->set_id( $item_id ); |
|
92 | - } elseif ( is_scalar( $item ) && $item_id = self::get_item_id_by_field( $item, 'name' ) ) { |
|
93 | - $this->set_id( $item_id ); |
|
94 | - } else { |
|
95 | - $this->set_object_read( true ); |
|
96 | - } |
|
77 | + * Get the item if ID is passed, otherwise the item is new and empty. |
|
78 | + * |
|
79 | + * @param int|object|WPInv_Item|WP_Post $item Item to read. |
|
80 | + */ |
|
81 | + public function __construct( $item = 0 ) { |
|
82 | + parent::__construct( $item ); |
|
83 | + |
|
84 | + if ( ! empty( $item ) && is_numeric( $item ) && 'wpi_item' == get_post_type( $item ) ) { |
|
85 | + $this->set_id( $item ); |
|
86 | + } elseif ( $item instanceof self ) { |
|
87 | + $this->set_id( $item->get_id() ); |
|
88 | + } elseif ( ! empty( $item->ID ) ) { |
|
89 | + $this->set_id( $item->ID ); |
|
90 | + } elseif ( is_scalar( $item ) && $item_id = self::get_item_id_by_field( $item, 'custom_id' ) ) { |
|
91 | + $this->set_id( $item_id ); |
|
92 | + } elseif ( is_scalar( $item ) && $item_id = self::get_item_id_by_field( $item, 'name' ) ) { |
|
93 | + $this->set_id( $item_id ); |
|
94 | + } else { |
|
95 | + $this->set_object_read( true ); |
|
96 | + } |
|
97 | 97 | |
98 | 98 | // Load the datastore. |
99 | - $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
99 | + $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
100 | 100 | |
101 | - if ( $this->get_id() > 0 ) { |
|
101 | + if ( $this->get_id() > 0 ) { |
|
102 | 102 | $this->post = get_post( $this->get_id() ); |
103 | 103 | $this->ID = $this->get_id(); |
104 | - $this->data_store->read( $this ); |
|
104 | + $this->data_store->read( $this ); |
|
105 | 105 | } |
106 | 106 | |
107 | - } |
|
107 | + } |
|
108 | 108 | |
109 | 109 | /* |
110 | 110 | |-------------------------------------------------------------------------- |
@@ -122,401 +122,401 @@ discard block |
||
122 | 122 | */ |
123 | 123 | |
124 | 124 | /** |
125 | - * Get parent item ID. |
|
126 | - * |
|
127 | - * @since 1.0.19 |
|
128 | - * @param string $context View or edit context. |
|
129 | - * @return int |
|
130 | - */ |
|
131 | - public function get_parent_id( $context = 'view' ) { |
|
132 | - return (int) $this->get_prop( 'parent_id', $context ); |
|
125 | + * Get parent item ID. |
|
126 | + * |
|
127 | + * @since 1.0.19 |
|
128 | + * @param string $context View or edit context. |
|
129 | + * @return int |
|
130 | + */ |
|
131 | + public function get_parent_id( $context = 'view' ) { |
|
132 | + return (int) $this->get_prop( 'parent_id', $context ); |
|
133 | 133 | } |
134 | 134 | |
135 | 135 | /** |
136 | - * Get item status. |
|
137 | - * |
|
138 | - * @since 1.0.19 |
|
139 | - * @param string $context View or edit context. |
|
140 | - * @return string |
|
141 | - */ |
|
142 | - public function get_status( $context = 'view' ) { |
|
143 | - return $this->get_prop( 'status', $context ); |
|
136 | + * Get item status. |
|
137 | + * |
|
138 | + * @since 1.0.19 |
|
139 | + * @param string $context View or edit context. |
|
140 | + * @return string |
|
141 | + */ |
|
142 | + public function get_status( $context = 'view' ) { |
|
143 | + return $this->get_prop( 'status', $context ); |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | /** |
147 | - * Get plugin version when the item was created. |
|
148 | - * |
|
149 | - * @since 1.0.19 |
|
150 | - * @param string $context View or edit context. |
|
151 | - * @return string |
|
152 | - */ |
|
153 | - public function get_version( $context = 'view' ) { |
|
154 | - return $this->get_prop( 'version', $context ); |
|
147 | + * Get plugin version when the item was created. |
|
148 | + * |
|
149 | + * @since 1.0.19 |
|
150 | + * @param string $context View or edit context. |
|
151 | + * @return string |
|
152 | + */ |
|
153 | + public function get_version( $context = 'view' ) { |
|
154 | + return $this->get_prop( 'version', $context ); |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | /** |
158 | - * Get date when the item was created. |
|
159 | - * |
|
160 | - * @since 1.0.19 |
|
161 | - * @param string $context View or edit context. |
|
162 | - * @return string |
|
163 | - */ |
|
164 | - public function get_date_created( $context = 'view' ) { |
|
165 | - return $this->get_prop( 'date_created', $context ); |
|
158 | + * Get date when the item was created. |
|
159 | + * |
|
160 | + * @since 1.0.19 |
|
161 | + * @param string $context View or edit context. |
|
162 | + * @return string |
|
163 | + */ |
|
164 | + public function get_date_created( $context = 'view' ) { |
|
165 | + return $this->get_prop( 'date_created', $context ); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | /** |
169 | - * Get GMT date when the item was created. |
|
170 | - * |
|
171 | - * @since 1.0.19 |
|
172 | - * @param string $context View or edit context. |
|
173 | - * @return string |
|
174 | - */ |
|
175 | - public function get_date_created_gmt( $context = 'view' ) { |
|
169 | + * Get GMT date when the item was created. |
|
170 | + * |
|
171 | + * @since 1.0.19 |
|
172 | + * @param string $context View or edit context. |
|
173 | + * @return string |
|
174 | + */ |
|
175 | + public function get_date_created_gmt( $context = 'view' ) { |
|
176 | 176 | $date = $this->get_date_created( $context ); |
177 | 177 | |
178 | 178 | if ( $date ) { |
179 | 179 | $date = get_gmt_from_date( $date ); |
180 | 180 | } |
181 | - return $date; |
|
181 | + return $date; |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | /** |
185 | - * Get date when the item was last modified. |
|
186 | - * |
|
187 | - * @since 1.0.19 |
|
188 | - * @param string $context View or edit context. |
|
189 | - * @return string |
|
190 | - */ |
|
191 | - public function get_date_modified( $context = 'view' ) { |
|
192 | - return $this->get_prop( 'date_modified', $context ); |
|
185 | + * Get date when the item was last modified. |
|
186 | + * |
|
187 | + * @since 1.0.19 |
|
188 | + * @param string $context View or edit context. |
|
189 | + * @return string |
|
190 | + */ |
|
191 | + public function get_date_modified( $context = 'view' ) { |
|
192 | + return $this->get_prop( 'date_modified', $context ); |
|
193 | 193 | } |
194 | 194 | |
195 | 195 | /** |
196 | - * Get GMT date when the item was last modified. |
|
197 | - * |
|
198 | - * @since 1.0.19 |
|
199 | - * @param string $context View or edit context. |
|
200 | - * @return string |
|
201 | - */ |
|
202 | - public function get_date_modified_gmt( $context = 'view' ) { |
|
196 | + * Get GMT date when the item was last modified. |
|
197 | + * |
|
198 | + * @since 1.0.19 |
|
199 | + * @param string $context View or edit context. |
|
200 | + * @return string |
|
201 | + */ |
|
202 | + public function get_date_modified_gmt( $context = 'view' ) { |
|
203 | 203 | $date = $this->get_date_modified( $context ); |
204 | 204 | |
205 | 205 | if ( $date ) { |
206 | 206 | $date = get_gmt_from_date( $date ); |
207 | 207 | } |
208 | - return $date; |
|
208 | + return $date; |
|
209 | 209 | } |
210 | 210 | |
211 | 211 | /** |
212 | - * Get the item name. |
|
213 | - * |
|
214 | - * @since 1.0.19 |
|
215 | - * @param string $context View or edit context. |
|
216 | - * @return string |
|
217 | - */ |
|
218 | - public function get_name( $context = 'view' ) { |
|
219 | - return $this->get_prop( 'name', $context ); |
|
212 | + * Get the item name. |
|
213 | + * |
|
214 | + * @since 1.0.19 |
|
215 | + * @param string $context View or edit context. |
|
216 | + * @return string |
|
217 | + */ |
|
218 | + public function get_name( $context = 'view' ) { |
|
219 | + return $this->get_prop( 'name', $context ); |
|
220 | 220 | } |
221 | 221 | |
222 | 222 | /** |
223 | - * Alias of self::get_name(). |
|
224 | - * |
|
225 | - * @since 1.0.19 |
|
226 | - * @param string $context View or edit context. |
|
227 | - * @return string |
|
228 | - */ |
|
229 | - public function get_title( $context = 'view' ) { |
|
230 | - return $this->get_name( $context ); |
|
223 | + * Alias of self::get_name(). |
|
224 | + * |
|
225 | + * @since 1.0.19 |
|
226 | + * @param string $context View or edit context. |
|
227 | + * @return string |
|
228 | + */ |
|
229 | + public function get_title( $context = 'view' ) { |
|
230 | + return $this->get_name( $context ); |
|
231 | 231 | } |
232 | 232 | |
233 | 233 | /** |
234 | - * Get the item description. |
|
235 | - * |
|
236 | - * @since 1.0.19 |
|
237 | - * @param string $context View or edit context. |
|
238 | - * @return string |
|
239 | - */ |
|
240 | - public function get_description( $context = 'view' ) { |
|
241 | - return $this->get_prop( 'description', $context ); |
|
234 | + * Get the item description. |
|
235 | + * |
|
236 | + * @since 1.0.19 |
|
237 | + * @param string $context View or edit context. |
|
238 | + * @return string |
|
239 | + */ |
|
240 | + public function get_description( $context = 'view' ) { |
|
241 | + return $this->get_prop( 'description', $context ); |
|
242 | 242 | } |
243 | 243 | |
244 | 244 | /** |
245 | - * Alias of self::get_description(). |
|
246 | - * |
|
247 | - * @since 1.0.19 |
|
248 | - * @param string $context View or edit context. |
|
249 | - * @return string |
|
250 | - */ |
|
251 | - public function get_excerpt( $context = 'view' ) { |
|
252 | - return $this->get_description( $context ); |
|
245 | + * Alias of self::get_description(). |
|
246 | + * |
|
247 | + * @since 1.0.19 |
|
248 | + * @param string $context View or edit context. |
|
249 | + * @return string |
|
250 | + */ |
|
251 | + public function get_excerpt( $context = 'view' ) { |
|
252 | + return $this->get_description( $context ); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | /** |
256 | - * Alias of self::get_description(). |
|
257 | - * |
|
258 | - * @since 1.0.19 |
|
259 | - * @param string $context View or edit context. |
|
260 | - * @return string |
|
261 | - */ |
|
262 | - public function get_summary( $context = 'view' ) { |
|
263 | - return $this->get_description( $context ); |
|
256 | + * Alias of self::get_description(). |
|
257 | + * |
|
258 | + * @since 1.0.19 |
|
259 | + * @param string $context View or edit context. |
|
260 | + * @return string |
|
261 | + */ |
|
262 | + public function get_summary( $context = 'view' ) { |
|
263 | + return $this->get_description( $context ); |
|
264 | 264 | } |
265 | 265 | |
266 | 266 | /** |
267 | - * Get the owner of the item. |
|
268 | - * |
|
269 | - * @since 1.0.19 |
|
270 | - * @param string $context View or edit context. |
|
271 | - * @return int |
|
272 | - */ |
|
273 | - public function get_author( $context = 'view' ) { |
|
274 | - return (int) $this->get_prop( 'author', $context ); |
|
275 | - } |
|
267 | + * Get the owner of the item. |
|
268 | + * |
|
269 | + * @since 1.0.19 |
|
270 | + * @param string $context View or edit context. |
|
271 | + * @return int |
|
272 | + */ |
|
273 | + public function get_author( $context = 'view' ) { |
|
274 | + return (int) $this->get_prop( 'author', $context ); |
|
275 | + } |
|
276 | 276 | |
277 | - /** |
|
278 | - * Alias of self::get_author(). |
|
279 | - * |
|
280 | - * @since 1.0.19 |
|
281 | - * @param string $context View or edit context. |
|
282 | - * @return int |
|
283 | - */ |
|
284 | - public function get_owner( $context = 'view' ) { |
|
285 | - return $this->get_author( $context ); |
|
286 | - } |
|
287 | - |
|
288 | - /** |
|
289 | - * Get the price of the item. |
|
290 | - * |
|
291 | - * @since 1.0.19 |
|
292 | - * @param string $context View or edit context. |
|
293 | - * @return float |
|
294 | - */ |
|
295 | - public function get_price( $context = 'view' ) { |
|
277 | + /** |
|
278 | + * Alias of self::get_author(). |
|
279 | + * |
|
280 | + * @since 1.0.19 |
|
281 | + * @param string $context View or edit context. |
|
282 | + * @return int |
|
283 | + */ |
|
284 | + public function get_owner( $context = 'view' ) { |
|
285 | + return $this->get_author( $context ); |
|
286 | + } |
|
287 | + |
|
288 | + /** |
|
289 | + * Get the price of the item. |
|
290 | + * |
|
291 | + * @since 1.0.19 |
|
292 | + * @param string $context View or edit context. |
|
293 | + * @return float |
|
294 | + */ |
|
295 | + public function get_price( $context = 'view' ) { |
|
296 | 296 | return wpinv_sanitize_amount( $this->get_prop( 'price', $context ) ); |
297 | - } |
|
297 | + } |
|
298 | 298 | |
299 | - /** |
|
300 | - * Get the inital price of the item. |
|
301 | - * |
|
302 | - * @since 1.0.19 |
|
303 | - * @param string $context View or edit context. |
|
304 | - * @return float |
|
305 | - */ |
|
306 | - public function get_initial_price( $context = 'view' ) { |
|
299 | + /** |
|
300 | + * Get the inital price of the item. |
|
301 | + * |
|
302 | + * @since 1.0.19 |
|
303 | + * @param string $context View or edit context. |
|
304 | + * @return float |
|
305 | + */ |
|
306 | + public function get_initial_price( $context = 'view' ) { |
|
307 | 307 | |
308 | - $price = (float) $this->get_price( $context ); |
|
308 | + $price = (float) $this->get_price( $context ); |
|
309 | 309 | |
310 | - if ( $this->has_free_trial() ) { |
|
311 | - $price = 0; |
|
312 | - } |
|
310 | + if ( $this->has_free_trial() ) { |
|
311 | + $price = 0; |
|
312 | + } |
|
313 | 313 | |
314 | 314 | return wpinv_sanitize_amount( apply_filters( 'wpinv_get_initial_item_price', $price, $this ) ); |
315 | 315 | } |
316 | 316 | |
317 | 317 | /** |
318 | - * Returns a formated price. |
|
319 | - * |
|
320 | - * @since 1.0.19 |
|
321 | - * @param string $context View or edit context. |
|
322 | - * @return string |
|
323 | - */ |
|
318 | + * Returns a formated price. |
|
319 | + * |
|
320 | + * @since 1.0.19 |
|
321 | + * @param string $context View or edit context. |
|
322 | + * @return string |
|
323 | + */ |
|
324 | 324 | public function get_the_price() { |
325 | 325 | return wpinv_price( $this->get_price() ); |
326 | - } |
|
327 | - |
|
328 | - /** |
|
329 | - * Returns the formated initial price. |
|
330 | - * |
|
331 | - * @since 1.0.19 |
|
332 | - * @param string $context View or edit context. |
|
333 | - * @return string |
|
334 | - */ |
|
326 | + } |
|
327 | + |
|
328 | + /** |
|
329 | + * Returns the formated initial price. |
|
330 | + * |
|
331 | + * @since 1.0.19 |
|
332 | + * @param string $context View or edit context. |
|
333 | + * @return string |
|
334 | + */ |
|
335 | 335 | public function get_the_initial_price() { |
336 | 336 | return wpinv_price( $this->get_initial_price() ); |
337 | 337 | } |
338 | 338 | |
339 | 339 | /** |
340 | - * Get the VAT rule of the item. |
|
341 | - * |
|
342 | - * @since 1.0.19 |
|
343 | - * @param string $context View or edit context. |
|
344 | - * @return string |
|
345 | - */ |
|
346 | - public function get_vat_rule( $context = 'view' ) { |
|
340 | + * Get the VAT rule of the item. |
|
341 | + * |
|
342 | + * @since 1.0.19 |
|
343 | + * @param string $context View or edit context. |
|
344 | + * @return string |
|
345 | + */ |
|
346 | + public function get_vat_rule( $context = 'view' ) { |
|
347 | 347 | return $this->get_prop( 'vat_rule', $context ); |
348 | 348 | } |
349 | 349 | |
350 | 350 | /** |
351 | - * Get the VAT class of the item. |
|
352 | - * |
|
353 | - * @since 1.0.19 |
|
354 | - * @param string $context View or edit context. |
|
355 | - * @return string |
|
356 | - */ |
|
357 | - public function get_vat_class( $context = 'view' ) { |
|
351 | + * Get the VAT class of the item. |
|
352 | + * |
|
353 | + * @since 1.0.19 |
|
354 | + * @param string $context View or edit context. |
|
355 | + * @return string |
|
356 | + */ |
|
357 | + public function get_vat_class( $context = 'view' ) { |
|
358 | 358 | return $this->get_prop( 'vat_class', $context ); |
359 | 359 | } |
360 | 360 | |
361 | 361 | /** |
362 | - * Get the type of the item. |
|
363 | - * |
|
364 | - * @since 1.0.19 |
|
365 | - * @param string $context View or edit context. |
|
366 | - * @return string |
|
367 | - */ |
|
368 | - public function get_type( $context = 'view' ) { |
|
362 | + * Get the type of the item. |
|
363 | + * |
|
364 | + * @since 1.0.19 |
|
365 | + * @param string $context View or edit context. |
|
366 | + * @return string |
|
367 | + */ |
|
368 | + public function get_type( $context = 'view' ) { |
|
369 | 369 | return $this->get_prop( 'type', $context ); |
370 | 370 | } |
371 | 371 | |
372 | 372 | /** |
373 | - * Get the custom id of the item. |
|
374 | - * |
|
375 | - * @since 1.0.19 |
|
376 | - * @param string $context View or edit context. |
|
377 | - * @return string |
|
378 | - */ |
|
379 | - public function get_custom_id( $context = 'view' ) { |
|
373 | + * Get the custom id of the item. |
|
374 | + * |
|
375 | + * @since 1.0.19 |
|
376 | + * @param string $context View or edit context. |
|
377 | + * @return string |
|
378 | + */ |
|
379 | + public function get_custom_id( $context = 'view' ) { |
|
380 | 380 | return $this->get_prop( 'custom_id', $context ); |
381 | 381 | } |
382 | 382 | |
383 | 383 | /** |
384 | - * Get the custom name of the item. |
|
385 | - * |
|
386 | - * @since 1.0.19 |
|
387 | - * @param string $context View or edit context. |
|
388 | - * @return string |
|
389 | - */ |
|
390 | - public function get_custom_name( $context = 'view' ) { |
|
384 | + * Get the custom name of the item. |
|
385 | + * |
|
386 | + * @since 1.0.19 |
|
387 | + * @param string $context View or edit context. |
|
388 | + * @return string |
|
389 | + */ |
|
390 | + public function get_custom_name( $context = 'view' ) { |
|
391 | 391 | return $this->get_prop( 'custom_name', $context ); |
392 | 392 | } |
393 | 393 | |
394 | 394 | /** |
395 | - * Get the custom singular name of the item. |
|
396 | - * |
|
397 | - * @since 1.0.19 |
|
398 | - * @param string $context View or edit context. |
|
399 | - * @return string |
|
400 | - */ |
|
401 | - public function get_custom_singular_name( $context = 'view' ) { |
|
395 | + * Get the custom singular name of the item. |
|
396 | + * |
|
397 | + * @since 1.0.19 |
|
398 | + * @param string $context View or edit context. |
|
399 | + * @return string |
|
400 | + */ |
|
401 | + public function get_custom_singular_name( $context = 'view' ) { |
|
402 | 402 | return $this->get_prop( 'custom_singular_name', $context ); |
403 | 403 | } |
404 | 404 | |
405 | 405 | /** |
406 | - * Checks if an item is editable.. |
|
407 | - * |
|
408 | - * @since 1.0.19 |
|
409 | - * @param string $context View or edit context. |
|
410 | - * @return int |
|
411 | - */ |
|
412 | - public function get_is_editable( $context = 'view' ) { |
|
406 | + * Checks if an item is editable.. |
|
407 | + * |
|
408 | + * @since 1.0.19 |
|
409 | + * @param string $context View or edit context. |
|
410 | + * @return int |
|
411 | + */ |
|
412 | + public function get_is_editable( $context = 'view' ) { |
|
413 | 413 | return (int) $this->get_prop( 'is_editable', $context ); |
414 | 414 | } |
415 | 415 | |
416 | 416 | /** |
417 | - * Alias of self::get_is_editable(). |
|
418 | - * |
|
419 | - * @since 1.0.19 |
|
420 | - * @param string $context View or edit context. |
|
421 | - * @return int |
|
422 | - */ |
|
423 | - public function get_editable( $context = 'view' ) { |
|
424 | - return $this->get_is_editable( $context ); |
|
417 | + * Alias of self::get_is_editable(). |
|
418 | + * |
|
419 | + * @since 1.0.19 |
|
420 | + * @param string $context View or edit context. |
|
421 | + * @return int |
|
422 | + */ |
|
423 | + public function get_editable( $context = 'view' ) { |
|
424 | + return $this->get_is_editable( $context ); |
|
425 | 425 | } |
426 | 426 | |
427 | 427 | /** |
428 | - * Checks if dynamic pricing is enabled. |
|
429 | - * |
|
430 | - * @since 1.0.19 |
|
431 | - * @param string $context View or edit context. |
|
432 | - * @return int |
|
433 | - */ |
|
434 | - public function get_is_dynamic_pricing( $context = 'view' ) { |
|
428 | + * Checks if dynamic pricing is enabled. |
|
429 | + * |
|
430 | + * @since 1.0.19 |
|
431 | + * @param string $context View or edit context. |
|
432 | + * @return int |
|
433 | + */ |
|
434 | + public function get_is_dynamic_pricing( $context = 'view' ) { |
|
435 | 435 | return (int) $this->get_prop( 'is_dynamic_pricing', $context ); |
436 | 436 | } |
437 | 437 | |
438 | 438 | /** |
439 | - * Returns the minimum price if dynamic pricing is enabled. |
|
440 | - * |
|
441 | - * @since 1.0.19 |
|
442 | - * @param string $context View or edit context. |
|
443 | - * @return float |
|
444 | - */ |
|
445 | - public function get_minimum_price( $context = 'view' ) { |
|
439 | + * Returns the minimum price if dynamic pricing is enabled. |
|
440 | + * |
|
441 | + * @since 1.0.19 |
|
442 | + * @param string $context View or edit context. |
|
443 | + * @return float |
|
444 | + */ |
|
445 | + public function get_minimum_price( $context = 'view' ) { |
|
446 | 446 | return wpinv_sanitize_amount( $this->get_prop( 'minimum_price', $context ) ); |
447 | 447 | } |
448 | 448 | |
449 | 449 | /** |
450 | - * Checks if this is a recurring item. |
|
451 | - * |
|
452 | - * @since 1.0.19 |
|
453 | - * @param string $context View or edit context. |
|
454 | - * @return int |
|
455 | - */ |
|
456 | - public function get_is_recurring( $context = 'view' ) { |
|
450 | + * Checks if this is a recurring item. |
|
451 | + * |
|
452 | + * @since 1.0.19 |
|
453 | + * @param string $context View or edit context. |
|
454 | + * @return int |
|
455 | + */ |
|
456 | + public function get_is_recurring( $context = 'view' ) { |
|
457 | 457 | return (int) $this->get_prop( 'is_recurring', $context ); |
458 | - } |
|
458 | + } |
|
459 | 459 | |
460 | - /** |
|
461 | - * Get the recurring price of the item. |
|
462 | - * |
|
463 | - * @since 1.0.19 |
|
464 | - * @param string $context View or edit context. |
|
465 | - * @return float |
|
466 | - */ |
|
467 | - public function get_recurring_price( $context = 'view' ) { |
|
468 | - $price = $this->get_price( $context ); |
|
460 | + /** |
|
461 | + * Get the recurring price of the item. |
|
462 | + * |
|
463 | + * @since 1.0.19 |
|
464 | + * @param string $context View or edit context. |
|
465 | + * @return float |
|
466 | + */ |
|
467 | + public function get_recurring_price( $context = 'view' ) { |
|
468 | + $price = $this->get_price( $context ); |
|
469 | 469 | return wpinv_sanitize_amount( apply_filters( 'wpinv_get_recurring_item_price', $price, $this->ID ) ); |
470 | - } |
|
471 | - |
|
472 | - /** |
|
473 | - * Get the formatted recurring price of the item. |
|
474 | - * |
|
475 | - * @since 1.0.19 |
|
476 | - * @param string $context View or edit context. |
|
477 | - * @return string |
|
478 | - */ |
|
470 | + } |
|
471 | + |
|
472 | + /** |
|
473 | + * Get the formatted recurring price of the item. |
|
474 | + * |
|
475 | + * @since 1.0.19 |
|
476 | + * @param string $context View or edit context. |
|
477 | + * @return string |
|
478 | + */ |
|
479 | 479 | public function get_the_recurring_price() { |
480 | 480 | return wpinv_price( $this->get_recurring_price() ); |
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Get the first renewal date (in timestamps) of the item. |
|
485 | - * |
|
486 | - * @since 1.0.19 |
|
487 | - * @return int |
|
488 | - */ |
|
489 | - public function get_first_renewal_date() { |
|
490 | - |
|
491 | - $periods = array( |
|
492 | - 'D' => 'days', |
|
493 | - 'W' => 'weeks', |
|
494 | - 'M' => 'months', |
|
495 | - 'Y' => 'years', |
|
496 | - ); |
|
497 | - |
|
498 | - $period = $this->get_recurring_period(); |
|
499 | - $interval = $this->get_recurring_interval(); |
|
500 | - |
|
501 | - if ( $this->has_free_trial() ) { |
|
502 | - $period = $this->get_trial_period(); |
|
503 | - $interval = $this->get_trial_interval(); |
|
504 | - } |
|
505 | - |
|
506 | - $period = $periods[ $period ]; |
|
507 | - $interval = empty( $interval ) ? 1 : $interval; |
|
508 | - $next_renewal = strtotime( "+$interval $period", current_time( 'timestamp' ) ); |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Get the first renewal date (in timestamps) of the item. |
|
485 | + * |
|
486 | + * @since 1.0.19 |
|
487 | + * @return int |
|
488 | + */ |
|
489 | + public function get_first_renewal_date() { |
|
490 | + |
|
491 | + $periods = array( |
|
492 | + 'D' => 'days', |
|
493 | + 'W' => 'weeks', |
|
494 | + 'M' => 'months', |
|
495 | + 'Y' => 'years', |
|
496 | + ); |
|
497 | + |
|
498 | + $period = $this->get_recurring_period(); |
|
499 | + $interval = $this->get_recurring_interval(); |
|
500 | + |
|
501 | + if ( $this->has_free_trial() ) { |
|
502 | + $period = $this->get_trial_period(); |
|
503 | + $interval = $this->get_trial_interval(); |
|
504 | + } |
|
505 | + |
|
506 | + $period = $periods[ $period ]; |
|
507 | + $interval = empty( $interval ) ? 1 : $interval; |
|
508 | + $next_renewal = strtotime( "+$interval $period", current_time( 'timestamp' ) ); |
|
509 | 509 | return apply_filters( 'wpinv_get_first_renewal_date', $next_renewal, $this ); |
510 | 510 | } |
511 | 511 | |
512 | 512 | /** |
513 | - * Get the recurring period. |
|
514 | - * |
|
515 | - * @since 1.0.19 |
|
516 | - * @param bool $full Return abbreviation or in full. |
|
517 | - * @return string |
|
518 | - */ |
|
519 | - public function get_recurring_period( $full = false ) { |
|
513 | + * Get the recurring period. |
|
514 | + * |
|
515 | + * @since 1.0.19 |
|
516 | + * @param bool $full Return abbreviation or in full. |
|
517 | + * @return string |
|
518 | + */ |
|
519 | + public function get_recurring_period( $full = false ) { |
|
520 | 520 | $period = $this->get_prop( 'recurring_period', 'view' ); |
521 | 521 | |
522 | 522 | if ( $full && ! is_bool( $full ) ) { |
@@ -527,63 +527,63 @@ discard block |
||
527 | 527 | } |
528 | 528 | |
529 | 529 | /** |
530 | - * Get the recurring interval. |
|
531 | - * |
|
532 | - * @since 1.0.19 |
|
533 | - * @param string $context View or edit context. |
|
534 | - * @return int |
|
535 | - */ |
|
536 | - public function get_recurring_interval( $context = 'view' ) { |
|
537 | - $interval = absint( $this->get_prop( 'recurring_interval', $context ) ); |
|
530 | + * Get the recurring interval. |
|
531 | + * |
|
532 | + * @since 1.0.19 |
|
533 | + * @param string $context View or edit context. |
|
534 | + * @return int |
|
535 | + */ |
|
536 | + public function get_recurring_interval( $context = 'view' ) { |
|
537 | + $interval = absint( $this->get_prop( 'recurring_interval', $context ) ); |
|
538 | 538 | |
539 | - if ( $interval < 1 ) { |
|
540 | - $interval = 1; |
|
541 | - } |
|
539 | + if ( $interval < 1 ) { |
|
540 | + $interval = 1; |
|
541 | + } |
|
542 | 542 | |
543 | 543 | return $interval; |
544 | 544 | } |
545 | 545 | |
546 | 546 | /** |
547 | - * Get the recurring limit. |
|
548 | - * |
|
549 | - * @since 1.0.19 |
|
550 | - * @param string $context View or edit context. |
|
551 | - * @return int |
|
552 | - */ |
|
553 | - public function get_recurring_limit( $context = 'view' ) { |
|
547 | + * Get the recurring limit. |
|
548 | + * |
|
549 | + * @since 1.0.19 |
|
550 | + * @param string $context View or edit context. |
|
551 | + * @return int |
|
552 | + */ |
|
553 | + public function get_recurring_limit( $context = 'view' ) { |
|
554 | 554 | return (int) $this->get_prop( 'recurring_limit', $context ); |
555 | 555 | } |
556 | 556 | |
557 | 557 | /** |
558 | - * Checks if we have a free trial. |
|
559 | - * |
|
560 | - * @since 1.0.19 |
|
561 | - * @param string $context View or edit context. |
|
562 | - * @return int |
|
563 | - */ |
|
564 | - public function get_is_free_trial( $context = 'view' ) { |
|
558 | + * Checks if we have a free trial. |
|
559 | + * |
|
560 | + * @since 1.0.19 |
|
561 | + * @param string $context View or edit context. |
|
562 | + * @return int |
|
563 | + */ |
|
564 | + public function get_is_free_trial( $context = 'view' ) { |
|
565 | 565 | return (int) $this->get_prop( 'is_free_trial', $context ); |
566 | 566 | } |
567 | 567 | |
568 | 568 | /** |
569 | - * Alias for self::get_is_free_trial(). |
|
570 | - * |
|
571 | - * @since 1.0.19 |
|
572 | - * @param string $context View or edit context. |
|
573 | - * @return int |
|
574 | - */ |
|
575 | - public function get_free_trial( $context = 'view' ) { |
|
569 | + * Alias for self::get_is_free_trial(). |
|
570 | + * |
|
571 | + * @since 1.0.19 |
|
572 | + * @param string $context View or edit context. |
|
573 | + * @return int |
|
574 | + */ |
|
575 | + public function get_free_trial( $context = 'view' ) { |
|
576 | 576 | return $this->get_is_free_trial( $context ); |
577 | 577 | } |
578 | 578 | |
579 | 579 | /** |
580 | - * Get the trial period. |
|
581 | - * |
|
582 | - * @since 1.0.19 |
|
583 | - * @param bool $full Return abbreviation or in full. |
|
584 | - * @return string |
|
585 | - */ |
|
586 | - public function get_trial_period( $full = false ) { |
|
580 | + * Get the trial period. |
|
581 | + * |
|
582 | + * @since 1.0.19 |
|
583 | + * @param bool $full Return abbreviation or in full. |
|
584 | + * @return string |
|
585 | + */ |
|
586 | + public function get_trial_period( $full = false ) { |
|
587 | 587 | $period = $this->get_prop( 'trial_period', 'view' ); |
588 | 588 | |
589 | 589 | if ( $full && ! is_bool( $full ) ) { |
@@ -594,104 +594,104 @@ discard block |
||
594 | 594 | } |
595 | 595 | |
596 | 596 | /** |
597 | - * Get the trial interval. |
|
598 | - * |
|
599 | - * @since 1.0.19 |
|
600 | - * @param string $context View or edit context. |
|
601 | - * @return int |
|
602 | - */ |
|
603 | - public function get_trial_interval( $context = 'view' ) { |
|
597 | + * Get the trial interval. |
|
598 | + * |
|
599 | + * @since 1.0.19 |
|
600 | + * @param string $context View or edit context. |
|
601 | + * @return int |
|
602 | + */ |
|
603 | + public function get_trial_interval( $context = 'view' ) { |
|
604 | 604 | return (int) $this->get_prop( 'trial_interval', $context ); |
605 | - } |
|
605 | + } |
|
606 | 606 | |
607 | - /** |
|
608 | - * Get the item's edit url. |
|
609 | - * |
|
610 | - * @since 1.0.19 |
|
611 | - * @return string |
|
612 | - */ |
|
613 | - public function get_edit_url() { |
|
607 | + /** |
|
608 | + * Get the item's edit url. |
|
609 | + * |
|
610 | + * @since 1.0.19 |
|
611 | + * @return string |
|
612 | + */ |
|
613 | + public function get_edit_url() { |
|
614 | 614 | return get_edit_post_link( $this->get_id() ); |
615 | - } |
|
616 | - |
|
617 | - /** |
|
618 | - * Given an item's name/custom id, it returns its id. |
|
619 | - * |
|
620 | - * |
|
621 | - * @static |
|
622 | - * @param string $value The item name or custom id. |
|
623 | - * @param string $field Either name or custom_id. |
|
624 | - * @param string $type in case you need to search for a given type. |
|
625 | - * @since 1.0.15 |
|
626 | - * @return int |
|
627 | - */ |
|
628 | - public static function get_item_id_by_field( $value, $field = 'custom_id', $type = '' ) { |
|
629 | - |
|
630 | - // Trim the value. |
|
631 | - $value = sanitize_text_field( $value ); |
|
632 | - if ( empty( $value ) ) { |
|
633 | - return 0; |
|
634 | - } |
|
615 | + } |
|
616 | + |
|
617 | + /** |
|
618 | + * Given an item's name/custom id, it returns its id. |
|
619 | + * |
|
620 | + * |
|
621 | + * @static |
|
622 | + * @param string $value The item name or custom id. |
|
623 | + * @param string $field Either name or custom_id. |
|
624 | + * @param string $type in case you need to search for a given type. |
|
625 | + * @since 1.0.15 |
|
626 | + * @return int |
|
627 | + */ |
|
628 | + public static function get_item_id_by_field( $value, $field = 'custom_id', $type = '' ) { |
|
629 | + |
|
630 | + // Trim the value. |
|
631 | + $value = sanitize_text_field( $value ); |
|
632 | + if ( empty( $value ) ) { |
|
633 | + return 0; |
|
634 | + } |
|
635 | 635 | |
636 | 636 | // Valid fields. |
637 | 637 | $fields = array( 'custom_id', 'name', 'slug' ); |
638 | 638 | |
639 | - // Ensure a field has been passed. |
|
640 | - if ( empty( $field ) || ! in_array( $field, $fields ) ) { |
|
641 | - return 0; |
|
642 | - } |
|
643 | - |
|
644 | - if ( $field == 'name' ) { |
|
645 | - $field = 'slug'; |
|
646 | - } |
|
647 | - |
|
648 | - // Maybe retrieve from the cache. |
|
649 | - $item_id = wp_cache_get( $value, "getpaid_{$type}_item_{$field}s_to_item_ids" ); |
|
650 | - if ( ! empty( $item_id ) ) { |
|
651 | - return $item_id; |
|
652 | - } |
|
653 | - |
|
654 | - // Fetch from the db. |
|
655 | - $items = array(); |
|
656 | - if ( $field == 'slug' ) { |
|
657 | - $items = get_posts( |
|
658 | - array( |
|
659 | - 'post_type' => 'wpi_item', |
|
660 | - 'name' => $value, |
|
661 | - 'posts_per_page' => 1, |
|
662 | - 'post_status' => 'any', |
|
663 | - ) |
|
664 | - ); |
|
665 | - } |
|
666 | - |
|
667 | - if ( $field =='custom_id' ) { |
|
668 | - $items = get_posts( |
|
669 | - array( |
|
670 | - 'post_type' => 'wpi_item', |
|
671 | - 'posts_per_page' => 1, |
|
672 | - 'post_status' => 'any', |
|
673 | - 'meta_query' => array( |
|
674 | - array( |
|
675 | - 'key' => '_wpinv_type', |
|
676 | - 'value' => $type, |
|
677 | - ), |
|
678 | - array( |
|
679 | - 'key' => '_wpinv_custom_id', |
|
680 | - 'value' => $value, |
|
681 | - ) |
|
682 | - ) |
|
683 | - ) |
|
684 | - ); |
|
685 | - } |
|
686 | - |
|
687 | - if ( empty( $items ) ) { |
|
688 | - return 0; |
|
689 | - } |
|
690 | - |
|
691 | - // Update the cache with our data |
|
692 | - wp_cache_set( $value, $items[0]->ID, "getpaid_{$type}_item_{$field}s_to_item_ids" ); |
|
693 | - |
|
694 | - return $items[0]->ID; |
|
639 | + // Ensure a field has been passed. |
|
640 | + if ( empty( $field ) || ! in_array( $field, $fields ) ) { |
|
641 | + return 0; |
|
642 | + } |
|
643 | + |
|
644 | + if ( $field == 'name' ) { |
|
645 | + $field = 'slug'; |
|
646 | + } |
|
647 | + |
|
648 | + // Maybe retrieve from the cache. |
|
649 | + $item_id = wp_cache_get( $value, "getpaid_{$type}_item_{$field}s_to_item_ids" ); |
|
650 | + if ( ! empty( $item_id ) ) { |
|
651 | + return $item_id; |
|
652 | + } |
|
653 | + |
|
654 | + // Fetch from the db. |
|
655 | + $items = array(); |
|
656 | + if ( $field == 'slug' ) { |
|
657 | + $items = get_posts( |
|
658 | + array( |
|
659 | + 'post_type' => 'wpi_item', |
|
660 | + 'name' => $value, |
|
661 | + 'posts_per_page' => 1, |
|
662 | + 'post_status' => 'any', |
|
663 | + ) |
|
664 | + ); |
|
665 | + } |
|
666 | + |
|
667 | + if ( $field =='custom_id' ) { |
|
668 | + $items = get_posts( |
|
669 | + array( |
|
670 | + 'post_type' => 'wpi_item', |
|
671 | + 'posts_per_page' => 1, |
|
672 | + 'post_status' => 'any', |
|
673 | + 'meta_query' => array( |
|
674 | + array( |
|
675 | + 'key' => '_wpinv_type', |
|
676 | + 'value' => $type, |
|
677 | + ), |
|
678 | + array( |
|
679 | + 'key' => '_wpinv_custom_id', |
|
680 | + 'value' => $value, |
|
681 | + ) |
|
682 | + ) |
|
683 | + ) |
|
684 | + ); |
|
685 | + } |
|
686 | + |
|
687 | + if ( empty( $items ) ) { |
|
688 | + return 0; |
|
689 | + } |
|
690 | + |
|
691 | + // Update the cache with our data |
|
692 | + wp_cache_set( $value, $items[0]->ID, "getpaid_{$type}_item_{$field}s_to_item_ids" ); |
|
693 | + |
|
694 | + return $items[0]->ID; |
|
695 | 695 | } |
696 | 696 | |
697 | 697 | /** |
@@ -724,52 +724,52 @@ discard block |
||
724 | 724 | */ |
725 | 725 | |
726 | 726 | /** |
727 | - * Set parent order ID. |
|
728 | - * |
|
729 | - * @since 1.0.19 |
|
730 | - */ |
|
731 | - public function set_parent_id( $value ) { |
|
732 | - if ( $value && ( $value === $this->get_id() || ! get_post( $value ) ) ) { |
|
733 | - return; |
|
734 | - } |
|
735 | - $this->set_prop( 'parent_id', absint( $value ) ); |
|
736 | - } |
|
737 | - |
|
738 | - /** |
|
739 | - * Sets item status. |
|
740 | - * |
|
741 | - * @since 1.0.19 |
|
742 | - * @param string $status New status. |
|
743 | - * @return array details of change. |
|
744 | - */ |
|
745 | - public function set_status( $status ) { |
|
727 | + * Set parent order ID. |
|
728 | + * |
|
729 | + * @since 1.0.19 |
|
730 | + */ |
|
731 | + public function set_parent_id( $value ) { |
|
732 | + if ( $value && ( $value === $this->get_id() || ! get_post( $value ) ) ) { |
|
733 | + return; |
|
734 | + } |
|
735 | + $this->set_prop( 'parent_id', absint( $value ) ); |
|
736 | + } |
|
737 | + |
|
738 | + /** |
|
739 | + * Sets item status. |
|
740 | + * |
|
741 | + * @since 1.0.19 |
|
742 | + * @param string $status New status. |
|
743 | + * @return array details of change. |
|
744 | + */ |
|
745 | + public function set_status( $status ) { |
|
746 | 746 | $old_status = $this->get_status(); |
747 | 747 | |
748 | 748 | $this->set_prop( 'status', $status ); |
749 | 749 | |
750 | - return array( |
|
751 | - 'from' => $old_status, |
|
752 | - 'to' => $status, |
|
753 | - ); |
|
750 | + return array( |
|
751 | + 'from' => $old_status, |
|
752 | + 'to' => $status, |
|
753 | + ); |
|
754 | 754 | } |
755 | 755 | |
756 | 756 | /** |
757 | - * Set plugin version when the item was created. |
|
758 | - * |
|
759 | - * @since 1.0.19 |
|
760 | - */ |
|
761 | - public function set_version( $value ) { |
|
762 | - $this->set_prop( 'version', $value ); |
|
757 | + * Set plugin version when the item was created. |
|
758 | + * |
|
759 | + * @since 1.0.19 |
|
760 | + */ |
|
761 | + public function set_version( $value ) { |
|
762 | + $this->set_prop( 'version', $value ); |
|
763 | 763 | } |
764 | 764 | |
765 | 765 | /** |
766 | - * Set date when the item was created. |
|
767 | - * |
|
768 | - * @since 1.0.19 |
|
769 | - * @param string $value Value to set. |
|
766 | + * Set date when the item was created. |
|
767 | + * |
|
768 | + * @since 1.0.19 |
|
769 | + * @param string $value Value to set. |
|
770 | 770 | * @return bool Whether or not the date was set. |
771 | - */ |
|
772 | - public function set_date_created( $value ) { |
|
771 | + */ |
|
772 | + public function set_date_created( $value ) { |
|
773 | 773 | $date = strtotime( $value ); |
774 | 774 | |
775 | 775 | if ( $date ) { |
@@ -782,13 +782,13 @@ discard block |
||
782 | 782 | } |
783 | 783 | |
784 | 784 | /** |
785 | - * Set date when the item was last modified. |
|
786 | - * |
|
787 | - * @since 1.0.19 |
|
788 | - * @param string $value Value to set. |
|
785 | + * Set date when the item was last modified. |
|
786 | + * |
|
787 | + * @since 1.0.19 |
|
788 | + * @param string $value Value to set. |
|
789 | 789 | * @return bool Whether or not the date was set. |
790 | - */ |
|
791 | - public function set_date_modified( $value ) { |
|
790 | + */ |
|
791 | + public function set_date_modified( $value ) { |
|
792 | 792 | $date = strtotime( $value ); |
793 | 793 | |
794 | 794 | if ( $date ) { |
@@ -801,115 +801,115 @@ discard block |
||
801 | 801 | } |
802 | 802 | |
803 | 803 | /** |
804 | - * Set the item name. |
|
805 | - * |
|
806 | - * @since 1.0.19 |
|
807 | - * @param string $value New name. |
|
808 | - */ |
|
809 | - public function set_name( $value ) { |
|
804 | + * Set the item name. |
|
805 | + * |
|
806 | + * @since 1.0.19 |
|
807 | + * @param string $value New name. |
|
808 | + */ |
|
809 | + public function set_name( $value ) { |
|
810 | 810 | $name = sanitize_text_field( $value ); |
811 | - $this->set_prop( 'name', $name ); |
|
811 | + $this->set_prop( 'name', $name ); |
|
812 | 812 | } |
813 | 813 | |
814 | 814 | /** |
815 | - * Alias of self::set_name(). |
|
816 | - * |
|
817 | - * @since 1.0.19 |
|
818 | - * @param string $value New name. |
|
819 | - */ |
|
820 | - public function set_title( $value ) { |
|
821 | - $this->set_name( $value ); |
|
815 | + * Alias of self::set_name(). |
|
816 | + * |
|
817 | + * @since 1.0.19 |
|
818 | + * @param string $value New name. |
|
819 | + */ |
|
820 | + public function set_title( $value ) { |
|
821 | + $this->set_name( $value ); |
|
822 | 822 | } |
823 | 823 | |
824 | 824 | /** |
825 | - * Set the item description. |
|
826 | - * |
|
827 | - * @since 1.0.19 |
|
828 | - * @param string $value New description. |
|
829 | - */ |
|
830 | - public function set_description( $value ) { |
|
831 | - $description = wp_kses_post( $value ); |
|
832 | - return $this->set_prop( 'description', $description ); |
|
825 | + * Set the item description. |
|
826 | + * |
|
827 | + * @since 1.0.19 |
|
828 | + * @param string $value New description. |
|
829 | + */ |
|
830 | + public function set_description( $value ) { |
|
831 | + $description = wp_kses_post( $value ); |
|
832 | + return $this->set_prop( 'description', $description ); |
|
833 | 833 | } |
834 | 834 | |
835 | 835 | /** |
836 | - * Alias of self::set_description(). |
|
837 | - * |
|
838 | - * @since 1.0.19 |
|
839 | - * @param string $value New description. |
|
840 | - */ |
|
841 | - public function set_excerpt( $value ) { |
|
842 | - $this->set_description( $value ); |
|
836 | + * Alias of self::set_description(). |
|
837 | + * |
|
838 | + * @since 1.0.19 |
|
839 | + * @param string $value New description. |
|
840 | + */ |
|
841 | + public function set_excerpt( $value ) { |
|
842 | + $this->set_description( $value ); |
|
843 | 843 | } |
844 | 844 | |
845 | 845 | /** |
846 | - * Alias of self::set_description(). |
|
847 | - * |
|
848 | - * @since 1.0.19 |
|
849 | - * @param string $value New description. |
|
850 | - */ |
|
851 | - public function set_summary( $value ) { |
|
852 | - $this->set_description( $value ); |
|
846 | + * Alias of self::set_description(). |
|
847 | + * |
|
848 | + * @since 1.0.19 |
|
849 | + * @param string $value New description. |
|
850 | + */ |
|
851 | + public function set_summary( $value ) { |
|
852 | + $this->set_description( $value ); |
|
853 | 853 | } |
854 | 854 | |
855 | 855 | /** |
856 | - * Set the owner of the item. |
|
857 | - * |
|
858 | - * @since 1.0.19 |
|
859 | - * @param int $value New author. |
|
860 | - */ |
|
861 | - public function set_author( $value ) { |
|
862 | - $this->set_prop( 'author', (int) $value ); |
|
863 | - } |
|
856 | + * Set the owner of the item. |
|
857 | + * |
|
858 | + * @since 1.0.19 |
|
859 | + * @param int $value New author. |
|
860 | + */ |
|
861 | + public function set_author( $value ) { |
|
862 | + $this->set_prop( 'author', (int) $value ); |
|
863 | + } |
|
864 | 864 | |
865 | - /** |
|
866 | - * Alias of self::set_author(). |
|
867 | - * |
|
868 | - * @since 1.0.19 |
|
869 | - * @param int $value New author. |
|
870 | - */ |
|
871 | - public function set_owner( $value ) { |
|
872 | - $this->set_author( $value ); |
|
873 | - } |
|
874 | - |
|
875 | - /** |
|
876 | - * Set the price of the item. |
|
877 | - * |
|
878 | - * @since 1.0.19 |
|
879 | - * @param float $value New price. |
|
880 | - */ |
|
881 | - public function set_price( $value ) { |
|
865 | + /** |
|
866 | + * Alias of self::set_author(). |
|
867 | + * |
|
868 | + * @since 1.0.19 |
|
869 | + * @param int $value New author. |
|
870 | + */ |
|
871 | + public function set_owner( $value ) { |
|
872 | + $this->set_author( $value ); |
|
873 | + } |
|
874 | + |
|
875 | + /** |
|
876 | + * Set the price of the item. |
|
877 | + * |
|
878 | + * @since 1.0.19 |
|
879 | + * @param float $value New price. |
|
880 | + */ |
|
881 | + public function set_price( $value ) { |
|
882 | 882 | $this->set_prop( 'price', (float) wpinv_sanitize_amount( $value ) ); |
883 | 883 | } |
884 | 884 | |
885 | 885 | /** |
886 | - * Set the VAT rule of the item. |
|
887 | - * |
|
888 | - * @since 1.0.19 |
|
889 | - * @param string $value new rule. |
|
890 | - */ |
|
891 | - public function set_vat_rule( $value ) { |
|
886 | + * Set the VAT rule of the item. |
|
887 | + * |
|
888 | + * @since 1.0.19 |
|
889 | + * @param string $value new rule. |
|
890 | + */ |
|
891 | + public function set_vat_rule( $value ) { |
|
892 | 892 | $this->set_prop( 'vat_rule', $value ); |
893 | 893 | } |
894 | 894 | |
895 | 895 | /** |
896 | - * Set the VAT class of the item. |
|
897 | - * |
|
898 | - * @since 1.0.19 |
|
899 | - * @param string $value new class. |
|
900 | - */ |
|
901 | - public function set_vat_class( $value ) { |
|
896 | + * Set the VAT class of the item. |
|
897 | + * |
|
898 | + * @since 1.0.19 |
|
899 | + * @param string $value new class. |
|
900 | + */ |
|
901 | + public function set_vat_class( $value ) { |
|
902 | 902 | $this->set_prop( 'vat_class', $value ); |
903 | 903 | } |
904 | 904 | |
905 | 905 | /** |
906 | - * Set the type of the item. |
|
907 | - * |
|
908 | - * @since 1.0.19 |
|
909 | - * @param string $value new item type. |
|
910 | - * @return string |
|
911 | - */ |
|
912 | - public function set_type( $value ) { |
|
906 | + * Set the type of the item. |
|
907 | + * |
|
908 | + * @since 1.0.19 |
|
909 | + * @param string $value new item type. |
|
910 | + * @return string |
|
911 | + */ |
|
912 | + public function set_type( $value ) { |
|
913 | 913 | |
914 | 914 | if ( empty( $value ) ) { |
915 | 915 | $value = 'custom'; |
@@ -919,132 +919,132 @@ discard block |
||
919 | 919 | } |
920 | 920 | |
921 | 921 | /** |
922 | - * Set the custom id of the item. |
|
923 | - * |
|
924 | - * @since 1.0.19 |
|
925 | - * @param string $value new custom id. |
|
926 | - */ |
|
927 | - public function set_custom_id( $value ) { |
|
922 | + * Set the custom id of the item. |
|
923 | + * |
|
924 | + * @since 1.0.19 |
|
925 | + * @param string $value new custom id. |
|
926 | + */ |
|
927 | + public function set_custom_id( $value ) { |
|
928 | 928 | $this->set_prop( 'custom_id', $value ); |
929 | 929 | } |
930 | 930 | |
931 | 931 | /** |
932 | - * Set the custom name of the item. |
|
933 | - * |
|
934 | - * @since 1.0.19 |
|
935 | - * @param string $value new custom name. |
|
936 | - */ |
|
937 | - public function set_custom_name( $value ) { |
|
932 | + * Set the custom name of the item. |
|
933 | + * |
|
934 | + * @since 1.0.19 |
|
935 | + * @param string $value new custom name. |
|
936 | + */ |
|
937 | + public function set_custom_name( $value ) { |
|
938 | 938 | $this->set_prop( 'custom_name', $value ); |
939 | 939 | } |
940 | 940 | |
941 | 941 | /** |
942 | - * Set the custom singular name of the item. |
|
943 | - * |
|
944 | - * @since 1.0.19 |
|
945 | - * @param string $value new custom singular name. |
|
946 | - */ |
|
947 | - public function set_custom_singular_name( $value ) { |
|
942 | + * Set the custom singular name of the item. |
|
943 | + * |
|
944 | + * @since 1.0.19 |
|
945 | + * @param string $value new custom singular name. |
|
946 | + */ |
|
947 | + public function set_custom_singular_name( $value ) { |
|
948 | 948 | $this->set_prop( 'custom_singular_name', $value ); |
949 | 949 | } |
950 | 950 | |
951 | 951 | /** |
952 | - * Sets if an item is editable.. |
|
953 | - * |
|
954 | - * @since 1.0.19 |
|
955 | - * @param int|bool $value whether or not the item is editable. |
|
956 | - */ |
|
957 | - public function set_is_editable( $value ) { |
|
958 | - $this->set_prop( 'is_editable', (int) $value ); |
|
952 | + * Sets if an item is editable.. |
|
953 | + * |
|
954 | + * @since 1.0.19 |
|
955 | + * @param int|bool $value whether or not the item is editable. |
|
956 | + */ |
|
957 | + public function set_is_editable( $value ) { |
|
958 | + $this->set_prop( 'is_editable', (int) $value ); |
|
959 | 959 | } |
960 | 960 | |
961 | 961 | /** |
962 | - * Sets if dynamic pricing is enabled. |
|
963 | - * |
|
964 | - * @since 1.0.19 |
|
965 | - * @param int|bool $value whether or not dynamic pricing is allowed. |
|
966 | - */ |
|
967 | - public function set_is_dynamic_pricing( $value ) { |
|
962 | + * Sets if dynamic pricing is enabled. |
|
963 | + * |
|
964 | + * @since 1.0.19 |
|
965 | + * @param int|bool $value whether or not dynamic pricing is allowed. |
|
966 | + */ |
|
967 | + public function set_is_dynamic_pricing( $value ) { |
|
968 | 968 | $this->set_prop( 'is_dynamic_pricing', (int) $value ); |
969 | 969 | } |
970 | 970 | |
971 | 971 | /** |
972 | - * Sets the minimum price if dynamic pricing is enabled. |
|
973 | - * |
|
974 | - * @since 1.0.19 |
|
975 | - * @param float $value minimum price. |
|
976 | - */ |
|
977 | - public function set_minimum_price( $value ) { |
|
972 | + * Sets the minimum price if dynamic pricing is enabled. |
|
973 | + * |
|
974 | + * @since 1.0.19 |
|
975 | + * @param float $value minimum price. |
|
976 | + */ |
|
977 | + public function set_minimum_price( $value ) { |
|
978 | 978 | $this->set_prop( 'minimum_price', (float) wpinv_sanitize_amount( $value ) ); |
979 | 979 | } |
980 | 980 | |
981 | 981 | /** |
982 | - * Sets if this is a recurring item. |
|
983 | - * |
|
984 | - * @since 1.0.19 |
|
985 | - * @param int|bool $value whether or not dynamic pricing is allowed. |
|
986 | - */ |
|
987 | - public function set_is_recurring( $value ) { |
|
982 | + * Sets if this is a recurring item. |
|
983 | + * |
|
984 | + * @since 1.0.19 |
|
985 | + * @param int|bool $value whether or not dynamic pricing is allowed. |
|
986 | + */ |
|
987 | + public function set_is_recurring( $value ) { |
|
988 | 988 | $this->set_prop( 'is_recurring', (int) $value ); |
989 | 989 | } |
990 | 990 | |
991 | 991 | /** |
992 | - * Set the recurring period. |
|
993 | - * |
|
994 | - * @since 1.0.19 |
|
995 | - * @param string $value new period. |
|
996 | - */ |
|
997 | - public function set_recurring_period( $value ) { |
|
992 | + * Set the recurring period. |
|
993 | + * |
|
994 | + * @since 1.0.19 |
|
995 | + * @param string $value new period. |
|
996 | + */ |
|
997 | + public function set_recurring_period( $value ) { |
|
998 | 998 | $this->set_prop( 'recurring_period', $value ); |
999 | 999 | } |
1000 | 1000 | |
1001 | 1001 | /** |
1002 | - * Set the recurring interval. |
|
1003 | - * |
|
1004 | - * @since 1.0.19 |
|
1005 | - * @param int $value recurring interval. |
|
1006 | - */ |
|
1007 | - public function set_recurring_interval( $value ) { |
|
1002 | + * Set the recurring interval. |
|
1003 | + * |
|
1004 | + * @since 1.0.19 |
|
1005 | + * @param int $value recurring interval. |
|
1006 | + */ |
|
1007 | + public function set_recurring_interval( $value ) { |
|
1008 | 1008 | return $this->set_prop( 'recurring_interval', (int) $value ); |
1009 | 1009 | } |
1010 | 1010 | |
1011 | 1011 | /** |
1012 | - * Get the recurring limit. |
|
1013 | - * @since 1.0.19 |
|
1014 | - * @param int $value The recurring limit. |
|
1015 | - * @return int |
|
1016 | - */ |
|
1017 | - public function set_recurring_limit( $value ) { |
|
1012 | + * Get the recurring limit. |
|
1013 | + * @since 1.0.19 |
|
1014 | + * @param int $value The recurring limit. |
|
1015 | + * @return int |
|
1016 | + */ |
|
1017 | + public function set_recurring_limit( $value ) { |
|
1018 | 1018 | $this->set_prop( 'recurring_limit', (int) $value ); |
1019 | 1019 | } |
1020 | 1020 | |
1021 | 1021 | /** |
1022 | - * Checks if we have a free trial. |
|
1023 | - * |
|
1024 | - * @since 1.0.19 |
|
1025 | - * @param int|bool $value whether or not it has a free trial. |
|
1026 | - */ |
|
1027 | - public function set_is_free_trial( $value ) { |
|
1022 | + * Checks if we have a free trial. |
|
1023 | + * |
|
1024 | + * @since 1.0.19 |
|
1025 | + * @param int|bool $value whether or not it has a free trial. |
|
1026 | + */ |
|
1027 | + public function set_is_free_trial( $value ) { |
|
1028 | 1028 | $this->set_prop( 'is_free_trial', (int) $value ); |
1029 | 1029 | } |
1030 | 1030 | |
1031 | 1031 | /** |
1032 | - * Set the trial period. |
|
1033 | - * |
|
1034 | - * @since 1.0.19 |
|
1035 | - * @param string $value trial period. |
|
1036 | - */ |
|
1037 | - public function set_trial_period( $value ) { |
|
1032 | + * Set the trial period. |
|
1033 | + * |
|
1034 | + * @since 1.0.19 |
|
1035 | + * @param string $value trial period. |
|
1036 | + */ |
|
1037 | + public function set_trial_period( $value ) { |
|
1038 | 1038 | $this->set_prop( 'trial_period', $value ); |
1039 | 1039 | } |
1040 | 1040 | |
1041 | 1041 | /** |
1042 | - * Set the trial interval. |
|
1043 | - * |
|
1044 | - * @since 1.0.19 |
|
1045 | - * @param int $value trial interval. |
|
1046 | - */ |
|
1047 | - public function set_trial_interval( $value ) { |
|
1042 | + * Set the trial interval. |
|
1043 | + * |
|
1044 | + * @since 1.0.19 |
|
1045 | + * @param int $value trial interval. |
|
1046 | + */ |
|
1047 | + public function set_trial_interval( $value ) { |
|
1048 | 1048 | $this->set_prop( 'trial_interval', $value ); |
1049 | 1049 | } |
1050 | 1050 | |
@@ -1052,17 +1052,17 @@ discard block |
||
1052 | 1052 | * Create an item. For backwards compatibilty. |
1053 | 1053 | * |
1054 | 1054 | * @deprecated |
1055 | - * @return int item id |
|
1055 | + * @return int item id |
|
1056 | 1056 | */ |
1057 | 1057 | public function create( $data = array() ) { |
1058 | 1058 | |
1059 | - // Set the properties. |
|
1060 | - if ( is_array( $data ) ) { |
|
1061 | - $this->set_props( $data ); |
|
1062 | - } |
|
1059 | + // Set the properties. |
|
1060 | + if ( is_array( $data ) ) { |
|
1061 | + $this->set_props( $data ); |
|
1062 | + } |
|
1063 | 1063 | |
1064 | - // Save the item. |
|
1065 | - return $this->save(); |
|
1064 | + // Save the item. |
|
1065 | + return $this->save(); |
|
1066 | 1066 | |
1067 | 1067 | } |
1068 | 1068 | |
@@ -1070,7 +1070,7 @@ discard block |
||
1070 | 1070 | * Updates an item. For backwards compatibilty. |
1071 | 1071 | * |
1072 | 1072 | * @deprecated |
1073 | - * @return int item id |
|
1073 | + * @return int item id |
|
1074 | 1074 | */ |
1075 | 1075 | public function update( $data = array() ) { |
1076 | 1076 | return $this->create( $data ); |
@@ -1086,93 +1086,93 @@ discard block |
||
1086 | 1086 | */ |
1087 | 1087 | |
1088 | 1088 | /** |
1089 | - * Checks whether the item has enabled dynamic pricing. |
|
1090 | - * |
|
1091 | - * @since 1.0.19 |
|
1092 | - * @return bool |
|
1093 | - */ |
|
1094 | - public function user_can_set_their_price() { |
|
1089 | + * Checks whether the item has enabled dynamic pricing. |
|
1090 | + * |
|
1091 | + * @since 1.0.19 |
|
1092 | + * @return bool |
|
1093 | + */ |
|
1094 | + public function user_can_set_their_price() { |
|
1095 | 1095 | return (bool) $this->get_is_dynamic_pricing(); |
1096 | - } |
|
1096 | + } |
|
1097 | 1097 | |
1098 | - /** |
|
1099 | - * Checks whether the item is recurring. |
|
1100 | - * |
|
1101 | - * @since 1.0.19 |
|
1102 | - * @return bool |
|
1103 | - */ |
|
1104 | - public function is_recurring() { |
|
1098 | + /** |
|
1099 | + * Checks whether the item is recurring. |
|
1100 | + * |
|
1101 | + * @since 1.0.19 |
|
1102 | + * @return bool |
|
1103 | + */ |
|
1104 | + public function is_recurring() { |
|
1105 | 1105 | return (bool) $this->get_is_recurring(); |
1106 | 1106 | } |
1107 | 1107 | |
1108 | 1108 | /** |
1109 | - * Checks whether the item has a free trial. |
|
1110 | - * |
|
1111 | - * @since 1.0.19 |
|
1112 | - * @return bool |
|
1113 | - */ |
|
1109 | + * Checks whether the item has a free trial. |
|
1110 | + * |
|
1111 | + * @since 1.0.19 |
|
1112 | + * @return bool |
|
1113 | + */ |
|
1114 | 1114 | public function has_free_trial() { |
1115 | 1115 | $has_trial = $this->is_recurring() && (bool) $this->get_free_trial() ? true : false; |
1116 | 1116 | return (bool) apply_filters( 'wpinv_item_has_free_trial', $has_trial, $this->ID, $this ); |
1117 | 1117 | } |
1118 | 1118 | |
1119 | 1119 | /** |
1120 | - * Checks whether the item is free. |
|
1121 | - * |
|
1122 | - * @since 1.0.19 |
|
1123 | - * @return bool |
|
1124 | - */ |
|
1120 | + * Checks whether the item is free. |
|
1121 | + * |
|
1122 | + * @since 1.0.19 |
|
1123 | + * @return bool |
|
1124 | + */ |
|
1125 | 1125 | public function is_free() { |
1126 | 1126 | $is_free = $this->get_price() == 0; |
1127 | 1127 | return (bool) apply_filters( 'wpinv_is_free_item', $is_free, $this->ID, $this ); |
1128 | 1128 | } |
1129 | 1129 | |
1130 | 1130 | /** |
1131 | - * Checks the item status against a passed in status. |
|
1132 | - * |
|
1133 | - * @param array|string $status Status to check. |
|
1134 | - * @return bool |
|
1135 | - */ |
|
1136 | - public function has_status( $status ) { |
|
1137 | - $has_status = ( is_array( $status ) && in_array( $this->get_status(), $status, true ) ) || $this->get_status() === $status; |
|
1138 | - return (bool) apply_filters( 'getpaid_item_has_status', $has_status, $this, $status ); |
|
1131 | + * Checks the item status against a passed in status. |
|
1132 | + * |
|
1133 | + * @param array|string $status Status to check. |
|
1134 | + * @return bool |
|
1135 | + */ |
|
1136 | + public function has_status( $status ) { |
|
1137 | + $has_status = ( is_array( $status ) && in_array( $this->get_status(), $status, true ) ) || $this->get_status() === $status; |
|
1138 | + return (bool) apply_filters( 'getpaid_item_has_status', $has_status, $this, $status ); |
|
1139 | 1139 | } |
1140 | 1140 | |
1141 | 1141 | /** |
1142 | - * Checks the item type against a passed in types. |
|
1143 | - * |
|
1144 | - * @param array|string $type Type to check. |
|
1145 | - * @return bool |
|
1146 | - */ |
|
1147 | - public function is_type( $type ) { |
|
1148 | - $is_type = ( is_array( $type ) && in_array( $this->get_type(), $type, true ) ) || $this->get_type() === $type; |
|
1149 | - return (bool) apply_filters( 'getpaid_item_is_type', $is_type, $this, $type ); |
|
1150 | - } |
|
1142 | + * Checks the item type against a passed in types. |
|
1143 | + * |
|
1144 | + * @param array|string $type Type to check. |
|
1145 | + * @return bool |
|
1146 | + */ |
|
1147 | + public function is_type( $type ) { |
|
1148 | + $is_type = ( is_array( $type ) && in_array( $this->get_type(), $type, true ) ) || $this->get_type() === $type; |
|
1149 | + return (bool) apply_filters( 'getpaid_item_is_type', $is_type, $this, $type ); |
|
1150 | + } |
|
1151 | 1151 | |
1152 | 1152 | /** |
1153 | - * Checks whether the item is editable. |
|
1154 | - * |
|
1155 | - * @since 1.0.19 |
|
1156 | - * @return bool |
|
1157 | - */ |
|
1153 | + * Checks whether the item is editable. |
|
1154 | + * |
|
1155 | + * @since 1.0.19 |
|
1156 | + * @return bool |
|
1157 | + */ |
|
1158 | 1158 | public function is_editable() { |
1159 | 1159 | $is_editable = $this->get_is_editable(); |
1160 | 1160 | return (bool) apply_filters( 'wpinv_item_is_editable', $is_editable, $this->ID, $this ); |
1161 | - } |
|
1161 | + } |
|
1162 | 1162 | |
1163 | - /** |
|
1164 | - * Returns an array of cart fees. |
|
1165 | - */ |
|
1166 | - public function get_fees() { |
|
1163 | + /** |
|
1164 | + * Returns an array of cart fees. |
|
1165 | + */ |
|
1166 | + public function get_fees() { |
|
1167 | 1167 | return array(); |
1168 | 1168 | } |
1169 | 1169 | |
1170 | 1170 | /** |
1171 | - * Checks whether the item is purchasable. |
|
1172 | - * |
|
1173 | - * @since 1.0.19 |
|
1174 | - * @return bool |
|
1175 | - */ |
|
1171 | + * Checks whether the item is purchasable. |
|
1172 | + * |
|
1173 | + * @since 1.0.19 |
|
1174 | + * @return bool |
|
1175 | + */ |
|
1176 | 1176 | public function can_purchase() { |
1177 | 1177 | $can_purchase = $this->exists(); |
1178 | 1178 | |
@@ -1184,11 +1184,11 @@ discard block |
||
1184 | 1184 | } |
1185 | 1185 | |
1186 | 1186 | /** |
1187 | - * Checks whether the item supports dynamic pricing. |
|
1188 | - * |
|
1189 | - * @since 1.0.19 |
|
1190 | - * @return bool |
|
1191 | - */ |
|
1187 | + * Checks whether the item supports dynamic pricing. |
|
1188 | + * |
|
1189 | + * @since 1.0.19 |
|
1190 | + * @return bool |
|
1191 | + */ |
|
1192 | 1192 | public function supports_dynamic_pricing() { |
1193 | 1193 | return (bool) apply_filters( 'wpinv_item_supports_dynamic_pricing', true, $this ); |
1194 | 1194 | } |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * |
5 | 5 | */ |
6 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
7 | - exit; |
|
7 | + exit; |
|
8 | 8 | } |
9 | 9 | |
10 | 10 | /** |
@@ -14,228 +14,228 @@ discard block |
||
14 | 14 | */ |
15 | 15 | class GetPaid_Item_Data_Store extends GetPaid_Data_Store_WP { |
16 | 16 | |
17 | - /** |
|
18 | - * Data stored in meta keys, but not considered "meta" for an item. |
|
19 | - * |
|
20 | - * @since 1.0.19 |
|
21 | - * @var array |
|
22 | - */ |
|
23 | - protected $internal_meta_keys = array( |
|
24 | - '_wpinv_price', |
|
25 | - '_wpinv_vat_rule', |
|
26 | - '_wpinv_vat_class', |
|
27 | - '_wpinv_type', |
|
28 | - '_wpinv_custom_id', |
|
29 | - '_wpinv_custom_name', |
|
30 | - '_wpinv_custom_singular_name', |
|
31 | - '_wpinv_editable', |
|
32 | - '_wpinv_dynamic_pricing', |
|
33 | - '_minimum_price', |
|
34 | - '_wpinv_is_recurring', |
|
35 | - '_wpinv_recurring_period', |
|
36 | - '_wpinv_recurring_interval', |
|
37 | - '_wpinv_recurring_limit', |
|
38 | - '_wpinv_free_trial', |
|
39 | - '_wpinv_trial_period', |
|
40 | - '_wpinv_trial_interval' |
|
41 | - ); |
|
42 | - |
|
43 | - /** |
|
44 | - * A map of meta keys to data props. |
|
45 | - * |
|
46 | - * @since 1.0.19 |
|
47 | - * |
|
48 | - * @var array |
|
49 | - */ |
|
50 | - protected $meta_key_to_props = array( |
|
51 | - '_wpinv_price' => 'price', |
|
52 | - '_wpinv_vat_rule' => 'vat_rule', |
|
53 | - '_wpinv_vat_class' => 'vat_class', |
|
54 | - '_wpinv_type' => 'type', |
|
55 | - '_wpinv_custom_id' => 'custom_id', |
|
56 | - '_wpinv_custom_name' => 'custom_name', |
|
57 | - '_wpinv_custom_singular_name' => 'custom_singular_name', |
|
58 | - '_wpinv_editable' => 'is_editable', |
|
59 | - '_wpinv_dynamic_pricing' => 'is_dynamic_pricing', |
|
60 | - '_minimum_price' => 'minimum_price', |
|
61 | - '_wpinv_custom_name' => 'custom_name', |
|
62 | - '_wpinv_is_recurring' => 'is_recurring', |
|
63 | - '_wpinv_recurring_period' => 'recurring_period', |
|
64 | - '_wpinv_recurring_interval' => 'recurring_interval', |
|
65 | - '_wpinv_recurring_limit' => 'recurring_limit', |
|
66 | - '_wpinv_free_trial' => 'is_free_trial', |
|
67 | - '_wpinv_trial_period' => 'trial_period', |
|
68 | - '_wpinv_trial_interval' => 'trial_interval', |
|
69 | - '_wpinv_version' => 'version', |
|
70 | - ); |
|
71 | - |
|
72 | - /* |
|
17 | + /** |
|
18 | + * Data stored in meta keys, but not considered "meta" for an item. |
|
19 | + * |
|
20 | + * @since 1.0.19 |
|
21 | + * @var array |
|
22 | + */ |
|
23 | + protected $internal_meta_keys = array( |
|
24 | + '_wpinv_price', |
|
25 | + '_wpinv_vat_rule', |
|
26 | + '_wpinv_vat_class', |
|
27 | + '_wpinv_type', |
|
28 | + '_wpinv_custom_id', |
|
29 | + '_wpinv_custom_name', |
|
30 | + '_wpinv_custom_singular_name', |
|
31 | + '_wpinv_editable', |
|
32 | + '_wpinv_dynamic_pricing', |
|
33 | + '_minimum_price', |
|
34 | + '_wpinv_is_recurring', |
|
35 | + '_wpinv_recurring_period', |
|
36 | + '_wpinv_recurring_interval', |
|
37 | + '_wpinv_recurring_limit', |
|
38 | + '_wpinv_free_trial', |
|
39 | + '_wpinv_trial_period', |
|
40 | + '_wpinv_trial_interval' |
|
41 | + ); |
|
42 | + |
|
43 | + /** |
|
44 | + * A map of meta keys to data props. |
|
45 | + * |
|
46 | + * @since 1.0.19 |
|
47 | + * |
|
48 | + * @var array |
|
49 | + */ |
|
50 | + protected $meta_key_to_props = array( |
|
51 | + '_wpinv_price' => 'price', |
|
52 | + '_wpinv_vat_rule' => 'vat_rule', |
|
53 | + '_wpinv_vat_class' => 'vat_class', |
|
54 | + '_wpinv_type' => 'type', |
|
55 | + '_wpinv_custom_id' => 'custom_id', |
|
56 | + '_wpinv_custom_name' => 'custom_name', |
|
57 | + '_wpinv_custom_singular_name' => 'custom_singular_name', |
|
58 | + '_wpinv_editable' => 'is_editable', |
|
59 | + '_wpinv_dynamic_pricing' => 'is_dynamic_pricing', |
|
60 | + '_minimum_price' => 'minimum_price', |
|
61 | + '_wpinv_custom_name' => 'custom_name', |
|
62 | + '_wpinv_is_recurring' => 'is_recurring', |
|
63 | + '_wpinv_recurring_period' => 'recurring_period', |
|
64 | + '_wpinv_recurring_interval' => 'recurring_interval', |
|
65 | + '_wpinv_recurring_limit' => 'recurring_limit', |
|
66 | + '_wpinv_free_trial' => 'is_free_trial', |
|
67 | + '_wpinv_trial_period' => 'trial_period', |
|
68 | + '_wpinv_trial_interval' => 'trial_interval', |
|
69 | + '_wpinv_version' => 'version', |
|
70 | + ); |
|
71 | + |
|
72 | + /* |
|
73 | 73 | |-------------------------------------------------------------------------- |
74 | 74 | | CRUD Methods |
75 | 75 | |-------------------------------------------------------------------------- |
76 | 76 | */ |
77 | 77 | |
78 | - /** |
|
79 | - * Method to create a new item in the database. |
|
80 | - * |
|
81 | - * @param WPInv_Item $item Item object. |
|
82 | - */ |
|
83 | - public function create( &$item ) { |
|
84 | - $item->set_version( WPINV_VERSION ); |
|
85 | - $item->set_date_created( current_time('mysql') ); |
|
86 | - |
|
87 | - // Create a new post. |
|
88 | - $id = wp_insert_post( |
|
89 | - apply_filters( |
|
90 | - 'getpaid_new_item_data', |
|
91 | - array( |
|
92 | - 'post_date' => $item->get_date_created( 'edit' ), |
|
93 | - 'post_type' => 'wpi_item', |
|
94 | - 'post_status' => $this->get_post_status( $item ), |
|
95 | - 'ping_status' => 'closed', |
|
96 | - 'post_author' => $item->get_author( 'edit' ), |
|
97 | - 'post_title' => $item->get_name( 'edit' ), |
|
98 | - 'post_parent' => 0, |
|
99 | - 'post_excerpt' => $item->get_description( 'edit' ), |
|
100 | - ) |
|
101 | - ), |
|
102 | - true |
|
103 | - ); |
|
104 | - |
|
105 | - if ( $id && ! is_wp_error( $id ) ) { |
|
106 | - $item->set_id( $id ); |
|
107 | - $this->update_post_meta( $item ); |
|
108 | - $item->save_meta_data(); |
|
109 | - $item->apply_changes(); |
|
110 | - $this->clear_caches( $item ); |
|
111 | - do_action( 'getpaid_new_item', $item ); |
|
112 | - return true; |
|
113 | - } |
|
114 | - |
|
115 | - if ( is_wp_error( $id ) ) { |
|
116 | - $item->last_error = $id->get_error_message(); |
|
117 | - } |
|
78 | + /** |
|
79 | + * Method to create a new item in the database. |
|
80 | + * |
|
81 | + * @param WPInv_Item $item Item object. |
|
82 | + */ |
|
83 | + public function create( &$item ) { |
|
84 | + $item->set_version( WPINV_VERSION ); |
|
85 | + $item->set_date_created( current_time('mysql') ); |
|
86 | + |
|
87 | + // Create a new post. |
|
88 | + $id = wp_insert_post( |
|
89 | + apply_filters( |
|
90 | + 'getpaid_new_item_data', |
|
91 | + array( |
|
92 | + 'post_date' => $item->get_date_created( 'edit' ), |
|
93 | + 'post_type' => 'wpi_item', |
|
94 | + 'post_status' => $this->get_post_status( $item ), |
|
95 | + 'ping_status' => 'closed', |
|
96 | + 'post_author' => $item->get_author( 'edit' ), |
|
97 | + 'post_title' => $item->get_name( 'edit' ), |
|
98 | + 'post_parent' => 0, |
|
99 | + 'post_excerpt' => $item->get_description( 'edit' ), |
|
100 | + ) |
|
101 | + ), |
|
102 | + true |
|
103 | + ); |
|
104 | + |
|
105 | + if ( $id && ! is_wp_error( $id ) ) { |
|
106 | + $item->set_id( $id ); |
|
107 | + $this->update_post_meta( $item ); |
|
108 | + $item->save_meta_data(); |
|
109 | + $item->apply_changes(); |
|
110 | + $this->clear_caches( $item ); |
|
111 | + do_action( 'getpaid_new_item', $item ); |
|
112 | + return true; |
|
113 | + } |
|
114 | + |
|
115 | + if ( is_wp_error( $id ) ) { |
|
116 | + $item->last_error = $id->get_error_message(); |
|
117 | + } |
|
118 | 118 | |
119 | - return false; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Method to read an item from the database. |
|
124 | - * |
|
125 | - * @param WPInv_Item $item Item object. |
|
126 | - * |
|
127 | - */ |
|
128 | - public function read( &$item ) { |
|
129 | - |
|
130 | - $item->set_defaults(); |
|
131 | - $item_object = get_post( $item->get_id() ); |
|
132 | - |
|
133 | - if ( ! $item->get_id() || ! $item_object || $item_object->post_type != 'wpi_item' ) { |
|
134 | - $item->last_error = __( 'Invalid item.', 'invoicing' ); |
|
135 | - $item->set_id( 0 ); |
|
136 | - return false; |
|
137 | - } |
|
138 | - |
|
139 | - $item->set_props( |
|
140 | - array( |
|
141 | - 'parent_id' => $item_object->post_parent, |
|
142 | - 'date_created' => 0 < $item_object->post_date ? $item_object->post_date : null, |
|
143 | - 'date_modified' => 0 < $item_object->post_modified ? $item_object->post_modified : null, |
|
144 | - 'status' => $item_object->post_status, |
|
145 | - 'name' => $item_object->post_title, |
|
146 | - 'description' => $item_object->post_excerpt, |
|
147 | - 'author' => $item_object->post_author, |
|
148 | - ) |
|
149 | - ); |
|
150 | - |
|
151 | - $this->read_object_data( $item, $item_object ); |
|
152 | - $item->read_meta_data(); |
|
153 | - $item->set_object_read( true ); |
|
154 | - do_action( 'getpaid_read_item', $item ); |
|
155 | - |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Method to update an item in the database. |
|
160 | - * |
|
161 | - * @param WPInv_Item $item Item object. |
|
162 | - */ |
|
163 | - public function update( &$item ) { |
|
164 | - $item->save_meta_data(); |
|
165 | - $item->set_version( WPINV_VERSION ); |
|
166 | - |
|
167 | - if ( null === $item->get_date_created( 'edit' ) ) { |
|
168 | - $item->set_date_created( current_time('mysql') ); |
|
169 | - } |
|
170 | - |
|
171 | - // Grab the current status so we can compare. |
|
172 | - $previous_status = get_post_status( $item->get_id() ); |
|
173 | - |
|
174 | - $changes = $item->get_changes(); |
|
175 | - |
|
176 | - // Only update the post when the post data changes. |
|
177 | - if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'description', 'name', 'author' ), array_keys( $changes ) ) ) { |
|
178 | - $post_data = array( |
|
179 | - 'post_date' => $item->get_date_created( 'edit' ), |
|
180 | - 'post_status' => $item->get_status( 'edit' ), |
|
181 | - 'post_parent' => $item->get_parent_id( 'edit' ), |
|
182 | - 'post_excerpt' => $item->get_description( 'edit' ), |
|
183 | - 'post_modified' => $item->get_date_modified( 'edit' ), |
|
184 | - 'post_title' => $item->get_name( 'edit' ), |
|
185 | - 'post_author' => $item->get_author( 'edit' ), |
|
186 | - ); |
|
187 | - |
|
188 | - /** |
|
189 | - * When updating this object, to prevent infinite loops, use $wpdb |
|
190 | - * to update data, since wp_update_post spawns more calls to the |
|
191 | - * save_post action. |
|
192 | - * |
|
193 | - * This ensures hooks are fired by either WP itself (admin screen save), |
|
194 | - * or an update purely from CRUD. |
|
195 | - */ |
|
196 | - if ( doing_action( 'save_post' ) ) { |
|
197 | - $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $item->get_id() ) ); |
|
198 | - clean_post_cache( $item->get_id() ); |
|
199 | - } else { |
|
200 | - wp_update_post( array_merge( array( 'ID' => $item->get_id() ), $post_data ) ); |
|
201 | - } |
|
202 | - $item->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
203 | - } |
|
204 | - $this->update_post_meta( $item ); |
|
205 | - $item->apply_changes(); |
|
206 | - $this->clear_caches( $item ); |
|
207 | - |
|
208 | - // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
209 | - $new_status = $item->get_status( 'edit' ); |
|
210 | - |
|
211 | - if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
212 | - do_action( 'getpaid_new_item', $item ); |
|
213 | - } else { |
|
214 | - do_action( 'getpaid_update_item', $item ); |
|
215 | - } |
|
216 | - |
|
217 | - } |
|
218 | - |
|
219 | - /* |
|
119 | + return false; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Method to read an item from the database. |
|
124 | + * |
|
125 | + * @param WPInv_Item $item Item object. |
|
126 | + * |
|
127 | + */ |
|
128 | + public function read( &$item ) { |
|
129 | + |
|
130 | + $item->set_defaults(); |
|
131 | + $item_object = get_post( $item->get_id() ); |
|
132 | + |
|
133 | + if ( ! $item->get_id() || ! $item_object || $item_object->post_type != 'wpi_item' ) { |
|
134 | + $item->last_error = __( 'Invalid item.', 'invoicing' ); |
|
135 | + $item->set_id( 0 ); |
|
136 | + return false; |
|
137 | + } |
|
138 | + |
|
139 | + $item->set_props( |
|
140 | + array( |
|
141 | + 'parent_id' => $item_object->post_parent, |
|
142 | + 'date_created' => 0 < $item_object->post_date ? $item_object->post_date : null, |
|
143 | + 'date_modified' => 0 < $item_object->post_modified ? $item_object->post_modified : null, |
|
144 | + 'status' => $item_object->post_status, |
|
145 | + 'name' => $item_object->post_title, |
|
146 | + 'description' => $item_object->post_excerpt, |
|
147 | + 'author' => $item_object->post_author, |
|
148 | + ) |
|
149 | + ); |
|
150 | + |
|
151 | + $this->read_object_data( $item, $item_object ); |
|
152 | + $item->read_meta_data(); |
|
153 | + $item->set_object_read( true ); |
|
154 | + do_action( 'getpaid_read_item', $item ); |
|
155 | + |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Method to update an item in the database. |
|
160 | + * |
|
161 | + * @param WPInv_Item $item Item object. |
|
162 | + */ |
|
163 | + public function update( &$item ) { |
|
164 | + $item->save_meta_data(); |
|
165 | + $item->set_version( WPINV_VERSION ); |
|
166 | + |
|
167 | + if ( null === $item->get_date_created( 'edit' ) ) { |
|
168 | + $item->set_date_created( current_time('mysql') ); |
|
169 | + } |
|
170 | + |
|
171 | + // Grab the current status so we can compare. |
|
172 | + $previous_status = get_post_status( $item->get_id() ); |
|
173 | + |
|
174 | + $changes = $item->get_changes(); |
|
175 | + |
|
176 | + // Only update the post when the post data changes. |
|
177 | + if ( array_intersect( array( 'date_created', 'date_modified', 'status', 'parent_id', 'description', 'name', 'author' ), array_keys( $changes ) ) ) { |
|
178 | + $post_data = array( |
|
179 | + 'post_date' => $item->get_date_created( 'edit' ), |
|
180 | + 'post_status' => $item->get_status( 'edit' ), |
|
181 | + 'post_parent' => $item->get_parent_id( 'edit' ), |
|
182 | + 'post_excerpt' => $item->get_description( 'edit' ), |
|
183 | + 'post_modified' => $item->get_date_modified( 'edit' ), |
|
184 | + 'post_title' => $item->get_name( 'edit' ), |
|
185 | + 'post_author' => $item->get_author( 'edit' ), |
|
186 | + ); |
|
187 | + |
|
188 | + /** |
|
189 | + * When updating this object, to prevent infinite loops, use $wpdb |
|
190 | + * to update data, since wp_update_post spawns more calls to the |
|
191 | + * save_post action. |
|
192 | + * |
|
193 | + * This ensures hooks are fired by either WP itself (admin screen save), |
|
194 | + * or an update purely from CRUD. |
|
195 | + */ |
|
196 | + if ( doing_action( 'save_post' ) ) { |
|
197 | + $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $item->get_id() ) ); |
|
198 | + clean_post_cache( $item->get_id() ); |
|
199 | + } else { |
|
200 | + wp_update_post( array_merge( array( 'ID' => $item->get_id() ), $post_data ) ); |
|
201 | + } |
|
202 | + $item->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. |
|
203 | + } |
|
204 | + $this->update_post_meta( $item ); |
|
205 | + $item->apply_changes(); |
|
206 | + $this->clear_caches( $item ); |
|
207 | + |
|
208 | + // Fire a hook depending on the status - this should be considered a creation if it was previously draft status. |
|
209 | + $new_status = $item->get_status( 'edit' ); |
|
210 | + |
|
211 | + if ( $new_status !== $previous_status && in_array( $previous_status, array( 'new', 'auto-draft', 'draft' ), true ) ) { |
|
212 | + do_action( 'getpaid_new_item', $item ); |
|
213 | + } else { |
|
214 | + do_action( 'getpaid_update_item', $item ); |
|
215 | + } |
|
216 | + |
|
217 | + } |
|
218 | + |
|
219 | + /* |
|
220 | 220 | |-------------------------------------------------------------------------- |
221 | 221 | | Additional Methods |
222 | 222 | |-------------------------------------------------------------------------- |
223 | 223 | */ |
224 | 224 | |
225 | - /** |
|
226 | - * Helper method that updates all the post meta for an item based on it's settings in the WPInv_Item class. |
|
227 | - * |
|
228 | - * @param WPInv_Item $item WPInv_Item object. |
|
229 | - * @since 1.0.19 |
|
230 | - */ |
|
231 | - protected function update_post_meta( &$item ) { |
|
225 | + /** |
|
226 | + * Helper method that updates all the post meta for an item based on it's settings in the WPInv_Item class. |
|
227 | + * |
|
228 | + * @param WPInv_Item $item WPInv_Item object. |
|
229 | + * @since 1.0.19 |
|
230 | + */ |
|
231 | + protected function update_post_meta( &$item ) { |
|
232 | 232 | |
233 | - // Ensure that we have a custom id. |
|
233 | + // Ensure that we have a custom id. |
|
234 | 234 | if ( ! $item->get_custom_id() ) { |
235 | 235 | $item->set_custom_id( $item->get_id() ); |
236 | - } |
|
236 | + } |
|
237 | 237 | |
238 | - parent::update_post_meta( $item ); |
|
239 | - } |
|
238 | + parent::update_post_meta( $item ); |
|
239 | + } |
|
240 | 240 | |
241 | 241 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | /** |
@@ -10,55 +10,55 @@ discard block |
||
10 | 10 | class GetPaid_Payment_Form extends GetPaid_Data { |
11 | 11 | |
12 | 12 | /** |
13 | - * Which data store to load. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
13 | + * Which data store to load. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | 17 | protected $data_store_name = 'payment_form'; |
18 | 18 | |
19 | 19 | /** |
20 | - * This is the name of this object type. |
|
21 | - * |
|
22 | - * @var string |
|
23 | - */ |
|
24 | - protected $object_type = 'payment_form'; |
|
20 | + * This is the name of this object type. |
|
21 | + * |
|
22 | + * @var string |
|
23 | + */ |
|
24 | + protected $object_type = 'payment_form'; |
|
25 | 25 | |
26 | 26 | /** |
27 | - * Form Data array. This is the core form data exposed in APIs. |
|
28 | - * |
|
29 | - * @since 1.0.19 |
|
30 | - * @var array |
|
31 | - */ |
|
32 | - protected $data = array( |
|
33 | - 'status' => 'draft', |
|
34 | - 'version' => '', |
|
35 | - 'date_created' => null, |
|
27 | + * Form Data array. This is the core form data exposed in APIs. |
|
28 | + * |
|
29 | + * @since 1.0.19 |
|
30 | + * @var array |
|
31 | + */ |
|
32 | + protected $data = array( |
|
33 | + 'status' => 'draft', |
|
34 | + 'version' => '', |
|
35 | + 'date_created' => null, |
|
36 | 36 | 'date_modified' => null, |
37 | 37 | 'name' => '', |
38 | 38 | 'author' => 1, |
39 | 39 | 'elements' => null, |
40 | - 'items' => null, |
|
41 | - 'earned' => 0, |
|
42 | - 'refunded' => 0, |
|
43 | - 'cancelled' => 0, |
|
44 | - 'failed' => 0, |
|
45 | - ); |
|
46 | - |
|
47 | - /** |
|
48 | - * Stores meta in cache for future reads. |
|
49 | - * |
|
50 | - * A group must be set to to enable caching. |
|
51 | - * |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - protected $cache_group = 'getpaid_forms'; |
|
55 | - |
|
56 | - /** |
|
57 | - * Stores a reference to the invoice if the form is for an invoice.. |
|
58 | - * |
|
59 | - * @var WPInv_Invoice |
|
60 | - */ |
|
61 | - public $invoice = 0; |
|
40 | + 'items' => null, |
|
41 | + 'earned' => 0, |
|
42 | + 'refunded' => 0, |
|
43 | + 'cancelled' => 0, |
|
44 | + 'failed' => 0, |
|
45 | + ); |
|
46 | + |
|
47 | + /** |
|
48 | + * Stores meta in cache for future reads. |
|
49 | + * |
|
50 | + * A group must be set to to enable caching. |
|
51 | + * |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + protected $cache_group = 'getpaid_forms'; |
|
55 | + |
|
56 | + /** |
|
57 | + * Stores a reference to the invoice if the form is for an invoice.. |
|
58 | + * |
|
59 | + * @var WPInv_Invoice |
|
60 | + */ |
|
61 | + public $invoice = 0; |
|
62 | 62 | |
63 | 63 | /** |
64 | 64 | * Stores a reference to the original WP_Post object |
@@ -68,35 +68,35 @@ discard block |
||
68 | 68 | protected $post = null; |
69 | 69 | |
70 | 70 | /** |
71 | - * Get the form if ID is passed, otherwise the form is new and empty. |
|
72 | - * |
|
73 | - * @param int|object|GetPaid_Payment_Form|WP_Post $form Form to read. |
|
74 | - */ |
|
75 | - public function __construct( $form = 0 ) { |
|
76 | - parent::__construct( $form ); |
|
71 | + * Get the form if ID is passed, otherwise the form is new and empty. |
|
72 | + * |
|
73 | + * @param int|object|GetPaid_Payment_Form|WP_Post $form Form to read. |
|
74 | + */ |
|
75 | + public function __construct( $form = 0 ) { |
|
76 | + parent::__construct( $form ); |
|
77 | 77 | |
78 | - if ( is_numeric( $form ) && $form > 0 ) { |
|
79 | - $this->set_id( $form ); |
|
80 | - } elseif ( $form instanceof self ) { |
|
78 | + if ( is_numeric( $form ) && $form > 0 ) { |
|
79 | + $this->set_id( $form ); |
|
80 | + } elseif ( $form instanceof self ) { |
|
81 | 81 | |
82 | - $this->set_id( $form->get_id() ); |
|
83 | - $this->invoice = $form->invoice; |
|
82 | + $this->set_id( $form->get_id() ); |
|
83 | + $this->invoice = $form->invoice; |
|
84 | 84 | |
85 | - } elseif ( ! empty( $form->ID ) ) { |
|
86 | - $this->set_id( $form->ID ); |
|
87 | - } else { |
|
88 | - $this->set_object_read( true ); |
|
89 | - } |
|
85 | + } elseif ( ! empty( $form->ID ) ) { |
|
86 | + $this->set_id( $form->ID ); |
|
87 | + } else { |
|
88 | + $this->set_object_read( true ); |
|
89 | + } |
|
90 | 90 | |
91 | 91 | // Load the datastore. |
92 | - $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
92 | + $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
93 | 93 | |
94 | - if ( $this->get_id() > 0 ) { |
|
94 | + if ( $this->get_id() > 0 ) { |
|
95 | 95 | $this->post = get_post( $this->get_id() ); |
96 | - $this->data_store->read( $this ); |
|
96 | + $this->data_store->read( $this ); |
|
97 | 97 | } |
98 | 98 | |
99 | - } |
|
99 | + } |
|
100 | 100 | |
101 | 101 | /* |
102 | 102 | |-------------------------------------------------------------------------- |
@@ -114,358 +114,358 @@ discard block |
||
114 | 114 | */ |
115 | 115 | |
116 | 116 | /** |
117 | - * Get plugin version when the form was created. |
|
118 | - * |
|
119 | - * @since 1.0.19 |
|
120 | - * @param string $context View or edit context. |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - public function get_version( $context = 'view' ) { |
|
124 | - return $this->get_prop( 'version', $context ); |
|
117 | + * Get plugin version when the form was created. |
|
118 | + * |
|
119 | + * @since 1.0.19 |
|
120 | + * @param string $context View or edit context. |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + public function get_version( $context = 'view' ) { |
|
124 | + return $this->get_prop( 'version', $context ); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | /** |
128 | - * Get date when the form was created. |
|
129 | - * |
|
130 | - * @since 1.0.19 |
|
131 | - * @param string $context View or edit context. |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - public function get_date_created( $context = 'view' ) { |
|
135 | - return $this->get_prop( 'date_created', $context ); |
|
128 | + * Get date when the form was created. |
|
129 | + * |
|
130 | + * @since 1.0.19 |
|
131 | + * @param string $context View or edit context. |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + public function get_date_created( $context = 'view' ) { |
|
135 | + return $this->get_prop( 'date_created', $context ); |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
139 | - * Get GMT date when the form was created. |
|
140 | - * |
|
141 | - * @since 1.0.19 |
|
142 | - * @param string $context View or edit context. |
|
143 | - * @return string |
|
144 | - */ |
|
145 | - public function get_date_created_gmt( $context = 'view' ) { |
|
139 | + * Get GMT date when the form was created. |
|
140 | + * |
|
141 | + * @since 1.0.19 |
|
142 | + * @param string $context View or edit context. |
|
143 | + * @return string |
|
144 | + */ |
|
145 | + public function get_date_created_gmt( $context = 'view' ) { |
|
146 | 146 | $date = $this->get_date_created( $context ); |
147 | 147 | |
148 | 148 | if ( $date ) { |
149 | 149 | $date = get_gmt_from_date( $date ); |
150 | 150 | } |
151 | - return $date; |
|
151 | + return $date; |
|
152 | 152 | } |
153 | 153 | |
154 | 154 | /** |
155 | - * Get date when the form was last modified. |
|
156 | - * |
|
157 | - * @since 1.0.19 |
|
158 | - * @param string $context View or edit context. |
|
159 | - * @return string |
|
160 | - */ |
|
161 | - public function get_date_modified( $context = 'view' ) { |
|
162 | - return $this->get_prop( 'date_modified', $context ); |
|
155 | + * Get date when the form was last modified. |
|
156 | + * |
|
157 | + * @since 1.0.19 |
|
158 | + * @param string $context View or edit context. |
|
159 | + * @return string |
|
160 | + */ |
|
161 | + public function get_date_modified( $context = 'view' ) { |
|
162 | + return $this->get_prop( 'date_modified', $context ); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | /** |
166 | - * Get GMT date when the form was last modified. |
|
167 | - * |
|
168 | - * @since 1.0.19 |
|
169 | - * @param string $context View or edit context. |
|
170 | - * @return string |
|
171 | - */ |
|
172 | - public function get_date_modified_gmt( $context = 'view' ) { |
|
166 | + * Get GMT date when the form was last modified. |
|
167 | + * |
|
168 | + * @since 1.0.19 |
|
169 | + * @param string $context View or edit context. |
|
170 | + * @return string |
|
171 | + */ |
|
172 | + public function get_date_modified_gmt( $context = 'view' ) { |
|
173 | 173 | $date = $this->get_date_modified( $context ); |
174 | 174 | |
175 | 175 | if ( $date ) { |
176 | 176 | $date = get_gmt_from_date( $date ); |
177 | 177 | } |
178 | - return $date; |
|
178 | + return $date; |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | /** |
182 | - * Get the form name. |
|
183 | - * |
|
184 | - * @since 1.0.19 |
|
185 | - * @param string $context View or edit context. |
|
186 | - * @return string |
|
187 | - */ |
|
188 | - public function get_name( $context = 'view' ) { |
|
189 | - return $this->get_prop( 'name', $context ); |
|
182 | + * Get the form name. |
|
183 | + * |
|
184 | + * @since 1.0.19 |
|
185 | + * @param string $context View or edit context. |
|
186 | + * @return string |
|
187 | + */ |
|
188 | + public function get_name( $context = 'view' ) { |
|
189 | + return $this->get_prop( 'name', $context ); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | /** |
193 | - * Alias of self::get_name(). |
|
194 | - * |
|
195 | - * @since 1.0.19 |
|
196 | - * @param string $context View or edit context. |
|
197 | - * @return string |
|
198 | - */ |
|
199 | - public function get_title( $context = 'view' ) { |
|
200 | - return $this->get_name( $context ); |
|
201 | - } |
|
193 | + * Alias of self::get_name(). |
|
194 | + * |
|
195 | + * @since 1.0.19 |
|
196 | + * @param string $context View or edit context. |
|
197 | + * @return string |
|
198 | + */ |
|
199 | + public function get_title( $context = 'view' ) { |
|
200 | + return $this->get_name( $context ); |
|
201 | + } |
|
202 | 202 | |
203 | 203 | /** |
204 | - * Get the owner of the form. |
|
205 | - * |
|
206 | - * @since 1.0.19 |
|
207 | - * @param string $context View or edit context. |
|
208 | - * @return int |
|
209 | - */ |
|
210 | - public function get_author( $context = 'view' ) { |
|
211 | - return (int) $this->get_prop( 'author', $context ); |
|
204 | + * Get the owner of the form. |
|
205 | + * |
|
206 | + * @since 1.0.19 |
|
207 | + * @param string $context View or edit context. |
|
208 | + * @return int |
|
209 | + */ |
|
210 | + public function get_author( $context = 'view' ) { |
|
211 | + return (int) $this->get_prop( 'author', $context ); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | /** |
215 | - * Get the elements that make up the form. |
|
216 | - * |
|
217 | - * @since 1.0.19 |
|
218 | - * @param string $context View or edit context. |
|
219 | - * @return array |
|
220 | - */ |
|
221 | - public function get_elements( $context = 'view' ) { |
|
222 | - $elements = $this->get_prop( 'elements', $context ); |
|
215 | + * Get the elements that make up the form. |
|
216 | + * |
|
217 | + * @since 1.0.19 |
|
218 | + * @param string $context View or edit context. |
|
219 | + * @return array |
|
220 | + */ |
|
221 | + public function get_elements( $context = 'view' ) { |
|
222 | + $elements = $this->get_prop( 'elements', $context ); |
|
223 | 223 | |
224 | - if ( empty( $elements ) || ! is_array( $elements ) ) { |
|
224 | + if ( empty( $elements ) || ! is_array( $elements ) ) { |
|
225 | 225 | return wpinv_get_data( 'sample-payment-form' ); |
226 | - } |
|
226 | + } |
|
227 | 227 | |
228 | - // Ensure that all required elements exist. |
|
229 | - $_elements = array(); |
|
230 | - foreach ( $elements as $element ) { |
|
228 | + // Ensure that all required elements exist. |
|
229 | + $_elements = array(); |
|
230 | + foreach ( $elements as $element ) { |
|
231 | 231 | |
232 | - if ( $element['type'] == 'pay_button' && ! $this->has_element_type( 'gateway_select' ) ) { |
|
232 | + if ( $element['type'] == 'pay_button' && ! $this->has_element_type( 'gateway_select' ) ) { |
|
233 | 233 | |
234 | - $_elements[] = array( |
|
235 | - 'text' => __( 'Select Payment Method', 'invoicing' ), |
|
236 | - 'id' => 'gtscicd', |
|
237 | - 'name' => 'gtscicd', |
|
238 | - 'type' => 'gateway_select', |
|
239 | - 'premade' => true |
|
234 | + $_elements[] = array( |
|
235 | + 'text' => __( 'Select Payment Method', 'invoicing' ), |
|
236 | + 'id' => 'gtscicd', |
|
237 | + 'name' => 'gtscicd', |
|
238 | + 'type' => 'gateway_select', |
|
239 | + 'premade' => true |
|
240 | 240 | |
241 | - ); |
|
241 | + ); |
|
242 | 242 | |
243 | - } |
|
243 | + } |
|
244 | 244 | |
245 | - $_elements[] = $element; |
|
245 | + $_elements[] = $element; |
|
246 | 246 | |
247 | - } |
|
247 | + } |
|
248 | 248 | |
249 | 249 | return $_elements; |
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Get the items sold via the form. |
|
254 | - * |
|
255 | - * @since 1.0.19 |
|
256 | - * @param string $context View or edit context. |
|
257 | - * @param string $return objects or arrays. |
|
258 | - * @return GetPaid_Form_Item[] |
|
259 | - */ |
|
260 | - public function get_items( $context = 'view', $return = 'objects' ) { |
|
261 | - $items = $this->get_prop( 'items', $context ); |
|
262 | - |
|
263 | - if ( empty( $items ) || ! is_array( $items ) ) { |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Get the items sold via the form. |
|
254 | + * |
|
255 | + * @since 1.0.19 |
|
256 | + * @param string $context View or edit context. |
|
257 | + * @param string $return objects or arrays. |
|
258 | + * @return GetPaid_Form_Item[] |
|
259 | + */ |
|
260 | + public function get_items( $context = 'view', $return = 'objects' ) { |
|
261 | + $items = $this->get_prop( 'items', $context ); |
|
262 | + |
|
263 | + if ( empty( $items ) || ! is_array( $items ) ) { |
|
264 | 264 | $items = wpinv_get_data( 'sample-payment-form-items' ); |
265 | - } |
|
265 | + } |
|
266 | + |
|
267 | + // Convert the items. |
|
268 | + $prepared = array(); |
|
266 | 269 | |
267 | - // Convert the items. |
|
268 | - $prepared = array(); |
|
270 | + foreach ( $items as $key => $value ) { |
|
269 | 271 | |
270 | - foreach ( $items as $key => $value ) { |
|
272 | + // Form items. |
|
273 | + if ( $value instanceof GetPaid_Form_Item ) { |
|
271 | 274 | |
272 | - // Form items. |
|
273 | - if ( $value instanceof GetPaid_Form_Item ) { |
|
275 | + if ( $value->can_purchase() ) { |
|
276 | + $prepared[] = $value; |
|
277 | + } |
|
274 | 278 | |
275 | - if ( $value->can_purchase() ) { |
|
276 | - $prepared[] = $value; |
|
277 | - } |
|
279 | + continue; |
|
278 | 280 | |
279 | - continue; |
|
281 | + } |
|
280 | 282 | |
281 | - } |
|
283 | + // $item_id => $quantity (buy buttons) |
|
284 | + if ( is_numeric( $key ) && is_numeric( $value ) ) { |
|
285 | + $item = new GetPaid_Form_Item( $key ); |
|
282 | 286 | |
283 | - // $item_id => $quantity (buy buttons) |
|
284 | - if ( is_numeric( $key ) && is_numeric( $value ) ) { |
|
285 | - $item = new GetPaid_Form_Item( $key ); |
|
287 | + if ( $item->can_purchase() ) { |
|
286 | 288 | |
287 | - if ( $item->can_purchase() ) { |
|
289 | + $value = (float) $value; |
|
290 | + $item->set_quantity( $value ); |
|
291 | + if ( 0 == $value ) { |
|
292 | + $item->set_quantity( 1 ); |
|
293 | + $item->set_allow_quantities( true ); |
|
294 | + } |
|
288 | 295 | |
289 | - $value = (float) $value; |
|
290 | - $item->set_quantity( $value ); |
|
291 | - if ( 0 == $value ) { |
|
292 | - $item->set_quantity( 1 ); |
|
293 | - $item->set_allow_quantities( true ); |
|
294 | - } |
|
296 | + $prepared[] = $item; |
|
297 | + } |
|
295 | 298 | |
296 | - $prepared[] = $item; |
|
297 | - } |
|
299 | + continue; |
|
300 | + } |
|
298 | 301 | |
299 | - continue; |
|
300 | - } |
|
302 | + // Items saved via payment forms editor. |
|
303 | + if ( is_array( $value ) && isset( $value['id'] ) ) { |
|
301 | 304 | |
302 | - // Items saved via payment forms editor. |
|
303 | - if ( is_array( $value ) && isset( $value['id'] ) ) { |
|
305 | + $item = new GetPaid_Form_Item( $value['id'] ); |
|
304 | 306 | |
305 | - $item = new GetPaid_Form_Item( $value['id'] ); |
|
307 | + if ( ! $item->can_purchase() ) { |
|
308 | + continue; |
|
309 | + } |
|
306 | 310 | |
307 | - if ( ! $item->can_purchase() ) { |
|
308 | - continue; |
|
309 | - } |
|
311 | + // Sub-total (Cart items). |
|
312 | + if ( isset( $value['subtotal'] ) ) { |
|
313 | + $item->set_price( $value['subtotal'] ); |
|
314 | + } |
|
310 | 315 | |
311 | - // Sub-total (Cart items). |
|
312 | - if ( isset( $value['subtotal'] ) ) { |
|
313 | - $item->set_price( $value['subtotal'] ); |
|
314 | - } |
|
316 | + if ( isset( $value['quantity'] ) ) { |
|
317 | + $item->set_quantity( $value['quantity'] ); |
|
318 | + } |
|
315 | 319 | |
316 | - if ( isset( $value['quantity'] ) ) { |
|
317 | - $item->set_quantity( $value['quantity'] ); |
|
318 | - } |
|
320 | + if ( isset( $value['allow_quantities'] ) ) { |
|
321 | + $item->set_allow_quantities( $value['allow_quantities'] ); |
|
322 | + } |
|
319 | 323 | |
320 | - if ( isset( $value['allow_quantities'] ) ) { |
|
321 | - $item->set_allow_quantities( $value['allow_quantities'] ); |
|
322 | - } |
|
324 | + if ( isset( $value['required'] ) ) { |
|
325 | + $item->set_is_required( $value['required'] ); |
|
326 | + } |
|
323 | 327 | |
324 | - if ( isset( $value['required'] ) ) { |
|
325 | - $item->set_is_required( $value['required'] ); |
|
326 | - } |
|
328 | + if ( isset( $value['description'] ) ) { |
|
329 | + $item->set_custom_description( $value['description'] ); |
|
330 | + } |
|
327 | 331 | |
328 | - if ( isset( $value['description'] ) ) { |
|
329 | - $item->set_custom_description( $value['description'] ); |
|
330 | - } |
|
332 | + $prepared[] = $item; |
|
333 | + continue; |
|
331 | 334 | |
332 | - $prepared[] = $item; |
|
333 | - continue; |
|
335 | + } |
|
336 | + |
|
337 | + // $item_id => array( 'price' => 10 ) (item variations) |
|
338 | + if ( is_numeric( $key ) && is_array( $value ) ) { |
|
339 | + $item = new GetPaid_Form_Item( $key ); |
|
340 | + |
|
341 | + if ( isset( $value['price'] ) && $item->user_can_set_their_price() ) { |
|
342 | + $item->set_price( $value['price'] ); |
|
343 | + } |
|
344 | + |
|
345 | + if ( $item->can_purchase() ) { |
|
346 | + $prepared[] = $item; |
|
347 | + } |
|
348 | + |
|
349 | + continue; |
|
350 | + } |
|
351 | + |
|
352 | + } |
|
353 | + |
|
354 | + if ( 'objects' == $return && 'view' == $context ) { |
|
355 | + return $prepared; |
|
356 | + } |
|
357 | + |
|
358 | + $items = array(); |
|
359 | + foreach ( $prepared as $item ) { |
|
360 | + $items[] = $item->prepare_data_for_use(); |
|
361 | + } |
|
362 | + |
|
363 | + return $items; |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * Get a single item belonging to the form. |
|
368 | + * |
|
369 | + * @since 1.0.19 |
|
370 | + * @param int $item_id The item id to return. |
|
371 | + * @return GetPaid_Form_Item|bool |
|
372 | + */ |
|
373 | + public function get_item( $item_id ) { |
|
374 | + |
|
375 | + if ( empty( $item_id ) || ! is_numeric( $item_id ) ) { |
|
376 | + return false; |
|
377 | + } |
|
378 | + |
|
379 | + foreach( $this->get_items() as $item ) { |
|
380 | + if ( $item->get_id() == (int) $item_id ) { |
|
381 | + return $item; |
|
382 | + } |
|
383 | + } |
|
384 | + |
|
385 | + return false; |
|
386 | + |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Gets a single element. |
|
391 | + * |
|
392 | + * @since 1.0.19 |
|
393 | + * @param string $element_type The element type to return. |
|
394 | + * @return array|bool |
|
395 | + */ |
|
396 | + public function get_element_type( $element_type ) { |
|
397 | + |
|
398 | + if ( empty( $element_type ) || ! is_scalar( $element_type ) ) { |
|
399 | + return false; |
|
400 | + } |
|
401 | + |
|
402 | + foreach ( $this->get_prop( 'elements' ) as $element ) { |
|
403 | + |
|
404 | + if ( $element['type'] == $element_type ) { |
|
405 | + return $element; |
|
406 | + } |
|
407 | + |
|
408 | + } |
|
409 | + |
|
410 | + return false; |
|
411 | + |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * Get the total amount earned via this form. |
|
416 | + * |
|
417 | + * @since 1.0.19 |
|
418 | + * @param string $context View or edit context. |
|
419 | + * @return float |
|
420 | + */ |
|
421 | + public function get_earned( $context = 'view' ) { |
|
422 | + return $this->get_prop( 'earned', $context ); |
|
423 | + } |
|
424 | + |
|
425 | + /** |
|
426 | + * Get the total amount refunded via this form. |
|
427 | + * |
|
428 | + * @since 1.0.19 |
|
429 | + * @param string $context View or edit context. |
|
430 | + * @return float |
|
431 | + */ |
|
432 | + public function get_refunded( $context = 'view' ) { |
|
433 | + return $this->get_prop( 'refunded', $context ); |
|
434 | + } |
|
334 | 435 | |
335 | - } |
|
436 | + /** |
|
437 | + * Get the total amount cancelled via this form. |
|
438 | + * |
|
439 | + * @since 1.0.19 |
|
440 | + * @param string $context View or edit context. |
|
441 | + * @return float |
|
442 | + */ |
|
443 | + public function get_cancelled( $context = 'view' ) { |
|
444 | + return $this->get_prop( 'cancelled', $context ); |
|
445 | + } |
|
336 | 446 | |
337 | - // $item_id => array( 'price' => 10 ) (item variations) |
|
338 | - if ( is_numeric( $key ) && is_array( $value ) ) { |
|
339 | - $item = new GetPaid_Form_Item( $key ); |
|
447 | + /** |
|
448 | + * Get the total amount failed via this form. |
|
449 | + * |
|
450 | + * @since 1.0.19 |
|
451 | + * @param string $context View or edit context. |
|
452 | + * @return float |
|
453 | + */ |
|
454 | + public function get_failed( $context = 'view' ) { |
|
455 | + return $this->get_prop( 'failed', $context ); |
|
456 | + } |
|
340 | 457 | |
341 | - if ( isset( $value['price'] ) && $item->user_can_set_their_price() ) { |
|
342 | - $item->set_price( $value['price'] ); |
|
343 | - } |
|
344 | - |
|
345 | - if ( $item->can_purchase() ) { |
|
346 | - $prepared[] = $item; |
|
347 | - } |
|
348 | - |
|
349 | - continue; |
|
350 | - } |
|
351 | - |
|
352 | - } |
|
353 | - |
|
354 | - if ( 'objects' == $return && 'view' == $context ) { |
|
355 | - return $prepared; |
|
356 | - } |
|
357 | - |
|
358 | - $items = array(); |
|
359 | - foreach ( $prepared as $item ) { |
|
360 | - $items[] = $item->prepare_data_for_use(); |
|
361 | - } |
|
362 | - |
|
363 | - return $items; |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * Get a single item belonging to the form. |
|
368 | - * |
|
369 | - * @since 1.0.19 |
|
370 | - * @param int $item_id The item id to return. |
|
371 | - * @return GetPaid_Form_Item|bool |
|
372 | - */ |
|
373 | - public function get_item( $item_id ) { |
|
374 | - |
|
375 | - if ( empty( $item_id ) || ! is_numeric( $item_id ) ) { |
|
376 | - return false; |
|
377 | - } |
|
378 | - |
|
379 | - foreach( $this->get_items() as $item ) { |
|
380 | - if ( $item->get_id() == (int) $item_id ) { |
|
381 | - return $item; |
|
382 | - } |
|
383 | - } |
|
384 | - |
|
385 | - return false; |
|
386 | - |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Gets a single element. |
|
391 | - * |
|
392 | - * @since 1.0.19 |
|
393 | - * @param string $element_type The element type to return. |
|
394 | - * @return array|bool |
|
395 | - */ |
|
396 | - public function get_element_type( $element_type ) { |
|
397 | - |
|
398 | - if ( empty( $element_type ) || ! is_scalar( $element_type ) ) { |
|
399 | - return false; |
|
400 | - } |
|
401 | - |
|
402 | - foreach ( $this->get_prop( 'elements' ) as $element ) { |
|
403 | - |
|
404 | - if ( $element['type'] == $element_type ) { |
|
405 | - return $element; |
|
406 | - } |
|
407 | - |
|
408 | - } |
|
409 | - |
|
410 | - return false; |
|
411 | - |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * Get the total amount earned via this form. |
|
416 | - * |
|
417 | - * @since 1.0.19 |
|
418 | - * @param string $context View or edit context. |
|
419 | - * @return float |
|
420 | - */ |
|
421 | - public function get_earned( $context = 'view' ) { |
|
422 | - return $this->get_prop( 'earned', $context ); |
|
423 | - } |
|
424 | - |
|
425 | - /** |
|
426 | - * Get the total amount refunded via this form. |
|
427 | - * |
|
428 | - * @since 1.0.19 |
|
429 | - * @param string $context View or edit context. |
|
430 | - * @return float |
|
431 | - */ |
|
432 | - public function get_refunded( $context = 'view' ) { |
|
433 | - return $this->get_prop( 'refunded', $context ); |
|
434 | - } |
|
435 | - |
|
436 | - /** |
|
437 | - * Get the total amount cancelled via this form. |
|
438 | - * |
|
439 | - * @since 1.0.19 |
|
440 | - * @param string $context View or edit context. |
|
441 | - * @return float |
|
442 | - */ |
|
443 | - public function get_cancelled( $context = 'view' ) { |
|
444 | - return $this->get_prop( 'cancelled', $context ); |
|
445 | - } |
|
446 | - |
|
447 | - /** |
|
448 | - * Get the total amount failed via this form. |
|
449 | - * |
|
450 | - * @since 1.0.19 |
|
451 | - * @param string $context View or edit context. |
|
452 | - * @return float |
|
453 | - */ |
|
454 | - public function get_failed( $context = 'view' ) { |
|
455 | - return $this->get_prop( 'failed', $context ); |
|
456 | - } |
|
457 | - |
|
458 | - /** |
|
459 | - * Get the currency. |
|
460 | - * |
|
461 | - * @since 1.0.19 |
|
462 | - * @param string $context View or edit context. |
|
463 | - * @return string |
|
464 | - */ |
|
465 | - public function get_currency() { |
|
466 | - $currency = empty( $this->invoice ) ? wpinv_get_currency() : $this->invoice->get_currency(); |
|
467 | - return apply_filters( 'getpaid-payment-form-currency', $currency, $this ); |
|
468 | - } |
|
458 | + /** |
|
459 | + * Get the currency. |
|
460 | + * |
|
461 | + * @since 1.0.19 |
|
462 | + * @param string $context View or edit context. |
|
463 | + * @return string |
|
464 | + */ |
|
465 | + public function get_currency() { |
|
466 | + $currency = empty( $this->invoice ) ? wpinv_get_currency() : $this->invoice->get_currency(); |
|
467 | + return apply_filters( 'getpaid-payment-form-currency', $currency, $this ); |
|
468 | + } |
|
469 | 469 | |
470 | 470 | /* |
471 | 471 | |-------------------------------------------------------------------------- |
@@ -478,22 +478,22 @@ discard block |
||
478 | 478 | */ |
479 | 479 | |
480 | 480 | /** |
481 | - * Set plugin version when the item was created. |
|
482 | - * |
|
483 | - * @since 1.0.19 |
|
484 | - */ |
|
485 | - public function set_version( $value ) { |
|
486 | - $this->set_prop( 'version', $value ); |
|
481 | + * Set plugin version when the item was created. |
|
482 | + * |
|
483 | + * @since 1.0.19 |
|
484 | + */ |
|
485 | + public function set_version( $value ) { |
|
486 | + $this->set_prop( 'version', $value ); |
|
487 | 487 | } |
488 | 488 | |
489 | 489 | /** |
490 | - * Set date when the item was created. |
|
491 | - * |
|
492 | - * @since 1.0.19 |
|
493 | - * @param string $value Value to set. |
|
490 | + * Set date when the item was created. |
|
491 | + * |
|
492 | + * @since 1.0.19 |
|
493 | + * @param string $value Value to set. |
|
494 | 494 | * @return bool Whether or not the date was set. |
495 | - */ |
|
496 | - public function set_date_created( $value ) { |
|
495 | + */ |
|
496 | + public function set_date_created( $value ) { |
|
497 | 497 | $date = strtotime( $value ); |
498 | 498 | |
499 | 499 | if ( $date ) { |
@@ -506,13 +506,13 @@ discard block |
||
506 | 506 | } |
507 | 507 | |
508 | 508 | /** |
509 | - * Set date when the item was last modified. |
|
510 | - * |
|
511 | - * @since 1.0.19 |
|
512 | - * @param string $value Value to set. |
|
509 | + * Set date when the item was last modified. |
|
510 | + * |
|
511 | + * @since 1.0.19 |
|
512 | + * @param string $value Value to set. |
|
513 | 513 | * @return bool Whether or not the date was set. |
514 | - */ |
|
515 | - public function set_date_modified( $value ) { |
|
514 | + */ |
|
515 | + public function set_date_modified( $value ) { |
|
516 | 516 | $date = strtotime( $value ); |
517 | 517 | |
518 | 518 | if ( $date ) { |
@@ -525,118 +525,118 @@ discard block |
||
525 | 525 | } |
526 | 526 | |
527 | 527 | /** |
528 | - * Set the item name. |
|
529 | - * |
|
530 | - * @since 1.0.19 |
|
531 | - * @param string $value New name. |
|
532 | - */ |
|
533 | - public function set_name( $value ) { |
|
534 | - $this->set_prop( 'name', sanitize_text_field( $value ) ); |
|
535 | - } |
|
536 | - |
|
537 | - /** |
|
538 | - * Alias of self::set_name(). |
|
539 | - * |
|
540 | - * @since 1.0.19 |
|
541 | - * @param string $value New name. |
|
542 | - */ |
|
543 | - public function set_title( $value ) { |
|
544 | - $this->set_name( $value ); |
|
545 | - } |
|
546 | - |
|
547 | - /** |
|
548 | - * Set the owner of the item. |
|
549 | - * |
|
550 | - * @since 1.0.19 |
|
551 | - * @param int $value New author. |
|
552 | - */ |
|
553 | - public function set_author( $value ) { |
|
554 | - $this->set_prop( 'author', (int) $value ); |
|
555 | - } |
|
556 | - |
|
557 | - /** |
|
558 | - * Set the form elements. |
|
559 | - * |
|
560 | - * @since 1.0.19 |
|
561 | - * @param array $value Form elements. |
|
562 | - */ |
|
563 | - public function set_elements( $value ) { |
|
564 | - if ( is_array( $value ) ) { |
|
565 | - $this->set_prop( 'elements', $value ); |
|
566 | - } |
|
567 | - } |
|
568 | - |
|
569 | - /** |
|
570 | - * Set the form items. |
|
571 | - * |
|
572 | - * @since 1.0.19 |
|
573 | - * @param array $value Form elements. |
|
574 | - */ |
|
575 | - public function set_items( $value ) { |
|
576 | - if ( is_array( $value ) ) { |
|
577 | - $this->set_prop( 'items', $value ); |
|
578 | - } |
|
579 | - } |
|
580 | - |
|
581 | - /** |
|
582 | - * Set the total amount earned via this form. |
|
583 | - * |
|
584 | - * @since 1.0.19 |
|
585 | - * @param float $value Amount earned. |
|
586 | - */ |
|
587 | - public function set_earned( $value ) { |
|
588 | - $value = max( (float) $value, 0 ); |
|
589 | - $this->set_prop( 'earned', $value ); |
|
590 | - } |
|
591 | - |
|
592 | - /** |
|
593 | - * Set the total amount refunded via this form. |
|
594 | - * |
|
595 | - * @since 1.0.19 |
|
596 | - * @param float $value Amount refunded. |
|
597 | - */ |
|
598 | - public function set_refunded( $value ) { |
|
599 | - $value = max( (float) $value, 0 ); |
|
600 | - $this->set_prop( 'refunded', $value ); |
|
601 | - } |
|
602 | - |
|
603 | - /** |
|
604 | - * Set the total amount cancelled via this form. |
|
605 | - * |
|
606 | - * @since 1.0.19 |
|
607 | - * @param float $value Amount cancelled. |
|
608 | - */ |
|
609 | - public function set_cancelled( $value ) { |
|
610 | - $value = max( (float) $value, 0 ); |
|
611 | - $this->set_prop( 'cancelled', $value ); |
|
612 | - } |
|
613 | - |
|
614 | - /** |
|
615 | - * Set the total amount failed via this form. |
|
616 | - * |
|
617 | - * @since 1.0.19 |
|
618 | - * @param float $value Amount cancelled. |
|
619 | - */ |
|
620 | - public function set_failed( $value ) { |
|
621 | - $value = max( (float) $value, 0 ); |
|
622 | - $this->set_prop( 'failed', $value ); |
|
623 | - } |
|
528 | + * Set the item name. |
|
529 | + * |
|
530 | + * @since 1.0.19 |
|
531 | + * @param string $value New name. |
|
532 | + */ |
|
533 | + public function set_name( $value ) { |
|
534 | + $this->set_prop( 'name', sanitize_text_field( $value ) ); |
|
535 | + } |
|
536 | + |
|
537 | + /** |
|
538 | + * Alias of self::set_name(). |
|
539 | + * |
|
540 | + * @since 1.0.19 |
|
541 | + * @param string $value New name. |
|
542 | + */ |
|
543 | + public function set_title( $value ) { |
|
544 | + $this->set_name( $value ); |
|
545 | + } |
|
546 | + |
|
547 | + /** |
|
548 | + * Set the owner of the item. |
|
549 | + * |
|
550 | + * @since 1.0.19 |
|
551 | + * @param int $value New author. |
|
552 | + */ |
|
553 | + public function set_author( $value ) { |
|
554 | + $this->set_prop( 'author', (int) $value ); |
|
555 | + } |
|
556 | + |
|
557 | + /** |
|
558 | + * Set the form elements. |
|
559 | + * |
|
560 | + * @since 1.0.19 |
|
561 | + * @param array $value Form elements. |
|
562 | + */ |
|
563 | + public function set_elements( $value ) { |
|
564 | + if ( is_array( $value ) ) { |
|
565 | + $this->set_prop( 'elements', $value ); |
|
566 | + } |
|
567 | + } |
|
568 | + |
|
569 | + /** |
|
570 | + * Set the form items. |
|
571 | + * |
|
572 | + * @since 1.0.19 |
|
573 | + * @param array $value Form elements. |
|
574 | + */ |
|
575 | + public function set_items( $value ) { |
|
576 | + if ( is_array( $value ) ) { |
|
577 | + $this->set_prop( 'items', $value ); |
|
578 | + } |
|
579 | + } |
|
580 | + |
|
581 | + /** |
|
582 | + * Set the total amount earned via this form. |
|
583 | + * |
|
584 | + * @since 1.0.19 |
|
585 | + * @param float $value Amount earned. |
|
586 | + */ |
|
587 | + public function set_earned( $value ) { |
|
588 | + $value = max( (float) $value, 0 ); |
|
589 | + $this->set_prop( 'earned', $value ); |
|
590 | + } |
|
591 | + |
|
592 | + /** |
|
593 | + * Set the total amount refunded via this form. |
|
594 | + * |
|
595 | + * @since 1.0.19 |
|
596 | + * @param float $value Amount refunded. |
|
597 | + */ |
|
598 | + public function set_refunded( $value ) { |
|
599 | + $value = max( (float) $value, 0 ); |
|
600 | + $this->set_prop( 'refunded', $value ); |
|
601 | + } |
|
602 | + |
|
603 | + /** |
|
604 | + * Set the total amount cancelled via this form. |
|
605 | + * |
|
606 | + * @since 1.0.19 |
|
607 | + * @param float $value Amount cancelled. |
|
608 | + */ |
|
609 | + public function set_cancelled( $value ) { |
|
610 | + $value = max( (float) $value, 0 ); |
|
611 | + $this->set_prop( 'cancelled', $value ); |
|
612 | + } |
|
613 | + |
|
614 | + /** |
|
615 | + * Set the total amount failed via this form. |
|
616 | + * |
|
617 | + * @since 1.0.19 |
|
618 | + * @param float $value Amount cancelled. |
|
619 | + */ |
|
620 | + public function set_failed( $value ) { |
|
621 | + $value = max( (float) $value, 0 ); |
|
622 | + $this->set_prop( 'failed', $value ); |
|
623 | + } |
|
624 | 624 | |
625 | 625 | /** |
626 | 626 | * Create an item. For backwards compatibilty. |
627 | 627 | * |
628 | 628 | * @deprecated |
629 | - * @return int item id |
|
629 | + * @return int item id |
|
630 | 630 | */ |
631 | 631 | public function create( $data = array() ) { |
632 | 632 | |
633 | - // Set the properties. |
|
634 | - if ( is_array( $data ) ) { |
|
635 | - $this->set_props( $data ); |
|
636 | - } |
|
633 | + // Set the properties. |
|
634 | + if ( is_array( $data ) ) { |
|
635 | + $this->set_props( $data ); |
|
636 | + } |
|
637 | 637 | |
638 | - // Save the item. |
|
639 | - return $this->save(); |
|
638 | + // Save the item. |
|
639 | + return $this->save(); |
|
640 | 640 | |
641 | 641 | } |
642 | 642 | |
@@ -644,7 +644,7 @@ discard block |
||
644 | 644 | * Updates an item. For backwards compatibilty. |
645 | 645 | * |
646 | 646 | * @deprecated |
647 | - * @return int item id |
|
647 | + * @return int item id |
|
648 | 648 | */ |
649 | 649 | public function update( $data = array() ) { |
650 | 650 | return $this->create( $data ); |
@@ -660,22 +660,22 @@ discard block |
||
660 | 660 | */ |
661 | 661 | |
662 | 662 | /** |
663 | - * Checks whether this is the default payment form. |
|
664 | - * |
|
665 | - * @since 1.0.19 |
|
666 | - * @return bool |
|
667 | - */ |
|
663 | + * Checks whether this is the default payment form. |
|
664 | + * |
|
665 | + * @since 1.0.19 |
|
666 | + * @return bool |
|
667 | + */ |
|
668 | 668 | public function is_default() { |
669 | 669 | $is_default = $this->get_id() == wpinv_get_default_payment_form(); |
670 | 670 | return (bool) apply_filters( 'wpinv_is_default_payment_form', $is_default, $this->get_id(), $this ); |
671 | - } |
|
671 | + } |
|
672 | 672 | |
673 | 673 | /** |
674 | - * Checks whether the form is active. |
|
675 | - * |
|
676 | - * @since 1.0.19 |
|
677 | - * @return bool |
|
678 | - */ |
|
674 | + * Checks whether the form is active. |
|
675 | + * |
|
676 | + * @since 1.0.19 |
|
677 | + * @return bool |
|
678 | + */ |
|
679 | 679 | public function is_active() { |
680 | 680 | $is_active = 0 !== (int) $this->get_id(); |
681 | 681 | |
@@ -684,76 +684,76 @@ discard block |
||
684 | 684 | } |
685 | 685 | |
686 | 686 | return (bool) apply_filters( 'wpinv_is_payment_form_active', $is_active, $this ); |
687 | - } |
|
688 | - |
|
689 | - /** |
|
690 | - * Checks whether the form has a given item. |
|
691 | - * |
|
692 | - * @since 1.0.19 |
|
693 | - * @return bool |
|
694 | - */ |
|
687 | + } |
|
688 | + |
|
689 | + /** |
|
690 | + * Checks whether the form has a given item. |
|
691 | + * |
|
692 | + * @since 1.0.19 |
|
693 | + * @return bool |
|
694 | + */ |
|
695 | 695 | public function has_item( $item_id ) { |
696 | 696 | return false !== $this->get_item( $item_id ); |
697 | - } |
|
698 | - |
|
699 | - /** |
|
700 | - * Checks whether the form has a given element. |
|
701 | - * |
|
702 | - * @since 1.0.19 |
|
703 | - * @return bool |
|
704 | - */ |
|
697 | + } |
|
698 | + |
|
699 | + /** |
|
700 | + * Checks whether the form has a given element. |
|
701 | + * |
|
702 | + * @since 1.0.19 |
|
703 | + * @return bool |
|
704 | + */ |
|
705 | 705 | public function has_element_type( $element_type ) { |
706 | 706 | return false !== $this->get_element_type( $element_type ); |
707 | - } |
|
708 | - |
|
709 | - /** |
|
710 | - * Checks whether this form is recurring or not. |
|
711 | - * |
|
712 | - * @since 1.0.19 |
|
713 | - * @return bool |
|
714 | - */ |
|
707 | + } |
|
708 | + |
|
709 | + /** |
|
710 | + * Checks whether this form is recurring or not. |
|
711 | + * |
|
712 | + * @since 1.0.19 |
|
713 | + * @return bool |
|
714 | + */ |
|
715 | 715 | public function is_recurring() { |
716 | 716 | |
717 | - if ( ! empty( $this->invoice ) ) { |
|
718 | - return $this->invoice->is_recurring(); |
|
719 | - } |
|
717 | + if ( ! empty( $this->invoice ) ) { |
|
718 | + return $this->invoice->is_recurring(); |
|
719 | + } |
|
720 | 720 | |
721 | - foreach ( $this->get_items() as $item ) { |
|
721 | + foreach ( $this->get_items() as $item ) { |
|
722 | 722 | |
723 | - if ( $item->is_recurring() ) { |
|
724 | - return true; |
|
725 | - } |
|
723 | + if ( $item->is_recurring() ) { |
|
724 | + return true; |
|
725 | + } |
|
726 | 726 | |
727 | - } |
|
727 | + } |
|
728 | 728 | |
729 | 729 | return false; |
730 | - } |
|
730 | + } |
|
731 | 731 | |
732 | - /** |
|
733 | - * Retrieves the form's html. |
|
734 | - * |
|
735 | - * @since 1.0.19 |
|
736 | - */ |
|
732 | + /** |
|
733 | + * Retrieves the form's html. |
|
734 | + * |
|
735 | + * @since 1.0.19 |
|
736 | + */ |
|
737 | 737 | public function get_html( $extra_markup = '' ) { |
738 | 738 | |
739 | - // Return the HTML. |
|
740 | - return wpinv_get_template_html( |
|
741 | - 'payment-forms/form.php', |
|
742 | - array( |
|
743 | - 'form' => $this, |
|
744 | - 'extra_markup' => $extra_markup, |
|
745 | - ) |
|
746 | - ); |
|
747 | - |
|
748 | - } |
|
749 | - |
|
750 | - /** |
|
751 | - * Displays the payment form. |
|
752 | - * |
|
753 | - * @since 1.0.19 |
|
754 | - */ |
|
739 | + // Return the HTML. |
|
740 | + return wpinv_get_template_html( |
|
741 | + 'payment-forms/form.php', |
|
742 | + array( |
|
743 | + 'form' => $this, |
|
744 | + 'extra_markup' => $extra_markup, |
|
745 | + ) |
|
746 | + ); |
|
747 | + |
|
748 | + } |
|
749 | + |
|
750 | + /** |
|
751 | + * Displays the payment form. |
|
752 | + * |
|
753 | + * @since 1.0.19 |
|
754 | + */ |
|
755 | 755 | public function display( $extra_markup = '' ) { |
756 | - echo $this->get_html( $extra_markup ); |
|
756 | + echo $this->get_html( $extra_markup ); |
|
757 | 757 | } |
758 | 758 | |
759 | 759 | } |
@@ -13,467 +13,467 @@ discard block |
||
13 | 13 | */ |
14 | 14 | abstract class GetPaid_Payment_Gateway { |
15 | 15 | |
16 | - /** |
|
17 | - * Set if the place checkout button should be renamed on selection. |
|
18 | - * |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - public $checkout_button_text; |
|
22 | - |
|
23 | - /** |
|
24 | - * Boolean whether the method is enabled. |
|
25 | - * |
|
26 | - * @var bool |
|
27 | - */ |
|
28 | - public $enabled = true; |
|
29 | - |
|
30 | - /** |
|
31 | - * Payment method id. |
|
32 | - * |
|
33 | - * @var string |
|
34 | - */ |
|
35 | - public $id; |
|
36 | - |
|
37 | - /** |
|
38 | - * Payment method order. |
|
39 | - * |
|
40 | - * @var int |
|
41 | - */ |
|
42 | - public $order = 10; |
|
43 | - |
|
44 | - /** |
|
45 | - * Payment method title for the frontend. |
|
46 | - * |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - public $title; |
|
50 | - |
|
51 | - /** |
|
52 | - * Payment method description for the frontend. |
|
53 | - * |
|
54 | - * @var string |
|
55 | - */ |
|
56 | - public $description; |
|
57 | - |
|
58 | - /** |
|
59 | - * Gateway title. |
|
60 | - * |
|
61 | - * @var string |
|
62 | - */ |
|
63 | - public $method_title = ''; |
|
64 | - |
|
65 | - /** |
|
66 | - * Gateway description. |
|
67 | - * |
|
68 | - * @var string |
|
69 | - */ |
|
70 | - public $method_description = ''; |
|
71 | - |
|
72 | - /** |
|
73 | - * Countries this gateway is allowed for. |
|
74 | - * |
|
75 | - * @var array |
|
76 | - */ |
|
77 | - public $countries; |
|
78 | - |
|
79 | - /** |
|
80 | - * Currencies this gateway is allowed for. |
|
81 | - * |
|
82 | - * @var array |
|
83 | - */ |
|
84 | - public $currencies; |
|
85 | - |
|
86 | - /** |
|
87 | - * Currencies this gateway is not allowed for. |
|
88 | - * |
|
89 | - * @var array |
|
90 | - */ |
|
91 | - public $exclude_currencies; |
|
92 | - |
|
93 | - /** |
|
94 | - * Maximum transaction amount, zero does not define a maximum. |
|
95 | - * |
|
96 | - * @var int |
|
97 | - */ |
|
98 | - public $max_amount = 0; |
|
99 | - |
|
100 | - /** |
|
101 | - * Optional URL to view a transaction. |
|
102 | - * |
|
103 | - * @var string |
|
104 | - */ |
|
105 | - public $view_transaction_url = ''; |
|
106 | - |
|
107 | - /** |
|
108 | - * Optional URL to view a subscription. |
|
109 | - * |
|
110 | - * @var string |
|
111 | - */ |
|
112 | - public $view_subscription_url = ''; |
|
113 | - |
|
114 | - /** |
|
115 | - * Optional label to show for "new payment method" in the payment |
|
116 | - * method/token selection radio selection. |
|
117 | - * |
|
118 | - * @var string |
|
119 | - */ |
|
120 | - public $new_method_label = ''; |
|
121 | - |
|
122 | - /** |
|
123 | - * Contains a user's saved tokens for this gateway. |
|
124 | - * |
|
125 | - * @var array |
|
126 | - */ |
|
127 | - protected $tokens = array(); |
|
128 | - |
|
129 | - /** |
|
130 | - * An array of features that this gateway supports. |
|
131 | - * |
|
132 | - * @var array |
|
133 | - */ |
|
134 | - protected $supports = array(); |
|
135 | - |
|
136 | - /** |
|
137 | - * Class constructor. |
|
138 | - */ |
|
139 | - public function __construct() { |
|
140 | - |
|
141 | - // Register gateway. |
|
142 | - add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
143 | - |
|
144 | - $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
145 | - |
|
146 | - // Enable Subscriptions. |
|
147 | - if ( $this->supports( 'subscription' ) ) { |
|
148 | - add_filter( "wpinv_{$this->id}_support_subscription", '__return_true' ); |
|
149 | - } |
|
150 | - |
|
151 | - // Enable sandbox. |
|
152 | - if ( $this->supports( 'sandbox' ) ) { |
|
153 | - add_filter( "wpinv_{$this->id}_supports_sandbox", '__return_true' ); |
|
154 | - } |
|
155 | - |
|
156 | - // Invoice addons. |
|
157 | - if ( $this->supports( 'addons' ) ) { |
|
158 | - add_filter( "getpaid_{$this->id}_supports_addons", '__return_true' ); |
|
159 | - add_action( "getpaid_process_{$this->id}_invoice_addons", array( $this, 'process_addons' ), 10, 2 ); |
|
160 | - } |
|
161 | - |
|
162 | - // Gateway settings. |
|
163 | - add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
16 | + /** |
|
17 | + * Set if the place checkout button should be renamed on selection. |
|
18 | + * |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + public $checkout_button_text; |
|
22 | + |
|
23 | + /** |
|
24 | + * Boolean whether the method is enabled. |
|
25 | + * |
|
26 | + * @var bool |
|
27 | + */ |
|
28 | + public $enabled = true; |
|
29 | + |
|
30 | + /** |
|
31 | + * Payment method id. |
|
32 | + * |
|
33 | + * @var string |
|
34 | + */ |
|
35 | + public $id; |
|
36 | + |
|
37 | + /** |
|
38 | + * Payment method order. |
|
39 | + * |
|
40 | + * @var int |
|
41 | + */ |
|
42 | + public $order = 10; |
|
43 | + |
|
44 | + /** |
|
45 | + * Payment method title for the frontend. |
|
46 | + * |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + public $title; |
|
50 | + |
|
51 | + /** |
|
52 | + * Payment method description for the frontend. |
|
53 | + * |
|
54 | + * @var string |
|
55 | + */ |
|
56 | + public $description; |
|
57 | + |
|
58 | + /** |
|
59 | + * Gateway title. |
|
60 | + * |
|
61 | + * @var string |
|
62 | + */ |
|
63 | + public $method_title = ''; |
|
64 | + |
|
65 | + /** |
|
66 | + * Gateway description. |
|
67 | + * |
|
68 | + * @var string |
|
69 | + */ |
|
70 | + public $method_description = ''; |
|
71 | + |
|
72 | + /** |
|
73 | + * Countries this gateway is allowed for. |
|
74 | + * |
|
75 | + * @var array |
|
76 | + */ |
|
77 | + public $countries; |
|
78 | + |
|
79 | + /** |
|
80 | + * Currencies this gateway is allowed for. |
|
81 | + * |
|
82 | + * @var array |
|
83 | + */ |
|
84 | + public $currencies; |
|
85 | + |
|
86 | + /** |
|
87 | + * Currencies this gateway is not allowed for. |
|
88 | + * |
|
89 | + * @var array |
|
90 | + */ |
|
91 | + public $exclude_currencies; |
|
92 | + |
|
93 | + /** |
|
94 | + * Maximum transaction amount, zero does not define a maximum. |
|
95 | + * |
|
96 | + * @var int |
|
97 | + */ |
|
98 | + public $max_amount = 0; |
|
99 | + |
|
100 | + /** |
|
101 | + * Optional URL to view a transaction. |
|
102 | + * |
|
103 | + * @var string |
|
104 | + */ |
|
105 | + public $view_transaction_url = ''; |
|
106 | + |
|
107 | + /** |
|
108 | + * Optional URL to view a subscription. |
|
109 | + * |
|
110 | + * @var string |
|
111 | + */ |
|
112 | + public $view_subscription_url = ''; |
|
113 | + |
|
114 | + /** |
|
115 | + * Optional label to show for "new payment method" in the payment |
|
116 | + * method/token selection radio selection. |
|
117 | + * |
|
118 | + * @var string |
|
119 | + */ |
|
120 | + public $new_method_label = ''; |
|
121 | + |
|
122 | + /** |
|
123 | + * Contains a user's saved tokens for this gateway. |
|
124 | + * |
|
125 | + * @var array |
|
126 | + */ |
|
127 | + protected $tokens = array(); |
|
128 | + |
|
129 | + /** |
|
130 | + * An array of features that this gateway supports. |
|
131 | + * |
|
132 | + * @var array |
|
133 | + */ |
|
134 | + protected $supports = array(); |
|
135 | + |
|
136 | + /** |
|
137 | + * Class constructor. |
|
138 | + */ |
|
139 | + public function __construct() { |
|
140 | + |
|
141 | + // Register gateway. |
|
142 | + add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
143 | + |
|
144 | + $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
145 | + |
|
146 | + // Enable Subscriptions. |
|
147 | + if ( $this->supports( 'subscription' ) ) { |
|
148 | + add_filter( "wpinv_{$this->id}_support_subscription", '__return_true' ); |
|
149 | + } |
|
150 | + |
|
151 | + // Enable sandbox. |
|
152 | + if ( $this->supports( 'sandbox' ) ) { |
|
153 | + add_filter( "wpinv_{$this->id}_supports_sandbox", '__return_true' ); |
|
154 | + } |
|
155 | + |
|
156 | + // Invoice addons. |
|
157 | + if ( $this->supports( 'addons' ) ) { |
|
158 | + add_filter( "getpaid_{$this->id}_supports_addons", '__return_true' ); |
|
159 | + add_action( "getpaid_process_{$this->id}_invoice_addons", array( $this, 'process_addons' ), 10, 2 ); |
|
160 | + } |
|
161 | + |
|
162 | + // Gateway settings. |
|
163 | + add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
164 | 164 | |
165 | 165 | |
166 | - // Gateway checkout fiellds. |
|
167 | - add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
168 | - |
|
169 | - // Process payment. |
|
170 | - add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
166 | + // Gateway checkout fiellds. |
|
167 | + add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
168 | + |
|
169 | + // Process payment. |
|
170 | + add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
171 | + |
|
172 | + // Change the checkout button text. |
|
173 | + if ( ! empty( $this->checkout_button_text ) ) { |
|
174 | + add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
175 | + } |
|
176 | + |
|
177 | + // Check if a gateway is valid for a given currency. |
|
178 | + add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
179 | + |
|
180 | + // Generate the transaction url. |
|
181 | + add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
182 | + |
|
183 | + // Generate the subscription url. |
|
184 | + add_filter( "getpaid_gateway_{$this->id}_subscription_url", array( $this, 'filter_subscription_url' ), 10, 2 ); |
|
185 | + |
|
186 | + // Confirm payments. |
|
187 | + add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
188 | + |
|
189 | + // Verify IPNs. |
|
190 | + add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
191 | + |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Checks if this gateway is a given gateway. |
|
196 | + * |
|
197 | + * @since 1.0.19 |
|
198 | + * @return bool |
|
199 | + */ |
|
200 | + public function is( $gateway ) { |
|
201 | + return $gateway == $this->id; |
|
202 | + } |
|
203 | + |
|
204 | + /** |
|
205 | + * Returns a users saved tokens for this gateway. |
|
206 | + * |
|
207 | + * @since 1.0.19 |
|
208 | + * @return array |
|
209 | + */ |
|
210 | + public function get_tokens( $sandbox = null ) { |
|
211 | + |
|
212 | + if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
213 | + $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
214 | + |
|
215 | + if ( is_array( $tokens ) ) { |
|
216 | + $this->tokens = $tokens; |
|
217 | + } |
|
218 | + |
|
219 | + } |
|
220 | + |
|
221 | + if ( ! is_bool( $sandbox ) ) { |
|
222 | + return $this->tokens; |
|
223 | + } |
|
224 | + |
|
225 | + $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
226 | + return wp_list_filter( $this->tokens, $args ); |
|
227 | + |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Saves a token for this gateway. |
|
232 | + * |
|
233 | + * @since 1.0.19 |
|
234 | + */ |
|
235 | + public function save_token( $token ) { |
|
236 | + |
|
237 | + $tokens = $this->get_tokens(); |
|
238 | + $tokens[] = $token; |
|
239 | + |
|
240 | + update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
241 | + |
|
242 | + $this->tokens = $tokens; |
|
243 | + |
|
244 | + } |
|
245 | + |
|
246 | + /** |
|
247 | + * Return the title for admin screens. |
|
248 | + * |
|
249 | + * @return string |
|
250 | + */ |
|
251 | + public function get_method_title() { |
|
252 | + return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * Return the description for admin screens. |
|
257 | + * |
|
258 | + * @return string |
|
259 | + */ |
|
260 | + public function get_method_description() { |
|
261 | + return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Get the success url. |
|
266 | + * |
|
267 | + * @param WPInv_Invoice $invoice Invoice object. |
|
268 | + * @return string |
|
269 | + */ |
|
270 | + public function get_return_url( $invoice ) { |
|
271 | + |
|
272 | + // Payment success url |
|
273 | + $return_url = add_query_arg( |
|
274 | + array( |
|
275 | + 'payment-confirm' => $this->id, |
|
276 | + 'invoice_key' => $invoice->get_key(), |
|
277 | + 'utm_nooverride' => 1 |
|
278 | + ), |
|
279 | + wpinv_get_success_page_uri() |
|
280 | + ); |
|
281 | + |
|
282 | + return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * Confirms payments when rendering the success page. |
|
287 | + * |
|
288 | + * @param string $content Success page content. |
|
289 | + * @return string |
|
290 | + */ |
|
291 | + public function confirm_payment( $content ) { |
|
292 | + |
|
293 | + // Retrieve the invoice. |
|
294 | + $invoice_id = getpaid_get_current_invoice_id(); |
|
295 | + $invoice = wpinv_get_invoice( $invoice_id ); |
|
296 | + |
|
297 | + // Ensure that it exists and that it is pending payment. |
|
298 | + if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
299 | + return $content; |
|
300 | + } |
|
301 | + |
|
302 | + // Can the user view this invoice?? |
|
303 | + if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
304 | + return $content; |
|
305 | + } |
|
306 | + |
|
307 | + // Show payment processing indicator. |
|
308 | + return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Processes ipns and marks payments as complete. |
|
313 | + * |
|
314 | + * @return void |
|
315 | + */ |
|
316 | + public function verify_ipn() {} |
|
317 | + |
|
318 | + /** |
|
319 | + * Processes invoice addons. |
|
320 | + * |
|
321 | + * @param WPInv_Invoice $invoice |
|
322 | + * @param GetPaid_Form_Item[] $items |
|
323 | + * @return WPInv_Invoice |
|
324 | + */ |
|
325 | + public function process_addons( $invoice, $items ) { |
|
326 | + |
|
327 | + } |
|
328 | + |
|
329 | + /** |
|
330 | + * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
331 | + * |
|
332 | + * @param string $transaction_url transaction url. |
|
333 | + * @param WPInv_Invoice $invoice Invoice object. |
|
334 | + * @return string transaction URL, or empty string. |
|
335 | + */ |
|
336 | + public function filter_transaction_url( $transaction_url, $invoice ) { |
|
337 | + |
|
338 | + $transaction_id = $invoice->get_transaction_id(); |
|
339 | + |
|
340 | + if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
341 | + $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
342 | + $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
343 | + $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
344 | + } |
|
345 | + |
|
346 | + return $transaction_url; |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
351 | + * |
|
352 | + * @param string $subscription_url transaction url. |
|
353 | + * @param WPInv_Invoice $invoice Invoice object. |
|
354 | + * @return string subscription URL, or empty string. |
|
355 | + */ |
|
356 | + public function filter_subscription_url( $subscription_url, $invoice ) { |
|
171 | 357 | |
172 | - // Change the checkout button text. |
|
173 | - if ( ! empty( $this->checkout_button_text ) ) { |
|
174 | - add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
175 | - } |
|
176 | - |
|
177 | - // Check if a gateway is valid for a given currency. |
|
178 | - add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
179 | - |
|
180 | - // Generate the transaction url. |
|
181 | - add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
182 | - |
|
183 | - // Generate the subscription url. |
|
184 | - add_filter( "getpaid_gateway_{$this->id}_subscription_url", array( $this, 'filter_subscription_url' ), 10, 2 ); |
|
185 | - |
|
186 | - // Confirm payments. |
|
187 | - add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
188 | - |
|
189 | - // Verify IPNs. |
|
190 | - add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
191 | - |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Checks if this gateway is a given gateway. |
|
196 | - * |
|
197 | - * @since 1.0.19 |
|
198 | - * @return bool |
|
199 | - */ |
|
200 | - public function is( $gateway ) { |
|
201 | - return $gateway == $this->id; |
|
202 | - } |
|
203 | - |
|
204 | - /** |
|
205 | - * Returns a users saved tokens for this gateway. |
|
206 | - * |
|
207 | - * @since 1.0.19 |
|
208 | - * @return array |
|
209 | - */ |
|
210 | - public function get_tokens( $sandbox = null ) { |
|
211 | - |
|
212 | - if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
213 | - $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
214 | - |
|
215 | - if ( is_array( $tokens ) ) { |
|
216 | - $this->tokens = $tokens; |
|
217 | - } |
|
218 | - |
|
219 | - } |
|
220 | - |
|
221 | - if ( ! is_bool( $sandbox ) ) { |
|
222 | - return $this->tokens; |
|
223 | - } |
|
224 | - |
|
225 | - $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
226 | - return wp_list_filter( $this->tokens, $args ); |
|
227 | - |
|
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Saves a token for this gateway. |
|
232 | - * |
|
233 | - * @since 1.0.19 |
|
234 | - */ |
|
235 | - public function save_token( $token ) { |
|
236 | - |
|
237 | - $tokens = $this->get_tokens(); |
|
238 | - $tokens[] = $token; |
|
239 | - |
|
240 | - update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
241 | - |
|
242 | - $this->tokens = $tokens; |
|
243 | - |
|
244 | - } |
|
245 | - |
|
246 | - /** |
|
247 | - * Return the title for admin screens. |
|
248 | - * |
|
249 | - * @return string |
|
250 | - */ |
|
251 | - public function get_method_title() { |
|
252 | - return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * Return the description for admin screens. |
|
257 | - * |
|
258 | - * @return string |
|
259 | - */ |
|
260 | - public function get_method_description() { |
|
261 | - return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Get the success url. |
|
266 | - * |
|
267 | - * @param WPInv_Invoice $invoice Invoice object. |
|
268 | - * @return string |
|
269 | - */ |
|
270 | - public function get_return_url( $invoice ) { |
|
271 | - |
|
272 | - // Payment success url |
|
273 | - $return_url = add_query_arg( |
|
274 | - array( |
|
275 | - 'payment-confirm' => $this->id, |
|
276 | - 'invoice_key' => $invoice->get_key(), |
|
277 | - 'utm_nooverride' => 1 |
|
278 | - ), |
|
279 | - wpinv_get_success_page_uri() |
|
280 | - ); |
|
281 | - |
|
282 | - return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * Confirms payments when rendering the success page. |
|
287 | - * |
|
288 | - * @param string $content Success page content. |
|
289 | - * @return string |
|
290 | - */ |
|
291 | - public function confirm_payment( $content ) { |
|
292 | - |
|
293 | - // Retrieve the invoice. |
|
294 | - $invoice_id = getpaid_get_current_invoice_id(); |
|
295 | - $invoice = wpinv_get_invoice( $invoice_id ); |
|
296 | - |
|
297 | - // Ensure that it exists and that it is pending payment. |
|
298 | - if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
299 | - return $content; |
|
300 | - } |
|
301 | - |
|
302 | - // Can the user view this invoice?? |
|
303 | - if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
304 | - return $content; |
|
305 | - } |
|
306 | - |
|
307 | - // Show payment processing indicator. |
|
308 | - return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Processes ipns and marks payments as complete. |
|
313 | - * |
|
314 | - * @return void |
|
315 | - */ |
|
316 | - public function verify_ipn() {} |
|
317 | - |
|
318 | - /** |
|
319 | - * Processes invoice addons. |
|
320 | - * |
|
321 | - * @param WPInv_Invoice $invoice |
|
322 | - * @param GetPaid_Form_Item[] $items |
|
323 | - * @return WPInv_Invoice |
|
324 | - */ |
|
325 | - public function process_addons( $invoice, $items ) { |
|
326 | - |
|
327 | - } |
|
328 | - |
|
329 | - /** |
|
330 | - * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
331 | - * |
|
332 | - * @param string $transaction_url transaction url. |
|
333 | - * @param WPInv_Invoice $invoice Invoice object. |
|
334 | - * @return string transaction URL, or empty string. |
|
335 | - */ |
|
336 | - public function filter_transaction_url( $transaction_url, $invoice ) { |
|
337 | - |
|
338 | - $transaction_id = $invoice->get_transaction_id(); |
|
339 | - |
|
340 | - if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
341 | - $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
342 | - $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
343 | - $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
344 | - } |
|
345 | - |
|
346 | - return $transaction_url; |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
351 | - * |
|
352 | - * @param string $subscription_url transaction url. |
|
353 | - * @param WPInv_Invoice $invoice Invoice object. |
|
354 | - * @return string subscription URL, or empty string. |
|
355 | - */ |
|
356 | - public function filter_subscription_url( $subscription_url, $invoice ) { |
|
357 | - |
|
358 | - $profile_id = $invoice->get_subscription_id(); |
|
359 | - |
|
360 | - if ( ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
361 | - |
|
362 | - $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
363 | - $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
364 | - $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
365 | - |
|
366 | - } |
|
367 | - |
|
368 | - return $subscription_url; |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Check if the gateway is available for use. |
|
373 | - * |
|
374 | - * @return bool |
|
375 | - */ |
|
376 | - public function is_available() { |
|
377 | - return ! empty( $this->enabled ); |
|
378 | - } |
|
379 | - |
|
380 | - /** |
|
381 | - * Return the gateway's title. |
|
382 | - * |
|
383 | - * @return string |
|
384 | - */ |
|
385 | - public function get_title() { |
|
386 | - return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
387 | - } |
|
388 | - |
|
389 | - /** |
|
390 | - * Return the gateway's description. |
|
391 | - * |
|
392 | - * @return string |
|
393 | - */ |
|
394 | - public function get_description() { |
|
395 | - return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
396 | - } |
|
397 | - |
|
398 | - /** |
|
399 | - * Process Payment. |
|
400 | - * |
|
401 | - * |
|
402 | - * @param WPInv_Invoice $invoice Invoice. |
|
403 | - * @param array $submission_data Posted checkout fields. |
|
404 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
405 | - * @return void |
|
406 | - */ |
|
407 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
408 | - // Process the payment then either redirect to the success page or the gateway. |
|
409 | - do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
410 | - } |
|
411 | - |
|
412 | - /** |
|
413 | - * Process refund. |
|
414 | - * |
|
415 | - * If the gateway declares 'refunds' support, this will allow it to refund. |
|
416 | - * a passed in amount. |
|
417 | - * |
|
418 | - * @param WPInv_Invoice $invoice Invoice. |
|
419 | - * @param float $amount Refund amount. |
|
420 | - * @param string $reason Refund reason. |
|
421 | - * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
422 | - */ |
|
423 | - public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
424 | - return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
425 | - } |
|
426 | - |
|
427 | - /** |
|
428 | - * Displays the payment fields, credit cards etc. |
|
429 | - * |
|
430 | - * @param int $invoice_id 0 or invoice id. |
|
431 | - * @param GetPaid_Payment_Form $form Current payment form. |
|
432 | - */ |
|
433 | - public function payment_fields( $invoice_id, $form ) { |
|
434 | - do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
435 | - } |
|
436 | - |
|
437 | - /** |
|
438 | - * Filters the gateway settings. |
|
439 | - * |
|
440 | - * @param array $admin_settings |
|
441 | - */ |
|
442 | - public function admin_settings( $admin_settings ) { |
|
443 | - return $admin_settings; |
|
444 | - } |
|
445 | - |
|
446 | - /** |
|
447 | - * Retrieves the value of a gateway setting. |
|
448 | - * |
|
449 | - * @param string $option |
|
450 | - */ |
|
451 | - public function get_option( $option, $default = false ) { |
|
452 | - return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
453 | - } |
|
454 | - |
|
455 | - /** |
|
456 | - * Check if a gateway supports a given feature. |
|
457 | - * |
|
458 | - * Gateways should override this to declare support (or lack of support) for a feature. |
|
459 | - * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
460 | - * |
|
461 | - * @param string $feature string The name of a feature to test support for. |
|
462 | - * @return bool True if the gateway supports the feature, false otherwise. |
|
463 | - * @since 1.0.19 |
|
464 | - */ |
|
465 | - public function supports( $feature ) { |
|
466 | - return apply_filters( 'getpaid_payment_gateway_supports', in_array( $feature, $this->supports ), $feature, $this ); |
|
467 | - } |
|
468 | - |
|
469 | - /** |
|
470 | - * Returns the credit card form html. |
|
471 | - * |
|
472 | - * @param bool $save whether or not to display the save button. |
|
473 | - */ |
|
358 | + $profile_id = $invoice->get_subscription_id(); |
|
359 | + |
|
360 | + if ( ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
361 | + |
|
362 | + $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
363 | + $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
364 | + $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
365 | + |
|
366 | + } |
|
367 | + |
|
368 | + return $subscription_url; |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Check if the gateway is available for use. |
|
373 | + * |
|
374 | + * @return bool |
|
375 | + */ |
|
376 | + public function is_available() { |
|
377 | + return ! empty( $this->enabled ); |
|
378 | + } |
|
379 | + |
|
380 | + /** |
|
381 | + * Return the gateway's title. |
|
382 | + * |
|
383 | + * @return string |
|
384 | + */ |
|
385 | + public function get_title() { |
|
386 | + return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
387 | + } |
|
388 | + |
|
389 | + /** |
|
390 | + * Return the gateway's description. |
|
391 | + * |
|
392 | + * @return string |
|
393 | + */ |
|
394 | + public function get_description() { |
|
395 | + return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
396 | + } |
|
397 | + |
|
398 | + /** |
|
399 | + * Process Payment. |
|
400 | + * |
|
401 | + * |
|
402 | + * @param WPInv_Invoice $invoice Invoice. |
|
403 | + * @param array $submission_data Posted checkout fields. |
|
404 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
405 | + * @return void |
|
406 | + */ |
|
407 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
408 | + // Process the payment then either redirect to the success page or the gateway. |
|
409 | + do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
410 | + } |
|
411 | + |
|
412 | + /** |
|
413 | + * Process refund. |
|
414 | + * |
|
415 | + * If the gateway declares 'refunds' support, this will allow it to refund. |
|
416 | + * a passed in amount. |
|
417 | + * |
|
418 | + * @param WPInv_Invoice $invoice Invoice. |
|
419 | + * @param float $amount Refund amount. |
|
420 | + * @param string $reason Refund reason. |
|
421 | + * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
422 | + */ |
|
423 | + public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
424 | + return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
425 | + } |
|
426 | + |
|
427 | + /** |
|
428 | + * Displays the payment fields, credit cards etc. |
|
429 | + * |
|
430 | + * @param int $invoice_id 0 or invoice id. |
|
431 | + * @param GetPaid_Payment_Form $form Current payment form. |
|
432 | + */ |
|
433 | + public function payment_fields( $invoice_id, $form ) { |
|
434 | + do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
435 | + } |
|
436 | + |
|
437 | + /** |
|
438 | + * Filters the gateway settings. |
|
439 | + * |
|
440 | + * @param array $admin_settings |
|
441 | + */ |
|
442 | + public function admin_settings( $admin_settings ) { |
|
443 | + return $admin_settings; |
|
444 | + } |
|
445 | + |
|
446 | + /** |
|
447 | + * Retrieves the value of a gateway setting. |
|
448 | + * |
|
449 | + * @param string $option |
|
450 | + */ |
|
451 | + public function get_option( $option, $default = false ) { |
|
452 | + return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
453 | + } |
|
454 | + |
|
455 | + /** |
|
456 | + * Check if a gateway supports a given feature. |
|
457 | + * |
|
458 | + * Gateways should override this to declare support (or lack of support) for a feature. |
|
459 | + * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
460 | + * |
|
461 | + * @param string $feature string The name of a feature to test support for. |
|
462 | + * @return bool True if the gateway supports the feature, false otherwise. |
|
463 | + * @since 1.0.19 |
|
464 | + */ |
|
465 | + public function supports( $feature ) { |
|
466 | + return apply_filters( 'getpaid_payment_gateway_supports', in_array( $feature, $this->supports ), $feature, $this ); |
|
467 | + } |
|
468 | + |
|
469 | + /** |
|
470 | + * Returns the credit card form html. |
|
471 | + * |
|
472 | + * @param bool $save whether or not to display the save button. |
|
473 | + */ |
|
474 | 474 | public function get_cc_form( $save = false ) { |
475 | 475 | |
476 | - ob_start(); |
|
476 | + ob_start(); |
|
477 | 477 | |
478 | 478 | $id_prefix = esc_attr( uniqid( $this->id ) ); |
479 | 479 | |
@@ -568,11 +568,11 @@ discard block |
||
568 | 568 | 'name' => $this->id . '[cc_cvv2]', |
569 | 569 | 'id' => "$id_prefix-cc-cvv2", |
570 | 570 | 'label' => __( 'CCV', 'invoicing' ), |
571 | - 'label_type' => 'vertical', |
|
572 | - 'class' => 'form-control-sm', |
|
573 | - 'extra_attributes' => array( |
|
574 | - 'autocomplete' => "cc-csc", |
|
575 | - ), |
|
571 | + 'label_type' => 'vertical', |
|
572 | + 'class' => 'form-control-sm', |
|
573 | + 'extra_attributes' => array( |
|
574 | + 'autocomplete' => "cc-csc", |
|
575 | + ), |
|
576 | 576 | ) |
577 | 577 | ); |
578 | 578 | ?> |
@@ -582,191 +582,191 @@ discard block |
||
582 | 582 | |
583 | 583 | <?php |
584 | 584 | |
585 | - if ( $save ) { |
|
586 | - echo $this->save_payment_method_checkbox(); |
|
587 | - } |
|
585 | + if ( $save ) { |
|
586 | + echo $this->save_payment_method_checkbox(); |
|
587 | + } |
|
588 | 588 | |
589 | - ?> |
|
589 | + ?> |
|
590 | 590 | </div> |
591 | 591 | |
592 | 592 | </div> |
593 | 593 | <?php |
594 | 594 | |
595 | - return ob_get_clean(); |
|
595 | + return ob_get_clean(); |
|
596 | 596 | |
597 | 597 | } |
598 | 598 | |
599 | - /** |
|
600 | - * Displays a new payment method entry form. |
|
601 | - * |
|
602 | - * @since 1.0.19 |
|
603 | - */ |
|
604 | - public function new_payment_method_entry( $form ) { |
|
605 | - echo "<div class='getpaid-new-payment-method-form' style='display:none;'>$form</div>"; |
|
606 | - } |
|
607 | - |
|
608 | - /** |
|
609 | - * Grab and display our saved payment methods. |
|
610 | - * |
|
611 | - * @since 1.0.19 |
|
612 | - */ |
|
613 | - public function saved_payment_methods() { |
|
614 | - $html = '<ul class="getpaid-saved-payment-methods list-unstyled m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
615 | - |
|
616 | - foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
617 | - $html .= $this->get_saved_payment_method_option_html( $token ); |
|
618 | - } |
|
619 | - |
|
620 | - $html .= $this->get_new_payment_method_option_html(); |
|
621 | - $html .= '</ul>'; |
|
622 | - |
|
623 | - echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this ); |
|
624 | - } |
|
625 | - |
|
626 | - /** |
|
627 | - * Gets saved payment method HTML from a token. |
|
628 | - * |
|
629 | - * @since 1.0.19 |
|
630 | - * @param array $token Payment Token. |
|
631 | - * @return string Generated payment method HTML |
|
632 | - */ |
|
633 | - public function get_saved_payment_method_option_html( $token ) { |
|
634 | - |
|
635 | - return sprintf( |
|
636 | - '<li class="getpaid-payment-method form-group"> |
|
599 | + /** |
|
600 | + * Displays a new payment method entry form. |
|
601 | + * |
|
602 | + * @since 1.0.19 |
|
603 | + */ |
|
604 | + public function new_payment_method_entry( $form ) { |
|
605 | + echo "<div class='getpaid-new-payment-method-form' style='display:none;'>$form</div>"; |
|
606 | + } |
|
607 | + |
|
608 | + /** |
|
609 | + * Grab and display our saved payment methods. |
|
610 | + * |
|
611 | + * @since 1.0.19 |
|
612 | + */ |
|
613 | + public function saved_payment_methods() { |
|
614 | + $html = '<ul class="getpaid-saved-payment-methods list-unstyled m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
615 | + |
|
616 | + foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
617 | + $html .= $this->get_saved_payment_method_option_html( $token ); |
|
618 | + } |
|
619 | + |
|
620 | + $html .= $this->get_new_payment_method_option_html(); |
|
621 | + $html .= '</ul>'; |
|
622 | + |
|
623 | + echo apply_filters( 'getpaid_payment_gateway_form_saved_payment_methods_html', $html, $this ); |
|
624 | + } |
|
625 | + |
|
626 | + /** |
|
627 | + * Gets saved payment method HTML from a token. |
|
628 | + * |
|
629 | + * @since 1.0.19 |
|
630 | + * @param array $token Payment Token. |
|
631 | + * @return string Generated payment method HTML |
|
632 | + */ |
|
633 | + public function get_saved_payment_method_option_html( $token ) { |
|
634 | + |
|
635 | + return sprintf( |
|
636 | + '<li class="getpaid-payment-method form-group"> |
|
637 | 637 | <label> |
638 | 638 | <input name="getpaid-%1$s-payment-method" type="radio" value="%2$s" style="width:auto;" class="getpaid-saved-payment-method-token-input" %4$s /> |
639 | 639 | <span>%3$s</span> |
640 | 640 | </label> |
641 | 641 | </li>', |
642 | - esc_attr( $this->id ), |
|
643 | - esc_attr( $token['id'] ), |
|
644 | - esc_html( $token['name'] ), |
|
645 | - checked( empty( $token['default'] ), false, false ) |
|
646 | - ); |
|
642 | + esc_attr( $this->id ), |
|
643 | + esc_attr( $token['id'] ), |
|
644 | + esc_html( $token['name'] ), |
|
645 | + checked( empty( $token['default'] ), false, false ) |
|
646 | + ); |
|
647 | 647 | |
648 | - } |
|
648 | + } |
|
649 | 649 | |
650 | - /** |
|
651 | - * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
652 | - * |
|
653 | - * @since 1.0.19 |
|
654 | - */ |
|
655 | - public function get_new_payment_method_option_html() { |
|
650 | + /** |
|
651 | + * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
652 | + * |
|
653 | + * @since 1.0.19 |
|
654 | + */ |
|
655 | + public function get_new_payment_method_option_html() { |
|
656 | 656 | |
657 | - $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
657 | + $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
658 | 658 | |
659 | - return sprintf( |
|
660 | - '<li class="getpaid-new-payment-method"> |
|
659 | + return sprintf( |
|
660 | + '<li class="getpaid-new-payment-method"> |
|
661 | 661 | <label> |
662 | 662 | <input name="getpaid-%1$s-payment-method" type="radio" value="new" style="width:auto;" /> |
663 | 663 | <span>%2$s</span> |
664 | 664 | </label> |
665 | 665 | </li>', |
666 | - esc_attr( $this->id ), |
|
667 | - esc_html( $label ) |
|
668 | - ); |
|
669 | - |
|
670 | - } |
|
671 | - |
|
672 | - /** |
|
673 | - * Outputs a checkbox for saving a new payment method to the database. |
|
674 | - * |
|
675 | - * @since 1.0.19 |
|
676 | - */ |
|
677 | - public function save_payment_method_checkbox() { |
|
678 | - |
|
679 | - return aui()->input( |
|
680 | - array( |
|
681 | - 'type' => 'checkbox', |
|
682 | - 'name' => esc_attr( "getpaid-$this->id-new-payment-method" ), |
|
683 | - 'id' => esc_attr( uniqid( $this->id ) ), |
|
684 | - 'required' => false, |
|
685 | - 'label' => esc_html__( 'Save payment method', 'invoicing' ), |
|
686 | - 'value' => 'true', |
|
687 | - 'checked' => true, |
|
688 | - 'wrap_class' => 'getpaid-save-payment-method pt-1 pb-1', |
|
689 | - ) |
|
690 | - ); |
|
691 | - |
|
692 | - } |
|
693 | - |
|
694 | - /** |
|
695 | - * Registers the gateway. |
|
696 | - * |
|
697 | - * @return array |
|
698 | - */ |
|
699 | - public function register_gateway( $gateways ) { |
|
700 | - |
|
701 | - $gateways[ $this->id ] = array( |
|
702 | - |
|
703 | - 'admin_label' => $this->method_title, |
|
666 | + esc_attr( $this->id ), |
|
667 | + esc_html( $label ) |
|
668 | + ); |
|
669 | + |
|
670 | + } |
|
671 | + |
|
672 | + /** |
|
673 | + * Outputs a checkbox for saving a new payment method to the database. |
|
674 | + * |
|
675 | + * @since 1.0.19 |
|
676 | + */ |
|
677 | + public function save_payment_method_checkbox() { |
|
678 | + |
|
679 | + return aui()->input( |
|
680 | + array( |
|
681 | + 'type' => 'checkbox', |
|
682 | + 'name' => esc_attr( "getpaid-$this->id-new-payment-method" ), |
|
683 | + 'id' => esc_attr( uniqid( $this->id ) ), |
|
684 | + 'required' => false, |
|
685 | + 'label' => esc_html__( 'Save payment method', 'invoicing' ), |
|
686 | + 'value' => 'true', |
|
687 | + 'checked' => true, |
|
688 | + 'wrap_class' => 'getpaid-save-payment-method pt-1 pb-1', |
|
689 | + ) |
|
690 | + ); |
|
691 | + |
|
692 | + } |
|
693 | + |
|
694 | + /** |
|
695 | + * Registers the gateway. |
|
696 | + * |
|
697 | + * @return array |
|
698 | + */ |
|
699 | + public function register_gateway( $gateways ) { |
|
700 | + |
|
701 | + $gateways[ $this->id ] = array( |
|
702 | + |
|
703 | + 'admin_label' => $this->method_title, |
|
704 | 704 | 'checkout_label' => $this->title, |
705 | - 'ordering' => $this->order, |
|
705 | + 'ordering' => $this->order, |
|
706 | 706 | |
707 | - ); |
|
707 | + ); |
|
708 | 708 | |
709 | - return $gateways; |
|
709 | + return $gateways; |
|
710 | 710 | |
711 | - } |
|
711 | + } |
|
712 | 712 | |
713 | - /** |
|
714 | - * Checks whether or not this is a sandbox request. |
|
715 | - * |
|
716 | - * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
717 | - * @return bool |
|
718 | - */ |
|
719 | - public function is_sandbox( $invoice = null ) { |
|
713 | + /** |
|
714 | + * Checks whether or not this is a sandbox request. |
|
715 | + * |
|
716 | + * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
717 | + * @return bool |
|
718 | + */ |
|
719 | + public function is_sandbox( $invoice = null ) { |
|
720 | 720 | |
721 | - if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) { |
|
722 | - return $invoice->get_mode() == 'test'; |
|
723 | - } |
|
721 | + if ( ! empty( $invoice ) && ! $invoice->needs_payment() ) { |
|
722 | + return $invoice->get_mode() == 'test'; |
|
723 | + } |
|
724 | 724 | |
725 | - return wpinv_is_test_mode( $this->id ); |
|
725 | + return wpinv_is_test_mode( $this->id ); |
|
726 | 726 | |
727 | - } |
|
727 | + } |
|
728 | 728 | |
729 | - /** |
|
730 | - * Renames the checkout button |
|
731 | - * |
|
732 | - * @return string |
|
733 | - */ |
|
734 | - public function rename_checkout_button() { |
|
735 | - return $this->checkout_button_text; |
|
736 | - } |
|
729 | + /** |
|
730 | + * Renames the checkout button |
|
731 | + * |
|
732 | + * @return string |
|
733 | + */ |
|
734 | + public function rename_checkout_button() { |
|
735 | + return $this->checkout_button_text; |
|
736 | + } |
|
737 | 737 | |
738 | - /** |
|
739 | - * Validate gateway currency |
|
740 | - * |
|
741 | - * @return bool |
|
742 | - */ |
|
743 | - public function validate_currency( $validation, $currency ) { |
|
738 | + /** |
|
739 | + * Validate gateway currency |
|
740 | + * |
|
741 | + * @return bool |
|
742 | + */ |
|
743 | + public function validate_currency( $validation, $currency ) { |
|
744 | 744 | |
745 | - // Required currencies. |
|
746 | - if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
747 | - return false; |
|
748 | - } |
|
745 | + // Required currencies. |
|
746 | + if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
747 | + return false; |
|
748 | + } |
|
749 | 749 | |
750 | - // Excluded currencies. |
|
751 | - if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
752 | - return false; |
|
753 | - } |
|
750 | + // Excluded currencies. |
|
751 | + if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
752 | + return false; |
|
753 | + } |
|
754 | 754 | |
755 | - return $validation; |
|
756 | - } |
|
755 | + return $validation; |
|
756 | + } |
|
757 | 757 | |
758 | - /** |
|
759 | - * Displays an error |
|
760 | - * |
|
761 | - */ |
|
762 | - public function show_error( $code, $message, $type ) { |
|
758 | + /** |
|
759 | + * Displays an error |
|
760 | + * |
|
761 | + */ |
|
762 | + public function show_error( $code, $message, $type ) { |
|
763 | 763 | |
764 | - if ( is_admin() ) { |
|
765 | - getpaid_admin()->{"show_$type"}( $message ); |
|
766 | - } |
|
764 | + if ( is_admin() ) { |
|
765 | + getpaid_admin()->{"show_$type"}( $message ); |
|
766 | + } |
|
767 | 767 | |
768 | - wpinv_set_error( $code, $message, $type ); |
|
768 | + wpinv_set_error( $code, $message, $type ); |
|
769 | 769 | |
770 | - } |
|
770 | + } |
|
771 | 771 | |
772 | 772 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | /** |
@@ -10,60 +10,60 @@ discard block |
||
10 | 10 | class GetPaid_Form_Item extends WPInv_Item { |
11 | 11 | |
12 | 12 | /** |
13 | - * Stores a custom description for the item. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - protected $custom_description = null; |
|
18 | - |
|
19 | - /** |
|
20 | - * Stores the item quantity. |
|
21 | - * |
|
22 | - * @var float |
|
23 | - */ |
|
24 | - protected $quantity = 1; |
|
25 | - |
|
26 | - /** |
|
27 | - * Stores the item meta. |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - protected $meta = array(); |
|
32 | - |
|
33 | - /** |
|
34 | - * Is this item required? |
|
35 | - * |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - protected $is_required = true; |
|
39 | - |
|
40 | - /** |
|
41 | - * Are quantities allowed? |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - protected $allow_quantities = false; |
|
46 | - |
|
47 | - /** |
|
48 | - * Associated invoice. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - public $invoice_id = 0; |
|
53 | - |
|
54 | - /** |
|
55 | - * Item discount. |
|
56 | - * |
|
57 | - * @var float |
|
58 | - */ |
|
59 | - public $item_discount = 0; |
|
60 | - |
|
61 | - /** |
|
62 | - * Item tax. |
|
63 | - * |
|
64 | - * @var float |
|
65 | - */ |
|
66 | - public $item_tax = 0; |
|
13 | + * Stores a custom description for the item. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + protected $custom_description = null; |
|
18 | + |
|
19 | + /** |
|
20 | + * Stores the item quantity. |
|
21 | + * |
|
22 | + * @var float |
|
23 | + */ |
|
24 | + protected $quantity = 1; |
|
25 | + |
|
26 | + /** |
|
27 | + * Stores the item meta. |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + protected $meta = array(); |
|
32 | + |
|
33 | + /** |
|
34 | + * Is this item required? |
|
35 | + * |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + protected $is_required = true; |
|
39 | + |
|
40 | + /** |
|
41 | + * Are quantities allowed? |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + protected $allow_quantities = false; |
|
46 | + |
|
47 | + /** |
|
48 | + * Associated invoice. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + public $invoice_id = 0; |
|
53 | + |
|
54 | + /** |
|
55 | + * Item discount. |
|
56 | + * |
|
57 | + * @var float |
|
58 | + */ |
|
59 | + public $item_discount = 0; |
|
60 | + |
|
61 | + /** |
|
62 | + * Item tax. |
|
63 | + * |
|
64 | + * @var float |
|
65 | + */ |
|
66 | + public $item_tax = 0; |
|
67 | 67 | |
68 | 68 | /* |
69 | 69 | |-------------------------------------------------------------------------- |
@@ -81,234 +81,234 @@ discard block |
||
81 | 81 | */ |
82 | 82 | |
83 | 83 | /** |
84 | - * Get the item name. |
|
85 | - * |
|
86 | - * @since 1.0.19 |
|
87 | - * @param string $context View or edit context. |
|
88 | - * @return string |
|
89 | - */ |
|
90 | - public function get_name( $context = 'view' ) { |
|
91 | - $name = parent::get_name( $context ); |
|
92 | - return $name . wpinv_get_item_suffix( $this ); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Get the item name without a suffix. |
|
97 | - * |
|
98 | - * @since 1.0.19 |
|
99 | - * @param string $context View or edit context. |
|
100 | - * @return string |
|
101 | - */ |
|
102 | - public function get_raw_name( $context = 'view' ) { |
|
103 | - return parent::get_name( $context ); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * Get the item description. |
|
108 | - * |
|
109 | - * @since 1.0.19 |
|
110 | - * @param string $context View or edit context. |
|
111 | - * @return string |
|
112 | - */ |
|
113 | - public function get_description( $context = 'view' ) { |
|
114 | - |
|
115 | - if ( isset( $this->custom_description ) ) { |
|
116 | - return $this->custom_description; |
|
117 | - } |
|
118 | - |
|
119 | - return parent::get_description( $context ); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Returns the sub total. |
|
124 | - * |
|
125 | - * @since 1.0.19 |
|
126 | - * @param string $context View or edit context. |
|
127 | - * @return float |
|
128 | - */ |
|
129 | - public function get_sub_total( $context = 'view' ) { |
|
130 | - return $this->get_quantity( $context ) * $this->get_initial_price( $context ); |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Returns the recurring sub total. |
|
135 | - * |
|
136 | - * @since 1.0.19 |
|
137 | - * @param string $context View or edit context. |
|
138 | - * @return float |
|
139 | - */ |
|
140 | - public function get_recurring_sub_total( $context = 'view' ) { |
|
141 | - |
|
142 | - if ( $this->is_recurring() ) { |
|
143 | - return $this->get_quantity( $context ) * $this->get_price( $context ); |
|
144 | - } |
|
145 | - |
|
146 | - return 0; |
|
147 | - } |
|
148 | - |
|
149 | - /** |
|
150 | - * @deprecated |
|
151 | - */ |
|
152 | - public function get_qantity( $context = 'view' ) { |
|
153 | - return $this->get_quantity( $context ); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Get the item quantity. |
|
158 | - * |
|
159 | - * @since 1.0.19 |
|
160 | - * @param string $context View or edit context. |
|
161 | - * @return float |
|
162 | - */ |
|
163 | - public function get_quantity( $context = 'view' ) { |
|
164 | - $quantity = (float) $this->quantity; |
|
165 | - |
|
166 | - if ( empty( $quantity ) || 1 > $quantity ) { |
|
167 | - $quantity = 1; |
|
168 | - } |
|
169 | - |
|
170 | - if ( 'view' == $context ) { |
|
171 | - return apply_filters( 'getpaid_payment_form_item_quantity', $quantity, $this ); |
|
172 | - } |
|
173 | - |
|
174 | - return $quantity; |
|
175 | - |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Get the item meta data. |
|
180 | - * |
|
181 | - * @since 1.0.19 |
|
182 | - * @param string $context View or edit context. |
|
183 | - * @return meta |
|
184 | - */ |
|
185 | - public function get_item_meta( $context = 'view' ) { |
|
186 | - $meta = $this->meta; |
|
187 | - |
|
188 | - if ( 'view' == $context ) { |
|
189 | - return apply_filters( 'getpaid_payment_form_item_meta', $meta, $this ); |
|
190 | - } |
|
191 | - |
|
192 | - return $meta; |
|
193 | - |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * Returns whether or not customers can update the item quantity. |
|
198 | - * |
|
199 | - * @since 1.0.19 |
|
200 | - * @param string $context View or edit context. |
|
201 | - * @return bool |
|
202 | - */ |
|
203 | - public function get_allow_quantities( $context = 'view' ) { |
|
204 | - $allow_quantities = (bool) $this->allow_quantities; |
|
205 | - |
|
206 | - if ( 'view' == $context ) { |
|
207 | - return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this ); |
|
208 | - } |
|
209 | - |
|
210 | - return $allow_quantities; |
|
211 | - |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Returns whether or not the item is required. |
|
216 | - * |
|
217 | - * @since 1.0.19 |
|
218 | - * @param string $context View or edit context. |
|
219 | - * @return bool |
|
220 | - */ |
|
221 | - public function get_is_required( $context = 'view' ) { |
|
222 | - $is_required = (bool) $this->is_required; |
|
223 | - |
|
224 | - if ( 'view' == $context ) { |
|
225 | - return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this ); |
|
226 | - } |
|
227 | - |
|
228 | - return $is_required; |
|
229 | - |
|
230 | - } |
|
231 | - |
|
232 | - /** |
|
233 | - * Prepares form data for use. |
|
234 | - * |
|
235 | - * @since 1.0.19 |
|
236 | - * @return array |
|
237 | - */ |
|
238 | - public function prepare_data_for_use( $required = null ) { |
|
239 | - |
|
240 | - $required = is_null( $required ) ? $this->is_required() : $required; |
|
241 | - return array( |
|
242 | - 'title' => strip_tags( $this->get_name() ), |
|
243 | - 'id' => $this->get_id(), |
|
244 | - 'price' => $this->get_price(), |
|
245 | - 'recurring' => $this->is_recurring(), |
|
246 | - 'description' => $this->get_description(), |
|
247 | - 'allow_quantities' => $this->allows_quantities(), |
|
248 | - 'required' => $required, |
|
249 | - ); |
|
250 | - |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Prepares form data for ajax use. |
|
255 | - * |
|
256 | - * @since 1.0.19 |
|
257 | - * @return array |
|
258 | - */ |
|
259 | - public function prepare_data_for_invoice_edit_ajax( $currency = '', $is_renewal = false ) { |
|
260 | - |
|
261 | - $description = getpaid_item_recurring_price_help_text( $this, $currency ); |
|
262 | - |
|
263 | - if ( $description ) { |
|
264 | - $description = "<div class='getpaid-subscription-help-text'>$description</div>"; |
|
265 | - } |
|
266 | - |
|
267 | - $price = ! $is_renewal ? $this->get_price() : $this->get_recurring_price(); |
|
268 | - $subtotal = ! $is_renewal ? $this->get_sub_total() : $this->get_recurring_sub_total(); |
|
269 | - return array( |
|
270 | - 'id' => $this->get_id(), |
|
271 | - 'texts' => array( |
|
272 | - 'item-name' => sanitize_text_field( $this->get_name() ), |
|
273 | - 'item-description' => wp_kses_post( $this->get_description() ) . $description, |
|
274 | - 'item-quantity' => floatval( $this->get_quantity() ), |
|
275 | - 'item-price' => wpinv_price( $price, $currency ), |
|
276 | - 'item-total' => wpinv_price( $subtotal, $currency ), |
|
277 | - ), |
|
278 | - 'inputs' => array( |
|
279 | - 'item-id' => $this->get_id(), |
|
280 | - 'item-name' => sanitize_text_field( $this->get_name() ), |
|
281 | - 'item-description' => wp_kses_post( $this->get_description() ), |
|
282 | - 'item-quantity' => floatval( $this->get_quantity() ), |
|
283 | - 'item-price' => $price, |
|
284 | - ) |
|
285 | - ); |
|
286 | - |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Prepares form data for saving (cart_details). |
|
291 | - * |
|
292 | - * @since 1.0.19 |
|
293 | - * @return array |
|
294 | - */ |
|
295 | - public function prepare_data_for_saving() { |
|
296 | - |
|
297 | - return array( |
|
298 | - 'post_id' => $this->invoice_id, |
|
299 | - 'item_id' => $this->get_id(), |
|
300 | - 'item_name' => sanitize_text_field( $this->get_raw_name() ), |
|
301 | - 'item_description' => $this->get_description(), |
|
302 | - 'tax' => $this->item_tax, |
|
303 | - 'item_price' => $this->get_price(), |
|
304 | - 'quantity' => (float) $this->get_quantity(), |
|
305 | - 'discount' => $this->item_discount, |
|
306 | - 'subtotal' => $this->get_sub_total(), |
|
307 | - 'price' => $this->get_sub_total() + $this->item_tax + $this->item_discount, |
|
308 | - 'meta' => $this->get_item_meta(), |
|
309 | - ); |
|
310 | - |
|
311 | - } |
|
84 | + * Get the item name. |
|
85 | + * |
|
86 | + * @since 1.0.19 |
|
87 | + * @param string $context View or edit context. |
|
88 | + * @return string |
|
89 | + */ |
|
90 | + public function get_name( $context = 'view' ) { |
|
91 | + $name = parent::get_name( $context ); |
|
92 | + return $name . wpinv_get_item_suffix( $this ); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Get the item name without a suffix. |
|
97 | + * |
|
98 | + * @since 1.0.19 |
|
99 | + * @param string $context View or edit context. |
|
100 | + * @return string |
|
101 | + */ |
|
102 | + public function get_raw_name( $context = 'view' ) { |
|
103 | + return parent::get_name( $context ); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * Get the item description. |
|
108 | + * |
|
109 | + * @since 1.0.19 |
|
110 | + * @param string $context View or edit context. |
|
111 | + * @return string |
|
112 | + */ |
|
113 | + public function get_description( $context = 'view' ) { |
|
114 | + |
|
115 | + if ( isset( $this->custom_description ) ) { |
|
116 | + return $this->custom_description; |
|
117 | + } |
|
118 | + |
|
119 | + return parent::get_description( $context ); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Returns the sub total. |
|
124 | + * |
|
125 | + * @since 1.0.19 |
|
126 | + * @param string $context View or edit context. |
|
127 | + * @return float |
|
128 | + */ |
|
129 | + public function get_sub_total( $context = 'view' ) { |
|
130 | + return $this->get_quantity( $context ) * $this->get_initial_price( $context ); |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Returns the recurring sub total. |
|
135 | + * |
|
136 | + * @since 1.0.19 |
|
137 | + * @param string $context View or edit context. |
|
138 | + * @return float |
|
139 | + */ |
|
140 | + public function get_recurring_sub_total( $context = 'view' ) { |
|
141 | + |
|
142 | + if ( $this->is_recurring() ) { |
|
143 | + return $this->get_quantity( $context ) * $this->get_price( $context ); |
|
144 | + } |
|
145 | + |
|
146 | + return 0; |
|
147 | + } |
|
148 | + |
|
149 | + /** |
|
150 | + * @deprecated |
|
151 | + */ |
|
152 | + public function get_qantity( $context = 'view' ) { |
|
153 | + return $this->get_quantity( $context ); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Get the item quantity. |
|
158 | + * |
|
159 | + * @since 1.0.19 |
|
160 | + * @param string $context View or edit context. |
|
161 | + * @return float |
|
162 | + */ |
|
163 | + public function get_quantity( $context = 'view' ) { |
|
164 | + $quantity = (float) $this->quantity; |
|
165 | + |
|
166 | + if ( empty( $quantity ) || 1 > $quantity ) { |
|
167 | + $quantity = 1; |
|
168 | + } |
|
169 | + |
|
170 | + if ( 'view' == $context ) { |
|
171 | + return apply_filters( 'getpaid_payment_form_item_quantity', $quantity, $this ); |
|
172 | + } |
|
173 | + |
|
174 | + return $quantity; |
|
175 | + |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Get the item meta data. |
|
180 | + * |
|
181 | + * @since 1.0.19 |
|
182 | + * @param string $context View or edit context. |
|
183 | + * @return meta |
|
184 | + */ |
|
185 | + public function get_item_meta( $context = 'view' ) { |
|
186 | + $meta = $this->meta; |
|
187 | + |
|
188 | + if ( 'view' == $context ) { |
|
189 | + return apply_filters( 'getpaid_payment_form_item_meta', $meta, $this ); |
|
190 | + } |
|
191 | + |
|
192 | + return $meta; |
|
193 | + |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * Returns whether or not customers can update the item quantity. |
|
198 | + * |
|
199 | + * @since 1.0.19 |
|
200 | + * @param string $context View or edit context. |
|
201 | + * @return bool |
|
202 | + */ |
|
203 | + public function get_allow_quantities( $context = 'view' ) { |
|
204 | + $allow_quantities = (bool) $this->allow_quantities; |
|
205 | + |
|
206 | + if ( 'view' == $context ) { |
|
207 | + return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this ); |
|
208 | + } |
|
209 | + |
|
210 | + return $allow_quantities; |
|
211 | + |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Returns whether or not the item is required. |
|
216 | + * |
|
217 | + * @since 1.0.19 |
|
218 | + * @param string $context View or edit context. |
|
219 | + * @return bool |
|
220 | + */ |
|
221 | + public function get_is_required( $context = 'view' ) { |
|
222 | + $is_required = (bool) $this->is_required; |
|
223 | + |
|
224 | + if ( 'view' == $context ) { |
|
225 | + return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this ); |
|
226 | + } |
|
227 | + |
|
228 | + return $is_required; |
|
229 | + |
|
230 | + } |
|
231 | + |
|
232 | + /** |
|
233 | + * Prepares form data for use. |
|
234 | + * |
|
235 | + * @since 1.0.19 |
|
236 | + * @return array |
|
237 | + */ |
|
238 | + public function prepare_data_for_use( $required = null ) { |
|
239 | + |
|
240 | + $required = is_null( $required ) ? $this->is_required() : $required; |
|
241 | + return array( |
|
242 | + 'title' => strip_tags( $this->get_name() ), |
|
243 | + 'id' => $this->get_id(), |
|
244 | + 'price' => $this->get_price(), |
|
245 | + 'recurring' => $this->is_recurring(), |
|
246 | + 'description' => $this->get_description(), |
|
247 | + 'allow_quantities' => $this->allows_quantities(), |
|
248 | + 'required' => $required, |
|
249 | + ); |
|
250 | + |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Prepares form data for ajax use. |
|
255 | + * |
|
256 | + * @since 1.0.19 |
|
257 | + * @return array |
|
258 | + */ |
|
259 | + public function prepare_data_for_invoice_edit_ajax( $currency = '', $is_renewal = false ) { |
|
260 | + |
|
261 | + $description = getpaid_item_recurring_price_help_text( $this, $currency ); |
|
262 | + |
|
263 | + if ( $description ) { |
|
264 | + $description = "<div class='getpaid-subscription-help-text'>$description</div>"; |
|
265 | + } |
|
266 | + |
|
267 | + $price = ! $is_renewal ? $this->get_price() : $this->get_recurring_price(); |
|
268 | + $subtotal = ! $is_renewal ? $this->get_sub_total() : $this->get_recurring_sub_total(); |
|
269 | + return array( |
|
270 | + 'id' => $this->get_id(), |
|
271 | + 'texts' => array( |
|
272 | + 'item-name' => sanitize_text_field( $this->get_name() ), |
|
273 | + 'item-description' => wp_kses_post( $this->get_description() ) . $description, |
|
274 | + 'item-quantity' => floatval( $this->get_quantity() ), |
|
275 | + 'item-price' => wpinv_price( $price, $currency ), |
|
276 | + 'item-total' => wpinv_price( $subtotal, $currency ), |
|
277 | + ), |
|
278 | + 'inputs' => array( |
|
279 | + 'item-id' => $this->get_id(), |
|
280 | + 'item-name' => sanitize_text_field( $this->get_name() ), |
|
281 | + 'item-description' => wp_kses_post( $this->get_description() ), |
|
282 | + 'item-quantity' => floatval( $this->get_quantity() ), |
|
283 | + 'item-price' => $price, |
|
284 | + ) |
|
285 | + ); |
|
286 | + |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Prepares form data for saving (cart_details). |
|
291 | + * |
|
292 | + * @since 1.0.19 |
|
293 | + * @return array |
|
294 | + */ |
|
295 | + public function prepare_data_for_saving() { |
|
296 | + |
|
297 | + return array( |
|
298 | + 'post_id' => $this->invoice_id, |
|
299 | + 'item_id' => $this->get_id(), |
|
300 | + 'item_name' => sanitize_text_field( $this->get_raw_name() ), |
|
301 | + 'item_description' => $this->get_description(), |
|
302 | + 'tax' => $this->item_tax, |
|
303 | + 'item_price' => $this->get_price(), |
|
304 | + 'quantity' => (float) $this->get_quantity(), |
|
305 | + 'discount' => $this->item_discount, |
|
306 | + 'subtotal' => $this->get_sub_total(), |
|
307 | + 'price' => $this->get_sub_total() + $this->item_tax + $this->item_discount, |
|
308 | + 'meta' => $this->get_item_meta(), |
|
309 | + ); |
|
310 | + |
|
311 | + } |
|
312 | 312 | |
313 | 313 | /* |
314 | 314 | |-------------------------------------------------------------------------- |
@@ -320,70 +320,70 @@ discard block |
||
320 | 320 | | object. |
321 | 321 | */ |
322 | 322 | |
323 | - /** |
|
324 | - * Set the item qantity. |
|
325 | - * |
|
326 | - * @since 1.0.19 |
|
327 | - * @param float $quantity The item quantity. |
|
328 | - */ |
|
329 | - public function set_quantity( $quantity ) { |
|
330 | - |
|
331 | - if ( empty( $quantity ) || ! is_numeric( $quantity ) ) { |
|
332 | - $quantity = 1; |
|
333 | - } |
|
334 | - |
|
335 | - $this->quantity = (float) $quantity; |
|
336 | - |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Set the item meta data. |
|
341 | - * |
|
342 | - * @since 1.0.19 |
|
343 | - * @param array $meta The item meta data. |
|
344 | - */ |
|
345 | - public function set_item_meta( $meta ) { |
|
346 | - $this->meta = maybe_unserialize( $meta ); |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Set whether or not the quantities are allowed. |
|
351 | - * |
|
352 | - * @since 1.0.19 |
|
353 | - * @param bool $allow_quantities |
|
354 | - */ |
|
355 | - public function set_allow_quantities( $allow_quantities ) { |
|
356 | - $this->allow_quantities = (bool) $allow_quantities; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Set whether or not the item is required. |
|
361 | - * |
|
362 | - * @since 1.0.19 |
|
363 | - * @param bool $is_required |
|
364 | - */ |
|
365 | - public function set_is_required( $is_required ) { |
|
366 | - $this->is_required = (bool) $is_required; |
|
367 | - } |
|
368 | - |
|
369 | - /** |
|
370 | - * Sets the custom item description. |
|
371 | - * |
|
372 | - * @since 1.0.19 |
|
373 | - * @param string $description |
|
374 | - */ |
|
375 | - public function set_custom_description( $description ) { |
|
376 | - $this->custom_description = $description; |
|
377 | - } |
|
323 | + /** |
|
324 | + * Set the item qantity. |
|
325 | + * |
|
326 | + * @since 1.0.19 |
|
327 | + * @param float $quantity The item quantity. |
|
328 | + */ |
|
329 | + public function set_quantity( $quantity ) { |
|
330 | + |
|
331 | + if ( empty( $quantity ) || ! is_numeric( $quantity ) ) { |
|
332 | + $quantity = 1; |
|
333 | + } |
|
334 | + |
|
335 | + $this->quantity = (float) $quantity; |
|
336 | + |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Set the item meta data. |
|
341 | + * |
|
342 | + * @since 1.0.19 |
|
343 | + * @param array $meta The item meta data. |
|
344 | + */ |
|
345 | + public function set_item_meta( $meta ) { |
|
346 | + $this->meta = maybe_unserialize( $meta ); |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Set whether or not the quantities are allowed. |
|
351 | + * |
|
352 | + * @since 1.0.19 |
|
353 | + * @param bool $allow_quantities |
|
354 | + */ |
|
355 | + public function set_allow_quantities( $allow_quantities ) { |
|
356 | + $this->allow_quantities = (bool) $allow_quantities; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Set whether or not the item is required. |
|
361 | + * |
|
362 | + * @since 1.0.19 |
|
363 | + * @param bool $is_required |
|
364 | + */ |
|
365 | + public function set_is_required( $is_required ) { |
|
366 | + $this->is_required = (bool) $is_required; |
|
367 | + } |
|
368 | + |
|
369 | + /** |
|
370 | + * Sets the custom item description. |
|
371 | + * |
|
372 | + * @since 1.0.19 |
|
373 | + * @param string $description |
|
374 | + */ |
|
375 | + public function set_custom_description( $description ) { |
|
376 | + $this->custom_description = $description; |
|
377 | + } |
|
378 | 378 | |
379 | 379 | /** |
380 | 380 | * We do not want to save items to the database. |
381 | 381 | * |
382 | - * @return int item id |
|
382 | + * @return int item id |
|
383 | 383 | */ |
384 | 384 | public function save( $data = array() ) { |
385 | 385 | return $this->get_id(); |
386 | - } |
|
386 | + } |
|
387 | 387 | |
388 | 388 | /* |
389 | 389 | |-------------------------------------------------------------------------- |
@@ -395,23 +395,23 @@ discard block |
||
395 | 395 | */ |
396 | 396 | |
397 | 397 | /** |
398 | - * Checks whether the item has enabled dynamic pricing. |
|
399 | - * |
|
400 | - * @since 1.0.19 |
|
401 | - * @return bool |
|
402 | - */ |
|
403 | - public function is_required() { |
|
398 | + * Checks whether the item has enabled dynamic pricing. |
|
399 | + * |
|
400 | + * @since 1.0.19 |
|
401 | + * @return bool |
|
402 | + */ |
|
403 | + public function is_required() { |
|
404 | 404 | return (bool) $this->get_is_required(); |
405 | - } |
|
406 | - |
|
407 | - /** |
|
408 | - * Checks whether users can edit the quantities. |
|
409 | - * |
|
410 | - * @since 1.0.19 |
|
411 | - * @return bool |
|
412 | - */ |
|
413 | - public function allows_quantities() { |
|
405 | + } |
|
406 | + |
|
407 | + /** |
|
408 | + * Checks whether users can edit the quantities. |
|
409 | + * |
|
410 | + * @since 1.0.19 |
|
411 | + * @return bool |
|
412 | + */ |
|
413 | + public function allows_quantities() { |
|
414 | 414 | return (bool) $this->get_allow_quantities(); |
415 | - } |
|
415 | + } |
|
416 | 416 | |
417 | 417 | } |
@@ -12,180 +12,180 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Checkout { |
14 | 14 | |
15 | - /** |
|
16 | - * @var GetPaid_Payment_Form_Submission |
|
17 | - */ |
|
18 | - protected $payment_form_submission; |
|
19 | - |
|
20 | - /** |
|
21 | - * Class constructor. |
|
22 | - * |
|
23 | - * @param GetPaid_Payment_Form_Submission $submission |
|
24 | - */ |
|
25 | - public function __construct( $submission ) { |
|
26 | - $this->payment_form_submission = $submission; |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Processes the checkout. |
|
31 | - * |
|
32 | - */ |
|
33 | - public function process_checkout() { |
|
34 | - |
|
35 | - // Validate the submission. |
|
36 | - $this->validate_submission(); |
|
37 | - |
|
38 | - // Prepare the invoice. |
|
39 | - $items = $this->get_submission_items(); |
|
40 | - $invoice = $this->get_submission_invoice(); |
|
41 | - $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
42 | - $prepared = $this->prepare_submission_data_for_saving(); |
|
43 | - |
|
44 | - $this->prepare_billing_info( $invoice ); |
|
45 | - |
|
46 | - $shipping = $this->prepare_shipping_info( $invoice ); |
|
47 | - |
|
48 | - // Save the invoice. |
|
49 | - $invoice->set_is_viewed( true ); |
|
50 | - $invoice->recalculate_total(); |
|
15 | + /** |
|
16 | + * @var GetPaid_Payment_Form_Submission |
|
17 | + */ |
|
18 | + protected $payment_form_submission; |
|
19 | + |
|
20 | + /** |
|
21 | + * Class constructor. |
|
22 | + * |
|
23 | + * @param GetPaid_Payment_Form_Submission $submission |
|
24 | + */ |
|
25 | + public function __construct( $submission ) { |
|
26 | + $this->payment_form_submission = $submission; |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Processes the checkout. |
|
31 | + * |
|
32 | + */ |
|
33 | + public function process_checkout() { |
|
34 | + |
|
35 | + // Validate the submission. |
|
36 | + $this->validate_submission(); |
|
37 | + |
|
38 | + // Prepare the invoice. |
|
39 | + $items = $this->get_submission_items(); |
|
40 | + $invoice = $this->get_submission_invoice(); |
|
41 | + $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
42 | + $prepared = $this->prepare_submission_data_for_saving(); |
|
43 | + |
|
44 | + $this->prepare_billing_info( $invoice ); |
|
45 | + |
|
46 | + $shipping = $this->prepare_shipping_info( $invoice ); |
|
47 | + |
|
48 | + // Save the invoice. |
|
49 | + $invoice->set_is_viewed( true ); |
|
50 | + $invoice->recalculate_total(); |
|
51 | 51 | $invoice->save(); |
52 | 52 | |
53 | - do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
53 | + do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
54 | 54 | |
55 | - // Send to the gateway. |
|
56 | - $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
57 | - } |
|
55 | + // Send to the gateway. |
|
56 | + $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Validates the submission. |
|
61 | - * |
|
62 | - */ |
|
63 | - protected function validate_submission() { |
|
59 | + /** |
|
60 | + * Validates the submission. |
|
61 | + * |
|
62 | + */ |
|
63 | + protected function validate_submission() { |
|
64 | 64 | |
65 | - $submission = $this->payment_form_submission; |
|
66 | - $data = $submission->get_data(); |
|
65 | + $submission = $this->payment_form_submission; |
|
66 | + $data = $submission->get_data(); |
|
67 | 67 | |
68 | - // Do we have an error? |
|
68 | + // Do we have an error? |
|
69 | 69 | if ( ! empty( $submission->last_error ) ) { |
70 | - wp_send_json_error( $submission->last_error ); |
|
70 | + wp_send_json_error( $submission->last_error ); |
|
71 | 71 | } |
72 | 72 | |
73 | - // We need a billing email. |
|
73 | + // We need a billing email. |
|
74 | 74 | if ( ! $submission->has_billing_email() ) { |
75 | 75 | wp_send_json_error( __( 'Provide a valid billing email.', 'invoicing' ) ); |
76 | - } |
|
76 | + } |
|
77 | 77 | |
78 | - // Non-recurring gateways should not be allowed to process recurring invoices. |
|
79 | - if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | - wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
81 | - } |
|
78 | + // Non-recurring gateways should not be allowed to process recurring invoices. |
|
79 | + if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
80 | + wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
81 | + } |
|
82 | 82 | |
83 | - // Ensure the gateway is active. |
|
84 | - if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | - wpinv_set_error( 'invalid_gateway', __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
86 | - } |
|
83 | + // Ensure the gateway is active. |
|
84 | + if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
85 | + wpinv_set_error( 'invalid_gateway', __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
86 | + } |
|
87 | 87 | |
88 | - // Clear any existing errors. |
|
89 | - wpinv_clear_errors(); |
|
88 | + // Clear any existing errors. |
|
89 | + wpinv_clear_errors(); |
|
90 | 90 | |
91 | - // Allow themes and plugins to hook to errors |
|
92 | - do_action( 'getpaid_checkout_error_checks', $submission ); |
|
91 | + // Allow themes and plugins to hook to errors |
|
92 | + do_action( 'getpaid_checkout_error_checks', $submission ); |
|
93 | 93 | |
94 | - // Do we have any errors? |
|
94 | + // Do we have any errors? |
|
95 | 95 | if ( wpinv_get_errors() ) { |
96 | 96 | wp_send_json_error( getpaid_get_errors_html() ); |
97 | - } |
|
97 | + } |
|
98 | 98 | |
99 | - } |
|
99 | + } |
|
100 | 100 | |
101 | - /** |
|
102 | - * Retrieves submission items. |
|
103 | - * |
|
104 | - * @return GetPaid_Form_Item[] |
|
105 | - */ |
|
106 | - protected function get_submission_items() { |
|
101 | + /** |
|
102 | + * Retrieves submission items. |
|
103 | + * |
|
104 | + * @return GetPaid_Form_Item[] |
|
105 | + */ |
|
106 | + protected function get_submission_items() { |
|
107 | 107 | |
108 | - $items = $this->payment_form_submission->get_items(); |
|
108 | + $items = $this->payment_form_submission->get_items(); |
|
109 | 109 | |
110 | 110 | // Ensure that we have items. |
111 | 111 | if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) { |
112 | 112 | wp_send_json_error( __( 'Please provide at least one item or amount.', 'invoicing' ) ); |
113 | - } |
|
114 | - |
|
115 | - return $items; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Retrieves submission invoice. |
|
120 | - * |
|
121 | - * @return WPInv_Invoice |
|
122 | - */ |
|
123 | - protected function get_submission_invoice() { |
|
124 | - $submission = $this->payment_form_submission; |
|
125 | - |
|
126 | - if ( ! $submission->has_invoice() ) { |
|
127 | - $invoice = new WPInv_Invoice(); |
|
128 | - $invoice->created_via( 'payment_form' ); |
|
129 | - return $invoice; |
|
130 | 113 | } |
131 | 114 | |
132 | - $invoice = $submission->get_invoice(); |
|
115 | + return $items; |
|
116 | + } |
|
133 | 117 | |
134 | - // Make sure that it is neither paid or refunded. |
|
135 | - if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | - wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
137 | - } |
|
118 | + /** |
|
119 | + * Retrieves submission invoice. |
|
120 | + * |
|
121 | + * @return WPInv_Invoice |
|
122 | + */ |
|
123 | + protected function get_submission_invoice() { |
|
124 | + $submission = $this->payment_form_submission; |
|
138 | 125 | |
139 | - return $invoice; |
|
140 | - } |
|
126 | + if ( ! $submission->has_invoice() ) { |
|
127 | + $invoice = new WPInv_Invoice(); |
|
128 | + $invoice->created_via( 'payment_form' ); |
|
129 | + return $invoice; |
|
130 | + } |
|
141 | 131 | |
142 | - /** |
|
143 | - * Processes the submission invoice. |
|
144 | - * |
|
145 | - * @param WPInv_Invoice $invoice |
|
146 | - * @param GetPaid_Form_Item[] $items |
|
147 | - * @return WPInv_Invoice |
|
148 | - */ |
|
149 | - protected function process_submission_invoice( $invoice, $items ) { |
|
132 | + $invoice = $submission->get_invoice(); |
|
150 | 133 | |
151 | - $submission = $this->payment_form_submission; |
|
152 | - $data = $submission->get_data(); |
|
134 | + // Make sure that it is neither paid or refunded. |
|
135 | + if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
136 | + wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
137 | + } |
|
138 | + |
|
139 | + return $invoice; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Processes the submission invoice. |
|
144 | + * |
|
145 | + * @param WPInv_Invoice $invoice |
|
146 | + * @param GetPaid_Form_Item[] $items |
|
147 | + * @return WPInv_Invoice |
|
148 | + */ |
|
149 | + protected function process_submission_invoice( $invoice, $items ) { |
|
153 | 150 | |
154 | - // Set-up the invoice details. |
|
155 | - $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
156 | - $invoice->set_user_id( $this->get_submission_customer() ); |
|
157 | - $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
151 | + $submission = $this->payment_form_submission; |
|
152 | + $data = $submission->get_data(); |
|
153 | + |
|
154 | + // Set-up the invoice details. |
|
155 | + $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
156 | + $invoice->set_user_id( $this->get_submission_customer() ); |
|
157 | + $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
158 | 158 | $invoice->set_items( $items ); |
159 | 159 | $invoice->set_fees( $submission->get_fees() ); |
160 | 160 | $invoice->set_taxes( $submission->get_taxes() ); |
161 | - $invoice->set_discounts( $submission->get_discounts() ); |
|
162 | - $invoice->set_gateway( $data['wpi-gateway'] ); |
|
161 | + $invoice->set_discounts( $submission->get_discounts() ); |
|
162 | + $invoice->set_gateway( $data['wpi-gateway'] ); |
|
163 | 163 | |
164 | - $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
165 | - $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
164 | + $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
165 | + $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
166 | 166 | |
167 | - if ( $submission->has_discount_code() ) { |
|
167 | + if ( $submission->has_discount_code() ) { |
|
168 | 168 | $invoice->set_discount_code( $submission->get_discount_code() ); |
169 | - } |
|
170 | - |
|
171 | - getpaid_maybe_add_default_address( $invoice ); |
|
172 | - return $invoice; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Retrieves the submission's customer. |
|
177 | - * |
|
178 | - * @return int The customer id. |
|
179 | - */ |
|
180 | - protected function get_submission_customer() { |
|
181 | - $submission = $this->payment_form_submission; |
|
182 | - |
|
183 | - // If this is an existing invoice... |
|
184 | - if ( $submission->has_invoice() ) { |
|
185 | - return $submission->get_invoice()->get_user_id(); |
|
186 | - } |
|
187 | - |
|
188 | - // (Maybe) create the user. |
|
169 | + } |
|
170 | + |
|
171 | + getpaid_maybe_add_default_address( $invoice ); |
|
172 | + return $invoice; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Retrieves the submission's customer. |
|
177 | + * |
|
178 | + * @return int The customer id. |
|
179 | + */ |
|
180 | + protected function get_submission_customer() { |
|
181 | + $submission = $this->payment_form_submission; |
|
182 | + |
|
183 | + // If this is an existing invoice... |
|
184 | + if ( $submission->has_invoice() ) { |
|
185 | + return $submission->get_invoice()->get_user_id(); |
|
186 | + } |
|
187 | + |
|
188 | + // (Maybe) create the user. |
|
189 | 189 | $user = get_current_user_id(); |
190 | 190 | |
191 | 191 | if ( empty( $user ) ) { |
@@ -202,31 +202,31 @@ discard block |
||
202 | 202 | |
203 | 203 | if ( is_numeric( $user ) ) { |
204 | 204 | return $user; |
205 | - } |
|
205 | + } |
|
206 | 206 | |
207 | - return $user->ID; |
|
207 | + return $user->ID; |
|
208 | 208 | |
209 | - } |
|
209 | + } |
|
210 | 210 | |
211 | - /** |
|
211 | + /** |
|
212 | 212 | * Prepares submission data for saving to the database. |
213 | 213 | * |
214 | - * @return array |
|
214 | + * @return array |
|
215 | 215 | */ |
216 | 216 | public function prepare_submission_data_for_saving() { |
217 | 217 | |
218 | - $submission = $this->payment_form_submission; |
|
218 | + $submission = $this->payment_form_submission; |
|
219 | 219 | |
220 | - // Prepared submission details. |
|
220 | + // Prepared submission details. |
|
221 | 221 | $prepared = array(); |
222 | 222 | |
223 | 223 | // Raw submission details. |
224 | - $data = $submission->get_data(); |
|
224 | + $data = $submission->get_data(); |
|
225 | 225 | |
226 | - // Loop through the submitted details. |
|
226 | + // Loop through the submitted details. |
|
227 | 227 | foreach ( $submission->get_payment_form()->get_elements() as $field ) { |
228 | 228 | |
229 | - // Skip premade fields. |
|
229 | + // Skip premade fields. |
|
230 | 230 | if ( ! empty( $field['premade'] ) || $field['type'] == 'address' ) { |
231 | 231 | continue; |
232 | 232 | } |
@@ -244,176 +244,176 @@ discard block |
||
244 | 244 | $label = $field['label']; |
245 | 245 | } |
246 | 246 | |
247 | - $prepared[ wpinv_clean( $label ) ] = wp_kses_post( $data[ $field['id'] ] ); |
|
247 | + $prepared[ wpinv_clean( $label ) ] = wp_kses_post( $data[ $field['id'] ] ); |
|
248 | 248 | |
249 | 249 | } |
250 | 250 | |
251 | - } |
|
251 | + } |
|
252 | 252 | |
253 | - return $prepared; |
|
253 | + return $prepared; |
|
254 | 254 | |
255 | - } |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
257 | + /** |
|
258 | 258 | * Retrieves address details. |
259 | 259 | * |
260 | - * @return array |
|
261 | - * @param WPInv_Invoice $invoice |
|
262 | - * @param string $type |
|
260 | + * @return array |
|
261 | + * @param WPInv_Invoice $invoice |
|
262 | + * @param string $type |
|
263 | 263 | */ |
264 | 264 | public function prepare_address_details( $invoice, $type = 'billing' ) { |
265 | 265 | |
266 | - $data = $this->payment_form_submission->get_data(); |
|
267 | - $type = sanitize_key( $type ); |
|
268 | - $address = array(); |
|
269 | - $prepared = array(); |
|
266 | + $data = $this->payment_form_submission->get_data(); |
|
267 | + $type = sanitize_key( $type ); |
|
268 | + $address = array(); |
|
269 | + $prepared = array(); |
|
270 | 270 | |
271 | - if ( ! empty( $data[ $type ] ) ) { |
|
272 | - $address = $data[ $type ]; |
|
273 | - } |
|
271 | + if ( ! empty( $data[ $type ] ) ) { |
|
272 | + $address = $data[ $type ]; |
|
273 | + } |
|
274 | 274 | |
275 | - // Clean address details. |
|
276 | - foreach ( $address as $key => $value ) { |
|
277 | - $key = sanitize_key( $key ); |
|
278 | - $key = str_replace( 'wpinv_', '', $key ); |
|
279 | - $value = wpinv_clean( $value ); |
|
280 | - $prepared[ $key] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
281 | - } |
|
275 | + // Clean address details. |
|
276 | + foreach ( $address as $key => $value ) { |
|
277 | + $key = sanitize_key( $key ); |
|
278 | + $key = str_replace( 'wpinv_', '', $key ); |
|
279 | + $value = wpinv_clean( $value ); |
|
280 | + $prepared[ $key] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
281 | + } |
|
282 | 282 | |
283 | - // Filter address details. |
|
284 | - $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
283 | + // Filter address details. |
|
284 | + $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
285 | 285 | |
286 | - // Remove non-whitelisted values. |
|
287 | - return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
286 | + // Remove non-whitelisted values. |
|
287 | + return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
288 | 288 | |
289 | - } |
|
289 | + } |
|
290 | 290 | |
291 | - /** |
|
291 | + /** |
|
292 | 292 | * Prepares the billing details. |
293 | 293 | * |
294 | - * @return array |
|
295 | - * @param WPInv_Invoice $invoice |
|
294 | + * @return array |
|
295 | + * @param WPInv_Invoice $invoice |
|
296 | 296 | */ |
297 | 297 | protected function prepare_billing_info( &$invoice ) { |
298 | 298 | |
299 | - $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
299 | + $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
300 | 300 | |
301 | - // Update the invoice with the billing details. |
|
302 | - $invoice->set_props( $billing_address ); |
|
301 | + // Update the invoice with the billing details. |
|
302 | + $invoice->set_props( $billing_address ); |
|
303 | 303 | |
304 | - } |
|
304 | + } |
|
305 | 305 | |
306 | - /** |
|
306 | + /** |
|
307 | 307 | * Prepares the shipping details. |
308 | 308 | * |
309 | - * @return array |
|
310 | - * @param WPInv_Invoice $invoice |
|
309 | + * @return array |
|
310 | + * @param WPInv_Invoice $invoice |
|
311 | 311 | */ |
312 | 312 | protected function prepare_shipping_info( $invoice ) { |
313 | 313 | |
314 | - $data = $this->payment_form_submission->get_data(); |
|
314 | + $data = $this->payment_form_submission->get_data(); |
|
315 | 315 | |
316 | - if ( empty( $data['same-shipping-address'] ) ) { |
|
317 | - return $this->prepare_address_details( $invoice, 'shipping' ); |
|
318 | - } |
|
316 | + if ( empty( $data['same-shipping-address'] ) ) { |
|
317 | + return $this->prepare_address_details( $invoice, 'shipping' ); |
|
318 | + } |
|
319 | 319 | |
320 | - return $this->prepare_address_details( $invoice, 'billing' ); |
|
320 | + return $this->prepare_address_details( $invoice, 'billing' ); |
|
321 | 321 | |
322 | - } |
|
322 | + } |
|
323 | 323 | |
324 | - /** |
|
325 | - * Confirms the submission is valid and send users to the gateway. |
|
326 | - * |
|
327 | - * @param WPInv_Invoice $invoice |
|
328 | - * @param array $prepared_payment_form_data |
|
329 | - * @param array $shipping |
|
330 | - */ |
|
331 | - protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
324 | + /** |
|
325 | + * Confirms the submission is valid and send users to the gateway. |
|
326 | + * |
|
327 | + * @param WPInv_Invoice $invoice |
|
328 | + * @param array $prepared_payment_form_data |
|
329 | + * @param array $shipping |
|
330 | + */ |
|
331 | + protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
332 | 332 | |
333 | - // Ensure the invoice exists. |
|
333 | + // Ensure the invoice exists. |
|
334 | 334 | if ( ! $invoice->exists() ) { |
335 | 335 | wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
336 | 336 | } |
337 | 337 | |
338 | - // Save payment form data. |
|
339 | - $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
338 | + // Save payment form data. |
|
339 | + $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
340 | 340 | if ( ! empty( $prepared_payment_form_data ) ) { |
341 | 341 | update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data ); |
342 | - } |
|
342 | + } |
|
343 | 343 | |
344 | - // Save payment form data. |
|
344 | + // Save payment form data. |
|
345 | 345 | if ( ! empty( $shipping ) ) { |
346 | 346 | update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
347 | - } |
|
347 | + } |
|
348 | 348 | |
349 | - // Backwards compatibility. |
|
349 | + // Backwards compatibility. |
|
350 | 350 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
351 | 351 | |
352 | - $this->process_payment( $invoice ); |
|
352 | + $this->process_payment( $invoice ); |
|
353 | 353 | |
354 | 354 | // If we are here, there was an error. |
355 | - wpinv_send_back_to_checkout( $invoice ); |
|
355 | + wpinv_send_back_to_checkout( $invoice ); |
|
356 | 356 | |
357 | - } |
|
357 | + } |
|
358 | 358 | |
359 | - /** |
|
360 | - * Processes the actual payment. |
|
361 | - * |
|
362 | - * @param WPInv_Invoice $invoice |
|
363 | - */ |
|
364 | - protected function process_payment( $invoice ) { |
|
359 | + /** |
|
360 | + * Processes the actual payment. |
|
361 | + * |
|
362 | + * @param WPInv_Invoice $invoice |
|
363 | + */ |
|
364 | + protected function process_payment( $invoice ) { |
|
365 | 365 | |
366 | - // Clear any checkout errors. |
|
367 | - wpinv_clear_errors(); |
|
366 | + // Clear any checkout errors. |
|
367 | + wpinv_clear_errors(); |
|
368 | 368 | |
369 | - // No need to send free invoices to the gateway. |
|
370 | - if ( $invoice->is_free() ) { |
|
371 | - $this->process_free_payment( $invoice ); |
|
372 | - } |
|
369 | + // No need to send free invoices to the gateway. |
|
370 | + if ( $invoice->is_free() ) { |
|
371 | + $this->process_free_payment( $invoice ); |
|
372 | + } |
|
373 | 373 | |
374 | - $submission = $this->payment_form_submission; |
|
374 | + $submission = $this->payment_form_submission; |
|
375 | 375 | |
376 | - // Fires before sending to the gateway. |
|
377 | - do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
376 | + // Fires before sending to the gateway. |
|
377 | + do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
378 | 378 | |
379 | - // Allow the sumission data to be modified before it is sent to the gateway. |
|
380 | - $submission_data = $submission->get_data(); |
|
381 | - $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
382 | - $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
379 | + // Allow the sumission data to be modified before it is sent to the gateway. |
|
380 | + $submission_data = $submission->get_data(); |
|
381 | + $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
382 | + $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
383 | 383 | |
384 | - // Validate the currency. |
|
385 | - if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
386 | - wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support this currency', 'invoicing' ) ); |
|
387 | - } |
|
384 | + // Validate the currency. |
|
385 | + if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
386 | + wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support this currency', 'invoicing' ) ); |
|
387 | + } |
|
388 | 388 | |
389 | - // Check to see if we have any errors. |
|
390 | - if ( wpinv_get_errors() ) { |
|
391 | - wpinv_send_back_to_checkout( $invoice ); |
|
392 | - } |
|
389 | + // Check to see if we have any errors. |
|
390 | + if ( wpinv_get_errors() ) { |
|
391 | + wpinv_send_back_to_checkout( $invoice ); |
|
392 | + } |
|
393 | 393 | |
394 | - // Send info to the gateway for payment processing |
|
395 | - do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
394 | + // Send info to the gateway for payment processing |
|
395 | + do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
396 | 396 | |
397 | - // Backwards compatibility. |
|
398 | - wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
397 | + // Backwards compatibility. |
|
398 | + wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
399 | 399 | |
400 | - } |
|
400 | + } |
|
401 | 401 | |
402 | - /** |
|
403 | - * Marks the invoice as paid in case the checkout is free. |
|
404 | - * |
|
405 | - * @param WPInv_Invoice $invoice |
|
406 | - */ |
|
407 | - protected function process_free_payment( $invoice ) { |
|
402 | + /** |
|
403 | + * Marks the invoice as paid in case the checkout is free. |
|
404 | + * |
|
405 | + * @param WPInv_Invoice $invoice |
|
406 | + */ |
|
407 | + protected function process_free_payment( $invoice ) { |
|
408 | 408 | |
409 | - $invoice->set_gateway( 'none' ); |
|
410 | - $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
411 | - $invoice->mark_paid(); |
|
412 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
409 | + $invoice->set_gateway( 'none' ); |
|
410 | + $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
411 | + $invoice->mark_paid(); |
|
412 | + wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
413 | 413 | |
414 | - } |
|
414 | + } |
|
415 | 415 | |
416 | - /** |
|
416 | + /** |
|
417 | 417 | * Sends a redrect response to payment details. |
418 | 418 | * |
419 | 419 | */ |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | |
6 | 6 | class OxidInstaller extends BaseInstaller |
7 | 7 | { |
8 | - const VENDOR_PATTERN = '/^modules\/(?P<vendor>.+)\/.+/'; |
|
8 | + const VENDOR_PATTERN = '/^modules\/(?P<vendor>.+)\/.+/'; |
|
9 | 9 | |
10 | 10 | protected $locations = array( |
11 | 11 | 'module' => 'modules/{$name}/', |
@@ -13,47 +13,47 @@ discard block |
||
13 | 13 | 'out' => 'out/{$name}/', |
14 | 14 | ); |
15 | 15 | |
16 | - /** |
|
17 | - * getInstallPath |
|
18 | - * |
|
19 | - * @param PackageInterface $package |
|
20 | - * @param string $frameworkType |
|
21 | - * @return string |
|
22 | - */ |
|
23 | - public function getInstallPath(PackageInterface $package, $frameworkType = '') |
|
24 | - { |
|
25 | - $installPath = parent::getInstallPath($package, $frameworkType); |
|
26 | - $type = $this->package->getType(); |
|
27 | - if ($type === 'oxid-module') { |
|
28 | - $this->prepareVendorDirectory($installPath); |
|
29 | - } |
|
30 | - return $installPath; |
|
31 | - } |
|
16 | + /** |
|
17 | + * getInstallPath |
|
18 | + * |
|
19 | + * @param PackageInterface $package |
|
20 | + * @param string $frameworkType |
|
21 | + * @return string |
|
22 | + */ |
|
23 | + public function getInstallPath(PackageInterface $package, $frameworkType = '') |
|
24 | + { |
|
25 | + $installPath = parent::getInstallPath($package, $frameworkType); |
|
26 | + $type = $this->package->getType(); |
|
27 | + if ($type === 'oxid-module') { |
|
28 | + $this->prepareVendorDirectory($installPath); |
|
29 | + } |
|
30 | + return $installPath; |
|
31 | + } |
|
32 | 32 | |
33 | - /** |
|
34 | - * prepareVendorDirectory |
|
35 | - * |
|
36 | - * Makes sure there is a vendormetadata.php file inside |
|
37 | - * the vendor folder if there is a vendor folder. |
|
38 | - * |
|
39 | - * @param string $installPath |
|
40 | - * @return void |
|
41 | - */ |
|
42 | - protected function prepareVendorDirectory($installPath) |
|
43 | - { |
|
44 | - $matches = ''; |
|
45 | - $hasVendorDirectory = preg_match(self::VENDOR_PATTERN, $installPath, $matches); |
|
46 | - if (!$hasVendorDirectory) { |
|
47 | - return; |
|
48 | - } |
|
33 | + /** |
|
34 | + * prepareVendorDirectory |
|
35 | + * |
|
36 | + * Makes sure there is a vendormetadata.php file inside |
|
37 | + * the vendor folder if there is a vendor folder. |
|
38 | + * |
|
39 | + * @param string $installPath |
|
40 | + * @return void |
|
41 | + */ |
|
42 | + protected function prepareVendorDirectory($installPath) |
|
43 | + { |
|
44 | + $matches = ''; |
|
45 | + $hasVendorDirectory = preg_match(self::VENDOR_PATTERN, $installPath, $matches); |
|
46 | + if (!$hasVendorDirectory) { |
|
47 | + return; |
|
48 | + } |
|
49 | 49 | |
50 | - $vendorDirectory = $matches['vendor']; |
|
51 | - $vendorPath = getcwd() . '/modules/' . $vendorDirectory; |
|
52 | - if (!file_exists($vendorPath)) { |
|
53 | - mkdir($vendorPath, 0755, true); |
|
54 | - } |
|
50 | + $vendorDirectory = $matches['vendor']; |
|
51 | + $vendorPath = getcwd() . '/modules/' . $vendorDirectory; |
|
52 | + if (!file_exists($vendorPath)) { |
|
53 | + mkdir($vendorPath, 0755, true); |
|
54 | + } |
|
55 | 55 | |
56 | - $vendorMetaDataPath = $vendorPath . '/vendormetadata.php'; |
|
57 | - touch($vendorMetaDataPath); |
|
58 | - } |
|
56 | + $vendorMetaDataPath = $vendorPath . '/vendormetadata.php'; |
|
57 | + touch($vendorMetaDataPath); |
|
58 | + } |
|
59 | 59 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
4 | - exit; // Exit if accessed directly |
|
4 | + exit; // Exit if accessed directly |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,372 +11,372 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class AUI_Component_Helper { |
13 | 13 | |
14 | - /** |
|
15 | - * A component helper for generating a input name. |
|
16 | - * |
|
17 | - * @param $text |
|
18 | - * @param $multiple bool If the name is set to be multiple but no brackets found then we add some. |
|
19 | - * |
|
20 | - * @return string |
|
21 | - */ |
|
22 | - public static function name($text,$multiple = false){ |
|
23 | - $output = ''; |
|
24 | - |
|
25 | - if($text){ |
|
26 | - $is_multiple = strpos($text, '[') === false && $multiple ? '[]' : ''; |
|
27 | - $output = ' name="'.esc_attr($text).$is_multiple.'" '; |
|
28 | - } |
|
29 | - |
|
30 | - return $output; |
|
31 | - } |
|
32 | - |
|
33 | - /** |
|
34 | - * A component helper for generating a item id. |
|
35 | - * |
|
36 | - * @param $text string The text to be used as the value. |
|
37 | - * |
|
38 | - * @return string The sanitized item. |
|
39 | - */ |
|
40 | - public static function id($text){ |
|
41 | - $output = ''; |
|
42 | - |
|
43 | - if($text){ |
|
44 | - $output = ' id="'.sanitize_html_class($text).'" '; |
|
45 | - } |
|
46 | - |
|
47 | - return $output; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * A component helper for generating a item title. |
|
52 | - * |
|
53 | - * @param $text string The text to be used as the value. |
|
54 | - * |
|
55 | - * @return string The sanitized item. |
|
56 | - */ |
|
57 | - public static function title($text){ |
|
58 | - $output = ''; |
|
59 | - |
|
60 | - if($text){ |
|
61 | - $output = ' title="'.esc_attr($text).'" '; |
|
62 | - } |
|
63 | - |
|
64 | - return $output; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * A component helper for generating a item value. |
|
69 | - * |
|
70 | - * @param $text string The text to be used as the value. |
|
71 | - * |
|
72 | - * @return string The sanitized item. |
|
73 | - */ |
|
74 | - public static function value($text){ |
|
75 | - $output = ''; |
|
76 | - |
|
77 | - if($text){ |
|
78 | - $output = ' value="'.sanitize_text_field($text).'" '; |
|
79 | - } |
|
80 | - |
|
81 | - return $output; |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * A component helper for generating a item class attribute. |
|
86 | - * |
|
87 | - * @param $text string The text to be used as the value. |
|
88 | - * |
|
89 | - * @return string The sanitized item. |
|
90 | - */ |
|
91 | - public static function class_attr($text){ |
|
92 | - $output = ''; |
|
93 | - |
|
94 | - if($text){ |
|
95 | - $classes = self::esc_classes($text); |
|
96 | - if(!empty($classes)){ |
|
97 | - $output = ' class="'.$classes.'" '; |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - return $output; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Escape a string of classes. |
|
106 | - * |
|
107 | - * @param $text |
|
108 | - * |
|
109 | - * @return string |
|
110 | - */ |
|
111 | - public static function esc_classes($text){ |
|
112 | - $output = ''; |
|
113 | - |
|
114 | - if($text){ |
|
115 | - $classes = explode(" ",$text); |
|
116 | - $classes = array_map("trim",$classes); |
|
117 | - $classes = array_map("sanitize_html_class",$classes); |
|
118 | - if(!empty($classes)){ |
|
119 | - $output = implode(" ",$classes); |
|
120 | - } |
|
121 | - } |
|
122 | - |
|
123 | - return $output; |
|
124 | - |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @param $args |
|
129 | - * |
|
130 | - * @return string |
|
131 | - */ |
|
132 | - public static function data_attributes($args){ |
|
133 | - $output = ''; |
|
134 | - |
|
135 | - if(!empty($args)){ |
|
136 | - |
|
137 | - foreach($args as $key => $val){ |
|
138 | - if(substr( $key, 0, 5 ) === "data-"){ |
|
139 | - $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
140 | - } |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - return $output; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param $args |
|
149 | - * |
|
150 | - * @return string |
|
151 | - */ |
|
152 | - public static function aria_attributes($args){ |
|
153 | - $output = ''; |
|
154 | - |
|
155 | - if(!empty($args)){ |
|
156 | - |
|
157 | - foreach($args as $key => $val){ |
|
158 | - if(substr( $key, 0, 5 ) === "aria-"){ |
|
159 | - $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
160 | - } |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - return $output; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Build a font awesome icon from a class. |
|
169 | - * |
|
170 | - * @param $class |
|
171 | - * @param bool $space_after |
|
172 | - * @param array $extra_attributes An array of extra attributes. |
|
173 | - * |
|
174 | - * @return string |
|
175 | - */ |
|
176 | - public static function icon($class,$space_after = false, $extra_attributes = array()){ |
|
177 | - $output = ''; |
|
178 | - |
|
179 | - if($class){ |
|
180 | - $classes = self::esc_classes($class); |
|
181 | - if(!empty($classes)){ |
|
182 | - $output = '<i class="'.$classes.'" '; |
|
183 | - // extra attributes |
|
184 | - if(!empty($extra_attributes)){ |
|
185 | - $output .= AUI_Component_Helper::extra_attributes($extra_attributes); |
|
186 | - } |
|
187 | - $output .= '></i>'; |
|
188 | - if($space_after){ |
|
189 | - $output .= " "; |
|
190 | - } |
|
191 | - } |
|
192 | - } |
|
193 | - |
|
194 | - return $output; |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * @param $args |
|
199 | - * |
|
200 | - * @return string |
|
201 | - */ |
|
202 | - public static function extra_attributes($args){ |
|
203 | - $output = ''; |
|
204 | - |
|
205 | - if(!empty($args)){ |
|
206 | - |
|
207 | - if( is_array($args) ){ |
|
208 | - foreach($args as $key => $val){ |
|
209 | - $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
210 | - } |
|
211 | - }else{ |
|
212 | - $output .= ' '.$args.' '; |
|
213 | - } |
|
214 | - |
|
215 | - } |
|
216 | - |
|
217 | - return $output; |
|
218 | - } |
|
219 | - |
|
220 | - /** |
|
221 | - * @param $args |
|
222 | - * |
|
223 | - * @return string |
|
224 | - */ |
|
225 | - public static function help_text($text){ |
|
226 | - $output = ''; |
|
227 | - |
|
228 | - if($text){ |
|
229 | - $output .= '<small class="form-text text-muted">'.wp_kses_post($text).'</small>'; |
|
230 | - } |
|
231 | - |
|
232 | - |
|
233 | - return $output; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Replace element require context with JS. |
|
238 | - * |
|
239 | - * @param $input |
|
240 | - * |
|
241 | - * @return string|void |
|
242 | - */ |
|
243 | - public static function element_require( $input ) { |
|
244 | - |
|
245 | - $input = str_replace( "'", '"', $input );// we only want double quotes |
|
246 | - |
|
247 | - $output = esc_attr( str_replace( array( "[%", "%]", "%:checked]" ), array( |
|
248 | - "jQuery(form).find('[data-argument=\"", |
|
249 | - "\"]').find('input,select,textarea').val()", |
|
250 | - "\"]').find('input:checked').val()", |
|
251 | - ), $input ) ); |
|
252 | - |
|
253 | - if($output){ |
|
254 | - $output = ' data-element-require="'.$output.'" '; |
|
255 | - } |
|
256 | - |
|
257 | - return $output; |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Returns an array of allowed HTML tags and attributes for a given context. |
|
262 | - * |
|
263 | - * @since 0.1.41 |
|
264 | - * |
|
265 | - * @param string|array $context The context for which to retrieve tags. Allowed values are 'post', |
|
266 | - * 'strip', 'data', 'entities', or the name of a field filter such as |
|
267 | - * 'pre_user_description'. |
|
268 | - * @param array $input Input. |
|
269 | - * @return array Array of allowed HTML tags and their allowed attributes. |
|
270 | - */ |
|
271 | - public static function kses_allowed_html( $context = 'post', $input = array() ) { |
|
272 | - $allowed_html = wp_kses_allowed_html( $context ); |
|
273 | - |
|
274 | - if ( is_array( $allowed_html ) ) { |
|
275 | - // <iframe> |
|
276 | - if ( ! isset( $allowed_html['iframe'] ) && $context == 'post' ) { |
|
277 | - $allowed_html['iframe'] = array( |
|
278 | - 'class' => true, |
|
279 | - 'id' => true, |
|
280 | - 'src' => true, |
|
281 | - 'width' => true, |
|
282 | - 'height' => true, |
|
283 | - 'frameborder' => true, |
|
284 | - 'marginwidth' => true, |
|
285 | - 'marginheight' => true, |
|
286 | - 'scrolling' => true, |
|
287 | - 'style' => true, |
|
288 | - 'title' => true, |
|
289 | - 'allow' => true, |
|
290 | - 'allowfullscreen' => true, |
|
291 | - 'data-*' => true, |
|
292 | - ); |
|
293 | - } |
|
294 | - } |
|
295 | - |
|
296 | - /** |
|
297 | - * Filters the allowed html tags. |
|
298 | - * |
|
299 | - * @since 0.1.41 |
|
300 | - * |
|
301 | - * @param array[]|string $allowed_html Allowed html tags. |
|
302 | - * @param @param string|array $context The context for which to retrieve tags. |
|
303 | - * @param array $input Input field. |
|
304 | - */ |
|
305 | - return apply_filters( 'ayecode_ui_kses_allowed_html', $allowed_html, $context, $input ); |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Filters content and keeps only allowable HTML elements. |
|
310 | - * |
|
311 | - * This function makes sure that only the allowed HTML element names, attribute |
|
312 | - * names and attribute values plus only sane HTML entities will occur in |
|
313 | - * $string. You have to remove any slashes from PHP's magic quotes before you |
|
314 | - * call this function. |
|
315 | - * |
|
316 | - * The default allowed protocols are 'http', 'https', 'ftp', 'mailto', 'news', |
|
317 | - * 'irc', 'gopher', 'nntp', 'feed', 'telnet, 'mms', 'rtsp' and 'svn'. This |
|
318 | - * covers all common link protocols, except for 'javascript' which should not |
|
319 | - * be allowed for untrusted users. |
|
320 | - * |
|
321 | - * @since 0.1.41 |
|
322 | - * |
|
323 | - * @param string|array $value Content to filter through kses. |
|
324 | - * @param array $input Input Field. |
|
325 | - * @return string Filtered content with only allowed HTML elements. |
|
326 | - */ |
|
327 | - public static function _sanitize_html_field( $value, $input = array() ) { |
|
328 | - if ( $value === '' ) { |
|
329 | - return $value; |
|
330 | - } |
|
331 | - |
|
332 | - $allowed_html = self::kses_allowed_html( 'post', $input ); |
|
333 | - |
|
334 | - if ( ! is_array( $allowed_html ) ) { |
|
335 | - $allowed_html = wp_kses_allowed_html( 'post' ); |
|
336 | - } |
|
337 | - |
|
338 | - $filtered = trim( wp_unslash( $value ) ); |
|
339 | - $filtered = wp_kses( $filtered, $allowed_html ); |
|
340 | - $filtered = balanceTags( $filtered ); // Balances tags |
|
341 | - |
|
342 | - return $filtered; |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Navigates through an array, object, or scalar, and removes slashes from the values. |
|
347 | - * |
|
348 | - * @since 0.1.41 |
|
349 | - * |
|
350 | - * @param mixed $value The value to be stripped. |
|
351 | - * @param array $input Input Field. |
|
352 | - * @return mixed Stripped value. |
|
353 | - */ |
|
354 | - public static function sanitize_html_field( $value, $input = array() ) { |
|
355 | - $original = $value; |
|
356 | - |
|
357 | - if ( is_array( $value ) ) { |
|
358 | - foreach ( $value as $index => $item ) { |
|
359 | - $value[ $index ] = self::_sanitize_html_field( $value, $input ); |
|
360 | - } |
|
361 | - } elseif ( is_object( $value ) ) { |
|
362 | - $object_vars = get_object_vars( $value ); |
|
363 | - |
|
364 | - foreach ( $object_vars as $property_name => $property_value ) { |
|
365 | - $value->$property_name = self::_sanitize_html_field( $property_value, $input ); |
|
366 | - } |
|
367 | - } else { |
|
368 | - $value = self::_sanitize_html_field( $value, $input ); |
|
369 | - } |
|
370 | - |
|
371 | - /** |
|
372 | - * Filters content and keeps only allowable HTML elements. |
|
373 | - * |
|
374 | - * @since 0.1.41 |
|
375 | - * |
|
376 | - * @param string|array $value Content to filter through kses. |
|
377 | - * @param string|array $value Original content without filter. |
|
378 | - * @param array $input Input Field. |
|
379 | - */ |
|
380 | - return apply_filters( 'ayecode_ui_sanitize_html_field', $value, $original, $input ); |
|
381 | - } |
|
14 | + /** |
|
15 | + * A component helper for generating a input name. |
|
16 | + * |
|
17 | + * @param $text |
|
18 | + * @param $multiple bool If the name is set to be multiple but no brackets found then we add some. |
|
19 | + * |
|
20 | + * @return string |
|
21 | + */ |
|
22 | + public static function name($text,$multiple = false){ |
|
23 | + $output = ''; |
|
24 | + |
|
25 | + if($text){ |
|
26 | + $is_multiple = strpos($text, '[') === false && $multiple ? '[]' : ''; |
|
27 | + $output = ' name="'.esc_attr($text).$is_multiple.'" '; |
|
28 | + } |
|
29 | + |
|
30 | + return $output; |
|
31 | + } |
|
32 | + |
|
33 | + /** |
|
34 | + * A component helper for generating a item id. |
|
35 | + * |
|
36 | + * @param $text string The text to be used as the value. |
|
37 | + * |
|
38 | + * @return string The sanitized item. |
|
39 | + */ |
|
40 | + public static function id($text){ |
|
41 | + $output = ''; |
|
42 | + |
|
43 | + if($text){ |
|
44 | + $output = ' id="'.sanitize_html_class($text).'" '; |
|
45 | + } |
|
46 | + |
|
47 | + return $output; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * A component helper for generating a item title. |
|
52 | + * |
|
53 | + * @param $text string The text to be used as the value. |
|
54 | + * |
|
55 | + * @return string The sanitized item. |
|
56 | + */ |
|
57 | + public static function title($text){ |
|
58 | + $output = ''; |
|
59 | + |
|
60 | + if($text){ |
|
61 | + $output = ' title="'.esc_attr($text).'" '; |
|
62 | + } |
|
63 | + |
|
64 | + return $output; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * A component helper for generating a item value. |
|
69 | + * |
|
70 | + * @param $text string The text to be used as the value. |
|
71 | + * |
|
72 | + * @return string The sanitized item. |
|
73 | + */ |
|
74 | + public static function value($text){ |
|
75 | + $output = ''; |
|
76 | + |
|
77 | + if($text){ |
|
78 | + $output = ' value="'.sanitize_text_field($text).'" '; |
|
79 | + } |
|
80 | + |
|
81 | + return $output; |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * A component helper for generating a item class attribute. |
|
86 | + * |
|
87 | + * @param $text string The text to be used as the value. |
|
88 | + * |
|
89 | + * @return string The sanitized item. |
|
90 | + */ |
|
91 | + public static function class_attr($text){ |
|
92 | + $output = ''; |
|
93 | + |
|
94 | + if($text){ |
|
95 | + $classes = self::esc_classes($text); |
|
96 | + if(!empty($classes)){ |
|
97 | + $output = ' class="'.$classes.'" '; |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + return $output; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Escape a string of classes. |
|
106 | + * |
|
107 | + * @param $text |
|
108 | + * |
|
109 | + * @return string |
|
110 | + */ |
|
111 | + public static function esc_classes($text){ |
|
112 | + $output = ''; |
|
113 | + |
|
114 | + if($text){ |
|
115 | + $classes = explode(" ",$text); |
|
116 | + $classes = array_map("trim",$classes); |
|
117 | + $classes = array_map("sanitize_html_class",$classes); |
|
118 | + if(!empty($classes)){ |
|
119 | + $output = implode(" ",$classes); |
|
120 | + } |
|
121 | + } |
|
122 | + |
|
123 | + return $output; |
|
124 | + |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @param $args |
|
129 | + * |
|
130 | + * @return string |
|
131 | + */ |
|
132 | + public static function data_attributes($args){ |
|
133 | + $output = ''; |
|
134 | + |
|
135 | + if(!empty($args)){ |
|
136 | + |
|
137 | + foreach($args as $key => $val){ |
|
138 | + if(substr( $key, 0, 5 ) === "data-"){ |
|
139 | + $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
140 | + } |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + return $output; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param $args |
|
149 | + * |
|
150 | + * @return string |
|
151 | + */ |
|
152 | + public static function aria_attributes($args){ |
|
153 | + $output = ''; |
|
154 | + |
|
155 | + if(!empty($args)){ |
|
156 | + |
|
157 | + foreach($args as $key => $val){ |
|
158 | + if(substr( $key, 0, 5 ) === "aria-"){ |
|
159 | + $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
160 | + } |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + return $output; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Build a font awesome icon from a class. |
|
169 | + * |
|
170 | + * @param $class |
|
171 | + * @param bool $space_after |
|
172 | + * @param array $extra_attributes An array of extra attributes. |
|
173 | + * |
|
174 | + * @return string |
|
175 | + */ |
|
176 | + public static function icon($class,$space_after = false, $extra_attributes = array()){ |
|
177 | + $output = ''; |
|
178 | + |
|
179 | + if($class){ |
|
180 | + $classes = self::esc_classes($class); |
|
181 | + if(!empty($classes)){ |
|
182 | + $output = '<i class="'.$classes.'" '; |
|
183 | + // extra attributes |
|
184 | + if(!empty($extra_attributes)){ |
|
185 | + $output .= AUI_Component_Helper::extra_attributes($extra_attributes); |
|
186 | + } |
|
187 | + $output .= '></i>'; |
|
188 | + if($space_after){ |
|
189 | + $output .= " "; |
|
190 | + } |
|
191 | + } |
|
192 | + } |
|
193 | + |
|
194 | + return $output; |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * @param $args |
|
199 | + * |
|
200 | + * @return string |
|
201 | + */ |
|
202 | + public static function extra_attributes($args){ |
|
203 | + $output = ''; |
|
204 | + |
|
205 | + if(!empty($args)){ |
|
206 | + |
|
207 | + if( is_array($args) ){ |
|
208 | + foreach($args as $key => $val){ |
|
209 | + $output .= ' '.sanitize_html_class($key).'="'.esc_attr($val).'" '; |
|
210 | + } |
|
211 | + }else{ |
|
212 | + $output .= ' '.$args.' '; |
|
213 | + } |
|
214 | + |
|
215 | + } |
|
216 | + |
|
217 | + return $output; |
|
218 | + } |
|
219 | + |
|
220 | + /** |
|
221 | + * @param $args |
|
222 | + * |
|
223 | + * @return string |
|
224 | + */ |
|
225 | + public static function help_text($text){ |
|
226 | + $output = ''; |
|
227 | + |
|
228 | + if($text){ |
|
229 | + $output .= '<small class="form-text text-muted">'.wp_kses_post($text).'</small>'; |
|
230 | + } |
|
231 | + |
|
232 | + |
|
233 | + return $output; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Replace element require context with JS. |
|
238 | + * |
|
239 | + * @param $input |
|
240 | + * |
|
241 | + * @return string|void |
|
242 | + */ |
|
243 | + public static function element_require( $input ) { |
|
244 | + |
|
245 | + $input = str_replace( "'", '"', $input );// we only want double quotes |
|
246 | + |
|
247 | + $output = esc_attr( str_replace( array( "[%", "%]", "%:checked]" ), array( |
|
248 | + "jQuery(form).find('[data-argument=\"", |
|
249 | + "\"]').find('input,select,textarea').val()", |
|
250 | + "\"]').find('input:checked').val()", |
|
251 | + ), $input ) ); |
|
252 | + |
|
253 | + if($output){ |
|
254 | + $output = ' data-element-require="'.$output.'" '; |
|
255 | + } |
|
256 | + |
|
257 | + return $output; |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Returns an array of allowed HTML tags and attributes for a given context. |
|
262 | + * |
|
263 | + * @since 0.1.41 |
|
264 | + * |
|
265 | + * @param string|array $context The context for which to retrieve tags. Allowed values are 'post', |
|
266 | + * 'strip', 'data', 'entities', or the name of a field filter such as |
|
267 | + * 'pre_user_description'. |
|
268 | + * @param array $input Input. |
|
269 | + * @return array Array of allowed HTML tags and their allowed attributes. |
|
270 | + */ |
|
271 | + public static function kses_allowed_html( $context = 'post', $input = array() ) { |
|
272 | + $allowed_html = wp_kses_allowed_html( $context ); |
|
273 | + |
|
274 | + if ( is_array( $allowed_html ) ) { |
|
275 | + // <iframe> |
|
276 | + if ( ! isset( $allowed_html['iframe'] ) && $context == 'post' ) { |
|
277 | + $allowed_html['iframe'] = array( |
|
278 | + 'class' => true, |
|
279 | + 'id' => true, |
|
280 | + 'src' => true, |
|
281 | + 'width' => true, |
|
282 | + 'height' => true, |
|
283 | + 'frameborder' => true, |
|
284 | + 'marginwidth' => true, |
|
285 | + 'marginheight' => true, |
|
286 | + 'scrolling' => true, |
|
287 | + 'style' => true, |
|
288 | + 'title' => true, |
|
289 | + 'allow' => true, |
|
290 | + 'allowfullscreen' => true, |
|
291 | + 'data-*' => true, |
|
292 | + ); |
|
293 | + } |
|
294 | + } |
|
295 | + |
|
296 | + /** |
|
297 | + * Filters the allowed html tags. |
|
298 | + * |
|
299 | + * @since 0.1.41 |
|
300 | + * |
|
301 | + * @param array[]|string $allowed_html Allowed html tags. |
|
302 | + * @param @param string|array $context The context for which to retrieve tags. |
|
303 | + * @param array $input Input field. |
|
304 | + */ |
|
305 | + return apply_filters( 'ayecode_ui_kses_allowed_html', $allowed_html, $context, $input ); |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Filters content and keeps only allowable HTML elements. |
|
310 | + * |
|
311 | + * This function makes sure that only the allowed HTML element names, attribute |
|
312 | + * names and attribute values plus only sane HTML entities will occur in |
|
313 | + * $string. You have to remove any slashes from PHP's magic quotes before you |
|
314 | + * call this function. |
|
315 | + * |
|
316 | + * The default allowed protocols are 'http', 'https', 'ftp', 'mailto', 'news', |
|
317 | + * 'irc', 'gopher', 'nntp', 'feed', 'telnet, 'mms', 'rtsp' and 'svn'. This |
|
318 | + * covers all common link protocols, except for 'javascript' which should not |
|
319 | + * be allowed for untrusted users. |
|
320 | + * |
|
321 | + * @since 0.1.41 |
|
322 | + * |
|
323 | + * @param string|array $value Content to filter through kses. |
|
324 | + * @param array $input Input Field. |
|
325 | + * @return string Filtered content with only allowed HTML elements. |
|
326 | + */ |
|
327 | + public static function _sanitize_html_field( $value, $input = array() ) { |
|
328 | + if ( $value === '' ) { |
|
329 | + return $value; |
|
330 | + } |
|
331 | + |
|
332 | + $allowed_html = self::kses_allowed_html( 'post', $input ); |
|
333 | + |
|
334 | + if ( ! is_array( $allowed_html ) ) { |
|
335 | + $allowed_html = wp_kses_allowed_html( 'post' ); |
|
336 | + } |
|
337 | + |
|
338 | + $filtered = trim( wp_unslash( $value ) ); |
|
339 | + $filtered = wp_kses( $filtered, $allowed_html ); |
|
340 | + $filtered = balanceTags( $filtered ); // Balances tags |
|
341 | + |
|
342 | + return $filtered; |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Navigates through an array, object, or scalar, and removes slashes from the values. |
|
347 | + * |
|
348 | + * @since 0.1.41 |
|
349 | + * |
|
350 | + * @param mixed $value The value to be stripped. |
|
351 | + * @param array $input Input Field. |
|
352 | + * @return mixed Stripped value. |
|
353 | + */ |
|
354 | + public static function sanitize_html_field( $value, $input = array() ) { |
|
355 | + $original = $value; |
|
356 | + |
|
357 | + if ( is_array( $value ) ) { |
|
358 | + foreach ( $value as $index => $item ) { |
|
359 | + $value[ $index ] = self::_sanitize_html_field( $value, $input ); |
|
360 | + } |
|
361 | + } elseif ( is_object( $value ) ) { |
|
362 | + $object_vars = get_object_vars( $value ); |
|
363 | + |
|
364 | + foreach ( $object_vars as $property_name => $property_value ) { |
|
365 | + $value->$property_name = self::_sanitize_html_field( $property_value, $input ); |
|
366 | + } |
|
367 | + } else { |
|
368 | + $value = self::_sanitize_html_field( $value, $input ); |
|
369 | + } |
|
370 | + |
|
371 | + /** |
|
372 | + * Filters content and keeps only allowable HTML elements. |
|
373 | + * |
|
374 | + * @since 0.1.41 |
|
375 | + * |
|
376 | + * @param string|array $value Content to filter through kses. |
|
377 | + * @param string|array $value Original content without filter. |
|
378 | + * @param array $input Input Field. |
|
379 | + */ |
|
380 | + return apply_filters( 'ayecode_ui_sanitize_html_field', $value, $original, $input ); |
|
381 | + } |
|
382 | 382 | } |
383 | 383 | \ No newline at end of file |
@@ -15,30 +15,30 @@ discard block |
||
15 | 15 | */ |
16 | 16 | class WPInv_Discount extends GetPaid_Data { |
17 | 17 | |
18 | - /** |
|
19 | - * Which data store to load. |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
18 | + /** |
|
19 | + * Which data store to load. |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | 23 | protected $data_store_name = 'discount'; |
24 | 24 | |
25 | 25 | /** |
26 | - * This is the name of this object type. |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - protected $object_type = 'discount'; |
|
31 | - |
|
32 | - /** |
|
33 | - * Discount Data array. This is the core item data exposed in APIs. |
|
34 | - * |
|
35 | - * @since 1.0.19 |
|
36 | - * @var array |
|
37 | - */ |
|
38 | - protected $data = array( |
|
39 | - 'status' => 'draft', |
|
40 | - 'version' => '', |
|
41 | - 'date_created' => null, |
|
26 | + * This is the name of this object type. |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + protected $object_type = 'discount'; |
|
31 | + |
|
32 | + /** |
|
33 | + * Discount Data array. This is the core item data exposed in APIs. |
|
34 | + * |
|
35 | + * @since 1.0.19 |
|
36 | + * @var array |
|
37 | + */ |
|
38 | + protected $data = array( |
|
39 | + 'status' => 'draft', |
|
40 | + 'version' => '', |
|
41 | + 'date_created' => null, |
|
42 | 42 | 'date_modified' => null, |
43 | 43 | 'name' => 'no-name', |
44 | 44 | 'description' => '', |
@@ -58,144 +58,144 @@ discard block |
||
58 | 58 | 'amount' => null, |
59 | 59 | ); |
60 | 60 | |
61 | - /** |
|
62 | - * Stores meta in cache for future reads. |
|
63 | - * |
|
64 | - * A group must be set to to enable caching. |
|
65 | - * |
|
66 | - * @var string |
|
67 | - */ |
|
68 | - protected $cache_group = 'getpaid_discounts'; |
|
61 | + /** |
|
62 | + * Stores meta in cache for future reads. |
|
63 | + * |
|
64 | + * A group must be set to to enable caching. |
|
65 | + * |
|
66 | + * @var string |
|
67 | + */ |
|
68 | + protected $cache_group = 'getpaid_discounts'; |
|
69 | 69 | |
70 | 70 | /** |
71 | 71 | * Stores a reference to the original WP_Post object |
72 | 72 | * |
73 | 73 | * @var WP_Post |
74 | 74 | */ |
75 | - protected $post = null; |
|
76 | - |
|
77 | - /** |
|
78 | - * Get the discount if ID is passed, otherwise the discount is new and empty. |
|
79 | - * |
|
80 | - * @param int|array|string|WPInv_Discount|WP_Post $discount discount data, object, ID or code. |
|
81 | - */ |
|
82 | - public function __construct( $discount = 0 ) { |
|
83 | - parent::__construct( $discount ); |
|
84 | - |
|
85 | - if ( is_numeric( $discount ) && 'wpi_discount' === get_post_type( $discount ) ) { |
|
86 | - $this->set_id( $discount ); |
|
87 | - } elseif ( $discount instanceof self ) { |
|
88 | - $this->set_id( $discount->get_id() ); |
|
89 | - } elseif ( ! empty( $discount->ID ) ) { |
|
90 | - $this->set_id( $discount->ID ); |
|
91 | - } elseif ( is_array( $discount ) ) { |
|
92 | - $this->set_props( $discount ); |
|
93 | - |
|
94 | - if ( isset( $discount['ID'] ) ) { |
|
95 | - $this->set_id( $discount['ID'] ); |
|
96 | - } |
|
97 | - |
|
98 | - } elseif ( is_scalar( $discount ) && $discount = self::get_discount_id_by_code( $discount ) ) { |
|
99 | - $this->set_id( $discount ); |
|
100 | - } else { |
|
101 | - $this->set_object_read( true ); |
|
102 | - } |
|
75 | + protected $post = null; |
|
76 | + |
|
77 | + /** |
|
78 | + * Get the discount if ID is passed, otherwise the discount is new and empty. |
|
79 | + * |
|
80 | + * @param int|array|string|WPInv_Discount|WP_Post $discount discount data, object, ID or code. |
|
81 | + */ |
|
82 | + public function __construct( $discount = 0 ) { |
|
83 | + parent::__construct( $discount ); |
|
84 | + |
|
85 | + if ( is_numeric( $discount ) && 'wpi_discount' === get_post_type( $discount ) ) { |
|
86 | + $this->set_id( $discount ); |
|
87 | + } elseif ( $discount instanceof self ) { |
|
88 | + $this->set_id( $discount->get_id() ); |
|
89 | + } elseif ( ! empty( $discount->ID ) ) { |
|
90 | + $this->set_id( $discount->ID ); |
|
91 | + } elseif ( is_array( $discount ) ) { |
|
92 | + $this->set_props( $discount ); |
|
93 | + |
|
94 | + if ( isset( $discount['ID'] ) ) { |
|
95 | + $this->set_id( $discount['ID'] ); |
|
96 | + } |
|
97 | + |
|
98 | + } elseif ( is_scalar( $discount ) && $discount = self::get_discount_id_by_code( $discount ) ) { |
|
99 | + $this->set_id( $discount ); |
|
100 | + } else { |
|
101 | + $this->set_object_read( true ); |
|
102 | + } |
|
103 | 103 | |
104 | 104 | // Load the datastore. |
105 | - $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
105 | + $this->data_store = GetPaid_Data_Store::load( $this->data_store_name ); |
|
106 | 106 | |
107 | - if ( $this->get_id() > 0 ) { |
|
107 | + if ( $this->get_id() > 0 ) { |
|
108 | 108 | $this->post = get_post( $this->get_id() ); |
109 | 109 | $this->ID = $this->get_id(); |
110 | - $this->data_store->read( $this ); |
|
110 | + $this->data_store->read( $this ); |
|
111 | + } |
|
112 | + |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Fetch a discount from the db/cache |
|
117 | + * |
|
118 | + * |
|
119 | + * @static |
|
120 | + * @param string $field The field to query against: 'ID', 'discount_code' |
|
121 | + * @param string|int $value The field value |
|
122 | + * @deprecated |
|
123 | + * @since 1.0.15 |
|
124 | + * @return array|bool array of discount details on success. False otherwise. |
|
125 | + */ |
|
126 | + public static function get_data_by( $field, $value ) { |
|
127 | + |
|
128 | + if ( 'id' == strtolower( $field ) ) { |
|
129 | + // Make sure the value is numeric to avoid casting objects, for example, |
|
130 | + // to int 1. |
|
131 | + if ( ! is_numeric( $value ) ) |
|
132 | + return false; |
|
133 | + $value = intval( $value ); |
|
134 | + if ( $value < 1 ) |
|
135 | + return false; |
|
136 | + } |
|
137 | + |
|
138 | + if ( ! $value || ! is_string( $field ) ) { |
|
139 | + return false; |
|
140 | + } |
|
141 | + |
|
142 | + $field = trim( $field ); |
|
143 | + |
|
144 | + // prepare query args |
|
145 | + switch ( strtolower( $field ) ) { |
|
146 | + case 'id': |
|
147 | + $discount_id = $value; |
|
148 | + $args = array( 'include' => array( $value ) ); |
|
149 | + break; |
|
150 | + case 'discount_code': |
|
151 | + case 'code': |
|
152 | + $value = trim( $value ); |
|
153 | + $discount_id = wp_cache_get( $value, 'WPInv_Discount_Codes' ); |
|
154 | + $args = array( 'meta_key' => '_wpi_discount_code', 'meta_value' => $value ); |
|
155 | + break; |
|
156 | + case 'name': |
|
157 | + $discount_id = 0; |
|
158 | + $args = array( 'name' => trim( $value ) ); |
|
159 | + break; |
|
160 | + default: |
|
161 | + $args = apply_filters( "wpinv_discount_get_data_by_{$field}_args", null, $value ); |
|
162 | + if ( ! is_array( $args ) ) { |
|
163 | + return apply_filters( "wpinv_discount_get_data_by_$field", false, $value ); |
|
164 | + } |
|
165 | + |
|
166 | + } |
|
167 | + |
|
168 | + // Check if there is a cached value. |
|
169 | + if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) { |
|
170 | + return $discount; |
|
171 | + } |
|
172 | + |
|
173 | + $args = array_merge( |
|
174 | + $args, |
|
175 | + array( |
|
176 | + 'post_type' => 'wpi_discount', |
|
177 | + 'posts_per_page' => 1, |
|
178 | + 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ) |
|
179 | + ) |
|
180 | + ); |
|
181 | + |
|
182 | + $discount = get_posts( $args ); |
|
183 | + |
|
184 | + if( empty( $discount ) ) { |
|
185 | + return false; |
|
111 | 186 | } |
112 | 187 | |
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Fetch a discount from the db/cache |
|
117 | - * |
|
118 | - * |
|
119 | - * @static |
|
120 | - * @param string $field The field to query against: 'ID', 'discount_code' |
|
121 | - * @param string|int $value The field value |
|
122 | - * @deprecated |
|
123 | - * @since 1.0.15 |
|
124 | - * @return array|bool array of discount details on success. False otherwise. |
|
125 | - */ |
|
126 | - public static function get_data_by( $field, $value ) { |
|
127 | - |
|
128 | - if ( 'id' == strtolower( $field ) ) { |
|
129 | - // Make sure the value is numeric to avoid casting objects, for example, |
|
130 | - // to int 1. |
|
131 | - if ( ! is_numeric( $value ) ) |
|
132 | - return false; |
|
133 | - $value = intval( $value ); |
|
134 | - if ( $value < 1 ) |
|
135 | - return false; |
|
136 | - } |
|
137 | - |
|
138 | - if ( ! $value || ! is_string( $field ) ) { |
|
139 | - return false; |
|
140 | - } |
|
141 | - |
|
142 | - $field = trim( $field ); |
|
143 | - |
|
144 | - // prepare query args |
|
145 | - switch ( strtolower( $field ) ) { |
|
146 | - case 'id': |
|
147 | - $discount_id = $value; |
|
148 | - $args = array( 'include' => array( $value ) ); |
|
149 | - break; |
|
150 | - case 'discount_code': |
|
151 | - case 'code': |
|
152 | - $value = trim( $value ); |
|
153 | - $discount_id = wp_cache_get( $value, 'WPInv_Discount_Codes' ); |
|
154 | - $args = array( 'meta_key' => '_wpi_discount_code', 'meta_value' => $value ); |
|
155 | - break; |
|
156 | - case 'name': |
|
157 | - $discount_id = 0; |
|
158 | - $args = array( 'name' => trim( $value ) ); |
|
159 | - break; |
|
160 | - default: |
|
161 | - $args = apply_filters( "wpinv_discount_get_data_by_{$field}_args", null, $value ); |
|
162 | - if ( ! is_array( $args ) ) { |
|
163 | - return apply_filters( "wpinv_discount_get_data_by_$field", false, $value ); |
|
164 | - } |
|
165 | - |
|
166 | - } |
|
167 | - |
|
168 | - // Check if there is a cached value. |
|
169 | - if ( ! empty( $discount_id ) && $discount = wp_cache_get( (int) $discount_id, 'WPInv_Discounts' ) ) { |
|
170 | - return $discount; |
|
171 | - } |
|
172 | - |
|
173 | - $args = array_merge( |
|
174 | - $args, |
|
175 | - array( |
|
176 | - 'post_type' => 'wpi_discount', |
|
177 | - 'posts_per_page' => 1, |
|
178 | - 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ) |
|
179 | - ) |
|
180 | - ); |
|
181 | - |
|
182 | - $discount = get_posts( $args ); |
|
183 | - |
|
184 | - if( empty( $discount ) ) { |
|
185 | - return false; |
|
186 | - } |
|
187 | - |
|
188 | - $discount = $discount[0]; |
|
189 | - |
|
190 | - // Prepare the return data. |
|
191 | - $return = array( |
|
188 | + $discount = $discount[0]; |
|
189 | + |
|
190 | + // Prepare the return data. |
|
191 | + $return = array( |
|
192 | 192 | 'ID' => $discount->ID, |
193 | 193 | 'code' => get_post_meta( $discount->ID, '_wpi_discount_code', true ), |
194 | 194 | 'amount' => get_post_meta( $discount->ID, '_wpi_discount_amount', true ), |
195 | 195 | 'date_created' => $discount->post_date, |
196 | - 'date_modified' => $discount->post_modified, |
|
197 | - 'status' => $discount->post_status, |
|
198 | - 'start' => get_post_meta( $discount->ID, '_wpi_discount_start', true ), |
|
196 | + 'date_modified' => $discount->post_modified, |
|
197 | + 'status' => $discount->post_status, |
|
198 | + 'start' => get_post_meta( $discount->ID, '_wpi_discount_start', true ), |
|
199 | 199 | 'expiration' => get_post_meta( $discount->ID, '_wpi_discount_expiration', true ), |
200 | 200 | 'type' => get_post_meta( $discount->ID, '_wpi_discount_type', true ), |
201 | 201 | 'description' => $discount->post_excerpt, |
@@ -209,77 +209,77 @@ discard block |
||
209 | 209 | 'max_total' => get_post_meta( $discount->ID, '_wpi_discount_max_total', true ), |
210 | 210 | ); |
211 | 211 | |
212 | - $return = apply_filters( 'wpinv_discount_properties', $return ); |
|
213 | - |
|
214 | - // Update the cache with our data |
|
215 | - wp_cache_add( $discount->ID, $return, 'WPInv_Discounts' ); |
|
216 | - wp_cache_add( $return['code'], $discount->ID, 'WPInv_Discount_Codes' ); |
|
217 | - |
|
218 | - return $return; |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Given a discount code, it returns a discount id. |
|
223 | - * |
|
224 | - * |
|
225 | - * @static |
|
226 | - * @param string $discount_code |
|
227 | - * @since 1.0.15 |
|
228 | - * @return int |
|
229 | - */ |
|
230 | - public static function get_discount_id_by_code( $discount_code ) { |
|
231 | - |
|
232 | - // Trim the code. |
|
233 | - $discount_code = trim( $discount_code ); |
|
234 | - |
|
235 | - // Ensure a value has been passed. |
|
236 | - if ( empty( $discount_code ) ) { |
|
237 | - return 0; |
|
238 | - } |
|
239 | - |
|
240 | - // Maybe retrieve from the cache. |
|
241 | - $discount_id = wp_cache_get( $discount_code, 'getpaid_discount_codes' ); |
|
242 | - if ( ! empty( $discount_id ) ) { |
|
243 | - return $discount_id; |
|
244 | - } |
|
245 | - |
|
246 | - // Fetch the first discount codes. |
|
247 | - $discounts = get_posts( |
|
248 | - array( |
|
249 | - 'meta_key' => '_wpi_discount_code', |
|
250 | - 'meta_value' => $discount_code, |
|
251 | - 'post_type' => 'wpi_discount', |
|
252 | - 'posts_per_page' => 1, |
|
253 | - 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ), |
|
254 | - 'fields' => 'ids', |
|
255 | - ) |
|
256 | - ); |
|
257 | - |
|
258 | - if ( empty( $discounts ) ) { |
|
259 | - return 0; |
|
260 | - } |
|
261 | - |
|
262 | - $discount_id = $discounts[0]; |
|
263 | - |
|
264 | - // Update the cache with our data |
|
265 | - wp_cache_add( get_post_meta( $discount_id, '_wpi_discount_code', true ), $discount_id, 'getpaid_discount_codes' ); |
|
266 | - |
|
267 | - return $discount_id; |
|
268 | - } |
|
269 | - |
|
270 | - /** |
|
271 | - * Magic method for checking the existence of a certain custom field. |
|
272 | - * |
|
273 | - * @since 1.0.15 |
|
274 | - * @access public |
|
275 | - * |
|
276 | - * @return bool Whether the given discount field is set. |
|
277 | - */ |
|
278 | - public function __isset( $key ){ |
|
279 | - return isset( $this->data[$key] ) || method_exists( $this, "get_$key"); |
|
280 | - } |
|
281 | - |
|
282 | - /* |
|
212 | + $return = apply_filters( 'wpinv_discount_properties', $return ); |
|
213 | + |
|
214 | + // Update the cache with our data |
|
215 | + wp_cache_add( $discount->ID, $return, 'WPInv_Discounts' ); |
|
216 | + wp_cache_add( $return['code'], $discount->ID, 'WPInv_Discount_Codes' ); |
|
217 | + |
|
218 | + return $return; |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Given a discount code, it returns a discount id. |
|
223 | + * |
|
224 | + * |
|
225 | + * @static |
|
226 | + * @param string $discount_code |
|
227 | + * @since 1.0.15 |
|
228 | + * @return int |
|
229 | + */ |
|
230 | + public static function get_discount_id_by_code( $discount_code ) { |
|
231 | + |
|
232 | + // Trim the code. |
|
233 | + $discount_code = trim( $discount_code ); |
|
234 | + |
|
235 | + // Ensure a value has been passed. |
|
236 | + if ( empty( $discount_code ) ) { |
|
237 | + return 0; |
|
238 | + } |
|
239 | + |
|
240 | + // Maybe retrieve from the cache. |
|
241 | + $discount_id = wp_cache_get( $discount_code, 'getpaid_discount_codes' ); |
|
242 | + if ( ! empty( $discount_id ) ) { |
|
243 | + return $discount_id; |
|
244 | + } |
|
245 | + |
|
246 | + // Fetch the first discount codes. |
|
247 | + $discounts = get_posts( |
|
248 | + array( |
|
249 | + 'meta_key' => '_wpi_discount_code', |
|
250 | + 'meta_value' => $discount_code, |
|
251 | + 'post_type' => 'wpi_discount', |
|
252 | + 'posts_per_page' => 1, |
|
253 | + 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ), |
|
254 | + 'fields' => 'ids', |
|
255 | + ) |
|
256 | + ); |
|
257 | + |
|
258 | + if ( empty( $discounts ) ) { |
|
259 | + return 0; |
|
260 | + } |
|
261 | + |
|
262 | + $discount_id = $discounts[0]; |
|
263 | + |
|
264 | + // Update the cache with our data |
|
265 | + wp_cache_add( get_post_meta( $discount_id, '_wpi_discount_code', true ), $discount_id, 'getpaid_discount_codes' ); |
|
266 | + |
|
267 | + return $discount_id; |
|
268 | + } |
|
269 | + |
|
270 | + /** |
|
271 | + * Magic method for checking the existence of a certain custom field. |
|
272 | + * |
|
273 | + * @since 1.0.15 |
|
274 | + * @access public |
|
275 | + * |
|
276 | + * @return bool Whether the given discount field is set. |
|
277 | + */ |
|
278 | + public function __isset( $key ){ |
|
279 | + return isset( $this->data[$key] ) || method_exists( $this, "get_$key"); |
|
280 | + } |
|
281 | + |
|
282 | + /* |
|
283 | 283 | |-------------------------------------------------------------------------- |
284 | 284 | | CRUD methods |
285 | 285 | |-------------------------------------------------------------------------- |
@@ -294,430 +294,430 @@ discard block |
||
294 | 294 | |-------------------------------------------------------------------------- |
295 | 295 | */ |
296 | 296 | |
297 | - /** |
|
298 | - * Get discount status. |
|
299 | - * |
|
300 | - * @since 1.0.19 |
|
301 | - * @param string $context View or edit context. |
|
302 | - * @return string |
|
303 | - */ |
|
304 | - public function get_status( $context = 'view' ) { |
|
305 | - return $this->get_prop( 'status', $context ); |
|
297 | + /** |
|
298 | + * Get discount status. |
|
299 | + * |
|
300 | + * @since 1.0.19 |
|
301 | + * @param string $context View or edit context. |
|
302 | + * @return string |
|
303 | + */ |
|
304 | + public function get_status( $context = 'view' ) { |
|
305 | + return $this->get_prop( 'status', $context ); |
|
306 | 306 | } |
307 | 307 | |
308 | 308 | /** |
309 | - * Get plugin version when the discount was created. |
|
310 | - * |
|
311 | - * @since 1.0.19 |
|
312 | - * @param string $context View or edit context. |
|
313 | - * @return string |
|
314 | - */ |
|
315 | - public function get_version( $context = 'view' ) { |
|
316 | - return $this->get_prop( 'version', $context ); |
|
309 | + * Get plugin version when the discount was created. |
|
310 | + * |
|
311 | + * @since 1.0.19 |
|
312 | + * @param string $context View or edit context. |
|
313 | + * @return string |
|
314 | + */ |
|
315 | + public function get_version( $context = 'view' ) { |
|
316 | + return $this->get_prop( 'version', $context ); |
|
317 | 317 | } |
318 | 318 | |
319 | 319 | /** |
320 | - * Get date when the discount was created. |
|
321 | - * |
|
322 | - * @since 1.0.19 |
|
323 | - * @param string $context View or edit context. |
|
324 | - * @return string |
|
325 | - */ |
|
326 | - public function get_date_created( $context = 'view' ) { |
|
327 | - return $this->get_prop( 'date_created', $context ); |
|
320 | + * Get date when the discount was created. |
|
321 | + * |
|
322 | + * @since 1.0.19 |
|
323 | + * @param string $context View or edit context. |
|
324 | + * @return string |
|
325 | + */ |
|
326 | + public function get_date_created( $context = 'view' ) { |
|
327 | + return $this->get_prop( 'date_created', $context ); |
|
328 | 328 | } |
329 | 329 | |
330 | 330 | /** |
331 | - * Get GMT date when the discount was created. |
|
332 | - * |
|
333 | - * @since 1.0.19 |
|
334 | - * @param string $context View or edit context. |
|
335 | - * @return string |
|
336 | - */ |
|
337 | - public function get_date_created_gmt( $context = 'view' ) { |
|
331 | + * Get GMT date when the discount was created. |
|
332 | + * |
|
333 | + * @since 1.0.19 |
|
334 | + * @param string $context View or edit context. |
|
335 | + * @return string |
|
336 | + */ |
|
337 | + public function get_date_created_gmt( $context = 'view' ) { |
|
338 | 338 | $date = $this->get_date_created( $context ); |
339 | 339 | |
340 | 340 | if ( $date ) { |
341 | 341 | $date = get_gmt_from_date( $date ); |
342 | 342 | } |
343 | - return $date; |
|
343 | + return $date; |
|
344 | 344 | } |
345 | 345 | |
346 | 346 | /** |
347 | - * Get date when the discount was last modified. |
|
348 | - * |
|
349 | - * @since 1.0.19 |
|
350 | - * @param string $context View or edit context. |
|
351 | - * @return string |
|
352 | - */ |
|
353 | - public function get_date_modified( $context = 'view' ) { |
|
354 | - return $this->get_prop( 'date_modified', $context ); |
|
347 | + * Get date when the discount was last modified. |
|
348 | + * |
|
349 | + * @since 1.0.19 |
|
350 | + * @param string $context View or edit context. |
|
351 | + * @return string |
|
352 | + */ |
|
353 | + public function get_date_modified( $context = 'view' ) { |
|
354 | + return $this->get_prop( 'date_modified', $context ); |
|
355 | 355 | } |
356 | 356 | |
357 | 357 | /** |
358 | - * Get GMT date when the discount was last modified. |
|
359 | - * |
|
360 | - * @since 1.0.19 |
|
361 | - * @param string $context View or edit context. |
|
362 | - * @return string |
|
363 | - */ |
|
364 | - public function get_date_modified_gmt( $context = 'view' ) { |
|
358 | + * Get GMT date when the discount was last modified. |
|
359 | + * |
|
360 | + * @since 1.0.19 |
|
361 | + * @param string $context View or edit context. |
|
362 | + * @return string |
|
363 | + */ |
|
364 | + public function get_date_modified_gmt( $context = 'view' ) { |
|
365 | 365 | $date = $this->get_date_modified( $context ); |
366 | 366 | |
367 | 367 | if ( $date ) { |
368 | 368 | $date = get_gmt_from_date( $date ); |
369 | 369 | } |
370 | - return $date; |
|
370 | + return $date; |
|
371 | 371 | } |
372 | 372 | |
373 | 373 | /** |
374 | - * Get the discount name. |
|
375 | - * |
|
376 | - * @since 1.0.19 |
|
377 | - * @param string $context View or edit context. |
|
378 | - * @return string |
|
379 | - */ |
|
380 | - public function get_name( $context = 'view' ) { |
|
381 | - return $this->get_prop( 'name', $context ); |
|
374 | + * Get the discount name. |
|
375 | + * |
|
376 | + * @since 1.0.19 |
|
377 | + * @param string $context View or edit context. |
|
378 | + * @return string |
|
379 | + */ |
|
380 | + public function get_name( $context = 'view' ) { |
|
381 | + return $this->get_prop( 'name', $context ); |
|
382 | 382 | } |
383 | 383 | |
384 | 384 | /** |
385 | - * Alias of self::get_name(). |
|
386 | - * |
|
387 | - * @since 1.0.19 |
|
388 | - * @param string $context View or edit context. |
|
389 | - * @return string |
|
390 | - */ |
|
391 | - public function get_title( $context = 'view' ) { |
|
392 | - return $this->get_name( $context ); |
|
385 | + * Alias of self::get_name(). |
|
386 | + * |
|
387 | + * @since 1.0.19 |
|
388 | + * @param string $context View or edit context. |
|
389 | + * @return string |
|
390 | + */ |
|
391 | + public function get_title( $context = 'view' ) { |
|
392 | + return $this->get_name( $context ); |
|
393 | 393 | } |
394 | 394 | |
395 | 395 | /** |
396 | - * Get the discount description. |
|
397 | - * |
|
398 | - * @since 1.0.19 |
|
399 | - * @param string $context View or edit context. |
|
400 | - * @return string |
|
401 | - */ |
|
402 | - public function get_description( $context = 'view' ) { |
|
403 | - return $this->get_prop( 'description', $context ); |
|
396 | + * Get the discount description. |
|
397 | + * |
|
398 | + * @since 1.0.19 |
|
399 | + * @param string $context View or edit context. |
|
400 | + * @return string |
|
401 | + */ |
|
402 | + public function get_description( $context = 'view' ) { |
|
403 | + return $this->get_prop( 'description', $context ); |
|
404 | 404 | } |
405 | 405 | |
406 | 406 | /** |
407 | - * Alias of self::get_description(). |
|
408 | - * |
|
409 | - * @since 1.0.19 |
|
410 | - * @param string $context View or edit context. |
|
411 | - * @return string |
|
412 | - */ |
|
413 | - public function get_excerpt( $context = 'view' ) { |
|
414 | - return $this->get_description( $context ); |
|
407 | + * Alias of self::get_description(). |
|
408 | + * |
|
409 | + * @since 1.0.19 |
|
410 | + * @param string $context View or edit context. |
|
411 | + * @return string |
|
412 | + */ |
|
413 | + public function get_excerpt( $context = 'view' ) { |
|
414 | + return $this->get_description( $context ); |
|
415 | 415 | } |
416 | 416 | |
417 | 417 | /** |
418 | - * Alias of self::get_description(). |
|
419 | - * |
|
420 | - * @since 1.0.19 |
|
421 | - * @param string $context View or edit context. |
|
422 | - * @return string |
|
423 | - */ |
|
424 | - public function get_summary( $context = 'view' ) { |
|
425 | - return $this->get_description( $context ); |
|
418 | + * Alias of self::get_description(). |
|
419 | + * |
|
420 | + * @since 1.0.19 |
|
421 | + * @param string $context View or edit context. |
|
422 | + * @return string |
|
423 | + */ |
|
424 | + public function get_summary( $context = 'view' ) { |
|
425 | + return $this->get_description( $context ); |
|
426 | 426 | } |
427 | 427 | |
428 | 428 | /** |
429 | - * Get the owner of the discount. |
|
430 | - * |
|
431 | - * @since 1.0.19 |
|
432 | - * @param string $context View or edit context. |
|
433 | - * @return string |
|
434 | - */ |
|
435 | - public function get_author( $context = 'view' ) { |
|
436 | - return (int) $this->get_prop( 'author', $context ); |
|
437 | - } |
|
429 | + * Get the owner of the discount. |
|
430 | + * |
|
431 | + * @since 1.0.19 |
|
432 | + * @param string $context View or edit context. |
|
433 | + * @return string |
|
434 | + */ |
|
435 | + public function get_author( $context = 'view' ) { |
|
436 | + return (int) $this->get_prop( 'author', $context ); |
|
437 | + } |
|
438 | 438 | |
439 | - /** |
|
440 | - * Get the discount code. |
|
441 | - * |
|
442 | - * @since 1.0.19 |
|
443 | - * @param string $context View or edit context. |
|
444 | - * @return string |
|
445 | - */ |
|
446 | - public function get_code( $context = 'view' ) { |
|
447 | - return $this->get_prop( 'code', $context ); |
|
448 | - } |
|
439 | + /** |
|
440 | + * Get the discount code. |
|
441 | + * |
|
442 | + * @since 1.0.19 |
|
443 | + * @param string $context View or edit context. |
|
444 | + * @return string |
|
445 | + */ |
|
446 | + public function get_code( $context = 'view' ) { |
|
447 | + return $this->get_prop( 'code', $context ); |
|
448 | + } |
|
449 | 449 | |
450 | - /** |
|
451 | - * Alias for self::get_code(). |
|
452 | - * |
|
453 | - * @since 1.0.19 |
|
454 | - * @param string $context View or edit context. |
|
455 | - * @return string |
|
456 | - */ |
|
457 | - public function get_coupon_code( $context = 'view' ) { |
|
458 | - return $this->get_code( $context ); |
|
459 | - } |
|
450 | + /** |
|
451 | + * Alias for self::get_code(). |
|
452 | + * |
|
453 | + * @since 1.0.19 |
|
454 | + * @param string $context View or edit context. |
|
455 | + * @return string |
|
456 | + */ |
|
457 | + public function get_coupon_code( $context = 'view' ) { |
|
458 | + return $this->get_code( $context ); |
|
459 | + } |
|
460 | 460 | |
461 | - /** |
|
462 | - * Alias for self::get_code(). |
|
463 | - * |
|
464 | - * @since 1.0.19 |
|
465 | - * @param string $context View or edit context. |
|
466 | - * @return string |
|
467 | - */ |
|
468 | - public function get_discount_code( $context = 'view' ) { |
|
469 | - return $this->get_code( $context ); |
|
470 | - } |
|
461 | + /** |
|
462 | + * Alias for self::get_code(). |
|
463 | + * |
|
464 | + * @since 1.0.19 |
|
465 | + * @param string $context View or edit context. |
|
466 | + * @return string |
|
467 | + */ |
|
468 | + public function get_discount_code( $context = 'view' ) { |
|
469 | + return $this->get_code( $context ); |
|
470 | + } |
|
471 | 471 | |
472 | - /** |
|
473 | - * Get the discount's amount. |
|
474 | - * |
|
475 | - * @since 1.0.19 |
|
476 | - * @param string $context View or edit context. |
|
477 | - * @return float |
|
478 | - */ |
|
479 | - public function get_amount( $context = 'view' ) { |
|
480 | - return $this->get_prop( 'amount', $context ); |
|
481 | - } |
|
482 | - |
|
483 | - /** |
|
484 | - * Get the discount's formated amount/rate. |
|
485 | - * |
|
486 | - * @since 1.0.19 |
|
487 | - * @return string |
|
488 | - */ |
|
489 | - public function get_formatted_amount() { |
|
490 | - |
|
491 | - if ( $this->is_type( 'flat' ) ) { |
|
492 | - $rate = wpinv_price( $this->get_amount() ); |
|
493 | - } else { |
|
494 | - $rate = $this->get_amount() . '%'; |
|
495 | - } |
|
496 | - |
|
497 | - return apply_filters( 'wpinv_format_discount_rate', $rate, $this->get_type(), $this->get_amount() ); |
|
498 | - } |
|
472 | + /** |
|
473 | + * Get the discount's amount. |
|
474 | + * |
|
475 | + * @since 1.0.19 |
|
476 | + * @param string $context View or edit context. |
|
477 | + * @return float |
|
478 | + */ |
|
479 | + public function get_amount( $context = 'view' ) { |
|
480 | + return $this->get_prop( 'amount', $context ); |
|
481 | + } |
|
482 | + |
|
483 | + /** |
|
484 | + * Get the discount's formated amount/rate. |
|
485 | + * |
|
486 | + * @since 1.0.19 |
|
487 | + * @return string |
|
488 | + */ |
|
489 | + public function get_formatted_amount() { |
|
490 | + |
|
491 | + if ( $this->is_type( 'flat' ) ) { |
|
492 | + $rate = wpinv_price( $this->get_amount() ); |
|
493 | + } else { |
|
494 | + $rate = $this->get_amount() . '%'; |
|
495 | + } |
|
496 | + |
|
497 | + return apply_filters( 'wpinv_format_discount_rate', $rate, $this->get_type(), $this->get_amount() ); |
|
498 | + } |
|
499 | 499 | |
500 | - /** |
|
501 | - * Get the discount's start date. |
|
502 | - * |
|
503 | - * @since 1.0.19 |
|
504 | - * @param string $context View or edit context. |
|
505 | - * @return string |
|
506 | - */ |
|
507 | - public function get_start( $context = 'view' ) { |
|
508 | - return $this->get_prop( 'start', $context ); |
|
509 | - } |
|
500 | + /** |
|
501 | + * Get the discount's start date. |
|
502 | + * |
|
503 | + * @since 1.0.19 |
|
504 | + * @param string $context View or edit context. |
|
505 | + * @return string |
|
506 | + */ |
|
507 | + public function get_start( $context = 'view' ) { |
|
508 | + return $this->get_prop( 'start', $context ); |
|
509 | + } |
|
510 | 510 | |
511 | - /** |
|
512 | - * Alias for self::get_start(). |
|
513 | - * |
|
514 | - * @since 1.0.19 |
|
515 | - * @param string $context View or edit context. |
|
516 | - * @return string |
|
517 | - */ |
|
518 | - public function get_start_date( $context = 'view' ) { |
|
519 | - return $this->get_start( $context ); |
|
520 | - } |
|
511 | + /** |
|
512 | + * Alias for self::get_start(). |
|
513 | + * |
|
514 | + * @since 1.0.19 |
|
515 | + * @param string $context View or edit context. |
|
516 | + * @return string |
|
517 | + */ |
|
518 | + public function get_start_date( $context = 'view' ) { |
|
519 | + return $this->get_start( $context ); |
|
520 | + } |
|
521 | 521 | |
522 | - /** |
|
523 | - * Get the discount's expiration date. |
|
524 | - * |
|
525 | - * @since 1.0.19 |
|
526 | - * @param string $context View or edit context. |
|
527 | - * @return string |
|
528 | - */ |
|
529 | - public function get_expiration( $context = 'view' ) { |
|
530 | - return $this->get_prop( 'expiration', $context ); |
|
531 | - } |
|
522 | + /** |
|
523 | + * Get the discount's expiration date. |
|
524 | + * |
|
525 | + * @since 1.0.19 |
|
526 | + * @param string $context View or edit context. |
|
527 | + * @return string |
|
528 | + */ |
|
529 | + public function get_expiration( $context = 'view' ) { |
|
530 | + return $this->get_prop( 'expiration', $context ); |
|
531 | + } |
|
532 | 532 | |
533 | - /** |
|
534 | - * Alias for self::get_expiration(). |
|
535 | - * |
|
536 | - * @since 1.0.19 |
|
537 | - * @param string $context View or edit context. |
|
538 | - * @return string |
|
539 | - */ |
|
540 | - public function get_expiration_date( $context = 'view' ) { |
|
541 | - return $this->get_expiration( $context ); |
|
542 | - } |
|
543 | - |
|
544 | - /** |
|
545 | - * Alias for self::get_expiration(). |
|
546 | - * |
|
547 | - * @since 1.0.19 |
|
548 | - * @param string $context View or edit context. |
|
549 | - * @return string |
|
550 | - */ |
|
551 | - public function get_end_date( $context = 'view' ) { |
|
552 | - return $this->get_expiration( $context ); |
|
553 | - } |
|
533 | + /** |
|
534 | + * Alias for self::get_expiration(). |
|
535 | + * |
|
536 | + * @since 1.0.19 |
|
537 | + * @param string $context View or edit context. |
|
538 | + * @return string |
|
539 | + */ |
|
540 | + public function get_expiration_date( $context = 'view' ) { |
|
541 | + return $this->get_expiration( $context ); |
|
542 | + } |
|
543 | + |
|
544 | + /** |
|
545 | + * Alias for self::get_expiration(). |
|
546 | + * |
|
547 | + * @since 1.0.19 |
|
548 | + * @param string $context View or edit context. |
|
549 | + * @return string |
|
550 | + */ |
|
551 | + public function get_end_date( $context = 'view' ) { |
|
552 | + return $this->get_expiration( $context ); |
|
553 | + } |
|
554 | 554 | |
555 | - /** |
|
556 | - * Get the discount's type. |
|
557 | - * |
|
558 | - * @since 1.0.19 |
|
559 | - * @param string $context View or edit context. |
|
560 | - * @return string |
|
561 | - */ |
|
562 | - public function get_type( $context = 'view' ) { |
|
563 | - return $this->get_prop( 'type', $context ); |
|
564 | - } |
|
565 | - |
|
566 | - /** |
|
567 | - * Get the number of times a discount has been used. |
|
568 | - * |
|
569 | - * @since 1.0.19 |
|
570 | - * @param string $context View or edit context. |
|
571 | - * @return int |
|
572 | - */ |
|
573 | - public function get_uses( $context = 'view' ) { |
|
574 | - return (int) $this->get_prop( 'uses', $context ); |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * Get the discount's usage, i.e uses / max uses. |
|
579 | - * |
|
580 | - * @since 1.0.19 |
|
581 | - * @return string |
|
582 | - */ |
|
583 | - public function get_usage() { |
|
584 | - |
|
585 | - if ( ! $this->has_limit() ) { |
|
586 | - return $this->get_uses() . ' / ' . ' ∞'; |
|
587 | - } |
|
588 | - |
|
589 | - return $this->get_uses() . ' / ' . (int) $this->get_max_uses(); |
|
590 | - |
|
591 | - } |
|
592 | - |
|
593 | - /** |
|
594 | - * Get the maximum number of time a discount can be used. |
|
595 | - * |
|
596 | - * @since 1.0.19 |
|
597 | - * @param string $context View or edit context. |
|
598 | - * @return int |
|
599 | - */ |
|
600 | - public function get_max_uses( $context = 'view' ) { |
|
601 | - $max_uses = $this->get_prop( 'max_uses', $context ); |
|
602 | - return empty( $max_uses ) ? null : $max_uses; |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * Checks if this is a single use discount or not. |
|
607 | - * |
|
608 | - * @since 1.0.19 |
|
609 | - * @param string $context View or edit context. |
|
610 | - * @return bool |
|
611 | - */ |
|
612 | - public function get_is_single_use( $context = 'view' ) { |
|
613 | - return $this->get_prop( 'is_single_use', $context ); |
|
614 | - } |
|
615 | - |
|
616 | - /** |
|
617 | - * Get the items that can be used with this discount. |
|
618 | - * |
|
619 | - * @since 1.0.19 |
|
620 | - * @param string $context View or edit context. |
|
621 | - * @return array |
|
622 | - */ |
|
623 | - public function get_items( $context = 'view' ) { |
|
624 | - return wpinv_parse_list( $this->get_prop( 'items', $context ) ); |
|
625 | - } |
|
626 | - |
|
627 | - /** |
|
628 | - * Alias for self::get_items(). |
|
629 | - * |
|
630 | - * @since 1.0.19 |
|
631 | - * @param string $context View or edit context. |
|
632 | - * @return array |
|
633 | - */ |
|
634 | - public function get_allowed_items( $context = 'view' ) { |
|
635 | - return $this->get_items( $context ); |
|
636 | - } |
|
637 | - |
|
638 | - /** |
|
639 | - * Get the items that are not allowed to use this discount. |
|
640 | - * |
|
641 | - * @since 1.0.19 |
|
642 | - * @param string $context View or edit context. |
|
643 | - * @return array |
|
644 | - */ |
|
645 | - public function get_excluded_items( $context = 'view' ) { |
|
646 | - return wpinv_parse_list( $this->get_prop( 'excluded_items', $context ) ); |
|
647 | - } |
|
648 | - |
|
649 | - /** |
|
650 | - * Checks if this is a recurring discount or not. |
|
651 | - * |
|
652 | - * @since 1.0.19 |
|
653 | - * @param string $context View or edit context. |
|
654 | - * @return int|string|bool |
|
655 | - */ |
|
656 | - public function get_is_recurring( $context = 'view' ) { |
|
657 | - return $this->get_prop( 'is_recurring', $context ); |
|
658 | - } |
|
659 | - |
|
660 | - /** |
|
661 | - * Get's the minimum total amount allowed for this discount. |
|
662 | - * |
|
663 | - * @since 1.0.19 |
|
664 | - * @param string $context View or edit context. |
|
665 | - * @return float |
|
666 | - */ |
|
667 | - public function get_min_total( $context = 'view' ) { |
|
668 | - $minimum = $this->get_prop( 'min_total', $context ); |
|
669 | - return empty( $minimum ) ? null : $minimum; |
|
670 | - } |
|
671 | - |
|
672 | - /** |
|
673 | - * Alias for self::get_min_total(). |
|
674 | - * |
|
675 | - * @since 1.0.19 |
|
676 | - * @param string $context View or edit context. |
|
677 | - * @return float |
|
678 | - */ |
|
679 | - public function get_minimum_total( $context = 'view' ) { |
|
680 | - return $this->get_min_total( $context ); |
|
681 | - } |
|
682 | - |
|
683 | - /** |
|
684 | - * Get's the maximum total amount allowed for this discount. |
|
685 | - * |
|
686 | - * @since 1.0.19 |
|
687 | - * @param string $context View or edit context. |
|
688 | - * @return float |
|
689 | - */ |
|
690 | - public function get_max_total( $context = 'view' ) { |
|
691 | - $maximum = $this->get_prop( 'max_total', $context ); |
|
692 | - return empty( $maximum ) ? null : $maximum; |
|
693 | - } |
|
694 | - |
|
695 | - /** |
|
696 | - * Alias for self::get_max_total(). |
|
697 | - * |
|
698 | - * @since 1.0.19 |
|
699 | - * @param string $context View or edit context. |
|
700 | - * @return float |
|
701 | - */ |
|
702 | - public function get_maximum_total( $context = 'view' ) { |
|
703 | - return $this->get_max_total( $context ); |
|
704 | - } |
|
705 | - |
|
706 | - /** |
|
707 | - * Magic method for accessing discount properties. |
|
708 | - * |
|
709 | - * @since 1.0.15 |
|
710 | - * @access public |
|
711 | - * |
|
712 | - * @param string $key Discount data to retrieve |
|
713 | - * @param string $context View or edit context. |
|
714 | - * @return mixed Value of the given discount property (if set). |
|
715 | - */ |
|
716 | - public function get( $key, $context = 'view' ) { |
|
555 | + /** |
|
556 | + * Get the discount's type. |
|
557 | + * |
|
558 | + * @since 1.0.19 |
|
559 | + * @param string $context View or edit context. |
|
560 | + * @return string |
|
561 | + */ |
|
562 | + public function get_type( $context = 'view' ) { |
|
563 | + return $this->get_prop( 'type', $context ); |
|
564 | + } |
|
565 | + |
|
566 | + /** |
|
567 | + * Get the number of times a discount has been used. |
|
568 | + * |
|
569 | + * @since 1.0.19 |
|
570 | + * @param string $context View or edit context. |
|
571 | + * @return int |
|
572 | + */ |
|
573 | + public function get_uses( $context = 'view' ) { |
|
574 | + return (int) $this->get_prop( 'uses', $context ); |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * Get the discount's usage, i.e uses / max uses. |
|
579 | + * |
|
580 | + * @since 1.0.19 |
|
581 | + * @return string |
|
582 | + */ |
|
583 | + public function get_usage() { |
|
584 | + |
|
585 | + if ( ! $this->has_limit() ) { |
|
586 | + return $this->get_uses() . ' / ' . ' ∞'; |
|
587 | + } |
|
588 | + |
|
589 | + return $this->get_uses() . ' / ' . (int) $this->get_max_uses(); |
|
590 | + |
|
591 | + } |
|
592 | + |
|
593 | + /** |
|
594 | + * Get the maximum number of time a discount can be used. |
|
595 | + * |
|
596 | + * @since 1.0.19 |
|
597 | + * @param string $context View or edit context. |
|
598 | + * @return int |
|
599 | + */ |
|
600 | + public function get_max_uses( $context = 'view' ) { |
|
601 | + $max_uses = $this->get_prop( 'max_uses', $context ); |
|
602 | + return empty( $max_uses ) ? null : $max_uses; |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * Checks if this is a single use discount or not. |
|
607 | + * |
|
608 | + * @since 1.0.19 |
|
609 | + * @param string $context View or edit context. |
|
610 | + * @return bool |
|
611 | + */ |
|
612 | + public function get_is_single_use( $context = 'view' ) { |
|
613 | + return $this->get_prop( 'is_single_use', $context ); |
|
614 | + } |
|
615 | + |
|
616 | + /** |
|
617 | + * Get the items that can be used with this discount. |
|
618 | + * |
|
619 | + * @since 1.0.19 |
|
620 | + * @param string $context View or edit context. |
|
621 | + * @return array |
|
622 | + */ |
|
623 | + public function get_items( $context = 'view' ) { |
|
624 | + return wpinv_parse_list( $this->get_prop( 'items', $context ) ); |
|
625 | + } |
|
626 | + |
|
627 | + /** |
|
628 | + * Alias for self::get_items(). |
|
629 | + * |
|
630 | + * @since 1.0.19 |
|
631 | + * @param string $context View or edit context. |
|
632 | + * @return array |
|
633 | + */ |
|
634 | + public function get_allowed_items( $context = 'view' ) { |
|
635 | + return $this->get_items( $context ); |
|
636 | + } |
|
637 | + |
|
638 | + /** |
|
639 | + * Get the items that are not allowed to use this discount. |
|
640 | + * |
|
641 | + * @since 1.0.19 |
|
642 | + * @param string $context View or edit context. |
|
643 | + * @return array |
|
644 | + */ |
|
645 | + public function get_excluded_items( $context = 'view' ) { |
|
646 | + return wpinv_parse_list( $this->get_prop( 'excluded_items', $context ) ); |
|
647 | + } |
|
648 | + |
|
649 | + /** |
|
650 | + * Checks if this is a recurring discount or not. |
|
651 | + * |
|
652 | + * @since 1.0.19 |
|
653 | + * @param string $context View or edit context. |
|
654 | + * @return int|string|bool |
|
655 | + */ |
|
656 | + public function get_is_recurring( $context = 'view' ) { |
|
657 | + return $this->get_prop( 'is_recurring', $context ); |
|
658 | + } |
|
659 | + |
|
660 | + /** |
|
661 | + * Get's the minimum total amount allowed for this discount. |
|
662 | + * |
|
663 | + * @since 1.0.19 |
|
664 | + * @param string $context View or edit context. |
|
665 | + * @return float |
|
666 | + */ |
|
667 | + public function get_min_total( $context = 'view' ) { |
|
668 | + $minimum = $this->get_prop( 'min_total', $context ); |
|
669 | + return empty( $minimum ) ? null : $minimum; |
|
670 | + } |
|
671 | + |
|
672 | + /** |
|
673 | + * Alias for self::get_min_total(). |
|
674 | + * |
|
675 | + * @since 1.0.19 |
|
676 | + * @param string $context View or edit context. |
|
677 | + * @return float |
|
678 | + */ |
|
679 | + public function get_minimum_total( $context = 'view' ) { |
|
680 | + return $this->get_min_total( $context ); |
|
681 | + } |
|
682 | + |
|
683 | + /** |
|
684 | + * Get's the maximum total amount allowed for this discount. |
|
685 | + * |
|
686 | + * @since 1.0.19 |
|
687 | + * @param string $context View or edit context. |
|
688 | + * @return float |
|
689 | + */ |
|
690 | + public function get_max_total( $context = 'view' ) { |
|
691 | + $maximum = $this->get_prop( 'max_total', $context ); |
|
692 | + return empty( $maximum ) ? null : $maximum; |
|
693 | + } |
|
694 | + |
|
695 | + /** |
|
696 | + * Alias for self::get_max_total(). |
|
697 | + * |
|
698 | + * @since 1.0.19 |
|
699 | + * @param string $context View or edit context. |
|
700 | + * @return float |
|
701 | + */ |
|
702 | + public function get_maximum_total( $context = 'view' ) { |
|
703 | + return $this->get_max_total( $context ); |
|
704 | + } |
|
705 | + |
|
706 | + /** |
|
707 | + * Magic method for accessing discount properties. |
|
708 | + * |
|
709 | + * @since 1.0.15 |
|
710 | + * @access public |
|
711 | + * |
|
712 | + * @param string $key Discount data to retrieve |
|
713 | + * @param string $context View or edit context. |
|
714 | + * @return mixed Value of the given discount property (if set). |
|
715 | + */ |
|
716 | + public function get( $key, $context = 'view' ) { |
|
717 | 717 | return $this->get_prop( $key, $context ); |
718 | - } |
|
718 | + } |
|
719 | 719 | |
720 | - /* |
|
720 | + /* |
|
721 | 721 | |-------------------------------------------------------------------------- |
722 | 722 | | Setters |
723 | 723 | |-------------------------------------------------------------------------- |
@@ -727,41 +727,41 @@ discard block |
||
727 | 727 | | object. |
728 | 728 | */ |
729 | 729 | |
730 | - /** |
|
731 | - * Sets discount status. |
|
732 | - * |
|
733 | - * @since 1.0.19 |
|
734 | - * @param string $status New status. |
|
735 | - * @return array details of change. |
|
736 | - */ |
|
737 | - public function set_status( $status ) { |
|
730 | + /** |
|
731 | + * Sets discount status. |
|
732 | + * |
|
733 | + * @since 1.0.19 |
|
734 | + * @param string $status New status. |
|
735 | + * @return array details of change. |
|
736 | + */ |
|
737 | + public function set_status( $status ) { |
|
738 | 738 | $old_status = $this->get_status(); |
739 | 739 | |
740 | 740 | $this->set_prop( 'status', $status ); |
741 | 741 | |
742 | - return array( |
|
743 | - 'from' => $old_status, |
|
744 | - 'to' => $status, |
|
745 | - ); |
|
742 | + return array( |
|
743 | + 'from' => $old_status, |
|
744 | + 'to' => $status, |
|
745 | + ); |
|
746 | 746 | } |
747 | 747 | |
748 | 748 | /** |
749 | - * Set plugin version when the discount was created. |
|
750 | - * |
|
751 | - * @since 1.0.19 |
|
752 | - */ |
|
753 | - public function set_version( $value ) { |
|
754 | - $this->set_prop( 'version', $value ); |
|
749 | + * Set plugin version when the discount was created. |
|
750 | + * |
|
751 | + * @since 1.0.19 |
|
752 | + */ |
|
753 | + public function set_version( $value ) { |
|
754 | + $this->set_prop( 'version', $value ); |
|
755 | 755 | } |
756 | 756 | |
757 | 757 | /** |
758 | - * Set date when the discount was created. |
|
759 | - * |
|
760 | - * @since 1.0.19 |
|
761 | - * @param string $value Value to set. |
|
758 | + * Set date when the discount was created. |
|
759 | + * |
|
760 | + * @since 1.0.19 |
|
761 | + * @param string $value Value to set. |
|
762 | 762 | * @return bool Whether or not the date was set. |
763 | - */ |
|
764 | - public function set_date_created( $value ) { |
|
763 | + */ |
|
764 | + public function set_date_created( $value ) { |
|
765 | 765 | $date = strtotime( $value ); |
766 | 766 | |
767 | 767 | if ( $date ) { |
@@ -774,13 +774,13 @@ discard block |
||
774 | 774 | } |
775 | 775 | |
776 | 776 | /** |
777 | - * Set date when the discount was last modified. |
|
778 | - * |
|
779 | - * @since 1.0.19 |
|
780 | - * @param string $value Value to set. |
|
777 | + * Set date when the discount was last modified. |
|
778 | + * |
|
779 | + * @since 1.0.19 |
|
780 | + * @param string $value Value to set. |
|
781 | 781 | * @return bool Whether or not the date was set. |
782 | - */ |
|
783 | - public function set_date_modified( $value ) { |
|
782 | + */ |
|
783 | + public function set_date_modified( $value ) { |
|
784 | 784 | $date = strtotime( $value ); |
785 | 785 | |
786 | 786 | if ( $date ) { |
@@ -793,324 +793,324 @@ discard block |
||
793 | 793 | } |
794 | 794 | |
795 | 795 | /** |
796 | - * Set the discount name. |
|
797 | - * |
|
798 | - * @since 1.0.19 |
|
799 | - * @param string $value New name. |
|
800 | - */ |
|
801 | - public function set_name( $value ) { |
|
796 | + * Set the discount name. |
|
797 | + * |
|
798 | + * @since 1.0.19 |
|
799 | + * @param string $value New name. |
|
800 | + */ |
|
801 | + public function set_name( $value ) { |
|
802 | 802 | $name = sanitize_text_field( $value ); |
803 | - $this->set_prop( 'name', $name ); |
|
803 | + $this->set_prop( 'name', $name ); |
|
804 | 804 | } |
805 | 805 | |
806 | 806 | /** |
807 | - * Alias of self::set_name(). |
|
808 | - * |
|
809 | - * @since 1.0.19 |
|
810 | - * @param string $value New name. |
|
811 | - */ |
|
812 | - public function set_title( $value ) { |
|
813 | - $this->set_name( $value ); |
|
807 | + * Alias of self::set_name(). |
|
808 | + * |
|
809 | + * @since 1.0.19 |
|
810 | + * @param string $value New name. |
|
811 | + */ |
|
812 | + public function set_title( $value ) { |
|
813 | + $this->set_name( $value ); |
|
814 | 814 | } |
815 | 815 | |
816 | 816 | /** |
817 | - * Set the discount description. |
|
818 | - * |
|
819 | - * @since 1.0.19 |
|
820 | - * @param string $value New description. |
|
821 | - */ |
|
822 | - public function set_description( $value ) { |
|
817 | + * Set the discount description. |
|
818 | + * |
|
819 | + * @since 1.0.19 |
|
820 | + * @param string $value New description. |
|
821 | + */ |
|
822 | + public function set_description( $value ) { |
|
823 | 823 | $description = wp_kses_post( $value ); |
824 | - return $this->set_prop( 'description', $description ); |
|
824 | + return $this->set_prop( 'description', $description ); |
|
825 | 825 | } |
826 | 826 | |
827 | 827 | /** |
828 | - * Alias of self::set_description(). |
|
829 | - * |
|
830 | - * @since 1.0.19 |
|
831 | - * @param string $value New description. |
|
832 | - */ |
|
833 | - public function set_excerpt( $value ) { |
|
834 | - $this->set_description( $value ); |
|
828 | + * Alias of self::set_description(). |
|
829 | + * |
|
830 | + * @since 1.0.19 |
|
831 | + * @param string $value New description. |
|
832 | + */ |
|
833 | + public function set_excerpt( $value ) { |
|
834 | + $this->set_description( $value ); |
|
835 | 835 | } |
836 | 836 | |
837 | 837 | /** |
838 | - * Alias of self::set_description(). |
|
839 | - * |
|
840 | - * @since 1.0.19 |
|
841 | - * @param string $value New description. |
|
842 | - */ |
|
843 | - public function set_summary( $value ) { |
|
844 | - $this->set_description( $value ); |
|
838 | + * Alias of self::set_description(). |
|
839 | + * |
|
840 | + * @since 1.0.19 |
|
841 | + * @param string $value New description. |
|
842 | + */ |
|
843 | + public function set_summary( $value ) { |
|
844 | + $this->set_description( $value ); |
|
845 | 845 | } |
846 | 846 | |
847 | 847 | /** |
848 | - * Set the owner of the discount. |
|
849 | - * |
|
850 | - * @since 1.0.19 |
|
851 | - * @param int $value New author. |
|
852 | - */ |
|
853 | - public function set_author( $value ) { |
|
854 | - $this->set_prop( 'author', (int) $value ); |
|
855 | - } |
|
848 | + * Set the owner of the discount. |
|
849 | + * |
|
850 | + * @since 1.0.19 |
|
851 | + * @param int $value New author. |
|
852 | + */ |
|
853 | + public function set_author( $value ) { |
|
854 | + $this->set_prop( 'author', (int) $value ); |
|
855 | + } |
|
856 | 856 | |
857 | - /** |
|
858 | - * Sets the discount code. |
|
859 | - * |
|
860 | - * @since 1.0.19 |
|
861 | - * @param string $value New discount code. |
|
862 | - */ |
|
863 | - public function set_code( $value ) { |
|
864 | - $code = sanitize_text_field( $value ); |
|
865 | - $this->set_prop( 'code', $code ); |
|
866 | - } |
|
857 | + /** |
|
858 | + * Sets the discount code. |
|
859 | + * |
|
860 | + * @since 1.0.19 |
|
861 | + * @param string $value New discount code. |
|
862 | + */ |
|
863 | + public function set_code( $value ) { |
|
864 | + $code = sanitize_text_field( $value ); |
|
865 | + $this->set_prop( 'code', $code ); |
|
866 | + } |
|
867 | 867 | |
868 | - /** |
|
869 | - * Alias of self::set_code(). |
|
870 | - * |
|
871 | - * @since 1.0.19 |
|
872 | - * @param string $value New discount code. |
|
873 | - */ |
|
874 | - public function set_coupon_code( $value ) { |
|
875 | - $this->set_code( $value ); |
|
876 | - } |
|
868 | + /** |
|
869 | + * Alias of self::set_code(). |
|
870 | + * |
|
871 | + * @since 1.0.19 |
|
872 | + * @param string $value New discount code. |
|
873 | + */ |
|
874 | + public function set_coupon_code( $value ) { |
|
875 | + $this->set_code( $value ); |
|
876 | + } |
|
877 | 877 | |
878 | - /** |
|
879 | - * Alias of self::set_code(). |
|
880 | - * |
|
881 | - * @since 1.0.19 |
|
882 | - * @param string $value New discount code. |
|
883 | - */ |
|
884 | - public function set_discount_code( $value ) { |
|
885 | - $this->set_code( $value ); |
|
886 | - } |
|
878 | + /** |
|
879 | + * Alias of self::set_code(). |
|
880 | + * |
|
881 | + * @since 1.0.19 |
|
882 | + * @param string $value New discount code. |
|
883 | + */ |
|
884 | + public function set_discount_code( $value ) { |
|
885 | + $this->set_code( $value ); |
|
886 | + } |
|
887 | 887 | |
888 | - /** |
|
889 | - * Sets the discount amount. |
|
890 | - * |
|
891 | - * @since 1.0.19 |
|
892 | - * @param float $value New discount code. |
|
893 | - */ |
|
894 | - public function set_amount( $value ) { |
|
895 | - $amount = floatval( wpinv_sanitize_amount( $value ) ); |
|
896 | - $this->set_prop( 'amount', $amount ); |
|
897 | - } |
|
898 | - |
|
899 | - /** |
|
900 | - * Sets the discount's start date. |
|
901 | - * |
|
902 | - * @since 1.0.19 |
|
903 | - * @param float $value New start date. |
|
904 | - */ |
|
905 | - public function set_start( $value ) { |
|
906 | - $date = strtotime( $value ); |
|
888 | + /** |
|
889 | + * Sets the discount amount. |
|
890 | + * |
|
891 | + * @since 1.0.19 |
|
892 | + * @param float $value New discount code. |
|
893 | + */ |
|
894 | + public function set_amount( $value ) { |
|
895 | + $amount = floatval( wpinv_sanitize_amount( $value ) ); |
|
896 | + $this->set_prop( 'amount', $amount ); |
|
897 | + } |
|
907 | 898 | |
908 | - if ( $date ) { |
|
909 | - $this->set_prop( 'start', date( 'Y-m-d H:i', $date ) ); |
|
910 | - return true; |
|
911 | - } |
|
899 | + /** |
|
900 | + * Sets the discount's start date. |
|
901 | + * |
|
902 | + * @since 1.0.19 |
|
903 | + * @param float $value New start date. |
|
904 | + */ |
|
905 | + public function set_start( $value ) { |
|
906 | + $date = strtotime( $value ); |
|
907 | + |
|
908 | + if ( $date ) { |
|
909 | + $this->set_prop( 'start', date( 'Y-m-d H:i', $date ) ); |
|
910 | + return true; |
|
911 | + } |
|
912 | 912 | |
913 | - $this->set_prop( 'start', '' ); |
|
913 | + $this->set_prop( 'start', '' ); |
|
914 | 914 | |
915 | 915 | return false; |
916 | - } |
|
917 | - |
|
918 | - /** |
|
919 | - * Alias of self::set_start(). |
|
920 | - * |
|
921 | - * @since 1.0.19 |
|
922 | - * @param string $value New start date. |
|
923 | - */ |
|
924 | - public function set_start_date( $value ) { |
|
925 | - $this->set_start( $value ); |
|
926 | - } |
|
927 | - |
|
928 | - /** |
|
929 | - * Sets the discount's expiration date. |
|
930 | - * |
|
931 | - * @since 1.0.19 |
|
932 | - * @param float $value New expiration date. |
|
933 | - */ |
|
934 | - public function set_expiration( $value ) { |
|
935 | - $date = strtotime( $value ); |
|
916 | + } |
|
917 | + |
|
918 | + /** |
|
919 | + * Alias of self::set_start(). |
|
920 | + * |
|
921 | + * @since 1.0.19 |
|
922 | + * @param string $value New start date. |
|
923 | + */ |
|
924 | + public function set_start_date( $value ) { |
|
925 | + $this->set_start( $value ); |
|
926 | + } |
|
927 | + |
|
928 | + /** |
|
929 | + * Sets the discount's expiration date. |
|
930 | + * |
|
931 | + * @since 1.0.19 |
|
932 | + * @param float $value New expiration date. |
|
933 | + */ |
|
934 | + public function set_expiration( $value ) { |
|
935 | + $date = strtotime( $value ); |
|
936 | 936 | |
937 | 937 | if ( $date ) { |
938 | 938 | $this->set_prop( 'expiration', date( 'Y-m-d H:i', $date ) ); |
939 | 939 | return true; |
940 | 940 | } |
941 | 941 | |
942 | - $this->set_prop( 'expiration', '' ); |
|
942 | + $this->set_prop( 'expiration', '' ); |
|
943 | 943 | return false; |
944 | - } |
|
945 | - |
|
946 | - /** |
|
947 | - * Alias of self::set_expiration(). |
|
948 | - * |
|
949 | - * @since 1.0.19 |
|
950 | - * @param string $value New expiration date. |
|
951 | - */ |
|
952 | - public function set_expiration_date( $value ) { |
|
953 | - $this->set_expiration( $value ); |
|
954 | - } |
|
955 | - |
|
956 | - /** |
|
957 | - * Alias of self::set_expiration(). |
|
958 | - * |
|
959 | - * @since 1.0.19 |
|
960 | - * @param string $value New expiration date. |
|
961 | - */ |
|
962 | - public function set_end_date( $value ) { |
|
963 | - $this->set_expiration( $value ); |
|
964 | - } |
|
965 | - |
|
966 | - /** |
|
967 | - * Sets the discount type. |
|
968 | - * |
|
969 | - * @since 1.0.19 |
|
970 | - * @param string $value New discount type. |
|
971 | - */ |
|
972 | - public function set_type( $value ) { |
|
973 | - if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) { |
|
974 | - $this->set_prop( 'type', sanitize_text_field( $value ) ); |
|
975 | - } |
|
976 | - } |
|
977 | - |
|
978 | - /** |
|
979 | - * Sets the number of times a discount has been used. |
|
980 | - * |
|
981 | - * @since 1.0.19 |
|
982 | - * @param int $value usage count. |
|
983 | - */ |
|
984 | - public function set_uses( $value ) { |
|
985 | - |
|
986 | - $value = (int) $value; |
|
987 | - |
|
988 | - if ( $value < 0 ) { |
|
989 | - $value = 0; |
|
990 | - } |
|
991 | - |
|
992 | - $this->set_prop( 'uses', (int) $value ); |
|
993 | - } |
|
994 | - |
|
995 | - /** |
|
996 | - * Sets the maximum number of times a discount can be used. |
|
997 | - * |
|
998 | - * @since 1.0.19 |
|
999 | - * @param int $value maximum usage count. |
|
1000 | - */ |
|
1001 | - public function set_max_uses( $value ) { |
|
1002 | - $this->set_prop( 'max_uses', absint( $value ) ); |
|
1003 | - } |
|
1004 | - |
|
1005 | - /** |
|
1006 | - * Sets if this is a single use discount or not. |
|
1007 | - * |
|
1008 | - * @since 1.0.19 |
|
1009 | - * @param int|bool $value is single use. |
|
1010 | - */ |
|
1011 | - public function set_is_single_use( $value ) { |
|
1012 | - $this->set_prop( 'is_single_use', (bool) $value ); |
|
1013 | - } |
|
1014 | - |
|
1015 | - /** |
|
1016 | - * Sets the items that can be used with this discount. |
|
1017 | - * |
|
1018 | - * @since 1.0.19 |
|
1019 | - * @param array $value items. |
|
1020 | - */ |
|
1021 | - public function set_items( $value ) { |
|
1022 | - $this->set_prop( 'items', wpinv_parse_list( $value ) ); |
|
1023 | - } |
|
1024 | - |
|
1025 | - /** |
|
1026 | - * Alias for self::set_items(). |
|
1027 | - * |
|
1028 | - * @since 1.0.19 |
|
1029 | - * @param array $value items. |
|
1030 | - */ |
|
1031 | - public function set_allowed_items( $value ) { |
|
1032 | - $this->set_items( $value ); |
|
1033 | - } |
|
1034 | - |
|
1035 | - /** |
|
1036 | - * Sets the items that can not be used with this discount. |
|
1037 | - * |
|
1038 | - * @since 1.0.19 |
|
1039 | - * @param array $value items. |
|
1040 | - */ |
|
1041 | - public function set_excluded_items( $value ) { |
|
1042 | - $this->set_prop( 'excluded_items', wpinv_parse_list( $value ) ); |
|
1043 | - } |
|
1044 | - |
|
1045 | - /** |
|
1046 | - * Sets if this is a recurring discounts or not. |
|
1047 | - * |
|
1048 | - * @since 1.0.19 |
|
1049 | - * @param int|bool $value is recurring. |
|
1050 | - */ |
|
1051 | - public function set_is_recurring( $value ) { |
|
1052 | - $this->set_prop( 'is_recurring', (bool) $value ); |
|
1053 | - } |
|
1054 | - |
|
1055 | - /** |
|
1056 | - * Sets the minimum total that can not be used with this discount. |
|
1057 | - * |
|
1058 | - * @since 1.0.19 |
|
1059 | - * @param float $value minimum total. |
|
1060 | - */ |
|
1061 | - public function set_min_total( $value ) { |
|
1062 | - $this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) ); |
|
1063 | - } |
|
1064 | - |
|
1065 | - /** |
|
1066 | - * Alias for self::set_min_total(). |
|
1067 | - * |
|
1068 | - * @since 1.0.19 |
|
1069 | - * @param float $value minimum total. |
|
1070 | - */ |
|
1071 | - public function set_minimum_total( $value ) { |
|
1072 | - $this->set_min_total( $value ); |
|
1073 | - } |
|
1074 | - |
|
1075 | - /** |
|
1076 | - * Sets the maximum total that can not be used with this discount. |
|
1077 | - * |
|
1078 | - * @since 1.0.19 |
|
1079 | - * @param float $value maximum total. |
|
1080 | - */ |
|
1081 | - public function set_max_total( $value ) { |
|
1082 | - $this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) ); |
|
1083 | - } |
|
1084 | - |
|
1085 | - /** |
|
1086 | - * Alias for self::set_max_total(). |
|
1087 | - * |
|
1088 | - * @since 1.0.19 |
|
1089 | - * @param float $value maximum total. |
|
1090 | - */ |
|
1091 | - public function set_maximum_total( $value ) { |
|
1092 | - $this->set_max_total( $value ); |
|
1093 | - } |
|
1094 | - |
|
1095 | - /** |
|
1096 | - * @deprecated |
|
1097 | - */ |
|
1098 | - public function refresh(){} |
|
1099 | - |
|
1100 | - /** |
|
1101 | - * @deprecated |
|
1102 | - * |
|
1103 | - */ |
|
1104 | - public function update_status( $status = 'publish' ){ |
|
1105 | - |
|
1106 | - if ( $this->exists() && $this->get_status() != $status ) { |
|
1107 | - $this->set_status( $status ); |
|
1108 | - $this->save(); |
|
1109 | - } |
|
1110 | - |
|
1111 | - } |
|
1112 | - |
|
1113 | - /* |
|
944 | + } |
|
945 | + |
|
946 | + /** |
|
947 | + * Alias of self::set_expiration(). |
|
948 | + * |
|
949 | + * @since 1.0.19 |
|
950 | + * @param string $value New expiration date. |
|
951 | + */ |
|
952 | + public function set_expiration_date( $value ) { |
|
953 | + $this->set_expiration( $value ); |
|
954 | + } |
|
955 | + |
|
956 | + /** |
|
957 | + * Alias of self::set_expiration(). |
|
958 | + * |
|
959 | + * @since 1.0.19 |
|
960 | + * @param string $value New expiration date. |
|
961 | + */ |
|
962 | + public function set_end_date( $value ) { |
|
963 | + $this->set_expiration( $value ); |
|
964 | + } |
|
965 | + |
|
966 | + /** |
|
967 | + * Sets the discount type. |
|
968 | + * |
|
969 | + * @since 1.0.19 |
|
970 | + * @param string $value New discount type. |
|
971 | + */ |
|
972 | + public function set_type( $value ) { |
|
973 | + if ( $value && array_key_exists( sanitize_text_field( $value ), wpinv_get_discount_types() ) ) { |
|
974 | + $this->set_prop( 'type', sanitize_text_field( $value ) ); |
|
975 | + } |
|
976 | + } |
|
977 | + |
|
978 | + /** |
|
979 | + * Sets the number of times a discount has been used. |
|
980 | + * |
|
981 | + * @since 1.0.19 |
|
982 | + * @param int $value usage count. |
|
983 | + */ |
|
984 | + public function set_uses( $value ) { |
|
985 | + |
|
986 | + $value = (int) $value; |
|
987 | + |
|
988 | + if ( $value < 0 ) { |
|
989 | + $value = 0; |
|
990 | + } |
|
991 | + |
|
992 | + $this->set_prop( 'uses', (int) $value ); |
|
993 | + } |
|
994 | + |
|
995 | + /** |
|
996 | + * Sets the maximum number of times a discount can be used. |
|
997 | + * |
|
998 | + * @since 1.0.19 |
|
999 | + * @param int $value maximum usage count. |
|
1000 | + */ |
|
1001 | + public function set_max_uses( $value ) { |
|
1002 | + $this->set_prop( 'max_uses', absint( $value ) ); |
|
1003 | + } |
|
1004 | + |
|
1005 | + /** |
|
1006 | + * Sets if this is a single use discount or not. |
|
1007 | + * |
|
1008 | + * @since 1.0.19 |
|
1009 | + * @param int|bool $value is single use. |
|
1010 | + */ |
|
1011 | + public function set_is_single_use( $value ) { |
|
1012 | + $this->set_prop( 'is_single_use', (bool) $value ); |
|
1013 | + } |
|
1014 | + |
|
1015 | + /** |
|
1016 | + * Sets the items that can be used with this discount. |
|
1017 | + * |
|
1018 | + * @since 1.0.19 |
|
1019 | + * @param array $value items. |
|
1020 | + */ |
|
1021 | + public function set_items( $value ) { |
|
1022 | + $this->set_prop( 'items', wpinv_parse_list( $value ) ); |
|
1023 | + } |
|
1024 | + |
|
1025 | + /** |
|
1026 | + * Alias for self::set_items(). |
|
1027 | + * |
|
1028 | + * @since 1.0.19 |
|
1029 | + * @param array $value items. |
|
1030 | + */ |
|
1031 | + public function set_allowed_items( $value ) { |
|
1032 | + $this->set_items( $value ); |
|
1033 | + } |
|
1034 | + |
|
1035 | + /** |
|
1036 | + * Sets the items that can not be used with this discount. |
|
1037 | + * |
|
1038 | + * @since 1.0.19 |
|
1039 | + * @param array $value items. |
|
1040 | + */ |
|
1041 | + public function set_excluded_items( $value ) { |
|
1042 | + $this->set_prop( 'excluded_items', wpinv_parse_list( $value ) ); |
|
1043 | + } |
|
1044 | + |
|
1045 | + /** |
|
1046 | + * Sets if this is a recurring discounts or not. |
|
1047 | + * |
|
1048 | + * @since 1.0.19 |
|
1049 | + * @param int|bool $value is recurring. |
|
1050 | + */ |
|
1051 | + public function set_is_recurring( $value ) { |
|
1052 | + $this->set_prop( 'is_recurring', (bool) $value ); |
|
1053 | + } |
|
1054 | + |
|
1055 | + /** |
|
1056 | + * Sets the minimum total that can not be used with this discount. |
|
1057 | + * |
|
1058 | + * @since 1.0.19 |
|
1059 | + * @param float $value minimum total. |
|
1060 | + */ |
|
1061 | + public function set_min_total( $value ) { |
|
1062 | + $this->set_prop( 'min_total', (float) wpinv_sanitize_amount( $value ) ); |
|
1063 | + } |
|
1064 | + |
|
1065 | + /** |
|
1066 | + * Alias for self::set_min_total(). |
|
1067 | + * |
|
1068 | + * @since 1.0.19 |
|
1069 | + * @param float $value minimum total. |
|
1070 | + */ |
|
1071 | + public function set_minimum_total( $value ) { |
|
1072 | + $this->set_min_total( $value ); |
|
1073 | + } |
|
1074 | + |
|
1075 | + /** |
|
1076 | + * Sets the maximum total that can not be used with this discount. |
|
1077 | + * |
|
1078 | + * @since 1.0.19 |
|
1079 | + * @param float $value maximum total. |
|
1080 | + */ |
|
1081 | + public function set_max_total( $value ) { |
|
1082 | + $this->set_prop( 'max_total', (float) wpinv_sanitize_amount( $value ) ); |
|
1083 | + } |
|
1084 | + |
|
1085 | + /** |
|
1086 | + * Alias for self::set_max_total(). |
|
1087 | + * |
|
1088 | + * @since 1.0.19 |
|
1089 | + * @param float $value maximum total. |
|
1090 | + */ |
|
1091 | + public function set_maximum_total( $value ) { |
|
1092 | + $this->set_max_total( $value ); |
|
1093 | + } |
|
1094 | + |
|
1095 | + /** |
|
1096 | + * @deprecated |
|
1097 | + */ |
|
1098 | + public function refresh(){} |
|
1099 | + |
|
1100 | + /** |
|
1101 | + * @deprecated |
|
1102 | + * |
|
1103 | + */ |
|
1104 | + public function update_status( $status = 'publish' ){ |
|
1105 | + |
|
1106 | + if ( $this->exists() && $this->get_status() != $status ) { |
|
1107 | + $this->set_status( $status ); |
|
1108 | + $this->save(); |
|
1109 | + } |
|
1110 | + |
|
1111 | + } |
|
1112 | + |
|
1113 | + /* |
|
1114 | 1114 | |-------------------------------------------------------------------------- |
1115 | 1115 | | Conditionals |
1116 | 1116 | |-------------------------------------------------------------------------- |
@@ -1119,263 +1119,263 @@ discard block |
||
1119 | 1119 | | |
1120 | 1120 | */ |
1121 | 1121 | |
1122 | - /** |
|
1123 | - * Checks whether a discount exists in the database or not |
|
1124 | - * |
|
1125 | - * @since 1.0.15 |
|
1126 | - */ |
|
1127 | - public function exists(){ |
|
1128 | - $id = $this->get_id(); |
|
1129 | - return ! empty( $id ); |
|
1130 | - } |
|
1131 | - |
|
1132 | - /** |
|
1133 | - * Checks the discount type. |
|
1134 | - * |
|
1135 | - * |
|
1136 | - * @param string $type the discount type to check against |
|
1137 | - * @since 1.0.15 |
|
1138 | - * @return bool |
|
1139 | - */ |
|
1140 | - public function is_type( $type ) { |
|
1141 | - return $this->get_type() == $type; |
|
1142 | - } |
|
1143 | - |
|
1144 | - /** |
|
1145 | - * Checks whether the discount is published or not |
|
1146 | - * |
|
1147 | - * @since 1.0.15 |
|
1148 | - * @return bool |
|
1149 | - */ |
|
1150 | - public function is_active() { |
|
1151 | - return $this->get_status() == 'publish'; |
|
1152 | - } |
|
1153 | - |
|
1154 | - /** |
|
1155 | - * Checks whether the discount has max uses |
|
1156 | - * |
|
1157 | - * @since 1.0.15 |
|
1158 | - * @return bool |
|
1159 | - */ |
|
1160 | - public function has_limit() { |
|
1161 | - $limit = $this->get_max_uses(); |
|
1162 | - return ! empty( $limit ); |
|
1163 | - } |
|
1164 | - |
|
1165 | - /** |
|
1166 | - * Checks whether the discount has ever been used. |
|
1167 | - * |
|
1168 | - * @since 1.0.15 |
|
1169 | - * @return bool |
|
1170 | - */ |
|
1171 | - public function has_uses() { |
|
1172 | - return $this->get_uses() > 0; |
|
1173 | - } |
|
1174 | - |
|
1175 | - /** |
|
1176 | - * Checks whether the discount is has exided the usage limit or not |
|
1177 | - * |
|
1178 | - * @since 1.0.15 |
|
1179 | - * @return bool |
|
1180 | - */ |
|
1181 | - public function has_exceeded_limit() { |
|
1182 | - |
|
1183 | - if ( ! $this->has_limit() || ! $this->has_uses() ) { |
|
1184 | - $exceeded = false ; |
|
1185 | - } else { |
|
1186 | - $exceeded = (int) $this->get_max_uses() <= $this->get_uses(); |
|
1187 | - } |
|
1188 | - |
|
1189 | - return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->get_id(), $this, $this->get_code() ); |
|
1190 | - } |
|
1191 | - |
|
1192 | - /** |
|
1193 | - * Checks whether the discount has an expiration date. |
|
1194 | - * |
|
1195 | - * @since 1.0.15 |
|
1196 | - * @return bool |
|
1197 | - */ |
|
1198 | - public function has_expiration_date() { |
|
1199 | - $date = $this->get_expiration_date(); |
|
1200 | - return ! empty( $date ); |
|
1201 | - } |
|
1202 | - |
|
1203 | - /** |
|
1204 | - * Checks if the discount is expired |
|
1205 | - * |
|
1206 | - * @since 1.0.15 |
|
1207 | - * @return bool |
|
1208 | - */ |
|
1209 | - public function is_expired() { |
|
1210 | - $expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false; |
|
1211 | - return apply_filters( 'wpinv_is_discount_expired', $expired, $this->get_id(), $this, $this->get_code() ); |
|
1212 | - } |
|
1213 | - |
|
1214 | - /** |
|
1215 | - * Checks whether the discount has a start date. |
|
1216 | - * |
|
1217 | - * @since 1.0.15 |
|
1218 | - * @return bool |
|
1219 | - */ |
|
1220 | - public function has_start_date() { |
|
1221 | - $date = $this->get_start_date(); |
|
1222 | - return ! empty( $date ); |
|
1223 | - } |
|
1224 | - |
|
1225 | - /** |
|
1226 | - * Checks the discount start date. |
|
1227 | - * |
|
1228 | - * @since 1.0.15 |
|
1229 | - * @return bool |
|
1230 | - */ |
|
1231 | - public function has_started() { |
|
1232 | - $started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() ); |
|
1233 | - return apply_filters( 'wpinv_is_discount_started', $started, $this->get_id(), $this, $this->get_code() ); |
|
1234 | - } |
|
1235 | - |
|
1236 | - /** |
|
1237 | - * Checks the discount has allowed items or not. |
|
1238 | - * |
|
1239 | - * @since 1.0.15 |
|
1240 | - * @return bool |
|
1241 | - */ |
|
1242 | - public function has_allowed_items() { |
|
1243 | - $allowed_items = $this->get_allowed_items(); |
|
1244 | - return ! empty( $allowed_items ); |
|
1245 | - } |
|
1246 | - |
|
1247 | - /** |
|
1248 | - * Checks the discount has excluded items or not. |
|
1249 | - * |
|
1250 | - * @since 1.0.15 |
|
1251 | - * @return bool |
|
1252 | - */ |
|
1253 | - public function has_excluded_items() { |
|
1254 | - $excluded_items = $this->get_excluded_items(); |
|
1255 | - return ! empty( $excluded_items ); |
|
1256 | - } |
|
1257 | - |
|
1258 | - /** |
|
1259 | - * Check if a discount is valid for a given item id. |
|
1260 | - * |
|
1261 | - * @param int|int[] $item_ids |
|
1262 | - * @since 1.0.15 |
|
1263 | - * @return boolean |
|
1264 | - */ |
|
1265 | - public function is_valid_for_items( $item_ids ) { |
|
1266 | - |
|
1267 | - $item_ids = wp_parse_id_list( $item_ids ); |
|
1268 | - $included = array_intersect( $item_ids, $this->get_allowed_items() ); |
|
1269 | - $excluded = array_intersect( $item_ids, $this->get_excluded_items() ); |
|
1270 | - |
|
1271 | - if ( $this->has_excluded_items() && ! empty( $excluded ) ) { |
|
1272 | - return false; |
|
1273 | - } |
|
1274 | - |
|
1275 | - if ( $this->has_allowed_items() && empty( $included ) ) { |
|
1276 | - return false; |
|
1277 | - } |
|
1278 | - |
|
1279 | - return true; |
|
1280 | - } |
|
1281 | - |
|
1282 | - /** |
|
1283 | - * Check if a discount is valid for the given amount |
|
1284 | - * |
|
1285 | - * @param float $amount The amount to check against |
|
1286 | - * @since 1.0.15 |
|
1287 | - * @return boolean |
|
1288 | - */ |
|
1289 | - public function is_valid_for_amount( $amount ) { |
|
1290 | - return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount ); |
|
1291 | - } |
|
1292 | - |
|
1293 | - /** |
|
1294 | - * Checks if the minimum amount is set |
|
1295 | - * |
|
1296 | - * @since 1.0.15 |
|
1297 | - * @return boolean |
|
1298 | - */ |
|
1299 | - public function has_minimum_amount() { |
|
1300 | - $minimum = $this->get_minimum_total(); |
|
1301 | - return ! empty( $minimum ); |
|
1302 | - } |
|
1303 | - |
|
1304 | - /** |
|
1305 | - * Checks if the minimum amount is met |
|
1306 | - * |
|
1307 | - * @param float $amount The amount to check against |
|
1308 | - * @since 1.0.15 |
|
1309 | - * @return boolean |
|
1310 | - */ |
|
1311 | - public function is_minimum_amount_met( $amount ) { |
|
1312 | - $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
1313 | - $min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) ); |
|
1314 | - return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->get_id(), $this, $this->get_code(), $amount ); |
|
1315 | - } |
|
1316 | - |
|
1317 | - /** |
|
1318 | - * Checks if the maximum amount is set |
|
1319 | - * |
|
1320 | - * @since 1.0.15 |
|
1321 | - * @return boolean |
|
1322 | - */ |
|
1323 | - public function has_maximum_amount() { |
|
1324 | - $maximum = $this->get_maximum_total(); |
|
1325 | - return ! empty( $maximum ); |
|
1326 | - } |
|
1327 | - |
|
1328 | - /** |
|
1329 | - * Checks if the maximum amount is met |
|
1330 | - * |
|
1331 | - * @param float $amount The amount to check against |
|
1332 | - * @since 1.0.15 |
|
1333 | - * @return boolean |
|
1334 | - */ |
|
1335 | - public function is_maximum_amount_met( $amount ) { |
|
1336 | - $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
1337 | - $max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) ); |
|
1338 | - return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->get_id(), $this, $this->get_code(), $amount ); |
|
1339 | - } |
|
1340 | - |
|
1341 | - /** |
|
1342 | - * Checks if the discount is recurring. |
|
1343 | - * |
|
1344 | - * @since 1.0.15 |
|
1345 | - * @return boolean |
|
1346 | - */ |
|
1347 | - public function is_recurring() { |
|
1348 | - $recurring = $this->get_is_recurring(); |
|
1349 | - return ! empty( $recurring ); |
|
1350 | - } |
|
1351 | - |
|
1352 | - /** |
|
1353 | - * Checks if the discount is single use. |
|
1354 | - * |
|
1355 | - * @since 1.0.15 |
|
1356 | - * @return boolean |
|
1357 | - */ |
|
1358 | - public function is_single_use() { |
|
1359 | - $usage = $this->get_is_single_use(); |
|
1360 | - return ! empty( $usage ); |
|
1361 | - } |
|
1362 | - |
|
1363 | - /** |
|
1364 | - * Check if a discount is valid for the given user |
|
1365 | - * |
|
1366 | - * @param int|string $user |
|
1367 | - * @since 1.0.15 |
|
1368 | - * @return boolean |
|
1369 | - */ |
|
1370 | - public function is_valid_for_user( $user ) { |
|
1371 | - |
|
1372 | - // Ensure that the discount is single use. |
|
1373 | - if ( empty( $user ) || ! $this->is_single_use() ) { |
|
1374 | - return true; |
|
1375 | - } |
|
1376 | - |
|
1377 | - // Prepare the user id. |
|
1378 | - $user_id = 0; |
|
1122 | + /** |
|
1123 | + * Checks whether a discount exists in the database or not |
|
1124 | + * |
|
1125 | + * @since 1.0.15 |
|
1126 | + */ |
|
1127 | + public function exists(){ |
|
1128 | + $id = $this->get_id(); |
|
1129 | + return ! empty( $id ); |
|
1130 | + } |
|
1131 | + |
|
1132 | + /** |
|
1133 | + * Checks the discount type. |
|
1134 | + * |
|
1135 | + * |
|
1136 | + * @param string $type the discount type to check against |
|
1137 | + * @since 1.0.15 |
|
1138 | + * @return bool |
|
1139 | + */ |
|
1140 | + public function is_type( $type ) { |
|
1141 | + return $this->get_type() == $type; |
|
1142 | + } |
|
1143 | + |
|
1144 | + /** |
|
1145 | + * Checks whether the discount is published or not |
|
1146 | + * |
|
1147 | + * @since 1.0.15 |
|
1148 | + * @return bool |
|
1149 | + */ |
|
1150 | + public function is_active() { |
|
1151 | + return $this->get_status() == 'publish'; |
|
1152 | + } |
|
1153 | + |
|
1154 | + /** |
|
1155 | + * Checks whether the discount has max uses |
|
1156 | + * |
|
1157 | + * @since 1.0.15 |
|
1158 | + * @return bool |
|
1159 | + */ |
|
1160 | + public function has_limit() { |
|
1161 | + $limit = $this->get_max_uses(); |
|
1162 | + return ! empty( $limit ); |
|
1163 | + } |
|
1164 | + |
|
1165 | + /** |
|
1166 | + * Checks whether the discount has ever been used. |
|
1167 | + * |
|
1168 | + * @since 1.0.15 |
|
1169 | + * @return bool |
|
1170 | + */ |
|
1171 | + public function has_uses() { |
|
1172 | + return $this->get_uses() > 0; |
|
1173 | + } |
|
1174 | + |
|
1175 | + /** |
|
1176 | + * Checks whether the discount is has exided the usage limit or not |
|
1177 | + * |
|
1178 | + * @since 1.0.15 |
|
1179 | + * @return bool |
|
1180 | + */ |
|
1181 | + public function has_exceeded_limit() { |
|
1182 | + |
|
1183 | + if ( ! $this->has_limit() || ! $this->has_uses() ) { |
|
1184 | + $exceeded = false ; |
|
1185 | + } else { |
|
1186 | + $exceeded = (int) $this->get_max_uses() <= $this->get_uses(); |
|
1187 | + } |
|
1188 | + |
|
1189 | + return apply_filters( 'wpinv_is_discount_maxed_out', $exceeded, $this->get_id(), $this, $this->get_code() ); |
|
1190 | + } |
|
1191 | + |
|
1192 | + /** |
|
1193 | + * Checks whether the discount has an expiration date. |
|
1194 | + * |
|
1195 | + * @since 1.0.15 |
|
1196 | + * @return bool |
|
1197 | + */ |
|
1198 | + public function has_expiration_date() { |
|
1199 | + $date = $this->get_expiration_date(); |
|
1200 | + return ! empty( $date ); |
|
1201 | + } |
|
1202 | + |
|
1203 | + /** |
|
1204 | + * Checks if the discount is expired |
|
1205 | + * |
|
1206 | + * @since 1.0.15 |
|
1207 | + * @return bool |
|
1208 | + */ |
|
1209 | + public function is_expired() { |
|
1210 | + $expired = $this->has_expiration_date() ? current_time( 'timestamp' ) > strtotime( $this->get_expiration_date() ) : false; |
|
1211 | + return apply_filters( 'wpinv_is_discount_expired', $expired, $this->get_id(), $this, $this->get_code() ); |
|
1212 | + } |
|
1213 | + |
|
1214 | + /** |
|
1215 | + * Checks whether the discount has a start date. |
|
1216 | + * |
|
1217 | + * @since 1.0.15 |
|
1218 | + * @return bool |
|
1219 | + */ |
|
1220 | + public function has_start_date() { |
|
1221 | + $date = $this->get_start_date(); |
|
1222 | + return ! empty( $date ); |
|
1223 | + } |
|
1224 | + |
|
1225 | + /** |
|
1226 | + * Checks the discount start date. |
|
1227 | + * |
|
1228 | + * @since 1.0.15 |
|
1229 | + * @return bool |
|
1230 | + */ |
|
1231 | + public function has_started() { |
|
1232 | + $started = $this->has_start_date() ? true : current_time( 'timestamp' ) > strtotime( $this->get_start_date() ); |
|
1233 | + return apply_filters( 'wpinv_is_discount_started', $started, $this->get_id(), $this, $this->get_code() ); |
|
1234 | + } |
|
1235 | + |
|
1236 | + /** |
|
1237 | + * Checks the discount has allowed items or not. |
|
1238 | + * |
|
1239 | + * @since 1.0.15 |
|
1240 | + * @return bool |
|
1241 | + */ |
|
1242 | + public function has_allowed_items() { |
|
1243 | + $allowed_items = $this->get_allowed_items(); |
|
1244 | + return ! empty( $allowed_items ); |
|
1245 | + } |
|
1246 | + |
|
1247 | + /** |
|
1248 | + * Checks the discount has excluded items or not. |
|
1249 | + * |
|
1250 | + * @since 1.0.15 |
|
1251 | + * @return bool |
|
1252 | + */ |
|
1253 | + public function has_excluded_items() { |
|
1254 | + $excluded_items = $this->get_excluded_items(); |
|
1255 | + return ! empty( $excluded_items ); |
|
1256 | + } |
|
1257 | + |
|
1258 | + /** |
|
1259 | + * Check if a discount is valid for a given item id. |
|
1260 | + * |
|
1261 | + * @param int|int[] $item_ids |
|
1262 | + * @since 1.0.15 |
|
1263 | + * @return boolean |
|
1264 | + */ |
|
1265 | + public function is_valid_for_items( $item_ids ) { |
|
1266 | + |
|
1267 | + $item_ids = wp_parse_id_list( $item_ids ); |
|
1268 | + $included = array_intersect( $item_ids, $this->get_allowed_items() ); |
|
1269 | + $excluded = array_intersect( $item_ids, $this->get_excluded_items() ); |
|
1270 | + |
|
1271 | + if ( $this->has_excluded_items() && ! empty( $excluded ) ) { |
|
1272 | + return false; |
|
1273 | + } |
|
1274 | + |
|
1275 | + if ( $this->has_allowed_items() && empty( $included ) ) { |
|
1276 | + return false; |
|
1277 | + } |
|
1278 | + |
|
1279 | + return true; |
|
1280 | + } |
|
1281 | + |
|
1282 | + /** |
|
1283 | + * Check if a discount is valid for the given amount |
|
1284 | + * |
|
1285 | + * @param float $amount The amount to check against |
|
1286 | + * @since 1.0.15 |
|
1287 | + * @return boolean |
|
1288 | + */ |
|
1289 | + public function is_valid_for_amount( $amount ) { |
|
1290 | + return $this->is_minimum_amount_met( $amount ) && $this->is_maximum_amount_met( $amount ); |
|
1291 | + } |
|
1292 | + |
|
1293 | + /** |
|
1294 | + * Checks if the minimum amount is set |
|
1295 | + * |
|
1296 | + * @since 1.0.15 |
|
1297 | + * @return boolean |
|
1298 | + */ |
|
1299 | + public function has_minimum_amount() { |
|
1300 | + $minimum = $this->get_minimum_total(); |
|
1301 | + return ! empty( $minimum ); |
|
1302 | + } |
|
1303 | + |
|
1304 | + /** |
|
1305 | + * Checks if the minimum amount is met |
|
1306 | + * |
|
1307 | + * @param float $amount The amount to check against |
|
1308 | + * @since 1.0.15 |
|
1309 | + * @return boolean |
|
1310 | + */ |
|
1311 | + public function is_minimum_amount_met( $amount ) { |
|
1312 | + $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
1313 | + $min_met= ! ( $this->has_minimum_amount() && $amount < floatval( wpinv_sanitize_amount( $this->get_minimum_total() ) ) ); |
|
1314 | + return apply_filters( 'wpinv_is_discount_min_met', $min_met, $this->get_id(), $this, $this->get_code(), $amount ); |
|
1315 | + } |
|
1316 | + |
|
1317 | + /** |
|
1318 | + * Checks if the maximum amount is set |
|
1319 | + * |
|
1320 | + * @since 1.0.15 |
|
1321 | + * @return boolean |
|
1322 | + */ |
|
1323 | + public function has_maximum_amount() { |
|
1324 | + $maximum = $this->get_maximum_total(); |
|
1325 | + return ! empty( $maximum ); |
|
1326 | + } |
|
1327 | + |
|
1328 | + /** |
|
1329 | + * Checks if the maximum amount is met |
|
1330 | + * |
|
1331 | + * @param float $amount The amount to check against |
|
1332 | + * @since 1.0.15 |
|
1333 | + * @return boolean |
|
1334 | + */ |
|
1335 | + public function is_maximum_amount_met( $amount ) { |
|
1336 | + $amount = floatval( wpinv_sanitize_amount( $amount ) ); |
|
1337 | + $max_met= ! ( $this->has_maximum_amount() && $amount > floatval( wpinv_sanitize_amount( $this->get_maximum_total() ) ) ); |
|
1338 | + return apply_filters( 'wpinv_is_discount_max_met', $max_met, $this->get_id(), $this, $this->get_code(), $amount ); |
|
1339 | + } |
|
1340 | + |
|
1341 | + /** |
|
1342 | + * Checks if the discount is recurring. |
|
1343 | + * |
|
1344 | + * @since 1.0.15 |
|
1345 | + * @return boolean |
|
1346 | + */ |
|
1347 | + public function is_recurring() { |
|
1348 | + $recurring = $this->get_is_recurring(); |
|
1349 | + return ! empty( $recurring ); |
|
1350 | + } |
|
1351 | + |
|
1352 | + /** |
|
1353 | + * Checks if the discount is single use. |
|
1354 | + * |
|
1355 | + * @since 1.0.15 |
|
1356 | + * @return boolean |
|
1357 | + */ |
|
1358 | + public function is_single_use() { |
|
1359 | + $usage = $this->get_is_single_use(); |
|
1360 | + return ! empty( $usage ); |
|
1361 | + } |
|
1362 | + |
|
1363 | + /** |
|
1364 | + * Check if a discount is valid for the given user |
|
1365 | + * |
|
1366 | + * @param int|string $user |
|
1367 | + * @since 1.0.15 |
|
1368 | + * @return boolean |
|
1369 | + */ |
|
1370 | + public function is_valid_for_user( $user ) { |
|
1371 | + |
|
1372 | + // Ensure that the discount is single use. |
|
1373 | + if ( empty( $user ) || ! $this->is_single_use() ) { |
|
1374 | + return true; |
|
1375 | + } |
|
1376 | + |
|
1377 | + // Prepare the user id. |
|
1378 | + $user_id = 0; |
|
1379 | 1379 | if ( is_numeric( $user ) ) { |
1380 | 1380 | $user_id = absint( $user ); |
1381 | 1381 | } else if ( is_email( $user ) && $user_data = get_user_by( 'email', $user ) ) { |
@@ -1384,117 +1384,117 @@ discard block |
||
1384 | 1384 | $user_id = $user_data->ID; |
1385 | 1385 | } |
1386 | 1386 | |
1387 | - // Ensure that we have a user. |
|
1388 | - if ( empty( $user_id ) ) { |
|
1389 | - return true; |
|
1390 | - } |
|
1387 | + // Ensure that we have a user. |
|
1388 | + if ( empty( $user_id ) ) { |
|
1389 | + return true; |
|
1390 | + } |
|
1391 | 1391 | |
1392 | - // Get all payments with matching user id. |
|
1392 | + // Get all payments with matching user id. |
|
1393 | 1393 | $payments = wpinv_get_invoices( array( 'user' => $user_id, 'limit' => false, 'paginate' => false ) ); |
1394 | - $code = strtolower( $this->get_code() ); |
|
1395 | - |
|
1396 | - // For each payment... |
|
1397 | - foreach ( $payments as $payment ) { |
|
1398 | - |
|
1399 | - // Only check for paid invoices. |
|
1400 | - if ( $payment->is_paid() && strtolower( $payment->get_discount_code() ) == $code ) { |
|
1401 | - return false; |
|
1402 | - } |
|
1403 | - |
|
1404 | - } |
|
1405 | - |
|
1406 | - return true; |
|
1407 | - } |
|
1408 | - |
|
1409 | - /** |
|
1410 | - * Deletes the discount from the database |
|
1411 | - * |
|
1412 | - * @since 1.0.15 |
|
1413 | - * @return boolean |
|
1414 | - */ |
|
1415 | - public function remove() { |
|
1416 | - return $this->delete(); |
|
1417 | - } |
|
1418 | - |
|
1419 | - /** |
|
1420 | - * Increases a discount's usage. |
|
1421 | - * |
|
1422 | - * @since 1.0.15 |
|
1423 | - * @param int $by The number of usages to increas by. |
|
1424 | - * @return int |
|
1425 | - */ |
|
1426 | - public function increase_usage( $by = 1 ) { |
|
1427 | - |
|
1428 | - // Abort if zero. |
|
1429 | - if ( empty( $by ) ) { |
|
1430 | - return; |
|
1431 | - } |
|
1432 | - |
|
1433 | - // Increase the usage. |
|
1434 | - $this->set_uses( $this->get_uses() + (int) $by ); |
|
1435 | - |
|
1436 | - // Save the discount. |
|
1437 | - $this->save(); |
|
1438 | - |
|
1439 | - // Fire relevant hooks. |
|
1440 | - if( (int) $by > 0 ) { |
|
1441 | - do_action( 'wpinv_discount_increase_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) ); |
|
1442 | - } else { |
|
1443 | - do_action( 'wpinv_discount_decrease_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) ); |
|
1444 | - } |
|
1445 | - |
|
1446 | - // Return the number of times the discount has been used. |
|
1447 | - return $this->get_uses(); |
|
1448 | - } |
|
1449 | - |
|
1450 | - /** |
|
1451 | - * Alias of self::__toString() |
|
1452 | - * |
|
1453 | - * @since 1.0.15 |
|
1454 | - * @return string|false |
|
1455 | - */ |
|
1456 | - public function get_data_as_json() { |
|
1457 | - return $this->__toString(); |
|
1458 | - } |
|
1459 | - |
|
1460 | - /** |
|
1461 | - * Returns a discount's discounted amount. |
|
1462 | - * |
|
1463 | - * @since 1.0.15 |
|
1464 | - * @param float $amount |
|
1465 | - * @return float |
|
1466 | - */ |
|
1467 | - public function get_discounted_amount( $amount ) { |
|
1468 | - |
|
1469 | - // Convert amount to float. |
|
1470 | - $amount = (float) $amount; |
|
1471 | - |
|
1472 | - // Get discount amount. |
|
1473 | - $discount_amount = $this->get_amount(); |
|
1474 | - |
|
1475 | - if ( empty( $discount_amount ) ) { |
|
1476 | - return 0; |
|
1477 | - } |
|
1478 | - |
|
1479 | - // Format the amount. |
|
1480 | - $discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) ); |
|
1394 | + $code = strtolower( $this->get_code() ); |
|
1395 | + |
|
1396 | + // For each payment... |
|
1397 | + foreach ( $payments as $payment ) { |
|
1398 | + |
|
1399 | + // Only check for paid invoices. |
|
1400 | + if ( $payment->is_paid() && strtolower( $payment->get_discount_code() ) == $code ) { |
|
1401 | + return false; |
|
1402 | + } |
|
1403 | + |
|
1404 | + } |
|
1405 | + |
|
1406 | + return true; |
|
1407 | + } |
|
1408 | + |
|
1409 | + /** |
|
1410 | + * Deletes the discount from the database |
|
1411 | + * |
|
1412 | + * @since 1.0.15 |
|
1413 | + * @return boolean |
|
1414 | + */ |
|
1415 | + public function remove() { |
|
1416 | + return $this->delete(); |
|
1417 | + } |
|
1418 | + |
|
1419 | + /** |
|
1420 | + * Increases a discount's usage. |
|
1421 | + * |
|
1422 | + * @since 1.0.15 |
|
1423 | + * @param int $by The number of usages to increas by. |
|
1424 | + * @return int |
|
1425 | + */ |
|
1426 | + public function increase_usage( $by = 1 ) { |
|
1427 | + |
|
1428 | + // Abort if zero. |
|
1429 | + if ( empty( $by ) ) { |
|
1430 | + return; |
|
1431 | + } |
|
1432 | + |
|
1433 | + // Increase the usage. |
|
1434 | + $this->set_uses( $this->get_uses() + (int) $by ); |
|
1435 | + |
|
1436 | + // Save the discount. |
|
1437 | + $this->save(); |
|
1438 | + |
|
1439 | + // Fire relevant hooks. |
|
1440 | + if( (int) $by > 0 ) { |
|
1441 | + do_action( 'wpinv_discount_increase_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) ); |
|
1442 | + } else { |
|
1443 | + do_action( 'wpinv_discount_decrease_use_count', $this->get_uses(), $this->get_id(), $this->get_code(), absint( $by ) ); |
|
1444 | + } |
|
1445 | + |
|
1446 | + // Return the number of times the discount has been used. |
|
1447 | + return $this->get_uses(); |
|
1448 | + } |
|
1449 | + |
|
1450 | + /** |
|
1451 | + * Alias of self::__toString() |
|
1452 | + * |
|
1453 | + * @since 1.0.15 |
|
1454 | + * @return string|false |
|
1455 | + */ |
|
1456 | + public function get_data_as_json() { |
|
1457 | + return $this->__toString(); |
|
1458 | + } |
|
1459 | + |
|
1460 | + /** |
|
1461 | + * Returns a discount's discounted amount. |
|
1462 | + * |
|
1463 | + * @since 1.0.15 |
|
1464 | + * @param float $amount |
|
1465 | + * @return float |
|
1466 | + */ |
|
1467 | + public function get_discounted_amount( $amount ) { |
|
1468 | + |
|
1469 | + // Convert amount to float. |
|
1470 | + $amount = (float) $amount; |
|
1471 | + |
|
1472 | + // Get discount amount. |
|
1473 | + $discount_amount = $this->get_amount(); |
|
1474 | + |
|
1475 | + if ( empty( $discount_amount ) ) { |
|
1476 | + return 0; |
|
1477 | + } |
|
1478 | + |
|
1479 | + // Format the amount. |
|
1480 | + $discount_amount = floatval( wpinv_sanitize_amount( $discount_amount ) ); |
|
1481 | 1481 | |
1482 | - // If this is a percentage discount. |
|
1483 | - if ( $this->is_type( 'percent' ) ) { |
|
1482 | + // If this is a percentage discount. |
|
1483 | + if ( $this->is_type( 'percent' ) ) { |
|
1484 | 1484 | $discount_amount = $amount * ( $discount_amount / 100 ); |
1485 | - } |
|
1485 | + } |
|
1486 | 1486 | |
1487 | - // Discount can not be less than zero... |
|
1488 | - if ( $discount_amount < 0 ) { |
|
1489 | - $discount_amount = 0; |
|
1490 | - } |
|
1487 | + // Discount can not be less than zero... |
|
1488 | + if ( $discount_amount < 0 ) { |
|
1489 | + $discount_amount = 0; |
|
1490 | + } |
|
1491 | 1491 | |
1492 | - // ... or more than the amount. |
|
1493 | - if ( $discount_amount > $amount ) { |
|
1494 | - $discount_amount = $amount; |
|
1495 | - } |
|
1492 | + // ... or more than the amount. |
|
1493 | + if ( $discount_amount > $amount ) { |
|
1494 | + $discount_amount = $amount; |
|
1495 | + } |
|
1496 | 1496 | |
1497 | - return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this ); |
|
1498 | - } |
|
1497 | + return apply_filters( 'wpinv_discount_total_discount_amount', $discount_amount, $amount, $this ); |
|
1498 | + } |
|
1499 | 1499 | |
1500 | 1500 | } |