@@ -799,7 +799,7 @@ |
||
799 | 799 | case 'trial_interval': |
800 | 800 | $value = absint( $value ); |
801 | 801 | break; |
802 | - case 'editable': |
|
802 | + case 'editable': |
|
803 | 803 | $value = (int) $value; |
804 | 804 | break; |
805 | 805 | } |
@@ -1,19 +1,19 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | // Exit if accessed directly |
3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
3 | +if (!defined('ABSPATH')) exit; |
|
4 | 4 | |
5 | -function wpinv_get_item_by( $field = '', $value = '', $type = '' ) { |
|
6 | - if( empty( $field ) || empty( $value ) ) { |
|
5 | +function wpinv_get_item_by($field = '', $value = '', $type = '') { |
|
6 | + if (empty($field) || empty($value)) { |
|
7 | 7 | return false; |
8 | 8 | } |
9 | 9 | |
10 | 10 | $posts = array(); |
11 | 11 | |
12 | - switch( strtolower( $field ) ) { |
|
12 | + switch (strtolower($field)) { |
|
13 | 13 | case 'id': |
14 | - $item = new WPInv_Item( $value ); |
|
14 | + $item = new WPInv_Item($value); |
|
15 | 15 | |
16 | - if ( !empty( $item ) && $item->post_type == 'wpi_item' ) { |
|
16 | + if (!empty($item) && $item->post_type == 'wpi_item') { |
|
17 | 17 | return $item; |
18 | 18 | } |
19 | 19 | return false; |
@@ -22,16 +22,16 @@ discard block |
||
22 | 22 | |
23 | 23 | case 'slug': |
24 | 24 | case 'name': |
25 | - $posts = get_posts( array( |
|
25 | + $posts = get_posts(array( |
|
26 | 26 | 'post_type' => 'wpi_item', |
27 | 27 | 'name' => $value, |
28 | 28 | 'posts_per_page' => 1, |
29 | 29 | 'post_status' => 'any' |
30 | - ) ); |
|
30 | + )); |
|
31 | 31 | |
32 | 32 | break; |
33 | 33 | case 'custom_id': |
34 | - if ( empty( $value ) || empty( $type ) ) { |
|
34 | + if (empty($value) || empty($type)) { |
|
35 | 35 | return false; |
36 | 36 | } |
37 | 37 | |
@@ -51,10 +51,10 @@ discard block |
||
51 | 51 | 'post_status' => 'any', |
52 | 52 | 'orderby' => 'ID', |
53 | 53 | 'order' => 'ASC', |
54 | - 'meta_query' => array( $meta_query ) |
|
54 | + 'meta_query' => array($meta_query) |
|
55 | 55 | ); |
56 | 56 | |
57 | - $posts = get_posts( $args ); |
|
57 | + $posts = get_posts($args); |
|
58 | 58 | |
59 | 59 | break; |
60 | 60 | |
@@ -62,10 +62,10 @@ discard block |
||
62 | 62 | return false; |
63 | 63 | } |
64 | 64 | |
65 | - if ( !empty( $posts[0] ) ) { |
|
66 | - $item = new WPInv_Item( $posts[0]->ID ); |
|
65 | + if (!empty($posts[0])) { |
|
66 | + $item = new WPInv_Item($posts[0]->ID); |
|
67 | 67 | |
68 | - if ( !empty( $item ) && $item->post_type == 'wpi_item' ) { |
|
68 | + if (!empty($item) && $item->post_type == 'wpi_item') { |
|
69 | 69 | return $item; |
70 | 70 | } |
71 | 71 | } |
@@ -73,10 +73,10 @@ discard block |
||
73 | 73 | return false; |
74 | 74 | } |
75 | 75 | |
76 | -function wpinv_get_item( $item = 0 ) { |
|
77 | - if ( is_numeric( $item ) ) { |
|
78 | - $item = get_post( $item ); |
|
79 | - if ( ! $item || 'wpi_item' !== $item->post_type ) |
|
76 | +function wpinv_get_item($item = 0) { |
|
77 | + if (is_numeric($item)) { |
|
78 | + $item = get_post($item); |
|
79 | + if (!$item || 'wpi_item' !== $item->post_type) |
|
80 | 80 | return null; |
81 | 81 | return $item; |
82 | 82 | } |
@@ -89,150 +89,150 @@ discard block |
||
89 | 89 | |
90 | 90 | $item = get_posts($args); |
91 | 91 | |
92 | - if ( $item ) { |
|
92 | + if ($item) { |
|
93 | 93 | return $item[0]; |
94 | 94 | } |
95 | 95 | |
96 | 96 | return null; |
97 | 97 | } |
98 | 98 | |
99 | -function wpinv_is_free_item( $item_id = 0 ) { |
|
100 | - if( empty( $item_id ) ) { |
|
99 | +function wpinv_is_free_item($item_id = 0) { |
|
100 | + if (empty($item_id)) { |
|
101 | 101 | return false; |
102 | 102 | } |
103 | 103 | |
104 | - $item = new WPInv_Item( $item_id ); |
|
104 | + $item = new WPInv_Item($item_id); |
|
105 | 105 | |
106 | 106 | return $item->is_free(); |
107 | 107 | } |
108 | 108 | |
109 | -function wpinv_item_is_editable( $item = 0 ) { |
|
110 | - if ( !empty( $item ) && is_a( $item, 'WP_Post' ) ) { |
|
109 | +function wpinv_item_is_editable($item = 0) { |
|
110 | + if (!empty($item) && is_a($item, 'WP_Post')) { |
|
111 | 111 | $item = $item->ID; |
112 | 112 | } |
113 | 113 | |
114 | - if ( empty( $item ) ) { |
|
114 | + if (empty($item)) { |
|
115 | 115 | return true; |
116 | 116 | } |
117 | 117 | |
118 | - $item = new WPInv_Item( $item ); |
|
118 | + $item = new WPInv_Item($item); |
|
119 | 119 | |
120 | - return (bool) $item->is_editable(); |
|
120 | + return (bool)$item->is_editable(); |
|
121 | 121 | } |
122 | 122 | |
123 | -function wpinv_get_item_price( $item_id = 0 ) { |
|
124 | - if( empty( $item_id ) ) { |
|
123 | +function wpinv_get_item_price($item_id = 0) { |
|
124 | + if (empty($item_id)) { |
|
125 | 125 | return false; |
126 | 126 | } |
127 | 127 | |
128 | - $item = new WPInv_Item( $item_id ); |
|
128 | + $item = new WPInv_Item($item_id); |
|
129 | 129 | |
130 | 130 | return $item->get_price(); |
131 | 131 | } |
132 | 132 | |
133 | -function wpinv_is_recurring_item( $item_id = 0 ) { |
|
134 | - if( empty( $item_id ) ) { |
|
133 | +function wpinv_is_recurring_item($item_id = 0) { |
|
134 | + if (empty($item_id)) { |
|
135 | 135 | return false; |
136 | 136 | } |
137 | 137 | |
138 | - $item = new WPInv_Item( $item_id ); |
|
138 | + $item = new WPInv_Item($item_id); |
|
139 | 139 | |
140 | 140 | return $item->is_recurring(); |
141 | 141 | } |
142 | 142 | |
143 | -function wpinv_item_price( $item_id = 0 ) { |
|
144 | - if( empty( $item_id ) ) { |
|
143 | +function wpinv_item_price($item_id = 0) { |
|
144 | + if (empty($item_id)) { |
|
145 | 145 | return false; |
146 | 146 | } |
147 | 147 | |
148 | - $price = wpinv_get_item_price( $item_id ); |
|
149 | - $price = wpinv_price( wpinv_format_amount( $price ) ); |
|
148 | + $price = wpinv_get_item_price($item_id); |
|
149 | + $price = wpinv_price(wpinv_format_amount($price)); |
|
150 | 150 | |
151 | - return apply_filters( 'wpinv_item_price', $price, $item_id ); |
|
151 | + return apply_filters('wpinv_item_price', $price, $item_id); |
|
152 | 152 | } |
153 | 153 | |
154 | -function wpinv_item_show_price( $item_id = 0, $echo = true ) { |
|
155 | - if ( empty( $item_id ) ) { |
|
154 | +function wpinv_item_show_price($item_id = 0, $echo = true) { |
|
155 | + if (empty($item_id)) { |
|
156 | 156 | $item_id = get_the_ID(); |
157 | 157 | } |
158 | 158 | |
159 | - $price = wpinv_item_price( $item_id ); |
|
159 | + $price = wpinv_item_price($item_id); |
|
160 | 160 | |
161 | - $price = apply_filters( 'wpinv_item_price', wpinv_sanitize_amount( $price ), $item_id ); |
|
161 | + $price = apply_filters('wpinv_item_price', wpinv_sanitize_amount($price), $item_id); |
|
162 | 162 | $formatted_price = '<span class="wpinv_price" id="wpinv_item_' . $item_id . '">' . $price . '</span>'; |
163 | - $formatted_price = apply_filters( 'wpinv_item_price_after_html', $formatted_price, $item_id, $price ); |
|
163 | + $formatted_price = apply_filters('wpinv_item_price_after_html', $formatted_price, $item_id, $price); |
|
164 | 164 | |
165 | - if ( $echo ) { |
|
165 | + if ($echo) { |
|
166 | 166 | echo $formatted_price; |
167 | 167 | } else { |
168 | 168 | return $formatted_price; |
169 | 169 | } |
170 | 170 | } |
171 | 171 | |
172 | -function wpinv_get_item_final_price( $item_id = 0, $amount_override = null ) { |
|
173 | - if ( is_null( $amount_override ) ) { |
|
174 | - $original_price = get_post_meta( $item_id, '_wpinv_price', true ); |
|
172 | +function wpinv_get_item_final_price($item_id = 0, $amount_override = null) { |
|
173 | + if (is_null($amount_override)) { |
|
174 | + $original_price = get_post_meta($item_id, '_wpinv_price', true); |
|
175 | 175 | } else { |
176 | 176 | $original_price = $amount_override; |
177 | 177 | } |
178 | 178 | |
179 | 179 | $price = $original_price; |
180 | 180 | |
181 | - return apply_filters( 'wpinv_get_item_final_price', $price, $item_id ); |
|
181 | + return apply_filters('wpinv_get_item_final_price', $price, $item_id); |
|
182 | 182 | } |
183 | 183 | |
184 | -function wpinv_item_custom_singular_name( $item_id ) { |
|
185 | - if( empty( $item_id ) ) { |
|
184 | +function wpinv_item_custom_singular_name($item_id) { |
|
185 | + if (empty($item_id)) { |
|
186 | 186 | return false; |
187 | 187 | } |
188 | 188 | |
189 | - $item = new WPInv_Item( $item_id ); |
|
189 | + $item = new WPInv_Item($item_id); |
|
190 | 190 | |
191 | 191 | return $item->get_custom_singular_name(); |
192 | 192 | } |
193 | 193 | |
194 | 194 | function wpinv_get_item_types() { |
195 | 195 | $item_types = array( |
196 | - 'custom' => __( 'Standard', 'invoicing' ), |
|
197 | - 'fee' => __( 'Fee', 'invoicing' ), |
|
196 | + 'custom' => __('Standard', 'invoicing'), |
|
197 | + 'fee' => __('Fee', 'invoicing'), |
|
198 | 198 | ); |
199 | - return apply_filters( 'wpinv_get_item_types', $item_types ); |
|
199 | + return apply_filters('wpinv_get_item_types', $item_types); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | function wpinv_item_types() { |
203 | 203 | $item_types = wpinv_get_item_types(); |
204 | 204 | |
205 | - return ( !empty( $item_types ) ? array_keys( $item_types ) : array() ); |
|
205 | + return (!empty($item_types) ? array_keys($item_types) : array()); |
|
206 | 206 | } |
207 | 207 | |
208 | -function wpinv_get_item_type( $item_id ) { |
|
209 | - if( empty( $item_id ) ) { |
|
208 | +function wpinv_get_item_type($item_id) { |
|
209 | + if (empty($item_id)) { |
|
210 | 210 | return false; |
211 | 211 | } |
212 | 212 | |
213 | - $item = new WPInv_Item( $item_id ); |
|
213 | + $item = new WPInv_Item($item_id); |
|
214 | 214 | |
215 | 215 | return $item->get_type(); |
216 | 216 | } |
217 | 217 | |
218 | -function wpinv_item_type( $item_id ) { |
|
218 | +function wpinv_item_type($item_id) { |
|
219 | 219 | $item_types = wpinv_get_item_types(); |
220 | 220 | |
221 | - $item_type = wpinv_get_item_type( $item_id ); |
|
221 | + $item_type = wpinv_get_item_type($item_id); |
|
222 | 222 | |
223 | - if ( empty( $item_type ) ) { |
|
223 | + if (empty($item_type)) { |
|
224 | 224 | $item_type = '-'; |
225 | 225 | } |
226 | 226 | |
227 | - $item_type = isset( $item_types[$item_type] ) ? $item_types[$item_type] : __( $item_type, 'invoicing' ); |
|
227 | + $item_type = isset($item_types[$item_type]) ? $item_types[$item_type] : __($item_type, 'invoicing'); |
|
228 | 228 | |
229 | - return apply_filters( 'wpinv_item_type', $item_type, $item_id ); |
|
229 | + return apply_filters('wpinv_item_type', $item_type, $item_id); |
|
230 | 230 | } |
231 | 231 | |
232 | -function wpinv_record_item_in_log( $item_id = 0, $file_id, $user_info, $ip, $invoice_id ) { |
|
232 | +function wpinv_record_item_in_log($item_id = 0, $file_id, $user_info, $ip, $invoice_id) { |
|
233 | 233 | global $wpinv_logs; |
234 | 234 | |
235 | - if ( empty( $wpinv_logs ) ) { |
|
235 | + if (empty($wpinv_logs)) { |
|
236 | 236 | return false; |
237 | 237 | } |
238 | 238 | |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | 'log_type' => 'wpi_item' |
242 | 242 | ); |
243 | 243 | |
244 | - $user_id = isset( $user_info['user_id'] ) ? $user_info['user_id'] : (int) -1; |
|
244 | + $user_id = isset($user_info['user_id']) ? $user_info['user_id'] : (int) -1; |
|
245 | 245 | |
246 | 246 | $log_meta = array( |
247 | 247 | 'user_info' => $user_info, |
@@ -251,253 +251,253 @@ discard block |
||
251 | 251 | 'invoice_id'=> $invoice_id, |
252 | 252 | ); |
253 | 253 | |
254 | - $wpinv_logs->insert_log( $log_data, $log_meta ); |
|
254 | + $wpinv_logs->insert_log($log_data, $log_meta); |
|
255 | 255 | } |
256 | 256 | |
257 | -function wpinv_remove_item_logs_on_delete( $item_id = 0 ) { |
|
258 | - if ( 'wpi_item' !== get_post_type( $item_id ) ) |
|
257 | +function wpinv_remove_item_logs_on_delete($item_id = 0) { |
|
258 | + if ('wpi_item' !== get_post_type($item_id)) |
|
259 | 259 | return; |
260 | 260 | |
261 | 261 | global $wpinv_logs; |
262 | 262 | |
263 | - if ( empty( $wpinv_logs ) ) { |
|
263 | + if (empty($wpinv_logs)) { |
|
264 | 264 | return false; |
265 | 265 | } |
266 | 266 | |
267 | 267 | // Remove all log entries related to this item |
268 | - $wpinv_logs->delete_logs( $item_id ); |
|
268 | + $wpinv_logs->delete_logs($item_id); |
|
269 | 269 | } |
270 | -add_action( 'delete_post', 'wpinv_remove_item_logs_on_delete' ); |
|
270 | +add_action('delete_post', 'wpinv_remove_item_logs_on_delete'); |
|
271 | 271 | |
272 | -function wpinv_get_random_item( $post_ids = true ) { |
|
273 | - wpinv_get_random_items( 1, $post_ids ); |
|
272 | +function wpinv_get_random_item($post_ids = true) { |
|
273 | + wpinv_get_random_items(1, $post_ids); |
|
274 | 274 | } |
275 | 275 | |
276 | -function wpinv_get_random_items( $num = 3, $post_ids = true ) { |
|
277 | - if ( $post_ids ) { |
|
278 | - $args = array( 'post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num, 'fields' => 'ids' ); |
|
276 | +function wpinv_get_random_items($num = 3, $post_ids = true) { |
|
277 | + if ($post_ids) { |
|
278 | + $args = array('post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num, 'fields' => 'ids'); |
|
279 | 279 | } else { |
280 | - $args = array( 'post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num ); |
|
280 | + $args = array('post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num); |
|
281 | 281 | } |
282 | 282 | |
283 | - $args = apply_filters( 'wpinv_get_random_items', $args ); |
|
283 | + $args = apply_filters('wpinv_get_random_items', $args); |
|
284 | 284 | |
285 | - return get_posts( $args ); |
|
285 | + return get_posts($args); |
|
286 | 286 | } |
287 | 287 | |
288 | -function wpinv_get_item_token( $url = '' ) { |
|
288 | +function wpinv_get_item_token($url = '') { |
|
289 | 289 | $args = array(); |
290 | - $hash = apply_filters( 'wpinv_get_url_token_algorithm', 'sha256' ); |
|
291 | - $secret = apply_filters( 'wpinv_get_url_token_secret', hash( $hash, wp_salt() ) ); |
|
290 | + $hash = apply_filters('wpinv_get_url_token_algorithm', 'sha256'); |
|
291 | + $secret = apply_filters('wpinv_get_url_token_secret', hash($hash, wp_salt())); |
|
292 | 292 | |
293 | - $parts = parse_url( $url ); |
|
293 | + $parts = parse_url($url); |
|
294 | 294 | $options = array(); |
295 | 295 | |
296 | - if ( isset( $parts['query'] ) ) { |
|
297 | - wp_parse_str( $parts['query'], $query_args ); |
|
296 | + if (isset($parts['query'])) { |
|
297 | + wp_parse_str($parts['query'], $query_args); |
|
298 | 298 | |
299 | - if ( ! empty( $query_args['o'] ) ) { |
|
300 | - $options = explode( ':', rawurldecode( $query_args['o'] ) ); |
|
299 | + if (!empty($query_args['o'])) { |
|
300 | + $options = explode(':', rawurldecode($query_args['o'])); |
|
301 | 301 | |
302 | - if ( in_array( 'ip', $options ) ) { |
|
302 | + if (in_array('ip', $options)) { |
|
303 | 303 | $args['ip'] = wpinv_get_ip(); |
304 | 304 | } |
305 | 305 | |
306 | - if ( in_array( 'ua', $options ) ) { |
|
306 | + if (in_array('ua', $options)) { |
|
307 | 307 | $ua = wpinv_get_user_agent(); |
308 | - $args['user_agent'] = rawurlencode( $ua ); |
|
308 | + $args['user_agent'] = rawurlencode($ua); |
|
309 | 309 | } |
310 | 310 | } |
311 | 311 | } |
312 | 312 | |
313 | - $args = apply_filters( 'wpinv_get_url_token_args', $args, $url, $options ); |
|
313 | + $args = apply_filters('wpinv_get_url_token_args', $args, $url, $options); |
|
314 | 314 | |
315 | 315 | $args['secret'] = $secret; |
316 | 316 | $args['token'] = false; |
317 | 317 | |
318 | - $url = add_query_arg( $args, $url ); |
|
319 | - $parts = parse_url( $url ); |
|
318 | + $url = add_query_arg($args, $url); |
|
319 | + $parts = parse_url($url); |
|
320 | 320 | |
321 | - if ( ! isset( $parts['path'] ) ) { |
|
321 | + if (!isset($parts['path'])) { |
|
322 | 322 | $parts['path'] = ''; |
323 | 323 | } |
324 | 324 | |
325 | - $token = md5( $parts['path'] . '?' . $parts['query'] ); |
|
325 | + $token = md5($parts['path'] . '?' . $parts['query']); |
|
326 | 326 | |
327 | 327 | return $token; |
328 | 328 | } |
329 | 329 | |
330 | -function wpinv_validate_url_token( $url = '' ) { |
|
330 | +function wpinv_validate_url_token($url = '') { |
|
331 | 331 | $ret = false; |
332 | - $parts = parse_url( $url ); |
|
332 | + $parts = parse_url($url); |
|
333 | 333 | |
334 | - if ( isset( $parts['query'] ) ) { |
|
335 | - wp_parse_str( $parts['query'], $query_args ); |
|
334 | + if (isset($parts['query'])) { |
|
335 | + wp_parse_str($parts['query'], $query_args); |
|
336 | 336 | |
337 | - $allowed = apply_filters( 'wpinv_url_token_allowed_params', array( |
|
337 | + $allowed = apply_filters('wpinv_url_token_allowed_params', array( |
|
338 | 338 | 'item', |
339 | 339 | 'ttl', |
340 | 340 | 'token' |
341 | - ) ); |
|
341 | + )); |
|
342 | 342 | |
343 | 343 | $remove = array(); |
344 | 344 | |
345 | - foreach( $query_args as $key => $value ) { |
|
346 | - if( false === in_array( $key, $allowed ) ) { |
|
345 | + foreach ($query_args as $key => $value) { |
|
346 | + if (false === in_array($key, $allowed)) { |
|
347 | 347 | $remove[] = $key; |
348 | 348 | } |
349 | 349 | } |
350 | 350 | |
351 | - if( ! empty( $remove ) ) { |
|
352 | - $url = remove_query_arg( $remove, $url ); |
|
351 | + if (!empty($remove)) { |
|
352 | + $url = remove_query_arg($remove, $url); |
|
353 | 353 | } |
354 | 354 | |
355 | - if ( isset( $query_args['ttl'] ) && current_time( 'timestamp' ) > $query_args['ttl'] ) { |
|
356 | - wp_die( apply_filters( 'wpinv_item_link_expired_text', __( 'Sorry but your item link has expired.', 'invoicing' ) ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
355 | + if (isset($query_args['ttl']) && current_time('timestamp') > $query_args['ttl']) { |
|
356 | + wp_die(apply_filters('wpinv_item_link_expired_text', __('Sorry but your item link has expired.', 'invoicing')), __('Error', 'invoicing'), array('response' => 403)); |
|
357 | 357 | } |
358 | 358 | |
359 | - if ( isset( $query_args['token'] ) && $query_args['token'] == wpinv_get_item_token( $url ) ) { |
|
359 | + if (isset($query_args['token']) && $query_args['token'] == wpinv_get_item_token($url)) { |
|
360 | 360 | $ret = true; |
361 | 361 | } |
362 | 362 | |
363 | 363 | } |
364 | 364 | |
365 | - return apply_filters( 'wpinv_validate_url_token', $ret, $url, $query_args ); |
|
365 | + return apply_filters('wpinv_validate_url_token', $ret, $url, $query_args); |
|
366 | 366 | } |
367 | 367 | |
368 | -function wpinv_item_in_cart( $item_id = 0, $options = array() ) { |
|
368 | +function wpinv_item_in_cart($item_id = 0, $options = array()) { |
|
369 | 369 | $cart_items = wpinv_get_cart_contents(); |
370 | 370 | |
371 | 371 | $ret = false; |
372 | 372 | |
373 | - if ( is_array( $cart_items ) ) { |
|
374 | - foreach ( $cart_items as $item ) { |
|
375 | - if ( $item['id'] == $item_id ) { |
|
373 | + if (is_array($cart_items)) { |
|
374 | + foreach ($cart_items as $item) { |
|
375 | + if ($item['id'] == $item_id) { |
|
376 | 376 | $ret = true; |
377 | 377 | break; |
378 | 378 | } |
379 | 379 | } |
380 | 380 | } |
381 | 381 | |
382 | - return (bool) apply_filters( 'wpinv_item_in_cart', $ret, $item_id, $options ); |
|
382 | + return (bool)apply_filters('wpinv_item_in_cart', $ret, $item_id, $options); |
|
383 | 383 | } |
384 | 384 | |
385 | -function wpinv_get_cart_item_tax( $item_id = 0, $subtotal = '', $options = array() ) { |
|
385 | +function wpinv_get_cart_item_tax($item_id = 0, $subtotal = '', $options = array()) { |
|
386 | 386 | $tax = 0; |
387 | - if ( ! wpinv_item_is_tax_exclusive( $item_id ) ) { |
|
388 | - $country = !empty( $_POST['country'] ) ? $_POST['country'] : false; |
|
389 | - $state = isset( $_POST['state'] ) ? $_POST['state'] : ''; |
|
387 | + if (!wpinv_item_is_tax_exclusive($item_id)) { |
|
388 | + $country = !empty($_POST['country']) ? $_POST['country'] : false; |
|
389 | + $state = isset($_POST['state']) ? $_POST['state'] : ''; |
|
390 | 390 | |
391 | - $tax = wpinv_calculate_tax( $subtotal, $country, $state, $item_id ); |
|
391 | + $tax = wpinv_calculate_tax($subtotal, $country, $state, $item_id); |
|
392 | 392 | } |
393 | 393 | |
394 | - return apply_filters( 'wpinv_get_cart_item_tax', $tax, $item_id, $subtotal, $options ); |
|
394 | + return apply_filters('wpinv_get_cart_item_tax', $tax, $item_id, $subtotal, $options); |
|
395 | 395 | } |
396 | 396 | |
397 | -function wpinv_cart_item_price( $item ) { |
|
397 | +function wpinv_cart_item_price($item) { |
|
398 | 398 | $use_taxes = wpinv_use_taxes(); |
399 | - $item_id = isset( $item['id'] ) ? $item['id'] : 0; |
|
400 | - $price = isset( $item['item_price'] ) ? wpinv_round_amount( $item['item_price'] ) : 0; |
|
401 | - $options = isset( $item['options'] ) ? $item['options'] : array(); |
|
402 | - $price_id = isset( $options['price_id'] ) ? $options['price_id'] : false; |
|
403 | - $tax = wpinv_price( wpinv_format_amount( $item['tax'] ) ); |
|
404 | - |
|
405 | - if ( !wpinv_is_free_item( $item_id, $price_id ) && !wpinv_item_is_tax_exclusive( $item_id ) ) { |
|
406 | - if ( wpinv_prices_show_tax_on_checkout() && !wpinv_prices_include_tax() ) { |
|
399 | + $item_id = isset($item['id']) ? $item['id'] : 0; |
|
400 | + $price = isset($item['item_price']) ? wpinv_round_amount($item['item_price']) : 0; |
|
401 | + $options = isset($item['options']) ? $item['options'] : array(); |
|
402 | + $price_id = isset($options['price_id']) ? $options['price_id'] : false; |
|
403 | + $tax = wpinv_price(wpinv_format_amount($item['tax'])); |
|
404 | + |
|
405 | + if (!wpinv_is_free_item($item_id, $price_id) && !wpinv_item_is_tax_exclusive($item_id)) { |
|
406 | + if (wpinv_prices_show_tax_on_checkout() && !wpinv_prices_include_tax()) { |
|
407 | 407 | $price += $tax; |
408 | 408 | } |
409 | 409 | |
410 | - if( !wpinv_prices_show_tax_on_checkout() && wpinv_prices_include_tax() ) { |
|
410 | + if (!wpinv_prices_show_tax_on_checkout() && wpinv_prices_include_tax()) { |
|
411 | 411 | $price -= $tax; |
412 | 412 | } |
413 | 413 | } |
414 | 414 | |
415 | - $price = wpinv_price( wpinv_format_amount( $price ) ); |
|
415 | + $price = wpinv_price(wpinv_format_amount($price)); |
|
416 | 416 | |
417 | - return apply_filters( 'wpinv_cart_item_price_label', $price, $item ); |
|
417 | + return apply_filters('wpinv_cart_item_price_label', $price, $item); |
|
418 | 418 | } |
419 | 419 | |
420 | -function wpinv_cart_item_subtotal( $item ) { |
|
421 | - $subtotal = isset( $item['subtotal'] ) ? $item['subtotal'] : 0; |
|
422 | - $subtotal = wpinv_price( wpinv_format_amount( $subtotal ) ); |
|
420 | +function wpinv_cart_item_subtotal($item) { |
|
421 | + $subtotal = isset($item['subtotal']) ? $item['subtotal'] : 0; |
|
422 | + $subtotal = wpinv_price(wpinv_format_amount($subtotal)); |
|
423 | 423 | |
424 | - return apply_filters( 'wpinv_cart_item_subtotal_label', $subtotal, $item ); |
|
424 | + return apply_filters('wpinv_cart_item_subtotal_label', $subtotal, $item); |
|
425 | 425 | } |
426 | 426 | |
427 | -function wpinv_cart_item_tax( $item ) { |
|
427 | +function wpinv_cart_item_tax($item) { |
|
428 | 428 | $tax = ''; |
429 | 429 | $tax_rate = ''; |
430 | 430 | |
431 | - if ( isset( $item['tax'] ) && $item['tax'] > 0 && $item['subtotal'] > 0 ) { |
|
432 | - $tax = wpinv_price( wpinv_format_amount( $item['tax'] ) ); |
|
433 | - $tax_rate = !empty( $item['vat_rate'] ) ? $item['vat_rate'] : ( $item['tax'] / $item['subtotal'] ) * 100; |
|
434 | - $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : ''; |
|
431 | + if (isset($item['tax']) && $item['tax'] > 0 && $item['subtotal'] > 0) { |
|
432 | + $tax = wpinv_price(wpinv_format_amount($item['tax'])); |
|
433 | + $tax_rate = !empty($item['vat_rate']) ? $item['vat_rate'] : ($item['tax'] / $item['subtotal']) * 100; |
|
434 | + $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount($tax_rate, 4) : ''; |
|
435 | 435 | $tax_rate = $tax_rate != '' ? ' <small class="tax-rate normal small">(' . $tax_rate . '%)</small>' : ''; |
436 | 436 | } |
437 | 437 | |
438 | - $tax = $tax . $tax_rate; |
|
438 | + $tax = $tax . $tax_rate; |
|
439 | 439 | |
440 | - if ( $tax === '' ) { |
|
440 | + if ($tax === '') { |
|
441 | 441 | $tax = 0; // Zero tax |
442 | 442 | } |
443 | 443 | |
444 | - return apply_filters( 'wpinv_cart_item_tax_label', $tax, $item ); |
|
444 | + return apply_filters('wpinv_cart_item_tax_label', $tax, $item); |
|
445 | 445 | } |
446 | 446 | |
447 | -function wpinv_get_cart_item_price( $item_id = 0, $cart_item = array(), $options = array(), $remove_tax_from_inclusive = false ) { |
|
447 | +function wpinv_get_cart_item_price($item_id = 0, $cart_item = array(), $options = array(), $remove_tax_from_inclusive = false) { |
|
448 | 448 | $price = 0; |
449 | 449 | |
450 | 450 | // Set custom price |
451 | - if ( isset( $cart_item['custom_price'] ) && $cart_item['custom_price'] !== '' ) { |
|
451 | + if (isset($cart_item['custom_price']) && $cart_item['custom_price'] !== '') { |
|
452 | 452 | $price = $cart_item['custom_price']; |
453 | 453 | } else { |
454 | - $variable_prices = wpinv_has_variable_prices( $item_id ); |
|
454 | + $variable_prices = wpinv_has_variable_prices($item_id); |
|
455 | 455 | |
456 | - if ( $variable_prices ) { |
|
457 | - $prices = wpinv_get_variable_prices( $item_id ); |
|
456 | + if ($variable_prices) { |
|
457 | + $prices = wpinv_get_variable_prices($item_id); |
|
458 | 458 | |
459 | - if ( $prices ) { |
|
460 | - if( ! empty( $options ) ) { |
|
461 | - $price = isset( $prices[ $options['price_id'] ] ) ? $prices[ $options['price_id'] ]['amount'] : false; |
|
459 | + if ($prices) { |
|
460 | + if (!empty($options)) { |
|
461 | + $price = isset($prices[$options['price_id']]) ? $prices[$options['price_id']]['amount'] : false; |
|
462 | 462 | } else { |
463 | 463 | $price = false; |
464 | 464 | } |
465 | 465 | } |
466 | 466 | } |
467 | 467 | |
468 | - if( ! $variable_prices || false === $price ) { |
|
468 | + if (!$variable_prices || false === $price) { |
|
469 | 469 | // Get the standard Item price if not using variable prices |
470 | - $price = wpinv_get_item_price( $item_id ); |
|
470 | + $price = wpinv_get_item_price($item_id); |
|
471 | 471 | } |
472 | 472 | } |
473 | 473 | |
474 | - if ( $remove_tax_from_inclusive && wpinv_prices_include_tax() ) { |
|
475 | - $price -= wpinv_get_cart_item_tax( $item_id, $price, $options ); |
|
474 | + if ($remove_tax_from_inclusive && wpinv_prices_include_tax()) { |
|
475 | + $price -= wpinv_get_cart_item_tax($item_id, $price, $options); |
|
476 | 476 | } |
477 | 477 | |
478 | - return apply_filters( 'wpinv_cart_item_price', $price, $item_id, $cart_item, $options, $remove_tax_from_inclusive ); |
|
478 | + return apply_filters('wpinv_cart_item_price', $price, $item_id, $cart_item, $options, $remove_tax_from_inclusive); |
|
479 | 479 | } |
480 | 480 | |
481 | -function wpinv_get_cart_item_price_id( $item = array() ) { |
|
482 | - if( isset( $item['item_number'] ) ) { |
|
483 | - $price_id = isset( $item['item_number']['options']['price_id'] ) ? $item['item_number']['options']['price_id'] : null; |
|
481 | +function wpinv_get_cart_item_price_id($item = array()) { |
|
482 | + if (isset($item['item_number'])) { |
|
483 | + $price_id = isset($item['item_number']['options']['price_id']) ? $item['item_number']['options']['price_id'] : null; |
|
484 | 484 | } else { |
485 | - $price_id = isset( $item['options']['price_id'] ) ? $item['options']['price_id'] : null; |
|
485 | + $price_id = isset($item['options']['price_id']) ? $item['options']['price_id'] : null; |
|
486 | 486 | } |
487 | 487 | return $price_id; |
488 | 488 | } |
489 | 489 | |
490 | -function wpinv_get_cart_item_price_name( $item = array() ) { |
|
491 | - $price_id = (int)wpinv_get_cart_item_price_id( $item ); |
|
492 | - $prices = wpinv_get_variable_prices( $item['id'] ); |
|
493 | - $name = ! empty( $prices[ $price_id ] ) ? $prices[ $price_id ]['name'] : ''; |
|
494 | - return apply_filters( 'wpinv_get_cart_item_price_name', $name, $item['id'], $price_id, $item ); |
|
490 | +function wpinv_get_cart_item_price_name($item = array()) { |
|
491 | + $price_id = (int)wpinv_get_cart_item_price_id($item); |
|
492 | + $prices = wpinv_get_variable_prices($item['id']); |
|
493 | + $name = !empty($prices[$price_id]) ? $prices[$price_id]['name'] : ''; |
|
494 | + return apply_filters('wpinv_get_cart_item_price_name', $name, $item['id'], $price_id, $item); |
|
495 | 495 | } |
496 | 496 | |
497 | -function wpinv_get_cart_item_name( $item = array() ) { |
|
498 | - $item_title = !empty( $item['name'] ) ? $item['name'] : get_the_title( $item['id'] ); |
|
497 | +function wpinv_get_cart_item_name($item = array()) { |
|
498 | + $item_title = !empty($item['name']) ? $item['name'] : get_the_title($item['id']); |
|
499 | 499 | |
500 | - if ( empty( $item_title ) ) { |
|
500 | + if (empty($item_title)) { |
|
501 | 501 | $item_title = $item['id']; |
502 | 502 | } |
503 | 503 | |
@@ -507,23 +507,23 @@ discard block |
||
507 | 507 | } |
508 | 508 | */ |
509 | 509 | |
510 | - return apply_filters( 'wpinv_get_cart_item_name', $item_title, $item['id'], $item ); |
|
510 | + return apply_filters('wpinv_get_cart_item_name', $item_title, $item['id'], $item); |
|
511 | 511 | } |
512 | 512 | |
513 | -function wpinv_has_variable_prices( $item_id = 0 ) { |
|
513 | +function wpinv_has_variable_prices($item_id = 0) { |
|
514 | 514 | return false; |
515 | 515 | } |
516 | 516 | |
517 | -function wpinv_get_item_position_in_cart( $item_id = 0, $options = array() ) { |
|
517 | +function wpinv_get_item_position_in_cart($item_id = 0, $options = array()) { |
|
518 | 518 | $cart_items = wpinv_get_cart_contents(); |
519 | 519 | |
520 | - if ( !is_array( $cart_items ) ) { |
|
520 | + if (!is_array($cart_items)) { |
|
521 | 521 | return false; // Empty cart |
522 | 522 | } else { |
523 | - foreach ( $cart_items as $position => $item ) { |
|
524 | - if ( $item['id'] == $item_id ) { |
|
525 | - if ( isset( $options['price_id'] ) && isset( $item['options']['price_id'] ) ) { |
|
526 | - if ( (int) $options['price_id'] == (int) $item['options']['price_id'] ) { |
|
523 | + foreach ($cart_items as $position => $item) { |
|
524 | + if ($item['id'] == $item_id) { |
|
525 | + if (isset($options['price_id']) && isset($item['options']['price_id'])) { |
|
526 | + if ((int)$options['price_id'] == (int)$item['options']['price_id']) { |
|
527 | 527 | return $position; |
528 | 528 | } |
529 | 529 | } else { |
@@ -536,80 +536,80 @@ discard block |
||
536 | 536 | return false; // Not found |
537 | 537 | } |
538 | 538 | |
539 | -function wpinv_get_cart_item_quantity( $item ) { |
|
540 | - if ( wpinv_item_quantities_enabled() ) { |
|
541 | - $quantity = !empty( $item['quantity'] ) && (int)$item['quantity'] > 0 ? absint( $item['quantity'] ) : 1; |
|
539 | +function wpinv_get_cart_item_quantity($item) { |
|
540 | + if (wpinv_item_quantities_enabled()) { |
|
541 | + $quantity = !empty($item['quantity']) && (int)$item['quantity'] > 0 ? absint($item['quantity']) : 1; |
|
542 | 542 | } else { |
543 | 543 | $quantity = 1; |
544 | 544 | } |
545 | 545 | |
546 | - if ( $quantity < 1 ) { |
|
546 | + if ($quantity < 1) { |
|
547 | 547 | $quantity = 1; |
548 | 548 | } |
549 | 549 | |
550 | - return apply_filters( 'wpinv_get_cart_item_quantity', $quantity, $item ); |
|
550 | + return apply_filters('wpinv_get_cart_item_quantity', $quantity, $item); |
|
551 | 551 | } |
552 | 552 | |
553 | -function wpinv_get_item_suffix( $item, $html = true ) { |
|
554 | - if ( empty( $item ) ) { |
|
553 | +function wpinv_get_item_suffix($item, $html = true) { |
|
554 | + if (empty($item)) { |
|
555 | 555 | return NULL; |
556 | 556 | } |
557 | 557 | |
558 | - if ( is_int( $item ) ) { |
|
559 | - $item = new WPInv_Item( $item ); |
|
558 | + if (is_int($item)) { |
|
559 | + $item = new WPInv_Item($item); |
|
560 | 560 | } |
561 | 561 | |
562 | - if ( !( is_object( $item ) && is_a( $item, 'WPInv_Item' ) ) ) { |
|
562 | + if (!(is_object($item) && is_a($item, 'WPInv_Item'))) { |
|
563 | 563 | return NULL; |
564 | 564 | } |
565 | 565 | |
566 | - $suffix = $item->is_recurring() ? ' <span class="wpi-suffix">' . __( '(r)', 'invoicing' ) . '</span>' : ''; |
|
566 | + $suffix = $item->is_recurring() ? ' <span class="wpi-suffix">' . __('(r)', 'invoicing') . '</span>' : ''; |
|
567 | 567 | |
568 | - if ( !$html && $suffix ) { |
|
569 | - $suffix = strip_tags( $suffix ); |
|
568 | + if (!$html && $suffix) { |
|
569 | + $suffix = strip_tags($suffix); |
|
570 | 570 | } |
571 | 571 | |
572 | - return apply_filters( 'wpinv_get_item_suffix', $suffix, $item, $html ); |
|
572 | + return apply_filters('wpinv_get_item_suffix', $suffix, $item, $html); |
|
573 | 573 | } |
574 | 574 | |
575 | -function wpinv_remove_item( $item = 0, $force_delete = false ) { |
|
576 | - if ( empty( $item ) ) { |
|
575 | +function wpinv_remove_item($item = 0, $force_delete = false) { |
|
576 | + if (empty($item)) { |
|
577 | 577 | return NULL; |
578 | 578 | } |
579 | 579 | |
580 | - if ( is_int( $item ) ) { |
|
581 | - $item = new WPInv_Item( $item ); |
|
580 | + if (is_int($item)) { |
|
581 | + $item = new WPInv_Item($item); |
|
582 | 582 | } |
583 | 583 | |
584 | - if ( !( is_object( $item ) && is_a( $item, 'WPInv_Item' ) ) ) { |
|
584 | + if (!(is_object($item) && is_a($item, 'WPInv_Item'))) { |
|
585 | 585 | return NULL; |
586 | 586 | } |
587 | 587 | |
588 | - do_action( 'wpinv_pre_delete_item', $item ); |
|
588 | + do_action('wpinv_pre_delete_item', $item); |
|
589 | 589 | |
590 | - wp_delete_post( $item->ID, $force_delete ); |
|
590 | + wp_delete_post($item->ID, $force_delete); |
|
591 | 591 | |
592 | - do_action( 'wpinv_post_delete_item', $item ); |
|
592 | + do_action('wpinv_post_delete_item', $item); |
|
593 | 593 | } |
594 | 594 | |
595 | -function wpinv_can_delete_item( $post_id ) { |
|
596 | - $return = current_user_can( 'manage_options' ) ? true : false; |
|
595 | +function wpinv_can_delete_item($post_id) { |
|
596 | + $return = current_user_can('manage_options') ? true : false; |
|
597 | 597 | |
598 | - if ( $return && wpinv_item_in_use( $post_id ) ) { |
|
598 | + if ($return && wpinv_item_in_use($post_id)) { |
|
599 | 599 | $return = false; // Don't delete item already use in invoices. |
600 | 600 | } |
601 | 601 | |
602 | - return apply_filters( 'wpinv_can_delete_item', $return, $post_id ); |
|
602 | + return apply_filters('wpinv_can_delete_item', $return, $post_id); |
|
603 | 603 | } |
604 | 604 | |
605 | 605 | function wpinv_admin_action_delete() { |
606 | 606 | $screen = get_current_screen(); |
607 | 607 | |
608 | - if ( !empty( $screen->post_type ) && $screen->post_type == 'wpi_item' && !empty( $_REQUEST['post'] ) && is_array( $_REQUEST['post'] ) ) { |
|
608 | + if (!empty($screen->post_type) && $screen->post_type == 'wpi_item' && !empty($_REQUEST['post']) && is_array($_REQUEST['post'])) { |
|
609 | 609 | $post_ids = array(); |
610 | 610 | |
611 | - foreach ( $_REQUEST['post'] as $post_id ) { |
|
612 | - if ( !wpinv_can_delete_item( $post_id ) ) { |
|
611 | + foreach ($_REQUEST['post'] as $post_id) { |
|
612 | + if (!wpinv_can_delete_item($post_id)) { |
|
613 | 613 | continue; |
614 | 614 | } |
615 | 615 | |
@@ -619,86 +619,86 @@ discard block |
||
619 | 619 | $_REQUEST['post'] = $post_ids; |
620 | 620 | } |
621 | 621 | } |
622 | -add_action( 'admin_action_trash', 'wpinv_admin_action_delete', -10 ); |
|
623 | -add_action( 'admin_action_delete', 'wpinv_admin_action_delete', -10 ); |
|
622 | +add_action('admin_action_trash', 'wpinv_admin_action_delete', -10); |
|
623 | +add_action('admin_action_delete', 'wpinv_admin_action_delete', -10); |
|
624 | 624 | |
625 | -function wpinv_check_delete_item( $check, $post, $force_delete ) { |
|
626 | - if ( $post->post_type == 'wpi_item' ) { |
|
627 | - if ( $force_delete && !wpinv_can_delete_item( $post->ID ) ) { |
|
625 | +function wpinv_check_delete_item($check, $post, $force_delete) { |
|
626 | + if ($post->post_type == 'wpi_item') { |
|
627 | + if ($force_delete && !wpinv_can_delete_item($post->ID)) { |
|
628 | 628 | return true; |
629 | 629 | } |
630 | 630 | } |
631 | 631 | |
632 | 632 | return $check; |
633 | 633 | } |
634 | -add_filter( 'pre_delete_post', 'wpinv_check_delete_item', 10, 3 ); |
|
634 | +add_filter('pre_delete_post', 'wpinv_check_delete_item', 10, 3); |
|
635 | 635 | |
636 | -function wpinv_item_in_use( $item_id ) { |
|
636 | +function wpinv_item_in_use($item_id) { |
|
637 | 637 | global $wpdb, $wpi_items_in_use; |
638 | 638 | |
639 | - if ( !$item_id > 0 ) { |
|
639 | + if (!$item_id > 0) { |
|
640 | 640 | return false; |
641 | 641 | } |
642 | 642 | |
643 | - if ( !empty( $wpi_items_in_use ) ) { |
|
644 | - if ( isset( $wpi_items_in_use[$item_id] ) ) { |
|
643 | + if (!empty($wpi_items_in_use)) { |
|
644 | + if (isset($wpi_items_in_use[$item_id])) { |
|
645 | 645 | return $wpi_items_in_use[$item_id]; |
646 | 646 | } |
647 | 647 | } else { |
648 | 648 | $wpi_items_in_use = array(); |
649 | 649 | } |
650 | 650 | |
651 | - $statuses = array_keys( wpinv_get_invoice_statuses( true, true ) ); |
|
651 | + $statuses = array_keys(wpinv_get_invoice_statuses(true, true)); |
|
652 | 652 | |
653 | - $query = "SELECT p.ID FROM " . $wpdb->posts . " AS p INNER JOIN " . $wpdb->postmeta . " AS pm ON p.ID = pm.post_id WHERE p.post_type = 'wpi_invoice' AND p.post_status IN( '" . implode( "','", $statuses ) . "' ) AND pm.meta_key = '_wpinv_item_ids' AND FIND_IN_SET( '" . (int)$item_id . "', pm.meta_value )"; |
|
654 | - $in_use = $wpdb->get_var( $query ) > 0 ? true : false; |
|
653 | + $query = "SELECT p.ID FROM " . $wpdb->posts . " AS p INNER JOIN " . $wpdb->postmeta . " AS pm ON p.ID = pm.post_id WHERE p.post_type = 'wpi_invoice' AND p.post_status IN( '" . implode("','", $statuses) . "' ) AND pm.meta_key = '_wpinv_item_ids' AND FIND_IN_SET( '" . (int)$item_id . "', pm.meta_value )"; |
|
654 | + $in_use = $wpdb->get_var($query) > 0 ? true : false; |
|
655 | 655 | |
656 | 656 | $wpi_items_in_use[$item_id] = $in_use; |
657 | 657 | |
658 | 658 | return $in_use; |
659 | 659 | } |
660 | 660 | |
661 | -function wpinv_create_item( $args = array(), $wp_error = false, $force_update = false ) { |
|
661 | +function wpinv_create_item($args = array(), $wp_error = false, $force_update = false) { |
|
662 | 662 | // Set some defaults |
663 | 663 | $defaults = array( |
664 | - 'type' => 'custom', // Optional. Item type. Default 'custom'. |
|
665 | - 'title' => '', // Required. Item title. |
|
666 | - 'custom_id' => 0, // Optional. Any integer or non numeric id. Must be unique within item type. |
|
667 | - 'price' => '0.00', // Optional. Item price. Default '0.00'. |
|
668 | - 'status' => 'pending', // Optional. pending, publish |
|
669 | - 'custom_name' => '', // Optional. Plural sub title for item. |
|
670 | - 'custom_singular_name' => '', // Optional. Singular sub title for item. |
|
671 | - 'vat_rule' => 'digital', // Optional. digital => Digital item, physical => Physical item |
|
672 | - 'editable' => true, // Optional. Item editable from Items list page? Default true. |
|
673 | - 'excerpt' => '', // Optional. Item short description |
|
664 | + 'type' => 'custom', // Optional. Item type. Default 'custom'. |
|
665 | + 'title' => '', // Required. Item title. |
|
666 | + 'custom_id' => 0, // Optional. Any integer or non numeric id. Must be unique within item type. |
|
667 | + 'price' => '0.00', // Optional. Item price. Default '0.00'. |
|
668 | + 'status' => 'pending', // Optional. pending, publish |
|
669 | + 'custom_name' => '', // Optional. Plural sub title for item. |
|
670 | + 'custom_singular_name' => '', // Optional. Singular sub title for item. |
|
671 | + 'vat_rule' => 'digital', // Optional. digital => Digital item, physical => Physical item |
|
672 | + 'editable' => true, // Optional. Item editable from Items list page? Default true. |
|
673 | + 'excerpt' => '', // Optional. Item short description |
|
674 | 674 | /* Recurring item fields */ |
675 | - 'is_recurring' => 0, // Optional. 1 => Allow recurring or 0 => Don't allow recurring |
|
676 | - 'recurring_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
677 | - 'recurring_interval' => 0, // Optional. Integer value between 1 - 90. |
|
678 | - 'recurring_limit' => 0, // Optional. Any integer number. 0 for recurring forever until cancelled. |
|
679 | - 'free_trial' => 0, // Optional. 1 => Allow free trial or 0 => Don't free trial |
|
680 | - 'trial_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
681 | - 'trial_interval' => 0, // Optional. Any integer number. |
|
675 | + 'is_recurring' => 0, // Optional. 1 => Allow recurring or 0 => Don't allow recurring |
|
676 | + 'recurring_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
677 | + 'recurring_interval' => 0, // Optional. Integer value between 1 - 90. |
|
678 | + 'recurring_limit' => 0, // Optional. Any integer number. 0 for recurring forever until cancelled. |
|
679 | + 'free_trial' => 0, // Optional. 1 => Allow free trial or 0 => Don't free trial |
|
680 | + 'trial_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
681 | + 'trial_interval' => 0, // Optional. Any integer number. |
|
682 | 682 | ); |
683 | 683 | |
684 | - $data = wp_parse_args( $args, $defaults ); |
|
684 | + $data = wp_parse_args($args, $defaults); |
|
685 | 685 | |
686 | - if ( empty( $data['type'] ) ) { |
|
686 | + if (empty($data['type'])) { |
|
687 | 687 | $data['type'] = 'custom'; |
688 | 688 | } |
689 | 689 | |
690 | - if ( !empty( $data['custom_id'] ) ) { |
|
691 | - $item = wpinv_get_item_by( 'custom_id', $data['custom_id'], $data['type'] ); |
|
690 | + if (!empty($data['custom_id'])) { |
|
691 | + $item = wpinv_get_item_by('custom_id', $data['custom_id'], $data['type']); |
|
692 | 692 | } else { |
693 | 693 | $item = NULL; |
694 | 694 | } |
695 | 695 | |
696 | - if ( !empty( $item ) ) { |
|
697 | - if ( $force_update ) { |
|
698 | - if ( empty( $args['ID'] ) ) { |
|
696 | + if (!empty($item)) { |
|
697 | + if ($force_update) { |
|
698 | + if (empty($args['ID'])) { |
|
699 | 699 | $args['ID'] = $item->ID; |
700 | 700 | } |
701 | - return wpinv_update_item( $args, $wp_error ); |
|
701 | + return wpinv_update_item($args, $wp_error); |
|
702 | 702 | } |
703 | 703 | |
704 | 704 | return $item; |
@@ -709,19 +709,19 @@ discard block |
||
709 | 709 | $meta['custom_id'] = $data['custom_id']; |
710 | 710 | $meta['custom_singular_name'] = $data['custom_singular_name']; |
711 | 711 | $meta['custom_name'] = $data['custom_name']; |
712 | - $meta['price'] = wpinv_round_amount( $data['price'] ); |
|
712 | + $meta['price'] = wpinv_round_amount($data['price']); |
|
713 | 713 | $meta['editable'] = (int)$data['editable']; |
714 | 714 | $meta['vat_rule'] = $data['vat_rule']; |
715 | 715 | $meta['vat_class'] = '_standard'; |
716 | 716 | |
717 | - if ( !empty( $data['is_recurring'] ) ) { |
|
717 | + if (!empty($data['is_recurring'])) { |
|
718 | 718 | $meta['is_recurring'] = $data['is_recurring']; |
719 | 719 | $meta['recurring_period'] = $data['recurring_period']; |
720 | - $meta['recurring_interval'] = absint( $data['recurring_interval'] ); |
|
721 | - $meta['recurring_limit'] = absint( $data['recurring_limit'] ); |
|
720 | + $meta['recurring_interval'] = absint($data['recurring_interval']); |
|
721 | + $meta['recurring_limit'] = absint($data['recurring_limit']); |
|
722 | 722 | $meta['free_trial'] = $data['free_trial']; |
723 | 723 | $meta['trial_period'] = $data['trial_period']; |
724 | - $meta['trial_interval'] = absint( $data['trial_interval'] ); |
|
724 | + $meta['trial_interval'] = absint($data['trial_interval']); |
|
725 | 725 | } else { |
726 | 726 | $meta['is_recurring'] = 0; |
727 | 727 | $meta['recurring_period'] = ''; |
@@ -732,7 +732,7 @@ discard block |
||
732 | 732 | $meta['trial_interval'] = ''; |
733 | 733 | } |
734 | 734 | |
735 | - $post_data = array( |
|
735 | + $post_data = array( |
|
736 | 736 | 'post_title' => $data['title'], |
737 | 737 | 'post_excerpt' => $data['excerpt'], |
738 | 738 | 'post_status' => $data['status'], |
@@ -740,89 +740,89 @@ discard block |
||
740 | 740 | ); |
741 | 741 | |
742 | 742 | $item = new WPInv_Item(); |
743 | - $return = $item->create( $post_data, $wp_error ); |
|
743 | + $return = $item->create($post_data, $wp_error); |
|
744 | 744 | |
745 | - if ( $return && !empty( $item ) && !is_wp_error( $return ) ) { |
|
745 | + if ($return && !empty($item) && !is_wp_error($return)) { |
|
746 | 746 | return $item; |
747 | 747 | } |
748 | 748 | |
749 | - if ( $wp_error && is_wp_error( $return ) ) { |
|
749 | + if ($wp_error && is_wp_error($return)) { |
|
750 | 750 | return $return; |
751 | 751 | } |
752 | 752 | return 0; |
753 | 753 | } |
754 | 754 | |
755 | -function wpinv_update_item( $args = array(), $wp_error = false ) { |
|
756 | - $item = !empty( $args['ID'] ) ? new WPInv_Item( $args['ID'] ) : NULL; |
|
755 | +function wpinv_update_item($args = array(), $wp_error = false) { |
|
756 | + $item = !empty($args['ID']) ? new WPInv_Item($args['ID']) : NULL; |
|
757 | 757 | |
758 | - if ( empty( $item ) || !( !empty( $item->post_type ) && $item->post_type == 'wpi_item' ) ) { |
|
759 | - if ( $wp_error ) { |
|
760 | - return new WP_Error( 'wpinv_invalid_item', __( 'Invalid item.', 'invoicing' ) ); |
|
758 | + if (empty($item) || !(!empty($item->post_type) && $item->post_type == 'wpi_item')) { |
|
759 | + if ($wp_error) { |
|
760 | + return new WP_Error('wpinv_invalid_item', __('Invalid item.', 'invoicing')); |
|
761 | 761 | } |
762 | 762 | return 0; |
763 | 763 | } |
764 | 764 | |
765 | - if ( !empty( $args['custom_id'] ) ) { |
|
766 | - $item_exists = wpinv_get_item_by( 'custom_id', $args['custom_id'], ( !empty( $args['type'] ) ? $args['type'] : $item->type ) ); |
|
765 | + if (!empty($args['custom_id'])) { |
|
766 | + $item_exists = wpinv_get_item_by('custom_id', $args['custom_id'], (!empty($args['type']) ? $args['type'] : $item->type)); |
|
767 | 767 | |
768 | - if ( !empty( $item_exists ) && $item_exists->ID != $args['ID'] ) { |
|
769 | - if ( $wp_error ) { |
|
770 | - return new WP_Error( 'wpinv_invalid_custom_id', __( 'Item with custom id already exists.', 'invoicing' ) ); |
|
768 | + if (!empty($item_exists) && $item_exists->ID != $args['ID']) { |
|
769 | + if ($wp_error) { |
|
770 | + return new WP_Error('wpinv_invalid_custom_id', __('Item with custom id already exists.', 'invoicing')); |
|
771 | 771 | } |
772 | 772 | return 0; |
773 | 773 | } |
774 | 774 | } |
775 | 775 | |
776 | - $meta_fields = array( 'type', 'custom_id', 'custom_singular_name', 'custom_name', 'price', 'editable', 'vat_rule', 'vat_class', 'is_recurring', 'recurring_period', 'recurring_interval', 'recurring_limit', 'free_trial', 'trial_period', 'trial_interval' ); |
|
776 | + $meta_fields = array('type', 'custom_id', 'custom_singular_name', 'custom_name', 'price', 'editable', 'vat_rule', 'vat_class', 'is_recurring', 'recurring_period', 'recurring_interval', 'recurring_limit', 'free_trial', 'trial_period', 'trial_interval'); |
|
777 | 777 | |
778 | 778 | $post_data = array(); |
779 | - if ( isset( $args['title'] ) ) { |
|
779 | + if (isset($args['title'])) { |
|
780 | 780 | $post_data['post_title'] = $args['title']; |
781 | 781 | } |
782 | - if ( isset( $args['excerpt'] ) ) { |
|
782 | + if (isset($args['excerpt'])) { |
|
783 | 783 | $post_data['post_excerpt'] = $args['excerpt']; |
784 | 784 | } |
785 | - if ( isset( $args['status'] ) ) { |
|
785 | + if (isset($args['status'])) { |
|
786 | 786 | $post_data['post_status'] = $args['status']; |
787 | 787 | } |
788 | 788 | |
789 | - foreach ( $meta_fields as $meta_field ) { |
|
790 | - if ( isset( $args[ $meta_field ] ) ) { |
|
791 | - $value = $args[ $meta_field ]; |
|
789 | + foreach ($meta_fields as $meta_field) { |
|
790 | + if (isset($args[$meta_field])) { |
|
791 | + $value = $args[$meta_field]; |
|
792 | 792 | |
793 | - switch ( $meta_field ) { |
|
793 | + switch ($meta_field) { |
|
794 | 794 | case 'price': |
795 | - $value = wpinv_round_amount( $value ); |
|
795 | + $value = wpinv_round_amount($value); |
|
796 | 796 | break; |
797 | 797 | case 'recurring_interval': |
798 | 798 | case 'recurring_limit': |
799 | 799 | case 'trial_interval': |
800 | - $value = absint( $value ); |
|
800 | + $value = absint($value); |
|
801 | 801 | break; |
802 | 802 | case 'editable': |
803 | - $value = (int) $value; |
|
803 | + $value = (int)$value; |
|
804 | 804 | break; |
805 | 805 | } |
806 | 806 | |
807 | - $post_data['meta'][ $meta_field ] = $value; |
|
807 | + $post_data['meta'][$meta_field] = $value; |
|
808 | 808 | }; |
809 | 809 | } |
810 | 810 | |
811 | - if ( empty( $post_data ) ) { |
|
812 | - if ( $wp_error ) { |
|
813 | - return new WP_Error( 'wpinv_invalid_item_data', __( 'Invalid item data.', 'invoicing' ) ); |
|
811 | + if (empty($post_data)) { |
|
812 | + if ($wp_error) { |
|
813 | + return new WP_Error('wpinv_invalid_item_data', __('Invalid item data.', 'invoicing')); |
|
814 | 814 | } |
815 | 815 | return 0; |
816 | 816 | } |
817 | 817 | $post_data['ID'] = $args['ID']; |
818 | 818 | |
819 | - $return = $item->update( $post_data, $wp_error ); |
|
819 | + $return = $item->update($post_data, $wp_error); |
|
820 | 820 | |
821 | - if ( $return && !empty( $item ) && !is_wp_error( $return ) ) { |
|
821 | + if ($return && !empty($item) && !is_wp_error($return)) { |
|
822 | 822 | return $item; |
823 | 823 | } |
824 | 824 | |
825 | - if ( $wp_error && is_wp_error( $return ) ) { |
|
825 | + if ($wp_error && is_wp_error($return)) { |
|
826 | 826 | return $return; |
827 | 827 | } |
828 | 828 | return 0; |