@@ -971,8 +971,9 @@ |
||
| 971 | 971 | return false; |
| 972 | 972 | } |
| 973 | 973 | |
| 974 | - if ( empty( $this->ID ) ) |
|
| 975 | - return false; |
|
| 974 | + if ( empty( $this->ID ) ) { |
|
| 975 | + return false; |
|
| 976 | + } |
|
| 976 | 977 | |
| 977 | 978 | if ( ( ( is_user_logged_in() && current_user_can( 'manage_options' ) ) || $added_by_user ) && !$system ) { |
| 978 | 979 | $user = get_user_by( 'id', get_current_user_id() ); |
@@ -83,6 +83,9 @@ discard block |
||
| 83 | 83 | return $value; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | + /** |
|
| 87 | + * @param string $key |
|
| 88 | + */ |
|
| 86 | 89 | public function set( $key, $value ) { |
| 87 | 90 | $ignore = array( 'items', 'cart_details', 'fees', '_ID' ); |
| 88 | 91 | |
@@ -916,6 +919,9 @@ discard block |
||
| 916 | 919 | return $removed; |
| 917 | 920 | } |
| 918 | 921 | |
| 922 | + /** |
|
| 923 | + * @param string $key |
|
| 924 | + */ |
|
| 919 | 925 | public function remove_fee_by( $key, $value, $global = false ) { |
| 920 | 926 | $allowed_fee_keys = apply_filters( 'wpinv_fee_keys', array( |
| 921 | 927 | 'index', 'label', 'amount', 'type', |
@@ -1375,6 +1381,9 @@ discard block |
||
| 1375 | 1381 | return apply_filters( 'wpinv_get_invoice_final_total', $final_total, $this, $currency ); |
| 1376 | 1382 | } |
| 1377 | 1383 | |
| 1384 | + /** |
|
| 1385 | + * @return boolean |
|
| 1386 | + */ |
|
| 1378 | 1387 | public function get_discounts( $array = false ) { |
| 1379 | 1388 | $discounts = $this->discounts; |
| 1380 | 1389 | if ( $array && $discounts ) { |
@@ -1383,6 +1392,9 @@ discard block |
||
| 1383 | 1392 | return apply_filters( 'wpinv_payment_discounts', $discounts, $this->ID, $this, $array ); |
| 1384 | 1393 | } |
| 1385 | 1394 | |
| 1395 | + /** |
|
| 1396 | + * @return string |
|
| 1397 | + */ |
|
| 1386 | 1398 | public function get_discount( $currency = false, $dash = false ) { |
| 1387 | 1399 | if ( !empty( $this->discounts ) ) { |
| 1388 | 1400 | global $ajax_cart_details; |
@@ -1479,10 +1491,16 @@ discard block |
||
| 1479 | 1491 | return apply_filters( 'wpinv_user_full_name', $this->full_name, $this->ID, $this ); |
| 1480 | 1492 | } |
| 1481 | 1493 | |
| 1494 | + /** |
|
| 1495 | + * @return string |
|
| 1496 | + */ |
|
| 1482 | 1497 | public function get_user_info() { |
| 1483 | 1498 | return apply_filters( 'wpinv_user_info', $this->user_info, $this->ID, $this ); |
| 1484 | 1499 | } |
| 1485 | 1500 | |
| 1501 | + /** |
|
| 1502 | + * @return string |
|
| 1503 | + */ |
|
| 1486 | 1504 | public function get_email() { |
| 1487 | 1505 | return apply_filters( 'wpinv_user_email', $this->email, $this->ID, $this ); |
| 1488 | 1506 | } |
@@ -1525,10 +1543,16 @@ discard block |
||
| 1525 | 1543 | return apply_filters( 'wpinv_currency_code', $this->currency, $this->ID, $this ); |
| 1526 | 1544 | } |
| 1527 | 1545 | |
| 1546 | + /** |
|
| 1547 | + * @return string |
|
| 1548 | + */ |
|
| 1528 | 1549 | public function get_created_date() { |
| 1529 | 1550 | return apply_filters( 'wpinv_created_date', $this->date, $this->ID, $this ); |
| 1530 | 1551 | } |
| 1531 | 1552 | |
| 1553 | + /** |
|
| 1554 | + * @return string |
|
| 1555 | + */ |
|
| 1532 | 1556 | public function get_due_date( $display = false ) { |
| 1533 | 1557 | $due_date = apply_filters( 'wpinv_due_date', $this->due_date, $this->ID, $this ); |
| 1534 | 1558 | |
@@ -2081,6 +2105,9 @@ discard block |
||
| 2081 | 2105 | return apply_filters( 'wpinv_invoice_get_subscription_name', $name, $this ); |
| 2082 | 2106 | } |
| 2083 | 2107 | |
| 2108 | + /** |
|
| 2109 | + * @return string |
|
| 2110 | + */ |
|
| 2084 | 2111 | public function get_expiration() { |
| 2085 | 2112 | $expiration = $this->get_meta( '_wpinv_subscr_expiration', true ); |
| 2086 | 2113 | return $expiration; |
@@ -2096,6 +2123,9 @@ discard block |
||
| 2096 | 2123 | return $cancelled_date; |
| 2097 | 2124 | } |
| 2098 | 2125 | |
| 2126 | + /** |
|
| 2127 | + * @return string |
|
| 2128 | + */ |
|
| 2099 | 2129 | public function get_trial_end_date( $formatted = true ) { |
| 2100 | 2130 | if ( !$this->is_free_trial() || !$this->is_paid() ) { |
| 2101 | 2131 | return NULL; |
@@ -2117,6 +2147,9 @@ discard block |
||
| 2117 | 2147 | return $trial_end_date; |
| 2118 | 2148 | } |
| 2119 | 2149 | |
| 2150 | + /** |
|
| 2151 | + * @return string |
|
| 2152 | + */ |
|
| 2120 | 2153 | public function get_subscription_created( $default = true ) { |
| 2121 | 2154 | $created = $this->get_meta( '_wpinv_subscr_created', true ); |
| 2122 | 2155 | |
@@ -2126,6 +2159,9 @@ discard block |
||
| 2126 | 2159 | return $created; |
| 2127 | 2160 | } |
| 2128 | 2161 | |
| 2162 | + /** |
|
| 2163 | + * @return string |
|
| 2164 | + */ |
|
| 2129 | 2165 | public function get_subscription_start( $formatted = true ) { |
| 2130 | 2166 | if ( !$this->is_paid() ) { |
| 2131 | 2167 | return '-'; |
@@ -7,12 +7,12 @@ 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 | final class WPInv_Invoice { |
| 15 | - public $ID = 0; |
|
| 15 | + public $ID = 0; |
|
| 16 | 16 | public $title; |
| 17 | 17 | public $post_type; |
| 18 | 18 | |
@@ -65,17 +65,17 @@ discard block |
||
| 65 | 65 | public $full_name = ''; |
| 66 | 66 | public $parent_invoice = 0; |
| 67 | 67 | |
| 68 | - public function __construct( $invoice_id = false ) { |
|
| 69 | - if( empty( $invoice_id ) ) { |
|
| 68 | + public function __construct($invoice_id = false) { |
|
| 69 | + if (empty($invoice_id)) { |
|
| 70 | 70 | return false; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - $this->setup_invoice( $invoice_id ); |
|
| 73 | + $this->setup_invoice($invoice_id); |
|
| 74 | 74 | } |
| 75 | 75 | |
| 76 | - public function get( $key ) { |
|
| 77 | - if ( method_exists( $this, 'get_' . $key ) ) { |
|
| 78 | - $value = call_user_func( array( $this, 'get_' . $key ) ); |
|
| 76 | + public function get($key) { |
|
| 77 | + if (method_exists($this, 'get_' . $key)) { |
|
| 78 | + $value = call_user_func(array($this, 'get_' . $key)); |
|
| 79 | 79 | } else { |
| 80 | 80 | $value = $this->$key; |
| 81 | 81 | } |
@@ -83,51 +83,51 @@ discard block |
||
| 83 | 83 | return $value; |
| 84 | 84 | } |
| 85 | 85 | |
| 86 | - public function set( $key, $value ) { |
|
| 87 | - $ignore = array( 'items', 'cart_details', 'fees', '_ID' ); |
|
| 86 | + public function set($key, $value) { |
|
| 87 | + $ignore = array('items', 'cart_details', 'fees', '_ID'); |
|
| 88 | 88 | |
| 89 | - if ( $key === 'status' ) { |
|
| 89 | + if ($key === 'status') { |
|
| 90 | 90 | $this->old_status = $this->status; |
| 91 | 91 | } |
| 92 | 92 | |
| 93 | - if ( ! in_array( $key, $ignore ) ) { |
|
| 94 | - $this->pending[ $key ] = $value; |
|
| 93 | + if (!in_array($key, $ignore)) { |
|
| 94 | + $this->pending[$key] = $value; |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | - if( '_ID' !== $key ) { |
|
| 97 | + if ('_ID' !== $key) { |
|
| 98 | 98 | $this->$key = $value; |
| 99 | 99 | } |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - public function _isset( $name ) { |
|
| 103 | - if ( property_exists( $this, $name) ) { |
|
| 104 | - return false === empty( $this->$name ); |
|
| 102 | + public function _isset($name) { |
|
| 103 | + if (property_exists($this, $name)) { |
|
| 104 | + return false === empty($this->$name); |
|
| 105 | 105 | } else { |
| 106 | 106 | return null; |
| 107 | 107 | } |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - private function setup_invoice( $invoice_id ) { |
|
| 110 | + private function setup_invoice($invoice_id) { |
|
| 111 | 111 | $this->pending = array(); |
| 112 | 112 | |
| 113 | - if ( empty( $invoice_id ) ) { |
|
| 113 | + if (empty($invoice_id)) { |
|
| 114 | 114 | return false; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - $invoice = get_post( $invoice_id ); |
|
| 117 | + $invoice = get_post($invoice_id); |
|
| 118 | 118 | |
| 119 | - if( !$invoice || is_wp_error( $invoice ) ) { |
|
| 119 | + if (!$invoice || is_wp_error($invoice)) { |
|
| 120 | 120 | return false; |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - if( !('wpi_invoice' == $invoice->post_type OR 'wpi_quote' == $invoice->post_type) ) { |
|
| 123 | + if (!('wpi_invoice' == $invoice->post_type OR 'wpi_quote' == $invoice->post_type)) { |
|
| 124 | 124 | return false; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - do_action( 'wpinv_pre_setup_invoice', $this, $invoice_id ); |
|
| 127 | + do_action('wpinv_pre_setup_invoice', $this, $invoice_id); |
|
| 128 | 128 | |
| 129 | 129 | // Primary Identifier |
| 130 | - $this->ID = absint( $invoice_id ); |
|
| 130 | + $this->ID = absint($invoice_id); |
|
| 131 | 131 | $this->post_type = $invoice->post_type; |
| 132 | 132 | |
| 133 | 133 | // We have a payment, get the generic payment_meta item to reduce calls to it |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | $this->post_status = $this->status; |
| 140 | 140 | $this->mode = $this->setup_mode(); |
| 141 | 141 | $this->parent_invoice = $invoice->post_parent; |
| 142 | - $this->post_name = $this->setup_post_name( $invoice ); |
|
| 142 | + $this->post_name = $this->setup_post_name($invoice); |
|
| 143 | 143 | $this->status_nicename = $this->setup_status_nicename($invoice->post_status); |
| 144 | 144 | |
| 145 | 145 | // Items |
@@ -161,8 +161,8 @@ discard block |
||
| 161 | 161 | |
| 162 | 162 | // User based |
| 163 | 163 | $this->ip = $this->setup_ip(); |
| 164 | - $this->user_id = !empty( $invoice->post_author ) ? $invoice->post_author : get_current_user_id();///$this->setup_user_id(); |
|
| 165 | - $this->email = get_the_author_meta( 'email', $this->user_id ); |
|
| 164 | + $this->user_id = !empty($invoice->post_author) ? $invoice->post_author : get_current_user_id(); ///$this->setup_user_id(); |
|
| 165 | + $this->email = get_the_author_meta('email', $this->user_id); |
|
| 166 | 166 | |
| 167 | 167 | $this->user_info = $this->setup_user_info(); |
| 168 | 168 | |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | $this->company = $this->user_info['company']; |
| 172 | 172 | $this->vat_number = $this->user_info['vat_number']; |
| 173 | 173 | $this->vat_rate = $this->user_info['vat_rate']; |
| 174 | - $this->adddress_confirmed = $this->user_info['adddress_confirmed']; |
|
| 174 | + $this->adddress_confirmed = $this->user_info['adddress_confirmed']; |
|
| 175 | 175 | $this->address = $this->user_info['address']; |
| 176 | 176 | $this->city = $this->user_info['city']; |
| 177 | 177 | $this->country = $this->user_info['country']; |
@@ -186,35 +186,35 @@ discard block |
||
| 186 | 186 | // Other Identifiers |
| 187 | 187 | $this->key = $this->setup_invoice_key(); |
| 188 | 188 | $this->number = $this->setup_invoice_number(); |
| 189 | - $this->title = !empty( $invoice->post_title ) ? $invoice->post_title : $this->number; |
|
| 189 | + $this->title = !empty($invoice->post_title) ? $invoice->post_title : $this->number; |
|
| 190 | 190 | |
| 191 | - $this->full_name = trim( $this->first_name . ' '. $this->last_name ); |
|
| 191 | + $this->full_name = trim($this->first_name . ' ' . $this->last_name); |
|
| 192 | 192 | |
| 193 | 193 | // Allow extensions to add items to this object via hook |
| 194 | - do_action( 'wpinv_setup_invoice', $this, $invoice_id ); |
|
| 194 | + do_action('wpinv_setup_invoice', $this, $invoice_id); |
|
| 195 | 195 | |
| 196 | 196 | return true; |
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | private function setup_status_nicename($status) { |
| 200 | - $all_invoice_statuses = wpinv_get_invoice_statuses(); |
|
| 201 | - $status = isset( $all_invoice_statuses[$status] ) ? $all_invoice_statuses[$status] : __( $status, 'invoicing' ); |
|
| 200 | + $all_invoice_statuses = wpinv_get_invoice_statuses(); |
|
| 201 | + $status = isset($all_invoice_statuses[$status]) ? $all_invoice_statuses[$status] : __($status, 'invoicing'); |
|
| 202 | 202 | |
| 203 | - return apply_filters( 'setup_status_nicename', $status ); |
|
| 203 | + return apply_filters('setup_status_nicename', $status); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - private function setup_post_name( $post = NULL ) { |
|
| 206 | + private function setup_post_name($post = NULL) { |
|
| 207 | 207 | global $wpdb; |
| 208 | 208 | |
| 209 | 209 | $post_name = ''; |
| 210 | 210 | |
| 211 | - if ( !empty( $post ) ) { |
|
| 212 | - if( !empty( $post->post_name ) ) { |
|
| 211 | + if (!empty($post)) { |
|
| 212 | + if (!empty($post->post_name)) { |
|
| 213 | 213 | $post_name = $post->post_name; |
| 214 | - } else if ( !empty( $post->ID ) ) { |
|
| 214 | + } else if (!empty($post->ID)) { |
|
| 215 | 215 | $post_name = 'inv-' . $post->ID; |
| 216 | 216 | |
| 217 | - $wpdb->update( $wpdb->posts, array( 'post_name' => 'inv-' . $post->ID ), array( 'ID' => $post->ID ) ); |
|
| 217 | + $wpdb->update($wpdb->posts, array('post_name' => 'inv-' . $post->ID), array('ID' => $post->ID)); |
|
| 218 | 218 | } |
| 219 | 219 | } |
| 220 | 220 | |
@@ -222,12 +222,12 @@ discard block |
||
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | private function setup_due_date() { |
| 225 | - $due_date = $this->get_meta( '_wpinv_due_date' ); |
|
| 225 | + $due_date = $this->get_meta('_wpinv_due_date'); |
|
| 226 | 226 | |
| 227 | - if ( empty( $due_date ) ) { |
|
| 228 | - $overdue_time = strtotime( $this->date ) + ( DAY_IN_SECONDS * absint( wpinv_get_option( 'overdue_days' ) ) ); |
|
| 229 | - $due_date = date_i18n( 'Y-m-d', $overdue_time ); |
|
| 230 | - } else if ( $due_date == 'none' ) { |
|
| 227 | + if (empty($due_date)) { |
|
| 228 | + $overdue_time = strtotime($this->date) + (DAY_IN_SECONDS * absint(wpinv_get_option('overdue_days'))); |
|
| 229 | + $due_date = date_i18n('Y-m-d', $overdue_time); |
|
| 230 | + } else if ($due_date == 'none') { |
|
| 231 | 231 | $due_date = ''; |
| 232 | 232 | } |
| 233 | 233 | |
@@ -235,63 +235,63 @@ discard block |
||
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | private function setup_completed_date() { |
| 238 | - $invoice = get_post( $this->ID ); |
|
| 238 | + $invoice = get_post($this->ID); |
|
| 239 | 239 | |
| 240 | - if ( 'pending' == $invoice->post_status || 'preapproved' == $invoice->post_status ) { |
|
| 240 | + if ('pending' == $invoice->post_status || 'preapproved' == $invoice->post_status) { |
|
| 241 | 241 | return false; // This invoice was never paid |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | - $date = ( $date = $this->get_meta( '_wpinv_completed_date', true ) ) ? $date : $invoice->modified_date; |
|
| 244 | + $date = ($date = $this->get_meta('_wpinv_completed_date', true)) ? $date : $invoice->modified_date; |
|
| 245 | 245 | |
| 246 | 246 | return $date; |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | 249 | private function setup_cart_details() { |
| 250 | - $cart_details = isset( $this->payment_meta['cart_details'] ) ? maybe_unserialize( $this->payment_meta['cart_details'] ) : array(); |
|
| 250 | + $cart_details = isset($this->payment_meta['cart_details']) ? maybe_unserialize($this->payment_meta['cart_details']) : array(); |
|
| 251 | 251 | return $cart_details; |
| 252 | 252 | } |
| 253 | 253 | |
| 254 | 254 | public function array_convert() { |
| 255 | - return get_object_vars( $this ); |
|
| 255 | + return get_object_vars($this); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | private function setup_items() { |
| 259 | - $items = isset( $this->payment_meta['items'] ) ? maybe_unserialize( $this->payment_meta['items'] ) : array(); |
|
| 259 | + $items = isset($this->payment_meta['items']) ? maybe_unserialize($this->payment_meta['items']) : array(); |
|
| 260 | 260 | return $items; |
| 261 | 261 | } |
| 262 | 262 | |
| 263 | 263 | private function setup_fees() { |
| 264 | - $payment_fees = isset( $this->payment_meta['fees'] ) ? $this->payment_meta['fees'] : array(); |
|
| 264 | + $payment_fees = isset($this->payment_meta['fees']) ? $this->payment_meta['fees'] : array(); |
|
| 265 | 265 | return $payment_fees; |
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | private function setup_currency() { |
| 269 | - $currency = isset( $this->payment_meta['currency'] ) ? $this->payment_meta['currency'] : apply_filters( 'wpinv_currency_default', wpinv_get_currency(), $this ); |
|
| 269 | + $currency = isset($this->payment_meta['currency']) ? $this->payment_meta['currency'] : apply_filters('wpinv_currency_default', wpinv_get_currency(), $this); |
|
| 270 | 270 | return $currency; |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | private function setup_discount() { |
| 274 | 274 | //$discount = $this->get_meta( '_wpinv_discount', true ); |
| 275 | - $discount = (float)$this->subtotal - ( (float)$this->total - (float)$this->tax - (float)$this->fees_total ); |
|
| 276 | - if ( $discount < 0 ) { |
|
| 275 | + $discount = (float)$this->subtotal - ((float)$this->total - (float)$this->tax - (float)$this->fees_total); |
|
| 276 | + if ($discount < 0) { |
|
| 277 | 277 | $discount = 0; |
| 278 | 278 | } |
| 279 | - $discount = wpinv_round_amount( $discount ); |
|
| 279 | + $discount = wpinv_round_amount($discount); |
|
| 280 | 280 | |
| 281 | 281 | return $discount; |
| 282 | 282 | } |
| 283 | 283 | |
| 284 | 284 | private function setup_discount_code() { |
| 285 | - $discount_code = !empty( $this->discounts ) ? $this->discounts : $this->get_meta( '_wpinv_discount_code', true ); |
|
| 285 | + $discount_code = !empty($this->discounts) ? $this->discounts : $this->get_meta('_wpinv_discount_code', true); |
|
| 286 | 286 | return $discount_code; |
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | private function setup_tax() { |
| 290 | - $tax = $this->get_meta( '_wpinv_tax', true ); |
|
| 290 | + $tax = $this->get_meta('_wpinv_tax', true); |
|
| 291 | 291 | |
| 292 | 292 | // We don't have tax as it's own meta and no meta was passed |
| 293 | - if ( '' === $tax ) { |
|
| 294 | - $tax = isset( $this->payment_meta['tax'] ) ? $this->payment_meta['tax'] : 0; |
|
| 293 | + if ('' === $tax) { |
|
| 294 | + $tax = isset($this->payment_meta['tax']) ? $this->payment_meta['tax'] : 0; |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | return $tax; |
@@ -301,9 +301,9 @@ discard block |
||
| 301 | 301 | $subtotal = 0; |
| 302 | 302 | $cart_details = $this->cart_details; |
| 303 | 303 | |
| 304 | - if ( is_array( $cart_details ) ) { |
|
| 305 | - foreach ( $cart_details as $item ) { |
|
| 306 | - if ( isset( $item['subtotal'] ) ) { |
|
| 304 | + if (is_array($cart_details)) { |
|
| 305 | + foreach ($cart_details as $item) { |
|
| 306 | + if (isset($item['subtotal'])) { |
|
| 307 | 307 | $subtotal += $item['subtotal']; |
| 308 | 308 | } |
| 309 | 309 | } |
@@ -317,18 +317,18 @@ discard block |
||
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | private function setup_discounts() { |
| 320 | - $discounts = ! empty( $this->payment_meta['user_info']['discount'] ) ? $this->payment_meta['user_info']['discount'] : array(); |
|
| 320 | + $discounts = !empty($this->payment_meta['user_info']['discount']) ? $this->payment_meta['user_info']['discount'] : array(); |
|
| 321 | 321 | return $discounts; |
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | private function setup_total() { |
| 325 | - $amount = $this->get_meta( '_wpinv_total', true ); |
|
| 325 | + $amount = $this->get_meta('_wpinv_total', true); |
|
| 326 | 326 | |
| 327 | - if ( empty( $amount ) && '0.00' != $amount ) { |
|
| 328 | - $meta = $this->get_meta( '_wpinv_payment_meta', true ); |
|
| 329 | - $meta = maybe_unserialize( $meta ); |
|
| 327 | + if (empty($amount) && '0.00' != $amount) { |
|
| 328 | + $meta = $this->get_meta('_wpinv_payment_meta', true); |
|
| 329 | + $meta = maybe_unserialize($meta); |
|
| 330 | 330 | |
| 331 | - if ( isset( $meta['amount'] ) ) { |
|
| 331 | + if (isset($meta['amount'])) { |
|
| 332 | 332 | $amount = $meta['amount']; |
| 333 | 333 | } |
| 334 | 334 | } |
@@ -337,13 +337,13 @@ discard block |
||
| 337 | 337 | } |
| 338 | 338 | |
| 339 | 339 | private function setup_mode() { |
| 340 | - return $this->get_meta( '_wpinv_mode' ); |
|
| 340 | + return $this->get_meta('_wpinv_mode'); |
|
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | private function setup_gateway() { |
| 344 | - $gateway = $this->get_meta( '_wpinv_gateway' ); |
|
| 344 | + $gateway = $this->get_meta('_wpinv_gateway'); |
|
| 345 | 345 | |
| 346 | - if ( empty( $gateway ) && 'publish' === $this->status ) { |
|
| 346 | + if (empty($gateway) && 'publish' === $this->status) { |
|
| 347 | 347 | $gateway = 'manual'; |
| 348 | 348 | } |
| 349 | 349 | |
@@ -351,23 +351,23 @@ discard block |
||
| 351 | 351 | } |
| 352 | 352 | |
| 353 | 353 | private function setup_gateway_title() { |
| 354 | - $gateway_title = wpinv_get_gateway_checkout_label( $this->gateway ); |
|
| 354 | + $gateway_title = wpinv_get_gateway_checkout_label($this->gateway); |
|
| 355 | 355 | return $gateway_title; |
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | private function setup_transaction_id() { |
| 359 | - $transaction_id = $this->get_meta( '_wpinv_transaction_id' ); |
|
| 359 | + $transaction_id = $this->get_meta('_wpinv_transaction_id'); |
|
| 360 | 360 | |
| 361 | - if ( empty( $transaction_id ) || (int) $transaction_id === (int) $this->ID ) { |
|
| 361 | + if (empty($transaction_id) || (int)$transaction_id === (int)$this->ID) { |
|
| 362 | 362 | $gateway = $this->gateway; |
| 363 | - $transaction_id = apply_filters( 'wpinv_get_invoice_transaction_id-' . $gateway, $this->ID ); |
|
| 363 | + $transaction_id = apply_filters('wpinv_get_invoice_transaction_id-' . $gateway, $this->ID); |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | return $transaction_id; |
| 367 | 367 | } |
| 368 | 368 | |
| 369 | 369 | private function setup_ip() { |
| 370 | - $ip = $this->get_meta( '_wpinv_user_ip' ); |
|
| 370 | + $ip = $this->get_meta('_wpinv_user_ip'); |
|
| 371 | 371 | return $ip; |
| 372 | 372 | } |
| 373 | 373 | |
@@ -377,62 +377,62 @@ discard block |
||
| 377 | 377 | ///} |
| 378 | 378 | |
| 379 | 379 | private function setup_first_name() { |
| 380 | - $first_name = $this->get_meta( '_wpinv_first_name' ); |
|
| 380 | + $first_name = $this->get_meta('_wpinv_first_name'); |
|
| 381 | 381 | return $first_name; |
| 382 | 382 | } |
| 383 | 383 | |
| 384 | 384 | private function setup_last_name() { |
| 385 | - $last_name = $this->get_meta( '_wpinv_last_name' ); |
|
| 385 | + $last_name = $this->get_meta('_wpinv_last_name'); |
|
| 386 | 386 | return $last_name; |
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | private function setup_company() { |
| 390 | - $company = $this->get_meta( '_wpinv_company' ); |
|
| 390 | + $company = $this->get_meta('_wpinv_company'); |
|
| 391 | 391 | return $company; |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | 394 | private function setup_vat_number() { |
| 395 | - $vat_number = $this->get_meta( '_wpinv_vat_number' ); |
|
| 395 | + $vat_number = $this->get_meta('_wpinv_vat_number'); |
|
| 396 | 396 | return $vat_number; |
| 397 | 397 | } |
| 398 | 398 | |
| 399 | 399 | private function setup_vat_rate() { |
| 400 | - $vat_rate = $this->get_meta( '_wpinv_vat_rate' ); |
|
| 400 | + $vat_rate = $this->get_meta('_wpinv_vat_rate'); |
|
| 401 | 401 | return $vat_rate; |
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | private function setup_adddress_confirmed() { |
| 405 | - $adddress_confirmed = $this->get_meta( '_wpinv_adddress_confirmed' ); |
|
| 405 | + $adddress_confirmed = $this->get_meta('_wpinv_adddress_confirmed'); |
|
| 406 | 406 | return $adddress_confirmed; |
| 407 | 407 | } |
| 408 | 408 | |
| 409 | 409 | private function setup_phone() { |
| 410 | - $phone = $this->get_meta( '_wpinv_phone' ); |
|
| 410 | + $phone = $this->get_meta('_wpinv_phone'); |
|
| 411 | 411 | return $phone; |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | 414 | private function setup_address() { |
| 415 | - $address = $this->get_meta( '_wpinv_address', true ); |
|
| 415 | + $address = $this->get_meta('_wpinv_address', true); |
|
| 416 | 416 | return $address; |
| 417 | 417 | } |
| 418 | 418 | |
| 419 | 419 | private function setup_city() { |
| 420 | - $city = $this->get_meta( '_wpinv_city', true ); |
|
| 420 | + $city = $this->get_meta('_wpinv_city', true); |
|
| 421 | 421 | return $city; |
| 422 | 422 | } |
| 423 | 423 | |
| 424 | 424 | private function setup_country() { |
| 425 | - $country = $this->get_meta( '_wpinv_country', true ); |
|
| 425 | + $country = $this->get_meta('_wpinv_country', true); |
|
| 426 | 426 | return $country; |
| 427 | 427 | } |
| 428 | 428 | |
| 429 | 429 | private function setup_state() { |
| 430 | - $state = $this->get_meta( '_wpinv_state', true ); |
|
| 430 | + $state = $this->get_meta('_wpinv_state', true); |
|
| 431 | 431 | return $state; |
| 432 | 432 | } |
| 433 | 433 | |
| 434 | 434 | private function setup_zip() { |
| 435 | - $zip = $this->get_meta( '_wpinv_zip', true ); |
|
| 435 | + $zip = $this->get_meta('_wpinv_zip', true); |
|
| 436 | 436 | return $zip; |
| 437 | 437 | } |
| 438 | 438 | |
@@ -441,7 +441,7 @@ discard block |
||
| 441 | 441 | 'user_id' => $this->user_id, |
| 442 | 442 | 'first_name' => $this->first_name, |
| 443 | 443 | 'last_name' => $this->last_name, |
| 444 | - 'email' => get_the_author_meta( 'email', $this->user_id ), |
|
| 444 | + 'email' => get_the_author_meta('email', $this->user_id), |
|
| 445 | 445 | 'phone' => $this->phone, |
| 446 | 446 | 'address' => $this->address, |
| 447 | 447 | 'city' => $this->city, |
@@ -456,12 +456,12 @@ discard block |
||
| 456 | 456 | ); |
| 457 | 457 | |
| 458 | 458 | $user_info = array(); |
| 459 | - if ( isset( $this->payment_meta['user_info'] ) ) { |
|
| 460 | - $user_info = maybe_unserialize( $this->payment_meta['user_info'] ); |
|
| 459 | + if (isset($this->payment_meta['user_info'])) { |
|
| 460 | + $user_info = maybe_unserialize($this->payment_meta['user_info']); |
|
| 461 | 461 | |
| 462 | - if ( !empty( $user_info ) && isset( $user_info['user_id'] ) && $post = get_post( $this->ID ) ) { |
|
| 462 | + if (!empty($user_info) && isset($user_info['user_id']) && $post = get_post($this->ID)) { |
|
| 463 | 463 | $this->user_id = $post->post_author; |
| 464 | - $this->email = get_the_author_meta( 'email', $this->user_id ); |
|
| 464 | + $this->email = get_the_author_meta('email', $this->user_id); |
|
| 465 | 465 | |
| 466 | 466 | $user_info['user_id'] = $this->user_id; |
| 467 | 467 | $user_info['email'] = $this->email; |
@@ -470,13 +470,13 @@ discard block |
||
| 470 | 470 | } |
| 471 | 471 | } |
| 472 | 472 | |
| 473 | - $user_info = wp_parse_args( $user_info, $defaults ); |
|
| 473 | + $user_info = wp_parse_args($user_info, $defaults); |
|
| 474 | 474 | |
| 475 | 475 | // Get the user, but only if it's been created |
| 476 | - $user = get_userdata( $this->user_id ); |
|
| 476 | + $user = get_userdata($this->user_id); |
|
| 477 | 477 | |
| 478 | - if ( !empty( $user ) && $user->ID > 0 ) { |
|
| 479 | - if ( empty( $user_info ) ) { |
|
| 478 | + if (!empty($user) && $user->ID > 0) { |
|
| 479 | + if (empty($user_info)) { |
|
| 480 | 480 | $user_info = array( |
| 481 | 481 | 'user_id' => $user->ID, |
| 482 | 482 | 'first_name' => $user->first_name, |
@@ -485,23 +485,23 @@ discard block |
||
| 485 | 485 | 'discount' => '', |
| 486 | 486 | ); |
| 487 | 487 | } else { |
| 488 | - foreach ( $user_info as $key => $value ) { |
|
| 489 | - if ( ! empty( $value ) ) { |
|
| 488 | + foreach ($user_info as $key => $value) { |
|
| 489 | + if (!empty($value)) { |
|
| 490 | 490 | continue; |
| 491 | 491 | } |
| 492 | 492 | |
| 493 | - switch( $key ) { |
|
| 493 | + switch ($key) { |
|
| 494 | 494 | case 'user_id': |
| 495 | - $user_info[ $key ] = $user->ID; |
|
| 495 | + $user_info[$key] = $user->ID; |
|
| 496 | 496 | break; |
| 497 | 497 | case 'first_name': |
| 498 | - $user_info[ $key ] = $user->first_name; |
|
| 498 | + $user_info[$key] = $user->first_name; |
|
| 499 | 499 | break; |
| 500 | 500 | case 'last_name': |
| 501 | - $user_info[ $key ] = $user->last_name; |
|
| 501 | + $user_info[$key] = $user->last_name; |
|
| 502 | 502 | break; |
| 503 | 503 | case 'email': |
| 504 | - $user_info[ $key ] = $user->user_email; |
|
| 504 | + $user_info[$key] = $user->user_email; |
|
| 505 | 505 | break; |
| 506 | 506 | } |
| 507 | 507 | } |
@@ -512,25 +512,25 @@ discard block |
||
| 512 | 512 | } |
| 513 | 513 | |
| 514 | 514 | private function setup_invoice_key() { |
| 515 | - $key = $this->get_meta( '_wpinv_key', true ); |
|
| 515 | + $key = $this->get_meta('_wpinv_key', true); |
|
| 516 | 516 | |
| 517 | 517 | return $key; |
| 518 | 518 | } |
| 519 | 519 | |
| 520 | 520 | private function setup_invoice_number() { |
| 521 | - $number = $this->get_meta( '_wpinv_number', true ); |
|
| 521 | + $number = $this->get_meta('_wpinv_number', true); |
|
| 522 | 522 | |
| 523 | - if ( !$number ) { |
|
| 523 | + if (!$number) { |
|
| 524 | 524 | $number = $this->ID; |
| 525 | 525 | |
| 526 | - if ( $this->status == 'auto-draft' ) { |
|
| 527 | - if ( wpinv_get_option( 'sequential_invoice_number' ) ) { |
|
| 526 | + if ($this->status == 'auto-draft') { |
|
| 527 | + if (wpinv_get_option('sequential_invoice_number')) { |
|
| 528 | 528 | $next_number = wpinv_get_next_invoice_number(); |
| 529 | 529 | $number = $next_number; |
| 530 | 530 | } |
| 531 | 531 | } |
| 532 | 532 | |
| 533 | - $number = wpinv_format_invoice_number( $number ); |
|
| 533 | + $number = wpinv_format_invoice_number($number); |
|
| 534 | 534 | } |
| 535 | 535 | |
| 536 | 536 | return $number; |
@@ -540,16 +540,16 @@ discard block |
||
| 540 | 540 | global $wpdb; |
| 541 | 541 | |
| 542 | 542 | $invoice_number = $this->ID; |
| 543 | - if ( $number = $this->get_meta( '_wpinv_number', true ) ) { |
|
| 543 | + if ($number = $this->get_meta('_wpinv_number', true)) { |
|
| 544 | 544 | $invoice_number = $number; |
| 545 | 545 | } |
| 546 | 546 | |
| 547 | - if ( empty( $this->key ) ) { |
|
| 547 | + if (empty($this->key)) { |
|
| 548 | 548 | $this->key = self::generate_key(); |
| 549 | 549 | $this->pending['key'] = $this->key; |
| 550 | 550 | } |
| 551 | 551 | |
| 552 | - if ( empty( $this->ip ) ) { |
|
| 552 | + if (empty($this->ip)) { |
|
| 553 | 553 | $this->ip = wpinv_get_ip(); |
| 554 | 554 | $this->pending['ip'] = $this->ip; |
| 555 | 555 | } |
@@ -586,61 +586,61 @@ discard block |
||
| 586 | 586 | 'post_status' => $this->status, |
| 587 | 587 | 'post_author' => $this->user_id, |
| 588 | 588 | 'post_type' => $this->post_type, |
| 589 | - 'post_date' => ! empty( $this->date ) && $this->date != '0000-00-00 00:00:00' ? $this->date : current_time( 'mysql' ), |
|
| 590 | - 'post_date_gmt' => ! empty( $this->date ) && $this->date != '0000-00-00 00:00:00' ? get_gmt_from_date( $this->date ) : current_time( 'mysql', 1 ), |
|
| 589 | + 'post_date' => !empty($this->date) && $this->date != '0000-00-00 00:00:00' ? $this->date : current_time('mysql'), |
|
| 590 | + 'post_date_gmt' => !empty($this->date) && $this->date != '0000-00-00 00:00:00' ? get_gmt_from_date($this->date) : current_time('mysql', 1), |
|
| 591 | 591 | 'post_parent' => $this->parent_invoice, |
| 592 | 592 | ); |
| 593 | - $args = apply_filters( 'wpinv_insert_invoice_args', $post_data, $this ); |
|
| 593 | + $args = apply_filters('wpinv_insert_invoice_args', $post_data, $this); |
|
| 594 | 594 | |
| 595 | 595 | // Create a blank invoice |
| 596 | - if ( !empty( $this->ID ) ) { |
|
| 597 | - $args['ID'] = $this->ID; |
|
| 596 | + if (!empty($this->ID)) { |
|
| 597 | + $args['ID'] = $this->ID; |
|
| 598 | 598 | |
| 599 | - $invoice_id = wp_update_post( $args, true ); |
|
| 599 | + $invoice_id = wp_update_post($args, true); |
|
| 600 | 600 | } else { |
| 601 | - $invoice_id = wp_insert_post( $args, true ); |
|
| 601 | + $invoice_id = wp_insert_post($args, true); |
|
| 602 | 602 | } |
| 603 | 603 | |
| 604 | - if ( is_wp_error( $invoice_id ) ) { |
|
| 604 | + if (is_wp_error($invoice_id)) { |
|
| 605 | 605 | return false; |
| 606 | 606 | } |
| 607 | 607 | |
| 608 | - if ( !empty( $invoice_id ) ) { |
|
| 608 | + if (!empty($invoice_id)) { |
|
| 609 | 609 | $this->ID = $invoice_id; |
| 610 | 610 | $this->_ID = $invoice_id; |
| 611 | 611 | |
| 612 | - $this->payment_meta = apply_filters( 'wpinv_payment_meta', $this->payment_meta, $payment_data ); |
|
| 613 | - if ( ! empty( $this->payment_meta['fees'] ) ) { |
|
| 614 | - $this->fees = array_merge( $this->fees, $this->payment_meta['fees'] ); |
|
| 615 | - foreach( $this->fees as $fee ) { |
|
| 616 | - $this->increase_fees( $fee['amount'] ); |
|
| 612 | + $this->payment_meta = apply_filters('wpinv_payment_meta', $this->payment_meta, $payment_data); |
|
| 613 | + if (!empty($this->payment_meta['fees'])) { |
|
| 614 | + $this->fees = array_merge($this->fees, $this->payment_meta['fees']); |
|
| 615 | + foreach ($this->fees as $fee) { |
|
| 616 | + $this->increase_fees($fee['amount']); |
|
| 617 | 617 | } |
| 618 | 618 | } |
| 619 | 619 | |
| 620 | - $this->update_meta( '_wpinv_payment_meta', $this->payment_meta ); |
|
| 620 | + $this->update_meta('_wpinv_payment_meta', $this->payment_meta); |
|
| 621 | 621 | $this->new = true; |
| 622 | 622 | } |
| 623 | 623 | |
| 624 | 624 | return $this->ID; |
| 625 | 625 | } |
| 626 | 626 | |
| 627 | - public function save( $setup = false ) { |
|
| 627 | + public function save($setup = false) { |
|
| 628 | 628 | global $wpi_session; |
| 629 | 629 | |
| 630 | 630 | $saved = false; |
| 631 | - if ( empty( $this->items ) ) { |
|
| 631 | + if (empty($this->items)) { |
|
| 632 | 632 | return $saved; // Don't save empty invoice. |
| 633 | 633 | } |
| 634 | 634 | |
| 635 | - if ( empty( $this->key ) ) { |
|
| 635 | + if (empty($this->key)) { |
|
| 636 | 636 | $this->key = self::generate_key(); |
| 637 | 637 | $this->pending['key'] = $this->key; |
| 638 | 638 | } |
| 639 | 639 | |
| 640 | - if ( empty( $this->ID ) ) { |
|
| 640 | + if (empty($this->ID)) { |
|
| 641 | 641 | $invoice_id = $this->insert_invoice(); |
| 642 | 642 | |
| 643 | - if ( false === $invoice_id ) { |
|
| 643 | + if (false === $invoice_id) { |
|
| 644 | 644 | $saved = false; |
| 645 | 645 | } else { |
| 646 | 646 | $this->ID = $invoice_id; |
@@ -648,27 +648,27 @@ discard block |
||
| 648 | 648 | } |
| 649 | 649 | |
| 650 | 650 | // If we have something pending, let's save it |
| 651 | - if ( !empty( $this->pending ) ) { |
|
| 651 | + if (!empty($this->pending)) { |
|
| 652 | 652 | $total_increase = 0; |
| 653 | 653 | $total_decrease = 0; |
| 654 | 654 | |
| 655 | - foreach ( $this->pending as $key => $value ) { |
|
| 656 | - switch( $key ) { |
|
| 655 | + foreach ($this->pending as $key => $value) { |
|
| 656 | + switch ($key) { |
|
| 657 | 657 | case 'items': |
| 658 | 658 | // Update totals for pending items |
| 659 | - foreach ( $this->pending[ $key ] as $item ) { |
|
| 660 | - switch( $item['action'] ) { |
|
| 659 | + foreach ($this->pending[$key] as $item) { |
|
| 660 | + switch ($item['action']) { |
|
| 661 | 661 | case 'add': |
| 662 | 662 | $price = $item['price']; |
| 663 | 663 | $taxes = $item['tax']; |
| 664 | 664 | |
| 665 | - if ( 'publish' === $this->status ) { |
|
| 665 | + if ('publish' === $this->status) { |
|
| 666 | 666 | $total_increase += $price; |
| 667 | 667 | } |
| 668 | 668 | break; |
| 669 | 669 | |
| 670 | 670 | case 'remove': |
| 671 | - if ( 'publish' === $this->status ) { |
|
| 671 | + if ('publish' === $this->status) { |
|
| 672 | 672 | $total_decrease += $item['price']; |
| 673 | 673 | } |
| 674 | 674 | break; |
@@ -676,16 +676,16 @@ discard block |
||
| 676 | 676 | } |
| 677 | 677 | break; |
| 678 | 678 | case 'fees': |
| 679 | - if ( 'publish' !== $this->status ) { |
|
| 679 | + if ('publish' !== $this->status) { |
|
| 680 | 680 | break; |
| 681 | 681 | } |
| 682 | 682 | |
| 683 | - if ( empty( $this->pending[ $key ] ) ) { |
|
| 683 | + if (empty($this->pending[$key])) { |
|
| 684 | 684 | break; |
| 685 | 685 | } |
| 686 | 686 | |
| 687 | - foreach ( $this->pending[ $key ] as $fee ) { |
|
| 688 | - switch( $fee['action'] ) { |
|
| 687 | + foreach ($this->pending[$key] as $fee) { |
|
| 688 | + switch ($fee['action']) { |
|
| 689 | 689 | case 'add': |
| 690 | 690 | $total_increase += $fee['amount']; |
| 691 | 691 | break; |
@@ -697,83 +697,83 @@ discard block |
||
| 697 | 697 | } |
| 698 | 698 | break; |
| 699 | 699 | case 'status': |
| 700 | - $this->update_status( $this->status ); |
|
| 700 | + $this->update_status($this->status); |
|
| 701 | 701 | break; |
| 702 | 702 | case 'gateway': |
| 703 | - $this->update_meta( '_wpinv_gateway', $this->gateway ); |
|
| 703 | + $this->update_meta('_wpinv_gateway', $this->gateway); |
|
| 704 | 704 | break; |
| 705 | 705 | case 'mode': |
| 706 | - $this->update_meta( '_wpinv_mode', $this->mode ); |
|
| 706 | + $this->update_meta('_wpinv_mode', $this->mode); |
|
| 707 | 707 | break; |
| 708 | 708 | case 'transaction_id': |
| 709 | - $this->update_meta( '_wpinv_transaction_id', $this->transaction_id ); |
|
| 709 | + $this->update_meta('_wpinv_transaction_id', $this->transaction_id); |
|
| 710 | 710 | break; |
| 711 | 711 | case 'ip': |
| 712 | - $this->update_meta( '_wpinv_user_ip', $this->ip ); |
|
| 712 | + $this->update_meta('_wpinv_user_ip', $this->ip); |
|
| 713 | 713 | break; |
| 714 | 714 | ///case 'user_id': |
| 715 | 715 | ///$this->update_meta( '_wpinv_user_id', $this->user_id ); |
| 716 | 716 | ///$this->user_info['user_id'] = $this->user_id; |
| 717 | 717 | ///break; |
| 718 | 718 | case 'first_name': |
| 719 | - $this->update_meta( '_wpinv_first_name', $this->first_name ); |
|
| 719 | + $this->update_meta('_wpinv_first_name', $this->first_name); |
|
| 720 | 720 | $this->user_info['first_name'] = $this->first_name; |
| 721 | 721 | break; |
| 722 | 722 | case 'last_name': |
| 723 | - $this->update_meta( '_wpinv_last_name', $this->last_name ); |
|
| 723 | + $this->update_meta('_wpinv_last_name', $this->last_name); |
|
| 724 | 724 | $this->user_info['last_name'] = $this->last_name; |
| 725 | 725 | break; |
| 726 | 726 | case 'phone': |
| 727 | - $this->update_meta( '_wpinv_phone', $this->phone ); |
|
| 727 | + $this->update_meta('_wpinv_phone', $this->phone); |
|
| 728 | 728 | $this->user_info['phone'] = $this->phone; |
| 729 | 729 | break; |
| 730 | 730 | case 'address': |
| 731 | - $this->update_meta( '_wpinv_address', $this->address ); |
|
| 731 | + $this->update_meta('_wpinv_address', $this->address); |
|
| 732 | 732 | $this->user_info['address'] = $this->address; |
| 733 | 733 | break; |
| 734 | 734 | case 'city': |
| 735 | - $this->update_meta( '_wpinv_city', $this->city ); |
|
| 735 | + $this->update_meta('_wpinv_city', $this->city); |
|
| 736 | 736 | $this->user_info['city'] = $this->city; |
| 737 | 737 | break; |
| 738 | 738 | case 'country': |
| 739 | - $this->update_meta( '_wpinv_country', $this->country ); |
|
| 739 | + $this->update_meta('_wpinv_country', $this->country); |
|
| 740 | 740 | $this->user_info['country'] = $this->country; |
| 741 | 741 | break; |
| 742 | 742 | case 'state': |
| 743 | - $this->update_meta( '_wpinv_state', $this->state ); |
|
| 743 | + $this->update_meta('_wpinv_state', $this->state); |
|
| 744 | 744 | $this->user_info['state'] = $this->state; |
| 745 | 745 | break; |
| 746 | 746 | case 'zip': |
| 747 | - $this->update_meta( '_wpinv_zip', $this->zip ); |
|
| 747 | + $this->update_meta('_wpinv_zip', $this->zip); |
|
| 748 | 748 | $this->user_info['zip'] = $this->zip; |
| 749 | 749 | break; |
| 750 | 750 | case 'company': |
| 751 | - $this->update_meta( '_wpinv_company', $this->company ); |
|
| 751 | + $this->update_meta('_wpinv_company', $this->company); |
|
| 752 | 752 | $this->user_info['company'] = $this->company; |
| 753 | 753 | break; |
| 754 | 754 | case 'vat_number': |
| 755 | - $this->update_meta( '_wpinv_vat_number', $this->vat_number ); |
|
| 755 | + $this->update_meta('_wpinv_vat_number', $this->vat_number); |
|
| 756 | 756 | $this->user_info['vat_number'] = $this->vat_number; |
| 757 | 757 | |
| 758 | - $vat_info = $wpi_session->get( 'user_vat_data' ); |
|
| 759 | - if ( $this->vat_number && !empty( $vat_info ) && isset( $vat_info['number'] ) && isset( $vat_info['valid'] ) && $vat_info['number'] == $this->vat_number ) { |
|
| 760 | - $adddress_confirmed = isset( $vat_info['adddress_confirmed'] ) ? $vat_info['adddress_confirmed'] : false; |
|
| 761 | - $this->update_meta( '_wpinv_adddress_confirmed', (bool)$adddress_confirmed ); |
|
| 758 | + $vat_info = $wpi_session->get('user_vat_data'); |
|
| 759 | + if ($this->vat_number && !empty($vat_info) && isset($vat_info['number']) && isset($vat_info['valid']) && $vat_info['number'] == $this->vat_number) { |
|
| 760 | + $adddress_confirmed = isset($vat_info['adddress_confirmed']) ? $vat_info['adddress_confirmed'] : false; |
|
| 761 | + $this->update_meta('_wpinv_adddress_confirmed', (bool)$adddress_confirmed); |
|
| 762 | 762 | $this->user_info['adddress_confirmed'] = (bool)$adddress_confirmed; |
| 763 | 763 | } |
| 764 | 764 | |
| 765 | 765 | break; |
| 766 | 766 | case 'vat_rate': |
| 767 | - $this->update_meta( '_wpinv_vat_rate', $this->vat_rate ); |
|
| 767 | + $this->update_meta('_wpinv_vat_rate', $this->vat_rate); |
|
| 768 | 768 | $this->user_info['vat_rate'] = $this->vat_rate; |
| 769 | 769 | break; |
| 770 | 770 | case 'adddress_confirmed': |
| 771 | - $this->update_meta( '_wpinv_adddress_confirmed', $this->adddress_confirmed ); |
|
| 771 | + $this->update_meta('_wpinv_adddress_confirmed', $this->adddress_confirmed); |
|
| 772 | 772 | $this->user_info['adddress_confirmed'] = $this->adddress_confirmed; |
| 773 | 773 | break; |
| 774 | 774 | |
| 775 | 775 | case 'key': |
| 776 | - $this->update_meta( '_wpinv_key', $this->key ); |
|
| 776 | + $this->update_meta('_wpinv_key', $this->key); |
|
| 777 | 777 | break; |
| 778 | 778 | case 'date': |
| 779 | 779 | $args = array( |
@@ -782,49 +782,49 @@ discard block |
||
| 782 | 782 | 'edit_date' => true, |
| 783 | 783 | ); |
| 784 | 784 | |
| 785 | - wp_update_post( $args ); |
|
| 785 | + wp_update_post($args); |
|
| 786 | 786 | break; |
| 787 | 787 | case 'due_date': |
| 788 | - if ( empty( $this->due_date ) ) { |
|
| 788 | + if (empty($this->due_date)) { |
|
| 789 | 789 | $this->due_date = 'none'; |
| 790 | 790 | } |
| 791 | 791 | |
| 792 | - $this->update_meta( '_wpinv_due_date', $this->due_date ); |
|
| 792 | + $this->update_meta('_wpinv_due_date', $this->due_date); |
|
| 793 | 793 | break; |
| 794 | 794 | case 'completed_date': |
| 795 | - $this->update_meta( '_wpinv_completed_date', $this->completed_date ); |
|
| 795 | + $this->update_meta('_wpinv_completed_date', $this->completed_date); |
|
| 796 | 796 | break; |
| 797 | 797 | case 'discounts': |
| 798 | - if ( ! is_array( $this->discounts ) ) { |
|
| 799 | - $this->discounts = explode( ',', $this->discounts ); |
|
| 798 | + if (!is_array($this->discounts)) { |
|
| 799 | + $this->discounts = explode(',', $this->discounts); |
|
| 800 | 800 | } |
| 801 | 801 | |
| 802 | - $this->user_info['discount'] = implode( ',', $this->discounts ); |
|
| 802 | + $this->user_info['discount'] = implode(',', $this->discounts); |
|
| 803 | 803 | break; |
| 804 | 804 | case 'discount': |
| 805 | - $this->update_meta( '_wpinv_discount', wpinv_round_amount( $this->discount ) ); |
|
| 805 | + $this->update_meta('_wpinv_discount', wpinv_round_amount($this->discount)); |
|
| 806 | 806 | break; |
| 807 | 807 | case 'discount_code': |
| 808 | - $this->update_meta( '_wpinv_discount_code', $this->discount_code ); |
|
| 808 | + $this->update_meta('_wpinv_discount_code', $this->discount_code); |
|
| 809 | 809 | break; |
| 810 | 810 | case 'parent_invoice': |
| 811 | 811 | $args = array( |
| 812 | 812 | 'ID' => $this->ID, |
| 813 | 813 | 'post_parent' => $this->parent_invoice, |
| 814 | 814 | ); |
| 815 | - wp_update_post( $args ); |
|
| 815 | + wp_update_post($args); |
|
| 816 | 816 | break; |
| 817 | 817 | default: |
| 818 | - do_action( 'wpinv_save', $this, $key ); |
|
| 818 | + do_action('wpinv_save', $this, $key); |
|
| 819 | 819 | break; |
| 820 | 820 | } |
| 821 | 821 | } |
| 822 | 822 | |
| 823 | - $this->update_meta( '_wpinv_subtotal', wpinv_round_amount( $this->subtotal ) ); |
|
| 824 | - $this->update_meta( '_wpinv_total', wpinv_round_amount( $this->total ) ); |
|
| 825 | - $this->update_meta( '_wpinv_tax', wpinv_round_amount( $this->tax ) ); |
|
| 823 | + $this->update_meta('_wpinv_subtotal', wpinv_round_amount($this->subtotal)); |
|
| 824 | + $this->update_meta('_wpinv_total', wpinv_round_amount($this->total)); |
|
| 825 | + $this->update_meta('_wpinv_tax', wpinv_round_amount($this->tax)); |
|
| 826 | 826 | |
| 827 | - $this->items = array_values( $this->items ); |
|
| 827 | + $this->items = array_values($this->items); |
|
| 828 | 828 | |
| 829 | 829 | $new_meta = array( |
| 830 | 830 | 'items' => $this->items, |
@@ -835,12 +835,12 @@ discard block |
||
| 835 | 835 | ); |
| 836 | 836 | |
| 837 | 837 | $meta = $this->get_meta(); |
| 838 | - $merged_meta = array_merge( $meta, $new_meta ); |
|
| 838 | + $merged_meta = array_merge($meta, $new_meta); |
|
| 839 | 839 | |
| 840 | 840 | // Only save the payment meta if it's changed |
| 841 | - if ( md5( serialize( $meta ) ) !== md5( serialize( $merged_meta) ) ) { |
|
| 842 | - $updated = $this->update_meta( '_wpinv_payment_meta', $merged_meta ); |
|
| 843 | - if ( false !== $updated ) { |
|
| 841 | + if (md5(serialize($meta)) !== md5(serialize($merged_meta))) { |
|
| 842 | + $updated = $this->update_meta('_wpinv_payment_meta', $merged_meta); |
|
| 843 | + if (false !== $updated) { |
|
| 844 | 844 | $saved = true; |
| 845 | 845 | } |
| 846 | 846 | } |
@@ -848,15 +848,15 @@ discard block |
||
| 848 | 848 | $this->pending = array(); |
| 849 | 849 | $saved = true; |
| 850 | 850 | } else { |
| 851 | - $this->update_meta( '_wpinv_subtotal', wpinv_round_amount( $this->subtotal ) ); |
|
| 852 | - $this->update_meta( '_wpinv_total', wpinv_round_amount( $this->total ) ); |
|
| 853 | - $this->update_meta( '_wpinv_tax', wpinv_round_amount( $this->tax ) ); |
|
| 851 | + $this->update_meta('_wpinv_subtotal', wpinv_round_amount($this->subtotal)); |
|
| 852 | + $this->update_meta('_wpinv_total', wpinv_round_amount($this->total)); |
|
| 853 | + $this->update_meta('_wpinv_tax', wpinv_round_amount($this->tax)); |
|
| 854 | 854 | } |
| 855 | 855 | |
| 856 | - do_action( 'wpinv_invoice_save', $this, $saved ); |
|
| 856 | + do_action('wpinv_invoice_save', $this, $saved); |
|
| 857 | 857 | |
| 858 | - if ( true === $saved || $setup ) { |
|
| 859 | - $this->setup_invoice( $this->ID ); |
|
| 858 | + if (true === $saved || $setup) { |
|
| 859 | + $this->setup_invoice($this->ID); |
|
| 860 | 860 | } |
| 861 | 861 | |
| 862 | 862 | $this->refresh_item_ids(); |
@@ -864,7 +864,7 @@ discard block |
||
| 864 | 864 | return $saved; |
| 865 | 865 | } |
| 866 | 866 | |
| 867 | - public function add_fee( $args, $global = true ) { |
|
| 867 | + public function add_fee($args, $global = true) { |
|
| 868 | 868 | $default_args = array( |
| 869 | 869 | 'label' => '', |
| 870 | 870 | 'amount' => 0, |
@@ -874,75 +874,75 @@ discard block |
||
| 874 | 874 | 'item_id' => 0, |
| 875 | 875 | ); |
| 876 | 876 | |
| 877 | - $fee = wp_parse_args( $args, $default_args ); |
|
| 877 | + $fee = wp_parse_args($args, $default_args); |
|
| 878 | 878 | |
| 879 | - if ( !empty( $fee['label'] ) ) { |
|
| 879 | + if (!empty($fee['label'])) { |
|
| 880 | 880 | return false; |
| 881 | 881 | } |
| 882 | 882 | |
| 883 | - $fee['id'] = sanitize_title( $fee['label'] ); |
|
| 883 | + $fee['id'] = sanitize_title($fee['label']); |
|
| 884 | 884 | |
| 885 | - $this->fees[] = $fee; |
|
| 885 | + $this->fees[] = $fee; |
|
| 886 | 886 | |
| 887 | 887 | $added_fee = $fee; |
| 888 | 888 | $added_fee['action'] = 'add'; |
| 889 | 889 | $this->pending['fees'][] = $added_fee; |
| 890 | - reset( $this->fees ); |
|
| 890 | + reset($this->fees); |
|
| 891 | 891 | |
| 892 | - $this->increase_fees( $fee['amount'] ); |
|
| 892 | + $this->increase_fees($fee['amount']); |
|
| 893 | 893 | return true; |
| 894 | 894 | } |
| 895 | 895 | |
| 896 | - public function remove_fee( $key ) { |
|
| 896 | + public function remove_fee($key) { |
|
| 897 | 897 | $removed = false; |
| 898 | 898 | |
| 899 | - if ( is_numeric( $key ) ) { |
|
| 900 | - $removed = $this->remove_fee_by( 'index', $key ); |
|
| 899 | + if (is_numeric($key)) { |
|
| 900 | + $removed = $this->remove_fee_by('index', $key); |
|
| 901 | 901 | } |
| 902 | 902 | |
| 903 | 903 | return $removed; |
| 904 | 904 | } |
| 905 | 905 | |
| 906 | - public function remove_fee_by( $key, $value, $global = false ) { |
|
| 907 | - $allowed_fee_keys = apply_filters( 'wpinv_fee_keys', array( |
|
| 906 | + public function remove_fee_by($key, $value, $global = false) { |
|
| 907 | + $allowed_fee_keys = apply_filters('wpinv_fee_keys', array( |
|
| 908 | 908 | 'index', 'label', 'amount', 'type', |
| 909 | - ) ); |
|
| 909 | + )); |
|
| 910 | 910 | |
| 911 | - if ( ! in_array( $key, $allowed_fee_keys ) ) { |
|
| 911 | + if (!in_array($key, $allowed_fee_keys)) { |
|
| 912 | 912 | return false; |
| 913 | 913 | } |
| 914 | 914 | |
| 915 | 915 | $removed = false; |
| 916 | - if ( 'index' === $key && array_key_exists( $value, $this->fees ) ) { |
|
| 917 | - $removed_fee = $this->fees[ $value ]; |
|
| 916 | + if ('index' === $key && array_key_exists($value, $this->fees)) { |
|
| 917 | + $removed_fee = $this->fees[$value]; |
|
| 918 | 918 | $removed_fee['action'] = 'remove'; |
| 919 | 919 | $this->pending['fees'][] = $removed_fee; |
| 920 | 920 | |
| 921 | - $this->decrease_fees( $removed_fee['amount'] ); |
|
| 921 | + $this->decrease_fees($removed_fee['amount']); |
|
| 922 | 922 | |
| 923 | - unset( $this->fees[ $value ] ); |
|
| 923 | + unset($this->fees[$value]); |
|
| 924 | 924 | $removed = true; |
| 925 | - } else if ( 'index' !== $key ) { |
|
| 926 | - foreach ( $this->fees as $index => $fee ) { |
|
| 927 | - if ( isset( $fee[ $key ] ) && $fee[ $key ] == $value ) { |
|
| 925 | + } else if ('index' !== $key) { |
|
| 926 | + foreach ($this->fees as $index => $fee) { |
|
| 927 | + if (isset($fee[$key]) && $fee[$key] == $value) { |
|
| 928 | 928 | $removed_fee = $fee; |
| 929 | 929 | $removed_fee['action'] = 'remove'; |
| 930 | 930 | $this->pending['fees'][] = $removed_fee; |
| 931 | 931 | |
| 932 | - $this->decrease_fees( $removed_fee['amount'] ); |
|
| 932 | + $this->decrease_fees($removed_fee['amount']); |
|
| 933 | 933 | |
| 934 | - unset( $this->fees[ $index ] ); |
|
| 934 | + unset($this->fees[$index]); |
|
| 935 | 935 | $removed = true; |
| 936 | 936 | |
| 937 | - if ( false === $global ) { |
|
| 937 | + if (false === $global) { |
|
| 938 | 938 | break; |
| 939 | 939 | } |
| 940 | 940 | } |
| 941 | 941 | } |
| 942 | 942 | } |
| 943 | 943 | |
| 944 | - if ( true === $removed ) { |
|
| 945 | - $this->fees = array_values( $this->fees ); |
|
| 944 | + if (true === $removed) { |
|
| 945 | + $this->fees = array_values($this->fees); |
|
| 946 | 946 | } |
| 947 | 947 | |
| 948 | 948 | return $removed; |
@@ -950,35 +950,35 @@ discard block |
||
| 950 | 950 | |
| 951 | 951 | |
| 952 | 952 | |
| 953 | - public function add_note( $note = '', $customer_type = false, $added_by_user = false, $system = false ) { |
|
| 953 | + public function add_note($note = '', $customer_type = false, $added_by_user = false, $system = false) { |
|
| 954 | 954 | // Bail if no note specified |
| 955 | - if( !$note ) { |
|
| 955 | + if (!$note) { |
|
| 956 | 956 | return false; |
| 957 | 957 | } |
| 958 | 958 | |
| 959 | - if ( empty( $this->ID ) ) |
|
| 959 | + if (empty($this->ID)) |
|
| 960 | 960 | return false; |
| 961 | 961 | |
| 962 | - if ( ( ( is_user_logged_in() && current_user_can( 'manage_options' ) ) || $added_by_user ) && !$system ) { |
|
| 963 | - $user = get_user_by( 'id', get_current_user_id() ); |
|
| 962 | + if (((is_user_logged_in() && current_user_can('manage_options')) || $added_by_user) && !$system) { |
|
| 963 | + $user = get_user_by('id', get_current_user_id()); |
|
| 964 | 964 | $comment_author = $user->display_name; |
| 965 | 965 | $comment_author_email = $user->user_email; |
| 966 | 966 | } else { |
| 967 | - $comment_author = __( 'System', 'invoicing' ); |
|
| 968 | - $comment_author_email = strtolower( __( 'System', 'invoicing' ) ) . '@'; |
|
| 969 | - $comment_author_email .= isset( $_SERVER['HTTP_HOST'] ) ? str_replace( 'www.', '', $_SERVER['HTTP_HOST'] ) : 'noreply.com'; |
|
| 970 | - $comment_author_email = sanitize_email( $comment_author_email ); |
|
| 967 | + $comment_author = __('System', 'invoicing'); |
|
| 968 | + $comment_author_email = strtolower(__('System', 'invoicing')) . '@'; |
|
| 969 | + $comment_author_email .= isset($_SERVER['HTTP_HOST']) ? str_replace('www.', '', $_SERVER['HTTP_HOST']) : 'noreply.com'; |
|
| 970 | + $comment_author_email = sanitize_email($comment_author_email); |
|
| 971 | 971 | } |
| 972 | 972 | |
| 973 | - do_action( 'wpinv_pre_insert_invoice_note', $this->ID, $note, $customer_type ); |
|
| 973 | + do_action('wpinv_pre_insert_invoice_note', $this->ID, $note, $customer_type); |
|
| 974 | 974 | |
| 975 | - $note_id = wp_insert_comment( wp_filter_comment( array( |
|
| 975 | + $note_id = wp_insert_comment(wp_filter_comment(array( |
|
| 976 | 976 | 'comment_post_ID' => $this->ID, |
| 977 | 977 | 'comment_content' => $note, |
| 978 | 978 | 'comment_agent' => 'GeoDirectory', |
| 979 | 979 | 'user_id' => is_admin() ? get_current_user_id() : 0, |
| 980 | - 'comment_date' => current_time( 'mysql' ), |
|
| 981 | - 'comment_date_gmt' => current_time( 'mysql', 1 ), |
|
| 980 | + 'comment_date' => current_time('mysql'), |
|
| 981 | + 'comment_date_gmt' => current_time('mysql', 1), |
|
| 982 | 982 | 'comment_approved' => 1, |
| 983 | 983 | 'comment_parent' => 0, |
| 984 | 984 | 'comment_author' => $comment_author, |
@@ -986,53 +986,53 @@ discard block |
||
| 986 | 986 | 'comment_author_url' => '', |
| 987 | 987 | 'comment_author_email' => $comment_author_email, |
| 988 | 988 | 'comment_type' => 'wpinv_note' |
| 989 | - ) ) ); |
|
| 989 | + ))); |
|
| 990 | 990 | |
| 991 | - do_action( 'wpinv_insert_payment_note', $note_id, $this->ID, $note ); |
|
| 991 | + do_action('wpinv_insert_payment_note', $note_id, $this->ID, $note); |
|
| 992 | 992 | |
| 993 | - if ( $customer_type ) { |
|
| 994 | - add_comment_meta( $note_id, '_wpi_customer_note', 1 ); |
|
| 993 | + if ($customer_type) { |
|
| 994 | + add_comment_meta($note_id, '_wpi_customer_note', 1); |
|
| 995 | 995 | |
| 996 | - do_action( 'wpinv_new_customer_note', array( 'invoice_id' => $this->ID, 'user_note' => $note ) ); |
|
| 996 | + do_action('wpinv_new_customer_note', array('invoice_id' => $this->ID, 'user_note' => $note)); |
|
| 997 | 997 | } |
| 998 | 998 | |
| 999 | 999 | return $note_id; |
| 1000 | 1000 | } |
| 1001 | 1001 | |
| 1002 | - private function increase_subtotal( $amount = 0.00 ) { |
|
| 1003 | - $amount = (float) $amount; |
|
| 1002 | + private function increase_subtotal($amount = 0.00) { |
|
| 1003 | + $amount = (float)$amount; |
|
| 1004 | 1004 | $this->subtotal += $amount; |
| 1005 | - $this->subtotal = wpinv_round_amount( $this->subtotal ); |
|
| 1005 | + $this->subtotal = wpinv_round_amount($this->subtotal); |
|
| 1006 | 1006 | |
| 1007 | 1007 | $this->recalculate_total(); |
| 1008 | 1008 | } |
| 1009 | 1009 | |
| 1010 | - private function decrease_subtotal( $amount = 0.00 ) { |
|
| 1011 | - $amount = (float) $amount; |
|
| 1010 | + private function decrease_subtotal($amount = 0.00) { |
|
| 1011 | + $amount = (float)$amount; |
|
| 1012 | 1012 | $this->subtotal -= $amount; |
| 1013 | - $this->subtotal = wpinv_round_amount( $this->subtotal ); |
|
| 1013 | + $this->subtotal = wpinv_round_amount($this->subtotal); |
|
| 1014 | 1014 | |
| 1015 | - if ( $this->subtotal < 0 ) { |
|
| 1015 | + if ($this->subtotal < 0) { |
|
| 1016 | 1016 | $this->subtotal = 0; |
| 1017 | 1017 | } |
| 1018 | 1018 | |
| 1019 | 1019 | $this->recalculate_total(); |
| 1020 | 1020 | } |
| 1021 | 1021 | |
| 1022 | - private function increase_fees( $amount = 0.00 ) { |
|
| 1022 | + private function increase_fees($amount = 0.00) { |
|
| 1023 | 1023 | $amount = (float)$amount; |
| 1024 | 1024 | $this->fees_total += $amount; |
| 1025 | - $this->fees_total = wpinv_round_amount( $this->fees_total ); |
|
| 1025 | + $this->fees_total = wpinv_round_amount($this->fees_total); |
|
| 1026 | 1026 | |
| 1027 | 1027 | $this->recalculate_total(); |
| 1028 | 1028 | } |
| 1029 | 1029 | |
| 1030 | - private function decrease_fees( $amount = 0.00 ) { |
|
| 1031 | - $amount = (float) $amount; |
|
| 1030 | + private function decrease_fees($amount = 0.00) { |
|
| 1031 | + $amount = (float)$amount; |
|
| 1032 | 1032 | $this->fees_total -= $amount; |
| 1033 | - $this->fees_total = wpinv_round_amount( $this->fees_total ); |
|
| 1033 | + $this->fees_total = wpinv_round_amount($this->fees_total); |
|
| 1034 | 1034 | |
| 1035 | - if ( $this->fees_total < 0 ) { |
|
| 1035 | + if ($this->fees_total < 0) { |
|
| 1036 | 1036 | $this->fees_total = 0; |
| 1037 | 1037 | } |
| 1038 | 1038 | |
@@ -1043,54 +1043,54 @@ discard block |
||
| 1043 | 1043 | global $wpi_nosave; |
| 1044 | 1044 | |
| 1045 | 1045 | $this->total = $this->subtotal + $this->tax + $this->fees_total; |
| 1046 | - $this->total = wpinv_round_amount( $this->total ); |
|
| 1046 | + $this->total = wpinv_round_amount($this->total); |
|
| 1047 | 1047 | |
| 1048 | - do_action( 'wpinv_invoice_recalculate_total', $this, $wpi_nosave ); |
|
| 1048 | + do_action('wpinv_invoice_recalculate_total', $this, $wpi_nosave); |
|
| 1049 | 1049 | } |
| 1050 | 1050 | |
| 1051 | - public function increase_tax( $amount = 0.00 ) { |
|
| 1052 | - $amount = (float) $amount; |
|
| 1051 | + public function increase_tax($amount = 0.00) { |
|
| 1052 | + $amount = (float)$amount; |
|
| 1053 | 1053 | $this->tax += $amount; |
| 1054 | 1054 | |
| 1055 | 1055 | $this->recalculate_total(); |
| 1056 | 1056 | } |
| 1057 | 1057 | |
| 1058 | - public function decrease_tax( $amount = 0.00 ) { |
|
| 1059 | - $amount = (float) $amount; |
|
| 1058 | + public function decrease_tax($amount = 0.00) { |
|
| 1059 | + $amount = (float)$amount; |
|
| 1060 | 1060 | $this->tax -= $amount; |
| 1061 | 1061 | |
| 1062 | - if ( $this->tax < 0 ) { |
|
| 1062 | + if ($this->tax < 0) { |
|
| 1063 | 1063 | $this->tax = 0; |
| 1064 | 1064 | } |
| 1065 | 1065 | |
| 1066 | 1066 | $this->recalculate_total(); |
| 1067 | 1067 | } |
| 1068 | 1068 | |
| 1069 | - public function update_status( $new_status = false, $note = '', $manual = false ) { |
|
| 1070 | - $old_status = ! empty( $this->old_status ) ? $this->old_status : get_post_status( $this->ID ); |
|
| 1069 | + public function update_status($new_status = false, $note = '', $manual = false) { |
|
| 1070 | + $old_status = !empty($this->old_status) ? $this->old_status : get_post_status($this->ID); |
|
| 1071 | 1071 | |
| 1072 | - if ( $old_status === $new_status && in_array( $new_status, array_keys( wpinv_get_invoice_statuses() ) ) ) { |
|
| 1072 | + if ($old_status === $new_status && in_array($new_status, array_keys(wpinv_get_invoice_statuses()))) { |
|
| 1073 | 1073 | return false; // Don't permit status changes that aren't changes |
| 1074 | 1074 | } |
| 1075 | 1075 | |
| 1076 | - $do_change = apply_filters( 'wpinv_should_update_invoice_status', true, $this->ID, $new_status, $old_status ); |
|
| 1076 | + $do_change = apply_filters('wpinv_should_update_invoice_status', true, $this->ID, $new_status, $old_status); |
|
| 1077 | 1077 | $updated = false; |
| 1078 | 1078 | |
| 1079 | - if ( $do_change ) { |
|
| 1080 | - do_action( 'wpinv_before_invoice_status_change', $this->ID, $new_status, $old_status ); |
|
| 1079 | + if ($do_change) { |
|
| 1080 | + do_action('wpinv_before_invoice_status_change', $this->ID, $new_status, $old_status); |
|
| 1081 | 1081 | |
| 1082 | 1082 | $update_post_data = array(); |
| 1083 | 1083 | $update_post_data['ID'] = $this->ID; |
| 1084 | 1084 | $update_post_data['post_status'] = $new_status; |
| 1085 | - $update_post_data['edit_date'] = current_time( 'mysql', 0 ); |
|
| 1086 | - $update_post_data['edit_date_gmt'] = current_time( 'mysql', 1 ); |
|
| 1085 | + $update_post_data['edit_date'] = current_time('mysql', 0); |
|
| 1086 | + $update_post_data['edit_date_gmt'] = current_time('mysql', 1); |
|
| 1087 | 1087 | |
| 1088 | - $update_post_data = apply_filters( 'wpinv_update_invoice_status_fields', $update_post_data, $this->ID ); |
|
| 1088 | + $update_post_data = apply_filters('wpinv_update_invoice_status_fields', $update_post_data, $this->ID); |
|
| 1089 | 1089 | |
| 1090 | - $updated = wp_update_post( $update_post_data ); |
|
| 1090 | + $updated = wp_update_post($update_post_data); |
|
| 1091 | 1091 | |
| 1092 | 1092 | // Process any specific status functions |
| 1093 | - switch( $new_status ) { |
|
| 1093 | + switch ($new_status) { |
|
| 1094 | 1094 | case 'wpi-refunded': |
| 1095 | 1095 | $this->process_refund(); |
| 1096 | 1096 | break; |
@@ -1103,9 +1103,9 @@ discard block |
||
| 1103 | 1103 | } |
| 1104 | 1104 | |
| 1105 | 1105 | // Status was changed. |
| 1106 | - do_action( 'wpinv_status_' . $new_status, $this->ID, $old_status ); |
|
| 1107 | - do_action( 'wpinv_status_' . $old_status . '_to_' . $new_status, $this->ID, $old_status ); |
|
| 1108 | - do_action( 'wpinv_update_status', $this->ID, $new_status, $old_status ); |
|
| 1106 | + do_action('wpinv_status_' . $new_status, $this->ID, $old_status); |
|
| 1107 | + do_action('wpinv_status_' . $old_status . '_to_' . $new_status, $this->ID, $old_status); |
|
| 1108 | + do_action('wpinv_update_status', $this->ID, $new_status, $old_status); |
|
| 1109 | 1109 | } |
| 1110 | 1110 | |
| 1111 | 1111 | return $updated; |
@@ -1119,72 +1119,72 @@ discard block |
||
| 1119 | 1119 | $this->save(); |
| 1120 | 1120 | } |
| 1121 | 1121 | |
| 1122 | - public function update_meta( $meta_key = '', $meta_value = '', $prev_value = '' ) { |
|
| 1123 | - if ( empty( $meta_key ) ) { |
|
| 1122 | + public function update_meta($meta_key = '', $meta_value = '', $prev_value = '') { |
|
| 1123 | + if (empty($meta_key)) { |
|
| 1124 | 1124 | return false; |
| 1125 | 1125 | } |
| 1126 | 1126 | |
| 1127 | - if ( $meta_key == 'key' || $meta_key == 'date' ) { |
|
| 1127 | + if ($meta_key == 'key' || $meta_key == 'date') { |
|
| 1128 | 1128 | $current_meta = $this->get_meta(); |
| 1129 | - $current_meta[ $meta_key ] = $meta_value; |
|
| 1129 | + $current_meta[$meta_key] = $meta_value; |
|
| 1130 | 1130 | |
| 1131 | 1131 | $meta_key = '_wpinv_payment_meta'; |
| 1132 | 1132 | $meta_value = $current_meta; |
| 1133 | 1133 | } |
| 1134 | 1134 | |
| 1135 | - $meta_value = apply_filters( 'wpinv_update_payment_meta_' . $meta_key, $meta_value, $this->ID ); |
|
| 1135 | + $meta_value = apply_filters('wpinv_update_payment_meta_' . $meta_key, $meta_value, $this->ID); |
|
| 1136 | 1136 | |
| 1137 | - if ( $meta_key == '_wpinv_completed_date' && !empty( $meta_value ) ) { |
|
| 1137 | + if ($meta_key == '_wpinv_completed_date' && !empty($meta_value)) { |
|
| 1138 | 1138 | $args = array( |
| 1139 | 1139 | 'ID' => $this->ID, |
| 1140 | 1140 | 'post_date' => $meta_value, |
| 1141 | 1141 | 'edit_date' => true, |
| 1142 | - 'post_date_gmt' => get_gmt_from_date( $meta_value ), |
|
| 1142 | + 'post_date_gmt' => get_gmt_from_date($meta_value), |
|
| 1143 | 1143 | 'post_modified' => $meta_value, |
| 1144 | - 'post_modified_gmt' => get_gmt_from_date( $meta_value ) |
|
| 1144 | + 'post_modified_gmt' => get_gmt_from_date($meta_value) |
|
| 1145 | 1145 | ); |
| 1146 | - wp_update_post( $args ); |
|
| 1146 | + wp_update_post($args); |
|
| 1147 | 1147 | } |
| 1148 | 1148 | |
| 1149 | - return update_post_meta( $this->ID, $meta_key, $meta_value, $prev_value ); |
|
| 1149 | + return update_post_meta($this->ID, $meta_key, $meta_value, $prev_value); |
|
| 1150 | 1150 | } |
| 1151 | 1151 | |
| 1152 | 1152 | private function process_refund() { |
| 1153 | 1153 | $process_refund = true; |
| 1154 | 1154 | |
| 1155 | 1155 | // If the payment was not in publish, don't decrement stats as they were never incremented |
| 1156 | - if ( 'publish' != $this->old_status || 'wpi-refunded' != $this->status ) { |
|
| 1156 | + if ('publish' != $this->old_status || 'wpi-refunded' != $this->status) { |
|
| 1157 | 1157 | $process_refund = false; |
| 1158 | 1158 | } |
| 1159 | 1159 | |
| 1160 | 1160 | // Allow extensions to filter for their own payment types, Example: Recurring Payments |
| 1161 | - $process_refund = apply_filters( 'wpinv_should_process_refund', $process_refund, $this ); |
|
| 1161 | + $process_refund = apply_filters('wpinv_should_process_refund', $process_refund, $this); |
|
| 1162 | 1162 | |
| 1163 | - if ( false === $process_refund ) { |
|
| 1163 | + if (false === $process_refund) { |
|
| 1164 | 1164 | return; |
| 1165 | 1165 | } |
| 1166 | 1166 | |
| 1167 | - do_action( 'wpinv_pre_refund_invoice', $this ); |
|
| 1167 | + do_action('wpinv_pre_refund_invoice', $this); |
|
| 1168 | 1168 | |
| 1169 | - $decrease_store_earnings = apply_filters( 'wpinv_decrease_store_earnings_on_refund', true, $this ); |
|
| 1170 | - $decrease_customer_value = apply_filters( 'wpinv_decrease_customer_value_on_refund', true, $this ); |
|
| 1171 | - $decrease_purchase_count = apply_filters( 'wpinv_decrease_customer_purchase_count_on_refund', true, $this ); |
|
| 1169 | + $decrease_store_earnings = apply_filters('wpinv_decrease_store_earnings_on_refund', true, $this); |
|
| 1170 | + $decrease_customer_value = apply_filters('wpinv_decrease_customer_value_on_refund', true, $this); |
|
| 1171 | + $decrease_purchase_count = apply_filters('wpinv_decrease_customer_purchase_count_on_refund', true, $this); |
|
| 1172 | 1172 | |
| 1173 | - do_action( 'wpinv_post_refund_invoice', $this ); |
|
| 1173 | + do_action('wpinv_post_refund_invoice', $this); |
|
| 1174 | 1174 | } |
| 1175 | 1175 | |
| 1176 | 1176 | private function process_failure() { |
| 1177 | 1177 | $discounts = $this->discounts; |
| 1178 | - if ( empty( $discounts ) ) { |
|
| 1178 | + if (empty($discounts)) { |
|
| 1179 | 1179 | return; |
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | - if ( ! is_array( $discounts ) ) { |
|
| 1183 | - $discounts = array_map( 'trim', explode( ',', $discounts ) ); |
|
| 1182 | + if (!is_array($discounts)) { |
|
| 1183 | + $discounts = array_map('trim', explode(',', $discounts)); |
|
| 1184 | 1184 | } |
| 1185 | 1185 | |
| 1186 | - foreach ( $discounts as $discount ) { |
|
| 1187 | - wpinv_decrease_discount_usage( $discount ); |
|
| 1186 | + foreach ($discounts as $discount) { |
|
| 1187 | + wpinv_decrease_discount_usage($discount); |
|
| 1188 | 1188 | } |
| 1189 | 1189 | } |
| 1190 | 1190 | |
@@ -1192,92 +1192,92 @@ discard block |
||
| 1192 | 1192 | $process_pending = true; |
| 1193 | 1193 | |
| 1194 | 1194 | // If the payment was not in publish or revoked status, don't decrement stats as they were never incremented |
| 1195 | - if ( ( 'publish' != $this->old_status && 'revoked' != $this->old_status ) || 'pending' != $this->status ) { |
|
| 1195 | + if (('publish' != $this->old_status && 'revoked' != $this->old_status) || 'pending' != $this->status) { |
|
| 1196 | 1196 | $process_pending = false; |
| 1197 | 1197 | } |
| 1198 | 1198 | |
| 1199 | 1199 | // Allow extensions to filter for their own payment types, Example: Recurring Payments |
| 1200 | - $process_pending = apply_filters( 'wpinv_should_process_pending', $process_pending, $this ); |
|
| 1200 | + $process_pending = apply_filters('wpinv_should_process_pending', $process_pending, $this); |
|
| 1201 | 1201 | |
| 1202 | - if ( false === $process_pending ) { |
|
| 1202 | + if (false === $process_pending) { |
|
| 1203 | 1203 | return; |
| 1204 | 1204 | } |
| 1205 | 1205 | |
| 1206 | - $decrease_store_earnings = apply_filters( 'wpinv_decrease_store_earnings_on_pending', true, $this ); |
|
| 1207 | - $decrease_customer_value = apply_filters( 'wpinv_decrease_customer_value_on_pending', true, $this ); |
|
| 1208 | - $decrease_purchase_count = apply_filters( 'wpinv_decrease_customer_purchase_count_on_pending', true, $this ); |
|
| 1206 | + $decrease_store_earnings = apply_filters('wpinv_decrease_store_earnings_on_pending', true, $this); |
|
| 1207 | + $decrease_customer_value = apply_filters('wpinv_decrease_customer_value_on_pending', true, $this); |
|
| 1208 | + $decrease_purchase_count = apply_filters('wpinv_decrease_customer_purchase_count_on_pending', true, $this); |
|
| 1209 | 1209 | |
| 1210 | 1210 | $this->completed_date = ''; |
| 1211 | - $this->update_meta( '_wpinv_completed_date', '' ); |
|
| 1211 | + $this->update_meta('_wpinv_completed_date', ''); |
|
| 1212 | 1212 | } |
| 1213 | 1213 | |
| 1214 | 1214 | // get data |
| 1215 | - public function get_meta( $meta_key = '_wpinv_payment_meta', $single = true ) { |
|
| 1216 | - $meta = get_post_meta( $this->ID, $meta_key, $single ); |
|
| 1215 | + public function get_meta($meta_key = '_wpinv_payment_meta', $single = true) { |
|
| 1216 | + $meta = get_post_meta($this->ID, $meta_key, $single); |
|
| 1217 | 1217 | |
| 1218 | - if ( $meta_key === '_wpinv_payment_meta' ) { |
|
| 1218 | + if ($meta_key === '_wpinv_payment_meta') { |
|
| 1219 | 1219 | |
| 1220 | - if(!is_array($meta)){$meta = array();} // we need this to be an array so make sure it is. |
|
| 1220 | + if (!is_array($meta)) {$meta = array(); } // we need this to be an array so make sure it is. |
|
| 1221 | 1221 | |
| 1222 | - if ( empty( $meta['key'] ) ) { |
|
| 1222 | + if (empty($meta['key'])) { |
|
| 1223 | 1223 | $meta['key'] = $this->setup_invoice_key(); |
| 1224 | 1224 | } |
| 1225 | 1225 | |
| 1226 | - if ( empty( $meta['date'] ) ) { |
|
| 1227 | - $meta['date'] = get_post_field( 'post_date', $this->ID ); |
|
| 1226 | + if (empty($meta['date'])) { |
|
| 1227 | + $meta['date'] = get_post_field('post_date', $this->ID); |
|
| 1228 | 1228 | } |
| 1229 | 1229 | } |
| 1230 | 1230 | |
| 1231 | - $meta = apply_filters( 'wpinv_get_invoice_meta_' . $meta_key, $meta, $this->ID ); |
|
| 1231 | + $meta = apply_filters('wpinv_get_invoice_meta_' . $meta_key, $meta, $this->ID); |
|
| 1232 | 1232 | |
| 1233 | - return apply_filters( 'wpinv_get_invoice_meta', $meta, $this->ID, $meta_key ); |
|
| 1233 | + return apply_filters('wpinv_get_invoice_meta', $meta, $this->ID, $meta_key); |
|
| 1234 | 1234 | } |
| 1235 | 1235 | |
| 1236 | 1236 | public function get_description() { |
| 1237 | - $post = get_post( $this->ID ); |
|
| 1237 | + $post = get_post($this->ID); |
|
| 1238 | 1238 | |
| 1239 | - $description = !empty( $post ) ? $post->post_content : ''; |
|
| 1240 | - return apply_filters( 'wpinv_get_description', $description, $this->ID, $this ); |
|
| 1239 | + $description = !empty($post) ? $post->post_content : ''; |
|
| 1240 | + return apply_filters('wpinv_get_description', $description, $this->ID, $this); |
|
| 1241 | 1241 | } |
| 1242 | 1242 | |
| 1243 | - public function get_status( $nicename = false ) { |
|
| 1244 | - if ( !$nicename ) { |
|
| 1243 | + public function get_status($nicename = false) { |
|
| 1244 | + if (!$nicename) { |
|
| 1245 | 1245 | $status = $this->status; |
| 1246 | 1246 | } else { |
| 1247 | 1247 | $status = $this->status_nicename; |
| 1248 | 1248 | } |
| 1249 | 1249 | |
| 1250 | - return apply_filters( 'wpinv_get_status', $status, $nicename, $this->ID, $this ); |
|
| 1250 | + return apply_filters('wpinv_get_status', $status, $nicename, $this->ID, $this); |
|
| 1251 | 1251 | } |
| 1252 | 1252 | |
| 1253 | 1253 | public function get_cart_details() { |
| 1254 | - return apply_filters( 'wpinv_cart_details', $this->cart_details, $this->ID, $this ); |
|
| 1254 | + return apply_filters('wpinv_cart_details', $this->cart_details, $this->ID, $this); |
|
| 1255 | 1255 | } |
| 1256 | 1256 | |
| 1257 | - public function get_subtotal( $currency = false ) { |
|
| 1258 | - $subtotal = wpinv_round_amount( $this->subtotal ); |
|
| 1257 | + public function get_subtotal($currency = false) { |
|
| 1258 | + $subtotal = wpinv_round_amount($this->subtotal); |
|
| 1259 | 1259 | |
| 1260 | - if ( $currency ) { |
|
| 1261 | - $subtotal = wpinv_price( wpinv_format_amount( $subtotal, NULL, !$currency ), $this->get_currency() ); |
|
| 1260 | + if ($currency) { |
|
| 1261 | + $subtotal = wpinv_price(wpinv_format_amount($subtotal, NULL, !$currency), $this->get_currency()); |
|
| 1262 | 1262 | } |
| 1263 | 1263 | |
| 1264 | - return apply_filters( 'wpinv_get_invoice_subtotal', $subtotal, $this->ID, $this, $currency ); |
|
| 1264 | + return apply_filters('wpinv_get_invoice_subtotal', $subtotal, $this->ID, $this, $currency); |
|
| 1265 | 1265 | } |
| 1266 | 1266 | |
| 1267 | - public function get_total( $currency = false ) { |
|
| 1268 | - if ( $this->is_free_trial() ) { |
|
| 1269 | - $total = wpinv_round_amount( 0 ); |
|
| 1267 | + public function get_total($currency = false) { |
|
| 1268 | + if ($this->is_free_trial()) { |
|
| 1269 | + $total = wpinv_round_amount(0); |
|
| 1270 | 1270 | } else { |
| 1271 | - $total = wpinv_round_amount( $this->total ); |
|
| 1271 | + $total = wpinv_round_amount($this->total); |
|
| 1272 | 1272 | } |
| 1273 | - if ( $currency ) { |
|
| 1274 | - $total = wpinv_price( wpinv_format_amount( $total, NULL, !$currency ), $this->get_currency() ); |
|
| 1273 | + if ($currency) { |
|
| 1274 | + $total = wpinv_price(wpinv_format_amount($total, NULL, !$currency), $this->get_currency()); |
|
| 1275 | 1275 | } |
| 1276 | 1276 | |
| 1277 | - return apply_filters( 'wpinv_get_invoice_total', $total, $this->ID, $this, $currency ); |
|
| 1277 | + return apply_filters('wpinv_get_invoice_total', $total, $this->ID, $this, $currency); |
|
| 1278 | 1278 | } |
| 1279 | 1279 | |
| 1280 | - public function get_recurring_details( $field = '', $currency = false ) { |
|
| 1280 | + public function get_recurring_details($field = '', $currency = false) { |
|
| 1281 | 1281 | $data = array(); |
| 1282 | 1282 | $data['cart_details'] = $this->cart_details; |
| 1283 | 1283 | $data['subtotal'] = $this->get_subtotal(); |
@@ -1285,45 +1285,45 @@ discard block |
||
| 1285 | 1285 | $data['tax'] = $this->get_tax(); |
| 1286 | 1286 | $data['total'] = $this->get_total(); |
| 1287 | 1287 | |
| 1288 | - if ( !empty( $this->cart_details ) && ( $this->is_parent() || $this->is_renewal() ) ) { |
|
| 1288 | + if (!empty($this->cart_details) && ($this->is_parent() || $this->is_renewal())) { |
|
| 1289 | 1289 | $is_free_trial = $this->is_free_trial(); |
| 1290 | - $discounts = $this->get_discounts( true ); |
|
| 1290 | + $discounts = $this->get_discounts(true); |
|
| 1291 | 1291 | |
| 1292 | - if ( $is_free_trial || !empty( $discounts ) ) { |
|
| 1292 | + if ($is_free_trial || !empty($discounts)) { |
|
| 1293 | 1293 | $first_use_only = false; |
| 1294 | 1294 | |
| 1295 | - if ( !empty( $discounts ) ) { |
|
| 1296 | - foreach ( $discounts as $key => $code ) { |
|
| 1297 | - if ( wpinv_discount_is_recurring( $code, true ) ) { |
|
| 1295 | + if (!empty($discounts)) { |
|
| 1296 | + foreach ($discounts as $key => $code) { |
|
| 1297 | + if (wpinv_discount_is_recurring($code, true)) { |
|
| 1298 | 1298 | $first_use_only = true; |
| 1299 | 1299 | break; |
| 1300 | 1300 | } |
| 1301 | 1301 | } |
| 1302 | 1302 | } |
| 1303 | 1303 | |
| 1304 | - if ( !$first_use_only ) { |
|
| 1305 | - $data['subtotal'] = wpinv_round_amount( $this->subtotal ); |
|
| 1306 | - $data['discount'] = wpinv_round_amount( $this->discount ); |
|
| 1307 | - $data['tax'] = wpinv_round_amount( $this->tax ); |
|
| 1308 | - $data['total'] = wpinv_round_amount( $this->total ); |
|
| 1304 | + if (!$first_use_only) { |
|
| 1305 | + $data['subtotal'] = wpinv_round_amount($this->subtotal); |
|
| 1306 | + $data['discount'] = wpinv_round_amount($this->discount); |
|
| 1307 | + $data['tax'] = wpinv_round_amount($this->tax); |
|
| 1308 | + $data['total'] = wpinv_round_amount($this->total); |
|
| 1309 | 1309 | } else { |
| 1310 | 1310 | $cart_subtotal = 0; |
| 1311 | 1311 | $cart_discount = 0; |
| 1312 | 1312 | $cart_tax = 0; |
| 1313 | 1313 | |
| 1314 | - foreach ( $this->cart_details as $key => $item ) { |
|
| 1315 | - $item_quantity = $item['quantity'] > 0 ? absint( $item['quantity'] ) : 1; |
|
| 1316 | - $item_subtotal = !empty( $item['subtotal'] ) ? $item['subtotal'] : $item['item_price'] * $item_quantity; |
|
| 1314 | + foreach ($this->cart_details as $key => $item) { |
|
| 1315 | + $item_quantity = $item['quantity'] > 0 ? absint($item['quantity']) : 1; |
|
| 1316 | + $item_subtotal = !empty($item['subtotal']) ? $item['subtotal'] : $item['item_price'] * $item_quantity; |
|
| 1317 | 1317 | $item_discount = 0; |
| 1318 | - $item_tax = $item_subtotal > 0 && !empty( $item['vat_rate'] ) ? ( $item_subtotal * 0.01 * (float)$item['vat_rate'] ) : 0; |
|
| 1318 | + $item_tax = $item_subtotal > 0 && !empty($item['vat_rate']) ? ($item_subtotal * 0.01 * (float)$item['vat_rate']) : 0; |
|
| 1319 | 1319 | |
| 1320 | - if ( wpinv_prices_include_tax() ) { |
|
| 1321 | - $item_subtotal -= wpinv_round_amount( $item_tax ); |
|
| 1320 | + if (wpinv_prices_include_tax()) { |
|
| 1321 | + $item_subtotal -= wpinv_round_amount($item_tax); |
|
| 1322 | 1322 | } |
| 1323 | 1323 | |
| 1324 | 1324 | $item_total = $item_subtotal - $item_discount + $item_tax; |
| 1325 | 1325 | // Do not allow totals to go negative |
| 1326 | - if ( $item_total < 0 ) { |
|
| 1326 | + if ($item_total < 0) { |
|
| 1327 | 1327 | $item_total = 0; |
| 1328 | 1328 | } |
| 1329 | 1329 | |
@@ -1331,113 +1331,113 @@ discard block |
||
| 1331 | 1331 | $cart_discount += (float)($item_discount); |
| 1332 | 1332 | $cart_tax += (float)($item_tax); |
| 1333 | 1333 | |
| 1334 | - $data['cart_details'][$key]['discount'] = wpinv_round_amount( $item_discount ); |
|
| 1335 | - $data['cart_details'][$key]['tax'] = wpinv_round_amount( $item_tax ); |
|
| 1336 | - $data['cart_details'][$key]['price'] = wpinv_round_amount( $item_total ); |
|
| 1334 | + $data['cart_details'][$key]['discount'] = wpinv_round_amount($item_discount); |
|
| 1335 | + $data['cart_details'][$key]['tax'] = wpinv_round_amount($item_tax); |
|
| 1336 | + $data['cart_details'][$key]['price'] = wpinv_round_amount($item_total); |
|
| 1337 | 1337 | } |
| 1338 | 1338 | |
| 1339 | - $data['subtotal'] = wpinv_round_amount( $cart_subtotal ); |
|
| 1340 | - $data['discount'] = wpinv_round_amount( $cart_discount ); |
|
| 1341 | - $data['tax'] = wpinv_round_amount( $cart_tax ); |
|
| 1342 | - $data['total'] = wpinv_round_amount( $data['subtotal'] + $data['tax'] ); |
|
| 1339 | + $data['subtotal'] = wpinv_round_amount($cart_subtotal); |
|
| 1340 | + $data['discount'] = wpinv_round_amount($cart_discount); |
|
| 1341 | + $data['tax'] = wpinv_round_amount($cart_tax); |
|
| 1342 | + $data['total'] = wpinv_round_amount($data['subtotal'] + $data['tax']); |
|
| 1343 | 1343 | } |
| 1344 | 1344 | } |
| 1345 | 1345 | } |
| 1346 | 1346 | |
| 1347 | - $data = apply_filters( 'wpinv_get_invoice_recurring_details', $data, $this, $field, $currency ); |
|
| 1347 | + $data = apply_filters('wpinv_get_invoice_recurring_details', $data, $this, $field, $currency); |
|
| 1348 | 1348 | |
| 1349 | - if ( isset( $data[$field] ) ) { |
|
| 1350 | - return ( $currency ? wpinv_price( $data[$field], $this->get_currency() ) : $data[$field] ); |
|
| 1349 | + if (isset($data[$field])) { |
|
| 1350 | + return ($currency ? wpinv_price($data[$field], $this->get_currency()) : $data[$field]); |
|
| 1351 | 1351 | } |
| 1352 | 1352 | |
| 1353 | 1353 | return $data; |
| 1354 | 1354 | } |
| 1355 | 1355 | |
| 1356 | - public function get_final_tax( $currency = false ) { |
|
| 1357 | - $final_total = wpinv_round_amount( $this->tax ); |
|
| 1358 | - if ( $currency ) { |
|
| 1359 | - $final_total = wpinv_price( wpinv_format_amount( $final_total, NULL, !$currency ), $this->get_currency() ); |
|
| 1356 | + public function get_final_tax($currency = false) { |
|
| 1357 | + $final_total = wpinv_round_amount($this->tax); |
|
| 1358 | + if ($currency) { |
|
| 1359 | + $final_total = wpinv_price(wpinv_format_amount($final_total, NULL, !$currency), $this->get_currency()); |
|
| 1360 | 1360 | } |
| 1361 | 1361 | |
| 1362 | - return apply_filters( 'wpinv_get_invoice_final_total', $final_total, $this, $currency ); |
|
| 1362 | + return apply_filters('wpinv_get_invoice_final_total', $final_total, $this, $currency); |
|
| 1363 | 1363 | } |
| 1364 | 1364 | |
| 1365 | - public function get_discounts( $array = false ) { |
|
| 1365 | + public function get_discounts($array = false) { |
|
| 1366 | 1366 | $discounts = $this->discounts; |
| 1367 | - if ( $array && $discounts ) { |
|
| 1368 | - $discounts = explode( ',', $discounts ); |
|
| 1367 | + if ($array && $discounts) { |
|
| 1368 | + $discounts = explode(',', $discounts); |
|
| 1369 | 1369 | } |
| 1370 | - return apply_filters( 'wpinv_payment_discounts', $discounts, $this->ID, $this, $array ); |
|
| 1370 | + return apply_filters('wpinv_payment_discounts', $discounts, $this->ID, $this, $array); |
|
| 1371 | 1371 | } |
| 1372 | 1372 | |
| 1373 | - public function get_discount( $currency = false, $dash = false ) { |
|
| 1374 | - if ( !empty( $this->discounts ) ) { |
|
| 1373 | + public function get_discount($currency = false, $dash = false) { |
|
| 1374 | + if (!empty($this->discounts)) { |
|
| 1375 | 1375 | global $ajax_cart_details; |
| 1376 | 1376 | $ajax_cart_details = $this->get_cart_details(); |
| 1377 | 1377 | |
| 1378 | - if ( !empty( $ajax_cart_details ) && count( $ajax_cart_details ) == count( $this->items ) ) { |
|
| 1378 | + if (!empty($ajax_cart_details) && count($ajax_cart_details) == count($this->items)) { |
|
| 1379 | 1379 | $cart_items = $ajax_cart_details; |
| 1380 | 1380 | } else { |
| 1381 | 1381 | $cart_items = $this->items; |
| 1382 | 1382 | } |
| 1383 | 1383 | |
| 1384 | - $this->discount = wpinv_get_cart_items_discount_amount( $cart_items , $this->discounts ); |
|
| 1384 | + $this->discount = wpinv_get_cart_items_discount_amount($cart_items, $this->discounts); |
|
| 1385 | 1385 | } |
| 1386 | - $discount = wpinv_round_amount( $this->discount ); |
|
| 1386 | + $discount = wpinv_round_amount($this->discount); |
|
| 1387 | 1387 | $dash = $dash && $discount > 0 ? '–' : ''; |
| 1388 | 1388 | |
| 1389 | - if ( $currency ) { |
|
| 1390 | - $discount = wpinv_price( wpinv_format_amount( $discount, NULL, !$currency ), $this->get_currency() ); |
|
| 1389 | + if ($currency) { |
|
| 1390 | + $discount = wpinv_price(wpinv_format_amount($discount, NULL, !$currency), $this->get_currency()); |
|
| 1391 | 1391 | } |
| 1392 | 1392 | |
| 1393 | - $discount = $dash . $discount; |
|
| 1393 | + $discount = $dash . $discount; |
|
| 1394 | 1394 | |
| 1395 | - return apply_filters( 'wpinv_get_invoice_discount', $discount, $this->ID, $this, $currency, $dash ); |
|
| 1395 | + return apply_filters('wpinv_get_invoice_discount', $discount, $this->ID, $this, $currency, $dash); |
|
| 1396 | 1396 | } |
| 1397 | 1397 | |
| 1398 | 1398 | public function get_discount_code() { |
| 1399 | 1399 | return $this->discount_code; |
| 1400 | 1400 | } |
| 1401 | 1401 | |
| 1402 | - public function get_tax( $currency = false ) { |
|
| 1403 | - $tax = wpinv_round_amount( $this->tax ); |
|
| 1402 | + public function get_tax($currency = false) { |
|
| 1403 | + $tax = wpinv_round_amount($this->tax); |
|
| 1404 | 1404 | |
| 1405 | - if ( $currency ) { |
|
| 1406 | - $tax = wpinv_price( wpinv_format_amount( $tax, NULL, !$currency ), $this->get_currency() ); |
|
| 1405 | + if ($currency) { |
|
| 1406 | + $tax = wpinv_price(wpinv_format_amount($tax, NULL, !$currency), $this->get_currency()); |
|
| 1407 | 1407 | } |
| 1408 | 1408 | |
| 1409 | - return apply_filters( 'wpinv_get_invoice_tax', $tax, $this->ID, $this, $currency ); |
|
| 1409 | + return apply_filters('wpinv_get_invoice_tax', $tax, $this->ID, $this, $currency); |
|
| 1410 | 1410 | } |
| 1411 | 1411 | |
| 1412 | - public function get_fees( $type = 'all' ) { |
|
| 1413 | - $fees = array(); |
|
| 1412 | + public function get_fees($type = 'all') { |
|
| 1413 | + $fees = array(); |
|
| 1414 | 1414 | |
| 1415 | - if ( ! empty( $this->fees ) && is_array( $this->fees ) ) { |
|
| 1416 | - foreach ( $this->fees as $fee ) { |
|
| 1417 | - if( 'all' != $type && ! empty( $fee['type'] ) && $type != $fee['type'] ) { |
|
| 1415 | + if (!empty($this->fees) && is_array($this->fees)) { |
|
| 1416 | + foreach ($this->fees as $fee) { |
|
| 1417 | + if ('all' != $type && !empty($fee['type']) && $type != $fee['type']) { |
|
| 1418 | 1418 | continue; |
| 1419 | 1419 | } |
| 1420 | 1420 | |
| 1421 | - $fee['label'] = stripslashes( $fee['label'] ); |
|
| 1422 | - $fee['amount_display'] = wpinv_price( $fee['amount'], $this->get_currency() ); |
|
| 1423 | - $fees[] = $fee; |
|
| 1421 | + $fee['label'] = stripslashes($fee['label']); |
|
| 1422 | + $fee['amount_display'] = wpinv_price($fee['amount'], $this->get_currency()); |
|
| 1423 | + $fees[] = $fee; |
|
| 1424 | 1424 | } |
| 1425 | 1425 | } |
| 1426 | 1426 | |
| 1427 | - return apply_filters( 'wpinv_get_invoice_fees', $fees, $this->ID, $this ); |
|
| 1427 | + return apply_filters('wpinv_get_invoice_fees', $fees, $this->ID, $this); |
|
| 1428 | 1428 | } |
| 1429 | 1429 | |
| 1430 | - public function get_fees_total( $type = 'all' ) { |
|
| 1431 | - $fees_total = (float) 0.00; |
|
| 1430 | + public function get_fees_total($type = 'all') { |
|
| 1431 | + $fees_total = (float)0.00; |
|
| 1432 | 1432 | |
| 1433 | - $payment_fees = isset( $this->payment_meta['fees'] ) ? $this->payment_meta['fees'] : array(); |
|
| 1434 | - if ( ! empty( $payment_fees ) ) { |
|
| 1435 | - foreach ( $payment_fees as $fee ) { |
|
| 1436 | - $fees_total += (float) $fee['amount']; |
|
| 1433 | + $payment_fees = isset($this->payment_meta['fees']) ? $this->payment_meta['fees'] : array(); |
|
| 1434 | + if (!empty($payment_fees)) { |
|
| 1435 | + foreach ($payment_fees as $fee) { |
|
| 1436 | + $fees_total += (float)$fee['amount']; |
|
| 1437 | 1437 | } |
| 1438 | 1438 | } |
| 1439 | 1439 | |
| 1440 | - return apply_filters( 'wpinv_get_invoice_fees_total', $fees_total, $this->ID, $this ); |
|
| 1440 | + return apply_filters('wpinv_get_invoice_fees_total', $fees_total, $this->ID, $this); |
|
| 1441 | 1441 | /* |
| 1442 | 1442 | $fees = $this->get_fees( $type ); |
| 1443 | 1443 | |
@@ -1457,116 +1457,116 @@ discard block |
||
| 1457 | 1457 | } |
| 1458 | 1458 | |
| 1459 | 1459 | public function get_user_id() { |
| 1460 | - return apply_filters( 'wpinv_user_id', $this->user_id, $this->ID, $this ); |
|
| 1460 | + return apply_filters('wpinv_user_id', $this->user_id, $this->ID, $this); |
|
| 1461 | 1461 | } |
| 1462 | 1462 | |
| 1463 | 1463 | public function get_first_name() { |
| 1464 | - return apply_filters( 'wpinv_first_name', $this->first_name, $this->ID, $this ); |
|
| 1464 | + return apply_filters('wpinv_first_name', $this->first_name, $this->ID, $this); |
|
| 1465 | 1465 | } |
| 1466 | 1466 | |
| 1467 | 1467 | public function get_last_name() { |
| 1468 | - return apply_filters( 'wpinv_last_name', $this->last_name, $this->ID, $this ); |
|
| 1468 | + return apply_filters('wpinv_last_name', $this->last_name, $this->ID, $this); |
|
| 1469 | 1469 | } |
| 1470 | 1470 | |
| 1471 | 1471 | public function get_user_full_name() { |
| 1472 | - return apply_filters( 'wpinv_user_full_name', $this->full_name, $this->ID, $this ); |
|
| 1472 | + return apply_filters('wpinv_user_full_name', $this->full_name, $this->ID, $this); |
|
| 1473 | 1473 | } |
| 1474 | 1474 | |
| 1475 | 1475 | public function get_user_info() { |
| 1476 | - return apply_filters( 'wpinv_user_info', $this->user_info, $this->ID, $this ); |
|
| 1476 | + return apply_filters('wpinv_user_info', $this->user_info, $this->ID, $this); |
|
| 1477 | 1477 | } |
| 1478 | 1478 | |
| 1479 | 1479 | public function get_email() { |
| 1480 | - return apply_filters( 'wpinv_user_email', $this->email, $this->ID, $this ); |
|
| 1480 | + return apply_filters('wpinv_user_email', $this->email, $this->ID, $this); |
|
| 1481 | 1481 | } |
| 1482 | 1482 | |
| 1483 | 1483 | public function get_address() { |
| 1484 | - return apply_filters( 'wpinv_address', $this->address, $this->ID, $this ); |
|
| 1484 | + return apply_filters('wpinv_address', $this->address, $this->ID, $this); |
|
| 1485 | 1485 | } |
| 1486 | 1486 | |
| 1487 | 1487 | public function get_phone() { |
| 1488 | - return apply_filters( 'wpinv_phone', $this->phone, $this->ID, $this ); |
|
| 1488 | + return apply_filters('wpinv_phone', $this->phone, $this->ID, $this); |
|
| 1489 | 1489 | } |
| 1490 | 1490 | |
| 1491 | 1491 | public function get_number() { |
| 1492 | - return apply_filters( 'wpinv_number', $this->number, $this->ID, $this ); |
|
| 1492 | + return apply_filters('wpinv_number', $this->number, $this->ID, $this); |
|
| 1493 | 1493 | } |
| 1494 | 1494 | |
| 1495 | 1495 | public function get_items() { |
| 1496 | - return apply_filters( 'wpinv_payment_meta_items', $this->items, $this->ID, $this ); |
|
| 1496 | + return apply_filters('wpinv_payment_meta_items', $this->items, $this->ID, $this); |
|
| 1497 | 1497 | } |
| 1498 | 1498 | |
| 1499 | 1499 | public function get_key() { |
| 1500 | - return apply_filters( 'wpinv_key', $this->key, $this->ID, $this ); |
|
| 1500 | + return apply_filters('wpinv_key', $this->key, $this->ID, $this); |
|
| 1501 | 1501 | } |
| 1502 | 1502 | |
| 1503 | 1503 | public function get_transaction_id() { |
| 1504 | - return apply_filters( 'wpinv_get_invoice_transaction_id', $this->transaction_id, $this->ID, $this ); |
|
| 1504 | + return apply_filters('wpinv_get_invoice_transaction_id', $this->transaction_id, $this->ID, $this); |
|
| 1505 | 1505 | } |
| 1506 | 1506 | |
| 1507 | 1507 | public function get_gateway() { |
| 1508 | - return apply_filters( 'wpinv_gateway', $this->gateway, $this->ID, $this ); |
|
| 1508 | + return apply_filters('wpinv_gateway', $this->gateway, $this->ID, $this); |
|
| 1509 | 1509 | } |
| 1510 | 1510 | |
| 1511 | 1511 | public function get_gateway_title() { |
| 1512 | - $this->gateway_title = !empty( $this->gateway_title ) ? $this->gateway_title : wpinv_get_gateway_checkout_label( $this->gateway ); |
|
| 1512 | + $this->gateway_title = !empty($this->gateway_title) ? $this->gateway_title : wpinv_get_gateway_checkout_label($this->gateway); |
|
| 1513 | 1513 | |
| 1514 | - return apply_filters( 'wpinv_gateway_title', $this->gateway_title, $this->ID, $this ); |
|
| 1514 | + return apply_filters('wpinv_gateway_title', $this->gateway_title, $this->ID, $this); |
|
| 1515 | 1515 | } |
| 1516 | 1516 | |
| 1517 | 1517 | public function get_currency() { |
| 1518 | - return apply_filters( 'wpinv_currency_code', $this->currency, $this->ID, $this ); |
|
| 1518 | + return apply_filters('wpinv_currency_code', $this->currency, $this->ID, $this); |
|
| 1519 | 1519 | } |
| 1520 | 1520 | |
| 1521 | 1521 | public function get_created_date() { |
| 1522 | - return apply_filters( 'wpinv_created_date', $this->date, $this->ID, $this ); |
|
| 1522 | + return apply_filters('wpinv_created_date', $this->date, $this->ID, $this); |
|
| 1523 | 1523 | } |
| 1524 | 1524 | |
| 1525 | - public function get_due_date( $display = false ) { |
|
| 1526 | - $due_date = apply_filters( 'wpinv_due_date', $this->due_date, $this->ID, $this ); |
|
| 1525 | + public function get_due_date($display = false) { |
|
| 1526 | + $due_date = apply_filters('wpinv_due_date', $this->due_date, $this->ID, $this); |
|
| 1527 | 1527 | |
| 1528 | - if ( !$display || empty( $due_date ) ) { |
|
| 1528 | + if (!$display || empty($due_date)) { |
|
| 1529 | 1529 | return $due_date; |
| 1530 | 1530 | } |
| 1531 | 1531 | |
| 1532 | - return date_i18n( get_option( 'date_format' ), strtotime( $due_date ) ); |
|
| 1532 | + return date_i18n(get_option('date_format'), strtotime($due_date)); |
|
| 1533 | 1533 | } |
| 1534 | 1534 | |
| 1535 | 1535 | public function get_completed_date() { |
| 1536 | - return apply_filters( 'wpinv_completed_date', $this->completed_date, $this->ID, $this ); |
|
| 1536 | + return apply_filters('wpinv_completed_date', $this->completed_date, $this->ID, $this); |
|
| 1537 | 1537 | } |
| 1538 | 1538 | |
| 1539 | - public function get_invoice_date( $formatted = true ) { |
|
| 1539 | + public function get_invoice_date($formatted = true) { |
|
| 1540 | 1540 | $date_completed = $this->completed_date; |
| 1541 | 1541 | $invoice_date = $date_completed != '' && $date_completed != '0000-00-00 00:00:00' ? $date_completed : ''; |
| 1542 | 1542 | |
| 1543 | - if ( $invoice_date == '' ) { |
|
| 1543 | + if ($invoice_date == '') { |
|
| 1544 | 1544 | $date_created = $this->date; |
| 1545 | 1545 | $invoice_date = $date_created != '' && $date_created != '0000-00-00 00:00:00' ? $date_created : ''; |
| 1546 | 1546 | } |
| 1547 | 1547 | |
| 1548 | - if ( $formatted && $invoice_date ) { |
|
| 1549 | - $invoice_date = date_i18n( get_option( 'date_format' ), strtotime( $invoice_date ) ); |
|
| 1548 | + if ($formatted && $invoice_date) { |
|
| 1549 | + $invoice_date = date_i18n(get_option('date_format'), strtotime($invoice_date)); |
|
| 1550 | 1550 | } |
| 1551 | 1551 | |
| 1552 | - return apply_filters( 'wpinv_get_invoice_date', $invoice_date, $formatted, $this->ID, $this ); |
|
| 1552 | + return apply_filters('wpinv_get_invoice_date', $invoice_date, $formatted, $this->ID, $this); |
|
| 1553 | 1553 | } |
| 1554 | 1554 | |
| 1555 | 1555 | public function get_ip() { |
| 1556 | - return apply_filters( 'wpinv_user_ip', $this->ip, $this->ID, $this ); |
|
| 1556 | + return apply_filters('wpinv_user_ip', $this->ip, $this->ID, $this); |
|
| 1557 | 1557 | } |
| 1558 | 1558 | |
| 1559 | - public function has_status( $status ) { |
|
| 1560 | - return apply_filters( 'wpinv_has_status', ( is_array( $status ) && in_array( $this->get_status(), $status ) ) || $this->get_status() === $status ? true : false, $this, $status ); |
|
| 1559 | + public function has_status($status) { |
|
| 1560 | + return apply_filters('wpinv_has_status', (is_array($status) && in_array($this->get_status(), $status)) || $this->get_status() === $status ? true : false, $this, $status); |
|
| 1561 | 1561 | } |
| 1562 | 1562 | |
| 1563 | - public function add_item( $item_id = 0, $args = array() ) { |
|
| 1563 | + public function add_item($item_id = 0, $args = array()) { |
|
| 1564 | 1564 | global $wpi_current_id, $wpi_item_id; |
| 1565 | 1565 | |
| 1566 | - $item = new WPInv_Item( $item_id ); |
|
| 1566 | + $item = new WPInv_Item($item_id); |
|
| 1567 | 1567 | |
| 1568 | 1568 | // Bail if this post isn't a item |
| 1569 | - if( !$item || $item->post_type !== 'wpi_item' ) { |
|
| 1569 | + if (!$item || $item->post_type !== 'wpi_item') { |
|
| 1570 | 1570 | return false; |
| 1571 | 1571 | } |
| 1572 | 1572 | |
@@ -1585,8 +1585,8 @@ discard block |
||
| 1585 | 1585 | 'fees' => array() |
| 1586 | 1586 | ); |
| 1587 | 1587 | |
| 1588 | - $args = wp_parse_args( apply_filters( 'wpinv_add_item_args', $args, $item->ID ), $defaults ); |
|
| 1589 | - $args['quantity'] = $has_quantities && $args['quantity'] > 0 ? absint( $args['quantity'] ) : 1; |
|
| 1588 | + $args = wp_parse_args(apply_filters('wpinv_add_item_args', $args, $item->ID), $defaults); |
|
| 1589 | + $args['quantity'] = $has_quantities && $args['quantity'] > 0 ? absint($args['quantity']) : 1; |
|
| 1590 | 1590 | |
| 1591 | 1591 | $wpi_current_id = $this->ID; |
| 1592 | 1592 | $wpi_item_id = $item->ID; |
@@ -1598,19 +1598,19 @@ discard block |
||
| 1598 | 1598 | $found_cart_key = false; |
| 1599 | 1599 | |
| 1600 | 1600 | if ($has_quantities) { |
| 1601 | - $this->cart_details = !empty( $this->cart_details ) ? array_values( $this->cart_details ) : $this->cart_details; |
|
| 1601 | + $this->cart_details = !empty($this->cart_details) ? array_values($this->cart_details) : $this->cart_details; |
|
| 1602 | 1602 | |
| 1603 | - foreach ( $this->items as $key => $cart_item ) { |
|
| 1604 | - if ( (int)$item_id !== (int)$cart_item['id'] ) { |
|
| 1603 | + foreach ($this->items as $key => $cart_item) { |
|
| 1604 | + if ((int)$item_id !== (int)$cart_item['id']) { |
|
| 1605 | 1605 | continue; |
| 1606 | 1606 | } |
| 1607 | 1607 | |
| 1608 | - $this->items[ $key ]['quantity'] += $args['quantity']; |
|
| 1608 | + $this->items[$key]['quantity'] += $args['quantity']; |
|
| 1609 | 1609 | break; |
| 1610 | 1610 | } |
| 1611 | 1611 | |
| 1612 | - foreach ( $this->cart_details as $cart_key => $cart_item ) { |
|
| 1613 | - if ( $item_id != $cart_item['id'] ) { |
|
| 1612 | + foreach ($this->cart_details as $cart_key => $cart_item) { |
|
| 1613 | + if ($item_id != $cart_item['id']) { |
|
| 1614 | 1614 | continue; |
| 1615 | 1615 | } |
| 1616 | 1616 | |
@@ -1622,29 +1622,29 @@ discard block |
||
| 1622 | 1622 | if ($has_quantities && $found_cart_key !== false) { |
| 1623 | 1623 | $cart_item = $this->cart_details[$found_cart_key]; |
| 1624 | 1624 | $item_price = $cart_item['item_price']; |
| 1625 | - $quantity = !empty( $cart_item['quantity'] ) ? $cart_item['quantity'] : 1; |
|
| 1626 | - $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : 0; |
|
| 1625 | + $quantity = !empty($cart_item['quantity']) ? $cart_item['quantity'] : 1; |
|
| 1626 | + $tax_rate = !empty($cart_item['vat_rate']) ? $cart_item['vat_rate'] : 0; |
|
| 1627 | 1627 | |
| 1628 | 1628 | $new_quantity = $quantity + $args['quantity']; |
| 1629 | 1629 | $subtotal = $item_price * $new_quantity; |
| 1630 | 1630 | |
| 1631 | 1631 | $args['quantity'] = $new_quantity; |
| 1632 | - $discount = !empty( $args['discount'] ) ? $args['discount'] : 0; |
|
| 1633 | - $tax = $subtotal > 0 && $tax_rate > 0 ? ( ( $subtotal - $discount ) * 0.01 * (float)$tax_rate ) : 0; |
|
| 1632 | + $discount = !empty($args['discount']) ? $args['discount'] : 0; |
|
| 1633 | + $tax = $subtotal > 0 && $tax_rate > 0 ? (($subtotal - $discount) * 0.01 * (float)$tax_rate) : 0; |
|
| 1634 | 1634 | |
| 1635 | 1635 | $discount_increased = $discount > 0 && $subtotal > 0 && $discount > (float)$cart_item['discount'] ? $discount - (float)$cart_item['discount'] : 0; |
| 1636 | 1636 | $tax_increased = $tax > 0 && $subtotal > 0 && $tax > (float)$cart_item['tax'] ? $tax - (float)$cart_item['tax'] : 0; |
| 1637 | 1637 | // The total increase equals the number removed * the item_price |
| 1638 | - $total_increased = wpinv_round_amount( $item_price ); |
|
| 1638 | + $total_increased = wpinv_round_amount($item_price); |
|
| 1639 | 1639 | |
| 1640 | - if ( wpinv_prices_include_tax() ) { |
|
| 1641 | - $subtotal -= wpinv_round_amount( $tax ); |
|
| 1640 | + if (wpinv_prices_include_tax()) { |
|
| 1641 | + $subtotal -= wpinv_round_amount($tax); |
|
| 1642 | 1642 | } |
| 1643 | 1643 | |
| 1644 | - $total = $subtotal - $discount + $tax; |
|
| 1644 | + $total = $subtotal - $discount + $tax; |
|
| 1645 | 1645 | |
| 1646 | 1646 | // Do not allow totals to go negative |
| 1647 | - if( $total < 0 ) { |
|
| 1647 | + if ($total < 0) { |
|
| 1648 | 1648 | $total = 0; |
| 1649 | 1649 | } |
| 1650 | 1650 | |
@@ -1660,25 +1660,25 @@ discard block |
||
| 1660 | 1660 | $this->cart_details[$found_cart_key] = $cart_item; |
| 1661 | 1661 | } else { |
| 1662 | 1662 | // Set custom price. |
| 1663 | - if ( $args['custom_price'] !== '' ) { |
|
| 1663 | + if ($args['custom_price'] !== '') { |
|
| 1664 | 1664 | $item_price = $args['custom_price']; |
| 1665 | 1665 | } else { |
| 1666 | 1666 | // Allow overriding the price |
| 1667 | - if ( false !== $args['item_price'] ) { |
|
| 1667 | + if (false !== $args['item_price']) { |
|
| 1668 | 1668 | $item_price = $args['item_price']; |
| 1669 | 1669 | } else { |
| 1670 | - $item_price = wpinv_get_item_price( $item->ID ); |
|
| 1670 | + $item_price = wpinv_get_item_price($item->ID); |
|
| 1671 | 1671 | } |
| 1672 | 1672 | } |
| 1673 | 1673 | |
| 1674 | 1674 | // Sanitizing the price here so we don't have a dozen calls later |
| 1675 | - $item_price = wpinv_sanitize_amount( $item_price ); |
|
| 1676 | - $subtotal = wpinv_round_amount( $item_price * $args['quantity'] ); |
|
| 1675 | + $item_price = wpinv_sanitize_amount($item_price); |
|
| 1676 | + $subtotal = wpinv_round_amount($item_price * $args['quantity']); |
|
| 1677 | 1677 | |
| 1678 | - $discount = !empty( $args['discount'] ) ? $args['discount'] : 0; |
|
| 1679 | - $tax_class = !empty( $args['vat_class'] ) ? $args['vat_class'] : ''; |
|
| 1680 | - $tax_rate = !empty( $args['vat_rate'] ) ? $args['vat_rate'] : 0; |
|
| 1681 | - $tax = $subtotal > 0 && $tax_rate > 0 ? ( ( $subtotal - $discount ) * 0.01 * (float)$tax_rate ) : 0; |
|
| 1678 | + $discount = !empty($args['discount']) ? $args['discount'] : 0; |
|
| 1679 | + $tax_class = !empty($args['vat_class']) ? $args['vat_class'] : ''; |
|
| 1680 | + $tax_rate = !empty($args['vat_rate']) ? $args['vat_rate'] : 0; |
|
| 1681 | + $tax = $subtotal > 0 && $tax_rate > 0 ? (($subtotal - $discount) * 0.01 * (float)$tax_rate) : 0; |
|
| 1682 | 1682 | |
| 1683 | 1683 | // Setup the items meta item |
| 1684 | 1684 | $new_item = array( |
@@ -1686,29 +1686,29 @@ discard block |
||
| 1686 | 1686 | 'quantity' => $args['quantity'], |
| 1687 | 1687 | ); |
| 1688 | 1688 | |
| 1689 | - $this->items[] = $new_item; |
|
| 1689 | + $this->items[] = $new_item; |
|
| 1690 | 1690 | |
| 1691 | - if ( wpinv_prices_include_tax() ) { |
|
| 1692 | - $subtotal -= wpinv_round_amount( $tax ); |
|
| 1691 | + if (wpinv_prices_include_tax()) { |
|
| 1692 | + $subtotal -= wpinv_round_amount($tax); |
|
| 1693 | 1693 | } |
| 1694 | 1694 | |
| 1695 | - $total = $subtotal - $discount + $tax; |
|
| 1695 | + $total = $subtotal - $discount + $tax; |
|
| 1696 | 1696 | |
| 1697 | 1697 | // Do not allow totals to go negative |
| 1698 | - if( $total < 0 ) { |
|
| 1698 | + if ($total < 0) { |
|
| 1699 | 1699 | $total = 0; |
| 1700 | 1700 | } |
| 1701 | 1701 | |
| 1702 | 1702 | $this->cart_details[] = array( |
| 1703 | 1703 | 'name' => !empty($args['name']) ? $args['name'] : $item->get_name(), |
| 1704 | 1704 | 'id' => $item->ID, |
| 1705 | - 'item_price' => wpinv_round_amount( $item_price ), |
|
| 1706 | - 'custom_price' => ( $args['custom_price'] !== '' ? wpinv_round_amount( $args['custom_price'] ) : '' ), |
|
| 1705 | + 'item_price' => wpinv_round_amount($item_price), |
|
| 1706 | + 'custom_price' => ($args['custom_price'] !== '' ? wpinv_round_amount($args['custom_price']) : ''), |
|
| 1707 | 1707 | 'quantity' => $args['quantity'], |
| 1708 | 1708 | 'discount' => $discount, |
| 1709 | - 'subtotal' => wpinv_round_amount( $subtotal ), |
|
| 1710 | - 'tax' => wpinv_round_amount( $tax ), |
|
| 1711 | - 'price' => wpinv_round_amount( $total ), |
|
| 1709 | + 'subtotal' => wpinv_round_amount($subtotal), |
|
| 1710 | + 'tax' => wpinv_round_amount($tax), |
|
| 1711 | + 'price' => wpinv_round_amount($total), |
|
| 1712 | 1712 | 'vat_rate' => $tax_rate, |
| 1713 | 1713 | 'vat_class' => $tax_class, |
| 1714 | 1714 | 'meta' => $args['meta'], |
@@ -1718,18 +1718,18 @@ discard block |
||
| 1718 | 1718 | $subtotal = $subtotal - $discount; |
| 1719 | 1719 | } |
| 1720 | 1720 | |
| 1721 | - $added_item = end( $this->cart_details ); |
|
| 1722 | - $added_item['action'] = 'add'; |
|
| 1721 | + $added_item = end($this->cart_details); |
|
| 1722 | + $added_item['action'] = 'add'; |
|
| 1723 | 1723 | |
| 1724 | 1724 | $this->pending['items'][] = $added_item; |
| 1725 | 1725 | |
| 1726 | - $this->increase_subtotal( $subtotal ); |
|
| 1727 | - $this->increase_tax( $tax ); |
|
| 1726 | + $this->increase_subtotal($subtotal); |
|
| 1727 | + $this->increase_tax($tax); |
|
| 1728 | 1728 | |
| 1729 | 1729 | return true; |
| 1730 | 1730 | } |
| 1731 | 1731 | |
| 1732 | - public function remove_item( $item_id, $args = array() ) { |
|
| 1732 | + public function remove_item($item_id, $args = array()) { |
|
| 1733 | 1733 | // Set some defaults |
| 1734 | 1734 | $defaults = array( |
| 1735 | 1735 | 'quantity' => 1, |
@@ -1737,51 +1737,51 @@ discard block |
||
| 1737 | 1737 | 'custom_price' => '', |
| 1738 | 1738 | 'cart_index' => false, |
| 1739 | 1739 | ); |
| 1740 | - $args = wp_parse_args( $args, $defaults ); |
|
| 1740 | + $args = wp_parse_args($args, $defaults); |
|
| 1741 | 1741 | |
| 1742 | 1742 | // Bail if this post isn't a item |
| 1743 | - if ( get_post_type( $item_id ) !== 'wpi_item' ) { |
|
| 1743 | + if (get_post_type($item_id) !== 'wpi_item') { |
|
| 1744 | 1744 | return false; |
| 1745 | 1745 | } |
| 1746 | 1746 | |
| 1747 | - $this->cart_details = !empty( $this->cart_details ) ? array_values( $this->cart_details ) : $this->cart_details; |
|
| 1747 | + $this->cart_details = !empty($this->cart_details) ? array_values($this->cart_details) : $this->cart_details; |
|
| 1748 | 1748 | |
| 1749 | - foreach ( $this->items as $key => $item ) { |
|
| 1750 | - if ( !empty($item['id']) && (int)$item_id !== (int)$item['id'] ) { |
|
| 1749 | + foreach ($this->items as $key => $item) { |
|
| 1750 | + if (!empty($item['id']) && (int)$item_id !== (int)$item['id']) { |
|
| 1751 | 1751 | continue; |
| 1752 | 1752 | } |
| 1753 | 1753 | |
| 1754 | - if ( false !== $args['cart_index'] ) { |
|
| 1755 | - $cart_index = absint( $args['cart_index'] ); |
|
| 1756 | - $cart_item = ! empty( $this->cart_details[ $cart_index ] ) ? $this->cart_details[ $cart_index ] : false; |
|
| 1754 | + if (false !== $args['cart_index']) { |
|
| 1755 | + $cart_index = absint($args['cart_index']); |
|
| 1756 | + $cart_item = !empty($this->cart_details[$cart_index]) ? $this->cart_details[$cart_index] : false; |
|
| 1757 | 1757 | |
| 1758 | - if ( ! empty( $cart_item ) ) { |
|
| 1758 | + if (!empty($cart_item)) { |
|
| 1759 | 1759 | // If the cart index item isn't the same item ID, don't remove it |
| 1760 | - if ( !empty($cart_item['id']) && $cart_item['id'] != $item['id'] ) { |
|
| 1760 | + if (!empty($cart_item['id']) && $cart_item['id'] != $item['id']) { |
|
| 1761 | 1761 | continue; |
| 1762 | 1762 | } |
| 1763 | 1763 | } |
| 1764 | 1764 | } |
| 1765 | 1765 | |
| 1766 | - $item_quantity = $this->items[ $key ]['quantity']; |
|
| 1767 | - if ( $item_quantity > $args['quantity'] ) { |
|
| 1768 | - $this->items[ $key ]['quantity'] -= $args['quantity']; |
|
| 1766 | + $item_quantity = $this->items[$key]['quantity']; |
|
| 1767 | + if ($item_quantity > $args['quantity']) { |
|
| 1768 | + $this->items[$key]['quantity'] -= $args['quantity']; |
|
| 1769 | 1769 | break; |
| 1770 | 1770 | } else { |
| 1771 | - unset( $this->items[ $key ] ); |
|
| 1771 | + unset($this->items[$key]); |
|
| 1772 | 1772 | break; |
| 1773 | 1773 | } |
| 1774 | 1774 | } |
| 1775 | 1775 | |
| 1776 | 1776 | $found_cart_key = false; |
| 1777 | - if ( false === $args['cart_index'] ) { |
|
| 1778 | - foreach ( $this->cart_details as $cart_key => $item ) { |
|
| 1779 | - if ( $item_id != $item['id'] ) { |
|
| 1777 | + if (false === $args['cart_index']) { |
|
| 1778 | + foreach ($this->cart_details as $cart_key => $item) { |
|
| 1779 | + if ($item_id != $item['id']) { |
|
| 1780 | 1780 | continue; |
| 1781 | 1781 | } |
| 1782 | 1782 | |
| 1783 | - if ( false !== $args['item_price'] ) { |
|
| 1784 | - if ( isset( $item['item_price'] ) && (float) $args['item_price'] != (float) $item['item_price'] ) { |
|
| 1783 | + if (false !== $args['item_price']) { |
|
| 1784 | + if (isset($item['item_price']) && (float)$args['item_price'] != (float)$item['item_price']) { |
|
| 1785 | 1785 | continue; |
| 1786 | 1786 | } |
| 1787 | 1787 | } |
@@ -1790,13 +1790,13 @@ discard block |
||
| 1790 | 1790 | break; |
| 1791 | 1791 | } |
| 1792 | 1792 | } else { |
| 1793 | - $cart_index = absint( $args['cart_index'] ); |
|
| 1793 | + $cart_index = absint($args['cart_index']); |
|
| 1794 | 1794 | |
| 1795 | - if ( ! array_key_exists( $cart_index, $this->cart_details ) ) { |
|
| 1795 | + if (!array_key_exists($cart_index, $this->cart_details)) { |
|
| 1796 | 1796 | return false; // Invalid cart index passed. |
| 1797 | 1797 | } |
| 1798 | 1798 | |
| 1799 | - if ( (int) $this->cart_details[ $cart_index ]['id'] > 0 && (int) $this->cart_details[ $cart_index ]['id'] !== (int) $item_id ) { |
|
| 1799 | + if ((int)$this->cart_details[$cart_index]['id'] > 0 && (int)$this->cart_details[$cart_index]['id'] !== (int)$item_id) { |
|
| 1800 | 1800 | return false; // We still need the proper Item ID to be sure. |
| 1801 | 1801 | } |
| 1802 | 1802 | |
@@ -1804,41 +1804,41 @@ discard block |
||
| 1804 | 1804 | } |
| 1805 | 1805 | |
| 1806 | 1806 | $cart_item = $this->cart_details[$found_cart_key]; |
| 1807 | - $quantity = !empty( $cart_item['quantity'] ) ? $cart_item['quantity'] : 1; |
|
| 1807 | + $quantity = !empty($cart_item['quantity']) ? $cart_item['quantity'] : 1; |
|
| 1808 | 1808 | |
| 1809 | - if ( count( $this->cart_details ) == 1 && ( $quantity - $args['quantity'] ) < 1 ) { |
|
| 1809 | + if (count($this->cart_details) == 1 && ($quantity - $args['quantity']) < 1) { |
|
| 1810 | 1810 | return false; // Invoice must contain at least one item. |
| 1811 | 1811 | } |
| 1812 | 1812 | |
| 1813 | - $discounts = $this->get_discounts(); |
|
| 1813 | + $discounts = $this->get_discounts(); |
|
| 1814 | 1814 | |
| 1815 | - if ( $quantity > $args['quantity'] ) { |
|
| 1815 | + if ($quantity > $args['quantity']) { |
|
| 1816 | 1816 | $item_price = $cart_item['item_price']; |
| 1817 | - $tax_rate = !empty( $cart_item['vat_rate'] ) ? $cart_item['vat_rate'] : 0; |
|
| 1817 | + $tax_rate = !empty($cart_item['vat_rate']) ? $cart_item['vat_rate'] : 0; |
|
| 1818 | 1818 | |
| 1819 | - $new_quantity = max( $quantity - $args['quantity'], 1); |
|
| 1819 | + $new_quantity = max($quantity - $args['quantity'], 1); |
|
| 1820 | 1820 | $subtotal = $item_price * $new_quantity; |
| 1821 | 1821 | |
| 1822 | 1822 | $args['quantity'] = $new_quantity; |
| 1823 | - $discount = !empty( $cart_item['discount'] ) ? $cart_item['discount'] : 0; |
|
| 1824 | - $tax = $subtotal > 0 && $tax_rate > 0 ? ( ( $subtotal - $discount ) * 0.01 * (float)$tax_rate ) : 0; |
|
| 1823 | + $discount = !empty($cart_item['discount']) ? $cart_item['discount'] : 0; |
|
| 1824 | + $tax = $subtotal > 0 && $tax_rate > 0 ? (($subtotal - $discount) * 0.01 * (float)$tax_rate) : 0; |
|
| 1825 | 1825 | |
| 1826 | - $discount_decrease = (float)$cart_item['discount'] > 0 && $quantity > 0 ? wpinv_round_amount( ( (float)$cart_item['discount'] / $quantity ) ) : 0; |
|
| 1826 | + $discount_decrease = (float)$cart_item['discount'] > 0 && $quantity > 0 ? wpinv_round_amount(((float)$cart_item['discount'] / $quantity)) : 0; |
|
| 1827 | 1827 | $discount_decrease = $discount > 0 && $subtotal > 0 && (float)$cart_item['discount'] > $discount ? (float)$cart_item['discount'] - $discount : $discount_decrease; |
| 1828 | - $tax_decrease = (float)$cart_item['tax'] > 0 && $quantity > 0 ? wpinv_round_amount( ( (float)$cart_item['tax'] / $quantity ) ) : 0; |
|
| 1828 | + $tax_decrease = (float)$cart_item['tax'] > 0 && $quantity > 0 ? wpinv_round_amount(((float)$cart_item['tax'] / $quantity)) : 0; |
|
| 1829 | 1829 | $tax_decrease = $tax > 0 && $subtotal > 0 && (float)$cart_item['tax'] > $tax ? (float)$cart_item['tax'] - $tax : $tax_decrease; |
| 1830 | 1830 | |
| 1831 | 1831 | // The total increase equals the number removed * the item_price |
| 1832 | - $total_decrease = wpinv_round_amount( $item_price ); |
|
| 1832 | + $total_decrease = wpinv_round_amount($item_price); |
|
| 1833 | 1833 | |
| 1834 | - if ( wpinv_prices_include_tax() ) { |
|
| 1835 | - $subtotal -= wpinv_round_amount( $tax ); |
|
| 1834 | + if (wpinv_prices_include_tax()) { |
|
| 1835 | + $subtotal -= wpinv_round_amount($tax); |
|
| 1836 | 1836 | } |
| 1837 | 1837 | |
| 1838 | - $total = $subtotal - $discount + $tax; |
|
| 1838 | + $total = $subtotal - $discount + $tax; |
|
| 1839 | 1839 | |
| 1840 | 1840 | // Do not allow totals to go negative |
| 1841 | - if( $total < 0 ) { |
|
| 1841 | + if ($total < 0) { |
|
| 1842 | 1842 | $total = 0; |
| 1843 | 1843 | } |
| 1844 | 1844 | |
@@ -1857,16 +1857,16 @@ discard block |
||
| 1857 | 1857 | |
| 1858 | 1858 | $this->cart_details[$found_cart_key] = $cart_item; |
| 1859 | 1859 | |
| 1860 | - $remove_item = end( $this->cart_details ); |
|
| 1860 | + $remove_item = end($this->cart_details); |
|
| 1861 | 1861 | } else { |
| 1862 | 1862 | $item_price = $cart_item['item_price']; |
| 1863 | - $discount = !empty( $cart_item['discount'] ) ? $cart_item['discount'] : 0; |
|
| 1864 | - $tax = !empty( $cart_item['tax'] ) ? $cart_item['tax'] : 0; |
|
| 1863 | + $discount = !empty($cart_item['discount']) ? $cart_item['discount'] : 0; |
|
| 1864 | + $tax = !empty($cart_item['tax']) ? $cart_item['tax'] : 0; |
|
| 1865 | 1865 | |
| 1866 | - $subtotal_decrease = ( $item_price * $quantity ) - $discount; |
|
| 1866 | + $subtotal_decrease = ($item_price * $quantity) - $discount; |
|
| 1867 | 1867 | $tax_decrease = $tax; |
| 1868 | 1868 | |
| 1869 | - unset( $this->cart_details[$found_cart_key] ); |
|
| 1869 | + unset($this->cart_details[$found_cart_key]); |
|
| 1870 | 1870 | |
| 1871 | 1871 | $remove_item = $args; |
| 1872 | 1872 | $remove_item['id'] = $item_id; |
@@ -1877,8 +1877,8 @@ discard block |
||
| 1877 | 1877 | $remove_item['action'] = 'remove'; |
| 1878 | 1878 | $this->pending['items'][] = $remove_item; |
| 1879 | 1879 | |
| 1880 | - $this->decrease_subtotal( $subtotal_decrease ); |
|
| 1881 | - $this->decrease_tax( $tax_decrease ); |
|
| 1880 | + $this->decrease_subtotal($subtotal_decrease); |
|
| 1881 | + $this->decrease_tax($tax_decrease); |
|
| 1882 | 1882 | |
| 1883 | 1883 | return true; |
| 1884 | 1884 | } |
@@ -1886,7 +1886,7 @@ discard block |
||
| 1886 | 1886 | public function update_items($temp = false) { |
| 1887 | 1887 | global $wpinv_euvat, $wpi_current_id, $wpi_item_id, $wpi_nosave; |
| 1888 | 1888 | |
| 1889 | - if ( !empty( $this->cart_details ) ) { |
|
| 1889 | + if (!empty($this->cart_details)) { |
|
| 1890 | 1890 | $wpi_nosave = $temp; |
| 1891 | 1891 | $cart_subtotal = 0; |
| 1892 | 1892 | $cart_discount = 0; |
@@ -1896,42 +1896,42 @@ discard block |
||
| 1896 | 1896 | $_POST['wpinv_country'] = $this->country; |
| 1897 | 1897 | $_POST['wpinv_state'] = $this->state; |
| 1898 | 1898 | |
| 1899 | - foreach ( $this->cart_details as $key => $item ) { |
|
| 1899 | + foreach ($this->cart_details as $key => $item) { |
|
| 1900 | 1900 | $item_price = $item['item_price']; |
| 1901 | - $quantity = wpinv_item_quantities_enabled() && $item['quantity'] > 0 ? absint( $item['quantity'] ) : 1; |
|
| 1902 | - $amount = wpinv_round_amount( $item_price * $quantity ); |
|
| 1901 | + $quantity = wpinv_item_quantities_enabled() && $item['quantity'] > 0 ? absint($item['quantity']) : 1; |
|
| 1902 | + $amount = wpinv_round_amount($item_price * $quantity); |
|
| 1903 | 1903 | $subtotal = $item_price * $quantity; |
| 1904 | 1904 | |
| 1905 | 1905 | $wpi_current_id = $this->ID; |
| 1906 | 1906 | $wpi_item_id = $item['id']; |
| 1907 | 1907 | |
| 1908 | - $discount = wpinv_get_cart_item_discount_amount( $item, $this->get_discounts() ); |
|
| 1908 | + $discount = wpinv_get_cart_item_discount_amount($item, $this->get_discounts()); |
|
| 1909 | 1909 | |
| 1910 | - $tax_rate = wpinv_get_tax_rate( $this->country, $this->state, $wpi_item_id ); |
|
| 1911 | - $tax_class = $wpinv_euvat->get_item_class( $wpi_item_id ); |
|
| 1912 | - $tax = $item_price > 0 ? ( ( $subtotal - $discount ) * 0.01 * (float)$tax_rate ) : 0; |
|
| 1910 | + $tax_rate = wpinv_get_tax_rate($this->country, $this->state, $wpi_item_id); |
|
| 1911 | + $tax_class = $wpinv_euvat->get_item_class($wpi_item_id); |
|
| 1912 | + $tax = $item_price > 0 ? (($subtotal - $discount) * 0.01 * (float)$tax_rate) : 0; |
|
| 1913 | 1913 | |
| 1914 | - if ( wpinv_prices_include_tax() ) { |
|
| 1915 | - $subtotal -= wpinv_round_amount( $tax ); |
|
| 1914 | + if (wpinv_prices_include_tax()) { |
|
| 1915 | + $subtotal -= wpinv_round_amount($tax); |
|
| 1916 | 1916 | } |
| 1917 | 1917 | |
| 1918 | - $total = $subtotal - $discount + $tax; |
|
| 1918 | + $total = $subtotal - $discount + $tax; |
|
| 1919 | 1919 | |
| 1920 | 1920 | // Do not allow totals to go negative |
| 1921 | - if( $total < 0 ) { |
|
| 1921 | + if ($total < 0) { |
|
| 1922 | 1922 | $total = 0; |
| 1923 | 1923 | } |
| 1924 | 1924 | |
| 1925 | 1925 | $cart_details[] = array( |
| 1926 | 1926 | 'id' => $item['id'], |
| 1927 | 1927 | 'name' => $item['name'], |
| 1928 | - 'item_price' => wpinv_round_amount( $item_price ), |
|
| 1929 | - 'custom_price'=> ( isset( $item['custom_price'] ) ? $item['custom_price'] : '' ), |
|
| 1928 | + 'item_price' => wpinv_round_amount($item_price), |
|
| 1929 | + 'custom_price'=> (isset($item['custom_price']) ? $item['custom_price'] : ''), |
|
| 1930 | 1930 | 'quantity' => $quantity, |
| 1931 | 1931 | 'discount' => $discount, |
| 1932 | - 'subtotal' => wpinv_round_amount( $subtotal ), |
|
| 1933 | - 'tax' => wpinv_round_amount( $tax ), |
|
| 1934 | - 'price' => wpinv_round_amount( $total ), |
|
| 1932 | + 'subtotal' => wpinv_round_amount($subtotal), |
|
| 1933 | + 'tax' => wpinv_round_amount($tax), |
|
| 1934 | + 'price' => wpinv_round_amount($total), |
|
| 1935 | 1935 | 'vat_rate' => $tax_rate, |
| 1936 | 1936 | 'vat_class' => $tax_class, |
| 1937 | 1937 | 'meta' => isset($item['meta']) ? $item['meta'] : array(), |
@@ -1942,9 +1942,9 @@ discard block |
||
| 1942 | 1942 | $cart_discount += (float)($discount); |
| 1943 | 1943 | $cart_tax += (float)($tax); |
| 1944 | 1944 | } |
| 1945 | - $this->subtotal = wpinv_round_amount( $cart_subtotal ); |
|
| 1946 | - $this->tax = wpinv_round_amount( $cart_tax ); |
|
| 1947 | - $this->discount = wpinv_round_amount( $cart_discount ); |
|
| 1945 | + $this->subtotal = wpinv_round_amount($cart_subtotal); |
|
| 1946 | + $this->tax = wpinv_round_amount($cart_tax); |
|
| 1947 | + $this->discount = wpinv_round_amount($cart_discount); |
|
| 1948 | 1948 | |
| 1949 | 1949 | $this->recalculate_total(); |
| 1950 | 1950 | |
@@ -1956,221 +1956,221 @@ discard block |
||
| 1956 | 1956 | |
| 1957 | 1957 | public function recalculate_totals($temp = false) { |
| 1958 | 1958 | $this->update_items($temp); |
| 1959 | - $this->save( true ); |
|
| 1959 | + $this->save(true); |
|
| 1960 | 1960 | |
| 1961 | 1961 | return $this; |
| 1962 | 1962 | } |
| 1963 | 1963 | |
| 1964 | 1964 | public function needs_payment() { |
| 1965 | - $valid_invoice_statuses = apply_filters( 'wpinv_valid_invoice_statuses_for_payment', array( 'pending' ), $this ); |
|
| 1965 | + $valid_invoice_statuses = apply_filters('wpinv_valid_invoice_statuses_for_payment', array('pending'), $this); |
|
| 1966 | 1966 | |
| 1967 | - if ( $this->has_status( $valid_invoice_statuses ) && ( $this->get_total() > 0 || $this->is_free_trial() || $this->is_free() ) ) { |
|
| 1967 | + if ($this->has_status($valid_invoice_statuses) && ($this->get_total() > 0 || $this->is_free_trial() || $this->is_free())) { |
|
| 1968 | 1968 | $needs_payment = true; |
| 1969 | 1969 | } else { |
| 1970 | 1970 | $needs_payment = false; |
| 1971 | 1971 | } |
| 1972 | 1972 | |
| 1973 | - return apply_filters( 'wpinv_needs_payment', $needs_payment, $this, $valid_invoice_statuses ); |
|
| 1973 | + return apply_filters('wpinv_needs_payment', $needs_payment, $this, $valid_invoice_statuses); |
|
| 1974 | 1974 | } |
| 1975 | 1975 | |
| 1976 | - public function get_checkout_payment_url( $on_checkout = false, $secret = false ) { |
|
| 1976 | + public function get_checkout_payment_url($on_checkout = false, $secret = false) { |
|
| 1977 | 1977 | $pay_url = wpinv_get_checkout_uri(); |
| 1978 | 1978 | |
| 1979 | - if ( is_ssl() ) { |
|
| 1980 | - $pay_url = str_replace( 'http:', 'https:', $pay_url ); |
|
| 1979 | + if (is_ssl()) { |
|
| 1980 | + $pay_url = str_replace('http:', 'https:', $pay_url); |
|
| 1981 | 1981 | } |
| 1982 | 1982 | |
| 1983 | 1983 | $key = $this->get_key(); |
| 1984 | 1984 | |
| 1985 | - if ( $on_checkout ) { |
|
| 1986 | - $pay_url = add_query_arg( 'invoice_key', $key, $pay_url ); |
|
| 1985 | + if ($on_checkout) { |
|
| 1986 | + $pay_url = add_query_arg('invoice_key', $key, $pay_url); |
|
| 1987 | 1987 | } else { |
| 1988 | - $pay_url = add_query_arg( array( 'wpi_action' => 'pay_for_invoice', 'invoice_key' => $key ), $pay_url ); |
|
| 1988 | + $pay_url = add_query_arg(array('wpi_action' => 'pay_for_invoice', 'invoice_key' => $key), $pay_url); |
|
| 1989 | 1989 | } |
| 1990 | 1990 | |
| 1991 | - if ( $secret ) { |
|
| 1992 | - $pay_url = add_query_arg( array( '_wpipay' => md5( $this->get_user_id() . '::' . $this->get_email() . '::' . $key ) ), $pay_url ); |
|
| 1991 | + if ($secret) { |
|
| 1992 | + $pay_url = add_query_arg(array('_wpipay' => md5($this->get_user_id() . '::' . $this->get_email() . '::' . $key)), $pay_url); |
|
| 1993 | 1993 | } |
| 1994 | 1994 | |
| 1995 | - return apply_filters( 'wpinv_get_checkout_payment_url', $pay_url, $this ); |
|
| 1995 | + return apply_filters('wpinv_get_checkout_payment_url', $pay_url, $this); |
|
| 1996 | 1996 | } |
| 1997 | 1997 | |
| 1998 | - public function get_view_url( $secret = false ) { |
|
| 1999 | - $print_url = get_permalink( $this->ID ); |
|
| 1998 | + public function get_view_url($secret = false) { |
|
| 1999 | + $print_url = get_permalink($this->ID); |
|
| 2000 | 2000 | |
| 2001 | - if ( $secret ) { |
|
| 2002 | - $print_url = add_query_arg( array( '_wpipay' => md5( $this->get_user_id() . '::' . $this->get_email() . '::' . $this->get_key() ) ), $print_url ); |
|
| 2001 | + if ($secret) { |
|
| 2002 | + $print_url = add_query_arg(array('_wpipay' => md5($this->get_user_id() . '::' . $this->get_email() . '::' . $this->get_key())), $print_url); |
|
| 2003 | 2003 | } |
| 2004 | 2004 | |
| 2005 | - return apply_filters( 'wpinv_get_view_url', $print_url, $this ); |
|
| 2005 | + return apply_filters('wpinv_get_view_url', $print_url, $this); |
|
| 2006 | 2006 | } |
| 2007 | 2007 | |
| 2008 | - public function generate_key( $string = '' ) { |
|
| 2009 | - $auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : ''; |
|
| 2010 | - return strtolower( md5( $string . date( 'Y-m-d H:i:s' ) . $auth_key . uniqid( 'wpinv', true ) ) ); // Unique key |
|
| 2008 | + public function generate_key($string = '') { |
|
| 2009 | + $auth_key = defined('AUTH_KEY') ? AUTH_KEY : ''; |
|
| 2010 | + return strtolower(md5($string . date('Y-m-d H:i:s') . $auth_key . uniqid('wpinv', true))); // Unique key |
|
| 2011 | 2011 | } |
| 2012 | 2012 | |
| 2013 | 2013 | public function is_recurring() { |
| 2014 | - if ( empty( $this->cart_details ) ) { |
|
| 2014 | + if (empty($this->cart_details)) { |
|
| 2015 | 2015 | return false; |
| 2016 | 2016 | } |
| 2017 | 2017 | |
| 2018 | 2018 | $has_subscription = false; |
| 2019 | - foreach( $this->cart_details as $cart_item ) { |
|
| 2020 | - if ( !empty( $cart_item['id'] ) && wpinv_is_recurring_item( $cart_item['id'] ) ) { |
|
| 2019 | + foreach ($this->cart_details as $cart_item) { |
|
| 2020 | + if (!empty($cart_item['id']) && wpinv_is_recurring_item($cart_item['id'])) { |
|
| 2021 | 2021 | $has_subscription = true; |
| 2022 | 2022 | break; |
| 2023 | 2023 | } |
| 2024 | 2024 | } |
| 2025 | 2025 | |
| 2026 | - if ( count( $this->cart_details ) > 1 ) { |
|
| 2026 | + if (count($this->cart_details) > 1) { |
|
| 2027 | 2027 | $has_subscription = false; |
| 2028 | 2028 | } |
| 2029 | 2029 | |
| 2030 | - return apply_filters( 'wpinv_invoice_has_recurring_item', $has_subscription, $this->cart_details ); |
|
| 2030 | + return apply_filters('wpinv_invoice_has_recurring_item', $has_subscription, $this->cart_details); |
|
| 2031 | 2031 | } |
| 2032 | 2032 | |
| 2033 | 2033 | public function is_free_trial() { |
| 2034 | 2034 | $is_free_trial = false; |
| 2035 | 2035 | |
| 2036 | - if ( $this->is_parent() && $item = $this->get_recurring( true ) ) { |
|
| 2037 | - if ( !empty( $item ) && $item->has_free_trial() ) { |
|
| 2036 | + if ($this->is_parent() && $item = $this->get_recurring(true)) { |
|
| 2037 | + if (!empty($item) && $item->has_free_trial()) { |
|
| 2038 | 2038 | $is_free_trial = true; |
| 2039 | 2039 | } |
| 2040 | 2040 | } |
| 2041 | 2041 | |
| 2042 | - return apply_filters( 'wpinv_invoice_is_free_trial', $is_free_trial, $this->cart_details ); |
|
| 2042 | + return apply_filters('wpinv_invoice_is_free_trial', $is_free_trial, $this->cart_details); |
|
| 2043 | 2043 | } |
| 2044 | 2044 | |
| 2045 | - public function get_recurring( $object = false ) { |
|
| 2045 | + public function get_recurring($object = false) { |
|
| 2046 | 2046 | $item = NULL; |
| 2047 | 2047 | |
| 2048 | - if ( empty( $this->cart_details ) ) { |
|
| 2048 | + if (empty($this->cart_details)) { |
|
| 2049 | 2049 | return $item; |
| 2050 | 2050 | } |
| 2051 | 2051 | |
| 2052 | - foreach( $this->cart_details as $cart_item ) { |
|
| 2053 | - if ( !empty( $cart_item['id'] ) && wpinv_is_recurring_item( $cart_item['id'] ) ) { |
|
| 2052 | + foreach ($this->cart_details as $cart_item) { |
|
| 2053 | + if (!empty($cart_item['id']) && wpinv_is_recurring_item($cart_item['id'])) { |
|
| 2054 | 2054 | $item = $cart_item['id']; |
| 2055 | 2055 | break; |
| 2056 | 2056 | } |
| 2057 | 2057 | } |
| 2058 | 2058 | |
| 2059 | - if ( $object ) { |
|
| 2060 | - $item = $item ? new WPInv_Item( $item ) : NULL; |
|
| 2059 | + if ($object) { |
|
| 2060 | + $item = $item ? new WPInv_Item($item) : NULL; |
|
| 2061 | 2061 | |
| 2062 | - apply_filters( 'wpinv_invoice_get_recurring_item', $item, $this ); |
|
| 2062 | + apply_filters('wpinv_invoice_get_recurring_item', $item, $this); |
|
| 2063 | 2063 | } |
| 2064 | 2064 | |
| 2065 | - return apply_filters( 'wpinv_invoice_get_recurring_item_id', $item, $this ); |
|
| 2065 | + return apply_filters('wpinv_invoice_get_recurring_item_id', $item, $this); |
|
| 2066 | 2066 | } |
| 2067 | 2067 | |
| 2068 | 2068 | public function get_subscription_name() { |
| 2069 | - $item = $this->get_recurring( true ); |
|
| 2069 | + $item = $this->get_recurring(true); |
|
| 2070 | 2070 | |
| 2071 | - if ( empty( $item ) ) { |
|
| 2071 | + if (empty($item)) { |
|
| 2072 | 2072 | return NULL; |
| 2073 | 2073 | } |
| 2074 | 2074 | |
| 2075 | - if ( !($name = $item->get_name()) ) { |
|
| 2075 | + if (!($name = $item->get_name())) { |
|
| 2076 | 2076 | $name = $item->post_name; |
| 2077 | 2077 | } |
| 2078 | 2078 | |
| 2079 | - return apply_filters( 'wpinv_invoice_get_subscription_name', $name, $this ); |
|
| 2079 | + return apply_filters('wpinv_invoice_get_subscription_name', $name, $this); |
|
| 2080 | 2080 | } |
| 2081 | 2081 | |
| 2082 | 2082 | public function get_expiration() { |
| 2083 | - $expiration = $this->get_meta( '_wpinv_subscr_expiration', true ); |
|
| 2083 | + $expiration = $this->get_meta('_wpinv_subscr_expiration', true); |
|
| 2084 | 2084 | return $expiration; |
| 2085 | 2085 | } |
| 2086 | 2086 | |
| 2087 | - public function get_cancelled_date( $formatted = true ) { |
|
| 2088 | - $cancelled_date = $this->get_subscription_status() == 'cancelled' ? $this->get_meta( '_wpinv_subscr_cancelled_on', true ) : ''; |
|
| 2087 | + public function get_cancelled_date($formatted = true) { |
|
| 2088 | + $cancelled_date = $this->get_subscription_status() == 'cancelled' ? $this->get_meta('_wpinv_subscr_cancelled_on', true) : ''; |
|
| 2089 | 2089 | |
| 2090 | - if ( $formatted && $cancelled_date ) { |
|
| 2091 | - $cancelled_date = date_i18n( get_option( 'date_format' ), strtotime( $cancelled_date ) ); |
|
| 2090 | + if ($formatted && $cancelled_date) { |
|
| 2091 | + $cancelled_date = date_i18n(get_option('date_format'), strtotime($cancelled_date)); |
|
| 2092 | 2092 | } |
| 2093 | 2093 | |
| 2094 | 2094 | return $cancelled_date; |
| 2095 | 2095 | } |
| 2096 | 2096 | |
| 2097 | - public function get_trial_end_date( $formatted = true ) { |
|
| 2098 | - if ( !$this->is_free_trial() || !$this->is_paid() ) { |
|
| 2097 | + public function get_trial_end_date($formatted = true) { |
|
| 2098 | + if (!$this->is_free_trial() || !$this->is_paid()) { |
|
| 2099 | 2099 | return NULL; |
| 2100 | 2100 | } |
| 2101 | 2101 | |
| 2102 | - $trial_end_date = $this->get_subscription_status() == 'trialing' ? $this->get_meta( '_wpinv_subscr_trial_end', true ) : ''; |
|
| 2102 | + $trial_end_date = $this->get_subscription_status() == 'trialing' ? $this->get_meta('_wpinv_subscr_trial_end', true) : ''; |
|
| 2103 | 2103 | |
| 2104 | - if ( empty( $trial_end_date ) ) { |
|
| 2105 | - $trial_start_time = strtotime( $this->get_subscription_start() ); |
|
| 2106 | - $trial_start_time += ( wpinv_period_in_days( $this->get_subscription_trial_interval(), $this->get_subscription_trial_period() ) * DAY_IN_SECONDS ) ; |
|
| 2104 | + if (empty($trial_end_date)) { |
|
| 2105 | + $trial_start_time = strtotime($this->get_subscription_start()); |
|
| 2106 | + $trial_start_time += (wpinv_period_in_days($this->get_subscription_trial_interval(), $this->get_subscription_trial_period()) * DAY_IN_SECONDS); |
|
| 2107 | 2107 | |
| 2108 | - $trial_end_date = date_i18n( 'Y-m-d H:i:s', $trial_start_time ); |
|
| 2108 | + $trial_end_date = date_i18n('Y-m-d H:i:s', $trial_start_time); |
|
| 2109 | 2109 | } |
| 2110 | 2110 | |
| 2111 | - if ( $formatted && $trial_end_date ) { |
|
| 2112 | - $trial_end_date = date_i18n( get_option( 'date_format' ), strtotime( $trial_end_date ) ); |
|
| 2111 | + if ($formatted && $trial_end_date) { |
|
| 2112 | + $trial_end_date = date_i18n(get_option('date_format'), strtotime($trial_end_date)); |
|
| 2113 | 2113 | } |
| 2114 | 2114 | |
| 2115 | 2115 | return $trial_end_date; |
| 2116 | 2116 | } |
| 2117 | 2117 | |
| 2118 | - public function get_subscription_created( $default = true ) { |
|
| 2119 | - $created = $this->get_meta( '_wpinv_subscr_created', true ); |
|
| 2118 | + public function get_subscription_created($default = true) { |
|
| 2119 | + $created = $this->get_meta('_wpinv_subscr_created', true); |
|
| 2120 | 2120 | |
| 2121 | - if ( empty( $created ) && $default ) { |
|
| 2121 | + if (empty($created) && $default) { |
|
| 2122 | 2122 | $created = $this->date; |
| 2123 | 2123 | } |
| 2124 | 2124 | return $created; |
| 2125 | 2125 | } |
| 2126 | 2126 | |
| 2127 | - public function get_subscription_start( $formatted = true ) { |
|
| 2128 | - if ( !$this->is_paid() ) { |
|
| 2127 | + public function get_subscription_start($formatted = true) { |
|
| 2128 | + if (!$this->is_paid()) { |
|
| 2129 | 2129 | return '-'; |
| 2130 | 2130 | } |
| 2131 | - $start = $this->get_subscription_created(); |
|
| 2131 | + $start = $this->get_subscription_created(); |
|
| 2132 | 2132 | |
| 2133 | - if ( $formatted ) { |
|
| 2134 | - $date = date_i18n( get_option( 'date_format' ), strtotime( $start ) ); |
|
| 2133 | + if ($formatted) { |
|
| 2134 | + $date = date_i18n(get_option('date_format'), strtotime($start)); |
|
| 2135 | 2135 | } else { |
| 2136 | - $date = date_i18n( 'Y-m-d H:i:s', strtotime( $start ) ); |
|
| 2136 | + $date = date_i18n('Y-m-d H:i:s', strtotime($start)); |
|
| 2137 | 2137 | } |
| 2138 | 2138 | |
| 2139 | 2139 | return $date; |
| 2140 | 2140 | } |
| 2141 | 2141 | |
| 2142 | - public function get_subscription_end( $formatted = true ) { |
|
| 2143 | - if ( !$this->is_paid() ) { |
|
| 2142 | + public function get_subscription_end($formatted = true) { |
|
| 2143 | + if (!$this->is_paid()) { |
|
| 2144 | 2144 | return '-'; |
| 2145 | 2145 | } |
| 2146 | 2146 | $start = $this->get_subscription_created(); |
| 2147 | 2147 | $interval = $this->get_subscription_interval(); |
| 2148 | - $period = $this->get_subscription_period( true ); |
|
| 2148 | + $period = $this->get_subscription_period(true); |
|
| 2149 | 2149 | $bill_times = (int)$this->get_bill_times(); |
| 2150 | 2150 | |
| 2151 | - if ( $bill_times == 0 ) { |
|
| 2152 | - return $formatted ? __( 'Until cancelled', 'invoicing' ) : $bill_times; |
|
| 2151 | + if ($bill_times == 0) { |
|
| 2152 | + return $formatted ? __('Until cancelled', 'invoicing') : $bill_times; |
|
| 2153 | 2153 | } |
| 2154 | 2154 | |
| 2155 | - $total_period = $start . '+' . ( $interval * $bill_times ) . ' ' . $period; |
|
| 2155 | + $total_period = $start . '+' . ($interval * $bill_times) . ' ' . $period; |
|
| 2156 | 2156 | |
| 2157 | - $end_time = strtotime( $start . '+' . ( $interval * $bill_times ) . ' ' . $period ); |
|
| 2157 | + $end_time = strtotime($start . '+' . ($interval * $bill_times) . ' ' . $period); |
|
| 2158 | 2158 | |
| 2159 | - if ( $this->is_free_trial() ) { |
|
| 2160 | - $end_time += ( wpinv_period_in_days( $this->get_subscription_trial_interval(), $this->get_subscription_trial_period() ) * DAY_IN_SECONDS ) ; |
|
| 2159 | + if ($this->is_free_trial()) { |
|
| 2160 | + $end_time += (wpinv_period_in_days($this->get_subscription_trial_interval(), $this->get_subscription_trial_period()) * DAY_IN_SECONDS); |
|
| 2161 | 2161 | } |
| 2162 | 2162 | |
| 2163 | - if ( $formatted ) { |
|
| 2164 | - $date = date_i18n( get_option( 'date_format' ), $end_time ); |
|
| 2163 | + if ($formatted) { |
|
| 2164 | + $date = date_i18n(get_option('date_format'), $end_time); |
|
| 2165 | 2165 | } else { |
| 2166 | - $date = date_i18n( 'Y-m-d H:i:s', $end_time ); |
|
| 2166 | + $date = date_i18n('Y-m-d H:i:s', $end_time); |
|
| 2167 | 2167 | } |
| 2168 | 2168 | |
| 2169 | 2169 | return $date; |
| 2170 | 2170 | } |
| 2171 | 2171 | |
| 2172 | 2172 | public function get_expiration_time() { |
| 2173 | - return strtotime( $this->get_expiration(), current_time( 'timestamp' ) ); |
|
| 2173 | + return strtotime($this->get_expiration(), current_time('timestamp')); |
|
| 2174 | 2174 | } |
| 2175 | 2175 | |
| 2176 | 2176 | public function get_original_invoice_id() { |
@@ -2182,125 +2182,125 @@ discard block |
||
| 2182 | 2182 | return $subscription_data['bill_times']; |
| 2183 | 2183 | } |
| 2184 | 2184 | |
| 2185 | - public function get_child_payments( $self = false ) { |
|
| 2186 | - $invoices = get_posts( array( |
|
| 2185 | + public function get_child_payments($self = false) { |
|
| 2186 | + $invoices = get_posts(array( |
|
| 2187 | 2187 | 'post_type' => $this->post_type, |
| 2188 | 2188 | 'post_parent' => (int)$this->ID, |
| 2189 | 2189 | 'posts_per_page' => '999', |
| 2190 | - 'post_status' => array( 'publish', 'wpi-processing', 'wpi-renewal' ), |
|
| 2190 | + 'post_status' => array('publish', 'wpi-processing', 'wpi-renewal'), |
|
| 2191 | 2191 | 'orderby' => 'ID', |
| 2192 | 2192 | 'order' => 'DESC', |
| 2193 | 2193 | 'fields' => 'ids' |
| 2194 | - ) ); |
|
| 2194 | + )); |
|
| 2195 | 2195 | |
| 2196 | - if ( $this->is_free_trial() ) { |
|
| 2196 | + if ($this->is_free_trial()) { |
|
| 2197 | 2197 | $self = false; |
| 2198 | 2198 | } |
| 2199 | 2199 | |
| 2200 | - if ( $self && $this->is_paid() ) { |
|
| 2201 | - if ( !empty( $invoices ) ) { |
|
| 2200 | + if ($self && $this->is_paid()) { |
|
| 2201 | + if (!empty($invoices)) { |
|
| 2202 | 2202 | $invoices[] = (int)$this->ID; |
| 2203 | 2203 | } else { |
| 2204 | - $invoices = array( $this->ID ); |
|
| 2204 | + $invoices = array($this->ID); |
|
| 2205 | 2205 | } |
| 2206 | 2206 | |
| 2207 | - $invoices = array_unique( $invoices ); |
|
| 2207 | + $invoices = array_unique($invoices); |
|
| 2208 | 2208 | } |
| 2209 | 2209 | |
| 2210 | 2210 | return $invoices; |
| 2211 | 2211 | } |
| 2212 | 2212 | |
| 2213 | - public function get_total_payments( $self = true ) { |
|
| 2214 | - return count( $this->get_child_payments( $self ) ); |
|
| 2213 | + public function get_total_payments($self = true) { |
|
| 2214 | + return count($this->get_child_payments($self)); |
|
| 2215 | 2215 | } |
| 2216 | 2216 | |
| 2217 | - public function get_subscriptions( $limit = -1 ) { |
|
| 2218 | - $subscriptions = wpinv_get_subscriptions( array( 'parent_invoice_id' => $this->ID, 'numberposts' => $limit ) ); |
|
| 2217 | + public function get_subscriptions($limit = -1) { |
|
| 2218 | + $subscriptions = wpinv_get_subscriptions(array('parent_invoice_id' => $this->ID, 'numberposts' => $limit)); |
|
| 2219 | 2219 | |
| 2220 | 2220 | return $subscriptions; |
| 2221 | 2221 | } |
| 2222 | 2222 | |
| 2223 | 2223 | public function get_subscription_id() { |
| 2224 | - $subscription_id = $this->get_meta( '_wpinv_subscr_profile_id', true ); |
|
| 2224 | + $subscription_id = $this->get_meta('_wpinv_subscr_profile_id', true); |
|
| 2225 | 2225 | |
| 2226 | - if ( empty( $subscription_id ) && !empty( $this->parent_invoice ) ) { |
|
| 2227 | - $parent_invoice = wpinv_get_invoice( $this->parent_invoice ); |
|
| 2226 | + if (empty($subscription_id) && !empty($this->parent_invoice)) { |
|
| 2227 | + $parent_invoice = wpinv_get_invoice($this->parent_invoice); |
|
| 2228 | 2228 | |
| 2229 | - $subscription_id = $parent_invoice->get_meta( '_wpinv_subscr_profile_id', true ); |
|
| 2229 | + $subscription_id = $parent_invoice->get_meta('_wpinv_subscr_profile_id', true); |
|
| 2230 | 2230 | } |
| 2231 | 2231 | |
| 2232 | 2232 | return $subscription_id; |
| 2233 | 2233 | } |
| 2234 | 2234 | |
| 2235 | 2235 | public function get_subscription_status() { |
| 2236 | - $subscription_status = $this->get_meta( '_wpinv_subscr_status', true ); |
|
| 2236 | + $subscription_status = $this->get_meta('_wpinv_subscr_status', true); |
|
| 2237 | 2237 | |
| 2238 | - if ( empty( $subscription_status ) ) { |
|
| 2238 | + if (empty($subscription_status)) { |
|
| 2239 | 2239 | $status = 'pending'; |
| 2240 | 2240 | |
| 2241 | - if ( $this->is_paid() ) { |
|
| 2241 | + if ($this->is_paid()) { |
|
| 2242 | 2242 | $bill_times = (int)$this->get_bill_times(); |
| 2243 | 2243 | $times_billed = (int)$this->get_total_payments(); |
| 2244 | - $expiration = $this->get_subscription_end( false ); |
|
| 2245 | - $expired = $bill_times != 0 && $expiration != '' && $expiration != '-' && strtotime( date_i18n( 'Y-m-d', strtotime( $expiration ) ) ) < strtotime( date_i18n( 'Y-m-d', current_time( 'timestamp' ) ) ) ? true : false; |
|
| 2244 | + $expiration = $this->get_subscription_end(false); |
|
| 2245 | + $expired = $bill_times != 0 && $expiration != '' && $expiration != '-' && strtotime(date_i18n('Y-m-d', strtotime($expiration))) < strtotime(date_i18n('Y-m-d', current_time('timestamp'))) ? true : false; |
|
| 2246 | 2246 | |
| 2247 | - if ( (int)$bill_times == 0 ) { |
|
| 2247 | + if ((int)$bill_times == 0) { |
|
| 2248 | 2248 | $status = $expired ? 'expired' : 'active'; |
| 2249 | - } else if ( $bill_times > 0 && $times_billed >= $bill_times ) { |
|
| 2249 | + } else if ($bill_times > 0 && $times_billed >= $bill_times) { |
|
| 2250 | 2250 | $status = 'completed'; |
| 2251 | - } else if ( $expired ) { |
|
| 2251 | + } else if ($expired) { |
|
| 2252 | 2252 | $status = 'expired'; |
| 2253 | - } else if ( $bill_times > 0 ) { |
|
| 2253 | + } else if ($bill_times > 0) { |
|
| 2254 | 2254 | $status = 'active'; |
| 2255 | 2255 | } else { |
| 2256 | 2256 | $status = 'pending'; |
| 2257 | 2257 | } |
| 2258 | 2258 | } |
| 2259 | 2259 | |
| 2260 | - if ( $status && $status != $subscription_status ) { |
|
| 2260 | + if ($status && $status != $subscription_status) { |
|
| 2261 | 2261 | $subscription_status = $status; |
| 2262 | 2262 | |
| 2263 | - $this->update_meta( '_wpinv_subscr_status', $status ); |
|
| 2263 | + $this->update_meta('_wpinv_subscr_status', $status); |
|
| 2264 | 2264 | } |
| 2265 | 2265 | } |
| 2266 | 2266 | |
| 2267 | 2267 | return $subscription_status; |
| 2268 | 2268 | } |
| 2269 | 2269 | |
| 2270 | - public function get_subscription_status_label( $status = '' ) { |
|
| 2271 | - $status = !empty( $status ) ? $status : $this->get_subscription_status(); |
|
| 2270 | + public function get_subscription_status_label($status = '') { |
|
| 2271 | + $status = !empty($status) ? $status : $this->get_subscription_status(); |
|
| 2272 | 2272 | |
| 2273 | - switch( $status ) { |
|
| 2273 | + switch ($status) { |
|
| 2274 | 2274 | case 'active' : |
| 2275 | - $status_label = __( 'Active', 'invoicing' ); |
|
| 2275 | + $status_label = __('Active', 'invoicing'); |
|
| 2276 | 2276 | break; |
| 2277 | 2277 | |
| 2278 | 2278 | case 'cancelled' : |
| 2279 | - $status_label = __( 'Cancelled', 'invoicing' ); |
|
| 2279 | + $status_label = __('Cancelled', 'invoicing'); |
|
| 2280 | 2280 | break; |
| 2281 | 2281 | |
| 2282 | 2282 | case 'completed' : |
| 2283 | - $status_label = __( 'Completed', 'invoicing' ); |
|
| 2283 | + $status_label = __('Completed', 'invoicing'); |
|
| 2284 | 2284 | break; |
| 2285 | 2285 | |
| 2286 | 2286 | case 'expired' : |
| 2287 | - $status_label = __( 'Expired', 'invoicing' ); |
|
| 2287 | + $status_label = __('Expired', 'invoicing'); |
|
| 2288 | 2288 | break; |
| 2289 | 2289 | |
| 2290 | 2290 | case 'pending' : |
| 2291 | - $status_label = __( 'Pending', 'invoicing' ); |
|
| 2291 | + $status_label = __('Pending', 'invoicing'); |
|
| 2292 | 2292 | break; |
| 2293 | 2293 | |
| 2294 | 2294 | case 'failing' : |
| 2295 | - $status_label = __( 'Failing', 'invoicing' ); |
|
| 2295 | + $status_label = __('Failing', 'invoicing'); |
|
| 2296 | 2296 | break; |
| 2297 | 2297 | |
| 2298 | 2298 | case 'stopped' : |
| 2299 | - $status_label = __( 'Stopped', 'invoicing' ); |
|
| 2299 | + $status_label = __('Stopped', 'invoicing'); |
|
| 2300 | 2300 | break; |
| 2301 | 2301 | |
| 2302 | 2302 | case 'trialing' : |
| 2303 | - $status_label = __( 'Trialing', 'invoicing' ); |
|
| 2303 | + $status_label = __('Trialing', 'invoicing'); |
|
| 2304 | 2304 | break; |
| 2305 | 2305 | |
| 2306 | 2306 | default: |
@@ -2311,26 +2311,26 @@ discard block |
||
| 2311 | 2311 | return $status_label; |
| 2312 | 2312 | } |
| 2313 | 2313 | |
| 2314 | - public function get_subscription_period( $full = false ) { |
|
| 2315 | - $period = $this->get_meta( '_wpinv_subscr_period', true ); |
|
| 2314 | + public function get_subscription_period($full = false) { |
|
| 2315 | + $period = $this->get_meta('_wpinv_subscr_period', true); |
|
| 2316 | 2316 | |
| 2317 | 2317 | // Fix period for old invoices |
| 2318 | - if ( $period == 'day' ) { |
|
| 2318 | + if ($period == 'day') { |
|
| 2319 | 2319 | $period = 'D'; |
| 2320 | - } else if ( $period == 'week' ) { |
|
| 2320 | + } else if ($period == 'week') { |
|
| 2321 | 2321 | $period = 'W'; |
| 2322 | - } else if ( $period == 'month' ) { |
|
| 2322 | + } else if ($period == 'month') { |
|
| 2323 | 2323 | $period = 'M'; |
| 2324 | - } else if ( $period == 'year' ) { |
|
| 2324 | + } else if ($period == 'year') { |
|
| 2325 | 2325 | $period = 'Y'; |
| 2326 | 2326 | } |
| 2327 | 2327 | |
| 2328 | - if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
|
| 2328 | + if (!in_array($period, array('D', 'W', 'M', 'Y'))) { |
|
| 2329 | 2329 | $period = 'D'; |
| 2330 | 2330 | } |
| 2331 | 2331 | |
| 2332 | - if ( $full ) { |
|
| 2333 | - switch( $period ) { |
|
| 2332 | + if ($full) { |
|
| 2333 | + switch ($period) { |
|
| 2334 | 2334 | case 'D': |
| 2335 | 2335 | $period = 'day'; |
| 2336 | 2336 | break; |
@@ -2350,39 +2350,39 @@ discard block |
||
| 2350 | 2350 | } |
| 2351 | 2351 | |
| 2352 | 2352 | public function get_subscription_interval() { |
| 2353 | - $interval = (int)$this->get_meta( '_wpinv_subscr_interval', true ); |
|
| 2353 | + $interval = (int)$this->get_meta('_wpinv_subscr_interval', true); |
|
| 2354 | 2354 | |
| 2355 | - if ( !$interval > 0 ) { |
|
| 2355 | + if (!$interval > 0) { |
|
| 2356 | 2356 | $interval = 1; |
| 2357 | 2357 | } |
| 2358 | 2358 | |
| 2359 | 2359 | return $interval; |
| 2360 | 2360 | } |
| 2361 | 2361 | |
| 2362 | - public function get_subscription_trial_period( $full = false ) { |
|
| 2363 | - if ( !$this->is_free_trial() ) { |
|
| 2362 | + public function get_subscription_trial_period($full = false) { |
|
| 2363 | + if (!$this->is_free_trial()) { |
|
| 2364 | 2364 | return ''; |
| 2365 | 2365 | } |
| 2366 | 2366 | |
| 2367 | - $period = $this->get_meta( '_wpinv_subscr_trial_period', true ); |
|
| 2367 | + $period = $this->get_meta('_wpinv_subscr_trial_period', true); |
|
| 2368 | 2368 | |
| 2369 | 2369 | // Fix period for old invoices |
| 2370 | - if ( $period == 'day' ) { |
|
| 2370 | + if ($period == 'day') { |
|
| 2371 | 2371 | $period = 'D'; |
| 2372 | - } else if ( $period == 'week' ) { |
|
| 2372 | + } else if ($period == 'week') { |
|
| 2373 | 2373 | $period = 'W'; |
| 2374 | - } else if ( $period == 'month' ) { |
|
| 2374 | + } else if ($period == 'month') { |
|
| 2375 | 2375 | $period = 'M'; |
| 2376 | - } else if ( $period == 'year' ) { |
|
| 2376 | + } else if ($period == 'year') { |
|
| 2377 | 2377 | $period = 'Y'; |
| 2378 | 2378 | } |
| 2379 | 2379 | |
| 2380 | - if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
|
| 2380 | + if (!in_array($period, array('D', 'W', 'M', 'Y'))) { |
|
| 2381 | 2381 | $period = 'D'; |
| 2382 | 2382 | } |
| 2383 | 2383 | |
| 2384 | - if ( $full ) { |
|
| 2385 | - switch( $period ) { |
|
| 2384 | + if ($full) { |
|
| 2385 | + switch ($period) { |
|
| 2386 | 2386 | case 'D': |
| 2387 | 2387 | $period = 'day'; |
| 2388 | 2388 | break; |
@@ -2402,13 +2402,13 @@ discard block |
||
| 2402 | 2402 | } |
| 2403 | 2403 | |
| 2404 | 2404 | public function get_subscription_trial_interval() { |
| 2405 | - if ( !$this->is_free_trial() ) { |
|
| 2405 | + if (!$this->is_free_trial()) { |
|
| 2406 | 2406 | return 0; |
| 2407 | 2407 | } |
| 2408 | 2408 | |
| 2409 | - $interval = (int)$this->get_meta( '_wpinv_subscr_trial_interval', true ); |
|
| 2409 | + $interval = (int)$this->get_meta('_wpinv_subscr_trial_interval', true); |
|
| 2410 | 2410 | |
| 2411 | - if ( !$interval > 0 ) { |
|
| 2411 | + if (!$interval > 0) { |
|
| 2412 | 2412 | $interval = 1; |
| 2413 | 2413 | } |
| 2414 | 2414 | |
@@ -2420,8 +2420,8 @@ discard block |
||
| 2420 | 2420 | 'status' => 'failing' |
| 2421 | 2421 | ); |
| 2422 | 2422 | |
| 2423 | - if ( $this->update_subscription( $args ) ) { |
|
| 2424 | - do_action( 'wpinv_subscription_failing', $this->ID, $this ); |
|
| 2423 | + if ($this->update_subscription($args)) { |
|
| 2424 | + do_action('wpinv_subscription_failing', $this->ID, $this); |
|
| 2425 | 2425 | return true; |
| 2426 | 2426 | } |
| 2427 | 2427 | |
@@ -2433,8 +2433,8 @@ discard block |
||
| 2433 | 2433 | 'status' => 'stopped' |
| 2434 | 2434 | ); |
| 2435 | 2435 | |
| 2436 | - if ( $this->update_subscription( $args ) ) { |
|
| 2437 | - do_action( 'wpinv_subscription_stopped', $this->ID, $this ); |
|
| 2436 | + if ($this->update_subscription($args)) { |
|
| 2437 | + do_action('wpinv_subscription_stopped', $this->ID, $this); |
|
| 2438 | 2438 | return true; |
| 2439 | 2439 | } |
| 2440 | 2440 | |
@@ -2446,8 +2446,8 @@ discard block |
||
| 2446 | 2446 | 'status' => 'active' |
| 2447 | 2447 | ); |
| 2448 | 2448 | |
| 2449 | - if ( $this->update_subscription( $args ) ) { |
|
| 2450 | - do_action( 'wpinv_subscription_restarted', $this->ID, $this ); |
|
| 2449 | + if ($this->update_subscription($args)) { |
|
| 2450 | + do_action('wpinv_subscription_restarted', $this->ID, $this); |
|
| 2451 | 2451 | return true; |
| 2452 | 2452 | } |
| 2453 | 2453 | |
@@ -2459,23 +2459,23 @@ discard block |
||
| 2459 | 2459 | 'status' => 'cancelled' |
| 2460 | 2460 | ); |
| 2461 | 2461 | |
| 2462 | - if ( $this->update_subscription( $args ) ) { |
|
| 2463 | - if ( is_user_logged_in() ) { |
|
| 2464 | - $userdata = get_userdata( get_current_user_id() ); |
|
| 2462 | + if ($this->update_subscription($args)) { |
|
| 2463 | + if (is_user_logged_in()) { |
|
| 2464 | + $userdata = get_userdata(get_current_user_id()); |
|
| 2465 | 2465 | $user = $userdata->user_login; |
| 2466 | 2466 | } else { |
| 2467 | - $user = __( 'gateway', 'invoicing' ); |
|
| 2467 | + $user = __('gateway', 'invoicing'); |
|
| 2468 | 2468 | } |
| 2469 | 2469 | |
| 2470 | 2470 | $subscription_id = $this->get_subscription_id(); |
| 2471 | - if ( !$subscription_id ) { |
|
| 2471 | + if (!$subscription_id) { |
|
| 2472 | 2472 | $subscription_id = $this->ID; |
| 2473 | 2473 | } |
| 2474 | 2474 | |
| 2475 | - $note = sprintf( __( 'Subscription %s has been cancelled by %s', 'invoicing' ), $subscription_id, $user ); |
|
| 2476 | - $this->add_note( $note ); |
|
| 2475 | + $note = sprintf(__('Subscription %s has been cancelled by %s', 'invoicing'), $subscription_id, $user); |
|
| 2476 | + $this->add_note($note); |
|
| 2477 | 2477 | |
| 2478 | - do_action( 'wpinv_subscription_cancelled', $this->ID, $this ); |
|
| 2478 | + do_action('wpinv_subscription_cancelled', $this->ID, $this); |
|
| 2479 | 2479 | return true; |
| 2480 | 2480 | } |
| 2481 | 2481 | |
@@ -2483,11 +2483,11 @@ discard block |
||
| 2483 | 2483 | } |
| 2484 | 2484 | |
| 2485 | 2485 | public function can_cancel() { |
| 2486 | - return apply_filters( 'wpinv_subscription_can_cancel', false, $this ); |
|
| 2486 | + return apply_filters('wpinv_subscription_can_cancel', false, $this); |
|
| 2487 | 2487 | } |
| 2488 | 2488 | |
| 2489 | - public function add_subscription( $data = array() ) { |
|
| 2490 | - if ( empty( $this->ID ) ) { |
|
| 2489 | + public function add_subscription($data = array()) { |
|
| 2490 | + if (empty($this->ID)) { |
|
| 2491 | 2491 | return false; |
| 2492 | 2492 | } |
| 2493 | 2493 | |
@@ -2506,85 +2506,85 @@ discard block |
||
| 2506 | 2506 | 'profile_id' => '', |
| 2507 | 2507 | ); |
| 2508 | 2508 | |
| 2509 | - $args = wp_parse_args( $data, $defaults ); |
|
| 2509 | + $args = wp_parse_args($data, $defaults); |
|
| 2510 | 2510 | |
| 2511 | - if ( $args['expiration'] && strtotime( 'NOW', current_time( 'timestamp' ) ) > strtotime( $args['expiration'], current_time( 'timestamp' ) ) ) { |
|
| 2512 | - if ( 'active' == $args['status'] || $args['status'] == 'trialing' ) { |
|
| 2511 | + if ($args['expiration'] && strtotime('NOW', current_time('timestamp')) > strtotime($args['expiration'], current_time('timestamp'))) { |
|
| 2512 | + if ('active' == $args['status'] || $args['status'] == 'trialing') { |
|
| 2513 | 2513 | $args['status'] = 'expired'; |
| 2514 | 2514 | } |
| 2515 | 2515 | } |
| 2516 | 2516 | |
| 2517 | - do_action( 'wpinv_subscription_pre_create', $args, $data, $this ); |
|
| 2517 | + do_action('wpinv_subscription_pre_create', $args, $data, $this); |
|
| 2518 | 2518 | |
| 2519 | - if ( !empty( $args ) ) { |
|
| 2520 | - foreach ( $args as $key => $value ) { |
|
| 2521 | - $this->update_meta( '_wpinv_subscr_' . $key, $value ); |
|
| 2519 | + if (!empty($args)) { |
|
| 2520 | + foreach ($args as $key => $value) { |
|
| 2521 | + $this->update_meta('_wpinv_subscr_' . $key, $value); |
|
| 2522 | 2522 | } |
| 2523 | 2523 | } |
| 2524 | 2524 | |
| 2525 | - do_action( 'wpinv_subscription_post_create', $args, $data, $this ); |
|
| 2525 | + do_action('wpinv_subscription_post_create', $args, $data, $this); |
|
| 2526 | 2526 | |
| 2527 | 2527 | return true; |
| 2528 | 2528 | } |
| 2529 | 2529 | |
| 2530 | - public function update_subscription( $args = array() ) { |
|
| 2531 | - if ( empty( $this->ID ) ) { |
|
| 2530 | + public function update_subscription($args = array()) { |
|
| 2531 | + if (empty($this->ID)) { |
|
| 2532 | 2532 | return false; |
| 2533 | 2533 | } |
| 2534 | 2534 | |
| 2535 | - if ( !empty( $args['expiration'] ) && $args['expiration'] && strtotime( 'NOW', current_time( 'timestamp' ) ) > strtotime( $args['expiration'], current_time( 'timestamp' ) ) ) { |
|
| 2536 | - if ( !isset( $args['status'] ) || ( isset( $args['status'] ) && ( 'active' == $args['status'] || $args['status'] == 'trialing' ) ) ) { |
|
| 2535 | + if (!empty($args['expiration']) && $args['expiration'] && strtotime('NOW', current_time('timestamp')) > strtotime($args['expiration'], current_time('timestamp'))) { |
|
| 2536 | + if (!isset($args['status']) || (isset($args['status']) && ('active' == $args['status'] || $args['status'] == 'trialing'))) { |
|
| 2537 | 2537 | $args['status'] = 'expired'; |
| 2538 | 2538 | } |
| 2539 | 2539 | } |
| 2540 | 2540 | |
| 2541 | - if ( isset( $args['status'] ) && $args['status'] == 'cancelled' && empty( $args['cancelled_on'] ) ) { |
|
| 2542 | - $args['cancelled_on'] = date_i18n( 'Y-m-d H:i:s', current_time( 'timestamp' ) ); |
|
| 2541 | + if (isset($args['status']) && $args['status'] == 'cancelled' && empty($args['cancelled_on'])) { |
|
| 2542 | + $args['cancelled_on'] = date_i18n('Y-m-d H:i:s', current_time('timestamp')); |
|
| 2543 | 2543 | } |
| 2544 | 2544 | |
| 2545 | - do_action( 'wpinv_subscription_pre_update', $args, $this ); |
|
| 2545 | + do_action('wpinv_subscription_pre_update', $args, $this); |
|
| 2546 | 2546 | |
| 2547 | - if ( !empty( $args ) ) { |
|
| 2548 | - foreach ( $args as $key => $value ) { |
|
| 2549 | - $this->update_meta( '_wpinv_subscr_' . $key, $value ); |
|
| 2547 | + if (!empty($args)) { |
|
| 2548 | + foreach ($args as $key => $value) { |
|
| 2549 | + $this->update_meta('_wpinv_subscr_' . $key, $value); |
|
| 2550 | 2550 | } |
| 2551 | 2551 | } |
| 2552 | 2552 | |
| 2553 | - do_action( 'wpinv_subscription_post_update', $args, $this ); |
|
| 2553 | + do_action('wpinv_subscription_post_update', $args, $this); |
|
| 2554 | 2554 | |
| 2555 | 2555 | return true; |
| 2556 | 2556 | } |
| 2557 | 2557 | |
| 2558 | 2558 | public function renew_subscription() { |
| 2559 | 2559 | $parent_invoice = $this->get_parent_payment(); |
| 2560 | - $parent_invoice = empty( $parent_invoice ) ? $this : $parent_invoice; |
|
| 2560 | + $parent_invoice = empty($parent_invoice) ? $this : $parent_invoice; |
|
| 2561 | 2561 | |
| 2562 | - $current_time = current_time( 'timestamp' ); |
|
| 2562 | + $current_time = current_time('timestamp'); |
|
| 2563 | 2563 | $start = $this->get_subscription_created(); |
| 2564 | - $start = $start ? strtotime( $start ) : $current_time; |
|
| 2564 | + $start = $start ? strtotime($start) : $current_time; |
|
| 2565 | 2565 | $expires = $this->get_expiration_time(); |
| 2566 | 2566 | |
| 2567 | - if ( !$expires ) { |
|
| 2568 | - $expires = strtotime( '+' . $parent_invoice->get_subscription_interval() . ' ' . $parent_invoice->get_subscription_period( true ), $start ); |
|
| 2567 | + if (!$expires) { |
|
| 2568 | + $expires = strtotime('+' . $parent_invoice->get_subscription_interval() . ' ' . $parent_invoice->get_subscription_period(true), $start); |
|
| 2569 | 2569 | } |
| 2570 | 2570 | |
| 2571 | - $expiration = date_i18n( 'Y-m-d 23:59:59', $expires ); |
|
| 2572 | - $expiration = apply_filters( 'wpinv_subscription_renewal_expiration', $expiration, $this->ID, $this ); |
|
| 2571 | + $expiration = date_i18n('Y-m-d 23:59:59', $expires); |
|
| 2572 | + $expiration = apply_filters('wpinv_subscription_renewal_expiration', $expiration, $this->ID, $this); |
|
| 2573 | 2573 | $bill_times = $parent_invoice->get_bill_times(); |
| 2574 | 2574 | $times_billed = $parent_invoice->get_total_payments(); |
| 2575 | 2575 | |
| 2576 | - if ( $parent_invoice->get_subscription_status() == 'trialing' && ( $times_billed > 0 || strtotime( date_i18n( 'Y-m-d' ) ) < strtotime( $parent_invoice->get_trial_end_date( false ) ) ) ) { |
|
| 2576 | + if ($parent_invoice->get_subscription_status() == 'trialing' && ($times_billed > 0 || strtotime(date_i18n('Y-m-d')) < strtotime($parent_invoice->get_trial_end_date(false)))) { |
|
| 2577 | 2577 | $args = array( |
| 2578 | 2578 | 'status' => 'active', |
| 2579 | 2579 | ); |
| 2580 | 2580 | |
| 2581 | - $parent_invoice->update_subscription( $args ); |
|
| 2581 | + $parent_invoice->update_subscription($args); |
|
| 2582 | 2582 | } |
| 2583 | 2583 | |
| 2584 | - do_action( 'wpinv_subscription_pre_renew', $this->ID, $expiration, $this ); |
|
| 2584 | + do_action('wpinv_subscription_pre_renew', $this->ID, $expiration, $this); |
|
| 2585 | 2585 | |
| 2586 | - $status = 'active'; |
|
| 2587 | - if ( $bill_times > 0 && $times_billed >= $bill_times ) { |
|
| 2586 | + $status = 'active'; |
|
| 2587 | + if ($bill_times > 0 && $times_billed >= $bill_times) { |
|
| 2588 | 2588 | $this->complete_subscription(); |
| 2589 | 2589 | $status = 'completed'; |
| 2590 | 2590 | } |
@@ -2594,10 +2594,10 @@ discard block |
||
| 2594 | 2594 | 'status' => $status, |
| 2595 | 2595 | ); |
| 2596 | 2596 | |
| 2597 | - $this->update_subscription( $args ); |
|
| 2597 | + $this->update_subscription($args); |
|
| 2598 | 2598 | |
| 2599 | - do_action( 'wpinv_subscription_post_renew', $this->ID, $expiration, $this ); |
|
| 2600 | - do_action( 'wpinv_recurring_set_subscription_status', $this->ID, $status, $this ); |
|
| 2599 | + do_action('wpinv_subscription_post_renew', $this->ID, $expiration, $this); |
|
| 2600 | + do_action('wpinv_recurring_set_subscription_status', $this->ID, $status, $this); |
|
| 2601 | 2601 | } |
| 2602 | 2602 | |
| 2603 | 2603 | public function complete_subscription() { |
@@ -2605,8 +2605,8 @@ discard block |
||
| 2605 | 2605 | 'status' => 'completed' |
| 2606 | 2606 | ); |
| 2607 | 2607 | |
| 2608 | - if ( $this->update_subscription( $args ) ) { |
|
| 2609 | - do_action( 'wpinv_subscription_completed', $this->ID, $this ); |
|
| 2608 | + if ($this->update_subscription($args)) { |
|
| 2609 | + do_action('wpinv_subscription_completed', $this->ID, $this); |
|
| 2610 | 2610 | } |
| 2611 | 2611 | } |
| 2612 | 2612 | |
@@ -2615,44 +2615,44 @@ discard block |
||
| 2615 | 2615 | 'status' => 'expired' |
| 2616 | 2616 | ); |
| 2617 | 2617 | |
| 2618 | - if ( $this->update_subscription( $args ) ) { |
|
| 2619 | - do_action( 'wpinv_subscription_expired', $this->ID, $this ); |
|
| 2618 | + if ($this->update_subscription($args)) { |
|
| 2619 | + do_action('wpinv_subscription_expired', $this->ID, $this); |
|
| 2620 | 2620 | } |
| 2621 | 2621 | } |
| 2622 | 2622 | |
| 2623 | 2623 | public function get_cancel_url() { |
| 2624 | - $url = wp_nonce_url( add_query_arg( array( 'wpi_action' => 'cancel_subscription', 'sub_id' => $this->ID ) ), 'wpinv-recurring-cancel' ); |
|
| 2624 | + $url = wp_nonce_url(add_query_arg(array('wpi_action' => 'cancel_subscription', 'sub_id' => $this->ID)), 'wpinv-recurring-cancel'); |
|
| 2625 | 2625 | |
| 2626 | - return apply_filters( 'wpinv_subscription_cancel_url', $url, $this ); |
|
| 2626 | + return apply_filters('wpinv_subscription_cancel_url', $url, $this); |
|
| 2627 | 2627 | } |
| 2628 | 2628 | |
| 2629 | 2629 | public function can_update() { |
| 2630 | - return apply_filters( 'wpinv_subscription_can_update', false, $this ); |
|
| 2630 | + return apply_filters('wpinv_subscription_can_update', false, $this); |
|
| 2631 | 2631 | } |
| 2632 | 2632 | |
| 2633 | 2633 | public function get_update_url() { |
| 2634 | - $url = add_query_arg( array( 'action' => 'update', 'sub_id' => $this->ID ) ); |
|
| 2634 | + $url = add_query_arg(array('action' => 'update', 'sub_id' => $this->ID)); |
|
| 2635 | 2635 | |
| 2636 | - return apply_filters( 'wpinv_subscription_update_url', $url, $this ); |
|
| 2636 | + return apply_filters('wpinv_subscription_update_url', $url, $this); |
|
| 2637 | 2637 | } |
| 2638 | 2638 | |
| 2639 | 2639 | public function is_parent() { |
| 2640 | - $is_parent = empty( $this->parent_invoice ) ? true : false; |
|
| 2640 | + $is_parent = empty($this->parent_invoice) ? true : false; |
|
| 2641 | 2641 | |
| 2642 | - return apply_filters( 'wpinv_invoice_is_parent', $is_parent, $this ); |
|
| 2642 | + return apply_filters('wpinv_invoice_is_parent', $is_parent, $this); |
|
| 2643 | 2643 | } |
| 2644 | 2644 | |
| 2645 | 2645 | public function is_renewal() { |
| 2646 | 2646 | $is_renewal = $this->parent_invoice && $this->parent_invoice != $this->ID ? true : false; |
| 2647 | 2647 | |
| 2648 | - return apply_filters( 'wpinv_invoice_is_renewal', $is_renewal, $this ); |
|
| 2648 | + return apply_filters('wpinv_invoice_is_renewal', $is_renewal, $this); |
|
| 2649 | 2649 | } |
| 2650 | 2650 | |
| 2651 | 2651 | public function get_parent_payment() { |
| 2652 | 2652 | $parent_payment = NULL; |
| 2653 | 2653 | |
| 2654 | - if ( $this->is_renewal() ) { |
|
| 2655 | - $parent_payment = wpinv_get_invoice( $this->parent_invoice ); |
|
| 2654 | + if ($this->is_renewal()) { |
|
| 2655 | + $parent_payment = wpinv_get_invoice($this->parent_invoice); |
|
| 2656 | 2656 | } |
| 2657 | 2657 | |
| 2658 | 2658 | return $parent_payment; |
@@ -2663,100 +2663,100 @@ discard block |
||
| 2663 | 2663 | |
| 2664 | 2664 | $subscription_status = $this->get_subscription_status(); |
| 2665 | 2665 | |
| 2666 | - if( ! $this->is_subscription_expired() && ( $subscription_status == 'active' || $subscription_status == 'cancelled' || $subscription_status == 'trialing' ) ) { |
|
| 2666 | + if (!$this->is_subscription_expired() && ($subscription_status == 'active' || $subscription_status == 'cancelled' || $subscription_status == 'trialing')) { |
|
| 2667 | 2667 | $ret = true; |
| 2668 | 2668 | } |
| 2669 | 2669 | |
| 2670 | - return apply_filters( 'wpinv_subscription_is_active', $ret, $this->ID, $this ); |
|
| 2670 | + return apply_filters('wpinv_subscription_is_active', $ret, $this->ID, $this); |
|
| 2671 | 2671 | } |
| 2672 | 2672 | |
| 2673 | 2673 | public function is_subscription_expired() { |
| 2674 | 2674 | $ret = false; |
| 2675 | 2675 | $subscription_status = $this->get_subscription_status(); |
| 2676 | 2676 | |
| 2677 | - if ( $subscription_status == 'expired' ) { |
|
| 2677 | + if ($subscription_status == 'expired') { |
|
| 2678 | 2678 | $ret = true; |
| 2679 | - } else if ( 'active' === $subscription_status || 'cancelled' === $subscription_status || 'trialing' == $subscription_status ) { |
|
| 2679 | + } else if ('active' === $subscription_status || 'cancelled' === $subscription_status || 'trialing' == $subscription_status) { |
|
| 2680 | 2680 | $ret = false; |
| 2681 | 2681 | $expiration = $this->get_expiration_time(); |
| 2682 | 2682 | |
| 2683 | - if ( $expiration && strtotime( 'NOW', current_time( 'timestamp' ) ) > $expiration ) { |
|
| 2683 | + if ($expiration && strtotime('NOW', current_time('timestamp')) > $expiration) { |
|
| 2684 | 2684 | $ret = true; |
| 2685 | 2685 | |
| 2686 | - if ( 'active' === $subscription_status || 'trialing' === $subscription_status ) { |
|
| 2686 | + if ('active' === $subscription_status || 'trialing' === $subscription_status) { |
|
| 2687 | 2687 | $this->expire_subscription(); |
| 2688 | 2688 | } |
| 2689 | 2689 | } |
| 2690 | 2690 | } |
| 2691 | 2691 | |
| 2692 | - return apply_filters( 'wpinv_subscription_is_expired', $ret, $this->ID, $this ); |
|
| 2692 | + return apply_filters('wpinv_subscription_is_expired', $ret, $this->ID, $this); |
|
| 2693 | 2693 | } |
| 2694 | 2694 | |
| 2695 | - public function get_new_expiration( $item_id = 0, $trial = true ) { |
|
| 2696 | - $item = new WPInv_Item( $item_id ); |
|
| 2695 | + public function get_new_expiration($item_id = 0, $trial = true) { |
|
| 2696 | + $item = new WPInv_Item($item_id); |
|
| 2697 | 2697 | $interval = $item->get_recurring_interval(); |
| 2698 | - $period = $item->get_recurring_period( true ); |
|
| 2698 | + $period = $item->get_recurring_period(true); |
|
| 2699 | 2699 | |
| 2700 | - $expiration_time = strtotime( '+' . $interval . ' ' . $period ); |
|
| 2700 | + $expiration_time = strtotime('+' . $interval . ' ' . $period); |
|
| 2701 | 2701 | |
| 2702 | - if ( $trial && $this->is_free_trial() && $item->has_free_trial() ) { |
|
| 2703 | - $expiration_time += ( wpinv_period_in_days( $item->get_trial_interval(), $item->get_trial_period() ) * DAY_IN_SECONDS ) ; |
|
| 2702 | + if ($trial && $this->is_free_trial() && $item->has_free_trial()) { |
|
| 2703 | + $expiration_time += (wpinv_period_in_days($item->get_trial_interval(), $item->get_trial_period()) * DAY_IN_SECONDS); |
|
| 2704 | 2704 | } |
| 2705 | 2705 | |
| 2706 | - return date_i18n( 'Y-m-d 23:59:59', $expiration_time ); |
|
| 2706 | + return date_i18n('Y-m-d 23:59:59', $expiration_time); |
|
| 2707 | 2707 | } |
| 2708 | 2708 | |
| 2709 | - public function get_subscription_data( $filed = '' ) { |
|
| 2710 | - $fields = array( 'item_id', 'status', 'period', 'initial_amount', 'recurring_amount', 'interval', 'bill_times', 'trial_period', 'trial_interval', 'expiration', 'profile_id', 'created', 'cancelled_on' ); |
|
| 2709 | + public function get_subscription_data($filed = '') { |
|
| 2710 | + $fields = array('item_id', 'status', 'period', 'initial_amount', 'recurring_amount', 'interval', 'bill_times', 'trial_period', 'trial_interval', 'expiration', 'profile_id', 'created', 'cancelled_on'); |
|
| 2711 | 2711 | |
| 2712 | 2712 | $subscription_meta = array(); |
| 2713 | - foreach ( $fields as $field ) { |
|
| 2714 | - $subscription_meta[ $field ] = $this->get_meta( '_wpinv_subscr_' . $field ); |
|
| 2713 | + foreach ($fields as $field) { |
|
| 2714 | + $subscription_meta[$field] = $this->get_meta('_wpinv_subscr_' . $field); |
|
| 2715 | 2715 | } |
| 2716 | 2716 | |
| 2717 | - $item = $this->get_recurring( true ); |
|
| 2717 | + $item = $this->get_recurring(true); |
|
| 2718 | 2718 | |
| 2719 | - if ( !empty( $item ) ) { |
|
| 2720 | - if ( empty( $subscription_meta['item_id'] ) ) { |
|
| 2719 | + if (!empty($item)) { |
|
| 2720 | + if (empty($subscription_meta['item_id'])) { |
|
| 2721 | 2721 | $subscription_meta['item_id'] = $item->ID; |
| 2722 | 2722 | } |
| 2723 | - if ( empty( $subscription_meta['period'] ) ) { |
|
| 2723 | + if (empty($subscription_meta['period'])) { |
|
| 2724 | 2724 | $subscription_meta['period'] = $item->get_recurring_period(); |
| 2725 | 2725 | } |
| 2726 | - if ( empty( $subscription_meta['interval'] ) ) { |
|
| 2726 | + if (empty($subscription_meta['interval'])) { |
|
| 2727 | 2727 | $subscription_meta['interval'] = $item->get_recurring_interval(); |
| 2728 | 2728 | } |
| 2729 | - if ( $item->has_free_trial() ) { |
|
| 2730 | - if ( empty( $subscription_meta['trial_period'] ) ) { |
|
| 2729 | + if ($item->has_free_trial()) { |
|
| 2730 | + if (empty($subscription_meta['trial_period'])) { |
|
| 2731 | 2731 | $subscription_meta['trial_period'] = $item->get_trial_period(); |
| 2732 | 2732 | } |
| 2733 | - if ( empty( $subscription_meta['trial_interval'] ) ) { |
|
| 2733 | + if (empty($subscription_meta['trial_interval'])) { |
|
| 2734 | 2734 | $subscription_meta['trial_interval'] = $item->get_trial_interval(); |
| 2735 | 2735 | } |
| 2736 | 2736 | } else { |
| 2737 | 2737 | $subscription_meta['trial_period'] = ''; |
| 2738 | 2738 | $subscription_meta['trial_interval'] = 0; |
| 2739 | 2739 | } |
| 2740 | - if ( !$subscription_meta['bill_times'] && $subscription_meta['bill_times'] !== 0 ) { |
|
| 2740 | + if (!$subscription_meta['bill_times'] && $subscription_meta['bill_times'] !== 0) { |
|
| 2741 | 2741 | $subscription_meta['bill_times'] = $item->get_recurring_limit(); |
| 2742 | 2742 | } |
| 2743 | - if ( $subscription_meta['initial_amount'] === '' || $subscription_meta['recurring_amount'] === '' ) { |
|
| 2744 | - $subscription_meta['initial_amount'] = wpinv_round_amount( $this->get_total() ); |
|
| 2745 | - $subscription_meta['recurring_amount'] = wpinv_round_amount( $this->get_recurring_details( 'total' ) ); |
|
| 2743 | + if ($subscription_meta['initial_amount'] === '' || $subscription_meta['recurring_amount'] === '') { |
|
| 2744 | + $subscription_meta['initial_amount'] = wpinv_round_amount($this->get_total()); |
|
| 2745 | + $subscription_meta['recurring_amount'] = wpinv_round_amount($this->get_recurring_details('total')); |
|
| 2746 | 2746 | } |
| 2747 | 2747 | } |
| 2748 | 2748 | |
| 2749 | - if ( $filed === '' ) { |
|
| 2750 | - return apply_filters( 'wpinv_get_invoice_subscription_data', $subscription_meta, $this ); |
|
| 2749 | + if ($filed === '') { |
|
| 2750 | + return apply_filters('wpinv_get_invoice_subscription_data', $subscription_meta, $this); |
|
| 2751 | 2751 | } |
| 2752 | 2752 | |
| 2753 | - $value = isset( $subscription_meta[$filed] ) ? $subscription_meta[$filed] : ''; |
|
| 2753 | + $value = isset($subscription_meta[$filed]) ? $subscription_meta[$filed] : ''; |
|
| 2754 | 2754 | |
| 2755 | - return apply_filters( 'wpinv_invoice_subscription_data_value', $value, $subscription_meta, $this ); |
|
| 2755 | + return apply_filters('wpinv_invoice_subscription_data_value', $value, $subscription_meta, $this); |
|
| 2756 | 2756 | } |
| 2757 | 2757 | |
| 2758 | 2758 | public function is_paid() { |
| 2759 | - if ( $this->has_status( array( 'publish', 'wpi-processing', 'wpi-renewal' ) ) ) { |
|
| 2759 | + if ($this->has_status(array('publish', 'wpi-processing', 'wpi-renewal'))) { |
|
| 2760 | 2760 | return true; |
| 2761 | 2761 | } |
| 2762 | 2762 | |
@@ -2766,15 +2766,15 @@ discard block |
||
| 2766 | 2766 | public function is_free() { |
| 2767 | 2767 | $is_free = false; |
| 2768 | 2768 | |
| 2769 | - if ( !( (float)wpinv_round_amount( $this->get_total() ) > 0 ) ) { |
|
| 2770 | - if ( $this->is_parent() && $this->is_recurring() ) { |
|
| 2771 | - $is_free = (float)wpinv_round_amount( $this->get_recurring_details( 'total' ) ) > 0 ? false : true; |
|
| 2769 | + if (!((float)wpinv_round_amount($this->get_total()) > 0)) { |
|
| 2770 | + if ($this->is_parent() && $this->is_recurring()) { |
|
| 2771 | + $is_free = (float)wpinv_round_amount($this->get_recurring_details('total')) > 0 ? false : true; |
|
| 2772 | 2772 | } else { |
| 2773 | 2773 | $is_free = true; |
| 2774 | 2774 | } |
| 2775 | 2775 | } |
| 2776 | 2776 | |
| 2777 | - return apply_filters( 'wpinv_invoice_is_free', $is_free, $this ); |
|
| 2777 | + return apply_filters('wpinv_invoice_is_free', $is_free, $this); |
|
| 2778 | 2778 | } |
| 2779 | 2779 | |
| 2780 | 2780 | public function has_vat() { |
@@ -2782,41 +2782,41 @@ discard block |
||
| 2782 | 2782 | |
| 2783 | 2783 | $requires_vat = false; |
| 2784 | 2784 | |
| 2785 | - if ( $this->country ) { |
|
| 2785 | + if ($this->country) { |
|
| 2786 | 2786 | $wpi_country = $this->country; |
| 2787 | 2787 | |
| 2788 | - $requires_vat = $wpinv_euvat->requires_vat( $requires_vat, $this->get_user_id(), $wpinv_euvat->invoice_has_digital_rule( $this ) ); |
|
| 2788 | + $requires_vat = $wpinv_euvat->requires_vat($requires_vat, $this->get_user_id(), $wpinv_euvat->invoice_has_digital_rule($this)); |
|
| 2789 | 2789 | } |
| 2790 | 2790 | |
| 2791 | - return apply_filters( 'wpinv_invoice_has_vat', $requires_vat, $this ); |
|
| 2791 | + return apply_filters('wpinv_invoice_has_vat', $requires_vat, $this); |
|
| 2792 | 2792 | } |
| 2793 | 2793 | |
| 2794 | 2794 | public function refresh_item_ids() { |
| 2795 | 2795 | $item_ids = array(); |
| 2796 | 2796 | |
| 2797 | - if ( !empty( $this->cart_details ) ) { |
|
| 2798 | - foreach ( $this->cart_details as $key => $item ) { |
|
| 2799 | - if ( !empty( $item['id'] ) ) { |
|
| 2797 | + if (!empty($this->cart_details)) { |
|
| 2798 | + foreach ($this->cart_details as $key => $item) { |
|
| 2799 | + if (!empty($item['id'])) { |
|
| 2800 | 2800 | $item_ids[] = $item['id']; |
| 2801 | 2801 | } |
| 2802 | 2802 | } |
| 2803 | 2803 | } |
| 2804 | 2804 | |
| 2805 | - $item_ids = !empty( $item_ids ) ? implode( ',', array_unique( $item_ids ) ) : ''; |
|
| 2805 | + $item_ids = !empty($item_ids) ? implode(',', array_unique($item_ids)) : ''; |
|
| 2806 | 2806 | |
| 2807 | - update_post_meta( $this->ID, '_wpinv_item_ids', $item_ids ); |
|
| 2807 | + update_post_meta($this->ID, '_wpinv_item_ids', $item_ids); |
|
| 2808 | 2808 | } |
| 2809 | 2809 | |
| 2810 | - public function get_invoice_quote_type( $post_id ) { |
|
| 2811 | - if ( empty( $post_id ) ) { |
|
| 2810 | + public function get_invoice_quote_type($post_id) { |
|
| 2811 | + if (empty($post_id)) { |
|
| 2812 | 2812 | return ''; |
| 2813 | 2813 | } |
| 2814 | 2814 | |
| 2815 | - $type = get_post_type( $post_id ); |
|
| 2815 | + $type = get_post_type($post_id); |
|
| 2816 | 2816 | |
| 2817 | - if ( 'wpi_invoice' === $type ) { |
|
| 2817 | + if ('wpi_invoice' === $type) { |
|
| 2818 | 2818 | $post_type = __('Invoice', 'invoicing'); |
| 2819 | - } else{ |
|
| 2819 | + } else { |
|
| 2820 | 2820 | $post_type = __('Quote', 'invoicing'); |
| 2821 | 2821 | } |
| 2822 | 2822 | |
@@ -130,7 +130,6 @@ |
||
| 130 | 130 | private function mostSpecificSubdivision() |
| 131 | 131 | { |
| 132 | 132 | return empty($this->subdivisions) ? |
| 133 | - new \GeoIp2\Record\Subdivision(array(), $this->locales) : |
|
| 134 | - end($this->subdivisions); |
|
| 133 | + new \GeoIp2\Record\Subdivision(array(), $this->locales) : end($this->subdivisions); |
|
| 135 | 134 | } |
| 136 | 135 | } |
@@ -5,12 +5,12 @@ |
||
| 5 | 5 | // @codingStandardsIgnoreFile |
| 6 | 6 | |
| 7 | 7 | /** |
| 8 | - * This interface exists to provide backwards compatibility with PHP 5.3 |
|
| 9 | - * |
|
| 10 | - * This should _not_ be used by any third-party code. |
|
| 11 | - * |
|
| 12 | - * @ignore |
|
| 13 | - */ |
|
| 8 | + * This interface exists to provide backwards compatibility with PHP 5.3 |
|
| 9 | + * |
|
| 10 | + * This should _not_ be used by any third-party code. |
|
| 11 | + * |
|
| 12 | + * @ignore |
|
| 13 | + */ |
|
| 14 | 14 | if (interface_exists('JsonSerializable')) { |
| 15 | 15 | interface JsonSerializable extends \JsonSerializable |
| 16 | 16 | { |
@@ -9,780 +9,780 @@ |
||
| 9 | 9 | * @author Roman Ožana <[email protected]> |
| 10 | 10 | */ |
| 11 | 11 | class Emogrifier { |
| 12 | - /** |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - const ENCODING = 'UTF-8'; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var integer |
|
| 19 | - */ |
|
| 20 | - const CACHE_KEY_CSS = 0; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var integer |
|
| 24 | - */ |
|
| 25 | - const CACHE_KEY_SELECTOR = 1; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @var integer |
|
| 29 | - */ |
|
| 30 | - const CACHE_KEY_XPATH = 2; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var integer |
|
| 34 | - */ |
|
| 35 | - const CACHE_KEY_CSS_DECLARATION_BLOCK = 3; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * for calculating nth-of-type and nth-child selectors. |
|
| 39 | - * |
|
| 40 | - * @var integer |
|
| 41 | - */ |
|
| 42 | - const INDEX = 0; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * for calculating nth-of-type and nth-child selectors. |
|
| 46 | - * |
|
| 47 | - * @var integer |
|
| 48 | - */ |
|
| 49 | - const MULTIPLIER = 1; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * @var string |
|
| 53 | - */ |
|
| 54 | - const ID_ATTRIBUTE_MATCHER = '/(\\w+)?\\#([\\w\\-]+)/'; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * @var string |
|
| 58 | - */ |
|
| 59 | - const CLASS_ATTRIBUTE_MATCHER = '/(\\w+|[\\*\\]])?((\\.[\\w\\-]+)+)/'; |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * @var string |
|
| 63 | - */ |
|
| 64 | - private $html = ''; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @var string |
|
| 68 | - */ |
|
| 69 | - private $css = ''; |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * @var array<string> |
|
| 73 | - */ |
|
| 74 | - private $unprocessableHtmlTags = array('wbr'); |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * @var array<array> |
|
| 78 | - */ |
|
| 79 | - private $caches = array( |
|
| 80 | - self::CACHE_KEY_CSS => array(), |
|
| 81 | - self::CACHE_KEY_SELECTOR => array(), |
|
| 82 | - self::CACHE_KEY_XPATH => array(), |
|
| 83 | - self::CACHE_KEY_CSS_DECLARATION_BLOCK => array(), |
|
| 84 | - ); |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * the visited nodes with the XPath paths as array keys. |
|
| 88 | - * |
|
| 89 | - * @var array<\DOMNode> |
|
| 90 | - */ |
|
| 91 | - private $visitedNodes = array(); |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * the styles to apply to the nodes with the XPath paths as array keys for the outer array and the attribute names/values. |
|
| 95 | - * as key/value pairs for the inner array. |
|
| 96 | - * |
|
| 97 | - * @var array<array><string> |
|
| 98 | - */ |
|
| 99 | - private $styleAttributesForNodes = array(); |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * This attribute applies to the case where you want to preserve your original text encoding. |
|
| 103 | - * |
|
| 104 | - * By default, emogrifier translates your text into HTML entities for two reasons: |
|
| 105 | - * |
|
| 106 | - * 1. Because of client incompatibilities, it is better practice to send out HTML entities rather than unicode over email. |
|
| 107 | - * |
|
| 108 | - * 2. It translates any illegal XML characters that DOMDocument cannot work with. |
|
| 109 | - * |
|
| 110 | - * If you would like to preserve your original encoding, set this attribute to TRUE. |
|
| 111 | - * |
|
| 112 | - * @var boolean |
|
| 113 | - */ |
|
| 114 | - public $preserveEncoding = false; |
|
| 115 | - |
|
| 116 | - public static $_media = ''; |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * The constructor. |
|
| 120 | - * |
|
| 121 | - * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
| 122 | - * @param string $css the CSS to merge, must be UTF-8-encoded |
|
| 123 | - */ |
|
| 124 | - public function __construct($html = '', $css = '') { |
|
| 125 | - $this->setHtml($html); |
|
| 126 | - $this->setCss($css); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - /** |
|
| 130 | - * The destructor. |
|
| 131 | - */ |
|
| 132 | - public function __destruct() { |
|
| 133 | - $this->purgeVisitedNodes(); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Sets the HTML to emogrify. |
|
| 138 | - * |
|
| 139 | - * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
| 140 | - */ |
|
| 141 | - public function setHtml($html = '') { |
|
| 142 | - $this->html = $html; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Sets the CSS to merge with the HTML. |
|
| 147 | - * |
|
| 148 | - * @param string $css the CSS to merge, must be UTF-8-encoded |
|
| 149 | - */ |
|
| 150 | - public function setCss($css = '') { |
|
| 151 | - $this->css = $css; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Clears all caches. |
|
| 156 | - */ |
|
| 157 | - private function clearAllCaches() { |
|
| 158 | - $this->clearCache(self::CACHE_KEY_CSS); |
|
| 159 | - $this->clearCache(self::CACHE_KEY_SELECTOR); |
|
| 160 | - $this->clearCache(self::CACHE_KEY_XPATH); |
|
| 161 | - $this->clearCache(self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Clears a single cache by key. |
|
| 166 | - * |
|
| 167 | - * @param integer $key the cache key, must be CACHE_KEY_CSS, CACHE_KEY_SELECTOR, CACHE_KEY_XPATH or CACHE_KEY_CSS_DECLARATION_BLOCK |
|
| 168 | - * |
|
| 169 | - * @throws InvalidArgumentException |
|
| 170 | - */ |
|
| 171 | - private function clearCache($key) { |
|
| 172 | - $allowedCacheKeys = array(self::CACHE_KEY_CSS, self::CACHE_KEY_SELECTOR, self::CACHE_KEY_XPATH, self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
| 173 | - if (!in_array($key, $allowedCacheKeys, true)) { |
|
| 174 | - throw new InvalidArgumentException('Invalid cache key: ' . $key, 1391822035); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - $this->caches[$key] = array(); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Purges the visited nodes. |
|
| 182 | - */ |
|
| 183 | - private function purgeVisitedNodes() { |
|
| 184 | - $this->visitedNodes = array(); |
|
| 185 | - $this->styleAttributesForNodes = array(); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Marks a tag for removal. |
|
| 190 | - * |
|
| 191 | - * There are some HTML tags that DOMDocument cannot process, and it will throw an error if it encounters them. |
|
| 192 | - * In particular, DOMDocument will complain if you try to use HTML5 tags in an XHTML document. |
|
| 193 | - * |
|
| 194 | - * Note: The tags will not be removed if they have any content. |
|
| 195 | - * |
|
| 196 | - * @param string $tagName the tag name, e.g., "p" |
|
| 197 | - */ |
|
| 198 | - public function addUnprocessableHtmlTag($tagName) { |
|
| 199 | - $this->unprocessableHtmlTags[] = $tagName; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Drops a tag from the removal list. |
|
| 204 | - * |
|
| 205 | - * @param string $tagName the tag name, e.g., "p" |
|
| 206 | - */ |
|
| 207 | - public function removeUnprocessableHtmlTag($tagName) { |
|
| 208 | - $key = array_search($tagName, $this->unprocessableHtmlTags, true); |
|
| 209 | - if ($key !== false) { |
|
| 210 | - unset($this->unprocessableHtmlTags[$key]); |
|
| 211 | - } |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - /** |
|
| 215 | - * Applies the CSS you submit to the HTML you submit. |
|
| 216 | - * |
|
| 217 | - * This method places the CSS inline. |
|
| 218 | - * |
|
| 219 | - * @return string |
|
| 220 | - * |
|
| 221 | - * @throws BadMethodCallException |
|
| 222 | - */ |
|
| 223 | - public function emogrify() { |
|
| 224 | - if ($this->html === '') { |
|
| 225 | - throw new BadMethodCallException('Please set some HTML first before calling emogrify.', 1390393096); |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - $xmlDocument = $this->createXmlDocument(); |
|
| 229 | - $xpath = new DOMXPath($xmlDocument); |
|
| 230 | - $this->clearAllCaches(); |
|
| 231 | - |
|
| 232 | - // before be begin processing the CSS file, parse the document and normalize all existing CSS attributes (changes 'DISPLAY: none' to 'display: none'); |
|
| 233 | - // we wouldn't have to do this if DOMXPath supported XPath 2.0. |
|
| 234 | - // also store a reference of nodes with existing inline styles so we don't overwrite them |
|
| 235 | - $this->purgeVisitedNodes(); |
|
| 236 | - |
|
| 237 | - $nodesWithStyleAttributes = $xpath->query('//*[@style]'); |
|
| 238 | - if ($nodesWithStyleAttributes !== false) { |
|
| 239 | - /** @var $nodeWithStyleAttribute DOMNode */ |
|
| 240 | - foreach ($nodesWithStyleAttributes as $node) { |
|
| 241 | - $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
| 242 | - |
|
| 243 | - // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
|
| 244 | - $nodePath = $node->getNodePath(); |
|
| 245 | - if (!isset($this->styleAttributesForNodes[$nodePath])) { |
|
| 246 | - $this->styleAttributesForNodes[$nodePath] = $this->parseCssDeclarationBlock($normalizedOriginalStyle); |
|
| 247 | - $this->visitedNodes[$nodePath] = $node; |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - $node->setAttribute('style', $normalizedOriginalStyle); |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - // grab any existing style blocks from the html and append them to the existing CSS |
|
| 255 | - // (these blocks should be appended so as to have precedence over conflicting styles in the existing CSS) |
|
| 256 | - $allCss = $this->css; |
|
| 257 | - |
|
| 258 | - $allCss .= $this->getCssFromAllStyleNodes($xpath); |
|
| 259 | - |
|
| 260 | - $cssParts = $this->splitCssAndMediaQuery($allCss); |
|
| 261 | - self::$_media = ''; // reset |
|
| 262 | - |
|
| 263 | - $cssKey = md5($cssParts['css']); |
|
| 264 | - if (!isset($this->caches[self::CACHE_KEY_CSS][$cssKey])) { |
|
| 265 | - // process the CSS file for selectors and definitions |
|
| 266 | - preg_match_all('/(?:^|[\\s^{}]*)([^{]+){([^}]*)}/mis', $cssParts['css'], $matches, PREG_SET_ORDER); |
|
| 267 | - |
|
| 268 | - $allSelectors = array(); |
|
| 269 | - foreach ($matches as $key => $selectorString) { |
|
| 270 | - // if there is a blank definition, skip |
|
| 271 | - if (!strlen(trim($selectorString[2]))) { |
|
| 272 | - continue; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - // else split by commas and duplicate attributes so we can sort by selector precedence |
|
| 276 | - $selectors = explode(',', $selectorString[1]); |
|
| 277 | - foreach ($selectors as $selector) { |
|
| 278 | - // don't process pseudo-elements and behavioral (dynamic) pseudo-classes; ONLY allow structural pseudo-classes |
|
| 279 | - if (strpos($selector, ':') !== false && !preg_match('/:\\S+\\-(child|type)\\(/i', $selector)) { |
|
| 280 | - continue; |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - $allSelectors[] = array('selector' => trim($selector), |
|
| 284 | - 'attributes' => trim($selectorString[2]), |
|
| 285 | - // keep track of where it appears in the file, since order is important |
|
| 286 | - 'line' => $key, |
|
| 287 | - ); |
|
| 288 | - } |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - // now sort the selectors by precedence |
|
| 292 | - usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
| 293 | - |
|
| 294 | - $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - foreach ($this->caches[self::CACHE_KEY_CSS][$cssKey] as $value) { |
|
| 298 | - // query the body for the xpath selector |
|
| 299 | - $nodesMatchingCssSelectors = $xpath->query($this->translateCssToXpath($value['selector'])); |
|
| 300 | - |
|
| 301 | - /** @var $node \DOMNode */ |
|
| 302 | - foreach ($nodesMatchingCssSelectors as $node) { |
|
| 303 | - // if it has a style attribute, get it, process it, and append (overwrite) new stuff |
|
| 304 | - if ($node->hasAttribute('style')) { |
|
| 305 | - // break it up into an associative array |
|
| 306 | - $oldStyleDeclarations = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
| 307 | - } else { |
|
| 308 | - $oldStyleDeclarations = array(); |
|
| 309 | - } |
|
| 310 | - $newStyleDeclarations = $this->parseCssDeclarationBlock($value['attributes']); |
|
| 311 | - $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($oldStyleDeclarations, $newStyleDeclarations)); |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - // now iterate through the nodes that contained inline styles in the original HTML |
|
| 316 | - foreach ($this->styleAttributesForNodes as $nodePath => $styleAttributesForNode) { |
|
| 317 | - $node = $this->visitedNodes[$nodePath]; |
|
| 318 | - $currentStyleAttributes = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
| 319 | - $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($currentStyleAttributes, $styleAttributesForNode)); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - // This removes styles from your email that contain display:none. |
|
| 323 | - // We need to look for display:none, but we need to do a case-insensitive search. Since DOMDocument only supports XPath 1.0, |
|
| 324 | - // lower-case() isn't available to us. We've thus far only set attributes to lowercase, not attribute values. Consequently, we need |
|
| 325 | - // to translate() the letters that would be in 'NONE' ("NOE") to lowercase. |
|
| 326 | - $nodesWithStyleDisplayNone = $xpath->query('//*[contains(translate(translate(@style," ",""),"NOE","noe"),"display:none")]'); |
|
| 327 | - // The checks on parentNode and is_callable below ensure that if we've deleted the parent node, |
|
| 328 | - // we don't try to call removeChild on a nonexistent child node |
|
| 329 | - if ($nodesWithStyleDisplayNone->length > 0) { |
|
| 330 | - /** @var $node \DOMNode */ |
|
| 331 | - foreach ($nodesWithStyleDisplayNone as $node) { |
|
| 332 | - if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
| 333 | - $node->parentNode->removeChild($node); |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument); |
|
| 339 | - |
|
| 340 | - if ($this->preserveEncoding) { |
|
| 341 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
| 342 | - return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
| 343 | - } else { |
|
| 344 | - return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
|
| 345 | - } |
|
| 346 | - } else { |
|
| 347 | - return $xmlDocument->saveHTML(); |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - public function strtolower(array $m) { |
|
| 352 | - return strtolower($m[0]); |
|
| 353 | - } |
|
| 354 | - |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * This method merges old or existing name/value array with new name/value array. |
|
| 358 | - * and then generates a string of the combined style suitable for placing inline. |
|
| 359 | - * This becomes the single point for CSS string generation allowing for consistent. |
|
| 360 | - * CSS output no matter where the CSS originally came from. |
|
| 361 | - * @param array $oldStyles |
|
| 362 | - * @param array $newStyles |
|
| 363 | - * @return string |
|
| 364 | - */ |
|
| 365 | - private function generateStyleStringFromDeclarationsArrays(array $oldStyles, array $newStyles) { |
|
| 366 | - $combinedStyles = array_merge($oldStyles, $newStyles); |
|
| 367 | - $style = ''; |
|
| 368 | - foreach ($combinedStyles as $attributeName => $attributeValue) { |
|
| 369 | - $style .= (strtolower(trim($attributeName)) . ': ' . trim($attributeValue) . '; '); |
|
| 370 | - } |
|
| 371 | - return trim($style); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Copies the media part from CSS array parts to $xmlDocument. |
|
| 377 | - * |
|
| 378 | - * @param array $cssParts |
|
| 379 | - * @param DOMDocument $xmlDocument |
|
| 380 | - */ |
|
| 381 | - public function copyCssWithMediaToStyleNode(array $cssParts, DOMDocument $xmlDocument) { |
|
| 382 | - if (isset($cssParts['media']) && $cssParts['media'] !== '') { |
|
| 383 | - $this->addStyleElementToDocument($xmlDocument, $cssParts['media']); |
|
| 384 | - } |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Returns CSS content. |
|
| 389 | - * |
|
| 390 | - * @param DOMXPath $xpath |
|
| 391 | - * @return string |
|
| 392 | - */ |
|
| 393 | - private function getCssFromAllStyleNodes(DOMXPath $xpath) { |
|
| 394 | - $styleNodes = $xpath->query('//style'); |
|
| 395 | - |
|
| 396 | - if ($styleNodes === false) { |
|
| 397 | - return ''; |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - $css = ''; |
|
| 401 | - /** @var $styleNode DOMNode */ |
|
| 402 | - foreach ($styleNodes as $styleNode) { |
|
| 403 | - $css .= "\n\n" . $styleNode->nodeValue; |
|
| 404 | - $styleNode->parentNode->removeChild($styleNode); |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - return $css; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * Adds a style element with $css to $document. |
|
| 412 | - * |
|
| 413 | - * @param DOMDocument $document |
|
| 414 | - * @param string $css |
|
| 415 | - */ |
|
| 416 | - private function addStyleElementToDocument(DOMDocument $document, $css) { |
|
| 417 | - $styleElement = $document->createElement('style', $css); |
|
| 418 | - $styleAttribute = $document->createAttribute('type'); |
|
| 419 | - $styleAttribute->value = 'text/css'; |
|
| 420 | - $styleElement->appendChild($styleAttribute); |
|
| 421 | - |
|
| 422 | - $head = $this->getOrCreateHeadElement($document); |
|
| 423 | - $head->appendChild($styleElement); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Returns the existing or creates a new head element in $document. |
|
| 428 | - * |
|
| 429 | - * @param DOMDocument $document |
|
| 430 | - * @return DOMNode the head element |
|
| 431 | - */ |
|
| 432 | - private function getOrCreateHeadElement(DOMDocument $document) { |
|
| 433 | - $head = $document->getElementsByTagName('head')->item(0); |
|
| 434 | - |
|
| 435 | - if ($head === null) { |
|
| 436 | - $head = $document->createElement('head'); |
|
| 437 | - $html = $document->getElementsByTagName('html')->item(0); |
|
| 438 | - $html->insertBefore($head, $document->getElementsByTagName('body')->item(0)); |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - return $head; |
|
| 442 | - } |
|
| 443 | - |
|
| 444 | - /** |
|
| 445 | - * Splits input CSS code to an array where: |
|
| 446 | - * |
|
| 447 | - * - key "css" will be contains clean CSS code. |
|
| 448 | - * - key "media" will be contains all valuable media queries. |
|
| 449 | - * |
|
| 450 | - * Example: |
|
| 451 | - * |
|
| 452 | - * The CSS code. |
|
| 453 | - * |
|
| 454 | - * "@import "file.css"; h1 { color:red; } @media { h1 {}} @media tv { h1 {}}" |
|
| 455 | - * |
|
| 456 | - * will be parsed into the following array: |
|
| 457 | - * |
|
| 458 | - * "css" => "h1 { color:red; }" |
|
| 459 | - * "media" => "@media { h1 {}}" |
|
| 460 | - * |
|
| 461 | - * @param string $css |
|
| 462 | - * @return array |
|
| 463 | - */ |
|
| 464 | - private function splitCssAndMediaQuery($css) { |
|
| 465 | - $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
| 466 | - |
|
| 467 | - // filter the CSS |
|
| 468 | - $search = array( |
|
| 469 | - // get rid of css comment code |
|
| 470 | - '/\\/\\*.*\\*\\//sU', |
|
| 471 | - // strip out any import directives |
|
| 472 | - '/^\\s*@import\\s[^;]+;/misU', |
|
| 473 | - // strip remains media enclosures |
|
| 474 | - '/^\\s*@media\\s[^{]+{(.*)}\\s*}\\s/misU', |
|
| 475 | - ); |
|
| 476 | - |
|
| 477 | - $replace = array( |
|
| 478 | - '', |
|
| 479 | - '', |
|
| 480 | - '', |
|
| 481 | - ); |
|
| 482 | - |
|
| 483 | - // clean CSS before output |
|
| 484 | - $css = preg_replace($search, $replace, $css); |
|
| 485 | - |
|
| 486 | - return array('css' => $css, 'media' => self::$_media); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - private function _media_concat( $matches ) { |
|
| 490 | - self::$_media .= $matches[0]; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - /** |
|
| 494 | - * Creates a DOMDocument instance with the current HTML. |
|
| 495 | - * |
|
| 496 | - * @return DOMDocument |
|
| 497 | - */ |
|
| 498 | - private function createXmlDocument() { |
|
| 499 | - $xmlDocument = new DOMDocument; |
|
| 500 | - $xmlDocument->encoding = self::ENCODING; |
|
| 501 | - $xmlDocument->strictErrorChecking = false; |
|
| 502 | - $xmlDocument->formatOutput = true; |
|
| 503 | - $libXmlState = libxml_use_internal_errors(true); |
|
| 504 | - $xmlDocument->loadHTML($this->getUnifiedHtml()); |
|
| 505 | - libxml_clear_errors(); |
|
| 506 | - libxml_use_internal_errors($libXmlState); |
|
| 507 | - $xmlDocument->normalizeDocument(); |
|
| 508 | - |
|
| 509 | - return $xmlDocument; |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - /** |
|
| 513 | - * Returns the HTML with the non-ASCII characters converts into HTML entities and the unprocessable HTML tags removed. |
|
| 514 | - * |
|
| 515 | - * @return string the unified HTML |
|
| 516 | - * |
|
| 517 | - * @throws BadMethodCallException |
|
| 518 | - */ |
|
| 519 | - private function getUnifiedHtml() { |
|
| 520 | - if (!empty($this->unprocessableHtmlTags)) { |
|
| 521 | - $unprocessableHtmlTags = implode('|', $this->unprocessableHtmlTags); |
|
| 522 | - $bodyWithoutUnprocessableTags = preg_replace('/<\\/?(' . $unprocessableHtmlTags . ')[^>]*>/i', '', $this->html); |
|
| 523 | - } else { |
|
| 524 | - $bodyWithoutUnprocessableTags = $this->html; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
| 528 | - return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
| 529 | - } else { |
|
| 530 | - return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
|
| 531 | - } |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - /** |
|
| 535 | - * @param array $a |
|
| 536 | - * @param array $b |
|
| 537 | - * |
|
| 538 | - * @return integer |
|
| 539 | - */ |
|
| 540 | - private function sortBySelectorPrecedence(array $a, array $b) { |
|
| 541 | - $precedenceA = $this->getCssSelectorPrecedence($a['selector']); |
|
| 542 | - $precedenceB = $this->getCssSelectorPrecedence($b['selector']); |
|
| 543 | - |
|
| 544 | - // We want these sorted in ascending order so selectors with lesser precedence get processed first and |
|
| 545 | - // selectors with greater precedence get sorted last. |
|
| 546 | - // The parenthesis around the -1 are necessary to avoid a PHP_CodeSniffer warning about missing spaces around |
|
| 547 | - // arithmetic operators. |
|
| 548 | - // @see http://forge.typo3.org/issues/55605 |
|
| 549 | - $precedenceForEquals = ($a['line'] < $b['line'] ? (-1) : 1); |
|
| 550 | - $precedenceForNotEquals = ($precedenceA < $precedenceB ? (-1) : 1); |
|
| 551 | - return ($precedenceA === $precedenceB) ? $precedenceForEquals : $precedenceForNotEquals; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * @param string $selector |
|
| 556 | - * |
|
| 557 | - * @return integer |
|
| 558 | - */ |
|
| 559 | - private function getCssSelectorPrecedence($selector) { |
|
| 560 | - $selectorKey = md5($selector); |
|
| 561 | - if (!isset($this->caches[self::CACHE_KEY_SELECTOR][$selectorKey])) { |
|
| 562 | - $precedence = 0; |
|
| 563 | - $value = 100; |
|
| 564 | - // ids: worth 100, classes: worth 10, elements: worth 1 |
|
| 565 | - $search = array('\\#','\\.',''); |
|
| 566 | - |
|
| 567 | - foreach ($search as $s) { |
|
| 568 | - if (trim($selector == '')) { |
|
| 569 | - break; |
|
| 570 | - } |
|
| 571 | - $number = 0; |
|
| 572 | - $selector = preg_replace('/' . $s . '\\w+/', '', $selector, -1, $number); |
|
| 573 | - $precedence += ($value * $number); |
|
| 574 | - $value /= 10; |
|
| 575 | - } |
|
| 576 | - $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey] = $precedence; |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - return $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey]; |
|
| 580 | - } |
|
| 581 | - |
|
| 582 | - /** |
|
| 583 | - * Right now, we support all CSS 1 selectors and most CSS2/3 selectors. |
|
| 584 | - * |
|
| 585 | - * @see http://plasmasturm.org/log/444/ |
|
| 586 | - * |
|
| 587 | - * @param string $paramCssSelector |
|
| 588 | - * |
|
| 589 | - * @return string |
|
| 590 | - */ |
|
| 591 | - private function translateCssToXpath($paramCssSelector) { |
|
| 592 | - $cssSelector = ' ' . $paramCssSelector . ' '; |
|
| 593 | - $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
| 594 | - $cssSelector = trim($cssSelector); |
|
| 595 | - $xpathKey = md5($cssSelector); |
|
| 596 | - if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
|
| 597 | - // returns an Xpath selector |
|
| 598 | - $search = array( |
|
| 599 | - // Matches any element that is a child of parent. |
|
| 600 | - '/\\s+>\\s+/', |
|
| 601 | - // Matches any element that is an adjacent sibling. |
|
| 602 | - '/\\s+\\+\\s+/', |
|
| 603 | - // Matches any element that is a descendant of an parent element element. |
|
| 604 | - '/\\s+/', |
|
| 605 | - // first-child pseudo-selector |
|
| 606 | - '/([^\\/]+):first-child/i', |
|
| 607 | - // last-child pseudo-selector |
|
| 608 | - '/([^\\/]+):last-child/i', |
|
| 609 | - // Matches attribute only selector |
|
| 610 | - '/^\\[(\\w+)\\]/', |
|
| 611 | - // Matches element with attribute |
|
| 612 | - '/(\\w)\\[(\\w+)\\]/', |
|
| 613 | - // Matches element with EXACT attribute |
|
| 614 | - '/(\\w)\\[(\\w+)\\=[\'"]?(\\w+)[\'"]?\\]/', |
|
| 615 | - ); |
|
| 616 | - $replace = array( |
|
| 617 | - '/', |
|
| 618 | - '/following-sibling::*[1]/self::', |
|
| 619 | - '//', |
|
| 620 | - '*[1]/self::\\1', |
|
| 621 | - '*[last()]/self::\\1', |
|
| 622 | - '*[@\\1]', |
|
| 623 | - '\\1[@\\2]', |
|
| 624 | - '\\1[@\\2="\\3"]', |
|
| 625 | - ); |
|
| 626 | - |
|
| 627 | - $cssSelector = '//' . preg_replace($search, $replace, $cssSelector); |
|
| 628 | - |
|
| 629 | - $cssSelector = preg_replace_callback(self::ID_ATTRIBUTE_MATCHER, array($this, 'matchIdAttributes'), $cssSelector); |
|
| 630 | - $cssSelector = preg_replace_callback(self::CLASS_ATTRIBUTE_MATCHER, array($this, 'matchClassAttributes'), $cssSelector); |
|
| 631 | - |
|
| 632 | - // Advanced selectors are going to require a bit more advanced emogrification. |
|
| 633 | - // When we required PHP 5.3, we could do this with closures. |
|
| 634 | - $cssSelector = preg_replace_callback( |
|
| 635 | - '/([^\\/]+):nth-child\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
| 636 | - array($this, 'translateNthChild'), $cssSelector |
|
| 637 | - ); |
|
| 638 | - $cssSelector = preg_replace_callback( |
|
| 639 | - '/([^\\/]+):nth-of-type\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
| 640 | - array($this, 'translateNthOfType'), $cssSelector |
|
| 641 | - ); |
|
| 642 | - |
|
| 643 | - $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey] = $cssSelector; |
|
| 644 | - } |
|
| 645 | - return $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey]; |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - /** |
|
| 649 | - * @param array $match |
|
| 650 | - * |
|
| 651 | - * @return string |
|
| 652 | - */ |
|
| 653 | - private function matchIdAttributes(array $match) { |
|
| 654 | - return (strlen($match[1]) ? $match[1] : '*') . '[@id="' . $match[2] . '"]'; |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - /** |
|
| 658 | - * @param array $match |
|
| 659 | - * |
|
| 660 | - * @return string |
|
| 661 | - */ |
|
| 662 | - private function matchClassAttributes(array $match) { |
|
| 663 | - return (strlen($match[1]) ? $match[1] : '*') . '[contains(concat(" ",@class," "),concat(" ","' . |
|
| 664 | - implode( |
|
| 665 | - '"," "))][contains(concat(" ",@class," "),concat(" ","', |
|
| 666 | - explode('.', substr($match[2], 1)) |
|
| 667 | - ) . '"," "))]'; |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * @param array $match |
|
| 672 | - * |
|
| 673 | - * @return string |
|
| 674 | - */ |
|
| 675 | - private function translateNthChild(array $match) { |
|
| 676 | - $result = $this->parseNth($match); |
|
| 677 | - |
|
| 678 | - if (isset($result[self::MULTIPLIER])) { |
|
| 679 | - if ($result[self::MULTIPLIER] < 0) { |
|
| 680 | - $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
| 681 | - return sprintf('*[(last() - position()) mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
| 682 | - } else { |
|
| 683 | - return sprintf('*[position() mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
| 684 | - } |
|
| 685 | - } else { |
|
| 686 | - return sprintf('*[%u]/self::%s', $result[self::INDEX], $match[1]); |
|
| 687 | - } |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * @param array $match |
|
| 692 | - * |
|
| 693 | - * @return string |
|
| 694 | - */ |
|
| 695 | - private function translateNthOfType(array $match) { |
|
| 696 | - $result = $this->parseNth($match); |
|
| 697 | - |
|
| 698 | - if (isset($result[self::MULTIPLIER])) { |
|
| 699 | - if ($result[self::MULTIPLIER] < 0) { |
|
| 700 | - $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
| 701 | - return sprintf('%s[(last() - position()) mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
| 702 | - } else { |
|
| 703 | - return sprintf('%s[position() mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
| 704 | - } |
|
| 705 | - } else { |
|
| 706 | - return sprintf('%s[%u]', $match[1], $result[self::INDEX]); |
|
| 707 | - } |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - /** |
|
| 711 | - * @param array $match |
|
| 712 | - * |
|
| 713 | - * @return array |
|
| 714 | - */ |
|
| 715 | - private function parseNth(array $match) { |
|
| 716 | - if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
| 717 | - $index = strtolower($match[2]) == 'even' ? 0 : 1; |
|
| 718 | - return array(self::MULTIPLIER => 2, self::INDEX => $index); |
|
| 719 | - } elseif (stripos($match[2], 'n') === false) { |
|
| 720 | - // if there is a multiplier |
|
| 721 | - $index = intval(str_replace(' ', '', $match[2])); |
|
| 722 | - return array(self::INDEX => $index); |
|
| 723 | - } else { |
|
| 724 | - if (isset($match[3])) { |
|
| 725 | - $multipleTerm = str_replace($match[3], '', $match[2]); |
|
| 726 | - $index = intval(str_replace(' ', '', $match[3])); |
|
| 727 | - } else { |
|
| 728 | - $multipleTerm = $match[2]; |
|
| 729 | - $index = 0; |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - $multiplier = str_ireplace('n', '', $multipleTerm); |
|
| 733 | - |
|
| 734 | - if (!strlen($multiplier)) { |
|
| 735 | - $multiplier = 1; |
|
| 736 | - } elseif ($multiplier == 0) { |
|
| 737 | - return array(self::INDEX => $index); |
|
| 738 | - } else { |
|
| 739 | - $multiplier = intval($multiplier); |
|
| 740 | - } |
|
| 741 | - |
|
| 742 | - while ($index < 0) { |
|
| 743 | - $index += abs($multiplier); |
|
| 744 | - } |
|
| 745 | - |
|
| 746 | - return array(self::MULTIPLIER => $multiplier, self::INDEX => $index); |
|
| 747 | - } |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - /** |
|
| 751 | - * Parses a CSS declaration block into property name/value pairs. |
|
| 752 | - * |
|
| 753 | - * Example: |
|
| 754 | - * |
|
| 755 | - * The declaration block. |
|
| 756 | - * |
|
| 757 | - * "color: #000; font-weight: bold;". |
|
| 758 | - * |
|
| 759 | - * will be parsed into the following array: |
|
| 760 | - * |
|
| 761 | - * "color" => "#000" |
|
| 762 | - * "font-weight" => "bold" |
|
| 763 | - * |
|
| 764 | - * @param string $cssDeclarationBlock the CSS declaration block without the curly braces, may be empty |
|
| 765 | - * |
|
| 766 | - * @return array the CSS declarations with the property names as array keys and the property values as array values |
|
| 767 | - */ |
|
| 768 | - private function parseCssDeclarationBlock($cssDeclarationBlock) { |
|
| 769 | - if (isset($this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock])) { |
|
| 770 | - return $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock]; |
|
| 771 | - } |
|
| 772 | - |
|
| 773 | - $properties = array(); |
|
| 774 | - $declarations = explode(';', $cssDeclarationBlock); |
|
| 775 | - foreach ($declarations as $declaration) { |
|
| 776 | - $matches = array(); |
|
| 777 | - if (!preg_match('/ *([A-Za-z\\-]+) *: *([^;]+) */', $declaration, $matches)) { |
|
| 778 | - continue; |
|
| 779 | - } |
|
| 780 | - $propertyName = strtolower($matches[1]); |
|
| 781 | - $propertyValue = $matches[2]; |
|
| 782 | - $properties[$propertyName] = $propertyValue; |
|
| 783 | - } |
|
| 784 | - $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock] = $properties; |
|
| 785 | - |
|
| 786 | - return $properties; |
|
| 787 | - } |
|
| 12 | + /** |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + const ENCODING = 'UTF-8'; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var integer |
|
| 19 | + */ |
|
| 20 | + const CACHE_KEY_CSS = 0; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var integer |
|
| 24 | + */ |
|
| 25 | + const CACHE_KEY_SELECTOR = 1; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @var integer |
|
| 29 | + */ |
|
| 30 | + const CACHE_KEY_XPATH = 2; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var integer |
|
| 34 | + */ |
|
| 35 | + const CACHE_KEY_CSS_DECLARATION_BLOCK = 3; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * for calculating nth-of-type and nth-child selectors. |
|
| 39 | + * |
|
| 40 | + * @var integer |
|
| 41 | + */ |
|
| 42 | + const INDEX = 0; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * for calculating nth-of-type and nth-child selectors. |
|
| 46 | + * |
|
| 47 | + * @var integer |
|
| 48 | + */ |
|
| 49 | + const MULTIPLIER = 1; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * @var string |
|
| 53 | + */ |
|
| 54 | + const ID_ATTRIBUTE_MATCHER = '/(\\w+)?\\#([\\w\\-]+)/'; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * @var string |
|
| 58 | + */ |
|
| 59 | + const CLASS_ATTRIBUTE_MATCHER = '/(\\w+|[\\*\\]])?((\\.[\\w\\-]+)+)/'; |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * @var string |
|
| 63 | + */ |
|
| 64 | + private $html = ''; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @var string |
|
| 68 | + */ |
|
| 69 | + private $css = ''; |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * @var array<string> |
|
| 73 | + */ |
|
| 74 | + private $unprocessableHtmlTags = array('wbr'); |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * @var array<array> |
|
| 78 | + */ |
|
| 79 | + private $caches = array( |
|
| 80 | + self::CACHE_KEY_CSS => array(), |
|
| 81 | + self::CACHE_KEY_SELECTOR => array(), |
|
| 82 | + self::CACHE_KEY_XPATH => array(), |
|
| 83 | + self::CACHE_KEY_CSS_DECLARATION_BLOCK => array(), |
|
| 84 | + ); |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * the visited nodes with the XPath paths as array keys. |
|
| 88 | + * |
|
| 89 | + * @var array<\DOMNode> |
|
| 90 | + */ |
|
| 91 | + private $visitedNodes = array(); |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * the styles to apply to the nodes with the XPath paths as array keys for the outer array and the attribute names/values. |
|
| 95 | + * as key/value pairs for the inner array. |
|
| 96 | + * |
|
| 97 | + * @var array<array><string> |
|
| 98 | + */ |
|
| 99 | + private $styleAttributesForNodes = array(); |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * This attribute applies to the case where you want to preserve your original text encoding. |
|
| 103 | + * |
|
| 104 | + * By default, emogrifier translates your text into HTML entities for two reasons: |
|
| 105 | + * |
|
| 106 | + * 1. Because of client incompatibilities, it is better practice to send out HTML entities rather than unicode over email. |
|
| 107 | + * |
|
| 108 | + * 2. It translates any illegal XML characters that DOMDocument cannot work with. |
|
| 109 | + * |
|
| 110 | + * If you would like to preserve your original encoding, set this attribute to TRUE. |
|
| 111 | + * |
|
| 112 | + * @var boolean |
|
| 113 | + */ |
|
| 114 | + public $preserveEncoding = false; |
|
| 115 | + |
|
| 116 | + public static $_media = ''; |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * The constructor. |
|
| 120 | + * |
|
| 121 | + * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
| 122 | + * @param string $css the CSS to merge, must be UTF-8-encoded |
|
| 123 | + */ |
|
| 124 | + public function __construct($html = '', $css = '') { |
|
| 125 | + $this->setHtml($html); |
|
| 126 | + $this->setCss($css); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + /** |
|
| 130 | + * The destructor. |
|
| 131 | + */ |
|
| 132 | + public function __destruct() { |
|
| 133 | + $this->purgeVisitedNodes(); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Sets the HTML to emogrify. |
|
| 138 | + * |
|
| 139 | + * @param string $html the HTML to emogrify, must be UTF-8-encoded |
|
| 140 | + */ |
|
| 141 | + public function setHtml($html = '') { |
|
| 142 | + $this->html = $html; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Sets the CSS to merge with the HTML. |
|
| 147 | + * |
|
| 148 | + * @param string $css the CSS to merge, must be UTF-8-encoded |
|
| 149 | + */ |
|
| 150 | + public function setCss($css = '') { |
|
| 151 | + $this->css = $css; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Clears all caches. |
|
| 156 | + */ |
|
| 157 | + private function clearAllCaches() { |
|
| 158 | + $this->clearCache(self::CACHE_KEY_CSS); |
|
| 159 | + $this->clearCache(self::CACHE_KEY_SELECTOR); |
|
| 160 | + $this->clearCache(self::CACHE_KEY_XPATH); |
|
| 161 | + $this->clearCache(self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Clears a single cache by key. |
|
| 166 | + * |
|
| 167 | + * @param integer $key the cache key, must be CACHE_KEY_CSS, CACHE_KEY_SELECTOR, CACHE_KEY_XPATH or CACHE_KEY_CSS_DECLARATION_BLOCK |
|
| 168 | + * |
|
| 169 | + * @throws InvalidArgumentException |
|
| 170 | + */ |
|
| 171 | + private function clearCache($key) { |
|
| 172 | + $allowedCacheKeys = array(self::CACHE_KEY_CSS, self::CACHE_KEY_SELECTOR, self::CACHE_KEY_XPATH, self::CACHE_KEY_CSS_DECLARATION_BLOCK); |
|
| 173 | + if (!in_array($key, $allowedCacheKeys, true)) { |
|
| 174 | + throw new InvalidArgumentException('Invalid cache key: ' . $key, 1391822035); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + $this->caches[$key] = array(); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Purges the visited nodes. |
|
| 182 | + */ |
|
| 183 | + private function purgeVisitedNodes() { |
|
| 184 | + $this->visitedNodes = array(); |
|
| 185 | + $this->styleAttributesForNodes = array(); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Marks a tag for removal. |
|
| 190 | + * |
|
| 191 | + * There are some HTML tags that DOMDocument cannot process, and it will throw an error if it encounters them. |
|
| 192 | + * In particular, DOMDocument will complain if you try to use HTML5 tags in an XHTML document. |
|
| 193 | + * |
|
| 194 | + * Note: The tags will not be removed if they have any content. |
|
| 195 | + * |
|
| 196 | + * @param string $tagName the tag name, e.g., "p" |
|
| 197 | + */ |
|
| 198 | + public function addUnprocessableHtmlTag($tagName) { |
|
| 199 | + $this->unprocessableHtmlTags[] = $tagName; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Drops a tag from the removal list. |
|
| 204 | + * |
|
| 205 | + * @param string $tagName the tag name, e.g., "p" |
|
| 206 | + */ |
|
| 207 | + public function removeUnprocessableHtmlTag($tagName) { |
|
| 208 | + $key = array_search($tagName, $this->unprocessableHtmlTags, true); |
|
| 209 | + if ($key !== false) { |
|
| 210 | + unset($this->unprocessableHtmlTags[$key]); |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + /** |
|
| 215 | + * Applies the CSS you submit to the HTML you submit. |
|
| 216 | + * |
|
| 217 | + * This method places the CSS inline. |
|
| 218 | + * |
|
| 219 | + * @return string |
|
| 220 | + * |
|
| 221 | + * @throws BadMethodCallException |
|
| 222 | + */ |
|
| 223 | + public function emogrify() { |
|
| 224 | + if ($this->html === '') { |
|
| 225 | + throw new BadMethodCallException('Please set some HTML first before calling emogrify.', 1390393096); |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + $xmlDocument = $this->createXmlDocument(); |
|
| 229 | + $xpath = new DOMXPath($xmlDocument); |
|
| 230 | + $this->clearAllCaches(); |
|
| 231 | + |
|
| 232 | + // before be begin processing the CSS file, parse the document and normalize all existing CSS attributes (changes 'DISPLAY: none' to 'display: none'); |
|
| 233 | + // we wouldn't have to do this if DOMXPath supported XPath 2.0. |
|
| 234 | + // also store a reference of nodes with existing inline styles so we don't overwrite them |
|
| 235 | + $this->purgeVisitedNodes(); |
|
| 236 | + |
|
| 237 | + $nodesWithStyleAttributes = $xpath->query('//*[@style]'); |
|
| 238 | + if ($nodesWithStyleAttributes !== false) { |
|
| 239 | + /** @var $nodeWithStyleAttribute DOMNode */ |
|
| 240 | + foreach ($nodesWithStyleAttributes as $node) { |
|
| 241 | + $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
| 242 | + |
|
| 243 | + // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
|
| 244 | + $nodePath = $node->getNodePath(); |
|
| 245 | + if (!isset($this->styleAttributesForNodes[$nodePath])) { |
|
| 246 | + $this->styleAttributesForNodes[$nodePath] = $this->parseCssDeclarationBlock($normalizedOriginalStyle); |
|
| 247 | + $this->visitedNodes[$nodePath] = $node; |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + $node->setAttribute('style', $normalizedOriginalStyle); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + // grab any existing style blocks from the html and append them to the existing CSS |
|
| 255 | + // (these blocks should be appended so as to have precedence over conflicting styles in the existing CSS) |
|
| 256 | + $allCss = $this->css; |
|
| 257 | + |
|
| 258 | + $allCss .= $this->getCssFromAllStyleNodes($xpath); |
|
| 259 | + |
|
| 260 | + $cssParts = $this->splitCssAndMediaQuery($allCss); |
|
| 261 | + self::$_media = ''; // reset |
|
| 262 | + |
|
| 263 | + $cssKey = md5($cssParts['css']); |
|
| 264 | + if (!isset($this->caches[self::CACHE_KEY_CSS][$cssKey])) { |
|
| 265 | + // process the CSS file for selectors and definitions |
|
| 266 | + preg_match_all('/(?:^|[\\s^{}]*)([^{]+){([^}]*)}/mis', $cssParts['css'], $matches, PREG_SET_ORDER); |
|
| 267 | + |
|
| 268 | + $allSelectors = array(); |
|
| 269 | + foreach ($matches as $key => $selectorString) { |
|
| 270 | + // if there is a blank definition, skip |
|
| 271 | + if (!strlen(trim($selectorString[2]))) { |
|
| 272 | + continue; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + // else split by commas and duplicate attributes so we can sort by selector precedence |
|
| 276 | + $selectors = explode(',', $selectorString[1]); |
|
| 277 | + foreach ($selectors as $selector) { |
|
| 278 | + // don't process pseudo-elements and behavioral (dynamic) pseudo-classes; ONLY allow structural pseudo-classes |
|
| 279 | + if (strpos($selector, ':') !== false && !preg_match('/:\\S+\\-(child|type)\\(/i', $selector)) { |
|
| 280 | + continue; |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + $allSelectors[] = array('selector' => trim($selector), |
|
| 284 | + 'attributes' => trim($selectorString[2]), |
|
| 285 | + // keep track of where it appears in the file, since order is important |
|
| 286 | + 'line' => $key, |
|
| 287 | + ); |
|
| 288 | + } |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + // now sort the selectors by precedence |
|
| 292 | + usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
| 293 | + |
|
| 294 | + $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + foreach ($this->caches[self::CACHE_KEY_CSS][$cssKey] as $value) { |
|
| 298 | + // query the body for the xpath selector |
|
| 299 | + $nodesMatchingCssSelectors = $xpath->query($this->translateCssToXpath($value['selector'])); |
|
| 300 | + |
|
| 301 | + /** @var $node \DOMNode */ |
|
| 302 | + foreach ($nodesMatchingCssSelectors as $node) { |
|
| 303 | + // if it has a style attribute, get it, process it, and append (overwrite) new stuff |
|
| 304 | + if ($node->hasAttribute('style')) { |
|
| 305 | + // break it up into an associative array |
|
| 306 | + $oldStyleDeclarations = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
| 307 | + } else { |
|
| 308 | + $oldStyleDeclarations = array(); |
|
| 309 | + } |
|
| 310 | + $newStyleDeclarations = $this->parseCssDeclarationBlock($value['attributes']); |
|
| 311 | + $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($oldStyleDeclarations, $newStyleDeclarations)); |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + // now iterate through the nodes that contained inline styles in the original HTML |
|
| 316 | + foreach ($this->styleAttributesForNodes as $nodePath => $styleAttributesForNode) { |
|
| 317 | + $node = $this->visitedNodes[$nodePath]; |
|
| 318 | + $currentStyleAttributes = $this->parseCssDeclarationBlock($node->getAttribute('style')); |
|
| 319 | + $node->setAttribute('style', $this->generateStyleStringFromDeclarationsArrays($currentStyleAttributes, $styleAttributesForNode)); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + // This removes styles from your email that contain display:none. |
|
| 323 | + // We need to look for display:none, but we need to do a case-insensitive search. Since DOMDocument only supports XPath 1.0, |
|
| 324 | + // lower-case() isn't available to us. We've thus far only set attributes to lowercase, not attribute values. Consequently, we need |
|
| 325 | + // to translate() the letters that would be in 'NONE' ("NOE") to lowercase. |
|
| 326 | + $nodesWithStyleDisplayNone = $xpath->query('//*[contains(translate(translate(@style," ",""),"NOE","noe"),"display:none")]'); |
|
| 327 | + // The checks on parentNode and is_callable below ensure that if we've deleted the parent node, |
|
| 328 | + // we don't try to call removeChild on a nonexistent child node |
|
| 329 | + if ($nodesWithStyleDisplayNone->length > 0) { |
|
| 330 | + /** @var $node \DOMNode */ |
|
| 331 | + foreach ($nodesWithStyleDisplayNone as $node) { |
|
| 332 | + if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
| 333 | + $node->parentNode->removeChild($node); |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument); |
|
| 339 | + |
|
| 340 | + if ($this->preserveEncoding) { |
|
| 341 | + if ( function_exists( 'mb_convert_encoding' ) ) { |
|
| 342 | + return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
| 343 | + } else { |
|
| 344 | + return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
|
| 345 | + } |
|
| 346 | + } else { |
|
| 347 | + return $xmlDocument->saveHTML(); |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + public function strtolower(array $m) { |
|
| 352 | + return strtolower($m[0]); |
|
| 353 | + } |
|
| 354 | + |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * This method merges old or existing name/value array with new name/value array. |
|
| 358 | + * and then generates a string of the combined style suitable for placing inline. |
|
| 359 | + * This becomes the single point for CSS string generation allowing for consistent. |
|
| 360 | + * CSS output no matter where the CSS originally came from. |
|
| 361 | + * @param array $oldStyles |
|
| 362 | + * @param array $newStyles |
|
| 363 | + * @return string |
|
| 364 | + */ |
|
| 365 | + private function generateStyleStringFromDeclarationsArrays(array $oldStyles, array $newStyles) { |
|
| 366 | + $combinedStyles = array_merge($oldStyles, $newStyles); |
|
| 367 | + $style = ''; |
|
| 368 | + foreach ($combinedStyles as $attributeName => $attributeValue) { |
|
| 369 | + $style .= (strtolower(trim($attributeName)) . ': ' . trim($attributeValue) . '; '); |
|
| 370 | + } |
|
| 371 | + return trim($style); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Copies the media part from CSS array parts to $xmlDocument. |
|
| 377 | + * |
|
| 378 | + * @param array $cssParts |
|
| 379 | + * @param DOMDocument $xmlDocument |
|
| 380 | + */ |
|
| 381 | + public function copyCssWithMediaToStyleNode(array $cssParts, DOMDocument $xmlDocument) { |
|
| 382 | + if (isset($cssParts['media']) && $cssParts['media'] !== '') { |
|
| 383 | + $this->addStyleElementToDocument($xmlDocument, $cssParts['media']); |
|
| 384 | + } |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Returns CSS content. |
|
| 389 | + * |
|
| 390 | + * @param DOMXPath $xpath |
|
| 391 | + * @return string |
|
| 392 | + */ |
|
| 393 | + private function getCssFromAllStyleNodes(DOMXPath $xpath) { |
|
| 394 | + $styleNodes = $xpath->query('//style'); |
|
| 395 | + |
|
| 396 | + if ($styleNodes === false) { |
|
| 397 | + return ''; |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + $css = ''; |
|
| 401 | + /** @var $styleNode DOMNode */ |
|
| 402 | + foreach ($styleNodes as $styleNode) { |
|
| 403 | + $css .= "\n\n" . $styleNode->nodeValue; |
|
| 404 | + $styleNode->parentNode->removeChild($styleNode); |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + return $css; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * Adds a style element with $css to $document. |
|
| 412 | + * |
|
| 413 | + * @param DOMDocument $document |
|
| 414 | + * @param string $css |
|
| 415 | + */ |
|
| 416 | + private function addStyleElementToDocument(DOMDocument $document, $css) { |
|
| 417 | + $styleElement = $document->createElement('style', $css); |
|
| 418 | + $styleAttribute = $document->createAttribute('type'); |
|
| 419 | + $styleAttribute->value = 'text/css'; |
|
| 420 | + $styleElement->appendChild($styleAttribute); |
|
| 421 | + |
|
| 422 | + $head = $this->getOrCreateHeadElement($document); |
|
| 423 | + $head->appendChild($styleElement); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Returns the existing or creates a new head element in $document. |
|
| 428 | + * |
|
| 429 | + * @param DOMDocument $document |
|
| 430 | + * @return DOMNode the head element |
|
| 431 | + */ |
|
| 432 | + private function getOrCreateHeadElement(DOMDocument $document) { |
|
| 433 | + $head = $document->getElementsByTagName('head')->item(0); |
|
| 434 | + |
|
| 435 | + if ($head === null) { |
|
| 436 | + $head = $document->createElement('head'); |
|
| 437 | + $html = $document->getElementsByTagName('html')->item(0); |
|
| 438 | + $html->insertBefore($head, $document->getElementsByTagName('body')->item(0)); |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + return $head; |
|
| 442 | + } |
|
| 443 | + |
|
| 444 | + /** |
|
| 445 | + * Splits input CSS code to an array where: |
|
| 446 | + * |
|
| 447 | + * - key "css" will be contains clean CSS code. |
|
| 448 | + * - key "media" will be contains all valuable media queries. |
|
| 449 | + * |
|
| 450 | + * Example: |
|
| 451 | + * |
|
| 452 | + * The CSS code. |
|
| 453 | + * |
|
| 454 | + * "@import "file.css"; h1 { color:red; } @media { h1 {}} @media tv { h1 {}}" |
|
| 455 | + * |
|
| 456 | + * will be parsed into the following array: |
|
| 457 | + * |
|
| 458 | + * "css" => "h1 { color:red; }" |
|
| 459 | + * "media" => "@media { h1 {}}" |
|
| 460 | + * |
|
| 461 | + * @param string $css |
|
| 462 | + * @return array |
|
| 463 | + */ |
|
| 464 | + private function splitCssAndMediaQuery($css) { |
|
| 465 | + $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
| 466 | + |
|
| 467 | + // filter the CSS |
|
| 468 | + $search = array( |
|
| 469 | + // get rid of css comment code |
|
| 470 | + '/\\/\\*.*\\*\\//sU', |
|
| 471 | + // strip out any import directives |
|
| 472 | + '/^\\s*@import\\s[^;]+;/misU', |
|
| 473 | + // strip remains media enclosures |
|
| 474 | + '/^\\s*@media\\s[^{]+{(.*)}\\s*}\\s/misU', |
|
| 475 | + ); |
|
| 476 | + |
|
| 477 | + $replace = array( |
|
| 478 | + '', |
|
| 479 | + '', |
|
| 480 | + '', |
|
| 481 | + ); |
|
| 482 | + |
|
| 483 | + // clean CSS before output |
|
| 484 | + $css = preg_replace($search, $replace, $css); |
|
| 485 | + |
|
| 486 | + return array('css' => $css, 'media' => self::$_media); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + private function _media_concat( $matches ) { |
|
| 490 | + self::$_media .= $matches[0]; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + /** |
|
| 494 | + * Creates a DOMDocument instance with the current HTML. |
|
| 495 | + * |
|
| 496 | + * @return DOMDocument |
|
| 497 | + */ |
|
| 498 | + private function createXmlDocument() { |
|
| 499 | + $xmlDocument = new DOMDocument; |
|
| 500 | + $xmlDocument->encoding = self::ENCODING; |
|
| 501 | + $xmlDocument->strictErrorChecking = false; |
|
| 502 | + $xmlDocument->formatOutput = true; |
|
| 503 | + $libXmlState = libxml_use_internal_errors(true); |
|
| 504 | + $xmlDocument->loadHTML($this->getUnifiedHtml()); |
|
| 505 | + libxml_clear_errors(); |
|
| 506 | + libxml_use_internal_errors($libXmlState); |
|
| 507 | + $xmlDocument->normalizeDocument(); |
|
| 508 | + |
|
| 509 | + return $xmlDocument; |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + /** |
|
| 513 | + * Returns the HTML with the non-ASCII characters converts into HTML entities and the unprocessable HTML tags removed. |
|
| 514 | + * |
|
| 515 | + * @return string the unified HTML |
|
| 516 | + * |
|
| 517 | + * @throws BadMethodCallException |
|
| 518 | + */ |
|
| 519 | + private function getUnifiedHtml() { |
|
| 520 | + if (!empty($this->unprocessableHtmlTags)) { |
|
| 521 | + $unprocessableHtmlTags = implode('|', $this->unprocessableHtmlTags); |
|
| 522 | + $bodyWithoutUnprocessableTags = preg_replace('/<\\/?(' . $unprocessableHtmlTags . ')[^>]*>/i', '', $this->html); |
|
| 523 | + } else { |
|
| 524 | + $bodyWithoutUnprocessableTags = $this->html; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + if ( function_exists( 'mb_convert_encoding' ) ) { |
|
| 528 | + return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
| 529 | + } else { |
|
| 530 | + return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
|
| 531 | + } |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + /** |
|
| 535 | + * @param array $a |
|
| 536 | + * @param array $b |
|
| 537 | + * |
|
| 538 | + * @return integer |
|
| 539 | + */ |
|
| 540 | + private function sortBySelectorPrecedence(array $a, array $b) { |
|
| 541 | + $precedenceA = $this->getCssSelectorPrecedence($a['selector']); |
|
| 542 | + $precedenceB = $this->getCssSelectorPrecedence($b['selector']); |
|
| 543 | + |
|
| 544 | + // We want these sorted in ascending order so selectors with lesser precedence get processed first and |
|
| 545 | + // selectors with greater precedence get sorted last. |
|
| 546 | + // The parenthesis around the -1 are necessary to avoid a PHP_CodeSniffer warning about missing spaces around |
|
| 547 | + // arithmetic operators. |
|
| 548 | + // @see http://forge.typo3.org/issues/55605 |
|
| 549 | + $precedenceForEquals = ($a['line'] < $b['line'] ? (-1) : 1); |
|
| 550 | + $precedenceForNotEquals = ($precedenceA < $precedenceB ? (-1) : 1); |
|
| 551 | + return ($precedenceA === $precedenceB) ? $precedenceForEquals : $precedenceForNotEquals; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * @param string $selector |
|
| 556 | + * |
|
| 557 | + * @return integer |
|
| 558 | + */ |
|
| 559 | + private function getCssSelectorPrecedence($selector) { |
|
| 560 | + $selectorKey = md5($selector); |
|
| 561 | + if (!isset($this->caches[self::CACHE_KEY_SELECTOR][$selectorKey])) { |
|
| 562 | + $precedence = 0; |
|
| 563 | + $value = 100; |
|
| 564 | + // ids: worth 100, classes: worth 10, elements: worth 1 |
|
| 565 | + $search = array('\\#','\\.',''); |
|
| 566 | + |
|
| 567 | + foreach ($search as $s) { |
|
| 568 | + if (trim($selector == '')) { |
|
| 569 | + break; |
|
| 570 | + } |
|
| 571 | + $number = 0; |
|
| 572 | + $selector = preg_replace('/' . $s . '\\w+/', '', $selector, -1, $number); |
|
| 573 | + $precedence += ($value * $number); |
|
| 574 | + $value /= 10; |
|
| 575 | + } |
|
| 576 | + $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey] = $precedence; |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + return $this->caches[self::CACHE_KEY_SELECTOR][$selectorKey]; |
|
| 580 | + } |
|
| 581 | + |
|
| 582 | + /** |
|
| 583 | + * Right now, we support all CSS 1 selectors and most CSS2/3 selectors. |
|
| 584 | + * |
|
| 585 | + * @see http://plasmasturm.org/log/444/ |
|
| 586 | + * |
|
| 587 | + * @param string $paramCssSelector |
|
| 588 | + * |
|
| 589 | + * @return string |
|
| 590 | + */ |
|
| 591 | + private function translateCssToXpath($paramCssSelector) { |
|
| 592 | + $cssSelector = ' ' . $paramCssSelector . ' '; |
|
| 593 | + $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
| 594 | + $cssSelector = trim($cssSelector); |
|
| 595 | + $xpathKey = md5($cssSelector); |
|
| 596 | + if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
|
| 597 | + // returns an Xpath selector |
|
| 598 | + $search = array( |
|
| 599 | + // Matches any element that is a child of parent. |
|
| 600 | + '/\\s+>\\s+/', |
|
| 601 | + // Matches any element that is an adjacent sibling. |
|
| 602 | + '/\\s+\\+\\s+/', |
|
| 603 | + // Matches any element that is a descendant of an parent element element. |
|
| 604 | + '/\\s+/', |
|
| 605 | + // first-child pseudo-selector |
|
| 606 | + '/([^\\/]+):first-child/i', |
|
| 607 | + // last-child pseudo-selector |
|
| 608 | + '/([^\\/]+):last-child/i', |
|
| 609 | + // Matches attribute only selector |
|
| 610 | + '/^\\[(\\w+)\\]/', |
|
| 611 | + // Matches element with attribute |
|
| 612 | + '/(\\w)\\[(\\w+)\\]/', |
|
| 613 | + // Matches element with EXACT attribute |
|
| 614 | + '/(\\w)\\[(\\w+)\\=[\'"]?(\\w+)[\'"]?\\]/', |
|
| 615 | + ); |
|
| 616 | + $replace = array( |
|
| 617 | + '/', |
|
| 618 | + '/following-sibling::*[1]/self::', |
|
| 619 | + '//', |
|
| 620 | + '*[1]/self::\\1', |
|
| 621 | + '*[last()]/self::\\1', |
|
| 622 | + '*[@\\1]', |
|
| 623 | + '\\1[@\\2]', |
|
| 624 | + '\\1[@\\2="\\3"]', |
|
| 625 | + ); |
|
| 626 | + |
|
| 627 | + $cssSelector = '//' . preg_replace($search, $replace, $cssSelector); |
|
| 628 | + |
|
| 629 | + $cssSelector = preg_replace_callback(self::ID_ATTRIBUTE_MATCHER, array($this, 'matchIdAttributes'), $cssSelector); |
|
| 630 | + $cssSelector = preg_replace_callback(self::CLASS_ATTRIBUTE_MATCHER, array($this, 'matchClassAttributes'), $cssSelector); |
|
| 631 | + |
|
| 632 | + // Advanced selectors are going to require a bit more advanced emogrification. |
|
| 633 | + // When we required PHP 5.3, we could do this with closures. |
|
| 634 | + $cssSelector = preg_replace_callback( |
|
| 635 | + '/([^\\/]+):nth-child\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
| 636 | + array($this, 'translateNthChild'), $cssSelector |
|
| 637 | + ); |
|
| 638 | + $cssSelector = preg_replace_callback( |
|
| 639 | + '/([^\\/]+):nth-of-type\\(\s*(odd|even|[+\-]?\\d|[+\\-]?\\d?n(\\s*[+\\-]\\s*\\d)?)\\s*\\)/i', |
|
| 640 | + array($this, 'translateNthOfType'), $cssSelector |
|
| 641 | + ); |
|
| 642 | + |
|
| 643 | + $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey] = $cssSelector; |
|
| 644 | + } |
|
| 645 | + return $this->caches[self::CACHE_KEY_SELECTOR][$xpathKey]; |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + /** |
|
| 649 | + * @param array $match |
|
| 650 | + * |
|
| 651 | + * @return string |
|
| 652 | + */ |
|
| 653 | + private function matchIdAttributes(array $match) { |
|
| 654 | + return (strlen($match[1]) ? $match[1] : '*') . '[@id="' . $match[2] . '"]'; |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + /** |
|
| 658 | + * @param array $match |
|
| 659 | + * |
|
| 660 | + * @return string |
|
| 661 | + */ |
|
| 662 | + private function matchClassAttributes(array $match) { |
|
| 663 | + return (strlen($match[1]) ? $match[1] : '*') . '[contains(concat(" ",@class," "),concat(" ","' . |
|
| 664 | + implode( |
|
| 665 | + '"," "))][contains(concat(" ",@class," "),concat(" ","', |
|
| 666 | + explode('.', substr($match[2], 1)) |
|
| 667 | + ) . '"," "))]'; |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * @param array $match |
|
| 672 | + * |
|
| 673 | + * @return string |
|
| 674 | + */ |
|
| 675 | + private function translateNthChild(array $match) { |
|
| 676 | + $result = $this->parseNth($match); |
|
| 677 | + |
|
| 678 | + if (isset($result[self::MULTIPLIER])) { |
|
| 679 | + if ($result[self::MULTIPLIER] < 0) { |
|
| 680 | + $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
| 681 | + return sprintf('*[(last() - position()) mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
| 682 | + } else { |
|
| 683 | + return sprintf('*[position() mod %u = %u]/self::%s', $result[self::MULTIPLIER], $result[self::INDEX], $match[1]); |
|
| 684 | + } |
|
| 685 | + } else { |
|
| 686 | + return sprintf('*[%u]/self::%s', $result[self::INDEX], $match[1]); |
|
| 687 | + } |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * @param array $match |
|
| 692 | + * |
|
| 693 | + * @return string |
|
| 694 | + */ |
|
| 695 | + private function translateNthOfType(array $match) { |
|
| 696 | + $result = $this->parseNth($match); |
|
| 697 | + |
|
| 698 | + if (isset($result[self::MULTIPLIER])) { |
|
| 699 | + if ($result[self::MULTIPLIER] < 0) { |
|
| 700 | + $result[self::MULTIPLIER] = abs($result[self::MULTIPLIER]); |
|
| 701 | + return sprintf('%s[(last() - position()) mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
| 702 | + } else { |
|
| 703 | + return sprintf('%s[position() mod %u = %u]', $match[1], $result[self::MULTIPLIER], $result[self::INDEX]); |
|
| 704 | + } |
|
| 705 | + } else { |
|
| 706 | + return sprintf('%s[%u]', $match[1], $result[self::INDEX]); |
|
| 707 | + } |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + /** |
|
| 711 | + * @param array $match |
|
| 712 | + * |
|
| 713 | + * @return array |
|
| 714 | + */ |
|
| 715 | + private function parseNth(array $match) { |
|
| 716 | + if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
| 717 | + $index = strtolower($match[2]) == 'even' ? 0 : 1; |
|
| 718 | + return array(self::MULTIPLIER => 2, self::INDEX => $index); |
|
| 719 | + } elseif (stripos($match[2], 'n') === false) { |
|
| 720 | + // if there is a multiplier |
|
| 721 | + $index = intval(str_replace(' ', '', $match[2])); |
|
| 722 | + return array(self::INDEX => $index); |
|
| 723 | + } else { |
|
| 724 | + if (isset($match[3])) { |
|
| 725 | + $multipleTerm = str_replace($match[3], '', $match[2]); |
|
| 726 | + $index = intval(str_replace(' ', '', $match[3])); |
|
| 727 | + } else { |
|
| 728 | + $multipleTerm = $match[2]; |
|
| 729 | + $index = 0; |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + $multiplier = str_ireplace('n', '', $multipleTerm); |
|
| 733 | + |
|
| 734 | + if (!strlen($multiplier)) { |
|
| 735 | + $multiplier = 1; |
|
| 736 | + } elseif ($multiplier == 0) { |
|
| 737 | + return array(self::INDEX => $index); |
|
| 738 | + } else { |
|
| 739 | + $multiplier = intval($multiplier); |
|
| 740 | + } |
|
| 741 | + |
|
| 742 | + while ($index < 0) { |
|
| 743 | + $index += abs($multiplier); |
|
| 744 | + } |
|
| 745 | + |
|
| 746 | + return array(self::MULTIPLIER => $multiplier, self::INDEX => $index); |
|
| 747 | + } |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + /** |
|
| 751 | + * Parses a CSS declaration block into property name/value pairs. |
|
| 752 | + * |
|
| 753 | + * Example: |
|
| 754 | + * |
|
| 755 | + * The declaration block. |
|
| 756 | + * |
|
| 757 | + * "color: #000; font-weight: bold;". |
|
| 758 | + * |
|
| 759 | + * will be parsed into the following array: |
|
| 760 | + * |
|
| 761 | + * "color" => "#000" |
|
| 762 | + * "font-weight" => "bold" |
|
| 763 | + * |
|
| 764 | + * @param string $cssDeclarationBlock the CSS declaration block without the curly braces, may be empty |
|
| 765 | + * |
|
| 766 | + * @return array the CSS declarations with the property names as array keys and the property values as array values |
|
| 767 | + */ |
|
| 768 | + private function parseCssDeclarationBlock($cssDeclarationBlock) { |
|
| 769 | + if (isset($this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock])) { |
|
| 770 | + return $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock]; |
|
| 771 | + } |
|
| 772 | + |
|
| 773 | + $properties = array(); |
|
| 774 | + $declarations = explode(';', $cssDeclarationBlock); |
|
| 775 | + foreach ($declarations as $declaration) { |
|
| 776 | + $matches = array(); |
|
| 777 | + if (!preg_match('/ *([A-Za-z\\-]+) *: *([^;]+) */', $declaration, $matches)) { |
|
| 778 | + continue; |
|
| 779 | + } |
|
| 780 | + $propertyName = strtolower($matches[1]); |
|
| 781 | + $propertyValue = $matches[2]; |
|
| 782 | + $properties[$propertyName] = $propertyValue; |
|
| 783 | + } |
|
| 784 | + $this->caches[self::CACHE_KEY_CSS_DECLARATION_BLOCK][$cssDeclarationBlock] = $properties; |
|
| 785 | + |
|
| 786 | + return $properties; |
|
| 787 | + } |
|
| 788 | 788 | } |
@@ -238,7 +238,7 @@ discard block |
||
| 238 | 238 | if ($nodesWithStyleAttributes !== false) { |
| 239 | 239 | /** @var $nodeWithStyleAttribute DOMNode */ |
| 240 | 240 | foreach ($nodesWithStyleAttributes as $node) { |
| 241 | - $normalizedOriginalStyle = preg_replace_callback( '/[A-z\\-]+(?=\\:)/S', array( $this, 'strtolower' ), $node->getAttribute('style') ); |
|
| 241 | + $normalizedOriginalStyle = preg_replace_callback('/[A-z\\-]+(?=\\:)/S', array($this, 'strtolower'), $node->getAttribute('style')); |
|
| 242 | 242 | |
| 243 | 243 | // in order to not overwrite existing style attributes in the HTML, we have to save the original HTML styles |
| 244 | 244 | $nodePath = $node->getNodePath(); |
@@ -289,7 +289,7 @@ discard block |
||
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | // now sort the selectors by precedence |
| 292 | - usort($allSelectors, array($this,'sortBySelectorPrecedence')); |
|
| 292 | + usort($allSelectors, array($this, 'sortBySelectorPrecedence')); |
|
| 293 | 293 | |
| 294 | 294 | $this->caches[self::CACHE_KEY_CSS][$cssKey] = $allSelectors; |
| 295 | 295 | } |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | if ($nodesWithStyleDisplayNone->length > 0) { |
| 330 | 330 | /** @var $node \DOMNode */ |
| 331 | 331 | foreach ($nodesWithStyleDisplayNone as $node) { |
| 332 | - if ($node->parentNode && is_callable(array($node->parentNode,'removeChild'))) { |
|
| 332 | + if ($node->parentNode && is_callable(array($node->parentNode, 'removeChild'))) { |
|
| 333 | 333 | $node->parentNode->removeChild($node); |
| 334 | 334 | } |
| 335 | 335 | } |
@@ -338,10 +338,10 @@ discard block |
||
| 338 | 338 | $this->copyCssWithMediaToStyleNode($cssParts, $xmlDocument); |
| 339 | 339 | |
| 340 | 340 | if ($this->preserveEncoding) { |
| 341 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
| 342 | - return mb_convert_encoding( $xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES' ); |
|
| 341 | + if (function_exists('mb_convert_encoding')) { |
|
| 342 | + return mb_convert_encoding($xmlDocument->saveHTML(), self::ENCODING, 'HTML-ENTITIES'); |
|
| 343 | 343 | } else { |
| 344 | - return htmlspecialchars_decode( utf8_encode( html_entity_decode( $xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING ) ) ); |
|
| 344 | + return htmlspecialchars_decode(utf8_encode(html_entity_decode($xmlDocument->saveHTML(), ENT_COMPAT, self::ENCODING))); |
|
| 345 | 345 | } |
| 346 | 346 | } else { |
| 347 | 347 | return $xmlDocument->saveHTML(); |
@@ -462,7 +462,7 @@ discard block |
||
| 462 | 462 | * @return array |
| 463 | 463 | */ |
| 464 | 464 | private function splitCssAndMediaQuery($css) { |
| 465 | - $css = preg_replace_callback( '#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array( $this, '_media_concat' ), $css ); |
|
| 465 | + $css = preg_replace_callback('#@media\\s+(?:only\\s)?(?:[\\s{\(]|screen|all)\\s?[^{]+{.*}\\s*}\\s*#misU', array($this, '_media_concat'), $css); |
|
| 466 | 466 | |
| 467 | 467 | // filter the CSS |
| 468 | 468 | $search = array( |
@@ -486,7 +486,7 @@ discard block |
||
| 486 | 486 | return array('css' => $css, 'media' => self::$_media); |
| 487 | 487 | } |
| 488 | 488 | |
| 489 | - private function _media_concat( $matches ) { |
|
| 489 | + private function _media_concat($matches) { |
|
| 490 | 490 | self::$_media .= $matches[0]; |
| 491 | 491 | } |
| 492 | 492 | |
@@ -524,10 +524,10 @@ discard block |
||
| 524 | 524 | $bodyWithoutUnprocessableTags = $this->html; |
| 525 | 525 | } |
| 526 | 526 | |
| 527 | - if ( function_exists( 'mb_convert_encoding' ) ) { |
|
| 528 | - return mb_convert_encoding( $bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING ); |
|
| 527 | + if (function_exists('mb_convert_encoding')) { |
|
| 528 | + return mb_convert_encoding($bodyWithoutUnprocessableTags, 'HTML-ENTITIES', self::ENCODING); |
|
| 529 | 529 | } else { |
| 530 | - return htmlspecialchars_decode( utf8_decode( htmlentities( $bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false ) ) ); |
|
| 530 | + return htmlspecialchars_decode(utf8_decode(htmlentities($bodyWithoutUnprocessableTags, ENT_COMPAT, self::ENCODING, false))); |
|
| 531 | 531 | } |
| 532 | 532 | } |
| 533 | 533 | |
@@ -562,7 +562,7 @@ discard block |
||
| 562 | 562 | $precedence = 0; |
| 563 | 563 | $value = 100; |
| 564 | 564 | // ids: worth 100, classes: worth 10, elements: worth 1 |
| 565 | - $search = array('\\#','\\.',''); |
|
| 565 | + $search = array('\\#', '\\.', ''); |
|
| 566 | 566 | |
| 567 | 567 | foreach ($search as $s) { |
| 568 | 568 | if (trim($selector == '')) { |
@@ -590,7 +590,7 @@ discard block |
||
| 590 | 590 | */ |
| 591 | 591 | private function translateCssToXpath($paramCssSelector) { |
| 592 | 592 | $cssSelector = ' ' . $paramCssSelector . ' '; |
| 593 | - $cssSelector = preg_replace_callback( '/\s+\w+\s+/', array( $this, 'strtolower' ), $cssSelector ); |
|
| 593 | + $cssSelector = preg_replace_callback('/\s+\w+\s+/', array($this, 'strtolower'), $cssSelector); |
|
| 594 | 594 | $cssSelector = trim($cssSelector); |
| 595 | 595 | $xpathKey = md5($cssSelector); |
| 596 | 596 | if (!isset($this->caches[self::CACHE_KEY_XPATH][$xpathKey])) { |
@@ -713,7 +713,7 @@ discard block |
||
| 713 | 713 | * @return array |
| 714 | 714 | */ |
| 715 | 715 | private function parseNth(array $match) { |
| 716 | - if (in_array(strtolower($match[2]), array('even','odd'))) { |
|
| 716 | + if (in_array(strtolower($match[2]), array('even', 'odd'))) { |
|
| 717 | 717 | $index = strtolower($match[2]) == 'even' ? 0 : 1; |
| 718 | 718 | return array(self::MULTIPLIER => 2, self::INDEX => $index); |
| 719 | 719 | } elseif (stripos($match[2], 'n') === false) { |
@@ -8,151 +8,151 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class WP_Session_Command extends \WP_CLI_Command { |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * Count the total number of sessions stored in the database. |
|
| 13 | - * |
|
| 14 | - * |
|
| 15 | - * ## EXAMPLES |
|
| 16 | - * |
|
| 17 | - * wp session count |
|
| 18 | - * |
|
| 19 | - * @global wpdb $wpdb |
|
| 20 | - * |
|
| 21 | - * @param array $args |
|
| 22 | - * @param array $assoc_args |
|
| 23 | - */ |
|
| 24 | - public function count( $args, $assoc_args ) { |
|
| 25 | - $sessions = WP_Session_Utils::count_sessions(); |
|
| 26 | - |
|
| 27 | - \WP_CLI::line( sprintf( '%d sessions currently exist.', absint( $sessions ) ) ); |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Delete sessions from the database. |
|
| 32 | - * |
|
| 33 | - * ## OPTIONS |
|
| 34 | - * |
|
| 35 | - * [--all] |
|
| 36 | - * : Flag whether or not to purge all sessions from the database. |
|
| 37 | - * |
|
| 38 | - * [--batch=<batch>] |
|
| 39 | - * : Set the batch size for deleting old sessions |
|
| 40 | - * |
|
| 41 | - * [--limit=<limit>] |
|
| 42 | - * : Delete just this number of old sessions |
|
| 43 | - * |
|
| 44 | - * ## EXAMPLES |
|
| 45 | - * |
|
| 46 | - * wp session delete |
|
| 47 | - * wp session delete [--batch=<batch>] |
|
| 48 | - * wp session delete [--limit=<limit>] |
|
| 49 | - * wp session delete [--all] |
|
| 50 | - * |
|
| 51 | - * @synopsis [--all] [--batch=<batch>] [--limit=<limit>] |
|
| 52 | - * |
|
| 53 | - * @param array $args |
|
| 54 | - * @param array $assoc_args |
|
| 55 | - */ |
|
| 56 | - public function delete( $args, $assoc_args ) { |
|
| 57 | - if ( isset( $assoc_args['limit'] ) ) { |
|
| 58 | - $limit = absint( $assoc_args['limit'] ); |
|
| 59 | - |
|
| 60 | - $count = WP_Session_Utils::delete_old_sessions( $limit ); |
|
| 61 | - |
|
| 62 | - if ( $count > 0 ) { |
|
| 63 | - \WP_CLI::line( sprintf( 'Deleted %d sessions.', $count ) ); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - // Clear memory |
|
| 67 | - self::free_up_memory(); |
|
| 68 | - return; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // Determine if we're deleting all sessions or just a subset. |
|
| 72 | - $all = isset( $assoc_args['all'] ); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Determine the size of each batch for deletion. |
|
| 76 | - * |
|
| 77 | - * @param int |
|
| 78 | - */ |
|
| 79 | - $batch = isset( $assoc_args['batch'] ) ? absint( $assoc_args['batch'] ) : apply_filters( 'wp_session_delete_batch_size', 1000 ); |
|
| 80 | - |
|
| 81 | - switch ( $all ) { |
|
| 82 | - case true: |
|
| 83 | - $count = WP_Session_Utils::delete_all_sessions(); |
|
| 84 | - |
|
| 85 | - \WP_CLI::line( sprintf( 'Deleted all %d sessions.', $count ) ); |
|
| 86 | - break; |
|
| 87 | - case false: |
|
| 88 | - do { |
|
| 89 | - $count = WP_Session_Utils::delete_old_sessions( $batch ); |
|
| 90 | - |
|
| 91 | - if ( $count > 0 ) { |
|
| 92 | - \WP_CLI::line( sprintf( 'Deleted %d sessions.', $count ) ); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - // Clear memory |
|
| 96 | - self::free_up_memory(); |
|
| 97 | - } while ( $count > 0 ); |
|
| 98 | - break; |
|
| 99 | - } |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Generate a number of dummy sessions for testing purposes. |
|
| 104 | - * |
|
| 105 | - * ## OPTIONS |
|
| 106 | - * |
|
| 107 | - * <count> |
|
| 108 | - * : Number of sessions to create. |
|
| 109 | - * |
|
| 110 | - * [--expires=<date>] |
|
| 111 | - * : Optional expiration time tagged for each session. Will use WordPress' local time. |
|
| 112 | - * |
|
| 113 | - * ## EXAMPLES |
|
| 114 | - * |
|
| 115 | - * wp session generate 5000 |
|
| 116 | - * wp session generate 5000 --expires="2014-11-09T08:00" |
|
| 117 | - * |
|
| 118 | - * @synopsis <count> [--expires=<date>] |
|
| 119 | - * |
|
| 120 | - * @param array $args |
|
| 121 | - * @param array $assoc_args |
|
| 122 | - */ |
|
| 123 | - public function generate( $args, $assoc_args ) { |
|
| 124 | - $count = absint( $args[0] ); |
|
| 125 | - $date = isset( $assoc_args['expires'] ) ? $assoc_args['expires'] : null; |
|
| 126 | - |
|
| 127 | - $notify = \WP_CLI\Utils\make_progress_bar( 'Generating sessions', $count ); |
|
| 128 | - |
|
| 129 | - for ( $i = 0; $i < $count; $i ++ ) { |
|
| 130 | - WP_Session_Utils::create_dummy_session( $date ); |
|
| 131 | - $notify->tick(); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - $notify->finish(); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Free up memory |
|
| 139 | - * |
|
| 140 | - * @global WP_Object_Cache $wp_object_cache |
|
| 141 | - * @global wpdb $wpdb |
|
| 142 | - */ |
|
| 143 | - private function free_up_memory() { |
|
| 144 | - global $wp_object_cache, $wpdb; |
|
| 145 | - $wpdb->queries = array(); |
|
| 146 | - |
|
| 147 | - if ( ! is_object( $wp_object_cache ) ) { |
|
| 148 | - return; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - $wp_object_cache->group_ops = array(); |
|
| 152 | - $wp_object_cache->stats = array(); |
|
| 153 | - $wp_object_cache->memcache_debug = array(); |
|
| 154 | - $wp_object_cache->cache = array(); |
|
| 155 | - } |
|
| 11 | + /** |
|
| 12 | + * Count the total number of sessions stored in the database. |
|
| 13 | + * |
|
| 14 | + * |
|
| 15 | + * ## EXAMPLES |
|
| 16 | + * |
|
| 17 | + * wp session count |
|
| 18 | + * |
|
| 19 | + * @global wpdb $wpdb |
|
| 20 | + * |
|
| 21 | + * @param array $args |
|
| 22 | + * @param array $assoc_args |
|
| 23 | + */ |
|
| 24 | + public function count( $args, $assoc_args ) { |
|
| 25 | + $sessions = WP_Session_Utils::count_sessions(); |
|
| 26 | + |
|
| 27 | + \WP_CLI::line( sprintf( '%d sessions currently exist.', absint( $sessions ) ) ); |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Delete sessions from the database. |
|
| 32 | + * |
|
| 33 | + * ## OPTIONS |
|
| 34 | + * |
|
| 35 | + * [--all] |
|
| 36 | + * : Flag whether or not to purge all sessions from the database. |
|
| 37 | + * |
|
| 38 | + * [--batch=<batch>] |
|
| 39 | + * : Set the batch size for deleting old sessions |
|
| 40 | + * |
|
| 41 | + * [--limit=<limit>] |
|
| 42 | + * : Delete just this number of old sessions |
|
| 43 | + * |
|
| 44 | + * ## EXAMPLES |
|
| 45 | + * |
|
| 46 | + * wp session delete |
|
| 47 | + * wp session delete [--batch=<batch>] |
|
| 48 | + * wp session delete [--limit=<limit>] |
|
| 49 | + * wp session delete [--all] |
|
| 50 | + * |
|
| 51 | + * @synopsis [--all] [--batch=<batch>] [--limit=<limit>] |
|
| 52 | + * |
|
| 53 | + * @param array $args |
|
| 54 | + * @param array $assoc_args |
|
| 55 | + */ |
|
| 56 | + public function delete( $args, $assoc_args ) { |
|
| 57 | + if ( isset( $assoc_args['limit'] ) ) { |
|
| 58 | + $limit = absint( $assoc_args['limit'] ); |
|
| 59 | + |
|
| 60 | + $count = WP_Session_Utils::delete_old_sessions( $limit ); |
|
| 61 | + |
|
| 62 | + if ( $count > 0 ) { |
|
| 63 | + \WP_CLI::line( sprintf( 'Deleted %d sessions.', $count ) ); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + // Clear memory |
|
| 67 | + self::free_up_memory(); |
|
| 68 | + return; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // Determine if we're deleting all sessions or just a subset. |
|
| 72 | + $all = isset( $assoc_args['all'] ); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Determine the size of each batch for deletion. |
|
| 76 | + * |
|
| 77 | + * @param int |
|
| 78 | + */ |
|
| 79 | + $batch = isset( $assoc_args['batch'] ) ? absint( $assoc_args['batch'] ) : apply_filters( 'wp_session_delete_batch_size', 1000 ); |
|
| 80 | + |
|
| 81 | + switch ( $all ) { |
|
| 82 | + case true: |
|
| 83 | + $count = WP_Session_Utils::delete_all_sessions(); |
|
| 84 | + |
|
| 85 | + \WP_CLI::line( sprintf( 'Deleted all %d sessions.', $count ) ); |
|
| 86 | + break; |
|
| 87 | + case false: |
|
| 88 | + do { |
|
| 89 | + $count = WP_Session_Utils::delete_old_sessions( $batch ); |
|
| 90 | + |
|
| 91 | + if ( $count > 0 ) { |
|
| 92 | + \WP_CLI::line( sprintf( 'Deleted %d sessions.', $count ) ); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + // Clear memory |
|
| 96 | + self::free_up_memory(); |
|
| 97 | + } while ( $count > 0 ); |
|
| 98 | + break; |
|
| 99 | + } |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Generate a number of dummy sessions for testing purposes. |
|
| 104 | + * |
|
| 105 | + * ## OPTIONS |
|
| 106 | + * |
|
| 107 | + * <count> |
|
| 108 | + * : Number of sessions to create. |
|
| 109 | + * |
|
| 110 | + * [--expires=<date>] |
|
| 111 | + * : Optional expiration time tagged for each session. Will use WordPress' local time. |
|
| 112 | + * |
|
| 113 | + * ## EXAMPLES |
|
| 114 | + * |
|
| 115 | + * wp session generate 5000 |
|
| 116 | + * wp session generate 5000 --expires="2014-11-09T08:00" |
|
| 117 | + * |
|
| 118 | + * @synopsis <count> [--expires=<date>] |
|
| 119 | + * |
|
| 120 | + * @param array $args |
|
| 121 | + * @param array $assoc_args |
|
| 122 | + */ |
|
| 123 | + public function generate( $args, $assoc_args ) { |
|
| 124 | + $count = absint( $args[0] ); |
|
| 125 | + $date = isset( $assoc_args['expires'] ) ? $assoc_args['expires'] : null; |
|
| 126 | + |
|
| 127 | + $notify = \WP_CLI\Utils\make_progress_bar( 'Generating sessions', $count ); |
|
| 128 | + |
|
| 129 | + for ( $i = 0; $i < $count; $i ++ ) { |
|
| 130 | + WP_Session_Utils::create_dummy_session( $date ); |
|
| 131 | + $notify->tick(); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + $notify->finish(); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Free up memory |
|
| 139 | + * |
|
| 140 | + * @global WP_Object_Cache $wp_object_cache |
|
| 141 | + * @global wpdb $wpdb |
|
| 142 | + */ |
|
| 143 | + private function free_up_memory() { |
|
| 144 | + global $wp_object_cache, $wpdb; |
|
| 145 | + $wpdb->queries = array(); |
|
| 146 | + |
|
| 147 | + if ( ! is_object( $wp_object_cache ) ) { |
|
| 148 | + return; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + $wp_object_cache->group_ops = array(); |
|
| 152 | + $wp_object_cache->stats = array(); |
|
| 153 | + $wp_object_cache->memcache_debug = array(); |
|
| 154 | + $wp_object_cache->cache = array(); |
|
| 155 | + } |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | \WP_CLI::add_command( 'session', 'WP_Session_Command' ); |
| 159 | 159 | \ No newline at end of file |
@@ -21,10 +21,10 @@ discard block |
||
| 21 | 21 | * @param array $args |
| 22 | 22 | * @param array $assoc_args |
| 23 | 23 | */ |
| 24 | - public function count( $args, $assoc_args ) { |
|
| 24 | + public function count($args, $assoc_args) { |
|
| 25 | 25 | $sessions = WP_Session_Utils::count_sessions(); |
| 26 | 26 | |
| 27 | - \WP_CLI::line( sprintf( '%d sessions currently exist.', absint( $sessions ) ) ); |
|
| 27 | + \WP_CLI::line(sprintf('%d sessions currently exist.', absint($sessions))); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -53,14 +53,14 @@ discard block |
||
| 53 | 53 | * @param array $args |
| 54 | 54 | * @param array $assoc_args |
| 55 | 55 | */ |
| 56 | - public function delete( $args, $assoc_args ) { |
|
| 57 | - if ( isset( $assoc_args['limit'] ) ) { |
|
| 58 | - $limit = absint( $assoc_args['limit'] ); |
|
| 56 | + public function delete($args, $assoc_args) { |
|
| 57 | + if (isset($assoc_args['limit'])) { |
|
| 58 | + $limit = absint($assoc_args['limit']); |
|
| 59 | 59 | |
| 60 | - $count = WP_Session_Utils::delete_old_sessions( $limit ); |
|
| 60 | + $count = WP_Session_Utils::delete_old_sessions($limit); |
|
| 61 | 61 | |
| 62 | - if ( $count > 0 ) { |
|
| 63 | - \WP_CLI::line( sprintf( 'Deleted %d sessions.', $count ) ); |
|
| 62 | + if ($count > 0) { |
|
| 63 | + \WP_CLI::line(sprintf('Deleted %d sessions.', $count)); |
|
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | // Clear memory |
@@ -69,32 +69,32 @@ discard block |
||
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | // Determine if we're deleting all sessions or just a subset. |
| 72 | - $all = isset( $assoc_args['all'] ); |
|
| 72 | + $all = isset($assoc_args['all']); |
|
| 73 | 73 | |
| 74 | 74 | /** |
| 75 | 75 | * Determine the size of each batch for deletion. |
| 76 | 76 | * |
| 77 | 77 | * @param int |
| 78 | 78 | */ |
| 79 | - $batch = isset( $assoc_args['batch'] ) ? absint( $assoc_args['batch'] ) : apply_filters( 'wp_session_delete_batch_size', 1000 ); |
|
| 79 | + $batch = isset($assoc_args['batch']) ? absint($assoc_args['batch']) : apply_filters('wp_session_delete_batch_size', 1000); |
|
| 80 | 80 | |
| 81 | - switch ( $all ) { |
|
| 81 | + switch ($all) { |
|
| 82 | 82 | case true: |
| 83 | 83 | $count = WP_Session_Utils::delete_all_sessions(); |
| 84 | 84 | |
| 85 | - \WP_CLI::line( sprintf( 'Deleted all %d sessions.', $count ) ); |
|
| 85 | + \WP_CLI::line(sprintf('Deleted all %d sessions.', $count)); |
|
| 86 | 86 | break; |
| 87 | 87 | case false: |
| 88 | 88 | do { |
| 89 | - $count = WP_Session_Utils::delete_old_sessions( $batch ); |
|
| 89 | + $count = WP_Session_Utils::delete_old_sessions($batch); |
|
| 90 | 90 | |
| 91 | - if ( $count > 0 ) { |
|
| 92 | - \WP_CLI::line( sprintf( 'Deleted %d sessions.', $count ) ); |
|
| 91 | + if ($count > 0) { |
|
| 92 | + \WP_CLI::line(sprintf('Deleted %d sessions.', $count)); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | // Clear memory |
| 96 | 96 | self::free_up_memory(); |
| 97 | - } while ( $count > 0 ); |
|
| 97 | + } while ($count > 0); |
|
| 98 | 98 | break; |
| 99 | 99 | } |
| 100 | 100 | } |
@@ -120,14 +120,14 @@ discard block |
||
| 120 | 120 | * @param array $args |
| 121 | 121 | * @param array $assoc_args |
| 122 | 122 | */ |
| 123 | - public function generate( $args, $assoc_args ) { |
|
| 124 | - $count = absint( $args[0] ); |
|
| 125 | - $date = isset( $assoc_args['expires'] ) ? $assoc_args['expires'] : null; |
|
| 123 | + public function generate($args, $assoc_args) { |
|
| 124 | + $count = absint($args[0]); |
|
| 125 | + $date = isset($assoc_args['expires']) ? $assoc_args['expires'] : null; |
|
| 126 | 126 | |
| 127 | - $notify = \WP_CLI\Utils\make_progress_bar( 'Generating sessions', $count ); |
|
| 127 | + $notify = \WP_CLI\Utils\make_progress_bar('Generating sessions', $count); |
|
| 128 | 128 | |
| 129 | - for ( $i = 0; $i < $count; $i ++ ) { |
|
| 130 | - WP_Session_Utils::create_dummy_session( $date ); |
|
| 129 | + for ($i = 0; $i < $count; $i++) { |
|
| 130 | + WP_Session_Utils::create_dummy_session($date); |
|
| 131 | 131 | $notify->tick(); |
| 132 | 132 | } |
| 133 | 133 | |
@@ -144,7 +144,7 @@ discard block |
||
| 144 | 144 | global $wp_object_cache, $wpdb; |
| 145 | 145 | $wpdb->queries = array(); |
| 146 | 146 | |
| 147 | - if ( ! is_object( $wp_object_cache ) ) { |
|
| 147 | + if (!is_object($wp_object_cache)) { |
|
| 148 | 148 | return; |
| 149 | 149 | } |
| 150 | 150 | |
@@ -155,4 +155,4 @@ discard block |
||
| 155 | 155 | } |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | -\WP_CLI::add_command( 'session', 'WP_Session_Command' ); |
|
| 159 | 158 | \ No newline at end of file |
| 159 | +\WP_CLI::add_command('session', 'WP_Session_Command'); |
|
| 160 | 160 | \ No newline at end of file |
@@ -16,16 +16,16 @@ discard block |
||
| 16 | 16 | * @return int |
| 17 | 17 | */ |
| 18 | 18 | function wp_session_cache_expire() { |
| 19 | - $wp_session = WP_Session::get_instance(); |
|
| 19 | + $wp_session = WP_Session::get_instance(); |
|
| 20 | 20 | |
| 21 | - return $wp_session->cache_expiration(); |
|
| 21 | + return $wp_session->cache_expiration(); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | /** |
| 25 | 25 | * Alias of wp_session_write_close() |
| 26 | 26 | */ |
| 27 | 27 | function wp_session_commit() { |
| 28 | - wp_session_write_close(); |
|
| 28 | + wp_session_write_close(); |
|
| 29 | 29 | } |
| 30 | 30 | |
| 31 | 31 | /** |
@@ -34,9 +34,9 @@ discard block |
||
| 34 | 34 | * @param string $data |
| 35 | 35 | */ |
| 36 | 36 | function wp_session_decode( $data ) { |
| 37 | - $wp_session = WP_Session::get_instance(); |
|
| 37 | + $wp_session = WP_Session::get_instance(); |
|
| 38 | 38 | |
| 39 | - return $wp_session->json_in( $data ); |
|
| 39 | + return $wp_session->json_in( $data ); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
@@ -45,9 +45,9 @@ discard block |
||
| 45 | 45 | * @return string |
| 46 | 46 | */ |
| 47 | 47 | function wp_session_encode() { |
| 48 | - $wp_session = WP_Session::get_instance(); |
|
| 48 | + $wp_session = WP_Session::get_instance(); |
|
| 49 | 49 | |
| 50 | - return $wp_session->json_out(); |
|
| 50 | + return $wp_session->json_out(); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | /** |
@@ -58,11 +58,11 @@ discard block |
||
| 58 | 58 | * @return bool |
| 59 | 59 | */ |
| 60 | 60 | function wp_session_regenerate_id( $delete_old_session = false ) { |
| 61 | - $wp_session = WP_Session::get_instance(); |
|
| 61 | + $wp_session = WP_Session::get_instance(); |
|
| 62 | 62 | |
| 63 | - $wp_session->regenerate_id( $delete_old_session ); |
|
| 63 | + $wp_session->regenerate_id( $delete_old_session ); |
|
| 64 | 64 | |
| 65 | - return true; |
|
| 65 | + return true; |
|
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
@@ -73,13 +73,13 @@ discard block |
||
| 73 | 73 | * @return bool |
| 74 | 74 | */ |
| 75 | 75 | function wp_session_start() { |
| 76 | - $wp_session = WP_Session::get_instance(); |
|
| 77 | - do_action( 'wp_session_start' ); |
|
| 76 | + $wp_session = WP_Session::get_instance(); |
|
| 77 | + do_action( 'wp_session_start' ); |
|
| 78 | 78 | |
| 79 | - return $wp_session->session_started(); |
|
| 79 | + return $wp_session->session_started(); |
|
| 80 | 80 | } |
| 81 | 81 | if ( ! defined( 'WP_CLI' ) || false === WP_CLI ) { |
| 82 | - add_action( 'plugins_loaded', 'wp_session_start' ); |
|
| 82 | + add_action( 'plugins_loaded', 'wp_session_start' ); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -88,35 +88,35 @@ discard block |
||
| 88 | 88 | * @return int |
| 89 | 89 | */ |
| 90 | 90 | function wp_session_status() { |
| 91 | - $wp_session = WP_Session::get_instance(); |
|
| 91 | + $wp_session = WP_Session::get_instance(); |
|
| 92 | 92 | |
| 93 | - if ( $wp_session->session_started() ) { |
|
| 94 | - return PHP_SESSION_ACTIVE; |
|
| 95 | - } |
|
| 93 | + if ( $wp_session->session_started() ) { |
|
| 94 | + return PHP_SESSION_ACTIVE; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - return PHP_SESSION_NONE; |
|
| 97 | + return PHP_SESSION_NONE; |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | /** |
| 101 | 101 | * Unset all session variables. |
| 102 | 102 | */ |
| 103 | 103 | function wp_session_unset() { |
| 104 | - $wp_session = WP_Session::get_instance(); |
|
| 104 | + $wp_session = WP_Session::get_instance(); |
|
| 105 | 105 | |
| 106 | - $wp_session->reset(); |
|
| 106 | + $wp_session->reset(); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | /** |
| 110 | 110 | * Write session data and end session |
| 111 | 111 | */ |
| 112 | 112 | function wp_session_write_close() { |
| 113 | - $wp_session = WP_Session::get_instance(); |
|
| 113 | + $wp_session = WP_Session::get_instance(); |
|
| 114 | 114 | |
| 115 | - $wp_session->write_data(); |
|
| 116 | - do_action( 'wp_session_commit' ); |
|
| 115 | + $wp_session->write_data(); |
|
| 116 | + do_action( 'wp_session_commit' ); |
|
| 117 | 117 | } |
| 118 | 118 | if ( ! defined( 'WP_CLI' ) || false === WP_CLI ) { |
| 119 | - add_action( 'shutdown', 'wp_session_write_close' ); |
|
| 119 | + add_action( 'shutdown', 'wp_session_write_close' ); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
@@ -127,24 +127,24 @@ discard block |
||
| 127 | 127 | * of a scheduled task or cron job. |
| 128 | 128 | */ |
| 129 | 129 | function wp_session_cleanup() { |
| 130 | - if ( defined( 'WP_SETUP_CONFIG' ) ) { |
|
| 131 | - return; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if ( ! defined( 'WP_INSTALLING' ) ) { |
|
| 135 | - /** |
|
| 136 | - * Determine the size of each batch for deletion. |
|
| 137 | - * |
|
| 138 | - * @param int |
|
| 139 | - */ |
|
| 140 | - $batch_size = apply_filters( 'wp_session_delete_batch_size', 1000 ); |
|
| 141 | - |
|
| 142 | - // Delete a batch of old sessions |
|
| 143 | - WP_Session_Utils::delete_old_sessions( $batch_size ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - // Allow other plugins to hook in to the garbage collection process. |
|
| 147 | - do_action( 'wp_session_cleanup' ); |
|
| 130 | + if ( defined( 'WP_SETUP_CONFIG' ) ) { |
|
| 131 | + return; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if ( ! defined( 'WP_INSTALLING' ) ) { |
|
| 135 | + /** |
|
| 136 | + * Determine the size of each batch for deletion. |
|
| 137 | + * |
|
| 138 | + * @param int |
|
| 139 | + */ |
|
| 140 | + $batch_size = apply_filters( 'wp_session_delete_batch_size', 1000 ); |
|
| 141 | + |
|
| 142 | + // Delete a batch of old sessions |
|
| 143 | + WP_Session_Utils::delete_old_sessions( $batch_size ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + // Allow other plugins to hook in to the garbage collection process. |
|
| 147 | + do_action( 'wp_session_cleanup' ); |
|
| 148 | 148 | } |
| 149 | 149 | add_action( 'wp_session_garbage_collection', 'wp_session_cleanup' ); |
| 150 | 150 | |
@@ -152,8 +152,8 @@ discard block |
||
| 152 | 152 | * Register the garbage collector as a twice daily event. |
| 153 | 153 | */ |
| 154 | 154 | function wp_session_register_garbage_collection() { |
| 155 | - if ( ! wp_next_scheduled( 'wp_session_garbage_collection' ) ) { |
|
| 156 | - wp_schedule_event( time(), 'hourly', 'wp_session_garbage_collection' ); |
|
| 157 | - } |
|
| 155 | + if ( ! wp_next_scheduled( 'wp_session_garbage_collection' ) ) { |
|
| 156 | + wp_schedule_event( time(), 'hourly', 'wp_session_garbage_collection' ); |
|
| 157 | + } |
|
| 158 | 158 | } |
| 159 | 159 | add_action( 'wp', 'wp_session_register_garbage_collection' ); |
@@ -33,10 +33,10 @@ discard block |
||
| 33 | 33 | * |
| 34 | 34 | * @param string $data |
| 35 | 35 | */ |
| 36 | -function wp_session_decode( $data ) { |
|
| 36 | +function wp_session_decode($data) { |
|
| 37 | 37 | $wp_session = WP_Session::get_instance(); |
| 38 | 38 | |
| 39 | - return $wp_session->json_in( $data ); |
|
| 39 | + return $wp_session->json_in($data); |
|
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
@@ -57,10 +57,10 @@ discard block |
||
| 57 | 57 | * |
| 58 | 58 | * @return bool |
| 59 | 59 | */ |
| 60 | -function wp_session_regenerate_id( $delete_old_session = false ) { |
|
| 60 | +function wp_session_regenerate_id($delete_old_session = false) { |
|
| 61 | 61 | $wp_session = WP_Session::get_instance(); |
| 62 | 62 | |
| 63 | - $wp_session->regenerate_id( $delete_old_session ); |
|
| 63 | + $wp_session->regenerate_id($delete_old_session); |
|
| 64 | 64 | |
| 65 | 65 | return true; |
| 66 | 66 | } |
@@ -74,12 +74,12 @@ discard block |
||
| 74 | 74 | */ |
| 75 | 75 | function wp_session_start() { |
| 76 | 76 | $wp_session = WP_Session::get_instance(); |
| 77 | - do_action( 'wp_session_start' ); |
|
| 77 | + do_action('wp_session_start'); |
|
| 78 | 78 | |
| 79 | 79 | return $wp_session->session_started(); |
| 80 | 80 | } |
| 81 | -if ( ! defined( 'WP_CLI' ) || false === WP_CLI ) { |
|
| 82 | - add_action( 'plugins_loaded', 'wp_session_start' ); |
|
| 81 | +if (!defined('WP_CLI') || false === WP_CLI) { |
|
| 82 | + add_action('plugins_loaded', 'wp_session_start'); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -90,7 +90,7 @@ discard block |
||
| 90 | 90 | function wp_session_status() { |
| 91 | 91 | $wp_session = WP_Session::get_instance(); |
| 92 | 92 | |
| 93 | - if ( $wp_session->session_started() ) { |
|
| 93 | + if ($wp_session->session_started()) { |
|
| 94 | 94 | return PHP_SESSION_ACTIVE; |
| 95 | 95 | } |
| 96 | 96 | |
@@ -113,10 +113,10 @@ discard block |
||
| 113 | 113 | $wp_session = WP_Session::get_instance(); |
| 114 | 114 | |
| 115 | 115 | $wp_session->write_data(); |
| 116 | - do_action( 'wp_session_commit' ); |
|
| 116 | + do_action('wp_session_commit'); |
|
| 117 | 117 | } |
| 118 | -if ( ! defined( 'WP_CLI' ) || false === WP_CLI ) { |
|
| 119 | - add_action( 'shutdown', 'wp_session_write_close' ); |
|
| 118 | +if (!defined('WP_CLI') || false === WP_CLI) { |
|
| 119 | + add_action('shutdown', 'wp_session_write_close'); |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
@@ -127,33 +127,33 @@ discard block |
||
| 127 | 127 | * of a scheduled task or cron job. |
| 128 | 128 | */ |
| 129 | 129 | function wp_session_cleanup() { |
| 130 | - if ( defined( 'WP_SETUP_CONFIG' ) ) { |
|
| 130 | + if (defined('WP_SETUP_CONFIG')) { |
|
| 131 | 131 | return; |
| 132 | 132 | } |
| 133 | 133 | |
| 134 | - if ( ! defined( 'WP_INSTALLING' ) ) { |
|
| 134 | + if (!defined('WP_INSTALLING')) { |
|
| 135 | 135 | /** |
| 136 | 136 | * Determine the size of each batch for deletion. |
| 137 | 137 | * |
| 138 | 138 | * @param int |
| 139 | 139 | */ |
| 140 | - $batch_size = apply_filters( 'wp_session_delete_batch_size', 1000 ); |
|
| 140 | + $batch_size = apply_filters('wp_session_delete_batch_size', 1000); |
|
| 141 | 141 | |
| 142 | 142 | // Delete a batch of old sessions |
| 143 | - WP_Session_Utils::delete_old_sessions( $batch_size ); |
|
| 143 | + WP_Session_Utils::delete_old_sessions($batch_size); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | // Allow other plugins to hook in to the garbage collection process. |
| 147 | - do_action( 'wp_session_cleanup' ); |
|
| 147 | + do_action('wp_session_cleanup'); |
|
| 148 | 148 | } |
| 149 | -add_action( 'wp_session_garbage_collection', 'wp_session_cleanup' ); |
|
| 149 | +add_action('wp_session_garbage_collection', 'wp_session_cleanup'); |
|
| 150 | 150 | |
| 151 | 151 | /** |
| 152 | 152 | * Register the garbage collector as a twice daily event. |
| 153 | 153 | */ |
| 154 | 154 | function wp_session_register_garbage_collection() { |
| 155 | - if ( ! wp_next_scheduled( 'wp_session_garbage_collection' ) ) { |
|
| 156 | - wp_schedule_event( time(), 'hourly', 'wp_session_garbage_collection' ); |
|
| 155 | + if (!wp_next_scheduled('wp_session_garbage_collection')) { |
|
| 156 | + wp_schedule_event(time(), 'hourly', 'wp_session_garbage_collection'); |
|
| 157 | 157 | } |
| 158 | 158 | } |
| 159 | -add_action( 'wp', 'wp_session_register_garbage_collection' ); |
|
| 159 | +add_action('wp', 'wp_session_register_garbage_collection'); |
|
@@ -6,134 +6,134 @@ |
||
| 6 | 6 | * THIS CLASS SHOULD NEVER BE INSTANTIATED |
| 7 | 7 | */ |
| 8 | 8 | class WP_Session_Utils { |
| 9 | - /** |
|
| 10 | - * Count the total sessions in the database. |
|
| 11 | - * |
|
| 12 | - * @global wpdb $wpdb |
|
| 13 | - * |
|
| 14 | - * @return int |
|
| 15 | - */ |
|
| 16 | - public static function count_sessions() { |
|
| 17 | - global $wpdb; |
|
| 18 | - |
|
| 19 | - $query = "SELECT COUNT(*) FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%'"; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Filter the query in case tables are non-standard. |
|
| 23 | - * |
|
| 24 | - * @param string $query Database count query |
|
| 25 | - */ |
|
| 26 | - $query = apply_filters( 'wp_session_count_query', $query ); |
|
| 27 | - |
|
| 28 | - $sessions = $wpdb->get_var( $query ); |
|
| 29 | - |
|
| 30 | - return absint( $sessions ); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * Create a new, random session in the database. |
|
| 35 | - * |
|
| 36 | - * @param null|string $date |
|
| 37 | - */ |
|
| 38 | - public static function create_dummy_session( $date = null ) { |
|
| 39 | - // Generate our date |
|
| 40 | - if ( null !== $date ) { |
|
| 41 | - $time = strtotime( $date ); |
|
| 42 | - |
|
| 43 | - if ( false === $time ) { |
|
| 44 | - $date = null; |
|
| 45 | - } else { |
|
| 46 | - $expires = date( 'U', strtotime( $date ) ); |
|
| 47 | - } |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - // If null was passed, or if the string parsing failed, fall back on a default |
|
| 51 | - if ( null === $date ) { |
|
| 52 | - /** |
|
| 53 | - * Filter the expiration of the session in the database |
|
| 54 | - * |
|
| 55 | - * @param int |
|
| 56 | - */ |
|
| 57 | - $expires = time() + (int) apply_filters( 'wp_session_expiration', 30 * 60 ); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - $session_id = self::generate_id(); |
|
| 61 | - |
|
| 62 | - // Store the session |
|
| 63 | - add_option( "_wp_session_{$session_id}", array(), '', 'no' ); |
|
| 64 | - add_option( "_wp_session_expires_{$session_id}", $expires, '', 'no' ); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Delete old sessions from the database. |
|
| 69 | - * |
|
| 70 | - * @param int $limit Maximum number of sessions to delete. |
|
| 71 | - * |
|
| 72 | - * @global wpdb $wpdb |
|
| 73 | - * |
|
| 74 | - * @return int Sessions deleted. |
|
| 75 | - */ |
|
| 76 | - public static function delete_old_sessions( $limit = 1000 ) { |
|
| 77 | - global $wpdb; |
|
| 78 | - |
|
| 79 | - $limit = absint( $limit ); |
|
| 80 | - $keys = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%' ORDER BY option_value ASC LIMIT 0, {$limit}" ); |
|
| 81 | - |
|
| 82 | - $now = time(); |
|
| 83 | - $expired = array(); |
|
| 84 | - $count = 0; |
|
| 85 | - |
|
| 86 | - foreach( $keys as $expiration ) { |
|
| 87 | - $key = $expiration->option_name; |
|
| 88 | - $expires = $expiration->option_value; |
|
| 89 | - |
|
| 90 | - if ( $now > $expires ) { |
|
| 91 | - $session_id = preg_replace("/[^A-Za-z0-9_]/", '', substr( $key, 20 ) ); |
|
| 92 | - |
|
| 93 | - $expired[] = $key; |
|
| 94 | - $expired[] = "_wp_session_{$session_id}"; |
|
| 95 | - |
|
| 96 | - $count += 1; |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - // Delete expired sessions |
|
| 101 | - if ( ! empty( $expired ) ) { |
|
| 102 | - $placeholders = array_fill( 0, count( $expired ), '%s' ); |
|
| 103 | - $format = implode( ', ', $placeholders ); |
|
| 104 | - $query = "DELETE FROM $wpdb->options WHERE option_name IN ($format)"; |
|
| 105 | - |
|
| 106 | - $prepared = $wpdb->prepare( $query, $expired ); |
|
| 107 | - $wpdb->query( $prepared ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - return $count; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Remove all sessions from the database, regardless of expiration. |
|
| 115 | - * |
|
| 116 | - * @global wpdb $wpdb |
|
| 117 | - * |
|
| 118 | - * @return int Sessions deleted |
|
| 119 | - */ |
|
| 120 | - public static function delete_all_sessions() { |
|
| 121 | - global $wpdb; |
|
| 122 | - |
|
| 123 | - $count = $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_wp_session_%'" ); |
|
| 124 | - |
|
| 125 | - return (int) ( $count / 2 ); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Generate a new, random session ID. |
|
| 130 | - * |
|
| 131 | - * @return string |
|
| 132 | - */ |
|
| 133 | - public static function generate_id() { |
|
| 134 | - require_once( ABSPATH . 'wp-includes/class-phpass.php' ); |
|
| 135 | - $hash = new PasswordHash( 8, false ); |
|
| 136 | - |
|
| 137 | - return md5( $hash->get_random_bytes( 32 ) ); |
|
| 138 | - } |
|
| 9 | + /** |
|
| 10 | + * Count the total sessions in the database. |
|
| 11 | + * |
|
| 12 | + * @global wpdb $wpdb |
|
| 13 | + * |
|
| 14 | + * @return int |
|
| 15 | + */ |
|
| 16 | + public static function count_sessions() { |
|
| 17 | + global $wpdb; |
|
| 18 | + |
|
| 19 | + $query = "SELECT COUNT(*) FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%'"; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Filter the query in case tables are non-standard. |
|
| 23 | + * |
|
| 24 | + * @param string $query Database count query |
|
| 25 | + */ |
|
| 26 | + $query = apply_filters( 'wp_session_count_query', $query ); |
|
| 27 | + |
|
| 28 | + $sessions = $wpdb->get_var( $query ); |
|
| 29 | + |
|
| 30 | + return absint( $sessions ); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * Create a new, random session in the database. |
|
| 35 | + * |
|
| 36 | + * @param null|string $date |
|
| 37 | + */ |
|
| 38 | + public static function create_dummy_session( $date = null ) { |
|
| 39 | + // Generate our date |
|
| 40 | + if ( null !== $date ) { |
|
| 41 | + $time = strtotime( $date ); |
|
| 42 | + |
|
| 43 | + if ( false === $time ) { |
|
| 44 | + $date = null; |
|
| 45 | + } else { |
|
| 46 | + $expires = date( 'U', strtotime( $date ) ); |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + // If null was passed, or if the string parsing failed, fall back on a default |
|
| 51 | + if ( null === $date ) { |
|
| 52 | + /** |
|
| 53 | + * Filter the expiration of the session in the database |
|
| 54 | + * |
|
| 55 | + * @param int |
|
| 56 | + */ |
|
| 57 | + $expires = time() + (int) apply_filters( 'wp_session_expiration', 30 * 60 ); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + $session_id = self::generate_id(); |
|
| 61 | + |
|
| 62 | + // Store the session |
|
| 63 | + add_option( "_wp_session_{$session_id}", array(), '', 'no' ); |
|
| 64 | + add_option( "_wp_session_expires_{$session_id}", $expires, '', 'no' ); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Delete old sessions from the database. |
|
| 69 | + * |
|
| 70 | + * @param int $limit Maximum number of sessions to delete. |
|
| 71 | + * |
|
| 72 | + * @global wpdb $wpdb |
|
| 73 | + * |
|
| 74 | + * @return int Sessions deleted. |
|
| 75 | + */ |
|
| 76 | + public static function delete_old_sessions( $limit = 1000 ) { |
|
| 77 | + global $wpdb; |
|
| 78 | + |
|
| 79 | + $limit = absint( $limit ); |
|
| 80 | + $keys = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%' ORDER BY option_value ASC LIMIT 0, {$limit}" ); |
|
| 81 | + |
|
| 82 | + $now = time(); |
|
| 83 | + $expired = array(); |
|
| 84 | + $count = 0; |
|
| 85 | + |
|
| 86 | + foreach( $keys as $expiration ) { |
|
| 87 | + $key = $expiration->option_name; |
|
| 88 | + $expires = $expiration->option_value; |
|
| 89 | + |
|
| 90 | + if ( $now > $expires ) { |
|
| 91 | + $session_id = preg_replace("/[^A-Za-z0-9_]/", '', substr( $key, 20 ) ); |
|
| 92 | + |
|
| 93 | + $expired[] = $key; |
|
| 94 | + $expired[] = "_wp_session_{$session_id}"; |
|
| 95 | + |
|
| 96 | + $count += 1; |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + // Delete expired sessions |
|
| 101 | + if ( ! empty( $expired ) ) { |
|
| 102 | + $placeholders = array_fill( 0, count( $expired ), '%s' ); |
|
| 103 | + $format = implode( ', ', $placeholders ); |
|
| 104 | + $query = "DELETE FROM $wpdb->options WHERE option_name IN ($format)"; |
|
| 105 | + |
|
| 106 | + $prepared = $wpdb->prepare( $query, $expired ); |
|
| 107 | + $wpdb->query( $prepared ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + return $count; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Remove all sessions from the database, regardless of expiration. |
|
| 115 | + * |
|
| 116 | + * @global wpdb $wpdb |
|
| 117 | + * |
|
| 118 | + * @return int Sessions deleted |
|
| 119 | + */ |
|
| 120 | + public static function delete_all_sessions() { |
|
| 121 | + global $wpdb; |
|
| 122 | + |
|
| 123 | + $count = $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_wp_session_%'" ); |
|
| 124 | + |
|
| 125 | + return (int) ( $count / 2 ); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Generate a new, random session ID. |
|
| 130 | + * |
|
| 131 | + * @return string |
|
| 132 | + */ |
|
| 133 | + public static function generate_id() { |
|
| 134 | + require_once( ABSPATH . 'wp-includes/class-phpass.php' ); |
|
| 135 | + $hash = new PasswordHash( 8, false ); |
|
| 136 | + |
|
| 137 | + return md5( $hash->get_random_bytes( 32 ) ); |
|
| 138 | + } |
|
| 139 | 139 | } |
| 140 | 140 | \ No newline at end of file |
@@ -23,11 +23,11 @@ discard block |
||
| 23 | 23 | * |
| 24 | 24 | * @param string $query Database count query |
| 25 | 25 | */ |
| 26 | - $query = apply_filters( 'wp_session_count_query', $query ); |
|
| 26 | + $query = apply_filters('wp_session_count_query', $query); |
|
| 27 | 27 | |
| 28 | - $sessions = $wpdb->get_var( $query ); |
|
| 28 | + $sessions = $wpdb->get_var($query); |
|
| 29 | 29 | |
| 30 | - return absint( $sessions ); |
|
| 30 | + return absint($sessions); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | /** |
@@ -35,33 +35,33 @@ discard block |
||
| 35 | 35 | * |
| 36 | 36 | * @param null|string $date |
| 37 | 37 | */ |
| 38 | - public static function create_dummy_session( $date = null ) { |
|
| 38 | + public static function create_dummy_session($date = null) { |
|
| 39 | 39 | // Generate our date |
| 40 | - if ( null !== $date ) { |
|
| 41 | - $time = strtotime( $date ); |
|
| 40 | + if (null !== $date) { |
|
| 41 | + $time = strtotime($date); |
|
| 42 | 42 | |
| 43 | - if ( false === $time ) { |
|
| 43 | + if (false === $time) { |
|
| 44 | 44 | $date = null; |
| 45 | 45 | } else { |
| 46 | - $expires = date( 'U', strtotime( $date ) ); |
|
| 46 | + $expires = date('U', strtotime($date)); |
|
| 47 | 47 | } |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | // If null was passed, or if the string parsing failed, fall back on a default |
| 51 | - if ( null === $date ) { |
|
| 51 | + if (null === $date) { |
|
| 52 | 52 | /** |
| 53 | 53 | * Filter the expiration of the session in the database |
| 54 | 54 | * |
| 55 | 55 | * @param int |
| 56 | 56 | */ |
| 57 | - $expires = time() + (int) apply_filters( 'wp_session_expiration', 30 * 60 ); |
|
| 57 | + $expires = time() + (int)apply_filters('wp_session_expiration', 30 * 60); |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | $session_id = self::generate_id(); |
| 61 | 61 | |
| 62 | 62 | // Store the session |
| 63 | - add_option( "_wp_session_{$session_id}", array(), '', 'no' ); |
|
| 64 | - add_option( "_wp_session_expires_{$session_id}", $expires, '', 'no' ); |
|
| 63 | + add_option("_wp_session_{$session_id}", array(), '', 'no'); |
|
| 64 | + add_option("_wp_session_expires_{$session_id}", $expires, '', 'no'); |
|
| 65 | 65 | } |
| 66 | 66 | |
| 67 | 67 | /** |
@@ -73,22 +73,22 @@ discard block |
||
| 73 | 73 | * |
| 74 | 74 | * @return int Sessions deleted. |
| 75 | 75 | */ |
| 76 | - public static function delete_old_sessions( $limit = 1000 ) { |
|
| 76 | + public static function delete_old_sessions($limit = 1000) { |
|
| 77 | 77 | global $wpdb; |
| 78 | 78 | |
| 79 | - $limit = absint( $limit ); |
|
| 80 | - $keys = $wpdb->get_results( "SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%' ORDER BY option_value ASC LIMIT 0, {$limit}" ); |
|
| 79 | + $limit = absint($limit); |
|
| 80 | + $keys = $wpdb->get_results("SELECT option_name, option_value FROM $wpdb->options WHERE option_name LIKE '_wp_session_expires_%' ORDER BY option_value ASC LIMIT 0, {$limit}"); |
|
| 81 | 81 | |
| 82 | 82 | $now = time(); |
| 83 | 83 | $expired = array(); |
| 84 | 84 | $count = 0; |
| 85 | 85 | |
| 86 | - foreach( $keys as $expiration ) { |
|
| 86 | + foreach ($keys as $expiration) { |
|
| 87 | 87 | $key = $expiration->option_name; |
| 88 | 88 | $expires = $expiration->option_value; |
| 89 | 89 | |
| 90 | - if ( $now > $expires ) { |
|
| 91 | - $session_id = preg_replace("/[^A-Za-z0-9_]/", '', substr( $key, 20 ) ); |
|
| 90 | + if ($now > $expires) { |
|
| 91 | + $session_id = preg_replace("/[^A-Za-z0-9_]/", '', substr($key, 20)); |
|
| 92 | 92 | |
| 93 | 93 | $expired[] = $key; |
| 94 | 94 | $expired[] = "_wp_session_{$session_id}"; |
@@ -98,13 +98,13 @@ discard block |
||
| 98 | 98 | } |
| 99 | 99 | |
| 100 | 100 | // Delete expired sessions |
| 101 | - if ( ! empty( $expired ) ) { |
|
| 102 | - $placeholders = array_fill( 0, count( $expired ), '%s' ); |
|
| 103 | - $format = implode( ', ', $placeholders ); |
|
| 101 | + if (!empty($expired)) { |
|
| 102 | + $placeholders = array_fill(0, count($expired), '%s'); |
|
| 103 | + $format = implode(', ', $placeholders); |
|
| 104 | 104 | $query = "DELETE FROM $wpdb->options WHERE option_name IN ($format)"; |
| 105 | 105 | |
| 106 | - $prepared = $wpdb->prepare( $query, $expired ); |
|
| 107 | - $wpdb->query( $prepared ); |
|
| 106 | + $prepared = $wpdb->prepare($query, $expired); |
|
| 107 | + $wpdb->query($prepared); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | return $count; |
@@ -120,9 +120,9 @@ discard block |
||
| 120 | 120 | public static function delete_all_sessions() { |
| 121 | 121 | global $wpdb; |
| 122 | 122 | |
| 123 | - $count = $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE '_wp_session_%'" ); |
|
| 123 | + $count = $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE '_wp_session_%'"); |
|
| 124 | 124 | |
| 125 | - return (int) ( $count / 2 ); |
|
| 125 | + return (int)($count / 2); |
|
| 126 | 126 | } |
| 127 | 127 | |
| 128 | 128 | /** |
@@ -131,9 +131,9 @@ discard block |
||
| 131 | 131 | * @return string |
| 132 | 132 | */ |
| 133 | 133 | public static function generate_id() { |
| 134 | - require_once( ABSPATH . 'wp-includes/class-phpass.php' ); |
|
| 135 | - $hash = new PasswordHash( 8, false ); |
|
| 134 | + require_once(ABSPATH . 'wp-includes/class-phpass.php'); |
|
| 135 | + $hash = new PasswordHash(8, false); |
|
| 136 | 136 | |
| 137 | - return md5( $hash->get_random_bytes( 32 ) ); |
|
| 137 | + return md5($hash->get_random_bytes(32)); |
|
| 138 | 138 | } |
| 139 | 139 | } |
| 140 | 140 | \ No newline at end of file |
@@ -1,6 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | // Exit if accessed directly |
| 3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
| 3 | +if ( ! defined( 'ABSPATH' ) ) { |
|
| 4 | + exit; |
|
| 5 | +} |
|
| 4 | 6 | |
| 5 | 7 | class WPInv_Item { |
| 6 | 8 | public $ID = 0; |
@@ -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; |
@@ -33,26 +33,26 @@ discard block |
||
| 33 | 33 | public $filter; |
| 34 | 34 | |
| 35 | 35 | |
| 36 | - public function __construct( $_id = false, $_args = array() ) { |
|
| 37 | - $item = WP_Post::get_instance( $_id ); |
|
| 38 | - return $this->setup_item( $item ); |
|
| 36 | + public function __construct($_id = false, $_args = array()) { |
|
| 37 | + $item = WP_Post::get_instance($_id); |
|
| 38 | + return $this->setup_item($item); |
|
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - private function setup_item( $item ) { |
|
| 42 | - if( ! is_object( $item ) ) { |
|
| 41 | + private function setup_item($item) { |
|
| 42 | + if (!is_object($item)) { |
|
| 43 | 43 | return false; |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - if( ! is_a( $item, 'WP_Post' ) ) { |
|
| 46 | + if (!is_a($item, 'WP_Post')) { |
|
| 47 | 47 | return false; |
| 48 | 48 | } |
| 49 | 49 | |
| 50 | - if( 'wpi_item' !== $item->post_type ) { |
|
| 50 | + if ('wpi_item' !== $item->post_type) { |
|
| 51 | 51 | return false; |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | - foreach ( $item as $key => $value ) { |
|
| 55 | - switch ( $key ) { |
|
| 54 | + foreach ($item as $key => $value) { |
|
| 55 | + switch ($key) { |
|
| 56 | 56 | default: |
| 57 | 57 | $this->$key = $value; |
| 58 | 58 | break; |
@@ -62,38 +62,38 @@ discard block |
||
| 62 | 62 | return true; |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | - public function __get( $key ) { |
|
| 66 | - if ( method_exists( $this, 'get_' . $key ) ) { |
|
| 67 | - return call_user_func( array( $this, 'get_' . $key ) ); |
|
| 65 | + public function __get($key) { |
|
| 66 | + if (method_exists($this, 'get_' . $key)) { |
|
| 67 | + return call_user_func(array($this, 'get_' . $key)); |
|
| 68 | 68 | } else { |
| 69 | - return new WP_Error( 'wpinv-item-invalid-property', sprintf( __( 'Can\'t get property %s', 'invoicing' ), $key ) ); |
|
| 69 | + return new WP_Error('wpinv-item-invalid-property', sprintf(__('Can\'t get property %s', 'invoicing'), $key)); |
|
| 70 | 70 | } |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - public function create( $data = array(), $wp_error = false ) { |
|
| 74 | - if ( $this->ID != 0 ) { |
|
| 73 | + public function create($data = array(), $wp_error = false) { |
|
| 74 | + if ($this->ID != 0) { |
|
| 75 | 75 | return false; |
| 76 | 76 | } |
| 77 | 77 | |
| 78 | 78 | $defaults = array( |
| 79 | 79 | 'post_type' => 'wpi_item', |
| 80 | 80 | 'post_status' => 'draft', |
| 81 | - 'post_title' => __( 'New Invoice Item', 'invoicing' ) |
|
| 81 | + 'post_title' => __('New Invoice Item', 'invoicing') |
|
| 82 | 82 | ); |
| 83 | 83 | |
| 84 | - $args = wp_parse_args( $data, $defaults ); |
|
| 84 | + $args = wp_parse_args($data, $defaults); |
|
| 85 | 85 | |
| 86 | - do_action( 'wpinv_item_pre_create', $args ); |
|
| 86 | + do_action('wpinv_item_pre_create', $args); |
|
| 87 | 87 | |
| 88 | - $id = wp_insert_post( $args, $wp_error ); |
|
| 88 | + $id = wp_insert_post($args, $wp_error); |
|
| 89 | 89 | if ($wp_error && is_wp_error($id)) { |
| 90 | 90 | return $id; |
| 91 | 91 | } |
| 92 | - if ( !$id ) { |
|
| 92 | + if (!$id) { |
|
| 93 | 93 | return false; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - $item = WP_Post::get_instance( $id ); |
|
| 96 | + $item = WP_Post::get_instance($id); |
|
| 97 | 97 | |
| 98 | 98 | if (!empty($item) && !empty($data['meta'])) { |
| 99 | 99 | $this->ID = $item->ID; |
@@ -101,47 +101,47 @@ discard block |
||
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | // Set custom id if not set. |
| 104 | - if ( empty( $data['meta']['custom_id'] ) && !$this->get_custom_id() ) { |
|
| 105 | - $this->save_metas( array( 'custom_id' => $id ) ); |
|
| 104 | + if (empty($data['meta']['custom_id']) && !$this->get_custom_id()) { |
|
| 105 | + $this->save_metas(array('custom_id' => $id)); |
|
| 106 | 106 | } |
| 107 | 107 | |
| 108 | - do_action( 'wpinv_item_create', $id, $args ); |
|
| 108 | + do_action('wpinv_item_create', $id, $args); |
|
| 109 | 109 | |
| 110 | - return $this->setup_item( $item ); |
|
| 110 | + return $this->setup_item($item); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | - public function update( $data = array(), $wp_error = false ) { |
|
| 114 | - if ( !$this->ID > 0 ) { |
|
| 113 | + public function update($data = array(), $wp_error = false) { |
|
| 114 | + if (!$this->ID > 0) { |
|
| 115 | 115 | return false; |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | 118 | $data['ID'] = $this->ID; |
| 119 | 119 | |
| 120 | - do_action( 'wpinv_item_pre_update', $data ); |
|
| 120 | + do_action('wpinv_item_pre_update', $data); |
|
| 121 | 121 | |
| 122 | - $id = wp_update_post( $data, $wp_error ); |
|
| 122 | + $id = wp_update_post($data, $wp_error); |
|
| 123 | 123 | if ($wp_error && is_wp_error($id)) { |
| 124 | 124 | return $id; |
| 125 | 125 | } |
| 126 | 126 | |
| 127 | - if ( !$id ) { |
|
| 127 | + if (!$id) { |
|
| 128 | 128 | return false; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | - $item = WP_Post::get_instance( $id ); |
|
| 131 | + $item = WP_Post::get_instance($id); |
|
| 132 | 132 | if (!empty($item) && !empty($data['meta'])) { |
| 133 | 133 | $this->ID = $item->ID; |
| 134 | 134 | $this->save_metas($data['meta']); |
| 135 | 135 | } |
| 136 | 136 | |
| 137 | 137 | // Set custom id if not set. |
| 138 | - if ( empty( $data['meta']['custom_id'] ) && !$this->get_custom_id() ) { |
|
| 139 | - $this->save_metas( array( 'custom_id' => $id ) ); |
|
| 138 | + if (empty($data['meta']['custom_id']) && !$this->get_custom_id()) { |
|
| 139 | + $this->save_metas(array('custom_id' => $id)); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | - do_action( 'wpinv_item_update', $id, $data ); |
|
| 142 | + do_action('wpinv_item_update', $id, $data); |
|
| 143 | 143 | |
| 144 | - return $this->setup_item( $item ); |
|
| 144 | + return $this->setup_item($item); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | public function get_ID() { |
@@ -149,105 +149,105 @@ discard block |
||
| 149 | 149 | } |
| 150 | 150 | |
| 151 | 151 | public function get_name() { |
| 152 | - return get_the_title( $this->ID ); |
|
| 152 | + return get_the_title($this->ID); |
|
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | public function get_summary() { |
| 156 | - return get_the_excerpt( $this->ID ); |
|
| 156 | + return get_the_excerpt($this->ID); |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | public function get_price() { |
| 160 | - if ( ! isset( $this->price ) ) { |
|
| 161 | - $this->price = get_post_meta( $this->ID, '_wpinv_price', true ); |
|
| 160 | + if (!isset($this->price)) { |
|
| 161 | + $this->price = get_post_meta($this->ID, '_wpinv_price', true); |
|
| 162 | 162 | |
| 163 | - if ( $this->price ) { |
|
| 164 | - $this->price = wpinv_sanitize_amount( $this->price ); |
|
| 163 | + if ($this->price) { |
|
| 164 | + $this->price = wpinv_sanitize_amount($this->price); |
|
| 165 | 165 | } else { |
| 166 | 166 | $this->price = 0; |
| 167 | 167 | } |
| 168 | 168 | } |
| 169 | 169 | |
| 170 | - return apply_filters( 'wpinv_get_item_price', $this->price, $this->ID ); |
|
| 170 | + return apply_filters('wpinv_get_item_price', $this->price, $this->ID); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | public function get_the_price() { |
| 174 | - $item_price = wpinv_price( wpinv_format_amount( $this->price ) ); |
|
| 174 | + $item_price = wpinv_price(wpinv_format_amount($this->price)); |
|
| 175 | 175 | |
| 176 | - return apply_filters( 'wpinv_get_the_item_price', $item_price, $this->ID ); |
|
| 176 | + return apply_filters('wpinv_get_the_item_price', $item_price, $this->ID); |
|
| 177 | 177 | } |
| 178 | 178 | |
| 179 | 179 | public function get_vat_rule() { |
| 180 | 180 | global $wpinv_euvat; |
| 181 | 181 | |
| 182 | - if( !isset( $this->vat_rule ) ) { |
|
| 183 | - $this->vat_rule = get_post_meta( $this->ID, '_wpinv_vat_rule', true ); |
|
| 182 | + if (!isset($this->vat_rule)) { |
|
| 183 | + $this->vat_rule = get_post_meta($this->ID, '_wpinv_vat_rule', true); |
|
| 184 | 184 | |
| 185 | - if ( empty( $this->vat_rule ) ) { |
|
| 185 | + if (empty($this->vat_rule)) { |
|
| 186 | 186 | $this->vat_rule = $wpinv_euvat->allow_vat_rules() ? 'digital' : 'physical'; |
| 187 | 187 | } |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - return apply_filters( 'wpinv_get_item_vat_rule', $this->vat_rule, $this->ID ); |
|
| 190 | + return apply_filters('wpinv_get_item_vat_rule', $this->vat_rule, $this->ID); |
|
| 191 | 191 | } |
| 192 | 192 | |
| 193 | 193 | public function get_vat_class() { |
| 194 | - if( !isset( $this->vat_class ) ) { |
|
| 195 | - $this->vat_class = get_post_meta( $this->ID, '_wpinv_vat_class', true ); |
|
| 194 | + if (!isset($this->vat_class)) { |
|
| 195 | + $this->vat_class = get_post_meta($this->ID, '_wpinv_vat_class', true); |
|
| 196 | 196 | |
| 197 | - if ( empty( $this->vat_class ) ) { |
|
| 197 | + if (empty($this->vat_class)) { |
|
| 198 | 198 | $this->vat_class = '_standard'; |
| 199 | 199 | } |
| 200 | 200 | } |
| 201 | 201 | |
| 202 | - return apply_filters( 'wpinv_get_item_vat_class', $this->vat_class, $this->ID ); |
|
| 202 | + return apply_filters('wpinv_get_item_vat_class', $this->vat_class, $this->ID); |
|
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | public function get_type() { |
| 206 | - if( ! isset( $this->type ) ) { |
|
| 207 | - $this->type = get_post_meta( $this->ID, '_wpinv_type', true ); |
|
| 206 | + if (!isset($this->type)) { |
|
| 207 | + $this->type = get_post_meta($this->ID, '_wpinv_type', true); |
|
| 208 | 208 | |
| 209 | - if ( empty( $this->type ) ) { |
|
| 209 | + if (empty($this->type)) { |
|
| 210 | 210 | $this->type = 'custom'; |
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | - return apply_filters( 'wpinv_get_item_type', $this->type, $this->ID ); |
|
| 214 | + return apply_filters('wpinv_get_item_type', $this->type, $this->ID); |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | public function get_custom_id() { |
| 218 | - $custom_id = get_post_meta( $this->ID, '_wpinv_custom_id', true ); |
|
| 218 | + $custom_id = get_post_meta($this->ID, '_wpinv_custom_id', true); |
|
| 219 | 219 | |
| 220 | - return apply_filters( 'wpinv_get_item_custom_id', $custom_id, $this->ID ); |
|
| 220 | + return apply_filters('wpinv_get_item_custom_id', $custom_id, $this->ID); |
|
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | public function get_custom_name() { |
| 224 | - $custom_name = get_post_meta( $this->ID, '_wpinv_custom_name', true ); |
|
| 224 | + $custom_name = get_post_meta($this->ID, '_wpinv_custom_name', true); |
|
| 225 | 225 | |
| 226 | - return apply_filters( 'wpinv_get_item_custom_name', $custom_name, $this->ID ); |
|
| 226 | + return apply_filters('wpinv_get_item_custom_name', $custom_name, $this->ID); |
|
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | public function get_custom_singular_name() { |
| 230 | - $custom_singular_name = get_post_meta( $this->ID, '_wpinv_custom_singular_name', true ); |
|
| 230 | + $custom_singular_name = get_post_meta($this->ID, '_wpinv_custom_singular_name', true); |
|
| 231 | 231 | |
| 232 | - return apply_filters( 'wpinv_get_item_custom_singular_name', $custom_singular_name, $this->ID ); |
|
| 232 | + return apply_filters('wpinv_get_item_custom_singular_name', $custom_singular_name, $this->ID); |
|
| 233 | 233 | } |
| 234 | 234 | |
| 235 | 235 | public function is_recurring() { |
| 236 | - $is_recurring = get_post_meta( $this->ID, '_wpinv_is_recurring', true ); |
|
| 236 | + $is_recurring = get_post_meta($this->ID, '_wpinv_is_recurring', true); |
|
| 237 | 237 | |
| 238 | - return (bool)apply_filters( 'wpinv_is_recurring_item', $is_recurring, $this->ID ); |
|
| 238 | + return (bool)apply_filters('wpinv_is_recurring_item', $is_recurring, $this->ID); |
|
| 239 | 239 | |
| 240 | 240 | } |
| 241 | 241 | |
| 242 | - public function get_recurring_period( $full = false ) { |
|
| 243 | - $period = get_post_meta( $this->ID, '_wpinv_recurring_period', true ); |
|
| 242 | + public function get_recurring_period($full = false) { |
|
| 243 | + $period = get_post_meta($this->ID, '_wpinv_recurring_period', true); |
|
| 244 | 244 | |
| 245 | - if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
|
| 245 | + if (!in_array($period, array('D', 'W', 'M', 'Y'))) { |
|
| 246 | 246 | $period = 'D'; |
| 247 | 247 | } |
| 248 | 248 | |
| 249 | - if ( $full ) { |
|
| 250 | - switch( $period ) { |
|
| 249 | + if ($full) { |
|
| 250 | + switch ($period) { |
|
| 251 | 251 | case 'D': |
| 252 | 252 | $period = 'day'; |
| 253 | 253 | break; |
@@ -263,45 +263,45 @@ discard block |
||
| 263 | 263 | } |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | - return apply_filters( 'wpinv_item_recurring_period', $period, $full, $this->ID ); |
|
| 266 | + return apply_filters('wpinv_item_recurring_period', $period, $full, $this->ID); |
|
| 267 | 267 | |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | 270 | public function get_recurring_interval() { |
| 271 | - $interval = (int)get_post_meta( $this->ID, '_wpinv_recurring_interval', true ); |
|
| 271 | + $interval = (int)get_post_meta($this->ID, '_wpinv_recurring_interval', true); |
|
| 272 | 272 | |
| 273 | - if ( !$interval > 0 ) { |
|
| 273 | + if (!$interval > 0) { |
|
| 274 | 274 | $interval = 1; |
| 275 | 275 | } |
| 276 | 276 | |
| 277 | - return apply_filters( 'wpinv_item_recurring_interval', $interval, $this->ID ); |
|
| 277 | + return apply_filters('wpinv_item_recurring_interval', $interval, $this->ID); |
|
| 278 | 278 | |
| 279 | 279 | } |
| 280 | 280 | |
| 281 | 281 | public function get_recurring_limit() { |
| 282 | - $limit = get_post_meta( $this->ID, '_wpinv_recurring_limit', true ); |
|
| 282 | + $limit = get_post_meta($this->ID, '_wpinv_recurring_limit', true); |
|
| 283 | 283 | |
| 284 | - return (int)apply_filters( 'wpinv_item_recurring_limit', $limit, $this->ID ); |
|
| 284 | + return (int)apply_filters('wpinv_item_recurring_limit', $limit, $this->ID); |
|
| 285 | 285 | |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | public function has_free_trial() { |
| 289 | - $free_trial = get_post_meta( $this->ID, '_wpinv_free_trial', true ); |
|
| 290 | - $free_trial = $this->is_recurring() && !empty( $free_trial ) ? true : false; |
|
| 289 | + $free_trial = get_post_meta($this->ID, '_wpinv_free_trial', true); |
|
| 290 | + $free_trial = $this->is_recurring() && !empty($free_trial) ? true : false; |
|
| 291 | 291 | |
| 292 | - return (bool)apply_filters( 'wpinv_item_has_free_trial', $free_trial, $this->ID ); |
|
| 292 | + return (bool)apply_filters('wpinv_item_has_free_trial', $free_trial, $this->ID); |
|
| 293 | 293 | |
| 294 | 294 | } |
| 295 | 295 | |
| 296 | - public function get_trial_period( $full = false ) { |
|
| 297 | - $period = get_post_meta( $this->ID, '_wpinv_trial_period', true ); |
|
| 296 | + public function get_trial_period($full = false) { |
|
| 297 | + $period = get_post_meta($this->ID, '_wpinv_trial_period', true); |
|
| 298 | 298 | |
| 299 | - if ( !in_array( $period, array( 'D', 'W', 'M', 'Y' ) ) ) { |
|
| 299 | + if (!in_array($period, array('D', 'W', 'M', 'Y'))) { |
|
| 300 | 300 | $period = 'D'; |
| 301 | 301 | } |
| 302 | 302 | |
| 303 | - if ( $full ) { |
|
| 304 | - switch( $period ) { |
|
| 303 | + if ($full) { |
|
| 304 | + switch ($period) { |
|
| 305 | 305 | case 'D': |
| 306 | 306 | $period = 'day'; |
| 307 | 307 | break; |
@@ -317,55 +317,55 @@ discard block |
||
| 317 | 317 | } |
| 318 | 318 | } |
| 319 | 319 | |
| 320 | - return apply_filters( 'wpinv_item_trial_period', $period, $full, $this->ID ); |
|
| 320 | + return apply_filters('wpinv_item_trial_period', $period, $full, $this->ID); |
|
| 321 | 321 | |
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | public function get_trial_interval() { |
| 325 | - $interval = absint( get_post_meta( $this->ID, '_wpinv_trial_interval', true ) ); |
|
| 325 | + $interval = absint(get_post_meta($this->ID, '_wpinv_trial_interval', true)); |
|
| 326 | 326 | |
| 327 | - if ( !$interval > 0 ) { |
|
| 327 | + if (!$interval > 0) { |
|
| 328 | 328 | $interval = 1; |
| 329 | 329 | } |
| 330 | 330 | |
| 331 | - return apply_filters( 'wpinv_item_trial_interval', $interval, $this->ID ); |
|
| 331 | + return apply_filters('wpinv_item_trial_interval', $interval, $this->ID); |
|
| 332 | 332 | |
| 333 | 333 | } |
| 334 | 334 | |
| 335 | 335 | public function is_free() { |
| 336 | 336 | $is_free = false; |
| 337 | 337 | |
| 338 | - $price = get_post_meta( $this->ID, '_wpinv_price', true ); |
|
| 338 | + $price = get_post_meta($this->ID, '_wpinv_price', true); |
|
| 339 | 339 | |
| 340 | - if ( (float)$price == 0 ) { |
|
| 340 | + if ((float)$price == 0) { |
|
| 341 | 341 | $is_free = true; |
| 342 | 342 | } |
| 343 | 343 | |
| 344 | - return (bool) apply_filters( 'wpinv_is_free_item', $is_free, $this->ID ); |
|
| 344 | + return (bool)apply_filters('wpinv_is_free_item', $is_free, $this->ID); |
|
| 345 | 345 | |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | public function is_package() { |
| 349 | 349 | $is_package = $this->get_type() == 'package' ? true : false; |
| 350 | 350 | |
| 351 | - return (bool) apply_filters( 'wpinv_is_package_item', $is_package, $this->ID ); |
|
| 351 | + return (bool)apply_filters('wpinv_is_package_item', $is_package, $this->ID); |
|
| 352 | 352 | |
| 353 | 353 | } |
| 354 | 354 | |
| 355 | 355 | public function is_editable() { |
| 356 | - $editable = get_post_meta( $this->ID, '_wpinv_editable', true ); |
|
| 356 | + $editable = get_post_meta($this->ID, '_wpinv_editable', true); |
|
| 357 | 357 | |
| 358 | 358 | $is_editable = $editable === 0 || $editable === '0' ? false : true; |
| 359 | 359 | |
| 360 | - return (bool) apply_filters( 'wpinv_item_is_editable', $is_editable, $this->ID ); |
|
| 360 | + return (bool)apply_filters('wpinv_item_is_editable', $is_editable, $this->ID); |
|
| 361 | 361 | } |
| 362 | 362 | |
| 363 | - public function save_metas( $metas = array() ) { |
|
| 364 | - if ( empty( $metas ) ) { |
|
| 363 | + public function save_metas($metas = array()) { |
|
| 364 | + if (empty($metas)) { |
|
| 365 | 365 | return false; |
| 366 | 366 | } |
| 367 | 367 | |
| 368 | - foreach ( $metas as $meta_key => $meta_value ) { |
|
| 368 | + foreach ($metas as $meta_key => $meta_value) { |
|
| 369 | 369 | $meta_key = strpos($meta_key, '_wpinv_') !== 0 ? '_wpinv_' . $meta_key : $meta_key; |
| 370 | 370 | |
| 371 | 371 | $this->update_meta($meta_key, $meta_value); |
@@ -374,66 +374,66 @@ discard block |
||
| 374 | 374 | return true; |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | - public function update_meta( $meta_key = '', $meta_value = '', $prev_value = '' ) { |
|
| 378 | - if ( empty( $meta_key ) ) { |
|
| 377 | + public function update_meta($meta_key = '', $meta_value = '', $prev_value = '') { |
|
| 378 | + if (empty($meta_key)) { |
|
| 379 | 379 | return false; |
| 380 | 380 | } |
| 381 | 381 | |
| 382 | - $meta_value = apply_filters( 'wpinv_update_item_meta_' . $meta_key, $meta_value, $this->ID ); |
|
| 382 | + $meta_value = apply_filters('wpinv_update_item_meta_' . $meta_key, $meta_value, $this->ID); |
|
| 383 | 383 | |
| 384 | - return update_post_meta( $this->ID, $meta_key, $meta_value, $prev_value ); |
|
| 384 | + return update_post_meta($this->ID, $meta_key, $meta_value, $prev_value); |
|
| 385 | 385 | } |
| 386 | 386 | |
| 387 | - public function get_fees( $type = 'fee', $item_id = 0 ) { |
|
| 387 | + public function get_fees($type = 'fee', $item_id = 0) { |
|
| 388 | 388 | global $wpi_session; |
| 389 | 389 | |
| 390 | - $fees = $wpi_session->get( 'wpi_cart_fees' ); |
|
| 390 | + $fees = $wpi_session->get('wpi_cart_fees'); |
|
| 391 | 391 | |
| 392 | - if ( ! wpinv_get_cart_contents() ) { |
|
| 392 | + if (!wpinv_get_cart_contents()) { |
|
| 393 | 393 | // We can only get item type fees when the cart is empty |
| 394 | 394 | $type = 'custom'; |
| 395 | 395 | } |
| 396 | 396 | |
| 397 | - if ( ! empty( $fees ) && ! empty( $type ) && 'all' !== $type ) { |
|
| 398 | - foreach( $fees as $key => $fee ) { |
|
| 399 | - if( ! empty( $fee['type'] ) && $type != $fee['type'] ) { |
|
| 400 | - unset( $fees[ $key ] ); |
|
| 397 | + if (!empty($fees) && !empty($type) && 'all' !== $type) { |
|
| 398 | + foreach ($fees as $key => $fee) { |
|
| 399 | + if (!empty($fee['type']) && $type != $fee['type']) { |
|
| 400 | + unset($fees[$key]); |
|
| 401 | 401 | } |
| 402 | 402 | } |
| 403 | 403 | } |
| 404 | 404 | |
| 405 | - if ( ! empty( $fees ) && ! empty( $item_id ) ) { |
|
| 405 | + if (!empty($fees) && !empty($item_id)) { |
|
| 406 | 406 | // Remove fees that don't belong to the specified Item |
| 407 | - foreach ( $fees as $key => $fee ) { |
|
| 408 | - if ( (int) $item_id !== (int)$fee['custom_id'] ) { |
|
| 409 | - unset( $fees[ $key ] ); |
|
| 407 | + foreach ($fees as $key => $fee) { |
|
| 408 | + if ((int)$item_id !== (int)$fee['custom_id']) { |
|
| 409 | + unset($fees[$key]); |
|
| 410 | 410 | } |
| 411 | 411 | } |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | - if ( ! empty( $fees ) ) { |
|
| 414 | + if (!empty($fees)) { |
|
| 415 | 415 | // Remove fees that belong to a specific item but are not in the cart |
| 416 | - foreach( $fees as $key => $fee ) { |
|
| 417 | - if( empty( $fee['custom_id'] ) ) { |
|
| 416 | + foreach ($fees as $key => $fee) { |
|
| 417 | + if (empty($fee['custom_id'])) { |
|
| 418 | 418 | continue; |
| 419 | 419 | } |
| 420 | 420 | |
| 421 | - if ( !wpinv_item_in_cart( $fee['custom_id'] ) ) { |
|
| 422 | - unset( $fees[ $key ] ); |
|
| 421 | + if (!wpinv_item_in_cart($fee['custom_id'])) { |
|
| 422 | + unset($fees[$key]); |
|
| 423 | 423 | } |
| 424 | 424 | } |
| 425 | 425 | } |
| 426 | 426 | |
| 427 | - return ! empty( $fees ) ? $fees : array(); |
|
| 427 | + return !empty($fees) ? $fees : array(); |
|
| 428 | 428 | } |
| 429 | 429 | |
| 430 | 430 | public function can_purchase() { |
| 431 | 431 | $can_purchase = true; |
| 432 | 432 | |
| 433 | - if ( !current_user_can( 'edit_post', $this->ID ) && $this->post_status != 'publish' ) { |
|
| 433 | + if (!current_user_can('edit_post', $this->ID) && $this->post_status != 'publish') { |
|
| 434 | 434 | $can_purchase = false; |
| 435 | 435 | } |
| 436 | 436 | |
| 437 | - return (bool)apply_filters( 'wpinv_can_purchase_item', $can_purchase, $this ); |
|
| 437 | + return (bool)apply_filters('wpinv_can_purchase_item', $can_purchase, $this); |
|
| 438 | 438 | } |
| 439 | 439 | } |
@@ -1,6 +1,8 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | // Exit if accessed directly |
| 3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
| 3 | +if ( ! defined( 'ABSPATH' ) ) { |
|
| 4 | + exit; |
|
| 5 | +} |
|
| 4 | 6 | |
| 5 | 7 | add_action( 'wpinv_worldpay_cc_form', '__return_false' ); |
| 6 | 8 | |
@@ -1,12 +1,12 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | // Exit if accessed directly |
| 3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
| 3 | +if (!defined('ABSPATH')) exit; |
|
| 4 | 4 | |
| 5 | -add_action( 'wpinv_worldpay_cc_form', '__return_false' ); |
|
| 5 | +add_action('wpinv_worldpay_cc_form', '__return_false'); |
|
| 6 | 6 | |
| 7 | -function wpinv_process_worldpay_payment( $purchase_data ) { |
|
| 8 | - if( ! wp_verify_nonce( $purchase_data['gateway_nonce'], 'wpi-gateway' ) ) { |
|
| 9 | - wp_die( __( 'Nonce verification has failed', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
| 7 | +function wpinv_process_worldpay_payment($purchase_data) { |
|
| 8 | + if (!wp_verify_nonce($purchase_data['gateway_nonce'], 'wpi-gateway')) { |
|
| 9 | + wp_die(__('Nonce verification has failed', 'invoicing'), __('Error', 'invoicing'), array('response' => 403)); |
|
| 10 | 10 | } |
| 11 | 11 | |
| 12 | 12 | // Collect payment data |
@@ -24,120 +24,120 @@ discard block |
||
| 24 | 24 | ); |
| 25 | 25 | |
| 26 | 26 | // Record the pending payment |
| 27 | - $invoice = wpinv_get_invoice( $purchase_data['invoice_id'] ); |
|
| 27 | + $invoice = wpinv_get_invoice($purchase_data['invoice_id']); |
|
| 28 | 28 | |
| 29 | - if ( !empty( $invoice ) ) { |
|
| 29 | + if (!empty($invoice)) { |
|
| 30 | 30 | $quantities_enabled = wpinv_item_quantities_enabled(); |
| 31 | 31 | |
| 32 | - $instId = wpinv_get_option( 'worldpay_instId', false ); |
|
| 32 | + $instId = wpinv_get_option('worldpay_instId', false); |
|
| 33 | 33 | $cartId = $invoice->get_number(); |
| 34 | - $testMode = wpinv_is_test_mode( 'worldpay' ) ? 100 : 0; |
|
| 34 | + $testMode = wpinv_is_test_mode('worldpay') ? 100 : 0; |
|
| 35 | 35 | $name = $invoice->get_user_full_name(); |
| 36 | - $address = wp_strip_all_tags( $invoice->get_address(), true ); |
|
| 36 | + $address = wp_strip_all_tags($invoice->get_address(), true); |
|
| 37 | 37 | $postcode = $invoice->zip; |
| 38 | 38 | $tel = $invoice->phone; |
| 39 | 39 | $email = $invoice->get_email(); |
| 40 | 40 | $country = $invoice->country; |
| 41 | - $amount = wpinv_sanitize_amount( $invoice->get_total() ); |
|
| 41 | + $amount = wpinv_sanitize_amount($invoice->get_total()); |
|
| 42 | 42 | $currency = wpinv_get_currency(); |
| 43 | 43 | |
| 44 | 44 | $items = array(); |
| 45 | - foreach ( $invoice->get_cart_details() as $item ) { |
|
| 45 | + foreach ($invoice->get_cart_details() as $item) { |
|
| 46 | 46 | $item_desc = $item['name']; |
| 47 | - $quantity = !empty( $item['quantity'] ) && $item['quantity'] > 0 ? $item['quantity'] : 1; |
|
| 48 | - $item_desc .= ' (' . ( $quantities_enabled ? $quantity . 'x ' : '' ) . wpinv_price( wpinv_format_amount( $item['item_price'] ) ) . ')'; |
|
| 47 | + $quantity = !empty($item['quantity']) && $item['quantity'] > 0 ? $item['quantity'] : 1; |
|
| 48 | + $item_desc .= ' (' . ($quantities_enabled ? $quantity . 'x ' : '') . wpinv_price(wpinv_format_amount($item['item_price'])) . ')'; |
|
| 49 | 49 | |
| 50 | 50 | $items[] = $item_desc; |
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - $desc = implode( ', ', $items ); |
|
| 54 | - if ( wpinv_use_taxes() && $invoice->get_tax() > 0 ) { |
|
| 55 | - $desc .= ', ' . wp_sprintf( __( 'Tax: %s', 'invoicing' ), $invoice->get_tax( true ) ); |
|
| 53 | + $desc = implode(', ', $items); |
|
| 54 | + if (wpinv_use_taxes() && $invoice->get_tax() > 0) { |
|
| 55 | + $desc .= ', ' . wp_sprintf(__('Tax: %s', 'invoicing'), $invoice->get_tax(true)); |
|
| 56 | 56 | } |
| 57 | 57 | |
| 58 | 58 | $extra_params = array(); |
| 59 | 59 | $extra_params['MC_description'] = $desc; |
| 60 | - $extra_params['MC_callback'] = wpinv_get_ipn_url( 'worldpay' ); |
|
| 60 | + $extra_params['MC_callback'] = wpinv_get_ipn_url('worldpay'); |
|
| 61 | 61 | $extra_params['MC_key'] = $invoice->get_key(); |
| 62 | 62 | $extra_params['MC_invoice_id'] = $invoice->ID; |
| 63 | 63 | $extra_params['address1'] = $address; |
| 64 | 64 | $extra_params['town'] = $invoice->city; |
| 65 | 65 | $extra_params['region'] = $invoice->state; |
| 66 | - $extra_params['amountString'] = $invoice->get_total( true ); |
|
| 67 | - $extra_params['countryString'] = wpinv_country_name( $invoice->country ); |
|
| 66 | + $extra_params['amountString'] = $invoice->get_total(true); |
|
| 67 | + $extra_params['countryString'] = wpinv_country_name($invoice->country); |
|
| 68 | 68 | $extra_params['compName'] = $invoice->company; |
| 69 | 69 | |
| 70 | - $extra_params = apply_filters( 'wpinv_worldpay_form_extra_parameters', $extra_params, $invoice ); |
|
| 70 | + $extra_params = apply_filters('wpinv_worldpay_form_extra_parameters', $extra_params, $invoice); |
|
| 71 | 71 | |
| 72 | - $redirect_text = __( 'Redirecting to Worldpay site, click on button if not redirected.', 'invoicing' ); |
|
| 73 | - $redirect_text = apply_filters( 'wpinv_worldpay_redirect_text', $redirect_text, $invoice ); |
|
| 72 | + $redirect_text = __('Redirecting to Worldpay site, click on button if not redirected.', 'invoicing'); |
|
| 73 | + $redirect_text = apply_filters('wpinv_worldpay_redirect_text', $redirect_text, $invoice); |
|
| 74 | 74 | |
| 75 | 75 | // Empty the shopping cart |
| 76 | 76 | wpinv_empty_cart(); |
| 77 | 77 | ?> |
| 78 | 78 | <div class="wpi-worldpay-form" style="padding:20px;font-family:arial,sans-serif;text-align:center;color:#555"> |
| 79 | -<?php do_action( 'wpinv_worldpay_form_before', $invoice ); ?> |
|
| 80 | -<h3><?php echo $redirect_text ;?></h3> |
|
| 79 | +<?php do_action('wpinv_worldpay_form_before', $invoice); ?> |
|
| 80 | +<h3><?php echo $redirect_text; ?></h3> |
|
| 81 | 81 | <form action="<?php echo wpinv_get_worldpay_redirect(); ?>" name="wpi_worldpay_form" method="POST"> |
| 82 | - <input type="hidden" value="<?php echo $amount;?>" name="amount"> |
|
| 83 | - <input type="hidden" value="<?php echo esc_attr( $cartId );?>" name="cartId"> |
|
| 84 | - <input type="hidden" value="<?php echo $currency;?>" name="currency"> |
|
| 85 | - <input type="hidden" value="<?php echo $instId;?>" name="instId"> |
|
| 86 | - <input type="hidden" value="<?php echo $testMode;?>" name="testMode"> |
|
| 87 | - <input type="hidden" value="<?php echo esc_attr( $name );?>" name="name"> |
|
| 88 | - <input type="hidden" value="<?php echo esc_attr( $address );?>" name="address"> |
|
| 89 | - <input type="hidden" value="<?php echo esc_attr( $postcode );?>" name="postcode"> |
|
| 90 | - <input type="hidden" value="<?php echo esc_attr( $tel );?>" name="tel"> |
|
| 91 | - <input type="hidden" value="<?php echo esc_attr( $email );?>" name="email"> |
|
| 92 | - <input type="hidden" value="<?php echo esc_attr( $country );?>" name="country"> |
|
| 93 | - <input type="hidden" value="<?php echo esc_attr( $desc );?>" name="desc"> |
|
| 94 | - <?php foreach ( $extra_params as $param => $value ) { ?> |
|
| 95 | - <?php if ( !empty( $value !== false ) ) { ?> |
|
| 96 | - <input type="hidden" value="<?php echo esc_attr( $value );?>" name="<?php echo esc_attr( $param );?>"> |
|
| 82 | + <input type="hidden" value="<?php echo $amount; ?>" name="amount"> |
|
| 83 | + <input type="hidden" value="<?php echo esc_attr($cartId); ?>" name="cartId"> |
|
| 84 | + <input type="hidden" value="<?php echo $currency; ?>" name="currency"> |
|
| 85 | + <input type="hidden" value="<?php echo $instId; ?>" name="instId"> |
|
| 86 | + <input type="hidden" value="<?php echo $testMode; ?>" name="testMode"> |
|
| 87 | + <input type="hidden" value="<?php echo esc_attr($name); ?>" name="name"> |
|
| 88 | + <input type="hidden" value="<?php echo esc_attr($address); ?>" name="address"> |
|
| 89 | + <input type="hidden" value="<?php echo esc_attr($postcode); ?>" name="postcode"> |
|
| 90 | + <input type="hidden" value="<?php echo esc_attr($tel); ?>" name="tel"> |
|
| 91 | + <input type="hidden" value="<?php echo esc_attr($email); ?>" name="email"> |
|
| 92 | + <input type="hidden" value="<?php echo esc_attr($country); ?>" name="country"> |
|
| 93 | + <input type="hidden" value="<?php echo esc_attr($desc); ?>" name="desc"> |
|
| 94 | + <?php foreach ($extra_params as $param => $value) { ?> |
|
| 95 | + <?php if (!empty($value !== false)) { ?> |
|
| 96 | + <input type="hidden" value="<?php echo esc_attr($value); ?>" name="<?php echo esc_attr($param); ?>"> |
|
| 97 | 97 | <?php } ?> |
| 98 | 98 | <?php } ?> |
| 99 | - <?php do_action( 'wpinv_worldpay_form_parameters', $invoice ); ?> |
|
| 100 | - <input type="submit" name="wpi_worldpay_submit" value="<?php esc_attr_e( 'Pay by Debit/Credit Card (WorldPay)', 'invoicing' ) ;?>"> |
|
| 99 | + <?php do_action('wpinv_worldpay_form_parameters', $invoice); ?> |
|
| 100 | + <input type="submit" name="wpi_worldpay_submit" value="<?php esc_attr_e('Pay by Debit/Credit Card (WorldPay)', 'invoicing'); ?>"> |
|
| 101 | 101 | </form> |
| 102 | 102 | <script type="text/javascript">document.wpi_worldpay_form.submit();</script> |
| 103 | -<?php do_action( 'wpinv_worldpay_form_after', $invoice ); ?> |
|
| 103 | +<?php do_action('wpinv_worldpay_form_after', $invoice); ?> |
|
| 104 | 104 | </div> |
| 105 | 105 | <?php |
| 106 | 106 | } else { |
| 107 | - wpinv_record_gateway_error( __( 'Payment Error', 'invoicing' ), sprintf( __( 'Payment creation failed while processing a worldpay payment. Payment data: %s', 'invoicing' ), json_encode( $payment_data ) ), $invoice ); |
|
| 107 | + wpinv_record_gateway_error(__('Payment Error', 'invoicing'), sprintf(__('Payment creation failed while processing a worldpay payment. Payment data: %s', 'invoicing'), json_encode($payment_data)), $invoice); |
|
| 108 | 108 | // If errors are present, send the user back to the purchase page so they can be corrected |
| 109 | - wpinv_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['wpi-gateway'] ); |
|
| 109 | + wpinv_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['wpi-gateway']); |
|
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | -add_action( 'wpinv_gateway_worldpay', 'wpinv_process_worldpay_payment' ); |
|
| 112 | +add_action('wpinv_gateway_worldpay', 'wpinv_process_worldpay_payment'); |
|
| 113 | 113 | |
| 114 | 114 | function wpinv_get_worldpay_redirect() { |
| 115 | - $redirect = wpinv_is_test_mode( 'worldpay' ) ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
| 115 | + $redirect = wpinv_is_test_mode('worldpay') ? 'https://secure-test.worldpay.com/wcc/purchase' : 'https://secure.worldpay.com/wcc/purchase'; |
|
| 116 | 116 | |
| 117 | - return apply_filters( 'wpinv_worldpay_redirect', $redirect ); |
|
| 117 | + return apply_filters('wpinv_worldpay_redirect', $redirect); |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | 120 | function wpinv_process_worldpay_ipn() { |
| 121 | - $request = wpinv_get_post_data( 'post' ); |
|
| 121 | + $request = wpinv_get_post_data('post'); |
|
| 122 | 122 | |
| 123 | - if ( !empty( $request['cartId'] ) && !empty( $request['transStatus'] ) && !empty( $request['installation'] ) && isset( $request['testMode'] ) && isset( $request['MC_invoice_id'] ) && isset( $request['MC_key'] ) ) { |
|
| 123 | + if (!empty($request['cartId']) && !empty($request['transStatus']) && !empty($request['installation']) && isset($request['testMode']) && isset($request['MC_invoice_id']) && isset($request['MC_key'])) { |
|
| 124 | 124 | $invoice_id = $request['MC_invoice_id']; |
| 125 | 125 | |
| 126 | - if ( $invoice_id == wpinv_get_invoice_id_by_key( $request['MC_key'] ) && $invoice = wpinv_get_invoice( $invoice_id ) ) { |
|
| 127 | - if ( $request['transStatus'] == 'Y' ) { |
|
| 128 | - wpinv_update_payment_status( $invoice_id, 'publish' ); |
|
| 129 | - wpinv_set_payment_transaction_id( $invoice_id, $request['transId'] ); |
|
| 130 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'Worldpay Transaction ID: %s', 'invoicing' ), $request['transId'] ) ); |
|
| 126 | + if ($invoice_id == wpinv_get_invoice_id_by_key($request['MC_key']) && $invoice = wpinv_get_invoice($invoice_id)) { |
|
| 127 | + if ($request['transStatus'] == 'Y') { |
|
| 128 | + wpinv_update_payment_status($invoice_id, 'publish'); |
|
| 129 | + wpinv_set_payment_transaction_id($invoice_id, $request['transId']); |
|
| 130 | + wpinv_insert_payment_note($invoice_id, sprintf(__('Worldpay Transaction ID: %s', 'invoicing'), $request['transId'])); |
|
| 131 | 131 | return; |
| 132 | - } else if ( $request['transStatus'] == 'C' ) { |
|
| 133 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 134 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment transaction failed while processing Worldpay payment, kindly check IPN log.', 'invoicing' ) ); |
|
| 132 | + } else if ($request['transStatus'] == 'C') { |
|
| 133 | + wpinv_update_payment_status($invoice_id, 'wpi-failed'); |
|
| 134 | + wpinv_insert_payment_note($invoice_id, __('Payment transaction failed while processing Worldpay payment, kindly check IPN log.', 'invoicing')); |
|
| 135 | 135 | |
| 136 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Payment transaction failed while processing Worldpay payment. IPN data: %s', 'invoicing' ), json_encode( $request ) ), $invoice_id ); |
|
| 136 | + wpinv_record_gateway_error(__('IPN Error', 'invoicing'), sprintf(__('Payment transaction failed while processing Worldpay payment. IPN data: %s', 'invoicing'), json_encode($request)), $invoice_id); |
|
| 137 | 137 | return; |
| 138 | 138 | } |
| 139 | 139 | } |
| 140 | 140 | } |
| 141 | 141 | return; |
| 142 | 142 | } |
| 143 | -add_action( 'wpinv_verify_worldpay_ipn', 'wpinv_process_worldpay_ipn' ); |
|
| 144 | 143 | \ No newline at end of file |
| 144 | +add_action('wpinv_verify_worldpay_ipn', 'wpinv_process_worldpay_ipn'); |
|
| 145 | 145 | \ No newline at end of file |