@@ -887,8 +887,8 @@ |
||
887 | 887 | if ( !empty( $data ) && isset( $data['cart_discounts'] ) ) { |
888 | 888 | unset( $data['cart_discounts'] ); |
889 | 889 | |
890 | - wpinv_set_checkout_session( $data ); |
|
891 | - return true; |
|
890 | + wpinv_set_checkout_session( $data ); |
|
891 | + return true; |
|
892 | 892 | } |
893 | 893 | |
894 | 894 | return false; |
@@ -7,110 +7,110 @@ discard block |
||
7 | 7 | */ |
8 | 8 | |
9 | 9 | // MUST have WordPress. |
10 | -if ( !defined( 'WPINC' ) ) { |
|
11 | - exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) ); |
|
10 | +if (!defined('WPINC')) { |
|
11 | + exit('Do NOT access this file directly: ' . basename(__FILE__)); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | function wpinv_get_discount_types() { |
15 | 15 | $discount_types = array( |
16 | - 'percent' => __( 'Percentage', 'invoicing' ), |
|
17 | - 'flat' => __( 'Flat Amount', 'invoicing' ), |
|
16 | + 'percent' => __('Percentage', 'invoicing'), |
|
17 | + 'flat' => __('Flat Amount', 'invoicing'), |
|
18 | 18 | ); |
19 | - return (array)apply_filters( 'wpinv_discount_types', $discount_types ); |
|
19 | + return (array) apply_filters('wpinv_discount_types', $discount_types); |
|
20 | 20 | } |
21 | 21 | |
22 | -function wpinv_get_discount_type_name( $type = '' ) { |
|
22 | +function wpinv_get_discount_type_name($type = '') { |
|
23 | 23 | $types = wpinv_get_discount_types(); |
24 | - return isset( $types[ $type ] ) ? $types[ $type ] : ''; |
|
24 | + return isset($types[$type]) ? $types[$type] : ''; |
|
25 | 25 | } |
26 | 26 | |
27 | -function wpinv_delete_discount( $data ) { |
|
28 | - if ( ! isset( $data['_wpnonce'] ) || ! wp_verify_nonce( $data['_wpnonce'], 'wpinv_discount_nonce' ) ) { |
|
29 | - wp_die( __( 'Trying to cheat or something?', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
27 | +function wpinv_delete_discount($data) { |
|
28 | + if (!isset($data['_wpnonce']) || !wp_verify_nonce($data['_wpnonce'], 'wpinv_discount_nonce')) { |
|
29 | + wp_die(__('Trying to cheat or something?', 'invoicing'), __('Error', 'invoicing'), array('response' => 403)); |
|
30 | 30 | } |
31 | 31 | |
32 | - if( ! wpinv_current_user_can_manage_invoicing() ) { |
|
33 | - wp_die( __( 'You do not have permission to delete discount codes', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
32 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
33 | + wp_die(__('You do not have permission to delete discount codes', 'invoicing'), __('Error', 'invoicing'), array('response' => 403)); |
|
34 | 34 | } |
35 | 35 | |
36 | 36 | $discount_id = $data['discount']; |
37 | - wpinv_remove_discount( $discount_id ); |
|
37 | + wpinv_remove_discount($discount_id); |
|
38 | 38 | } |
39 | -add_action( 'wpinv_delete_discount', 'wpinv_delete_discount' ); |
|
39 | +add_action('wpinv_delete_discount', 'wpinv_delete_discount'); |
|
40 | 40 | |
41 | -function wpinv_activate_discount( $data ) { |
|
42 | - if ( ! isset( $data['_wpnonce'] ) || ! wp_verify_nonce( $data['_wpnonce'], 'wpinv_discount_nonce' ) ) { |
|
43 | - wp_die( __( 'Trying to cheat or something?', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
41 | +function wpinv_activate_discount($data) { |
|
42 | + if (!isset($data['_wpnonce']) || !wp_verify_nonce($data['_wpnonce'], 'wpinv_discount_nonce')) { |
|
43 | + wp_die(__('Trying to cheat or something?', 'invoicing'), __('Error', 'invoicing'), array('response' => 403)); |
|
44 | 44 | } |
45 | 45 | |
46 | - if( ! wpinv_current_user_can_manage_invoicing() ) { |
|
47 | - wp_die( __( 'You do not have permission to edit discount codes', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
46 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
47 | + wp_die(__('You do not have permission to edit discount codes', 'invoicing'), __('Error', 'invoicing'), array('response' => 403)); |
|
48 | 48 | } |
49 | 49 | |
50 | - $id = absint( $data['discount'] ); |
|
51 | - wpinv_update_discount_status( $id, 'publish' ); |
|
50 | + $id = absint($data['discount']); |
|
51 | + wpinv_update_discount_status($id, 'publish'); |
|
52 | 52 | } |
53 | -add_action( 'wpinv_activate_discount', 'wpinv_activate_discount' ); |
|
53 | +add_action('wpinv_activate_discount', 'wpinv_activate_discount'); |
|
54 | 54 | |
55 | -function wpinv_deactivate_discount( $data ) { |
|
56 | - if ( ! isset( $data['_wpnonce'] ) || ! wp_verify_nonce( $data['_wpnonce'], 'wpinv_discount_nonce' ) ) { |
|
57 | - wp_die( __( 'Trying to cheat or something?', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
55 | +function wpinv_deactivate_discount($data) { |
|
56 | + if (!isset($data['_wpnonce']) || !wp_verify_nonce($data['_wpnonce'], 'wpinv_discount_nonce')) { |
|
57 | + wp_die(__('Trying to cheat or something?', 'invoicing'), __('Error', 'invoicing'), array('response' => 403)); |
|
58 | 58 | } |
59 | 59 | |
60 | - if( ! wpinv_current_user_can_manage_invoicing() ) { |
|
61 | - wp_die( __( 'You do not have permission to create discount codes', 'invoicing' ), array( 'response' => 403 ) ); |
|
60 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
61 | + wp_die(__('You do not have permission to create discount codes', 'invoicing'), array('response' => 403)); |
|
62 | 62 | } |
63 | 63 | |
64 | - $id = absint( $data['discount'] ); |
|
65 | - wpinv_update_discount_status( $id, 'pending' ); |
|
64 | + $id = absint($data['discount']); |
|
65 | + wpinv_update_discount_status($id, 'pending'); |
|
66 | 66 | } |
67 | -add_action( 'wpinv_deactivate_discount', 'wpinv_deactivate_discount' ); |
|
67 | +add_action('wpinv_deactivate_discount', 'wpinv_deactivate_discount'); |
|
68 | 68 | |
69 | -function wpinv_get_discounts( $args = array() ) { |
|
69 | +function wpinv_get_discounts($args = array()) { |
|
70 | 70 | $defaults = array( |
71 | 71 | 'post_type' => 'wpi_discount', |
72 | 72 | 'posts_per_page' => 20, |
73 | 73 | 'paged' => null, |
74 | - 'post_status' => array( 'publish', 'pending', 'draft', 'expired' ) |
|
74 | + 'post_status' => array('publish', 'pending', 'draft', 'expired') |
|
75 | 75 | ); |
76 | 76 | |
77 | - $args = wp_parse_args( $args, $defaults ); |
|
77 | + $args = wp_parse_args($args, $defaults); |
|
78 | 78 | |
79 | - $discounts = get_posts( $args ); |
|
79 | + $discounts = get_posts($args); |
|
80 | 80 | |
81 | - if ( $discounts ) { |
|
81 | + if ($discounts) { |
|
82 | 82 | return $discounts; |
83 | 83 | } |
84 | 84 | |
85 | - if( ! $discounts && ! empty( $args['s'] ) ) { |
|
85 | + if (!$discounts && !empty($args['s'])) { |
|
86 | 86 | $args['meta_key'] = '_wpi_discount_code'; |
87 | 87 | $args['meta_value'] = $args['s']; |
88 | 88 | $args['meta_compare'] = 'LIKE'; |
89 | - unset( $args['s'] ); |
|
90 | - $discounts = get_posts( $args ); |
|
89 | + unset($args['s']); |
|
90 | + $discounts = get_posts($args); |
|
91 | 91 | } |
92 | 92 | |
93 | - if( $discounts ) { |
|
93 | + if ($discounts) { |
|
94 | 94 | return $discounts; |
95 | 95 | } |
96 | 96 | |
97 | 97 | return false; |
98 | 98 | } |
99 | 99 | |
100 | -function wpinv_get_all_discounts( $args = array() ) { |
|
100 | +function wpinv_get_all_discounts($args = array()) { |
|
101 | 101 | |
102 | - $args = wp_parse_args( $args, array( |
|
103 | - 'status' => array( 'publish' ), |
|
104 | - 'limit' => get_option( 'posts_per_page' ), |
|
102 | + $args = wp_parse_args($args, array( |
|
103 | + 'status' => array('publish'), |
|
104 | + 'limit' => get_option('posts_per_page'), |
|
105 | 105 | 'page' => 1, |
106 | 106 | 'exclude' => array(), |
107 | 107 | 'orderby' => 'date', |
108 | 108 | 'order' => 'DESC', |
109 | - 'type' => array_keys( wpinv_get_discount_types() ), |
|
109 | + 'type' => array_keys(wpinv_get_discount_types()), |
|
110 | 110 | 'meta_query' => array(), |
111 | 111 | 'return' => 'objects', |
112 | 112 | 'paginate' => false, |
113 | - ) ); |
|
113 | + )); |
|
114 | 114 | |
115 | 115 | $wp_query_args = array( |
116 | 116 | 'post_type' => 'wpi_discount', |
@@ -120,18 +120,18 @@ discard block |
||
120 | 120 | 'fields' => 'ids', |
121 | 121 | 'orderby' => $args['orderby'], |
122 | 122 | 'order' => $args['order'], |
123 | - 'paged' => absint( $args['page'] ), |
|
123 | + 'paged' => absint($args['page']), |
|
124 | 124 | ); |
125 | 125 | |
126 | - if ( ! empty( $args['exclude'] ) ) { |
|
127 | - $wp_query_args['post__not_in'] = array_map( 'absint', $args['exclude'] ); |
|
126 | + if (!empty($args['exclude'])) { |
|
127 | + $wp_query_args['post__not_in'] = array_map('absint', $args['exclude']); |
|
128 | 128 | } |
129 | 129 | |
130 | - if ( ! $args['paginate' ] ) { |
|
130 | + if (!$args['paginate']) { |
|
131 | 131 | $wp_query_args['no_found_rows'] = true; |
132 | 132 | } |
133 | 133 | |
134 | - if ( ! empty( $args['search'] ) ) { |
|
134 | + if (!empty($args['search'])) { |
|
135 | 135 | |
136 | 136 | $wp_query_args['meta_query'][] = array( |
137 | 137 | 'key' => '_wpi_discount_code', |
@@ -141,11 +141,11 @@ discard block |
||
141 | 141 | |
142 | 142 | } |
143 | 143 | |
144 | - if ( ! empty( $args['type'] ) ) { |
|
145 | - $types = wpinv_parse_list( $args['type'] ); |
|
144 | + if (!empty($args['type'])) { |
|
145 | + $types = wpinv_parse_list($args['type']); |
|
146 | 146 | $wp_query_args['meta_query'][] = array( |
147 | 147 | 'key' => '_wpi_discount_type', |
148 | - 'value' => implode( ',', $types ), |
|
148 | + 'value' => implode(',', $types), |
|
149 | 149 | 'compare' => 'IN', |
150 | 150 | ); |
151 | 151 | } |
@@ -153,17 +153,17 @@ discard block |
||
153 | 153 | $wp_query_args = apply_filters('wpinv_get_discount_args', $wp_query_args, $args); |
154 | 154 | |
155 | 155 | // Get results. |
156 | - $discounts = new WP_Query( $wp_query_args ); |
|
156 | + $discounts = new WP_Query($wp_query_args); |
|
157 | 157 | |
158 | - if ( 'objects' === $args['return'] ) { |
|
159 | - $return = array_map( 'get_post', $discounts->posts ); |
|
160 | - } elseif ( 'self' === $args['return'] ) { |
|
158 | + if ('objects' === $args['return']) { |
|
159 | + $return = array_map('get_post', $discounts->posts); |
|
160 | + } elseif ('self' === $args['return']) { |
|
161 | 161 | return $discounts; |
162 | 162 | } else { |
163 | 163 | $return = $discounts->posts; |
164 | 164 | } |
165 | 165 | |
166 | - if ( $args['paginate' ] ) { |
|
166 | + if ($args['paginate']) { |
|
167 | 167 | return (object) array( |
168 | 168 | 'discounts' => $return, |
169 | 169 | 'total' => $discounts->found_posts, |
@@ -180,9 +180,9 @@ discard block |
||
180 | 180 | |
181 | 181 | $discounts = wpinv_get_discounts(); |
182 | 182 | |
183 | - if ( $discounts) { |
|
184 | - foreach ( $discounts as $discount ) { |
|
185 | - if ( wpinv_is_discount_active( $discount->ID, true ) ) { |
|
183 | + if ($discounts) { |
|
184 | + foreach ($discounts as $discount) { |
|
185 | + if (wpinv_is_discount_active($discount->ID, true)) { |
|
186 | 186 | $has_active = true; |
187 | 187 | break; |
188 | 188 | } |
@@ -191,16 +191,16 @@ discard block |
||
191 | 191 | return $has_active; |
192 | 192 | } |
193 | 193 | |
194 | -function wpinv_get_discount( $discount_id = 0 ) { |
|
195 | - if( empty( $discount_id ) ) { |
|
194 | +function wpinv_get_discount($discount_id = 0) { |
|
195 | + if (empty($discount_id)) { |
|
196 | 196 | return false; |
197 | 197 | } |
198 | 198 | |
199 | - if ( get_post_type( $discount_id ) != 'wpi_discount' ) { |
|
199 | + if (get_post_type($discount_id) != 'wpi_discount') { |
|
200 | 200 | return false; |
201 | 201 | } |
202 | 202 | |
203 | - $discount = get_post( $discount_id ); |
|
203 | + $discount = get_post($discount_id); |
|
204 | 204 | |
205 | 205 | return $discount; |
206 | 206 | } |
@@ -212,8 +212,8 @@ discard block |
||
212 | 212 | * @since 1.0.15 |
213 | 213 | * @return WPInv_Discount |
214 | 214 | */ |
215 | -function wpinv_get_discount_obj( $discount = 0 ) { |
|
216 | - return new WPInv_Discount( $discount ); |
|
215 | +function wpinv_get_discount_obj($discount = 0) { |
|
216 | + return new WPInv_Discount($discount); |
|
217 | 217 | } |
218 | 218 | |
219 | 219 | /** |
@@ -222,8 +222,8 @@ discard block |
||
222 | 222 | * @param string $code The discount code. |
223 | 223 | * @return bool|WPInv_Discount |
224 | 224 | */ |
225 | -function wpinv_get_discount_by_code( $code = '' ) { |
|
226 | - return wpinv_get_discount_by( null, $code ); |
|
225 | +function wpinv_get_discount_by_code($code = '') { |
|
226 | + return wpinv_get_discount_by(null, $code); |
|
227 | 227 | } |
228 | 228 | |
229 | 229 | /** |
@@ -233,10 +233,10 @@ discard block |
||
233 | 233 | * @param string|int $value The field value |
234 | 234 | * @return bool|WPInv_Discount |
235 | 235 | */ |
236 | -function wpinv_get_discount_by( $deprecated = null, $value = '' ) { |
|
237 | - $discount = new WPInv_Discount( $value ); |
|
236 | +function wpinv_get_discount_by($deprecated = null, $value = '') { |
|
237 | + $discount = new WPInv_Discount($value); |
|
238 | 238 | |
239 | - if ( $discount->get_id() != 0 ) { |
|
239 | + if ($discount->get_id() != 0) { |
|
240 | 240 | return $discount; |
241 | 241 | } |
242 | 242 | |
@@ -250,68 +250,68 @@ discard block |
||
250 | 250 | * @param array $data The discount's properties. |
251 | 251 | * @return bool |
252 | 252 | */ |
253 | -function wpinv_store_discount( $post_id, $data, $post, $update = false ) { |
|
253 | +function wpinv_store_discount($post_id, $data, $post, $update = false) { |
|
254 | 254 | $meta = array( |
255 | - 'code' => isset( $data['code'] ) ? sanitize_text_field( $data['code'] ) : '', |
|
256 | - 'type' => isset( $data['type'] ) ? sanitize_text_field( $data['type'] ) : 'percent', |
|
257 | - 'amount' => isset( $data['amount'] ) ? wpinv_sanitize_amount( $data['amount'] ) : '', |
|
258 | - 'start' => isset( $data['start'] ) ? sanitize_text_field( $data['start'] ) : '', |
|
259 | - 'expiration' => isset( $data['expiration'] ) ? sanitize_text_field( $data['expiration'] ) : '', |
|
260 | - 'min_total' => isset( $data['min_total'] ) ? wpinv_sanitize_amount( $data['min_total'] ) : '', |
|
261 | - 'max_total' => isset( $data['max_total'] ) ? wpinv_sanitize_amount( $data['max_total'] ) : '', |
|
262 | - 'max_uses' => isset( $data['max_uses'] ) ? absint( $data['max_uses'] ) : '', |
|
263 | - 'items' => isset( $data['items'] ) ? $data['items'] : array(), |
|
264 | - 'excluded_items' => isset( $data['excluded_items'] ) ? $data['excluded_items'] : array(), |
|
265 | - 'is_recurring' => isset( $data['recurring'] ) ? (bool)$data['recurring'] : false, |
|
266 | - 'is_single_use' => isset( $data['single_use'] ) ? (bool)$data['single_use'] : false, |
|
267 | - 'uses' => isset( $data['uses'] ) ? (int)$data['uses'] : false, |
|
255 | + 'code' => isset($data['code']) ? sanitize_text_field($data['code']) : '', |
|
256 | + 'type' => isset($data['type']) ? sanitize_text_field($data['type']) : 'percent', |
|
257 | + 'amount' => isset($data['amount']) ? wpinv_sanitize_amount($data['amount']) : '', |
|
258 | + 'start' => isset($data['start']) ? sanitize_text_field($data['start']) : '', |
|
259 | + 'expiration' => isset($data['expiration']) ? sanitize_text_field($data['expiration']) : '', |
|
260 | + 'min_total' => isset($data['min_total']) ? wpinv_sanitize_amount($data['min_total']) : '', |
|
261 | + 'max_total' => isset($data['max_total']) ? wpinv_sanitize_amount($data['max_total']) : '', |
|
262 | + 'max_uses' => isset($data['max_uses']) ? absint($data['max_uses']) : '', |
|
263 | + 'items' => isset($data['items']) ? $data['items'] : array(), |
|
264 | + 'excluded_items' => isset($data['excluded_items']) ? $data['excluded_items'] : array(), |
|
265 | + 'is_recurring' => isset($data['recurring']) ? (bool) $data['recurring'] : false, |
|
266 | + 'is_single_use' => isset($data['single_use']) ? (bool) $data['single_use'] : false, |
|
267 | + 'uses' => isset($data['uses']) ? (int) $data['uses'] : false, |
|
268 | 268 | ); |
269 | 269 | |
270 | - if ( $meta['type'] == 'percent' && (float)$meta['amount'] > 100 ) { |
|
270 | + if ($meta['type'] == 'percent' && (float) $meta['amount'] > 100) { |
|
271 | 271 | $meta['amount'] = 100; |
272 | 272 | } |
273 | 273 | |
274 | - if ( !empty( $meta['start'] ) ) { |
|
275 | - $meta['start'] = date_i18n( 'Y-m-d H:i:s', strtotime( $meta['start'] ) ); |
|
274 | + if (!empty($meta['start'])) { |
|
275 | + $meta['start'] = date_i18n('Y-m-d H:i:s', strtotime($meta['start'])); |
|
276 | 276 | } |
277 | 277 | |
278 | - if ( !empty( $meta['expiration'] ) ) { |
|
279 | - $meta['expiration'] = date_i18n( 'Y-m-d H:i:s', strtotime( $meta['expiration'] ) ); |
|
278 | + if (!empty($meta['expiration'])) { |
|
279 | + $meta['expiration'] = date_i18n('Y-m-d H:i:s', strtotime($meta['expiration'])); |
|
280 | 280 | |
281 | - if ( !empty( $meta['start'] ) && strtotime( $meta['start'] ) > strtotime( $meta['expiration'] ) ) { |
|
281 | + if (!empty($meta['start']) && strtotime($meta['start']) > strtotime($meta['expiration'])) { |
|
282 | 282 | $meta['expiration'] = $meta['start']; |
283 | 283 | } |
284 | 284 | } |
285 | 285 | |
286 | - if ( $meta['uses'] === false ) { |
|
287 | - unset( $meta['uses'] ); |
|
286 | + if ($meta['uses'] === false) { |
|
287 | + unset($meta['uses']); |
|
288 | 288 | } |
289 | 289 | |
290 | - if ( ! empty( $meta['items'] ) ) { |
|
291 | - foreach ( $meta['items'] as $key => $item ) { |
|
292 | - if ( 0 === intval( $item ) ) { |
|
293 | - unset( $meta['items'][ $key ] ); |
|
290 | + if (!empty($meta['items'])) { |
|
291 | + foreach ($meta['items'] as $key => $item) { |
|
292 | + if (0 === intval($item)) { |
|
293 | + unset($meta['items'][$key]); |
|
294 | 294 | } |
295 | 295 | } |
296 | 296 | } |
297 | 297 | |
298 | - if ( ! empty( $meta['excluded_items'] ) ) { |
|
299 | - foreach ( $meta['excluded_items'] as $key => $item ) { |
|
300 | - if ( 0 === intval( $item ) ) { |
|
301 | - unset( $meta['excluded_items'][ $key ] ); |
|
298 | + if (!empty($meta['excluded_items'])) { |
|
299 | + foreach ($meta['excluded_items'] as $key => $item) { |
|
300 | + if (0 === intval($item)) { |
|
301 | + unset($meta['excluded_items'][$key]); |
|
302 | 302 | } |
303 | 303 | } |
304 | 304 | } |
305 | 305 | |
306 | - $meta = apply_filters( 'wpinv_update_discount', $meta, $post_id, $post ); |
|
306 | + $meta = apply_filters('wpinv_update_discount', $meta, $post_id, $post); |
|
307 | 307 | |
308 | - do_action( 'wpinv_pre_update_discount', $meta, $post_id, $post ); |
|
308 | + do_action('wpinv_pre_update_discount', $meta, $post_id, $post); |
|
309 | 309 | |
310 | - foreach( $meta as $key => $value ) { |
|
311 | - update_post_meta( $post_id, '_wpi_discount_' . $key, $value ); |
|
310 | + foreach ($meta as $key => $value) { |
|
311 | + update_post_meta($post_id, '_wpi_discount_' . $key, $value); |
|
312 | 312 | } |
313 | 313 | |
314 | - do_action( 'wpinv_post_update_discount', $meta, $post_id, $post ); |
|
314 | + do_action('wpinv_post_update_discount', $meta, $post_id, $post); |
|
315 | 315 | |
316 | 316 | return $post_id; |
317 | 317 | } |
@@ -322,10 +322,10 @@ discard block |
||
322 | 322 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
323 | 323 | * @return bool |
324 | 324 | */ |
325 | -function wpinv_remove_discount( $discount = 0 ) { |
|
325 | +function wpinv_remove_discount($discount = 0) { |
|
326 | 326 | |
327 | - $discount = wpinv_get_discount_obj( $discount ); |
|
328 | - if( ! $discount->exists() ) { |
|
327 | + $discount = wpinv_get_discount_obj($discount); |
|
328 | + if (!$discount->exists()) { |
|
329 | 329 | return false; |
330 | 330 | } |
331 | 331 | |
@@ -340,9 +340,9 @@ discard block |
||
340 | 340 | * @param string $new_status |
341 | 341 | * @return bool |
342 | 342 | */ |
343 | -function wpinv_update_discount_status( $discount = 0, $new_status = 'publish' ) { |
|
344 | - $discount = wpinv_get_discount_obj( $discount ); |
|
345 | - return $discount->update_status( $new_status ); |
|
343 | +function wpinv_update_discount_status($discount = 0, $new_status = 'publish') { |
|
344 | + $discount = wpinv_get_discount_obj($discount); |
|
345 | + return $discount->update_status($new_status); |
|
346 | 346 | } |
347 | 347 | |
348 | 348 | /** |
@@ -351,48 +351,48 @@ discard block |
||
351 | 351 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
352 | 352 | * @return bool |
353 | 353 | */ |
354 | -function wpinv_discount_exists( $discount ) { |
|
355 | - $discount = wpinv_get_discount_obj( $discount ); |
|
354 | +function wpinv_discount_exists($discount) { |
|
355 | + $discount = wpinv_get_discount_obj($discount); |
|
356 | 356 | return $discount->exists(); |
357 | 357 | } |
358 | 358 | |
359 | -function wpinv_is_discount_active( $code_id = null, $silent = false ) { |
|
360 | - $discount = wpinv_get_discount( $code_id ); |
|
359 | +function wpinv_is_discount_active($code_id = null, $silent = false) { |
|
360 | + $discount = wpinv_get_discount($code_id); |
|
361 | 361 | $return = false; |
362 | 362 | |
363 | - if ( $discount ) { |
|
364 | - if ( wpinv_is_discount_expired( $code_id, $silent ) ) { |
|
365 | - if( defined( 'DOING_AJAX' ) && ! $silent ) { |
|
366 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is expired.', 'invoicing' ) ); |
|
363 | + if ($discount) { |
|
364 | + if (wpinv_is_discount_expired($code_id, $silent)) { |
|
365 | + if (defined('DOING_AJAX') && !$silent) { |
|
366 | + wpinv_set_error('wpinv-discount-error', __('This discount is expired.', 'invoicing')); |
|
367 | 367 | } |
368 | - } elseif ( $discount->post_status == 'publish' ) { |
|
368 | + } elseif ($discount->post_status == 'publish') { |
|
369 | 369 | $return = true; |
370 | 370 | } else { |
371 | - if( defined( 'DOING_AJAX' ) && ! $silent ) { |
|
372 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not active.', 'invoicing' ) ); |
|
371 | + if (defined('DOING_AJAX') && !$silent) { |
|
372 | + wpinv_set_error('wpinv-discount-error', __('This discount is not active.', 'invoicing')); |
|
373 | 373 | } |
374 | 374 | } |
375 | 375 | } |
376 | 376 | |
377 | - return apply_filters( 'wpinv_is_discount_active', $return, $code_id ); |
|
377 | + return apply_filters('wpinv_is_discount_active', $return, $code_id); |
|
378 | 378 | } |
379 | 379 | |
380 | -function wpinv_get_discount_code( $code_id = null ) { |
|
381 | - $code = get_post_meta( $code_id, '_wpi_discount_code', true ); |
|
380 | +function wpinv_get_discount_code($code_id = null) { |
|
381 | + $code = get_post_meta($code_id, '_wpi_discount_code', true); |
|
382 | 382 | |
383 | - return apply_filters( 'wpinv_get_discount_code', $code, $code_id ); |
|
383 | + return apply_filters('wpinv_get_discount_code', $code, $code_id); |
|
384 | 384 | } |
385 | 385 | |
386 | -function wpinv_get_discount_start_date( $code_id = null ) { |
|
387 | - $start_date = get_post_meta( $code_id, '_wpi_discount_start', true ); |
|
386 | +function wpinv_get_discount_start_date($code_id = null) { |
|
387 | + $start_date = get_post_meta($code_id, '_wpi_discount_start', true); |
|
388 | 388 | |
389 | - return apply_filters( 'wpinv_get_discount_start_date', $start_date, $code_id ); |
|
389 | + return apply_filters('wpinv_get_discount_start_date', $start_date, $code_id); |
|
390 | 390 | } |
391 | 391 | |
392 | -function wpinv_get_discount_expiration( $code_id = null ) { |
|
393 | - $expiration = get_post_meta( $code_id, '_wpi_discount_expiration', true ); |
|
392 | +function wpinv_get_discount_expiration($code_id = null) { |
|
393 | + $expiration = get_post_meta($code_id, '_wpi_discount_expiration', true); |
|
394 | 394 | |
395 | - return apply_filters( 'wpinv_get_discount_expiration', $expiration, $code_id ); |
|
395 | + return apply_filters('wpinv_get_discount_expiration', $expiration, $code_id); |
|
396 | 396 | } |
397 | 397 | |
398 | 398 | /** |
@@ -401,8 +401,8 @@ discard block |
||
401 | 401 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
402 | 402 | * @return int |
403 | 403 | */ |
404 | -function wpinv_get_discount_max_uses( $discount = array() ) { |
|
405 | - $discount = wpinv_get_discount_obj( $discount ); |
|
404 | +function wpinv_get_discount_max_uses($discount = array()) { |
|
405 | + $discount = wpinv_get_discount_obj($discount); |
|
406 | 406 | return (int) $discount->max_uses; |
407 | 407 | } |
408 | 408 | |
@@ -412,8 +412,8 @@ discard block |
||
412 | 412 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
413 | 413 | * @return int |
414 | 414 | */ |
415 | -function wpinv_get_discount_uses( $discount = array() ) { |
|
416 | - $discount = wpinv_get_discount_obj( $discount ); |
|
415 | +function wpinv_get_discount_uses($discount = array()) { |
|
416 | + $discount = wpinv_get_discount_obj($discount); |
|
417 | 417 | return (int) $discount->uses; |
418 | 418 | } |
419 | 419 | |
@@ -423,8 +423,8 @@ discard block |
||
423 | 423 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
424 | 424 | * @return float |
425 | 425 | */ |
426 | -function wpinv_get_discount_min_total( $discount = array() ) { |
|
427 | - $discount = wpinv_get_discount_obj( $discount ); |
|
426 | +function wpinv_get_discount_min_total($discount = array()) { |
|
427 | + $discount = wpinv_get_discount_obj($discount); |
|
428 | 428 | return (float) $discount->min_total; |
429 | 429 | } |
430 | 430 | |
@@ -434,8 +434,8 @@ discard block |
||
434 | 434 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
435 | 435 | * @return float |
436 | 436 | */ |
437 | -function wpinv_get_discount_max_total( $discount = array() ) { |
|
438 | - $discount = wpinv_get_discount_obj( $discount ); |
|
437 | +function wpinv_get_discount_max_total($discount = array()) { |
|
438 | + $discount = wpinv_get_discount_obj($discount); |
|
439 | 439 | return (float) $discount->max_total; |
440 | 440 | } |
441 | 441 | |
@@ -445,8 +445,8 @@ discard block |
||
445 | 445 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
446 | 446 | * @return float |
447 | 447 | */ |
448 | -function wpinv_get_discount_amount( $discount = array() ) { |
|
449 | - $discount = wpinv_get_discount_obj( $discount ); |
|
448 | +function wpinv_get_discount_amount($discount = array()) { |
|
449 | + $discount = wpinv_get_discount_obj($discount); |
|
450 | 450 | return (float) $discount->amount; |
451 | 451 | } |
452 | 452 | |
@@ -457,28 +457,28 @@ discard block |
||
457 | 457 | * @param bool $name |
458 | 458 | * @return string |
459 | 459 | */ |
460 | -function wpinv_get_discount_type( $discount = array(), $name = false ) { |
|
461 | - $discount = wpinv_get_discount_obj( $discount ); |
|
460 | +function wpinv_get_discount_type($discount = array(), $name = false) { |
|
461 | + $discount = wpinv_get_discount_obj($discount); |
|
462 | 462 | |
463 | 463 | // Are we returning the name or just the type. |
464 | - if( $name ) { |
|
464 | + if ($name) { |
|
465 | 465 | return $discount->type_name; |
466 | 466 | } |
467 | 467 | |
468 | 468 | return $discount->type; |
469 | 469 | } |
470 | 470 | |
471 | -function wpinv_discount_status( $status ) { |
|
472 | - switch( $status ){ |
|
471 | +function wpinv_discount_status($status) { |
|
472 | + switch ($status) { |
|
473 | 473 | case 'expired' : |
474 | - $name = __( 'Expired', 'invoicing' ); |
|
474 | + $name = __('Expired', 'invoicing'); |
|
475 | 475 | break; |
476 | 476 | case 'publish' : |
477 | 477 | case 'active' : |
478 | - $name = __( 'Active', 'invoicing' ); |
|
478 | + $name = __('Active', 'invoicing'); |
|
479 | 479 | break; |
480 | 480 | default : |
481 | - $name = __( 'Inactive', 'invoicing' ); |
|
481 | + $name = __('Inactive', 'invoicing'); |
|
482 | 482 | break; |
483 | 483 | } |
484 | 484 | return $name; |
@@ -490,8 +490,8 @@ discard block |
||
490 | 490 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
491 | 491 | * @return array |
492 | 492 | */ |
493 | -function wpinv_get_discount_excluded_items( $discount = array() ) { |
|
494 | - $discount = wpinv_get_discount_obj( $discount ); |
|
493 | +function wpinv_get_discount_excluded_items($discount = array()) { |
|
494 | + $discount = wpinv_get_discount_obj($discount); |
|
495 | 495 | return $discount->excluded_items; |
496 | 496 | } |
497 | 497 | |
@@ -501,17 +501,17 @@ discard block |
||
501 | 501 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
502 | 502 | * @return array |
503 | 503 | */ |
504 | -function wpinv_get_discount_item_reqs( $discount = array() ) { |
|
505 | - $discount = wpinv_get_discount_obj( $discount ); |
|
504 | +function wpinv_get_discount_item_reqs($discount = array()) { |
|
505 | + $discount = wpinv_get_discount_obj($discount); |
|
506 | 506 | return $discount->items; |
507 | 507 | } |
508 | 508 | |
509 | -function wpinv_get_discount_item_condition( $code_id = 0 ) { |
|
510 | - return get_post_meta( $code_id, '_wpi_discount_item_condition', true ); |
|
509 | +function wpinv_get_discount_item_condition($code_id = 0) { |
|
510 | + return get_post_meta($code_id, '_wpi_discount_item_condition', true); |
|
511 | 511 | } |
512 | 512 | |
513 | -function wpinv_is_discount_not_global( $code_id = 0 ) { |
|
514 | - return (bool) get_post_meta( $code_id, '_wpi_discount_is_not_global', true ); |
|
513 | +function wpinv_is_discount_not_global($code_id = 0) { |
|
514 | + return (bool) get_post_meta($code_id, '_wpi_discount_is_not_global', true); |
|
515 | 515 | } |
516 | 516 | |
517 | 517 | /** |
@@ -520,14 +520,14 @@ discard block |
||
520 | 520 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
521 | 521 | * @return bool |
522 | 522 | */ |
523 | -function wpinv_is_discount_expired( $discount = array(), $silent = false ) { |
|
524 | - $discount = wpinv_get_discount_obj( $discount ); |
|
523 | +function wpinv_is_discount_expired($discount = array(), $silent = false) { |
|
524 | + $discount = wpinv_get_discount_obj($discount); |
|
525 | 525 | |
526 | - if ( $discount->is_expired() ) { |
|
527 | - $discount->update_status( 'pending' ); |
|
526 | + if ($discount->is_expired()) { |
|
527 | + $discount->update_status('pending'); |
|
528 | 528 | |
529 | - if( empty( $silent ) ) { |
|
530 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount has expired.', 'invoicing' ) ); |
|
529 | + if (empty($silent)) { |
|
530 | + wpinv_set_error('wpinv-discount-error', __('This discount has expired.', 'invoicing')); |
|
531 | 531 | } |
532 | 532 | return true; |
533 | 533 | } |
@@ -541,12 +541,12 @@ discard block |
||
541 | 541 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
542 | 542 | * @return bool |
543 | 543 | */ |
544 | -function wpinv_is_discount_started( $discount = array() ) { |
|
545 | - $discount = wpinv_get_discount_obj( $discount ); |
|
544 | +function wpinv_is_discount_started($discount = array()) { |
|
545 | + $discount = wpinv_get_discount_obj($discount); |
|
546 | 546 | $started = $discount->has_started(); |
547 | 547 | |
548 | - if( empty( $started ) ) { |
|
549 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not active yet.', 'invoicing' ) ); |
|
548 | + if (empty($started)) { |
|
549 | + wpinv_set_error('wpinv-discount-error', __('This discount is not active yet.', 'invoicing')); |
|
550 | 550 | } |
551 | 551 | |
552 | 552 | return $started; |
@@ -558,10 +558,10 @@ discard block |
||
558 | 558 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
559 | 559 | * @return bool |
560 | 560 | */ |
561 | -function wpinv_check_discount_dates( $discount ) { |
|
562 | - $discount = wpinv_get_discount_obj( $discount ); |
|
563 | - $return = wpinv_is_discount_started( $discount ) && ! wpinv_is_discount_expired( $discount ); |
|
564 | - return apply_filters( 'wpinv_check_discount_dates', $return, $discount->ID, $discount, $discount->code ); |
|
561 | +function wpinv_check_discount_dates($discount) { |
|
562 | + $discount = wpinv_get_discount_obj($discount); |
|
563 | + $return = wpinv_is_discount_started($discount) && !wpinv_is_discount_expired($discount); |
|
564 | + return apply_filters('wpinv_check_discount_dates', $return, $discount->ID, $discount, $discount->code); |
|
565 | 565 | } |
566 | 566 | |
567 | 567 | /** |
@@ -570,12 +570,12 @@ discard block |
||
570 | 570 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
571 | 571 | * @return bool |
572 | 572 | */ |
573 | -function wpinv_is_discount_maxed_out( $discount ) { |
|
574 | - $discount = wpinv_get_discount_obj( $discount ); |
|
573 | +function wpinv_is_discount_maxed_out($discount) { |
|
574 | + $discount = wpinv_get_discount_obj($discount); |
|
575 | 575 | $maxed_out = $discount->has_exceeded_limit(); |
576 | 576 | |
577 | - if ( $maxed_out ) { |
|
578 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount has reached its maximum usage.', 'invoicing' ) ); |
|
577 | + if ($maxed_out) { |
|
578 | + wpinv_set_error('wpinv-discount-error', __('This discount has reached its maximum usage.', 'invoicing')); |
|
579 | 579 | } |
580 | 580 | |
581 | 581 | return $maxed_out; |
@@ -587,13 +587,13 @@ discard block |
||
587 | 587 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
588 | 588 | * @return bool |
589 | 589 | */ |
590 | -function wpinv_discount_is_min_met( $discount ) { |
|
591 | - $discount = wpinv_get_discount_obj( $discount ); |
|
592 | - $cart_amount = (float)wpinv_get_cart_discountable_subtotal( $discount->ID ); |
|
593 | - $min_met = $discount->is_minimum_amount_met( $cart_amount ); |
|
590 | +function wpinv_discount_is_min_met($discount) { |
|
591 | + $discount = wpinv_get_discount_obj($discount); |
|
592 | + $cart_amount = (float) wpinv_get_cart_discountable_subtotal($discount->ID); |
|
593 | + $min_met = $discount->is_minimum_amount_met($cart_amount); |
|
594 | 594 | |
595 | - if ( ! $min_met ) { |
|
596 | - wpinv_set_error( 'wpinv-discount-error', sprintf( __( 'Minimum invoice amount should be %s', 'invoicing' ), wpinv_price( wpinv_format_amount( $discount->min_total ) ) ) ); |
|
595 | + if (!$min_met) { |
|
596 | + wpinv_set_error('wpinv-discount-error', sprintf(__('Minimum invoice amount should be %s', 'invoicing'), wpinv_price(wpinv_format_amount($discount->min_total)))); |
|
597 | 597 | } |
598 | 598 | |
599 | 599 | return $min_met; |
@@ -605,13 +605,13 @@ discard block |
||
605 | 605 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
606 | 606 | * @return bool |
607 | 607 | */ |
608 | -function wpinv_discount_is_max_met( $discount ) { |
|
609 | - $discount = wpinv_get_discount_obj( $discount ); |
|
610 | - $cart_amount = (float)wpinv_get_cart_discountable_subtotal( $discount->ID ); |
|
611 | - $max_met = $discount->is_maximum_amount_met( $cart_amount ); |
|
608 | +function wpinv_discount_is_max_met($discount) { |
|
609 | + $discount = wpinv_get_discount_obj($discount); |
|
610 | + $cart_amount = (float) wpinv_get_cart_discountable_subtotal($discount->ID); |
|
611 | + $max_met = $discount->is_maximum_amount_met($cart_amount); |
|
612 | 612 | |
613 | - if ( ! $max_met ) { |
|
614 | - wpinv_set_error( 'wpinv-discount-error', sprintf( __( 'Maximum invoice amount should be %s', 'invoicing' ), wpinv_price( wpinv_format_amount( $discount->max_total ) ) ) ); |
|
613 | + if (!$max_met) { |
|
614 | + wpinv_set_error('wpinv-discount-error', sprintf(__('Maximum invoice amount should be %s', 'invoicing'), wpinv_price(wpinv_format_amount($discount->max_total)))); |
|
615 | 615 | } |
616 | 616 | |
617 | 617 | return $max_met; |
@@ -623,8 +623,8 @@ discard block |
||
623 | 623 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
624 | 624 | * @return bool |
625 | 625 | */ |
626 | -function wpinv_discount_is_single_use( $discount ) { |
|
627 | - $discount = wpinv_get_discount_obj( $discount ); |
|
626 | +function wpinv_discount_is_single_use($discount) { |
|
627 | + $discount = wpinv_get_discount_obj($discount); |
|
628 | 628 | return $discount->is_single_use; |
629 | 629 | } |
630 | 630 | |
@@ -635,53 +635,53 @@ discard block |
||
635 | 635 | * @param int|array|string|WPInv_Discount $code discount data, object, ID or code. |
636 | 636 | * @return bool |
637 | 637 | */ |
638 | -function wpinv_discount_is_recurring( $discount = 0, $code = 0 ) { |
|
638 | +function wpinv_discount_is_recurring($discount = 0, $code = 0) { |
|
639 | 639 | |
640 | - if( ! empty( $discount ) ) { |
|
641 | - $discount = wpinv_get_discount_obj( $discount ); |
|
640 | + if (!empty($discount)) { |
|
641 | + $discount = wpinv_get_discount_obj($discount); |
|
642 | 642 | } else { |
643 | - $discount = wpinv_get_discount_obj( $code ); |
|
643 | + $discount = wpinv_get_discount_obj($code); |
|
644 | 644 | } |
645 | 645 | |
646 | 646 | return $discount->get_is_recurring(); |
647 | 647 | } |
648 | 648 | |
649 | -function wpinv_discount_item_reqs_met( $code_id = null ) { |
|
650 | - $item_reqs = wpinv_get_discount_item_reqs( $code_id ); |
|
651 | - $condition = wpinv_get_discount_item_condition( $code_id ); |
|
652 | - $excluded_ps = wpinv_get_discount_excluded_items( $code_id ); |
|
649 | +function wpinv_discount_item_reqs_met($code_id = null) { |
|
650 | + $item_reqs = wpinv_get_discount_item_reqs($code_id); |
|
651 | + $condition = wpinv_get_discount_item_condition($code_id); |
|
652 | + $excluded_ps = wpinv_get_discount_excluded_items($code_id); |
|
653 | 653 | $cart_items = wpinv_get_cart_contents(); |
654 | - $cart_ids = $cart_items ? wp_list_pluck( $cart_items, 'id' ) : null; |
|
654 | + $cart_ids = $cart_items ? wp_list_pluck($cart_items, 'id') : null; |
|
655 | 655 | $ret = false; |
656 | 656 | |
657 | - if ( empty( $item_reqs ) && empty( $excluded_ps ) ) { |
|
657 | + if (empty($item_reqs) && empty($excluded_ps)) { |
|
658 | 658 | $ret = true; |
659 | 659 | } |
660 | 660 | |
661 | 661 | // Normalize our data for item requirements, exclusions and cart data |
662 | 662 | // First absint the items, then sort, and reset the array keys |
663 | - $item_reqs = array_map( 'absint', $item_reqs ); |
|
664 | - asort( $item_reqs ); |
|
665 | - $item_reqs = array_values( $item_reqs ); |
|
663 | + $item_reqs = array_map('absint', $item_reqs); |
|
664 | + asort($item_reqs); |
|
665 | + $item_reqs = array_values($item_reqs); |
|
666 | 666 | |
667 | - $excluded_ps = array_map( 'absint', $excluded_ps ); |
|
668 | - asort( $excluded_ps ); |
|
669 | - $excluded_ps = array_values( $excluded_ps ); |
|
667 | + $excluded_ps = array_map('absint', $excluded_ps); |
|
668 | + asort($excluded_ps); |
|
669 | + $excluded_ps = array_values($excluded_ps); |
|
670 | 670 | |
671 | - $cart_ids = array_map( 'absint', $cart_ids ); |
|
672 | - asort( $cart_ids ); |
|
673 | - $cart_ids = array_values( $cart_ids ); |
|
671 | + $cart_ids = array_map('absint', $cart_ids); |
|
672 | + asort($cart_ids); |
|
673 | + $cart_ids = array_values($cart_ids); |
|
674 | 674 | |
675 | 675 | // Ensure we have requirements before proceeding |
676 | - if ( !$ret && ! empty( $item_reqs ) ) { |
|
677 | - switch( $condition ) { |
|
676 | + if (!$ret && !empty($item_reqs)) { |
|
677 | + switch ($condition) { |
|
678 | 678 | case 'all' : |
679 | 679 | // Default back to true |
680 | 680 | $ret = true; |
681 | 681 | |
682 | - foreach ( $item_reqs as $item_id ) { |
|
683 | - if ( !wpinv_item_in_cart( $item_id ) ) { |
|
684 | - wpinv_set_error( 'wpinv-discount-error', __( 'The item requirements for this discount are not met.', 'invoicing' ) ); |
|
682 | + foreach ($item_reqs as $item_id) { |
|
683 | + if (!wpinv_item_in_cart($item_id)) { |
|
684 | + wpinv_set_error('wpinv-discount-error', __('The item requirements for this discount are not met.', 'invoicing')); |
|
685 | 685 | $ret = false; |
686 | 686 | break; |
687 | 687 | } |
@@ -690,15 +690,15 @@ discard block |
||
690 | 690 | break; |
691 | 691 | |
692 | 692 | default : // Any |
693 | - foreach ( $item_reqs as $item_id ) { |
|
694 | - if ( wpinv_item_in_cart( $item_id ) ) { |
|
693 | + foreach ($item_reqs as $item_id) { |
|
694 | + if (wpinv_item_in_cart($item_id)) { |
|
695 | 695 | $ret = true; |
696 | 696 | break; |
697 | 697 | } |
698 | 698 | } |
699 | 699 | |
700 | - if( ! $ret ) { |
|
701 | - wpinv_set_error( 'wpinv-discount-error', __( 'The item requirements for this discount are not met.', 'invoicing' ) ); |
|
700 | + if (!$ret) { |
|
701 | + wpinv_set_error('wpinv-discount-error', __('The item requirements for this discount are not met.', 'invoicing')); |
|
702 | 702 | } |
703 | 703 | |
704 | 704 | break; |
@@ -707,15 +707,15 @@ discard block |
||
707 | 707 | $ret = true; |
708 | 708 | } |
709 | 709 | |
710 | - if( ! empty( $excluded_ps ) ) { |
|
710 | + if (!empty($excluded_ps)) { |
|
711 | 711 | // Check that there are items other than excluded ones in the cart |
712 | - if( $cart_ids == $excluded_ps ) { |
|
713 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not valid for the cart contents.', 'invoicing' ) ); |
|
712 | + if ($cart_ids == $excluded_ps) { |
|
713 | + wpinv_set_error('wpinv-discount-error', __('This discount is not valid for the cart contents.', 'invoicing')); |
|
714 | 714 | $ret = false; |
715 | 715 | } |
716 | 716 | } |
717 | 717 | |
718 | - return (bool) apply_filters( 'wpinv_is_discount_item_req_met', $ret, $code_id, $condition ); |
|
718 | + return (bool) apply_filters('wpinv_is_discount_item_req_met', $ret, $code_id, $condition); |
|
719 | 719 | } |
720 | 720 | |
721 | 721 | /** |
@@ -726,54 +726,54 @@ discard block |
||
726 | 726 | * @param int|array|string|WPInv_Discount $code_id discount data, object, ID or code. |
727 | 727 | * @return bool |
728 | 728 | */ |
729 | -function wpinv_is_discount_used( $discount = array(), $user = '', $code_id = array() ) { |
|
729 | +function wpinv_is_discount_used($discount = array(), $user = '', $code_id = array()) { |
|
730 | 730 | |
731 | - if( ! empty( $discount ) ) { |
|
732 | - $discount = wpinv_get_discount_obj( $discount ); |
|
731 | + if (!empty($discount)) { |
|
732 | + $discount = wpinv_get_discount_obj($discount); |
|
733 | 733 | } else { |
734 | - $discount = wpinv_get_discount_obj( $code_id ); |
|
734 | + $discount = wpinv_get_discount_obj($code_id); |
|
735 | 735 | } |
736 | 736 | |
737 | - $is_used = ! $discount->is_valid_for_user( $user ); |
|
738 | - $is_used = apply_filters( 'wpinv_is_discount_used', $is_used, $discount->code, $user, $discount->ID, $discount ); |
|
737 | + $is_used = !$discount->is_valid_for_user($user); |
|
738 | + $is_used = apply_filters('wpinv_is_discount_used', $is_used, $discount->code, $user, $discount->ID, $discount); |
|
739 | 739 | |
740 | - if( $is_used ) { |
|
741 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount has already been redeemed.', 'invoicing' ) ); |
|
740 | + if ($is_used) { |
|
741 | + wpinv_set_error('wpinv-discount-error', __('This discount has already been redeemed.', 'invoicing')); |
|
742 | 742 | } |
743 | 743 | |
744 | 744 | return $is_used; |
745 | 745 | } |
746 | 746 | |
747 | -function wpinv_is_discount_valid( $code = '', $user = '', $set_error = true ) { |
|
747 | +function wpinv_is_discount_valid($code = '', $user = '', $set_error = true) { |
|
748 | 748 | |
749 | 749 | // Abort early if there is no discount code. |
750 | - if ( empty( $code ) ) { |
|
750 | + if (empty($code)) { |
|
751 | 751 | return false; |
752 | 752 | } |
753 | 753 | |
754 | 754 | $return = false; |
755 | - $discount_id = wpinv_get_discount_id_by_code( $code ); |
|
756 | - $user = trim( $user ); |
|
755 | + $discount_id = wpinv_get_discount_id_by_code($code); |
|
756 | + $user = trim($user); |
|
757 | 757 | |
758 | - if ( wpinv_get_cart_contents() ) { |
|
759 | - if ( $discount_id !== false ) { |
|
758 | + if (wpinv_get_cart_contents()) { |
|
759 | + if ($discount_id !== false) { |
|
760 | 760 | if ( |
761 | - wpinv_is_discount_active( $discount_id ) && |
|
762 | - wpinv_check_discount_dates( $discount_id ) && |
|
763 | - !wpinv_is_discount_maxed_out( $discount_id ) && |
|
764 | - !wpinv_is_discount_used( $code, $user, $discount_id ) && |
|
765 | - wpinv_discount_is_min_met( $discount_id ) && |
|
766 | - wpinv_discount_is_max_met( $discount_id ) && |
|
767 | - wpinv_discount_item_reqs_met( $discount_id ) |
|
761 | + wpinv_is_discount_active($discount_id) && |
|
762 | + wpinv_check_discount_dates($discount_id) && |
|
763 | + !wpinv_is_discount_maxed_out($discount_id) && |
|
764 | + !wpinv_is_discount_used($code, $user, $discount_id) && |
|
765 | + wpinv_discount_is_min_met($discount_id) && |
|
766 | + wpinv_discount_is_max_met($discount_id) && |
|
767 | + wpinv_discount_item_reqs_met($discount_id) |
|
768 | 768 | ) { |
769 | 769 | $return = true; |
770 | 770 | } |
771 | - } elseif( $set_error ) { |
|
772 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is invalid.', 'invoicing' ) ); |
|
771 | + } elseif ($set_error) { |
|
772 | + wpinv_set_error('wpinv-discount-error', __('This discount is invalid.', 'invoicing')); |
|
773 | 773 | } |
774 | 774 | } |
775 | 775 | |
776 | - return apply_filters( 'wpinv_is_discount_valid', $return, $discount_id, $code, $user ); |
|
776 | + return apply_filters('wpinv_is_discount_valid', $return, $discount_id, $code, $user); |
|
777 | 777 | } |
778 | 778 | |
779 | 779 | /** |
@@ -782,9 +782,9 @@ discard block |
||
782 | 782 | * @param string $code |
783 | 783 | * @return bool|false |
784 | 784 | */ |
785 | -function wpinv_get_discount_id_by_code( $code ) { |
|
786 | - $discount = wpinv_get_discount_by_code( $code ); |
|
787 | - if ( $discount ) { |
|
785 | +function wpinv_get_discount_id_by_code($code) { |
|
786 | + $discount = wpinv_get_discount_by_code($code); |
|
787 | + if ($discount) { |
|
788 | 788 | return $discount->get_id(); |
789 | 789 | } |
790 | 790 | return false; |
@@ -797,9 +797,9 @@ discard block |
||
797 | 797 | * @param float $base_price The number of usages to increase by |
798 | 798 | * @return float |
799 | 799 | */ |
800 | -function wpinv_get_discounted_amount( $discount, $base_price ) { |
|
801 | - $discount = wpinv_get_discount_obj( $discount ); |
|
802 | - return $discount->get_discounted_amount( $base_price ); |
|
800 | +function wpinv_get_discounted_amount($discount, $base_price) { |
|
801 | + $discount = wpinv_get_discount_obj($discount); |
|
802 | + return $discount->get_discounted_amount($base_price); |
|
803 | 803 | } |
804 | 804 | |
805 | 805 | /** |
@@ -809,9 +809,9 @@ discard block |
||
809 | 809 | * @param int $by The number of usages to increase by. |
810 | 810 | * @return int the new number of uses. |
811 | 811 | */ |
812 | -function wpinv_increase_discount_usage( $discount, $by = 1 ) { |
|
813 | - $discount = wpinv_get_discount_obj( $discount ); |
|
814 | - return $discount->increase_usage( $by ); |
|
812 | +function wpinv_increase_discount_usage($discount, $by = 1) { |
|
813 | + $discount = wpinv_get_discount_obj($discount); |
|
814 | + return $discount->increase_usage($by); |
|
815 | 815 | } |
816 | 816 | |
817 | 817 | /** |
@@ -821,72 +821,72 @@ discard block |
||
821 | 821 | * @param int $by The number of usages to decrease by. |
822 | 822 | * @return int the new number of uses. |
823 | 823 | */ |
824 | -function wpinv_decrease_discount_usage( $discount, $by = 1 ) { |
|
825 | - $discount = wpinv_get_discount_obj( $discount ); |
|
826 | - return $discount->increase_usage( 0 - $by ); |
|
824 | +function wpinv_decrease_discount_usage($discount, $by = 1) { |
|
825 | + $discount = wpinv_get_discount_obj($discount); |
|
826 | + return $discount->increase_usage(0 - $by); |
|
827 | 827 | } |
828 | 828 | |
829 | -function wpinv_format_discount_rate( $type, $amount ) { |
|
830 | - if ( $type == 'flat' ) { |
|
831 | - $rate = wpinv_price( wpinv_format_amount( $amount ) ); |
|
829 | +function wpinv_format_discount_rate($type, $amount) { |
|
830 | + if ($type == 'flat') { |
|
831 | + $rate = wpinv_price(wpinv_format_amount($amount)); |
|
832 | 832 | } else { |
833 | 833 | $rate = $amount . '%'; |
834 | 834 | } |
835 | 835 | |
836 | - return apply_filters( 'wpinv_format_discount_rate', $rate, $type, $amount ); |
|
836 | + return apply_filters('wpinv_format_discount_rate', $rate, $type, $amount); |
|
837 | 837 | } |
838 | 838 | |
839 | -function wpinv_set_cart_discount( $code = '' ) { |
|
840 | - if ( wpinv_multiple_discounts_allowed() ) { |
|
839 | +function wpinv_set_cart_discount($code = '') { |
|
840 | + if (wpinv_multiple_discounts_allowed()) { |
|
841 | 841 | // Get all active cart discounts |
842 | 842 | $discounts = wpinv_get_cart_discounts(); |
843 | 843 | } else { |
844 | 844 | $discounts = false; // Only one discount allowed per purchase, so override any existing |
845 | 845 | } |
846 | 846 | |
847 | - if ( $discounts ) { |
|
848 | - $key = array_search( strtolower( $code ), array_map( 'strtolower', $discounts ) ); |
|
849 | - if( false !== $key ) { |
|
850 | - unset( $discounts[ $key ] ); // Can't set the same discount more than once |
|
847 | + if ($discounts) { |
|
848 | + $key = array_search(strtolower($code), array_map('strtolower', $discounts)); |
|
849 | + if (false !== $key) { |
|
850 | + unset($discounts[$key]); // Can't set the same discount more than once |
|
851 | 851 | } |
852 | 852 | $discounts[] = $code; |
853 | 853 | } else { |
854 | 854 | $discounts = array(); |
855 | 855 | $discounts[] = $code; |
856 | 856 | } |
857 | - $discounts = array_values( $discounts ); |
|
857 | + $discounts = array_values($discounts); |
|
858 | 858 | |
859 | 859 | $data = wpinv_get_checkout_session(); |
860 | - if ( empty( $data ) ) { |
|
860 | + if (empty($data)) { |
|
861 | 861 | $data = array(); |
862 | 862 | } else { |
863 | - if ( !empty( $data['invoice_id'] ) && $payment_meta = wpinv_get_invoice_meta( $data['invoice_id'] ) ) { |
|
864 | - $payment_meta['user_info']['discount'] = implode( ',', $discounts ); |
|
865 | - update_post_meta( $data['invoice_id'], '_wpinv_payment_meta', $payment_meta ); |
|
863 | + if (!empty($data['invoice_id']) && $payment_meta = wpinv_get_invoice_meta($data['invoice_id'])) { |
|
864 | + $payment_meta['user_info']['discount'] = implode(',', $discounts); |
|
865 | + update_post_meta($data['invoice_id'], '_wpinv_payment_meta', $payment_meta); |
|
866 | 866 | } |
867 | 867 | } |
868 | 868 | $data['cart_discounts'] = $discounts; |
869 | 869 | |
870 | - wpinv_set_checkout_session( $data ); |
|
870 | + wpinv_set_checkout_session($data); |
|
871 | 871 | |
872 | 872 | return $discounts; |
873 | 873 | } |
874 | 874 | |
875 | -function wpinv_unset_cart_discount( $code = '' ) { |
|
875 | +function wpinv_unset_cart_discount($code = '') { |
|
876 | 876 | $discounts = wpinv_get_cart_discounts(); |
877 | 877 | |
878 | - if ( $code && !empty( $discounts ) && in_array( $code, $discounts ) ) { |
|
879 | - $key = array_search( $code, $discounts ); |
|
880 | - unset( $discounts[ $key ] ); |
|
878 | + if ($code && !empty($discounts) && in_array($code, $discounts)) { |
|
879 | + $key = array_search($code, $discounts); |
|
880 | + unset($discounts[$key]); |
|
881 | 881 | |
882 | 882 | $data = wpinv_get_checkout_session(); |
883 | 883 | $data['cart_discounts'] = $discounts; |
884 | - if ( !empty( $data['invoice_id'] ) && $payment_meta = wpinv_get_invoice_meta( $data['invoice_id'] ) ) { |
|
885 | - $payment_meta['user_info']['discount'] = !empty( $discounts ) ? implode( ',', $discounts ) : ''; |
|
886 | - update_post_meta( $data['invoice_id'], '_wpinv_payment_meta', $payment_meta ); |
|
884 | + if (!empty($data['invoice_id']) && $payment_meta = wpinv_get_invoice_meta($data['invoice_id'])) { |
|
885 | + $payment_meta['user_info']['discount'] = !empty($discounts) ? implode(',', $discounts) : ''; |
|
886 | + update_post_meta($data['invoice_id'], '_wpinv_payment_meta', $payment_meta); |
|
887 | 887 | } |
888 | 888 | |
889 | - wpinv_set_checkout_session( $data ); |
|
889 | + wpinv_set_checkout_session($data); |
|
890 | 890 | } |
891 | 891 | |
892 | 892 | return $discounts; |
@@ -895,10 +895,10 @@ discard block |
||
895 | 895 | function wpinv_unset_all_cart_discounts() { |
896 | 896 | $data = wpinv_get_checkout_session(); |
897 | 897 | |
898 | - if ( !empty( $data ) && isset( $data['cart_discounts'] ) ) { |
|
899 | - unset( $data['cart_discounts'] ); |
|
898 | + if (!empty($data) && isset($data['cart_discounts'])) { |
|
899 | + unset($data['cart_discounts']); |
|
900 | 900 | |
901 | - wpinv_set_checkout_session( $data ); |
|
901 | + wpinv_set_checkout_session($data); |
|
902 | 902 | return true; |
903 | 903 | } |
904 | 904 | |
@@ -907,13 +907,13 @@ discard block |
||
907 | 907 | |
908 | 908 | function wpinv_get_cart_discounts() { |
909 | 909 | $session = wpinv_get_checkout_session(); |
910 | - return empty( $session['cart_discounts'] ) ? false : $session['cart_discounts']; |
|
910 | + return empty($session['cart_discounts']) ? false : $session['cart_discounts']; |
|
911 | 911 | } |
912 | 912 | |
913 | -function wpinv_cart_has_discounts( $items = array() ) { |
|
913 | +function wpinv_cart_has_discounts($items = array()) { |
|
914 | 914 | $ret = false; |
915 | 915 | |
916 | - if ( wpinv_get_cart_discounts( $items ) ) { |
|
916 | + if (wpinv_get_cart_discounts($items)) { |
|
917 | 917 | $ret = true; |
918 | 918 | } |
919 | 919 | |
@@ -924,131 +924,131 @@ discard block |
||
924 | 924 | } |
925 | 925 | */ |
926 | 926 | |
927 | - return apply_filters( 'wpinv_cart_has_discounts', $ret ); |
|
927 | + return apply_filters('wpinv_cart_has_discounts', $ret); |
|
928 | 928 | } |
929 | 929 | |
930 | -function wpinv_get_cart_discounted_amount( $items = array(), $discounts = false ) { |
|
930 | +function wpinv_get_cart_discounted_amount($items = array(), $discounts = false) { |
|
931 | 931 | $amount = 0.00; |
932 | - $items = !empty( $items ) ? $items : wpinv_get_cart_content_details(); |
|
932 | + $items = !empty($items) ? $items : wpinv_get_cart_content_details(); |
|
933 | 933 | |
934 | - if ( $items ) { |
|
935 | - $discounts = wp_list_pluck( $items, 'discount' ); |
|
934 | + if ($items) { |
|
935 | + $discounts = wp_list_pluck($items, 'discount'); |
|
936 | 936 | |
937 | - if ( is_array( $discounts ) ) { |
|
938 | - $discounts = array_map( 'floatval', $discounts ); |
|
939 | - $amount = array_sum( $discounts ); |
|
937 | + if (is_array($discounts)) { |
|
938 | + $discounts = array_map('floatval', $discounts); |
|
939 | + $amount = array_sum($discounts); |
|
940 | 940 | } |
941 | 941 | } |
942 | 942 | |
943 | - return apply_filters( 'wpinv_get_cart_discounted_amount', $amount ); |
|
943 | + return apply_filters('wpinv_get_cart_discounted_amount', $amount); |
|
944 | 944 | } |
945 | 945 | |
946 | -function wpinv_get_cart_items_discount_amount( $items = array(), $discount = false ) { |
|
947 | - $items = !empty( $items ) ? $items : wpinv_get_cart_content_details(); |
|
946 | +function wpinv_get_cart_items_discount_amount($items = array(), $discount = false) { |
|
947 | + $items = !empty($items) ? $items : wpinv_get_cart_content_details(); |
|
948 | 948 | |
949 | - if ( empty( $discount ) || empty( $items ) ) { |
|
949 | + if (empty($discount) || empty($items)) { |
|
950 | 950 | return 0; |
951 | 951 | } |
952 | 952 | |
953 | 953 | $amount = 0; |
954 | 954 | |
955 | - foreach ( $items as $item ) { |
|
956 | - $amount += wpinv_get_cart_item_discount_amount( $item, $discount ); |
|
955 | + foreach ($items as $item) { |
|
956 | + $amount += wpinv_get_cart_item_discount_amount($item, $discount); |
|
957 | 957 | } |
958 | 958 | |
959 | - $amount = wpinv_round_amount( $amount ); |
|
959 | + $amount = wpinv_round_amount($amount); |
|
960 | 960 | |
961 | 961 | return $amount; |
962 | 962 | } |
963 | 963 | |
964 | -function wpinv_get_cart_item_discount_amount( $item = array(), $discount = false ) { |
|
964 | +function wpinv_get_cart_item_discount_amount($item = array(), $discount = false) { |
|
965 | 965 | global $wpinv_is_last_cart_item, $wpinv_flat_discount_total; |
966 | 966 | |
967 | 967 | $amount = 0; |
968 | 968 | |
969 | - if ( empty( $item ) || empty( $item['id'] ) ) { |
|
969 | + if (empty($item) || empty($item['id'])) { |
|
970 | 970 | return $amount; |
971 | 971 | } |
972 | 972 | |
973 | - if ( empty( $item['quantity'] ) ) { |
|
973 | + if (empty($item['quantity'])) { |
|
974 | 974 | return $amount; |
975 | 975 | } |
976 | 976 | |
977 | - if ( empty( $item['options'] ) ) { |
|
977 | + if (empty($item['options'])) { |
|
978 | 978 | $item['options'] = array(); |
979 | 979 | } |
980 | 980 | |
981 | - $price = wpinv_get_cart_item_price( $item['id'], $item, $item['options'] ); |
|
981 | + $price = wpinv_get_cart_item_price($item['id'], $item, $item['options']); |
|
982 | 982 | $discounted_price = $price; |
983 | 983 | |
984 | 984 | $discounts = false === $discount ? wpinv_get_cart_discounts() : $discount; |
985 | - if ( empty( $discounts ) ) { |
|
985 | + if (empty($discounts)) { |
|
986 | 986 | return $amount; |
987 | 987 | } |
988 | 988 | |
989 | - if ( $discounts ) { |
|
990 | - if ( is_array( $discounts ) ) { |
|
991 | - $discounts = array_values( $discounts ); |
|
989 | + if ($discounts) { |
|
990 | + if (is_array($discounts)) { |
|
991 | + $discounts = array_values($discounts); |
|
992 | 992 | } else { |
993 | - $discounts = explode( ',', $discounts ); |
|
993 | + $discounts = explode(',', $discounts); |
|
994 | 994 | } |
995 | 995 | } |
996 | 996 | |
997 | - if( $discounts ) { |
|
998 | - foreach ( $discounts as $discount ) { |
|
999 | - $code_id = wpinv_get_discount_id_by_code( $discount ); |
|
997 | + if ($discounts) { |
|
998 | + foreach ($discounts as $discount) { |
|
999 | + $code_id = wpinv_get_discount_id_by_code($discount); |
|
1000 | 1000 | |
1001 | 1001 | // Check discount exists |
1002 | - if( $code_id === false ) { |
|
1002 | + if ($code_id === false) { |
|
1003 | 1003 | continue; |
1004 | 1004 | } |
1005 | 1005 | |
1006 | - $reqs = wpinv_get_discount_item_reqs( $code_id ); |
|
1007 | - $excluded_items = wpinv_get_discount_excluded_items( $code_id ); |
|
1006 | + $reqs = wpinv_get_discount_item_reqs($code_id); |
|
1007 | + $excluded_items = wpinv_get_discount_excluded_items($code_id); |
|
1008 | 1008 | |
1009 | 1009 | // Make sure requirements are set and that this discount shouldn't apply to the whole cart |
1010 | - if ( !empty( $reqs ) && wpinv_is_discount_not_global( $code_id ) ) { |
|
1011 | - foreach ( $reqs as $item_id ) { |
|
1012 | - if ( $item_id == $item['id'] && ! in_array( $item['id'], $excluded_items ) ) { |
|
1013 | - $discounted_price -= wpinv_get_discounted_amount( $discount, $price ); |
|
1010 | + if (!empty($reqs) && wpinv_is_discount_not_global($code_id)) { |
|
1011 | + foreach ($reqs as $item_id) { |
|
1012 | + if ($item_id == $item['id'] && !in_array($item['id'], $excluded_items)) { |
|
1013 | + $discounted_price -= wpinv_get_discounted_amount($discount, $price); |
|
1014 | 1014 | } |
1015 | 1015 | } |
1016 | 1016 | } else { |
1017 | 1017 | // This is a global cart discount |
1018 | - if ( !in_array( $item['id'], $excluded_items ) ) { |
|
1019 | - if ( 'flat' === wpinv_get_discount_type( $code_id ) ) { |
|
1018 | + if (!in_array($item['id'], $excluded_items)) { |
|
1019 | + if ('flat' === wpinv_get_discount_type($code_id)) { |
|
1020 | 1020 | $items_subtotal = 0.00; |
1021 | 1021 | $cart_items = wpinv_get_cart_contents(); |
1022 | 1022 | |
1023 | - foreach ( $cart_items as $cart_item ) { |
|
1024 | - if ( ! in_array( $cart_item['id'], $excluded_items ) ) { |
|
1025 | - $options = !empty( $cart_item['options'] ) ? $cart_item['options'] : array(); |
|
1026 | - $item_price = wpinv_get_cart_item_price( $cart_item['id'], $cart_item, $options ); |
|
1023 | + foreach ($cart_items as $cart_item) { |
|
1024 | + if (!in_array($cart_item['id'], $excluded_items)) { |
|
1025 | + $options = !empty($cart_item['options']) ? $cart_item['options'] : array(); |
|
1026 | + $item_price = wpinv_get_cart_item_price($cart_item['id'], $cart_item, $options); |
|
1027 | 1027 | $items_subtotal += $item_price * $cart_item['quantity']; |
1028 | 1028 | } |
1029 | 1029 | } |
1030 | 1030 | |
1031 | - $subtotal_percent = ( ( $price * $item['quantity'] ) / $items_subtotal ); |
|
1032 | - $code_amount = wpinv_get_discount_amount( $code_id ); |
|
1031 | + $subtotal_percent = (($price * $item['quantity']) / $items_subtotal); |
|
1032 | + $code_amount = wpinv_get_discount_amount($code_id); |
|
1033 | 1033 | $discounted_amount = $code_amount * $subtotal_percent; |
1034 | 1034 | $discounted_price -= $discounted_amount; |
1035 | 1035 | |
1036 | - $wpinv_flat_discount_total += round( $discounted_amount, wpinv_currency_decimal_filter() ); |
|
1036 | + $wpinv_flat_discount_total += round($discounted_amount, wpinv_currency_decimal_filter()); |
|
1037 | 1037 | |
1038 | - if ( $wpinv_is_last_cart_item && $wpinv_flat_discount_total < $code_amount ) { |
|
1038 | + if ($wpinv_is_last_cart_item && $wpinv_flat_discount_total < $code_amount) { |
|
1039 | 1039 | $adjustment = $code_amount - $wpinv_flat_discount_total; |
1040 | 1040 | $discounted_price -= $adjustment; |
1041 | 1041 | } |
1042 | 1042 | } else { |
1043 | - $discounted_price -= wpinv_get_discounted_amount( $discount, $price ); |
|
1043 | + $discounted_price -= wpinv_get_discounted_amount($discount, $price); |
|
1044 | 1044 | } |
1045 | 1045 | } |
1046 | 1046 | } |
1047 | 1047 | } |
1048 | 1048 | |
1049 | - $amount = ( $price - apply_filters( 'wpinv_get_cart_item_discounted_amount', $discounted_price, $discounts, $item, $price ) ); |
|
1049 | + $amount = ($price - apply_filters('wpinv_get_cart_item_discounted_amount', $discounted_price, $discounts, $item, $price)); |
|
1050 | 1050 | |
1051 | - if ( 'flat' !== wpinv_get_discount_type( $code_id ) ) { |
|
1051 | + if ('flat' !== wpinv_get_discount_type($code_id)) { |
|
1052 | 1052 | $amount = $amount * $item['quantity']; |
1053 | 1053 | } |
1054 | 1054 | } |
@@ -1056,59 +1056,59 @@ discard block |
||
1056 | 1056 | return $amount; |
1057 | 1057 | } |
1058 | 1058 | |
1059 | -function wpinv_cart_discounts_html( $items = array() ) { |
|
1060 | - echo wpinv_get_cart_discounts_html( $items ); |
|
1059 | +function wpinv_cart_discounts_html($items = array()) { |
|
1060 | + echo wpinv_get_cart_discounts_html($items); |
|
1061 | 1061 | } |
1062 | 1062 | |
1063 | -function wpinv_get_cart_discounts_html( $items = array(), $discounts = false ) { |
|
1063 | +function wpinv_get_cart_discounts_html($items = array(), $discounts = false) { |
|
1064 | 1064 | global $wpi_cart_columns; |
1065 | 1065 | |
1066 | - $items = !empty( $items ) ? $items : wpinv_get_cart_content_details(); |
|
1066 | + $items = !empty($items) ? $items : wpinv_get_cart_content_details(); |
|
1067 | 1067 | |
1068 | - if ( !$discounts ) { |
|
1069 | - $discounts = wpinv_get_cart_discounts( $items ); |
|
1068 | + if (!$discounts) { |
|
1069 | + $discounts = wpinv_get_cart_discounts($items); |
|
1070 | 1070 | } |
1071 | 1071 | |
1072 | - if ( !$discounts ) { |
|
1072 | + if (!$discounts) { |
|
1073 | 1073 | return; |
1074 | 1074 | } |
1075 | 1075 | |
1076 | - $discounts = is_array( $discounts ) ? $discounts : array( $discounts ); |
|
1076 | + $discounts = is_array($discounts) ? $discounts : array($discounts); |
|
1077 | 1077 | |
1078 | 1078 | $html = ''; |
1079 | 1079 | |
1080 | - foreach ( $discounts as $discount ) { |
|
1081 | - $discount_id = wpinv_get_discount_id_by_code( $discount ); |
|
1082 | - $discount_value = wpinv_get_discount_amount( $discount_id ); |
|
1083 | - $rate = wpinv_format_discount_rate( wpinv_get_discount_type( $discount_id ), $discount_value ); |
|
1084 | - $amount = wpinv_get_cart_items_discount_amount( $items, $discount ); |
|
1085 | - $remove_btn = '<a title="' . esc_attr__( 'Remove discount', 'invoicing' ) . '" data-code="' . $discount . '" data-value="' . $discount_value . '" class="wpi-discount-remove" href="javascript:void(0);">[<i class="fa fa-times" aria-hidden="true"></i>]</a> '; |
|
1080 | + foreach ($discounts as $discount) { |
|
1081 | + $discount_id = wpinv_get_discount_id_by_code($discount); |
|
1082 | + $discount_value = wpinv_get_discount_amount($discount_id); |
|
1083 | + $rate = wpinv_format_discount_rate(wpinv_get_discount_type($discount_id), $discount_value); |
|
1084 | + $amount = wpinv_get_cart_items_discount_amount($items, $discount); |
|
1085 | + $remove_btn = '<a title="' . esc_attr__('Remove discount', 'invoicing') . '" data-code="' . $discount . '" data-value="' . $discount_value . '" class="wpi-discount-remove" href="javascript:void(0);">[<i class="fa fa-times" aria-hidden="true"></i>]</a> '; |
|
1086 | 1086 | |
1087 | 1087 | $html .= '<tr class="wpinv_cart_footer_row wpinv_cart_discount_row">'; |
1088 | 1088 | ob_start(); |
1089 | - do_action( 'wpinv_checkout_table_discount_first', $items ); |
|
1089 | + do_action('wpinv_checkout_table_discount_first', $items); |
|
1090 | 1090 | $html .= ob_get_clean(); |
1091 | - $html .= '<td class="wpinv_cart_discount_label text-right" colspan="' . $wpi_cart_columns . '">' . $remove_btn . '<strong>' . wpinv_cart_discount_label( $discount, $rate, false ) . '</strong></td><td class="wpinv_cart_discount text-right"><span data-discount="' . $amount . '" class="wpinv_cart_discount_amount">–' . wpinv_price( wpinv_format_amount( $amount ) ) . '</span></td>'; |
|
1091 | + $html .= '<td class="wpinv_cart_discount_label text-right" colspan="' . $wpi_cart_columns . '">' . $remove_btn . '<strong>' . wpinv_cart_discount_label($discount, $rate, false) . '</strong></td><td class="wpinv_cart_discount text-right"><span data-discount="' . $amount . '" class="wpinv_cart_discount_amount">–' . wpinv_price(wpinv_format_amount($amount)) . '</span></td>'; |
|
1092 | 1092 | ob_start(); |
1093 | - do_action( 'wpinv_checkout_table_discount_last', $items ); |
|
1093 | + do_action('wpinv_checkout_table_discount_last', $items); |
|
1094 | 1094 | $html .= ob_get_clean(); |
1095 | 1095 | $html .= '</tr>'; |
1096 | 1096 | } |
1097 | 1097 | |
1098 | - return apply_filters( 'wpinv_get_cart_discounts_html', $html, $discounts, $rate ); |
|
1098 | + return apply_filters('wpinv_get_cart_discounts_html', $html, $discounts, $rate); |
|
1099 | 1099 | } |
1100 | 1100 | |
1101 | -function wpinv_display_cart_discount( /** @scrutinizer ignore-unused */ $formatted = false, $echo = false ) { |
|
1101 | +function wpinv_display_cart_discount(/** @scrutinizer ignore-unused */ $formatted = false, $echo = false) { |
|
1102 | 1102 | $discounts = wpinv_get_cart_discounts(); |
1103 | 1103 | |
1104 | - if ( empty( $discounts ) ) { |
|
1104 | + if (empty($discounts)) { |
|
1105 | 1105 | return false; |
1106 | 1106 | } |
1107 | 1107 | |
1108 | - $discount_id = wpinv_get_discount_id_by_code( $discounts[0] ); |
|
1109 | - $amount = wpinv_format_discount_rate( wpinv_get_discount_type( $discount_id ), wpinv_get_discount_amount( $discount_id ) ); |
|
1108 | + $discount_id = wpinv_get_discount_id_by_code($discounts[0]); |
|
1109 | + $amount = wpinv_format_discount_rate(wpinv_get_discount_type($discount_id), wpinv_get_discount_amount($discount_id)); |
|
1110 | 1110 | |
1111 | - if ( $echo ) { |
|
1111 | + if ($echo) { |
|
1112 | 1112 | echo $amount; |
1113 | 1113 | } |
1114 | 1114 | |
@@ -1116,65 +1116,65 @@ discard block |
||
1116 | 1116 | } |
1117 | 1117 | |
1118 | 1118 | function wpinv_remove_cart_discount() { |
1119 | - if ( !isset( $_GET['discount_id'] ) || ! isset( $_GET['discount_code'] ) ) { |
|
1119 | + if (!isset($_GET['discount_id']) || !isset($_GET['discount_code'])) { |
|
1120 | 1120 | return; |
1121 | 1121 | } |
1122 | 1122 | |
1123 | - do_action( 'wpinv_pre_remove_cart_discount', absint( $_GET['discount_id'] ) ); |
|
1123 | + do_action('wpinv_pre_remove_cart_discount', absint($_GET['discount_id'])); |
|
1124 | 1124 | |
1125 | - wpinv_unset_cart_discount( urldecode( $_GET['discount_code'] ) ); |
|
1125 | + wpinv_unset_cart_discount(urldecode($_GET['discount_code'])); |
|
1126 | 1126 | |
1127 | - do_action( 'wpinv_post_remove_cart_discount', absint( $_GET['discount_id'] ) ); |
|
1127 | + do_action('wpinv_post_remove_cart_discount', absint($_GET['discount_id'])); |
|
1128 | 1128 | |
1129 | - wp_redirect( wpinv_get_checkout_uri() ); wpinv_die(); |
|
1129 | + wp_redirect(wpinv_get_checkout_uri()); wpinv_die(); |
|
1130 | 1130 | } |
1131 | -add_action( 'wpinv_remove_cart_discount', 'wpinv_remove_cart_discount' ); |
|
1131 | +add_action('wpinv_remove_cart_discount', 'wpinv_remove_cart_discount'); |
|
1132 | 1132 | |
1133 | 1133 | function wpinv_maybe_remove_cart_discount() { |
1134 | 1134 | $discounts = wpinv_get_cart_discounts(); |
1135 | 1135 | |
1136 | - if ( !$discounts ) { |
|
1136 | + if (!$discounts) { |
|
1137 | 1137 | return; |
1138 | 1138 | } |
1139 | 1139 | |
1140 | - $discounts = array_filter( $discounts ); |
|
1141 | - foreach ( $discounts as $discount ) { |
|
1142 | - if ( !wpinv_is_discount_valid( $discount ) ) { |
|
1143 | - wpinv_unset_cart_discount( $discount ); |
|
1140 | + $discounts = array_filter($discounts); |
|
1141 | + foreach ($discounts as $discount) { |
|
1142 | + if (!wpinv_is_discount_valid($discount)) { |
|
1143 | + wpinv_unset_cart_discount($discount); |
|
1144 | 1144 | } |
1145 | 1145 | } |
1146 | 1146 | } |
1147 | -add_action( 'wpinv_post_remove_from_cart', 'wpinv_maybe_remove_cart_discount' ); |
|
1147 | +add_action('wpinv_post_remove_from_cart', 'wpinv_maybe_remove_cart_discount'); |
|
1148 | 1148 | |
1149 | 1149 | function wpinv_multiple_discounts_allowed() { |
1150 | - $ret = wpinv_get_option( 'allow_multiple_discounts', false ); |
|
1151 | - return (bool) apply_filters( 'wpinv_multiple_discounts_allowed', $ret ); |
|
1150 | + $ret = wpinv_get_option('allow_multiple_discounts', false); |
|
1151 | + return (bool) apply_filters('wpinv_multiple_discounts_allowed', $ret); |
|
1152 | 1152 | } |
1153 | 1153 | |
1154 | -function wpinv_get_discount_label( $code, $echo = true ) { |
|
1155 | - $label = wp_sprintf( __( 'Discount%1$s', 'invoicing' ), ( $code != '' && $code != 'none' ? ' (<code>' . $code . '</code>)': '' ) ); |
|
1156 | - $label = apply_filters( 'wpinv_get_discount_label', $label, $code ); |
|
1154 | +function wpinv_get_discount_label($code, $echo = true) { |
|
1155 | + $label = wp_sprintf(__('Discount%1$s', 'invoicing'), ($code != '' && $code != 'none' ? ' (<code>' . $code . '</code>)' : '')); |
|
1156 | + $label = apply_filters('wpinv_get_discount_label', $label, $code); |
|
1157 | 1157 | |
1158 | - if ( $echo ) { |
|
1158 | + if ($echo) { |
|
1159 | 1159 | echo $label; |
1160 | 1160 | } else { |
1161 | 1161 | return $label; |
1162 | 1162 | } |
1163 | 1163 | } |
1164 | 1164 | |
1165 | -function wpinv_cart_discount_label( $code, $rate, $echo = true ) { |
|
1166 | - $label = wp_sprintf( __( 'Discount: %s', 'invoicing' ), $code ); |
|
1167 | - $label = apply_filters( 'wpinv_cart_discount_label', $label, $code, $rate ); |
|
1165 | +function wpinv_cart_discount_label($code, $rate, $echo = true) { |
|
1166 | + $label = wp_sprintf(__('Discount: %s', 'invoicing'), $code); |
|
1167 | + $label = apply_filters('wpinv_cart_discount_label', $label, $code, $rate); |
|
1168 | 1168 | |
1169 | - if ( $echo ) { |
|
1169 | + if ($echo) { |
|
1170 | 1170 | echo $label; |
1171 | 1171 | } else { |
1172 | 1172 | return $label; |
1173 | 1173 | } |
1174 | 1174 | } |
1175 | 1175 | |
1176 | -function wpinv_check_delete_discount( $check, $post ) { |
|
1177 | - if ( $post->post_type == 'wpi_discount' && wpinv_get_discount_uses( $post->ID ) > 0 ) { |
|
1176 | +function wpinv_check_delete_discount($check, $post) { |
|
1177 | + if ($post->post_type == 'wpi_discount' && wpinv_get_discount_uses($post->ID) > 0) { |
|
1178 | 1178 | return true; |
1179 | 1179 | } |
1180 | 1180 | |
@@ -1185,33 +1185,33 @@ discard block |
||
1185 | 1185 | global $wpi_checkout_id; |
1186 | 1186 | |
1187 | 1187 | $discounts = wpinv_get_cart_discounts(); |
1188 | - $discounts = array_filter( $discounts ); |
|
1188 | + $discounts = array_filter($discounts); |
|
1189 | 1189 | |
1190 | - if ( !empty( $discounts ) ) { |
|
1190 | + if (!empty($discounts)) { |
|
1191 | 1191 | $invalid = false; |
1192 | 1192 | |
1193 | - foreach ( $discounts as $key => $code ) { |
|
1194 | - if ( !wpinv_is_discount_valid( $code, (int)wpinv_get_user_id( $wpi_checkout_id ) ) ) { |
|
1193 | + foreach ($discounts as $key => $code) { |
|
1194 | + if (!wpinv_is_discount_valid($code, (int) wpinv_get_user_id($wpi_checkout_id))) { |
|
1195 | 1195 | $invalid = true; |
1196 | 1196 | |
1197 | - wpinv_unset_cart_discount( $code ); |
|
1197 | + wpinv_unset_cart_discount($code); |
|
1198 | 1198 | } |
1199 | 1199 | } |
1200 | 1200 | |
1201 | - if ( $invalid ) { |
|
1201 | + if ($invalid) { |
|
1202 | 1202 | $errors = wpinv_get_errors(); |
1203 | - $error = !empty( $errors['wpinv-discount-error'] ) ? $errors['wpinv-discount-error'] . ' ' : ''; |
|
1204 | - $error .= __( 'The discount has been removed from cart.', 'invoicing' ); |
|
1205 | - wpinv_set_error( 'wpinv-discount-error', $error ); |
|
1203 | + $error = !empty($errors['wpinv-discount-error']) ? $errors['wpinv-discount-error'] . ' ' : ''; |
|
1204 | + $error .= __('The discount has been removed from cart.', 'invoicing'); |
|
1205 | + wpinv_set_error('wpinv-discount-error', $error); |
|
1206 | 1206 | |
1207 | - wpinv_recalculate_tax( true ); |
|
1207 | + wpinv_recalculate_tax(true); |
|
1208 | 1208 | } |
1209 | 1209 | } |
1210 | 1210 | } |
1211 | -add_action( 'wpinv_before_checkout_form', 'wpinv_checkout_form_validate_discounts', -10 ); |
|
1211 | +add_action('wpinv_before_checkout_form', 'wpinv_checkout_form_validate_discounts', -10); |
|
1212 | 1212 | |
1213 | 1213 | function wpinv_discount_amount() { |
1214 | 1214 | $output = 0.00; |
1215 | 1215 | |
1216 | - return apply_filters( 'wpinv_discount_amount', $output ); |
|
1216 | + return apply_filters('wpinv_discount_amount', $output); |
|
1217 | 1217 | } |
@@ -70,8 +70,9 @@ discard block |
||
70 | 70 | if( !empty( $tax_rates ) ) { |
71 | 71 | // Locate the tax rate for this country / state, if it exists |
72 | 72 | foreach( $tax_rates as $key => $tax_rate ) { |
73 | - if( $country != $tax_rate['country'] ) |
|
74 | - continue; |
|
73 | + if( $country != $tax_rate['country'] ) { |
|
74 | + continue; |
|
75 | + } |
|
75 | 76 | |
76 | 77 | if( !empty( $tax_rate['global'] ) ) { |
77 | 78 | if( !empty( $tax_rate['rate'] ) ) { |
@@ -79,8 +80,9 @@ discard block |
||
79 | 80 | } |
80 | 81 | } else { |
81 | 82 | |
82 | - if( empty( $tax_rate['state'] ) || strtolower( $state ) != strtolower( $tax_rate['state'] ) ) |
|
83 | - continue; |
|
83 | + if( empty( $tax_rate['state'] ) || strtolower( $state ) != strtolower( $tax_rate['state'] ) ) { |
|
84 | + continue; |
|
85 | + } |
|
84 | 86 | |
85 | 87 | $state_rate = $tax_rate['rate']; |
86 | 88 | if( 0 !== $state_rate || !empty( $state_rate ) ) { |
@@ -183,8 +185,9 @@ discard block |
||
183 | 185 | } |
184 | 186 | |
185 | 187 | function wpinv_cart_needs_tax_address_fields() { |
186 | - if( !wpinv_is_cart_taxed() ) |
|
187 | - return false; |
|
188 | + if( !wpinv_is_cart_taxed() ) { |
|
189 | + return false; |
|
190 | + } |
|
188 | 191 | |
189 | 192 | return ! did_action( 'wpinv_after_cc_fields', 'wpinv_default_cc_address_fields' ); |
190 | 193 | } |
@@ -618,10 +618,10 @@ |
||
618 | 618 | |
619 | 619 | } |
620 | 620 | |
621 | - if ( empty( $iso ) ) { |
|
622 | - return false; |
|
623 | - } |
|
621 | + if ( empty( $iso ) ) { |
|
622 | + return false; |
|
623 | + } |
|
624 | 624 | |
625 | - return compact( 'iso', 'country', 'region', 'city', 'longitude', 'latitude', 'credit' ); |
|
625 | + return compact( 'iso', 'country', 'region', 'city', 'longitude', 'latitude', 'credit' ); |
|
626 | 626 | |
627 | 627 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | // MUST have WordPress. |
3 | -if ( !defined( 'WPINC' ) ) { |
|
4 | - exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) ); |
|
3 | +if (!defined('WPINC')) { |
|
4 | + exit('Do NOT access this file directly: ' . basename(__FILE__)); |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -18,8 +18,8 @@ discard block |
||
18 | 18 | * |
19 | 19 | * @return bool |
20 | 20 | */ |
21 | -function getpaid_is_eu_state( $country ) { |
|
22 | - return WPInv_EUVat::is_eu_state( $country ); |
|
21 | +function getpaid_is_eu_state($country) { |
|
22 | + return WPInv_EUVat::is_eu_state($country); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | /** |
@@ -27,8 +27,8 @@ discard block |
||
27 | 27 | * |
28 | 28 | * @return bool |
29 | 29 | */ |
30 | -function getpaid_is_gst_country( $country ) { |
|
31 | - return WPInv_EUVat::is_gst_country( $country ); |
|
30 | +function getpaid_is_gst_country($country) { |
|
31 | + return WPInv_EUVat::is_gst_country($country); |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | /** |
@@ -42,117 +42,117 @@ discard block |
||
42 | 42 | |
43 | 43 | |
44 | 44 | function wpinv_use_taxes() { |
45 | - $ret = wpinv_get_option( 'enable_taxes', false ); |
|
45 | + $ret = wpinv_get_option('enable_taxes', false); |
|
46 | 46 | |
47 | - return (bool) apply_filters( 'wpinv_use_taxes', $ret ); |
|
47 | + return (bool) apply_filters('wpinv_use_taxes', $ret); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | function wpinv_get_tax_rates() { |
51 | - $rates = get_option( 'wpinv_tax_rates', array() ); |
|
51 | + $rates = get_option('wpinv_tax_rates', array()); |
|
52 | 52 | |
53 | - return apply_filters( 'wpinv_get_tax_rates', $rates ); |
|
53 | + return apply_filters('wpinv_get_tax_rates', $rates); |
|
54 | 54 | } |
55 | 55 | |
56 | -function wpinv_get_tax_rate( $country = false, $state = false, $item_id = 0 ) { |
|
56 | +function wpinv_get_tax_rate($country = false, $state = false, $item_id = 0) { |
|
57 | 57 | global $wpinv_euvat, $wpi_tax_rates, $wpi_userID; |
58 | - $wpi_tax_rates = !empty( $wpi_tax_rates ) ? $wpi_tax_rates : array(); |
|
58 | + $wpi_tax_rates = !empty($wpi_tax_rates) ? $wpi_tax_rates : array(); |
|
59 | 59 | |
60 | - if ( !empty( $wpi_tax_rates ) && !empty( $item_id ) && isset( $wpi_tax_rates[$item_id] ) ) { |
|
60 | + if (!empty($wpi_tax_rates) && !empty($item_id) && isset($wpi_tax_rates[$item_id])) { |
|
61 | 61 | return $wpi_tax_rates[$item_id]; |
62 | 62 | } |
63 | 63 | |
64 | - if ( !$wpinv_euvat->item_is_taxable( $item_id, $country, $state ) ) { |
|
64 | + if (!$wpinv_euvat->item_is_taxable($item_id, $country, $state)) { |
|
65 | 65 | $wpi_tax_rates[$item_id] = 0; |
66 | 66 | return 0; |
67 | 67 | } |
68 | 68 | |
69 | 69 | $is_global = false; |
70 | - if ( $item_id == 'global' ) { |
|
70 | + if ($item_id == 'global') { |
|
71 | 71 | $is_global = true; |
72 | 72 | $item_id = 0; |
73 | 73 | } |
74 | 74 | |
75 | - $rate = (float)wpinv_get_option( 'tax_rate', 0 ); |
|
76 | - $user_address = wpinv_get_user_address( $wpi_userID ); |
|
75 | + $rate = (float) wpinv_get_option('tax_rate', 0); |
|
76 | + $user_address = wpinv_get_user_address($wpi_userID); |
|
77 | 77 | |
78 | - if( empty( $country ) ) { |
|
79 | - if( !empty( $_POST['wpinv_country'] ) ) { |
|
78 | + if (empty($country)) { |
|
79 | + if (!empty($_POST['wpinv_country'])) { |
|
80 | 80 | $country = $_POST['wpinv_country']; |
81 | - } elseif( !empty( $_POST['wpinv_country'] ) ) { |
|
81 | + } elseif (!empty($_POST['wpinv_country'])) { |
|
82 | 82 | $country = $_POST['wpinv_country']; |
83 | - } elseif( !empty( $_POST['country'] ) ) { |
|
83 | + } elseif (!empty($_POST['country'])) { |
|
84 | 84 | $country = $_POST['country']; |
85 | - } elseif( is_user_logged_in() && !empty( $user_address ) ) { |
|
85 | + } elseif (is_user_logged_in() && !empty($user_address)) { |
|
86 | 86 | $country = $user_address['country']; |
87 | 87 | } |
88 | - $country = !empty( $country ) ? $country : wpinv_get_default_country(); |
|
88 | + $country = !empty($country) ? $country : wpinv_get_default_country(); |
|
89 | 89 | } |
90 | 90 | |
91 | - if( empty( $state ) ) { |
|
92 | - if( !empty( $_POST['wpinv_state'] ) ) { |
|
91 | + if (empty($state)) { |
|
92 | + if (!empty($_POST['wpinv_state'])) { |
|
93 | 93 | $state = $_POST['wpinv_state']; |
94 | - } elseif( !empty( $_POST['wpinv_state'] ) ) { |
|
94 | + } elseif (!empty($_POST['wpinv_state'])) { |
|
95 | 95 | $state = $_POST['wpinv_state']; |
96 | - } elseif( !empty( $_POST['state'] ) ) { |
|
96 | + } elseif (!empty($_POST['state'])) { |
|
97 | 97 | $state = $_POST['state']; |
98 | - } elseif( is_user_logged_in() && !empty( $user_address ) ) { |
|
98 | + } elseif (is_user_logged_in() && !empty($user_address)) { |
|
99 | 99 | $state = $user_address['state']; |
100 | 100 | } |
101 | - $state = !empty( $state ) ? $state : wpinv_get_default_state(); |
|
101 | + $state = !empty($state) ? $state : wpinv_get_default_state(); |
|
102 | 102 | } |
103 | 103 | |
104 | - if( !empty( $country ) ) { |
|
105 | - $tax_rates = wpinv_get_tax_rates(); |
|
104 | + if (!empty($country)) { |
|
105 | + $tax_rates = wpinv_get_tax_rates(); |
|
106 | 106 | |
107 | - if( !empty( $tax_rates ) ) { |
|
107 | + if (!empty($tax_rates)) { |
|
108 | 108 | // Locate the tax rate for this country / state, if it exists |
109 | - foreach( $tax_rates as $key => $tax_rate ) { |
|
110 | - if( $country != $tax_rate['country'] ) |
|
109 | + foreach ($tax_rates as $key => $tax_rate) { |
|
110 | + if ($country != $tax_rate['country']) |
|
111 | 111 | continue; |
112 | 112 | |
113 | - if( !empty( $tax_rate['global'] ) ) { |
|
114 | - if( !empty( $tax_rate['rate'] ) ) { |
|
115 | - $rate = number_format( $tax_rate['rate'], 4 ); |
|
113 | + if (!empty($tax_rate['global'])) { |
|
114 | + if (!empty($tax_rate['rate'])) { |
|
115 | + $rate = number_format($tax_rate['rate'], 4); |
|
116 | 116 | } |
117 | 117 | } else { |
118 | 118 | |
119 | - if( empty( $tax_rate['state'] ) || strtolower( $state ) != strtolower( $tax_rate['state'] ) ) |
|
119 | + if (empty($tax_rate['state']) || strtolower($state) != strtolower($tax_rate['state'])) |
|
120 | 120 | continue; |
121 | 121 | |
122 | 122 | $state_rate = $tax_rate['rate']; |
123 | - if( 0 !== $state_rate || !empty( $state_rate ) ) { |
|
124 | - $rate = number_format( $state_rate, 4 ); |
|
123 | + if (0 !== $state_rate || !empty($state_rate)) { |
|
124 | + $rate = number_format($state_rate, 4); |
|
125 | 125 | } |
126 | 126 | } |
127 | 127 | } |
128 | 128 | } |
129 | 129 | } |
130 | 130 | |
131 | - $rate = apply_filters( 'wpinv_tax_rate', $rate, $country, $state, $item_id ); |
|
131 | + $rate = apply_filters('wpinv_tax_rate', $rate, $country, $state, $item_id); |
|
132 | 132 | |
133 | - if ( !empty( $item_id ) ) { |
|
133 | + if (!empty($item_id)) { |
|
134 | 134 | $wpi_tax_rates[$item_id] = $rate; |
135 | - } else if ( $is_global ) { |
|
135 | + } else if ($is_global) { |
|
136 | 136 | $wpi_tax_rates['global'] = $rate; |
137 | 137 | } |
138 | 138 | |
139 | 139 | return $rate; |
140 | 140 | } |
141 | 141 | |
142 | -function wpinv_get_formatted_tax_rate( $country = false, $state = false, $item_id ) { |
|
143 | - $rate = wpinv_get_tax_rate( $country, $state, $item_id ); |
|
144 | - $rate = round( $rate, 4 ); |
|
142 | +function wpinv_get_formatted_tax_rate($country = false, $state = false, $item_id) { |
|
143 | + $rate = wpinv_get_tax_rate($country, $state, $item_id); |
|
144 | + $rate = round($rate, 4); |
|
145 | 145 | $formatted = $rate .= '%'; |
146 | - return apply_filters( 'wpinv_formatted_tax_rate', $formatted, $rate, $country, $state, $item_id ); |
|
146 | + return apply_filters('wpinv_formatted_tax_rate', $formatted, $rate, $country, $state, $item_id); |
|
147 | 147 | } |
148 | 148 | |
149 | -function wpinv_calculate_tax( $amount = 0, $country = false, $state = false, $item_id = 0 ) { |
|
150 | - $rate = wpinv_get_tax_rate( $country, $state, $item_id ); |
|
149 | +function wpinv_calculate_tax($amount = 0, $country = false, $state = false, $item_id = 0) { |
|
150 | + $rate = wpinv_get_tax_rate($country, $state, $item_id); |
|
151 | 151 | $tax = 0.00; |
152 | 152 | |
153 | - if ( wpinv_use_taxes() ) { |
|
154 | - if ( wpinv_prices_include_tax() ) { |
|
155 | - $pre_tax = ( $amount / ( ( 1 + $rate ) * 0.01 ) ); |
|
153 | + if (wpinv_use_taxes()) { |
|
154 | + if (wpinv_prices_include_tax()) { |
|
155 | + $pre_tax = ($amount / ((1 + $rate) * 0.01)); |
|
156 | 156 | $tax = $amount - $pre_tax; |
157 | 157 | } else { |
158 | 158 | $tax = $amount * $rate * 0.01; |
@@ -160,46 +160,46 @@ discard block |
||
160 | 160 | |
161 | 161 | } |
162 | 162 | |
163 | - return apply_filters( 'wpinv_taxed_amount', $tax, $rate, $country, $state, $item_id ); |
|
163 | + return apply_filters('wpinv_taxed_amount', $tax, $rate, $country, $state, $item_id); |
|
164 | 164 | } |
165 | 165 | |
166 | 166 | function wpinv_prices_include_tax() { |
167 | 167 | return false; // TODO |
168 | - $ret = ( wpinv_get_option( 'prices_include_tax', false ) == 'yes' && wpinv_use_taxes() ); |
|
168 | + $ret = (wpinv_get_option('prices_include_tax', false) == 'yes' && wpinv_use_taxes()); |
|
169 | 169 | |
170 | - return apply_filters( 'wpinv_prices_include_tax', $ret ); |
|
170 | + return apply_filters('wpinv_prices_include_tax', $ret); |
|
171 | 171 | } |
172 | 172 | |
173 | -function wpinv_sales_tax_for_year( $year = null ) { |
|
174 | - return wpinv_price( wpinv_format_amount( wpinv_get_sales_tax_for_year( $year ) ) ); |
|
173 | +function wpinv_sales_tax_for_year($year = null) { |
|
174 | + return wpinv_price(wpinv_format_amount(wpinv_get_sales_tax_for_year($year))); |
|
175 | 175 | } |
176 | 176 | |
177 | -function wpinv_get_sales_tax_for_year( $year = null ) { |
|
177 | +function wpinv_get_sales_tax_for_year($year = null) { |
|
178 | 178 | global $wpdb; |
179 | 179 | |
180 | 180 | // Start at zero |
181 | 181 | $tax = 0; |
182 | 182 | |
183 | - if ( ! empty( $year ) ) { |
|
183 | + if (!empty($year)) { |
|
184 | 184 | $args = array( |
185 | 185 | 'post_type' => 'wpi_invoice', |
186 | - 'post_status' => array( 'publish' ), |
|
186 | + 'post_status' => array('publish'), |
|
187 | 187 | 'posts_per_page' => -1, |
188 | 188 | 'year' => $year, |
189 | 189 | 'fields' => 'ids' |
190 | 190 | ); |
191 | 191 | |
192 | - $payments = get_posts( $args ); |
|
193 | - $payment_ids = implode( ',', $payments ); |
|
192 | + $payments = get_posts($args); |
|
193 | + $payment_ids = implode(',', $payments); |
|
194 | 194 | |
195 | - if ( count( $payments ) > 0 ) { |
|
195 | + if (count($payments) > 0) { |
|
196 | 196 | $sql = "SELECT SUM( meta_value ) FROM $wpdb->postmeta WHERE meta_key = '_wpinv_tax' AND post_id IN( $payment_ids )"; |
197 | - $tax = $wpdb->get_var( $sql ); |
|
197 | + $tax = $wpdb->get_var($sql); |
|
198 | 198 | } |
199 | 199 | |
200 | 200 | } |
201 | 201 | |
202 | - return apply_filters( 'wpinv_get_sales_tax_for_year', $tax, $year ); |
|
202 | + return apply_filters('wpinv_get_sales_tax_for_year', $tax, $year); |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | function wpinv_is_cart_taxed() { |
@@ -208,33 +208,33 @@ discard block |
||
208 | 208 | |
209 | 209 | function wpinv_prices_show_tax_on_checkout() { |
210 | 210 | return false; // TODO |
211 | - $ret = ( wpinv_get_option( 'checkout_include_tax', false ) == 'yes' && wpinv_use_taxes() ); |
|
211 | + $ret = (wpinv_get_option('checkout_include_tax', false) == 'yes' && wpinv_use_taxes()); |
|
212 | 212 | |
213 | - return apply_filters( 'wpinv_taxes_on_prices_on_checkout', $ret ); |
|
213 | + return apply_filters('wpinv_taxes_on_prices_on_checkout', $ret); |
|
214 | 214 | } |
215 | 215 | |
216 | 216 | function wpinv_display_tax_rate() { |
217 | - $ret = wpinv_use_taxes() && wpinv_get_option( 'display_tax_rate', false ); |
|
217 | + $ret = wpinv_use_taxes() && wpinv_get_option('display_tax_rate', false); |
|
218 | 218 | |
219 | - return apply_filters( 'wpinv_display_tax_rate', $ret ); |
|
219 | + return apply_filters('wpinv_display_tax_rate', $ret); |
|
220 | 220 | } |
221 | 221 | |
222 | 222 | function wpinv_cart_needs_tax_address_fields() { |
223 | - if( !wpinv_is_cart_taxed() ) |
|
223 | + if (!wpinv_is_cart_taxed()) |
|
224 | 224 | return false; |
225 | 225 | |
226 | - return ! did_action( 'wpinv_after_cc_fields', 'wpinv_default_cc_address_fields' ); |
|
226 | + return !did_action('wpinv_after_cc_fields', 'wpinv_default_cc_address_fields'); |
|
227 | 227 | } |
228 | 228 | |
229 | -function wpinv_item_is_tax_exclusive( $item_id = 0 ) { |
|
230 | - $ret = (bool)get_post_meta( $item_id, '_wpinv_tax_exclusive', false ); |
|
231 | - return apply_filters( 'wpinv_is_tax_exclusive', $ret, $item_id ); |
|
229 | +function wpinv_item_is_tax_exclusive($item_id = 0) { |
|
230 | + $ret = (bool) get_post_meta($item_id, '_wpinv_tax_exclusive', false); |
|
231 | + return apply_filters('wpinv_is_tax_exclusive', $ret, $item_id); |
|
232 | 232 | } |
233 | 233 | |
234 | -function wpinv_currency_decimal_filter( $decimals = 2 ) { |
|
234 | +function wpinv_currency_decimal_filter($decimals = 2) { |
|
235 | 235 | $currency = wpinv_get_currency(); |
236 | 236 | |
237 | - switch ( $currency ) { |
|
237 | + switch ($currency) { |
|
238 | 238 | case 'RIAL' : |
239 | 239 | case 'JPY' : |
240 | 240 | case 'TWD' : |
@@ -243,48 +243,48 @@ discard block |
||
243 | 243 | break; |
244 | 244 | } |
245 | 245 | |
246 | - return apply_filters( 'wpinv_currency_decimal_count', $decimals, $currency ); |
|
246 | + return apply_filters('wpinv_currency_decimal_count', $decimals, $currency); |
|
247 | 247 | } |
248 | 248 | |
249 | 249 | function wpinv_tax_amount() { |
250 | 250 | $output = 0.00; |
251 | 251 | |
252 | - return apply_filters( 'wpinv_tax_amount', $output ); |
|
252 | + return apply_filters('wpinv_tax_amount', $output); |
|
253 | 253 | } |
254 | 254 | |
255 | 255 | function wpinv_recalculated_tax() { |
256 | - define( 'WPINV_RECALCTAX', true ); |
|
256 | + define('WPINV_RECALCTAX', true); |
|
257 | 257 | } |
258 | -add_action( 'wp_ajax_wpinv_recalculate_tax', 'wpinv_recalculated_tax', 1 ); |
|
258 | +add_action('wp_ajax_wpinv_recalculate_tax', 'wpinv_recalculated_tax', 1); |
|
259 | 259 | |
260 | -function wpinv_recalculate_tax( $return = false ) { |
|
261 | - $invoice_id = (int)wpinv_get_invoice_cart_id(); |
|
262 | - if ( empty( $invoice_id ) ) { |
|
260 | +function wpinv_recalculate_tax($return = false) { |
|
261 | + $invoice_id = (int) wpinv_get_invoice_cart_id(); |
|
262 | + if (empty($invoice_id)) { |
|
263 | 263 | return false; |
264 | 264 | } |
265 | 265 | |
266 | - $invoice = wpinv_get_invoice_cart( $invoice_id ); |
|
266 | + $invoice = wpinv_get_invoice_cart($invoice_id); |
|
267 | 267 | |
268 | - if ( empty( $invoice ) ) { |
|
268 | + if (empty($invoice)) { |
|
269 | 269 | return false; |
270 | 270 | } |
271 | 271 | |
272 | - if ( empty( $_POST['country'] ) ) { |
|
272 | + if (empty($_POST['country'])) { |
|
273 | 273 | $_POST['country'] = !empty($invoice->country) ? $invoice->country : wpinv_get_default_country(); |
274 | 274 | } |
275 | 275 | |
276 | 276 | $invoice->country = sanitize_text_field($_POST['country']); |
277 | - $invoice->set( 'country', sanitize_text_field( $_POST['country'] ) ); |
|
277 | + $invoice->set('country', sanitize_text_field($_POST['country'])); |
|
278 | 278 | if (isset($_POST['state'])) { |
279 | 279 | $invoice->state = sanitize_text_field($_POST['state']); |
280 | - $invoice->set( 'state', sanitize_text_field( $_POST['state'] ) ); |
|
280 | + $invoice->set('state', sanitize_text_field($_POST['state'])); |
|
281 | 281 | } |
282 | 282 | |
283 | 283 | $invoice->cart_details = wpinv_get_cart_content_details(); |
284 | 284 | |
285 | - $subtotal = wpinv_get_cart_subtotal( $invoice->cart_details ); |
|
286 | - $tax = wpinv_get_cart_tax( $invoice->cart_details ); |
|
287 | - $total = wpinv_get_cart_total( $invoice->cart_details ); |
|
285 | + $subtotal = wpinv_get_cart_subtotal($invoice->cart_details); |
|
286 | + $tax = wpinv_get_cart_tax($invoice->cart_details); |
|
287 | + $total = wpinv_get_cart_total($invoice->cart_details); |
|
288 | 288 | |
289 | 289 | $invoice->tax = $tax; |
290 | 290 | $invoice->subtotal = $subtotal; |
@@ -292,61 +292,61 @@ discard block |
||
292 | 292 | |
293 | 293 | $invoice->save(); |
294 | 294 | |
295 | - if ( $invoice->is_free_trial() ) { |
|
295 | + if ($invoice->is_free_trial()) { |
|
296 | 296 | $total = 0; |
297 | 297 | } |
298 | 298 | |
299 | 299 | $response = array( |
300 | - 'total' => html_entity_decode( wpinv_price( wpinv_format_amount( $total ) ), ENT_COMPAT, 'UTF-8' ), |
|
300 | + 'total' => html_entity_decode(wpinv_price(wpinv_format_amount($total)), ENT_COMPAT, 'UTF-8'), |
|
301 | 301 | 'total_raw' => $total, |
302 | - 'free' => !( (float)$total > 0 ) && $invoice->is_free() ? true : false, |
|
303 | - 'html' => wpinv_checkout_cart( $invoice->cart_details, false ), |
|
302 | + 'free' => !((float) $total > 0) && $invoice->is_free() ? true : false, |
|
303 | + 'html' => wpinv_checkout_cart($invoice->cart_details, false), |
|
304 | 304 | ); |
305 | 305 | |
306 | - if ( $return ) { |
|
306 | + if ($return) { |
|
307 | 307 | return $response; |
308 | 308 | } |
309 | 309 | |
310 | - wp_send_json( $response ); |
|
310 | + wp_send_json($response); |
|
311 | 311 | } |
312 | -add_action( 'wp_ajax_wpinv_recalculate_tax', 'wpinv_recalculate_tax' ); |
|
313 | -add_action( 'wp_ajax_nopriv_wpinv_recalculate_tax', 'wpinv_recalculate_tax' ); |
|
312 | +add_action('wp_ajax_wpinv_recalculate_tax', 'wpinv_recalculate_tax'); |
|
313 | +add_action('wp_ajax_nopriv_wpinv_recalculate_tax', 'wpinv_recalculate_tax'); |
|
314 | 314 | |
315 | 315 | // VAT Settings |
316 | -function wpinv_vat_rate_add_callback( $args ) { |
|
316 | +function wpinv_vat_rate_add_callback($args) { |
|
317 | 317 | ?> |
318 | - <p class="wpi-vat-rate-actions"><input id="wpi_vat_rate_add" type="button" value="<?php esc_attr_e( 'Add', 'invoicing' );?>" class="button button-primary" /> <i style="display:none;" class="fa fa-refresh fa-spin"></i></p> |
|
318 | + <p class="wpi-vat-rate-actions"><input id="wpi_vat_rate_add" type="button" value="<?php esc_attr_e('Add', 'invoicing'); ?>" class="button button-primary" /> <i style="display:none;" class="fa fa-refresh fa-spin"></i></p> |
|
319 | 319 | <?php |
320 | 320 | } |
321 | 321 | |
322 | -function wpinv_vat_rate_delete_callback( $args ) { |
|
322 | +function wpinv_vat_rate_delete_callback($args) { |
|
323 | 323 | global $wpinv_euvat; |
324 | 324 | |
325 | 325 | $vat_classes = $wpinv_euvat->get_rate_classes(); |
326 | - $vat_class = isset( $_REQUEST['wpi_sub'] ) && $_REQUEST['wpi_sub'] !== '' && isset( $vat_classes[$_REQUEST['wpi_sub']] )? sanitize_text_field( $_REQUEST['wpi_sub'] ) : ''; |
|
327 | - if ( isset( $vat_classes[$vat_class] ) ) { |
|
326 | + $vat_class = isset($_REQUEST['wpi_sub']) && $_REQUEST['wpi_sub'] !== '' && isset($vat_classes[$_REQUEST['wpi_sub']]) ? sanitize_text_field($_REQUEST['wpi_sub']) : ''; |
|
327 | + if (isset($vat_classes[$vat_class])) { |
|
328 | 328 | ?> |
329 | - <p class="wpi-vat-rate-actions"><input id="wpi_vat_rate_delete" type="button" value="<?php echo wp_sprintf( esc_attr__( 'Delete class "%s"', 'invoicing' ), $vat_classes[$vat_class] );?>" class="button button-primary" /> <i style="display:none;" class="fa fa-refresh fa-spin"></i></p> |
|
329 | + <p class="wpi-vat-rate-actions"><input id="wpi_vat_rate_delete" type="button" value="<?php echo wp_sprintf(esc_attr__('Delete class "%s"', 'invoicing'), $vat_classes[$vat_class]); ?>" class="button button-primary" /> <i style="display:none;" class="fa fa-refresh fa-spin"></i></p> |
|
330 | 330 | <?php |
331 | 331 | } |
332 | 332 | } |
333 | 333 | |
334 | -function wpinv_vat_rates_callback( $args ) { |
|
334 | +function wpinv_vat_rates_callback($args) { |
|
335 | 335 | global $wpinv_euvat; |
336 | 336 | |
337 | 337 | $vat_classes = $wpinv_euvat->get_rate_classes(); |
338 | - $vat_class = isset( $_REQUEST['wpi_sub'] ) && $_REQUEST['wpi_sub'] !== '' && isset( $vat_classes[$_REQUEST['wpi_sub']] )? sanitize_text_field( $_REQUEST['wpi_sub'] ) : '_standard'; |
|
338 | + $vat_class = isset($_REQUEST['wpi_sub']) && $_REQUEST['wpi_sub'] !== '' && isset($vat_classes[$_REQUEST['wpi_sub']]) ? sanitize_text_field($_REQUEST['wpi_sub']) : '_standard'; |
|
339 | 339 | |
340 | 340 | $eu_states = $wpinv_euvat->get_eu_states(); |
341 | 341 | $countries = wpinv_get_country_list(); |
342 | 342 | $vat_groups = $wpinv_euvat->get_vat_groups(); |
343 | - $rates = $wpinv_euvat->get_vat_rates( $vat_class ); |
|
343 | + $rates = $wpinv_euvat->get_vat_rates($vat_class); |
|
344 | 344 | ob_start(); |
345 | 345 | ?> |
346 | 346 | </td><tr> |
347 | 347 | <td colspan="2" class="wpinv_vat_tdbox"> |
348 | - <input type="hidden" name="wpi_vat_class" value="<?php echo $vat_class;?>" /> |
|
349 | - <p><?php echo ( isset( $args['desc'] ) ? $args['desc'] : '' ); ?></p> |
|
348 | + <input type="hidden" name="wpi_vat_class" value="<?php echo $vat_class; ?>" /> |
|
349 | + <p><?php echo (isset($args['desc']) ? $args['desc'] : ''); ?></p> |
|
350 | 350 | <table id="wpinv_vat_rates" class="wp-list-table widefat fixed posts"> |
351 | 351 | <colgroup> |
352 | 352 | <col width="50px" /> |
@@ -358,43 +358,43 @@ discard block |
||
358 | 358 | </colgroup> |
359 | 359 | <thead> |
360 | 360 | <tr> |
361 | - <th scope="col" colspan="2" class="wpinv_vat_country_name"><?php _e( 'Country', 'invoicing' ); ?></th> |
|
362 | - <th scope="col" class="wpinv_vat_global" title="<?php esc_attr_e( 'Apply rate to whole country', 'invoicing' ); ?>"><?php _e( 'Country Wide', 'invoicing' ); ?></th> |
|
363 | - <th scope="col" class="wpinv_vat_rate"><?php _e( 'Rate %', 'invoicing' ); ?></th> |
|
364 | - <th scope="col" class="wpinv_vat_name"><?php _e( 'VAT Name', 'invoicing' ); ?></th> |
|
365 | - <th scope="col" class="wpinv_vat_group"><?php _e( 'Tax Group', 'invoicing' ); ?></th> |
|
361 | + <th scope="col" colspan="2" class="wpinv_vat_country_name"><?php _e('Country', 'invoicing'); ?></th> |
|
362 | + <th scope="col" class="wpinv_vat_global" title="<?php esc_attr_e('Apply rate to whole country', 'invoicing'); ?>"><?php _e('Country Wide', 'invoicing'); ?></th> |
|
363 | + <th scope="col" class="wpinv_vat_rate"><?php _e('Rate %', 'invoicing'); ?></th> |
|
364 | + <th scope="col" class="wpinv_vat_name"><?php _e('VAT Name', 'invoicing'); ?></th> |
|
365 | + <th scope="col" class="wpinv_vat_group"><?php _e('Tax Group', 'invoicing'); ?></th> |
|
366 | 366 | </tr> |
367 | 367 | </thead> |
368 | 368 | <tbody> |
369 | - <?php if( !empty( $eu_states ) ) { ?> |
|
369 | + <?php if (!empty($eu_states)) { ?> |
|
370 | 370 | <?php |
371 | - foreach ( $eu_states as $state ) { |
|
372 | - $country_name = isset( $countries[$state] ) ? $countries[$state] : ''; |
|
371 | + foreach ($eu_states as $state) { |
|
372 | + $country_name = isset($countries[$state]) ? $countries[$state] : ''; |
|
373 | 373 | |
374 | 374 | // Filter the rate for each country |
375 | - $country_rate = array_filter( $rates, function( $rate ) use( $state ) { return $rate['country'] === $state; } ); |
|
375 | + $country_rate = array_filter($rates, function($rate) use($state) { return $rate['country'] === $state; } ); |
|
376 | 376 | |
377 | 377 | // If one does not exist create a default |
378 | - $country_rate = is_array( $country_rate ) && count( $country_rate ) > 0 ? reset( $country_rate ) : array(); |
|
378 | + $country_rate = is_array($country_rate) && count($country_rate) > 0 ? reset($country_rate) : array(); |
|
379 | 379 | |
380 | - $vat_global = isset( $country_rate['global'] ) ? !empty( $country_rate['global'] ) : true; |
|
381 | - $vat_rate = isset( $country_rate['rate'] ) ? $country_rate['rate'] : ''; |
|
382 | - $vat_name = !empty( $country_rate['name'] ) ? esc_attr( stripslashes( $country_rate['name'] ) ) : ''; |
|
383 | - $vat_group = !empty( $country_rate['group'] ) ? $country_rate['group'] : ( $vat_class === '_standard' ? 'standard' : 'reduced' ); |
|
380 | + $vat_global = isset($country_rate['global']) ? !empty($country_rate['global']) : true; |
|
381 | + $vat_rate = isset($country_rate['rate']) ? $country_rate['rate'] : ''; |
|
382 | + $vat_name = !empty($country_rate['name']) ? esc_attr(stripslashes($country_rate['name'])) : ''; |
|
383 | + $vat_group = !empty($country_rate['group']) ? $country_rate['group'] : ($vat_class === '_standard' ? 'standard' : 'reduced'); |
|
384 | 384 | ?> |
385 | 385 | <tr> |
386 | 386 | <td class="wpinv_vat_country"><?php echo $state; ?><input type="hidden" name="vat_rates[<?php echo $state; ?>][country]" value="<?php echo $state; ?>" /><input type="hidden" name="vat_rates[<?php echo $state; ?>][state]" value="" /></td> |
387 | 387 | <td class="wpinv_vat_country_name"><?php echo $country_name; ?></td> |
388 | 388 | <td class="wpinv_vat_global"> |
389 | - <input type="checkbox" name="vat_rates[<?php echo $state;?>][global]" id="vat_rates[<?php echo $state;?>][global]" value="1" <?php checked( true, $vat_global );?> disabled="disabled" /> |
|
390 | - <label for="tax_rates[<?php echo $state;?>][global]"><?php _e( 'Apply to whole country', 'invoicing' ); ?></label> |
|
391 | - <input type="hidden" name="vat_rates[<?php echo $state;?>][global]" value="1" checked="checked" /> |
|
389 | + <input type="checkbox" name="vat_rates[<?php echo $state; ?>][global]" id="vat_rates[<?php echo $state; ?>][global]" value="1" <?php checked(true, $vat_global); ?> disabled="disabled" /> |
|
390 | + <label for="tax_rates[<?php echo $state; ?>][global]"><?php _e('Apply to whole country', 'invoicing'); ?></label> |
|
391 | + <input type="hidden" name="vat_rates[<?php echo $state; ?>][global]" value="1" checked="checked" /> |
|
392 | 392 | </td> |
393 | - <td class="wpinv_vat_rate"><input type="number" class="small-text" step="any" min="0" max="99" name="vat_rates[<?php echo $state;?>][rate]" value="<?php echo $vat_rate; ?>" /></td> |
|
394 | - <td class="wpinv_vat_name"><input type="text" class="regular-text" name="vat_rates[<?php echo $state;?>][name]" value="<?php echo $vat_name; ?>" /></td> |
|
393 | + <td class="wpinv_vat_rate"><input type="number" class="small-text" step="any" min="0" max="99" name="vat_rates[<?php echo $state; ?>][rate]" value="<?php echo $vat_rate; ?>" /></td> |
|
394 | + <td class="wpinv_vat_name"><input type="text" class="regular-text" name="vat_rates[<?php echo $state; ?>][name]" value="<?php echo $vat_name; ?>" /></td> |
|
395 | 395 | <td class="wpinv_vat_group"> |
396 | 396 | <?php |
397 | - echo wpinv_html_select( array( |
|
397 | + echo wpinv_html_select(array( |
|
398 | 398 | 'name' => 'vat_rates[' . $state . '][group]', |
399 | 399 | 'selected' => $vat_group, |
400 | 400 | 'id' => 'vat_rates[' . $state . '][group]', |
@@ -403,14 +403,14 @@ discard block |
||
403 | 403 | 'multiple' => false, |
404 | 404 | 'show_option_all' => false, |
405 | 405 | 'show_option_none' => false |
406 | - ) ); |
|
406 | + )); |
|
407 | 407 | ?> |
408 | 408 | </td> |
409 | 409 | </tr> |
410 | 410 | <?php } ?> |
411 | 411 | <tr> |
412 | 412 | <td colspan="6" style="background-color:#fafafa;"> |
413 | - <span><input id="wpi_vat_get_rates_group" type="button" class="button-secondary" value="<?php esc_attr_e( 'Update EU VAT Rates', 'invoicing' ); ?>" /> <i style="display:none" class="fa fa-refresh fa-spin"></i></span><span id="wpinv-rates-error-wrap" class="wpinv_errors" style="display:none;"></span> |
|
413 | + <span><input id="wpi_vat_get_rates_group" type="button" class="button-secondary" value="<?php esc_attr_e('Update EU VAT Rates', 'invoicing'); ?>" /> <i style="display:none" class="fa fa-refresh fa-spin"></i></span><span id="wpinv-rates-error-wrap" class="wpinv_errors" style="display:none;"></span> |
|
414 | 414 | </td> |
415 | 415 | </tr> |
416 | 416 | <?php } ?> |
@@ -422,35 +422,35 @@ discard block |
||
422 | 422 | echo $content; |
423 | 423 | } |
424 | 424 | |
425 | -function wpinv_vat_number_callback( $args ) { |
|
425 | +function wpinv_vat_number_callback($args) { |
|
426 | 426 | global $wpinv_euvat; |
427 | 427 | |
428 | 428 | $vat_number = $wpinv_euvat->get_vat_number(); |
429 | 429 | $vat_valid = $wpinv_euvat->is_vat_validated(); |
430 | 430 | |
431 | - $size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ) ? $args['size'] : 'regular'; |
|
432 | - $validated_text = $vat_valid ? __( 'VAT number validated', 'invoicing' ) : __( 'VAT number not validated', 'invoicing' ); |
|
431 | + $size = (isset($args['size']) && !is_null($args['size'])) ? $args['size'] : 'regular'; |
|
432 | + $validated_text = $vat_valid ? __('VAT number validated', 'invoicing') : __('VAT number not validated', 'invoicing'); |
|
433 | 433 | $disabled = $vat_valid ? 'disabled="disabled"' : " "; |
434 | 434 | |
435 | - $html = '<input type="text" class="' . $size . '-text" id="wpinv_settings[' . $args['id'] . ']" name="wpinv_settings[' . $args['id'] . ']" placeholder="GB123456789" value="' . esc_attr( stripslashes( $vat_number ) ) . '"/>'; |
|
436 | - $html .= '<span> <input type="button" id="wpinv_vat_validate" class="wpinv_validate_vat_button button-secondary" ' . $disabled . ' value="' . esc_attr__( 'Validate VAT Number', 'invoicing' ) . '" /></span>'; |
|
437 | - $html .= '<span class="wpinv-vat-stat wpinv-vat-stat-' . (int)$vat_valid . '"><i class="fa"></i> <font>' . $validated_text . '</font></span>'; |
|
438 | - $html .= '<label for="wpinv_settings[' . $args['id'] . ']">' . '<p>' . __( 'Enter your VAT number including country identifier, eg: GB123456789 (Settings must be saved after validation)', 'invoicing' ).'</p>' . '</label>'; |
|
439 | - $html .= '<input type="hidden" name="_wpi_nonce" value="' . wp_create_nonce( 'vat_validation' ) . '">'; |
|
435 | + $html = '<input type="text" class="' . $size . '-text" id="wpinv_settings[' . $args['id'] . ']" name="wpinv_settings[' . $args['id'] . ']" placeholder="GB123456789" value="' . esc_attr(stripslashes($vat_number)) . '"/>'; |
|
436 | + $html .= '<span> <input type="button" id="wpinv_vat_validate" class="wpinv_validate_vat_button button-secondary" ' . $disabled . ' value="' . esc_attr__('Validate VAT Number', 'invoicing') . '" /></span>'; |
|
437 | + $html .= '<span class="wpinv-vat-stat wpinv-vat-stat-' . (int) $vat_valid . '"><i class="fa"></i> <font>' . $validated_text . '</font></span>'; |
|
438 | + $html .= '<label for="wpinv_settings[' . $args['id'] . ']">' . '<p>' . __('Enter your VAT number including country identifier, eg: GB123456789 (Settings must be saved after validation)', 'invoicing') . '</p>' . '</label>'; |
|
439 | + $html .= '<input type="hidden" name="_wpi_nonce" value="' . wp_create_nonce('vat_validation') . '">'; |
|
440 | 440 | |
441 | 441 | echo $html; |
442 | 442 | } |
443 | 443 | |
444 | -function wpinv_eu_fallback_rate_callback( $args ) { |
|
444 | +function wpinv_eu_fallback_rate_callback($args) { |
|
445 | 445 | global $wpinv_options; |
446 | 446 | |
447 | - $value = isset( $wpinv_options[$args['id']] ) ? $wpinv_options[ $args['id'] ] : ( isset( $args['std'] ) ? $args['std'] : '' ); |
|
448 | - $size = ( isset( $args['size'] ) && !is_null( $args['size'] ) ) ? $args['size'] : 'small'; |
|
447 | + $value = isset($wpinv_options[$args['id']]) ? $wpinv_options[$args['id']] : (isset($args['std']) ? $args['std'] : ''); |
|
448 | + $size = (isset($args['size']) && !is_null($args['size'])) ? $args['size'] : 'small'; |
|
449 | 449 | |
450 | - $html = '<input type="number" min="0" max="99" step="any" class="' . $size . '-text" id="wpinv_settings_' . $args['section'] . '_' . $args['id'] . '" name="wpinv_settings[' . $args['id'] . ']" value="' . esc_attr( stripslashes( $value ) ) . '" />'; |
|
451 | - $html .= '<span> <input id="wpi_add_eu_states" type="button" class="button-secondary" value="' . esc_attr__( 'Add EU Member States', 'invoicing' ) . '" /></span>'; |
|
452 | - $html .= '<span> <input id="wpi_remove_eu_states" type="button" class="button-secondary" value="' . esc_attr__( 'Remove EU Member States', 'invoicing' ) . '" /></span>'; |
|
453 | - $html .= '<span> <input id="wpi_vat_get_rates" type="button" class="button-secondary" value="' . esc_attr__( 'Update EU VAT Rates', 'invoicing' ) . '" /> <i style="display:none" class="fa fa-refresh fa-spin"></i></span>'; |
|
450 | + $html = '<input type="number" min="0" max="99" step="any" class="' . $size . '-text" id="wpinv_settings_' . $args['section'] . '_' . $args['id'] . '" name="wpinv_settings[' . $args['id'] . ']" value="' . esc_attr(stripslashes($value)) . '" />'; |
|
451 | + $html .= '<span> <input id="wpi_add_eu_states" type="button" class="button-secondary" value="' . esc_attr__('Add EU Member States', 'invoicing') . '" /></span>'; |
|
452 | + $html .= '<span> <input id="wpi_remove_eu_states" type="button" class="button-secondary" value="' . esc_attr__('Remove EU Member States', 'invoicing') . '" /></span>'; |
|
453 | + $html .= '<span> <input id="wpi_vat_get_rates" type="button" class="button-secondary" value="' . esc_attr__('Update EU VAT Rates', 'invoicing') . '" /> <i style="display:none" class="fa fa-refresh fa-spin"></i></span>'; |
|
454 | 454 | $html .= '<p><label for="wpinv_settings_' . $args['section'] . '_' . $args['id'] . '">' . $args['desc'] . '</label></p>'; |
455 | 455 | echo $html; |
456 | 456 | ?> |
@@ -458,36 +458,36 @@ discard block |
||
458 | 458 | <?php |
459 | 459 | } |
460 | 460 | |
461 | -function wpinv_vat_ip_lookup_callback( $args ) { |
|
461 | +function wpinv_vat_ip_lookup_callback($args) { |
|
462 | 462 | global $wpinv_options, $wpinv_euvat; |
463 | 463 | |
464 | - $value = isset( $wpinv_options[ $args['id'] ] ) ? $wpinv_options[ $args['id'] ] : ( isset( $args['std'] ) ? $args['std'] : 'default' ); |
|
464 | + $value = isset($wpinv_options[$args['id']]) ? $wpinv_options[$args['id']] : (isset($args['std']) ? $args['std'] : 'default'); |
|
465 | 465 | |
466 | 466 | $options = array(); |
467 | - if ( function_exists( 'geoip_country_code_by_name' ) ) { |
|
468 | - $options['geoip'] = __( 'PHP GeoIP extension', 'invoicing' ); |
|
467 | + if (function_exists('geoip_country_code_by_name')) { |
|
468 | + $options['geoip'] = __('PHP GeoIP extension', 'invoicing'); |
|
469 | 469 | } |
470 | 470 | |
471 | 471 | $geoip2_database = $wpinv_euvat->geoip2_country_dbfile(); |
472 | 472 | |
473 | - if ( !function_exists( 'bcadd' ) ) { |
|
474 | - $geoip2_message = __( 'GeoIP2 service requires the BC Math PHP extension, it is not loaded in your version of PHP!', 'invoicing' ); |
|
473 | + if (!function_exists('bcadd')) { |
|
474 | + $geoip2_message = __('GeoIP2 service requires the BC Math PHP extension, it is not loaded in your version of PHP!', 'invoicing'); |
|
475 | 475 | } else { |
476 | - $geoip2_message = ini_get('safe_mode') ? __( 'GeoIP2 is not supported with PHP safe mode enabled!', 'invoicing' ) : ''; |
|
476 | + $geoip2_message = ini_get('safe_mode') ? __('GeoIP2 is not supported with PHP safe mode enabled!', 'invoicing') : ''; |
|
477 | 477 | } |
478 | 478 | |
479 | - if ( $geoip2_database !== false && empty( $geoip2_message ) ) { |
|
480 | - $options['geoip2'] = __( 'GeoIP2 Database', 'invoicing' ); |
|
479 | + if ($geoip2_database !== false && empty($geoip2_message)) { |
|
480 | + $options['geoip2'] = __('GeoIP2 Database', 'invoicing'); |
|
481 | 481 | } |
482 | 482 | |
483 | - if ( function_exists( 'simplexml_load_file' ) ) { |
|
484 | - $options['geoplugin'] = __( 'geoPlugin Web Service', 'invoicing' ); |
|
483 | + if (function_exists('simplexml_load_file')) { |
|
484 | + $options['geoplugin'] = __('geoPlugin Web Service', 'invoicing'); |
|
485 | 485 | } |
486 | 486 | |
487 | - $options['site'] = __( 'Use default country', 'invoicing' ); |
|
488 | - $options['default'] = __( 'Auto', 'invoicing' ); |
|
487 | + $options['site'] = __('Use default country', 'invoicing'); |
|
488 | + $options['default'] = __('Auto', 'invoicing'); |
|
489 | 489 | |
490 | - $html = wpinv_html_select( array( |
|
490 | + $html = wpinv_html_select(array( |
|
491 | 491 | 'name' => "wpinv_settings[{$args['id']}]", |
492 | 492 | 'selected' => $value, |
493 | 493 | 'id' => "wpinv_settings[{$args['id']}]", |
@@ -499,23 +499,23 @@ discard block |
||
499 | 499 | )); |
500 | 500 | |
501 | 501 | $desc = '<label for="wpinv_settings[' . $args['id'] . ']">'; |
502 | - $desc .= __( 'Select the option Invoicing should use to determine the country from the IP address of the user.', 'invoicing' ); |
|
502 | + $desc .= __('Select the option Invoicing should use to determine the country from the IP address of the user.', 'invoicing'); |
|
503 | 503 | $desc .= '<p>'; |
504 | - if ( empty( $geoip2_message ) ) { |
|
505 | - if ( $geoip2_database ) { |
|
504 | + if (empty($geoip2_message)) { |
|
505 | + if ($geoip2_database) { |
|
506 | 506 | $last_updated = ''; |
507 | - if ( $time_updated = wpinv_get_option( 'wpinv_geoip2_date_updated' ) ) { |
|
508 | - $date_updated = date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $time_updated ); |
|
509 | - $last_updated = '<br>' . sprintf( __( 'The GeoIP2 database was last updated on: <b>%s</b>', 'invoicing' ), $date_updated ); |
|
507 | + if ($time_updated = wpinv_get_option('wpinv_geoip2_date_updated')) { |
|
508 | + $date_updated = date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $time_updated); |
|
509 | + $last_updated = '<br>' . sprintf(__('The GeoIP2 database was last updated on: <b>%s</b>', 'invoicing'), $date_updated); |
|
510 | 510 | } |
511 | - $desc .= __( 'GeoIP2 database exists:', 'invoicing' ) . $last_updated . ' <input type="button" id="wpi_geoip2" action="update" class="wpinv-refresh-geoip2-btn button-secondary" value="' . __( 'Update GeoIP2 database now (~30MB)', 'invoicing' ) . '"></input>'; |
|
511 | + $desc .= __('GeoIP2 database exists:', 'invoicing') . $last_updated . ' <input type="button" id="wpi_geoip2" action="update" class="wpinv-refresh-geoip2-btn button-secondary" value="' . __('Update GeoIP2 database now (~30MB)', 'invoicing') . '"></input>'; |
|
512 | 512 | } else { |
513 | - $desc .= __( 'GeoIP2 database does not exist:', 'invoicing' ) . ' <input type="button" id="wpi_geoip2" action="download" class="wpinv-download-geoip2-btn button-secondary" value="' . __( 'Download GeoIP2 database now', 'invoicing' ) . ' (~30MB)"></input><br>' . __( 'After downloading the GeoIP2 database the GeoIP2 lookup option will show.', 'invoicing' ); |
|
513 | + $desc .= __('GeoIP2 database does not exist:', 'invoicing') . ' <input type="button" id="wpi_geoip2" action="download" class="wpinv-download-geoip2-btn button-secondary" value="' . __('Download GeoIP2 database now', 'invoicing') . ' (~30MB)"></input><br>' . __('After downloading the GeoIP2 database the GeoIP2 lookup option will show.', 'invoicing'); |
|
514 | 514 | } |
515 | 515 | } else { |
516 | 516 | $desc .= $geoip2_message; |
517 | 517 | } |
518 | - $desc .= '</p><p>'. __( 'geoPlugin is a great free service please consider supporting them: ', 'invoicing' ) . ' <a href="http://www.geoplugin.com/" target="_blank">GeoPlugin.com</a></p>'; |
|
518 | + $desc .= '</p><p>' . __('geoPlugin is a great free service please consider supporting them: ', 'invoicing') . ' <a href="http://www.geoplugin.com/" target="_blank">GeoPlugin.com</a></p>'; |
|
519 | 519 | $desc .= '</label>'; |
520 | 520 | |
521 | 521 | $html .= $desc; |
@@ -531,39 +531,39 @@ discard block |
||
531 | 531 | * |
532 | 532 | * @param string|bool|null $vat_rule |
533 | 533 | */ |
534 | -function getpaid_filter_vat_rule( $vat_rule ) { |
|
534 | +function getpaid_filter_vat_rule($vat_rule) { |
|
535 | 535 | |
536 | - if ( empty( $vat_rule ) ) { |
|
536 | + if (empty($vat_rule)) { |
|
537 | 537 | return getpaid_tax()->allow_vat_rules() ? 'digital' : 'physical'; |
538 | 538 | } |
539 | 539 | |
540 | 540 | return $vat_rule; |
541 | 541 | } |
542 | -add_filter( 'wpinv_get_item_vat_rule', 'getpaid_filter_vat_rule' ); |
|
542 | +add_filter('wpinv_get_item_vat_rule', 'getpaid_filter_vat_rule'); |
|
543 | 543 | |
544 | 544 | /** |
545 | 545 | * Filters the VAT class to ensure that each item has a VAT class. |
546 | 546 | * |
547 | 547 | * @param string|bool|null $vat_rule |
548 | 548 | */ |
549 | -function getpaid_filter_vat_class( $vat_class ) { |
|
550 | - return empty( $vat_class ) ? '_standard' : $vat_class; |
|
549 | +function getpaid_filter_vat_class($vat_class) { |
|
550 | + return empty($vat_class) ? '_standard' : $vat_class; |
|
551 | 551 | } |
552 | -add_filter( 'wpinv_get_item_vat_class', 'getpaid_filter_vat_class' ); |
|
552 | +add_filter('wpinv_get_item_vat_class', 'getpaid_filter_vat_class'); |
|
553 | 553 | |
554 | 554 | /** |
555 | 555 | * Returns the ip address location url. |
556 | 556 | * |
557 | 557 | */ |
558 | -function getpaid_ip_location_url( $ip_address ) { |
|
558 | +function getpaid_ip_location_url($ip_address) { |
|
559 | 559 | |
560 | 560 | return add_query_arg( |
561 | 561 | array( |
562 | 562 | 'action' => 'wpinv_ip_geolocation', |
563 | 563 | 'ip' => $ip_address, |
564 | - '_wpnonce' => wp_create_nonce( 'getpaid-ip-location' ) |
|
564 | + '_wpnonce' => wp_create_nonce('getpaid-ip-location') |
|
565 | 565 | ), |
566 | - admin_url( 'admin-ajax.php' ) |
|
566 | + admin_url('admin-ajax.php') |
|
567 | 567 | ); |
568 | 568 | |
569 | 569 | } |
@@ -573,54 +573,54 @@ discard block |
||
573 | 573 | * |
574 | 574 | * @return array|bool |
575 | 575 | */ |
576 | -function getpaid_geolocate_ip_address( $ip_address ) { |
|
576 | +function getpaid_geolocate_ip_address($ip_address) { |
|
577 | 577 | |
578 | 578 | // Do we have an ip address? |
579 | - if ( empty( $ip_address ) ) { |
|
579 | + if (empty($ip_address)) { |
|
580 | 580 | return false; |
581 | 581 | } |
582 | 582 | |
583 | 583 | /** |
584 | 584 | * Retrieve ip address using max mind. |
585 | 585 | */ |
586 | - if ( wpinv_get_option( 'vat_ip_lookup' ) == 'geoip2' && $geoip2_city = getpaid_tax()->geoip2_city_record( $ip_address ) ) { |
|
586 | + if (wpinv_get_option('vat_ip_lookup') == 'geoip2' && $geoip2_city = getpaid_tax()->geoip2_city_record($ip_address)) { |
|
587 | 587 | |
588 | 588 | try { |
589 | 589 | $iso = $geoip2_city->country->isoCode; |
590 | 590 | $country = $geoip2_city->country->name; |
591 | - $region = ! empty( $geoip2_city->subdivisions ) && ! empty( $geoip2_city->subdivisions[0]->name ) ? $geoip2_city->subdivisions[0]->name : ''; |
|
591 | + $region = !empty($geoip2_city->subdivisions) && !empty($geoip2_city->subdivisions[0]->name) ? $geoip2_city->subdivisions[0]->name : ''; |
|
592 | 592 | $city = $geoip2_city->city->name; |
593 | 593 | $longitude = $geoip2_city->location->longitude; |
594 | 594 | $latitude = $geoip2_city->location->latitude; |
595 | - $credit = __( 'Geolocated using the information by MaxMind, available from <a href="http://www.maxmind.com" target="_blank">www.maxmind.com</a>', 'invoicing' ); |
|
596 | - } catch( Exception $e ) { } |
|
595 | + $credit = __('Geolocated using the information by MaxMind, available from <a href="http://www.maxmind.com" target="_blank">www.maxmind.com</a>', 'invoicing'); |
|
596 | + } catch (Exception $e) { } |
|
597 | 597 | |
598 | 598 | } |
599 | 599 | |
600 | 600 | // If that fails, GeoLocate using GeoPlugin. |
601 | - if ( ( empty( $iso ) || empty( $longitude ) || empty( $latitude ) ) && function_exists( 'simplexml_load_file' ) ) { |
|
601 | + if ((empty($iso) || empty($longitude) || empty($latitude)) && function_exists('simplexml_load_file')) { |
|
602 | 602 | |
603 | 603 | try { |
604 | - $load_xml = simplexml_load_file( 'http://www.geoplugin.net/xml.gp?ip=' . $ip_address ); |
|
604 | + $load_xml = simplexml_load_file('http://www.geoplugin.net/xml.gp?ip=' . $ip_address); |
|
605 | 605 | |
606 | - if ( ! empty( $load_xml ) && isset( $load_xml->geoplugin_countryCode ) && ! empty( $load_xml->geoplugin_latitude ) && ! empty( $load_xml->geoplugin_longitude ) ) { |
|
606 | + if (!empty($load_xml) && isset($load_xml->geoplugin_countryCode) && !empty($load_xml->geoplugin_latitude) && !empty($load_xml->geoplugin_longitude)) { |
|
607 | 607 | $iso = $load_xml->geoplugin_countryCode; |
608 | 608 | $country = $load_xml->geoplugin_countryName; |
609 | - $region = ! empty( $load_xml->geoplugin_regionName ) ? $load_xml->geoplugin_regionName : ''; |
|
610 | - $city = ! empty( $load_xml->geoplugin_city ) ? $load_xml->geoplugin_city : ''; |
|
609 | + $region = !empty($load_xml->geoplugin_regionName) ? $load_xml->geoplugin_regionName : ''; |
|
610 | + $city = !empty($load_xml->geoplugin_city) ? $load_xml->geoplugin_city : ''; |
|
611 | 611 | $longitude = $load_xml->geoplugin_longitude; |
612 | 612 | $latitude = $load_xml->geoplugin_latitude; |
613 | 613 | $credit = $load_xml->geoplugin_credit; |
614 | - $credit = __( 'Geolocated using the information by geoPlugin, available from <a href="http://www.geoplugin.com" target="_blank">www.geoplugin.com</a>', 'invoicing' ) . '<br>' . $load_xml->geoplugin_credit; |
|
614 | + $credit = __('Geolocated using the information by geoPlugin, available from <a href="http://www.geoplugin.com" target="_blank">www.geoplugin.com</a>', 'invoicing') . '<br>' . $load_xml->geoplugin_credit; |
|
615 | 615 | } |
616 | - } catch( Exception $e ) { } |
|
616 | + } catch (Exception $e) { } |
|
617 | 617 | |
618 | 618 | } |
619 | 619 | |
620 | - if ( empty( $iso ) ) { |
|
620 | + if (empty($iso)) { |
|
621 | 621 | return false; |
622 | 622 | } |
623 | 623 | |
624 | - return compact( 'iso', 'country', 'region', 'city', 'longitude', 'latitude', 'credit' ); |
|
624 | + return compact('iso', 'country', 'region', 'city', 'longitude', 'latitude', 'credit'); |
|
625 | 625 | |
626 | 626 | } |
@@ -140,8 +140,8 @@ discard block |
||
140 | 140 | |
141 | 141 | function wpinv_get_default_labels() { |
142 | 142 | $defaults = array( |
143 | - 'singular' => __( 'Invoice', 'invoicing' ), |
|
144 | - 'plural' => __( 'Invoices', 'invoicing' ) |
|
143 | + 'singular' => __( 'Invoice', 'invoicing' ), |
|
144 | + 'plural' => __( 'Invoices', 'invoicing' ) |
|
145 | 145 | ); |
146 | 146 | |
147 | 147 | return apply_filters( 'wpinv_default_invoices_name', $defaults ); |
@@ -160,20 +160,20 @@ discard block |
||
160 | 160 | } |
161 | 161 | |
162 | 162 | function wpinv_change_default_title( $title ) { |
163 | - if ( !is_admin() ) { |
|
163 | + if ( !is_admin() ) { |
|
164 | 164 | $label = wpinv_get_label_singular(); |
165 | 165 | $title = sprintf( __( 'Enter %s name here', 'invoicing' ), $label ); |
166 | 166 | return $title; |
167 | - } |
|
167 | + } |
|
168 | 168 | |
169 | - $screen = get_current_screen(); |
|
169 | + $screen = get_current_screen(); |
|
170 | 170 | |
171 | - if ( 'wpi_invoice' == $screen->post_type ) { |
|
171 | + if ( 'wpi_invoice' == $screen->post_type ) { |
|
172 | 172 | $label = wpinv_get_label_singular(); |
173 | 173 | $title = sprintf( __( 'Enter %s name here', 'invoicing' ), $label ); |
174 | - } |
|
174 | + } |
|
175 | 175 | |
176 | - return $title; |
|
176 | + return $title; |
|
177 | 177 | } |
178 | 178 | add_filter( 'enter_title_here', 'wpinv_change_default_title' ); |
179 | 179 |
@@ -7,46 +7,46 @@ |
||
7 | 7 | */ |
8 | 8 | |
9 | 9 | // MUST have WordPress. |
10 | -if ( !defined( 'WPINC' ) ) { |
|
11 | - exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) ); |
|
10 | +if (!defined('WPINC')) { |
|
11 | + exit('Do NOT access this file directly: ' . basename(__FILE__)); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | |
15 | 15 | function wpinv_get_default_labels() { |
16 | 16 | $defaults = array( |
17 | - 'singular' => __( 'Invoice', 'invoicing' ), |
|
18 | - 'plural' => __( 'Invoices', 'invoicing' ) |
|
17 | + 'singular' => __('Invoice', 'invoicing'), |
|
18 | + 'plural' => __('Invoices', 'invoicing') |
|
19 | 19 | ); |
20 | 20 | |
21 | - return apply_filters( 'wpinv_default_invoices_name', $defaults ); |
|
21 | + return apply_filters('wpinv_default_invoices_name', $defaults); |
|
22 | 22 | } |
23 | 23 | |
24 | -function wpinv_get_label_singular( $lowercase = false ) { |
|
24 | +function wpinv_get_label_singular($lowercase = false) { |
|
25 | 25 | $defaults = wpinv_get_default_labels(); |
26 | 26 | |
27 | - return ($lowercase) ? strtolower( $defaults['singular'] ) : $defaults['singular']; |
|
27 | + return ($lowercase) ? strtolower($defaults['singular']) : $defaults['singular']; |
|
28 | 28 | } |
29 | 29 | |
30 | -function wpinv_get_label_plural( $lowercase = false ) { |
|
30 | +function wpinv_get_label_plural($lowercase = false) { |
|
31 | 31 | $defaults = wpinv_get_default_labels(); |
32 | 32 | |
33 | - return ( $lowercase ) ? strtolower( $defaults['plural'] ) : $defaults['plural']; |
|
33 | + return ($lowercase) ? strtolower($defaults['plural']) : $defaults['plural']; |
|
34 | 34 | } |
35 | 35 | |
36 | -function wpinv_change_default_title( $title ) { |
|
37 | - if ( !is_admin() ) { |
|
36 | +function wpinv_change_default_title($title) { |
|
37 | + if (!is_admin()) { |
|
38 | 38 | $label = wpinv_get_label_singular(); |
39 | - $title = sprintf( __( 'Enter %s name here', 'invoicing' ), $label ); |
|
39 | + $title = sprintf(__('Enter %s name here', 'invoicing'), $label); |
|
40 | 40 | return $title; |
41 | 41 | } |
42 | 42 | |
43 | 43 | $screen = get_current_screen(); |
44 | 44 | |
45 | - if ( 'wpi_invoice' == $screen->post_type ) { |
|
45 | + if ('wpi_invoice' == $screen->post_type) { |
|
46 | 46 | $label = wpinv_get_label_singular(); |
47 | - $title = sprintf( __( 'Enter %s name here', 'invoicing' ), $label ); |
|
47 | + $title = sprintf(__('Enter %s name here', 'invoicing'), $label); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | return $title; |
51 | 51 | } |
52 | -add_filter( 'enter_title_here', 'wpinv_change_default_title' ); |
|
52 | +add_filter('enter_title_here', 'wpinv_change_default_title'); |
@@ -130,7 +130,6 @@ |
||
130 | 130 | private function mostSpecificSubdivision() |
131 | 131 | { |
132 | 132 | return empty($this->subdivisions) ? |
133 | - new \GeoIp2\Record\Subdivision(array(), $this->locales) : |
|
134 | - end($this->subdivisions); |
|
133 | + new \GeoIp2\Record\Subdivision(array(), $this->locales) : end($this->subdivisions); |
|
135 | 134 | } |
136 | 135 | } |
@@ -76,7 +76,7 @@ |
||
76 | 76 | // This is for backwards compatibility. Do not remove except for a |
77 | 77 | // major version bump. |
78 | 78 | if (is_string($options)) { |
79 | - $options = array( 'host' => $options ); |
|
79 | + $options = array('host' => $options); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | if (!isset($options['host'])) { |
@@ -5,12 +5,12 @@ |
||
5 | 5 | // @codingStandardsIgnoreFile |
6 | 6 | |
7 | 7 | /** |
8 | - * This interface exists to provide backwards compatibility with PHP 5.3 |
|
9 | - * |
|
10 | - * This should _not_ be used by any third-party code. |
|
11 | - * |
|
12 | - * @ignore |
|
13 | - */ |
|
8 | + * This interface exists to provide backwards compatibility with PHP 5.3 |
|
9 | + * |
|
10 | + * This should _not_ be used by any third-party code. |
|
11 | + * |
|
12 | + * @ignore |
|
13 | + */ |
|
14 | 14 | if (interface_exists('JsonSerializable')) { |
15 | 15 | interface JsonSerializable extends \JsonSerializable |
16 | 16 | { |
@@ -9,780 +9,780 @@ |
||
9 | 9 | * @author Roman Ožana <[email protected]> |
10 | 10 | */ |
11 | 11 | class Emogrifier { |
12 | - /** |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - const ENCODING = 'UTF-8'; |
|
16 | - |
|
17 | - /** |
|
18 | - * @var integer |
|
19 | - */ |
|
20 | - const CACHE_KEY_CSS = 0; |
|
21 | - |
|
22 | - /** |
|
23 | - * @var integer |
|
24 | - */ |
|
25 | - const CACHE_KEY_SELECTOR = 1; |
|
26 | - |
|
27 | - /** |
|
28 | - * @var integer |
|
29 | - */ |
|
30 | - const CACHE_KEY_XPATH = 2; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var integer |
|
34 | - */ |
|
35 | - const CACHE_KEY_CSS_DECLARATION_BLOCK = 3; |
|
36 | - |
|
37 | - /** |
|
38 | - * for calculating nth-of-type and nth-child selectors. |
|
39 | - * |
|
40 | - * @var integer |
|
41 | - */ |
|
42 | - const INDEX = 0; |
|
43 | - |
|
44 | - /** |
|
45 | - * for calculating nth-of-type and nth-child selectors. |
|
46 | - * |
|
47 | - * @var integer |
|
48 | - */ |
|
49 | - const MULTIPLIER = 1; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - const ID_ATTRIBUTE_MATCHER = '/(\\w+)?\\#([\\w\\-]+)/'; |
|
55 | - |
|
56 | - /** |
|
57 | - * @var string |
|
58 | - */ |
|
59 | - const CLASS_ATTRIBUTE_MATCHER = '/(\\w+|[\\*\\]])?((\\.[\\w\\-]+)+)/'; |
|
60 | - |
|
61 | - /** |
|
62 | - * @var string |
|
63 | - */ |
|
64 | - private $html = ''; |
|
65 | - |
|
66 | - /** |
|
67 | - * @var string |
|
68 | - */ |
|
69 | - private $css = ''; |
|
70 | - |
|
71 | - /** |
|
72 | - * @var array<string> |
|
73 | - */ |
|
74 | - private $unprocessableHtmlTags = array('wbr'); |
|
75 | - |
|
76 | - /** |
|
77 | - * @var array<array> |
|
78 | - */ |
|
79 | - private $caches = array( |
|
80 | - self::CACHE_KEY_CSS => array(), |
|
81 | - self::CACHE_KEY_SELECTOR => array(), |
|
82 | - self::CACHE_KEY_XPATH => array(), |
|
83 | - self::CACHE_KEY_CSS_DECLARATION_BLOCK => array(), |
|
84 | - ); |
|
85 | - |
|
86 | - /** |
|
87 | - * the visited nodes with the XPath paths as array keys. |
|
88 | - * |
|
89 | - * @var array<\DOMNode> |
|
90 | - */ |
|
91 | - private $visitedNodes = array(); |
|
92 | - |
|
93 | - /** |
|
94 | - * the styles to apply to the nodes with the XPath paths as array keys for the outer array and the attribute names/values. |
|
95 | - * as key/value pairs for the inner array. |
|
96 | - * |
|
97 | - * @var array<array><string> |
|
98 | - */ |
|
99 | - private $styleAttributesForNodes = array(); |
|
100 | - |
|
101 | - /** |
|
102 | - * This attribute applies to the case where you want to preserve your original text encoding. |
|
103 | - * |
|
104 | - * By default, emogrifier translates your text into HTML entities for two reasons: |
|
105 | - * |
|
106 | - * 1. Because of client incompatibilities, it is better practice to send out HTML entities rather than unicode over email. |
|
107 | - * |
|
108 | - * 2. It translates any illegal XML characters that DOMDocument cannot work with. |
|
109 | - * |
|
110 | - * If you would like to preserve your original encoding, set this attribute to TRUE. |
|
111 | - * |
|
112 | - * @var boolean |
|
113 | - */ |
|
114 | - public $preserveEncoding = false; |
|
115 | - |
|
116 | - public static $_media = ''; |
|
117 | - |
|
118 | - /** |
|
119 | - * The constructor. |
|
120 | - * |
|
121 | - * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
122 | - * @param string $css the CSS to merge, must be UTF-8-encoded |
|
123 | - */ |
|
124 | - public function __construct($html = '', $css = '') { |
|
125 | - $this->setHtml($html); |
|
126 | - $this->setCss($css); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * The destructor. |
|
131 | - */ |
|
132 | - public function __destruct() { |
|
133 | - $this->purgeVisitedNodes(); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * Sets the HTML to emogrify. |
|
138 | - * |
|
139 | - * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
140 | - */ |
|
141 | - public function setHtml($html = '') { |
|
142 | - $this->html = $html; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Sets the CSS to merge with the HTML. |
|
147 | - * |
|
148 | - * @param string $css the CSS to merge, must be UTF-8-encoded |
|
149 | - */ |
|
150 | - public function setCss($css = '') { |
|
151 | - $this->css = $css; |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Clears all caches. |
|
156 | - */ |
|
157 | - private function clearAllCaches() { |
|
158 | - $this->clearCache(self::CACHE_KEY_CSS); |
|
159 | - $this->clearCache(self::CACHE_KEY_SELECTOR); |
|
160 | - $this->clearCache(self::CACHE_KEY_XPATH); |
|
161 | - $this->clearCache(self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Clears a single cache by key. |
|
166 | - * |
|
167 | - * @param integer $key the cache key, must be CACHE_KEY_CSS, CACHE_KEY_SELECTOR, CACHE_KEY_XPATH or CACHE_KEY_CSS_DECLARATION_BLOCK |
|
168 | - * |
|
169 | - * @throws InvalidArgumentException |
|
170 | - */ |
|
171 | - private function clearCache($key) { |
|
172 | - $allowedCacheKeys = array(self::CACHE_KEY_CSS, self::CACHE_KEY_SELECTOR, self::CACHE_KEY_XPATH, self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
173 | - if (!in_array($key, $allowedCacheKeys, true)) { |
|
174 | - throw new InvalidArgumentException('Invalid cache key: ' . $key, 1391822035); |
|
175 | - } |
|
176 | - |
|
177 | - $this->caches[$key] = array(); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * Purges the visited nodes. |
|
182 | - */ |
|
183 | - private function purgeVisitedNodes() { |
|
184 | - $this->visitedNodes = array(); |
|
185 | - $this->styleAttributesForNodes = array(); |
|
186 | - } |
|
187 | - |
|
188 | - /** |
|
189 | - * Marks a tag for removal. |
|
190 | - * |
|
191 | - * There are some HTML tags that DOMDocument cannot process, and it will throw an error if it encounters them. |
|
192 | - * In particular, DOMDocument will complain if you try to use HTML5 tags in an XHTML document. |
|
193 | - * |
|
194 | - * Note: The tags will not be removed if they have any content. |
|
195 | - * |
|
196 | - * @param string $tagName the tag name, e.g., "p" |
|
197 | - */ |
|
198 | - public function addUnprocessableHtmlTag($tagName) { |
|
199 | - $this->unprocessableHtmlTags[] = $tagName; |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Drops a tag from the removal list. |
|
204 | - * |
|
205 | - * @param string $tagName the tag name, e.g., "p" |
|
206 | - */ |
|
207 | - public function removeUnprocessableHtmlTag($tagName) { |
|
208 | - $key = array_search($tagName, $this->unprocessableHtmlTags, true); |
|
209 | - if ($key !== false) { |
|
210 | - unset($this->unprocessableHtmlTags[$key]); |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - /** |
|
215 | - * Applies the CSS you submit to the HTML you submit. |
|
216 | - * |
|
217 | - * This method places the CSS inline. |
|
218 | - * |
|
219 | - * @return string |
|
220 | - * |
|
221 | - * @throws BadMethodCallException |
|
222 | - */ |
|
223 | - public function emogrify() { |
|
224 | - if ($this->html === '') { |
|
225 | - throw new BadMethodCallException('Please set some HTML first before calling emogrify.', 1390393096); |
|
226 | - } |
|
227 | - |
|
228 | - $xmlDocument = $this->createXmlDocument(); |
|
229 | - $xpath = new DOMXPath($xmlDocument); |
|
230 | - $this->clearAllCaches(); |
|
231 | - |
|
232 | - // before be begin processing the CSS file, parse the document and normalize all existing CSS attributes (changes 'DISPLAY: none' to 'display: none'); |
|
233 | - // we wouldn't have to do this if DOMXPath supported XPath 2.0. |
|
234 | - // also store a reference of nodes with existing inline styles so we don't overwrite them |
|
235 | - $this->purgeVisitedNodes(); |
|
236 | - |
|
237 | - $nodesWithStyleAttributes = $xpath->query('//*[@style]'); |
|
238 | - if ($nodesWithStyleAttributes !== false) { |
|
239 | - /** @var $nodeWithStyleAttribute DOMNode */ |
|
240 | - foreach ($nodesWithStyleAttributes as $node) { |
|
241 | - $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
242 | - |
|
243 | - // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
|
244 | - $nodePath = $node->getNodePath(); |
|
245 | - if (!isset($this->styleAttributesForNodes[$nodePath])) { |
|
246 | - $this->styleAttributesForNodes[$nodePath] = $this->parseCssDeclarationBlock($normalizedOriginalStyle); |
|
247 | - $this->visitedNodes[$nodePath] = $node; |
|
248 | - } |
|
249 | - |
|
250 | - $node->setAttribute('style', $normalizedOriginalStyle); |
|
251 | - } |
|
252 | - } |
|
253 | - |
|
254 | - // grab any existing style blocks from the html and append them to the existing CSS |
|
255 | - // (these blocks should be appended so as to have precedence over conflicting styles in the existing CSS) |
|
256 | - $allCss = $this->css; |
|
257 | - |
|
258 | - $allCss .= $this->getCssFromAllStyleNodes($xpath); |
|
259 | - |
|
260 | - $cssParts = $this->splitCssAndMediaQuery($allCss); |
|
261 | - self::$_media = ''; // reset |
|
262 | - |
|
263 | - $cssKey = md5($cssParts['css']); |
|
264 | - if (!isset($this->caches[self::CACHE_KEY_CSS][$cssKey])) { |
|
265 | - // process the CSS file for selectors and definitions |
|
266 | - preg_match_all('/(?:^|[\\s^{}]*)([^{]+){([^}]*)}/mis', $cssParts['css'], $matches, PREG_SET_ORDER); |
|
267 | - |
|
268 | - $allSelectors = array(); |
|
269 | - foreach ($matches as $key => $selectorString) { |
|
270 | - // if there is a blank definition, skip |
|
271 | - if (!strlen(trim($selectorString[2]))) { |
|
272 | - continue; |
|
273 | - } |
|
274 | - |
|
275 | - // else split by commas and duplicate attributes so we can sort by selector precedence |
|
276 | - $selectors = explode(',', $selectorString[1]); |
|
277 | - foreach ($selectors as $selector) { |
|
278 | - // don't process pseudo-elements and behavioral (dynamic) pseudo-classes; ONLY allow structural pseudo-classes |
|
279 | - if (strpos($selector, ':') !== false && !preg_match('/:\\S+\\-(child|type)\\(/i', $selector)) { |
|
280 | - continue; |
|
281 | - } |
|
282 | - |
|
283 | - $allSelectors[] = array('selector' => trim($selector), |
|
284 | - 'attributes' => trim($selectorString[2]), |
|
285 | - // keep track of where it appears in the file, since order is important |
|
286 | - 'line' => $key, |
|
287 | - ); |
|
288 | - } |
|
289 | - } |
|
290 | - |
|
291 | - // now sort the selectors by precedence |
|
292 | - usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
293 | - |
|
294 | - $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
|
295 | - } |
|
296 | - |
|
297 | - foreach ($this->caches[self::CACHE_KEY_CSS][$cssKey] as $value) { |
|
298 | - // query the body for the xpath selector |
|
299 | - $nodesMatchingCssSelectors = $xpath->query($this->translateCssToXpath($value['selector'])); |
|
300 | - |
|
301 | - /** @var $node \DOMNode */ |
|
302 | - foreach ($nodesMatchingCssSelectors as $node) { |
|
303 | - // if it has a style attribute, get it, process it, and append (overwrite) new stuff |
|
304 | - if ($node->hasAttribute('style')) { |
|
305 | - // break it up into an associative array |
|
306 | - $oldStyleDeclarations = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
307 | - } else { |
|
308 | - $oldStyleDeclarations = array(); |
|
309 | - } |
|
310 | - $newStyleDeclarations = $this->parseCssDeclarationBlock($value['attributes']); |
|
311 | - $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($oldStyleDeclarations, $newStyleDeclarations)); |
|
312 | - } |
|
313 | - } |
|
314 | - |
|
315 | - // now iterate through the nodes that contained inline styles in the original HTML |
|
316 | - foreach ($this->styleAttributesForNodes as $nodePath => $styleAttributesForNode) { |
|
317 | - $node = $this->visitedNodes[$nodePath]; |
|
318 | - $currentStyleAttributes = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
319 | - $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($currentStyleAttributes, $styleAttributesForNode)); |
|
320 | - } |
|
321 | - |
|
322 | - // This removes styles from your email that contain display:none. |
|
323 | - // We need to look for display:none, but we need to do a case-insensitive search. Since DOMDocument only supports XPath 1.0, |
|
324 | - // lower-case() isn't available to us. We've thus far only set attributes to lowercase, not attribute values. Consequently, we need |
|
325 | - // to translate() the letters that would be in 'NONE' ("NOE") to lowercase. |
|
326 | - $nodesWithStyleDisplayNone = $xpath->query('//*[contains(translate(translate(@style," ",""),"NOE","noe"),"display:none")]'); |
|
327 | - // The checks on parentNode and is_callable below ensure that if we've deleted the parent node, |
|
328 | - // we don't try to call removeChild on a nonexistent child node |
|
329 | - if ($nodesWithStyleDisplayNone->length > 0) { |
|
330 | - /** @var $node \DOMNode */ |
|
331 | - foreach ($nodesWithStyleDisplayNone as $node) { |
|
332 | - if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
333 | - $node->parentNode->removeChild($node); |
|
334 | - } |
|
335 | - } |
|
336 | - } |
|
337 | - |
|
338 | - $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument); |
|
339 | - |
|
340 | - if ($this->preserveEncoding) { |
|
341 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
342 | - return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
343 | - } else { |
|
344 | - return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
|
345 | - } |
|
346 | - } else { |
|
347 | - return $xmlDocument->saveHTML(); |
|
348 | - } |
|
349 | - } |
|
350 | - |
|
351 | - public function strtolower(array $m) { |
|
352 | - return strtolower($m[0]); |
|
353 | - } |
|
354 | - |
|
355 | - |
|
356 | - /** |
|
357 | - * This method merges old or existing name/value array with new name/value array. |
|
358 | - * and then generates a string of the combined style suitable for placing inline. |
|
359 | - * This becomes the single point for CSS string generation allowing for consistent. |
|
360 | - * CSS output no matter where the CSS originally came from. |
|
361 | - * @param array $oldStyles |
|
362 | - * @param array $newStyles |
|
363 | - * @return string |
|
364 | - */ |
|
365 | - private function generateStyleStringFromDeclarationsArrays(array $oldStyles, array $newStyles) { |
|
366 | - $combinedStyles = array_merge($oldStyles, $newStyles); |
|
367 | - $style = ''; |
|
368 | - foreach ($combinedStyles as $attributeName => $attributeValue) { |
|
369 | - $style .= (strtolower(trim($attributeName)) . ': ' . trim($attributeValue) . '; '); |
|
370 | - } |
|
371 | - return trim($style); |
|
372 | - } |
|
373 | - |
|
374 | - |
|
375 | - /** |
|
376 | - * Copies the media part from CSS array parts to $xmlDocument. |
|
377 | - * |
|
378 | - * @param array $cssParts |
|
379 | - * @param DOMDocument $xmlDocument |
|
380 | - */ |
|
381 | - public function copyCssWithMediaToStyleNode(array $cssParts, DOMDocument $xmlDocument) { |
|
382 | - if (isset($cssParts['media']) && $cssParts['media'] !== '') { |
|
383 | - $this->addStyleElementToDocument($xmlDocument, $cssParts['media']); |
|
384 | - } |
|
385 | - } |
|
386 | - |
|
387 | - /** |
|
388 | - * Returns CSS content. |
|
389 | - * |
|
390 | - * @param DOMXPath $xpath |
|
391 | - * @return string |
|
392 | - */ |
|
393 | - private function getCssFromAllStyleNodes(DOMXPath $xpath) { |
|
394 | - $styleNodes = $xpath->query('//style'); |
|
395 | - |
|
396 | - if ($styleNodes === false) { |
|
397 | - return ''; |
|
398 | - } |
|
399 | - |
|
400 | - $css = ''; |
|
401 | - /** @var $styleNode DOMNode */ |
|
402 | - foreach ($styleNodes as $styleNode) { |
|
403 | - $css .= "\n\n" . $styleNode->nodeValue; |
|
404 | - $styleNode->parentNode->removeChild($styleNode); |
|
405 | - } |
|
406 | - |
|
407 | - return $css; |
|
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * Adds a style element with $css to $document. |
|
412 | - * |
|
413 | - * @param DOMDocument $document |
|
414 | - * @param string $css |
|
415 | - */ |
|
416 | - private function addStyleElementToDocument(DOMDocument $document, $css) { |
|
417 | - $styleElement = $document->createElement('style', $css); |
|
418 | - $styleAttribute = $document->createAttribute('type'); |
|
419 | - $styleAttribute->value = 'text/css'; |
|
420 | - $styleElement->appendChild($styleAttribute); |
|
421 | - |
|
422 | - $head = $this->getOrCreateHeadElement($document); |
|
423 | - $head->appendChild($styleElement); |
|
424 | - } |
|
425 | - |
|
426 | - /** |
|
427 | - * Returns the existing or creates a new head element in $document. |
|
428 | - * |
|
429 | - * @param DOMDocument $document |
|
430 | - * @return DOMNode the head element |
|
431 | - */ |
|
432 | - private function getOrCreateHeadElement(DOMDocument $document) { |
|
433 | - $head = $document->getElementsByTagName('head')->item(0); |
|
434 | - |
|
435 | - if ($head === null) { |
|
436 | - $head = $document->createElement('head'); |
|
437 | - $html = $document->getElementsByTagName('html')->item(0); |
|
438 | - $html->insertBefore($head, $document->getElementsByTagName('body')->item(0)); |
|
439 | - } |
|
440 | - |
|
441 | - return $head; |
|
442 | - } |
|
443 | - |
|
444 | - /** |
|
445 | - * Splits input CSS code to an array where: |
|
446 | - * |
|
447 | - * - key "css" will be contains clean CSS code. |
|
448 | - * - key "media" will be contains all valuable media queries. |
|
449 | - * |
|
450 | - * Example: |
|
451 | - * |
|
452 | - * The CSS code. |
|
453 | - * |
|
454 | - * "@import "file.css"; h1 { color:red; } @media { h1 {}} @media tv { h1 {}}" |
|
455 | - * |
|
456 | - * will be parsed into the following array: |
|
457 | - * |
|
458 | - * "css" => "h1 { color:red; }" |
|
459 | - * "media" => "@media { h1 {}}" |
|
460 | - * |
|
461 | - * @param string $css |
|
462 | - * @return array |
|
463 | - */ |
|
464 | - private function splitCssAndMediaQuery($css) { |
|
465 | - $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
466 | - |
|
467 | - // filter the CSS |
|
468 | - $search = array( |
|
469 | - // get rid of css comment code |
|
470 | - '/\\/\\*.*\\*\\//sU', |
|
471 | - // strip out any import directives |
|
472 | - '/^\\s*@import\\s[^;]+;/misU', |
|
473 | - // strip remains media enclosures |
|
474 | - '/^\\s*@media\\s[^{]+{(.*)}\\s*}\\s/misU', |
|
475 | - ); |
|
476 | - |
|
477 | - $replace = array( |
|
478 | - '', |
|
479 | - '', |
|
480 | - '', |
|
481 | - ); |
|
482 | - |
|
483 | - // clean CSS before output |
|
484 | - $css = preg_replace($search, $replace, $css); |
|
485 | - |
|
486 | - return array('css' => $css, 'media' => self::$_media); |
|
487 | - } |
|
488 | - |
|
489 | - private function _media_concat( $matches ) { |
|
490 | - self::$_media .= $matches[0]; |
|
491 | - } |
|
492 | - |
|
493 | - /** |
|
494 | - * Creates a DOMDocument instance with the current HTML. |
|
495 | - * |
|
496 | - * @return DOMDocument |
|
497 | - */ |
|
498 | - private function createXmlDocument() { |
|
499 | - $xmlDocument = new DOMDocument; |
|
500 | - $xmlDocument->encoding = self::ENCODING; |
|
501 | - $xmlDocument->strictErrorChecking = false; |
|
502 | - $xmlDocument->formatOutput = true; |
|
503 | - $libXmlState = libxml_use_internal_errors(true); |
|
504 | - $xmlDocument->loadHTML($this->getUnifiedHtml()); |
|
505 | - libxml_clear_errors(); |
|
506 | - libxml_use_internal_errors($libXmlState); |
|
507 | - $xmlDocument->normalizeDocument(); |
|
508 | - |
|
509 | - return $xmlDocument; |
|
510 | - } |
|
511 | - |
|
512 | - /** |
|
513 | - * Returns the HTML with the non-ASCII characters converts into HTML entities and the unprocessable HTML tags removed. |
|
514 | - * |
|
515 | - * @return string the unified HTML |
|
516 | - * |
|
517 | - * @throws BadMethodCallException |
|
518 | - */ |
|
519 | - private function getUnifiedHtml() { |
|
520 | - if (!empty($this->unprocessableHtmlTags)) { |
|
521 | - $unprocessableHtmlTags = implode('|', $this->unprocessableHtmlTags); |
|
522 | - $bodyWithoutUnprocessableTags = preg_replace('/<\\/?(' . $unprocessableHtmlTags . ')[^>]*>/i', '', $this->html); |
|
523 | - } else { |
|
524 | - $bodyWithoutUnprocessableTags = $this->html; |
|
525 | - } |
|
526 | - |
|
527 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
528 | - return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
529 | - } else { |
|
530 | - return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
|
531 | - } |
|
532 | - } |
|
533 | - |
|
534 | - /** |
|
535 | - * @param array $a |
|
536 | - * @param array $b |
|
537 | - * |
|
538 | - * @return integer |
|
539 | - */ |
|
540 | - private function sortBySelectorPrecedence(array $a, array $b) { |
|
541 | - $precedenceA = $this->getCssSelectorPrecedence($a['selector']); |
|
542 | - $precedenceB = $this->getCssSelectorPrecedence($b['selector']); |
|
543 | - |
|
544 | - // We want these sorted in ascending order so selectors with lesser precedence get processed first and |
|
545 | - // selectors with greater precedence get sorted last. |
|
546 | - // The parenthesis around the -1 are necessary to avoid a PHP_CodeSniffer warning about missing spaces around |
|
547 | - // arithmetic operators. |
|
548 | - // @see http://forge.typo3.org/issues/55605 |
|
549 | - $precedenceForEquals = ($a['line'] < $b['line'] ? (-1) : 1); |
|
550 | - $precedenceForNotEquals = ($precedenceA < $precedenceB ? (-1) : 1); |
|
551 | - return ($precedenceA === $precedenceB) ? $precedenceForEquals : $precedenceForNotEquals; |
|
552 | - } |
|
553 | - |
|
554 | - /** |
|
555 | - * @param string $selector |
|
556 | - * |
|
557 | - * @return integer |
|
558 | - */ |
|
559 | - private function getCssSelectorPrecedence($selector) { |
|
560 | - $selectorKey = md5($selector); |
|
561 | - if (!isset($this->caches[self::CACHE_KEY_SELECTOR][$selectorKey])) { |
|
562 | - $precedence = 0; |
|
563 | - $value = 100; |
|
564 | - // ids: worth 100, classes: worth 10, elements: worth 1 |
|
565 | - $search = array('\\#','\\.',''); |
|
566 | - |
|
567 | - foreach ($search as $s) { |
|
568 | - if (trim($selector == '')) { |
|
569 | - break; |
|
570 | - } |
|
571 | - $number = 0; |
|
572 | - $selector = preg_replace('/' . $s . '\\w+/', '', $selector, -1, $number); |
|
573 | - $precedence += ($value * $number); |
|
574 | - $value /= 10; |
|
575 | - } |
|
576 | - $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey] = $precedence; |
|
577 | - } |
|
578 | - |
|
579 | - return $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey]; |
|
580 | - } |
|
581 | - |
|
582 | - /** |
|
583 | - * Right now, we support all CSS 1 selectors and most CSS2/3 selectors. |
|
584 | - * |
|
585 | - * @see http://plasmasturm.org/log/444/ |
|
586 | - * |
|
587 | - * @param string $paramCssSelector |
|
588 | - * |
|
589 | - * @return string |
|
590 | - */ |
|
591 | - private function translateCssToXpath($paramCssSelector) { |
|
592 | - $cssSelector = ' ' . $paramCssSelector . ' '; |
|
593 | - $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
594 | - $cssSelector = trim($cssSelector); |
|
595 | - $xpathKey = md5($cssSelector); |
|
596 | - if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
|
597 | - // returns an Xpath selector |
|
598 | - $search = array( |
|
599 | - // Matches any element that is a child of parent. |
|
600 | - '/\\s+>\\s+/', |
|
601 | - // Matches any element that is an adjacent sibling. |
|
602 | - '/\\s+\\+\\s+/', |
|
603 | - // Matches any element that is a descendant of an parent element element. |
|
604 | - '/\\s+/', |
|
605 | - // first-child pseudo-selector |
|
606 | - '/([^\\/]+):first-child/i', |
|
607 | - // last-child pseudo-selector |
|
608 | - '/([^\\/]+):last-child/i', |
|
609 | - // Matches attribute only selector |
|
610 | - '/^\\[(\\w+)\\]/', |
|
611 | - // Matches element with attribute |
|
612 | - '/(\\w)\\[(\\w+)\\]/', |
|
613 | - // Matches element with EXACT attribute |
|
614 | - '/(\\w)\\[(\\w+)\\=[\'"]?(\\w+)[\'"]?\\]/', |
|
615 | - ); |
|
616 | - $replace = array( |
|
617 | - '/', |
|
618 | - '/following-sibling::*[1]/self::', |
|
619 | - '//', |
|
620 | - '*[1]/self::\\1', |
|
621 | - '*[last()]/self::\\1', |
|
622 | - '*[@\\1]', |
|
623 | - '\\1[@\\2]', |
|
624 | - '\\1[@\\2="\\3"]', |
|
625 | - ); |
|
626 | - |
|
627 | - $cssSelector = '//' . preg_replace($search, $replace, $cssSelector); |
|
628 | - |
|
629 | - $cssSelector = preg_replace_callback(self::ID_ATTRIBUTE_MATCHER, array($this, 'matchIdAttributes'), $cssSelector); |
|
630 | - $cssSelector = preg_replace_callback(self::CLASS_ATTRIBUTE_MATCHER, array($this, 'matchClassAttributes'), $cssSelector); |
|
631 | - |
|
632 | - // Advanced selectors are going to require a bit more advanced emogrification. |
|
633 | - // When we required PHP 5.3, we could do this with closures. |
|
634 | - $cssSelector = preg_replace_callback( |
|
635 | - '/([^\\/]+):nth-child\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
636 | - array($this, 'translateNthChild'), $cssSelector |
|
637 | - ); |
|
638 | - $cssSelector = preg_replace_callback( |
|
639 | - '/([^\\/]+):nth-of-type\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
640 | - array($this, 'translateNthOfType'), $cssSelector |
|
641 | - ); |
|
642 | - |
|
643 | - $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey] = $cssSelector; |
|
644 | - } |
|
645 | - return $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey]; |
|
646 | - } |
|
647 | - |
|
648 | - /** |
|
649 | - * @param array $match |
|
650 | - * |
|
651 | - * @return string |
|
652 | - */ |
|
653 | - private function matchIdAttributes(array $match) { |
|
654 | - return (strlen($match[1]) ? $match[1] : '*') . '[@id="' . $match[2] . '"]'; |
|
655 | - } |
|
656 | - |
|
657 | - /** |
|
658 | - * @param array $match |
|
659 | - * |
|
660 | - * @return string |
|
661 | - */ |
|
662 | - private function matchClassAttributes(array $match) { |
|
663 | - return (strlen($match[1]) ? $match[1] : '*') . '[contains(concat(" ",@class," "),concat(" ","' . |
|
664 | - implode( |
|
665 | - '"," "))][contains(concat(" ",@class," "),concat(" ","', |
|
666 | - explode('.', substr($match[2], 1)) |
|
667 | - ) . '"," "))]'; |
|
668 | - } |
|
669 | - |
|
670 | - /** |
|
671 | - * @param array $match |
|
672 | - * |
|
673 | - * @return string |
|
674 | - */ |
|
675 | - private function translateNthChild(array $match) { |
|
676 | - $result = $this->parseNth($match); |
|
677 | - |
|
678 | - if (isset($result[self::MULTIPLIER])) { |
|
679 | - if ($result[self::MULTIPLIER] < 0) { |
|
680 | - $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
681 | - return sprintf('*[(last() - position()) mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
682 | - } else { |
|
683 | - return sprintf('*[position() mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
684 | - } |
|
685 | - } else { |
|
686 | - return sprintf('*[%u]/self::%s', $result[self::INDEX], $match[1]); |
|
687 | - } |
|
688 | - } |
|
689 | - |
|
690 | - /** |
|
691 | - * @param array $match |
|
692 | - * |
|
693 | - * @return string |
|
694 | - */ |
|
695 | - private function translateNthOfType(array $match) { |
|
696 | - $result = $this->parseNth($match); |
|
697 | - |
|
698 | - if (isset($result[self::MULTIPLIER])) { |
|
699 | - if ($result[self::MULTIPLIER] < 0) { |
|
700 | - $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
701 | - return sprintf('%s[(last() - position()) mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
702 | - } else { |
|
703 | - return sprintf('%s[position() mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
704 | - } |
|
705 | - } else { |
|
706 | - return sprintf('%s[%u]', $match[1], $result[self::INDEX]); |
|
707 | - } |
|
708 | - } |
|
709 | - |
|
710 | - /** |
|
711 | - * @param array $match |
|
712 | - * |
|
713 | - * @return array |
|
714 | - */ |
|
715 | - private function parseNth(array $match) { |
|
716 | - if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
717 | - $index = strtolower($match[2]) == 'even' ? 0 : 1; |
|
718 | - return array(self::MULTIPLIER => 2, self::INDEX => $index); |
|
719 | - } elseif (stripos($match[2], 'n') === false) { |
|
720 | - // if there is a multiplier |
|
721 | - $index = intval(str_replace(' ', '', $match[2])); |
|
722 | - return array(self::INDEX => $index); |
|
723 | - } else { |
|
724 | - if (isset($match[3])) { |
|
725 | - $multipleTerm = str_replace($match[3], '', $match[2]); |
|
726 | - $index = intval(str_replace(' ', '', $match[3])); |
|
727 | - } else { |
|
728 | - $multipleTerm = $match[2]; |
|
729 | - $index = 0; |
|
730 | - } |
|
731 | - |
|
732 | - $multiplier = str_ireplace('n', '', $multipleTerm); |
|
733 | - |
|
734 | - if (!strlen($multiplier)) { |
|
735 | - $multiplier = 1; |
|
736 | - } elseif ($multiplier == 0) { |
|
737 | - return array(self::INDEX => $index); |
|
738 | - } else { |
|
739 | - $multiplier = intval($multiplier); |
|
740 | - } |
|
741 | - |
|
742 | - while ($index < 0) { |
|
743 | - $index += abs($multiplier); |
|
744 | - } |
|
745 | - |
|
746 | - return array(self::MULTIPLIER => $multiplier, self::INDEX => $index); |
|
747 | - } |
|
748 | - } |
|
749 | - |
|
750 | - /** |
|
751 | - * Parses a CSS declaration block into property name/value pairs. |
|
752 | - * |
|
753 | - * Example: |
|
754 | - * |
|
755 | - * The declaration block. |
|
756 | - * |
|
757 | - * "color: #000; font-weight: bold;". |
|
758 | - * |
|
759 | - * will be parsed into the following array: |
|
760 | - * |
|
761 | - * "color" => "#000" |
|
762 | - * "font-weight" => "bold" |
|
763 | - * |
|
764 | - * @param string $cssDeclarationBlock the CSS declaration block without the curly braces, may be empty |
|
765 | - * |
|
766 | - * @return array the CSS declarations with the property names as array keys and the property values as array values |
|
767 | - */ |
|
768 | - private function parseCssDeclarationBlock($cssDeclarationBlock) { |
|
769 | - if (isset($this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock])) { |
|
770 | - return $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock]; |
|
771 | - } |
|
772 | - |
|
773 | - $properties = array(); |
|
774 | - $declarations = explode(';', $cssDeclarationBlock); |
|
775 | - foreach ($declarations as $declaration) { |
|
776 | - $matches = array(); |
|
777 | - if (!preg_match('/ *([A-Za-z\\-]+) *: *([^;]+) */', $declaration, $matches)) { |
|
778 | - continue; |
|
779 | - } |
|
780 | - $propertyName = strtolower($matches[1]); |
|
781 | - $propertyValue = $matches[2]; |
|
782 | - $properties[$propertyName] = $propertyValue; |
|
783 | - } |
|
784 | - $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock] = $properties; |
|
785 | - |
|
786 | - return $properties; |
|
787 | - } |
|
12 | + /** |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + const ENCODING = 'UTF-8'; |
|
16 | + |
|
17 | + /** |
|
18 | + * @var integer |
|
19 | + */ |
|
20 | + const CACHE_KEY_CSS = 0; |
|
21 | + |
|
22 | + /** |
|
23 | + * @var integer |
|
24 | + */ |
|
25 | + const CACHE_KEY_SELECTOR = 1; |
|
26 | + |
|
27 | + /** |
|
28 | + * @var integer |
|
29 | + */ |
|
30 | + const CACHE_KEY_XPATH = 2; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var integer |
|
34 | + */ |
|
35 | + const CACHE_KEY_CSS_DECLARATION_BLOCK = 3; |
|
36 | + |
|
37 | + /** |
|
38 | + * for calculating nth-of-type and nth-child selectors. |
|
39 | + * |
|
40 | + * @var integer |
|
41 | + */ |
|
42 | + const INDEX = 0; |
|
43 | + |
|
44 | + /** |
|
45 | + * for calculating nth-of-type and nth-child selectors. |
|
46 | + * |
|
47 | + * @var integer |
|
48 | + */ |
|
49 | + const MULTIPLIER = 1; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + const ID_ATTRIBUTE_MATCHER = '/(\\w+)?\\#([\\w\\-]+)/'; |
|
55 | + |
|
56 | + /** |
|
57 | + * @var string |
|
58 | + */ |
|
59 | + const CLASS_ATTRIBUTE_MATCHER = '/(\\w+|[\\*\\]])?((\\.[\\w\\-]+)+)/'; |
|
60 | + |
|
61 | + /** |
|
62 | + * @var string |
|
63 | + */ |
|
64 | + private $html = ''; |
|
65 | + |
|
66 | + /** |
|
67 | + * @var string |
|
68 | + */ |
|
69 | + private $css = ''; |
|
70 | + |
|
71 | + /** |
|
72 | + * @var array<string> |
|
73 | + */ |
|
74 | + private $unprocessableHtmlTags = array('wbr'); |
|
75 | + |
|
76 | + /** |
|
77 | + * @var array<array> |
|
78 | + */ |
|
79 | + private $caches = array( |
|
80 | + self::CACHE_KEY_CSS => array(), |
|
81 | + self::CACHE_KEY_SELECTOR => array(), |
|
82 | + self::CACHE_KEY_XPATH => array(), |
|
83 | + self::CACHE_KEY_CSS_DECLARATION_BLOCK => array(), |
|
84 | + ); |
|
85 | + |
|
86 | + /** |
|
87 | + * the visited nodes with the XPath paths as array keys. |
|
88 | + * |
|
89 | + * @var array<\DOMNode> |
|
90 | + */ |
|
91 | + private $visitedNodes = array(); |
|
92 | + |
|
93 | + /** |
|
94 | + * the styles to apply to the nodes with the XPath paths as array keys for the outer array and the attribute names/values. |
|
95 | + * as key/value pairs for the inner array. |
|
96 | + * |
|
97 | + * @var array<array><string> |
|
98 | + */ |
|
99 | + private $styleAttributesForNodes = array(); |
|
100 | + |
|
101 | + /** |
|
102 | + * This attribute applies to the case where you want to preserve your original text encoding. |
|
103 | + * |
|
104 | + * By default, emogrifier translates your text into HTML entities for two reasons: |
|
105 | + * |
|
106 | + * 1. Because of client incompatibilities, it is better practice to send out HTML entities rather than unicode over email. |
|
107 | + * |
|
108 | + * 2. It translates any illegal XML characters that DOMDocument cannot work with. |
|
109 | + * |
|
110 | + * If you would like to preserve your original encoding, set this attribute to TRUE. |
|
111 | + * |
|
112 | + * @var boolean |
|
113 | + */ |
|
114 | + public $preserveEncoding = false; |
|
115 | + |
|
116 | + public static $_media = ''; |
|
117 | + |
|
118 | + /** |
|
119 | + * The constructor. |
|
120 | + * |
|
121 | + * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
122 | + * @param string $css the CSS to merge, must be UTF-8-encoded |
|
123 | + */ |
|
124 | + public function __construct($html = '', $css = '') { |
|
125 | + $this->setHtml($html); |
|
126 | + $this->setCss($css); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * The destructor. |
|
131 | + */ |
|
132 | + public function __destruct() { |
|
133 | + $this->purgeVisitedNodes(); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * Sets the HTML to emogrify. |
|
138 | + * |
|
139 | + * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
140 | + */ |
|
141 | + public function setHtml($html = '') { |
|
142 | + $this->html = $html; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Sets the CSS to merge with the HTML. |
|
147 | + * |
|
148 | + * @param string $css the CSS to merge, must be UTF-8-encoded |
|
149 | + */ |
|
150 | + public function setCss($css = '') { |
|
151 | + $this->css = $css; |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Clears all caches. |
|
156 | + */ |
|
157 | + private function clearAllCaches() { |
|
158 | + $this->clearCache(self::CACHE_KEY_CSS); |
|
159 | + $this->clearCache(self::CACHE_KEY_SELECTOR); |
|
160 | + $this->clearCache(self::CACHE_KEY_XPATH); |
|
161 | + $this->clearCache(self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Clears a single cache by key. |
|
166 | + * |
|
167 | + * @param integer $key the cache key, must be CACHE_KEY_CSS, CACHE_KEY_SELECTOR, CACHE_KEY_XPATH or CACHE_KEY_CSS_DECLARATION_BLOCK |
|
168 | + * |
|
169 | + * @throws InvalidArgumentException |
|
170 | + */ |
|
171 | + private function clearCache($key) { |
|
172 | + $allowedCacheKeys = array(self::CACHE_KEY_CSS, self::CACHE_KEY_SELECTOR, self::CACHE_KEY_XPATH, self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
173 | + if (!in_array($key, $allowedCacheKeys, true)) { |
|
174 | + throw new InvalidArgumentException('Invalid cache key: ' . $key, 1391822035); |
|
175 | + } |
|
176 | + |
|
177 | + $this->caches[$key] = array(); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * Purges the visited nodes. |
|
182 | + */ |
|
183 | + private function purgeVisitedNodes() { |
|
184 | + $this->visitedNodes = array(); |
|
185 | + $this->styleAttributesForNodes = array(); |
|
186 | + } |
|
187 | + |
|
188 | + /** |
|
189 | + * Marks a tag for removal. |
|
190 | + * |
|
191 | + * There are some HTML tags that DOMDocument cannot process, and it will throw an error if it encounters them. |
|
192 | + * In particular, DOMDocument will complain if you try to use HTML5 tags in an XHTML document. |
|
193 | + * |
|
194 | + * Note: The tags will not be removed if they have any content. |
|
195 | + * |
|
196 | + * @param string $tagName the tag name, e.g., "p" |
|
197 | + */ |
|
198 | + public function addUnprocessableHtmlTag($tagName) { |
|
199 | + $this->unprocessableHtmlTags[] = $tagName; |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Drops a tag from the removal list. |
|
204 | + * |
|
205 | + * @param string $tagName the tag name, e.g., "p" |
|
206 | + */ |
|
207 | + public function removeUnprocessableHtmlTag($tagName) { |
|
208 | + $key = array_search($tagName, $this->unprocessableHtmlTags, true); |
|
209 | + if ($key !== false) { |
|
210 | + unset($this->unprocessableHtmlTags[$key]); |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + /** |
|
215 | + * Applies the CSS you submit to the HTML you submit. |
|
216 | + * |
|
217 | + * This method places the CSS inline. |
|
218 | + * |
|
219 | + * @return string |
|
220 | + * |
|
221 | + * @throws BadMethodCallException |
|
222 | + */ |
|
223 | + public function emogrify() { |
|
224 | + if ($this->html === '') { |
|
225 | + throw new BadMethodCallException('Please set some HTML first before calling emogrify.', 1390393096); |
|
226 | + } |
|
227 | + |
|
228 | + $xmlDocument = $this->createXmlDocument(); |
|
229 | + $xpath = new DOMXPath($xmlDocument); |
|
230 | + $this->clearAllCaches(); |
|
231 | + |
|
232 | + // before be begin processing the CSS file, parse the document and normalize all existing CSS attributes (changes 'DISPLAY: none' to 'display: none'); |
|
233 | + // we wouldn't have to do this if DOMXPath supported XPath 2.0. |
|
234 | + // also store a reference of nodes with existing inline styles so we don't overwrite them |
|
235 | + $this->purgeVisitedNodes(); |
|
236 | + |
|
237 | + $nodesWithStyleAttributes = $xpath->query('//*[@style]'); |
|
238 | + if ($nodesWithStyleAttributes !== false) { |
|
239 | + /** @var $nodeWithStyleAttribute DOMNode */ |
|
240 | + foreach ($nodesWithStyleAttributes as $node) { |
|
241 | + $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
242 | + |
|
243 | + // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
|
244 | + $nodePath = $node->getNodePath(); |
|
245 | + if (!isset($this->styleAttributesForNodes[$nodePath])) { |
|
246 | + $this->styleAttributesForNodes[$nodePath] = $this->parseCssDeclarationBlock($normalizedOriginalStyle); |
|
247 | + $this->visitedNodes[$nodePath] = $node; |
|
248 | + } |
|
249 | + |
|
250 | + $node->setAttribute('style', $normalizedOriginalStyle); |
|
251 | + } |
|
252 | + } |
|
253 | + |
|
254 | + // grab any existing style blocks from the html and append them to the existing CSS |
|
255 | + // (these blocks should be appended so as to have precedence over conflicting styles in the existing CSS) |
|
256 | + $allCss = $this->css; |
|
257 | + |
|
258 | + $allCss .= $this->getCssFromAllStyleNodes($xpath); |
|
259 | + |
|
260 | + $cssParts = $this->splitCssAndMediaQuery($allCss); |
|
261 | + self::$_media = ''; // reset |
|
262 | + |
|
263 | + $cssKey = md5($cssParts['css']); |
|
264 | + if (!isset($this->caches[self::CACHE_KEY_CSS][$cssKey])) { |
|
265 | + // process the CSS file for selectors and definitions |
|
266 | + preg_match_all('/(?:^|[\\s^{}]*)([^{]+){([^}]*)}/mis', $cssParts['css'], $matches, PREG_SET_ORDER); |
|
267 | + |
|
268 | + $allSelectors = array(); |
|
269 | + foreach ($matches as $key => $selectorString) { |
|
270 | + // if there is a blank definition, skip |
|
271 | + if (!strlen(trim($selectorString[2]))) { |
|
272 | + continue; |
|
273 | + } |
|
274 | + |
|
275 | + // else split by commas and duplicate attributes so we can sort by selector precedence |
|
276 | + $selectors = explode(',', $selectorString[1]); |
|
277 | + foreach ($selectors as $selector) { |
|
278 | + // don't process pseudo-elements and behavioral (dynamic) pseudo-classes; ONLY allow structural pseudo-classes |
|
279 | + if (strpos($selector, ':') !== false && !preg_match('/:\\S+\\-(child|type)\\(/i', $selector)) { |
|
280 | + continue; |
|
281 | + } |
|
282 | + |
|
283 | + $allSelectors[] = array('selector' => trim($selector), |
|
284 | + 'attributes' => trim($selectorString[2]), |
|
285 | + // keep track of where it appears in the file, since order is important |
|
286 | + 'line' => $key, |
|
287 | + ); |
|
288 | + } |
|
289 | + } |
|
290 | + |
|
291 | + // now sort the selectors by precedence |
|
292 | + usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
293 | + |
|
294 | + $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
|
295 | + } |
|
296 | + |
|
297 | + foreach ($this->caches[self::CACHE_KEY_CSS][$cssKey] as $value) { |
|
298 | + // query the body for the xpath selector |
|
299 | + $nodesMatchingCssSelectors = $xpath->query($this->translateCssToXpath($value['selector'])); |
|
300 | + |
|
301 | + /** @var $node \DOMNode */ |
|
302 | + foreach ($nodesMatchingCssSelectors as $node) { |
|
303 | + // if it has a style attribute, get it, process it, and append (overwrite) new stuff |
|
304 | + if ($node->hasAttribute('style')) { |
|
305 | + // break it up into an associative array |
|
306 | + $oldStyleDeclarations = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
307 | + } else { |
|
308 | + $oldStyleDeclarations = array(); |
|
309 | + } |
|
310 | + $newStyleDeclarations = $this->parseCssDeclarationBlock($value['attributes']); |
|
311 | + $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($oldStyleDeclarations, $newStyleDeclarations)); |
|
312 | + } |
|
313 | + } |
|
314 | + |
|
315 | + // now iterate through the nodes that contained inline styles in the original HTML |
|
316 | + foreach ($this->styleAttributesForNodes as $nodePath => $styleAttributesForNode) { |
|
317 | + $node = $this->visitedNodes[$nodePath]; |
|
318 | + $currentStyleAttributes = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
319 | + $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($currentStyleAttributes, $styleAttributesForNode)); |
|
320 | + } |
|
321 | + |
|
322 | + // This removes styles from your email that contain display:none. |
|
323 | + // We need to look for display:none, but we need to do a case-insensitive search. Since DOMDocument only supports XPath 1.0, |
|
324 | + // lower-case() isn't available to us. We've thus far only set attributes to lowercase, not attribute values. Consequently, we need |
|
325 | + // to translate() the letters that would be in 'NONE' ("NOE") to lowercase. |
|
326 | + $nodesWithStyleDisplayNone = $xpath->query('//*[contains(translate(translate(@style," ",""),"NOE","noe"),"display:none")]'); |
|
327 | + // The checks on parentNode and is_callable below ensure that if we've deleted the parent node, |
|
328 | + // we don't try to call removeChild on a nonexistent child node |
|
329 | + if ($nodesWithStyleDisplayNone->length > 0) { |
|
330 | + /** @var $node \DOMNode */ |
|
331 | + foreach ($nodesWithStyleDisplayNone as $node) { |
|
332 | + if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
333 | + $node->parentNode->removeChild($node); |
|
334 | + } |
|
335 | + } |
|
336 | + } |
|
337 | + |
|
338 | + $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument); |
|
339 | + |
|
340 | + if ($this->preserveEncoding) { |
|
341 | + if ( function_exists( 'mb_convert_encoding' ) ) { |
|
342 | + return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
343 | + } else { |
|
344 | + return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
|
345 | + } |
|
346 | + } else { |
|
347 | + return $xmlDocument->saveHTML(); |
|
348 | + } |
|
349 | + } |
|
350 | + |
|
351 | + public function strtolower(array $m) { |
|
352 | + return strtolower($m[0]); |
|
353 | + } |
|
354 | + |
|
355 | + |
|
356 | + /** |
|
357 | + * This method merges old or existing name/value array with new name/value array. |
|
358 | + * and then generates a string of the combined style suitable for placing inline. |
|
359 | + * This becomes the single point for CSS string generation allowing for consistent. |
|
360 | + * CSS output no matter where the CSS originally came from. |
|
361 | + * @param array $oldStyles |
|
362 | + * @param array $newStyles |
|
363 | + * @return string |
|
364 | + */ |
|
365 | + private function generateStyleStringFromDeclarationsArrays(array $oldStyles, array $newStyles) { |
|
366 | + $combinedStyles = array_merge($oldStyles, $newStyles); |
|
367 | + $style = ''; |
|
368 | + foreach ($combinedStyles as $attributeName => $attributeValue) { |
|
369 | + $style .= (strtolower(trim($attributeName)) . ': ' . trim($attributeValue) . '; '); |
|
370 | + } |
|
371 | + return trim($style); |
|
372 | + } |
|
373 | + |
|
374 | + |
|
375 | + /** |
|
376 | + * Copies the media part from CSS array parts to $xmlDocument. |
|
377 | + * |
|
378 | + * @param array $cssParts |
|
379 | + * @param DOMDocument $xmlDocument |
|
380 | + */ |
|
381 | + public function copyCssWithMediaToStyleNode(array $cssParts, DOMDocument $xmlDocument) { |
|
382 | + if (isset($cssParts['media']) && $cssParts['media'] !== '') { |
|
383 | + $this->addStyleElementToDocument($xmlDocument, $cssParts['media']); |
|
384 | + } |
|
385 | + } |
|
386 | + |
|
387 | + /** |
|
388 | + * Returns CSS content. |
|
389 | + * |
|
390 | + * @param DOMXPath $xpath |
|
391 | + * @return string |
|
392 | + */ |
|
393 | + private function getCssFromAllStyleNodes(DOMXPath $xpath) { |
|
394 | + $styleNodes = $xpath->query('//style'); |
|
395 | + |
|
396 | + if ($styleNodes === false) { |
|
397 | + return ''; |
|
398 | + } |
|
399 | + |
|
400 | + $css = ''; |
|
401 | + /** @var $styleNode DOMNode */ |
|
402 | + foreach ($styleNodes as $styleNode) { |
|
403 | + $css .= "\n\n" . $styleNode->nodeValue; |
|
404 | + $styleNode->parentNode->removeChild($styleNode); |
|
405 | + } |
|
406 | + |
|
407 | + return $css; |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * Adds a style element with $css to $document. |
|
412 | + * |
|
413 | + * @param DOMDocument $document |
|
414 | + * @param string $css |
|
415 | + */ |
|
416 | + private function addStyleElementToDocument(DOMDocument $document, $css) { |
|
417 | + $styleElement = $document->createElement('style', $css); |
|
418 | + $styleAttribute = $document->createAttribute('type'); |
|
419 | + $styleAttribute->value = 'text/css'; |
|
420 | + $styleElement->appendChild($styleAttribute); |
|
421 | + |
|
422 | + $head = $this->getOrCreateHeadElement($document); |
|
423 | + $head->appendChild($styleElement); |
|
424 | + } |
|
425 | + |
|
426 | + /** |
|
427 | + * Returns the existing or creates a new head element in $document. |
|
428 | + * |
|
429 | + * @param DOMDocument $document |
|
430 | + * @return DOMNode the head element |
|
431 | + */ |
|
432 | + private function getOrCreateHeadElement(DOMDocument $document) { |
|
433 | + $head = $document->getElementsByTagName('head')->item(0); |
|
434 | + |
|
435 | + if ($head === null) { |
|
436 | + $head = $document->createElement('head'); |
|
437 | + $html = $document->getElementsByTagName('html')->item(0); |
|
438 | + $html->insertBefore($head, $document->getElementsByTagName('body')->item(0)); |
|
439 | + } |
|
440 | + |
|
441 | + return $head; |
|
442 | + } |
|
443 | + |
|
444 | + /** |
|
445 | + * Splits input CSS code to an array where: |
|
446 | + * |
|
447 | + * - key "css" will be contains clean CSS code. |
|
448 | + * - key "media" will be contains all valuable media queries. |
|
449 | + * |
|
450 | + * Example: |
|
451 | + * |
|
452 | + * The CSS code. |
|
453 | + * |
|
454 | + * "@import "file.css"; h1 { color:red; } @media { h1 {}} @media tv { h1 {}}" |
|
455 | + * |
|
456 | + * will be parsed into the following array: |
|
457 | + * |
|
458 | + * "css" => "h1 { color:red; }" |
|
459 | + * "media" => "@media { h1 {}}" |
|
460 | + * |
|
461 | + * @param string $css |
|
462 | + * @return array |
|
463 | + */ |
|
464 | + private function splitCssAndMediaQuery($css) { |
|
465 | + $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
466 | + |
|
467 | + // filter the CSS |
|
468 | + $search = array( |
|
469 | + // get rid of css comment code |
|
470 | + '/\\/\\*.*\\*\\//sU', |
|
471 | + // strip out any import directives |
|
472 | + '/^\\s*@import\\s[^;]+;/misU', |
|
473 | + // strip remains media enclosures |
|
474 | + '/^\\s*@media\\s[^{]+{(.*)}\\s*}\\s/misU', |
|
475 | + ); |
|
476 | + |
|
477 | + $replace = array( |
|
478 | + '', |
|
479 | + '', |
|
480 | + '', |
|
481 | + ); |
|
482 | + |
|
483 | + // clean CSS before output |
|
484 | + $css = preg_replace($search, $replace, $css); |
|
485 | + |
|
486 | + return array('css' => $css, 'media' => self::$_media); |
|
487 | + } |
|
488 | + |
|
489 | + private function _media_concat( $matches ) { |
|
490 | + self::$_media .= $matches[0]; |
|
491 | + } |
|
492 | + |
|
493 | + /** |
|
494 | + * Creates a DOMDocument instance with the current HTML. |
|
495 | + * |
|
496 | + * @return DOMDocument |
|
497 | + */ |
|
498 | + private function createXmlDocument() { |
|
499 | + $xmlDocument = new DOMDocument; |
|
500 | + $xmlDocument->encoding = self::ENCODING; |
|
501 | + $xmlDocument->strictErrorChecking = false; |
|
502 | + $xmlDocument->formatOutput = true; |
|
503 | + $libXmlState = libxml_use_internal_errors(true); |
|
504 | + $xmlDocument->loadHTML($this->getUnifiedHtml()); |
|
505 | + libxml_clear_errors(); |
|
506 | + libxml_use_internal_errors($libXmlState); |
|
507 | + $xmlDocument->normalizeDocument(); |
|
508 | + |
|
509 | + return $xmlDocument; |
|
510 | + } |
|
511 | + |
|
512 | + /** |
|
513 | + * Returns the HTML with the non-ASCII characters converts into HTML entities and the unprocessable HTML tags removed. |
|
514 | + * |
|
515 | + * @return string the unified HTML |
|
516 | + * |
|
517 | + * @throws BadMethodCallException |
|
518 | + */ |
|
519 | + private function getUnifiedHtml() { |
|
520 | + if (!empty($this->unprocessableHtmlTags)) { |
|
521 | + $unprocessableHtmlTags = implode('|', $this->unprocessableHtmlTags); |
|
522 | + $bodyWithoutUnprocessableTags = preg_replace('/<\\/?(' . $unprocessableHtmlTags . ')[^>]*>/i', '', $this->html); |
|
523 | + } else { |
|
524 | + $bodyWithoutUnprocessableTags = $this->html; |
|
525 | + } |
|
526 | + |
|
527 | + if ( function_exists( 'mb_convert_encoding' ) ) { |
|
528 | + return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
529 | + } else { |
|
530 | + return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
|
531 | + } |
|
532 | + } |
|
533 | + |
|
534 | + /** |
|
535 | + * @param array $a |
|
536 | + * @param array $b |
|
537 | + * |
|
538 | + * @return integer |
|
539 | + */ |
|
540 | + private function sortBySelectorPrecedence(array $a, array $b) { |
|
541 | + $precedenceA = $this->getCssSelectorPrecedence($a['selector']); |
|
542 | + $precedenceB = $this->getCssSelectorPrecedence($b['selector']); |
|
543 | + |
|
544 | + // We want these sorted in ascending order so selectors with lesser precedence get processed first and |
|
545 | + // selectors with greater precedence get sorted last. |
|
546 | + // The parenthesis around the -1 are necessary to avoid a PHP_CodeSniffer warning about missing spaces around |
|
547 | + // arithmetic operators. |
|
548 | + // @see http://forge.typo3.org/issues/55605 |
|
549 | + $precedenceForEquals = ($a['line'] < $b['line'] ? (-1) : 1); |
|
550 | + $precedenceForNotEquals = ($precedenceA < $precedenceB ? (-1) : 1); |
|
551 | + return ($precedenceA === $precedenceB) ? $precedenceForEquals : $precedenceForNotEquals; |
|
552 | + } |
|
553 | + |
|
554 | + /** |
|
555 | + * @param string $selector |
|
556 | + * |
|
557 | + * @return integer |
|
558 | + */ |
|
559 | + private function getCssSelectorPrecedence($selector) { |
|
560 | + $selectorKey = md5($selector); |
|
561 | + if (!isset($this->caches[self::CACHE_KEY_SELECTOR][$selectorKey])) { |
|
562 | + $precedence = 0; |
|
563 | + $value = 100; |
|
564 | + // ids: worth 100, classes: worth 10, elements: worth 1 |
|
565 | + $search = array('\\#','\\.',''); |
|
566 | + |
|
567 | + foreach ($search as $s) { |
|
568 | + if (trim($selector == '')) { |
|
569 | + break; |
|
570 | + } |
|
571 | + $number = 0; |
|
572 | + $selector = preg_replace('/' . $s . '\\w+/', '', $selector, -1, $number); |
|
573 | + $precedence += ($value * $number); |
|
574 | + $value /= 10; |
|
575 | + } |
|
576 | + $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey] = $precedence; |
|
577 | + } |
|
578 | + |
|
579 | + return $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey]; |
|
580 | + } |
|
581 | + |
|
582 | + /** |
|
583 | + * Right now, we support all CSS 1 selectors and most CSS2/3 selectors. |
|
584 | + * |
|
585 | + * @see http://plasmasturm.org/log/444/ |
|
586 | + * |
|
587 | + * @param string $paramCssSelector |
|
588 | + * |
|
589 | + * @return string |
|
590 | + */ |
|
591 | + private function translateCssToXpath($paramCssSelector) { |
|
592 | + $cssSelector = ' ' . $paramCssSelector . ' '; |
|
593 | + $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
594 | + $cssSelector = trim($cssSelector); |
|
595 | + $xpathKey = md5($cssSelector); |
|
596 | + if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
|
597 | + // returns an Xpath selector |
|
598 | + $search = array( |
|
599 | + // Matches any element that is a child of parent. |
|
600 | + '/\\s+>\\s+/', |
|
601 | + // Matches any element that is an adjacent sibling. |
|
602 | + '/\\s+\\+\\s+/', |
|
603 | + // Matches any element that is a descendant of an parent element element. |
|
604 | + '/\\s+/', |
|
605 | + // first-child pseudo-selector |
|
606 | + '/([^\\/]+):first-child/i', |
|
607 | + // last-child pseudo-selector |
|
608 | + '/([^\\/]+):last-child/i', |
|
609 | + // Matches attribute only selector |
|
610 | + '/^\\[(\\w+)\\]/', |
|
611 | + // Matches element with attribute |
|
612 | + '/(\\w)\\[(\\w+)\\]/', |
|
613 | + // Matches element with EXACT attribute |
|
614 | + '/(\\w)\\[(\\w+)\\=[\'"]?(\\w+)[\'"]?\\]/', |
|
615 | + ); |
|
616 | + $replace = array( |
|
617 | + '/', |
|
618 | + '/following-sibling::*[1]/self::', |
|
619 | + '//', |
|
620 | + '*[1]/self::\\1', |
|
621 | + '*[last()]/self::\\1', |
|
622 | + '*[@\\1]', |
|
623 | + '\\1[@\\2]', |
|
624 | + '\\1[@\\2="\\3"]', |
|
625 | + ); |
|
626 | + |
|
627 | + $cssSelector = '//' . preg_replace($search, $replace, $cssSelector); |
|
628 | + |
|
629 | + $cssSelector = preg_replace_callback(self::ID_ATTRIBUTE_MATCHER, array($this, 'matchIdAttributes'), $cssSelector); |
|
630 | + $cssSelector = preg_replace_callback(self::CLASS_ATTRIBUTE_MATCHER, array($this, 'matchClassAttributes'), $cssSelector); |
|
631 | + |
|
632 | + // Advanced selectors are going to require a bit more advanced emogrification. |
|
633 | + // When we required PHP 5.3, we could do this with closures. |
|
634 | + $cssSelector = preg_replace_callback( |
|
635 | + '/([^\\/]+):nth-child\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
636 | + array($this, 'translateNthChild'), $cssSelector |
|
637 | + ); |
|
638 | + $cssSelector = preg_replace_callback( |
|
639 | + '/([^\\/]+):nth-of-type\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
640 | + array($this, 'translateNthOfType'), $cssSelector |
|
641 | + ); |
|
642 | + |
|
643 | + $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey] = $cssSelector; |
|
644 | + } |
|
645 | + return $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey]; |
|
646 | + } |
|
647 | + |
|
648 | + /** |
|
649 | + * @param array $match |
|
650 | + * |
|
651 | + * @return string |
|
652 | + */ |
|
653 | + private function matchIdAttributes(array $match) { |
|
654 | + return (strlen($match[1]) ? $match[1] : '*') . '[@id="' . $match[2] . '"]'; |
|
655 | + } |
|
656 | + |
|
657 | + /** |
|
658 | + * @param array $match |
|
659 | + * |
|
660 | + * @return string |
|
661 | + */ |
|
662 | + private function matchClassAttributes(array $match) { |
|
663 | + return (strlen($match[1]) ? $match[1] : '*') . '[contains(concat(" ",@class," "),concat(" ","' . |
|
664 | + implode( |
|
665 | + '"," "))][contains(concat(" ",@class," "),concat(" ","', |
|
666 | + explode('.', substr($match[2], 1)) |
|
667 | + ) . '"," "))]'; |
|
668 | + } |
|
669 | + |
|
670 | + /** |
|
671 | + * @param array $match |
|
672 | + * |
|
673 | + * @return string |
|
674 | + */ |
|
675 | + private function translateNthChild(array $match) { |
|
676 | + $result = $this->parseNth($match); |
|
677 | + |
|
678 | + if (isset($result[self::MULTIPLIER])) { |
|
679 | + if ($result[self::MULTIPLIER] < 0) { |
|
680 | + $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
681 | + return sprintf('*[(last() - position()) mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
682 | + } else { |
|
683 | + return sprintf('*[position() mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
684 | + } |
|
685 | + } else { |
|
686 | + return sprintf('*[%u]/self::%s', $result[self::INDEX], $match[1]); |
|
687 | + } |
|
688 | + } |
|
689 | + |
|
690 | + /** |
|
691 | + * @param array $match |
|
692 | + * |
|
693 | + * @return string |
|
694 | + */ |
|
695 | + private function translateNthOfType(array $match) { |
|
696 | + $result = $this->parseNth($match); |
|
697 | + |
|
698 | + if (isset($result[self::MULTIPLIER])) { |
|
699 | + if ($result[self::MULTIPLIER] < 0) { |
|
700 | + $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
701 | + return sprintf('%s[(last() - position()) mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
702 | + } else { |
|
703 | + return sprintf('%s[position() mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
704 | + } |
|
705 | + } else { |
|
706 | + return sprintf('%s[%u]', $match[1], $result[self::INDEX]); |
|
707 | + } |
|
708 | + } |
|
709 | + |
|
710 | + /** |
|
711 | + * @param array $match |
|
712 | + * |
|
713 | + * @return array |
|
714 | + */ |
|
715 | + private function parseNth(array $match) { |
|
716 | + if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
717 | + $index = strtolower($match[2]) == 'even' ? 0 : 1; |
|
718 | + return array(self::MULTIPLIER => 2, self::INDEX => $index); |
|
719 | + } elseif (stripos($match[2], 'n') === false) { |
|
720 | + // if there is a multiplier |
|
721 | + $index = intval(str_replace(' ', '', $match[2])); |
|
722 | + return array(self::INDEX => $index); |
|
723 | + } else { |
|
724 | + if (isset($match[3])) { |
|
725 | + $multipleTerm = str_replace($match[3], '', $match[2]); |
|
726 | + $index = intval(str_replace(' ', '', $match[3])); |
|
727 | + } else { |
|
728 | + $multipleTerm = $match[2]; |
|
729 | + $index = 0; |
|
730 | + } |
|
731 | + |
|
732 | + $multiplier = str_ireplace('n', '', $multipleTerm); |
|
733 | + |
|
734 | + if (!strlen($multiplier)) { |
|
735 | + $multiplier = 1; |
|
736 | + } elseif ($multiplier == 0) { |
|
737 | + return array(self::INDEX => $index); |
|
738 | + } else { |
|
739 | + $multiplier = intval($multiplier); |
|
740 | + } |
|
741 | + |
|
742 | + while ($index < 0) { |
|
743 | + $index += abs($multiplier); |
|
744 | + } |
|
745 | + |
|
746 | + return array(self::MULTIPLIER => $multiplier, self::INDEX => $index); |
|
747 | + } |
|
748 | + } |
|
749 | + |
|
750 | + /** |
|
751 | + * Parses a CSS declaration block into property name/value pairs. |
|
752 | + * |
|
753 | + * Example: |
|
754 | + * |
|
755 | + * The declaration block. |
|
756 | + * |
|
757 | + * "color: #000; font-weight: bold;". |
|
758 | + * |
|
759 | + * will be parsed into the following array: |
|
760 | + * |
|
761 | + * "color" => "#000" |
|
762 | + * "font-weight" => "bold" |
|
763 | + * |
|
764 | + * @param string $cssDeclarationBlock the CSS declaration block without the curly braces, may be empty |
|
765 | + * |
|
766 | + * @return array the CSS declarations with the property names as array keys and the property values as array values |
|
767 | + */ |
|
768 | + private function parseCssDeclarationBlock($cssDeclarationBlock) { |
|
769 | + if (isset($this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock])) { |
|
770 | + return $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock]; |
|
771 | + } |
|
772 | + |
|
773 | + $properties = array(); |
|
774 | + $declarations = explode(';', $cssDeclarationBlock); |
|
775 | + foreach ($declarations as $declaration) { |
|
776 | + $matches = array(); |
|
777 | + if (!preg_match('/ *([A-Za-z\\-]+) *: *([^;]+) */', $declaration, $matches)) { |
|
778 | + continue; |
|
779 | + } |
|
780 | + $propertyName = strtolower($matches[1]); |
|
781 | + $propertyValue = $matches[2]; |
|
782 | + $properties[$propertyName] = $propertyValue; |
|
783 | + } |
|
784 | + $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock] = $properties; |
|
785 | + |
|
786 | + return $properties; |
|
787 | + } |
|
788 | 788 | } |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | if ($nodesWithStyleAttributes !== false) { |
239 | 239 | /** @var $nodeWithStyleAttribute DOMNode */ |
240 | 240 | foreach ($nodesWithStyleAttributes as $node) { |
241 | - $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
241 | + $normalizedOriginalStyle = preg_replace_callback('/[A-z\\-]+(?=\\:)/S', array($this, 'strtolower'), $node->getAttribute('style')); |
|
242 | 242 | |
243 | 243 | // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
244 | 244 | $nodePath = $node->getNodePath(); |
@@ -289,7 +289,7 @@ discard block |
||
289 | 289 | } |
290 | 290 | |
291 | 291 | // now sort the selectors by precedence |
292 | - usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
292 | + usort($allSelectors, array($this, 'sortBySelectorPrecedence')); |
|
293 | 293 | |
294 | 294 | $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
295 | 295 | } |
@@ -329,7 +329,7 @@ discard block |
||
329 | 329 | if ($nodesWithStyleDisplayNone->length > 0) { |
330 | 330 | /** @var $node \DOMNode */ |
331 | 331 | foreach ($nodesWithStyleDisplayNone as $node) { |
332 | - if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
332 | + if ($node->parentNode && is_callable(array($node->parentNode, 'removeChild'))) { |
|
333 | 333 | $node->parentNode->removeChild($node); |
334 | 334 | } |
335 | 335 | } |
@@ -338,10 +338,10 @@ discard block |
||
338 | 338 | $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument); |
339 | 339 | |
340 | 340 | if ($this->preserveEncoding) { |
341 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
342 | - return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
341 | + if (function_exists('mb_convert_encoding')) { |
|
342 | + return mb_convert_encoding($xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES'); |
|
343 | 343 | } else { |
344 | - return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
|
344 | + return htmlspecialchars_decode(utf8_encode(html_entity_decode($xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING))); |
|
345 | 345 | } |
346 | 346 | } else { |
347 | 347 | return $xmlDocument->saveHTML(); |
@@ -462,7 +462,7 @@ discard block |
||
462 | 462 | * @return array |
463 | 463 | */ |
464 | 464 | private function splitCssAndMediaQuery($css) { |
465 | - $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
465 | + $css = preg_replace_callback('#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array($this, '_media_concat'), $css); |
|
466 | 466 | |
467 | 467 | // filter the CSS |
468 | 468 | $search = array( |
@@ -486,7 +486,7 @@ discard block |
||
486 | 486 | return array('css' => $css, 'media' => self::$_media); |
487 | 487 | } |
488 | 488 | |
489 | - private function _media_concat( $matches ) { |
|
489 | + private function _media_concat($matches) { |
|
490 | 490 | self::$_media .= $matches[0]; |
491 | 491 | } |
492 | 492 | |
@@ -524,10 +524,10 @@ discard block |
||
524 | 524 | $bodyWithoutUnprocessableTags = $this->html; |
525 | 525 | } |
526 | 526 | |
527 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
528 | - return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
527 | + if (function_exists('mb_convert_encoding')) { |
|
528 | + return mb_convert_encoding($bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING); |
|
529 | 529 | } else { |
530 | - return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
|
530 | + return htmlspecialchars_decode(utf8_decode(htmlentities($bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false))); |
|
531 | 531 | } |
532 | 532 | } |
533 | 533 | |
@@ -562,7 +562,7 @@ discard block |
||
562 | 562 | $precedence = 0; |
563 | 563 | $value = 100; |
564 | 564 | // ids: worth 100, classes: worth 10, elements: worth 1 |
565 | - $search = array('\\#','\\.',''); |
|
565 | + $search = array('\\#', '\\.', ''); |
|
566 | 566 | |
567 | 567 | foreach ($search as $s) { |
568 | 568 | if (trim($selector == '')) { |
@@ -590,7 +590,7 @@ discard block |
||
590 | 590 | */ |
591 | 591 | private function translateCssToXpath($paramCssSelector) { |
592 | 592 | $cssSelector = ' ' . $paramCssSelector . ' '; |
593 | - $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
593 | + $cssSelector = preg_replace_callback('/\s+\w+\s+/', array($this, 'strtolower'), $cssSelector); |
|
594 | 594 | $cssSelector = trim($cssSelector); |
595 | 595 | $xpathKey = md5($cssSelector); |
596 | 596 | if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
@@ -713,7 +713,7 @@ discard block |
||
713 | 713 | * @return array |
714 | 714 | */ |
715 | 715 | private function parseNth(array $match) { |
716 | - if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
716 | + if (in_array(strtolower($match[2]), array('even', 'odd'))) { |
|
717 | 717 | $index = strtolower($match[2]) == 'even' ? 0 : 1; |
718 | 718 | return array(self::MULTIPLIER => 2, self::INDEX => $index); |
719 | 719 | } elseif (stripos($match[2], 'n') === false) { |
@@ -95,20 +95,24 @@ |
||
95 | 95 | } |
96 | 96 | |
97 | 97 | if ( !empty( $user_info ) ) { |
98 | - if( empty( $address['first_name'] ) ) |
|
99 | - $address['first_name'] = $user_info->first_name; |
|
98 | + if( empty( $address['first_name'] ) ) { |
|
99 | + $address['first_name'] = $user_info->first_name; |
|
100 | + } |
|
100 | 101 | |
101 | - if( empty( $address['last_name'] ) ) |
|
102 | - $address['last_name'] = $user_info->last_name; |
|
102 | + if( empty( $address['last_name'] ) ) { |
|
103 | + $address['last_name'] = $user_info->last_name; |
|
104 | + } |
|
103 | 105 | } |
104 | 106 | |
105 | 107 | $address['name'] = trim( trim( $address['first_name'] . ' ' . $address['last_name'] ), "," ); |
106 | 108 | |
107 | - if( empty( $address['state'] ) && $with_default ) |
|
108 | - $address['state'] = wpinv_get_default_state(); |
|
109 | + if( empty( $address['state'] ) && $with_default ) { |
|
110 | + $address['state'] = wpinv_get_default_state(); |
|
111 | + } |
|
109 | 112 | |
110 | - if( empty( $address['country'] ) && $with_default ) |
|
111 | - $address['country'] = wpinv_get_default_country(); |
|
113 | + if( empty( $address['country'] ) && $with_default ) { |
|
114 | + $address['country'] = wpinv_get_default_country(); |
|
115 | + } |
|
112 | 116 | |
113 | 117 | |
114 | 118 | return $address; |
@@ -13,9 +13,9 @@ discard block |
||
13 | 13 | |
14 | 14 | |
15 | 15 | function wpinv_get_default_country() { |
16 | - $country = wpinv_get_option( 'default_country', 'UK' ); |
|
16 | + $country = wpinv_get_option( 'default_country', 'UK' ); |
|
17 | 17 | |
18 | - return apply_filters( 'wpinv_default_country', $country ); |
|
18 | + return apply_filters( 'wpinv_default_country', $country ); |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | /** |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | */ |
27 | 27 | function wpinv_sanitize_country( $country ) { |
28 | 28 | |
29 | - // Enure the country is specified |
|
29 | + // Enure the country is specified |
|
30 | 30 | if ( empty( $country ) ) { |
31 | 31 | $country = wpinv_get_default_country(); |
32 | 32 | } |
@@ -56,9 +56,9 @@ discard block |
||
56 | 56 | } |
57 | 57 | |
58 | 58 | function wpinv_get_default_state() { |
59 | - $state = wpinv_get_option( 'default_state', false ); |
|
59 | + $state = wpinv_get_option( 'default_state', false ); |
|
60 | 60 | |
61 | - return apply_filters( 'wpinv_default_state', $state ); |
|
61 | + return apply_filters( 'wpinv_default_state', $state ); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | function wpinv_state_name( $state_code = '', $country_code = '' ) { |
@@ -166,11 +166,11 @@ discard block |
||
166 | 166 | |
167 | 167 | $country = wpinv_sanitize_country( $country ); |
168 | 168 | |
169 | - foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) { |
|
170 | - if ( false !== array_search( $country, $countries, true ) ) { |
|
171 | - return $continent_code; |
|
172 | - } |
|
173 | - } |
|
169 | + foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) { |
|
170 | + if ( false !== array_search( $country, $countries, true ) ) { |
|
171 | + return $continent_code; |
|
172 | + } |
|
173 | + } |
|
174 | 174 | |
175 | 175 | return ''; |
176 | 176 | |
@@ -462,30 +462,30 @@ discard block |
||
462 | 462 | } |
463 | 463 | |
464 | 464 | function wpinv_get_states_field() { |
465 | - if( empty( $_POST['country'] ) ) { |
|
466 | - $_POST['country'] = wpinv_get_default_country(); |
|
467 | - } |
|
468 | - $states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) ); |
|
465 | + if( empty( $_POST['country'] ) ) { |
|
466 | + $_POST['country'] = wpinv_get_default_country(); |
|
467 | + } |
|
468 | + $states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) ); |
|
469 | 469 | |
470 | - if( !empty( $states ) ) { |
|
471 | - $sanitized_field_name = sanitize_text_field( $_POST['field_name'] ); |
|
470 | + if( !empty( $states ) ) { |
|
471 | + $sanitized_field_name = sanitize_text_field( $_POST['field_name'] ); |
|
472 | 472 | |
473 | 473 | $args = array( |
474 | - 'name' => $sanitized_field_name, |
|
475 | - 'id' => $sanitized_field_name, |
|
476 | - 'class' => $sanitized_field_name . 'custom-select wpinv-select wpi_select2', |
|
477 | - 'options' => array_merge( array( '' => '' ), $states ), |
|
478 | - 'show_option_all' => false, |
|
479 | - 'show_option_none' => false |
|
480 | - ); |
|
481 | - |
|
482 | - $response = wpinv_html_select( $args ); |
|
483 | - |
|
484 | - } else { |
|
485 | - $response = 'nostates'; |
|
486 | - } |
|
474 | + 'name' => $sanitized_field_name, |
|
475 | + 'id' => $sanitized_field_name, |
|
476 | + 'class' => $sanitized_field_name . 'custom-select wpinv-select wpi_select2', |
|
477 | + 'options' => array_merge( array( '' => '' ), $states ), |
|
478 | + 'show_option_all' => false, |
|
479 | + 'show_option_none' => false |
|
480 | + ); |
|
481 | + |
|
482 | + $response = wpinv_html_select( $args ); |
|
483 | + |
|
484 | + } else { |
|
485 | + $response = 'nostates'; |
|
486 | + } |
|
487 | 487 | |
488 | - return $response; |
|
488 | + return $response; |
|
489 | 489 | } |
490 | 490 | |
491 | 491 | function wpinv_default_billing_country( $country = '', $user_id = 0 ) { |
@@ -503,46 +503,46 @@ discard block |
||
503 | 503 | */ |
504 | 504 | function wpinv_get_address_formats() { |
505 | 505 | |
506 | - return apply_filters( 'wpinv_localisation_address_formats', |
|
507 | - array( |
|
508 | - 'default' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}\n{{zip}}\n{{country}}", |
|
509 | - 'AU' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}} {{zip}}\n{{country}}", |
|
510 | - 'AT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
511 | - 'BE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
512 | - 'CA' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{state_code}} {{zip}}\n{{country}}", |
|
513 | - 'CH' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
514 | - 'CL' => "{{company}}\n{{name}}\n{{address}}\n{{state}}\n{{zip}} {{city}}\n{{country}}", |
|
515 | - 'CN' => "{{country}} {{zip}}\n{{state}}, {{city}}, {{address}}\n{{company}}\n{{name}}", |
|
516 | - 'CZ' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
517 | - 'DE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
518 | - 'EE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
519 | - 'FI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
520 | - 'DK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
521 | - 'FR' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city_upper}}\n{{country}}", |
|
522 | - 'HK' => "{{company}}\n{{first_name}} {{last_name_upper}}\n{{address}}\n{{city_upper}}\n{{state_upper}}\n{{country}}", |
|
523 | - 'HU' => "{{name}}\n{{company}}\n{{city}}\n{{address}}\n{{zip}}\n{{country}}", |
|
524 | - 'IN' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{zip}}\n{{state}}, {{country}}", |
|
525 | - 'IS' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
526 | - 'IT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}}\n{{city}}\n{{state_upper}}\n{{country}}", |
|
527 | - 'JP' => "{{zip}}\n{{state}} {{city}} {{address}}\n{{company}}\n{{last_name}} {{first_name}}\n{{country}}", |
|
528 | - 'TW' => "{{company}}\n{{last_name}} {{first_name}}\n{{address}}\n{{state}}, {{city}} {{zip}}\n{{country}}", |
|
529 | - 'LI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
530 | - 'NL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
531 | - 'NZ' => "{{name}}\n{{company}}\n{{address}}\n{{city}} {{zip}}\n{{country}}", |
|
532 | - 'NO' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
533 | - 'PL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
534 | - 'PT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
535 | - 'SK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
536 | - 'RS' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
537 | - 'SI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
538 | - 'ES' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{state}}\n{{country}}", |
|
539 | - 'SE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
540 | - 'TR' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}} {{state}}\n{{country}}", |
|
541 | - 'UG' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}, {{country}}", |
|
542 | - 'US' => "{{name}}\n{{company}}\n{{address}}\n{{city}}, {{state_code}} {{zip}}\n{{country}}", |
|
543 | - 'VN' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{country}}", |
|
544 | - ) |
|
545 | - ); |
|
506 | + return apply_filters( 'wpinv_localisation_address_formats', |
|
507 | + array( |
|
508 | + 'default' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}\n{{zip}}\n{{country}}", |
|
509 | + 'AU' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}} {{zip}}\n{{country}}", |
|
510 | + 'AT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
511 | + 'BE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
512 | + 'CA' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{state_code}} {{zip}}\n{{country}}", |
|
513 | + 'CH' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
514 | + 'CL' => "{{company}}\n{{name}}\n{{address}}\n{{state}}\n{{zip}} {{city}}\n{{country}}", |
|
515 | + 'CN' => "{{country}} {{zip}}\n{{state}}, {{city}}, {{address}}\n{{company}}\n{{name}}", |
|
516 | + 'CZ' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
517 | + 'DE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
518 | + 'EE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
519 | + 'FI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
520 | + 'DK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
521 | + 'FR' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city_upper}}\n{{country}}", |
|
522 | + 'HK' => "{{company}}\n{{first_name}} {{last_name_upper}}\n{{address}}\n{{city_upper}}\n{{state_upper}}\n{{country}}", |
|
523 | + 'HU' => "{{name}}\n{{company}}\n{{city}}\n{{address}}\n{{zip}}\n{{country}}", |
|
524 | + 'IN' => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{zip}}\n{{state}}, {{country}}", |
|
525 | + 'IS' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
526 | + 'IT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}}\n{{city}}\n{{state_upper}}\n{{country}}", |
|
527 | + 'JP' => "{{zip}}\n{{state}} {{city}} {{address}}\n{{company}}\n{{last_name}} {{first_name}}\n{{country}}", |
|
528 | + 'TW' => "{{company}}\n{{last_name}} {{first_name}}\n{{address}}\n{{state}}, {{city}} {{zip}}\n{{country}}", |
|
529 | + 'LI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
530 | + 'NL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
531 | + 'NZ' => "{{name}}\n{{company}}\n{{address}}\n{{city}} {{zip}}\n{{country}}", |
|
532 | + 'NO' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
533 | + 'PL' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
534 | + 'PT' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
535 | + 'SK' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
536 | + 'RS' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
537 | + 'SI' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
538 | + 'ES' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{state}}\n{{country}}", |
|
539 | + 'SE' => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}", |
|
540 | + 'TR' => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}} {{state}}\n{{country}}", |
|
541 | + 'UG' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}, {{country}}", |
|
542 | + 'US' => "{{name}}\n{{company}}\n{{address}}\n{{city}}, {{state_code}} {{zip}}\n{{country}}", |
|
543 | + 'VN' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{country}}", |
|
544 | + ) |
|
545 | + ); |
|
546 | 546 | } |
547 | 547 | |
548 | 548 | /** |
@@ -559,21 +559,21 @@ discard block |
||
559 | 559 | } |
560 | 560 | |
561 | 561 | // Get all formats. |
562 | - $formats = wpinv_get_address_formats(); |
|
562 | + $formats = wpinv_get_address_formats(); |
|
563 | 563 | |
564 | - // Get format for the specified country. |
|
565 | - $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default']; |
|
564 | + // Get format for the specified country. |
|
565 | + $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default']; |
|
566 | 566 | |
567 | 567 | /** |
568 | - * Filters the address format to use on Invoices. |
|
568 | + * Filters the address format to use on Invoices. |
|
569 | 569 | * |
570 | 570 | * New lines will be replaced by a `br` element. Double new lines will be replaced by a paragraph. HTML tags are allowed. |
571 | - * |
|
572 | - * @since 1.0.13 |
|
573 | - * |
|
574 | - * @param string $format The address format to use. |
|
571 | + * |
|
572 | + * @since 1.0.13 |
|
573 | + * |
|
574 | + * @param string $format The address format to use. |
|
575 | 575 | * @param string $country The country who's address format is being retrieved. |
576 | - */ |
|
576 | + */ |
|
577 | 577 | return apply_filters( 'wpinv_get_full_address_format', $format, $country ); |
578 | 578 | } |
579 | 579 | |
@@ -594,8 +594,8 @@ discard block |
||
594 | 594 | 'country' => '', |
595 | 595 | 'zip' => '', |
596 | 596 | 'first_name' => '', |
597 | - 'last_name' => '', |
|
598 | - 'company' => '', |
|
597 | + 'last_name' => '', |
|
598 | + 'company' => '', |
|
599 | 599 | ); |
600 | 600 | |
601 | 601 | $args = map_deep( wp_parse_args( $billing_details, $default_args ), 'trim' ); |
@@ -616,14 +616,14 @@ discard block |
||
616 | 616 | $args['country_code']= $country; |
617 | 617 | |
618 | 618 | /** |
619 | - * Filters the address format replacements to use on Invoices. |
|
619 | + * Filters the address format replacements to use on Invoices. |
|
620 | 620 | * |
621 | - * |
|
622 | - * @since 1.0.13 |
|
623 | - * |
|
624 | - * @param array $replacements The address replacements to use. |
|
621 | + * |
|
622 | + * @since 1.0.13 |
|
623 | + * |
|
624 | + * @param array $replacements The address replacements to use. |
|
625 | 625 | * @param array $billing_details The billing details to use. |
626 | - */ |
|
626 | + */ |
|
627 | 627 | $replacements = apply_filters( 'wpinv_get_invoice_address_replacements', $args, $billing_details ); |
628 | 628 | |
629 | 629 | $return = array(); |
@@ -646,5 +646,5 @@ discard block |
||
646 | 646 | * @return string |
647 | 647 | */ |
648 | 648 | function wpinv_trim_formatted_address_line( $line ) { |
649 | - return trim( $line, ', ' ); |
|
649 | + return trim( $line, ', ' ); |
|
650 | 650 | } |
651 | 651 | \ No newline at end of file |
@@ -7,15 +7,15 @@ discard block |
||
7 | 7 | */ |
8 | 8 | |
9 | 9 | // MUST have WordPress. |
10 | -if ( !defined( 'WPINC' ) ) { |
|
11 | - exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) ); |
|
10 | +if (!defined('WPINC')) { |
|
11 | + exit('Do NOT access this file directly: ' . basename(__FILE__)); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | |
15 | 15 | function wpinv_get_default_country() { |
16 | - $country = wpinv_get_option( 'default_country', 'UK' ); |
|
16 | + $country = wpinv_get_option('default_country', 'UK'); |
|
17 | 17 | |
18 | - return apply_filters( 'wpinv_default_country', $country ); |
|
18 | + return apply_filters('wpinv_default_country', $country); |
|
19 | 19 | } |
20 | 20 | |
21 | 21 | /** |
@@ -24,59 +24,59 @@ discard block |
||
24 | 24 | * @param string $country The country code to sanitize |
25 | 25 | * @return array |
26 | 26 | */ |
27 | -function wpinv_sanitize_country( $country ) { |
|
27 | +function wpinv_sanitize_country($country) { |
|
28 | 28 | |
29 | 29 | // Enure the country is specified |
30 | - if ( empty( $country ) ) { |
|
30 | + if (empty($country)) { |
|
31 | 31 | $country = wpinv_get_default_country(); |
32 | 32 | } |
33 | - return trim( wpinv_utf8_strtoupper( $country ) ); |
|
33 | + return trim(wpinv_utf8_strtoupper($country)); |
|
34 | 34 | |
35 | 35 | } |
36 | 36 | |
37 | -function wpinv_is_base_country( $country ) { |
|
37 | +function wpinv_is_base_country($country) { |
|
38 | 38 | $base_country = wpinv_get_default_country(); |
39 | 39 | |
40 | - if ( $base_country === 'UK' ) { |
|
40 | + if ($base_country === 'UK') { |
|
41 | 41 | $base_country = 'GB'; |
42 | 42 | } |
43 | - if ( $country == 'UK' ) { |
|
43 | + if ($country == 'UK') { |
|
44 | 44 | $country = 'GB'; |
45 | 45 | } |
46 | 46 | |
47 | - return ( $country && $country === $base_country ) ? true : false; |
|
47 | + return ($country && $country === $base_country) ? true : false; |
|
48 | 48 | } |
49 | 49 | |
50 | -function wpinv_country_name( $country_code = '' ) { |
|
50 | +function wpinv_country_name($country_code = '') { |
|
51 | 51 | $countries = wpinv_get_country_list(); |
52 | 52 | $country_code = $country_code == 'UK' ? 'GB' : $country_code; |
53 | - $country = isset( $countries[$country_code] ) ? $countries[$country_code] : $country_code; |
|
53 | + $country = isset($countries[$country_code]) ? $countries[$country_code] : $country_code; |
|
54 | 54 | |
55 | - return apply_filters( 'wpinv_country_name', $country, $country_code ); |
|
55 | + return apply_filters('wpinv_country_name', $country, $country_code); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | function wpinv_get_default_state() { |
59 | - $state = wpinv_get_option( 'default_state', false ); |
|
59 | + $state = wpinv_get_option('default_state', false); |
|
60 | 60 | |
61 | - return apply_filters( 'wpinv_default_state', $state ); |
|
61 | + return apply_filters('wpinv_default_state', $state); |
|
62 | 62 | } |
63 | 63 | |
64 | -function wpinv_state_name( $state_code = '', $country_code = '' ) { |
|
64 | +function wpinv_state_name($state_code = '', $country_code = '') { |
|
65 | 65 | $state = $state_code; |
66 | 66 | |
67 | - if ( !empty( $country_code ) ) { |
|
68 | - $states = wpinv_get_country_states( $country_code ); |
|
67 | + if (!empty($country_code)) { |
|
68 | + $states = wpinv_get_country_states($country_code); |
|
69 | 69 | |
70 | - $state = !empty( $states ) && isset( $states[$state_code] ) ? $states[$state_code] : $state; |
|
70 | + $state = !empty($states) && isset($states[$state_code]) ? $states[$state_code] : $state; |
|
71 | 71 | } |
72 | 72 | |
73 | - return apply_filters( 'wpinv_state_name', $state, $state_code, $country_code ); |
|
73 | + return apply_filters('wpinv_state_name', $state, $state_code, $country_code); |
|
74 | 74 | } |
75 | 75 | |
76 | 76 | function wpinv_store_address() { |
77 | - $address = wpinv_get_option( 'store_address', '' ); |
|
77 | + $address = wpinv_get_option('store_address', ''); |
|
78 | 78 | |
79 | - return apply_filters( 'wpinv_store_address', $address ); |
|
79 | + return apply_filters('wpinv_store_address', $address); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
@@ -86,13 +86,13 @@ discard block |
||
86 | 86 | * |
87 | 87 | * @param WPInv_Invoice $invoice |
88 | 88 | */ |
89 | -function getpaid_save_invoice_user_address( $invoice ) { |
|
89 | +function getpaid_save_invoice_user_address($invoice) { |
|
90 | 90 | |
91 | 91 | // Retrieve the invoice. |
92 | - $invoice = wpinv_get_invoice( $invoice ); |
|
92 | + $invoice = wpinv_get_invoice($invoice); |
|
93 | 93 | |
94 | 94 | // Abort if it does not exist. |
95 | - if ( empty( $invoice ) ) { |
|
95 | + if (empty($invoice)) { |
|
96 | 96 | return; |
97 | 97 | } |
98 | 98 | |
@@ -110,25 +110,25 @@ discard block |
||
110 | 110 | 'zip' |
111 | 111 | ); |
112 | 112 | |
113 | - foreach ( $address_fields as $field ) { |
|
113 | + foreach ($address_fields as $field) { |
|
114 | 114 | $method = "get_{$field}"; |
115 | 115 | $value = $invoice->$method(); |
116 | 116 | |
117 | 117 | // Only save if it is not empty. |
118 | - if ( ! empty( $value ) ) { |
|
119 | - update_user_meta( $invoice->get_user_id(), '_wpinv_' . $field, $value ); |
|
118 | + if (!empty($value)) { |
|
119 | + update_user_meta($invoice->get_user_id(), '_wpinv_' . $field, $value); |
|
120 | 120 | } |
121 | 121 | } |
122 | 122 | |
123 | 123 | } |
124 | -add_action( 'getpaid_new_invoice', 'getpaid_save_invoice_user_address' ); |
|
125 | -add_action( 'getpaid_update_invoice', 'getpaid_save_invoice_user_address' ); |
|
124 | +add_action('getpaid_new_invoice', 'getpaid_save_invoice_user_address'); |
|
125 | +add_action('getpaid_update_invoice', 'getpaid_save_invoice_user_address'); |
|
126 | 126 | |
127 | -function wpinv_get_user_address( $user_id = 0, $with_default = true ) { |
|
127 | +function wpinv_get_user_address($user_id = 0, $with_default = true) { |
|
128 | 128 | global $wpi_userID; |
129 | 129 | |
130 | - if( empty( $user_id ) ) { |
|
131 | - $user_id = !empty( $wpi_userID ) ? $wpi_userID : get_current_user_id(); |
|
130 | + if (empty($user_id)) { |
|
131 | + $user_id = !empty($wpi_userID) ? $wpi_userID : get_current_user_id(); |
|
132 | 132 | } |
133 | 133 | |
134 | 134 | $address_fields = array( |
@@ -145,29 +145,29 @@ discard block |
||
145 | 145 | 'phone', |
146 | 146 | ); |
147 | 147 | |
148 | - $user_info = get_userdata( $user_id ); |
|
148 | + $user_info = get_userdata($user_id); |
|
149 | 149 | |
150 | 150 | $address = array(); |
151 | 151 | $address['user_id'] = $user_id; |
152 | - $address['email'] = !empty( $user_info ) ? $user_info->user_email : ''; |
|
153 | - foreach ( $address_fields as $field ) { |
|
154 | - $address[$field] = get_user_meta( $user_id, '_wpinv_' . $field, true ); |
|
152 | + $address['email'] = !empty($user_info) ? $user_info->user_email : ''; |
|
153 | + foreach ($address_fields as $field) { |
|
154 | + $address[$field] = get_user_meta($user_id, '_wpinv_' . $field, true); |
|
155 | 155 | } |
156 | 156 | |
157 | - if ( !empty( $user_info ) ) { |
|
158 | - if( empty( $address['first_name'] ) ) |
|
157 | + if (!empty($user_info)) { |
|
158 | + if (empty($address['first_name'])) |
|
159 | 159 | $address['first_name'] = $user_info->first_name; |
160 | 160 | |
161 | - if( empty( $address['last_name'] ) ) |
|
161 | + if (empty($address['last_name'])) |
|
162 | 162 | $address['last_name'] = $user_info->last_name; |
163 | 163 | } |
164 | 164 | |
165 | - $address['name'] = trim( trim( $address['first_name'] . ' ' . $address['last_name'] ), "," ); |
|
165 | + $address['name'] = trim(trim($address['first_name'] . ' ' . $address['last_name']), ","); |
|
166 | 166 | |
167 | - if( empty( $address['state'] ) && $with_default ) |
|
167 | + if (empty($address['state']) && $with_default) |
|
168 | 168 | $address['state'] = wpinv_get_default_state(); |
169 | 169 | |
170 | - if( empty( $address['country'] ) && $with_default ) |
|
170 | + if (empty($address['country']) && $with_default) |
|
171 | 171 | $address['country'] = wpinv_get_default_country(); |
172 | 172 | |
173 | 173 | |
@@ -181,16 +181,16 @@ discard block |
||
181 | 181 | * @param string $return What to return. |
182 | 182 | * @return array |
183 | 183 | */ |
184 | -function wpinv_get_continents( $return = 'all' ) { |
|
184 | +function wpinv_get_continents($return = 'all') { |
|
185 | 185 | |
186 | - $continents = wpinv_get_data( 'continents' ); |
|
186 | + $continents = wpinv_get_data('continents'); |
|
187 | 187 | |
188 | - switch( $return ) { |
|
188 | + switch ($return) { |
|
189 | 189 | case 'name' : |
190 | - return wp_list_pluck( $continents, 'name' ); |
|
190 | + return wp_list_pluck($continents, 'name'); |
|
191 | 191 | break; |
192 | 192 | case 'countries' : |
193 | - return wp_list_pluck( $continents, 'countries' ); |
|
193 | + return wp_list_pluck($continents, 'countries'); |
|
194 | 194 | break; |
195 | 195 | default : |
196 | 196 | return $continents; |
@@ -206,12 +206,12 @@ discard block |
||
206 | 206 | * @param string $country Country code. If no code is specified, defaults to the default country. |
207 | 207 | * @return string |
208 | 208 | */ |
209 | -function wpinv_get_continent_code_for_country( $country = false ) { |
|
209 | +function wpinv_get_continent_code_for_country($country = false) { |
|
210 | 210 | |
211 | - $country = wpinv_sanitize_country( $country ); |
|
211 | + $country = wpinv_sanitize_country($country); |
|
212 | 212 | |
213 | - foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) { |
|
214 | - if ( false !== array_search( $country, $countries, true ) ) { |
|
213 | + foreach (wpinv_get_continents('countries') as $continent_code => $countries) { |
|
214 | + if (false !== array_search($country, $countries, true)) { |
|
215 | 215 | return $continent_code; |
216 | 216 | } |
217 | 217 | } |
@@ -227,13 +227,13 @@ discard block |
||
227 | 227 | * @param string $country Country code. If no code is specified, defaults to the default country. |
228 | 228 | * @return array |
229 | 229 | */ |
230 | -function wpinv_get_country_calling_code( $country = null) { |
|
230 | +function wpinv_get_country_calling_code($country = null) { |
|
231 | 231 | |
232 | - $country = wpinv_sanitize_country( $country ); |
|
233 | - $codes = wpinv_get_data( 'phone-codes' ); |
|
234 | - $code = isset( $codes[ $country ] ) ? $codes[ $country ] : ''; |
|
232 | + $country = wpinv_sanitize_country($country); |
|
233 | + $codes = wpinv_get_data('phone-codes'); |
|
234 | + $code = isset($codes[$country]) ? $codes[$country] : ''; |
|
235 | 235 | |
236 | - if ( is_array( $code ) ) { |
|
236 | + if (is_array($code)) { |
|
237 | 237 | return $code[0]; |
238 | 238 | } |
239 | 239 | return $code; |
@@ -246,8 +246,8 @@ discard block |
||
246 | 246 | * @param bool $first_empty Whether or not the first item in the list should be empty |
247 | 247 | * @return array |
248 | 248 | */ |
249 | -function wpinv_get_country_list( $first_empty = false ) { |
|
250 | - return wpinv_maybe_add_empty_option( apply_filters( 'wpinv_countries', wpinv_get_data( 'countries' ) ), $first_empty ); |
|
249 | +function wpinv_get_country_list($first_empty = false) { |
|
250 | + return wpinv_maybe_add_empty_option(apply_filters('wpinv_countries', wpinv_get_data('countries')), $first_empty); |
|
251 | 251 | } |
252 | 252 | |
253 | 253 | /** |
@@ -257,22 +257,22 @@ discard block |
||
257 | 257 | * @param bool $first_empty Whether or not the first item in the list should be empty |
258 | 258 | * @return array |
259 | 259 | */ |
260 | -function wpinv_get_country_states( $country = null, $first_empty = false ) { |
|
260 | +function wpinv_get_country_states($country = null, $first_empty = false) { |
|
261 | 261 | |
262 | 262 | // Prepare the country. |
263 | - $country = wpinv_sanitize_country( $country ); |
|
263 | + $country = wpinv_sanitize_country($country); |
|
264 | 264 | |
265 | 265 | // Fetch all states. |
266 | - $all_states = wpinv_get_data( 'states' ); |
|
266 | + $all_states = wpinv_get_data('states'); |
|
267 | 267 | |
268 | 268 | // Fetch the specified country's states. |
269 | - $states = isset( $all_states[ $country ] ) ? $all_states[ $country ] : array() ; |
|
270 | - $states = apply_filters( "wpinv_{$country}_states", $states ); |
|
271 | - $states = apply_filters( 'wpinv_country_states', $states, $country ); |
|
269 | + $states = isset($all_states[$country]) ? $all_states[$country] : array(); |
|
270 | + $states = apply_filters("wpinv_{$country}_states", $states); |
|
271 | + $states = apply_filters('wpinv_country_states', $states, $country); |
|
272 | 272 | |
273 | - asort( $states ); |
|
273 | + asort($states); |
|
274 | 274 | |
275 | - return wpinv_maybe_add_empty_option( $states, $first_empty ); |
|
275 | + return wpinv_maybe_add_empty_option($states, $first_empty); |
|
276 | 276 | } |
277 | 277 | |
278 | 278 | /** |
@@ -282,7 +282,7 @@ discard block |
||
282 | 282 | * @return array |
283 | 283 | */ |
284 | 284 | function wpinv_get_us_states_list() { |
285 | - return apply_filters( 'wpinv_usa_states', wpinv_get_country_states( 'US' ) ); |
|
285 | + return apply_filters('wpinv_usa_states', wpinv_get_country_states('US')); |
|
286 | 286 | } |
287 | 287 | |
288 | 288 | /** |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | * @return array |
293 | 293 | */ |
294 | 294 | function wpinv_get_canada_states_list() { |
295 | - return apply_filters( 'wpinv_canada_provinces', wpinv_get_country_states( 'CA' ) ); |
|
295 | + return apply_filters('wpinv_canada_provinces', wpinv_get_country_states('CA')); |
|
296 | 296 | } |
297 | 297 | |
298 | 298 | /** |
@@ -302,7 +302,7 @@ discard block |
||
302 | 302 | * @return array |
303 | 303 | */ |
304 | 304 | function wpinv_get_australia_states_list() { |
305 | - return apply_filters( 'wpinv_australia_states', wpinv_get_country_states( 'AU' ) ); |
|
305 | + return apply_filters('wpinv_australia_states', wpinv_get_country_states('AU')); |
|
306 | 306 | } |
307 | 307 | |
308 | 308 | /** |
@@ -312,7 +312,7 @@ discard block |
||
312 | 312 | * @return array |
313 | 313 | */ |
314 | 314 | function wpinv_get_bangladesh_states_list() { |
315 | - return apply_filters( 'wpinv_bangladesh_states', wpinv_get_country_states( 'BD' ) ); |
|
315 | + return apply_filters('wpinv_bangladesh_states', wpinv_get_country_states('BD')); |
|
316 | 316 | } |
317 | 317 | |
318 | 318 | /** |
@@ -322,7 +322,7 @@ discard block |
||
322 | 322 | * @return array |
323 | 323 | */ |
324 | 324 | function wpinv_get_brazil_states_list() { |
325 | - return apply_filters( 'wpinv_brazil_states', wpinv_get_country_states( 'BR' ) ); |
|
325 | + return apply_filters('wpinv_brazil_states', wpinv_get_country_states('BR')); |
|
326 | 326 | } |
327 | 327 | |
328 | 328 | /** |
@@ -332,7 +332,7 @@ discard block |
||
332 | 332 | * @return array |
333 | 333 | */ |
334 | 334 | function wpinv_get_bulgaria_states_list() { |
335 | - return apply_filters( 'wpinv_bulgaria_states', wpinv_get_country_states( 'BG' ) ); |
|
335 | + return apply_filters('wpinv_bulgaria_states', wpinv_get_country_states('BG')); |
|
336 | 336 | } |
337 | 337 | |
338 | 338 | /** |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | * @return array |
343 | 343 | */ |
344 | 344 | function wpinv_get_hong_kong_states_list() { |
345 | - return apply_filters( 'wpinv_hong_kong_states', wpinv_get_country_states( 'HK' ) ); |
|
345 | + return apply_filters('wpinv_hong_kong_states', wpinv_get_country_states('HK')); |
|
346 | 346 | } |
347 | 347 | |
348 | 348 | /** |
@@ -352,7 +352,7 @@ discard block |
||
352 | 352 | * @return array |
353 | 353 | */ |
354 | 354 | function wpinv_get_hungary_states_list() { |
355 | - return apply_filters( 'wpinv_hungary_states', wpinv_get_country_states( 'HU' ) ); |
|
355 | + return apply_filters('wpinv_hungary_states', wpinv_get_country_states('HU')); |
|
356 | 356 | } |
357 | 357 | |
358 | 358 | /** |
@@ -362,7 +362,7 @@ discard block |
||
362 | 362 | * @return array |
363 | 363 | */ |
364 | 364 | function wpinv_get_japan_states_list() { |
365 | - return apply_filters( 'wpinv_japan_states', wpinv_get_country_states( 'JP' ) ); |
|
365 | + return apply_filters('wpinv_japan_states', wpinv_get_country_states('JP')); |
|
366 | 366 | } |
367 | 367 | |
368 | 368 | /** |
@@ -372,7 +372,7 @@ discard block |
||
372 | 372 | * @return array |
373 | 373 | */ |
374 | 374 | function wpinv_get_china_states_list() { |
375 | - return apply_filters( 'wpinv_china_states', wpinv_get_country_states( 'CN' ) ); |
|
375 | + return apply_filters('wpinv_china_states', wpinv_get_country_states('CN')); |
|
376 | 376 | } |
377 | 377 | |
378 | 378 | /** |
@@ -382,7 +382,7 @@ discard block |
||
382 | 382 | * @return array |
383 | 383 | */ |
384 | 384 | function wpinv_get_new_zealand_states_list() { |
385 | - return apply_filters( 'wpinv_new_zealand_states', wpinv_get_country_states( 'NZ' ) ); |
|
385 | + return apply_filters('wpinv_new_zealand_states', wpinv_get_country_states('NZ')); |
|
386 | 386 | } |
387 | 387 | |
388 | 388 | /** |
@@ -392,7 +392,7 @@ discard block |
||
392 | 392 | * @return array |
393 | 393 | */ |
394 | 394 | function wpinv_get_peru_states_list() { |
395 | - return apply_filters( 'wpinv_peru_states', wpinv_get_country_states( 'PE' ) ); |
|
395 | + return apply_filters('wpinv_peru_states', wpinv_get_country_states('PE')); |
|
396 | 396 | } |
397 | 397 | |
398 | 398 | /** |
@@ -402,7 +402,7 @@ discard block |
||
402 | 402 | * @return array |
403 | 403 | */ |
404 | 404 | function wpinv_get_indonesia_states_list() { |
405 | - return apply_filters( 'wpinv_indonesia_states', wpinv_get_country_states( 'ID' ) ); |
|
405 | + return apply_filters('wpinv_indonesia_states', wpinv_get_country_states('ID')); |
|
406 | 406 | } |
407 | 407 | |
408 | 408 | /** |
@@ -412,7 +412,7 @@ discard block |
||
412 | 412 | * @return array |
413 | 413 | */ |
414 | 414 | function wpinv_get_india_states_list() { |
415 | - return apply_filters( 'wpinv_india_states', wpinv_get_country_states( 'IN' ) ); |
|
415 | + return apply_filters('wpinv_india_states', wpinv_get_country_states('IN')); |
|
416 | 416 | } |
417 | 417 | |
418 | 418 | /** |
@@ -422,7 +422,7 @@ discard block |
||
422 | 422 | * @return array |
423 | 423 | */ |
424 | 424 | function wpinv_get_iran_states_list() { |
425 | - return apply_filters( 'wpinv_iran_states', wpinv_get_country_states( 'IR' ) ); |
|
425 | + return apply_filters('wpinv_iran_states', wpinv_get_country_states('IR')); |
|
426 | 426 | } |
427 | 427 | |
428 | 428 | /** |
@@ -432,7 +432,7 @@ discard block |
||
432 | 432 | * @return array |
433 | 433 | */ |
434 | 434 | function wpinv_get_italy_states_list() { |
435 | - return apply_filters( 'wpinv_italy_states', wpinv_get_country_states( 'IT' ) ); |
|
435 | + return apply_filters('wpinv_italy_states', wpinv_get_country_states('IT')); |
|
436 | 436 | } |
437 | 437 | |
438 | 438 | /** |
@@ -442,7 +442,7 @@ discard block |
||
442 | 442 | * @return array |
443 | 443 | */ |
444 | 444 | function wpinv_get_malaysia_states_list() { |
445 | - return apply_filters( 'wpinv_malaysia_states', wpinv_get_country_states( 'MY' ) ); |
|
445 | + return apply_filters('wpinv_malaysia_states', wpinv_get_country_states('MY')); |
|
446 | 446 | } |
447 | 447 | |
448 | 448 | /** |
@@ -452,7 +452,7 @@ discard block |
||
452 | 452 | * @return array |
453 | 453 | */ |
454 | 454 | function wpinv_get_mexico_states_list() { |
455 | - return apply_filters( 'wpinv_mexico_states', wpinv_get_country_states( 'MX' ) ); |
|
455 | + return apply_filters('wpinv_mexico_states', wpinv_get_country_states('MX')); |
|
456 | 456 | } |
457 | 457 | |
458 | 458 | /** |
@@ -462,7 +462,7 @@ discard block |
||
462 | 462 | * @return array |
463 | 463 | */ |
464 | 464 | function wpinv_get_nepal_states_list() { |
465 | - return apply_filters( 'wpinv_nepal_states', wpinv_get_country_states( 'NP' ) ); |
|
465 | + return apply_filters('wpinv_nepal_states', wpinv_get_country_states('NP')); |
|
466 | 466 | } |
467 | 467 | |
468 | 468 | /** |
@@ -472,7 +472,7 @@ discard block |
||
472 | 472 | * @return array |
473 | 473 | */ |
474 | 474 | function wpinv_get_south_africa_states_list() { |
475 | - return apply_filters( 'wpinv_south_africa_states', wpinv_get_country_states( 'ZA' ) ); |
|
475 | + return apply_filters('wpinv_south_africa_states', wpinv_get_country_states('ZA')); |
|
476 | 476 | } |
477 | 477 | |
478 | 478 | /** |
@@ -482,7 +482,7 @@ discard block |
||
482 | 482 | * @return array |
483 | 483 | */ |
484 | 484 | function wpinv_get_thailand_states_list() { |
485 | - return apply_filters( 'wpinv_thailand_states', wpinv_get_country_states( 'TH' ) ); |
|
485 | + return apply_filters('wpinv_thailand_states', wpinv_get_country_states('TH')); |
|
486 | 486 | } |
487 | 487 | |
488 | 488 | /** |
@@ -492,7 +492,7 @@ discard block |
||
492 | 492 | * @return array |
493 | 493 | */ |
494 | 494 | function wpinv_get_turkey_states_list() { |
495 | - return apply_filters( 'wpinv_turkey_states', wpinv_get_country_states( 'TR' ) ); |
|
495 | + return apply_filters('wpinv_turkey_states', wpinv_get_country_states('TR')); |
|
496 | 496 | } |
497 | 497 | |
498 | 498 | /** |
@@ -502,28 +502,28 @@ discard block |
||
502 | 502 | * @return array |
503 | 503 | */ |
504 | 504 | function wpinv_get_spain_states_list() { |
505 | - return apply_filters( 'wpinv_spain_states', wpinv_get_country_states( 'ES' ) ); |
|
505 | + return apply_filters('wpinv_spain_states', wpinv_get_country_states('ES')); |
|
506 | 506 | } |
507 | 507 | |
508 | 508 | function wpinv_get_states_field() { |
509 | - if( empty( $_POST['country'] ) ) { |
|
509 | + if (empty($_POST['country'])) { |
|
510 | 510 | $_POST['country'] = wpinv_get_default_country(); |
511 | 511 | } |
512 | - $states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) ); |
|
512 | + $states = wpinv_get_country_states(sanitize_text_field($_POST['country'])); |
|
513 | 513 | |
514 | - if( !empty( $states ) ) { |
|
515 | - $sanitized_field_name = sanitize_text_field( $_POST['field_name'] ); |
|
514 | + if (!empty($states)) { |
|
515 | + $sanitized_field_name = sanitize_text_field($_POST['field_name']); |
|
516 | 516 | |
517 | 517 | $args = array( |
518 | 518 | 'name' => $sanitized_field_name, |
519 | 519 | 'id' => $sanitized_field_name, |
520 | 520 | 'class' => $sanitized_field_name . 'custom-select wpinv-select wpi_select2', |
521 | - 'options' => array_merge( array( '' => '' ), $states ), |
|
521 | + 'options' => array_merge(array('' => ''), $states), |
|
522 | 522 | 'show_option_all' => false, |
523 | 523 | 'show_option_none' => false |
524 | 524 | ); |
525 | 525 | |
526 | - $response = wpinv_html_select( $args ); |
|
526 | + $response = wpinv_html_select($args); |
|
527 | 527 | |
528 | 528 | } else { |
529 | 529 | $response = 'nostates'; |
@@ -532,10 +532,10 @@ discard block |
||
532 | 532 | return $response; |
533 | 533 | } |
534 | 534 | |
535 | -function wpinv_default_billing_country( $country = '', $user_id = 0 ) { |
|
536 | - $country = !empty( $country ) ? $country : wpinv_get_default_country(); |
|
535 | +function wpinv_default_billing_country($country = '', $user_id = 0) { |
|
536 | + $country = !empty($country) ? $country : wpinv_get_default_country(); |
|
537 | 537 | |
538 | - return apply_filters( 'wpinv_default_billing_country', $country, $user_id ); |
|
538 | + return apply_filters('wpinv_default_billing_country', $country, $user_id); |
|
539 | 539 | } |
540 | 540 | |
541 | 541 | /** |
@@ -547,7 +547,7 @@ discard block |
||
547 | 547 | */ |
548 | 548 | function wpinv_get_address_formats() { |
549 | 549 | |
550 | - return apply_filters( 'wpinv_localisation_address_formats', |
|
550 | + return apply_filters('wpinv_localisation_address_formats', |
|
551 | 551 | array( |
552 | 552 | 'default' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}\n{{zip}}\n{{country}}", |
553 | 553 | 'AU' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}} {{zip}}\n{{country}}", |
@@ -596,9 +596,9 @@ discard block |
||
596 | 596 | * @see `wpinv_get_invoice_address_replacements` |
597 | 597 | * @return string |
598 | 598 | */ |
599 | -function wpinv_get_full_address_format( $country = false) { |
|
599 | +function wpinv_get_full_address_format($country = false) { |
|
600 | 600 | |
601 | - if( empty( $country ) ) { |
|
601 | + if (empty($country)) { |
|
602 | 602 | $country = wpinv_get_default_country(); |
603 | 603 | } |
604 | 604 | |
@@ -606,7 +606,7 @@ discard block |
||
606 | 606 | $formats = wpinv_get_address_formats(); |
607 | 607 | |
608 | 608 | // Get format for the specified country. |
609 | - $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default']; |
|
609 | + $format = ($country && isset($formats[$country])) ? $formats[$country] : $formats['default']; |
|
610 | 610 | |
611 | 611 | /** |
612 | 612 | * Filters the address format to use on Invoices. |
@@ -618,7 +618,7 @@ discard block |
||
618 | 618 | * @param string $format The address format to use. |
619 | 619 | * @param string $country The country who's address format is being retrieved. |
620 | 620 | */ |
621 | - return apply_filters( 'wpinv_get_full_address_format', $format, $country ); |
|
621 | + return apply_filters('wpinv_get_full_address_format', $format, $country); |
|
622 | 622 | } |
623 | 623 | |
624 | 624 | /** |
@@ -629,7 +629,7 @@ discard block |
||
629 | 629 | * @param array $billing_details customer's billing details |
630 | 630 | * @return array |
631 | 631 | */ |
632 | -function wpinv_get_invoice_address_replacements( $billing_details ) { |
|
632 | +function wpinv_get_invoice_address_replacements($billing_details) { |
|
633 | 633 | |
634 | 634 | $default_args = array( |
635 | 635 | 'address' => '', |
@@ -642,22 +642,22 @@ discard block |
||
642 | 642 | 'company' => '', |
643 | 643 | ); |
644 | 644 | |
645 | - $args = map_deep( wp_parse_args( $billing_details, $default_args ), 'trim' ); |
|
645 | + $args = map_deep(wp_parse_args($billing_details, $default_args), 'trim'); |
|
646 | 646 | $state = $args['state']; |
647 | 647 | $country = $args['country']; |
648 | 648 | |
649 | 649 | // Handle full country name. |
650 | - $full_country = empty( $country ) ? $country : wpinv_country_name( $country ); |
|
650 | + $full_country = empty($country) ? $country : wpinv_country_name($country); |
|
651 | 651 | |
652 | 652 | // Handle full state name. |
653 | - $full_state = ( $country && $state ) ? wpinv_state_name( $state, $country ) : $state; |
|
653 | + $full_state = ($country && $state) ? wpinv_state_name($state, $country) : $state; |
|
654 | 654 | |
655 | 655 | $args['postcode'] = $args['zip']; |
656 | 656 | $args['name'] = $args['first_name'] . ' ' . $args['last_name']; |
657 | 657 | $args['state'] = $full_state; |
658 | 658 | $args['state_code'] = $state; |
659 | 659 | $args['country'] = $full_country; |
660 | - $args['country_code']= $country; |
|
660 | + $args['country_code'] = $country; |
|
661 | 661 | |
662 | 662 | /** |
663 | 663 | * Filters the address format replacements to use on Invoices. |
@@ -668,14 +668,14 @@ discard block |
||
668 | 668 | * @param array $replacements The address replacements to use. |
669 | 669 | * @param array $billing_details The billing details to use. |
670 | 670 | */ |
671 | - $replacements = apply_filters( 'wpinv_get_invoice_address_replacements', $args, $billing_details ); |
|
671 | + $replacements = apply_filters('wpinv_get_invoice_address_replacements', $args, $billing_details); |
|
672 | 672 | |
673 | 673 | $return = array(); |
674 | 674 | |
675 | - foreach( $replacements as $key => $value ) { |
|
676 | - $value = is_scalar( $value ) ? trim( sanitize_text_field( $value ) ) : ''; |
|
675 | + foreach ($replacements as $key => $value) { |
|
676 | + $value = is_scalar($value) ? trim(sanitize_text_field($value)) : ''; |
|
677 | 677 | $return['{{' . $key . '}}'] = $value; |
678 | - $return['{{' . $key . '_upper}}'] = wpinv_utf8_strtoupper( $value ); |
|
678 | + $return['{{' . $key . '_upper}}'] = wpinv_utf8_strtoupper($value); |
|
679 | 679 | } |
680 | 680 | |
681 | 681 | return $return; |
@@ -689,6 +689,6 @@ discard block |
||
689 | 689 | * @since 1.0.14 |
690 | 690 | * @return string |
691 | 691 | */ |
692 | -function wpinv_trim_formatted_address_line( $line ) { |
|
693 | - return trim( $line, ', ' ); |
|
692 | +function wpinv_trim_formatted_address_line($line) { |
|
693 | + return trim($line, ', '); |
|
694 | 694 | } |
695 | 695 | \ No newline at end of file |
@@ -1,7 +1,8 @@ |
||
1 | 1 | <?php |
2 | 2 | // don't load directly |
3 | -if ( !defined('ABSPATH') ) |
|
3 | +if ( !defined('ABSPATH') ) { |
|
4 | 4 | die('-1'); |
5 | +} |
|
5 | 6 | |
6 | 7 | do_action( 'wpinv_email_header', $email_heading, $invoice, $email_type, $sent_to_admin ); |
7 | 8 |
@@ -1,22 +1,22 @@ |
||
1 | 1 | <?php |
2 | 2 | // don't load directly |
3 | -if ( !defined('ABSPATH') ) |
|
3 | +if (!defined('ABSPATH')) |
|
4 | 4 | die('-1'); |
5 | 5 | |
6 | -do_action( 'wpinv_email_header', $email_heading, $invoice, $email_type, $sent_to_admin ); |
|
6 | +do_action('wpinv_email_header', $email_heading, $invoice, $email_type, $sent_to_admin); |
|
7 | 7 | |
8 | -do_action( 'wpinv_email_before_note_details', $invoice, $email_type, $sent_to_admin, $customer_note ); |
|
8 | +do_action('wpinv_email_before_note_details', $invoice, $email_type, $sent_to_admin, $customer_note); |
|
9 | 9 | |
10 | -if ( ! empty( $message_body ) ) { |
|
11 | - echo wpautop( wptexturize( $message_body ) ); |
|
10 | +if (!empty($message_body)) { |
|
11 | + echo wpautop(wptexturize($message_body)); |
|
12 | 12 | } |
13 | 13 | |
14 | -do_action( 'wpinv_email_invoice_details', $invoice, $email_type, $sent_to_admin ); |
|
14 | +do_action('wpinv_email_invoice_details', $invoice, $email_type, $sent_to_admin); |
|
15 | 15 | |
16 | -do_action( 'wpinv_email_invoice_items', $invoice, $email_type, $sent_to_admin ); |
|
16 | +do_action('wpinv_email_invoice_items', $invoice, $email_type, $sent_to_admin); |
|
17 | 17 | |
18 | -do_action( 'wpinv_email_billing_details', $invoice, $email_type, $sent_to_admin ); |
|
18 | +do_action('wpinv_email_billing_details', $invoice, $email_type, $sent_to_admin); |
|
19 | 19 | |
20 | -do_action( 'wpinv_email_after_note_details', $invoice, $email_type, $sent_to_admin, $customer_note ); |
|
20 | +do_action('wpinv_email_after_note_details', $invoice, $email_type, $sent_to_admin, $customer_note); |
|
21 | 21 | |
22 | -do_action( 'wpinv_email_footer', $invoice, $email_type, $sent_to_admin ); |
|
23 | 22 | \ No newline at end of file |
23 | +do_action('wpinv_email_footer', $invoice, $email_type, $sent_to_admin); |
|
24 | 24 | \ No newline at end of file |