@@ -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 ) ) { |
|
| 183 | + if ($discounts) { |
|
| 184 | + foreach ($discounts as $discount) { |
|
| 185 | + if (wpinv_is_discount_active($discount->ID)) { |
|
| 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|WP_Post |
| 224 | 224 | */ |
| 225 | -function wpinv_get_discount_by_code( $code = '' ) { |
|
| 226 | - return wpinv_get_discount_by( 'code', $code ); |
|
| 225 | +function wpinv_get_discount_by_code($code = '') { |
|
| 226 | + return wpinv_get_discount_by('code', $code); |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | /** |
@@ -233,13 +233,13 @@ discard block |
||
| 233 | 233 | * @param string|int $value The field value |
| 234 | 234 | * @return bool|WP_Post |
| 235 | 235 | */ |
| 236 | -function wpinv_get_discount_by( $field = '', $value = '' ) { |
|
| 237 | - $data = WPInv_Discount::get_data_by( $field, $value ); |
|
| 238 | - if( empty( $data ) ) { |
|
| 236 | +function wpinv_get_discount_by($field = '', $value = '') { |
|
| 237 | + $data = WPInv_Discount::get_data_by($field, $value); |
|
| 238 | + if (empty($data)) { |
|
| 239 | 239 | return false; |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | - return get_post( $data['ID'] ); |
|
| 242 | + return get_post($data['ID']); |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | /** |
@@ -249,68 +249,68 @@ discard block |
||
| 249 | 249 | * @param array $data The discount's properties. |
| 250 | 250 | * @return bool |
| 251 | 251 | */ |
| 252 | -function wpinv_store_discount( $post_id, $data, $post, $update = false ) { |
|
| 252 | +function wpinv_store_discount($post_id, $data, $post, $update = false) { |
|
| 253 | 253 | $meta = array( |
| 254 | - 'code' => isset( $data['code'] ) ? sanitize_text_field( $data['code'] ) : '', |
|
| 255 | - 'type' => isset( $data['type'] ) ? sanitize_text_field( $data['type'] ) : 'percent', |
|
| 256 | - 'amount' => isset( $data['amount'] ) ? wpinv_sanitize_amount( $data['amount'] ) : '', |
|
| 257 | - 'start' => isset( $data['start'] ) ? sanitize_text_field( $data['start'] ) : '', |
|
| 258 | - 'expiration' => isset( $data['expiration'] ) ? sanitize_text_field( $data['expiration'] ) : '', |
|
| 259 | - 'min_total' => isset( $data['min_total'] ) ? wpinv_sanitize_amount( $data['min_total'] ) : '', |
|
| 260 | - 'max_total' => isset( $data['max_total'] ) ? wpinv_sanitize_amount( $data['max_total'] ) : '', |
|
| 261 | - 'max_uses' => isset( $data['max_uses'] ) ? absint( $data['max_uses'] ) : '', |
|
| 262 | - 'items' => isset( $data['items'] ) ? $data['items'] : array(), |
|
| 263 | - 'excluded_items' => isset( $data['excluded_items'] ) ? $data['excluded_items'] : array(), |
|
| 264 | - 'is_recurring' => isset( $data['recurring'] ) ? (bool)$data['recurring'] : false, |
|
| 265 | - 'is_single_use' => isset( $data['single_use'] ) ? (bool)$data['single_use'] : false, |
|
| 266 | - 'uses' => isset( $data['uses'] ) ? (int)$data['uses'] : false, |
|
| 254 | + 'code' => isset($data['code']) ? sanitize_text_field($data['code']) : '', |
|
| 255 | + 'type' => isset($data['type']) ? sanitize_text_field($data['type']) : 'percent', |
|
| 256 | + 'amount' => isset($data['amount']) ? wpinv_sanitize_amount($data['amount']) : '', |
|
| 257 | + 'start' => isset($data['start']) ? sanitize_text_field($data['start']) : '', |
|
| 258 | + 'expiration' => isset($data['expiration']) ? sanitize_text_field($data['expiration']) : '', |
|
| 259 | + 'min_total' => isset($data['min_total']) ? wpinv_sanitize_amount($data['min_total']) : '', |
|
| 260 | + 'max_total' => isset($data['max_total']) ? wpinv_sanitize_amount($data['max_total']) : '', |
|
| 261 | + 'max_uses' => isset($data['max_uses']) ? absint($data['max_uses']) : '', |
|
| 262 | + 'items' => isset($data['items']) ? $data['items'] : array(), |
|
| 263 | + 'excluded_items' => isset($data['excluded_items']) ? $data['excluded_items'] : array(), |
|
| 264 | + 'is_recurring' => isset($data['recurring']) ? (bool) $data['recurring'] : false, |
|
| 265 | + 'is_single_use' => isset($data['single_use']) ? (bool) $data['single_use'] : false, |
|
| 266 | + 'uses' => isset($data['uses']) ? (int) $data['uses'] : false, |
|
| 267 | 267 | ); |
| 268 | 268 | |
| 269 | - if ( $meta['type'] == 'percent' && (float)$meta['amount'] > 100 ) { |
|
| 269 | + if ($meta['type'] == 'percent' && (float) $meta['amount'] > 100) { |
|
| 270 | 270 | $meta['amount'] = 100; |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | - if ( !empty( $meta['start'] ) ) { |
|
| 274 | - $meta['start'] = date_i18n( 'Y-m-d H:i:s', strtotime( $meta['start'] ) ); |
|
| 273 | + if (!empty($meta['start'])) { |
|
| 274 | + $meta['start'] = date_i18n('Y-m-d H:i:s', strtotime($meta['start'])); |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | - if ( !empty( $meta['expiration'] ) ) { |
|
| 278 | - $meta['expiration'] = date_i18n( 'Y-m-d H:i:s', strtotime( $meta['expiration'] ) ); |
|
| 277 | + if (!empty($meta['expiration'])) { |
|
| 278 | + $meta['expiration'] = date_i18n('Y-m-d H:i:s', strtotime($meta['expiration'])); |
|
| 279 | 279 | |
| 280 | - if ( !empty( $meta['start'] ) && strtotime( $meta['start'] ) > strtotime( $meta['expiration'] ) ) { |
|
| 280 | + if (!empty($meta['start']) && strtotime($meta['start']) > strtotime($meta['expiration'])) { |
|
| 281 | 281 | $meta['expiration'] = $meta['start']; |
| 282 | 282 | } |
| 283 | 283 | } |
| 284 | 284 | |
| 285 | - if ( $meta['uses'] === false ) { |
|
| 286 | - unset( $meta['uses'] ); |
|
| 285 | + if ($meta['uses'] === false) { |
|
| 286 | + unset($meta['uses']); |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | - if ( ! empty( $meta['items'] ) ) { |
|
| 290 | - foreach ( $meta['items'] as $key => $item ) { |
|
| 291 | - if ( 0 === intval( $item ) ) { |
|
| 292 | - unset( $meta['items'][ $key ] ); |
|
| 289 | + if (!empty($meta['items'])) { |
|
| 290 | + foreach ($meta['items'] as $key => $item) { |
|
| 291 | + if (0 === intval($item)) { |
|
| 292 | + unset($meta['items'][$key]); |
|
| 293 | 293 | } |
| 294 | 294 | } |
| 295 | 295 | } |
| 296 | 296 | |
| 297 | - if ( ! empty( $meta['excluded_items'] ) ) { |
|
| 298 | - foreach ( $meta['excluded_items'] as $key => $item ) { |
|
| 299 | - if ( 0 === intval( $item ) ) { |
|
| 300 | - unset( $meta['excluded_items'][ $key ] ); |
|
| 297 | + if (!empty($meta['excluded_items'])) { |
|
| 298 | + foreach ($meta['excluded_items'] as $key => $item) { |
|
| 299 | + if (0 === intval($item)) { |
|
| 300 | + unset($meta['excluded_items'][$key]); |
|
| 301 | 301 | } |
| 302 | 302 | } |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | - $meta = apply_filters( 'wpinv_update_discount', $meta, $post_id, $post ); |
|
| 305 | + $meta = apply_filters('wpinv_update_discount', $meta, $post_id, $post); |
|
| 306 | 306 | |
| 307 | - do_action( 'wpinv_pre_update_discount', $meta, $post_id, $post ); |
|
| 307 | + do_action('wpinv_pre_update_discount', $meta, $post_id, $post); |
|
| 308 | 308 | |
| 309 | - foreach( $meta as $key => $value ) { |
|
| 310 | - update_post_meta( $post_id, '_wpi_discount_' . $key, $value ); |
|
| 309 | + foreach ($meta as $key => $value) { |
|
| 310 | + update_post_meta($post_id, '_wpi_discount_' . $key, $value); |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | - do_action( 'wpinv_post_update_discount', $meta, $post_id, $post ); |
|
| 313 | + do_action('wpinv_post_update_discount', $meta, $post_id, $post); |
|
| 314 | 314 | |
| 315 | 315 | return $post_id; |
| 316 | 316 | } |
@@ -321,10 +321,10 @@ discard block |
||
| 321 | 321 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 322 | 322 | * @return bool |
| 323 | 323 | */ |
| 324 | -function wpinv_remove_discount( $discount = 0 ) { |
|
| 324 | +function wpinv_remove_discount($discount = 0) { |
|
| 325 | 325 | |
| 326 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 327 | - if( ! $discount->exists() ) { |
|
| 326 | + $discount = wpinv_get_discount_obj($discount); |
|
| 327 | + if (!$discount->exists()) { |
|
| 328 | 328 | return false; |
| 329 | 329 | } |
| 330 | 330 | |
@@ -339,9 +339,9 @@ discard block |
||
| 339 | 339 | * @param string $new_status |
| 340 | 340 | * @return bool |
| 341 | 341 | */ |
| 342 | -function wpinv_update_discount_status( $discount = 0, $new_status = 'publish' ) { |
|
| 343 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 344 | - return $discount->update_status( $new_status ); |
|
| 342 | +function wpinv_update_discount_status($discount = 0, $new_status = 'publish') { |
|
| 343 | + $discount = wpinv_get_discount_obj($discount); |
|
| 344 | + return $discount->update_status($new_status); |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | /** |
@@ -350,48 +350,48 @@ discard block |
||
| 350 | 350 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 351 | 351 | * @return bool |
| 352 | 352 | */ |
| 353 | -function wpinv_discount_exists( $discount ) { |
|
| 354 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 353 | +function wpinv_discount_exists($discount) { |
|
| 354 | + $discount = wpinv_get_discount_obj($discount); |
|
| 355 | 355 | return $discount->exists(); |
| 356 | 356 | } |
| 357 | 357 | |
| 358 | -function wpinv_is_discount_active( $code_id = null ) { |
|
| 359 | - $discount = wpinv_get_discount( $code_id ); |
|
| 358 | +function wpinv_is_discount_active($code_id = null) { |
|
| 359 | + $discount = wpinv_get_discount($code_id); |
|
| 360 | 360 | $return = false; |
| 361 | 361 | |
| 362 | - if ( $discount ) { |
|
| 363 | - if ( wpinv_is_discount_expired( $code_id ) ) { |
|
| 364 | - if( defined( 'DOING_AJAX' ) ) { |
|
| 365 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is expired.', 'invoicing' ) ); |
|
| 362 | + if ($discount) { |
|
| 363 | + if (wpinv_is_discount_expired($code_id)) { |
|
| 364 | + if (defined('DOING_AJAX')) { |
|
| 365 | + wpinv_set_error('wpinv-discount-error', __('This discount is expired.', 'invoicing')); |
|
| 366 | 366 | } |
| 367 | - } elseif ( $discount->post_status == 'publish' ) { |
|
| 367 | + } elseif ($discount->post_status == 'publish') { |
|
| 368 | 368 | $return = true; |
| 369 | 369 | } else { |
| 370 | - if( defined( 'DOING_AJAX' ) ) { |
|
| 371 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not active.', 'invoicing' ) ); |
|
| 370 | + if (defined('DOING_AJAX')) { |
|
| 371 | + wpinv_set_error('wpinv-discount-error', __('This discount is not active.', 'invoicing')); |
|
| 372 | 372 | } |
| 373 | 373 | } |
| 374 | 374 | } |
| 375 | 375 | |
| 376 | - return apply_filters( 'wpinv_is_discount_active', $return, $code_id ); |
|
| 376 | + return apply_filters('wpinv_is_discount_active', $return, $code_id); |
|
| 377 | 377 | } |
| 378 | 378 | |
| 379 | -function wpinv_get_discount_code( $code_id = null ) { |
|
| 380 | - $code = get_post_meta( $code_id, '_wpi_discount_code', true ); |
|
| 379 | +function wpinv_get_discount_code($code_id = null) { |
|
| 380 | + $code = get_post_meta($code_id, '_wpi_discount_code', true); |
|
| 381 | 381 | |
| 382 | - return apply_filters( 'wpinv_get_discount_code', $code, $code_id ); |
|
| 382 | + return apply_filters('wpinv_get_discount_code', $code, $code_id); |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | -function wpinv_get_discount_start_date( $code_id = null ) { |
|
| 386 | - $start_date = get_post_meta( $code_id, '_wpi_discount_start', true ); |
|
| 385 | +function wpinv_get_discount_start_date($code_id = null) { |
|
| 386 | + $start_date = get_post_meta($code_id, '_wpi_discount_start', true); |
|
| 387 | 387 | |
| 388 | - return apply_filters( 'wpinv_get_discount_start_date', $start_date, $code_id ); |
|
| 388 | + return apply_filters('wpinv_get_discount_start_date', $start_date, $code_id); |
|
| 389 | 389 | } |
| 390 | 390 | |
| 391 | -function wpinv_get_discount_expiration( $code_id = null ) { |
|
| 392 | - $expiration = get_post_meta( $code_id, '_wpi_discount_expiration', true ); |
|
| 391 | +function wpinv_get_discount_expiration($code_id = null) { |
|
| 392 | + $expiration = get_post_meta($code_id, '_wpi_discount_expiration', true); |
|
| 393 | 393 | |
| 394 | - return apply_filters( 'wpinv_get_discount_expiration', $expiration, $code_id ); |
|
| 394 | + return apply_filters('wpinv_get_discount_expiration', $expiration, $code_id); |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | /** |
@@ -400,8 +400,8 @@ discard block |
||
| 400 | 400 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 401 | 401 | * @return int |
| 402 | 402 | */ |
| 403 | -function wpinv_get_discount_max_uses( $discount = array() ) { |
|
| 404 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 403 | +function wpinv_get_discount_max_uses($discount = array()) { |
|
| 404 | + $discount = wpinv_get_discount_obj($discount); |
|
| 405 | 405 | return (int) $discount->max_uses; |
| 406 | 406 | } |
| 407 | 407 | |
@@ -411,8 +411,8 @@ discard block |
||
| 411 | 411 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 412 | 412 | * @return int |
| 413 | 413 | */ |
| 414 | -function wpinv_get_discount_uses( $discount = array() ) { |
|
| 415 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 414 | +function wpinv_get_discount_uses($discount = array()) { |
|
| 415 | + $discount = wpinv_get_discount_obj($discount); |
|
| 416 | 416 | return (int) $discount->uses; |
| 417 | 417 | } |
| 418 | 418 | |
@@ -422,8 +422,8 @@ discard block |
||
| 422 | 422 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 423 | 423 | * @return float |
| 424 | 424 | */ |
| 425 | -function wpinv_get_discount_min_total( $discount = array() ) { |
|
| 426 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 425 | +function wpinv_get_discount_min_total($discount = array()) { |
|
| 426 | + $discount = wpinv_get_discount_obj($discount); |
|
| 427 | 427 | return (float) $discount->min_total; |
| 428 | 428 | } |
| 429 | 429 | |
@@ -433,8 +433,8 @@ discard block |
||
| 433 | 433 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 434 | 434 | * @return float |
| 435 | 435 | */ |
| 436 | -function wpinv_get_discount_max_total( $discount = array() ) { |
|
| 437 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 436 | +function wpinv_get_discount_max_total($discount = array()) { |
|
| 437 | + $discount = wpinv_get_discount_obj($discount); |
|
| 438 | 438 | return (float) $discount->max_total; |
| 439 | 439 | } |
| 440 | 440 | |
@@ -444,8 +444,8 @@ discard block |
||
| 444 | 444 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 445 | 445 | * @return float |
| 446 | 446 | */ |
| 447 | -function wpinv_get_discount_amount( $discount = array() ) { |
|
| 448 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 447 | +function wpinv_get_discount_amount($discount = array()) { |
|
| 448 | + $discount = wpinv_get_discount_obj($discount); |
|
| 449 | 449 | return (float) $discount->amount; |
| 450 | 450 | } |
| 451 | 451 | |
@@ -456,28 +456,28 @@ discard block |
||
| 456 | 456 | * @param bool $name |
| 457 | 457 | * @return string |
| 458 | 458 | */ |
| 459 | -function wpinv_get_discount_type( $discount = array(), $name = false ) { |
|
| 460 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 459 | +function wpinv_get_discount_type($discount = array(), $name = false) { |
|
| 460 | + $discount = wpinv_get_discount_obj($discount); |
|
| 461 | 461 | |
| 462 | 462 | // Are we returning the name or just the type. |
| 463 | - if( $name ) { |
|
| 463 | + if ($name) { |
|
| 464 | 464 | return $discount->type_name; |
| 465 | 465 | } |
| 466 | 466 | |
| 467 | 467 | return $discount->type; |
| 468 | 468 | } |
| 469 | 469 | |
| 470 | -function wpinv_discount_status( $status ) { |
|
| 471 | - switch( $status ){ |
|
| 470 | +function wpinv_discount_status($status) { |
|
| 471 | + switch ($status) { |
|
| 472 | 472 | case 'expired' : |
| 473 | - $name = __( 'Expired', 'invoicing' ); |
|
| 473 | + $name = __('Expired', 'invoicing'); |
|
| 474 | 474 | break; |
| 475 | 475 | case 'publish' : |
| 476 | 476 | case 'active' : |
| 477 | - $name = __( 'Active', 'invoicing' ); |
|
| 477 | + $name = __('Active', 'invoicing'); |
|
| 478 | 478 | break; |
| 479 | 479 | default : |
| 480 | - $name = __( 'Inactive', 'invoicing' ); |
|
| 480 | + $name = __('Inactive', 'invoicing'); |
|
| 481 | 481 | break; |
| 482 | 482 | } |
| 483 | 483 | return $name; |
@@ -489,8 +489,8 @@ discard block |
||
| 489 | 489 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 490 | 490 | * @return array |
| 491 | 491 | */ |
| 492 | -function wpinv_get_discount_excluded_items( $discount = array() ) { |
|
| 493 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 492 | +function wpinv_get_discount_excluded_items($discount = array()) { |
|
| 493 | + $discount = wpinv_get_discount_obj($discount); |
|
| 494 | 494 | return $discount->excluded_items; |
| 495 | 495 | } |
| 496 | 496 | |
@@ -500,17 +500,17 @@ discard block |
||
| 500 | 500 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 501 | 501 | * @return array |
| 502 | 502 | */ |
| 503 | -function wpinv_get_discount_item_reqs( $discount = array() ) { |
|
| 504 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 503 | +function wpinv_get_discount_item_reqs($discount = array()) { |
|
| 504 | + $discount = wpinv_get_discount_obj($discount); |
|
| 505 | 505 | return $discount->items; |
| 506 | 506 | } |
| 507 | 507 | |
| 508 | -function wpinv_get_discount_item_condition( $code_id = 0 ) { |
|
| 509 | - return get_post_meta( $code_id, '_wpi_discount_item_condition', true ); |
|
| 508 | +function wpinv_get_discount_item_condition($code_id = 0) { |
|
| 509 | + return get_post_meta($code_id, '_wpi_discount_item_condition', true); |
|
| 510 | 510 | } |
| 511 | 511 | |
| 512 | -function wpinv_is_discount_not_global( $code_id = 0 ) { |
|
| 513 | - return (bool) get_post_meta( $code_id, '_wpi_discount_is_not_global', true ); |
|
| 512 | +function wpinv_is_discount_not_global($code_id = 0) { |
|
| 513 | + return (bool) get_post_meta($code_id, '_wpi_discount_is_not_global', true); |
|
| 514 | 514 | } |
| 515 | 515 | |
| 516 | 516 | /** |
@@ -519,14 +519,14 @@ discard block |
||
| 519 | 519 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 520 | 520 | * @return bool |
| 521 | 521 | */ |
| 522 | -function wpinv_is_discount_expired( $discount = array() ) { |
|
| 523 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 522 | +function wpinv_is_discount_expired($discount = array()) { |
|
| 523 | + $discount = wpinv_get_discount_obj($discount); |
|
| 524 | 524 | |
| 525 | - if ( $discount->is_expired() ) { |
|
| 526 | - $discount->update_status( 'pending' ); |
|
| 525 | + if ($discount->is_expired()) { |
|
| 526 | + $discount->update_status('pending'); |
|
| 527 | 527 | |
| 528 | - if( empty( $started ) ) { |
|
| 529 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount has expired.', 'invoicing' ) ); |
|
| 528 | + if (empty($started)) { |
|
| 529 | + wpinv_set_error('wpinv-discount-error', __('This discount has expired.', 'invoicing')); |
|
| 530 | 530 | } |
| 531 | 531 | return true; |
| 532 | 532 | } |
@@ -540,12 +540,12 @@ discard block |
||
| 540 | 540 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 541 | 541 | * @return bool |
| 542 | 542 | */ |
| 543 | -function wpinv_is_discount_started( $discount = array() ) { |
|
| 544 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 543 | +function wpinv_is_discount_started($discount = array()) { |
|
| 544 | + $discount = wpinv_get_discount_obj($discount); |
|
| 545 | 545 | $started = $discount->has_started(); |
| 546 | 546 | |
| 547 | - if( empty( $started ) ) { |
|
| 548 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not active yet.', 'invoicing' ) ); |
|
| 547 | + if (empty($started)) { |
|
| 548 | + wpinv_set_error('wpinv-discount-error', __('This discount is not active yet.', 'invoicing')); |
|
| 549 | 549 | } |
| 550 | 550 | |
| 551 | 551 | return $started; |
@@ -557,10 +557,10 @@ discard block |
||
| 557 | 557 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 558 | 558 | * @return bool |
| 559 | 559 | */ |
| 560 | -function wpinv_check_discount_dates( $discount ) { |
|
| 561 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 562 | - $return = wpinv_is_discount_started( $discount ) && ! wpinv_is_discount_expired( $discount ); |
|
| 563 | - return apply_filters( 'wpinv_check_discount_dates', $return, $discount->ID, $discount, $discount->code ); |
|
| 560 | +function wpinv_check_discount_dates($discount) { |
|
| 561 | + $discount = wpinv_get_discount_obj($discount); |
|
| 562 | + $return = wpinv_is_discount_started($discount) && !wpinv_is_discount_expired($discount); |
|
| 563 | + return apply_filters('wpinv_check_discount_dates', $return, $discount->ID, $discount, $discount->code); |
|
| 564 | 564 | } |
| 565 | 565 | |
| 566 | 566 | /** |
@@ -569,12 +569,12 @@ discard block |
||
| 569 | 569 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 570 | 570 | * @return bool |
| 571 | 571 | */ |
| 572 | -function wpinv_is_discount_maxed_out( $discount ) { |
|
| 573 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 572 | +function wpinv_is_discount_maxed_out($discount) { |
|
| 573 | + $discount = wpinv_get_discount_obj($discount); |
|
| 574 | 574 | $maxed_out = $discount->has_exceeded_limit(); |
| 575 | 575 | |
| 576 | - if ( $maxed_out ) { |
|
| 577 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount has reached its maximum usage.', 'invoicing' ) ); |
|
| 576 | + if ($maxed_out) { |
|
| 577 | + wpinv_set_error('wpinv-discount-error', __('This discount has reached its maximum usage.', 'invoicing')); |
|
| 578 | 578 | } |
| 579 | 579 | |
| 580 | 580 | return $maxed_out; |
@@ -586,13 +586,13 @@ discard block |
||
| 586 | 586 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 587 | 587 | * @return bool |
| 588 | 588 | */ |
| 589 | -function wpinv_discount_is_min_met( $discount ) { |
|
| 590 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 591 | - $cart_amount = (float)wpinv_get_cart_discountable_subtotal( $discount->ID ); |
|
| 592 | - $min_met = $discount->is_minimum_amount_met( $cart_amount ); |
|
| 589 | +function wpinv_discount_is_min_met($discount) { |
|
| 590 | + $discount = wpinv_get_discount_obj($discount); |
|
| 591 | + $cart_amount = (float) wpinv_get_cart_discountable_subtotal($discount->ID); |
|
| 592 | + $min_met = $discount->is_minimum_amount_met($cart_amount); |
|
| 593 | 593 | |
| 594 | - if ( ! $min_met ) { |
|
| 595 | - wpinv_set_error( 'wpinv-discount-error', sprintf( __( 'Minimum invoice amount should be %s', 'invoicing' ), wpinv_price( wpinv_format_amount( $discount->min_total ) ) ) ); |
|
| 594 | + if (!$min_met) { |
|
| 595 | + wpinv_set_error('wpinv-discount-error', sprintf(__('Minimum invoice amount should be %s', 'invoicing'), wpinv_price(wpinv_format_amount($discount->min_total)))); |
|
| 596 | 596 | } |
| 597 | 597 | |
| 598 | 598 | return $min_met; |
@@ -604,13 +604,13 @@ discard block |
||
| 604 | 604 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 605 | 605 | * @return bool |
| 606 | 606 | */ |
| 607 | -function wpinv_discount_is_max_met( $discount ) { |
|
| 608 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 609 | - $cart_amount = (float)wpinv_get_cart_discountable_subtotal( $discount->ID ); |
|
| 610 | - $max_met = $discount->is_maximum_amount_met( $cart_amount ); |
|
| 607 | +function wpinv_discount_is_max_met($discount) { |
|
| 608 | + $discount = wpinv_get_discount_obj($discount); |
|
| 609 | + $cart_amount = (float) wpinv_get_cart_discountable_subtotal($discount->ID); |
|
| 610 | + $max_met = $discount->is_maximum_amount_met($cart_amount); |
|
| 611 | 611 | |
| 612 | - if ( ! $max_met ) { |
|
| 613 | - wpinv_set_error( 'wpinv-discount-error', sprintf( __( 'Maximum invoice amount should be %s', 'invoicing' ), wpinv_price( wpinv_format_amount( $discount->max_total ) ) ) ); |
|
| 612 | + if (!$max_met) { |
|
| 613 | + wpinv_set_error('wpinv-discount-error', sprintf(__('Maximum invoice amount should be %s', 'invoicing'), wpinv_price(wpinv_format_amount($discount->max_total)))); |
|
| 614 | 614 | } |
| 615 | 615 | |
| 616 | 616 | return $max_met; |
@@ -622,8 +622,8 @@ discard block |
||
| 622 | 622 | * @param int|array|string|WPInv_Discount $discount discount data, object, ID or code. |
| 623 | 623 | * @return bool |
| 624 | 624 | */ |
| 625 | -function wpinv_discount_is_single_use( $discount ) { |
|
| 626 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 625 | +function wpinv_discount_is_single_use($discount) { |
|
| 626 | + $discount = wpinv_get_discount_obj($discount); |
|
| 627 | 627 | return $discount->is_single_use; |
| 628 | 628 | } |
| 629 | 629 | |
@@ -634,53 +634,53 @@ discard block |
||
| 634 | 634 | * @param int|array|string|WPInv_Discount $code discount data, object, ID or code. |
| 635 | 635 | * @return bool |
| 636 | 636 | */ |
| 637 | -function wpinv_discount_is_recurring( $discount = 0, $code = 0 ) { |
|
| 637 | +function wpinv_discount_is_recurring($discount = 0, $code = 0) { |
|
| 638 | 638 | |
| 639 | - if( ! empty( $discount ) ) { |
|
| 640 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 639 | + if (!empty($discount)) { |
|
| 640 | + $discount = wpinv_get_discount_obj($discount); |
|
| 641 | 641 | } else { |
| 642 | - $discount = wpinv_get_discount_obj( $code ); |
|
| 642 | + $discount = wpinv_get_discount_obj($code); |
|
| 643 | 643 | } |
| 644 | 644 | |
| 645 | 645 | return $discount->is_recurring; |
| 646 | 646 | } |
| 647 | 647 | |
| 648 | -function wpinv_discount_item_reqs_met( $code_id = null ) { |
|
| 649 | - $item_reqs = wpinv_get_discount_item_reqs( $code_id ); |
|
| 650 | - $condition = wpinv_get_discount_item_condition( $code_id ); |
|
| 651 | - $excluded_ps = wpinv_get_discount_excluded_items( $code_id ); |
|
| 648 | +function wpinv_discount_item_reqs_met($code_id = null) { |
|
| 649 | + $item_reqs = wpinv_get_discount_item_reqs($code_id); |
|
| 650 | + $condition = wpinv_get_discount_item_condition($code_id); |
|
| 651 | + $excluded_ps = wpinv_get_discount_excluded_items($code_id); |
|
| 652 | 652 | $cart_items = wpinv_get_cart_contents(); |
| 653 | - $cart_ids = $cart_items ? wp_list_pluck( $cart_items, 'id' ) : null; |
|
| 653 | + $cart_ids = $cart_items ? wp_list_pluck($cart_items, 'id') : null; |
|
| 654 | 654 | $ret = false; |
| 655 | 655 | |
| 656 | - if ( empty( $item_reqs ) && empty( $excluded_ps ) ) { |
|
| 656 | + if (empty($item_reqs) && empty($excluded_ps)) { |
|
| 657 | 657 | $ret = true; |
| 658 | 658 | } |
| 659 | 659 | |
| 660 | 660 | // Normalize our data for item requirements, exclusions and cart data |
| 661 | 661 | // First absint the items, then sort, and reset the array keys |
| 662 | - $item_reqs = array_map( 'absint', $item_reqs ); |
|
| 663 | - asort( $item_reqs ); |
|
| 664 | - $item_reqs = array_values( $item_reqs ); |
|
| 662 | + $item_reqs = array_map('absint', $item_reqs); |
|
| 663 | + asort($item_reqs); |
|
| 664 | + $item_reqs = array_values($item_reqs); |
|
| 665 | 665 | |
| 666 | - $excluded_ps = array_map( 'absint', $excluded_ps ); |
|
| 667 | - asort( $excluded_ps ); |
|
| 668 | - $excluded_ps = array_values( $excluded_ps ); |
|
| 666 | + $excluded_ps = array_map('absint', $excluded_ps); |
|
| 667 | + asort($excluded_ps); |
|
| 668 | + $excluded_ps = array_values($excluded_ps); |
|
| 669 | 669 | |
| 670 | - $cart_ids = array_map( 'absint', $cart_ids ); |
|
| 671 | - asort( $cart_ids ); |
|
| 672 | - $cart_ids = array_values( $cart_ids ); |
|
| 670 | + $cart_ids = array_map('absint', $cart_ids); |
|
| 671 | + asort($cart_ids); |
|
| 672 | + $cart_ids = array_values($cart_ids); |
|
| 673 | 673 | |
| 674 | 674 | // Ensure we have requirements before proceeding |
| 675 | - if ( !$ret && ! empty( $item_reqs ) ) { |
|
| 676 | - switch( $condition ) { |
|
| 675 | + if (!$ret && !empty($item_reqs)) { |
|
| 676 | + switch ($condition) { |
|
| 677 | 677 | case 'all' : |
| 678 | 678 | // Default back to true |
| 679 | 679 | $ret = true; |
| 680 | 680 | |
| 681 | - foreach ( $item_reqs as $item_id ) { |
|
| 682 | - if ( !wpinv_item_in_cart( $item_id ) ) { |
|
| 683 | - wpinv_set_error( 'wpinv-discount-error', __( 'The item requirements for this discount are not met.', 'invoicing' ) ); |
|
| 681 | + foreach ($item_reqs as $item_id) { |
|
| 682 | + if (!wpinv_item_in_cart($item_id)) { |
|
| 683 | + wpinv_set_error('wpinv-discount-error', __('The item requirements for this discount are not met.', 'invoicing')); |
|
| 684 | 684 | $ret = false; |
| 685 | 685 | break; |
| 686 | 686 | } |
@@ -689,15 +689,15 @@ discard block |
||
| 689 | 689 | break; |
| 690 | 690 | |
| 691 | 691 | default : // Any |
| 692 | - foreach ( $item_reqs as $item_id ) { |
|
| 693 | - if ( wpinv_item_in_cart( $item_id ) ) { |
|
| 692 | + foreach ($item_reqs as $item_id) { |
|
| 693 | + if (wpinv_item_in_cart($item_id)) { |
|
| 694 | 694 | $ret = true; |
| 695 | 695 | break; |
| 696 | 696 | } |
| 697 | 697 | } |
| 698 | 698 | |
| 699 | - if( ! $ret ) { |
|
| 700 | - wpinv_set_error( 'wpinv-discount-error', __( 'The item requirements for this discount are not met.', 'invoicing' ) ); |
|
| 699 | + if (!$ret) { |
|
| 700 | + wpinv_set_error('wpinv-discount-error', __('The item requirements for this discount are not met.', 'invoicing')); |
|
| 701 | 701 | } |
| 702 | 702 | |
| 703 | 703 | break; |
@@ -706,15 +706,15 @@ discard block |
||
| 706 | 706 | $ret = true; |
| 707 | 707 | } |
| 708 | 708 | |
| 709 | - if( ! empty( $excluded_ps ) ) { |
|
| 709 | + if (!empty($excluded_ps)) { |
|
| 710 | 710 | // Check that there are items other than excluded ones in the cart |
| 711 | - if( $cart_ids == $excluded_ps ) { |
|
| 712 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not valid for the cart contents.', 'invoicing' ) ); |
|
| 711 | + if ($cart_ids == $excluded_ps) { |
|
| 712 | + wpinv_set_error('wpinv-discount-error', __('This discount is not valid for the cart contents.', 'invoicing')); |
|
| 713 | 713 | $ret = false; |
| 714 | 714 | } |
| 715 | 715 | } |
| 716 | 716 | |
| 717 | - return (bool) apply_filters( 'wpinv_is_discount_item_req_met', $ret, $code_id, $condition ); |
|
| 717 | + return (bool) apply_filters('wpinv_is_discount_item_req_met', $ret, $code_id, $condition); |
|
| 718 | 718 | } |
| 719 | 719 | |
| 720 | 720 | /** |
@@ -725,53 +725,53 @@ discard block |
||
| 725 | 725 | * @param int|array|string|WPInv_Discount $code_id discount data, object, ID or code. |
| 726 | 726 | * @return bool |
| 727 | 727 | */ |
| 728 | -function wpinv_is_discount_used( $discount = array(), $user = '', $code_id = array() ) { |
|
| 728 | +function wpinv_is_discount_used($discount = array(), $user = '', $code_id = array()) { |
|
| 729 | 729 | |
| 730 | - if( ! empty( $discount ) ) { |
|
| 731 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 730 | + if (!empty($discount)) { |
|
| 731 | + $discount = wpinv_get_discount_obj($discount); |
|
| 732 | 732 | } else { |
| 733 | - $discount = wpinv_get_discount_obj( $code_id ); |
|
| 733 | + $discount = wpinv_get_discount_obj($code_id); |
|
| 734 | 734 | } |
| 735 | 735 | |
| 736 | - $is_used = ! $discount->is_valid_for_user( $user ); |
|
| 737 | - $is_used = apply_filters( 'wpinv_is_discount_used', $is_used, $discount->code, $user, $discount->ID, $discount ); |
|
| 736 | + $is_used = !$discount->is_valid_for_user($user); |
|
| 737 | + $is_used = apply_filters('wpinv_is_discount_used', $is_used, $discount->code, $user, $discount->ID, $discount); |
|
| 738 | 738 | |
| 739 | - if( $is_used ) { |
|
| 740 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount has already been redeemed.', 'invoicing' ) ); |
|
| 739 | + if ($is_used) { |
|
| 740 | + wpinv_set_error('wpinv-discount-error', __('This discount has already been redeemed.', 'invoicing')); |
|
| 741 | 741 | } |
| 742 | 742 | |
| 743 | 743 | return $is_used; |
| 744 | 744 | } |
| 745 | 745 | |
| 746 | -function wpinv_is_discount_valid( $code = '', $user = '', $set_error = true ) { |
|
| 746 | +function wpinv_is_discount_valid($code = '', $user = '', $set_error = true) { |
|
| 747 | 747 | $return = false; |
| 748 | - $discount_id = wpinv_get_discount_id_by_code( $code ); |
|
| 749 | - $user = trim( $user ); |
|
| 748 | + $discount_id = wpinv_get_discount_id_by_code($code); |
|
| 749 | + $user = trim($user); |
|
| 750 | 750 | |
| 751 | - if ( wpinv_get_cart_contents() ) { |
|
| 752 | - if ( $discount_id !== false ) { |
|
| 751 | + if (wpinv_get_cart_contents()) { |
|
| 752 | + if ($discount_id !== false) { |
|
| 753 | 753 | if ( |
| 754 | - wpinv_is_discount_active( $discount_id ) && |
|
| 755 | - wpinv_check_discount_dates( $discount_id ) && |
|
| 756 | - !wpinv_is_discount_maxed_out( $discount_id ) && |
|
| 757 | - !wpinv_is_discount_used( $code, $user, $discount_id ) && |
|
| 758 | - wpinv_discount_is_min_met( $discount_id ) && |
|
| 759 | - wpinv_discount_is_max_met( $discount_id ) && |
|
| 760 | - wpinv_discount_item_reqs_met( $discount_id ) |
|
| 754 | + wpinv_is_discount_active($discount_id) && |
|
| 755 | + wpinv_check_discount_dates($discount_id) && |
|
| 756 | + !wpinv_is_discount_maxed_out($discount_id) && |
|
| 757 | + !wpinv_is_discount_used($code, $user, $discount_id) && |
|
| 758 | + wpinv_discount_is_min_met($discount_id) && |
|
| 759 | + wpinv_discount_is_max_met($discount_id) && |
|
| 760 | + wpinv_discount_item_reqs_met($discount_id) |
|
| 761 | 761 | ) { |
| 762 | 762 | $return = true; |
| 763 | 763 | } |
| 764 | - } elseif( $set_error ) { |
|
| 765 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is invalid.', 'invoicing' ) ); |
|
| 764 | + } elseif ($set_error) { |
|
| 765 | + wpinv_set_error('wpinv-discount-error', __('This discount is invalid.', 'invoicing')); |
|
| 766 | 766 | } |
| 767 | 767 | } |
| 768 | 768 | |
| 769 | - return apply_filters( 'wpinv_is_discount_valid', $return, $discount_id, $code, $user ); |
|
| 769 | + return apply_filters('wpinv_is_discount_valid', $return, $discount_id, $code, $user); |
|
| 770 | 770 | } |
| 771 | 771 | |
| 772 | -function wpinv_get_discount_id_by_code( $code ) { |
|
| 773 | - $discount = wpinv_get_discount_by_code( $code ); |
|
| 774 | - if( $discount ) { |
|
| 772 | +function wpinv_get_discount_id_by_code($code) { |
|
| 773 | + $discount = wpinv_get_discount_by_code($code); |
|
| 774 | + if ($discount) { |
|
| 775 | 775 | return $discount->ID; |
| 776 | 776 | } |
| 777 | 777 | return false; |
@@ -784,9 +784,9 @@ discard block |
||
| 784 | 784 | * @param float $base_price The number of usages to increase by |
| 785 | 785 | * @return float |
| 786 | 786 | */ |
| 787 | -function wpinv_get_discounted_amount( $discount, $base_price ) { |
|
| 788 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 789 | - return $discount->get_discounted_amount( $base_price ); |
|
| 787 | +function wpinv_get_discounted_amount($discount, $base_price) { |
|
| 788 | + $discount = wpinv_get_discount_obj($discount); |
|
| 789 | + return $discount->get_discounted_amount($base_price); |
|
| 790 | 790 | } |
| 791 | 791 | |
| 792 | 792 | /** |
@@ -796,9 +796,9 @@ discard block |
||
| 796 | 796 | * @param int $by The number of usages to increase by. |
| 797 | 797 | * @return int the new number of uses. |
| 798 | 798 | */ |
| 799 | -function wpinv_increase_discount_usage( $discount, $by = 1 ) { |
|
| 800 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 801 | - return $discount->increase_usage( $by ); |
|
| 799 | +function wpinv_increase_discount_usage($discount, $by = 1) { |
|
| 800 | + $discount = wpinv_get_discount_obj($discount); |
|
| 801 | + return $discount->increase_usage($by); |
|
| 802 | 802 | } |
| 803 | 803 | |
| 804 | 804 | /** |
@@ -808,72 +808,72 @@ discard block |
||
| 808 | 808 | * @param int $by The number of usages to decrease by. |
| 809 | 809 | * @return int the new number of uses. |
| 810 | 810 | */ |
| 811 | -function wpinv_decrease_discount_usage( $discount, $by = 1 ) { |
|
| 812 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 813 | - return $discount->increase_usage( 0 - $by ); |
|
| 811 | +function wpinv_decrease_discount_usage($discount, $by = 1) { |
|
| 812 | + $discount = wpinv_get_discount_obj($discount); |
|
| 813 | + return $discount->increase_usage(0 - $by); |
|
| 814 | 814 | } |
| 815 | 815 | |
| 816 | -function wpinv_format_discount_rate( $type, $amount ) { |
|
| 817 | - if ( $type == 'flat' ) { |
|
| 818 | - $rate = wpinv_price( wpinv_format_amount( $amount ) ); |
|
| 816 | +function wpinv_format_discount_rate($type, $amount) { |
|
| 817 | + if ($type == 'flat') { |
|
| 818 | + $rate = wpinv_price(wpinv_format_amount($amount)); |
|
| 819 | 819 | } else { |
| 820 | 820 | $rate = $amount . '%'; |
| 821 | 821 | } |
| 822 | 822 | |
| 823 | - return apply_filters( 'wpinv_format_discount_rate', $rate, $type, $amount ); |
|
| 823 | + return apply_filters('wpinv_format_discount_rate', $rate, $type, $amount); |
|
| 824 | 824 | } |
| 825 | 825 | |
| 826 | -function wpinv_set_cart_discount( $code = '' ) { |
|
| 827 | - if ( wpinv_multiple_discounts_allowed() ) { |
|
| 826 | +function wpinv_set_cart_discount($code = '') { |
|
| 827 | + if (wpinv_multiple_discounts_allowed()) { |
|
| 828 | 828 | // Get all active cart discounts |
| 829 | 829 | $discounts = wpinv_get_cart_discounts(); |
| 830 | 830 | } else { |
| 831 | 831 | $discounts = false; // Only one discount allowed per purchase, so override any existing |
| 832 | 832 | } |
| 833 | 833 | |
| 834 | - if ( $discounts ) { |
|
| 835 | - $key = array_search( strtolower( $code ), array_map( 'strtolower', $discounts ) ); |
|
| 836 | - if( false !== $key ) { |
|
| 837 | - unset( $discounts[ $key ] ); // Can't set the same discount more than once |
|
| 834 | + if ($discounts) { |
|
| 835 | + $key = array_search(strtolower($code), array_map('strtolower', $discounts)); |
|
| 836 | + if (false !== $key) { |
|
| 837 | + unset($discounts[$key]); // Can't set the same discount more than once |
|
| 838 | 838 | } |
| 839 | 839 | $discounts[] = $code; |
| 840 | 840 | } else { |
| 841 | 841 | $discounts = array(); |
| 842 | 842 | $discounts[] = $code; |
| 843 | 843 | } |
| 844 | - $discounts = array_values( $discounts ); |
|
| 844 | + $discounts = array_values($discounts); |
|
| 845 | 845 | |
| 846 | 846 | $data = wpinv_get_checkout_session(); |
| 847 | - if ( empty( $data ) ) { |
|
| 847 | + if (empty($data)) { |
|
| 848 | 848 | $data = array(); |
| 849 | 849 | } else { |
| 850 | - if ( !empty( $data['invoice_id'] ) && $payment_meta = wpinv_get_invoice_meta( $data['invoice_id'] ) ) { |
|
| 851 | - $payment_meta['user_info']['discount'] = implode( ',', $discounts ); |
|
| 852 | - update_post_meta( $data['invoice_id'], '_wpinv_payment_meta', $payment_meta ); |
|
| 850 | + if (!empty($data['invoice_id']) && $payment_meta = wpinv_get_invoice_meta($data['invoice_id'])) { |
|
| 851 | + $payment_meta['user_info']['discount'] = implode(',', $discounts); |
|
| 852 | + update_post_meta($data['invoice_id'], '_wpinv_payment_meta', $payment_meta); |
|
| 853 | 853 | } |
| 854 | 854 | } |
| 855 | 855 | $data['cart_discounts'] = $discounts; |
| 856 | 856 | |
| 857 | - wpinv_set_checkout_session( $data ); |
|
| 857 | + wpinv_set_checkout_session($data); |
|
| 858 | 858 | |
| 859 | 859 | return $discounts; |
| 860 | 860 | } |
| 861 | 861 | |
| 862 | -function wpinv_unset_cart_discount( $code = '' ) { |
|
| 862 | +function wpinv_unset_cart_discount($code = '') { |
|
| 863 | 863 | $discounts = wpinv_get_cart_discounts(); |
| 864 | 864 | |
| 865 | - if ( $code && !empty( $discounts ) && in_array( $code, $discounts ) ) { |
|
| 866 | - $key = array_search( $code, $discounts ); |
|
| 867 | - unset( $discounts[ $key ] ); |
|
| 865 | + if ($code && !empty($discounts) && in_array($code, $discounts)) { |
|
| 866 | + $key = array_search($code, $discounts); |
|
| 867 | + unset($discounts[$key]); |
|
| 868 | 868 | |
| 869 | 869 | $data = wpinv_get_checkout_session(); |
| 870 | 870 | $data['cart_discounts'] = $discounts; |
| 871 | - if ( !empty( $data['invoice_id'] ) && $payment_meta = wpinv_get_invoice_meta( $data['invoice_id'] ) ) { |
|
| 872 | - $payment_meta['user_info']['discount'] = !empty( $discounts ) ? implode( ',', $discounts ) : ''; |
|
| 873 | - update_post_meta( $data['invoice_id'], '_wpinv_payment_meta', $payment_meta ); |
|
| 871 | + if (!empty($data['invoice_id']) && $payment_meta = wpinv_get_invoice_meta($data['invoice_id'])) { |
|
| 872 | + $payment_meta['user_info']['discount'] = !empty($discounts) ? implode(',', $discounts) : ''; |
|
| 873 | + update_post_meta($data['invoice_id'], '_wpinv_payment_meta', $payment_meta); |
|
| 874 | 874 | } |
| 875 | 875 | |
| 876 | - wpinv_set_checkout_session( $data ); |
|
| 876 | + wpinv_set_checkout_session($data); |
|
| 877 | 877 | } |
| 878 | 878 | |
| 879 | 879 | return $discounts; |
@@ -882,10 +882,10 @@ discard block |
||
| 882 | 882 | function wpinv_unset_all_cart_discounts() { |
| 883 | 883 | $data = wpinv_get_checkout_session(); |
| 884 | 884 | |
| 885 | - if ( !empty( $data ) && isset( $data['cart_discounts'] ) ) { |
|
| 886 | - unset( $data['cart_discounts'] ); |
|
| 885 | + if (!empty($data) && isset($data['cart_discounts'])) { |
|
| 886 | + unset($data['cart_discounts']); |
|
| 887 | 887 | |
| 888 | - wpinv_set_checkout_session( $data ); |
|
| 888 | + wpinv_set_checkout_session($data); |
|
| 889 | 889 | return true; |
| 890 | 890 | } |
| 891 | 891 | |
@@ -894,13 +894,13 @@ discard block |
||
| 894 | 894 | |
| 895 | 895 | function wpinv_get_cart_discounts() { |
| 896 | 896 | $session = wpinv_get_checkout_session(); |
| 897 | - return empty( $session['cart_discounts'] ) ? false : $session['cart_discounts']; |
|
| 897 | + return empty($session['cart_discounts']) ? false : $session['cart_discounts']; |
|
| 898 | 898 | } |
| 899 | 899 | |
| 900 | -function wpinv_cart_has_discounts( $items = array() ) { |
|
| 900 | +function wpinv_cart_has_discounts($items = array()) { |
|
| 901 | 901 | $ret = false; |
| 902 | 902 | |
| 903 | - if ( wpinv_get_cart_discounts( $items ) ) { |
|
| 903 | + if (wpinv_get_cart_discounts($items)) { |
|
| 904 | 904 | $ret = true; |
| 905 | 905 | } |
| 906 | 906 | |
@@ -911,131 +911,131 @@ discard block |
||
| 911 | 911 | } |
| 912 | 912 | */ |
| 913 | 913 | |
| 914 | - return apply_filters( 'wpinv_cart_has_discounts', $ret ); |
|
| 914 | + return apply_filters('wpinv_cart_has_discounts', $ret); |
|
| 915 | 915 | } |
| 916 | 916 | |
| 917 | -function wpinv_get_cart_discounted_amount( $items = array(), $discounts = false ) { |
|
| 917 | +function wpinv_get_cart_discounted_amount($items = array(), $discounts = false) { |
|
| 918 | 918 | $amount = 0.00; |
| 919 | - $items = !empty( $items ) ? $items : wpinv_get_cart_content_details(); |
|
| 919 | + $items = !empty($items) ? $items : wpinv_get_cart_content_details(); |
|
| 920 | 920 | |
| 921 | - if ( $items ) { |
|
| 922 | - $discounts = wp_list_pluck( $items, 'discount' ); |
|
| 921 | + if ($items) { |
|
| 922 | + $discounts = wp_list_pluck($items, 'discount'); |
|
| 923 | 923 | |
| 924 | - if ( is_array( $discounts ) ) { |
|
| 925 | - $discounts = array_map( 'floatval', $discounts ); |
|
| 926 | - $amount = array_sum( $discounts ); |
|
| 924 | + if (is_array($discounts)) { |
|
| 925 | + $discounts = array_map('floatval', $discounts); |
|
| 926 | + $amount = array_sum($discounts); |
|
| 927 | 927 | } |
| 928 | 928 | } |
| 929 | 929 | |
| 930 | - return apply_filters( 'wpinv_get_cart_discounted_amount', $amount ); |
|
| 930 | + return apply_filters('wpinv_get_cart_discounted_amount', $amount); |
|
| 931 | 931 | } |
| 932 | 932 | |
| 933 | -function wpinv_get_cart_items_discount_amount( $items = array(), $discount = false ) { |
|
| 934 | - $items = !empty( $items ) ? $items : wpinv_get_cart_content_details(); |
|
| 933 | +function wpinv_get_cart_items_discount_amount($items = array(), $discount = false) { |
|
| 934 | + $items = !empty($items) ? $items : wpinv_get_cart_content_details(); |
|
| 935 | 935 | |
| 936 | - if ( empty( $discount ) || empty( $items ) ) { |
|
| 936 | + if (empty($discount) || empty($items)) { |
|
| 937 | 937 | return 0; |
| 938 | 938 | } |
| 939 | 939 | |
| 940 | 940 | $amount = 0; |
| 941 | 941 | |
| 942 | - foreach ( $items as $item ) { |
|
| 943 | - $amount += wpinv_get_cart_item_discount_amount( $item, $discount ); |
|
| 942 | + foreach ($items as $item) { |
|
| 943 | + $amount += wpinv_get_cart_item_discount_amount($item, $discount); |
|
| 944 | 944 | } |
| 945 | 945 | |
| 946 | - $amount = wpinv_round_amount( $amount ); |
|
| 946 | + $amount = wpinv_round_amount($amount); |
|
| 947 | 947 | |
| 948 | 948 | return $amount; |
| 949 | 949 | } |
| 950 | 950 | |
| 951 | -function wpinv_get_cart_item_discount_amount( $item = array(), $discount = false ) { |
|
| 951 | +function wpinv_get_cart_item_discount_amount($item = array(), $discount = false) { |
|
| 952 | 952 | global $wpinv_is_last_cart_item, $wpinv_flat_discount_total; |
| 953 | 953 | |
| 954 | 954 | $amount = 0; |
| 955 | 955 | |
| 956 | - if ( empty( $item ) || empty( $item['id'] ) ) { |
|
| 956 | + if (empty($item) || empty($item['id'])) { |
|
| 957 | 957 | return $amount; |
| 958 | 958 | } |
| 959 | 959 | |
| 960 | - if ( empty( $item['quantity'] ) ) { |
|
| 960 | + if (empty($item['quantity'])) { |
|
| 961 | 961 | return $amount; |
| 962 | 962 | } |
| 963 | 963 | |
| 964 | - if ( empty( $item['options'] ) ) { |
|
| 964 | + if (empty($item['options'])) { |
|
| 965 | 965 | $item['options'] = array(); |
| 966 | 966 | } |
| 967 | 967 | |
| 968 | - $price = wpinv_get_cart_item_price( $item['id'], $item, $item['options'] ); |
|
| 968 | + $price = wpinv_get_cart_item_price($item['id'], $item, $item['options']); |
|
| 969 | 969 | $discounted_price = $price; |
| 970 | 970 | |
| 971 | 971 | $discounts = false === $discount ? wpinv_get_cart_discounts() : $discount; |
| 972 | - if ( empty( $discounts ) ) { |
|
| 972 | + if (empty($discounts)) { |
|
| 973 | 973 | return $amount; |
| 974 | 974 | } |
| 975 | 975 | |
| 976 | - if ( $discounts ) { |
|
| 977 | - if ( is_array( $discounts ) ) { |
|
| 978 | - $discounts = array_values( $discounts ); |
|
| 976 | + if ($discounts) { |
|
| 977 | + if (is_array($discounts)) { |
|
| 978 | + $discounts = array_values($discounts); |
|
| 979 | 979 | } else { |
| 980 | - $discounts = explode( ',', $discounts ); |
|
| 980 | + $discounts = explode(',', $discounts); |
|
| 981 | 981 | } |
| 982 | 982 | } |
| 983 | 983 | |
| 984 | - if( $discounts ) { |
|
| 985 | - foreach ( $discounts as $discount ) { |
|
| 986 | - $code_id = wpinv_get_discount_id_by_code( $discount ); |
|
| 984 | + if ($discounts) { |
|
| 985 | + foreach ($discounts as $discount) { |
|
| 986 | + $code_id = wpinv_get_discount_id_by_code($discount); |
|
| 987 | 987 | |
| 988 | 988 | // Check discount exists |
| 989 | - if( $code_id === false ) { |
|
| 989 | + if ($code_id === false) { |
|
| 990 | 990 | continue; |
| 991 | 991 | } |
| 992 | 992 | |
| 993 | - $reqs = wpinv_get_discount_item_reqs( $code_id ); |
|
| 994 | - $excluded_items = wpinv_get_discount_excluded_items( $code_id ); |
|
| 993 | + $reqs = wpinv_get_discount_item_reqs($code_id); |
|
| 994 | + $excluded_items = wpinv_get_discount_excluded_items($code_id); |
|
| 995 | 995 | |
| 996 | 996 | // Make sure requirements are set and that this discount shouldn't apply to the whole cart |
| 997 | - if ( !empty( $reqs ) && wpinv_is_discount_not_global( $code_id ) ) { |
|
| 998 | - foreach ( $reqs as $item_id ) { |
|
| 999 | - if ( $item_id == $item['id'] && ! in_array( $item['id'], $excluded_items ) ) { |
|
| 1000 | - $discounted_price -= wpinv_get_discounted_amount( $discount, $price ); |
|
| 997 | + if (!empty($reqs) && wpinv_is_discount_not_global($code_id)) { |
|
| 998 | + foreach ($reqs as $item_id) { |
|
| 999 | + if ($item_id == $item['id'] && !in_array($item['id'], $excluded_items)) { |
|
| 1000 | + $discounted_price -= wpinv_get_discounted_amount($discount, $price); |
|
| 1001 | 1001 | } |
| 1002 | 1002 | } |
| 1003 | 1003 | } else { |
| 1004 | 1004 | // This is a global cart discount |
| 1005 | - if ( !in_array( $item['id'], $excluded_items ) ) { |
|
| 1006 | - if ( 'flat' === wpinv_get_discount_type( $code_id ) ) { |
|
| 1005 | + if (!in_array($item['id'], $excluded_items)) { |
|
| 1006 | + if ('flat' === wpinv_get_discount_type($code_id)) { |
|
| 1007 | 1007 | $items_subtotal = 0.00; |
| 1008 | 1008 | $cart_items = wpinv_get_cart_contents(); |
| 1009 | 1009 | |
| 1010 | - foreach ( $cart_items as $cart_item ) { |
|
| 1011 | - if ( ! in_array( $cart_item['id'], $excluded_items ) ) { |
|
| 1012 | - $options = !empty( $cart_item['options'] ) ? $cart_item['options'] : array(); |
|
| 1013 | - $item_price = wpinv_get_cart_item_price( $cart_item['id'], $cart_item, $options ); |
|
| 1010 | + foreach ($cart_items as $cart_item) { |
|
| 1011 | + if (!in_array($cart_item['id'], $excluded_items)) { |
|
| 1012 | + $options = !empty($cart_item['options']) ? $cart_item['options'] : array(); |
|
| 1013 | + $item_price = wpinv_get_cart_item_price($cart_item['id'], $cart_item, $options); |
|
| 1014 | 1014 | $items_subtotal += $item_price * $cart_item['quantity']; |
| 1015 | 1015 | } |
| 1016 | 1016 | } |
| 1017 | 1017 | |
| 1018 | - $subtotal_percent = ( ( $price * $item['quantity'] ) / $items_subtotal ); |
|
| 1019 | - $code_amount = wpinv_get_discount_amount( $code_id ); |
|
| 1018 | + $subtotal_percent = (($price * $item['quantity']) / $items_subtotal); |
|
| 1019 | + $code_amount = wpinv_get_discount_amount($code_id); |
|
| 1020 | 1020 | $discounted_amount = $code_amount * $subtotal_percent; |
| 1021 | 1021 | $discounted_price -= $discounted_amount; |
| 1022 | 1022 | |
| 1023 | - $wpinv_flat_discount_total += round( $discounted_amount, wpinv_currency_decimal_filter() ); |
|
| 1023 | + $wpinv_flat_discount_total += round($discounted_amount, wpinv_currency_decimal_filter()); |
|
| 1024 | 1024 | |
| 1025 | - if ( $wpinv_is_last_cart_item && $wpinv_flat_discount_total < $code_amount ) { |
|
| 1025 | + if ($wpinv_is_last_cart_item && $wpinv_flat_discount_total < $code_amount) { |
|
| 1026 | 1026 | $adjustment = $code_amount - $wpinv_flat_discount_total; |
| 1027 | 1027 | $discounted_price -= $adjustment; |
| 1028 | 1028 | } |
| 1029 | 1029 | } else { |
| 1030 | - $discounted_price -= wpinv_get_discounted_amount( $discount, $price ); |
|
| 1030 | + $discounted_price -= wpinv_get_discounted_amount($discount, $price); |
|
| 1031 | 1031 | } |
| 1032 | 1032 | } |
| 1033 | 1033 | } |
| 1034 | 1034 | } |
| 1035 | 1035 | |
| 1036 | - $amount = ( $price - apply_filters( 'wpinv_get_cart_item_discounted_amount', $discounted_price, $discounts, $item, $price ) ); |
|
| 1036 | + $amount = ($price - apply_filters('wpinv_get_cart_item_discounted_amount', $discounted_price, $discounts, $item, $price)); |
|
| 1037 | 1037 | |
| 1038 | - if ( 'flat' !== wpinv_get_discount_type( $code_id ) ) { |
|
| 1038 | + if ('flat' !== wpinv_get_discount_type($code_id)) { |
|
| 1039 | 1039 | $amount = $amount * $item['quantity']; |
| 1040 | 1040 | } |
| 1041 | 1041 | } |
@@ -1043,59 +1043,59 @@ discard block |
||
| 1043 | 1043 | return $amount; |
| 1044 | 1044 | } |
| 1045 | 1045 | |
| 1046 | -function wpinv_cart_discounts_html( $items = array() ) { |
|
| 1047 | - echo wpinv_get_cart_discounts_html( $items ); |
|
| 1046 | +function wpinv_cart_discounts_html($items = array()) { |
|
| 1047 | + echo wpinv_get_cart_discounts_html($items); |
|
| 1048 | 1048 | } |
| 1049 | 1049 | |
| 1050 | -function wpinv_get_cart_discounts_html( $items = array(), $discounts = false ) { |
|
| 1050 | +function wpinv_get_cart_discounts_html($items = array(), $discounts = false) { |
|
| 1051 | 1051 | global $wpi_cart_columns; |
| 1052 | 1052 | |
| 1053 | - $items = !empty( $items ) ? $items : wpinv_get_cart_content_details(); |
|
| 1053 | + $items = !empty($items) ? $items : wpinv_get_cart_content_details(); |
|
| 1054 | 1054 | |
| 1055 | - if ( !$discounts ) { |
|
| 1056 | - $discounts = wpinv_get_cart_discounts( $items ); |
|
| 1055 | + if (!$discounts) { |
|
| 1056 | + $discounts = wpinv_get_cart_discounts($items); |
|
| 1057 | 1057 | } |
| 1058 | 1058 | |
| 1059 | - if ( !$discounts ) { |
|
| 1059 | + if (!$discounts) { |
|
| 1060 | 1060 | return; |
| 1061 | 1061 | } |
| 1062 | 1062 | |
| 1063 | - $discounts = is_array( $discounts ) ? $discounts : array( $discounts ); |
|
| 1063 | + $discounts = is_array($discounts) ? $discounts : array($discounts); |
|
| 1064 | 1064 | |
| 1065 | 1065 | $html = ''; |
| 1066 | 1066 | |
| 1067 | - foreach ( $discounts as $discount ) { |
|
| 1068 | - $discount_id = wpinv_get_discount_id_by_code( $discount ); |
|
| 1069 | - $discount_value = wpinv_get_discount_amount( $discount_id ); |
|
| 1070 | - $rate = wpinv_format_discount_rate( wpinv_get_discount_type( $discount_id ), $discount_value ); |
|
| 1071 | - $amount = wpinv_get_cart_items_discount_amount( $items, $discount ); |
|
| 1072 | - $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> '; |
|
| 1067 | + foreach ($discounts as $discount) { |
|
| 1068 | + $discount_id = wpinv_get_discount_id_by_code($discount); |
|
| 1069 | + $discount_value = wpinv_get_discount_amount($discount_id); |
|
| 1070 | + $rate = wpinv_format_discount_rate(wpinv_get_discount_type($discount_id), $discount_value); |
|
| 1071 | + $amount = wpinv_get_cart_items_discount_amount($items, $discount); |
|
| 1072 | + $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> '; |
|
| 1073 | 1073 | |
| 1074 | 1074 | $html .= '<tr class="wpinv_cart_footer_row wpinv_cart_discount_row">'; |
| 1075 | 1075 | ob_start(); |
| 1076 | - do_action( 'wpinv_checkout_table_discount_first', $items ); |
|
| 1076 | + do_action('wpinv_checkout_table_discount_first', $items); |
|
| 1077 | 1077 | $html .= ob_get_clean(); |
| 1078 | - $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>'; |
|
| 1078 | + $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>'; |
|
| 1079 | 1079 | ob_start(); |
| 1080 | - do_action( 'wpinv_checkout_table_discount_last', $items ); |
|
| 1080 | + do_action('wpinv_checkout_table_discount_last', $items); |
|
| 1081 | 1081 | $html .= ob_get_clean(); |
| 1082 | 1082 | $html .= '</tr>'; |
| 1083 | 1083 | } |
| 1084 | 1084 | |
| 1085 | - return apply_filters( 'wpinv_get_cart_discounts_html', $html, $discounts, $rate ); |
|
| 1085 | + return apply_filters('wpinv_get_cart_discounts_html', $html, $discounts, $rate); |
|
| 1086 | 1086 | } |
| 1087 | 1087 | |
| 1088 | -function wpinv_display_cart_discount( /** @scrutinizer ignore-unused */ $formatted = false, $echo = false ) { |
|
| 1088 | +function wpinv_display_cart_discount(/** @scrutinizer ignore-unused */ $formatted = false, $echo = false) { |
|
| 1089 | 1089 | $discounts = wpinv_get_cart_discounts(); |
| 1090 | 1090 | |
| 1091 | - if ( empty( $discounts ) ) { |
|
| 1091 | + if (empty($discounts)) { |
|
| 1092 | 1092 | return false; |
| 1093 | 1093 | } |
| 1094 | 1094 | |
| 1095 | - $discount_id = wpinv_get_discount_id_by_code( $discounts[0] ); |
|
| 1096 | - $amount = wpinv_format_discount_rate( wpinv_get_discount_type( $discount_id ), wpinv_get_discount_amount( $discount_id ) ); |
|
| 1095 | + $discount_id = wpinv_get_discount_id_by_code($discounts[0]); |
|
| 1096 | + $amount = wpinv_format_discount_rate(wpinv_get_discount_type($discount_id), wpinv_get_discount_amount($discount_id)); |
|
| 1097 | 1097 | |
| 1098 | - if ( $echo ) { |
|
| 1098 | + if ($echo) { |
|
| 1099 | 1099 | echo $amount; |
| 1100 | 1100 | } |
| 1101 | 1101 | |
@@ -1103,101 +1103,101 @@ discard block |
||
| 1103 | 1103 | } |
| 1104 | 1104 | |
| 1105 | 1105 | function wpinv_remove_cart_discount() { |
| 1106 | - if ( !isset( $_GET['discount_id'] ) || ! isset( $_GET['discount_code'] ) ) { |
|
| 1106 | + if (!isset($_GET['discount_id']) || !isset($_GET['discount_code'])) { |
|
| 1107 | 1107 | return; |
| 1108 | 1108 | } |
| 1109 | 1109 | |
| 1110 | - do_action( 'wpinv_pre_remove_cart_discount', absint( $_GET['discount_id'] ) ); |
|
| 1110 | + do_action('wpinv_pre_remove_cart_discount', absint($_GET['discount_id'])); |
|
| 1111 | 1111 | |
| 1112 | - wpinv_unset_cart_discount( urldecode( $_GET['discount_code'] ) ); |
|
| 1112 | + wpinv_unset_cart_discount(urldecode($_GET['discount_code'])); |
|
| 1113 | 1113 | |
| 1114 | - do_action( 'wpinv_post_remove_cart_discount', absint( $_GET['discount_id'] ) ); |
|
| 1114 | + do_action('wpinv_post_remove_cart_discount', absint($_GET['discount_id'])); |
|
| 1115 | 1115 | |
| 1116 | - wp_redirect( wpinv_get_checkout_uri() ); wpinv_die(); |
|
| 1116 | + wp_redirect(wpinv_get_checkout_uri()); wpinv_die(); |
|
| 1117 | 1117 | } |
| 1118 | -add_action( 'wpinv_remove_cart_discount', 'wpinv_remove_cart_discount' ); |
|
| 1118 | +add_action('wpinv_remove_cart_discount', 'wpinv_remove_cart_discount'); |
|
| 1119 | 1119 | |
| 1120 | 1120 | function wpinv_maybe_remove_cart_discount() { |
| 1121 | 1121 | $discounts = wpinv_get_cart_discounts(); |
| 1122 | 1122 | |
| 1123 | - if ( !$discounts ) { |
|
| 1123 | + if (!$discounts) { |
|
| 1124 | 1124 | return; |
| 1125 | 1125 | } |
| 1126 | 1126 | |
| 1127 | - foreach ( $discounts as $discount ) { |
|
| 1128 | - if ( !wpinv_is_discount_valid( $discount ) ) { |
|
| 1129 | - wpinv_unset_cart_discount( $discount ); |
|
| 1127 | + foreach ($discounts as $discount) { |
|
| 1128 | + if (!wpinv_is_discount_valid($discount)) { |
|
| 1129 | + wpinv_unset_cart_discount($discount); |
|
| 1130 | 1130 | } |
| 1131 | 1131 | } |
| 1132 | 1132 | } |
| 1133 | -add_action( 'wpinv_post_remove_from_cart', 'wpinv_maybe_remove_cart_discount' ); |
|
| 1133 | +add_action('wpinv_post_remove_from_cart', 'wpinv_maybe_remove_cart_discount'); |
|
| 1134 | 1134 | |
| 1135 | 1135 | function wpinv_multiple_discounts_allowed() { |
| 1136 | - $ret = wpinv_get_option( 'allow_multiple_discounts', false ); |
|
| 1137 | - return (bool) apply_filters( 'wpinv_multiple_discounts_allowed', $ret ); |
|
| 1136 | + $ret = wpinv_get_option('allow_multiple_discounts', false); |
|
| 1137 | + return (bool) apply_filters('wpinv_multiple_discounts_allowed', $ret); |
|
| 1138 | 1138 | } |
| 1139 | 1139 | |
| 1140 | -function wpinv_get_discount_label( $code, $echo = true ) { |
|
| 1141 | - $label = wp_sprintf( __( 'Discount%1$s', 'invoicing' ), ( $code != '' && $code != 'none' ? ' (<code>' . $code . '</code>)': '' ) ); |
|
| 1142 | - $label = apply_filters( 'wpinv_get_discount_label', $label, $code ); |
|
| 1140 | +function wpinv_get_discount_label($code, $echo = true) { |
|
| 1141 | + $label = wp_sprintf(__('Discount%1$s', 'invoicing'), ($code != '' && $code != 'none' ? ' (<code>' . $code . '</code>)' : '')); |
|
| 1142 | + $label = apply_filters('wpinv_get_discount_label', $label, $code); |
|
| 1143 | 1143 | |
| 1144 | - if ( $echo ) { |
|
| 1144 | + if ($echo) { |
|
| 1145 | 1145 | echo $label; |
| 1146 | 1146 | } else { |
| 1147 | 1147 | return $label; |
| 1148 | 1148 | } |
| 1149 | 1149 | } |
| 1150 | 1150 | |
| 1151 | -function wpinv_cart_discount_label( $code, $rate, $echo = true ) { |
|
| 1152 | - $label = wp_sprintf( __( 'Discount: %s', 'invoicing' ), $code ); |
|
| 1153 | - $label = apply_filters( 'wpinv_cart_discount_label', $label, $code, $rate ); |
|
| 1151 | +function wpinv_cart_discount_label($code, $rate, $echo = true) { |
|
| 1152 | + $label = wp_sprintf(__('Discount: %s', 'invoicing'), $code); |
|
| 1153 | + $label = apply_filters('wpinv_cart_discount_label', $label, $code, $rate); |
|
| 1154 | 1154 | |
| 1155 | - if ( $echo ) { |
|
| 1155 | + if ($echo) { |
|
| 1156 | 1156 | echo $label; |
| 1157 | 1157 | } else { |
| 1158 | 1158 | return $label; |
| 1159 | 1159 | } |
| 1160 | 1160 | } |
| 1161 | 1161 | |
| 1162 | -function wpinv_check_delete_discount( $check, $post ) { |
|
| 1163 | - if ( $post->post_type == 'wpi_discount' && wpinv_get_discount_uses( $post->ID ) > 0 ) { |
|
| 1162 | +function wpinv_check_delete_discount($check, $post) { |
|
| 1163 | + if ($post->post_type == 'wpi_discount' && wpinv_get_discount_uses($post->ID) > 0) { |
|
| 1164 | 1164 | return true; |
| 1165 | 1165 | } |
| 1166 | 1166 | |
| 1167 | 1167 | return $check; |
| 1168 | 1168 | } |
| 1169 | -add_filter( 'pre_delete_post', 'wpinv_check_delete_discount', 10, 2 ); |
|
| 1169 | +add_filter('pre_delete_post', 'wpinv_check_delete_discount', 10, 2); |
|
| 1170 | 1170 | |
| 1171 | 1171 | function wpinv_checkout_form_validate_discounts() { |
| 1172 | 1172 | global $wpi_checkout_id; |
| 1173 | 1173 | |
| 1174 | 1174 | $discounts = wpinv_get_cart_discounts(); |
| 1175 | 1175 | |
| 1176 | - if ( !empty( $discounts ) ) { |
|
| 1176 | + if (!empty($discounts)) { |
|
| 1177 | 1177 | $invalid = false; |
| 1178 | 1178 | |
| 1179 | - foreach ( $discounts as $key => $code ) { |
|
| 1180 | - if ( !wpinv_is_discount_valid( $code, (int)wpinv_get_user_id( $wpi_checkout_id ) ) ) { |
|
| 1179 | + foreach ($discounts as $key => $code) { |
|
| 1180 | + if (!wpinv_is_discount_valid($code, (int) wpinv_get_user_id($wpi_checkout_id))) { |
|
| 1181 | 1181 | $invalid = true; |
| 1182 | 1182 | |
| 1183 | - wpinv_unset_cart_discount( $code ); |
|
| 1183 | + wpinv_unset_cart_discount($code); |
|
| 1184 | 1184 | } |
| 1185 | 1185 | } |
| 1186 | 1186 | |
| 1187 | - if ( $invalid ) { |
|
| 1187 | + if ($invalid) { |
|
| 1188 | 1188 | $errors = wpinv_get_errors(); |
| 1189 | - $error = !empty( $errors['wpinv-discount-error'] ) ? $errors['wpinv-discount-error'] . ' ' : ''; |
|
| 1190 | - $error .= __( 'The discount has been removed from cart.', 'invoicing' ); |
|
| 1191 | - wpinv_set_error( 'wpinv-discount-error', $error ); |
|
| 1189 | + $error = !empty($errors['wpinv-discount-error']) ? $errors['wpinv-discount-error'] . ' ' : ''; |
|
| 1190 | + $error .= __('The discount has been removed from cart.', 'invoicing'); |
|
| 1191 | + wpinv_set_error('wpinv-discount-error', $error); |
|
| 1192 | 1192 | |
| 1193 | - wpinv_recalculate_tax( true ); |
|
| 1193 | + wpinv_recalculate_tax(true); |
|
| 1194 | 1194 | } |
| 1195 | 1195 | } |
| 1196 | 1196 | } |
| 1197 | -add_action( 'wpinv_before_checkout_form', 'wpinv_checkout_form_validate_discounts', -10 ); |
|
| 1197 | +add_action('wpinv_before_checkout_form', 'wpinv_checkout_form_validate_discounts', -10); |
|
| 1198 | 1198 | |
| 1199 | 1199 | function wpinv_discount_amount() { |
| 1200 | 1200 | $output = 0.00; |
| 1201 | 1201 | |
| 1202 | - return apply_filters( 'wpinv_discount_amount', $output ); |
|
| 1202 | + return apply_filters('wpinv_discount_amount', $output); |
|
| 1203 | 1203 | } |
| 1204 | 1204 | \ No newline at end of file |