@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | // Exit if accessed directly |
| 3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
| 3 | +if (!defined('ABSPATH')) exit; |
|
| 4 | 4 | |
| 5 | 5 | class WPInv_Item { |
| 6 | 6 | public $ID = 0; |
@@ -47,26 +47,26 @@ discard block |
||
| 47 | 47 | public $filter; |
| 48 | 48 | |
| 49 | 49 | |
| 50 | - public function __construct( $_id = false, $_args = array() ) { |
|
| 51 | - $item = WP_Post::get_instance( $_id ); |
|
| 52 | - return $this->setup_item( $item ); |
|
| 50 | + public function __construct($_id = false, $_args = array()) { |
|
| 51 | + $item = WP_Post::get_instance($_id); |
|
| 52 | + return $this->setup_item($item); |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - private function setup_item( $item ) { |
|
| 56 | - if( ! is_object( $item ) ) { |
|
| 55 | + private function setup_item($item) { |
|
| 56 | + if (!is_object($item)) { |
|
| 57 | 57 | return false; |
| 58 | 58 | } |
| 59 | 59 | |
| 60 | - if( ! is_a( $item, 'WP_Post' ) ) { |
|
| 60 | + if (!is_a($item, 'WP_Post')) { |
|
| 61 | 61 | return false; |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | - if( 'wpi_item' !== $item->post_type ) { |
|
| 64 | + if ('wpi_item' !== $item->post_type) { |
|
| 65 | 65 | return false; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | - foreach ( $item as $key => $value ) { |
|
| 69 | - switch ( $key ) { |
|
| 68 | + foreach ($item as $key => $value) { |
|
| 69 | + switch ($key) { |
|
| 70 | 70 | default: |
| 71 | 71 | $this->$key = $value; |
| 72 | 72 | break; |
@@ -76,38 +76,38 @@ discard block |
||
| 76 | 76 | return true; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | - public function __get( $key ) { |
|
| 80 | - if ( method_exists( $this, 'get_' . $key ) ) { |
|
| 81 | - return call_user_func( array( $this, 'get_' . $key ) ); |
|
| 79 | + public function __get($key) { |
|
| 80 | + if (method_exists($this, 'get_' . $key)) { |
|
| 81 | + return call_user_func(array($this, 'get_' . $key)); |
|
| 82 | 82 | } else { |
| 83 | - return new WP_Error( 'wpinv-item-invalid-property', sprintf( __( 'Can\'t get property %s', 'invoicing' ), $key ) ); |
|
| 83 | + return new WP_Error('wpinv-item-invalid-property', sprintf(__('Can\'t get property %s', 'invoicing'), $key)); |
|
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | |
| 87 | - public function create( $data = array(), $wp_error = false ) { |
|
| 88 | - if ( $this->ID != 0 ) { |
|
| 87 | + public function create($data = array(), $wp_error = false) { |
|
| 88 | + if ($this->ID != 0) { |
|
| 89 | 89 | return false; |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | $defaults = array( |
| 93 | 93 | 'post_type' => 'wpi_item', |
| 94 | 94 | 'post_status' => 'draft', |
| 95 | - 'post_title' => __( 'New Invoice Item', 'invoicing' ) |
|
| 95 | + 'post_title' => __('New Invoice Item', 'invoicing') |
|
| 96 | 96 | ); |
| 97 | 97 | |
| 98 | - $args = wp_parse_args( $data, $defaults ); |
|
| 98 | + $args = wp_parse_args($data, $defaults); |
|
| 99 | 99 | |
| 100 | - do_action( 'wpinv_item_pre_create', $args ); |
|
| 100 | + do_action('wpinv_item_pre_create', $args); |
|
| 101 | 101 | |
| 102 | - $id = wp_insert_post( $args, $wp_error ); |
|
| 102 | + $id = wp_insert_post($args, $wp_error); |
|
| 103 | 103 | if ($wp_error && is_wp_error($id)) { |
| 104 | 104 | return $id; |
| 105 | 105 | } |
| 106 | - if ( !$id ) { |
|
| 106 | + if (!$id) { |
|
| 107 | 107 | return false; |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - $item = WP_Post::get_instance( $id ); |
|
| 110 | + $item = WP_Post::get_instance($id); |
|
| 111 | 111 | |
| 112 | 112 | if (!empty($item) && !empty($data['meta'])) { |
| 113 | 113 | $this->ID = $item->ID; |
@@ -115,47 +115,47 @@ discard block |
||
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | // Set custom id if not set. |
| 118 | - if ( empty( $data['meta']['custom_id'] ) && !$this->get_custom_id() ) { |
|
| 119 | - $this->save_metas( array( 'custom_id' => $id ) ); |
|
| 118 | + if (empty($data['meta']['custom_id']) && !$this->get_custom_id()) { |
|
| 119 | + $this->save_metas(array('custom_id' => $id)); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | - do_action( 'wpinv_item_create', $id, $args ); |
|
| 122 | + do_action('wpinv_item_create', $id, $args); |
|
| 123 | 123 | |
| 124 | - return $this->setup_item( $item ); |
|
| 124 | + return $this->setup_item($item); |
|
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - public function update( $data = array(), $wp_error = false ) { |
|
| 128 | - if ( !$this->ID > 0 ) { |
|
| 127 | + public function update($data = array(), $wp_error = false) { |
|
| 128 | + if (!$this->ID > 0) { |
|
| 129 | 129 | return false; |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | $data['ID'] = $this->ID; |
| 133 | 133 | |
| 134 | - do_action( 'wpinv_item_pre_update', $data ); |
|
| 134 | + do_action('wpinv_item_pre_update', $data); |
|
| 135 | 135 | |
| 136 | - $id = wp_update_post( $data, $wp_error ); |
|
| 136 | + $id = wp_update_post($data, $wp_error); |
|
| 137 | 137 | if ($wp_error && is_wp_error($id)) { |
| 138 | 138 | return $id; |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | - if ( !$id ) { |
|
| 141 | + if (!$id) { |
|
| 142 | 142 | return false; |
| 143 | 143 | } |
| 144 | 144 | |
| 145 | - $item = WP_Post::get_instance( $id ); |
|
| 145 | + $item = WP_Post::get_instance($id); |
|
| 146 | 146 | if (!empty($item) && !empty($data['meta'])) { |
| 147 | 147 | $this->ID = $item->ID; |
| 148 | 148 | $this->save_metas($data['meta']); |
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | // Set custom id if not set. |
| 152 | - if ( empty( $data['meta']['custom_id'] ) && !$this->get_custom_id() ) { |
|
| 153 | - $this->save_metas( array( 'custom_id' => $id ) ); |
|
| 152 | + if (empty($data['meta']['custom_id']) && !$this->get_custom_id()) { |
|
| 153 | + $this->save_metas(array('custom_id' => $id)); |
|
| 154 | 154 | } |
| 155 | 155 | |
| 156 | - do_action( 'wpinv_item_update', $id, $data ); |
|
| 156 | + do_action('wpinv_item_update', $id, $data); |
|
| 157 | 157 | |
| 158 | - return $this->setup_item( $item ); |
|
| 158 | + return $this->setup_item($item); |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | public function get_ID() { |
@@ -163,119 +163,119 @@ discard block |
||
| 163 | 163 | } |
| 164 | 164 | |
| 165 | 165 | public function get_name() { |
| 166 | - return get_the_title( $this->ID ); |
|
| 166 | + return get_the_title($this->ID); |
|
| 167 | 167 | } |
| 168 | 168 | |
| 169 | 169 | public function get_title() { |
| 170 | - return get_the_title( $this->ID ); |
|
| 170 | + return get_the_title($this->ID); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | public function get_status() { |
| 174 | - return get_post_status( $this->ID ); |
|
| 174 | + return get_post_status($this->ID); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | public function get_summary() { |
| 178 | - return get_the_excerpt( $this->ID ); |
|
| 178 | + return get_the_excerpt($this->ID); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | public function get_price() { |
| 182 | - if ( ! isset( $this->price ) ) { |
|
| 183 | - $this->price = get_post_meta( $this->ID, '_wpinv_price', true ); |
|
| 182 | + if (!isset($this->price)) { |
|
| 183 | + $this->price = get_post_meta($this->ID, '_wpinv_price', true); |
|
| 184 | 184 | |
| 185 | - if ( $this->price ) { |
|
| 186 | - $this->price = wpinv_sanitize_amount( $this->price ); |
|
| 185 | + if ($this->price) { |
|
| 186 | + $this->price = wpinv_sanitize_amount($this->price); |
|
| 187 | 187 | } else { |
| 188 | 188 | $this->price = 0; |
| 189 | 189 | } |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | - return apply_filters( 'wpinv_get_item_price', $this->price, $this->ID ); |
|
| 192 | + return apply_filters('wpinv_get_item_price', $this->price, $this->ID); |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | public function get_vat_rule() { |
| 196 | 196 | global $wpinv_euvat; |
| 197 | 197 | |
| 198 | - if( !isset( $this->vat_rule ) ) { |
|
| 199 | - $this->vat_rule = get_post_meta( $this->ID, '_wpinv_vat_rule', true ); |
|
| 198 | + if (!isset($this->vat_rule)) { |
|
| 199 | + $this->vat_rule = get_post_meta($this->ID, '_wpinv_vat_rule', true); |
|
| 200 | 200 | |
| 201 | - if ( empty( $this->vat_rule ) ) { |
|
| 201 | + if (empty($this->vat_rule)) { |
|
| 202 | 202 | $this->vat_rule = $wpinv_euvat->allow_vat_rules() ? 'digital' : 'physical'; |
| 203 | 203 | } |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - return apply_filters( 'wpinv_get_item_vat_rule', $this->vat_rule, $this->ID ); |
|
| 206 | + return apply_filters('wpinv_get_item_vat_rule', $this->vat_rule, $this->ID); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | public function get_vat_class() { |
| 210 | - if( !isset( $this->vat_class ) ) { |
|
| 211 | - $this->vat_class = get_post_meta( $this->ID, '_wpinv_vat_class', true ); |
|
| 210 | + if (!isset($this->vat_class)) { |
|
| 211 | + $this->vat_class = get_post_meta($this->ID, '_wpinv_vat_class', true); |
|
| 212 | 212 | |
| 213 | - if ( empty( $this->vat_class ) ) { |
|
| 213 | + if (empty($this->vat_class)) { |
|
| 214 | 214 | $this->vat_class = '_standard'; |
| 215 | 215 | } |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | - return apply_filters( 'wpinv_get_item_vat_class', $this->vat_class, $this->ID ); |
|
| 218 | + return apply_filters('wpinv_get_item_vat_class', $this->vat_class, $this->ID); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | public function get_type() { |
| 222 | - if( ! isset( $this->type ) ) { |
|
| 223 | - $this->type = get_post_meta( $this->ID, '_wpinv_type', true ); |
|
| 222 | + if (!isset($this->type)) { |
|
| 223 | + $this->type = get_post_meta($this->ID, '_wpinv_type', true); |
|
| 224 | 224 | |
| 225 | - if ( empty( $this->type ) ) { |
|
| 225 | + if (empty($this->type)) { |
|
| 226 | 226 | $this->type = 'custom'; |
| 227 | 227 | } |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | - return apply_filters( 'wpinv_get_item_type', $this->type, $this->ID ); |
|
| 230 | + return apply_filters('wpinv_get_item_type', $this->type, $this->ID); |
|
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | public function get_custom_id() { |
| 234 | - $custom_id = get_post_meta( $this->ID, '_wpinv_custom_id', true ); |
|
| 234 | + $custom_id = get_post_meta($this->ID, '_wpinv_custom_id', true); |
|
| 235 | 235 | |
| 236 | - return apply_filters( 'wpinv_get_item_custom_id', $custom_id, $this->ID ); |
|
| 236 | + return apply_filters('wpinv_get_item_custom_id', $custom_id, $this->ID); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | 239 | public function get_custom_name() { |
| 240 | - $custom_name = get_post_meta( $this->ID, '_wpinv_custom_name', true ); |
|
| 240 | + $custom_name = get_post_meta($this->ID, '_wpinv_custom_name', true); |
|
| 241 | 241 | |
| 242 | - return apply_filters( 'wpinv_get_item_custom_name', $custom_name, $this->ID ); |
|
| 242 | + return apply_filters('wpinv_get_item_custom_name', $custom_name, $this->ID); |
|
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | public function get_custom_singular_name() { |
| 246 | - $custom_singular_name = get_post_meta( $this->ID, '_wpinv_custom_singular_name', true ); |
|
| 246 | + $custom_singular_name = get_post_meta($this->ID, '_wpinv_custom_singular_name', true); |
|
| 247 | 247 | |
| 248 | - return apply_filters( 'wpinv_get_item_custom_singular_name', $custom_singular_name, $this->ID ); |
|
| 248 | + return apply_filters('wpinv_get_item_custom_singular_name', $custom_singular_name, $this->ID); |
|
| 249 | 249 | } |
| 250 | 250 | |
| 251 | 251 | public function get_editable() { |
| 252 | - $editable = get_post_meta( $this->ID, '_wpinv_editable', true ); |
|
| 252 | + $editable = get_post_meta($this->ID, '_wpinv_editable', true); |
|
| 253 | 253 | |
| 254 | - return apply_filters( 'wpinv_item_get_editable', $editable, $this->ID ); |
|
| 254 | + return apply_filters('wpinv_item_get_editable', $editable, $this->ID); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | 257 | public function get_excerpt() { |
| 258 | - $excerpt = get_the_excerpt( $this->ID ); |
|
| 258 | + $excerpt = get_the_excerpt($this->ID); |
|
| 259 | 259 | |
| 260 | - return apply_filters( 'wpinv_item_get_excerpt', $excerpt, $this->ID ); |
|
| 260 | + return apply_filters('wpinv_item_get_excerpt', $excerpt, $this->ID); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | public function get_is_recurring() { |
| 264 | - $is_recurring = get_post_meta( $this->ID, '_wpinv_is_recurring', true ); |
|
| 264 | + $is_recurring = get_post_meta($this->ID, '_wpinv_is_recurring', true); |
|
| 265 | 265 | |
| 266 | - return apply_filters( 'wpinv_item_get_is_recurring', $is_recurring, $this->ID ); |
|
| 266 | + return apply_filters('wpinv_item_get_is_recurring', $is_recurring, $this->ID); |
|
| 267 | 267 | |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | - public function get_recurring_period( $full = false ) { |
|
| 271 | - $period = get_post_meta( $this->ID, '_wpinv_recurring_period', true ); |
|
| 270 | + public function get_recurring_period($full = false) { |
|
| 271 | + $period = get_post_meta($this->ID, '_wpinv_recurring_period', true); |
|
| 272 | 272 | |
| 273 | - if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
|
| 273 | + if (!in_array($period, array('D', 'W', 'M', 'Y'))) { |
|
| 274 | 274 | $period = 'D'; |
| 275 | 275 | } |
| 276 | 276 | |
| 277 | - if ( $full ) { |
|
| 278 | - switch( $period ) { |
|
| 277 | + if ($full) { |
|
| 278 | + switch ($period) { |
|
| 279 | 279 | case 'D': |
| 280 | 280 | $period = 'day'; |
| 281 | 281 | break; |
@@ -291,40 +291,40 @@ discard block |
||
| 291 | 291 | } |
| 292 | 292 | } |
| 293 | 293 | |
| 294 | - return apply_filters( 'wpinv_item_recurring_period', $period, $full, $this->ID ); |
|
| 294 | + return apply_filters('wpinv_item_recurring_period', $period, $full, $this->ID); |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | public function get_recurring_interval() { |
| 298 | - $interval = (int)get_post_meta( $this->ID, '_wpinv_recurring_interval', true ); |
|
| 298 | + $interval = (int)get_post_meta($this->ID, '_wpinv_recurring_interval', true); |
|
| 299 | 299 | |
| 300 | - if ( !$interval > 0 ) { |
|
| 300 | + if (!$interval > 0) { |
|
| 301 | 301 | $interval = 1; |
| 302 | 302 | } |
| 303 | 303 | |
| 304 | - return apply_filters( 'wpinv_item_recurring_interval', $interval, $this->ID ); |
|
| 304 | + return apply_filters('wpinv_item_recurring_interval', $interval, $this->ID); |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | public function get_recurring_limit() { |
| 308 | - $limit = get_post_meta( $this->ID, '_wpinv_recurring_limit', true ); |
|
| 308 | + $limit = get_post_meta($this->ID, '_wpinv_recurring_limit', true); |
|
| 309 | 309 | |
| 310 | - return (int)apply_filters( 'wpinv_item_recurring_limit', $limit, $this->ID ); |
|
| 310 | + return (int)apply_filters('wpinv_item_recurring_limit', $limit, $this->ID); |
|
| 311 | 311 | } |
| 312 | 312 | |
| 313 | 313 | public function get_free_trial() { |
| 314 | - $free_trial = get_post_meta( $this->ID, '_wpinv_free_trial', true ); |
|
| 314 | + $free_trial = get_post_meta($this->ID, '_wpinv_free_trial', true); |
|
| 315 | 315 | |
| 316 | - return apply_filters( 'wpinv_item_get_free_trial', $free_trial, $this->ID ); |
|
| 316 | + return apply_filters('wpinv_item_get_free_trial', $free_trial, $this->ID); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | - public function get_trial_period( $full = false ) { |
|
| 320 | - $period = get_post_meta( $this->ID, '_wpinv_trial_period', true ); |
|
| 319 | + public function get_trial_period($full = false) { |
|
| 320 | + $period = get_post_meta($this->ID, '_wpinv_trial_period', true); |
|
| 321 | 321 | |
| 322 | - if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
|
| 322 | + if (!in_array($period, array('D', 'W', 'M', 'Y'))) { |
|
| 323 | 323 | $period = 'D'; |
| 324 | 324 | } |
| 325 | 325 | |
| 326 | - if ( $full ) { |
|
| 327 | - switch( $period ) { |
|
| 326 | + if ($full) { |
|
| 327 | + switch ($period) { |
|
| 328 | 328 | case 'D': |
| 329 | 329 | $period = 'day'; |
| 330 | 330 | break; |
@@ -340,54 +340,54 @@ discard block |
||
| 340 | 340 | } |
| 341 | 341 | } |
| 342 | 342 | |
| 343 | - return apply_filters( 'wpinv_item_trial_period', $period, $full, $this->ID ); |
|
| 343 | + return apply_filters('wpinv_item_trial_period', $period, $full, $this->ID); |
|
| 344 | 344 | } |
| 345 | 345 | |
| 346 | 346 | public function get_trial_interval() { |
| 347 | - $interval = absint( get_post_meta( $this->ID, '_wpinv_trial_interval', true ) ); |
|
| 347 | + $interval = absint(get_post_meta($this->ID, '_wpinv_trial_interval', true)); |
|
| 348 | 348 | |
| 349 | - if ( !$interval > 0 ) { |
|
| 349 | + if (!$interval > 0) { |
|
| 350 | 350 | $interval = 1; |
| 351 | 351 | } |
| 352 | 352 | |
| 353 | - return apply_filters( 'wpinv_item_trial_interval', $interval, $this->ID ); |
|
| 353 | + return apply_filters('wpinv_item_trial_interval', $interval, $this->ID); |
|
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | public function get_the_price() { |
| 357 | - $item_price = wpinv_price( wpinv_format_amount( $this->price ) ); |
|
| 357 | + $item_price = wpinv_price(wpinv_format_amount($this->price)); |
|
| 358 | 358 | |
| 359 | - return apply_filters( 'wpinv_get_the_item_price', $item_price, $this->ID ); |
|
| 359 | + return apply_filters('wpinv_get_the_item_price', $item_price, $this->ID); |
|
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | public function is_recurring() { |
| 363 | 363 | $is_recurring = $this->get_is_recurring(); |
| 364 | 364 | |
| 365 | - return (bool)apply_filters( 'wpinv_is_recurring_item', $is_recurring, $this->ID ); |
|
| 365 | + return (bool)apply_filters('wpinv_is_recurring_item', $is_recurring, $this->ID); |
|
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | public function has_free_trial() { |
| 369 | 369 | $free_trial = $this->is_recurring() && $this->get_free_trial() ? true : false; |
| 370 | 370 | |
| 371 | - return (bool)apply_filters( 'wpinv_item_has_free_trial', $free_trial, $this->ID ); |
|
| 371 | + return (bool)apply_filters('wpinv_item_has_free_trial', $free_trial, $this->ID); |
|
| 372 | 372 | } |
| 373 | 373 | |
| 374 | 374 | public function is_free() { |
| 375 | 375 | $is_free = false; |
| 376 | 376 | |
| 377 | - $price = get_post_meta( $this->ID, '_wpinv_price', true ); |
|
| 377 | + $price = get_post_meta($this->ID, '_wpinv_price', true); |
|
| 378 | 378 | |
| 379 | - if ( (float)$price == 0 ) { |
|
| 379 | + if ((float)$price == 0) { |
|
| 380 | 380 | $is_free = true; |
| 381 | 381 | } |
| 382 | 382 | |
| 383 | - return (bool) apply_filters( 'wpinv_is_free_item', $is_free, $this->ID ); |
|
| 383 | + return (bool)apply_filters('wpinv_is_free_item', $is_free, $this->ID); |
|
| 384 | 384 | |
| 385 | 385 | } |
| 386 | 386 | |
| 387 | 387 | public function is_package() { |
| 388 | 388 | $is_package = $this->get_type() == 'package' ? true : false; |
| 389 | 389 | |
| 390 | - return (bool) apply_filters( 'wpinv_is_package_item', $is_package, $this->ID ); |
|
| 390 | + return (bool)apply_filters('wpinv_is_package_item', $is_package, $this->ID); |
|
| 391 | 391 | |
| 392 | 392 | } |
| 393 | 393 | |
@@ -396,15 +396,15 @@ discard block |
||
| 396 | 396 | |
| 397 | 397 | $is_editable = $editable === 0 || $editable === '0' ? false : true; |
| 398 | 398 | |
| 399 | - return (bool) apply_filters( 'wpinv_item_is_editable', $is_editable, $this->ID ); |
|
| 399 | + return (bool)apply_filters('wpinv_item_is_editable', $is_editable, $this->ID); |
|
| 400 | 400 | } |
| 401 | 401 | |
| 402 | - public function save_metas( $metas = array() ) { |
|
| 403 | - if ( empty( $metas ) ) { |
|
| 402 | + public function save_metas($metas = array()) { |
|
| 403 | + if (empty($metas)) { |
|
| 404 | 404 | return false; |
| 405 | 405 | } |
| 406 | 406 | |
| 407 | - foreach ( $metas as $meta_key => $meta_value ) { |
|
| 407 | + foreach ($metas as $meta_key => $meta_value) { |
|
| 408 | 408 | $meta_key = strpos($meta_key, '_wpinv_') !== 0 ? '_wpinv_' . $meta_key : $meta_key; |
| 409 | 409 | |
| 410 | 410 | $this->update_meta($meta_key, $meta_value); |
@@ -413,66 +413,66 @@ discard block |
||
| 413 | 413 | return true; |
| 414 | 414 | } |
| 415 | 415 | |
| 416 | - public function update_meta( $meta_key = '', $meta_value = '', $prev_value = '' ) { |
|
| 417 | - if ( empty( $meta_key ) ) { |
|
| 416 | + public function update_meta($meta_key = '', $meta_value = '', $prev_value = '') { |
|
| 417 | + if (empty($meta_key)) { |
|
| 418 | 418 | return false; |
| 419 | 419 | } |
| 420 | 420 | |
| 421 | - $meta_value = apply_filters( 'wpinv_update_item_meta_' . $meta_key, $meta_value, $this->ID ); |
|
| 421 | + $meta_value = apply_filters('wpinv_update_item_meta_' . $meta_key, $meta_value, $this->ID); |
|
| 422 | 422 | |
| 423 | - return update_post_meta( $this->ID, $meta_key, $meta_value, $prev_value ); |
|
| 423 | + return update_post_meta($this->ID, $meta_key, $meta_value, $prev_value); |
|
| 424 | 424 | } |
| 425 | 425 | |
| 426 | - public function get_fees( $type = 'fee', $item_id = 0 ) { |
|
| 426 | + public function get_fees($type = 'fee', $item_id = 0) { |
|
| 427 | 427 | global $wpi_session; |
| 428 | 428 | |
| 429 | - $fees = $wpi_session->get( 'wpi_cart_fees' ); |
|
| 429 | + $fees = $wpi_session->get('wpi_cart_fees'); |
|
| 430 | 430 | |
| 431 | - if ( ! wpinv_get_cart_contents() ) { |
|
| 431 | + if (!wpinv_get_cart_contents()) { |
|
| 432 | 432 | // We can only get item type fees when the cart is empty |
| 433 | 433 | $type = 'custom'; |
| 434 | 434 | } |
| 435 | 435 | |
| 436 | - if ( ! empty( $fees ) && ! empty( $type ) && 'all' !== $type ) { |
|
| 437 | - foreach( $fees as $key => $fee ) { |
|
| 438 | - if( ! empty( $fee['type'] ) && $type != $fee['type'] ) { |
|
| 439 | - unset( $fees[ $key ] ); |
|
| 436 | + if (!empty($fees) && !empty($type) && 'all' !== $type) { |
|
| 437 | + foreach ($fees as $key => $fee) { |
|
| 438 | + if (!empty($fee['type']) && $type != $fee['type']) { |
|
| 439 | + unset($fees[$key]); |
|
| 440 | 440 | } |
| 441 | 441 | } |
| 442 | 442 | } |
| 443 | 443 | |
| 444 | - if ( ! empty( $fees ) && ! empty( $item_id ) ) { |
|
| 444 | + if (!empty($fees) && !empty($item_id)) { |
|
| 445 | 445 | // Remove fees that don't belong to the specified Item |
| 446 | - foreach ( $fees as $key => $fee ) { |
|
| 447 | - if ( (int) $item_id !== (int)$fee['custom_id'] ) { |
|
| 448 | - unset( $fees[ $key ] ); |
|
| 446 | + foreach ($fees as $key => $fee) { |
|
| 447 | + if ((int)$item_id !== (int)$fee['custom_id']) { |
|
| 448 | + unset($fees[$key]); |
|
| 449 | 449 | } |
| 450 | 450 | } |
| 451 | 451 | } |
| 452 | 452 | |
| 453 | - if ( ! empty( $fees ) ) { |
|
| 453 | + if (!empty($fees)) { |
|
| 454 | 454 | // Remove fees that belong to a specific item but are not in the cart |
| 455 | - foreach( $fees as $key => $fee ) { |
|
| 456 | - if( empty( $fee['custom_id'] ) ) { |
|
| 455 | + foreach ($fees as $key => $fee) { |
|
| 456 | + if (empty($fee['custom_id'])) { |
|
| 457 | 457 | continue; |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | - if ( !wpinv_item_in_cart( $fee['custom_id'] ) ) { |
|
| 461 | - unset( $fees[ $key ] ); |
|
| 460 | + if (!wpinv_item_in_cart($fee['custom_id'])) { |
|
| 461 | + unset($fees[$key]); |
|
| 462 | 462 | } |
| 463 | 463 | } |
| 464 | 464 | } |
| 465 | 465 | |
| 466 | - return ! empty( $fees ) ? $fees : array(); |
|
| 466 | + return !empty($fees) ? $fees : array(); |
|
| 467 | 467 | } |
| 468 | 468 | |
| 469 | 469 | public function can_purchase() { |
| 470 | 470 | $can_purchase = true; |
| 471 | 471 | |
| 472 | - if ( !current_user_can( 'edit_post', $this->ID ) && $this->post_status != 'publish' ) { |
|
| 472 | + if (!current_user_can('edit_post', $this->ID) && $this->post_status != 'publish') { |
|
| 473 | 473 | $can_purchase = false; |
| 474 | 474 | } |
| 475 | 475 | |
| 476 | - return (bool)apply_filters( 'wpinv_can_purchase_item', $can_purchase, $this ); |
|
| 476 | + return (bool)apply_filters('wpinv_can_purchase_item', $can_purchase, $this); |
|
| 477 | 477 | } |
| 478 | 478 | } |
@@ -1,19 +1,19 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | // Exit if accessed directly |
| 3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
| 3 | +if (!defined('ABSPATH')) exit; |
|
| 4 | 4 | |
| 5 | -function wpinv_get_item_by( $field = '', $value = '', $type = '' ) { |
|
| 6 | - if( empty( $field ) || empty( $value ) ) { |
|
| 5 | +function wpinv_get_item_by($field = '', $value = '', $type = '') { |
|
| 6 | + if (empty($field) || empty($value)) { |
|
| 7 | 7 | return false; |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | $posts = array(); |
| 11 | 11 | |
| 12 | - switch( strtolower( $field ) ) { |
|
| 12 | + switch (strtolower($field)) { |
|
| 13 | 13 | case 'id': |
| 14 | - $item = new WPInv_Item( $value ); |
|
| 14 | + $item = new WPInv_Item($value); |
|
| 15 | 15 | |
| 16 | - if ( !empty( $item ) && $item->post_type == 'wpi_item' ) { |
|
| 16 | + if (!empty($item) && $item->post_type == 'wpi_item') { |
|
| 17 | 17 | return $item; |
| 18 | 18 | } |
| 19 | 19 | return false; |
@@ -22,16 +22,16 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | case 'slug': |
| 24 | 24 | case 'name': |
| 25 | - $posts = get_posts( array( |
|
| 25 | + $posts = get_posts(array( |
|
| 26 | 26 | 'post_type' => 'wpi_item', |
| 27 | 27 | 'name' => $value, |
| 28 | 28 | 'posts_per_page' => 1, |
| 29 | 29 | 'post_status' => 'any' |
| 30 | - ) ); |
|
| 30 | + )); |
|
| 31 | 31 | |
| 32 | 32 | break; |
| 33 | 33 | case 'custom_id': |
| 34 | - if ( empty( $value ) || empty( $type ) ) { |
|
| 34 | + if (empty($value) || empty($type)) { |
|
| 35 | 35 | return false; |
| 36 | 36 | } |
| 37 | 37 | |
@@ -51,10 +51,10 @@ discard block |
||
| 51 | 51 | 'post_status' => 'any', |
| 52 | 52 | 'orderby' => 'ID', |
| 53 | 53 | 'order' => 'ASC', |
| 54 | - 'meta_query' => array( $meta_query ) |
|
| 54 | + 'meta_query' => array($meta_query) |
|
| 55 | 55 | ); |
| 56 | 56 | |
| 57 | - $posts = get_posts( $args ); |
|
| 57 | + $posts = get_posts($args); |
|
| 58 | 58 | |
| 59 | 59 | break; |
| 60 | 60 | |
@@ -62,10 +62,10 @@ discard block |
||
| 62 | 62 | return false; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - if ( !empty( $posts[0] ) ) { |
|
| 66 | - $item = new WPInv_Item( $posts[0]->ID ); |
|
| 65 | + if (!empty($posts[0])) { |
|
| 66 | + $item = new WPInv_Item($posts[0]->ID); |
|
| 67 | 67 | |
| 68 | - if ( !empty( $item ) && $item->post_type == 'wpi_item' ) { |
|
| 68 | + if (!empty($item) && $item->post_type == 'wpi_item') { |
|
| 69 | 69 | return $item; |
| 70 | 70 | } |
| 71 | 71 | } |
@@ -73,10 +73,10 @@ discard block |
||
| 73 | 73 | return false; |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | -function wpinv_get_item( $item = 0 ) { |
|
| 77 | - if ( is_numeric( $item ) ) { |
|
| 78 | - $item = get_post( $item ); |
|
| 79 | - if ( ! $item || 'wpi_item' !== $item->post_type ) |
|
| 76 | +function wpinv_get_item($item = 0) { |
|
| 77 | + if (is_numeric($item)) { |
|
| 78 | + $item = get_post($item); |
|
| 79 | + if (!$item || 'wpi_item' !== $item->post_type) |
|
| 80 | 80 | return null; |
| 81 | 81 | return $item; |
| 82 | 82 | } |
@@ -89,150 +89,150 @@ discard block |
||
| 89 | 89 | |
| 90 | 90 | $item = get_posts($args); |
| 91 | 91 | |
| 92 | - if ( $item ) { |
|
| 92 | + if ($item) { |
|
| 93 | 93 | return $item[0]; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 96 | return null; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | -function wpinv_is_free_item( $item_id = 0 ) { |
|
| 100 | - if( empty( $item_id ) ) { |
|
| 99 | +function wpinv_is_free_item($item_id = 0) { |
|
| 100 | + if (empty($item_id)) { |
|
| 101 | 101 | return false; |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - $item = new WPInv_Item( $item_id ); |
|
| 104 | + $item = new WPInv_Item($item_id); |
|
| 105 | 105 | |
| 106 | 106 | return $item->is_free(); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | -function wpinv_item_is_editable( $item = 0 ) { |
|
| 110 | - if ( !empty( $item ) && is_a( $item, 'WP_Post' ) ) { |
|
| 109 | +function wpinv_item_is_editable($item = 0) { |
|
| 110 | + if (!empty($item) && is_a($item, 'WP_Post')) { |
|
| 111 | 111 | $item = $item->ID; |
| 112 | 112 | } |
| 113 | 113 | |
| 114 | - if ( empty( $item ) ) { |
|
| 114 | + if (empty($item)) { |
|
| 115 | 115 | return true; |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | - $item = new WPInv_Item( $item ); |
|
| 118 | + $item = new WPInv_Item($item); |
|
| 119 | 119 | |
| 120 | - return (bool) $item->is_editable(); |
|
| 120 | + return (bool)$item->is_editable(); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | -function wpinv_get_item_price( $item_id = 0 ) { |
|
| 124 | - if( empty( $item_id ) ) { |
|
| 123 | +function wpinv_get_item_price($item_id = 0) { |
|
| 124 | + if (empty($item_id)) { |
|
| 125 | 125 | return false; |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | - $item = new WPInv_Item( $item_id ); |
|
| 128 | + $item = new WPInv_Item($item_id); |
|
| 129 | 129 | |
| 130 | 130 | return $item->get_price(); |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | -function wpinv_is_recurring_item( $item_id = 0 ) { |
|
| 134 | - if( empty( $item_id ) ) { |
|
| 133 | +function wpinv_is_recurring_item($item_id = 0) { |
|
| 134 | + if (empty($item_id)) { |
|
| 135 | 135 | return false; |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - $item = new WPInv_Item( $item_id ); |
|
| 138 | + $item = new WPInv_Item($item_id); |
|
| 139 | 139 | |
| 140 | 140 | return $item->is_recurring(); |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | -function wpinv_item_price( $item_id = 0 ) { |
|
| 144 | - if( empty( $item_id ) ) { |
|
| 143 | +function wpinv_item_price($item_id = 0) { |
|
| 144 | + if (empty($item_id)) { |
|
| 145 | 145 | return false; |
| 146 | 146 | } |
| 147 | 147 | |
| 148 | - $price = wpinv_get_item_price( $item_id ); |
|
| 149 | - $price = wpinv_price( wpinv_format_amount( $price ) ); |
|
| 148 | + $price = wpinv_get_item_price($item_id); |
|
| 149 | + $price = wpinv_price(wpinv_format_amount($price)); |
|
| 150 | 150 | |
| 151 | - return apply_filters( 'wpinv_item_price', $price, $item_id ); |
|
| 151 | + return apply_filters('wpinv_item_price', $price, $item_id); |
|
| 152 | 152 | } |
| 153 | 153 | |
| 154 | -function wpinv_item_show_price( $item_id = 0, $echo = true ) { |
|
| 155 | - if ( empty( $item_id ) ) { |
|
| 154 | +function wpinv_item_show_price($item_id = 0, $echo = true) { |
|
| 155 | + if (empty($item_id)) { |
|
| 156 | 156 | $item_id = get_the_ID(); |
| 157 | 157 | } |
| 158 | 158 | |
| 159 | - $price = wpinv_item_price( $item_id ); |
|
| 159 | + $price = wpinv_item_price($item_id); |
|
| 160 | 160 | |
| 161 | - $price = apply_filters( 'wpinv_item_price', wpinv_sanitize_amount( $price ), $item_id ); |
|
| 161 | + $price = apply_filters('wpinv_item_price', wpinv_sanitize_amount($price), $item_id); |
|
| 162 | 162 | $formatted_price = '<span class="wpinv_price" id="wpinv_item_' . $item_id . '">' . $price . '</span>'; |
| 163 | - $formatted_price = apply_filters( 'wpinv_item_price_after_html', $formatted_price, $item_id, $price ); |
|
| 163 | + $formatted_price = apply_filters('wpinv_item_price_after_html', $formatted_price, $item_id, $price); |
|
| 164 | 164 | |
| 165 | - if ( $echo ) { |
|
| 165 | + if ($echo) { |
|
| 166 | 166 | echo $formatted_price; |
| 167 | 167 | } else { |
| 168 | 168 | return $formatted_price; |
| 169 | 169 | } |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | -function wpinv_get_item_final_price( $item_id = 0, $amount_override = null ) { |
|
| 173 | - if ( is_null( $amount_override ) ) { |
|
| 174 | - $original_price = get_post_meta( $item_id, '_wpinv_price', true ); |
|
| 172 | +function wpinv_get_item_final_price($item_id = 0, $amount_override = null) { |
|
| 173 | + if (is_null($amount_override)) { |
|
| 174 | + $original_price = get_post_meta($item_id, '_wpinv_price', true); |
|
| 175 | 175 | } else { |
| 176 | 176 | $original_price = $amount_override; |
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | $price = $original_price; |
| 180 | 180 | |
| 181 | - return apply_filters( 'wpinv_get_item_final_price', $price, $item_id ); |
|
| 181 | + return apply_filters('wpinv_get_item_final_price', $price, $item_id); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | -function wpinv_item_custom_singular_name( $item_id ) { |
|
| 185 | - if( empty( $item_id ) ) { |
|
| 184 | +function wpinv_item_custom_singular_name($item_id) { |
|
| 185 | + if (empty($item_id)) { |
|
| 186 | 186 | return false; |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - $item = new WPInv_Item( $item_id ); |
|
| 189 | + $item = new WPInv_Item($item_id); |
|
| 190 | 190 | |
| 191 | 191 | return $item->get_custom_singular_name(); |
| 192 | 192 | } |
| 193 | 193 | |
| 194 | 194 | function wpinv_get_item_types() { |
| 195 | 195 | $item_types = array( |
| 196 | - 'custom' => __( 'Standard', 'invoicing' ), |
|
| 197 | - 'fee' => __( 'Fee', 'invoicing' ), |
|
| 196 | + 'custom' => __('Standard', 'invoicing'), |
|
| 197 | + 'fee' => __('Fee', 'invoicing'), |
|
| 198 | 198 | ); |
| 199 | - return apply_filters( 'wpinv_get_item_types', $item_types ); |
|
| 199 | + return apply_filters('wpinv_get_item_types', $item_types); |
|
| 200 | 200 | } |
| 201 | 201 | |
| 202 | 202 | function wpinv_item_types() { |
| 203 | 203 | $item_types = wpinv_get_item_types(); |
| 204 | 204 | |
| 205 | - return ( !empty( $item_types ) ? array_keys( $item_types ) : array() ); |
|
| 205 | + return (!empty($item_types) ? array_keys($item_types) : array()); |
|
| 206 | 206 | } |
| 207 | 207 | |
| 208 | -function wpinv_get_item_type( $item_id ) { |
|
| 209 | - if( empty( $item_id ) ) { |
|
| 208 | +function wpinv_get_item_type($item_id) { |
|
| 209 | + if (empty($item_id)) { |
|
| 210 | 210 | return false; |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | - $item = new WPInv_Item( $item_id ); |
|
| 213 | + $item = new WPInv_Item($item_id); |
|
| 214 | 214 | |
| 215 | 215 | return $item->get_type(); |
| 216 | 216 | } |
| 217 | 217 | |
| 218 | -function wpinv_item_type( $item_id ) { |
|
| 218 | +function wpinv_item_type($item_id) { |
|
| 219 | 219 | $item_types = wpinv_get_item_types(); |
| 220 | 220 | |
| 221 | - $item_type = wpinv_get_item_type( $item_id ); |
|
| 221 | + $item_type = wpinv_get_item_type($item_id); |
|
| 222 | 222 | |
| 223 | - if ( empty( $item_type ) ) { |
|
| 223 | + if (empty($item_type)) { |
|
| 224 | 224 | $item_type = '-'; |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | - $item_type = isset( $item_types[$item_type] ) ? $item_types[$item_type] : __( $item_type, 'invoicing' ); |
|
| 227 | + $item_type = isset($item_types[$item_type]) ? $item_types[$item_type] : __($item_type, 'invoicing'); |
|
| 228 | 228 | |
| 229 | - return apply_filters( 'wpinv_item_type', $item_type, $item_id ); |
|
| 229 | + return apply_filters('wpinv_item_type', $item_type, $item_id); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | -function wpinv_record_item_in_log( $item_id = 0, $file_id, $user_info, $ip, $invoice_id ) { |
|
| 232 | +function wpinv_record_item_in_log($item_id = 0, $file_id, $user_info, $ip, $invoice_id) { |
|
| 233 | 233 | global $wpinv_logs; |
| 234 | 234 | |
| 235 | - if ( empty( $wpinv_logs ) ) { |
|
| 235 | + if (empty($wpinv_logs)) { |
|
| 236 | 236 | return false; |
| 237 | 237 | } |
| 238 | 238 | |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | 'log_type' => 'wpi_item' |
| 242 | 242 | ); |
| 243 | 243 | |
| 244 | - $user_id = isset( $user_info['user_id'] ) ? $user_info['user_id'] : (int) -1; |
|
| 244 | + $user_id = isset($user_info['user_id']) ? $user_info['user_id'] : (int) -1; |
|
| 245 | 245 | |
| 246 | 246 | $log_meta = array( |
| 247 | 247 | 'user_info' => $user_info, |
@@ -251,253 +251,253 @@ discard block |
||
| 251 | 251 | 'invoice_id'=> $invoice_id, |
| 252 | 252 | ); |
| 253 | 253 | |
| 254 | - $wpinv_logs->insert_log( $log_data, $log_meta ); |
|
| 254 | + $wpinv_logs->insert_log($log_data, $log_meta); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | -function wpinv_remove_item_logs_on_delete( $item_id = 0 ) { |
|
| 258 | - if ( 'wpi_item' !== get_post_type( $item_id ) ) |
|
| 257 | +function wpinv_remove_item_logs_on_delete($item_id = 0) { |
|
| 258 | + if ('wpi_item' !== get_post_type($item_id)) |
|
| 259 | 259 | return; |
| 260 | 260 | |
| 261 | 261 | global $wpinv_logs; |
| 262 | 262 | |
| 263 | - if ( empty( $wpinv_logs ) ) { |
|
| 263 | + if (empty($wpinv_logs)) { |
|
| 264 | 264 | return false; |
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | // Remove all log entries related to this item |
| 268 | - $wpinv_logs->delete_logs( $item_id ); |
|
| 268 | + $wpinv_logs->delete_logs($item_id); |
|
| 269 | 269 | } |
| 270 | -add_action( 'delete_post', 'wpinv_remove_item_logs_on_delete' ); |
|
| 270 | +add_action('delete_post', 'wpinv_remove_item_logs_on_delete'); |
|
| 271 | 271 | |
| 272 | -function wpinv_get_random_item( $post_ids = true ) { |
|
| 273 | - wpinv_get_random_items( 1, $post_ids ); |
|
| 272 | +function wpinv_get_random_item($post_ids = true) { |
|
| 273 | + wpinv_get_random_items(1, $post_ids); |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | -function wpinv_get_random_items( $num = 3, $post_ids = true ) { |
|
| 277 | - if ( $post_ids ) { |
|
| 278 | - $args = array( 'post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num, 'fields' => 'ids' ); |
|
| 276 | +function wpinv_get_random_items($num = 3, $post_ids = true) { |
|
| 277 | + if ($post_ids) { |
|
| 278 | + $args = array('post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num, 'fields' => 'ids'); |
|
| 279 | 279 | } else { |
| 280 | - $args = array( 'post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num ); |
|
| 280 | + $args = array('post_type' => 'wpi_item', 'orderby' => 'rand', 'post_count' => $num); |
|
| 281 | 281 | } |
| 282 | 282 | |
| 283 | - $args = apply_filters( 'wpinv_get_random_items', $args ); |
|
| 283 | + $args = apply_filters('wpinv_get_random_items', $args); |
|
| 284 | 284 | |
| 285 | - return get_posts( $args ); |
|
| 285 | + return get_posts($args); |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | -function wpinv_get_item_token( $url = '' ) { |
|
| 288 | +function wpinv_get_item_token($url = '') { |
|
| 289 | 289 | $args = array(); |
| 290 | - $hash = apply_filters( 'wpinv_get_url_token_algorithm', 'sha256' ); |
|
| 291 | - $secret = apply_filters( 'wpinv_get_url_token_secret', hash( $hash, wp_salt() ) ); |
|
| 290 | + $hash = apply_filters('wpinv_get_url_token_algorithm', 'sha256'); |
|
| 291 | + $secret = apply_filters('wpinv_get_url_token_secret', hash($hash, wp_salt())); |
|
| 292 | 292 | |
| 293 | - $parts = parse_url( $url ); |
|
| 293 | + $parts = parse_url($url); |
|
| 294 | 294 | $options = array(); |
| 295 | 295 | |
| 296 | - if ( isset( $parts['query'] ) ) { |
|
| 297 | - wp_parse_str( $parts['query'], $query_args ); |
|
| 296 | + if (isset($parts['query'])) { |
|
| 297 | + wp_parse_str($parts['query'], $query_args); |
|
| 298 | 298 | |
| 299 | - if ( ! empty( $query_args['o'] ) ) { |
|
| 300 | - $options = explode( ':', rawurldecode( $query_args['o'] ) ); |
|
| 299 | + if (!empty($query_args['o'])) { |
|
| 300 | + $options = explode(':', rawurldecode($query_args['o'])); |
|
| 301 | 301 | |
| 302 | - if ( in_array( 'ip', $options ) ) { |
|
| 302 | + if (in_array('ip', $options)) { |
|
| 303 | 303 | $args['ip'] = wpinv_get_ip(); |
| 304 | 304 | } |
| 305 | 305 | |
| 306 | - if ( in_array( 'ua', $options ) ) { |
|
| 306 | + if (in_array('ua', $options)) { |
|
| 307 | 307 | $ua = wpinv_get_user_agent(); |
| 308 | - $args['user_agent'] = rawurlencode( $ua ); |
|
| 308 | + $args['user_agent'] = rawurlencode($ua); |
|
| 309 | 309 | } |
| 310 | 310 | } |
| 311 | 311 | } |
| 312 | 312 | |
| 313 | - $args = apply_filters( 'wpinv_get_url_token_args', $args, $url, $options ); |
|
| 313 | + $args = apply_filters('wpinv_get_url_token_args', $args, $url, $options); |
|
| 314 | 314 | |
| 315 | 315 | $args['secret'] = $secret; |
| 316 | 316 | $args['token'] = false; |
| 317 | 317 | |
| 318 | - $url = add_query_arg( $args, $url ); |
|
| 319 | - $parts = parse_url( $url ); |
|
| 318 | + $url = add_query_arg($args, $url); |
|
| 319 | + $parts = parse_url($url); |
|
| 320 | 320 | |
| 321 | - if ( ! isset( $parts['path'] ) ) { |
|
| 321 | + if (!isset($parts['path'])) { |
|
| 322 | 322 | $parts['path'] = ''; |
| 323 | 323 | } |
| 324 | 324 | |
| 325 | - $token = md5( $parts['path'] . '?' . $parts['query'] ); |
|
| 325 | + $token = md5($parts['path'] . '?' . $parts['query']); |
|
| 326 | 326 | |
| 327 | 327 | return $token; |
| 328 | 328 | } |
| 329 | 329 | |
| 330 | -function wpinv_validate_url_token( $url = '' ) { |
|
| 330 | +function wpinv_validate_url_token($url = '') { |
|
| 331 | 331 | $ret = false; |
| 332 | - $parts = parse_url( $url ); |
|
| 332 | + $parts = parse_url($url); |
|
| 333 | 333 | |
| 334 | - if ( isset( $parts['query'] ) ) { |
|
| 335 | - wp_parse_str( $parts['query'], $query_args ); |
|
| 334 | + if (isset($parts['query'])) { |
|
| 335 | + wp_parse_str($parts['query'], $query_args); |
|
| 336 | 336 | |
| 337 | - $allowed = apply_filters( 'wpinv_url_token_allowed_params', array( |
|
| 337 | + $allowed = apply_filters('wpinv_url_token_allowed_params', array( |
|
| 338 | 338 | 'item', |
| 339 | 339 | 'ttl', |
| 340 | 340 | 'token' |
| 341 | - ) ); |
|
| 341 | + )); |
|
| 342 | 342 | |
| 343 | 343 | $remove = array(); |
| 344 | 344 | |
| 345 | - foreach( $query_args as $key => $value ) { |
|
| 346 | - if( false === in_array( $key, $allowed ) ) { |
|
| 345 | + foreach ($query_args as $key => $value) { |
|
| 346 | + if (false === in_array($key, $allowed)) { |
|
| 347 | 347 | $remove[] = $key; |
| 348 | 348 | } |
| 349 | 349 | } |
| 350 | 350 | |
| 351 | - if( ! empty( $remove ) ) { |
|
| 352 | - $url = remove_query_arg( $remove, $url ); |
|
| 351 | + if (!empty($remove)) { |
|
| 352 | + $url = remove_query_arg($remove, $url); |
|
| 353 | 353 | } |
| 354 | 354 | |
| 355 | - if ( isset( $query_args['ttl'] ) && current_time( 'timestamp' ) > $query_args['ttl'] ) { |
|
| 356 | - wp_die( apply_filters( 'wpinv_item_link_expired_text', __( 'Sorry but your item link has expired.', 'invoicing' ) ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
| 355 | + if (isset($query_args['ttl']) && current_time('timestamp') > $query_args['ttl']) { |
|
| 356 | + wp_die(apply_filters('wpinv_item_link_expired_text', __('Sorry but your item link has expired.', 'invoicing')), __('Error', 'invoicing'), array('response' => 403)); |
|
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - if ( isset( $query_args['token'] ) && $query_args['token'] == wpinv_get_item_token( $url ) ) { |
|
| 359 | + if (isset($query_args['token']) && $query_args['token'] == wpinv_get_item_token($url)) { |
|
| 360 | 360 | $ret = true; |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | 363 | } |
| 364 | 364 | |
| 365 | - return apply_filters( 'wpinv_validate_url_token', $ret, $url, $query_args ); |
|
| 365 | + return apply_filters('wpinv_validate_url_token', $ret, $url, $query_args); |
|
| 366 | 366 | } |
| 367 | 367 | |
| 368 | -function wpinv_item_in_cart( $item_id = 0, $options = array() ) { |
|
| 368 | +function wpinv_item_in_cart($item_id = 0, $options = array()) { |
|
| 369 | 369 | $cart_items = wpinv_get_cart_contents(); |
| 370 | 370 | |
| 371 | 371 | $ret = false; |
| 372 | 372 | |
| 373 | - if ( is_array( $cart_items ) ) { |
|
| 374 | - foreach ( $cart_items as $item ) { |
|
| 375 | - if ( $item['id'] == $item_id ) { |
|
| 373 | + if (is_array($cart_items)) { |
|
| 374 | + foreach ($cart_items as $item) { |
|
| 375 | + if ($item['id'] == $item_id) { |
|
| 376 | 376 | $ret = true; |
| 377 | 377 | break; |
| 378 | 378 | } |
| 379 | 379 | } |
| 380 | 380 | } |
| 381 | 381 | |
| 382 | - return (bool) apply_filters( 'wpinv_item_in_cart', $ret, $item_id, $options ); |
|
| 382 | + return (bool)apply_filters('wpinv_item_in_cart', $ret, $item_id, $options); |
|
| 383 | 383 | } |
| 384 | 384 | |
| 385 | -function wpinv_get_cart_item_tax( $item_id = 0, $subtotal = '', $options = array() ) { |
|
| 385 | +function wpinv_get_cart_item_tax($item_id = 0, $subtotal = '', $options = array()) { |
|
| 386 | 386 | $tax = 0; |
| 387 | - if ( ! wpinv_item_is_tax_exclusive( $item_id ) ) { |
|
| 388 | - $country = !empty( $_POST['country'] ) ? $_POST['country'] : false; |
|
| 389 | - $state = isset( $_POST['state'] ) ? $_POST['state'] : ''; |
|
| 387 | + if (!wpinv_item_is_tax_exclusive($item_id)) { |
|
| 388 | + $country = !empty($_POST['country']) ? $_POST['country'] : false; |
|
| 389 | + $state = isset($_POST['state']) ? $_POST['state'] : ''; |
|
| 390 | 390 | |
| 391 | - $tax = wpinv_calculate_tax( $subtotal, $country, $state, $item_id ); |
|
| 391 | + $tax = wpinv_calculate_tax($subtotal, $country, $state, $item_id); |
|
| 392 | 392 | } |
| 393 | 393 | |
| 394 | - return apply_filters( 'wpinv_get_cart_item_tax', $tax, $item_id, $subtotal, $options ); |
|
| 394 | + return apply_filters('wpinv_get_cart_item_tax', $tax, $item_id, $subtotal, $options); |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | -function wpinv_cart_item_price( $item ) { |
|
| 397 | +function wpinv_cart_item_price($item) { |
|
| 398 | 398 | $use_taxes = wpinv_use_taxes(); |
| 399 | - $item_id = isset( $item['id'] ) ? $item['id'] : 0; |
|
| 400 | - $price = isset( $item['item_price'] ) ? wpinv_round_amount( $item['item_price'] ) : 0; |
|
| 401 | - $options = isset( $item['options'] ) ? $item['options'] : array(); |
|
| 402 | - $price_id = isset( $options['price_id'] ) ? $options['price_id'] : false; |
|
| 403 | - $tax = wpinv_price( wpinv_format_amount( $item['tax'] ) ); |
|
| 404 | - |
|
| 405 | - if ( !wpinv_is_free_item( $item_id, $price_id ) && !wpinv_item_is_tax_exclusive( $item_id ) ) { |
|
| 406 | - if ( wpinv_prices_show_tax_on_checkout() && !wpinv_prices_include_tax() ) { |
|
| 399 | + $item_id = isset($item['id']) ? $item['id'] : 0; |
|
| 400 | + $price = isset($item['item_price']) ? wpinv_round_amount($item['item_price']) : 0; |
|
| 401 | + $options = isset($item['options']) ? $item['options'] : array(); |
|
| 402 | + $price_id = isset($options['price_id']) ? $options['price_id'] : false; |
|
| 403 | + $tax = wpinv_price(wpinv_format_amount($item['tax'])); |
|
| 404 | + |
|
| 405 | + if (!wpinv_is_free_item($item_id, $price_id) && !wpinv_item_is_tax_exclusive($item_id)) { |
|
| 406 | + if (wpinv_prices_show_tax_on_checkout() && !wpinv_prices_include_tax()) { |
|
| 407 | 407 | $price += $tax; |
| 408 | 408 | } |
| 409 | 409 | |
| 410 | - if( !wpinv_prices_show_tax_on_checkout() && wpinv_prices_include_tax() ) { |
|
| 410 | + if (!wpinv_prices_show_tax_on_checkout() && wpinv_prices_include_tax()) { |
|
| 411 | 411 | $price -= $tax; |
| 412 | 412 | } |
| 413 | 413 | } |
| 414 | 414 | |
| 415 | - $price = wpinv_price( wpinv_format_amount( $price ) ); |
|
| 415 | + $price = wpinv_price(wpinv_format_amount($price)); |
|
| 416 | 416 | |
| 417 | - return apply_filters( 'wpinv_cart_item_price_label', $price, $item ); |
|
| 417 | + return apply_filters('wpinv_cart_item_price_label', $price, $item); |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | -function wpinv_cart_item_subtotal( $item ) { |
|
| 421 | - $subtotal = isset( $item['subtotal'] ) ? $item['subtotal'] : 0; |
|
| 422 | - $subtotal = wpinv_price( wpinv_format_amount( $subtotal ) ); |
|
| 420 | +function wpinv_cart_item_subtotal($item) { |
|
| 421 | + $subtotal = isset($item['subtotal']) ? $item['subtotal'] : 0; |
|
| 422 | + $subtotal = wpinv_price(wpinv_format_amount($subtotal)); |
|
| 423 | 423 | |
| 424 | - return apply_filters( 'wpinv_cart_item_subtotal_label', $subtotal, $item ); |
|
| 424 | + return apply_filters('wpinv_cart_item_subtotal_label', $subtotal, $item); |
|
| 425 | 425 | } |
| 426 | 426 | |
| 427 | -function wpinv_cart_item_tax( $item ) { |
|
| 427 | +function wpinv_cart_item_tax($item) { |
|
| 428 | 428 | $tax = ''; |
| 429 | 429 | $tax_rate = ''; |
| 430 | 430 | |
| 431 | - if ( isset( $item['tax'] ) && $item['tax'] > 0 && $item['subtotal'] > 0 ) { |
|
| 432 | - $tax = wpinv_price( wpinv_format_amount( $item['tax'] ) ); |
|
| 433 | - $tax_rate = !empty( $item['vat_rate'] ) ? $item['vat_rate'] : ( $item['tax'] / $item['subtotal'] ) * 100; |
|
| 434 | - $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount( $tax_rate, 4 ) : ''; |
|
| 431 | + if (isset($item['tax']) && $item['tax'] > 0 && $item['subtotal'] > 0) { |
|
| 432 | + $tax = wpinv_price(wpinv_format_amount($item['tax'])); |
|
| 433 | + $tax_rate = !empty($item['vat_rate']) ? $item['vat_rate'] : ($item['tax'] / $item['subtotal']) * 100; |
|
| 434 | + $tax_rate = $tax_rate > 0 ? (float)wpinv_round_amount($tax_rate, 4) : ''; |
|
| 435 | 435 | $tax_rate = $tax_rate != '' ? ' <small class="tax-rate normal small">(' . $tax_rate . '%)</small>' : ''; |
| 436 | 436 | } |
| 437 | 437 | |
| 438 | - $tax = $tax . $tax_rate; |
|
| 438 | + $tax = $tax . $tax_rate; |
|
| 439 | 439 | |
| 440 | - if ( $tax === '' ) { |
|
| 440 | + if ($tax === '') { |
|
| 441 | 441 | $tax = 0; // Zero tax |
| 442 | 442 | } |
| 443 | 443 | |
| 444 | - return apply_filters( 'wpinv_cart_item_tax_label', $tax, $item ); |
|
| 444 | + return apply_filters('wpinv_cart_item_tax_label', $tax, $item); |
|
| 445 | 445 | } |
| 446 | 446 | |
| 447 | -function wpinv_get_cart_item_price( $item_id = 0, $cart_item = array(), $options = array(), $remove_tax_from_inclusive = false ) { |
|
| 447 | +function wpinv_get_cart_item_price($item_id = 0, $cart_item = array(), $options = array(), $remove_tax_from_inclusive = false) { |
|
| 448 | 448 | $price = 0; |
| 449 | 449 | |
| 450 | 450 | // Set custom price |
| 451 | - if ( isset( $cart_item['custom_price'] ) && $cart_item['custom_price'] !== '' ) { |
|
| 451 | + if (isset($cart_item['custom_price']) && $cart_item['custom_price'] !== '') { |
|
| 452 | 452 | $price = $cart_item['custom_price']; |
| 453 | 453 | } else { |
| 454 | - $variable_prices = wpinv_has_variable_prices( $item_id ); |
|
| 454 | + $variable_prices = wpinv_has_variable_prices($item_id); |
|
| 455 | 455 | |
| 456 | - if ( $variable_prices ) { |
|
| 457 | - $prices = wpinv_get_variable_prices( $item_id ); |
|
| 456 | + if ($variable_prices) { |
|
| 457 | + $prices = wpinv_get_variable_prices($item_id); |
|
| 458 | 458 | |
| 459 | - if ( $prices ) { |
|
| 460 | - if( ! empty( $options ) ) { |
|
| 461 | - $price = isset( $prices[ $options['price_id'] ] ) ? $prices[ $options['price_id'] ]['amount'] : false; |
|
| 459 | + if ($prices) { |
|
| 460 | + if (!empty($options)) { |
|
| 461 | + $price = isset($prices[$options['price_id']]) ? $prices[$options['price_id']]['amount'] : false; |
|
| 462 | 462 | } else { |
| 463 | 463 | $price = false; |
| 464 | 464 | } |
| 465 | 465 | } |
| 466 | 466 | } |
| 467 | 467 | |
| 468 | - if( ! $variable_prices || false === $price ) { |
|
| 468 | + if (!$variable_prices || false === $price) { |
|
| 469 | 469 | // Get the standard Item price if not using variable prices |
| 470 | - $price = wpinv_get_item_price( $item_id ); |
|
| 470 | + $price = wpinv_get_item_price($item_id); |
|
| 471 | 471 | } |
| 472 | 472 | } |
| 473 | 473 | |
| 474 | - if ( $remove_tax_from_inclusive && wpinv_prices_include_tax() ) { |
|
| 475 | - $price -= wpinv_get_cart_item_tax( $item_id, $price, $options ); |
|
| 474 | + if ($remove_tax_from_inclusive && wpinv_prices_include_tax()) { |
|
| 475 | + $price -= wpinv_get_cart_item_tax($item_id, $price, $options); |
|
| 476 | 476 | } |
| 477 | 477 | |
| 478 | - return apply_filters( 'wpinv_cart_item_price', $price, $item_id, $cart_item, $options, $remove_tax_from_inclusive ); |
|
| 478 | + return apply_filters('wpinv_cart_item_price', $price, $item_id, $cart_item, $options, $remove_tax_from_inclusive); |
|
| 479 | 479 | } |
| 480 | 480 | |
| 481 | -function wpinv_get_cart_item_price_id( $item = array() ) { |
|
| 482 | - if( isset( $item['item_number'] ) ) { |
|
| 483 | - $price_id = isset( $item['item_number']['options']['price_id'] ) ? $item['item_number']['options']['price_id'] : null; |
|
| 481 | +function wpinv_get_cart_item_price_id($item = array()) { |
|
| 482 | + if (isset($item['item_number'])) { |
|
| 483 | + $price_id = isset($item['item_number']['options']['price_id']) ? $item['item_number']['options']['price_id'] : null; |
|
| 484 | 484 | } else { |
| 485 | - $price_id = isset( $item['options']['price_id'] ) ? $item['options']['price_id'] : null; |
|
| 485 | + $price_id = isset($item['options']['price_id']) ? $item['options']['price_id'] : null; |
|
| 486 | 486 | } |
| 487 | 487 | return $price_id; |
| 488 | 488 | } |
| 489 | 489 | |
| 490 | -function wpinv_get_cart_item_price_name( $item = array() ) { |
|
| 491 | - $price_id = (int)wpinv_get_cart_item_price_id( $item ); |
|
| 492 | - $prices = wpinv_get_variable_prices( $item['id'] ); |
|
| 493 | - $name = ! empty( $prices[ $price_id ] ) ? $prices[ $price_id ]['name'] : ''; |
|
| 494 | - return apply_filters( 'wpinv_get_cart_item_price_name', $name, $item['id'], $price_id, $item ); |
|
| 490 | +function wpinv_get_cart_item_price_name($item = array()) { |
|
| 491 | + $price_id = (int)wpinv_get_cart_item_price_id($item); |
|
| 492 | + $prices = wpinv_get_variable_prices($item['id']); |
|
| 493 | + $name = !empty($prices[$price_id]) ? $prices[$price_id]['name'] : ''; |
|
| 494 | + return apply_filters('wpinv_get_cart_item_price_name', $name, $item['id'], $price_id, $item); |
|
| 495 | 495 | } |
| 496 | 496 | |
| 497 | -function wpinv_get_cart_item_name( $item = array() ) { |
|
| 498 | - $item_title = !empty( $item['name'] ) ? $item['name'] : get_the_title( $item['id'] ); |
|
| 497 | +function wpinv_get_cart_item_name($item = array()) { |
|
| 498 | + $item_title = !empty($item['name']) ? $item['name'] : get_the_title($item['id']); |
|
| 499 | 499 | |
| 500 | - if ( empty( $item_title ) ) { |
|
| 500 | + if (empty($item_title)) { |
|
| 501 | 501 | $item_title = $item['id']; |
| 502 | 502 | } |
| 503 | 503 | |
@@ -507,23 +507,23 @@ discard block |
||
| 507 | 507 | } |
| 508 | 508 | */ |
| 509 | 509 | |
| 510 | - return apply_filters( 'wpinv_get_cart_item_name', $item_title, $item['id'], $item ); |
|
| 510 | + return apply_filters('wpinv_get_cart_item_name', $item_title, $item['id'], $item); |
|
| 511 | 511 | } |
| 512 | 512 | |
| 513 | -function wpinv_has_variable_prices( $item_id = 0 ) { |
|
| 513 | +function wpinv_has_variable_prices($item_id = 0) { |
|
| 514 | 514 | return false; |
| 515 | 515 | } |
| 516 | 516 | |
| 517 | -function wpinv_get_item_position_in_cart( $item_id = 0, $options = array() ) { |
|
| 517 | +function wpinv_get_item_position_in_cart($item_id = 0, $options = array()) { |
|
| 518 | 518 | $cart_items = wpinv_get_cart_contents(); |
| 519 | 519 | |
| 520 | - if ( !is_array( $cart_items ) ) { |
|
| 520 | + if (!is_array($cart_items)) { |
|
| 521 | 521 | return false; // Empty cart |
| 522 | 522 | } else { |
| 523 | - foreach ( $cart_items as $position => $item ) { |
|
| 524 | - if ( $item['id'] == $item_id ) { |
|
| 525 | - if ( isset( $options['price_id'] ) && isset( $item['options']['price_id'] ) ) { |
|
| 526 | - if ( (int) $options['price_id'] == (int) $item['options']['price_id'] ) { |
|
| 523 | + foreach ($cart_items as $position => $item) { |
|
| 524 | + if ($item['id'] == $item_id) { |
|
| 525 | + if (isset($options['price_id']) && isset($item['options']['price_id'])) { |
|
| 526 | + if ((int)$options['price_id'] == (int)$item['options']['price_id']) { |
|
| 527 | 527 | return $position; |
| 528 | 528 | } |
| 529 | 529 | } else { |
@@ -536,80 +536,80 @@ discard block |
||
| 536 | 536 | return false; // Not found |
| 537 | 537 | } |
| 538 | 538 | |
| 539 | -function wpinv_get_cart_item_quantity( $item ) { |
|
| 540 | - if ( wpinv_item_quantities_enabled() ) { |
|
| 541 | - $quantity = !empty( $item['quantity'] ) && (int)$item['quantity'] > 0 ? absint( $item['quantity'] ) : 1; |
|
| 539 | +function wpinv_get_cart_item_quantity($item) { |
|
| 540 | + if (wpinv_item_quantities_enabled()) { |
|
| 541 | + $quantity = !empty($item['quantity']) && (int)$item['quantity'] > 0 ? absint($item['quantity']) : 1; |
|
| 542 | 542 | } else { |
| 543 | 543 | $quantity = 1; |
| 544 | 544 | } |
| 545 | 545 | |
| 546 | - if ( $quantity < 1 ) { |
|
| 546 | + if ($quantity < 1) { |
|
| 547 | 547 | $quantity = 1; |
| 548 | 548 | } |
| 549 | 549 | |
| 550 | - return apply_filters( 'wpinv_get_cart_item_quantity', $quantity, $item ); |
|
| 550 | + return apply_filters('wpinv_get_cart_item_quantity', $quantity, $item); |
|
| 551 | 551 | } |
| 552 | 552 | |
| 553 | -function wpinv_get_item_suffix( $item, $html = true ) { |
|
| 554 | - if ( empty( $item ) ) { |
|
| 553 | +function wpinv_get_item_suffix($item, $html = true) { |
|
| 554 | + if (empty($item)) { |
|
| 555 | 555 | return NULL; |
| 556 | 556 | } |
| 557 | 557 | |
| 558 | - if ( is_int( $item ) ) { |
|
| 559 | - $item = new WPInv_Item( $item ); |
|
| 558 | + if (is_int($item)) { |
|
| 559 | + $item = new WPInv_Item($item); |
|
| 560 | 560 | } |
| 561 | 561 | |
| 562 | - if ( !( is_object( $item ) && is_a( $item, 'WPInv_Item' ) ) ) { |
|
| 562 | + if (!(is_object($item) && is_a($item, 'WPInv_Item'))) { |
|
| 563 | 563 | return NULL; |
| 564 | 564 | } |
| 565 | 565 | |
| 566 | - $suffix = $item->is_recurring() ? ' <span class="wpi-suffix">' . __( '(r)', 'invoicing' ) . '</span>' : ''; |
|
| 566 | + $suffix = $item->is_recurring() ? ' <span class="wpi-suffix">' . __('(r)', 'invoicing') . '</span>' : ''; |
|
| 567 | 567 | |
| 568 | - if ( !$html && $suffix ) { |
|
| 569 | - $suffix = strip_tags( $suffix ); |
|
| 568 | + if (!$html && $suffix) { |
|
| 569 | + $suffix = strip_tags($suffix); |
|
| 570 | 570 | } |
| 571 | 571 | |
| 572 | - return apply_filters( 'wpinv_get_item_suffix', $suffix, $item, $html ); |
|
| 572 | + return apply_filters('wpinv_get_item_suffix', $suffix, $item, $html); |
|
| 573 | 573 | } |
| 574 | 574 | |
| 575 | -function wpinv_remove_item( $item = 0, $force_delete = false ) { |
|
| 576 | - if ( empty( $item ) ) { |
|
| 575 | +function wpinv_remove_item($item = 0, $force_delete = false) { |
|
| 576 | + if (empty($item)) { |
|
| 577 | 577 | return NULL; |
| 578 | 578 | } |
| 579 | 579 | |
| 580 | - if ( is_int( $item ) ) { |
|
| 581 | - $item = new WPInv_Item( $item ); |
|
| 580 | + if (is_int($item)) { |
|
| 581 | + $item = new WPInv_Item($item); |
|
| 582 | 582 | } |
| 583 | 583 | |
| 584 | - if ( !( is_object( $item ) && is_a( $item, 'WPInv_Item' ) ) ) { |
|
| 584 | + if (!(is_object($item) && is_a($item, 'WPInv_Item'))) { |
|
| 585 | 585 | return NULL; |
| 586 | 586 | } |
| 587 | 587 | |
| 588 | - do_action( 'wpinv_pre_delete_item', $item ); |
|
| 588 | + do_action('wpinv_pre_delete_item', $item); |
|
| 589 | 589 | |
| 590 | - wp_delete_post( $item->ID, $force_delete ); |
|
| 590 | + wp_delete_post($item->ID, $force_delete); |
|
| 591 | 591 | |
| 592 | - do_action( 'wpinv_post_delete_item', $item ); |
|
| 592 | + do_action('wpinv_post_delete_item', $item); |
|
| 593 | 593 | } |
| 594 | 594 | |
| 595 | -function wpinv_can_delete_item( $post_id ) { |
|
| 596 | - $return = current_user_can( 'manage_options' ) ? true : false; |
|
| 595 | +function wpinv_can_delete_item($post_id) { |
|
| 596 | + $return = current_user_can('manage_options') ? true : false; |
|
| 597 | 597 | |
| 598 | - if ( $return && wpinv_item_in_use( $post_id ) ) { |
|
| 598 | + if ($return && wpinv_item_in_use($post_id)) { |
|
| 599 | 599 | $return = false; // Don't delete item already use in invoices. |
| 600 | 600 | } |
| 601 | 601 | |
| 602 | - return apply_filters( 'wpinv_can_delete_item', $return, $post_id ); |
|
| 602 | + return apply_filters('wpinv_can_delete_item', $return, $post_id); |
|
| 603 | 603 | } |
| 604 | 604 | |
| 605 | 605 | function wpinv_admin_action_delete() { |
| 606 | 606 | $screen = get_current_screen(); |
| 607 | 607 | |
| 608 | - if ( !empty( $screen->post_type ) && $screen->post_type == 'wpi_item' && !empty( $_REQUEST['post'] ) && is_array( $_REQUEST['post'] ) ) { |
|
| 608 | + if (!empty($screen->post_type) && $screen->post_type == 'wpi_item' && !empty($_REQUEST['post']) && is_array($_REQUEST['post'])) { |
|
| 609 | 609 | $post_ids = array(); |
| 610 | 610 | |
| 611 | - foreach ( $_REQUEST['post'] as $post_id ) { |
|
| 612 | - if ( !wpinv_can_delete_item( $post_id ) ) { |
|
| 611 | + foreach ($_REQUEST['post'] as $post_id) { |
|
| 612 | + if (!wpinv_can_delete_item($post_id)) { |
|
| 613 | 613 | continue; |
| 614 | 614 | } |
| 615 | 615 | |
@@ -619,86 +619,86 @@ discard block |
||
| 619 | 619 | $_REQUEST['post'] = $post_ids; |
| 620 | 620 | } |
| 621 | 621 | } |
| 622 | -add_action( 'admin_action_trash', 'wpinv_admin_action_delete', -10 ); |
|
| 623 | -add_action( 'admin_action_delete', 'wpinv_admin_action_delete', -10 ); |
|
| 622 | +add_action('admin_action_trash', 'wpinv_admin_action_delete', -10); |
|
| 623 | +add_action('admin_action_delete', 'wpinv_admin_action_delete', -10); |
|
| 624 | 624 | |
| 625 | -function wpinv_check_delete_item( $check, $post, $force_delete ) { |
|
| 626 | - if ( $post->post_type == 'wpi_item' ) { |
|
| 627 | - if ( $force_delete && !wpinv_can_delete_item( $post->ID ) ) { |
|
| 625 | +function wpinv_check_delete_item($check, $post, $force_delete) { |
|
| 626 | + if ($post->post_type == 'wpi_item') { |
|
| 627 | + if ($force_delete && !wpinv_can_delete_item($post->ID)) { |
|
| 628 | 628 | return true; |
| 629 | 629 | } |
| 630 | 630 | } |
| 631 | 631 | |
| 632 | 632 | return $check; |
| 633 | 633 | } |
| 634 | -add_filter( 'pre_delete_post', 'wpinv_check_delete_item', 10, 3 ); |
|
| 634 | +add_filter('pre_delete_post', 'wpinv_check_delete_item', 10, 3); |
|
| 635 | 635 | |
| 636 | -function wpinv_item_in_use( $item_id ) { |
|
| 636 | +function wpinv_item_in_use($item_id) { |
|
| 637 | 637 | global $wpdb, $wpi_items_in_use; |
| 638 | 638 | |
| 639 | - if ( !$item_id > 0 ) { |
|
| 639 | + if (!$item_id > 0) { |
|
| 640 | 640 | return false; |
| 641 | 641 | } |
| 642 | 642 | |
| 643 | - if ( !empty( $wpi_items_in_use ) ) { |
|
| 644 | - if ( isset( $wpi_items_in_use[$item_id] ) ) { |
|
| 643 | + if (!empty($wpi_items_in_use)) { |
|
| 644 | + if (isset($wpi_items_in_use[$item_id])) { |
|
| 645 | 645 | return $wpi_items_in_use[$item_id]; |
| 646 | 646 | } |
| 647 | 647 | } else { |
| 648 | 648 | $wpi_items_in_use = array(); |
| 649 | 649 | } |
| 650 | 650 | |
| 651 | - $statuses = array_keys( wpinv_get_invoice_statuses( true ) ); |
|
| 651 | + $statuses = array_keys(wpinv_get_invoice_statuses(true)); |
|
| 652 | 652 | |
| 653 | - $query = "SELECT p.ID FROM " . $wpdb->posts . " AS p INNER JOIN " . $wpdb->postmeta . " AS pm ON p.ID = pm.post_id WHERE p.post_type = 'wpi_invoice' AND p.post_status IN( '" . implode( "','", $statuses ) . "' ) AND pm.meta_key = '_wpinv_item_ids' AND FIND_IN_SET( '" . (int)$item_id . "', pm.meta_value )"; |
|
| 654 | - $in_use = $wpdb->get_var( $query ) > 0 ? true : false; |
|
| 653 | + $query = "SELECT p.ID FROM " . $wpdb->posts . " AS p INNER JOIN " . $wpdb->postmeta . " AS pm ON p.ID = pm.post_id WHERE p.post_type = 'wpi_invoice' AND p.post_status IN( '" . implode("','", $statuses) . "' ) AND pm.meta_key = '_wpinv_item_ids' AND FIND_IN_SET( '" . (int)$item_id . "', pm.meta_value )"; |
|
| 654 | + $in_use = $wpdb->get_var($query) > 0 ? true : false; |
|
| 655 | 655 | |
| 656 | 656 | $wpi_items_in_use[$item_id] = $in_use; |
| 657 | 657 | |
| 658 | 658 | return $in_use; |
| 659 | 659 | } |
| 660 | 660 | |
| 661 | -function wpinv_create_item( $args = array(), $wp_error = false, $force_update = false ) { |
|
| 661 | +function wpinv_create_item($args = array(), $wp_error = false, $force_update = false) { |
|
| 662 | 662 | // Set some defaults |
| 663 | 663 | $defaults = array( |
| 664 | - 'type' => 'custom', // Optional. Item type. Default 'custom'. |
|
| 665 | - 'title' => '', // Required. Item title. |
|
| 666 | - 'custom_id' => 0, // Optional. Any integer or non numeric id. Must be unique within item type. |
|
| 667 | - 'price' => '0.00', // Optional. Item price. Default '0.00'. |
|
| 668 | - 'status' => 'pending', // Optional. pending, publish |
|
| 669 | - 'custom_name' => '', // Optional. Plural sub title for item. |
|
| 670 | - 'custom_singular_name' => '', // Optional. Singular sub title for item. |
|
| 671 | - 'vat_rule' => 'digital', // Optional. digital => Digital item, physical => Physical item |
|
| 672 | - 'editable' => true, // Optional. Item editable from Items list page? Default true. |
|
| 673 | - 'excerpt' => '', // Optional. Item short description |
|
| 664 | + 'type' => 'custom', // Optional. Item type. Default 'custom'. |
|
| 665 | + 'title' => '', // Required. Item title. |
|
| 666 | + 'custom_id' => 0, // Optional. Any integer or non numeric id. Must be unique within item type. |
|
| 667 | + 'price' => '0.00', // Optional. Item price. Default '0.00'. |
|
| 668 | + 'status' => 'pending', // Optional. pending, publish |
|
| 669 | + 'custom_name' => '', // Optional. Plural sub title for item. |
|
| 670 | + 'custom_singular_name' => '', // Optional. Singular sub title for item. |
|
| 671 | + 'vat_rule' => 'digital', // Optional. digital => Digital item, physical => Physical item |
|
| 672 | + 'editable' => true, // Optional. Item editable from Items list page? Default true. |
|
| 673 | + 'excerpt' => '', // Optional. Item short description |
|
| 674 | 674 | /* Recurring item fields */ |
| 675 | - 'is_recurring' => 0, // Optional. 1 => Allow recurring or 0 => Don't allow recurring |
|
| 676 | - 'recurring_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
| 677 | - 'recurring_interval' => 0, // Optional. Integer value between 1 - 90. |
|
| 678 | - 'recurring_limit' => 0, // Optional. Any integer number. 0 for recurring forever until cancelled. |
|
| 679 | - 'free_trial' => 0, // Optional. 1 => Allow free trial or 0 => Don't free trial |
|
| 680 | - 'trial_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
| 681 | - 'trial_interval' => 0, // Optional. Any integer number. |
|
| 675 | + 'is_recurring' => 0, // Optional. 1 => Allow recurring or 0 => Don't allow recurring |
|
| 676 | + 'recurring_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
| 677 | + 'recurring_interval' => 0, // Optional. Integer value between 1 - 90. |
|
| 678 | + 'recurring_limit' => 0, // Optional. Any integer number. 0 for recurring forever until cancelled. |
|
| 679 | + 'free_trial' => 0, // Optional. 1 => Allow free trial or 0 => Don't free trial |
|
| 680 | + 'trial_period' => 'M', // Optional. D => Daily, W => Weekly, M => Monthly, Y => Yearly |
|
| 681 | + 'trial_interval' => 0, // Optional. Any integer number. |
|
| 682 | 682 | ); |
| 683 | 683 | |
| 684 | - $data = wp_parse_args( $args, $defaults ); |
|
| 684 | + $data = wp_parse_args($args, $defaults); |
|
| 685 | 685 | |
| 686 | - if ( empty( $data['type'] ) ) { |
|
| 686 | + if (empty($data['type'])) { |
|
| 687 | 687 | $data['type'] = 'custom'; |
| 688 | 688 | } |
| 689 | 689 | |
| 690 | - if ( !empty( $data['custom_id'] ) ) { |
|
| 691 | - $item = wpinv_get_item_by( 'custom_id', $data['custom_id'], $data['type'] ); |
|
| 690 | + if (!empty($data['custom_id'])) { |
|
| 691 | + $item = wpinv_get_item_by('custom_id', $data['custom_id'], $data['type']); |
|
| 692 | 692 | } else { |
| 693 | 693 | $item = NULL; |
| 694 | 694 | } |
| 695 | 695 | |
| 696 | - if ( !empty( $item ) ) { |
|
| 697 | - if ( $force_update ) { |
|
| 698 | - if ( empty( $args['ID'] ) ) { |
|
| 696 | + if (!empty($item)) { |
|
| 697 | + if ($force_update) { |
|
| 698 | + if (empty($args['ID'])) { |
|
| 699 | 699 | $args['ID'] = $item->ID; |
| 700 | 700 | } |
| 701 | - return wpinv_update_item( $args, $wp_error ); |
|
| 701 | + return wpinv_update_item($args, $wp_error); |
|
| 702 | 702 | } |
| 703 | 703 | |
| 704 | 704 | return $item; |
@@ -709,19 +709,19 @@ discard block |
||
| 709 | 709 | $meta['custom_id'] = $data['custom_id']; |
| 710 | 710 | $meta['custom_singular_name'] = $data['custom_singular_name']; |
| 711 | 711 | $meta['custom_name'] = $data['custom_name']; |
| 712 | - $meta['price'] = wpinv_round_amount( $data['price'] ); |
|
| 712 | + $meta['price'] = wpinv_round_amount($data['price']); |
|
| 713 | 713 | $meta['editable'] = (int)$data['editable']; |
| 714 | 714 | $meta['vat_rule'] = $data['vat_rule']; |
| 715 | 715 | $meta['vat_class'] = '_standard'; |
| 716 | 716 | |
| 717 | - if ( !empty( $data['is_recurring'] ) ) { |
|
| 717 | + if (!empty($data['is_recurring'])) { |
|
| 718 | 718 | $meta['is_recurring'] = $data['is_recurring']; |
| 719 | 719 | $meta['recurring_period'] = $data['recurring_period']; |
| 720 | - $meta['recurring_interval'] = absint( $data['recurring_interval'] ); |
|
| 721 | - $meta['recurring_limit'] = absint( $data['recurring_limit'] ); |
|
| 720 | + $meta['recurring_interval'] = absint($data['recurring_interval']); |
|
| 721 | + $meta['recurring_limit'] = absint($data['recurring_limit']); |
|
| 722 | 722 | $meta['free_trial'] = $data['free_trial']; |
| 723 | 723 | $meta['trial_period'] = $data['trial_period']; |
| 724 | - $meta['trial_interval'] = absint( $data['trial_interval'] ); |
|
| 724 | + $meta['trial_interval'] = absint($data['trial_interval']); |
|
| 725 | 725 | } else { |
| 726 | 726 | $meta['is_recurring'] = 0; |
| 727 | 727 | $meta['recurring_period'] = ''; |
@@ -732,7 +732,7 @@ discard block |
||
| 732 | 732 | $meta['trial_interval'] = ''; |
| 733 | 733 | } |
| 734 | 734 | |
| 735 | - $post_data = array( |
|
| 735 | + $post_data = array( |
|
| 736 | 736 | 'post_title' => $data['title'], |
| 737 | 737 | 'post_excerpt' => $data['excerpt'], |
| 738 | 738 | 'post_status' => $data['status'], |
@@ -740,86 +740,86 @@ discard block |
||
| 740 | 740 | ); |
| 741 | 741 | |
| 742 | 742 | $item = new WPInv_Item(); |
| 743 | - $return = $item->create( $post_data, $wp_error ); |
|
| 743 | + $return = $item->create($post_data, $wp_error); |
|
| 744 | 744 | |
| 745 | - if ( $return && !empty( $item ) && !is_wp_error( $return ) ) { |
|
| 745 | + if ($return && !empty($item) && !is_wp_error($return)) { |
|
| 746 | 746 | return $item; |
| 747 | 747 | } |
| 748 | 748 | |
| 749 | - if ( $wp_error && is_wp_error( $return ) ) { |
|
| 749 | + if ($wp_error && is_wp_error($return)) { |
|
| 750 | 750 | return $return; |
| 751 | 751 | } |
| 752 | 752 | return 0; |
| 753 | 753 | } |
| 754 | 754 | |
| 755 | -function wpinv_update_item( $args = array(), $wp_error = false ) { |
|
| 756 | - $item = !empty( $args['ID'] ) ? new WPInv_Item( $args['ID'] ) : NULL; |
|
| 755 | +function wpinv_update_item($args = array(), $wp_error = false) { |
|
| 756 | + $item = !empty($args['ID']) ? new WPInv_Item($args['ID']) : NULL; |
|
| 757 | 757 | |
| 758 | - if ( empty( $item ) || !( !empty( $item->post_type ) && $item->post_type == 'wpi_item' ) ) { |
|
| 759 | - if ( $wp_error ) { |
|
| 760 | - return new WP_Error( 'wpinv_invalid_item', __( 'Invalid item.', 'invoicing' ) ); |
|
| 758 | + if (empty($item) || !(!empty($item->post_type) && $item->post_type == 'wpi_item')) { |
|
| 759 | + if ($wp_error) { |
|
| 760 | + return new WP_Error('wpinv_invalid_item', __('Invalid item.', 'invoicing')); |
|
| 761 | 761 | } |
| 762 | 762 | return 0; |
| 763 | 763 | } |
| 764 | 764 | |
| 765 | - if ( !empty( $args['custom_id'] ) ) { |
|
| 766 | - $item_exists = wpinv_get_item_by( 'custom_id', $args['custom_id'], ( !empty( $args['type'] ) ? $args['type'] : $item->type ) ); |
|
| 765 | + if (!empty($args['custom_id'])) { |
|
| 766 | + $item_exists = wpinv_get_item_by('custom_id', $args['custom_id'], (!empty($args['type']) ? $args['type'] : $item->type)); |
|
| 767 | 767 | |
| 768 | - if ( !empty( $item_exists ) && $item_exists->ID != $args['ID'] ) { |
|
| 769 | - if ( $wp_error ) { |
|
| 770 | - return new WP_Error( 'wpinv_invalid_custom_id', __( 'Item with custom id already exists.', 'invoicing' ) ); |
|
| 768 | + if (!empty($item_exists) && $item_exists->ID != $args['ID']) { |
|
| 769 | + if ($wp_error) { |
|
| 770 | + return new WP_Error('wpinv_invalid_custom_id', __('Item with custom id already exists.', 'invoicing')); |
|
| 771 | 771 | } |
| 772 | 772 | return 0; |
| 773 | 773 | } |
| 774 | 774 | } |
| 775 | 775 | |
| 776 | - $meta_fields = array( 'type', 'custom_id', 'custom_singular_name', 'custom_name', 'price', 'editable', 'vat_rule', 'vat_class', 'is_recurring', 'recurring_period', 'recurring_interval', 'recurring_limit', 'free_trial', 'trial_period', 'trial_interval' ); |
|
| 776 | + $meta_fields = array('type', 'custom_id', 'custom_singular_name', 'custom_name', 'price', 'editable', 'vat_rule', 'vat_class', 'is_recurring', 'recurring_period', 'recurring_interval', 'recurring_limit', 'free_trial', 'trial_period', 'trial_interval'); |
|
| 777 | 777 | |
| 778 | 778 | $post_data = array(); |
| 779 | - if ( isset( $args['title'] ) ) { |
|
| 779 | + if (isset($args['title'])) { |
|
| 780 | 780 | $post_data['post_title'] = $args['title']; |
| 781 | 781 | } |
| 782 | - if ( isset( $args['excerpt'] ) ) { |
|
| 782 | + if (isset($args['excerpt'])) { |
|
| 783 | 783 | $post_data['post_excerpt'] = $args['excerpt']; |
| 784 | 784 | } |
| 785 | - if ( isset( $args['status'] ) ) { |
|
| 785 | + if (isset($args['status'])) { |
|
| 786 | 786 | $post_data['post_status'] = $args['status']; |
| 787 | 787 | } |
| 788 | 788 | |
| 789 | - foreach ( $meta_fields as $meta_field ) { |
|
| 790 | - if ( isset( $args[ $meta_field ] ) ) { |
|
| 791 | - $value = $args[ $meta_field ]; |
|
| 789 | + foreach ($meta_fields as $meta_field) { |
|
| 790 | + if (isset($args[$meta_field])) { |
|
| 791 | + $value = $args[$meta_field]; |
|
| 792 | 792 | |
| 793 | - switch ( $meta_field ) { |
|
| 793 | + switch ($meta_field) { |
|
| 794 | 794 | case 'price': |
| 795 | - $value = wpinv_round_amount( $value ); |
|
| 795 | + $value = wpinv_round_amount($value); |
|
| 796 | 796 | break; |
| 797 | 797 | case 'recurring_interval': |
| 798 | 798 | case 'recurring_limit': |
| 799 | 799 | case 'trial_interval': |
| 800 | - $value = absint( $value ); |
|
| 800 | + $value = absint($value); |
|
| 801 | 801 | break; |
| 802 | 802 | } |
| 803 | 803 | |
| 804 | - $post_data['meta'][ $meta_field ] = $value; |
|
| 804 | + $post_data['meta'][$meta_field] = $value; |
|
| 805 | 805 | }; |
| 806 | 806 | } |
| 807 | 807 | |
| 808 | - if ( empty( $post_data ) ) { |
|
| 809 | - if ( $wp_error ) { |
|
| 810 | - return new WP_Error( 'wpinv_invalid_item_data', __( 'Invalid item data.', 'invoicing' ) ); |
|
| 808 | + if (empty($post_data)) { |
|
| 809 | + if ($wp_error) { |
|
| 810 | + return new WP_Error('wpinv_invalid_item_data', __('Invalid item data.', 'invoicing')); |
|
| 811 | 811 | } |
| 812 | 812 | return 0; |
| 813 | 813 | } |
| 814 | 814 | $post_data['ID'] = $args['ID']; |
| 815 | 815 | |
| 816 | - $return = $item->update( $post_data, $wp_error ); |
|
| 816 | + $return = $item->update($post_data, $wp_error); |
|
| 817 | 817 | |
| 818 | - if ( $return && !empty( $item ) && !is_wp_error( $return ) ) { |
|
| 818 | + if ($return && !empty($item) && !is_wp_error($return)) { |
|
| 819 | 819 | return $item; |
| 820 | 820 | } |
| 821 | 821 | |
| 822 | - if ( $wp_error && is_wp_error( $return ) ) { |
|
| 822 | + if ($wp_error && is_wp_error($return)) { |
|
| 823 | 823 | return $return; |
| 824 | 824 | } |
| 825 | 825 | return 0; |
@@ -7,90 +7,90 @@ 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( ! current_user_can( 'manage_options' ) ) { |
|
| 33 | - wp_die( __( 'You do not have permission to delete discount codes', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
| 32 | + if (!current_user_can('manage_options')) { |
|
| 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( ! current_user_can( 'manage_options' ) ) { |
|
| 47 | - wp_die( __( 'You do not have permission to edit discount codes', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
| 46 | + if (!current_user_can('manage_options')) { |
|
| 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( ! current_user_can( 'manage_options' ) ) { |
|
| 61 | - wp_die( __( 'You do not have permission to create discount codes', 'invoicing' ), array( 'response' => 403 ) ); |
|
| 60 | + if (!current_user_can('manage_options')) { |
|
| 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'] = 'gd_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 | |
@@ -102,9 +102,9 @@ discard block |
||
| 102 | 102 | |
| 103 | 103 | $discounts = wpinv_get_discounts(); |
| 104 | 104 | |
| 105 | - if ( $discounts) { |
|
| 106 | - foreach ( $discounts as $discount ) { |
|
| 107 | - if ( wpinv_is_discount_active( $discount->ID ) ) { |
|
| 105 | + if ($discounts) { |
|
| 106 | + foreach ($discounts as $discount) { |
|
| 107 | + if (wpinv_is_discount_active($discount->ID)) { |
|
| 108 | 108 | $has_active = true; |
| 109 | 109 | break; |
| 110 | 110 | } |
@@ -113,38 +113,38 @@ discard block |
||
| 113 | 113 | return $has_active; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | -function wpinv_get_discount( $discount_id = 0 ) { |
|
| 117 | - if( empty( $discount_id ) ) { |
|
| 116 | +function wpinv_get_discount($discount_id = 0) { |
|
| 117 | + if (empty($discount_id)) { |
|
| 118 | 118 | return false; |
| 119 | 119 | } |
| 120 | 120 | |
| 121 | - if ( get_post_type( $discount_id ) != 'wpi_discount' ) { |
|
| 121 | + if (get_post_type($discount_id) != 'wpi_discount') { |
|
| 122 | 122 | return false; |
| 123 | 123 | } |
| 124 | 124 | |
| 125 | - $discount = get_post( $discount_id ); |
|
| 125 | + $discount = get_post($discount_id); |
|
| 126 | 126 | |
| 127 | 127 | return $discount; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | -function wpinv_get_discount_by_code( $code = '' ) { |
|
| 131 | - if( empty( $code ) || ! is_string( $code ) ) { |
|
| 130 | +function wpinv_get_discount_by_code($code = '') { |
|
| 131 | + if (empty($code) || !is_string($code)) { |
|
| 132 | 132 | return false; |
| 133 | 133 | } |
| 134 | 134 | |
| 135 | - return wpinv_get_discount_by( 'code', $code ); |
|
| 135 | + return wpinv_get_discount_by('code', $code); |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | -function wpinv_get_discount_by( $field = '', $value = '' ) { |
|
| 139 | - if( empty( $field ) || empty( $value ) ) { |
|
| 138 | +function wpinv_get_discount_by($field = '', $value = '') { |
|
| 139 | + if (empty($field) || empty($value)) { |
|
| 140 | 140 | return false; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - if( ! is_string( $field ) ) { |
|
| 143 | + if (!is_string($field)) { |
|
| 144 | 144 | return false; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - switch( strtolower( $field ) ) { |
|
| 147 | + switch (strtolower($field)) { |
|
| 148 | 148 | |
| 149 | 149 | case 'code': |
| 150 | 150 | $meta_query = array(); |
@@ -154,32 +154,32 @@ discard block |
||
| 154 | 154 | 'compare' => '=' |
| 155 | 155 | ); |
| 156 | 156 | |
| 157 | - $discount = wpinv_get_discounts( array( |
|
| 157 | + $discount = wpinv_get_discounts(array( |
|
| 158 | 158 | 'posts_per_page' => 1, |
| 159 | 159 | 'post_status' => 'any', |
| 160 | 160 | 'meta_query' => $meta_query, |
| 161 | - ) ); |
|
| 161 | + )); |
|
| 162 | 162 | |
| 163 | - if( $discount ) { |
|
| 163 | + if ($discount) { |
|
| 164 | 164 | $discount = $discount[0]; |
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | break; |
| 168 | 168 | |
| 169 | 169 | case 'id': |
| 170 | - $discount = wpinv_get_discount( $value ); |
|
| 170 | + $discount = wpinv_get_discount($value); |
|
| 171 | 171 | |
| 172 | 172 | break; |
| 173 | 173 | |
| 174 | 174 | case 'name': |
| 175 | - $discount = get_posts( array( |
|
| 175 | + $discount = get_posts(array( |
|
| 176 | 176 | 'post_type' => 'wpi_discount', |
| 177 | 177 | 'name' => $value, |
| 178 | 178 | 'posts_per_page' => 1, |
| 179 | 179 | 'post_status' => 'any' |
| 180 | - ) ); |
|
| 180 | + )); |
|
| 181 | 181 | |
| 182 | - if( $discount ) { |
|
| 182 | + if ($discount) { |
|
| 183 | 183 | $discount = $discount[0]; |
| 184 | 184 | } |
| 185 | 185 | |
@@ -189,99 +189,99 @@ discard block |
||
| 189 | 189 | return false; |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | - if( ! empty( $discount ) ) { |
|
| 192 | + if (!empty($discount)) { |
|
| 193 | 193 | return $discount; |
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | return false; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | -function wpinv_store_discount( $post_id, $data, $post, $update = false ) { |
|
| 199 | +function wpinv_store_discount($post_id, $data, $post, $update = false) { |
|
| 200 | 200 | $meta = array( |
| 201 | - 'code' => isset( $data['code'] ) ? sanitize_text_field( $data['code'] ) : '', |
|
| 202 | - 'type' => isset( $data['type'] ) ? sanitize_text_field( $data['type'] ) : 'percent', |
|
| 203 | - 'amount' => isset( $data['amount'] ) ? wpinv_sanitize_amount( $data['amount'] ) : '', |
|
| 204 | - 'start' => isset( $data['start'] ) ? sanitize_text_field( $data['start'] ) : '', |
|
| 205 | - 'expiration' => isset( $data['expiration'] ) ? sanitize_text_field( $data['expiration'] ) : '', |
|
| 206 | - 'min_total' => isset( $data['min_total'] ) ? wpinv_sanitize_amount( $data['min_total'] ) : '', |
|
| 207 | - 'max_total' => isset( $data['max_total'] ) ? wpinv_sanitize_amount( $data['max_total'] ) : '', |
|
| 208 | - 'max_uses' => isset( $data['max_uses'] ) ? absint( $data['max_uses'] ) : '', |
|
| 209 | - 'items' => isset( $data['items'] ) ? $data['items'] : array(), |
|
| 210 | - 'excluded_items' => isset( $data['excluded_items'] ) ? $data['excluded_items'] : array(), |
|
| 211 | - 'is_recurring' => isset( $data['recurring'] ) ? (bool)$data['recurring'] : false, |
|
| 212 | - 'is_single_use' => isset( $data['single_use'] ) ? (bool)$data['single_use'] : false, |
|
| 213 | - 'uses' => isset( $data['uses'] ) ? (int)$data['uses'] : false, |
|
| 201 | + 'code' => isset($data['code']) ? sanitize_text_field($data['code']) : '', |
|
| 202 | + 'type' => isset($data['type']) ? sanitize_text_field($data['type']) : 'percent', |
|
| 203 | + 'amount' => isset($data['amount']) ? wpinv_sanitize_amount($data['amount']) : '', |
|
| 204 | + 'start' => isset($data['start']) ? sanitize_text_field($data['start']) : '', |
|
| 205 | + 'expiration' => isset($data['expiration']) ? sanitize_text_field($data['expiration']) : '', |
|
| 206 | + 'min_total' => isset($data['min_total']) ? wpinv_sanitize_amount($data['min_total']) : '', |
|
| 207 | + 'max_total' => isset($data['max_total']) ? wpinv_sanitize_amount($data['max_total']) : '', |
|
| 208 | + 'max_uses' => isset($data['max_uses']) ? absint($data['max_uses']) : '', |
|
| 209 | + 'items' => isset($data['items']) ? $data['items'] : array(), |
|
| 210 | + 'excluded_items' => isset($data['excluded_items']) ? $data['excluded_items'] : array(), |
|
| 211 | + 'is_recurring' => isset($data['recurring']) ? (bool)$data['recurring'] : false, |
|
| 212 | + 'is_single_use' => isset($data['single_use']) ? (bool)$data['single_use'] : false, |
|
| 213 | + 'uses' => isset($data['uses']) ? (int)$data['uses'] : false, |
|
| 214 | 214 | ); |
| 215 | 215 | |
| 216 | - $start_timestamp = strtotime( $meta['start'] ); |
|
| 216 | + $start_timestamp = strtotime($meta['start']); |
|
| 217 | 217 | |
| 218 | - if ( !empty( $meta['start'] ) ) { |
|
| 219 | - $meta['start'] = date( 'Y-m-d H:i:s', $start_timestamp ); |
|
| 218 | + if (!empty($meta['start'])) { |
|
| 219 | + $meta['start'] = date('Y-m-d H:i:s', $start_timestamp); |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | - if ( $meta['type'] == 'percent' && (float)$meta['amount'] > 100 ) { |
|
| 222 | + if ($meta['type'] == 'percent' && (float)$meta['amount'] > 100) { |
|
| 223 | 223 | $meta['amount'] = 100; |
| 224 | 224 | } |
| 225 | 225 | |
| 226 | - if ( !empty( $meta['expiration'] ) ) { |
|
| 227 | - $meta['expiration'] = date( 'Y-m-d H:i:s', strtotime( date( 'Y-m-d', strtotime( $meta['expiration'] ) ) . ' 23:59:59' ) ); |
|
| 228 | - $end_timestamp = strtotime( $meta['expiration'] ); |
|
| 226 | + if (!empty($meta['expiration'])) { |
|
| 227 | + $meta['expiration'] = date('Y-m-d H:i:s', strtotime(date('Y-m-d', strtotime($meta['expiration'])) . ' 23:59:59')); |
|
| 228 | + $end_timestamp = strtotime($meta['expiration']); |
|
| 229 | 229 | |
| 230 | - if ( !empty( $meta['start'] ) && $start_timestamp > $end_timestamp ) { |
|
| 230 | + if (!empty($meta['start']) && $start_timestamp > $end_timestamp) { |
|
| 231 | 231 | $meta['expiration'] = $meta['start']; // Set the expiration date to the start date if start is later than expiration date. |
| 232 | 232 | } |
| 233 | 233 | } |
| 234 | 234 | |
| 235 | - if ( $meta['uses'] === false ) { |
|
| 236 | - unset( $meta['uses'] ); |
|
| 235 | + if ($meta['uses'] === false) { |
|
| 236 | + unset($meta['uses']); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | - if ( ! empty( $meta['items'] ) ) { |
|
| 240 | - foreach ( $meta['items'] as $key => $item ) { |
|
| 241 | - if ( 0 === intval( $item ) ) { |
|
| 242 | - unset( $meta['items'][ $key ] ); |
|
| 239 | + if (!empty($meta['items'])) { |
|
| 240 | + foreach ($meta['items'] as $key => $item) { |
|
| 241 | + if (0 === intval($item)) { |
|
| 242 | + unset($meta['items'][$key]); |
|
| 243 | 243 | } |
| 244 | 244 | } |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - if ( ! empty( $meta['excluded_items'] ) ) { |
|
| 248 | - foreach ( $meta['excluded_items'] as $key => $item ) { |
|
| 249 | - if ( 0 === intval( $item ) ) { |
|
| 250 | - unset( $meta['excluded_items'][ $key ] ); |
|
| 247 | + if (!empty($meta['excluded_items'])) { |
|
| 248 | + foreach ($meta['excluded_items'] as $key => $item) { |
|
| 249 | + if (0 === intval($item)) { |
|
| 250 | + unset($meta['excluded_items'][$key]); |
|
| 251 | 251 | } |
| 252 | 252 | } |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | - $meta = apply_filters( 'wpinv_update_discount', $meta, $post_id, $post ); |
|
| 255 | + $meta = apply_filters('wpinv_update_discount', $meta, $post_id, $post); |
|
| 256 | 256 | |
| 257 | - do_action( 'wpinv_pre_update_discount', $meta, $post_id, $post ); |
|
| 257 | + do_action('wpinv_pre_update_discount', $meta, $post_id, $post); |
|
| 258 | 258 | |
| 259 | - foreach( $meta as $key => $value ) { |
|
| 260 | - update_post_meta( $post_id, '_wpi_discount_' . $key, $value ); |
|
| 259 | + foreach ($meta as $key => $value) { |
|
| 260 | + update_post_meta($post_id, '_wpi_discount_' . $key, $value); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | - do_action( 'wpinv_post_update_discount', $meta, $post_id, $post ); |
|
| 263 | + do_action('wpinv_post_update_discount', $meta, $post_id, $post); |
|
| 264 | 264 | |
| 265 | 265 | return $post_id; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | -function wpinv_remove_discount( $discount_id = 0 ) { |
|
| 269 | - do_action( 'wpinv_pre_delete_discount', $discount_id ); |
|
| 268 | +function wpinv_remove_discount($discount_id = 0) { |
|
| 269 | + do_action('wpinv_pre_delete_discount', $discount_id); |
|
| 270 | 270 | |
| 271 | - wp_delete_post( $discount_id, true ); |
|
| 271 | + wp_delete_post($discount_id, true); |
|
| 272 | 272 | |
| 273 | - do_action( 'wpinv_post_delete_discount', $discount_id ); |
|
| 273 | + do_action('wpinv_post_delete_discount', $discount_id); |
|
| 274 | 274 | } |
| 275 | 275 | |
| 276 | -function wpinv_update_discount_status( $code_id = 0, $new_status = 'publish' ) { |
|
| 277 | - $discount = wpinv_get_discount( $code_id ); |
|
| 276 | +function wpinv_update_discount_status($code_id = 0, $new_status = 'publish') { |
|
| 277 | + $discount = wpinv_get_discount($code_id); |
|
| 278 | 278 | |
| 279 | - if ( $discount ) { |
|
| 280 | - do_action( 'wpinv_pre_update_discount_status', $code_id, $new_status, $discount->post_status ); |
|
| 279 | + if ($discount) { |
|
| 280 | + do_action('wpinv_pre_update_discount_status', $code_id, $new_status, $discount->post_status); |
|
| 281 | 281 | |
| 282 | - wp_update_post( array( 'ID' => $code_id, 'post_status' => $new_status ) ); |
|
| 282 | + wp_update_post(array('ID' => $code_id, 'post_status' => $new_status)); |
|
| 283 | 283 | |
| 284 | - do_action( 'wpinv_post_update_discount_status', $code_id, $new_status, $discount->post_status ); |
|
| 284 | + do_action('wpinv_post_update_discount_status', $code_id, $new_status, $discount->post_status); |
|
| 285 | 285 | |
| 286 | 286 | return true; |
| 287 | 287 | } |
@@ -289,173 +289,173 @@ discard block |
||
| 289 | 289 | return false; |
| 290 | 290 | } |
| 291 | 291 | |
| 292 | -function wpinv_discount_exists( $code_id ) { |
|
| 293 | - if ( wpinv_get_discount( $code_id ) ) { |
|
| 292 | +function wpinv_discount_exists($code_id) { |
|
| 293 | + if (wpinv_get_discount($code_id)) { |
|
| 294 | 294 | return true; |
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | return false; |
| 298 | 298 | } |
| 299 | 299 | |
| 300 | -function wpinv_is_discount_active( $code_id = null ) { |
|
| 301 | - $discount = wpinv_get_discount( $code_id ); |
|
| 300 | +function wpinv_is_discount_active($code_id = null) { |
|
| 301 | + $discount = wpinv_get_discount($code_id); |
|
| 302 | 302 | $return = false; |
| 303 | 303 | |
| 304 | - if ( $discount ) { |
|
| 305 | - if ( wpinv_is_discount_expired( $code_id ) ) { |
|
| 306 | - if( defined( 'DOING_AJAX' ) ) { |
|
| 307 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is expired.', 'invoicing' ) ); |
|
| 304 | + if ($discount) { |
|
| 305 | + if (wpinv_is_discount_expired($code_id)) { |
|
| 306 | + if (defined('DOING_AJAX')) { |
|
| 307 | + wpinv_set_error('wpinv-discount-error', __('This discount is expired.', 'invoicing')); |
|
| 308 | 308 | } |
| 309 | - } elseif ( $discount->post_status == 'publish' ) { |
|
| 309 | + } elseif ($discount->post_status == 'publish') { |
|
| 310 | 310 | $return = true; |
| 311 | 311 | } else { |
| 312 | - if( defined( 'DOING_AJAX' ) ) { |
|
| 313 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not active.', 'invoicing' ) ); |
|
| 312 | + if (defined('DOING_AJAX')) { |
|
| 313 | + wpinv_set_error('wpinv-discount-error', __('This discount is not active.', 'invoicing')); |
|
| 314 | 314 | } |
| 315 | 315 | } |
| 316 | 316 | } |
| 317 | 317 | |
| 318 | - return apply_filters( 'wpinv_is_discount_active', $return, $code_id ); |
|
| 318 | + return apply_filters('wpinv_is_discount_active', $return, $code_id); |
|
| 319 | 319 | } |
| 320 | 320 | |
| 321 | -function wpinv_get_discount_code( $code_id = null ) { |
|
| 322 | - $code = get_post_meta( $code_id, '_wpi_discount_code', true ); |
|
| 321 | +function wpinv_get_discount_code($code_id = null) { |
|
| 322 | + $code = get_post_meta($code_id, '_wpi_discount_code', true); |
|
| 323 | 323 | |
| 324 | - return apply_filters( 'wpinv_get_discount_code', $code, $code_id ); |
|
| 324 | + return apply_filters('wpinv_get_discount_code', $code, $code_id); |
|
| 325 | 325 | } |
| 326 | 326 | |
| 327 | -function wpinv_get_discount_start_date( $code_id = null ) { |
|
| 328 | - $start_date = get_post_meta( $code_id, '_wpi_discount_start', true ); |
|
| 327 | +function wpinv_get_discount_start_date($code_id = null) { |
|
| 328 | + $start_date = get_post_meta($code_id, '_wpi_discount_start', true); |
|
| 329 | 329 | |
| 330 | - return apply_filters( 'wpinv_get_discount_start_date', $start_date, $code_id ); |
|
| 330 | + return apply_filters('wpinv_get_discount_start_date', $start_date, $code_id); |
|
| 331 | 331 | } |
| 332 | 332 | |
| 333 | -function wpinv_get_discount_expiration( $code_id = null ) { |
|
| 334 | - $expiration = get_post_meta( $code_id, '_wpi_discount_expiration', true ); |
|
| 333 | +function wpinv_get_discount_expiration($code_id = null) { |
|
| 334 | + $expiration = get_post_meta($code_id, '_wpi_discount_expiration', true); |
|
| 335 | 335 | |
| 336 | - return apply_filters( 'wpinv_get_discount_expiration', $expiration, $code_id ); |
|
| 336 | + return apply_filters('wpinv_get_discount_expiration', $expiration, $code_id); |
|
| 337 | 337 | } |
| 338 | 338 | |
| 339 | -function wpinv_get_discount_max_uses( $code_id = null ) { |
|
| 340 | - $max_uses = get_post_meta( $code_id, '_wpi_discount_max_uses', true ); |
|
| 339 | +function wpinv_get_discount_max_uses($code_id = null) { |
|
| 340 | + $max_uses = get_post_meta($code_id, '_wpi_discount_max_uses', true); |
|
| 341 | 341 | |
| 342 | - return (int) apply_filters( 'wpinv_get_discount_max_uses', $max_uses, $code_id ); |
|
| 342 | + return (int)apply_filters('wpinv_get_discount_max_uses', $max_uses, $code_id); |
|
| 343 | 343 | } |
| 344 | 344 | |
| 345 | -function wpinv_get_discount_uses( $code_id = null ) { |
|
| 346 | - $uses = get_post_meta( $code_id, '_wpi_discount_uses', true ); |
|
| 345 | +function wpinv_get_discount_uses($code_id = null) { |
|
| 346 | + $uses = get_post_meta($code_id, '_wpi_discount_uses', true); |
|
| 347 | 347 | |
| 348 | - return (int) apply_filters( 'wpinv_get_discount_uses', $uses, $code_id ); |
|
| 348 | + return (int)apply_filters('wpinv_get_discount_uses', $uses, $code_id); |
|
| 349 | 349 | } |
| 350 | 350 | |
| 351 | -function wpinv_get_discount_min_total( $code_id = null ) { |
|
| 352 | - $min_total = get_post_meta( $code_id, '_wpi_discount_min_total', true ); |
|
| 351 | +function wpinv_get_discount_min_total($code_id = null) { |
|
| 352 | + $min_total = get_post_meta($code_id, '_wpi_discount_min_total', true); |
|
| 353 | 353 | |
| 354 | - return (float) apply_filters( 'wpinv_get_discount_min_total', $min_total, $code_id ); |
|
| 354 | + return (float)apply_filters('wpinv_get_discount_min_total', $min_total, $code_id); |
|
| 355 | 355 | } |
| 356 | 356 | |
| 357 | -function wpinv_get_discount_max_total( $code_id = null ) { |
|
| 358 | - $max_total = get_post_meta( $code_id, '_wpi_discount_max_total', true ); |
|
| 357 | +function wpinv_get_discount_max_total($code_id = null) { |
|
| 358 | + $max_total = get_post_meta($code_id, '_wpi_discount_max_total', true); |
|
| 359 | 359 | |
| 360 | - return (float) apply_filters( 'wpinv_get_discount_max_total', $max_total, $code_id ); |
|
| 360 | + return (float)apply_filters('wpinv_get_discount_max_total', $max_total, $code_id); |
|
| 361 | 361 | } |
| 362 | 362 | |
| 363 | -function wpinv_get_discount_amount( $code_id = null ) { |
|
| 364 | - $amount = get_post_meta( $code_id, '_wpi_discount_amount', true ); |
|
| 363 | +function wpinv_get_discount_amount($code_id = null) { |
|
| 364 | + $amount = get_post_meta($code_id, '_wpi_discount_amount', true); |
|
| 365 | 365 | |
| 366 | - return (float) apply_filters( 'wpinv_get_discount_amount', $amount, $code_id ); |
|
| 366 | + return (float)apply_filters('wpinv_get_discount_amount', $amount, $code_id); |
|
| 367 | 367 | } |
| 368 | 368 | |
| 369 | -function wpinv_get_discount_type( $code_id = null, $name = false ) { |
|
| 370 | - $type = strtolower( get_post_meta( $code_id, '_wpi_discount_type', true ) ); |
|
| 369 | +function wpinv_get_discount_type($code_id = null, $name = false) { |
|
| 370 | + $type = strtolower(get_post_meta($code_id, '_wpi_discount_type', true)); |
|
| 371 | 371 | |
| 372 | - if ( $name ) { |
|
| 373 | - $name = wpinv_get_discount_type_name( $type ); |
|
| 372 | + if ($name) { |
|
| 373 | + $name = wpinv_get_discount_type_name($type); |
|
| 374 | 374 | |
| 375 | - return apply_filters( 'wpinv_get_discount_type_name', $name, $code_id ); |
|
| 375 | + return apply_filters('wpinv_get_discount_type_name', $name, $code_id); |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | - return apply_filters( 'wpinv_get_discount_type', $type, $code_id ); |
|
| 378 | + return apply_filters('wpinv_get_discount_type', $type, $code_id); |
|
| 379 | 379 | } |
| 380 | 380 | |
| 381 | -function wpinv_discount_status( $status ) { |
|
| 382 | - switch( $status ){ |
|
| 381 | +function wpinv_discount_status($status) { |
|
| 382 | + switch ($status) { |
|
| 383 | 383 | case 'expired' : |
| 384 | - $name = __( 'Expired', 'invoicing' ); |
|
| 384 | + $name = __('Expired', 'invoicing'); |
|
| 385 | 385 | break; |
| 386 | 386 | case 'publish' : |
| 387 | 387 | case 'active' : |
| 388 | - $name = __( 'Active', 'invoicing' ); |
|
| 388 | + $name = __('Active', 'invoicing'); |
|
| 389 | 389 | break; |
| 390 | 390 | default : |
| 391 | - $name = __( 'Inactive', 'invoicing' ); |
|
| 391 | + $name = __('Inactive', 'invoicing'); |
|
| 392 | 392 | break; |
| 393 | 393 | } |
| 394 | 394 | return $name; |
| 395 | 395 | } |
| 396 | 396 | |
| 397 | -function wpinv_get_discount_excluded_items( $code_id = null ) { |
|
| 398 | - $excluded_items = get_post_meta( $code_id, '_wpi_discount_excluded_items', true ); |
|
| 397 | +function wpinv_get_discount_excluded_items($code_id = null) { |
|
| 398 | + $excluded_items = get_post_meta($code_id, '_wpi_discount_excluded_items', true); |
|
| 399 | 399 | |
| 400 | - if ( empty( $excluded_items ) || ! is_array( $excluded_items ) ) { |
|
| 400 | + if (empty($excluded_items) || !is_array($excluded_items)) { |
|
| 401 | 401 | $excluded_items = array(); |
| 402 | 402 | } |
| 403 | 403 | |
| 404 | - return (array) apply_filters( 'wpinv_get_discount_excluded_items', $excluded_items, $code_id ); |
|
| 404 | + return (array)apply_filters('wpinv_get_discount_excluded_items', $excluded_items, $code_id); |
|
| 405 | 405 | } |
| 406 | 406 | |
| 407 | -function wpinv_get_discount_item_reqs( $code_id = null ) { |
|
| 408 | - $item_reqs = get_post_meta( $code_id, '_wpi_discount_items', true ); |
|
| 407 | +function wpinv_get_discount_item_reqs($code_id = null) { |
|
| 408 | + $item_reqs = get_post_meta($code_id, '_wpi_discount_items', true); |
|
| 409 | 409 | |
| 410 | - if ( empty( $item_reqs ) || ! is_array( $item_reqs ) ) { |
|
| 410 | + if (empty($item_reqs) || !is_array($item_reqs)) { |
|
| 411 | 411 | $item_reqs = array(); |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | - return (array) apply_filters( 'wpinv_get_discount_item_reqs', $item_reqs, $code_id ); |
|
| 414 | + return (array)apply_filters('wpinv_get_discount_item_reqs', $item_reqs, $code_id); |
|
| 415 | 415 | } |
| 416 | 416 | |
| 417 | -function wpinv_get_discount_item_condition( $code_id = 0 ) { |
|
| 418 | - return get_post_meta( $code_id, '_wpi_discount_item_condition', true ); |
|
| 417 | +function wpinv_get_discount_item_condition($code_id = 0) { |
|
| 418 | + return get_post_meta($code_id, '_wpi_discount_item_condition', true); |
|
| 419 | 419 | } |
| 420 | 420 | |
| 421 | -function wpinv_is_discount_not_global( $code_id = 0 ) { |
|
| 422 | - return (bool) get_post_meta( $code_id, '_wpi_discount_is_not_global', true ); |
|
| 421 | +function wpinv_is_discount_not_global($code_id = 0) { |
|
| 422 | + return (bool)get_post_meta($code_id, '_wpi_discount_is_not_global', true); |
|
| 423 | 423 | } |
| 424 | 424 | |
| 425 | -function wpinv_is_discount_expired( $code_id = null ) { |
|
| 426 | - $discount = wpinv_get_discount( $code_id ); |
|
| 425 | +function wpinv_is_discount_expired($code_id = null) { |
|
| 426 | + $discount = wpinv_get_discount($code_id); |
|
| 427 | 427 | $return = false; |
| 428 | 428 | |
| 429 | - if ( $discount ) { |
|
| 430 | - $expiration = wpinv_get_discount_expiration( $code_id ); |
|
| 431 | - if ( $expiration ) { |
|
| 432 | - $expiration = strtotime( $expiration ); |
|
| 433 | - if ( $expiration < current_time( 'timestamp' ) ) { |
|
| 429 | + if ($discount) { |
|
| 430 | + $expiration = wpinv_get_discount_expiration($code_id); |
|
| 431 | + if ($expiration) { |
|
| 432 | + $expiration = strtotime($expiration); |
|
| 433 | + if ($expiration < current_time('timestamp')) { |
|
| 434 | 434 | // Discount is expired |
| 435 | - wpinv_update_discount_status( $code_id, 'pending' ); |
|
| 435 | + wpinv_update_discount_status($code_id, 'pending'); |
|
| 436 | 436 | $return = true; |
| 437 | 437 | } |
| 438 | 438 | } |
| 439 | 439 | } |
| 440 | 440 | |
| 441 | - return apply_filters( 'wpinv_is_discount_expired', $return, $code_id ); |
|
| 441 | + return apply_filters('wpinv_is_discount_expired', $return, $code_id); |
|
| 442 | 442 | } |
| 443 | 443 | |
| 444 | -function wpinv_is_discount_started( $code_id = null ) { |
|
| 445 | - $discount = wpinv_get_discount( $code_id ); |
|
| 444 | +function wpinv_is_discount_started($code_id = null) { |
|
| 445 | + $discount = wpinv_get_discount($code_id); |
|
| 446 | 446 | $return = false; |
| 447 | 447 | |
| 448 | - if ( $discount ) { |
|
| 449 | - $start_date = wpinv_get_discount_start_date( $code_id ); |
|
| 448 | + if ($discount) { |
|
| 449 | + $start_date = wpinv_get_discount_start_date($code_id); |
|
| 450 | 450 | |
| 451 | - if ( $start_date ) { |
|
| 452 | - $start_date = strtotime( $start_date ); |
|
| 451 | + if ($start_date) { |
|
| 452 | + $start_date = strtotime($start_date); |
|
| 453 | 453 | |
| 454 | - if ( $start_date < current_time( 'timestamp' ) ) { |
|
| 454 | + if ($start_date < current_time('timestamp')) { |
|
| 455 | 455 | // Discount has past the start date |
| 456 | 456 | $return = true; |
| 457 | 457 | } else { |
| 458 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not active yet.', 'invoicing' ) ); |
|
| 458 | + wpinv_set_error('wpinv-discount-error', __('This discount is not active yet.', 'invoicing')); |
|
| 459 | 459 | } |
| 460 | 460 | } else { |
| 461 | 461 | // No start date for this discount, so has to be true |
@@ -463,159 +463,159 @@ discard block |
||
| 463 | 463 | } |
| 464 | 464 | } |
| 465 | 465 | |
| 466 | - return apply_filters( 'wpinv_is_discount_started', $return, $code_id ); |
|
| 466 | + return apply_filters('wpinv_is_discount_started', $return, $code_id); |
|
| 467 | 467 | } |
| 468 | 468 | |
| 469 | -function wpinv_check_discount_dates( $code_id = null ) { |
|
| 470 | - $discount = wpinv_get_discount( $code_id ); |
|
| 469 | +function wpinv_check_discount_dates($code_id = null) { |
|
| 470 | + $discount = wpinv_get_discount($code_id); |
|
| 471 | 471 | $return = false; |
| 472 | 472 | |
| 473 | - if ( $discount ) { |
|
| 474 | - $start_date = wpinv_get_discount_start_date( $code_id ); |
|
| 473 | + if ($discount) { |
|
| 474 | + $start_date = wpinv_get_discount_start_date($code_id); |
|
| 475 | 475 | |
| 476 | - if ( $start_date ) { |
|
| 477 | - $start_date = strtotime( $start_date ); |
|
| 476 | + if ($start_date) { |
|
| 477 | + $start_date = strtotime($start_date); |
|
| 478 | 478 | |
| 479 | - if ( $start_date < current_time( 'timestamp' ) ) { |
|
| 479 | + if ($start_date < current_time('timestamp')) { |
|
| 480 | 480 | // Discount has past the start date |
| 481 | 481 | $return = true; |
| 482 | 482 | } else { |
| 483 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not active yet.', 'invoicing' ) ); |
|
| 483 | + wpinv_set_error('wpinv-discount-error', __('This discount is not active yet.', 'invoicing')); |
|
| 484 | 484 | } |
| 485 | 485 | } else { |
| 486 | 486 | // No start date for this discount, so has to be true |
| 487 | 487 | $return = true; |
| 488 | 488 | } |
| 489 | 489 | |
| 490 | - if ( $return ) { |
|
| 491 | - $expiration = wpinv_get_discount_expiration( $code_id ); |
|
| 490 | + if ($return) { |
|
| 491 | + $expiration = wpinv_get_discount_expiration($code_id); |
|
| 492 | 492 | |
| 493 | - if ( $expiration ) { |
|
| 494 | - $expiration = strtotime( $expiration ); |
|
| 495 | - if ( $expiration < current_time( 'timestamp' ) ) { |
|
| 493 | + if ($expiration) { |
|
| 494 | + $expiration = strtotime($expiration); |
|
| 495 | + if ($expiration < current_time('timestamp')) { |
|
| 496 | 496 | // Discount is expired |
| 497 | - wpinv_update_discount_status( $code_id, 'pending' ); |
|
| 497 | + wpinv_update_discount_status($code_id, 'pending'); |
|
| 498 | 498 | $return = true; |
| 499 | 499 | } |
| 500 | 500 | } |
| 501 | 501 | } |
| 502 | 502 | } |
| 503 | 503 | |
| 504 | - return apply_filters( 'wpinv_check_discount_dates', $return, $code_id ); |
|
| 504 | + return apply_filters('wpinv_check_discount_dates', $return, $code_id); |
|
| 505 | 505 | } |
| 506 | 506 | |
| 507 | -function wpinv_is_discount_maxed_out( $code_id = null ) { |
|
| 508 | - $discount = wpinv_get_discount( $code_id ); |
|
| 507 | +function wpinv_is_discount_maxed_out($code_id = null) { |
|
| 508 | + $discount = wpinv_get_discount($code_id); |
|
| 509 | 509 | $return = false; |
| 510 | 510 | |
| 511 | - if ( $discount ) { |
|
| 512 | - $uses = wpinv_get_discount_uses( $code_id ); |
|
| 511 | + if ($discount) { |
|
| 512 | + $uses = wpinv_get_discount_uses($code_id); |
|
| 513 | 513 | // Large number that will never be reached |
| 514 | - $max_uses = wpinv_get_discount_max_uses( $code_id ); |
|
| 514 | + $max_uses = wpinv_get_discount_max_uses($code_id); |
|
| 515 | 515 | // Should never be greater than, but just in case |
| 516 | - if ( $uses >= $max_uses && ! empty( $max_uses ) ) { |
|
| 516 | + if ($uses >= $max_uses && !empty($max_uses)) { |
|
| 517 | 517 | // Discount is maxed out |
| 518 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount has reached its maximum usage.', 'invoicing' ) ); |
|
| 518 | + wpinv_set_error('wpinv-discount-error', __('This discount has reached its maximum usage.', 'invoicing')); |
|
| 519 | 519 | $return = true; |
| 520 | 520 | } |
| 521 | 521 | } |
| 522 | 522 | |
| 523 | - return apply_filters( 'wpinv_is_discount_maxed_out', $return, $code_id ); |
|
| 523 | + return apply_filters('wpinv_is_discount_maxed_out', $return, $code_id); |
|
| 524 | 524 | } |
| 525 | 525 | |
| 526 | -function wpinv_discount_is_min_met( $code_id = null ) { |
|
| 527 | - $discount = wpinv_get_discount( $code_id ); |
|
| 526 | +function wpinv_discount_is_min_met($code_id = null) { |
|
| 527 | + $discount = wpinv_get_discount($code_id); |
|
| 528 | 528 | $return = false; |
| 529 | 529 | |
| 530 | - if ( $discount ) { |
|
| 531 | - $min = (float)wpinv_get_discount_min_total( $code_id ); |
|
| 532 | - $cart_amount = (float)wpinv_get_cart_discountable_subtotal( $code_id ); |
|
| 530 | + if ($discount) { |
|
| 531 | + $min = (float)wpinv_get_discount_min_total($code_id); |
|
| 532 | + $cart_amount = (float)wpinv_get_cart_discountable_subtotal($code_id); |
|
| 533 | 533 | |
| 534 | - if ( !$min > 0 || $cart_amount >= $min ) { |
|
| 534 | + if (!$min > 0 || $cart_amount >= $min) { |
|
| 535 | 535 | // Minimum has been met |
| 536 | 536 | $return = true; |
| 537 | 537 | } else { |
| 538 | - wpinv_set_error( 'wpinv-discount-error', sprintf( __( 'Minimum invoice of %s not met.', 'invoicing' ), wpinv_price( wpinv_format_amount( $min ) ) ) ); |
|
| 538 | + wpinv_set_error('wpinv-discount-error', sprintf(__('Minimum invoice of %s not met.', 'invoicing'), wpinv_price(wpinv_format_amount($min)))); |
|
| 539 | 539 | } |
| 540 | 540 | } |
| 541 | 541 | |
| 542 | - return apply_filters( 'wpinv_is_discount_min_met', $return, $code_id ); |
|
| 542 | + return apply_filters('wpinv_is_discount_min_met', $return, $code_id); |
|
| 543 | 543 | } |
| 544 | 544 | |
| 545 | -function wpinv_discount_is_max_met( $code_id = null ) { |
|
| 546 | - $discount = wpinv_get_discount( $code_id ); |
|
| 545 | +function wpinv_discount_is_max_met($code_id = null) { |
|
| 546 | + $discount = wpinv_get_discount($code_id); |
|
| 547 | 547 | $return = false; |
| 548 | 548 | |
| 549 | - if ( $discount ) { |
|
| 550 | - $max = (float)wpinv_get_discount_max_total( $code_id ); |
|
| 551 | - $cart_amount = (float)wpinv_get_cart_discountable_subtotal( $code_id ); |
|
| 549 | + if ($discount) { |
|
| 550 | + $max = (float)wpinv_get_discount_max_total($code_id); |
|
| 551 | + $cart_amount = (float)wpinv_get_cart_discountable_subtotal($code_id); |
|
| 552 | 552 | |
| 553 | - if ( !$max > 0 || $cart_amount <= $max ) { |
|
| 553 | + if (!$max > 0 || $cart_amount <= $max) { |
|
| 554 | 554 | // Minimum has been met |
| 555 | 555 | $return = true; |
| 556 | 556 | } else { |
| 557 | - wpinv_set_error( 'wpinv-discount-error', sprintf( __( 'Maximum invoice of %s not met.', 'invoicing' ), wpinv_price( wpinv_format_amount( $max ) ) ) ); |
|
| 557 | + wpinv_set_error('wpinv-discount-error', sprintf(__('Maximum invoice of %s not met.', 'invoicing'), wpinv_price(wpinv_format_amount($max)))); |
|
| 558 | 558 | } |
| 559 | 559 | } |
| 560 | 560 | |
| 561 | - return apply_filters( 'wpinv_is_discount_max_met', $return, $code_id ); |
|
| 561 | + return apply_filters('wpinv_is_discount_max_met', $return, $code_id); |
|
| 562 | 562 | } |
| 563 | 563 | |
| 564 | -function wpinv_discount_is_single_use( $code_id = 0 ) { |
|
| 565 | - $single_use = get_post_meta( $code_id, '_wpi_discount_is_single_use', true ); |
|
| 566 | - return (bool) apply_filters( 'wpinv_is_discount_single_use', $single_use, $code_id ); |
|
| 564 | +function wpinv_discount_is_single_use($code_id = 0) { |
|
| 565 | + $single_use = get_post_meta($code_id, '_wpi_discount_is_single_use', true); |
|
| 566 | + return (bool)apply_filters('wpinv_is_discount_single_use', $single_use, $code_id); |
|
| 567 | 567 | } |
| 568 | 568 | |
| 569 | -function wpinv_discount_is_recurring( $code_id = 0, $code = false ) { |
|
| 570 | - if ( $code ) { |
|
| 571 | - $discount = wpinv_get_discount_by_code( $code_id ); |
|
| 569 | +function wpinv_discount_is_recurring($code_id = 0, $code = false) { |
|
| 570 | + if ($code) { |
|
| 571 | + $discount = wpinv_get_discount_by_code($code_id); |
|
| 572 | 572 | |
| 573 | - if ( !empty( $discount ) ) { |
|
| 573 | + if (!empty($discount)) { |
|
| 574 | 574 | $code_id = $discount->ID; |
| 575 | 575 | } |
| 576 | 576 | } |
| 577 | 577 | |
| 578 | - $recurring = get_post_meta( $code_id, '_wpi_discount_is_recurring', true ); |
|
| 578 | + $recurring = get_post_meta($code_id, '_wpi_discount_is_recurring', true); |
|
| 579 | 579 | |
| 580 | - return (bool) apply_filters( 'wpinv_is_discount_recurring', $recurring, $code_id, $code ); |
|
| 580 | + return (bool)apply_filters('wpinv_is_discount_recurring', $recurring, $code_id, $code); |
|
| 581 | 581 | } |
| 582 | 582 | |
| 583 | -function wpinv_discount_item_reqs_met( $code_id = null ) { |
|
| 584 | - $item_reqs = wpinv_get_discount_item_reqs( $code_id ); |
|
| 585 | - $condition = wpinv_get_discount_item_condition( $code_id ); |
|
| 586 | - $excluded_ps = wpinv_get_discount_excluded_items( $code_id ); |
|
| 583 | +function wpinv_discount_item_reqs_met($code_id = null) { |
|
| 584 | + $item_reqs = wpinv_get_discount_item_reqs($code_id); |
|
| 585 | + $condition = wpinv_get_discount_item_condition($code_id); |
|
| 586 | + $excluded_ps = wpinv_get_discount_excluded_items($code_id); |
|
| 587 | 587 | $cart_items = wpinv_get_cart_contents(); |
| 588 | - $cart_ids = $cart_items ? wp_list_pluck( $cart_items, 'id' ) : null; |
|
| 588 | + $cart_ids = $cart_items ? wp_list_pluck($cart_items, 'id') : null; |
|
| 589 | 589 | $ret = false; |
| 590 | 590 | |
| 591 | - if ( empty( $item_reqs ) && empty( $excluded_ps ) ) { |
|
| 591 | + if (empty($item_reqs) && empty($excluded_ps)) { |
|
| 592 | 592 | $ret = true; |
| 593 | 593 | } |
| 594 | 594 | |
| 595 | 595 | // Normalize our data for item requirements, exclusions and cart data |
| 596 | 596 | // First absint the items, then sort, and reset the array keys |
| 597 | - $item_reqs = array_map( 'absint', $item_reqs ); |
|
| 598 | - asort( $item_reqs ); |
|
| 599 | - $item_reqs = array_values( $item_reqs ); |
|
| 597 | + $item_reqs = array_map('absint', $item_reqs); |
|
| 598 | + asort($item_reqs); |
|
| 599 | + $item_reqs = array_values($item_reqs); |
|
| 600 | 600 | |
| 601 | - $excluded_ps = array_map( 'absint', $excluded_ps ); |
|
| 602 | - asort( $excluded_ps ); |
|
| 603 | - $excluded_ps = array_values( $excluded_ps ); |
|
| 601 | + $excluded_ps = array_map('absint', $excluded_ps); |
|
| 602 | + asort($excluded_ps); |
|
| 603 | + $excluded_ps = array_values($excluded_ps); |
|
| 604 | 604 | |
| 605 | - $cart_ids = array_map( 'absint', $cart_ids ); |
|
| 606 | - asort( $cart_ids ); |
|
| 607 | - $cart_ids = array_values( $cart_ids ); |
|
| 605 | + $cart_ids = array_map('absint', $cart_ids); |
|
| 606 | + asort($cart_ids); |
|
| 607 | + $cart_ids = array_values($cart_ids); |
|
| 608 | 608 | |
| 609 | 609 | // Ensure we have requirements before proceeding |
| 610 | - if ( !$ret && ! empty( $item_reqs ) ) { |
|
| 611 | - switch( $condition ) { |
|
| 610 | + if (!$ret && !empty($item_reqs)) { |
|
| 611 | + switch ($condition) { |
|
| 612 | 612 | case 'all' : |
| 613 | 613 | // Default back to true |
| 614 | 614 | $ret = true; |
| 615 | 615 | |
| 616 | - foreach ( $item_reqs as $item_id ) { |
|
| 617 | - if ( !wpinv_item_in_cart( $item_id ) ) { |
|
| 618 | - wpinv_set_error( 'wpinv-discount-error', __( 'The item requirements for this discount are not met.', 'invoicing' ) ); |
|
| 616 | + foreach ($item_reqs as $item_id) { |
|
| 617 | + if (!wpinv_item_in_cart($item_id)) { |
|
| 618 | + wpinv_set_error('wpinv-discount-error', __('The item requirements for this discount are not met.', 'invoicing')); |
|
| 619 | 619 | $ret = false; |
| 620 | 620 | break; |
| 621 | 621 | } |
@@ -624,15 +624,15 @@ discard block |
||
| 624 | 624 | break; |
| 625 | 625 | |
| 626 | 626 | default : // Any |
| 627 | - foreach ( $item_reqs as $item_id ) { |
|
| 628 | - if ( wpinv_item_in_cart( $item_id ) ) { |
|
| 627 | + foreach ($item_reqs as $item_id) { |
|
| 628 | + if (wpinv_item_in_cart($item_id)) { |
|
| 629 | 629 | $ret = true; |
| 630 | 630 | break; |
| 631 | 631 | } |
| 632 | 632 | } |
| 633 | 633 | |
| 634 | - if( ! $ret ) { |
|
| 635 | - wpinv_set_error( 'wpinv-discount-error', __( 'The item requirements for this discount are not met.', 'invoicing' ) ); |
|
| 634 | + if (!$ret) { |
|
| 635 | + wpinv_set_error('wpinv-discount-error', __('The item requirements for this discount are not met.', 'invoicing')); |
|
| 636 | 636 | } |
| 637 | 637 | |
| 638 | 638 | break; |
@@ -641,70 +641,70 @@ discard block |
||
| 641 | 641 | $ret = true; |
| 642 | 642 | } |
| 643 | 643 | |
| 644 | - if( ! empty( $excluded_ps ) ) { |
|
| 644 | + if (!empty($excluded_ps)) { |
|
| 645 | 645 | // Check that there are items other than excluded ones in the cart |
| 646 | - if( $cart_ids == $excluded_ps ) { |
|
| 647 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is not valid for the cart contents.', 'invoicing' ) ); |
|
| 646 | + if ($cart_ids == $excluded_ps) { |
|
| 647 | + wpinv_set_error('wpinv-discount-error', __('This discount is not valid for the cart contents.', 'invoicing')); |
|
| 648 | 648 | $ret = false; |
| 649 | 649 | } |
| 650 | 650 | } |
| 651 | 651 | |
| 652 | - return (bool) apply_filters( 'wpinv_is_discount_item_req_met', $ret, $code_id, $condition ); |
|
| 652 | + return (bool)apply_filters('wpinv_is_discount_item_req_met', $ret, $code_id, $condition); |
|
| 653 | 653 | } |
| 654 | 654 | |
| 655 | -function wpinv_is_discount_used( $code = null, $user = '', $code_id = 0 ) { |
|
| 655 | +function wpinv_is_discount_used($code = null, $user = '', $code_id = 0) { |
|
| 656 | 656 | global $wpi_checkout_id; |
| 657 | 657 | |
| 658 | 658 | $return = false; |
| 659 | 659 | |
| 660 | - if ( empty( $code_id ) ) { |
|
| 661 | - $code_id = wpinv_get_discount_id_by_code( $code ); |
|
| 660 | + if (empty($code_id)) { |
|
| 661 | + $code_id = wpinv_get_discount_id_by_code($code); |
|
| 662 | 662 | |
| 663 | - if( empty( $code_id ) ) { |
|
| 663 | + if (empty($code_id)) { |
|
| 664 | 664 | return false; // No discount was found |
| 665 | 665 | } |
| 666 | 666 | } |
| 667 | 667 | |
| 668 | - if ( wpinv_discount_is_single_use( $code_id ) ) { |
|
| 668 | + if (wpinv_discount_is_single_use($code_id)) { |
|
| 669 | 669 | $payments = array(); |
| 670 | 670 | |
| 671 | 671 | $user_id = 0; |
| 672 | - if ( is_int( $user ) ) { |
|
| 673 | - $user_id = absint( $user ); |
|
| 674 | - } else if ( is_email( $user ) && $user_data = get_user_by( 'email', $user ) ) { |
|
| 672 | + if (is_int($user)) { |
|
| 673 | + $user_id = absint($user); |
|
| 674 | + } else if (is_email($user) && $user_data = get_user_by('email', $user)) { |
|
| 675 | 675 | $user_id = $user_data->ID; |
| 676 | - } else if ( $user_data = get_user_by( 'login', $user ) ) { |
|
| 676 | + } else if ($user_data = get_user_by('login', $user)) { |
|
| 677 | 677 | $user_id = $user_data->ID; |
| 678 | - } else if ( absint( $user ) > 0 ) { |
|
| 679 | - $user_id = absint( $user ); |
|
| 678 | + } else if (absint($user) > 0) { |
|
| 679 | + $user_id = absint($user); |
|
| 680 | 680 | } |
| 681 | 681 | |
| 682 | - if ( !empty( $user_id ) ) { |
|
| 683 | - $query = array( 'user' => $user_id, 'limit' => false ); |
|
| 684 | - $payments = wpinv_get_invoices( $query ); // Get all payments with matching user id |
|
| 682 | + if (!empty($user_id)) { |
|
| 683 | + $query = array('user' => $user_id, 'limit' => false); |
|
| 684 | + $payments = wpinv_get_invoices($query); // Get all payments with matching user id |
|
| 685 | 685 | } |
| 686 | 686 | |
| 687 | - if ( $payments ) { |
|
| 688 | - foreach ( $payments as $payment ) { |
|
| 687 | + if ($payments) { |
|
| 688 | + foreach ($payments as $payment) { |
|
| 689 | 689 | // Don't count discount used for current invoice chekcout. |
| 690 | - if ( !empty( $wpi_checkout_id ) && $wpi_checkout_id == $payment->ID ) { |
|
| 690 | + if (!empty($wpi_checkout_id) && $wpi_checkout_id == $payment->ID) { |
|
| 691 | 691 | continue; |
| 692 | 692 | } |
| 693 | 693 | |
| 694 | - if ( $payment->has_status( array( 'wpi-cancelled', 'wpi-failed' ) ) ) { |
|
| 694 | + if ($payment->has_status(array('wpi-cancelled', 'wpi-failed'))) { |
|
| 695 | 695 | continue; |
| 696 | 696 | } |
| 697 | 697 | |
| 698 | - $discounts = $payment->get_discounts( true ); |
|
| 699 | - if ( empty( $discounts ) ) { |
|
| 698 | + $discounts = $payment->get_discounts(true); |
|
| 699 | + if (empty($discounts)) { |
|
| 700 | 700 | continue; |
| 701 | 701 | } |
| 702 | 702 | |
| 703 | - $discounts = $discounts && !is_array( $discounts ) ? explode( ',', $discounts ) : $discounts; |
|
| 703 | + $discounts = $discounts && !is_array($discounts) ? explode(',', $discounts) : $discounts; |
|
| 704 | 704 | |
| 705 | - if ( !empty( $discounts ) && is_array( $discounts ) ) { |
|
| 706 | - if ( in_array( strtolower( $code ), array_map( 'strtolower', $discounts ) ) ) { |
|
| 707 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount has already been redeemed.', 'invoicing' ) ); |
|
| 705 | + if (!empty($discounts) && is_array($discounts)) { |
|
| 706 | + if (in_array(strtolower($code), array_map('strtolower', $discounts))) { |
|
| 707 | + wpinv_set_error('wpinv-discount-error', __('This discount has already been redeemed.', 'invoicing')); |
|
| 708 | 708 | $return = true; |
| 709 | 709 | break; |
| 710 | 710 | } |
@@ -713,61 +713,61 @@ discard block |
||
| 713 | 713 | } |
| 714 | 714 | } |
| 715 | 715 | |
| 716 | - return apply_filters( 'wpinv_is_discount_used', $return, $code, $user ); |
|
| 716 | + return apply_filters('wpinv_is_discount_used', $return, $code, $user); |
|
| 717 | 717 | } |
| 718 | 718 | |
| 719 | -function wpinv_is_discount_valid( $code = '', $user = '', $set_error = true ) { |
|
| 719 | +function wpinv_is_discount_valid($code = '', $user = '', $set_error = true) { |
|
| 720 | 720 | $return = false; |
| 721 | - $discount_id = wpinv_get_discount_id_by_code( $code ); |
|
| 722 | - $user = trim( $user ); |
|
| 721 | + $discount_id = wpinv_get_discount_id_by_code($code); |
|
| 722 | + $user = trim($user); |
|
| 723 | 723 | |
| 724 | - if ( wpinv_get_cart_contents() ) { |
|
| 725 | - if ( $discount_id ) { |
|
| 724 | + if (wpinv_get_cart_contents()) { |
|
| 725 | + if ($discount_id) { |
|
| 726 | 726 | if ( |
| 727 | - wpinv_is_discount_active( $discount_id ) && |
|
| 728 | - wpinv_check_discount_dates( $discount_id ) && |
|
| 729 | - !wpinv_is_discount_maxed_out( $discount_id ) && |
|
| 730 | - !wpinv_is_discount_used( $code, $user, $discount_id ) && |
|
| 731 | - wpinv_discount_is_min_met( $discount_id ) && |
|
| 732 | - wpinv_discount_is_max_met( $discount_id ) && |
|
| 733 | - wpinv_discount_item_reqs_met( $discount_id ) |
|
| 727 | + wpinv_is_discount_active($discount_id) && |
|
| 728 | + wpinv_check_discount_dates($discount_id) && |
|
| 729 | + !wpinv_is_discount_maxed_out($discount_id) && |
|
| 730 | + !wpinv_is_discount_used($code, $user, $discount_id) && |
|
| 731 | + wpinv_discount_is_min_met($discount_id) && |
|
| 732 | + wpinv_discount_is_max_met($discount_id) && |
|
| 733 | + wpinv_discount_item_reqs_met($discount_id) |
|
| 734 | 734 | ) { |
| 735 | 735 | $return = true; |
| 736 | 736 | } |
| 737 | - } elseif( $set_error ) { |
|
| 738 | - wpinv_set_error( 'wpinv-discount-error', __( 'This discount is invalid.', 'invoicing' ) ); |
|
| 737 | + } elseif ($set_error) { |
|
| 738 | + wpinv_set_error('wpinv-discount-error', __('This discount is invalid.', 'invoicing')); |
|
| 739 | 739 | } |
| 740 | 740 | } |
| 741 | 741 | |
| 742 | - return apply_filters( 'wpinv_is_discount_valid', $return, $discount_id, $code, $user ); |
|
| 742 | + return apply_filters('wpinv_is_discount_valid', $return, $discount_id, $code, $user); |
|
| 743 | 743 | } |
| 744 | 744 | |
| 745 | -function wpinv_get_discount_id_by_code( $code ) { |
|
| 746 | - $discount = wpinv_get_discount_by_code( $code ); |
|
| 747 | - if( $discount ) { |
|
| 745 | +function wpinv_get_discount_id_by_code($code) { |
|
| 746 | + $discount = wpinv_get_discount_by_code($code); |
|
| 747 | + if ($discount) { |
|
| 748 | 748 | return $discount->ID; |
| 749 | 749 | } |
| 750 | 750 | return false; |
| 751 | 751 | } |
| 752 | 752 | |
| 753 | -function wpinv_get_discounted_amount( $code, $base_price ) { |
|
| 753 | +function wpinv_get_discounted_amount($code, $base_price) { |
|
| 754 | 754 | $amount = $base_price; |
| 755 | - $discount_id = wpinv_get_discount_id_by_code( $code ); |
|
| 755 | + $discount_id = wpinv_get_discount_id_by_code($code); |
|
| 756 | 756 | |
| 757 | - if( $discount_id ) { |
|
| 758 | - $type = wpinv_get_discount_type( $discount_id ); |
|
| 759 | - $rate = wpinv_get_discount_amount( $discount_id ); |
|
| 757 | + if ($discount_id) { |
|
| 758 | + $type = wpinv_get_discount_type($discount_id); |
|
| 759 | + $rate = wpinv_get_discount_amount($discount_id); |
|
| 760 | 760 | |
| 761 | - if ( $type == 'flat' ) { |
|
| 761 | + if ($type == 'flat') { |
|
| 762 | 762 | // Set amount |
| 763 | 763 | $amount = $base_price - $rate; |
| 764 | - if ( $amount < 0 ) { |
|
| 764 | + if ($amount < 0) { |
|
| 765 | 765 | $amount = 0; |
| 766 | 766 | } |
| 767 | 767 | |
| 768 | 768 | } else { |
| 769 | 769 | // Percentage discount |
| 770 | - $amount = $base_price - ( $base_price * ( $rate / 100 ) ); |
|
| 770 | + $amount = $base_price - ($base_price * ($rate / 100)); |
|
| 771 | 771 | } |
| 772 | 772 | |
| 773 | 773 | } else { |
@@ -776,108 +776,108 @@ discard block |
||
| 776 | 776 | |
| 777 | 777 | } |
| 778 | 778 | |
| 779 | - return apply_filters( 'wpinv_discounted_amount', $amount ); |
|
| 779 | + return apply_filters('wpinv_discounted_amount', $amount); |
|
| 780 | 780 | } |
| 781 | 781 | |
| 782 | -function wpinv_increase_discount_usage( $code ) { |
|
| 782 | +function wpinv_increase_discount_usage($code) { |
|
| 783 | 783 | |
| 784 | - $id = wpinv_get_discount_id_by_code( $code ); |
|
| 785 | - $uses = wpinv_get_discount_uses( $id ); |
|
| 784 | + $id = wpinv_get_discount_id_by_code($code); |
|
| 785 | + $uses = wpinv_get_discount_uses($id); |
|
| 786 | 786 | |
| 787 | - if ( $uses ) { |
|
| 787 | + if ($uses) { |
|
| 788 | 788 | $uses++; |
| 789 | 789 | } else { |
| 790 | 790 | $uses = 1; |
| 791 | 791 | } |
| 792 | 792 | |
| 793 | - update_post_meta( $id, '_wpi_discount_uses', $uses ); |
|
| 793 | + update_post_meta($id, '_wpi_discount_uses', $uses); |
|
| 794 | 794 | |
| 795 | - do_action( 'wpinv_discount_increase_use_count', $uses, $id, $code ); |
|
| 795 | + do_action('wpinv_discount_increase_use_count', $uses, $id, $code); |
|
| 796 | 796 | |
| 797 | 797 | return $uses; |
| 798 | 798 | |
| 799 | 799 | } |
| 800 | 800 | |
| 801 | -function wpinv_decrease_discount_usage( $code ) { |
|
| 801 | +function wpinv_decrease_discount_usage($code) { |
|
| 802 | 802 | |
| 803 | - $id = wpinv_get_discount_id_by_code( $code ); |
|
| 804 | - $uses = wpinv_get_discount_uses( $id ); |
|
| 803 | + $id = wpinv_get_discount_id_by_code($code); |
|
| 804 | + $uses = wpinv_get_discount_uses($id); |
|
| 805 | 805 | |
| 806 | - if ( $uses ) { |
|
| 806 | + if ($uses) { |
|
| 807 | 807 | $uses--; |
| 808 | 808 | } |
| 809 | 809 | |
| 810 | - if ( $uses < 0 ) { |
|
| 810 | + if ($uses < 0) { |
|
| 811 | 811 | $uses = 0; |
| 812 | 812 | } |
| 813 | 813 | |
| 814 | - update_post_meta( $id, '_wpi_discount_uses', $uses ); |
|
| 814 | + update_post_meta($id, '_wpi_discount_uses', $uses); |
|
| 815 | 815 | |
| 816 | - do_action( 'wpinv_discount_decrease_use_count', $uses, $id, $code ); |
|
| 816 | + do_action('wpinv_discount_decrease_use_count', $uses, $id, $code); |
|
| 817 | 817 | |
| 818 | 818 | return $uses; |
| 819 | 819 | |
| 820 | 820 | } |
| 821 | 821 | |
| 822 | -function wpinv_format_discount_rate( $type, $amount ) { |
|
| 823 | - if ( $type == 'flat' ) { |
|
| 824 | - return wpinv_price( wpinv_format_amount( $amount ) ); |
|
| 822 | +function wpinv_format_discount_rate($type, $amount) { |
|
| 823 | + if ($type == 'flat') { |
|
| 824 | + return wpinv_price(wpinv_format_amount($amount)); |
|
| 825 | 825 | } else { |
| 826 | 826 | return $amount . '%'; |
| 827 | 827 | } |
| 828 | 828 | } |
| 829 | 829 | |
| 830 | -function wpinv_set_cart_discount( $code = '' ) { |
|
| 831 | - if ( wpinv_multiple_discounts_allowed() ) { |
|
| 830 | +function wpinv_set_cart_discount($code = '') { |
|
| 831 | + if (wpinv_multiple_discounts_allowed()) { |
|
| 832 | 832 | // Get all active cart discounts |
| 833 | 833 | $discounts = wpinv_get_cart_discounts(); |
| 834 | 834 | } else { |
| 835 | 835 | $discounts = false; // Only one discount allowed per purchase, so override any existing |
| 836 | 836 | } |
| 837 | 837 | |
| 838 | - if ( $discounts ) { |
|
| 839 | - $key = array_search( strtolower( $code ), array_map( 'strtolower', $discounts ) ); |
|
| 840 | - if( false !== $key ) { |
|
| 841 | - unset( $discounts[ $key ] ); // Can't set the same discount more than once |
|
| 838 | + if ($discounts) { |
|
| 839 | + $key = array_search(strtolower($code), array_map('strtolower', $discounts)); |
|
| 840 | + if (false !== $key) { |
|
| 841 | + unset($discounts[$key]); // Can't set the same discount more than once |
|
| 842 | 842 | } |
| 843 | 843 | $discounts[] = $code; |
| 844 | 844 | } else { |
| 845 | 845 | $discounts = array(); |
| 846 | 846 | $discounts[] = $code; |
| 847 | 847 | } |
| 848 | - $discounts = array_values( $discounts ); |
|
| 848 | + $discounts = array_values($discounts); |
|
| 849 | 849 | |
| 850 | 850 | $data = wpinv_get_checkout_session(); |
| 851 | - if ( empty( $data ) ) { |
|
| 851 | + if (empty($data)) { |
|
| 852 | 852 | $data = array(); |
| 853 | 853 | } else { |
| 854 | - if ( !empty( $data['invoice_id'] ) && $payment_meta = wpinv_get_invoice_meta( $data['invoice_id'] ) ) { |
|
| 855 | - $payment_meta['user_info']['discount'] = implode( ',', $discounts ); |
|
| 856 | - update_post_meta( $data['invoice_id'], '_wpinv_payment_meta', $payment_meta ); |
|
| 854 | + if (!empty($data['invoice_id']) && $payment_meta = wpinv_get_invoice_meta($data['invoice_id'])) { |
|
| 855 | + $payment_meta['user_info']['discount'] = implode(',', $discounts); |
|
| 856 | + update_post_meta($data['invoice_id'], '_wpinv_payment_meta', $payment_meta); |
|
| 857 | 857 | } |
| 858 | 858 | } |
| 859 | 859 | $data['cart_discounts'] = $discounts; |
| 860 | 860 | |
| 861 | - wpinv_set_checkout_session( $data ); |
|
| 861 | + wpinv_set_checkout_session($data); |
|
| 862 | 862 | |
| 863 | 863 | return $discounts; |
| 864 | 864 | } |
| 865 | 865 | |
| 866 | -function wpinv_unset_cart_discount( $code = '' ) { |
|
| 866 | +function wpinv_unset_cart_discount($code = '') { |
|
| 867 | 867 | $discounts = wpinv_get_cart_discounts(); |
| 868 | 868 | |
| 869 | - if ( $code && !empty( $discounts ) && in_array( $code, $discounts ) ) { |
|
| 870 | - $key = array_search( $code, $discounts ); |
|
| 871 | - unset( $discounts[ $key ] ); |
|
| 869 | + if ($code && !empty($discounts) && in_array($code, $discounts)) { |
|
| 870 | + $key = array_search($code, $discounts); |
|
| 871 | + unset($discounts[$key]); |
|
| 872 | 872 | |
| 873 | 873 | $data = wpinv_get_checkout_session(); |
| 874 | 874 | $data['cart_discounts'] = $discounts; |
| 875 | - if ( !empty( $data['invoice_id'] ) && $payment_meta = wpinv_get_invoice_meta( $data['invoice_id'] ) ) { |
|
| 876 | - $payment_meta['user_info']['discount'] = !empty( $discounts ) ? implode( ',', $discounts ) : ''; |
|
| 877 | - update_post_meta( $data['invoice_id'], '_wpinv_payment_meta', $payment_meta ); |
|
| 875 | + if (!empty($data['invoice_id']) && $payment_meta = wpinv_get_invoice_meta($data['invoice_id'])) { |
|
| 876 | + $payment_meta['user_info']['discount'] = !empty($discounts) ? implode(',', $discounts) : ''; |
|
| 877 | + update_post_meta($data['invoice_id'], '_wpinv_payment_meta', $payment_meta); |
|
| 878 | 878 | } |
| 879 | 879 | |
| 880 | - wpinv_set_checkout_session( $data ); |
|
| 880 | + wpinv_set_checkout_session($data); |
|
| 881 | 881 | } |
| 882 | 882 | |
| 883 | 883 | return $discounts; |
@@ -886,27 +886,27 @@ discard block |
||
| 886 | 886 | function wpinv_unset_all_cart_discounts() { |
| 887 | 887 | $data = wpinv_get_checkout_session(); |
| 888 | 888 | |
| 889 | - if ( !empty( $data ) && isset( $data['cart_discounts'] ) ) { |
|
| 890 | - unset( $data['cart_discounts'] ); |
|
| 889 | + if (!empty($data) && isset($data['cart_discounts'])) { |
|
| 890 | + unset($data['cart_discounts']); |
|
| 891 | 891 | |
| 892 | - wpinv_set_checkout_session( $data ); |
|
| 892 | + wpinv_set_checkout_session($data); |
|
| 893 | 893 | return true; |
| 894 | 894 | } |
| 895 | 895 | |
| 896 | 896 | return false; |
| 897 | 897 | } |
| 898 | 898 | |
| 899 | -function wpinv_get_cart_discounts( $items = array() ) { |
|
| 899 | +function wpinv_get_cart_discounts($items = array()) { |
|
| 900 | 900 | $session = wpinv_get_checkout_session(); |
| 901 | 901 | |
| 902 | - $discounts = !empty( $session['cart_discounts'] ) ? $session['cart_discounts'] : false; |
|
| 902 | + $discounts = !empty($session['cart_discounts']) ? $session['cart_discounts'] : false; |
|
| 903 | 903 | return $discounts; |
| 904 | 904 | } |
| 905 | 905 | |
| 906 | -function wpinv_cart_has_discounts( $items = array() ) { |
|
| 906 | +function wpinv_cart_has_discounts($items = array()) { |
|
| 907 | 907 | $ret = false; |
| 908 | 908 | |
| 909 | - if ( wpinv_get_cart_discounts( $items ) ) { |
|
| 909 | + if (wpinv_get_cart_discounts($items)) { |
|
| 910 | 910 | $ret = true; |
| 911 | 911 | } |
| 912 | 912 | |
@@ -917,131 +917,131 @@ discard block |
||
| 917 | 917 | } |
| 918 | 918 | */ |
| 919 | 919 | |
| 920 | - return apply_filters( 'wpinv_cart_has_discounts', $ret ); |
|
| 920 | + return apply_filters('wpinv_cart_has_discounts', $ret); |
|
| 921 | 921 | } |
| 922 | 922 | |
| 923 | -function wpinv_get_cart_discounted_amount( $items = array(), $discounts = false ) { |
|
| 923 | +function wpinv_get_cart_discounted_amount($items = array(), $discounts = false) { |
|
| 924 | 924 | $amount = 0.00; |
| 925 | - $items = !empty( $items ) ? $items : wpinv_get_cart_content_details(); |
|
| 925 | + $items = !empty($items) ? $items : wpinv_get_cart_content_details(); |
|
| 926 | 926 | |
| 927 | - if ( $items ) { |
|
| 928 | - $discounts = wp_list_pluck( $items, 'discount' ); |
|
| 927 | + if ($items) { |
|
| 928 | + $discounts = wp_list_pluck($items, 'discount'); |
|
| 929 | 929 | |
| 930 | - if ( is_array( $discounts ) ) { |
|
| 931 | - $discounts = array_map( 'floatval', $discounts ); |
|
| 932 | - $amount = array_sum( $discounts ); |
|
| 930 | + if (is_array($discounts)) { |
|
| 931 | + $discounts = array_map('floatval', $discounts); |
|
| 932 | + $amount = array_sum($discounts); |
|
| 933 | 933 | } |
| 934 | 934 | } |
| 935 | 935 | |
| 936 | - return apply_filters( 'wpinv_get_cart_discounted_amount', $amount ); |
|
| 936 | + return apply_filters('wpinv_get_cart_discounted_amount', $amount); |
|
| 937 | 937 | } |
| 938 | 938 | |
| 939 | -function wpinv_get_cart_items_discount_amount( $items = array(), $discount = false ) { |
|
| 940 | - $items = !empty( $items ) ? $items : wpinv_get_cart_content_details(); |
|
| 939 | +function wpinv_get_cart_items_discount_amount($items = array(), $discount = false) { |
|
| 940 | + $items = !empty($items) ? $items : wpinv_get_cart_content_details(); |
|
| 941 | 941 | |
| 942 | - if ( empty( $discount ) || empty( $items ) ) { |
|
| 942 | + if (empty($discount) || empty($items)) { |
|
| 943 | 943 | return 0; |
| 944 | 944 | } |
| 945 | 945 | |
| 946 | 946 | $amount = 0; |
| 947 | 947 | |
| 948 | - foreach ( $items as $item ) { |
|
| 949 | - $amount += wpinv_get_cart_item_discount_amount( $item, $discount ); |
|
| 948 | + foreach ($items as $item) { |
|
| 949 | + $amount += wpinv_get_cart_item_discount_amount($item, $discount); |
|
| 950 | 950 | } |
| 951 | 951 | |
| 952 | - $amount = wpinv_round_amount( $amount ); |
|
| 952 | + $amount = wpinv_round_amount($amount); |
|
| 953 | 953 | |
| 954 | 954 | return $amount; |
| 955 | 955 | } |
| 956 | 956 | |
| 957 | -function wpinv_get_cart_item_discount_amount( $item = array(), $discount = false ) { |
|
| 957 | +function wpinv_get_cart_item_discount_amount($item = array(), $discount = false) { |
|
| 958 | 958 | global $wpinv_is_last_cart_item, $wpinv_flat_discount_total; |
| 959 | 959 | |
| 960 | 960 | $amount = 0; |
| 961 | 961 | |
| 962 | - if ( empty( $item ) || empty( $item['id'] ) ) { |
|
| 962 | + if (empty($item) || empty($item['id'])) { |
|
| 963 | 963 | return $amount; |
| 964 | 964 | } |
| 965 | 965 | |
| 966 | - if ( empty( $item['quantity'] ) ) { |
|
| 966 | + if (empty($item['quantity'])) { |
|
| 967 | 967 | return $amount; |
| 968 | 968 | } |
| 969 | 969 | |
| 970 | - if ( empty( $item['options'] ) ) { |
|
| 970 | + if (empty($item['options'])) { |
|
| 971 | 971 | $item['options'] = array(); |
| 972 | 972 | } |
| 973 | 973 | |
| 974 | - $price = wpinv_get_cart_item_price( $item['id'], $item, $item['options'] ); |
|
| 974 | + $price = wpinv_get_cart_item_price($item['id'], $item, $item['options']); |
|
| 975 | 975 | $discounted_price = $price; |
| 976 | 976 | |
| 977 | 977 | $discounts = false === $discount ? wpinv_get_cart_discounts() : $discount; |
| 978 | - if ( empty( $discounts ) ) { |
|
| 978 | + if (empty($discounts)) { |
|
| 979 | 979 | return $amount; |
| 980 | 980 | } |
| 981 | 981 | |
| 982 | - if ( $discounts ) { |
|
| 983 | - if ( is_array( $discounts ) ) { |
|
| 984 | - $discounts = array_values( $discounts ); |
|
| 982 | + if ($discounts) { |
|
| 983 | + if (is_array($discounts)) { |
|
| 984 | + $discounts = array_values($discounts); |
|
| 985 | 985 | } else { |
| 986 | - $discounts = explode( ',', $discounts ); |
|
| 986 | + $discounts = explode(',', $discounts); |
|
| 987 | 987 | } |
| 988 | 988 | } |
| 989 | 989 | |
| 990 | - if( $discounts ) { |
|
| 991 | - foreach ( $discounts as $discount ) { |
|
| 992 | - $code_id = wpinv_get_discount_id_by_code( $discount ); |
|
| 990 | + if ($discounts) { |
|
| 991 | + foreach ($discounts as $discount) { |
|
| 992 | + $code_id = wpinv_get_discount_id_by_code($discount); |
|
| 993 | 993 | |
| 994 | 994 | // Check discount exists |
| 995 | - if( ! $code_id ) { |
|
| 995 | + if (!$code_id) { |
|
| 996 | 996 | continue; |
| 997 | 997 | } |
| 998 | 998 | |
| 999 | - $reqs = wpinv_get_discount_item_reqs( $code_id ); |
|
| 1000 | - $excluded_items = wpinv_get_discount_excluded_items( $code_id ); |
|
| 999 | + $reqs = wpinv_get_discount_item_reqs($code_id); |
|
| 1000 | + $excluded_items = wpinv_get_discount_excluded_items($code_id); |
|
| 1001 | 1001 | |
| 1002 | 1002 | // Make sure requirements are set and that this discount shouldn't apply to the whole cart |
| 1003 | - if ( !empty( $reqs ) && wpinv_is_discount_not_global( $code_id ) ) { |
|
| 1004 | - foreach ( $reqs as $item_id ) { |
|
| 1005 | - if ( $item_id == $item['id'] && ! in_array( $item['id'], $excluded_items ) ) { |
|
| 1006 | - $discounted_price -= $price - wpinv_get_discounted_amount( $discount, $price ); |
|
| 1003 | + if (!empty($reqs) && wpinv_is_discount_not_global($code_id)) { |
|
| 1004 | + foreach ($reqs as $item_id) { |
|
| 1005 | + if ($item_id == $item['id'] && !in_array($item['id'], $excluded_items)) { |
|
| 1006 | + $discounted_price -= $price - wpinv_get_discounted_amount($discount, $price); |
|
| 1007 | 1007 | } |
| 1008 | 1008 | } |
| 1009 | 1009 | } else { |
| 1010 | 1010 | // This is a global cart discount |
| 1011 | - if ( !in_array( $item['id'], $excluded_items ) ) { |
|
| 1012 | - if ( 'flat' === wpinv_get_discount_type( $code_id ) ) { |
|
| 1011 | + if (!in_array($item['id'], $excluded_items)) { |
|
| 1012 | + if ('flat' === wpinv_get_discount_type($code_id)) { |
|
| 1013 | 1013 | $items_subtotal = 0.00; |
| 1014 | 1014 | $cart_items = wpinv_get_cart_contents(); |
| 1015 | 1015 | |
| 1016 | - foreach ( $cart_items as $cart_item ) { |
|
| 1017 | - if ( ! in_array( $cart_item['id'], $excluded_items ) ) { |
|
| 1018 | - $options = !empty( $cart_item['options'] ) ? $cart_item['options'] : array(); |
|
| 1019 | - $item_price = wpinv_get_cart_item_price( $cart_item['id'], $cart_item, $options ); |
|
| 1016 | + foreach ($cart_items as $cart_item) { |
|
| 1017 | + if (!in_array($cart_item['id'], $excluded_items)) { |
|
| 1018 | + $options = !empty($cart_item['options']) ? $cart_item['options'] : array(); |
|
| 1019 | + $item_price = wpinv_get_cart_item_price($cart_item['id'], $cart_item, $options); |
|
| 1020 | 1020 | $items_subtotal += $item_price * $cart_item['quantity']; |
| 1021 | 1021 | } |
| 1022 | 1022 | } |
| 1023 | 1023 | |
| 1024 | - $subtotal_percent = ( ( $price * $item['quantity'] ) / $items_subtotal ); |
|
| 1025 | - $code_amount = wpinv_get_discount_amount( $code_id ); |
|
| 1024 | + $subtotal_percent = (($price * $item['quantity']) / $items_subtotal); |
|
| 1025 | + $code_amount = wpinv_get_discount_amount($code_id); |
|
| 1026 | 1026 | $discounted_amount = $code_amount * $subtotal_percent; |
| 1027 | 1027 | $discounted_price -= $discounted_amount; |
| 1028 | 1028 | |
| 1029 | - $wpinv_flat_discount_total += round( $discounted_amount, wpinv_currency_decimal_filter() ); |
|
| 1029 | + $wpinv_flat_discount_total += round($discounted_amount, wpinv_currency_decimal_filter()); |
|
| 1030 | 1030 | |
| 1031 | - if ( $wpinv_is_last_cart_item && $wpinv_flat_discount_total < $code_amount ) { |
|
| 1031 | + if ($wpinv_is_last_cart_item && $wpinv_flat_discount_total < $code_amount) { |
|
| 1032 | 1032 | $adjustment = $code_amount - $wpinv_flat_discount_total; |
| 1033 | 1033 | $discounted_price -= $adjustment; |
| 1034 | 1034 | } |
| 1035 | 1035 | } else { |
| 1036 | - $discounted_price -= $price - wpinv_get_discounted_amount( $discount, $price ); |
|
| 1036 | + $discounted_price -= $price - wpinv_get_discounted_amount($discount, $price); |
|
| 1037 | 1037 | } |
| 1038 | 1038 | } |
| 1039 | 1039 | } |
| 1040 | 1040 | } |
| 1041 | 1041 | |
| 1042 | - $amount = ( $price - apply_filters( 'wpinv_get_cart_item_discounted_amount', $discounted_price, $discounts, $item, $price ) ); |
|
| 1042 | + $amount = ($price - apply_filters('wpinv_get_cart_item_discounted_amount', $discounted_price, $discounts, $item, $price)); |
|
| 1043 | 1043 | |
| 1044 | - if ( 'flat' !== wpinv_get_discount_type( $code_id ) ) { |
|
| 1044 | + if ('flat' !== wpinv_get_discount_type($code_id)) { |
|
| 1045 | 1045 | $amount = $amount * $item['quantity']; |
| 1046 | 1046 | } |
| 1047 | 1047 | } |
@@ -1049,59 +1049,59 @@ discard block |
||
| 1049 | 1049 | return $amount; |
| 1050 | 1050 | } |
| 1051 | 1051 | |
| 1052 | -function wpinv_cart_discounts_html( $items = array() ) { |
|
| 1053 | - echo wpinv_get_cart_discounts_html( $items ); |
|
| 1052 | +function wpinv_cart_discounts_html($items = array()) { |
|
| 1053 | + echo wpinv_get_cart_discounts_html($items); |
|
| 1054 | 1054 | } |
| 1055 | 1055 | |
| 1056 | -function wpinv_get_cart_discounts_html( $items = array(), $discounts = false ) { |
|
| 1056 | +function wpinv_get_cart_discounts_html($items = array(), $discounts = false) { |
|
| 1057 | 1057 | global $wpi_cart_columns; |
| 1058 | 1058 | |
| 1059 | - $items = !empty( $items ) ? $items : wpinv_get_cart_content_details(); |
|
| 1059 | + $items = !empty($items) ? $items : wpinv_get_cart_content_details(); |
|
| 1060 | 1060 | |
| 1061 | - if ( !$discounts ) { |
|
| 1062 | - $discounts = wpinv_get_cart_discounts( $items ); |
|
| 1061 | + if (!$discounts) { |
|
| 1062 | + $discounts = wpinv_get_cart_discounts($items); |
|
| 1063 | 1063 | } |
| 1064 | 1064 | |
| 1065 | - if ( !$discounts ) { |
|
| 1065 | + if (!$discounts) { |
|
| 1066 | 1066 | return; |
| 1067 | 1067 | } |
| 1068 | 1068 | |
| 1069 | - $discounts = is_array( $discounts ) ? $discounts : array( $discounts ); |
|
| 1069 | + $discounts = is_array($discounts) ? $discounts : array($discounts); |
|
| 1070 | 1070 | |
| 1071 | 1071 | $html = ''; |
| 1072 | 1072 | |
| 1073 | - foreach ( $discounts as $discount ) { |
|
| 1074 | - $discount_id = wpinv_get_discount_id_by_code( $discount ); |
|
| 1075 | - $discount_value = wpinv_get_discount_amount( $discount_id ); |
|
| 1076 | - $rate = wpinv_format_discount_rate( wpinv_get_discount_type( $discount_id ), $discount_value ); |
|
| 1077 | - $amount = wpinv_get_cart_items_discount_amount( $items, $discount ); |
|
| 1078 | - $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 | + foreach ($discounts as $discount) { |
|
| 1074 | + $discount_id = wpinv_get_discount_id_by_code($discount); |
|
| 1075 | + $discount_value = wpinv_get_discount_amount($discount_id); |
|
| 1076 | + $rate = wpinv_format_discount_rate(wpinv_get_discount_type($discount_id), $discount_value); |
|
| 1077 | + $amount = wpinv_get_cart_items_discount_amount($items, $discount); |
|
| 1078 | + $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> '; |
|
| 1079 | 1079 | |
| 1080 | 1080 | $html .= '<tr class="wpinv_cart_footer_row wpinv_cart_discount_row">'; |
| 1081 | 1081 | ob_start(); |
| 1082 | - do_action( 'wpinv_checkout_table_discount_first', $items ); |
|
| 1082 | + do_action('wpinv_checkout_table_discount_first', $items); |
|
| 1083 | 1083 | $html .= ob_get_clean(); |
| 1084 | - $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>'; |
|
| 1084 | + $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>'; |
|
| 1085 | 1085 | ob_start(); |
| 1086 | - do_action( 'wpinv_checkout_table_discount_last', $items ); |
|
| 1086 | + do_action('wpinv_checkout_table_discount_last', $items); |
|
| 1087 | 1087 | $html .= ob_get_clean(); |
| 1088 | 1088 | $html .= '</tr>'; |
| 1089 | 1089 | } |
| 1090 | 1090 | |
| 1091 | - return apply_filters( 'wpinv_get_cart_discounts_html', $html, $discounts, $rate ); |
|
| 1091 | + return apply_filters('wpinv_get_cart_discounts_html', $html, $discounts, $rate); |
|
| 1092 | 1092 | } |
| 1093 | 1093 | |
| 1094 | -function wpinv_display_cart_discount( $formatted = false, $echo = false ) { |
|
| 1094 | +function wpinv_display_cart_discount($formatted = false, $echo = false) { |
|
| 1095 | 1095 | $discounts = wpinv_get_cart_discounts(); |
| 1096 | 1096 | |
| 1097 | - if ( empty( $discounts ) ) { |
|
| 1097 | + if (empty($discounts)) { |
|
| 1098 | 1098 | return false; |
| 1099 | 1099 | } |
| 1100 | 1100 | |
| 1101 | - $discount_id = wpinv_get_discount_id_by_code( $discounts[0] ); |
|
| 1102 | - $amount = wpinv_format_discount_rate( wpinv_get_discount_type( $discount_id ), wpinv_get_discount_amount( $discount_id ) ); |
|
| 1101 | + $discount_id = wpinv_get_discount_id_by_code($discounts[0]); |
|
| 1102 | + $amount = wpinv_format_discount_rate(wpinv_get_discount_type($discount_id), wpinv_get_discount_amount($discount_id)); |
|
| 1103 | 1103 | |
| 1104 | - if ( $echo ) { |
|
| 1104 | + if ($echo) { |
|
| 1105 | 1105 | echo $amount; |
| 1106 | 1106 | } |
| 1107 | 1107 | |
@@ -1109,133 +1109,133 @@ discard block |
||
| 1109 | 1109 | } |
| 1110 | 1110 | |
| 1111 | 1111 | function wpinv_remove_cart_discount() { |
| 1112 | - if ( !isset( $_GET['discount_id'] ) || ! isset( $_GET['discount_code'] ) ) { |
|
| 1112 | + if (!isset($_GET['discount_id']) || !isset($_GET['discount_code'])) { |
|
| 1113 | 1113 | return; |
| 1114 | 1114 | } |
| 1115 | 1115 | |
| 1116 | - do_action( 'wpinv_pre_remove_cart_discount', absint( $_GET['discount_id'] ) ); |
|
| 1116 | + do_action('wpinv_pre_remove_cart_discount', absint($_GET['discount_id'])); |
|
| 1117 | 1117 | |
| 1118 | - wpinv_unset_cart_discount( urldecode( $_GET['discount_code'] ) ); |
|
| 1118 | + wpinv_unset_cart_discount(urldecode($_GET['discount_code'])); |
|
| 1119 | 1119 | |
| 1120 | - do_action( 'wpinv_post_remove_cart_discount', absint( $_GET['discount_id'] ) ); |
|
| 1120 | + do_action('wpinv_post_remove_cart_discount', absint($_GET['discount_id'])); |
|
| 1121 | 1121 | |
| 1122 | - wp_redirect( wpinv_get_checkout_uri() ); wpinv_die(); |
|
| 1122 | + wp_redirect(wpinv_get_checkout_uri()); wpinv_die(); |
|
| 1123 | 1123 | } |
| 1124 | -add_action( 'wpinv_remove_cart_discount', 'wpinv_remove_cart_discount' ); |
|
| 1124 | +add_action('wpinv_remove_cart_discount', 'wpinv_remove_cart_discount'); |
|
| 1125 | 1125 | |
| 1126 | -function wpinv_maybe_remove_cart_discount( $cart_key = 0 ) { |
|
| 1126 | +function wpinv_maybe_remove_cart_discount($cart_key = 0) { |
|
| 1127 | 1127 | $discounts = wpinv_get_cart_discounts(); |
| 1128 | 1128 | |
| 1129 | - if ( !$discounts ) { |
|
| 1129 | + if (!$discounts) { |
|
| 1130 | 1130 | return; |
| 1131 | 1131 | } |
| 1132 | 1132 | |
| 1133 | - foreach ( $discounts as $discount ) { |
|
| 1134 | - if ( !wpinv_is_discount_valid( $discount ) ) { |
|
| 1135 | - wpinv_unset_cart_discount( $discount ); |
|
| 1133 | + foreach ($discounts as $discount) { |
|
| 1134 | + if (!wpinv_is_discount_valid($discount)) { |
|
| 1135 | + wpinv_unset_cart_discount($discount); |
|
| 1136 | 1136 | } |
| 1137 | 1137 | } |
| 1138 | 1138 | } |
| 1139 | -add_action( 'wpinv_post_remove_from_cart', 'wpinv_maybe_remove_cart_discount' ); |
|
| 1139 | +add_action('wpinv_post_remove_from_cart', 'wpinv_maybe_remove_cart_discount'); |
|
| 1140 | 1140 | |
| 1141 | 1141 | function wpinv_multiple_discounts_allowed() { |
| 1142 | - $ret = wpinv_get_option( 'allow_multiple_discounts', false ); |
|
| 1143 | - return (bool) apply_filters( 'wpinv_multiple_discounts_allowed', $ret ); |
|
| 1142 | + $ret = wpinv_get_option('allow_multiple_discounts', false); |
|
| 1143 | + return (bool)apply_filters('wpinv_multiple_discounts_allowed', $ret); |
|
| 1144 | 1144 | } |
| 1145 | 1145 | |
| 1146 | 1146 | function wpinv_listen_for_cart_discount() { |
| 1147 | 1147 | global $wpi_session; |
| 1148 | 1148 | |
| 1149 | - if ( empty( $_REQUEST['discount'] ) || is_array( $_REQUEST['discount'] ) ) { |
|
| 1149 | + if (empty($_REQUEST['discount']) || is_array($_REQUEST['discount'])) { |
|
| 1150 | 1150 | return; |
| 1151 | 1151 | } |
| 1152 | 1152 | |
| 1153 | - $code = preg_replace('/[^a-zA-Z0-9-_]+/', '', $_REQUEST['discount'] ); |
|
| 1153 | + $code = preg_replace('/[^a-zA-Z0-9-_]+/', '', $_REQUEST['discount']); |
|
| 1154 | 1154 | |
| 1155 | - $wpi_session->set( 'preset_discount', $code ); |
|
| 1155 | + $wpi_session->set('preset_discount', $code); |
|
| 1156 | 1156 | } |
| 1157 | 1157 | //add_action( 'init', 'wpinv_listen_for_cart_discount', 0 ); |
| 1158 | 1158 | |
| 1159 | 1159 | function wpinv_apply_preset_discount() { |
| 1160 | 1160 | global $wpi_session; |
| 1161 | 1161 | |
| 1162 | - $code = $wpi_session->get( 'preset_discount' ); |
|
| 1162 | + $code = $wpi_session->get('preset_discount'); |
|
| 1163 | 1163 | |
| 1164 | - if ( !$code ) { |
|
| 1164 | + if (!$code) { |
|
| 1165 | 1165 | return; |
| 1166 | 1166 | } |
| 1167 | 1167 | |
| 1168 | - if ( !wpinv_is_discount_valid( $code, '', false ) ) { |
|
| 1168 | + if (!wpinv_is_discount_valid($code, '', false)) { |
|
| 1169 | 1169 | return; |
| 1170 | 1170 | } |
| 1171 | 1171 | |
| 1172 | - $code = apply_filters( 'wpinv_apply_preset_discount', $code ); |
|
| 1172 | + $code = apply_filters('wpinv_apply_preset_discount', $code); |
|
| 1173 | 1173 | |
| 1174 | - wpinv_set_cart_discount( $code ); |
|
| 1174 | + wpinv_set_cart_discount($code); |
|
| 1175 | 1175 | |
| 1176 | - $wpi_session->set( 'preset_discount', null ); |
|
| 1176 | + $wpi_session->set('preset_discount', null); |
|
| 1177 | 1177 | } |
| 1178 | 1178 | //add_action( 'init', 'wpinv_apply_preset_discount', 999 ); |
| 1179 | 1179 | |
| 1180 | -function wpinv_get_discount_label( $code, $echo = true ) { |
|
| 1181 | - $label = wp_sprintf( __( 'Discount%1$s', 'invoicing' ), ( $code != '' && $code != 'none' ? ' (<code>' . $code . '</code>)': '' ) ); |
|
| 1182 | - $label = apply_filters( 'wpinv_get_discount_label', $label, $code ); |
|
| 1180 | +function wpinv_get_discount_label($code, $echo = true) { |
|
| 1181 | + $label = wp_sprintf(__('Discount%1$s', 'invoicing'), ($code != '' && $code != 'none' ? ' (<code>' . $code . '</code>)' : '')); |
|
| 1182 | + $label = apply_filters('wpinv_get_discount_label', $label, $code); |
|
| 1183 | 1183 | |
| 1184 | - if ( $echo ) { |
|
| 1184 | + if ($echo) { |
|
| 1185 | 1185 | echo $label; |
| 1186 | 1186 | } else { |
| 1187 | 1187 | return $label; |
| 1188 | 1188 | } |
| 1189 | 1189 | } |
| 1190 | 1190 | |
| 1191 | -function wpinv_cart_discount_label( $code, $rate, $echo = true ) { |
|
| 1192 | - $label = wp_sprintf( __( '%1$s Discount: %2$s', 'invoicing' ), $rate, $code ); |
|
| 1193 | - $label = apply_filters( 'wpinv_cart_discount_label', $label, $code, $rate ); |
|
| 1191 | +function wpinv_cart_discount_label($code, $rate, $echo = true) { |
|
| 1192 | + $label = wp_sprintf(__('%1$s Discount: %2$s', 'invoicing'), $rate, $code); |
|
| 1193 | + $label = apply_filters('wpinv_cart_discount_label', $label, $code, $rate); |
|
| 1194 | 1194 | |
| 1195 | - if ( $echo ) { |
|
| 1195 | + if ($echo) { |
|
| 1196 | 1196 | echo $label; |
| 1197 | 1197 | } else { |
| 1198 | 1198 | return $label; |
| 1199 | 1199 | } |
| 1200 | 1200 | } |
| 1201 | 1201 | |
| 1202 | -function wpinv_check_delete_discount( $check, $post, $force_delete ) { |
|
| 1203 | - if ( $post->post_type == 'wpi_discount' && wpinv_get_discount_uses( $post->ID ) > 0 ) { |
|
| 1202 | +function wpinv_check_delete_discount($check, $post, $force_delete) { |
|
| 1203 | + if ($post->post_type == 'wpi_discount' && wpinv_get_discount_uses($post->ID) > 0) { |
|
| 1204 | 1204 | return true; |
| 1205 | 1205 | } |
| 1206 | 1206 | |
| 1207 | 1207 | return $check; |
| 1208 | 1208 | } |
| 1209 | -add_filter( 'pre_delete_post', 'wpinv_check_delete_discount', 10, 3 ); |
|
| 1209 | +add_filter('pre_delete_post', 'wpinv_check_delete_discount', 10, 3); |
|
| 1210 | 1210 | |
| 1211 | 1211 | function wpinv_checkout_form_validate_discounts() { |
| 1212 | 1212 | $discounts = wpinv_get_cart_discounts(); |
| 1213 | 1213 | |
| 1214 | - if ( !empty( $discounts ) ) { |
|
| 1214 | + if (!empty($discounts)) { |
|
| 1215 | 1215 | $invalid = false; |
| 1216 | 1216 | |
| 1217 | - foreach ( $discounts as $key => $code ) { |
|
| 1218 | - if ( !wpinv_is_discount_valid( $code, get_current_user_id() ) ) { |
|
| 1217 | + foreach ($discounts as $key => $code) { |
|
| 1218 | + if (!wpinv_is_discount_valid($code, get_current_user_id())) { |
|
| 1219 | 1219 | $invalid = true; |
| 1220 | 1220 | |
| 1221 | - wpinv_unset_cart_discount( $code ); |
|
| 1221 | + wpinv_unset_cart_discount($code); |
|
| 1222 | 1222 | } |
| 1223 | 1223 | } |
| 1224 | 1224 | |
| 1225 | - if ( $invalid ) { |
|
| 1225 | + if ($invalid) { |
|
| 1226 | 1226 | $errors = wpinv_get_errors(); |
| 1227 | - $error = !empty( $errors['wpinv-discount-error'] ) ? $errors['wpinv-discount-error'] . ' ' : ''; |
|
| 1228 | - $error .= __( 'The discount has been removed from cart.', 'invoicing' ); |
|
| 1229 | - wpinv_set_error( 'wpinv-discount-error', $error ); |
|
| 1227 | + $error = !empty($errors['wpinv-discount-error']) ? $errors['wpinv-discount-error'] . ' ' : ''; |
|
| 1228 | + $error .= __('The discount has been removed from cart.', 'invoicing'); |
|
| 1229 | + wpinv_set_error('wpinv-discount-error', $error); |
|
| 1230 | 1230 | |
| 1231 | - wpinv_recalculate_tax( true ); |
|
| 1231 | + wpinv_recalculate_tax(true); |
|
| 1232 | 1232 | } |
| 1233 | 1233 | } |
| 1234 | 1234 | } |
| 1235 | -add_action( 'wpinv_before_checkout_form', 'wpinv_checkout_form_validate_discounts', -10 ); |
|
| 1235 | +add_action('wpinv_before_checkout_form', 'wpinv_checkout_form_validate_discounts', -10); |
|
| 1236 | 1236 | |
| 1237 | 1237 | function wpinv_discount_amount() { |
| 1238 | 1238 | $output = 0.00; |
| 1239 | 1239 | |
| 1240 | - return apply_filters( 'wpinv_discount_amount', $output ); |
|
| 1240 | + return apply_filters('wpinv_discount_amount', $output); |
|
| 1241 | 1241 | } |
| 1242 | 1242 | \ No newline at end of file |