@@ -160,50 +160,50 @@ |
||
| 160 | 160 | */ |
| 161 | 161 | function getpaid_calculate_invoice_discount( $invoice, $discount ) { |
| 162 | 162 | |
| 163 | - $initial_discount = 0; |
|
| 164 | - $recurring_discount = 0; |
|
| 163 | + $initial_discount = 0; |
|
| 164 | + $recurring_discount = 0; |
|
| 165 | 165 | |
| 166 | - foreach ( $invoice->get_items() as $item ) { |
|
| 166 | + foreach ( $invoice->get_items() as $item ) { |
|
| 167 | 167 | |
| 168 | - // Abort if it is not valid for this item. |
|
| 169 | - if ( ! $discount->is_valid_for_items( array( $item->get_id() ) ) ) { |
|
| 170 | - continue; |
|
| 171 | - } |
|
| 168 | + // Abort if it is not valid for this item. |
|
| 169 | + if ( ! $discount->is_valid_for_items( array( $item->get_id() ) ) ) { |
|
| 170 | + continue; |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | - // Calculate the initial amount... |
|
| 174 | - $item_discount = $discount->get_discounted_amount( $item->get_sub_total() ); |
|
| 175 | - $recurring_item_discount = 0; |
|
| 173 | + // Calculate the initial amount... |
|
| 174 | + $item_discount = $discount->get_discounted_amount( $item->get_sub_total() ); |
|
| 175 | + $recurring_item_discount = 0; |
|
| 176 | 176 | |
| 177 | - // ... and maybe the recurring amount. |
|
| 178 | - if ( $item->is_recurring() && $discount->is_recurring() ) { |
|
| 179 | - $recurring_item_discount = $discount->get_discounted_amount( $item->get_recurring_sub_total() ); |
|
| 180 | - } |
|
| 177 | + // ... and maybe the recurring amount. |
|
| 178 | + if ( $item->is_recurring() && $discount->is_recurring() ) { |
|
| 179 | + $recurring_item_discount = $discount->get_discounted_amount( $item->get_recurring_sub_total() ); |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - // Discount should not exceed discounted amount. |
|
| 183 | - if ( ! $discount->is_type( 'percent' ) ) { |
|
| 182 | + // Discount should not exceed discounted amount. |
|
| 183 | + if ( ! $discount->is_type( 'percent' ) ) { |
|
| 184 | 184 | |
| 185 | - if ( ( $initial_discount + $item_discount ) > $discount->get_amount() ) { |
|
| 186 | - $item_discount = $discount->get_amount() - $initial_discount; |
|
| 187 | - } |
|
| 185 | + if ( ( $initial_discount + $item_discount ) > $discount->get_amount() ) { |
|
| 186 | + $item_discount = $discount->get_amount() - $initial_discount; |
|
| 187 | + } |
|
| 188 | 188 | |
| 189 | - if ( ( $recurring_discount + $recurring_item_discount ) > $discount->get_amount() ) { |
|
| 190 | - $recurring_item_discount = $discount->get_amount() - $recurring_discount; |
|
| 191 | - } |
|
| 189 | + if ( ( $recurring_discount + $recurring_item_discount ) > $discount->get_amount() ) { |
|
| 190 | + $recurring_item_discount = $discount->get_amount() - $recurring_discount; |
|
| 191 | + } |
|
| 192 | 192 | |
| 193 | - } |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - $initial_discount += $item_discount; |
|
| 196 | - $recurring_discount += $recurring_discount; |
|
| 197 | - $item->item_discount = $item_discount; |
|
| 198 | - $item->recurring_item_discount = $recurring_item_discount; |
|
| 195 | + $initial_discount += $item_discount; |
|
| 196 | + $recurring_discount += $recurring_discount; |
|
| 197 | + $item->item_discount = $item_discount; |
|
| 198 | + $item->recurring_item_discount = $recurring_item_discount; |
|
| 199 | 199 | |
| 200 | - } |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - return array( |
|
| 203 | - 'name' => 'discount_code', |
|
| 204 | - 'discount_code' => $discount->get_code(), |
|
| 205 | - 'initial_discount' => $initial_discount, |
|
| 206 | - 'recurring_discount' => $recurring_discount, |
|
| 207 | - ); |
|
| 202 | + return array( |
|
| 203 | + 'name' => 'discount_code', |
|
| 204 | + 'discount_code' => $discount->get_code(), |
|
| 205 | + 'initial_discount' => $initial_discount, |
|
| 206 | + 'recurring_discount' => $recurring_discount, |
|
| 207 | + ); |
|
| 208 | 208 | |
| 209 | 209 | } |
@@ -6,7 +6,7 @@ discard block |
||
| 6 | 6 | * @package Invoicing |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | -defined( 'ABSPATH' ) || exit; |
|
| 9 | +defined('ABSPATH') || exit; |
|
| 10 | 10 | |
| 11 | 11 | /** |
| 12 | 12 | * Returns an array of discount type. |
@@ -17,8 +17,8 @@ discard block |
||
| 17 | 17 | return apply_filters( |
| 18 | 18 | 'wpinv_discount_types', |
| 19 | 19 | array( |
| 20 | - 'percent' => __( 'Percentage', 'invoicing' ), |
|
| 21 | - 'flat' => __( 'Flat Amount', 'invoicing' ), |
|
| 20 | + 'percent' => __('Percentage', 'invoicing'), |
|
| 21 | + 'flat' => __('Flat Amount', 'invoicing'), |
|
| 22 | 22 | ) |
| 23 | 23 | ); |
| 24 | 24 | } |
@@ -28,46 +28,46 @@ discard block |
||
| 28 | 28 | * |
| 29 | 29 | * @return string |
| 30 | 30 | */ |
| 31 | -function wpinv_get_discount_type_name( $type = '' ) { |
|
| 31 | +function wpinv_get_discount_type_name($type = '') { |
|
| 32 | 32 | $types = wpinv_get_discount_types(); |
| 33 | - return isset( $types[ $type ] ) ? $types[ $type ] : $type; |
|
| 33 | + return isset($types[$type]) ? $types[$type] : $type; |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * Deletes a discount via the admin page. |
| 38 | 38 | * |
| 39 | 39 | */ |
| 40 | -function wpinv_delete_discount( $data ) { |
|
| 40 | +function wpinv_delete_discount($data) { |
|
| 41 | 41 | |
| 42 | - $discount = new WPInv_Discount( absint( $data['discount'] ) ); |
|
| 43 | - $discount->delete( true ); |
|
| 42 | + $discount = new WPInv_Discount(absint($data['discount'])); |
|
| 43 | + $discount->delete(true); |
|
| 44 | 44 | |
| 45 | 45 | } |
| 46 | -add_action( 'getpaid_authenticated_admin_action_delete_discount', 'wpinv_delete_discount' ); |
|
| 46 | +add_action('getpaid_authenticated_admin_action_delete_discount', 'wpinv_delete_discount'); |
|
| 47 | 47 | |
| 48 | 48 | /** |
| 49 | 49 | * Deactivates a discount via the admin page. |
| 50 | 50 | */ |
| 51 | -function wpinv_activate_discount( $data ) { |
|
| 51 | +function wpinv_activate_discount($data) { |
|
| 52 | 52 | |
| 53 | - $discount = new WPInv_Discount( absint( $data['discount'] ) ); |
|
| 54 | - $discount->set_status( 'publish' ); |
|
| 53 | + $discount = new WPInv_Discount(absint($data['discount'])); |
|
| 54 | + $discount->set_status('publish'); |
|
| 55 | 55 | $discount->save(); |
| 56 | 56 | |
| 57 | 57 | } |
| 58 | -add_action( 'getpaid_authenticated_admin_action_activate_discount', 'wpinv_activate_discount' ); |
|
| 58 | +add_action('getpaid_authenticated_admin_action_activate_discount', 'wpinv_activate_discount'); |
|
| 59 | 59 | |
| 60 | 60 | /** |
| 61 | 61 | * Activates a discount via the admin page. |
| 62 | 62 | */ |
| 63 | -function wpinv_deactivate_discount( $data ) { |
|
| 63 | +function wpinv_deactivate_discount($data) { |
|
| 64 | 64 | |
| 65 | - $discount = new WPInv_Discount( absint( $data['discount'] ) ); |
|
| 66 | - $discount->set_status( 'pending' ); |
|
| 65 | + $discount = new WPInv_Discount(absint($data['discount'])); |
|
| 66 | + $discount->set_status('pending'); |
|
| 67 | 67 | $discount->save(); |
| 68 | 68 | |
| 69 | 69 | } |
| 70 | -add_action( 'getpaid_authenticated_admin_action_deactivate_discount', 'wpinv_deactivate_discount' ); |
|
| 70 | +add_action('getpaid_authenticated_admin_action_deactivate_discount', 'wpinv_deactivate_discount'); |
|
| 71 | 71 | |
| 72 | 72 | /** |
| 73 | 73 | * Fetches a discount object. |
@@ -76,8 +76,8 @@ discard block |
||
| 76 | 76 | * @since 1.0.15 |
| 77 | 77 | * @return WPInv_Discount |
| 78 | 78 | */ |
| 79 | -function wpinv_get_discount( $discount ) { |
|
| 80 | - return new WPInv_Discount( $discount ); |
|
| 79 | +function wpinv_get_discount($discount) { |
|
| 80 | + return new WPInv_Discount($discount); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | /** |
@@ -87,8 +87,8 @@ discard block |
||
| 87 | 87 | * @since 1.0.15 |
| 88 | 88 | * @return WPInv_Discount |
| 89 | 89 | */ |
| 90 | -function wpinv_get_discount_obj( $discount = 0 ) { |
|
| 91 | - return new WPInv_Discount( $discount ); |
|
| 90 | +function wpinv_get_discount_obj($discount = 0) { |
|
| 91 | + return new WPInv_Discount($discount); |
|
| 92 | 92 | } |
| 93 | 93 | |
| 94 | 94 | /** |
@@ -98,10 +98,10 @@ discard block |
||
| 98 | 98 | * @param string|int $value The field value |
| 99 | 99 | * @return bool|WPInv_Discount |
| 100 | 100 | */ |
| 101 | -function wpinv_get_discount_by( $deprecated = null, $value = '' ) { |
|
| 102 | - $discount = new WPInv_Discount( $value ); |
|
| 101 | +function wpinv_get_discount_by($deprecated = null, $value = '') { |
|
| 102 | + $discount = new WPInv_Discount($value); |
|
| 103 | 103 | |
| 104 | - if ( $discount->get_id() != 0 ) { |
|
| 104 | + if ($discount->get_id() != 0) { |
|
| 105 | 105 | return $discount; |
| 106 | 106 | } |
| 107 | 107 | |
@@ -116,9 +116,9 @@ discard block |
||
| 116 | 116 | function wpinv_get_discount_statuses() { |
| 117 | 117 | |
| 118 | 118 | return array( |
| 119 | - 'expired' => __( 'Expired', 'invoicing' ), |
|
| 120 | - 'publish' => __( 'Active', 'invoicing' ), |
|
| 121 | - 'inactive' => __( 'Inactive', 'invoicing' ), |
|
| 119 | + 'expired' => __('Expired', 'invoicing'), |
|
| 120 | + 'publish' => __('Active', 'invoicing'), |
|
| 121 | + 'inactive' => __('Inactive', 'invoicing'), |
|
| 122 | 122 | ); |
| 123 | 123 | |
| 124 | 124 | } |
@@ -126,9 +126,9 @@ discard block |
||
| 126 | 126 | /** |
| 127 | 127 | * Retrieves an invoice status label. |
| 128 | 128 | */ |
| 129 | -function wpinv_discount_status( $status ) { |
|
| 129 | +function wpinv_discount_status($status) { |
|
| 130 | 130 | $statuses = wpinv_get_discount_statuses(); |
| 131 | - return isset( $statuses[ $status ] ) ? $statuses[ $status ] : __( 'Inactive', 'invoicing' ); |
|
| 131 | + return isset($statuses[$status]) ? $statuses[$status] : __('Inactive', 'invoicing'); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /** |
@@ -138,12 +138,12 @@ discard block |
||
| 138 | 138 | * @param int|array|string|WPInv_Discount $code discount data, object, ID or code. |
| 139 | 139 | * @return bool |
| 140 | 140 | */ |
| 141 | -function wpinv_discount_is_recurring( $discount = 0, $code = 0 ) { |
|
| 141 | +function wpinv_discount_is_recurring($discount = 0, $code = 0) { |
|
| 142 | 142 | |
| 143 | - if( ! empty( $discount ) ) { |
|
| 144 | - $discount = wpinv_get_discount_obj( $discount ); |
|
| 143 | + if (!empty($discount)) { |
|
| 144 | + $discount = wpinv_get_discount_obj($discount); |
|
| 145 | 145 | } else { |
| 146 | - $discount = wpinv_get_discount_obj( $code ); |
|
| 146 | + $discount = wpinv_get_discount_obj($code); |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | return $discount->get_is_recurring(); |
@@ -158,35 +158,35 @@ discard block |
||
| 158 | 158 | * @param WPInv_Discount $discount |
| 159 | 159 | * @return array |
| 160 | 160 | */ |
| 161 | -function getpaid_calculate_invoice_discount( $invoice, $discount ) { |
|
| 161 | +function getpaid_calculate_invoice_discount($invoice, $discount) { |
|
| 162 | 162 | |
| 163 | 163 | $initial_discount = 0; |
| 164 | 164 | $recurring_discount = 0; |
| 165 | 165 | |
| 166 | - foreach ( $invoice->get_items() as $item ) { |
|
| 166 | + foreach ($invoice->get_items() as $item) { |
|
| 167 | 167 | |
| 168 | 168 | // Abort if it is not valid for this item. |
| 169 | - if ( ! $discount->is_valid_for_items( array( $item->get_id() ) ) ) { |
|
| 169 | + if (!$discount->is_valid_for_items(array($item->get_id()))) { |
|
| 170 | 170 | continue; |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | // Calculate the initial amount... |
| 174 | - $item_discount = $discount->get_discounted_amount( $item->get_sub_total() ); |
|
| 174 | + $item_discount = $discount->get_discounted_amount($item->get_sub_total()); |
|
| 175 | 175 | $recurring_item_discount = 0; |
| 176 | 176 | |
| 177 | 177 | // ... and maybe the recurring amount. |
| 178 | - if ( $item->is_recurring() && $discount->is_recurring() ) { |
|
| 179 | - $recurring_item_discount = $discount->get_discounted_amount( $item->get_recurring_sub_total() ); |
|
| 178 | + if ($item->is_recurring() && $discount->is_recurring()) { |
|
| 179 | + $recurring_item_discount = $discount->get_discounted_amount($item->get_recurring_sub_total()); |
|
| 180 | 180 | } |
| 181 | 181 | |
| 182 | 182 | // Discount should not exceed discounted amount. |
| 183 | - if ( ! $discount->is_type( 'percent' ) ) { |
|
| 183 | + if (!$discount->is_type('percent')) { |
|
| 184 | 184 | |
| 185 | - if ( ( $initial_discount + $item_discount ) > $discount->get_amount() ) { |
|
| 185 | + if (($initial_discount + $item_discount) > $discount->get_amount()) { |
|
| 186 | 186 | $item_discount = $discount->get_amount() - $initial_discount; |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - if ( ( $recurring_discount + $recurring_item_discount ) > $discount->get_amount() ) { |
|
| 189 | + if (($recurring_discount + $recurring_item_discount) > $discount->get_amount()) { |
|
| 190 | 190 | $recurring_item_discount = $discount->get_amount() - $recurring_discount; |
| 191 | 191 | } |
| 192 | 192 | |
@@ -12,162 +12,162 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Payment_Form_Submission_Discount { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Submission discounts. |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - public $discounts = array(); |
|
| 15 | + /** |
|
| 16 | + * Submission discounts. |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + public $discounts = array(); |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Class constructor |
|
| 23 | + * |
|
| 24 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 25 | + * @param float $initial_total |
|
| 26 | + * @param float $recurring_total |
|
| 27 | + */ |
|
| 28 | + public function __construct( $submission, $initial_total, $recurring_total ) { |
|
| 29 | + |
|
| 30 | + // Process any existing invoice discounts. |
|
| 31 | + if ( $submission->has_invoice() ) { |
|
| 32 | + $this->discounts = $submission->get_invoice()->get_discounts(); |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + // Do we have a discount? |
|
| 36 | + $discount = $submission->get_field( 'discount' ); |
|
| 37 | + |
|
| 38 | + if ( empty( $discount ) ) { |
|
| 39 | + |
|
| 40 | + if ( isset( $this->discounts['discount_code'] ) ) { |
|
| 41 | + unset( $this->discounts['discount_code'] ); |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + return; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + // Processes the discount code. |
|
| 48 | + $amount = max( $initial_total, $recurring_total ); |
|
| 49 | + $this->process_discount( $submission, $discount, $amount ); |
|
| 50 | + |
|
| 51 | + } |
|
| 20 | 52 | |
| 21 | 53 | /** |
| 22 | - * Class constructor |
|
| 23 | - * |
|
| 24 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 25 | - * @param float $initial_total |
|
| 26 | - * @param float $recurring_total |
|
| 27 | - */ |
|
| 28 | - public function __construct( $submission, $initial_total, $recurring_total ) { |
|
| 29 | - |
|
| 30 | - // Process any existing invoice discounts. |
|
| 31 | - if ( $submission->has_invoice() ) { |
|
| 32 | - $this->discounts = $submission->get_invoice()->get_discounts(); |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - // Do we have a discount? |
|
| 36 | - $discount = $submission->get_field( 'discount' ); |
|
| 37 | - |
|
| 38 | - if ( empty( $discount ) ) { |
|
| 39 | - |
|
| 40 | - if ( isset( $this->discounts['discount_code'] ) ) { |
|
| 41 | - unset( $this->discounts['discount_code'] ); |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - return; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - // Processes the discount code. |
|
| 48 | - $amount = max( $initial_total, $recurring_total ); |
|
| 49 | - $this->process_discount( $submission, $discount, $amount ); |
|
| 50 | - |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Processes a submission discount. |
|
| 55 | - * |
|
| 56 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 57 | - * @param string $discount |
|
| 58 | - * @param float $amount |
|
| 59 | - */ |
|
| 60 | - public function process_discount( $submission, $discount, $amount ) { |
|
| 61 | - |
|
| 62 | - // Fetch the discount. |
|
| 63 | - $discount = new WPInv_Discount( $discount ); |
|
| 64 | - |
|
| 65 | - // Ensure it is active. |
|
| 54 | + * Processes a submission discount. |
|
| 55 | + * |
|
| 56 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 57 | + * @param string $discount |
|
| 58 | + * @param float $amount |
|
| 59 | + */ |
|
| 60 | + public function process_discount( $submission, $discount, $amount ) { |
|
| 61 | + |
|
| 62 | + // Fetch the discount. |
|
| 63 | + $discount = new WPInv_Discount( $discount ); |
|
| 64 | + |
|
| 65 | + // Ensure it is active. |
|
| 66 | 66 | if ( ! $this->is_discount_active( $discount ) ) { |
| 67 | - throw new Exception( __( 'Invalid or expired discount code', 'invoicing' ) ); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - // Exceeded limit. |
|
| 71 | - if ( $discount->has_exceeded_limit() ) { |
|
| 72 | - throw new Exception( __( 'This discount code has been used up', 'invoicing' ) ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - // Validate usages. |
|
| 76 | - $this->validate_single_use_discount( $submission, $discount ); |
|
| 77 | - |
|
| 78 | - // Validate amount. |
|
| 79 | - $this->validate_discount_amount( $submission, $discount, $amount ); |
|
| 80 | - |
|
| 81 | - // Save the discount. |
|
| 82 | - $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Validates a single use discount. |
|
| 87 | - * |
|
| 88 | - * @param WPInv_Discount $discount |
|
| 89 | - * @return bool |
|
| 90 | - */ |
|
| 91 | - public function is_discount_active( $discount ) { |
|
| 92 | - return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired(); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Returns a user's id or email. |
|
| 97 | - * |
|
| 98 | - * @param string $email |
|
| 99 | - * @return int|string|false |
|
| 100 | - */ |
|
| 101 | - public function get_user_id_or_email( $email ) { |
|
| 102 | - |
|
| 103 | - if ( is_user_logged_in() ) { |
|
| 104 | - return get_current_user_id(); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - return empty( $email ) ? false : sanitize_email( $email ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Validates a single use discount. |
|
| 112 | - * |
|
| 113 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 114 | - * @param WPInv_Discount $discount |
|
| 115 | - */ |
|
| 116 | - public function validate_single_use_discount( $submission, $discount ) { |
|
| 117 | - |
|
| 118 | - // Abort if it is not a single use discount. |
|
| 119 | - if ( ! $discount->is_single_use() ) { |
|
| 120 | - return; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - // Ensure there is a valid billing email. |
|
| 124 | - $user = $this->get_user_id_or_email( $submission->get_billing_email() ); |
|
| 125 | - |
|
| 126 | - if ( empty( $user ) ) { |
|
| 127 | - throw new Exception( __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) ); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - // Has the user used this discount code before? |
|
| 131 | - if ( ! $discount->is_valid_for_user( $user ) ) { |
|
| 132 | - throw new Exception( __( 'You have already used this discount', 'invoicing' ) ); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Validates the discount's amount. |
|
| 139 | - * |
|
| 140 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 141 | - * @param WPInv_Discount $discount |
|
| 142 | - * @param float $amount |
|
| 143 | - */ |
|
| 144 | - public function validate_discount_amount( $submission, $discount, $amount ) { |
|
| 145 | - |
|
| 146 | - // Validate minimum amount. |
|
| 147 | - if ( ! $discount->is_minimum_amount_met( $amount ) ) { |
|
| 148 | - $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() ); |
|
| 149 | - throw new Exception( sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) ); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - // Validate the maximum amount. |
|
| 153 | - if ( ! $discount->is_maximum_amount_met( $amount ) ) { |
|
| 154 | - $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() ); |
|
| 155 | - throw new Exception( sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) ); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Calculates the discount code's amount. |
|
| 162 | - * |
|
| 163 | - * Ensure that the discount exists and has been validated before calling this method. |
|
| 164 | - * |
|
| 165 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 166 | - * @param WPInv_Discount $discount |
|
| 167 | - * @return array |
|
| 168 | - */ |
|
| 169 | - public function calculate_discount( $submission, $discount ) { |
|
| 170 | - return getpaid_calculate_invoice_discount( $submission, $discount ); |
|
| 171 | - } |
|
| 67 | + throw new Exception( __( 'Invalid or expired discount code', 'invoicing' ) ); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + // Exceeded limit. |
|
| 71 | + if ( $discount->has_exceeded_limit() ) { |
|
| 72 | + throw new Exception( __( 'This discount code has been used up', 'invoicing' ) ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + // Validate usages. |
|
| 76 | + $this->validate_single_use_discount( $submission, $discount ); |
|
| 77 | + |
|
| 78 | + // Validate amount. |
|
| 79 | + $this->validate_discount_amount( $submission, $discount, $amount ); |
|
| 80 | + |
|
| 81 | + // Save the discount. |
|
| 82 | + $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Validates a single use discount. |
|
| 87 | + * |
|
| 88 | + * @param WPInv_Discount $discount |
|
| 89 | + * @return bool |
|
| 90 | + */ |
|
| 91 | + public function is_discount_active( $discount ) { |
|
| 92 | + return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired(); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Returns a user's id or email. |
|
| 97 | + * |
|
| 98 | + * @param string $email |
|
| 99 | + * @return int|string|false |
|
| 100 | + */ |
|
| 101 | + public function get_user_id_or_email( $email ) { |
|
| 102 | + |
|
| 103 | + if ( is_user_logged_in() ) { |
|
| 104 | + return get_current_user_id(); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + return empty( $email ) ? false : sanitize_email( $email ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Validates a single use discount. |
|
| 112 | + * |
|
| 113 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 114 | + * @param WPInv_Discount $discount |
|
| 115 | + */ |
|
| 116 | + public function validate_single_use_discount( $submission, $discount ) { |
|
| 117 | + |
|
| 118 | + // Abort if it is not a single use discount. |
|
| 119 | + if ( ! $discount->is_single_use() ) { |
|
| 120 | + return; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + // Ensure there is a valid billing email. |
|
| 124 | + $user = $this->get_user_id_or_email( $submission->get_billing_email() ); |
|
| 125 | + |
|
| 126 | + if ( empty( $user ) ) { |
|
| 127 | + throw new Exception( __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) ); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + // Has the user used this discount code before? |
|
| 131 | + if ( ! $discount->is_valid_for_user( $user ) ) { |
|
| 132 | + throw new Exception( __( 'You have already used this discount', 'invoicing' ) ); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Validates the discount's amount. |
|
| 139 | + * |
|
| 140 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 141 | + * @param WPInv_Discount $discount |
|
| 142 | + * @param float $amount |
|
| 143 | + */ |
|
| 144 | + public function validate_discount_amount( $submission, $discount, $amount ) { |
|
| 145 | + |
|
| 146 | + // Validate minimum amount. |
|
| 147 | + if ( ! $discount->is_minimum_amount_met( $amount ) ) { |
|
| 148 | + $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() ); |
|
| 149 | + throw new Exception( sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) ); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + // Validate the maximum amount. |
|
| 153 | + if ( ! $discount->is_maximum_amount_met( $amount ) ) { |
|
| 154 | + $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() ); |
|
| 155 | + throw new Exception( sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) ); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Calculates the discount code's amount. |
|
| 162 | + * |
|
| 163 | + * Ensure that the discount exists and has been validated before calling this method. |
|
| 164 | + * |
|
| 165 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 166 | + * @param WPInv_Discount $discount |
|
| 167 | + * @return array |
|
| 168 | + */ |
|
| 169 | + public function calculate_discount( $submission, $discount ) { |
|
| 170 | + return getpaid_calculate_invoice_discount( $submission, $discount ); |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | 173 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | -defined( 'ABSPATH' ) || exit; |
|
| 7 | +defined('ABSPATH') || exit; |
|
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * Payment form submission discount class |
@@ -25,28 +25,28 @@ discard block |
||
| 25 | 25 | * @param float $initial_total |
| 26 | 26 | * @param float $recurring_total |
| 27 | 27 | */ |
| 28 | - public function __construct( $submission, $initial_total, $recurring_total ) { |
|
| 28 | + public function __construct($submission, $initial_total, $recurring_total) { |
|
| 29 | 29 | |
| 30 | 30 | // Process any existing invoice discounts. |
| 31 | - if ( $submission->has_invoice() ) { |
|
| 31 | + if ($submission->has_invoice()) { |
|
| 32 | 32 | $this->discounts = $submission->get_invoice()->get_discounts(); |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | // Do we have a discount? |
| 36 | - $discount = $submission->get_field( 'discount' ); |
|
| 36 | + $discount = $submission->get_field('discount'); |
|
| 37 | 37 | |
| 38 | - if ( empty( $discount ) ) { |
|
| 38 | + if (empty($discount)) { |
|
| 39 | 39 | |
| 40 | - if ( isset( $this->discounts['discount_code'] ) ) { |
|
| 41 | - unset( $this->discounts['discount_code'] ); |
|
| 40 | + if (isset($this->discounts['discount_code'])) { |
|
| 41 | + unset($this->discounts['discount_code']); |
|
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | return; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | // Processes the discount code. |
| 48 | - $amount = max( $initial_total, $recurring_total ); |
|
| 49 | - $this->process_discount( $submission, $discount, $amount ); |
|
| 48 | + $amount = max($initial_total, $recurring_total); |
|
| 49 | + $this->process_discount($submission, $discount, $amount); |
|
| 50 | 50 | |
| 51 | 51 | } |
| 52 | 52 | |
@@ -57,29 +57,29 @@ discard block |
||
| 57 | 57 | * @param string $discount |
| 58 | 58 | * @param float $amount |
| 59 | 59 | */ |
| 60 | - public function process_discount( $submission, $discount, $amount ) { |
|
| 60 | + public function process_discount($submission, $discount, $amount) { |
|
| 61 | 61 | |
| 62 | 62 | // Fetch the discount. |
| 63 | - $discount = new WPInv_Discount( $discount ); |
|
| 63 | + $discount = new WPInv_Discount($discount); |
|
| 64 | 64 | |
| 65 | 65 | // Ensure it is active. |
| 66 | - if ( ! $this->is_discount_active( $discount ) ) { |
|
| 67 | - throw new Exception( __( 'Invalid or expired discount code', 'invoicing' ) ); |
|
| 66 | + if (!$this->is_discount_active($discount)) { |
|
| 67 | + throw new Exception(__('Invalid or expired discount code', 'invoicing')); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | // Exceeded limit. |
| 71 | - if ( $discount->has_exceeded_limit() ) { |
|
| 72 | - throw new Exception( __( 'This discount code has been used up', 'invoicing' ) ); |
|
| 71 | + if ($discount->has_exceeded_limit()) { |
|
| 72 | + throw new Exception(__('This discount code has been used up', 'invoicing')); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | // Validate usages. |
| 76 | - $this->validate_single_use_discount( $submission, $discount ); |
|
| 76 | + $this->validate_single_use_discount($submission, $discount); |
|
| 77 | 77 | |
| 78 | 78 | // Validate amount. |
| 79 | - $this->validate_discount_amount( $submission, $discount, $amount ); |
|
| 79 | + $this->validate_discount_amount($submission, $discount, $amount); |
|
| 80 | 80 | |
| 81 | 81 | // Save the discount. |
| 82 | - $this->discounts['discount_code'] = $this->calculate_discount( $submission, $discount ); |
|
| 82 | + $this->discounts['discount_code'] = $this->calculate_discount($submission, $discount); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | /** |
@@ -88,8 +88,8 @@ discard block |
||
| 88 | 88 | * @param WPInv_Discount $discount |
| 89 | 89 | * @return bool |
| 90 | 90 | */ |
| 91 | - public function is_discount_active( $discount ) { |
|
| 92 | - return $discount->exists() && $discount->is_active() && $discount->has_started() && ! $discount->is_expired(); |
|
| 91 | + public function is_discount_active($discount) { |
|
| 92 | + return $discount->exists() && $discount->is_active() && $discount->has_started() && !$discount->is_expired(); |
|
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | /** |
@@ -98,13 +98,13 @@ discard block |
||
| 98 | 98 | * @param string $email |
| 99 | 99 | * @return int|string|false |
| 100 | 100 | */ |
| 101 | - public function get_user_id_or_email( $email ) { |
|
| 101 | + public function get_user_id_or_email($email) { |
|
| 102 | 102 | |
| 103 | - if ( is_user_logged_in() ) { |
|
| 103 | + if (is_user_logged_in()) { |
|
| 104 | 104 | return get_current_user_id(); |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | - return empty( $email ) ? false : sanitize_email( $email ); |
|
| 107 | + return empty($email) ? false : sanitize_email($email); |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
@@ -113,23 +113,23 @@ discard block |
||
| 113 | 113 | * @param GetPaid_Payment_Form_Submission $submission |
| 114 | 114 | * @param WPInv_Discount $discount |
| 115 | 115 | */ |
| 116 | - public function validate_single_use_discount( $submission, $discount ) { |
|
| 116 | + public function validate_single_use_discount($submission, $discount) { |
|
| 117 | 117 | |
| 118 | 118 | // Abort if it is not a single use discount. |
| 119 | - if ( ! $discount->is_single_use() ) { |
|
| 119 | + if (!$discount->is_single_use()) { |
|
| 120 | 120 | return; |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | // Ensure there is a valid billing email. |
| 124 | - $user = $this->get_user_id_or_email( $submission->get_billing_email() ); |
|
| 124 | + $user = $this->get_user_id_or_email($submission->get_billing_email()); |
|
| 125 | 125 | |
| 126 | - if ( empty( $user ) ) { |
|
| 127 | - throw new Exception( __( 'You need to either log in or enter your billing email before applying this discount', 'invoicing' ) ); |
|
| 126 | + if (empty($user)) { |
|
| 127 | + throw new Exception(__('You need to either log in or enter your billing email before applying this discount', 'invoicing')); |
|
| 128 | 128 | } |
| 129 | 129 | |
| 130 | 130 | // Has the user used this discount code before? |
| 131 | - if ( ! $discount->is_valid_for_user( $user ) ) { |
|
| 132 | - throw new Exception( __( 'You have already used this discount', 'invoicing' ) ); |
|
| 131 | + if (!$discount->is_valid_for_user($user)) { |
|
| 132 | + throw new Exception(__('You have already used this discount', 'invoicing')); |
|
| 133 | 133 | } |
| 134 | 134 | |
| 135 | 135 | } |
@@ -141,18 +141,18 @@ discard block |
||
| 141 | 141 | * @param WPInv_Discount $discount |
| 142 | 142 | * @param float $amount |
| 143 | 143 | */ |
| 144 | - public function validate_discount_amount( $submission, $discount, $amount ) { |
|
| 144 | + public function validate_discount_amount($submission, $discount, $amount) { |
|
| 145 | 145 | |
| 146 | 146 | // Validate minimum amount. |
| 147 | - if ( ! $discount->is_minimum_amount_met( $amount ) ) { |
|
| 148 | - $min = wpinv_price( $discount->get_minimum_total(), $submission->get_currency() ); |
|
| 149 | - throw new Exception( sprintf( __( 'The minimum total for using this discount is %s', 'invoicing' ), $min ) ); |
|
| 147 | + if (!$discount->is_minimum_amount_met($amount)) { |
|
| 148 | + $min = wpinv_price($discount->get_minimum_total(), $submission->get_currency()); |
|
| 149 | + throw new Exception(sprintf(__('The minimum total for using this discount is %s', 'invoicing'), $min)); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | // Validate the maximum amount. |
| 153 | - if ( ! $discount->is_maximum_amount_met( $amount ) ) { |
|
| 154 | - $max = wpinv_price( $discount->get_maximum_total(), $submission->get_currency() ); |
|
| 155 | - throw new Exception( sprintf( __( 'The maximum total for using this discount is %s', 'invoicing' ), $max ) ); |
|
| 153 | + if (!$discount->is_maximum_amount_met($amount)) { |
|
| 154 | + $max = wpinv_price($discount->get_maximum_total(), $submission->get_currency()); |
|
| 155 | + throw new Exception(sprintf(__('The maximum total for using this discount is %s', 'invoicing'), $max)); |
|
| 156 | 156 | } |
| 157 | 157 | |
| 158 | 158 | } |
@@ -166,8 +166,8 @@ discard block |
||
| 166 | 166 | * @param WPInv_Discount $discount |
| 167 | 167 | * @return array |
| 168 | 168 | */ |
| 169 | - public function calculate_discount( $submission, $discount ) { |
|
| 170 | - return getpaid_calculate_invoice_discount( $submission, $discount ); |
|
| 169 | + public function calculate_discount($submission, $discount) { |
|
| 170 | + return getpaid_calculate_invoice_discount($submission, $discount); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | } |
@@ -12,228 +12,228 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Payment_Form_Submission_Taxes { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Submission taxes. |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - public $taxes = array(); |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * Whether or not we should skip the taxes. |
|
| 23 | - * @var bool |
|
| 24 | - */ |
|
| 25 | - protected $skip_taxes = false; |
|
| 15 | + /** |
|
| 16 | + * Submission taxes. |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + public $taxes = array(); |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * Whether or not we should skip the taxes. |
|
| 23 | + * @var bool |
|
| 24 | + */ |
|
| 25 | + protected $skip_taxes = false; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Class constructor |
|
| 29 | + * |
|
| 30 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 31 | + */ |
|
| 32 | + public function __construct( $submission ) { |
|
| 33 | + |
|
| 34 | + // Validate VAT number. |
|
| 35 | + $this->validate_vat( $submission ); |
|
| 36 | + |
|
| 37 | + if ( $this->skip_taxes ) { |
|
| 38 | + return; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + foreach ( $submission->get_items() as $item ) { |
|
| 42 | + $this->process_item_tax( $item, $submission ); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + // Process any existing invoice taxes. |
|
| 46 | + if ( $submission->has_invoice() ) { |
|
| 47 | + $this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes ); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Maybe process tax. |
|
| 54 | + * |
|
| 55 | + * @since 1.0.19 |
|
| 56 | + * @param GetPaid_Form_Item $item |
|
| 57 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 58 | + */ |
|
| 59 | + public function process_item_tax( $item, $submission ) { |
|
| 60 | + |
|
| 61 | + $rates = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state ); |
|
| 62 | + $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
|
| 63 | + $taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates ); |
|
| 64 | + $r_taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates ); |
|
| 65 | + |
|
| 66 | + foreach ( $taxes as $name => $amount ) { |
|
| 67 | + $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0; |
|
| 68 | + $tax = getpaid_prepare_item_tax( $item, $name, $amount, $recurring ); |
|
| 69 | + |
|
| 70 | + if ( ! isset( $this->taxes[ $name ] ) ) { |
|
| 71 | + $this->taxes[ $name ] = $tax; |
|
| 72 | + continue; |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
|
| 76 | + $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
|
| 77 | + |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Checks if the submission has a digital item. |
|
| 84 | + * |
|
| 85 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 86 | + * @since 1.0.19 |
|
| 87 | + * @return bool |
|
| 88 | + */ |
|
| 89 | + public function has_digital_item( $submission ) { |
|
| 90 | + |
|
| 91 | + foreach ( $submission->get_items() as $item ) { |
|
| 92 | + |
|
| 93 | + if ( 'digital' == $item->get_vat_rule() ) { |
|
| 94 | + return true; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Checks if this is an eu store. |
|
| 104 | + * |
|
| 105 | + * @since 1.0.19 |
|
| 106 | + * @return bool |
|
| 107 | + */ |
|
| 108 | + public static function is_eu_store() { |
|
| 109 | + return self::is_eu_country( wpinv_get_default_country() ); |
|
| 110 | + } |
|
| 26 | 111 | |
| 27 | 112 | /** |
| 28 | - * Class constructor |
|
| 29 | - * |
|
| 30 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 31 | - */ |
|
| 32 | - public function __construct( $submission ) { |
|
| 33 | - |
|
| 34 | - // Validate VAT number. |
|
| 35 | - $this->validate_vat( $submission ); |
|
| 36 | - |
|
| 37 | - if ( $this->skip_taxes ) { |
|
| 38 | - return; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - foreach ( $submission->get_items() as $item ) { |
|
| 42 | - $this->process_item_tax( $item, $submission ); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - // Process any existing invoice taxes. |
|
| 46 | - if ( $submission->has_invoice() ) { |
|
| 47 | - $this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes ); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Maybe process tax. |
|
| 54 | - * |
|
| 55 | - * @since 1.0.19 |
|
| 56 | - * @param GetPaid_Form_Item $item |
|
| 57 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 58 | - */ |
|
| 59 | - public function process_item_tax( $item, $submission ) { |
|
| 60 | - |
|
| 61 | - $rates = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state ); |
|
| 62 | - $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
|
| 63 | - $taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates ); |
|
| 64 | - $r_taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates ); |
|
| 65 | - |
|
| 66 | - foreach ( $taxes as $name => $amount ) { |
|
| 67 | - $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0; |
|
| 68 | - $tax = getpaid_prepare_item_tax( $item, $name, $amount, $recurring ); |
|
| 69 | - |
|
| 70 | - if ( ! isset( $this->taxes[ $name ] ) ) { |
|
| 71 | - $this->taxes[ $name ] = $tax; |
|
| 72 | - continue; |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
|
| 76 | - $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
|
| 77 | - |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Checks if the submission has a digital item. |
|
| 84 | - * |
|
| 85 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 86 | - * @since 1.0.19 |
|
| 87 | - * @return bool |
|
| 88 | - */ |
|
| 89 | - public function has_digital_item( $submission ) { |
|
| 90 | - |
|
| 91 | - foreach ( $submission->get_items() as $item ) { |
|
| 92 | - |
|
| 93 | - if ( 'digital' == $item->get_vat_rule() ) { |
|
| 94 | - return true; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Checks if this is an eu store. |
|
| 104 | - * |
|
| 105 | - * @since 1.0.19 |
|
| 106 | - * @return bool |
|
| 107 | - */ |
|
| 108 | - public static function is_eu_store() { |
|
| 109 | - return self::is_eu_country( wpinv_get_default_country() ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Checks if this is an eu country. |
|
| 114 | - * |
|
| 115 | - * @param string $country |
|
| 116 | - * @since 1.0.19 |
|
| 117 | - * @return bool |
|
| 118 | - */ |
|
| 119 | - public static function is_eu_country( $country ) { |
|
| 120 | - return getpaid_is_eu_state( $country ) || getpaid_is_gst_country( $country ); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Checks if this is an eu purchase. |
|
| 125 | - * |
|
| 126 | - * @param string $customer_country |
|
| 127 | - * @since 1.0.19 |
|
| 128 | - * @return bool |
|
| 129 | - */ |
|
| 130 | - public static function is_eu_transaction( $customer_country ) { |
|
| 131 | - return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Retrieves the vat number. |
|
| 136 | - * |
|
| 137 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 138 | - * @since 1.0.19 |
|
| 139 | - * @return string |
|
| 140 | - */ |
|
| 141 | - public function get_vat_number( $submission ) { |
|
| 142 | - |
|
| 143 | - // Retrieve from the posted number. |
|
| 144 | - $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
|
| 145 | - if ( ! is_null( $vat_number ) ) { |
|
| 146 | - return wpinv_clean( $vat_number ); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Retrieves the company. |
|
| 154 | - * |
|
| 155 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 156 | - * @since 1.0.19 |
|
| 157 | - * @return string |
|
| 158 | - */ |
|
| 159 | - public function get_company( $submission ) { |
|
| 160 | - |
|
| 161 | - // Retrieve from the posted data. |
|
| 162 | - $company = $submission->get_field( 'wpinv_company', 'billing' ); |
|
| 163 | - if ( ! empty( $company ) ) { |
|
| 164 | - return wpinv_clean( $company ); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - // Retrieve from the invoice. |
|
| 168 | - return $submission->has_invoice() ? $submission->get_invoice()->get_company() : ''; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * Checks if we require a VAT number. |
|
| 173 | - * |
|
| 174 | - * @param bool $ip_in_eu Whether the customer IP is from the EU |
|
| 175 | - * @param bool $country_in_eu Whether the customer country is from the EU |
|
| 176 | - * @since 1.0.19 |
|
| 177 | - * @return string |
|
| 178 | - */ |
|
| 179 | - public function requires_vat( $ip_in_eu, $country_in_eu ) { |
|
| 180 | - |
|
| 181 | - $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' ); |
|
| 182 | - $prevent_b2c = ! empty( $prevent_b2c ); |
|
| 183 | - $is_eu = $ip_in_eu || $country_in_eu; |
|
| 184 | - |
|
| 185 | - return $prevent_b2c && $is_eu; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Validate VAT data. |
|
| 190 | - * |
|
| 191 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 192 | - * @since 1.0.19 |
|
| 193 | - */ |
|
| 194 | - public function validate_vat( $submission ) { |
|
| 195 | - |
|
| 196 | - $in_eu = $this->is_eu_transaction( $submission->country ); |
|
| 197 | - |
|
| 198 | - // Abort if we are not validating vat numbers. |
|
| 199 | - if ( ! $in_eu ) { |
|
| 113 | + * Checks if this is an eu country. |
|
| 114 | + * |
|
| 115 | + * @param string $country |
|
| 116 | + * @since 1.0.19 |
|
| 117 | + * @return bool |
|
| 118 | + */ |
|
| 119 | + public static function is_eu_country( $country ) { |
|
| 120 | + return getpaid_is_eu_state( $country ) || getpaid_is_gst_country( $country ); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Checks if this is an eu purchase. |
|
| 125 | + * |
|
| 126 | + * @param string $customer_country |
|
| 127 | + * @since 1.0.19 |
|
| 128 | + * @return bool |
|
| 129 | + */ |
|
| 130 | + public static function is_eu_transaction( $customer_country ) { |
|
| 131 | + return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Retrieves the vat number. |
|
| 136 | + * |
|
| 137 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 138 | + * @since 1.0.19 |
|
| 139 | + * @return string |
|
| 140 | + */ |
|
| 141 | + public function get_vat_number( $submission ) { |
|
| 142 | + |
|
| 143 | + // Retrieve from the posted number. |
|
| 144 | + $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
|
| 145 | + if ( ! is_null( $vat_number ) ) { |
|
| 146 | + return wpinv_clean( $vat_number ); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Retrieves the company. |
|
| 154 | + * |
|
| 155 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 156 | + * @since 1.0.19 |
|
| 157 | + * @return string |
|
| 158 | + */ |
|
| 159 | + public function get_company( $submission ) { |
|
| 160 | + |
|
| 161 | + // Retrieve from the posted data. |
|
| 162 | + $company = $submission->get_field( 'wpinv_company', 'billing' ); |
|
| 163 | + if ( ! empty( $company ) ) { |
|
| 164 | + return wpinv_clean( $company ); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + // Retrieve from the invoice. |
|
| 168 | + return $submission->has_invoice() ? $submission->get_invoice()->get_company() : ''; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * Checks if we require a VAT number. |
|
| 173 | + * |
|
| 174 | + * @param bool $ip_in_eu Whether the customer IP is from the EU |
|
| 175 | + * @param bool $country_in_eu Whether the customer country is from the EU |
|
| 176 | + * @since 1.0.19 |
|
| 177 | + * @return string |
|
| 178 | + */ |
|
| 179 | + public function requires_vat( $ip_in_eu, $country_in_eu ) { |
|
| 180 | + |
|
| 181 | + $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' ); |
|
| 182 | + $prevent_b2c = ! empty( $prevent_b2c ); |
|
| 183 | + $is_eu = $ip_in_eu || $country_in_eu; |
|
| 184 | + |
|
| 185 | + return $prevent_b2c && $is_eu; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Validate VAT data. |
|
| 190 | + * |
|
| 191 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 192 | + * @since 1.0.19 |
|
| 193 | + */ |
|
| 194 | + public function validate_vat( $submission ) { |
|
| 195 | + |
|
| 196 | + $in_eu = $this->is_eu_transaction( $submission->country ); |
|
| 197 | + |
|
| 198 | + // Abort if we are not validating vat numbers. |
|
| 199 | + if ( ! $in_eu ) { |
|
| 200 | 200 | return; |
| 201 | - } |
|
| 201 | + } |
|
| 202 | 202 | |
| 203 | - // Prepare variables. |
|
| 204 | - $vat_number = $this->get_vat_number( $submission ); |
|
| 205 | - $ip_country = getpaid_get_ip_country(); |
|
| 203 | + // Prepare variables. |
|
| 204 | + $vat_number = $this->get_vat_number( $submission ); |
|
| 205 | + $ip_country = getpaid_get_ip_country(); |
|
| 206 | 206 | $is_eu = $this->is_eu_country( $submission->country ); |
| 207 | 207 | $is_ip_eu = $this->is_eu_country( $ip_country ); |
| 208 | 208 | |
| 209 | - // Maybe abort early for initial fetches. |
|
| 210 | - if ( $submission->is_initial_fetch() && empty( $vat_number ) ) { |
|
| 211 | - return; |
|
| 212 | - } |
|
| 209 | + // Maybe abort early for initial fetches. |
|
| 210 | + if ( $submission->is_initial_fetch() && empty( $vat_number ) ) { |
|
| 211 | + return; |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - // If we're preventing business to consumer purchases, |
|
| 215 | - if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) { |
|
| 214 | + // If we're preventing business to consumer purchases, |
|
| 215 | + if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) { |
|
| 216 | 216 | |
| 217 | - // Ensure that a vat number has been specified. |
|
| 218 | - throw new Exception( |
|
| 219 | - __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) |
|
| 220 | - ); |
|
| 217 | + // Ensure that a vat number has been specified. |
|
| 218 | + throw new Exception( |
|
| 219 | + __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) |
|
| 220 | + ); |
|
| 221 | 221 | |
| 222 | - } |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | - if ( empty( $vat_number ) ) { |
|
| 225 | - return; |
|
| 226 | - } |
|
| 224 | + if ( empty( $vat_number ) ) { |
|
| 225 | + return; |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) { |
|
| 229 | - throw new Exception( __( 'Your VAT number is invalid', 'invoicing' ) ); |
|
| 230 | - } |
|
| 228 | + if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) { |
|
| 229 | + throw new Exception( __( 'Your VAT number is invalid', 'invoicing' ) ); |
|
| 230 | + } |
|
| 231 | 231 | |
| 232 | - if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) { |
|
| 233 | - return; |
|
| 234 | - } |
|
| 232 | + if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) { |
|
| 233 | + return; |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - $this->skip_taxes = true; |
|
| 237 | - } |
|
| 236 | + $this->skip_taxes = true; |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | 239 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | -defined( 'ABSPATH' ) || exit; |
|
| 7 | +defined('ABSPATH') || exit; |
|
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * Payment form submission taxes class |
@@ -29,22 +29,22 @@ discard block |
||
| 29 | 29 | * |
| 30 | 30 | * @param GetPaid_Payment_Form_Submission $submission |
| 31 | 31 | */ |
| 32 | - public function __construct( $submission ) { |
|
| 32 | + public function __construct($submission) { |
|
| 33 | 33 | |
| 34 | 34 | // Validate VAT number. |
| 35 | - $this->validate_vat( $submission ); |
|
| 35 | + $this->validate_vat($submission); |
|
| 36 | 36 | |
| 37 | - if ( $this->skip_taxes ) { |
|
| 37 | + if ($this->skip_taxes) { |
|
| 38 | 38 | return; |
| 39 | 39 | } |
| 40 | 40 | |
| 41 | - foreach ( $submission->get_items() as $item ) { |
|
| 42 | - $this->process_item_tax( $item, $submission ); |
|
| 41 | + foreach ($submission->get_items() as $item) { |
|
| 42 | + $this->process_item_tax($item, $submission); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | // Process any existing invoice taxes. |
| 46 | - if ( $submission->has_invoice() ) { |
|
| 47 | - $this->taxes = array_replace( $submission->get_invoice()->get_taxes(), $this->taxes ); |
|
| 46 | + if ($submission->has_invoice()) { |
|
| 47 | + $this->taxes = array_replace($submission->get_invoice()->get_taxes(), $this->taxes); |
|
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | } |
@@ -56,24 +56,24 @@ discard block |
||
| 56 | 56 | * @param GetPaid_Form_Item $item |
| 57 | 57 | * @param GetPaid_Payment_Form_Submission $submission |
| 58 | 58 | */ |
| 59 | - public function process_item_tax( $item, $submission ) { |
|
| 59 | + public function process_item_tax($item, $submission) { |
|
| 60 | 60 | |
| 61 | - $rates = getpaid_get_item_tax_rates( $item, $submission->country, $submission->state ); |
|
| 62 | - $rates = getpaid_filter_item_tax_rates( $item, $rates ); |
|
| 63 | - $taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, false ), $rates ); |
|
| 64 | - $r_taxes = getpaid_calculate_item_taxes( getpaid_get_taxable_amount( $item, true ), $rates ); |
|
| 61 | + $rates = getpaid_get_item_tax_rates($item, $submission->country, $submission->state); |
|
| 62 | + $rates = getpaid_filter_item_tax_rates($item, $rates); |
|
| 63 | + $taxes = getpaid_calculate_item_taxes(getpaid_get_taxable_amount($item, false), $rates); |
|
| 64 | + $r_taxes = getpaid_calculate_item_taxes(getpaid_get_taxable_amount($item, true), $rates); |
|
| 65 | 65 | |
| 66 | - foreach ( $taxes as $name => $amount ) { |
|
| 67 | - $recurring = isset( $r_taxes[ $name ] ) ? $r_taxes[ $name ] : 0; |
|
| 68 | - $tax = getpaid_prepare_item_tax( $item, $name, $amount, $recurring ); |
|
| 66 | + foreach ($taxes as $name => $amount) { |
|
| 67 | + $recurring = isset($r_taxes[$name]) ? $r_taxes[$name] : 0; |
|
| 68 | + $tax = getpaid_prepare_item_tax($item, $name, $amount, $recurring); |
|
| 69 | 69 | |
| 70 | - if ( ! isset( $this->taxes[ $name ] ) ) { |
|
| 71 | - $this->taxes[ $name ] = $tax; |
|
| 70 | + if (!isset($this->taxes[$name])) { |
|
| 71 | + $this->taxes[$name] = $tax; |
|
| 72 | 72 | continue; |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
|
| 76 | - $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
|
| 75 | + $this->taxes[$name]['initial_tax'] += $tax['initial_tax']; |
|
| 76 | + $this->taxes[$name]['recurring_tax'] += $tax['recurring_tax']; |
|
| 77 | 77 | |
| 78 | 78 | } |
| 79 | 79 | |
@@ -86,11 +86,11 @@ discard block |
||
| 86 | 86 | * @since 1.0.19 |
| 87 | 87 | * @return bool |
| 88 | 88 | */ |
| 89 | - public function has_digital_item( $submission ) { |
|
| 89 | + public function has_digital_item($submission) { |
|
| 90 | 90 | |
| 91 | - foreach ( $submission->get_items() as $item ) { |
|
| 91 | + foreach ($submission->get_items() as $item) { |
|
| 92 | 92 | |
| 93 | - if ( 'digital' == $item->get_vat_rule() ) { |
|
| 93 | + if ('digital' == $item->get_vat_rule()) { |
|
| 94 | 94 | return true; |
| 95 | 95 | } |
| 96 | 96 | |
@@ -106,7 +106,7 @@ discard block |
||
| 106 | 106 | * @return bool |
| 107 | 107 | */ |
| 108 | 108 | public static function is_eu_store() { |
| 109 | - return self::is_eu_country( wpinv_get_default_country() ); |
|
| 109 | + return self::is_eu_country(wpinv_get_default_country()); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
@@ -116,8 +116,8 @@ discard block |
||
| 116 | 116 | * @since 1.0.19 |
| 117 | 117 | * @return bool |
| 118 | 118 | */ |
| 119 | - public static function is_eu_country( $country ) { |
|
| 120 | - return getpaid_is_eu_state( $country ) || getpaid_is_gst_country( $country ); |
|
| 119 | + public static function is_eu_country($country) { |
|
| 120 | + return getpaid_is_eu_state($country) || getpaid_is_gst_country($country); |
|
| 121 | 121 | } |
| 122 | 122 | |
| 123 | 123 | /** |
@@ -127,8 +127,8 @@ discard block |
||
| 127 | 127 | * @since 1.0.19 |
| 128 | 128 | * @return bool |
| 129 | 129 | */ |
| 130 | - public static function is_eu_transaction( $customer_country ) { |
|
| 131 | - return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
|
| 130 | + public static function is_eu_transaction($customer_country) { |
|
| 131 | + return self::is_eu_country($customer_country) && self::is_eu_store(); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /** |
@@ -138,12 +138,12 @@ discard block |
||
| 138 | 138 | * @since 1.0.19 |
| 139 | 139 | * @return string |
| 140 | 140 | */ |
| 141 | - public function get_vat_number( $submission ) { |
|
| 141 | + public function get_vat_number($submission) { |
|
| 142 | 142 | |
| 143 | 143 | // Retrieve from the posted number. |
| 144 | - $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
|
| 145 | - if ( ! is_null( $vat_number ) ) { |
|
| 146 | - return wpinv_clean( $vat_number ); |
|
| 144 | + $vat_number = $submission->get_field('wpinv_vat_number', 'billing'); |
|
| 145 | + if (!is_null($vat_number)) { |
|
| 146 | + return wpinv_clean($vat_number); |
|
| 147 | 147 | } |
| 148 | 148 | |
| 149 | 149 | return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
@@ -156,12 +156,12 @@ discard block |
||
| 156 | 156 | * @since 1.0.19 |
| 157 | 157 | * @return string |
| 158 | 158 | */ |
| 159 | - public function get_company( $submission ) { |
|
| 159 | + public function get_company($submission) { |
|
| 160 | 160 | |
| 161 | 161 | // Retrieve from the posted data. |
| 162 | - $company = $submission->get_field( 'wpinv_company', 'billing' ); |
|
| 163 | - if ( ! empty( $company ) ) { |
|
| 164 | - return wpinv_clean( $company ); |
|
| 162 | + $company = $submission->get_field('wpinv_company', 'billing'); |
|
| 163 | + if (!empty($company)) { |
|
| 164 | + return wpinv_clean($company); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | // Retrieve from the invoice. |
@@ -176,10 +176,10 @@ discard block |
||
| 176 | 176 | * @since 1.0.19 |
| 177 | 177 | * @return string |
| 178 | 178 | */ |
| 179 | - public function requires_vat( $ip_in_eu, $country_in_eu ) { |
|
| 179 | + public function requires_vat($ip_in_eu, $country_in_eu) { |
|
| 180 | 180 | |
| 181 | - $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' ); |
|
| 182 | - $prevent_b2c = ! empty( $prevent_b2c ); |
|
| 181 | + $prevent_b2c = wpinv_get_option('vat_prevent_b2c_purchase'); |
|
| 182 | + $prevent_b2c = !empty($prevent_b2c); |
|
| 183 | 183 | $is_eu = $ip_in_eu || $country_in_eu; |
| 184 | 184 | |
| 185 | 185 | return $prevent_b2c && $is_eu; |
@@ -191,45 +191,45 @@ discard block |
||
| 191 | 191 | * @param GetPaid_Payment_Form_Submission $submission |
| 192 | 192 | * @since 1.0.19 |
| 193 | 193 | */ |
| 194 | - public function validate_vat( $submission ) { |
|
| 194 | + public function validate_vat($submission) { |
|
| 195 | 195 | |
| 196 | - $in_eu = $this->is_eu_transaction( $submission->country ); |
|
| 196 | + $in_eu = $this->is_eu_transaction($submission->country); |
|
| 197 | 197 | |
| 198 | 198 | // Abort if we are not validating vat numbers. |
| 199 | - if ( ! $in_eu ) { |
|
| 199 | + if (!$in_eu) { |
|
| 200 | 200 | return; |
| 201 | 201 | } |
| 202 | 202 | |
| 203 | 203 | // Prepare variables. |
| 204 | - $vat_number = $this->get_vat_number( $submission ); |
|
| 204 | + $vat_number = $this->get_vat_number($submission); |
|
| 205 | 205 | $ip_country = getpaid_get_ip_country(); |
| 206 | - $is_eu = $this->is_eu_country( $submission->country ); |
|
| 207 | - $is_ip_eu = $this->is_eu_country( $ip_country ); |
|
| 206 | + $is_eu = $this->is_eu_country($submission->country); |
|
| 207 | + $is_ip_eu = $this->is_eu_country($ip_country); |
|
| 208 | 208 | |
| 209 | 209 | // Maybe abort early for initial fetches. |
| 210 | - if ( $submission->is_initial_fetch() && empty( $vat_number ) ) { |
|
| 210 | + if ($submission->is_initial_fetch() && empty($vat_number)) { |
|
| 211 | 211 | return; |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | // If we're preventing business to consumer purchases, |
| 215 | - if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) { |
|
| 215 | + if ($this->requires_vat($is_ip_eu, $is_eu) && empty($vat_number)) { |
|
| 216 | 216 | |
| 217 | 217 | // Ensure that a vat number has been specified. |
| 218 | 218 | throw new Exception( |
| 219 | - __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) |
|
| 219 | + __('Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing') |
|
| 220 | 220 | ); |
| 221 | 221 | |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | - if ( empty( $vat_number ) ) { |
|
| 224 | + if (empty($vat_number)) { |
|
| 225 | 225 | return; |
| 226 | 226 | } |
| 227 | 227 | |
| 228 | - if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) { |
|
| 229 | - throw new Exception( __( 'Your VAT number is invalid', 'invoicing' ) ); |
|
| 228 | + if (wpinv_should_validate_vat_number() && !wpinv_validate_vat_number($vat_number, $submission->country)) { |
|
| 229 | + throw new Exception(__('Your VAT number is invalid', 'invoicing')); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | - if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) { |
|
| 232 | + if (wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option('vat_same_country_rule', 'vat_too')) { |
|
| 233 | 233 | return; |
| 234 | 234 | } |
| 235 | 235 | |
@@ -9,7 +9,7 @@ discard block |
||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | - exit; |
|
| 12 | + exit; |
|
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | /** |
@@ -21,356 +21,356 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | abstract class GetPaid_Data { |
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * ID for this object. |
|
| 26 | - * |
|
| 27 | - * @since 1.0.19 |
|
| 28 | - * @var int |
|
| 29 | - */ |
|
| 30 | - protected $id = 0; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * Core data for this object. Name value pairs (name + default value). |
|
| 34 | - * |
|
| 35 | - * @since 1.0.19 |
|
| 36 | - * @var array |
|
| 37 | - */ |
|
| 38 | - protected $data = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * Core data changes for this object. |
|
| 42 | - * |
|
| 43 | - * @since 1.0.19 |
|
| 44 | - * @var array |
|
| 45 | - */ |
|
| 46 | - protected $changes = array(); |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * This is false until the object is read from the DB. |
|
| 50 | - * |
|
| 51 | - * @since 1.0.19 |
|
| 52 | - * @var bool |
|
| 53 | - */ |
|
| 54 | - protected $object_read = false; |
|
| 55 | - |
|
| 56 | - /** |
|
| 57 | - * This is the name of this object type. |
|
| 58 | - * |
|
| 59 | - * @since 1.0.19 |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 62 | - protected $object_type = 'data'; |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Extra data for this object. Name value pairs (name + default value). |
|
| 66 | - * Used as a standard way for sub classes (like item types) to add |
|
| 67 | - * additional information to an inherited class. |
|
| 68 | - * |
|
| 69 | - * @since 1.0.19 |
|
| 70 | - * @var array |
|
| 71 | - */ |
|
| 72 | - protected $extra_data = array(); |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Set to _data on construct so we can track and reset data if needed. |
|
| 76 | - * |
|
| 77 | - * @since 1.0.19 |
|
| 78 | - * @var array |
|
| 79 | - */ |
|
| 80 | - protected $default_data = array(); |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Contains a reference to the data store for this class. |
|
| 84 | - * |
|
| 85 | - * @since 1.0.19 |
|
| 86 | - * @var GetPaid_Data_Store |
|
| 87 | - */ |
|
| 88 | - protected $data_store; |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Stores meta in cache for future reads. |
|
| 92 | - * A group must be set to to enable caching. |
|
| 93 | - * |
|
| 94 | - * @since 1.0.19 |
|
| 95 | - * @var string |
|
| 96 | - */ |
|
| 97 | - protected $cache_group = ''; |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * Stores the last error. |
|
| 101 | - * |
|
| 102 | - * @since 1.0.19 |
|
| 103 | - * @var string |
|
| 104 | - */ |
|
| 105 | - public $last_error = ''; |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Stores additional meta data. |
|
| 109 | - * |
|
| 110 | - * @since 1.0.19 |
|
| 111 | - * @var array |
|
| 112 | - */ |
|
| 113 | - protected $meta_data = null; |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * Default constructor. |
|
| 117 | - * |
|
| 118 | - * @param int|object|array|string $read ID to load from the DB (optional) or already queried data. |
|
| 119 | - */ |
|
| 120 | - public function __construct( $read = 0 ) { |
|
| 121 | - $this->data = array_merge( $this->data, $this->extra_data ); |
|
| 122 | - $this->default_data = $this->data; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Only store the object ID to avoid serializing the data object instance. |
|
| 127 | - * |
|
| 128 | - * @return array |
|
| 129 | - */ |
|
| 130 | - public function __sleep() { |
|
| 131 | - return array( 'id' ); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Re-run the constructor with the object ID. |
|
| 136 | - * |
|
| 137 | - * If the object no longer exists, remove the ID. |
|
| 138 | - */ |
|
| 139 | - public function __wakeup() { |
|
| 140 | - $this->__construct( absint( $this->id ) ); |
|
| 141 | - |
|
| 142 | - if ( ! empty( $this->last_error ) ) { |
|
| 143 | - $this->set_id( 0 ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * When the object is cloned, make sure meta is duplicated correctly. |
|
| 150 | - * |
|
| 151 | - * @since 1.0.19 |
|
| 152 | - */ |
|
| 153 | - public function __clone() { |
|
| 154 | - $this->maybe_read_meta_data(); |
|
| 155 | - if ( ! empty( $this->meta_data ) ) { |
|
| 156 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 157 | - $this->meta_data[ $array_key ] = clone $meta; |
|
| 158 | - if ( ! empty( $meta->id ) ) { |
|
| 159 | - $this->meta_data[ $array_key ]->id = null; |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - } |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * Get the data store. |
|
| 167 | - * |
|
| 168 | - * @since 1.0.19 |
|
| 169 | - * @return object |
|
| 170 | - */ |
|
| 171 | - public function get_data_store() { |
|
| 172 | - return $this->data_store; |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Get the object type. |
|
| 177 | - * |
|
| 178 | - * @since 1.0.19 |
|
| 179 | - * @return string |
|
| 180 | - */ |
|
| 181 | - public function get_object_type() { |
|
| 182 | - return $this->object_type; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Returns the unique ID for this object. |
|
| 187 | - * |
|
| 188 | - * @since 1.0.19 |
|
| 189 | - * @return int |
|
| 190 | - */ |
|
| 191 | - public function get_id() { |
|
| 192 | - return $this->id; |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * Get form status. |
|
| 197 | - * |
|
| 198 | - * @since 1.0.19 |
|
| 199 | - * @param string $context View or edit context. |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - public function get_status( $context = 'view' ) { |
|
| 203 | - return $this->get_prop( 'status', $context ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Delete an object, set the ID to 0, and return result. |
|
| 208 | - * |
|
| 209 | - * @since 1.0.19 |
|
| 210 | - * @param bool $force_delete Should the data be deleted permanently. |
|
| 211 | - * @return bool result |
|
| 212 | - */ |
|
| 213 | - public function delete( $force_delete = false ) { |
|
| 214 | - if ( $this->data_store && $this->exists() ) { |
|
| 215 | - $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
| 216 | - $this->set_id( 0 ); |
|
| 217 | - return true; |
|
| 218 | - } |
|
| 219 | - return false; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Save should create or update based on object existence. |
|
| 224 | - * |
|
| 225 | - * @since 1.0.19 |
|
| 226 | - * @return int |
|
| 227 | - */ |
|
| 228 | - public function save() { |
|
| 229 | - if ( ! $this->data_store ) { |
|
| 230 | - return $this->get_id(); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
| 235 | - * |
|
| 236 | - * @param GetPaid_Data $this The object being saved. |
|
| 237 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 238 | - */ |
|
| 239 | - do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 240 | - |
|
| 241 | - if ( $this->get_id() ) { |
|
| 242 | - $this->data_store->update( $this ); |
|
| 243 | - } else { |
|
| 244 | - $this->data_store->create( $this ); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * Trigger action after saving to the DB. |
|
| 249 | - * |
|
| 250 | - * @param GetPaid_Data $this The object being saved. |
|
| 251 | - * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 252 | - */ |
|
| 253 | - do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 254 | - |
|
| 255 | - return $this->get_id(); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - /** |
|
| 259 | - * Change data to JSON format. |
|
| 260 | - * |
|
| 261 | - * @since 1.0.19 |
|
| 262 | - * @return string Data in JSON format. |
|
| 263 | - */ |
|
| 264 | - public function __toString() { |
|
| 265 | - return wp_json_encode( $this->get_data() ); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Returns all data for this object. |
|
| 270 | - * |
|
| 271 | - * @since 1.0.19 |
|
| 272 | - * @return array |
|
| 273 | - */ |
|
| 274 | - public function get_data() { |
|
| 275 | - return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Returns array of expected data keys for this object. |
|
| 280 | - * |
|
| 281 | - * @since 1.0.19 |
|
| 282 | - * @return array |
|
| 283 | - */ |
|
| 284 | - public function get_data_keys() { |
|
| 285 | - return array_keys( $this->data ); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Returns all "extra" data keys for an object (for sub objects like item types). |
|
| 290 | - * |
|
| 291 | - * @since 1.0.19 |
|
| 292 | - * @return array |
|
| 293 | - */ |
|
| 294 | - public function get_extra_data_keys() { |
|
| 295 | - return array_keys( $this->extra_data ); |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Filter null meta values from array. |
|
| 300 | - * |
|
| 301 | - * @since 1.0.19 |
|
| 302 | - * @param mixed $meta Meta value to check. |
|
| 303 | - * @return bool |
|
| 304 | - */ |
|
| 305 | - protected function filter_null_meta( $meta ) { |
|
| 306 | - return ! is_null( $meta->value ); |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - /** |
|
| 310 | - * Get All Meta Data. |
|
| 311 | - * |
|
| 312 | - * @since 1.0.19 |
|
| 313 | - * @return array of objects. |
|
| 314 | - */ |
|
| 315 | - public function get_meta_data() { |
|
| 316 | - $this->maybe_read_meta_data(); |
|
| 317 | - return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Check if the key is an internal one. |
|
| 322 | - * |
|
| 323 | - * @since 1.0.19 |
|
| 324 | - * @param string $key Key to check. |
|
| 325 | - * @return bool true if it's an internal key, false otherwise |
|
| 326 | - */ |
|
| 327 | - protected function is_internal_meta_key( $key ) { |
|
| 328 | - $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
| 329 | - |
|
| 330 | - if ( ! $internal_meta_key ) { |
|
| 331 | - return false; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
| 335 | - |
|
| 336 | - if ( ! $has_setter_or_getter ) { |
|
| 337 | - return false; |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - /* translators: %s: $key Key to check */ |
|
| 341 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 342 | - |
|
| 343 | - return true; |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Magic method for setting data fields. |
|
| 348 | - * |
|
| 349 | - * This method does not update custom fields in the database. |
|
| 350 | - * |
|
| 351 | - * @since 1.0.19 |
|
| 352 | - * @access public |
|
| 353 | - * |
|
| 354 | - */ |
|
| 355 | - public function __set( $key, $value ) { |
|
| 356 | - |
|
| 357 | - if ( 'id' == strtolower( $key ) ) { |
|
| 358 | - return $this->set_id( $value ); |
|
| 359 | - } |
|
| 360 | - |
|
| 361 | - if ( method_exists( $this, "set_$key") ) { |
|
| 362 | - |
|
| 363 | - /* translators: %s: $key Key to set */ |
|
| 364 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 365 | - |
|
| 366 | - call_user_func( array( $this, "set_$key" ), $value ); |
|
| 367 | - } else { |
|
| 368 | - $this->set_prop( $key, $value ); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** |
|
| 24 | + /** |
|
| 25 | + * ID for this object. |
|
| 26 | + * |
|
| 27 | + * @since 1.0.19 |
|
| 28 | + * @var int |
|
| 29 | + */ |
|
| 30 | + protected $id = 0; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * Core data for this object. Name value pairs (name + default value). |
|
| 34 | + * |
|
| 35 | + * @since 1.0.19 |
|
| 36 | + * @var array |
|
| 37 | + */ |
|
| 38 | + protected $data = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * Core data changes for this object. |
|
| 42 | + * |
|
| 43 | + * @since 1.0.19 |
|
| 44 | + * @var array |
|
| 45 | + */ |
|
| 46 | + protected $changes = array(); |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * This is false until the object is read from the DB. |
|
| 50 | + * |
|
| 51 | + * @since 1.0.19 |
|
| 52 | + * @var bool |
|
| 53 | + */ |
|
| 54 | + protected $object_read = false; |
|
| 55 | + |
|
| 56 | + /** |
|
| 57 | + * This is the name of this object type. |
|
| 58 | + * |
|
| 59 | + * @since 1.0.19 |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | + protected $object_type = 'data'; |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Extra data for this object. Name value pairs (name + default value). |
|
| 66 | + * Used as a standard way for sub classes (like item types) to add |
|
| 67 | + * additional information to an inherited class. |
|
| 68 | + * |
|
| 69 | + * @since 1.0.19 |
|
| 70 | + * @var array |
|
| 71 | + */ |
|
| 72 | + protected $extra_data = array(); |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Set to _data on construct so we can track and reset data if needed. |
|
| 76 | + * |
|
| 77 | + * @since 1.0.19 |
|
| 78 | + * @var array |
|
| 79 | + */ |
|
| 80 | + protected $default_data = array(); |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Contains a reference to the data store for this class. |
|
| 84 | + * |
|
| 85 | + * @since 1.0.19 |
|
| 86 | + * @var GetPaid_Data_Store |
|
| 87 | + */ |
|
| 88 | + protected $data_store; |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Stores meta in cache for future reads. |
|
| 92 | + * A group must be set to to enable caching. |
|
| 93 | + * |
|
| 94 | + * @since 1.0.19 |
|
| 95 | + * @var string |
|
| 96 | + */ |
|
| 97 | + protected $cache_group = ''; |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * Stores the last error. |
|
| 101 | + * |
|
| 102 | + * @since 1.0.19 |
|
| 103 | + * @var string |
|
| 104 | + */ |
|
| 105 | + public $last_error = ''; |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Stores additional meta data. |
|
| 109 | + * |
|
| 110 | + * @since 1.0.19 |
|
| 111 | + * @var array |
|
| 112 | + */ |
|
| 113 | + protected $meta_data = null; |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * Default constructor. |
|
| 117 | + * |
|
| 118 | + * @param int|object|array|string $read ID to load from the DB (optional) or already queried data. |
|
| 119 | + */ |
|
| 120 | + public function __construct( $read = 0 ) { |
|
| 121 | + $this->data = array_merge( $this->data, $this->extra_data ); |
|
| 122 | + $this->default_data = $this->data; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Only store the object ID to avoid serializing the data object instance. |
|
| 127 | + * |
|
| 128 | + * @return array |
|
| 129 | + */ |
|
| 130 | + public function __sleep() { |
|
| 131 | + return array( 'id' ); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Re-run the constructor with the object ID. |
|
| 136 | + * |
|
| 137 | + * If the object no longer exists, remove the ID. |
|
| 138 | + */ |
|
| 139 | + public function __wakeup() { |
|
| 140 | + $this->__construct( absint( $this->id ) ); |
|
| 141 | + |
|
| 142 | + if ( ! empty( $this->last_error ) ) { |
|
| 143 | + $this->set_id( 0 ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * When the object is cloned, make sure meta is duplicated correctly. |
|
| 150 | + * |
|
| 151 | + * @since 1.0.19 |
|
| 152 | + */ |
|
| 153 | + public function __clone() { |
|
| 154 | + $this->maybe_read_meta_data(); |
|
| 155 | + if ( ! empty( $this->meta_data ) ) { |
|
| 156 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 157 | + $this->meta_data[ $array_key ] = clone $meta; |
|
| 158 | + if ( ! empty( $meta->id ) ) { |
|
| 159 | + $this->meta_data[ $array_key ]->id = null; |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + } |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * Get the data store. |
|
| 167 | + * |
|
| 168 | + * @since 1.0.19 |
|
| 169 | + * @return object |
|
| 170 | + */ |
|
| 171 | + public function get_data_store() { |
|
| 172 | + return $this->data_store; |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Get the object type. |
|
| 177 | + * |
|
| 178 | + * @since 1.0.19 |
|
| 179 | + * @return string |
|
| 180 | + */ |
|
| 181 | + public function get_object_type() { |
|
| 182 | + return $this->object_type; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Returns the unique ID for this object. |
|
| 187 | + * |
|
| 188 | + * @since 1.0.19 |
|
| 189 | + * @return int |
|
| 190 | + */ |
|
| 191 | + public function get_id() { |
|
| 192 | + return $this->id; |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * Get form status. |
|
| 197 | + * |
|
| 198 | + * @since 1.0.19 |
|
| 199 | + * @param string $context View or edit context. |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + public function get_status( $context = 'view' ) { |
|
| 203 | + return $this->get_prop( 'status', $context ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Delete an object, set the ID to 0, and return result. |
|
| 208 | + * |
|
| 209 | + * @since 1.0.19 |
|
| 210 | + * @param bool $force_delete Should the data be deleted permanently. |
|
| 211 | + * @return bool result |
|
| 212 | + */ |
|
| 213 | + public function delete( $force_delete = false ) { |
|
| 214 | + if ( $this->data_store && $this->exists() ) { |
|
| 215 | + $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
| 216 | + $this->set_id( 0 ); |
|
| 217 | + return true; |
|
| 218 | + } |
|
| 219 | + return false; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Save should create or update based on object existence. |
|
| 224 | + * |
|
| 225 | + * @since 1.0.19 |
|
| 226 | + * @return int |
|
| 227 | + */ |
|
| 228 | + public function save() { |
|
| 229 | + if ( ! $this->data_store ) { |
|
| 230 | + return $this->get_id(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Trigger action before saving to the DB. Allows you to adjust object props before save. |
|
| 235 | + * |
|
| 236 | + * @param GetPaid_Data $this The object being saved. |
|
| 237 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 238 | + */ |
|
| 239 | + do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 240 | + |
|
| 241 | + if ( $this->get_id() ) { |
|
| 242 | + $this->data_store->update( $this ); |
|
| 243 | + } else { |
|
| 244 | + $this->data_store->create( $this ); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * Trigger action after saving to the DB. |
|
| 249 | + * |
|
| 250 | + * @param GetPaid_Data $this The object being saved. |
|
| 251 | + * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
|
| 252 | + */ |
|
| 253 | + do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 254 | + |
|
| 255 | + return $this->get_id(); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + /** |
|
| 259 | + * Change data to JSON format. |
|
| 260 | + * |
|
| 261 | + * @since 1.0.19 |
|
| 262 | + * @return string Data in JSON format. |
|
| 263 | + */ |
|
| 264 | + public function __toString() { |
|
| 265 | + return wp_json_encode( $this->get_data() ); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Returns all data for this object. |
|
| 270 | + * |
|
| 271 | + * @since 1.0.19 |
|
| 272 | + * @return array |
|
| 273 | + */ |
|
| 274 | + public function get_data() { |
|
| 275 | + return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Returns array of expected data keys for this object. |
|
| 280 | + * |
|
| 281 | + * @since 1.0.19 |
|
| 282 | + * @return array |
|
| 283 | + */ |
|
| 284 | + public function get_data_keys() { |
|
| 285 | + return array_keys( $this->data ); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Returns all "extra" data keys for an object (for sub objects like item types). |
|
| 290 | + * |
|
| 291 | + * @since 1.0.19 |
|
| 292 | + * @return array |
|
| 293 | + */ |
|
| 294 | + public function get_extra_data_keys() { |
|
| 295 | + return array_keys( $this->extra_data ); |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Filter null meta values from array. |
|
| 300 | + * |
|
| 301 | + * @since 1.0.19 |
|
| 302 | + * @param mixed $meta Meta value to check. |
|
| 303 | + * @return bool |
|
| 304 | + */ |
|
| 305 | + protected function filter_null_meta( $meta ) { |
|
| 306 | + return ! is_null( $meta->value ); |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + /** |
|
| 310 | + * Get All Meta Data. |
|
| 311 | + * |
|
| 312 | + * @since 1.0.19 |
|
| 313 | + * @return array of objects. |
|
| 314 | + */ |
|
| 315 | + public function get_meta_data() { |
|
| 316 | + $this->maybe_read_meta_data(); |
|
| 317 | + return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Check if the key is an internal one. |
|
| 322 | + * |
|
| 323 | + * @since 1.0.19 |
|
| 324 | + * @param string $key Key to check. |
|
| 325 | + * @return bool true if it's an internal key, false otherwise |
|
| 326 | + */ |
|
| 327 | + protected function is_internal_meta_key( $key ) { |
|
| 328 | + $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
| 329 | + |
|
| 330 | + if ( ! $internal_meta_key ) { |
|
| 331 | + return false; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
| 335 | + |
|
| 336 | + if ( ! $has_setter_or_getter ) { |
|
| 337 | + return false; |
|
| 338 | + } |
|
| 339 | + |
|
| 340 | + /* translators: %s: $key Key to check */ |
|
| 341 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 342 | + |
|
| 343 | + return true; |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Magic method for setting data fields. |
|
| 348 | + * |
|
| 349 | + * This method does not update custom fields in the database. |
|
| 350 | + * |
|
| 351 | + * @since 1.0.19 |
|
| 352 | + * @access public |
|
| 353 | + * |
|
| 354 | + */ |
|
| 355 | + public function __set( $key, $value ) { |
|
| 356 | + |
|
| 357 | + if ( 'id' == strtolower( $key ) ) { |
|
| 358 | + return $this->set_id( $value ); |
|
| 359 | + } |
|
| 360 | + |
|
| 361 | + if ( method_exists( $this, "set_$key") ) { |
|
| 362 | + |
|
| 363 | + /* translators: %s: $key Key to set */ |
|
| 364 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 365 | + |
|
| 366 | + call_user_func( array( $this, "set_$key" ), $value ); |
|
| 367 | + } else { |
|
| 368 | + $this->set_prop( $key, $value ); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** |
|
| 374 | 374 | * Margic method for retrieving a property. |
| 375 | 375 | */ |
| 376 | 376 | public function __get( $key ) { |
@@ -378,10 +378,10 @@ discard block |
||
| 378 | 378 | // Check if we have a helper method for that. |
| 379 | 379 | if ( method_exists( $this, 'get_' . $key ) ) { |
| 380 | 380 | |
| 381 | - if ( 'post_type' != $key ) { |
|
| 382 | - /* translators: %s: $key Key to set */ |
|
| 383 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 384 | - } |
|
| 381 | + if ( 'post_type' != $key ) { |
|
| 382 | + /* translators: %s: $key Key to set */ |
|
| 383 | + getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 384 | + } |
|
| 385 | 385 | |
| 386 | 386 | return call_user_func( array( $this, 'get_' . $key ) ); |
| 387 | 387 | } |
@@ -391,514 +391,514 @@ discard block |
||
| 391 | 391 | return $this->post->$key; |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | - return $this->get_prop( $key ); |
|
| 395 | - |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - /** |
|
| 399 | - * Get Meta Data by Key. |
|
| 400 | - * |
|
| 401 | - * @since 1.0.19 |
|
| 402 | - * @param string $key Meta Key. |
|
| 403 | - * @param bool $single return first found meta with key, or all with $key. |
|
| 404 | - * @param string $context What the value is for. Valid values are view and edit. |
|
| 405 | - * @return mixed |
|
| 406 | - */ |
|
| 407 | - public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
| 408 | - |
|
| 409 | - // Check if this is an internal meta key. |
|
| 410 | - $_key = str_replace( '_wpinv', '', $key ); |
|
| 411 | - $_key = str_replace( 'wpinv', '', $_key ); |
|
| 412 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 413 | - $function = 'get_' . $_key; |
|
| 414 | - |
|
| 415 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 416 | - return $this->{$function}(); |
|
| 417 | - } |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - // Read the meta data if not yet read. |
|
| 421 | - $this->maybe_read_meta_data(); |
|
| 422 | - $meta_data = $this->get_meta_data(); |
|
| 423 | - $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
| 424 | - $value = $single ? '' : array(); |
|
| 425 | - |
|
| 426 | - if ( ! empty( $array_keys ) ) { |
|
| 427 | - // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
| 428 | - if ( $single ) { |
|
| 429 | - $value = $meta_data[ current( $array_keys ) ]->value; |
|
| 430 | - } else { |
|
| 431 | - $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
| 432 | - } |
|
| 433 | - } |
|
| 434 | - |
|
| 435 | - if ( 'view' === $context ) { |
|
| 436 | - $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - return $value; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * See if meta data exists, since get_meta always returns a '' or array(). |
|
| 444 | - * |
|
| 445 | - * @since 1.0.19 |
|
| 446 | - * @param string $key Meta Key. |
|
| 447 | - * @return boolean |
|
| 448 | - */ |
|
| 449 | - public function meta_exists( $key = '' ) { |
|
| 450 | - $this->maybe_read_meta_data(); |
|
| 451 | - $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
| 452 | - return in_array( $key, $array_keys, true ); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Set all meta data from array. |
|
| 457 | - * |
|
| 458 | - * @since 1.0.19 |
|
| 459 | - * @param array $data Key/Value pairs. |
|
| 460 | - */ |
|
| 461 | - public function set_meta_data( $data ) { |
|
| 462 | - if ( ! empty( $data ) && is_array( $data ) ) { |
|
| 463 | - $this->maybe_read_meta_data(); |
|
| 464 | - foreach ( $data as $meta ) { |
|
| 465 | - $meta = (array) $meta; |
|
| 466 | - if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
| 467 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 468 | - array( |
|
| 469 | - 'id' => $meta['id'], |
|
| 470 | - 'key' => $meta['key'], |
|
| 471 | - 'value' => $meta['value'], |
|
| 472 | - ) |
|
| 473 | - ); |
|
| 474 | - } |
|
| 475 | - } |
|
| 476 | - } |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - /** |
|
| 480 | - * Add meta data. |
|
| 481 | - * |
|
| 482 | - * @since 1.0.19 |
|
| 483 | - * |
|
| 484 | - * @param string $key Meta key. |
|
| 485 | - * @param string|array $value Meta value. |
|
| 486 | - * @param bool $unique Should this be a unique key?. |
|
| 487 | - */ |
|
| 488 | - public function add_meta_data( $key, $value, $unique = false ) { |
|
| 489 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 490 | - $function = 'set_' . $key; |
|
| 491 | - |
|
| 492 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 493 | - return $this->{$function}( $value ); |
|
| 494 | - } |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - $this->maybe_read_meta_data(); |
|
| 498 | - if ( $unique ) { |
|
| 499 | - $this->delete_meta_data( $key ); |
|
| 500 | - } |
|
| 501 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 502 | - array( |
|
| 503 | - 'key' => $key, |
|
| 504 | - 'value' => $value, |
|
| 505 | - ) |
|
| 506 | - ); |
|
| 507 | - |
|
| 508 | - $this->save(); |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * Update meta data by key or ID, if provided. |
|
| 513 | - * |
|
| 514 | - * @since 1.0.19 |
|
| 515 | - * |
|
| 516 | - * @param string $key Meta key. |
|
| 517 | - * @param string|array $value Meta value. |
|
| 518 | - * @param int $meta_id Meta ID. |
|
| 519 | - */ |
|
| 520 | - public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
| 521 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 522 | - $function = 'set_' . $key; |
|
| 523 | - |
|
| 524 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 525 | - return $this->{$function}( $value ); |
|
| 526 | - } |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - $this->maybe_read_meta_data(); |
|
| 530 | - |
|
| 531 | - $array_key = false; |
|
| 532 | - |
|
| 533 | - if ( $meta_id ) { |
|
| 534 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
| 535 | - $array_key = $array_keys ? current( $array_keys ) : false; |
|
| 536 | - } else { |
|
| 537 | - // Find matches by key. |
|
| 538 | - $matches = array(); |
|
| 539 | - foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
| 540 | - if ( $meta->key === $key ) { |
|
| 541 | - $matches[] = $meta_data_array_key; |
|
| 542 | - } |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - if ( ! empty( $matches ) ) { |
|
| 546 | - // Set matches to null so only one key gets the new value. |
|
| 547 | - foreach ( $matches as $meta_data_array_key ) { |
|
| 548 | - $this->meta_data[ $meta_data_array_key ]->value = null; |
|
| 549 | - } |
|
| 550 | - $array_key = current( $matches ); |
|
| 551 | - } |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - if ( false !== $array_key ) { |
|
| 555 | - $meta = $this->meta_data[ $array_key ]; |
|
| 556 | - $meta->key = $key; |
|
| 557 | - $meta->value = $value; |
|
| 558 | - } else { |
|
| 559 | - $this->add_meta_data( $key, $value, true ); |
|
| 560 | - } |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * Delete meta data. |
|
| 565 | - * |
|
| 566 | - * @since 1.0.19 |
|
| 567 | - * @param string $key Meta key. |
|
| 568 | - */ |
|
| 569 | - public function delete_meta_data( $key ) { |
|
| 570 | - $this->maybe_read_meta_data(); |
|
| 571 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
| 572 | - |
|
| 573 | - if ( $array_keys ) { |
|
| 574 | - foreach ( $array_keys as $array_key ) { |
|
| 575 | - $this->meta_data[ $array_key ]->value = null; |
|
| 576 | - } |
|
| 577 | - } |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Delete meta data. |
|
| 582 | - * |
|
| 583 | - * @since 1.0.19 |
|
| 584 | - * @param int $mid Meta ID. |
|
| 585 | - */ |
|
| 586 | - public function delete_meta_data_by_mid( $mid ) { |
|
| 587 | - $this->maybe_read_meta_data(); |
|
| 588 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
| 589 | - |
|
| 590 | - if ( $array_keys ) { |
|
| 591 | - foreach ( $array_keys as $array_key ) { |
|
| 592 | - $this->meta_data[ $array_key ]->value = null; |
|
| 593 | - } |
|
| 594 | - } |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * Read meta data if null. |
|
| 599 | - * |
|
| 600 | - * @since 1.0.19 |
|
| 601 | - */ |
|
| 602 | - protected function maybe_read_meta_data() { |
|
| 603 | - if ( is_null( $this->meta_data ) ) { |
|
| 604 | - $this->read_meta_data(); |
|
| 605 | - } |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - /** |
|
| 609 | - * Read Meta Data from the database. Ignore any internal properties. |
|
| 610 | - * Uses it's own caches because get_metadata does not provide meta_ids. |
|
| 611 | - * |
|
| 612 | - * @since 1.0.19 |
|
| 613 | - * @param bool $force_read True to force a new DB read (and update cache). |
|
| 614 | - */ |
|
| 615 | - public function read_meta_data( $force_read = false ) { |
|
| 616 | - |
|
| 617 | - // Reset meta data. |
|
| 618 | - $this->meta_data = array(); |
|
| 619 | - |
|
| 620 | - // Maybe abort early. |
|
| 621 | - if ( ! $this->get_id() || ! $this->data_store ) { |
|
| 622 | - return; |
|
| 623 | - } |
|
| 624 | - |
|
| 625 | - // Only read from cache if the cache key is set. |
|
| 626 | - $cache_key = null; |
|
| 627 | - if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
| 628 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 629 | - $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - // Should we force read? |
|
| 633 | - if ( empty( $raw_meta_data ) ) { |
|
| 634 | - $raw_meta_data = $this->data_store->read_meta( $this ); |
|
| 635 | - |
|
| 636 | - if ( ! empty( $cache_key ) ) { |
|
| 637 | - wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - // Set meta data. |
|
| 643 | - if ( is_array( $raw_meta_data ) ) { |
|
| 644 | - |
|
| 645 | - foreach ( $raw_meta_data as $meta ) { |
|
| 646 | - $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 647 | - array( |
|
| 648 | - 'id' => (int) $meta->meta_id, |
|
| 649 | - 'key' => $meta->meta_key, |
|
| 650 | - 'value' => maybe_unserialize( $meta->meta_value ), |
|
| 651 | - ) |
|
| 652 | - ); |
|
| 653 | - } |
|
| 654 | - |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - /** |
|
| 660 | - * Update Meta Data in the database. |
|
| 661 | - * |
|
| 662 | - * @since 1.0.19 |
|
| 663 | - */ |
|
| 664 | - public function save_meta_data() { |
|
| 665 | - if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
| 666 | - return; |
|
| 667 | - } |
|
| 668 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 669 | - if ( is_null( $meta->value ) ) { |
|
| 670 | - if ( ! empty( $meta->id ) ) { |
|
| 671 | - $this->data_store->delete_meta( $this, $meta ); |
|
| 672 | - unset( $this->meta_data[ $array_key ] ); |
|
| 673 | - } |
|
| 674 | - } elseif ( empty( $meta->id ) ) { |
|
| 675 | - $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
| 676 | - $meta->apply_changes(); |
|
| 677 | - } else { |
|
| 678 | - if ( $meta->get_changes() ) { |
|
| 679 | - $this->data_store->update_meta( $this, $meta ); |
|
| 680 | - $meta->apply_changes(); |
|
| 681 | - } |
|
| 682 | - } |
|
| 683 | - } |
|
| 684 | - if ( ! empty( $this->cache_group ) ) { |
|
| 685 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 686 | - wp_cache_delete( $cache_key, $this->cache_group ); |
|
| 687 | - } |
|
| 688 | - } |
|
| 689 | - |
|
| 690 | - /** |
|
| 691 | - * Set ID. |
|
| 692 | - * |
|
| 693 | - * @since 1.0.19 |
|
| 694 | - * @param int $id ID. |
|
| 695 | - */ |
|
| 696 | - public function set_id( $id ) { |
|
| 697 | - $this->id = absint( $id ); |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * Sets item status. |
|
| 702 | - * |
|
| 703 | - * @since 1.0.19 |
|
| 704 | - * @param string $status New status. |
|
| 705 | - * @return array details of change. |
|
| 706 | - */ |
|
| 707 | - public function set_status( $status ) { |
|
| 394 | + return $this->get_prop( $key ); |
|
| 395 | + |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + /** |
|
| 399 | + * Get Meta Data by Key. |
|
| 400 | + * |
|
| 401 | + * @since 1.0.19 |
|
| 402 | + * @param string $key Meta Key. |
|
| 403 | + * @param bool $single return first found meta with key, or all with $key. |
|
| 404 | + * @param string $context What the value is for. Valid values are view and edit. |
|
| 405 | + * @return mixed |
|
| 406 | + */ |
|
| 407 | + public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
| 408 | + |
|
| 409 | + // Check if this is an internal meta key. |
|
| 410 | + $_key = str_replace( '_wpinv', '', $key ); |
|
| 411 | + $_key = str_replace( 'wpinv', '', $_key ); |
|
| 412 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 413 | + $function = 'get_' . $_key; |
|
| 414 | + |
|
| 415 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 416 | + return $this->{$function}(); |
|
| 417 | + } |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + // Read the meta data if not yet read. |
|
| 421 | + $this->maybe_read_meta_data(); |
|
| 422 | + $meta_data = $this->get_meta_data(); |
|
| 423 | + $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
| 424 | + $value = $single ? '' : array(); |
|
| 425 | + |
|
| 426 | + if ( ! empty( $array_keys ) ) { |
|
| 427 | + // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
|
| 428 | + if ( $single ) { |
|
| 429 | + $value = $meta_data[ current( $array_keys ) ]->value; |
|
| 430 | + } else { |
|
| 431 | + $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
| 432 | + } |
|
| 433 | + } |
|
| 434 | + |
|
| 435 | + if ( 'view' === $context ) { |
|
| 436 | + $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + return $value; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * See if meta data exists, since get_meta always returns a '' or array(). |
|
| 444 | + * |
|
| 445 | + * @since 1.0.19 |
|
| 446 | + * @param string $key Meta Key. |
|
| 447 | + * @return boolean |
|
| 448 | + */ |
|
| 449 | + public function meta_exists( $key = '' ) { |
|
| 450 | + $this->maybe_read_meta_data(); |
|
| 451 | + $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
| 452 | + return in_array( $key, $array_keys, true ); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Set all meta data from array. |
|
| 457 | + * |
|
| 458 | + * @since 1.0.19 |
|
| 459 | + * @param array $data Key/Value pairs. |
|
| 460 | + */ |
|
| 461 | + public function set_meta_data( $data ) { |
|
| 462 | + if ( ! empty( $data ) && is_array( $data ) ) { |
|
| 463 | + $this->maybe_read_meta_data(); |
|
| 464 | + foreach ( $data as $meta ) { |
|
| 465 | + $meta = (array) $meta; |
|
| 466 | + if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
| 467 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 468 | + array( |
|
| 469 | + 'id' => $meta['id'], |
|
| 470 | + 'key' => $meta['key'], |
|
| 471 | + 'value' => $meta['value'], |
|
| 472 | + ) |
|
| 473 | + ); |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | + } |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + /** |
|
| 480 | + * Add meta data. |
|
| 481 | + * |
|
| 482 | + * @since 1.0.19 |
|
| 483 | + * |
|
| 484 | + * @param string $key Meta key. |
|
| 485 | + * @param string|array $value Meta value. |
|
| 486 | + * @param bool $unique Should this be a unique key?. |
|
| 487 | + */ |
|
| 488 | + public function add_meta_data( $key, $value, $unique = false ) { |
|
| 489 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 490 | + $function = 'set_' . $key; |
|
| 491 | + |
|
| 492 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 493 | + return $this->{$function}( $value ); |
|
| 494 | + } |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + $this->maybe_read_meta_data(); |
|
| 498 | + if ( $unique ) { |
|
| 499 | + $this->delete_meta_data( $key ); |
|
| 500 | + } |
|
| 501 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 502 | + array( |
|
| 503 | + 'key' => $key, |
|
| 504 | + 'value' => $value, |
|
| 505 | + ) |
|
| 506 | + ); |
|
| 507 | + |
|
| 508 | + $this->save(); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * Update meta data by key or ID, if provided. |
|
| 513 | + * |
|
| 514 | + * @since 1.0.19 |
|
| 515 | + * |
|
| 516 | + * @param string $key Meta key. |
|
| 517 | + * @param string|array $value Meta value. |
|
| 518 | + * @param int $meta_id Meta ID. |
|
| 519 | + */ |
|
| 520 | + public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
| 521 | + if ( $this->is_internal_meta_key( $key ) ) { |
|
| 522 | + $function = 'set_' . $key; |
|
| 523 | + |
|
| 524 | + if ( is_callable( array( $this, $function ) ) ) { |
|
| 525 | + return $this->{$function}( $value ); |
|
| 526 | + } |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + $this->maybe_read_meta_data(); |
|
| 530 | + |
|
| 531 | + $array_key = false; |
|
| 532 | + |
|
| 533 | + if ( $meta_id ) { |
|
| 534 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
| 535 | + $array_key = $array_keys ? current( $array_keys ) : false; |
|
| 536 | + } else { |
|
| 537 | + // Find matches by key. |
|
| 538 | + $matches = array(); |
|
| 539 | + foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
| 540 | + if ( $meta->key === $key ) { |
|
| 541 | + $matches[] = $meta_data_array_key; |
|
| 542 | + } |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + if ( ! empty( $matches ) ) { |
|
| 546 | + // Set matches to null so only one key gets the new value. |
|
| 547 | + foreach ( $matches as $meta_data_array_key ) { |
|
| 548 | + $this->meta_data[ $meta_data_array_key ]->value = null; |
|
| 549 | + } |
|
| 550 | + $array_key = current( $matches ); |
|
| 551 | + } |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + if ( false !== $array_key ) { |
|
| 555 | + $meta = $this->meta_data[ $array_key ]; |
|
| 556 | + $meta->key = $key; |
|
| 557 | + $meta->value = $value; |
|
| 558 | + } else { |
|
| 559 | + $this->add_meta_data( $key, $value, true ); |
|
| 560 | + } |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * Delete meta data. |
|
| 565 | + * |
|
| 566 | + * @since 1.0.19 |
|
| 567 | + * @param string $key Meta key. |
|
| 568 | + */ |
|
| 569 | + public function delete_meta_data( $key ) { |
|
| 570 | + $this->maybe_read_meta_data(); |
|
| 571 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
| 572 | + |
|
| 573 | + if ( $array_keys ) { |
|
| 574 | + foreach ( $array_keys as $array_key ) { |
|
| 575 | + $this->meta_data[ $array_key ]->value = null; |
|
| 576 | + } |
|
| 577 | + } |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Delete meta data. |
|
| 582 | + * |
|
| 583 | + * @since 1.0.19 |
|
| 584 | + * @param int $mid Meta ID. |
|
| 585 | + */ |
|
| 586 | + public function delete_meta_data_by_mid( $mid ) { |
|
| 587 | + $this->maybe_read_meta_data(); |
|
| 588 | + $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
| 589 | + |
|
| 590 | + if ( $array_keys ) { |
|
| 591 | + foreach ( $array_keys as $array_key ) { |
|
| 592 | + $this->meta_data[ $array_key ]->value = null; |
|
| 593 | + } |
|
| 594 | + } |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * Read meta data if null. |
|
| 599 | + * |
|
| 600 | + * @since 1.0.19 |
|
| 601 | + */ |
|
| 602 | + protected function maybe_read_meta_data() { |
|
| 603 | + if ( is_null( $this->meta_data ) ) { |
|
| 604 | + $this->read_meta_data(); |
|
| 605 | + } |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + /** |
|
| 609 | + * Read Meta Data from the database. Ignore any internal properties. |
|
| 610 | + * Uses it's own caches because get_metadata does not provide meta_ids. |
|
| 611 | + * |
|
| 612 | + * @since 1.0.19 |
|
| 613 | + * @param bool $force_read True to force a new DB read (and update cache). |
|
| 614 | + */ |
|
| 615 | + public function read_meta_data( $force_read = false ) { |
|
| 616 | + |
|
| 617 | + // Reset meta data. |
|
| 618 | + $this->meta_data = array(); |
|
| 619 | + |
|
| 620 | + // Maybe abort early. |
|
| 621 | + if ( ! $this->get_id() || ! $this->data_store ) { |
|
| 622 | + return; |
|
| 623 | + } |
|
| 624 | + |
|
| 625 | + // Only read from cache if the cache key is set. |
|
| 626 | + $cache_key = null; |
|
| 627 | + if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
| 628 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 629 | + $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + // Should we force read? |
|
| 633 | + if ( empty( $raw_meta_data ) ) { |
|
| 634 | + $raw_meta_data = $this->data_store->read_meta( $this ); |
|
| 635 | + |
|
| 636 | + if ( ! empty( $cache_key ) ) { |
|
| 637 | + wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + // Set meta data. |
|
| 643 | + if ( is_array( $raw_meta_data ) ) { |
|
| 644 | + |
|
| 645 | + foreach ( $raw_meta_data as $meta ) { |
|
| 646 | + $this->meta_data[] = new GetPaid_Meta_Data( |
|
| 647 | + array( |
|
| 648 | + 'id' => (int) $meta->meta_id, |
|
| 649 | + 'key' => $meta->meta_key, |
|
| 650 | + 'value' => maybe_unserialize( $meta->meta_value ), |
|
| 651 | + ) |
|
| 652 | + ); |
|
| 653 | + } |
|
| 654 | + |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * Update Meta Data in the database. |
|
| 661 | + * |
|
| 662 | + * @since 1.0.19 |
|
| 663 | + */ |
|
| 664 | + public function save_meta_data() { |
|
| 665 | + if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
| 666 | + return; |
|
| 667 | + } |
|
| 668 | + foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 669 | + if ( is_null( $meta->value ) ) { |
|
| 670 | + if ( ! empty( $meta->id ) ) { |
|
| 671 | + $this->data_store->delete_meta( $this, $meta ); |
|
| 672 | + unset( $this->meta_data[ $array_key ] ); |
|
| 673 | + } |
|
| 674 | + } elseif ( empty( $meta->id ) ) { |
|
| 675 | + $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
| 676 | + $meta->apply_changes(); |
|
| 677 | + } else { |
|
| 678 | + if ( $meta->get_changes() ) { |
|
| 679 | + $this->data_store->update_meta( $this, $meta ); |
|
| 680 | + $meta->apply_changes(); |
|
| 681 | + } |
|
| 682 | + } |
|
| 683 | + } |
|
| 684 | + if ( ! empty( $this->cache_group ) ) { |
|
| 685 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 686 | + wp_cache_delete( $cache_key, $this->cache_group ); |
|
| 687 | + } |
|
| 688 | + } |
|
| 689 | + |
|
| 690 | + /** |
|
| 691 | + * Set ID. |
|
| 692 | + * |
|
| 693 | + * @since 1.0.19 |
|
| 694 | + * @param int $id ID. |
|
| 695 | + */ |
|
| 696 | + public function set_id( $id ) { |
|
| 697 | + $this->id = absint( $id ); |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * Sets item status. |
|
| 702 | + * |
|
| 703 | + * @since 1.0.19 |
|
| 704 | + * @param string $status New status. |
|
| 705 | + * @return array details of change. |
|
| 706 | + */ |
|
| 707 | + public function set_status( $status ) { |
|
| 708 | 708 | $old_status = $this->get_status(); |
| 709 | 709 | |
| 710 | - $this->set_prop( 'status', $status ); |
|
| 711 | - |
|
| 712 | - return array( |
|
| 713 | - 'from' => $old_status, |
|
| 714 | - 'to' => $status, |
|
| 715 | - ); |
|
| 716 | - } |
|
| 717 | - |
|
| 718 | - /** |
|
| 719 | - * Set all props to default values. |
|
| 720 | - * |
|
| 721 | - * @since 1.0.19 |
|
| 722 | - */ |
|
| 723 | - public function set_defaults() { |
|
| 724 | - $this->data = $this->default_data; |
|
| 725 | - $this->changes = array(); |
|
| 726 | - $this->set_object_read( false ); |
|
| 727 | - } |
|
| 728 | - |
|
| 729 | - /** |
|
| 730 | - * Set object read property. |
|
| 731 | - * |
|
| 732 | - * @since 1.0.19 |
|
| 733 | - * @param boolean $read Should read?. |
|
| 734 | - */ |
|
| 735 | - public function set_object_read( $read = true ) { |
|
| 736 | - $this->object_read = (bool) $read; |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * Get object read property. |
|
| 741 | - * |
|
| 742 | - * @since 1.0.19 |
|
| 743 | - * @return boolean |
|
| 744 | - */ |
|
| 745 | - public function get_object_read() { |
|
| 746 | - return (bool) $this->object_read; |
|
| 747 | - } |
|
| 748 | - |
|
| 749 | - /** |
|
| 750 | - * Set a collection of props in one go, collect any errors, and return the result. |
|
| 751 | - * Only sets using public methods. |
|
| 752 | - * |
|
| 753 | - * @since 1.0.19 |
|
| 754 | - * |
|
| 755 | - * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
| 756 | - * @param string $context In what context to run this. |
|
| 757 | - * |
|
| 758 | - * @return bool|WP_Error |
|
| 759 | - */ |
|
| 760 | - public function set_props( $props, $context = 'set' ) { |
|
| 761 | - $errors = false; |
|
| 762 | - |
|
| 763 | - foreach ( $props as $prop => $value ) { |
|
| 764 | - try { |
|
| 765 | - /** |
|
| 766 | - * Checks if the prop being set is allowed, and the value is not null. |
|
| 767 | - */ |
|
| 768 | - if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
| 769 | - continue; |
|
| 770 | - } |
|
| 771 | - $setter = "set_$prop"; |
|
| 772 | - |
|
| 773 | - if ( is_callable( array( $this, $setter ) ) ) { |
|
| 774 | - $this->{$setter}( $value ); |
|
| 775 | - } |
|
| 776 | - } catch ( Exception $e ) { |
|
| 777 | - if ( ! $errors ) { |
|
| 778 | - $errors = new WP_Error(); |
|
| 779 | - } |
|
| 780 | - $errors->add( $e->getCode(), $e->getMessage() ); |
|
| 781 | - $this->last_error = $e->getMessage(); |
|
| 782 | - } |
|
| 783 | - } |
|
| 784 | - |
|
| 785 | - return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
| 786 | - } |
|
| 787 | - |
|
| 788 | - /** |
|
| 789 | - * Sets a prop for a setter method. |
|
| 790 | - * |
|
| 791 | - * This stores changes in a special array so we can track what needs saving |
|
| 792 | - * the the DB later. |
|
| 793 | - * |
|
| 794 | - * @since 1.0.19 |
|
| 795 | - * @param string $prop Name of prop to set. |
|
| 796 | - * @param mixed $value Value of the prop. |
|
| 797 | - */ |
|
| 798 | - protected function set_prop( $prop, $value ) { |
|
| 799 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 800 | - if ( true === $this->object_read ) { |
|
| 801 | - if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
| 802 | - $this->changes[ $prop ] = $value; |
|
| 803 | - } |
|
| 804 | - } else { |
|
| 805 | - $this->data[ $prop ] = $value; |
|
| 806 | - } |
|
| 807 | - } |
|
| 808 | - } |
|
| 809 | - |
|
| 810 | - /** |
|
| 811 | - * Return data changes only. |
|
| 812 | - * |
|
| 813 | - * @since 1.0.19 |
|
| 814 | - * @return array |
|
| 815 | - */ |
|
| 816 | - public function get_changes() { |
|
| 817 | - return $this->changes; |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - /** |
|
| 821 | - * Merge changes with data and clear. |
|
| 822 | - * |
|
| 823 | - * @since 1.0.19 |
|
| 824 | - */ |
|
| 825 | - public function apply_changes() { |
|
| 826 | - $this->data = array_replace( $this->data, $this->changes ); |
|
| 827 | - $this->changes = array(); |
|
| 828 | - } |
|
| 829 | - |
|
| 830 | - /** |
|
| 831 | - * Prefix for action and filter hooks on data. |
|
| 832 | - * |
|
| 833 | - * @since 1.0.19 |
|
| 834 | - * @return string |
|
| 835 | - */ |
|
| 836 | - protected function get_hook_prefix() { |
|
| 837 | - return 'wpinv_get_' . $this->object_type . '_'; |
|
| 838 | - } |
|
| 839 | - |
|
| 840 | - /** |
|
| 841 | - * Gets a prop for a getter method. |
|
| 842 | - * |
|
| 843 | - * Gets the value from either current pending changes, or the data itself. |
|
| 844 | - * Context controls what happens to the value before it's returned. |
|
| 845 | - * |
|
| 846 | - * @since 1.0.19 |
|
| 847 | - * @param string $prop Name of prop to get. |
|
| 848 | - * @param string $context What the value is for. Valid values are view and edit. |
|
| 849 | - * @return mixed |
|
| 850 | - */ |
|
| 851 | - protected function get_prop( $prop, $context = 'view' ) { |
|
| 852 | - $value = null; |
|
| 853 | - |
|
| 854 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 855 | - $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
| 856 | - |
|
| 857 | - if ( 'view' === $context ) { |
|
| 858 | - $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
| 859 | - } |
|
| 860 | - } |
|
| 861 | - |
|
| 862 | - return $value; |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - /** |
|
| 866 | - * Sets a date prop whilst handling formatting and datetime objects. |
|
| 867 | - * |
|
| 868 | - * @since 1.0.19 |
|
| 869 | - * @param string $prop Name of prop to set. |
|
| 870 | - * @param string|integer $value Value of the prop. |
|
| 871 | - */ |
|
| 872 | - protected function set_date_prop( $prop, $value ) { |
|
| 873 | - |
|
| 874 | - if ( empty( $value ) ) { |
|
| 875 | - $this->set_prop( $prop, null ); |
|
| 876 | - return; |
|
| 877 | - } |
|
| 878 | - $this->set_prop( $prop, $value ); |
|
| 879 | - |
|
| 880 | - } |
|
| 881 | - |
|
| 882 | - /** |
|
| 883 | - * When invalid data is found, throw an exception unless reading from the DB. |
|
| 884 | - * |
|
| 885 | - * @since 1.0.19 |
|
| 886 | - * @param string $code Error code. |
|
| 887 | - * @param string $message Error message. |
|
| 888 | - */ |
|
| 889 | - protected function error( $code, $message ) { |
|
| 890 | - $this->last_error = $message; |
|
| 891 | - } |
|
| 892 | - |
|
| 893 | - /** |
|
| 894 | - * Checks if the object is saved in the database |
|
| 895 | - * |
|
| 896 | - * @since 1.0.19 |
|
| 897 | - * @return bool |
|
| 898 | - */ |
|
| 899 | - public function exists() { |
|
| 900 | - $id = $this->get_id(); |
|
| 901 | - return ! empty( $id ); |
|
| 902 | - } |
|
| 710 | + $this->set_prop( 'status', $status ); |
|
| 711 | + |
|
| 712 | + return array( |
|
| 713 | + 'from' => $old_status, |
|
| 714 | + 'to' => $status, |
|
| 715 | + ); |
|
| 716 | + } |
|
| 717 | + |
|
| 718 | + /** |
|
| 719 | + * Set all props to default values. |
|
| 720 | + * |
|
| 721 | + * @since 1.0.19 |
|
| 722 | + */ |
|
| 723 | + public function set_defaults() { |
|
| 724 | + $this->data = $this->default_data; |
|
| 725 | + $this->changes = array(); |
|
| 726 | + $this->set_object_read( false ); |
|
| 727 | + } |
|
| 728 | + |
|
| 729 | + /** |
|
| 730 | + * Set object read property. |
|
| 731 | + * |
|
| 732 | + * @since 1.0.19 |
|
| 733 | + * @param boolean $read Should read?. |
|
| 734 | + */ |
|
| 735 | + public function set_object_read( $read = true ) { |
|
| 736 | + $this->object_read = (bool) $read; |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * Get object read property. |
|
| 741 | + * |
|
| 742 | + * @since 1.0.19 |
|
| 743 | + * @return boolean |
|
| 744 | + */ |
|
| 745 | + public function get_object_read() { |
|
| 746 | + return (bool) $this->object_read; |
|
| 747 | + } |
|
| 748 | + |
|
| 749 | + /** |
|
| 750 | + * Set a collection of props in one go, collect any errors, and return the result. |
|
| 751 | + * Only sets using public methods. |
|
| 752 | + * |
|
| 753 | + * @since 1.0.19 |
|
| 754 | + * |
|
| 755 | + * @param array $props Key value pairs to set. Key is the prop and should map to a setter function name. |
|
| 756 | + * @param string $context In what context to run this. |
|
| 757 | + * |
|
| 758 | + * @return bool|WP_Error |
|
| 759 | + */ |
|
| 760 | + public function set_props( $props, $context = 'set' ) { |
|
| 761 | + $errors = false; |
|
| 762 | + |
|
| 763 | + foreach ( $props as $prop => $value ) { |
|
| 764 | + try { |
|
| 765 | + /** |
|
| 766 | + * Checks if the prop being set is allowed, and the value is not null. |
|
| 767 | + */ |
|
| 768 | + if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
| 769 | + continue; |
|
| 770 | + } |
|
| 771 | + $setter = "set_$prop"; |
|
| 772 | + |
|
| 773 | + if ( is_callable( array( $this, $setter ) ) ) { |
|
| 774 | + $this->{$setter}( $value ); |
|
| 775 | + } |
|
| 776 | + } catch ( Exception $e ) { |
|
| 777 | + if ( ! $errors ) { |
|
| 778 | + $errors = new WP_Error(); |
|
| 779 | + } |
|
| 780 | + $errors->add( $e->getCode(), $e->getMessage() ); |
|
| 781 | + $this->last_error = $e->getMessage(); |
|
| 782 | + } |
|
| 783 | + } |
|
| 784 | + |
|
| 785 | + return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
| 786 | + } |
|
| 787 | + |
|
| 788 | + /** |
|
| 789 | + * Sets a prop for a setter method. |
|
| 790 | + * |
|
| 791 | + * This stores changes in a special array so we can track what needs saving |
|
| 792 | + * the the DB later. |
|
| 793 | + * |
|
| 794 | + * @since 1.0.19 |
|
| 795 | + * @param string $prop Name of prop to set. |
|
| 796 | + * @param mixed $value Value of the prop. |
|
| 797 | + */ |
|
| 798 | + protected function set_prop( $prop, $value ) { |
|
| 799 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
| 800 | + if ( true === $this->object_read ) { |
|
| 801 | + if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
| 802 | + $this->changes[ $prop ] = $value; |
|
| 803 | + } |
|
| 804 | + } else { |
|
| 805 | + $this->data[ $prop ] = $value; |
|
| 806 | + } |
|
| 807 | + } |
|
| 808 | + } |
|
| 809 | + |
|
| 810 | + /** |
|
| 811 | + * Return data changes only. |
|
| 812 | + * |
|
| 813 | + * @since 1.0.19 |
|
| 814 | + * @return array |
|
| 815 | + */ |
|
| 816 | + public function get_changes() { |
|
| 817 | + return $this->changes; |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + /** |
|
| 821 | + * Merge changes with data and clear. |
|
| 822 | + * |
|
| 823 | + * @since 1.0.19 |
|
| 824 | + */ |
|
| 825 | + public function apply_changes() { |
|
| 826 | + $this->data = array_replace( $this->data, $this->changes ); |
|
| 827 | + $this->changes = array(); |
|
| 828 | + } |
|
| 829 | + |
|
| 830 | + /** |
|
| 831 | + * Prefix for action and filter hooks on data. |
|
| 832 | + * |
|
| 833 | + * @since 1.0.19 |
|
| 834 | + * @return string |
|
| 835 | + */ |
|
| 836 | + protected function get_hook_prefix() { |
|
| 837 | + return 'wpinv_get_' . $this->object_type . '_'; |
|
| 838 | + } |
|
| 839 | + |
|
| 840 | + /** |
|
| 841 | + * Gets a prop for a getter method. |
|
| 842 | + * |
|
| 843 | + * Gets the value from either current pending changes, or the data itself. |
|
| 844 | + * Context controls what happens to the value before it's returned. |
|
| 845 | + * |
|
| 846 | + * @since 1.0.19 |
|
| 847 | + * @param string $prop Name of prop to get. |
|
| 848 | + * @param string $context What the value is for. Valid values are view and edit. |
|
| 849 | + * @return mixed |
|
| 850 | + */ |
|
| 851 | + protected function get_prop( $prop, $context = 'view' ) { |
|
| 852 | + $value = null; |
|
| 853 | + |
|
| 854 | + if ( array_key_exists( $prop, $this->data ) ) { |
|
| 855 | + $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
| 856 | + |
|
| 857 | + if ( 'view' === $context ) { |
|
| 858 | + $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
| 859 | + } |
|
| 860 | + } |
|
| 861 | + |
|
| 862 | + return $value; |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + /** |
|
| 866 | + * Sets a date prop whilst handling formatting and datetime objects. |
|
| 867 | + * |
|
| 868 | + * @since 1.0.19 |
|
| 869 | + * @param string $prop Name of prop to set. |
|
| 870 | + * @param string|integer $value Value of the prop. |
|
| 871 | + */ |
|
| 872 | + protected function set_date_prop( $prop, $value ) { |
|
| 873 | + |
|
| 874 | + if ( empty( $value ) ) { |
|
| 875 | + $this->set_prop( $prop, null ); |
|
| 876 | + return; |
|
| 877 | + } |
|
| 878 | + $this->set_prop( $prop, $value ); |
|
| 879 | + |
|
| 880 | + } |
|
| 881 | + |
|
| 882 | + /** |
|
| 883 | + * When invalid data is found, throw an exception unless reading from the DB. |
|
| 884 | + * |
|
| 885 | + * @since 1.0.19 |
|
| 886 | + * @param string $code Error code. |
|
| 887 | + * @param string $message Error message. |
|
| 888 | + */ |
|
| 889 | + protected function error( $code, $message ) { |
|
| 890 | + $this->last_error = $message; |
|
| 891 | + } |
|
| 892 | + |
|
| 893 | + /** |
|
| 894 | + * Checks if the object is saved in the database |
|
| 895 | + * |
|
| 896 | + * @since 1.0.19 |
|
| 897 | + * @return bool |
|
| 898 | + */ |
|
| 899 | + public function exists() { |
|
| 900 | + $id = $this->get_id(); |
|
| 901 | + return ! empty( $id ); |
|
| 902 | + } |
|
| 903 | 903 | |
| 904 | 904 | } |
@@ -8,7 +8,7 @@ discard block |
||
| 8 | 8 | * |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 11 | +if (!defined('ABSPATH')) { |
|
| 12 | 12 | exit; |
| 13 | 13 | } |
| 14 | 14 | |
@@ -117,8 +117,8 @@ discard block |
||
| 117 | 117 | * |
| 118 | 118 | * @param int|object|array|string $read ID to load from the DB (optional) or already queried data. |
| 119 | 119 | */ |
| 120 | - public function __construct( $read = 0 ) { |
|
| 121 | - $this->data = array_merge( $this->data, $this->extra_data ); |
|
| 120 | + public function __construct($read = 0) { |
|
| 121 | + $this->data = array_merge($this->data, $this->extra_data); |
|
| 122 | 122 | $this->default_data = $this->data; |
| 123 | 123 | } |
| 124 | 124 | |
@@ -128,7 +128,7 @@ discard block |
||
| 128 | 128 | * @return array |
| 129 | 129 | */ |
| 130 | 130 | public function __sleep() { |
| 131 | - return array( 'id' ); |
|
| 131 | + return array('id'); |
|
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /** |
@@ -137,10 +137,10 @@ discard block |
||
| 137 | 137 | * If the object no longer exists, remove the ID. |
| 138 | 138 | */ |
| 139 | 139 | public function __wakeup() { |
| 140 | - $this->__construct( absint( $this->id ) ); |
|
| 140 | + $this->__construct(absint($this->id)); |
|
| 141 | 141 | |
| 142 | - if ( ! empty( $this->last_error ) ) { |
|
| 143 | - $this->set_id( 0 ); |
|
| 142 | + if (!empty($this->last_error)) { |
|
| 143 | + $this->set_id(0); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | } |
@@ -152,11 +152,11 @@ discard block |
||
| 152 | 152 | */ |
| 153 | 153 | public function __clone() { |
| 154 | 154 | $this->maybe_read_meta_data(); |
| 155 | - if ( ! empty( $this->meta_data ) ) { |
|
| 156 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 157 | - $this->meta_data[ $array_key ] = clone $meta; |
|
| 158 | - if ( ! empty( $meta->id ) ) { |
|
| 159 | - $this->meta_data[ $array_key ]->id = null; |
|
| 155 | + if (!empty($this->meta_data)) { |
|
| 156 | + foreach ($this->meta_data as $array_key => $meta) { |
|
| 157 | + $this->meta_data[$array_key] = clone $meta; |
|
| 158 | + if (!empty($meta->id)) { |
|
| 159 | + $this->meta_data[$array_key]->id = null; |
|
| 160 | 160 | } |
| 161 | 161 | } |
| 162 | 162 | } |
@@ -199,8 +199,8 @@ discard block |
||
| 199 | 199 | * @param string $context View or edit context. |
| 200 | 200 | * @return string |
| 201 | 201 | */ |
| 202 | - public function get_status( $context = 'view' ) { |
|
| 203 | - return $this->get_prop( 'status', $context ); |
|
| 202 | + public function get_status($context = 'view') { |
|
| 203 | + return $this->get_prop('status', $context); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | /** |
@@ -210,10 +210,10 @@ discard block |
||
| 210 | 210 | * @param bool $force_delete Should the data be deleted permanently. |
| 211 | 211 | * @return bool result |
| 212 | 212 | */ |
| 213 | - public function delete( $force_delete = false ) { |
|
| 214 | - if ( $this->data_store && $this->exists() ) { |
|
| 215 | - $this->data_store->delete( $this, array( 'force_delete' => $force_delete ) ); |
|
| 216 | - $this->set_id( 0 ); |
|
| 213 | + public function delete($force_delete = false) { |
|
| 214 | + if ($this->data_store && $this->exists()) { |
|
| 215 | + $this->data_store->delete($this, array('force_delete' => $force_delete)); |
|
| 216 | + $this->set_id(0); |
|
| 217 | 217 | return true; |
| 218 | 218 | } |
| 219 | 219 | return false; |
@@ -226,7 +226,7 @@ discard block |
||
| 226 | 226 | * @return int |
| 227 | 227 | */ |
| 228 | 228 | public function save() { |
| 229 | - if ( ! $this->data_store ) { |
|
| 229 | + if (!$this->data_store) { |
|
| 230 | 230 | return $this->get_id(); |
| 231 | 231 | } |
| 232 | 232 | |
@@ -236,12 +236,12 @@ discard block |
||
| 236 | 236 | * @param GetPaid_Data $this The object being saved. |
| 237 | 237 | * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
| 238 | 238 | */ |
| 239 | - do_action( 'getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 239 | + do_action('getpaid_before_' . $this->object_type . '_object_save', $this, $this->data_store); |
|
| 240 | 240 | |
| 241 | - if ( $this->get_id() ) { |
|
| 242 | - $this->data_store->update( $this ); |
|
| 241 | + if ($this->get_id()) { |
|
| 242 | + $this->data_store->update($this); |
|
| 243 | 243 | } else { |
| 244 | - $this->data_store->create( $this ); |
|
| 244 | + $this->data_store->create($this); |
|
| 245 | 245 | } |
| 246 | 246 | |
| 247 | 247 | /** |
@@ -250,7 +250,7 @@ discard block |
||
| 250 | 250 | * @param GetPaid_Data $this The object being saved. |
| 251 | 251 | * @param GetPaid_Data_Store_WP $data_store The data store persisting the data. |
| 252 | 252 | */ |
| 253 | - do_action( 'getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store ); |
|
| 253 | + do_action('getpaid_after_' . $this->object_type . '_object_save', $this, $this->data_store); |
|
| 254 | 254 | |
| 255 | 255 | return $this->get_id(); |
| 256 | 256 | } |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | * @return string Data in JSON format. |
| 263 | 263 | */ |
| 264 | 264 | public function __toString() { |
| 265 | - return wp_json_encode( $this->get_data() ); |
|
| 265 | + return wp_json_encode($this->get_data()); |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | /** |
@@ -272,7 +272,7 @@ discard block |
||
| 272 | 272 | * @return array |
| 273 | 273 | */ |
| 274 | 274 | public function get_data() { |
| 275 | - return array_merge( array( 'id' => $this->get_id() ), $this->data, array( 'meta_data' => $this->get_meta_data() ) ); |
|
| 275 | + return array_merge(array('id' => $this->get_id()), $this->data, array('meta_data' => $this->get_meta_data())); |
|
| 276 | 276 | } |
| 277 | 277 | |
| 278 | 278 | /** |
@@ -282,7 +282,7 @@ discard block |
||
| 282 | 282 | * @return array |
| 283 | 283 | */ |
| 284 | 284 | public function get_data_keys() { |
| 285 | - return array_keys( $this->data ); |
|
| 285 | + return array_keys($this->data); |
|
| 286 | 286 | } |
| 287 | 287 | |
| 288 | 288 | /** |
@@ -292,7 +292,7 @@ discard block |
||
| 292 | 292 | * @return array |
| 293 | 293 | */ |
| 294 | 294 | public function get_extra_data_keys() { |
| 295 | - return array_keys( $this->extra_data ); |
|
| 295 | + return array_keys($this->extra_data); |
|
| 296 | 296 | } |
| 297 | 297 | |
| 298 | 298 | /** |
@@ -302,8 +302,8 @@ discard block |
||
| 302 | 302 | * @param mixed $meta Meta value to check. |
| 303 | 303 | * @return bool |
| 304 | 304 | */ |
| 305 | - protected function filter_null_meta( $meta ) { |
|
| 306 | - return ! is_null( $meta->value ); |
|
| 305 | + protected function filter_null_meta($meta) { |
|
| 306 | + return !is_null($meta->value); |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | /** |
@@ -314,7 +314,7 @@ discard block |
||
| 314 | 314 | */ |
| 315 | 315 | public function get_meta_data() { |
| 316 | 316 | $this->maybe_read_meta_data(); |
| 317 | - return array_values( array_filter( $this->meta_data, array( $this, 'filter_null_meta' ) ) ); |
|
| 317 | + return array_values(array_filter($this->meta_data, array($this, 'filter_null_meta'))); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /** |
@@ -324,21 +324,21 @@ discard block |
||
| 324 | 324 | * @param string $key Key to check. |
| 325 | 325 | * @return bool true if it's an internal key, false otherwise |
| 326 | 326 | */ |
| 327 | - protected function is_internal_meta_key( $key ) { |
|
| 328 | - $internal_meta_key = ! empty( $key ) && $this->data_store && in_array( $key, $this->data_store->get_internal_meta_keys(), true ); |
|
| 327 | + protected function is_internal_meta_key($key) { |
|
| 328 | + $internal_meta_key = !empty($key) && $this->data_store && in_array($key, $this->data_store->get_internal_meta_keys(), true); |
|
| 329 | 329 | |
| 330 | - if ( ! $internal_meta_key ) { |
|
| 330 | + if (!$internal_meta_key) { |
|
| 331 | 331 | return false; |
| 332 | 332 | } |
| 333 | 333 | |
| 334 | - $has_setter_or_getter = is_callable( array( $this, 'set_' . $key ) ) || is_callable( array( $this, 'get_' . $key ) ); |
|
| 334 | + $has_setter_or_getter = is_callable(array($this, 'set_' . $key)) || is_callable(array($this, 'get_' . $key)); |
|
| 335 | 335 | |
| 336 | - if ( ! $has_setter_or_getter ) { |
|
| 336 | + if (!$has_setter_or_getter) { |
|
| 337 | 337 | return false; |
| 338 | 338 | } |
| 339 | 339 | |
| 340 | 340 | /* translators: %s: $key Key to check */ |
| 341 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 341 | + getpaid_doing_it_wrong(__FUNCTION__, sprintf(__('Generic add/update/get meta methods should not be used for internal meta data, including "%s". Use getters and setters.', 'invoicing'), $key), '1.0.19'); |
|
| 342 | 342 | |
| 343 | 343 | return true; |
| 344 | 344 | } |
@@ -352,20 +352,20 @@ discard block |
||
| 352 | 352 | * @access public |
| 353 | 353 | * |
| 354 | 354 | */ |
| 355 | - public function __set( $key, $value ) { |
|
| 355 | + public function __set($key, $value) { |
|
| 356 | 356 | |
| 357 | - if ( 'id' == strtolower( $key ) ) { |
|
| 358 | - return $this->set_id( $value ); |
|
| 357 | + if ('id' == strtolower($key)) { |
|
| 358 | + return $this->set_id($value); |
|
| 359 | 359 | } |
| 360 | 360 | |
| 361 | - if ( method_exists( $this, "set_$key") ) { |
|
| 361 | + if (method_exists($this, "set_$key")) { |
|
| 362 | 362 | |
| 363 | 363 | /* translators: %s: $key Key to set */ |
| 364 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 364 | + getpaid_doing_it_wrong(__FUNCTION__, sprintf(__('Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing'), $key), '1.0.19'); |
|
| 365 | 365 | |
| 366 | - call_user_func( array( $this, "set_$key" ), $value ); |
|
| 366 | + call_user_func(array($this, "set_$key"), $value); |
|
| 367 | 367 | } else { |
| 368 | - $this->set_prop( $key, $value ); |
|
| 368 | + $this->set_prop($key, $value); |
|
| 369 | 369 | } |
| 370 | 370 | |
| 371 | 371 | } |
@@ -373,25 +373,25 @@ discard block |
||
| 373 | 373 | /** |
| 374 | 374 | * Margic method for retrieving a property. |
| 375 | 375 | */ |
| 376 | - public function __get( $key ) { |
|
| 376 | + public function __get($key) { |
|
| 377 | 377 | |
| 378 | 378 | // Check if we have a helper method for that. |
| 379 | - if ( method_exists( $this, 'get_' . $key ) ) { |
|
| 379 | + if (method_exists($this, 'get_' . $key)) { |
|
| 380 | 380 | |
| 381 | - if ( 'post_type' != $key ) { |
|
| 381 | + if ('post_type' != $key) { |
|
| 382 | 382 | /* translators: %s: $key Key to set */ |
| 383 | - getpaid_doing_it_wrong( __FUNCTION__, sprintf( __( 'Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing' ), $key ), '1.0.19' ); |
|
| 383 | + getpaid_doing_it_wrong(__FUNCTION__, sprintf(__('Object data such as "%s" should not be accessed directly. Use getters and setters.', 'invoicing'), $key), '1.0.19'); |
|
| 384 | 384 | } |
| 385 | 385 | |
| 386 | - return call_user_func( array( $this, 'get_' . $key ) ); |
|
| 386 | + return call_user_func(array($this, 'get_' . $key)); |
|
| 387 | 387 | } |
| 388 | 388 | |
| 389 | 389 | // Check if the key is in the associated $post object. |
| 390 | - if ( ! empty( $this->post ) && isset( $this->post->$key ) ) { |
|
| 390 | + if (!empty($this->post) && isset($this->post->$key)) { |
|
| 391 | 391 | return $this->post->$key; |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | - return $this->get_prop( $key ); |
|
| 394 | + return $this->get_prop($key); |
|
| 395 | 395 | |
| 396 | 396 | } |
| 397 | 397 | |
@@ -404,15 +404,15 @@ discard block |
||
| 404 | 404 | * @param string $context What the value is for. Valid values are view and edit. |
| 405 | 405 | * @return mixed |
| 406 | 406 | */ |
| 407 | - public function get_meta( $key = '', $single = true, $context = 'view' ) { |
|
| 407 | + public function get_meta($key = '', $single = true, $context = 'view') { |
|
| 408 | 408 | |
| 409 | 409 | // Check if this is an internal meta key. |
| 410 | - $_key = str_replace( '_wpinv', '', $key ); |
|
| 411 | - $_key = str_replace( 'wpinv', '', $_key ); |
|
| 412 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 410 | + $_key = str_replace('_wpinv', '', $key); |
|
| 411 | + $_key = str_replace('wpinv', '', $_key); |
|
| 412 | + if ($this->is_internal_meta_key($key)) { |
|
| 413 | 413 | $function = 'get_' . $_key; |
| 414 | 414 | |
| 415 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 415 | + if (is_callable(array($this, $function))) { |
|
| 416 | 416 | return $this->{$function}(); |
| 417 | 417 | } |
| 418 | 418 | } |
@@ -420,20 +420,20 @@ discard block |
||
| 420 | 420 | // Read the meta data if not yet read. |
| 421 | 421 | $this->maybe_read_meta_data(); |
| 422 | 422 | $meta_data = $this->get_meta_data(); |
| 423 | - $array_keys = array_keys( wp_list_pluck( $meta_data, 'key' ), $key, true ); |
|
| 423 | + $array_keys = array_keys(wp_list_pluck($meta_data, 'key'), $key, true); |
|
| 424 | 424 | $value = $single ? '' : array(); |
| 425 | 425 | |
| 426 | - if ( ! empty( $array_keys ) ) { |
|
| 426 | + if (!empty($array_keys)) { |
|
| 427 | 427 | // We don't use the $this->meta_data property directly here because we don't want meta with a null value (i.e. meta which has been deleted via $this->delete_meta_data()). |
| 428 | - if ( $single ) { |
|
| 429 | - $value = $meta_data[ current( $array_keys ) ]->value; |
|
| 428 | + if ($single) { |
|
| 429 | + $value = $meta_data[current($array_keys)]->value; |
|
| 430 | 430 | } else { |
| 431 | - $value = array_intersect_key( $meta_data, array_flip( $array_keys ) ); |
|
| 431 | + $value = array_intersect_key($meta_data, array_flip($array_keys)); |
|
| 432 | 432 | } |
| 433 | 433 | } |
| 434 | 434 | |
| 435 | - if ( 'view' === $context ) { |
|
| 436 | - $value = apply_filters( $this->get_hook_prefix() . $key, $value, $this ); |
|
| 435 | + if ('view' === $context) { |
|
| 436 | + $value = apply_filters($this->get_hook_prefix() . $key, $value, $this); |
|
| 437 | 437 | } |
| 438 | 438 | |
| 439 | 439 | return $value; |
@@ -446,10 +446,10 @@ discard block |
||
| 446 | 446 | * @param string $key Meta Key. |
| 447 | 447 | * @return boolean |
| 448 | 448 | */ |
| 449 | - public function meta_exists( $key = '' ) { |
|
| 449 | + public function meta_exists($key = '') { |
|
| 450 | 450 | $this->maybe_read_meta_data(); |
| 451 | - $array_keys = wp_list_pluck( $this->get_meta_data(), 'key' ); |
|
| 452 | - return in_array( $key, $array_keys, true ); |
|
| 451 | + $array_keys = wp_list_pluck($this->get_meta_data(), 'key'); |
|
| 452 | + return in_array($key, $array_keys, true); |
|
| 453 | 453 | } |
| 454 | 454 | |
| 455 | 455 | /** |
@@ -458,12 +458,12 @@ discard block |
||
| 458 | 458 | * @since 1.0.19 |
| 459 | 459 | * @param array $data Key/Value pairs. |
| 460 | 460 | */ |
| 461 | - public function set_meta_data( $data ) { |
|
| 462 | - if ( ! empty( $data ) && is_array( $data ) ) { |
|
| 461 | + public function set_meta_data($data) { |
|
| 462 | + if (!empty($data) && is_array($data)) { |
|
| 463 | 463 | $this->maybe_read_meta_data(); |
| 464 | - foreach ( $data as $meta ) { |
|
| 464 | + foreach ($data as $meta) { |
|
| 465 | 465 | $meta = (array) $meta; |
| 466 | - if ( isset( $meta['key'], $meta['value'], $meta['id'] ) ) { |
|
| 466 | + if (isset($meta['key'], $meta['value'], $meta['id'])) { |
|
| 467 | 467 | $this->meta_data[] = new GetPaid_Meta_Data( |
| 468 | 468 | array( |
| 469 | 469 | 'id' => $meta['id'], |
@@ -485,18 +485,18 @@ discard block |
||
| 485 | 485 | * @param string|array $value Meta value. |
| 486 | 486 | * @param bool $unique Should this be a unique key?. |
| 487 | 487 | */ |
| 488 | - public function add_meta_data( $key, $value, $unique = false ) { |
|
| 489 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 488 | + public function add_meta_data($key, $value, $unique = false) { |
|
| 489 | + if ($this->is_internal_meta_key($key)) { |
|
| 490 | 490 | $function = 'set_' . $key; |
| 491 | 491 | |
| 492 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 493 | - return $this->{$function}( $value ); |
|
| 492 | + if (is_callable(array($this, $function))) { |
|
| 493 | + return $this->{$function}($value); |
|
| 494 | 494 | } |
| 495 | 495 | } |
| 496 | 496 | |
| 497 | 497 | $this->maybe_read_meta_data(); |
| 498 | - if ( $unique ) { |
|
| 499 | - $this->delete_meta_data( $key ); |
|
| 498 | + if ($unique) { |
|
| 499 | + $this->delete_meta_data($key); |
|
| 500 | 500 | } |
| 501 | 501 | $this->meta_data[] = new GetPaid_Meta_Data( |
| 502 | 502 | array( |
@@ -517,12 +517,12 @@ discard block |
||
| 517 | 517 | * @param string|array $value Meta value. |
| 518 | 518 | * @param int $meta_id Meta ID. |
| 519 | 519 | */ |
| 520 | - public function update_meta_data( $key, $value, $meta_id = 0 ) { |
|
| 521 | - if ( $this->is_internal_meta_key( $key ) ) { |
|
| 520 | + public function update_meta_data($key, $value, $meta_id = 0) { |
|
| 521 | + if ($this->is_internal_meta_key($key)) { |
|
| 522 | 522 | $function = 'set_' . $key; |
| 523 | 523 | |
| 524 | - if ( is_callable( array( $this, $function ) ) ) { |
|
| 525 | - return $this->{$function}( $value ); |
|
| 524 | + if (is_callable(array($this, $function))) { |
|
| 525 | + return $this->{$function}($value); |
|
| 526 | 526 | } |
| 527 | 527 | } |
| 528 | 528 | |
@@ -530,33 +530,33 @@ discard block |
||
| 530 | 530 | |
| 531 | 531 | $array_key = false; |
| 532 | 532 | |
| 533 | - if ( $meta_id ) { |
|
| 534 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), $meta_id, true ); |
|
| 535 | - $array_key = $array_keys ? current( $array_keys ) : false; |
|
| 533 | + if ($meta_id) { |
|
| 534 | + $array_keys = array_keys(wp_list_pluck($this->meta_data, 'id'), $meta_id, true); |
|
| 535 | + $array_key = $array_keys ? current($array_keys) : false; |
|
| 536 | 536 | } else { |
| 537 | 537 | // Find matches by key. |
| 538 | 538 | $matches = array(); |
| 539 | - foreach ( $this->meta_data as $meta_data_array_key => $meta ) { |
|
| 540 | - if ( $meta->key === $key ) { |
|
| 539 | + foreach ($this->meta_data as $meta_data_array_key => $meta) { |
|
| 540 | + if ($meta->key === $key) { |
|
| 541 | 541 | $matches[] = $meta_data_array_key; |
| 542 | 542 | } |
| 543 | 543 | } |
| 544 | 544 | |
| 545 | - if ( ! empty( $matches ) ) { |
|
| 545 | + if (!empty($matches)) { |
|
| 546 | 546 | // Set matches to null so only one key gets the new value. |
| 547 | - foreach ( $matches as $meta_data_array_key ) { |
|
| 548 | - $this->meta_data[ $meta_data_array_key ]->value = null; |
|
| 547 | + foreach ($matches as $meta_data_array_key) { |
|
| 548 | + $this->meta_data[$meta_data_array_key]->value = null; |
|
| 549 | 549 | } |
| 550 | - $array_key = current( $matches ); |
|
| 550 | + $array_key = current($matches); |
|
| 551 | 551 | } |
| 552 | 552 | } |
| 553 | 553 | |
| 554 | - if ( false !== $array_key ) { |
|
| 555 | - $meta = $this->meta_data[ $array_key ]; |
|
| 554 | + if (false !== $array_key) { |
|
| 555 | + $meta = $this->meta_data[$array_key]; |
|
| 556 | 556 | $meta->key = $key; |
| 557 | 557 | $meta->value = $value; |
| 558 | 558 | } else { |
| 559 | - $this->add_meta_data( $key, $value, true ); |
|
| 559 | + $this->add_meta_data($key, $value, true); |
|
| 560 | 560 | } |
| 561 | 561 | } |
| 562 | 562 | |
@@ -566,13 +566,13 @@ discard block |
||
| 566 | 566 | * @since 1.0.19 |
| 567 | 567 | * @param string $key Meta key. |
| 568 | 568 | */ |
| 569 | - public function delete_meta_data( $key ) { |
|
| 569 | + public function delete_meta_data($key) { |
|
| 570 | 570 | $this->maybe_read_meta_data(); |
| 571 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'key' ), $key, true ); |
|
| 571 | + $array_keys = array_keys(wp_list_pluck($this->meta_data, 'key'), $key, true); |
|
| 572 | 572 | |
| 573 | - if ( $array_keys ) { |
|
| 574 | - foreach ( $array_keys as $array_key ) { |
|
| 575 | - $this->meta_data[ $array_key ]->value = null; |
|
| 573 | + if ($array_keys) { |
|
| 574 | + foreach ($array_keys as $array_key) { |
|
| 575 | + $this->meta_data[$array_key]->value = null; |
|
| 576 | 576 | } |
| 577 | 577 | } |
| 578 | 578 | } |
@@ -583,13 +583,13 @@ discard block |
||
| 583 | 583 | * @since 1.0.19 |
| 584 | 584 | * @param int $mid Meta ID. |
| 585 | 585 | */ |
| 586 | - public function delete_meta_data_by_mid( $mid ) { |
|
| 586 | + public function delete_meta_data_by_mid($mid) { |
|
| 587 | 587 | $this->maybe_read_meta_data(); |
| 588 | - $array_keys = array_keys( wp_list_pluck( $this->meta_data, 'id' ), (int) $mid, true ); |
|
| 588 | + $array_keys = array_keys(wp_list_pluck($this->meta_data, 'id'), (int) $mid, true); |
|
| 589 | 589 | |
| 590 | - if ( $array_keys ) { |
|
| 591 | - foreach ( $array_keys as $array_key ) { |
|
| 592 | - $this->meta_data[ $array_key ]->value = null; |
|
| 590 | + if ($array_keys) { |
|
| 591 | + foreach ($array_keys as $array_key) { |
|
| 592 | + $this->meta_data[$array_key]->value = null; |
|
| 593 | 593 | } |
| 594 | 594 | } |
| 595 | 595 | } |
@@ -600,7 +600,7 @@ discard block |
||
| 600 | 600 | * @since 1.0.19 |
| 601 | 601 | */ |
| 602 | 602 | protected function maybe_read_meta_data() { |
| 603 | - if ( is_null( $this->meta_data ) ) { |
|
| 603 | + if (is_null($this->meta_data)) { |
|
| 604 | 604 | $this->read_meta_data(); |
| 605 | 605 | } |
| 606 | 606 | } |
@@ -612,42 +612,42 @@ discard block |
||
| 612 | 612 | * @since 1.0.19 |
| 613 | 613 | * @param bool $force_read True to force a new DB read (and update cache). |
| 614 | 614 | */ |
| 615 | - public function read_meta_data( $force_read = false ) { |
|
| 615 | + public function read_meta_data($force_read = false) { |
|
| 616 | 616 | |
| 617 | 617 | // Reset meta data. |
| 618 | 618 | $this->meta_data = array(); |
| 619 | 619 | |
| 620 | 620 | // Maybe abort early. |
| 621 | - if ( ! $this->get_id() || ! $this->data_store ) { |
|
| 621 | + if (!$this->get_id() || !$this->data_store) { |
|
| 622 | 622 | return; |
| 623 | 623 | } |
| 624 | 624 | |
| 625 | 625 | // Only read from cache if the cache key is set. |
| 626 | 626 | $cache_key = null; |
| 627 | - if ( ! $force_read && ! empty( $this->cache_group ) ) { |
|
| 628 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 629 | - $raw_meta_data = wp_cache_get( $cache_key, $this->cache_group ); |
|
| 627 | + if (!$force_read && !empty($this->cache_group)) { |
|
| 628 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix($this->cache_group) . GetPaid_Cache_Helper::get_cache_prefix('object_' . $this->get_id()) . 'object_meta_' . $this->get_id(); |
|
| 629 | + $raw_meta_data = wp_cache_get($cache_key, $this->cache_group); |
|
| 630 | 630 | } |
| 631 | 631 | |
| 632 | 632 | // Should we force read? |
| 633 | - if ( empty( $raw_meta_data ) ) { |
|
| 634 | - $raw_meta_data = $this->data_store->read_meta( $this ); |
|
| 633 | + if (empty($raw_meta_data)) { |
|
| 634 | + $raw_meta_data = $this->data_store->read_meta($this); |
|
| 635 | 635 | |
| 636 | - if ( ! empty( $cache_key ) ) { |
|
| 637 | - wp_cache_set( $cache_key, $raw_meta_data, $this->cache_group ); |
|
| 636 | + if (!empty($cache_key)) { |
|
| 637 | + wp_cache_set($cache_key, $raw_meta_data, $this->cache_group); |
|
| 638 | 638 | } |
| 639 | 639 | |
| 640 | 640 | } |
| 641 | 641 | |
| 642 | 642 | // Set meta data. |
| 643 | - if ( is_array( $raw_meta_data ) ) { |
|
| 643 | + if (is_array($raw_meta_data)) { |
|
| 644 | 644 | |
| 645 | - foreach ( $raw_meta_data as $meta ) { |
|
| 645 | + foreach ($raw_meta_data as $meta) { |
|
| 646 | 646 | $this->meta_data[] = new GetPaid_Meta_Data( |
| 647 | 647 | array( |
| 648 | 648 | 'id' => (int) $meta->meta_id, |
| 649 | 649 | 'key' => $meta->meta_key, |
| 650 | - 'value' => maybe_unserialize( $meta->meta_value ), |
|
| 650 | + 'value' => maybe_unserialize($meta->meta_value), |
|
| 651 | 651 | ) |
| 652 | 652 | ); |
| 653 | 653 | } |
@@ -662,28 +662,28 @@ discard block |
||
| 662 | 662 | * @since 1.0.19 |
| 663 | 663 | */ |
| 664 | 664 | public function save_meta_data() { |
| 665 | - if ( ! $this->data_store || is_null( $this->meta_data ) ) { |
|
| 665 | + if (!$this->data_store || is_null($this->meta_data)) { |
|
| 666 | 666 | return; |
| 667 | 667 | } |
| 668 | - foreach ( $this->meta_data as $array_key => $meta ) { |
|
| 669 | - if ( is_null( $meta->value ) ) { |
|
| 670 | - if ( ! empty( $meta->id ) ) { |
|
| 671 | - $this->data_store->delete_meta( $this, $meta ); |
|
| 672 | - unset( $this->meta_data[ $array_key ] ); |
|
| 668 | + foreach ($this->meta_data as $array_key => $meta) { |
|
| 669 | + if (is_null($meta->value)) { |
|
| 670 | + if (!empty($meta->id)) { |
|
| 671 | + $this->data_store->delete_meta($this, $meta); |
|
| 672 | + unset($this->meta_data[$array_key]); |
|
| 673 | 673 | } |
| 674 | - } elseif ( empty( $meta->id ) ) { |
|
| 675 | - $meta->id = $this->data_store->add_meta( $this, $meta ); |
|
| 674 | + } elseif (empty($meta->id)) { |
|
| 675 | + $meta->id = $this->data_store->add_meta($this, $meta); |
|
| 676 | 676 | $meta->apply_changes(); |
| 677 | 677 | } else { |
| 678 | - if ( $meta->get_changes() ) { |
|
| 679 | - $this->data_store->update_meta( $this, $meta ); |
|
| 678 | + if ($meta->get_changes()) { |
|
| 679 | + $this->data_store->update_meta($this, $meta); |
|
| 680 | 680 | $meta->apply_changes(); |
| 681 | 681 | } |
| 682 | 682 | } |
| 683 | 683 | } |
| 684 | - if ( ! empty( $this->cache_group ) ) { |
|
| 685 | - $cache_key = GetPaid_Cache_Helper::get_cache_prefix( $this->cache_group ) . GetPaid_Cache_Helper::get_cache_prefix( 'object_' . $this->get_id() ) . 'object_meta_' . $this->get_id(); |
|
| 686 | - wp_cache_delete( $cache_key, $this->cache_group ); |
|
| 684 | + if (!empty($this->cache_group)) { |
|
| 685 | + $cache_key = GetPaid_Cache_Helper::get_cache_prefix($this->cache_group) . GetPaid_Cache_Helper::get_cache_prefix('object_' . $this->get_id()) . 'object_meta_' . $this->get_id(); |
|
| 686 | + wp_cache_delete($cache_key, $this->cache_group); |
|
| 687 | 687 | } |
| 688 | 688 | } |
| 689 | 689 | |
@@ -693,8 +693,8 @@ discard block |
||
| 693 | 693 | * @since 1.0.19 |
| 694 | 694 | * @param int $id ID. |
| 695 | 695 | */ |
| 696 | - public function set_id( $id ) { |
|
| 697 | - $this->id = absint( $id ); |
|
| 696 | + public function set_id($id) { |
|
| 697 | + $this->id = absint($id); |
|
| 698 | 698 | } |
| 699 | 699 | |
| 700 | 700 | /** |
@@ -704,10 +704,10 @@ discard block |
||
| 704 | 704 | * @param string $status New status. |
| 705 | 705 | * @return array details of change. |
| 706 | 706 | */ |
| 707 | - public function set_status( $status ) { |
|
| 707 | + public function set_status($status) { |
|
| 708 | 708 | $old_status = $this->get_status(); |
| 709 | 709 | |
| 710 | - $this->set_prop( 'status', $status ); |
|
| 710 | + $this->set_prop('status', $status); |
|
| 711 | 711 | |
| 712 | 712 | return array( |
| 713 | 713 | 'from' => $old_status, |
@@ -723,7 +723,7 @@ discard block |
||
| 723 | 723 | public function set_defaults() { |
| 724 | 724 | $this->data = $this->default_data; |
| 725 | 725 | $this->changes = array(); |
| 726 | - $this->set_object_read( false ); |
|
| 726 | + $this->set_object_read(false); |
|
| 727 | 727 | } |
| 728 | 728 | |
| 729 | 729 | /** |
@@ -732,7 +732,7 @@ discard block |
||
| 732 | 732 | * @since 1.0.19 |
| 733 | 733 | * @param boolean $read Should read?. |
| 734 | 734 | */ |
| 735 | - public function set_object_read( $read = true ) { |
|
| 735 | + public function set_object_read($read = true) { |
|
| 736 | 736 | $this->object_read = (bool) $read; |
| 737 | 737 | } |
| 738 | 738 | |
@@ -757,32 +757,32 @@ discard block |
||
| 757 | 757 | * |
| 758 | 758 | * @return bool|WP_Error |
| 759 | 759 | */ |
| 760 | - public function set_props( $props, $context = 'set' ) { |
|
| 760 | + public function set_props($props, $context = 'set') { |
|
| 761 | 761 | $errors = false; |
| 762 | 762 | |
| 763 | - foreach ( $props as $prop => $value ) { |
|
| 763 | + foreach ($props as $prop => $value) { |
|
| 764 | 764 | try { |
| 765 | 765 | /** |
| 766 | 766 | * Checks if the prop being set is allowed, and the value is not null. |
| 767 | 767 | */ |
| 768 | - if ( is_null( $value ) || in_array( $prop, array( 'prop', 'date_prop', 'meta_data' ), true ) ) { |
|
| 768 | + if (is_null($value) || in_array($prop, array('prop', 'date_prop', 'meta_data'), true)) { |
|
| 769 | 769 | continue; |
| 770 | 770 | } |
| 771 | 771 | $setter = "set_$prop"; |
| 772 | 772 | |
| 773 | - if ( is_callable( array( $this, $setter ) ) ) { |
|
| 774 | - $this->{$setter}( $value ); |
|
| 773 | + if (is_callable(array($this, $setter))) { |
|
| 774 | + $this->{$setter}($value); |
|
| 775 | 775 | } |
| 776 | - } catch ( Exception $e ) { |
|
| 777 | - if ( ! $errors ) { |
|
| 776 | + } catch (Exception $e) { |
|
| 777 | + if (!$errors) { |
|
| 778 | 778 | $errors = new WP_Error(); |
| 779 | 779 | } |
| 780 | - $errors->add( $e->getCode(), $e->getMessage() ); |
|
| 780 | + $errors->add($e->getCode(), $e->getMessage()); |
|
| 781 | 781 | $this->last_error = $e->getMessage(); |
| 782 | 782 | } |
| 783 | 783 | } |
| 784 | 784 | |
| 785 | - return $errors && count( $errors->get_error_codes() ) ? $errors : true; |
|
| 785 | + return $errors && count($errors->get_error_codes()) ? $errors : true; |
|
| 786 | 786 | } |
| 787 | 787 | |
| 788 | 788 | /** |
@@ -795,14 +795,14 @@ discard block |
||
| 795 | 795 | * @param string $prop Name of prop to set. |
| 796 | 796 | * @param mixed $value Value of the prop. |
| 797 | 797 | */ |
| 798 | - protected function set_prop( $prop, $value ) { |
|
| 799 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 800 | - if ( true === $this->object_read ) { |
|
| 801 | - if ( $value !== $this->data[ $prop ] || array_key_exists( $prop, $this->changes ) ) { |
|
| 802 | - $this->changes[ $prop ] = $value; |
|
| 798 | + protected function set_prop($prop, $value) { |
|
| 799 | + if (array_key_exists($prop, $this->data)) { |
|
| 800 | + if (true === $this->object_read) { |
|
| 801 | + if ($value !== $this->data[$prop] || array_key_exists($prop, $this->changes)) { |
|
| 802 | + $this->changes[$prop] = $value; |
|
| 803 | 803 | } |
| 804 | 804 | } else { |
| 805 | - $this->data[ $prop ] = $value; |
|
| 805 | + $this->data[$prop] = $value; |
|
| 806 | 806 | } |
| 807 | 807 | } |
| 808 | 808 | } |
@@ -823,7 +823,7 @@ discard block |
||
| 823 | 823 | * @since 1.0.19 |
| 824 | 824 | */ |
| 825 | 825 | public function apply_changes() { |
| 826 | - $this->data = array_replace( $this->data, $this->changes ); |
|
| 826 | + $this->data = array_replace($this->data, $this->changes); |
|
| 827 | 827 | $this->changes = array(); |
| 828 | 828 | } |
| 829 | 829 | |
@@ -848,14 +848,14 @@ discard block |
||
| 848 | 848 | * @param string $context What the value is for. Valid values are view and edit. |
| 849 | 849 | * @return mixed |
| 850 | 850 | */ |
| 851 | - protected function get_prop( $prop, $context = 'view' ) { |
|
| 851 | + protected function get_prop($prop, $context = 'view') { |
|
| 852 | 852 | $value = null; |
| 853 | 853 | |
| 854 | - if ( array_key_exists( $prop, $this->data ) ) { |
|
| 855 | - $value = array_key_exists( $prop, $this->changes ) ? $this->changes[ $prop ] : $this->data[ $prop ]; |
|
| 854 | + if (array_key_exists($prop, $this->data)) { |
|
| 855 | + $value = array_key_exists($prop, $this->changes) ? $this->changes[$prop] : $this->data[$prop]; |
|
| 856 | 856 | |
| 857 | - if ( 'view' === $context ) { |
|
| 858 | - $value = apply_filters( $this->get_hook_prefix() . $prop, $value, $this ); |
|
| 857 | + if ('view' === $context) { |
|
| 858 | + $value = apply_filters($this->get_hook_prefix() . $prop, $value, $this); |
|
| 859 | 859 | } |
| 860 | 860 | } |
| 861 | 861 | |
@@ -869,13 +869,13 @@ discard block |
||
| 869 | 869 | * @param string $prop Name of prop to set. |
| 870 | 870 | * @param string|integer $value Value of the prop. |
| 871 | 871 | */ |
| 872 | - protected function set_date_prop( $prop, $value ) { |
|
| 872 | + protected function set_date_prop($prop, $value) { |
|
| 873 | 873 | |
| 874 | - if ( empty( $value ) ) { |
|
| 875 | - $this->set_prop( $prop, null ); |
|
| 874 | + if (empty($value)) { |
|
| 875 | + $this->set_prop($prop, null); |
|
| 876 | 876 | return; |
| 877 | 877 | } |
| 878 | - $this->set_prop( $prop, $value ); |
|
| 878 | + $this->set_prop($prop, $value); |
|
| 879 | 879 | |
| 880 | 880 | } |
| 881 | 881 | |
@@ -886,7 +886,7 @@ discard block |
||
| 886 | 886 | * @param string $code Error code. |
| 887 | 887 | * @param string $message Error message. |
| 888 | 888 | */ |
| 889 | - protected function error( $code, $message ) { |
|
| 889 | + protected function error($code, $message) { |
|
| 890 | 890 | $this->last_error = $message; |
| 891 | 891 | } |
| 892 | 892 | |
@@ -898,7 +898,7 @@ discard block |
||
| 898 | 898 | */ |
| 899 | 899 | public function exists() { |
| 900 | 900 | $id = $this->get_id(); |
| 901 | - return ! empty( $id ); |
|
| 901 | + return !empty($id); |
|
| 902 | 902 | } |
| 903 | 903 | |
| 904 | 904 | } |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | - exit; |
|
| 3 | + exit; |
|
| 4 | 4 | } |
| 5 | 5 | |
| 6 | 6 | /** |
@@ -10,187 +10,187 @@ discard block |
||
| 10 | 10 | class GetPaid_Payment_Form_Submission { |
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | - * Submission ID |
|
| 14 | - * |
|
| 15 | - * @var string |
|
| 16 | - */ |
|
| 17 | - public $id = null; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * The raw submission data. |
|
| 21 | - * |
|
| 22 | - * @var array |
|
| 23 | - */ |
|
| 24 | - protected $data = null; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Submission totals |
|
| 28 | - * |
|
| 29 | - * @var array |
|
| 30 | - */ |
|
| 31 | - protected $totals = array( |
|
| 32 | - |
|
| 33 | - 'subtotal' => array( |
|
| 34 | - 'initial' => 0, |
|
| 35 | - 'recurring' => 0, |
|
| 36 | - ), |
|
| 37 | - |
|
| 38 | - 'discount' => array( |
|
| 39 | - 'initial' => 0, |
|
| 40 | - 'recurring' => 0, |
|
| 41 | - ), |
|
| 42 | - |
|
| 43 | - 'fees' => array( |
|
| 44 | - 'initial' => 0, |
|
| 45 | - 'recurring' => 0, |
|
| 46 | - ), |
|
| 47 | - |
|
| 48 | - 'taxes' => array( |
|
| 49 | - 'initial' => 0, |
|
| 50 | - 'recurring' => 0, |
|
| 51 | - ), |
|
| 52 | - |
|
| 53 | - ); |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * Sets the associated payment form. |
|
| 57 | - * |
|
| 58 | - * @var GetPaid_Payment_Form |
|
| 59 | - */ |
|
| 13 | + * Submission ID |
|
| 14 | + * |
|
| 15 | + * @var string |
|
| 16 | + */ |
|
| 17 | + public $id = null; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * The raw submission data. |
|
| 21 | + * |
|
| 22 | + * @var array |
|
| 23 | + */ |
|
| 24 | + protected $data = null; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Submission totals |
|
| 28 | + * |
|
| 29 | + * @var array |
|
| 30 | + */ |
|
| 31 | + protected $totals = array( |
|
| 32 | + |
|
| 33 | + 'subtotal' => array( |
|
| 34 | + 'initial' => 0, |
|
| 35 | + 'recurring' => 0, |
|
| 36 | + ), |
|
| 37 | + |
|
| 38 | + 'discount' => array( |
|
| 39 | + 'initial' => 0, |
|
| 40 | + 'recurring' => 0, |
|
| 41 | + ), |
|
| 42 | + |
|
| 43 | + 'fees' => array( |
|
| 44 | + 'initial' => 0, |
|
| 45 | + 'recurring' => 0, |
|
| 46 | + ), |
|
| 47 | + |
|
| 48 | + 'taxes' => array( |
|
| 49 | + 'initial' => 0, |
|
| 50 | + 'recurring' => 0, |
|
| 51 | + ), |
|
| 52 | + |
|
| 53 | + ); |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * Sets the associated payment form. |
|
| 57 | + * |
|
| 58 | + * @var GetPaid_Payment_Form |
|
| 59 | + */ |
|
| 60 | 60 | protected $payment_form = null; |
| 61 | 61 | |
| 62 | 62 | /** |
| 63 | - * The country for the submission. |
|
| 64 | - * |
|
| 65 | - * @var string |
|
| 66 | - */ |
|
| 67 | - public $country = null; |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * The state for the submission. |
|
| 71 | - * |
|
| 72 | - * @since 1.0.19 |
|
| 73 | - * @var string |
|
| 74 | - */ |
|
| 75 | - public $state = null; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * The invoice associated with the submission. |
|
| 79 | - * |
|
| 80 | - * @var WPInv_Invoice |
|
| 81 | - */ |
|
| 82 | - protected $invoice = null; |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * The recurring item for the submission. |
|
| 86 | - * |
|
| 87 | - * @var int |
|
| 88 | - */ |
|
| 89 | - public $has_recurring = 0; |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * An array of fees for the submission. |
|
| 93 | - * |
|
| 94 | - * @var array |
|
| 95 | - */ |
|
| 96 | - protected $fees = array(); |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * An array of discounts for the submission. |
|
| 100 | - * |
|
| 101 | - * @var array |
|
| 102 | - */ |
|
| 103 | - protected $discounts = array(); |
|
| 104 | - |
|
| 105 | - /** |
|
| 106 | - * An array of taxes for the submission. |
|
| 107 | - * |
|
| 108 | - * @var array |
|
| 109 | - */ |
|
| 110 | - protected $taxes = array(); |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * An array of items for the submission. |
|
| 114 | - * |
|
| 115 | - * @var GetPaid_Form_Item[] |
|
| 116 | - */ |
|
| 117 | - protected $items = array(); |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * The last error. |
|
| 121 | - * |
|
| 122 | - * @var string |
|
| 123 | - */ |
|
| 124 | - public $last_error = null; |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Class constructor. |
|
| 128 | - * |
|
| 129 | - */ |
|
| 130 | - public function __construct() { |
|
| 131 | - |
|
| 132 | - // Set the state and country to the default state and country. |
|
| 133 | - $this->country = wpinv_default_billing_country(); |
|
| 134 | - $this->state = wpinv_get_default_state(); |
|
| 135 | - |
|
| 136 | - // Do we have an actual submission? |
|
| 137 | - if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
| 138 | - $this->load_data( $_POST ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * Loads submission data. |
|
| 145 | - * |
|
| 146 | - * @param array $data |
|
| 147 | - */ |
|
| 148 | - public function load_data( $data ) { |
|
| 149 | - |
|
| 150 | - // Remove slashes from the submitted data... |
|
| 151 | - $data = wp_unslash( $data ); |
|
| 152 | - |
|
| 153 | - // Allow plugins to filter the data. |
|
| 154 | - $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
| 155 | - |
|
| 156 | - // Cache it... |
|
| 157 | - $this->data = $data; |
|
| 158 | - |
|
| 159 | - // Then generate a unique id from the data. |
|
| 160 | - $this->id = md5( wp_json_encode( $data ) ); |
|
| 161 | - |
|
| 162 | - // Finally, process the submission. |
|
| 163 | - try { |
|
| 164 | - |
|
| 165 | - // Each process is passed an instance of the class (with reference) |
|
| 166 | - // and should throw an Exception whenever it encounters one. |
|
| 167 | - $processors = apply_filters( |
|
| 168 | - 'getpaid_payment_form_submission_processors', |
|
| 169 | - array( |
|
| 170 | - array( $this, 'process_payment_form' ), |
|
| 171 | - array( $this, 'process_invoice' ), |
|
| 172 | - array( $this, 'process_fees' ), |
|
| 173 | - array( $this, 'process_items' ), |
|
| 174 | - array( $this, 'process_discount' ), |
|
| 175 | - array( $this, 'process_taxes' ), |
|
| 176 | - ), |
|
| 177 | - $this |
|
| 178 | - ); |
|
| 179 | - |
|
| 180 | - foreach ( $processors as $processor ) { |
|
| 181 | - call_user_func_array( $processor, array( &$this ) ); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - } catch ( Exception $e ) { |
|
| 185 | - $this->last_error = $e->getMessage(); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - // Fired when we are done processing a submission. |
|
| 189 | - do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
|
| 190 | - |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - /* |
|
| 63 | + * The country for the submission. |
|
| 64 | + * |
|
| 65 | + * @var string |
|
| 66 | + */ |
|
| 67 | + public $country = null; |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * The state for the submission. |
|
| 71 | + * |
|
| 72 | + * @since 1.0.19 |
|
| 73 | + * @var string |
|
| 74 | + */ |
|
| 75 | + public $state = null; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * The invoice associated with the submission. |
|
| 79 | + * |
|
| 80 | + * @var WPInv_Invoice |
|
| 81 | + */ |
|
| 82 | + protected $invoice = null; |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * The recurring item for the submission. |
|
| 86 | + * |
|
| 87 | + * @var int |
|
| 88 | + */ |
|
| 89 | + public $has_recurring = 0; |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * An array of fees for the submission. |
|
| 93 | + * |
|
| 94 | + * @var array |
|
| 95 | + */ |
|
| 96 | + protected $fees = array(); |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * An array of discounts for the submission. |
|
| 100 | + * |
|
| 101 | + * @var array |
|
| 102 | + */ |
|
| 103 | + protected $discounts = array(); |
|
| 104 | + |
|
| 105 | + /** |
|
| 106 | + * An array of taxes for the submission. |
|
| 107 | + * |
|
| 108 | + * @var array |
|
| 109 | + */ |
|
| 110 | + protected $taxes = array(); |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * An array of items for the submission. |
|
| 114 | + * |
|
| 115 | + * @var GetPaid_Form_Item[] |
|
| 116 | + */ |
|
| 117 | + protected $items = array(); |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * The last error. |
|
| 121 | + * |
|
| 122 | + * @var string |
|
| 123 | + */ |
|
| 124 | + public $last_error = null; |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Class constructor. |
|
| 128 | + * |
|
| 129 | + */ |
|
| 130 | + public function __construct() { |
|
| 131 | + |
|
| 132 | + // Set the state and country to the default state and country. |
|
| 133 | + $this->country = wpinv_default_billing_country(); |
|
| 134 | + $this->state = wpinv_get_default_state(); |
|
| 135 | + |
|
| 136 | + // Do we have an actual submission? |
|
| 137 | + if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
| 138 | + $this->load_data( $_POST ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * Loads submission data. |
|
| 145 | + * |
|
| 146 | + * @param array $data |
|
| 147 | + */ |
|
| 148 | + public function load_data( $data ) { |
|
| 149 | + |
|
| 150 | + // Remove slashes from the submitted data... |
|
| 151 | + $data = wp_unslash( $data ); |
|
| 152 | + |
|
| 153 | + // Allow plugins to filter the data. |
|
| 154 | + $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
| 155 | + |
|
| 156 | + // Cache it... |
|
| 157 | + $this->data = $data; |
|
| 158 | + |
|
| 159 | + // Then generate a unique id from the data. |
|
| 160 | + $this->id = md5( wp_json_encode( $data ) ); |
|
| 161 | + |
|
| 162 | + // Finally, process the submission. |
|
| 163 | + try { |
|
| 164 | + |
|
| 165 | + // Each process is passed an instance of the class (with reference) |
|
| 166 | + // and should throw an Exception whenever it encounters one. |
|
| 167 | + $processors = apply_filters( |
|
| 168 | + 'getpaid_payment_form_submission_processors', |
|
| 169 | + array( |
|
| 170 | + array( $this, 'process_payment_form' ), |
|
| 171 | + array( $this, 'process_invoice' ), |
|
| 172 | + array( $this, 'process_fees' ), |
|
| 173 | + array( $this, 'process_items' ), |
|
| 174 | + array( $this, 'process_discount' ), |
|
| 175 | + array( $this, 'process_taxes' ), |
|
| 176 | + ), |
|
| 177 | + $this |
|
| 178 | + ); |
|
| 179 | + |
|
| 180 | + foreach ( $processors as $processor ) { |
|
| 181 | + call_user_func_array( $processor, array( &$this ) ); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + } catch ( Exception $e ) { |
|
| 185 | + $this->last_error = $e->getMessage(); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + // Fired when we are done processing a submission. |
|
| 189 | + do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
|
| 190 | + |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + /* |
|
| 194 | 194 | |-------------------------------------------------------------------------- |
| 195 | 195 | | Payment Forms. |
| 196 | 196 | |-------------------------------------------------------------------------- |
@@ -199,39 +199,39 @@ discard block |
||
| 199 | 199 | | submission has an active payment form etc. |
| 200 | 200 | */ |
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * Prepares the submission's payment form. |
|
| 204 | - * |
|
| 205 | - * @since 1.0.19 |
|
| 206 | - */ |
|
| 207 | - public function process_payment_form() { |
|
| 202 | + /** |
|
| 203 | + * Prepares the submission's payment form. |
|
| 204 | + * |
|
| 205 | + * @since 1.0.19 |
|
| 206 | + */ |
|
| 207 | + public function process_payment_form() { |
|
| 208 | 208 | |
| 209 | - // Every submission needs an active payment form. |
|
| 210 | - if ( empty( $this->data['form_id'] ) ) { |
|
| 211 | - throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
|
| 212 | - } |
|
| 209 | + // Every submission needs an active payment form. |
|
| 210 | + if ( empty( $this->data['form_id'] ) ) { |
|
| 211 | + throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | - // Fetch the payment form. |
|
| 215 | - $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
| 214 | + // Fetch the payment form. |
|
| 215 | + $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
| 216 | 216 | |
| 217 | - if ( ! $this->payment_form->is_active() ) { |
|
| 218 | - throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
| 219 | - } |
|
| 217 | + if ( ! $this->payment_form->is_active() ) { |
|
| 218 | + throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
| 222 | - } |
|
| 221 | + do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | 224 | /** |
| 225 | - * Returns the payment form. |
|
| 226 | - * |
|
| 227 | - * @since 1.0.19 |
|
| 228 | - * @return GetPaid_Payment_Form |
|
| 229 | - */ |
|
| 230 | - public function get_payment_form() { |
|
| 231 | - return $this->payment_form; |
|
| 232 | - } |
|
| 225 | + * Returns the payment form. |
|
| 226 | + * |
|
| 227 | + * @since 1.0.19 |
|
| 228 | + * @return GetPaid_Payment_Form |
|
| 229 | + */ |
|
| 230 | + public function get_payment_form() { |
|
| 231 | + return $this->payment_form; |
|
| 232 | + } |
|
| 233 | 233 | |
| 234 | - /* |
|
| 234 | + /* |
|
| 235 | 235 | |-------------------------------------------------------------------------- |
| 236 | 236 | | Invoices. |
| 237 | 237 | |-------------------------------------------------------------------------- |
@@ -240,84 +240,84 @@ discard block |
||
| 240 | 240 | | might be for an existing invoice. |
| 241 | 241 | */ |
| 242 | 242 | |
| 243 | - /** |
|
| 244 | - * Prepares the submission's invoice. |
|
| 245 | - * |
|
| 246 | - * @since 1.0.19 |
|
| 247 | - */ |
|
| 248 | - public function process_invoice() { |
|
| 243 | + /** |
|
| 244 | + * Prepares the submission's invoice. |
|
| 245 | + * |
|
| 246 | + * @since 1.0.19 |
|
| 247 | + */ |
|
| 248 | + public function process_invoice() { |
|
| 249 | 249 | |
| 250 | - // Abort if there is no invoice. |
|
| 251 | - if ( empty( $this->data['invoice_id'] ) ) { |
|
| 252 | - return; |
|
| 253 | - } |
|
| 250 | + // Abort if there is no invoice. |
|
| 251 | + if ( empty( $this->data['invoice_id'] ) ) { |
|
| 252 | + return; |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - // If the submission is for an existing invoice, ensure that it exists |
|
| 256 | - // and that it is not paid for. |
|
| 257 | - $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
|
| 255 | + // If the submission is for an existing invoice, ensure that it exists |
|
| 256 | + // and that it is not paid for. |
|
| 257 | + $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
|
| 258 | 258 | |
| 259 | 259 | if ( empty( $invoice ) ) { |
| 260 | - throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
| 261 | - } |
|
| 260 | + throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
| 261 | + } |
|
| 262 | 262 | |
| 263 | - if ( $invoice->is_paid() ) { |
|
| 264 | - throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
| 265 | - } |
|
| 263 | + if ( $invoice->is_paid() ) { |
|
| 264 | + throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - $this->payment_form->invoice = $invoice; |
|
| 268 | - if ( ! $this->payment_form->is_default() ) { |
|
| 267 | + $this->payment_form->invoice = $invoice; |
|
| 268 | + if ( ! $this->payment_form->is_default() ) { |
|
| 269 | 269 | |
| 270 | - $items = array(); |
|
| 271 | - $item_ids = array(); |
|
| 270 | + $items = array(); |
|
| 271 | + $item_ids = array(); |
|
| 272 | 272 | |
| 273 | - foreach ( $invoice->get_items() as $item ) { |
|
| 274 | - if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 275 | - $item_ids[] = $item->get_id(); |
|
| 276 | - $items[] = $item; |
|
| 277 | - } |
|
| 278 | - } |
|
| 273 | + foreach ( $invoice->get_items() as $item ) { |
|
| 274 | + if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 275 | + $item_ids[] = $item->get_id(); |
|
| 276 | + $items[] = $item; |
|
| 277 | + } |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - foreach ( $this->payment_form->get_items() as $item ) { |
|
| 281 | - if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 282 | - $item_ids[] = $item->get_id(); |
|
| 283 | - $items[] = $item; |
|
| 284 | - } |
|
| 285 | - } |
|
| 280 | + foreach ( $this->payment_form->get_items() as $item ) { |
|
| 281 | + if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 282 | + $item_ids[] = $item->get_id(); |
|
| 283 | + $items[] = $item; |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | 286 | |
| 287 | - $this->payment_form->set_items( $items ); |
|
| 287 | + $this->payment_form->set_items( $items ); |
|
| 288 | 288 | |
| 289 | - } else { |
|
| 290 | - $this->payment_form->set_items( $invoice->get_items() ); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - $this->country = $invoice->get_country(); |
|
| 294 | - $this->state = $invoice->get_state(); |
|
| 295 | - $this->invoice = $invoice; |
|
| 296 | - |
|
| 297 | - do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * Returns the associated invoice. |
|
| 302 | - * |
|
| 303 | - * @since 1.0.19 |
|
| 304 | - * @return WPInv_Invoice |
|
| 305 | - */ |
|
| 306 | - public function get_invoice() { |
|
| 307 | - return $this->invoice; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Checks whether there is an invoice associated with this submission. |
|
| 312 | - * |
|
| 313 | - * @since 1.0.19 |
|
| 314 | - * @return bool |
|
| 315 | - */ |
|
| 316 | - public function has_invoice() { |
|
| 317 | - return ! empty( $this->invoice ); |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /* |
|
| 289 | + } else { |
|
| 290 | + $this->payment_form->set_items( $invoice->get_items() ); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + $this->country = $invoice->get_country(); |
|
| 294 | + $this->state = $invoice->get_state(); |
|
| 295 | + $this->invoice = $invoice; |
|
| 296 | + |
|
| 297 | + do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * Returns the associated invoice. |
|
| 302 | + * |
|
| 303 | + * @since 1.0.19 |
|
| 304 | + * @return WPInv_Invoice |
|
| 305 | + */ |
|
| 306 | + public function get_invoice() { |
|
| 307 | + return $this->invoice; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Checks whether there is an invoice associated with this submission. |
|
| 312 | + * |
|
| 313 | + * @since 1.0.19 |
|
| 314 | + * @return bool |
|
| 315 | + */ |
|
| 316 | + public function has_invoice() { |
|
| 317 | + return ! empty( $this->invoice ); |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /* |
|
| 321 | 321 | |-------------------------------------------------------------------------- |
| 322 | 322 | | Items. |
| 323 | 323 | |-------------------------------------------------------------------------- |
@@ -326,115 +326,115 @@ discard block |
||
| 326 | 326 | | recurring item. But can have an unlimited number of non-recurring items. |
| 327 | 327 | */ |
| 328 | 328 | |
| 329 | - /** |
|
| 330 | - * Prepares the submission's items. |
|
| 331 | - * |
|
| 332 | - * @since 1.0.19 |
|
| 333 | - */ |
|
| 334 | - public function process_items() { |
|
| 335 | - |
|
| 336 | - $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
| 337 | - |
|
| 338 | - foreach ( $processor->items as $item ) { |
|
| 339 | - $this->add_item( $item ); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * Adds an item to the submission. |
|
| 347 | - * |
|
| 348 | - * @since 1.0.19 |
|
| 349 | - * @param GetPaid_Form_Item $item |
|
| 350 | - */ |
|
| 351 | - public function add_item( $item ) { |
|
| 352 | - |
|
| 353 | - // Make sure that it is available for purchase. |
|
| 354 | - if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
|
| 355 | - return; |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - // Each submission can only contain one recurring item. |
|
| 359 | - if ( $item->is_recurring() ) { |
|
| 360 | - |
|
| 361 | - if ( $this->has_recurring != 0 ) { |
|
| 362 | - throw new Exception( __( 'You can only buy one recurring item at a time.', 'invoicing' ) ); |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - $this->has_recurring = $item->get_id(); |
|
| 366 | - |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - // Update the items and totals. |
|
| 370 | - $this->items[ $item->get_id() ] = $item; |
|
| 371 | - $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
|
| 372 | - $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
|
| 373 | - |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - /** |
|
| 377 | - * Removes a specific item. |
|
| 378 | - * |
|
| 379 | - * You should not call this method after the discounts and taxes |
|
| 380 | - * have been calculated. |
|
| 381 | - * |
|
| 382 | - * @since 1.0.19 |
|
| 383 | - */ |
|
| 384 | - public function remove_item( $item_id ) { |
|
| 385 | - |
|
| 386 | - if ( isset( $this->items[ $item_id ] ) ) { |
|
| 387 | - $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
|
| 388 | - $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
|
| 389 | - |
|
| 390 | - if ( $this->items[ $item_id ]->is_recurring() ) { |
|
| 391 | - $this->has_recurring = 0; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - unset( $this->items[ $item_id ] ); |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Returns the subtotal. |
|
| 401 | - * |
|
| 402 | - * @since 1.0.19 |
|
| 403 | - */ |
|
| 404 | - public function get_subtotal() { |
|
| 405 | - |
|
| 406 | - if ( wpinv_prices_include_tax() ) { |
|
| 407 | - return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - return $this->totals['subtotal']['initial']; |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - /** |
|
| 414 | - * Returns the recurring subtotal. |
|
| 415 | - * |
|
| 416 | - * @since 1.0.19 |
|
| 417 | - */ |
|
| 418 | - public function get_recurring_subtotal() { |
|
| 419 | - |
|
| 420 | - if ( wpinv_prices_include_tax() ) { |
|
| 421 | - return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - return $this->totals['subtotal']['recurring']; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Returns all items. |
|
| 429 | - * |
|
| 430 | - * @since 1.0.19 |
|
| 431 | - * @return GetPaid_Form_Item[] |
|
| 432 | - */ |
|
| 433 | - public function get_items() { |
|
| 434 | - return $this->items; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - /* |
|
| 329 | + /** |
|
| 330 | + * Prepares the submission's items. |
|
| 331 | + * |
|
| 332 | + * @since 1.0.19 |
|
| 333 | + */ |
|
| 334 | + public function process_items() { |
|
| 335 | + |
|
| 336 | + $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
| 337 | + |
|
| 338 | + foreach ( $processor->items as $item ) { |
|
| 339 | + $this->add_item( $item ); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * Adds an item to the submission. |
|
| 347 | + * |
|
| 348 | + * @since 1.0.19 |
|
| 349 | + * @param GetPaid_Form_Item $item |
|
| 350 | + */ |
|
| 351 | + public function add_item( $item ) { |
|
| 352 | + |
|
| 353 | + // Make sure that it is available for purchase. |
|
| 354 | + if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
|
| 355 | + return; |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + // Each submission can only contain one recurring item. |
|
| 359 | + if ( $item->is_recurring() ) { |
|
| 360 | + |
|
| 361 | + if ( $this->has_recurring != 0 ) { |
|
| 362 | + throw new Exception( __( 'You can only buy one recurring item at a time.', 'invoicing' ) ); |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + $this->has_recurring = $item->get_id(); |
|
| 366 | + |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + // Update the items and totals. |
|
| 370 | + $this->items[ $item->get_id() ] = $item; |
|
| 371 | + $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
|
| 372 | + $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
|
| 373 | + |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + /** |
|
| 377 | + * Removes a specific item. |
|
| 378 | + * |
|
| 379 | + * You should not call this method after the discounts and taxes |
|
| 380 | + * have been calculated. |
|
| 381 | + * |
|
| 382 | + * @since 1.0.19 |
|
| 383 | + */ |
|
| 384 | + public function remove_item( $item_id ) { |
|
| 385 | + |
|
| 386 | + if ( isset( $this->items[ $item_id ] ) ) { |
|
| 387 | + $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
|
| 388 | + $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
|
| 389 | + |
|
| 390 | + if ( $this->items[ $item_id ]->is_recurring() ) { |
|
| 391 | + $this->has_recurring = 0; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + unset( $this->items[ $item_id ] ); |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Returns the subtotal. |
|
| 401 | + * |
|
| 402 | + * @since 1.0.19 |
|
| 403 | + */ |
|
| 404 | + public function get_subtotal() { |
|
| 405 | + |
|
| 406 | + if ( wpinv_prices_include_tax() ) { |
|
| 407 | + return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + return $this->totals['subtotal']['initial']; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + /** |
|
| 414 | + * Returns the recurring subtotal. |
|
| 415 | + * |
|
| 416 | + * @since 1.0.19 |
|
| 417 | + */ |
|
| 418 | + public function get_recurring_subtotal() { |
|
| 419 | + |
|
| 420 | + if ( wpinv_prices_include_tax() ) { |
|
| 421 | + return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + return $this->totals['subtotal']['recurring']; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Returns all items. |
|
| 429 | + * |
|
| 430 | + * @since 1.0.19 |
|
| 431 | + * @return GetPaid_Form_Item[] |
|
| 432 | + */ |
|
| 433 | + public function get_items() { |
|
| 434 | + return $this->items; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + /* |
|
| 438 | 438 | |-------------------------------------------------------------------------- |
| 439 | 439 | | Taxes |
| 440 | 440 | |-------------------------------------------------------------------------- |
@@ -443,128 +443,128 @@ discard block |
||
| 443 | 443 | | or only one-time. |
| 444 | 444 | */ |
| 445 | 445 | |
| 446 | - /** |
|
| 447 | - * Prepares the submission's taxes. |
|
| 448 | - * |
|
| 449 | - * @since 1.0.19 |
|
| 450 | - */ |
|
| 451 | - public function process_taxes() { |
|
| 452 | - |
|
| 453 | - // Abort if we're not using taxes. |
|
| 454 | - if ( ! $this->use_taxes() ) { |
|
| 455 | - return; |
|
| 456 | - } |
|
| 457 | - |
|
| 458 | - // If a custom country && state has been passed in, use it to calculate taxes. |
|
| 459 | - $country = $this->get_field( 'wpinv_country', 'billing' ); |
|
| 460 | - if ( ! empty( $country ) ) { |
|
| 461 | - $this->country = $country; |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - $state = $this->get_field( 'wpinv_state', 'billing' ); |
|
| 465 | - if ( ! empty( $state ) ) { |
|
| 466 | - $this->state = $state; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - // Confirm if the provided country and the ip country are similar. |
|
| 470 | - $address_confirmed = $this->get_field( 'confirm-address' ); |
|
| 471 | - if ( wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
|
| 472 | - throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - // Abort if the country is not taxable. |
|
| 476 | - if ( ! wpinv_is_country_taxable( $this->country ) ) { |
|
| 477 | - return; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
|
| 481 | - |
|
| 482 | - foreach ( $processor->taxes as $tax ) { |
|
| 483 | - $this->add_tax( $tax ); |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * Adds a tax to the submission. |
|
| 491 | - * |
|
| 492 | - * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
|
| 493 | - * @since 1.0.19 |
|
| 494 | - */ |
|
| 495 | - public function add_tax( $tax ) { |
|
| 496 | - |
|
| 497 | - if ( wpinv_round_tax_per_tax_rate() ) { |
|
| 498 | - $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
|
| 499 | - $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - $this->taxes[ $tax['name'] ] = $tax; |
|
| 503 | - $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
| 504 | - $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
|
| 505 | - |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * Removes a specific tax. |
|
| 510 | - * |
|
| 511 | - * @since 1.0.19 |
|
| 512 | - */ |
|
| 513 | - public function remove_tax( $tax_name ) { |
|
| 514 | - |
|
| 515 | - if ( isset( $this->taxes[ $tax_name ] ) ) { |
|
| 516 | - $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
|
| 517 | - $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
|
| 518 | - unset( $this->taxes[ $tax_name ] ); |
|
| 519 | - } |
|
| 520 | - |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Whether or not we'll use taxes for the submission. |
|
| 525 | - * |
|
| 526 | - * @since 1.0.19 |
|
| 527 | - */ |
|
| 528 | - public function use_taxes() { |
|
| 529 | - |
|
| 530 | - $use_taxes = wpinv_use_taxes(); |
|
| 531 | - |
|
| 532 | - if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
|
| 533 | - $use_taxes = false; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
| 537 | - |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * Returns the tax. |
|
| 542 | - * |
|
| 543 | - * @since 1.0.19 |
|
| 544 | - */ |
|
| 545 | - public function get_tax() { |
|
| 546 | - return $this->totals['taxes']['initial']; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - /** |
|
| 550 | - * Returns the recurring tax. |
|
| 551 | - * |
|
| 552 | - * @since 1.0.19 |
|
| 553 | - */ |
|
| 554 | - public function get_recurring_tax() { |
|
| 555 | - return $this->totals['taxes']['recurring']; |
|
| 556 | - } |
|
| 557 | - |
|
| 558 | - /** |
|
| 559 | - * Returns all taxes. |
|
| 560 | - * |
|
| 561 | - * @since 1.0.19 |
|
| 562 | - */ |
|
| 563 | - public function get_taxes() { |
|
| 564 | - return $this->taxes; |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /* |
|
| 446 | + /** |
|
| 447 | + * Prepares the submission's taxes. |
|
| 448 | + * |
|
| 449 | + * @since 1.0.19 |
|
| 450 | + */ |
|
| 451 | + public function process_taxes() { |
|
| 452 | + |
|
| 453 | + // Abort if we're not using taxes. |
|
| 454 | + if ( ! $this->use_taxes() ) { |
|
| 455 | + return; |
|
| 456 | + } |
|
| 457 | + |
|
| 458 | + // If a custom country && state has been passed in, use it to calculate taxes. |
|
| 459 | + $country = $this->get_field( 'wpinv_country', 'billing' ); |
|
| 460 | + if ( ! empty( $country ) ) { |
|
| 461 | + $this->country = $country; |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + $state = $this->get_field( 'wpinv_state', 'billing' ); |
|
| 465 | + if ( ! empty( $state ) ) { |
|
| 466 | + $this->state = $state; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + // Confirm if the provided country and the ip country are similar. |
|
| 470 | + $address_confirmed = $this->get_field( 'confirm-address' ); |
|
| 471 | + if ( wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
|
| 472 | + throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + // Abort if the country is not taxable. |
|
| 476 | + if ( ! wpinv_is_country_taxable( $this->country ) ) { |
|
| 477 | + return; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
|
| 481 | + |
|
| 482 | + foreach ( $processor->taxes as $tax ) { |
|
| 483 | + $this->add_tax( $tax ); |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Adds a tax to the submission. |
|
| 491 | + * |
|
| 492 | + * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
|
| 493 | + * @since 1.0.19 |
|
| 494 | + */ |
|
| 495 | + public function add_tax( $tax ) { |
|
| 496 | + |
|
| 497 | + if ( wpinv_round_tax_per_tax_rate() ) { |
|
| 498 | + $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
|
| 499 | + $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + $this->taxes[ $tax['name'] ] = $tax; |
|
| 503 | + $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
| 504 | + $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
|
| 505 | + |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * Removes a specific tax. |
|
| 510 | + * |
|
| 511 | + * @since 1.0.19 |
|
| 512 | + */ |
|
| 513 | + public function remove_tax( $tax_name ) { |
|
| 514 | + |
|
| 515 | + if ( isset( $this->taxes[ $tax_name ] ) ) { |
|
| 516 | + $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
|
| 517 | + $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
|
| 518 | + unset( $this->taxes[ $tax_name ] ); |
|
| 519 | + } |
|
| 520 | + |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Whether or not we'll use taxes for the submission. |
|
| 525 | + * |
|
| 526 | + * @since 1.0.19 |
|
| 527 | + */ |
|
| 528 | + public function use_taxes() { |
|
| 529 | + |
|
| 530 | + $use_taxes = wpinv_use_taxes(); |
|
| 531 | + |
|
| 532 | + if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
|
| 533 | + $use_taxes = false; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
| 537 | + |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * Returns the tax. |
|
| 542 | + * |
|
| 543 | + * @since 1.0.19 |
|
| 544 | + */ |
|
| 545 | + public function get_tax() { |
|
| 546 | + return $this->totals['taxes']['initial']; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + /** |
|
| 550 | + * Returns the recurring tax. |
|
| 551 | + * |
|
| 552 | + * @since 1.0.19 |
|
| 553 | + */ |
|
| 554 | + public function get_recurring_tax() { |
|
| 555 | + return $this->totals['taxes']['recurring']; |
|
| 556 | + } |
|
| 557 | + |
|
| 558 | + /** |
|
| 559 | + * Returns all taxes. |
|
| 560 | + * |
|
| 561 | + * @since 1.0.19 |
|
| 562 | + */ |
|
| 563 | + public function get_taxes() { |
|
| 564 | + return $this->taxes; |
|
| 565 | + } |
|
| 566 | + |
|
| 567 | + /* |
|
| 568 | 568 | |-------------------------------------------------------------------------- |
| 569 | 569 | | Discounts |
| 570 | 570 | |-------------------------------------------------------------------------- |
@@ -573,99 +573,99 @@ discard block |
||
| 573 | 573 | | or only one-time. They also do not have to come from a discount code. |
| 574 | 574 | */ |
| 575 | 575 | |
| 576 | - /** |
|
| 577 | - * Prepares the submission's discount. |
|
| 578 | - * |
|
| 579 | - * @since 1.0.19 |
|
| 580 | - */ |
|
| 581 | - public function process_discount() { |
|
| 582 | - |
|
| 583 | - $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
|
| 584 | - $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
|
| 585 | - $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
|
| 586 | - |
|
| 587 | - foreach ( $processor->discounts as $discount ) { |
|
| 588 | - $this->add_discount( $discount ); |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * Adds a discount to the submission. |
|
| 596 | - * |
|
| 597 | - * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
|
| 598 | - * @since 1.0.19 |
|
| 599 | - */ |
|
| 600 | - public function add_discount( $discount ) { |
|
| 601 | - $this->discounts[ $discount['name'] ] = $discount; |
|
| 602 | - $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
|
| 603 | - $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * Removes a discount from the submission. |
|
| 608 | - * |
|
| 609 | - * @since 1.0.19 |
|
| 610 | - */ |
|
| 611 | - public function remove_discount( $name ) { |
|
| 612 | - |
|
| 613 | - if ( isset( $this->discounts[ $name ] ) ) { |
|
| 614 | - $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
|
| 615 | - $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
|
| 616 | - unset( $this->discounts[ $name ] ); |
|
| 617 | - } |
|
| 618 | - |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - /** |
|
| 622 | - * Checks whether there is a discount code associated with this submission. |
|
| 623 | - * |
|
| 624 | - * @since 1.0.19 |
|
| 625 | - * @return bool |
|
| 626 | - */ |
|
| 627 | - public function has_discount_code() { |
|
| 628 | - return ! empty( $this->discounts['discount_code'] ); |
|
| 629 | - } |
|
| 630 | - |
|
| 631 | - /** |
|
| 632 | - * Returns the discount code. |
|
| 633 | - * |
|
| 634 | - * @since 1.0.19 |
|
| 635 | - * @return string |
|
| 636 | - */ |
|
| 637 | - public function get_discount_code() { |
|
| 638 | - return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : ''; |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - /** |
|
| 642 | - * Returns the discount. |
|
| 643 | - * |
|
| 644 | - * @since 1.0.19 |
|
| 645 | - */ |
|
| 646 | - public function get_discount() { |
|
| 647 | - return $this->totals['discount']['initial']; |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - /** |
|
| 651 | - * Returns the recurring discount. |
|
| 652 | - * |
|
| 653 | - * @since 1.0.19 |
|
| 654 | - */ |
|
| 655 | - public function get_recurring_discount() { |
|
| 656 | - return $this->totals['discount']['recurring']; |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - /** |
|
| 660 | - * Returns all discounts. |
|
| 661 | - * |
|
| 662 | - * @since 1.0.19 |
|
| 663 | - */ |
|
| 664 | - public function get_discounts() { |
|
| 665 | - return $this->discounts; |
|
| 666 | - } |
|
| 667 | - |
|
| 668 | - /* |
|
| 576 | + /** |
|
| 577 | + * Prepares the submission's discount. |
|
| 578 | + * |
|
| 579 | + * @since 1.0.19 |
|
| 580 | + */ |
|
| 581 | + public function process_discount() { |
|
| 582 | + |
|
| 583 | + $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
|
| 584 | + $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
|
| 585 | + $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
|
| 586 | + |
|
| 587 | + foreach ( $processor->discounts as $discount ) { |
|
| 588 | + $this->add_discount( $discount ); |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * Adds a discount to the submission. |
|
| 596 | + * |
|
| 597 | + * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
|
| 598 | + * @since 1.0.19 |
|
| 599 | + */ |
|
| 600 | + public function add_discount( $discount ) { |
|
| 601 | + $this->discounts[ $discount['name'] ] = $discount; |
|
| 602 | + $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
|
| 603 | + $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * Removes a discount from the submission. |
|
| 608 | + * |
|
| 609 | + * @since 1.0.19 |
|
| 610 | + */ |
|
| 611 | + public function remove_discount( $name ) { |
|
| 612 | + |
|
| 613 | + if ( isset( $this->discounts[ $name ] ) ) { |
|
| 614 | + $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
|
| 615 | + $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
|
| 616 | + unset( $this->discounts[ $name ] ); |
|
| 617 | + } |
|
| 618 | + |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + /** |
|
| 622 | + * Checks whether there is a discount code associated with this submission. |
|
| 623 | + * |
|
| 624 | + * @since 1.0.19 |
|
| 625 | + * @return bool |
|
| 626 | + */ |
|
| 627 | + public function has_discount_code() { |
|
| 628 | + return ! empty( $this->discounts['discount_code'] ); |
|
| 629 | + } |
|
| 630 | + |
|
| 631 | + /** |
|
| 632 | + * Returns the discount code. |
|
| 633 | + * |
|
| 634 | + * @since 1.0.19 |
|
| 635 | + * @return string |
|
| 636 | + */ |
|
| 637 | + public function get_discount_code() { |
|
| 638 | + return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : ''; |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + /** |
|
| 642 | + * Returns the discount. |
|
| 643 | + * |
|
| 644 | + * @since 1.0.19 |
|
| 645 | + */ |
|
| 646 | + public function get_discount() { |
|
| 647 | + return $this->totals['discount']['initial']; |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + /** |
|
| 651 | + * Returns the recurring discount. |
|
| 652 | + * |
|
| 653 | + * @since 1.0.19 |
|
| 654 | + */ |
|
| 655 | + public function get_recurring_discount() { |
|
| 656 | + return $this->totals['discount']['recurring']; |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * Returns all discounts. |
|
| 661 | + * |
|
| 662 | + * @since 1.0.19 |
|
| 663 | + */ |
|
| 664 | + public function get_discounts() { |
|
| 665 | + return $this->discounts; |
|
| 666 | + } |
|
| 667 | + |
|
| 668 | + /* |
|
| 669 | 669 | |-------------------------------------------------------------------------- |
| 670 | 670 | | Fees |
| 671 | 671 | |-------------------------------------------------------------------------- |
@@ -675,89 +675,89 @@ discard block |
||
| 675 | 675 | | fees. |
| 676 | 676 | */ |
| 677 | 677 | |
| 678 | - /** |
|
| 679 | - * Prepares the submission's fees. |
|
| 680 | - * |
|
| 681 | - * @since 1.0.19 |
|
| 682 | - */ |
|
| 683 | - public function process_fees() { |
|
| 684 | - |
|
| 685 | - $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
|
| 686 | - |
|
| 687 | - foreach ( $fees_processor->fees as $fee ) { |
|
| 688 | - $this->add_fee( $fee ); |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
|
| 692 | - } |
|
| 693 | - |
|
| 694 | - /** |
|
| 695 | - * Adds a fee to the submission. |
|
| 696 | - * |
|
| 697 | - * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
|
| 698 | - * @since 1.0.19 |
|
| 699 | - */ |
|
| 700 | - public function add_fee( $fee ) { |
|
| 701 | - |
|
| 702 | - $this->fees[ $fee['name'] ] = $fee; |
|
| 703 | - $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
| 704 | - $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
| 705 | - |
|
| 706 | - } |
|
| 707 | - |
|
| 708 | - /** |
|
| 709 | - * Removes a fee from the submission. |
|
| 710 | - * |
|
| 711 | - * @since 1.0.19 |
|
| 712 | - */ |
|
| 713 | - public function remove_fee( $name ) { |
|
| 714 | - |
|
| 715 | - if ( isset( $this->fees[ $name ] ) ) { |
|
| 716 | - $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
|
| 717 | - $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
|
| 718 | - unset( $this->fees[ $name ] ); |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - /** |
|
| 724 | - * Returns the fees. |
|
| 725 | - * |
|
| 726 | - * @since 1.0.19 |
|
| 727 | - */ |
|
| 728 | - public function get_fee() { |
|
| 729 | - return $this->totals['fees']['initial']; |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - /** |
|
| 733 | - * Returns the recurring fees. |
|
| 734 | - * |
|
| 735 | - * @since 1.0.19 |
|
| 736 | - */ |
|
| 737 | - public function get_recurring_fee() { |
|
| 738 | - return $this->totals['fees']['recurring']; |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - /** |
|
| 742 | - * Returns all fees. |
|
| 743 | - * |
|
| 744 | - * @since 1.0.19 |
|
| 745 | - */ |
|
| 746 | - public function get_fees() { |
|
| 747 | - return $this->fees; |
|
| 748 | - } |
|
| 749 | - |
|
| 750 | - /** |
|
| 751 | - * Checks if there are any fees for the form. |
|
| 752 | - * |
|
| 753 | - * @return bool |
|
| 754 | - * @since 1.0.19 |
|
| 755 | - */ |
|
| 756 | - public function has_fees() { |
|
| 757 | - return count( $this->fees ) !== 0; |
|
| 758 | - } |
|
| 759 | - |
|
| 760 | - /* |
|
| 678 | + /** |
|
| 679 | + * Prepares the submission's fees. |
|
| 680 | + * |
|
| 681 | + * @since 1.0.19 |
|
| 682 | + */ |
|
| 683 | + public function process_fees() { |
|
| 684 | + |
|
| 685 | + $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
|
| 686 | + |
|
| 687 | + foreach ( $fees_processor->fees as $fee ) { |
|
| 688 | + $this->add_fee( $fee ); |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
|
| 692 | + } |
|
| 693 | + |
|
| 694 | + /** |
|
| 695 | + * Adds a fee to the submission. |
|
| 696 | + * |
|
| 697 | + * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
|
| 698 | + * @since 1.0.19 |
|
| 699 | + */ |
|
| 700 | + public function add_fee( $fee ) { |
|
| 701 | + |
|
| 702 | + $this->fees[ $fee['name'] ] = $fee; |
|
| 703 | + $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
| 704 | + $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
| 705 | + |
|
| 706 | + } |
|
| 707 | + |
|
| 708 | + /** |
|
| 709 | + * Removes a fee from the submission. |
|
| 710 | + * |
|
| 711 | + * @since 1.0.19 |
|
| 712 | + */ |
|
| 713 | + public function remove_fee( $name ) { |
|
| 714 | + |
|
| 715 | + if ( isset( $this->fees[ $name ] ) ) { |
|
| 716 | + $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
|
| 717 | + $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
|
| 718 | + unset( $this->fees[ $name ] ); |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + /** |
|
| 724 | + * Returns the fees. |
|
| 725 | + * |
|
| 726 | + * @since 1.0.19 |
|
| 727 | + */ |
|
| 728 | + public function get_fee() { |
|
| 729 | + return $this->totals['fees']['initial']; |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + /** |
|
| 733 | + * Returns the recurring fees. |
|
| 734 | + * |
|
| 735 | + * @since 1.0.19 |
|
| 736 | + */ |
|
| 737 | + public function get_recurring_fee() { |
|
| 738 | + return $this->totals['fees']['recurring']; |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + /** |
|
| 742 | + * Returns all fees. |
|
| 743 | + * |
|
| 744 | + * @since 1.0.19 |
|
| 745 | + */ |
|
| 746 | + public function get_fees() { |
|
| 747 | + return $this->fees; |
|
| 748 | + } |
|
| 749 | + |
|
| 750 | + /** |
|
| 751 | + * Checks if there are any fees for the form. |
|
| 752 | + * |
|
| 753 | + * @return bool |
|
| 754 | + * @since 1.0.19 |
|
| 755 | + */ |
|
| 756 | + public function has_fees() { |
|
| 757 | + return count( $this->fees ) !== 0; |
|
| 758 | + } |
|
| 759 | + |
|
| 760 | + /* |
|
| 761 | 761 | |-------------------------------------------------------------------------- |
| 762 | 762 | | MISC |
| 763 | 763 | |-------------------------------------------------------------------------- |
@@ -765,119 +765,119 @@ discard block |
||
| 765 | 765 | | Extra submission functions. |
| 766 | 766 | */ |
| 767 | 767 | |
| 768 | - /** |
|
| 769 | - * Checks if this is the initial fetch. |
|
| 770 | - * |
|
| 771 | - * @return bool |
|
| 772 | - * @since 1.0.19 |
|
| 773 | - */ |
|
| 774 | - public function is_initial_fetch() { |
|
| 775 | - return empty( $this->data['initial_state'] ); |
|
| 776 | - } |
|
| 777 | - |
|
| 778 | - /** |
|
| 779 | - * Returns the total amount to collect for this submission. |
|
| 780 | - * |
|
| 781 | - * @since 1.0.19 |
|
| 782 | - */ |
|
| 783 | - public function get_total() { |
|
| 784 | - $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount(); |
|
| 785 | - return max( $total, 0 ); |
|
| 786 | - } |
|
| 787 | - |
|
| 788 | - /** |
|
| 789 | - * Returns the recurring total amount to collect for this submission. |
|
| 790 | - * |
|
| 791 | - * @since 1.0.19 |
|
| 792 | - */ |
|
| 793 | - public function get_recurring_total() { |
|
| 794 | - $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount(); |
|
| 795 | - return max( $total, 0 ); |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - /** |
|
| 799 | - * Whether payment details should be collected for this submission. |
|
| 800 | - * |
|
| 801 | - * @since 1.0.19 |
|
| 802 | - */ |
|
| 803 | - public function should_collect_payment_details() { |
|
| 804 | - $initial = $this->get_total(); |
|
| 805 | - $recurring = $this->get_recurring_total(); |
|
| 806 | - |
|
| 807 | - if ( $this->has_recurring == 0 ) { |
|
| 808 | - $recurring = 0; |
|
| 809 | - } |
|
| 810 | - |
|
| 811 | - $collect = $initial > 0 || $recurring > 0; |
|
| 812 | - return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
|
| 813 | - } |
|
| 814 | - |
|
| 815 | - /** |
|
| 816 | - * Returns the billing email of the user. |
|
| 817 | - * |
|
| 818 | - * @since 1.0.19 |
|
| 819 | - */ |
|
| 820 | - public function get_billing_email() { |
|
| 821 | - return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
|
| 822 | - } |
|
| 823 | - |
|
| 824 | - /** |
|
| 825 | - * Checks if the submitter has a billing email. |
|
| 826 | - * |
|
| 827 | - * @since 1.0.19 |
|
| 828 | - */ |
|
| 829 | - public function has_billing_email() { |
|
| 830 | - $billing_email = $this->get_billing_email(); |
|
| 831 | - return ! empty( $billing_email ) && is_email( $billing_email ); |
|
| 832 | - } |
|
| 833 | - |
|
| 834 | - /** |
|
| 835 | - * Returns the appropriate currency for the submission. |
|
| 836 | - * |
|
| 837 | - * @since 1.0.19 |
|
| 838 | - * @return string |
|
| 839 | - */ |
|
| 840 | - public function get_currency() { |
|
| 841 | - return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency(); |
|
| 842 | - } |
|
| 843 | - |
|
| 844 | - /** |
|
| 845 | - * Returns the raw submission data. |
|
| 846 | - * |
|
| 847 | - * @since 1.0.19 |
|
| 848 | - * @return array |
|
| 849 | - */ |
|
| 850 | - public function get_data() { |
|
| 851 | - return $this->data; |
|
| 852 | - } |
|
| 853 | - |
|
| 854 | - /** |
|
| 855 | - * Returns a field from the submission data |
|
| 856 | - * |
|
| 857 | - * @param string $field |
|
| 858 | - * @since 1.0.19 |
|
| 859 | - * @return mixed|null |
|
| 860 | - */ |
|
| 861 | - public function get_field( $field, $sub_array_key = null ) { |
|
| 862 | - return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
|
| 863 | - } |
|
| 864 | - |
|
| 865 | - /** |
|
| 866 | - * Checks if a required field is set. |
|
| 867 | - * |
|
| 868 | - * @since 1.0.19 |
|
| 869 | - */ |
|
| 870 | - public function is_required_field_set( $field ) { |
|
| 871 | - return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
| 872 | - } |
|
| 873 | - |
|
| 874 | - /** |
|
| 875 | - * Formats an amount |
|
| 876 | - * |
|
| 877 | - * @since 1.0.19 |
|
| 878 | - */ |
|
| 879 | - public function format_amount( $amount ) { |
|
| 880 | - return wpinv_price( $amount, $this->get_currency() ); |
|
| 881 | - } |
|
| 768 | + /** |
|
| 769 | + * Checks if this is the initial fetch. |
|
| 770 | + * |
|
| 771 | + * @return bool |
|
| 772 | + * @since 1.0.19 |
|
| 773 | + */ |
|
| 774 | + public function is_initial_fetch() { |
|
| 775 | + return empty( $this->data['initial_state'] ); |
|
| 776 | + } |
|
| 777 | + |
|
| 778 | + /** |
|
| 779 | + * Returns the total amount to collect for this submission. |
|
| 780 | + * |
|
| 781 | + * @since 1.0.19 |
|
| 782 | + */ |
|
| 783 | + public function get_total() { |
|
| 784 | + $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount(); |
|
| 785 | + return max( $total, 0 ); |
|
| 786 | + } |
|
| 787 | + |
|
| 788 | + /** |
|
| 789 | + * Returns the recurring total amount to collect for this submission. |
|
| 790 | + * |
|
| 791 | + * @since 1.0.19 |
|
| 792 | + */ |
|
| 793 | + public function get_recurring_total() { |
|
| 794 | + $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount(); |
|
| 795 | + return max( $total, 0 ); |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + /** |
|
| 799 | + * Whether payment details should be collected for this submission. |
|
| 800 | + * |
|
| 801 | + * @since 1.0.19 |
|
| 802 | + */ |
|
| 803 | + public function should_collect_payment_details() { |
|
| 804 | + $initial = $this->get_total(); |
|
| 805 | + $recurring = $this->get_recurring_total(); |
|
| 806 | + |
|
| 807 | + if ( $this->has_recurring == 0 ) { |
|
| 808 | + $recurring = 0; |
|
| 809 | + } |
|
| 810 | + |
|
| 811 | + $collect = $initial > 0 || $recurring > 0; |
|
| 812 | + return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
|
| 813 | + } |
|
| 814 | + |
|
| 815 | + /** |
|
| 816 | + * Returns the billing email of the user. |
|
| 817 | + * |
|
| 818 | + * @since 1.0.19 |
|
| 819 | + */ |
|
| 820 | + public function get_billing_email() { |
|
| 821 | + return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + /** |
|
| 825 | + * Checks if the submitter has a billing email. |
|
| 826 | + * |
|
| 827 | + * @since 1.0.19 |
|
| 828 | + */ |
|
| 829 | + public function has_billing_email() { |
|
| 830 | + $billing_email = $this->get_billing_email(); |
|
| 831 | + return ! empty( $billing_email ) && is_email( $billing_email ); |
|
| 832 | + } |
|
| 833 | + |
|
| 834 | + /** |
|
| 835 | + * Returns the appropriate currency for the submission. |
|
| 836 | + * |
|
| 837 | + * @since 1.0.19 |
|
| 838 | + * @return string |
|
| 839 | + */ |
|
| 840 | + public function get_currency() { |
|
| 841 | + return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency(); |
|
| 842 | + } |
|
| 843 | + |
|
| 844 | + /** |
|
| 845 | + * Returns the raw submission data. |
|
| 846 | + * |
|
| 847 | + * @since 1.0.19 |
|
| 848 | + * @return array |
|
| 849 | + */ |
|
| 850 | + public function get_data() { |
|
| 851 | + return $this->data; |
|
| 852 | + } |
|
| 853 | + |
|
| 854 | + /** |
|
| 855 | + * Returns a field from the submission data |
|
| 856 | + * |
|
| 857 | + * @param string $field |
|
| 858 | + * @since 1.0.19 |
|
| 859 | + * @return mixed|null |
|
| 860 | + */ |
|
| 861 | + public function get_field( $field, $sub_array_key = null ) { |
|
| 862 | + return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
|
| 863 | + } |
|
| 864 | + |
|
| 865 | + /** |
|
| 866 | + * Checks if a required field is set. |
|
| 867 | + * |
|
| 868 | + * @since 1.0.19 |
|
| 869 | + */ |
|
| 870 | + public function is_required_field_set( $field ) { |
|
| 871 | + return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
| 872 | + } |
|
| 873 | + |
|
| 874 | + /** |
|
| 875 | + * Formats an amount |
|
| 876 | + * |
|
| 877 | + * @since 1.0.19 |
|
| 878 | + */ |
|
| 879 | + public function format_amount( $amount ) { |
|
| 880 | + return wpinv_price( $amount, $this->get_currency() ); |
|
| 881 | + } |
|
| 882 | 882 | |
| 883 | 883 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 2 | +if (!defined('ABSPATH')) { |
|
| 3 | 3 | exit; |
| 4 | 4 | } |
| 5 | 5 | |
@@ -134,8 +134,8 @@ discard block |
||
| 134 | 134 | $this->state = wpinv_get_default_state(); |
| 135 | 135 | |
| 136 | 136 | // Do we have an actual submission? |
| 137 | - if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
| 138 | - $this->load_data( $_POST ); |
|
| 137 | + if (isset($_POST['getpaid_payment_form_submission'])) { |
|
| 138 | + $this->load_data($_POST); |
|
| 139 | 139 | } |
| 140 | 140 | |
| 141 | 141 | } |
@@ -145,19 +145,19 @@ discard block |
||
| 145 | 145 | * |
| 146 | 146 | * @param array $data |
| 147 | 147 | */ |
| 148 | - public function load_data( $data ) { |
|
| 148 | + public function load_data($data) { |
|
| 149 | 149 | |
| 150 | 150 | // Remove slashes from the submitted data... |
| 151 | - $data = wp_unslash( $data ); |
|
| 151 | + $data = wp_unslash($data); |
|
| 152 | 152 | |
| 153 | 153 | // Allow plugins to filter the data. |
| 154 | - $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
| 154 | + $data = apply_filters('getpaid_submission_data', $data, $this); |
|
| 155 | 155 | |
| 156 | 156 | // Cache it... |
| 157 | 157 | $this->data = $data; |
| 158 | 158 | |
| 159 | 159 | // Then generate a unique id from the data. |
| 160 | - $this->id = md5( wp_json_encode( $data ) ); |
|
| 160 | + $this->id = md5(wp_json_encode($data)); |
|
| 161 | 161 | |
| 162 | 162 | // Finally, process the submission. |
| 163 | 163 | try { |
@@ -167,26 +167,26 @@ discard block |
||
| 167 | 167 | $processors = apply_filters( |
| 168 | 168 | 'getpaid_payment_form_submission_processors', |
| 169 | 169 | array( |
| 170 | - array( $this, 'process_payment_form' ), |
|
| 171 | - array( $this, 'process_invoice' ), |
|
| 172 | - array( $this, 'process_fees' ), |
|
| 173 | - array( $this, 'process_items' ), |
|
| 174 | - array( $this, 'process_discount' ), |
|
| 175 | - array( $this, 'process_taxes' ), |
|
| 170 | + array($this, 'process_payment_form'), |
|
| 171 | + array($this, 'process_invoice'), |
|
| 172 | + array($this, 'process_fees'), |
|
| 173 | + array($this, 'process_items'), |
|
| 174 | + array($this, 'process_discount'), |
|
| 175 | + array($this, 'process_taxes'), |
|
| 176 | 176 | ), |
| 177 | 177 | $this |
| 178 | 178 | ); |
| 179 | 179 | |
| 180 | - foreach ( $processors as $processor ) { |
|
| 181 | - call_user_func_array( $processor, array( &$this ) ); |
|
| 180 | + foreach ($processors as $processor) { |
|
| 181 | + call_user_func_array($processor, array(&$this)); |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | - } catch ( Exception $e ) { |
|
| 184 | + } catch (Exception $e) { |
|
| 185 | 185 | $this->last_error = $e->getMessage(); |
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | // Fired when we are done processing a submission. |
| 189 | - do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
|
| 189 | + do_action_ref_array('getpaid_process_submission', array(&$this)); |
|
| 190 | 190 | |
| 191 | 191 | } |
| 192 | 192 | |
@@ -207,18 +207,18 @@ discard block |
||
| 207 | 207 | public function process_payment_form() { |
| 208 | 208 | |
| 209 | 209 | // Every submission needs an active payment form. |
| 210 | - if ( empty( $this->data['form_id'] ) ) { |
|
| 211 | - throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
|
| 210 | + if (empty($this->data['form_id'])) { |
|
| 211 | + throw new Exception(__('Missing payment form', 'invoicing')); |
|
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | // Fetch the payment form. |
| 215 | - $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
| 215 | + $this->payment_form = new GetPaid_Payment_Form($this->data['form_id']); |
|
| 216 | 216 | |
| 217 | - if ( ! $this->payment_form->is_active() ) { |
|
| 218 | - throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
| 217 | + if (!$this->payment_form->is_active()) { |
|
| 218 | + throw new Exception(__('Payment form not active', 'invoicing')); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | - do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
| 221 | + do_action_ref_array('getpaid_submissions_process_payment_form', array(&$this)); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | /** |
@@ -248,53 +248,53 @@ discard block |
||
| 248 | 248 | public function process_invoice() { |
| 249 | 249 | |
| 250 | 250 | // Abort if there is no invoice. |
| 251 | - if ( empty( $this->data['invoice_id'] ) ) { |
|
| 251 | + if (empty($this->data['invoice_id'])) { |
|
| 252 | 252 | return; |
| 253 | 253 | } |
| 254 | 254 | |
| 255 | 255 | // If the submission is for an existing invoice, ensure that it exists |
| 256 | 256 | // and that it is not paid for. |
| 257 | - $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
|
| 257 | + $invoice = wpinv_get_invoice($this->data['invoice_id']); |
|
| 258 | 258 | |
| 259 | - if ( empty( $invoice ) ) { |
|
| 260 | - throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
| 259 | + if (empty($invoice)) { |
|
| 260 | + throw new Exception(__('Invalid invoice', 'invoicing')); |
|
| 261 | 261 | } |
| 262 | 262 | |
| 263 | - if ( $invoice->is_paid() ) { |
|
| 264 | - throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
| 263 | + if ($invoice->is_paid()) { |
|
| 264 | + throw new Exception(__('This invoice is already paid for.', 'invoicing')); |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | $this->payment_form->invoice = $invoice; |
| 268 | - if ( ! $this->payment_form->is_default() ) { |
|
| 268 | + if (!$this->payment_form->is_default()) { |
|
| 269 | 269 | |
| 270 | 270 | $items = array(); |
| 271 | 271 | $item_ids = array(); |
| 272 | 272 | |
| 273 | - foreach ( $invoice->get_items() as $item ) { |
|
| 274 | - if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 273 | + foreach ($invoice->get_items() as $item) { |
|
| 274 | + if (!in_array($item->get_id(), $item_ids)) { |
|
| 275 | 275 | $item_ids[] = $item->get_id(); |
| 276 | 276 | $items[] = $item; |
| 277 | 277 | } |
| 278 | 278 | } |
| 279 | 279 | |
| 280 | - foreach ( $this->payment_form->get_items() as $item ) { |
|
| 281 | - if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
| 280 | + foreach ($this->payment_form->get_items() as $item) { |
|
| 281 | + if (!in_array($item->get_id(), $item_ids)) { |
|
| 282 | 282 | $item_ids[] = $item->get_id(); |
| 283 | 283 | $items[] = $item; |
| 284 | 284 | } |
| 285 | 285 | } |
| 286 | 286 | |
| 287 | - $this->payment_form->set_items( $items ); |
|
| 287 | + $this->payment_form->set_items($items); |
|
| 288 | 288 | |
| 289 | 289 | } else { |
| 290 | - $this->payment_form->set_items( $invoice->get_items() ); |
|
| 290 | + $this->payment_form->set_items($invoice->get_items()); |
|
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | $this->country = $invoice->get_country(); |
| 294 | 294 | $this->state = $invoice->get_state(); |
| 295 | 295 | $this->invoice = $invoice; |
| 296 | 296 | |
| 297 | - do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
|
| 297 | + do_action_ref_array('getpaid_submissions_process_invoice', array(&$this)); |
|
| 298 | 298 | } |
| 299 | 299 | |
| 300 | 300 | /** |
@@ -314,7 +314,7 @@ discard block |
||
| 314 | 314 | * @return bool |
| 315 | 315 | */ |
| 316 | 316 | public function has_invoice() { |
| 317 | - return ! empty( $this->invoice ); |
|
| 317 | + return !empty($this->invoice); |
|
| 318 | 318 | } |
| 319 | 319 | |
| 320 | 320 | /* |
@@ -333,13 +333,13 @@ discard block |
||
| 333 | 333 | */ |
| 334 | 334 | public function process_items() { |
| 335 | 335 | |
| 336 | - $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
| 336 | + $processor = new GetPaid_Payment_Form_Submission_Items($this); |
|
| 337 | 337 | |
| 338 | - foreach ( $processor->items as $item ) { |
|
| 339 | - $this->add_item( $item ); |
|
| 338 | + foreach ($processor->items as $item) { |
|
| 339 | + $this->add_item($item); |
|
| 340 | 340 | } |
| 341 | 341 | |
| 342 | - do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
| 342 | + do_action_ref_array('getpaid_submissions_process_items', array(&$this)); |
|
| 343 | 343 | } |
| 344 | 344 | |
| 345 | 345 | /** |
@@ -348,18 +348,18 @@ discard block |
||
| 348 | 348 | * @since 1.0.19 |
| 349 | 349 | * @param GetPaid_Form_Item $item |
| 350 | 350 | */ |
| 351 | - public function add_item( $item ) { |
|
| 351 | + public function add_item($item) { |
|
| 352 | 352 | |
| 353 | 353 | // Make sure that it is available for purchase. |
| 354 | - if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
|
| 354 | + if (!$item->can_purchase() || isset($this->items[$item->get_id()])) { |
|
| 355 | 355 | return; |
| 356 | 356 | } |
| 357 | 357 | |
| 358 | 358 | // Each submission can only contain one recurring item. |
| 359 | - if ( $item->is_recurring() ) { |
|
| 359 | + if ($item->is_recurring()) { |
|
| 360 | 360 | |
| 361 | - if ( $this->has_recurring != 0 ) { |
|
| 362 | - throw new Exception( __( 'You can only buy one recurring item at a time.', 'invoicing' ) ); |
|
| 361 | + if ($this->has_recurring != 0) { |
|
| 362 | + throw new Exception(__('You can only buy one recurring item at a time.', 'invoicing')); |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | $this->has_recurring = $item->get_id(); |
@@ -367,7 +367,7 @@ discard block |
||
| 367 | 367 | } |
| 368 | 368 | |
| 369 | 369 | // Update the items and totals. |
| 370 | - $this->items[ $item->get_id() ] = $item; |
|
| 370 | + $this->items[$item->get_id()] = $item; |
|
| 371 | 371 | $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
| 372 | 372 | $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
| 373 | 373 | |
@@ -381,17 +381,17 @@ discard block |
||
| 381 | 381 | * |
| 382 | 382 | * @since 1.0.19 |
| 383 | 383 | */ |
| 384 | - public function remove_item( $item_id ) { |
|
| 384 | + public function remove_item($item_id) { |
|
| 385 | 385 | |
| 386 | - if ( isset( $this->items[ $item_id ] ) ) { |
|
| 387 | - $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
|
| 388 | - $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
|
| 386 | + if (isset($this->items[$item_id])) { |
|
| 387 | + $this->totals['subtotal']['initial'] -= $this->items[$item_id]->get_sub_total(); |
|
| 388 | + $this->totals['subtotal']['recurring'] -= $this->items[$item_id]->get_recurring_sub_total(); |
|
| 389 | 389 | |
| 390 | - if ( $this->items[ $item_id ]->is_recurring() ) { |
|
| 390 | + if ($this->items[$item_id]->is_recurring()) { |
|
| 391 | 391 | $this->has_recurring = 0; |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | - unset( $this->items[ $item_id ] ); |
|
| 394 | + unset($this->items[$item_id]); |
|
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | } |
@@ -403,7 +403,7 @@ discard block |
||
| 403 | 403 | */ |
| 404 | 404 | public function get_subtotal() { |
| 405 | 405 | |
| 406 | - if ( wpinv_prices_include_tax() ) { |
|
| 406 | + if (wpinv_prices_include_tax()) { |
|
| 407 | 407 | return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
| 408 | 408 | } |
| 409 | 409 | |
@@ -417,7 +417,7 @@ discard block |
||
| 417 | 417 | */ |
| 418 | 418 | public function get_recurring_subtotal() { |
| 419 | 419 | |
| 420 | - if ( wpinv_prices_include_tax() ) { |
|
| 420 | + if (wpinv_prices_include_tax()) { |
|
| 421 | 421 | return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
| 422 | 422 | } |
| 423 | 423 | |
@@ -451,39 +451,39 @@ discard block |
||
| 451 | 451 | public function process_taxes() { |
| 452 | 452 | |
| 453 | 453 | // Abort if we're not using taxes. |
| 454 | - if ( ! $this->use_taxes() ) { |
|
| 454 | + if (!$this->use_taxes()) { |
|
| 455 | 455 | return; |
| 456 | 456 | } |
| 457 | 457 | |
| 458 | 458 | // If a custom country && state has been passed in, use it to calculate taxes. |
| 459 | - $country = $this->get_field( 'wpinv_country', 'billing' ); |
|
| 460 | - if ( ! empty( $country ) ) { |
|
| 459 | + $country = $this->get_field('wpinv_country', 'billing'); |
|
| 460 | + if (!empty($country)) { |
|
| 461 | 461 | $this->country = $country; |
| 462 | 462 | } |
| 463 | 463 | |
| 464 | - $state = $this->get_field( 'wpinv_state', 'billing' ); |
|
| 465 | - if ( ! empty( $state ) ) { |
|
| 464 | + $state = $this->get_field('wpinv_state', 'billing'); |
|
| 465 | + if (!empty($state)) { |
|
| 466 | 466 | $this->state = $state; |
| 467 | 467 | } |
| 468 | 468 | |
| 469 | 469 | // Confirm if the provided country and the ip country are similar. |
| 470 | - $address_confirmed = $this->get_field( 'confirm-address' ); |
|
| 471 | - if ( wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
|
| 472 | - throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
|
| 470 | + $address_confirmed = $this->get_field('confirm-address'); |
|
| 471 | + if (wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty($address_confirmed)) { |
|
| 472 | + throw new Exception(__('The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing')); |
|
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | // Abort if the country is not taxable. |
| 476 | - if ( ! wpinv_is_country_taxable( $this->country ) ) { |
|
| 476 | + if (!wpinv_is_country_taxable($this->country)) { |
|
| 477 | 477 | return; |
| 478 | 478 | } |
| 479 | 479 | |
| 480 | - $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
|
| 480 | + $processor = new GetPaid_Payment_Form_Submission_Taxes($this); |
|
| 481 | 481 | |
| 482 | - foreach ( $processor->taxes as $tax ) { |
|
| 483 | - $this->add_tax( $tax ); |
|
| 482 | + foreach ($processor->taxes as $tax) { |
|
| 483 | + $this->add_tax($tax); |
|
| 484 | 484 | } |
| 485 | 485 | |
| 486 | - do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
|
| 486 | + do_action_ref_array('getpaid_submissions_process_taxes', array(&$this)); |
|
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | /** |
@@ -492,16 +492,16 @@ discard block |
||
| 492 | 492 | * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
| 493 | 493 | * @since 1.0.19 |
| 494 | 494 | */ |
| 495 | - public function add_tax( $tax ) { |
|
| 495 | + public function add_tax($tax) { |
|
| 496 | 496 | |
| 497 | - if ( wpinv_round_tax_per_tax_rate() ) { |
|
| 498 | - $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
|
| 499 | - $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
|
| 497 | + if (wpinv_round_tax_per_tax_rate()) { |
|
| 498 | + $tax['initial_tax'] = wpinv_round_amount($tax['initial_tax']); |
|
| 499 | + $tax['recurring_tax'] = wpinv_round_amount($tax['recurring_tax']); |
|
| 500 | 500 | } |
| 501 | 501 | |
| 502 | - $this->taxes[ $tax['name'] ] = $tax; |
|
| 503 | - $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
| 504 | - $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
|
| 502 | + $this->taxes[$tax['name']] = $tax; |
|
| 503 | + $this->totals['taxes']['initial'] += wpinv_sanitize_amount($tax['initial_tax']); |
|
| 504 | + $this->totals['taxes']['recurring'] += wpinv_sanitize_amount($tax['recurring_tax']); |
|
| 505 | 505 | |
| 506 | 506 | } |
| 507 | 507 | |
@@ -510,12 +510,12 @@ discard block |
||
| 510 | 510 | * |
| 511 | 511 | * @since 1.0.19 |
| 512 | 512 | */ |
| 513 | - public function remove_tax( $tax_name ) { |
|
| 513 | + public function remove_tax($tax_name) { |
|
| 514 | 514 | |
| 515 | - if ( isset( $this->taxes[ $tax_name ] ) ) { |
|
| 516 | - $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
|
| 517 | - $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
|
| 518 | - unset( $this->taxes[ $tax_name ] ); |
|
| 515 | + if (isset($this->taxes[$tax_name])) { |
|
| 516 | + $this->totals['taxes']['initial'] -= $this->taxes[$tax_name]['initial_tax']; |
|
| 517 | + $this->totals['taxes']['recurring'] -= $this->taxes[$tax_name]['recurring_tax']; |
|
| 518 | + unset($this->taxes[$tax_name]); |
|
| 519 | 519 | } |
| 520 | 520 | |
| 521 | 521 | } |
@@ -529,11 +529,11 @@ discard block |
||
| 529 | 529 | |
| 530 | 530 | $use_taxes = wpinv_use_taxes(); |
| 531 | 531 | |
| 532 | - if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
|
| 532 | + if ($this->has_invoice() && !$this->invoice->is_taxable()) { |
|
| 533 | 533 | $use_taxes = false; |
| 534 | 534 | } |
| 535 | 535 | |
| 536 | - return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
| 536 | + return apply_filters('getpaid_submission_use_taxes', $use_taxes, $this); |
|
| 537 | 537 | |
| 538 | 538 | } |
| 539 | 539 | |
@@ -582,13 +582,13 @@ discard block |
||
| 582 | 582 | |
| 583 | 583 | $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
| 584 | 584 | $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
| 585 | - $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
|
| 585 | + $processor = new GetPaid_Payment_Form_Submission_Discount($this, $initial_total, $recurring_total); |
|
| 586 | 586 | |
| 587 | - foreach ( $processor->discounts as $discount ) { |
|
| 588 | - $this->add_discount( $discount ); |
|
| 587 | + foreach ($processor->discounts as $discount) { |
|
| 588 | + $this->add_discount($discount); |
|
| 589 | 589 | } |
| 590 | 590 | |
| 591 | - do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
|
| 591 | + do_action_ref_array('getpaid_submissions_process_discounts', array(&$this)); |
|
| 592 | 592 | } |
| 593 | 593 | |
| 594 | 594 | /** |
@@ -597,10 +597,10 @@ discard block |
||
| 597 | 597 | * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
| 598 | 598 | * @since 1.0.19 |
| 599 | 599 | */ |
| 600 | - public function add_discount( $discount ) { |
|
| 601 | - $this->discounts[ $discount['name'] ] = $discount; |
|
| 602 | - $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
|
| 603 | - $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
|
| 600 | + public function add_discount($discount) { |
|
| 601 | + $this->discounts[$discount['name']] = $discount; |
|
| 602 | + $this->totals['discount']['initial'] += wpinv_sanitize_amount($discount['initial_discount']); |
|
| 603 | + $this->totals['discount']['recurring'] += wpinv_sanitize_amount($discount['recurring_discount']); |
|
| 604 | 604 | } |
| 605 | 605 | |
| 606 | 606 | /** |
@@ -608,12 +608,12 @@ discard block |
||
| 608 | 608 | * |
| 609 | 609 | * @since 1.0.19 |
| 610 | 610 | */ |
| 611 | - public function remove_discount( $name ) { |
|
| 611 | + public function remove_discount($name) { |
|
| 612 | 612 | |
| 613 | - if ( isset( $this->discounts[ $name ] ) ) { |
|
| 614 | - $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
|
| 615 | - $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
|
| 616 | - unset( $this->discounts[ $name ] ); |
|
| 613 | + if (isset($this->discounts[$name])) { |
|
| 614 | + $this->totals['discount']['initial'] -= $this->discounts[$name]['initial_discount']; |
|
| 615 | + $this->totals['discount']['recurring'] -= $this->discounts[$name]['recurring_discount']; |
|
| 616 | + unset($this->discounts[$name]); |
|
| 617 | 617 | } |
| 618 | 618 | |
| 619 | 619 | } |
@@ -625,7 +625,7 @@ discard block |
||
| 625 | 625 | * @return bool |
| 626 | 626 | */ |
| 627 | 627 | public function has_discount_code() { |
| 628 | - return ! empty( $this->discounts['discount_code'] ); |
|
| 628 | + return !empty($this->discounts['discount_code']); |
|
| 629 | 629 | } |
| 630 | 630 | |
| 631 | 631 | /** |
@@ -682,13 +682,13 @@ discard block |
||
| 682 | 682 | */ |
| 683 | 683 | public function process_fees() { |
| 684 | 684 | |
| 685 | - $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
|
| 685 | + $fees_processor = new GetPaid_Payment_Form_Submission_Fees($this); |
|
| 686 | 686 | |
| 687 | - foreach ( $fees_processor->fees as $fee ) { |
|
| 688 | - $this->add_fee( $fee ); |
|
| 687 | + foreach ($fees_processor->fees as $fee) { |
|
| 688 | + $this->add_fee($fee); |
|
| 689 | 689 | } |
| 690 | 690 | |
| 691 | - do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
|
| 691 | + do_action_ref_array('getpaid_submissions_process_fees', array(&$this)); |
|
| 692 | 692 | } |
| 693 | 693 | |
| 694 | 694 | /** |
@@ -697,11 +697,11 @@ discard block |
||
| 697 | 697 | * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
| 698 | 698 | * @since 1.0.19 |
| 699 | 699 | */ |
| 700 | - public function add_fee( $fee ) { |
|
| 700 | + public function add_fee($fee) { |
|
| 701 | 701 | |
| 702 | - $this->fees[ $fee['name'] ] = $fee; |
|
| 703 | - $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
| 704 | - $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
| 702 | + $this->fees[$fee['name']] = $fee; |
|
| 703 | + $this->totals['fees']['initial'] += wpinv_sanitize_amount($fee['initial_fee']); |
|
| 704 | + $this->totals['fees']['recurring'] += wpinv_sanitize_amount($fee['recurring_fee']); |
|
| 705 | 705 | |
| 706 | 706 | } |
| 707 | 707 | |
@@ -710,12 +710,12 @@ discard block |
||
| 710 | 710 | * |
| 711 | 711 | * @since 1.0.19 |
| 712 | 712 | */ |
| 713 | - public function remove_fee( $name ) { |
|
| 713 | + public function remove_fee($name) { |
|
| 714 | 714 | |
| 715 | - if ( isset( $this->fees[ $name ] ) ) { |
|
| 716 | - $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
|
| 717 | - $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
|
| 718 | - unset( $this->fees[ $name ] ); |
|
| 715 | + if (isset($this->fees[$name])) { |
|
| 716 | + $this->totals['fees']['initial'] -= $this->fees[$name]['initial_fee']; |
|
| 717 | + $this->totals['fees']['recurring'] -= $this->fees[$name]['recurring_fee']; |
|
| 718 | + unset($this->fees[$name]); |
|
| 719 | 719 | } |
| 720 | 720 | |
| 721 | 721 | } |
@@ -754,7 +754,7 @@ discard block |
||
| 754 | 754 | * @since 1.0.19 |
| 755 | 755 | */ |
| 756 | 756 | public function has_fees() { |
| 757 | - return count( $this->fees ) !== 0; |
|
| 757 | + return count($this->fees) !== 0; |
|
| 758 | 758 | } |
| 759 | 759 | |
| 760 | 760 | /* |
@@ -772,7 +772,7 @@ discard block |
||
| 772 | 772 | * @since 1.0.19 |
| 773 | 773 | */ |
| 774 | 774 | public function is_initial_fetch() { |
| 775 | - return empty( $this->data['initial_state'] ); |
|
| 775 | + return empty($this->data['initial_state']); |
|
| 776 | 776 | } |
| 777 | 777 | |
| 778 | 778 | /** |
@@ -782,7 +782,7 @@ discard block |
||
| 782 | 782 | */ |
| 783 | 783 | public function get_total() { |
| 784 | 784 | $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() - $this->get_discount(); |
| 785 | - return max( $total, 0 ); |
|
| 785 | + return max($total, 0); |
|
| 786 | 786 | } |
| 787 | 787 | |
| 788 | 788 | /** |
@@ -792,7 +792,7 @@ discard block |
||
| 792 | 792 | */ |
| 793 | 793 | public function get_recurring_total() { |
| 794 | 794 | $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() - $this->get_recurring_discount(); |
| 795 | - return max( $total, 0 ); |
|
| 795 | + return max($total, 0); |
|
| 796 | 796 | } |
| 797 | 797 | |
| 798 | 798 | /** |
@@ -804,12 +804,12 @@ discard block |
||
| 804 | 804 | $initial = $this->get_total(); |
| 805 | 805 | $recurring = $this->get_recurring_total(); |
| 806 | 806 | |
| 807 | - if ( $this->has_recurring == 0 ) { |
|
| 807 | + if ($this->has_recurring == 0) { |
|
| 808 | 808 | $recurring = 0; |
| 809 | 809 | } |
| 810 | 810 | |
| 811 | 811 | $collect = $initial > 0 || $recurring > 0; |
| 812 | - return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
|
| 812 | + return apply_filters('getpaid_submission_should_collect_payment_details', $collect, $this); |
|
| 813 | 813 | } |
| 814 | 814 | |
| 815 | 815 | /** |
@@ -818,7 +818,7 @@ discard block |
||
| 818 | 818 | * @since 1.0.19 |
| 819 | 819 | */ |
| 820 | 820 | public function get_billing_email() { |
| 821 | - return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
|
| 821 | + return apply_filters('getpaid_get_submission_billing_email', $this->get_field('billing_email'), $this); |
|
| 822 | 822 | } |
| 823 | 823 | |
| 824 | 824 | /** |
@@ -828,7 +828,7 @@ discard block |
||
| 828 | 828 | */ |
| 829 | 829 | public function has_billing_email() { |
| 830 | 830 | $billing_email = $this->get_billing_email(); |
| 831 | - return ! empty( $billing_email ) && is_email( $billing_email ); |
|
| 831 | + return !empty($billing_email) && is_email($billing_email); |
|
| 832 | 832 | } |
| 833 | 833 | |
| 834 | 834 | /** |
@@ -858,8 +858,8 @@ discard block |
||
| 858 | 858 | * @since 1.0.19 |
| 859 | 859 | * @return mixed|null |
| 860 | 860 | */ |
| 861 | - public function get_field( $field, $sub_array_key = null ) { |
|
| 862 | - return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
|
| 861 | + public function get_field($field, $sub_array_key = null) { |
|
| 862 | + return getpaid_get_array_field($this->data, $field, $sub_array_key); |
|
| 863 | 863 | } |
| 864 | 864 | |
| 865 | 865 | /** |
@@ -867,8 +867,8 @@ discard block |
||
| 867 | 867 | * |
| 868 | 868 | * @since 1.0.19 |
| 869 | 869 | */ |
| 870 | - public function is_required_field_set( $field ) { |
|
| 871 | - return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
| 870 | + public function is_required_field_set($field) { |
|
| 871 | + return empty($field['required']) || !empty($this->data[$field['id']]); |
|
| 872 | 872 | } |
| 873 | 873 | |
| 874 | 874 | /** |
@@ -876,8 +876,8 @@ discard block |
||
| 876 | 876 | * |
| 877 | 877 | * @since 1.0.19 |
| 878 | 878 | */ |
| 879 | - public function format_amount( $amount ) { |
|
| 880 | - return wpinv_price( $amount, $this->get_currency() ); |
|
| 879 | + public function format_amount($amount) { |
|
| 880 | + return wpinv_price($amount, $this->get_currency()); |
|
| 881 | 881 | } |
| 882 | 882 | |
| 883 | 883 | } |
@@ -12,455 +12,455 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Invoice_Notification_Emails { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * The array of invoice email actions. |
|
| 17 | - * |
|
| 18 | - * @param array |
|
| 19 | - */ |
|
| 20 | - public $invoice_actions; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Class constructor |
|
| 24 | - * |
|
| 25 | - */ |
|
| 26 | - public function __construct() { |
|
| 27 | - |
|
| 28 | - $this->invoice_actions = apply_filters( |
|
| 29 | - 'getpaid_notification_email_invoice_triggers', |
|
| 30 | - array( |
|
| 31 | - 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
| 32 | - 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
| 33 | - 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
| 34 | - 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
| 35 | - 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
| 36 | - 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
| 37 | - 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
| 38 | - 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
| 39 | - 'getpaid_new_customer_note' => 'user_note', |
|
| 40 | - 'getpaid_daily_maintenance' => 'overdue', |
|
| 41 | - ) |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - $this->init_hooks(); |
|
| 45 | - |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * Registers email hooks. |
|
| 50 | - */ |
|
| 51 | - public function init_hooks() { |
|
| 52 | - |
|
| 53 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
| 54 | - add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
| 55 | - |
|
| 56 | - foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
| 57 | - $this->init_email_type_hook( $hook, $email_type ); |
|
| 58 | - } |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * Registers an email hook for an invoice action. |
|
| 63 | - * |
|
| 64 | - * @param string $hook |
|
| 65 | - * @param string|array $email_type |
|
| 66 | - */ |
|
| 67 | - public function init_email_type_hook( $hook, $email_type ) { |
|
| 68 | - |
|
| 69 | - $email_type = wpinv_parse_list( $email_type ); |
|
| 70 | - |
|
| 71 | - foreach ( $email_type as $type ) { |
|
| 72 | - |
|
| 73 | - $email = new GetPaid_Notification_Email( $type ); |
|
| 74 | - |
|
| 75 | - // Abort if it is not active. |
|
| 76 | - if ( ! $email->is_active() ) { |
|
| 77 | - continue; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - if ( method_exists( $this, $type ) ) { |
|
| 81 | - add_action( $hook, array( $this, $type ), 100, 2 ); |
|
| 82 | - continue; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Filters invoice merge tags. |
|
| 92 | - * |
|
| 93 | - * @param array $merge_tags |
|
| 94 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 95 | - */ |
|
| 96 | - public function invoice_merge_tags( $merge_tags, $object ) { |
|
| 97 | - |
|
| 98 | - if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
| 99 | - return array_merge( |
|
| 100 | - $merge_tags, |
|
| 101 | - $this->get_invoice_merge_tags( $object ) |
|
| 102 | - ); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 106 | - return array_merge( |
|
| 107 | - $merge_tags, |
|
| 108 | - $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
| 109 | - ); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - return $merge_tags; |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Generates invoice merge tags. |
|
| 118 | - * |
|
| 119 | - * @param WPInv_Invoice $invoice |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - public function get_invoice_merge_tags( $invoice ) { |
|
| 123 | - |
|
| 124 | - // Abort if it does not exist. |
|
| 125 | - if ( ! $invoice->get_id() ) { |
|
| 126 | - return array(); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $merge_tags = array( |
|
| 130 | - '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 131 | - '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 132 | - '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
| 133 | - '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
| 134 | - '{email}' => sanitize_email( $invoice->get_email() ), |
|
| 135 | - '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
| 136 | - '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
| 137 | - '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
| 138 | - '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
| 139 | - '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
| 140 | - '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
| 141 | - '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
| 142 | - '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
| 143 | - '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
| 144 | - '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
| 145 | - '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
| 146 | - '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
| 147 | - '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
| 148 | - ); |
|
| 149 | - |
|
| 150 | - return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Helper function to send an email. |
|
| 155 | - * |
|
| 156 | - * @param WPInv_Invoice $invoice |
|
| 157 | - * @param GetPaid_Notification_Email $email |
|
| 158 | - * @param string $type |
|
| 159 | - * @param string|array $recipients |
|
| 160 | - * @param array $extra_args Extra template args. |
|
| 161 | - */ |
|
| 162 | - public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
| 163 | - |
|
| 164 | - do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
| 165 | - |
|
| 166 | - if ( apply_filters( 'getpaid_skip_invoice_email', false, $type, $invoice ) ) { |
|
| 167 | - return; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 171 | - $merge_tags = $email->get_merge_tags(); |
|
| 172 | - |
|
| 173 | - $result = $mailer->send( |
|
| 174 | - apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 175 | - $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
| 176 | - $email->get_content( $merge_tags, $extra_args ), |
|
| 177 | - $email->get_attachments() |
|
| 178 | - ); |
|
| 179 | - |
|
| 180 | - // Maybe send a copy to the admin. |
|
| 181 | - if ( $email->include_admin_bcc() ) { |
|
| 182 | - $mailer->send( |
|
| 183 | - wpinv_get_admin_email(), |
|
| 184 | - $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
| 185 | - $email->get_content( $merge_tags ), |
|
| 186 | - $email->get_attachments() |
|
| 187 | - ); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - if ( $result ) { |
|
| 191 | - $invoice->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 192 | - } else { |
|
| 193 | - $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
| 197 | - |
|
| 198 | - return $result; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Also send emails to any cc users. |
|
| 203 | - * |
|
| 204 | - * @param array $recipients |
|
| 205 | - * @param GetPaid_Notification_Email $email |
|
| 206 | - */ |
|
| 207 | - public function filter_email_recipients( $recipients, $email ) { |
|
| 208 | - |
|
| 209 | - if ( ! $email->is_admin_email() ) { |
|
| 210 | - $cc = $email->object->get_email_cc(); |
|
| 211 | - |
|
| 212 | - if ( ! empty( $cc ) ) { |
|
| 213 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 214 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - return $recipients; |
|
| 220 | - |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Sends a new invoice notification. |
|
| 225 | - * |
|
| 226 | - * @param WPInv_Invoice $invoice |
|
| 227 | - */ |
|
| 228 | - public function new_invoice( $invoice ) { |
|
| 229 | - |
|
| 230 | - // Only send this email for invoices created via the admin page. |
|
| 231 | - if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 232 | - return; |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 236 | - $recipient = wpinv_get_admin_email(); |
|
| 237 | - |
|
| 238 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 239 | - |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Sends a cancelled invoice notification. |
|
| 244 | - * |
|
| 245 | - * @param WPInv_Invoice $invoice |
|
| 246 | - */ |
|
| 247 | - public function cancelled_invoice( $invoice ) { |
|
| 15 | + /** |
|
| 16 | + * The array of invoice email actions. |
|
| 17 | + * |
|
| 18 | + * @param array |
|
| 19 | + */ |
|
| 20 | + public $invoice_actions; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Class constructor |
|
| 24 | + * |
|
| 25 | + */ |
|
| 26 | + public function __construct() { |
|
| 27 | + |
|
| 28 | + $this->invoice_actions = apply_filters( |
|
| 29 | + 'getpaid_notification_email_invoice_triggers', |
|
| 30 | + array( |
|
| 31 | + 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
| 32 | + 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
|
| 33 | + 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
|
| 34 | + 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
|
| 35 | + 'getpaid_invoice_status_wpi-processing' => 'processing_invoice', |
|
| 36 | + 'getpaid_invoice_status_publish' => 'completed_invoice', |
|
| 37 | + 'getpaid_invoice_status_wpi-renewal' => 'completed_invoice', |
|
| 38 | + 'getpaid_invoice_status_wpi-refunded' => 'refunded_invoice', |
|
| 39 | + 'getpaid_new_customer_note' => 'user_note', |
|
| 40 | + 'getpaid_daily_maintenance' => 'overdue', |
|
| 41 | + ) |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + $this->init_hooks(); |
|
| 45 | + |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * Registers email hooks. |
|
| 50 | + */ |
|
| 51 | + public function init_hooks() { |
|
| 52 | + |
|
| 53 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
| 54 | + add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
| 55 | + |
|
| 56 | + foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
| 57 | + $this->init_email_type_hook( $hook, $email_type ); |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * Registers an email hook for an invoice action. |
|
| 63 | + * |
|
| 64 | + * @param string $hook |
|
| 65 | + * @param string|array $email_type |
|
| 66 | + */ |
|
| 67 | + public function init_email_type_hook( $hook, $email_type ) { |
|
| 68 | + |
|
| 69 | + $email_type = wpinv_parse_list( $email_type ); |
|
| 70 | + |
|
| 71 | + foreach ( $email_type as $type ) { |
|
| 72 | + |
|
| 73 | + $email = new GetPaid_Notification_Email( $type ); |
|
| 74 | + |
|
| 75 | + // Abort if it is not active. |
|
| 76 | + if ( ! $email->is_active() ) { |
|
| 77 | + continue; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + if ( method_exists( $this, $type ) ) { |
|
| 81 | + add_action( $hook, array( $this, $type ), 100, 2 ); |
|
| 82 | + continue; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Filters invoice merge tags. |
|
| 92 | + * |
|
| 93 | + * @param array $merge_tags |
|
| 94 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 95 | + */ |
|
| 96 | + public function invoice_merge_tags( $merge_tags, $object ) { |
|
| 97 | + |
|
| 98 | + if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
| 99 | + return array_merge( |
|
| 100 | + $merge_tags, |
|
| 101 | + $this->get_invoice_merge_tags( $object ) |
|
| 102 | + ); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 106 | + return array_merge( |
|
| 107 | + $merge_tags, |
|
| 108 | + $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
| 109 | + ); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + return $merge_tags; |
|
| 113 | + |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Generates invoice merge tags. |
|
| 118 | + * |
|
| 119 | + * @param WPInv_Invoice $invoice |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + public function get_invoice_merge_tags( $invoice ) { |
|
| 123 | + |
|
| 124 | + // Abort if it does not exist. |
|
| 125 | + if ( ! $invoice->get_id() ) { |
|
| 126 | + return array(); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $merge_tags = array( |
|
| 130 | + '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 131 | + '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 132 | + '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
| 133 | + '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
| 134 | + '{email}' => sanitize_email( $invoice->get_email() ), |
|
| 135 | + '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
| 136 | + '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
| 137 | + '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
| 138 | + '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
| 139 | + '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
| 140 | + '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
| 141 | + '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
| 142 | + '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
| 143 | + '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
| 144 | + '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
| 145 | + '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
| 146 | + '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
| 147 | + '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
| 148 | + ); |
|
| 149 | + |
|
| 150 | + return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Helper function to send an email. |
|
| 155 | + * |
|
| 156 | + * @param WPInv_Invoice $invoice |
|
| 157 | + * @param GetPaid_Notification_Email $email |
|
| 158 | + * @param string $type |
|
| 159 | + * @param string|array $recipients |
|
| 160 | + * @param array $extra_args Extra template args. |
|
| 161 | + */ |
|
| 162 | + public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
| 163 | + |
|
| 164 | + do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
| 165 | + |
|
| 166 | + if ( apply_filters( 'getpaid_skip_invoice_email', false, $type, $invoice ) ) { |
|
| 167 | + return; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 171 | + $merge_tags = $email->get_merge_tags(); |
|
| 172 | + |
|
| 173 | + $result = $mailer->send( |
|
| 174 | + apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 175 | + $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
| 176 | + $email->get_content( $merge_tags, $extra_args ), |
|
| 177 | + $email->get_attachments() |
|
| 178 | + ); |
|
| 179 | + |
|
| 180 | + // Maybe send a copy to the admin. |
|
| 181 | + if ( $email->include_admin_bcc() ) { |
|
| 182 | + $mailer->send( |
|
| 183 | + wpinv_get_admin_email(), |
|
| 184 | + $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
| 185 | + $email->get_content( $merge_tags ), |
|
| 186 | + $email->get_attachments() |
|
| 187 | + ); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + if ( $result ) { |
|
| 191 | + $invoice->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 192 | + } else { |
|
| 193 | + $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
| 197 | + |
|
| 198 | + return $result; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Also send emails to any cc users. |
|
| 203 | + * |
|
| 204 | + * @param array $recipients |
|
| 205 | + * @param GetPaid_Notification_Email $email |
|
| 206 | + */ |
|
| 207 | + public function filter_email_recipients( $recipients, $email ) { |
|
| 208 | + |
|
| 209 | + if ( ! $email->is_admin_email() ) { |
|
| 210 | + $cc = $email->object->get_email_cc(); |
|
| 211 | + |
|
| 212 | + if ( ! empty( $cc ) ) { |
|
| 213 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 214 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + return $recipients; |
|
| 220 | + |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Sends a new invoice notification. |
|
| 225 | + * |
|
| 226 | + * @param WPInv_Invoice $invoice |
|
| 227 | + */ |
|
| 228 | + public function new_invoice( $invoice ) { |
|
| 229 | + |
|
| 230 | + // Only send this email for invoices created via the admin page. |
|
| 231 | + if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 232 | + return; |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 236 | + $recipient = wpinv_get_admin_email(); |
|
| 237 | + |
|
| 238 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 239 | + |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Sends a cancelled invoice notification. |
|
| 244 | + * |
|
| 245 | + * @param WPInv_Invoice $invoice |
|
| 246 | + */ |
|
| 247 | + public function cancelled_invoice( $invoice ) { |
|
| 248 | 248 | |
| 249 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 250 | - $recipient = wpinv_get_admin_email(); |
|
| 249 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 250 | + $recipient = wpinv_get_admin_email(); |
|
| 251 | 251 | |
| 252 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 252 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 253 | 253 | |
| 254 | - } |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - /** |
|
| 257 | - * Sends a failed invoice notification. |
|
| 258 | - * |
|
| 259 | - * @param WPInv_Invoice $invoice |
|
| 260 | - */ |
|
| 261 | - public function failed_invoice( $invoice ) { |
|
| 256 | + /** |
|
| 257 | + * Sends a failed invoice notification. |
|
| 258 | + * |
|
| 259 | + * @param WPInv_Invoice $invoice |
|
| 260 | + */ |
|
| 261 | + public function failed_invoice( $invoice ) { |
|
| 262 | 262 | |
| 263 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 264 | - $recipient = wpinv_get_admin_email(); |
|
| 263 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 264 | + $recipient = wpinv_get_admin_email(); |
|
| 265 | 265 | |
| 266 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 266 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 267 | 267 | |
| 268 | - } |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - /** |
|
| 271 | - * Sends a notification whenever an invoice is put on hold. |
|
| 272 | - * |
|
| 273 | - * @param WPInv_Invoice $invoice |
|
| 274 | - */ |
|
| 275 | - public function onhold_invoice( $invoice ) { |
|
| 270 | + /** |
|
| 271 | + * Sends a notification whenever an invoice is put on hold. |
|
| 272 | + * |
|
| 273 | + * @param WPInv_Invoice $invoice |
|
| 274 | + */ |
|
| 275 | + public function onhold_invoice( $invoice ) { |
|
| 276 | 276 | |
| 277 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 278 | - $recipient = $invoice->get_email(); |
|
| 277 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 278 | + $recipient = $invoice->get_email(); |
|
| 279 | 279 | |
| 280 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 280 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 281 | 281 | |
| 282 | - } |
|
| 282 | + } |
|
| 283 | 283 | |
| 284 | - /** |
|
| 285 | - * Sends a notification whenever an invoice is marked as processing payment. |
|
| 286 | - * |
|
| 287 | - * @param WPInv_Invoice $invoice |
|
| 288 | - */ |
|
| 289 | - public function processing_invoice( $invoice ) { |
|
| 284 | + /** |
|
| 285 | + * Sends a notification whenever an invoice is marked as processing payment. |
|
| 286 | + * |
|
| 287 | + * @param WPInv_Invoice $invoice |
|
| 288 | + */ |
|
| 289 | + public function processing_invoice( $invoice ) { |
|
| 290 | 290 | |
| 291 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 292 | - $recipient = $invoice->get_email(); |
|
| 291 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 292 | + $recipient = $invoice->get_email(); |
|
| 293 | 293 | |
| 294 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 294 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 295 | 295 | |
| 296 | - } |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - /** |
|
| 299 | - * Sends a notification whenever an invoice is paid. |
|
| 300 | - * |
|
| 301 | - * @param WPInv_Invoice $invoice |
|
| 302 | - */ |
|
| 303 | - public function completed_invoice( $invoice ) { |
|
| 298 | + /** |
|
| 299 | + * Sends a notification whenever an invoice is paid. |
|
| 300 | + * |
|
| 301 | + * @param WPInv_Invoice $invoice |
|
| 302 | + */ |
|
| 303 | + public function completed_invoice( $invoice ) { |
|
| 304 | 304 | |
| 305 | - // (Maybe) abort if it is a renewal invoice. |
|
| 306 | - if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
| 307 | - return; |
|
| 308 | - } |
|
| 305 | + // (Maybe) abort if it is a renewal invoice. |
|
| 306 | + if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
| 307 | + return; |
|
| 308 | + } |
|
| 309 | 309 | |
| 310 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 311 | - $recipient = $invoice->get_email(); |
|
| 310 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 311 | + $recipient = $invoice->get_email(); |
|
| 312 | 312 | |
| 313 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 313 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 314 | 314 | |
| 315 | - } |
|
| 315 | + } |
|
| 316 | 316 | |
| 317 | - /** |
|
| 318 | - * Sends a notification whenever an invoice is refunded. |
|
| 319 | - * |
|
| 320 | - * @param WPInv_Invoice $invoice |
|
| 321 | - */ |
|
| 322 | - public function refunded_invoice( $invoice ) { |
|
| 317 | + /** |
|
| 318 | + * Sends a notification whenever an invoice is refunded. |
|
| 319 | + * |
|
| 320 | + * @param WPInv_Invoice $invoice |
|
| 321 | + */ |
|
| 322 | + public function refunded_invoice( $invoice ) { |
|
| 323 | 323 | |
| 324 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 325 | - $recipient = $invoice->get_email(); |
|
| 324 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 325 | + $recipient = $invoice->get_email(); |
|
| 326 | 326 | |
| 327 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 327 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 328 | 328 | |
| 329 | - } |
|
| 329 | + } |
|
| 330 | 330 | |
| 331 | - /** |
|
| 332 | - * Notifies a user about new invoices |
|
| 333 | - * |
|
| 334 | - * @param WPInv_Invoice $invoice |
|
| 335 | - * @param bool $force |
|
| 336 | - */ |
|
| 337 | - public function user_invoice( $invoice, $force = false ) { |
|
| 331 | + /** |
|
| 332 | + * Notifies a user about new invoices |
|
| 333 | + * |
|
| 334 | + * @param WPInv_Invoice $invoice |
|
| 335 | + * @param bool $force |
|
| 336 | + */ |
|
| 337 | + public function user_invoice( $invoice, $force = false ) { |
|
| 338 | 338 | |
| 339 | - if ( ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) { |
|
| 340 | - return; |
|
| 341 | - } |
|
| 339 | + if ( ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) { |
|
| 340 | + return; |
|
| 341 | + } |
|
| 342 | 342 | |
| 343 | - // Only send this email for invoices created via the admin page. |
|
| 344 | - if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) { |
|
| 345 | - return; |
|
| 346 | - } |
|
| 343 | + // Only send this email for invoices created via the admin page. |
|
| 344 | + if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) { |
|
| 345 | + return; |
|
| 346 | + } |
|
| 347 | 347 | |
| 348 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 349 | - $recipient = $invoice->get_email(); |
|
| 348 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 349 | + $recipient = $invoice->get_email(); |
|
| 350 | 350 | |
| 351 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 351 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 352 | 352 | |
| 353 | - } |
|
| 353 | + } |
|
| 354 | 354 | |
| 355 | - /** |
|
| 356 | - * Checks if an invoice is a payment form invoice. |
|
| 357 | - * |
|
| 358 | - * @param int $invoice |
|
| 359 | - * @return bool |
|
| 360 | - */ |
|
| 361 | - public function is_payment_form_invoice( $invoice ) { |
|
| 362 | - return empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
| 363 | - } |
|
| 355 | + /** |
|
| 356 | + * Checks if an invoice is a payment form invoice. |
|
| 357 | + * |
|
| 358 | + * @param int $invoice |
|
| 359 | + * @return bool |
|
| 360 | + */ |
|
| 361 | + public function is_payment_form_invoice( $invoice ) { |
|
| 362 | + return empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
| 363 | + } |
|
| 364 | 364 | |
| 365 | - /** |
|
| 366 | - * Notifies admin about new invoice notes |
|
| 367 | - * |
|
| 368 | - * @param WPInv_Invoice $invoice |
|
| 369 | - * @param string $note |
|
| 370 | - */ |
|
| 371 | - public function user_note( $invoice, $note ) { |
|
| 372 | - |
|
| 373 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 374 | - $recipient = $invoice->get_email(); |
|
| 365 | + /** |
|
| 366 | + * Notifies admin about new invoice notes |
|
| 367 | + * |
|
| 368 | + * @param WPInv_Invoice $invoice |
|
| 369 | + * @param string $note |
|
| 370 | + */ |
|
| 371 | + public function user_note( $invoice, $note ) { |
|
| 372 | + |
|
| 373 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 374 | + $recipient = $invoice->get_email(); |
|
| 375 | 375 | |
| 376 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
| 377 | - |
|
| 378 | - } |
|
| 376 | + return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
| 377 | + |
|
| 378 | + } |
|
| 379 | 379 | |
| 380 | - /** |
|
| 381 | - * (Force) Sends overdue notices. |
|
| 382 | - * |
|
| 383 | - * @param WPInv_Invoice $invoice |
|
| 384 | - */ |
|
| 385 | - public function force_send_overdue_notice( $invoice ) { |
|
| 386 | - $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
| 387 | - return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
| 388 | - } |
|
| 389 | - |
|
| 390 | - /** |
|
| 391 | - * Sends overdue notices. |
|
| 392 | - * |
|
| 393 | - * @TODO: Create an invoices query class. |
|
| 394 | - */ |
|
| 395 | - public function overdue() { |
|
| 396 | - global $wpdb; |
|
| 397 | - |
|
| 398 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 399 | - |
|
| 400 | - // Fetch reminder days. |
|
| 401 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 402 | - |
|
| 403 | - // Abort if non is set. |
|
| 404 | - if ( empty( $reminder_days ) ) { |
|
| 405 | - return; |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - // Retrieve date query. |
|
| 409 | - $date_query = $this->get_date_query( $reminder_days ); |
|
| 410 | - |
|
| 411 | - // Invoices table. |
|
| 412 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 413 | - |
|
| 414 | - // Fetch invoices. |
|
| 415 | - $invoices = $wpdb->get_col( |
|
| 416 | - "SELECT posts.ID FROM $wpdb->posts as posts |
|
| 380 | + /** |
|
| 381 | + * (Force) Sends overdue notices. |
|
| 382 | + * |
|
| 383 | + * @param WPInv_Invoice $invoice |
|
| 384 | + */ |
|
| 385 | + public function force_send_overdue_notice( $invoice ) { |
|
| 386 | + $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
| 387 | + return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
| 388 | + } |
|
| 389 | + |
|
| 390 | + /** |
|
| 391 | + * Sends overdue notices. |
|
| 392 | + * |
|
| 393 | + * @TODO: Create an invoices query class. |
|
| 394 | + */ |
|
| 395 | + public function overdue() { |
|
| 396 | + global $wpdb; |
|
| 397 | + |
|
| 398 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 399 | + |
|
| 400 | + // Fetch reminder days. |
|
| 401 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 402 | + |
|
| 403 | + // Abort if non is set. |
|
| 404 | + if ( empty( $reminder_days ) ) { |
|
| 405 | + return; |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + // Retrieve date query. |
|
| 409 | + $date_query = $this->get_date_query( $reminder_days ); |
|
| 410 | + |
|
| 411 | + // Invoices table. |
|
| 412 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 413 | + |
|
| 414 | + // Fetch invoices. |
|
| 415 | + $invoices = $wpdb->get_col( |
|
| 416 | + "SELECT posts.ID FROM $wpdb->posts as posts |
|
| 417 | 417 | LEFT JOIN $table as invoices ON invoices.post_id = posts.ID |
| 418 | 418 | WHERE posts.post_type = 'wpi_invoice' AND posts.post_status = 'wpi-pending' $date_query"); |
| 419 | 419 | |
| 420 | - foreach ( $invoices as $invoice ) { |
|
| 420 | + foreach ( $invoices as $invoice ) { |
|
| 421 | 421 | |
| 422 | - // Only send this email for invoices created via the admin page. |
|
| 423 | - if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
| 424 | - $invoice = new WPInv_Invoice( $invoice ); |
|
| 425 | - $email->object = $invoice; |
|
| 422 | + // Only send this email for invoices created via the admin page. |
|
| 423 | + if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
| 424 | + $invoice = new WPInv_Invoice( $invoice ); |
|
| 425 | + $email->object = $invoice; |
|
| 426 | 426 | |
| 427 | - if ( $invoice->needs_payment() ) { |
|
| 428 | - $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
| 429 | - } |
|
| 427 | + if ( $invoice->needs_payment() ) { |
|
| 428 | + $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
| 429 | + } |
|
| 430 | 430 | |
| 431 | - } |
|
| 431 | + } |
|
| 432 | 432 | |
| 433 | - } |
|
| 433 | + } |
|
| 434 | 434 | |
| 435 | - } |
|
| 435 | + } |
|
| 436 | 436 | |
| 437 | - /** |
|
| 438 | - * Calculates the date query for an invoices query |
|
| 439 | - * |
|
| 440 | - * @param array $reminder_days |
|
| 441 | - * @return string |
|
| 442 | - */ |
|
| 443 | - public function get_date_query( $reminder_days ) { |
|
| 437 | + /** |
|
| 438 | + * Calculates the date query for an invoices query |
|
| 439 | + * |
|
| 440 | + * @param array $reminder_days |
|
| 441 | + * @return string |
|
| 442 | + */ |
|
| 443 | + public function get_date_query( $reminder_days ) { |
|
| 444 | 444 | |
| 445 | - $date_query = array( |
|
| 446 | - 'relation' => 'OR' |
|
| 447 | - ); |
|
| 445 | + $date_query = array( |
|
| 446 | + 'relation' => 'OR' |
|
| 447 | + ); |
|
| 448 | 448 | |
| 449 | - foreach ( $reminder_days as $days ) { |
|
| 450 | - $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
| 449 | + foreach ( $reminder_days as $days ) { |
|
| 450 | + $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
| 451 | 451 | |
| 452 | - $date_query[] = array( |
|
| 453 | - 'year' => $date['year'], |
|
| 454 | - 'month' => $date['month'], |
|
| 455 | - 'day' => $date['day'], |
|
| 456 | - ); |
|
| 452 | + $date_query[] = array( |
|
| 453 | + 'year' => $date['year'], |
|
| 454 | + 'month' => $date['month'], |
|
| 455 | + 'day' => $date['day'], |
|
| 456 | + ); |
|
| 457 | 457 | |
| 458 | - } |
|
| 458 | + } |
|
| 459 | 459 | |
| 460 | - $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
| 460 | + $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
| 461 | 461 | |
| 462 | - return $date_query->get_sql(); |
|
| 462 | + return $date_query->get_sql(); |
|
| 463 | 463 | |
| 464 | - } |
|
| 464 | + } |
|
| 465 | 465 | |
| 466 | 466 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | -defined( 'ABSPATH' ) || exit; |
|
| 7 | +defined('ABSPATH') || exit; |
|
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * This class handles invoice notificaiton emails. |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | $this->invoice_actions = apply_filters( |
| 29 | 29 | 'getpaid_notification_email_invoice_triggers', |
| 30 | 30 | array( |
| 31 | - 'getpaid_new_invoice' => array( 'new_invoice', 'user_invoice' ), |
|
| 31 | + 'getpaid_new_invoice' => array('new_invoice', 'user_invoice'), |
|
| 32 | 32 | 'getpaid_invoice_status_wpi-cancelled' => 'cancelled_invoice', |
| 33 | 33 | 'getpaid_invoice_status_wpi-failed' => 'failed_invoice', |
| 34 | 34 | 'getpaid_invoice_status_wpi-onhold' => 'onhold_invoice', |
@@ -50,11 +50,11 @@ discard block |
||
| 50 | 50 | */ |
| 51 | 51 | public function init_hooks() { |
| 52 | 52 | |
| 53 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 ); |
|
| 54 | - add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 ); |
|
| 53 | + add_filter('getpaid_get_email_merge_tags', array($this, 'invoice_merge_tags'), 10, 2); |
|
| 54 | + add_filter('getpaid_invoice_email_recipients', array($this, 'filter_email_recipients'), 10, 2); |
|
| 55 | 55 | |
| 56 | - foreach ( $this->invoice_actions as $hook => $email_type ) { |
|
| 57 | - $this->init_email_type_hook( $hook, $email_type ); |
|
| 56 | + foreach ($this->invoice_actions as $hook => $email_type) { |
|
| 57 | + $this->init_email_type_hook($hook, $email_type); |
|
| 58 | 58 | } |
| 59 | 59 | } |
| 60 | 60 | |
@@ -64,25 +64,25 @@ discard block |
||
| 64 | 64 | * @param string $hook |
| 65 | 65 | * @param string|array $email_type |
| 66 | 66 | */ |
| 67 | - public function init_email_type_hook( $hook, $email_type ) { |
|
| 67 | + public function init_email_type_hook($hook, $email_type) { |
|
| 68 | 68 | |
| 69 | - $email_type = wpinv_parse_list( $email_type ); |
|
| 69 | + $email_type = wpinv_parse_list($email_type); |
|
| 70 | 70 | |
| 71 | - foreach ( $email_type as $type ) { |
|
| 71 | + foreach ($email_type as $type) { |
|
| 72 | 72 | |
| 73 | - $email = new GetPaid_Notification_Email( $type ); |
|
| 73 | + $email = new GetPaid_Notification_Email($type); |
|
| 74 | 74 | |
| 75 | 75 | // Abort if it is not active. |
| 76 | - if ( ! $email->is_active() ) { |
|
| 76 | + if (!$email->is_active()) { |
|
| 77 | 77 | continue; |
| 78 | 78 | } |
| 79 | 79 | |
| 80 | - if ( method_exists( $this, $type ) ) { |
|
| 81 | - add_action( $hook, array( $this, $type ), 100, 2 ); |
|
| 80 | + if (method_exists($this, $type)) { |
|
| 81 | + add_action($hook, array($this, $type), 100, 2); |
|
| 82 | 82 | continue; |
| 83 | 83 | } |
| 84 | 84 | |
| 85 | - do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook ); |
|
| 85 | + do_action('getpaid_invoice_init_email_type_hook', $type, $hook); |
|
| 86 | 86 | } |
| 87 | 87 | |
| 88 | 88 | } |
@@ -93,19 +93,19 @@ discard block |
||
| 93 | 93 | * @param array $merge_tags |
| 94 | 94 | * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
| 95 | 95 | */ |
| 96 | - public function invoice_merge_tags( $merge_tags, $object ) { |
|
| 96 | + public function invoice_merge_tags($merge_tags, $object) { |
|
| 97 | 97 | |
| 98 | - if ( is_a( $object, 'WPInv_Invoice' ) ) { |
|
| 98 | + if (is_a($object, 'WPInv_Invoice')) { |
|
| 99 | 99 | return array_merge( |
| 100 | 100 | $merge_tags, |
| 101 | - $this->get_invoice_merge_tags( $object ) |
|
| 101 | + $this->get_invoice_merge_tags($object) |
|
| 102 | 102 | ); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 105 | + if (is_a($object, 'WPInv_Subscription')) { |
|
| 106 | 106 | return array_merge( |
| 107 | 107 | $merge_tags, |
| 108 | - $this->get_invoice_merge_tags( $object->get_parent_payment() ) |
|
| 108 | + $this->get_invoice_merge_tags($object->get_parent_payment()) |
|
| 109 | 109 | ); |
| 110 | 110 | } |
| 111 | 111 | |
@@ -119,35 +119,35 @@ discard block |
||
| 119 | 119 | * @param WPInv_Invoice $invoice |
| 120 | 120 | * @return array |
| 121 | 121 | */ |
| 122 | - public function get_invoice_merge_tags( $invoice ) { |
|
| 122 | + public function get_invoice_merge_tags($invoice) { |
|
| 123 | 123 | |
| 124 | 124 | // Abort if it does not exist. |
| 125 | - if ( ! $invoice->get_id() ) { |
|
| 125 | + if (!$invoice->get_id()) { |
|
| 126 | 126 | return array(); |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | 129 | $merge_tags = array( |
| 130 | - '{name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 131 | - '{full_name}' => sanitize_text_field( $invoice->get_user_full_name() ), |
|
| 132 | - '{first_name}' => sanitize_text_field( $invoice->get_first_name() ), |
|
| 133 | - '{last_name}' => sanitize_text_field( $invoice->get_last_name() ), |
|
| 134 | - '{email}' => sanitize_email( $invoice->get_email() ), |
|
| 135 | - '{invoice_number}' => sanitize_text_field( $invoice->get_number() ), |
|
| 136 | - '{invoice_currency}' => sanitize_text_field( $invoice->get_currency() ), |
|
| 137 | - '{invoice_total}' => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ), |
|
| 138 | - '{invoice_link}' => esc_url( $invoice->get_view_url() ), |
|
| 139 | - '{invoice_pay_link}' => esc_url( $invoice->get_checkout_payment_url() ), |
|
| 140 | - '{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ), |
|
| 141 | - '{invoice_date}' => getpaid_format_date_value( $invoice->get_date_created() ), |
|
| 142 | - '{invoice_due_date}' => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ), |
|
| 143 | - '{invoice_quote}' => sanitize_text_field( strtolower( $invoice->get_label() ) ), |
|
| 144 | - '{invoice_label}' => sanitize_text_field( ucfirst( $invoice->get_label() ) ), |
|
| 145 | - '{invoice_description}' => wp_kses_post( $invoice->get_description() ), |
|
| 146 | - '{subscription_name}' => wp_kses_post( $invoice->get_subscription_name() ), |
|
| 147 | - '{is_was}' => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ), |
|
| 130 | + '{name}' => sanitize_text_field($invoice->get_user_full_name()), |
|
| 131 | + '{full_name}' => sanitize_text_field($invoice->get_user_full_name()), |
|
| 132 | + '{first_name}' => sanitize_text_field($invoice->get_first_name()), |
|
| 133 | + '{last_name}' => sanitize_text_field($invoice->get_last_name()), |
|
| 134 | + '{email}' => sanitize_email($invoice->get_email()), |
|
| 135 | + '{invoice_number}' => sanitize_text_field($invoice->get_number()), |
|
| 136 | + '{invoice_currency}' => sanitize_text_field($invoice->get_currency()), |
|
| 137 | + '{invoice_total}' => sanitize_text_field(wpinv_price($invoice->get_total(), $invoice->get_currency())), |
|
| 138 | + '{invoice_link}' => esc_url($invoice->get_view_url()), |
|
| 139 | + '{invoice_pay_link}' => esc_url($invoice->get_checkout_payment_url()), |
|
| 140 | + '{invoice_receipt_link}'=> esc_url($invoice->get_receipt_url()), |
|
| 141 | + '{invoice_date}' => getpaid_format_date_value($invoice->get_date_created()), |
|
| 142 | + '{invoice_due_date}' => getpaid_format_date_value($invoice->get_due_date(), __('on receipt', 'invoicing')), |
|
| 143 | + '{invoice_quote}' => sanitize_text_field(strtolower($invoice->get_label())), |
|
| 144 | + '{invoice_label}' => sanitize_text_field(ucfirst($invoice->get_label())), |
|
| 145 | + '{invoice_description}' => wp_kses_post($invoice->get_description()), |
|
| 146 | + '{subscription_name}' => wp_kses_post($invoice->get_subscription_name()), |
|
| 147 | + '{is_was}' => strtotime($invoice->get_due_date()) < current_time('timestamp') ? __('was', 'invoicing') : __('is', 'invoicing'), |
|
| 148 | 148 | ); |
| 149 | 149 | |
| 150 | - return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice ); |
|
| 150 | + return apply_filters('getpaid_invoice_email_merge_tags', $merge_tags, $invoice); |
|
| 151 | 151 | } |
| 152 | 152 | |
| 153 | 153 | /** |
@@ -159,11 +159,11 @@ discard block |
||
| 159 | 159 | * @param string|array $recipients |
| 160 | 160 | * @param array $extra_args Extra template args. |
| 161 | 161 | */ |
| 162 | - public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) { |
|
| 162 | + public function send_email($invoice, $email, $type, $recipients, $extra_args = array()) { |
|
| 163 | 163 | |
| 164 | - do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email ); |
|
| 164 | + do_action('getpaid_before_send_invoice_notification', $type, $invoice, $email); |
|
| 165 | 165 | |
| 166 | - if ( apply_filters( 'getpaid_skip_invoice_email', false, $type, $invoice ) ) { |
|
| 166 | + if (apply_filters('getpaid_skip_invoice_email', false, $type, $invoice)) { |
|
| 167 | 167 | return; |
| 168 | 168 | } |
| 169 | 169 | |
@@ -171,29 +171,29 @@ discard block |
||
| 171 | 171 | $merge_tags = $email->get_merge_tags(); |
| 172 | 172 | |
| 173 | 173 | $result = $mailer->send( |
| 174 | - apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 175 | - $email->add_merge_tags( $email->get_subject(), $merge_tags ), |
|
| 176 | - $email->get_content( $merge_tags, $extra_args ), |
|
| 174 | + apply_filters('getpaid_invoice_email_recipients', wpinv_parse_list($recipients), $email), |
|
| 175 | + $email->add_merge_tags($email->get_subject(), $merge_tags), |
|
| 176 | + $email->get_content($merge_tags, $extra_args), |
|
| 177 | 177 | $email->get_attachments() |
| 178 | 178 | ); |
| 179 | 179 | |
| 180 | 180 | // Maybe send a copy to the admin. |
| 181 | - if ( $email->include_admin_bcc() ) { |
|
| 181 | + if ($email->include_admin_bcc()) { |
|
| 182 | 182 | $mailer->send( |
| 183 | 183 | wpinv_get_admin_email(), |
| 184 | - $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ), |
|
| 185 | - $email->get_content( $merge_tags ), |
|
| 184 | + $email->add_merge_tags($email->get_subject() . __(' - ADMIN BCC COPY', 'invoicing'), $merge_tags), |
|
| 185 | + $email->get_content($merge_tags), |
|
| 186 | 186 | $email->get_attachments() |
| 187 | 187 | ); |
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - if ( $result ) { |
|
| 191 | - $invoice->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 190 | + if ($result) { |
|
| 191 | + $invoice->add_note(sprintf(__('Successfully sent %s notification email.', 'invoicing'), sanitize_key($type)), false, false, true); |
|
| 192 | 192 | } else { |
| 193 | - $invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 193 | + $invoice->add_note(sprintf(__('Failed sending %s notification email.', 'invoicing'), sanitize_key($type)), false, false, true); |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | - do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email ); |
|
| 196 | + do_action('getpaid_after_send_invoice_notification', $type, $invoice, $email); |
|
| 197 | 197 | |
| 198 | 198 | return $result; |
| 199 | 199 | } |
@@ -204,14 +204,14 @@ discard block |
||
| 204 | 204 | * @param array $recipients |
| 205 | 205 | * @param GetPaid_Notification_Email $email |
| 206 | 206 | */ |
| 207 | - public function filter_email_recipients( $recipients, $email ) { |
|
| 207 | + public function filter_email_recipients($recipients, $email) { |
|
| 208 | 208 | |
| 209 | - if ( ! $email->is_admin_email() ) { |
|
| 209 | + if (!$email->is_admin_email()) { |
|
| 210 | 210 | $cc = $email->object->get_email_cc(); |
| 211 | 211 | |
| 212 | - if ( ! empty( $cc ) ) { |
|
| 213 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 214 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 212 | + if (!empty($cc)) { |
|
| 213 | + $cc = array_map('sanitize_email', wpinv_parse_list($cc)); |
|
| 214 | + $recipients = array_filter(array_unique(array_merge($recipients, $cc))); |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | } |
@@ -225,17 +225,17 @@ discard block |
||
| 225 | 225 | * |
| 226 | 226 | * @param WPInv_Invoice $invoice |
| 227 | 227 | */ |
| 228 | - public function new_invoice( $invoice ) { |
|
| 228 | + public function new_invoice($invoice) { |
|
| 229 | 229 | |
| 230 | 230 | // Only send this email for invoices created via the admin page. |
| 231 | - if ( ! $invoice->is_type( 'invoice' ) || $this->is_payment_form_invoice( $invoice->get_id() ) ) { |
|
| 231 | + if (!$invoice->is_type('invoice') || $this->is_payment_form_invoice($invoice->get_id())) { |
|
| 232 | 232 | return; |
| 233 | 233 | } |
| 234 | 234 | |
| 235 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 235 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 236 | 236 | $recipient = wpinv_get_admin_email(); |
| 237 | 237 | |
| 238 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 238 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 239 | 239 | |
| 240 | 240 | } |
| 241 | 241 | |
@@ -244,12 +244,12 @@ discard block |
||
| 244 | 244 | * |
| 245 | 245 | * @param WPInv_Invoice $invoice |
| 246 | 246 | */ |
| 247 | - public function cancelled_invoice( $invoice ) { |
|
| 247 | + public function cancelled_invoice($invoice) { |
|
| 248 | 248 | |
| 249 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 249 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 250 | 250 | $recipient = wpinv_get_admin_email(); |
| 251 | 251 | |
| 252 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 252 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 253 | 253 | |
| 254 | 254 | } |
| 255 | 255 | |
@@ -258,12 +258,12 @@ discard block |
||
| 258 | 258 | * |
| 259 | 259 | * @param WPInv_Invoice $invoice |
| 260 | 260 | */ |
| 261 | - public function failed_invoice( $invoice ) { |
|
| 261 | + public function failed_invoice($invoice) { |
|
| 262 | 262 | |
| 263 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 263 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 264 | 264 | $recipient = wpinv_get_admin_email(); |
| 265 | 265 | |
| 266 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 266 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 267 | 267 | |
| 268 | 268 | } |
| 269 | 269 | |
@@ -272,12 +272,12 @@ discard block |
||
| 272 | 272 | * |
| 273 | 273 | * @param WPInv_Invoice $invoice |
| 274 | 274 | */ |
| 275 | - public function onhold_invoice( $invoice ) { |
|
| 275 | + public function onhold_invoice($invoice) { |
|
| 276 | 276 | |
| 277 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 277 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 278 | 278 | $recipient = $invoice->get_email(); |
| 279 | 279 | |
| 280 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 280 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 281 | 281 | |
| 282 | 282 | } |
| 283 | 283 | |
@@ -286,12 +286,12 @@ discard block |
||
| 286 | 286 | * |
| 287 | 287 | * @param WPInv_Invoice $invoice |
| 288 | 288 | */ |
| 289 | - public function processing_invoice( $invoice ) { |
|
| 289 | + public function processing_invoice($invoice) { |
|
| 290 | 290 | |
| 291 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 291 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 292 | 292 | $recipient = $invoice->get_email(); |
| 293 | 293 | |
| 294 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 294 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 295 | 295 | |
| 296 | 296 | } |
| 297 | 297 | |
@@ -300,17 +300,17 @@ discard block |
||
| 300 | 300 | * |
| 301 | 301 | * @param WPInv_Invoice $invoice |
| 302 | 302 | */ |
| 303 | - public function completed_invoice( $invoice ) { |
|
| 303 | + public function completed_invoice($invoice) { |
|
| 304 | 304 | |
| 305 | 305 | // (Maybe) abort if it is a renewal invoice. |
| 306 | - if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) { |
|
| 306 | + if ($invoice->is_renewal() && !wpinv_get_option('email_completed_invoice_renewal_active', false)) { |
|
| 307 | 307 | return; |
| 308 | 308 | } |
| 309 | 309 | |
| 310 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 310 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 311 | 311 | $recipient = $invoice->get_email(); |
| 312 | 312 | |
| 313 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 313 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 314 | 314 | |
| 315 | 315 | } |
| 316 | 316 | |
@@ -319,12 +319,12 @@ discard block |
||
| 319 | 319 | * |
| 320 | 320 | * @param WPInv_Invoice $invoice |
| 321 | 321 | */ |
| 322 | - public function refunded_invoice( $invoice ) { |
|
| 322 | + public function refunded_invoice($invoice) { |
|
| 323 | 323 | |
| 324 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 324 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 325 | 325 | $recipient = $invoice->get_email(); |
| 326 | 326 | |
| 327 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 327 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 328 | 328 | |
| 329 | 329 | } |
| 330 | 330 | |
@@ -334,21 +334,21 @@ discard block |
||
| 334 | 334 | * @param WPInv_Invoice $invoice |
| 335 | 335 | * @param bool $force |
| 336 | 336 | */ |
| 337 | - public function user_invoice( $invoice, $force = false ) { |
|
| 337 | + public function user_invoice($invoice, $force = false) { |
|
| 338 | 338 | |
| 339 | - if ( ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) { |
|
| 339 | + if (!empty($GLOBALS['wpinv_skip_invoice_notification'])) { |
|
| 340 | 340 | return; |
| 341 | 341 | } |
| 342 | 342 | |
| 343 | 343 | // Only send this email for invoices created via the admin page. |
| 344 | - if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) { |
|
| 344 | + if (!$invoice->is_type('invoice') || (empty($force) && $this->is_payment_form_invoice($invoice->get_id()))) { |
|
| 345 | 345 | return; |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 348 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 349 | 349 | $recipient = $invoice->get_email(); |
| 350 | 350 | |
| 351 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient ); |
|
| 351 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient); |
|
| 352 | 352 | |
| 353 | 353 | } |
| 354 | 354 | |
@@ -358,8 +358,8 @@ discard block |
||
| 358 | 358 | * @param int $invoice |
| 359 | 359 | * @return bool |
| 360 | 360 | */ |
| 361 | - public function is_payment_form_invoice( $invoice ) { |
|
| 362 | - return empty( $_GET['getpaid-admin-action'] ) && ( 'payment_form' == get_post_meta( $invoice, 'wpinv_created_via', true ) || 'geodirectory' == get_post_meta( $invoice, 'wpinv_created_via', true ) ); |
|
| 361 | + public function is_payment_form_invoice($invoice) { |
|
| 362 | + return empty($_GET['getpaid-admin-action']) && ('payment_form' == get_post_meta($invoice, 'wpinv_created_via', true) || 'geodirectory' == get_post_meta($invoice, 'wpinv_created_via', true)); |
|
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | /** |
@@ -368,12 +368,12 @@ discard block |
||
| 368 | 368 | * @param WPInv_Invoice $invoice |
| 369 | 369 | * @param string $note |
| 370 | 370 | */ |
| 371 | - public function user_note( $invoice, $note ) { |
|
| 371 | + public function user_note($invoice, $note) { |
|
| 372 | 372 | |
| 373 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $invoice ); |
|
| 373 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $invoice); |
|
| 374 | 374 | $recipient = $invoice->get_email(); |
| 375 | 375 | |
| 376 | - return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) ); |
|
| 376 | + return $this->send_email($invoice, $email, __FUNCTION__, $recipient, array('customer_note' => $note)); |
|
| 377 | 377 | |
| 378 | 378 | } |
| 379 | 379 | |
@@ -382,9 +382,9 @@ discard block |
||
| 382 | 382 | * |
| 383 | 383 | * @param WPInv_Invoice $invoice |
| 384 | 384 | */ |
| 385 | - public function force_send_overdue_notice( $invoice ) { |
|
| 386 | - $email = new GetPaid_Notification_Email( 'overdue', $invoice ); |
|
| 387 | - return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() ); |
|
| 385 | + public function force_send_overdue_notice($invoice) { |
|
| 386 | + $email = new GetPaid_Notification_Email('overdue', $invoice); |
|
| 387 | + return $this->send_email($invoice, $email, 'overdue', $invoice->get_email()); |
|
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | /** |
@@ -395,37 +395,37 @@ discard block |
||
| 395 | 395 | public function overdue() { |
| 396 | 396 | global $wpdb; |
| 397 | 397 | |
| 398 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 398 | + $email = new GetPaid_Notification_Email(__FUNCTION__); |
|
| 399 | 399 | |
| 400 | 400 | // Fetch reminder days. |
| 401 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 401 | + $reminder_days = array_unique(wp_parse_id_list($email->get_option('days'))); |
|
| 402 | 402 | |
| 403 | 403 | // Abort if non is set. |
| 404 | - if ( empty( $reminder_days ) ) { |
|
| 404 | + if (empty($reminder_days)) { |
|
| 405 | 405 | return; |
| 406 | 406 | } |
| 407 | 407 | |
| 408 | 408 | // Retrieve date query. |
| 409 | - $date_query = $this->get_date_query( $reminder_days ); |
|
| 409 | + $date_query = $this->get_date_query($reminder_days); |
|
| 410 | 410 | |
| 411 | 411 | // Invoices table. |
| 412 | 412 | $table = $wpdb->prefix . 'getpaid_invoices'; |
| 413 | 413 | |
| 414 | 414 | // Fetch invoices. |
| 415 | - $invoices = $wpdb->get_col( |
|
| 415 | + $invoices = $wpdb->get_col( |
|
| 416 | 416 | "SELECT posts.ID FROM $wpdb->posts as posts |
| 417 | 417 | LEFT JOIN $table as invoices ON invoices.post_id = posts.ID |
| 418 | 418 | WHERE posts.post_type = 'wpi_invoice' AND posts.post_status = 'wpi-pending' $date_query"); |
| 419 | 419 | |
| 420 | - foreach ( $invoices as $invoice ) { |
|
| 420 | + foreach ($invoices as $invoice) { |
|
| 421 | 421 | |
| 422 | 422 | // Only send this email for invoices created via the admin page. |
| 423 | - if ( ! $this->is_payment_form_invoice( $invoice ) ) { |
|
| 424 | - $invoice = new WPInv_Invoice( $invoice ); |
|
| 423 | + if (!$this->is_payment_form_invoice($invoice)) { |
|
| 424 | + $invoice = new WPInv_Invoice($invoice); |
|
| 425 | 425 | $email->object = $invoice; |
| 426 | 426 | |
| 427 | - if ( $invoice->needs_payment() ) { |
|
| 428 | - $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() ); |
|
| 427 | + if ($invoice->needs_payment()) { |
|
| 428 | + $this->send_email($invoice, $email, __FUNCTION__, $invoice->get_email()); |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | } |
@@ -440,14 +440,14 @@ discard block |
||
| 440 | 440 | * @param array $reminder_days |
| 441 | 441 | * @return string |
| 442 | 442 | */ |
| 443 | - public function get_date_query( $reminder_days ) { |
|
| 443 | + public function get_date_query($reminder_days) { |
|
| 444 | 444 | |
| 445 | 445 | $date_query = array( |
| 446 | 446 | 'relation' => 'OR' |
| 447 | 447 | ); |
| 448 | 448 | |
| 449 | - foreach ( $reminder_days as $days ) { |
|
| 450 | - $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) ); |
|
| 449 | + foreach ($reminder_days as $days) { |
|
| 450 | + $date = date_parse(date('Y-m-d', strtotime("-$days days", current_time('timestamp')))); |
|
| 451 | 451 | |
| 452 | 452 | $date_query[] = array( |
| 453 | 453 | 'year' => $date['year'], |
@@ -457,7 +457,7 @@ discard block |
||
| 457 | 457 | |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | - $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' ); |
|
| 460 | + $date_query = new WP_Date_Query($date_query, 'invoices.due_date'); |
|
| 461 | 461 | |
| 462 | 462 | return $date_query->get_sql(); |
| 463 | 463 | |
@@ -13,288 +13,288 @@ |
||
| 13 | 13 | class GetPaid_Subscription_Notification_Emails { |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | - * The array of subscription email actions. |
|
| 17 | - * |
|
| 18 | - * @param array |
|
| 19 | - */ |
|
| 20 | - public $subscription_actions; |
|
| 16 | + * The array of subscription email actions. |
|
| 17 | + * |
|
| 18 | + * @param array |
|
| 19 | + */ |
|
| 20 | + public $subscription_actions; |
|
| 21 | 21 | |
| 22 | 22 | /** |
| 23 | - * Class constructor |
|
| 23 | + * Class constructor |
|
| 24 | 24 | * |
| 25 | - */ |
|
| 26 | - public function __construct() { |
|
| 27 | - |
|
| 28 | - $this->subscription_actions = apply_filters( |
|
| 29 | - 'getpaid_notification_email_subscription_triggers', |
|
| 30 | - array( |
|
| 31 | - 'getpaid_subscription_trialling' => 'subscription_trial', |
|
| 32 | - 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
| 33 | - 'getpaid_subscription_expired' => 'subscription_expired', |
|
| 34 | - 'getpaid_subscription_completed' => 'subscription_complete', |
|
| 35 | - 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
| 36 | - ) |
|
| 37 | - ); |
|
| 38 | - |
|
| 39 | - $this->init_hooks(); |
|
| 25 | + */ |
|
| 26 | + public function __construct() { |
|
| 27 | + |
|
| 28 | + $this->subscription_actions = apply_filters( |
|
| 29 | + 'getpaid_notification_email_subscription_triggers', |
|
| 30 | + array( |
|
| 31 | + 'getpaid_subscription_trialling' => 'subscription_trial', |
|
| 32 | + 'getpaid_subscription_cancelled' => 'subscription_cancelled', |
|
| 33 | + 'getpaid_subscription_expired' => 'subscription_expired', |
|
| 34 | + 'getpaid_subscription_completed' => 'subscription_complete', |
|
| 35 | + 'getpaid_daily_maintenance' => 'renewal_reminder', |
|
| 36 | + ) |
|
| 37 | + ); |
|
| 38 | + |
|
| 39 | + $this->init_hooks(); |
|
| 40 | 40 | |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | - * Registers email hooks. |
|
| 45 | - */ |
|
| 46 | - public function init_hooks() { |
|
| 47 | - |
|
| 48 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | - foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 50 | - |
|
| 51 | - $email = new GetPaid_Notification_Email( $email_type ); |
|
| 52 | - |
|
| 53 | - if ( ! $email->is_active() ) { |
|
| 54 | - continue; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - if ( method_exists( $this, $email_type ) ) { |
|
| 58 | - add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 59 | - continue; |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 63 | - |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Filters subscription merge tags. |
|
| 70 | - * |
|
| 71 | - * @param array $merge_tags |
|
| 72 | - * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 73 | - */ |
|
| 74 | - public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 75 | - |
|
| 76 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 77 | - $merge_tags = array_merge( |
|
| 78 | - $merge_tags, |
|
| 79 | - $this->get_subscription_merge_tags( $object ) |
|
| 80 | - ); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - return $merge_tags; |
|
| 84 | - |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Generates subscription merge tags. |
|
| 89 | - * |
|
| 90 | - * @param WPInv_Subscription $subscription |
|
| 91 | - * @return array |
|
| 92 | - */ |
|
| 93 | - public function get_subscription_merge_tags( $subscription ) { |
|
| 94 | - |
|
| 95 | - // Abort if it does not exist. |
|
| 96 | - if ( ! $subscription->get_id() ) { |
|
| 97 | - return array(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $invoice = $subscription->get_parent_invoice(); |
|
| 101 | - return array( |
|
| 102 | - '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | - '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | - '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | - '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | - '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | - '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | - '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
| 109 | - '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 110 | - '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
| 111 | - '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 112 | - ); |
|
| 113 | - |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Checks if we should send a notification for a subscription. |
|
| 118 | - * |
|
| 119 | - * @param WPInv_Invoice $invoice |
|
| 120 | - * @return bool |
|
| 121 | - */ |
|
| 122 | - public function should_send_notification( $invoice ) { |
|
| 123 | - return 0 != $invoice->get_id(); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Returns notification recipients. |
|
| 128 | - * |
|
| 129 | - * @param WPInv_Invoice $invoice |
|
| 130 | - * @return array |
|
| 131 | - */ |
|
| 132 | - public function get_recipients( $invoice ) { |
|
| 133 | - $recipients = array( $invoice->get_email() ); |
|
| 134 | - |
|
| 135 | - $cc = $invoice->get_email_cc(); |
|
| 136 | - |
|
| 137 | - if ( ! empty( $cc ) ) { |
|
| 138 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - return $recipients; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Helper function to send an email. |
|
| 147 | - * |
|
| 148 | - * @param WPInv_Subscription $subscription |
|
| 149 | - * @param GetPaid_Notification_Email $email |
|
| 150 | - * @param string $type |
|
| 151 | - * @param array $extra_args Extra template args. |
|
| 152 | - */ |
|
| 153 | - public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 154 | - |
|
| 155 | - // Abort in case the parent invoice does not exist. |
|
| 156 | - $invoice = $subscription->get_parent_invoice(); |
|
| 157 | - if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 158 | - return; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 162 | - return; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 166 | - |
|
| 167 | - $recipients = $this->get_recipients( $invoice ); |
|
| 168 | - $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 169 | - $merge_tags = $email->get_merge_tags(); |
|
| 170 | - $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | - $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 172 | - $attachments = $email->get_attachments(); |
|
| 173 | - |
|
| 174 | - $result = $mailer->send( |
|
| 175 | - apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 176 | - $subject, |
|
| 177 | - $content, |
|
| 178 | - $attachments |
|
| 179 | - ); |
|
| 180 | - |
|
| 181 | - // Maybe send a copy to the admin. |
|
| 182 | - if ( $email->include_admin_bcc() ) { |
|
| 183 | - $mailer->send( |
|
| 184 | - wpinv_get_admin_email(), |
|
| 185 | - $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 186 | - $content, |
|
| 187 | - $attachments |
|
| 188 | - ); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - if ( $result ) { |
|
| 192 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 193 | - } else { |
|
| 194 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 198 | - |
|
| 199 | - } |
|
| 44 | + * Registers email hooks. |
|
| 45 | + */ |
|
| 46 | + public function init_hooks() { |
|
| 47 | + |
|
| 48 | + add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | + foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 50 | + |
|
| 51 | + $email = new GetPaid_Notification_Email( $email_type ); |
|
| 52 | + |
|
| 53 | + if ( ! $email->is_active() ) { |
|
| 54 | + continue; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + if ( method_exists( $this, $email_type ) ) { |
|
| 58 | + add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 59 | + continue; |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 63 | + |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + } |
|
| 200 | 67 | |
| 201 | 68 | /** |
| 202 | - * Sends a new trial notification. |
|
| 203 | - * |
|
| 204 | - * @param WPInv_Subscription $subscription |
|
| 205 | - */ |
|
| 206 | - public function subscription_trial( $subscription ) { |
|
| 69 | + * Filters subscription merge tags. |
|
| 70 | + * |
|
| 71 | + * @param array $merge_tags |
|
| 72 | + * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
|
| 73 | + */ |
|
| 74 | + public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 207 | 75 | |
| 208 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 209 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 76 | + if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 77 | + $merge_tags = array_merge( |
|
| 78 | + $merge_tags, |
|
| 79 | + $this->get_subscription_merge_tags( $object ) |
|
| 80 | + ); |
|
| 81 | + } |
|
| 210 | 82 | |
| 211 | - } |
|
| 83 | + return $merge_tags; |
|
| 212 | 84 | |
| 213 | - /** |
|
| 214 | - * Sends a cancelled subscription notification. |
|
| 215 | - * |
|
| 216 | - * @param WPInv_Subscription $subscription |
|
| 217 | - */ |
|
| 218 | - public function subscription_cancelled( $subscription ) { |
|
| 85 | + } |
|
| 219 | 86 | |
| 220 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 87 | + /** |
|
| 88 | + * Generates subscription merge tags. |
|
| 89 | + * |
|
| 90 | + * @param WPInv_Subscription $subscription |
|
| 91 | + * @return array |
|
| 92 | + */ |
|
| 93 | + public function get_subscription_merge_tags( $subscription ) { |
|
| 94 | + |
|
| 95 | + // Abort if it does not exist. |
|
| 96 | + if ( ! $subscription->get_id() ) { |
|
| 97 | + return array(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $invoice = $subscription->get_parent_invoice(); |
|
| 101 | + return array( |
|
| 102 | + '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | + '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | + '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | + '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | + '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | + '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | + '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
| 109 | + '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 110 | + '{subscription_bill_times}' => $subscription->get_bill_times(), |
|
| 111 | + '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 112 | + ); |
|
| 222 | 113 | |
| 223 | - } |
|
| 114 | + } |
|
| 224 | 115 | |
| 225 | - /** |
|
| 226 | - * Sends a subscription expired notification. |
|
| 227 | - * |
|
| 228 | - * @param WPInv_Subscription $subscription |
|
| 229 | - */ |
|
| 230 | - public function subscription_expired( $subscription ) { |
|
| 116 | + /** |
|
| 117 | + * Checks if we should send a notification for a subscription. |
|
| 118 | + * |
|
| 119 | + * @param WPInv_Invoice $invoice |
|
| 120 | + * @return bool |
|
| 121 | + */ |
|
| 122 | + public function should_send_notification( $invoice ) { |
|
| 123 | + return 0 != $invoice->get_id(); |
|
| 124 | + } |
|
| 231 | 125 | |
| 232 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 126 | + /** |
|
| 127 | + * Returns notification recipients. |
|
| 128 | + * |
|
| 129 | + * @param WPInv_Invoice $invoice |
|
| 130 | + * @return array |
|
| 131 | + */ |
|
| 132 | + public function get_recipients( $invoice ) { |
|
| 133 | + $recipients = array( $invoice->get_email() ); |
|
| 234 | 134 | |
| 235 | - } |
|
| 135 | + $cc = $invoice->get_email_cc(); |
|
| 236 | 136 | |
| 237 | - /** |
|
| 238 | - * Sends a completed subscription notification. |
|
| 239 | - * |
|
| 240 | - * @param WPInv_Subscription $subscription |
|
| 241 | - */ |
|
| 242 | - public function subscription_complete( $subscription ) { |
|
| 137 | + if ( ! empty( $cc ) ) { |
|
| 138 | + $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | + $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 140 | + } |
|
| 243 | 141 | |
| 244 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 142 | + return $recipients; |
|
| 143 | + } |
|
| 246 | 144 | |
| 247 | - } |
|
| 145 | + /** |
|
| 146 | + * Helper function to send an email. |
|
| 147 | + * |
|
| 148 | + * @param WPInv_Subscription $subscription |
|
| 149 | + * @param GetPaid_Notification_Email $email |
|
| 150 | + * @param string $type |
|
| 151 | + * @param array $extra_args Extra template args. |
|
| 152 | + */ |
|
| 153 | + public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 154 | + |
|
| 155 | + // Abort in case the parent invoice does not exist. |
|
| 156 | + $invoice = $subscription->get_parent_invoice(); |
|
| 157 | + if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 158 | + return; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 162 | + return; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 166 | + |
|
| 167 | + $recipients = $this->get_recipients( $invoice ); |
|
| 168 | + $mailer = new GetPaid_Notification_Email_Sender(); |
|
| 169 | + $merge_tags = $email->get_merge_tags(); |
|
| 170 | + $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | + $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 172 | + $attachments = $email->get_attachments(); |
|
| 173 | + |
|
| 174 | + $result = $mailer->send( |
|
| 175 | + apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 176 | + $subject, |
|
| 177 | + $content, |
|
| 178 | + $attachments |
|
| 179 | + ); |
|
| 180 | + |
|
| 181 | + // Maybe send a copy to the admin. |
|
| 182 | + if ( $email->include_admin_bcc() ) { |
|
| 183 | + $mailer->send( |
|
| 184 | + wpinv_get_admin_email(), |
|
| 185 | + $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 186 | + $content, |
|
| 187 | + $attachments |
|
| 188 | + ); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + if ( $result ) { |
|
| 192 | + $subscription->get_parent_invoice()->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 193 | + } else { |
|
| 194 | + $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 248 | 198 | |
| 249 | - /** |
|
| 250 | - * Sends a subscription renewal reminder notification. |
|
| 251 | - * |
|
| 252 | - */ |
|
| 253 | - public function renewal_reminder() { |
|
| 199 | + } |
|
| 254 | 200 | |
| 255 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 201 | + /** |
|
| 202 | + * Sends a new trial notification. |
|
| 203 | + * |
|
| 204 | + * @param WPInv_Subscription $subscription |
|
| 205 | + */ |
|
| 206 | + public function subscription_trial( $subscription ) { |
|
| 256 | 207 | |
| 257 | - // Fetch reminder days. |
|
| 258 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 208 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 209 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 259 | 210 | |
| 260 | - // Abort if non is set. |
|
| 261 | - if ( empty( $reminder_days ) ) { |
|
| 262 | - return; |
|
| 263 | - } |
|
| 211 | + } |
|
| 264 | 212 | |
| 265 | - // Fetch matching subscriptions. |
|
| 213 | + /** |
|
| 214 | + * Sends a cancelled subscription notification. |
|
| 215 | + * |
|
| 216 | + * @param WPInv_Subscription $subscription |
|
| 217 | + */ |
|
| 218 | + public function subscription_cancelled( $subscription ) { |
|
| 219 | + |
|
| 220 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 222 | + |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Sends a subscription expired notification. |
|
| 227 | + * |
|
| 228 | + * @param WPInv_Subscription $subscription |
|
| 229 | + */ |
|
| 230 | + public function subscription_expired( $subscription ) { |
|
| 231 | + |
|
| 232 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 234 | + |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Sends a completed subscription notification. |
|
| 239 | + * |
|
| 240 | + * @param WPInv_Subscription $subscription |
|
| 241 | + */ |
|
| 242 | + public function subscription_complete( $subscription ) { |
|
| 243 | + |
|
| 244 | + $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 246 | + |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Sends a subscription renewal reminder notification. |
|
| 251 | + * |
|
| 252 | + */ |
|
| 253 | + public function renewal_reminder() { |
|
| 254 | + |
|
| 255 | + $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 256 | + |
|
| 257 | + // Fetch reminder days. |
|
| 258 | + $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 259 | + |
|
| 260 | + // Abort if non is set. |
|
| 261 | + if ( empty( $reminder_days ) ) { |
|
| 262 | + return; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + // Fetch matching subscriptions. |
|
| 266 | 266 | $args = array( |
| 267 | 267 | 'number' => -1, |
| 268 | - 'count_total' => false, |
|
| 269 | - 'status' => 'trialling active', |
|
| 268 | + 'count_total' => false, |
|
| 269 | + 'status' => 'trialling active', |
|
| 270 | 270 | 'date_expires_query' => array( |
| 271 | - 'relation' => 'OR' |
|
| 271 | + 'relation' => 'OR' |
|
| 272 | 272 | ), |
| 273 | - ); |
|
| 273 | + ); |
|
| 274 | 274 | |
| 275 | - foreach ( $reminder_days as $days ) { |
|
| 276 | - $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 275 | + foreach ( $reminder_days as $days ) { |
|
| 276 | + $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 277 | 277 | |
| 278 | - $args['date_expires_query'][] = array( |
|
| 279 | - 'year' => $date['year'], |
|
| 280 | - 'month' => $date['month'], |
|
| 281 | - 'day' => $date['day'], |
|
| 282 | - ); |
|
| 278 | + $args['date_expires_query'][] = array( |
|
| 279 | + 'year' => $date['year'], |
|
| 280 | + 'month' => $date['month'], |
|
| 281 | + 'day' => $date['day'], |
|
| 282 | + ); |
|
| 283 | 283 | |
| 284 | - } |
|
| 284 | + } |
|
| 285 | 285 | |
| 286 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 286 | + $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 287 | 287 | |
| 288 | 288 | foreach ( $subscriptions as $subscription ) { |
| 289 | 289 | |
| 290 | - // Skip packages. |
|
| 291 | - if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 292 | - $email->object = $subscription; |
|
| 293 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 294 | - } |
|
| 290 | + // Skip packages. |
|
| 291 | + if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 292 | + $email->object = $subscription; |
|
| 293 | + $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 294 | + } |
|
| 295 | 295 | |
| 296 | - } |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - } |
|
| 298 | + } |
|
| 299 | 299 | |
| 300 | 300 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | -defined( 'ABSPATH' ) || exit; |
|
| 7 | +defined('ABSPATH') || exit; |
|
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * This class handles subscription notificaiton emails. |
@@ -45,21 +45,21 @@ discard block |
||
| 45 | 45 | */ |
| 46 | 46 | public function init_hooks() { |
| 47 | 47 | |
| 48 | - add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 ); |
|
| 49 | - foreach ( $this->subscription_actions as $hook => $email_type ) { |
|
| 48 | + add_filter('getpaid_get_email_merge_tags', array($this, 'subscription_merge_tags'), 10, 2); |
|
| 49 | + foreach ($this->subscription_actions as $hook => $email_type) { |
|
| 50 | 50 | |
| 51 | - $email = new GetPaid_Notification_Email( $email_type ); |
|
| 51 | + $email = new GetPaid_Notification_Email($email_type); |
|
| 52 | 52 | |
| 53 | - if ( ! $email->is_active() ) { |
|
| 53 | + if (!$email->is_active()) { |
|
| 54 | 54 | continue; |
| 55 | 55 | } |
| 56 | 56 | |
| 57 | - if ( method_exists( $this, $email_type ) ) { |
|
| 58 | - add_action( $hook, array( $this, $email_type ), 100, 2 ); |
|
| 57 | + if (method_exists($this, $email_type)) { |
|
| 58 | + add_action($hook, array($this, $email_type), 100, 2); |
|
| 59 | 59 | continue; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - do_action( 'getpaid_subscription_notification_email_register_hook', $email_type, $hook ); |
|
| 62 | + do_action('getpaid_subscription_notification_email_register_hook', $email_type, $hook); |
|
| 63 | 63 | |
| 64 | 64 | } |
| 65 | 65 | |
@@ -71,12 +71,12 @@ discard block |
||
| 71 | 71 | * @param array $merge_tags |
| 72 | 72 | * @param mixed|WPInv_Invoice|WPInv_Subscription $object |
| 73 | 73 | */ |
| 74 | - public function subscription_merge_tags( $merge_tags, $object ) { |
|
| 74 | + public function subscription_merge_tags($merge_tags, $object) { |
|
| 75 | 75 | |
| 76 | - if ( is_a( $object, 'WPInv_Subscription' ) ) { |
|
| 76 | + if (is_a($object, 'WPInv_Subscription')) { |
|
| 77 | 77 | $merge_tags = array_merge( |
| 78 | 78 | $merge_tags, |
| 79 | - $this->get_subscription_merge_tags( $object ) |
|
| 79 | + $this->get_subscription_merge_tags($object) |
|
| 80 | 80 | ); |
| 81 | 81 | } |
| 82 | 82 | |
@@ -90,25 +90,25 @@ discard block |
||
| 90 | 90 | * @param WPInv_Subscription $subscription |
| 91 | 91 | * @return array |
| 92 | 92 | */ |
| 93 | - public function get_subscription_merge_tags( $subscription ) { |
|
| 93 | + public function get_subscription_merge_tags($subscription) { |
|
| 94 | 94 | |
| 95 | 95 | // Abort if it does not exist. |
| 96 | - if ( ! $subscription->get_id() ) { |
|
| 96 | + if (!$subscription->get_id()) { |
|
| 97 | 97 | return array(); |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - $invoice = $subscription->get_parent_invoice(); |
|
| 100 | + $invoice = $subscription->get_parent_invoice(); |
|
| 101 | 101 | return array( |
| 102 | - '{subscription_renewal_date}' => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ), |
|
| 103 | - '{subscription_created}' => getpaid_format_date_value( $subscription->get_date_created() ), |
|
| 104 | - '{subscription_status}' => sanitize_text_field( $subscription->get_status_label() ), |
|
| 105 | - '{subscription_profile_id}' => sanitize_text_field( $subscription->get_profile_id() ), |
|
| 106 | - '{subscription_id}' => absint( $subscription->get_id() ), |
|
| 107 | - '{subscription_recurring_amount}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ), |
|
| 108 | - '{subscription_initial_amount}' => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ), |
|
| 109 | - '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ), |
|
| 102 | + '{subscription_renewal_date}' => getpaid_format_date_value($subscription->get_next_renewal_date(), __('Never', 'invoicing')), |
|
| 103 | + '{subscription_created}' => getpaid_format_date_value($subscription->get_date_created()), |
|
| 104 | + '{subscription_status}' => sanitize_text_field($subscription->get_status_label()), |
|
| 105 | + '{subscription_profile_id}' => sanitize_text_field($subscription->get_profile_id()), |
|
| 106 | + '{subscription_id}' => absint($subscription->get_id()), |
|
| 107 | + '{subscription_recurring_amount}' => sanitize_text_field(wpinv_price($subscription->get_recurring_amount(), $invoice->get_currency())), |
|
| 108 | + '{subscription_initial_amount}' => sanitize_text_field(wpinv_price($subscription->get_initial_amount(), $invoice->get_currency())), |
|
| 109 | + '{subscription_recurring_period}' => getpaid_get_subscription_period_label($subscription->get_period(), $subscription->get_frequency(), ''), |
|
| 110 | 110 | '{subscription_bill_times}' => $subscription->get_bill_times(), |
| 111 | - '{subscription_url}' => esc_url( $subscription->get_view_url() ), |
|
| 111 | + '{subscription_url}' => esc_url($subscription->get_view_url()), |
|
| 112 | 112 | ); |
| 113 | 113 | |
| 114 | 114 | } |
@@ -119,7 +119,7 @@ discard block |
||
| 119 | 119 | * @param WPInv_Invoice $invoice |
| 120 | 120 | * @return bool |
| 121 | 121 | */ |
| 122 | - public function should_send_notification( $invoice ) { |
|
| 122 | + public function should_send_notification($invoice) { |
|
| 123 | 123 | return 0 != $invoice->get_id(); |
| 124 | 124 | } |
| 125 | 125 | |
@@ -129,14 +129,14 @@ discard block |
||
| 129 | 129 | * @param WPInv_Invoice $invoice |
| 130 | 130 | * @return array |
| 131 | 131 | */ |
| 132 | - public function get_recipients( $invoice ) { |
|
| 133 | - $recipients = array( $invoice->get_email() ); |
|
| 132 | + public function get_recipients($invoice) { |
|
| 133 | + $recipients = array($invoice->get_email()); |
|
| 134 | 134 | |
| 135 | 135 | $cc = $invoice->get_email_cc(); |
| 136 | 136 | |
| 137 | - if ( ! empty( $cc ) ) { |
|
| 138 | - $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) ); |
|
| 139 | - $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) ); |
|
| 137 | + if (!empty($cc)) { |
|
| 138 | + $cc = array_map('sanitize_email', wpinv_parse_list($cc)); |
|
| 139 | + $recipients = array_filter(array_unique(array_merge($recipients, $cc))); |
|
| 140 | 140 | } |
| 141 | 141 | |
| 142 | 142 | return $recipients; |
@@ -150,51 +150,51 @@ discard block |
||
| 150 | 150 | * @param string $type |
| 151 | 151 | * @param array $extra_args Extra template args. |
| 152 | 152 | */ |
| 153 | - public function send_email( $subscription, $email, $type, $extra_args = array() ) { |
|
| 153 | + public function send_email($subscription, $email, $type, $extra_args = array()) { |
|
| 154 | 154 | |
| 155 | 155 | // Abort in case the parent invoice does not exist. |
| 156 | 156 | $invoice = $subscription->get_parent_invoice(); |
| 157 | - if ( ! $this->should_send_notification( $invoice ) ) { |
|
| 157 | + if (!$this->should_send_notification($invoice)) { |
|
| 158 | 158 | return; |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) { |
|
| 161 | + if (apply_filters('getpaid_skip_subscription_email', false, $type, $subscription)) { |
|
| 162 | 162 | return; |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | - do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email ); |
|
| 165 | + do_action('getpaid_before_send_subscription_notification', $type, $subscription, $email); |
|
| 166 | 166 | |
| 167 | - $recipients = $this->get_recipients( $invoice ); |
|
| 167 | + $recipients = $this->get_recipients($invoice); |
|
| 168 | 168 | $mailer = new GetPaid_Notification_Email_Sender(); |
| 169 | 169 | $merge_tags = $email->get_merge_tags(); |
| 170 | - $content = $email->get_content( $merge_tags, $extra_args ); |
|
| 171 | - $subject = $email->add_merge_tags( $email->get_subject(), $merge_tags ); |
|
| 170 | + $content = $email->get_content($merge_tags, $extra_args); |
|
| 171 | + $subject = $email->add_merge_tags($email->get_subject(), $merge_tags); |
|
| 172 | 172 | $attachments = $email->get_attachments(); |
| 173 | 173 | |
| 174 | 174 | $result = $mailer->send( |
| 175 | - apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ), |
|
| 175 | + apply_filters('getpaid_subscription_email_recipients', wpinv_parse_list($recipients), $email), |
|
| 176 | 176 | $subject, |
| 177 | 177 | $content, |
| 178 | 178 | $attachments |
| 179 | 179 | ); |
| 180 | 180 | |
| 181 | 181 | // Maybe send a copy to the admin. |
| 182 | - if ( $email->include_admin_bcc() ) { |
|
| 182 | + if ($email->include_admin_bcc()) { |
|
| 183 | 183 | $mailer->send( |
| 184 | 184 | wpinv_get_admin_email(), |
| 185 | - $subject . __( ' - ADMIN BCC COPY', 'invoicing' ), |
|
| 185 | + $subject . __(' - ADMIN BCC COPY', 'invoicing'), |
|
| 186 | 186 | $content, |
| 187 | 187 | $attachments |
| 188 | 188 | ); |
| 189 | 189 | } |
| 190 | 190 | |
| 191 | - if ( $result ) { |
|
| 192 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Successfully sent %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 191 | + if ($result) { |
|
| 192 | + $subscription->get_parent_invoice()->add_note(sprintf(__('Successfully sent %s notification email.', 'invoicing'), sanitize_key($type)), false, false, true); |
|
| 193 | 193 | } else { |
| 194 | - $subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true ); |
|
| 194 | + $subscription->get_parent_invoice()->add_note(sprintf(__('Failed sending %s notification email.', 'invoicing'), sanitize_key($type)), false, false, true); |
|
| 195 | 195 | } |
| 196 | 196 | |
| 197 | - do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email ); |
|
| 197 | + do_action('getpaid_after_send_subscription_notification', $type, $subscription, $email); |
|
| 198 | 198 | |
| 199 | 199 | } |
| 200 | 200 | |
@@ -203,10 +203,10 @@ discard block |
||
| 203 | 203 | * |
| 204 | 204 | * @param WPInv_Subscription $subscription |
| 205 | 205 | */ |
| 206 | - public function subscription_trial( $subscription ) { |
|
| 206 | + public function subscription_trial($subscription) { |
|
| 207 | 207 | |
| 208 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 209 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 208 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $subscription); |
|
| 209 | + $this->send_email($subscription, $email, __FUNCTION__); |
|
| 210 | 210 | |
| 211 | 211 | } |
| 212 | 212 | |
@@ -215,10 +215,10 @@ discard block |
||
| 215 | 215 | * |
| 216 | 216 | * @param WPInv_Subscription $subscription |
| 217 | 217 | */ |
| 218 | - public function subscription_cancelled( $subscription ) { |
|
| 218 | + public function subscription_cancelled($subscription) { |
|
| 219 | 219 | |
| 220 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 221 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 220 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $subscription); |
|
| 221 | + $this->send_email($subscription, $email, __FUNCTION__); |
|
| 222 | 222 | |
| 223 | 223 | } |
| 224 | 224 | |
@@ -227,10 +227,10 @@ discard block |
||
| 227 | 227 | * |
| 228 | 228 | * @param WPInv_Subscription $subscription |
| 229 | 229 | */ |
| 230 | - public function subscription_expired( $subscription ) { |
|
| 230 | + public function subscription_expired($subscription) { |
|
| 231 | 231 | |
| 232 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 233 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 232 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $subscription); |
|
| 233 | + $this->send_email($subscription, $email, __FUNCTION__); |
|
| 234 | 234 | |
| 235 | 235 | } |
| 236 | 236 | |
@@ -239,10 +239,10 @@ discard block |
||
| 239 | 239 | * |
| 240 | 240 | * @param WPInv_Subscription $subscription |
| 241 | 241 | */ |
| 242 | - public function subscription_complete( $subscription ) { |
|
| 242 | + public function subscription_complete($subscription) { |
|
| 243 | 243 | |
| 244 | - $email = new GetPaid_Notification_Email( __FUNCTION__, $subscription ); |
|
| 245 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 244 | + $email = new GetPaid_Notification_Email(__FUNCTION__, $subscription); |
|
| 245 | + $this->send_email($subscription, $email, __FUNCTION__); |
|
| 246 | 246 | |
| 247 | 247 | } |
| 248 | 248 | |
@@ -252,18 +252,18 @@ discard block |
||
| 252 | 252 | */ |
| 253 | 253 | public function renewal_reminder() { |
| 254 | 254 | |
| 255 | - $email = new GetPaid_Notification_Email( __FUNCTION__ ); |
|
| 255 | + $email = new GetPaid_Notification_Email(__FUNCTION__); |
|
| 256 | 256 | |
| 257 | 257 | // Fetch reminder days. |
| 258 | - $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) ); |
|
| 258 | + $reminder_days = array_unique(wp_parse_id_list($email->get_option('days'))); |
|
| 259 | 259 | |
| 260 | 260 | // Abort if non is set. |
| 261 | - if ( empty( $reminder_days ) ) { |
|
| 261 | + if (empty($reminder_days)) { |
|
| 262 | 262 | return; |
| 263 | 263 | } |
| 264 | 264 | |
| 265 | 265 | // Fetch matching subscriptions. |
| 266 | - $args = array( |
|
| 266 | + $args = array( |
|
| 267 | 267 | 'number' => -1, |
| 268 | 268 | 'count_total' => false, |
| 269 | 269 | 'status' => 'trialling active', |
@@ -272,8 +272,8 @@ discard block |
||
| 272 | 272 | ), |
| 273 | 273 | ); |
| 274 | 274 | |
| 275 | - foreach ( $reminder_days as $days ) { |
|
| 276 | - $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) ); |
|
| 275 | + foreach ($reminder_days as $days) { |
|
| 276 | + $date = date_parse(date('Y-m-d', strtotime("+$days days", current_time('timestamp')))); |
|
| 277 | 277 | |
| 278 | 278 | $args['date_expires_query'][] = array( |
| 279 | 279 | 'year' => $date['year'], |
@@ -283,14 +283,14 @@ discard block |
||
| 283 | 283 | |
| 284 | 284 | } |
| 285 | 285 | |
| 286 | - $subscriptions = new GetPaid_Subscriptions_Query( $args ); |
|
| 286 | + $subscriptions = new GetPaid_Subscriptions_Query($args); |
|
| 287 | 287 | |
| 288 | - foreach ( $subscriptions as $subscription ) { |
|
| 288 | + foreach ($subscriptions as $subscription) { |
|
| 289 | 289 | |
| 290 | 290 | // Skip packages. |
| 291 | - if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) { |
|
| 291 | + if (get_post_meta($subscription->get_product_id(), '_wpinv_type', true) != 'package') { |
|
| 292 | 292 | $email->object = $subscription; |
| 293 | - $this->send_email( $subscription, $email, __FUNCTION__ ); |
|
| 293 | + $this->send_email($subscription, $email, __FUNCTION__); |
|
| 294 | 294 | } |
| 295 | 295 | |
| 296 | 296 | } |
@@ -14,7 +14,7 @@ discard block |
||
| 14 | 14 | */ |
| 15 | 15 | function wpinv_subscriptions_page() { |
| 16 | 16 | |
| 17 | - ?> |
|
| 17 | + ?> |
|
| 18 | 18 | |
| 19 | 19 | <div class="wrap"> |
| 20 | 20 | <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> |
@@ -22,27 +22,27 @@ discard block |
||
| 22 | 22 | |
| 23 | 23 | <?php |
| 24 | 24 | |
| 25 | - // Verify user permissions. |
|
| 26 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 25 | + // Verify user permissions. |
|
| 26 | + if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 27 | 27 | |
| 28 | - echo aui()->alert( |
|
| 29 | - array( |
|
| 30 | - 'type' => 'danger', |
|
| 31 | - 'content' => __( 'You are not permitted to view this page.', 'invoicing' ), |
|
| 32 | - ) |
|
| 33 | - ); |
|
| 28 | + echo aui()->alert( |
|
| 29 | + array( |
|
| 30 | + 'type' => 'danger', |
|
| 31 | + 'content' => __( 'You are not permitted to view this page.', 'invoicing' ), |
|
| 32 | + ) |
|
| 33 | + ); |
|
| 34 | 34 | |
| 35 | - } else if ( ! empty( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) { |
|
| 35 | + } else if ( ! empty( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) { |
|
| 36 | 36 | |
| 37 | - // Display a single subscription. |
|
| 38 | - wpinv_recurring_subscription_details(); |
|
| 39 | - } else { |
|
| 37 | + // Display a single subscription. |
|
| 38 | + wpinv_recurring_subscription_details(); |
|
| 39 | + } else { |
|
| 40 | 40 | |
| 41 | - // Display a list of available subscriptions. |
|
| 42 | - getpaid_print_subscriptions_list(); |
|
| 43 | - } |
|
| 41 | + // Display a list of available subscriptions. |
|
| 42 | + getpaid_print_subscriptions_list(); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - ?> |
|
| 45 | + ?> |
|
| 46 | 46 | |
| 47 | 47 | </div> |
| 48 | 48 | </div> |
@@ -59,10 +59,10 @@ discard block |
||
| 59 | 59 | */ |
| 60 | 60 | function getpaid_print_subscriptions_list() { |
| 61 | 61 | |
| 62 | - $subscribers_table = new WPInv_Subscriptions_List_Table(); |
|
| 63 | - $subscribers_table->prepare_items(); |
|
| 62 | + $subscribers_table = new WPInv_Subscriptions_List_Table(); |
|
| 63 | + $subscribers_table->prepare_items(); |
|
| 64 | 64 | |
| 65 | - ?> |
|
| 65 | + ?> |
|
| 66 | 66 | <form id="subscribers-filter" class="bsui" method="get"> |
| 67 | 67 | <input type="hidden" name="page" value="wpinv-subscriptions" /> |
| 68 | 68 | <?php $subscribers_table->views(); ?> |
@@ -80,27 +80,27 @@ discard block |
||
| 80 | 80 | */ |
| 81 | 81 | function wpinv_recurring_subscription_details() { |
| 82 | 82 | |
| 83 | - // Fetch the subscription. |
|
| 84 | - $sub = new WPInv_Subscription( (int) $_GET['id'] ); |
|
| 85 | - if ( ! $sub->get_id() ) { |
|
| 83 | + // Fetch the subscription. |
|
| 84 | + $sub = new WPInv_Subscription( (int) $_GET['id'] ); |
|
| 85 | + if ( ! $sub->get_id() ) { |
|
| 86 | 86 | |
| 87 | - echo aui()->alert( |
|
| 88 | - array( |
|
| 89 | - 'type' => 'danger', |
|
| 90 | - 'content' => __( 'Subscription not found.', 'invoicing' ), |
|
| 91 | - ) |
|
| 92 | - ); |
|
| 87 | + echo aui()->alert( |
|
| 88 | + array( |
|
| 89 | + 'type' => 'danger', |
|
| 90 | + 'content' => __( 'Subscription not found.', 'invoicing' ), |
|
| 91 | + ) |
|
| 92 | + ); |
|
| 93 | 93 | |
| 94 | - return; |
|
| 95 | - } |
|
| 94 | + return; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - // Use metaboxes to display the subscription details. |
|
| 98 | - add_meta_box( 'getpaid_admin_subscription_details_metabox', __( 'Subscription Details', 'invoicing' ), 'getpaid_admin_subscription_details_metabox', get_current_screen(), 'normal' ); |
|
| 99 | - add_meta_box( 'getpaid_admin_subscription_update_metabox', __( 'Change Status', 'invoicing' ), 'getpaid_admin_subscription_update_metabox', get_current_screen(), 'side' ); |
|
| 100 | - add_meta_box( 'getpaid_admin_subscription_invoice_details_metabox', __( 'Invoices', 'invoicing' ), 'getpaid_admin_subscription_invoice_details_metabox', get_current_screen(), 'advanced' ); |
|
| 101 | - do_action( 'getpaid_admin_single_subscription_register_metabox', $sub ); |
|
| 97 | + // Use metaboxes to display the subscription details. |
|
| 98 | + add_meta_box( 'getpaid_admin_subscription_details_metabox', __( 'Subscription Details', 'invoicing' ), 'getpaid_admin_subscription_details_metabox', get_current_screen(), 'normal' ); |
|
| 99 | + add_meta_box( 'getpaid_admin_subscription_update_metabox', __( 'Change Status', 'invoicing' ), 'getpaid_admin_subscription_update_metabox', get_current_screen(), 'side' ); |
|
| 100 | + add_meta_box( 'getpaid_admin_subscription_invoice_details_metabox', __( 'Invoices', 'invoicing' ), 'getpaid_admin_subscription_invoice_details_metabox', get_current_screen(), 'advanced' ); |
|
| 101 | + do_action( 'getpaid_admin_single_subscription_register_metabox', $sub ); |
|
| 102 | 102 | |
| 103 | - ?> |
|
| 103 | + ?> |
|
| 104 | 104 | |
| 105 | 105 | <form method="post" action="<?php echo admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $sub->get_id() ) ); ?>"> |
| 106 | 106 | |
@@ -140,41 +140,41 @@ discard block |
||
| 140 | 140 | */ |
| 141 | 141 | function getpaid_admin_subscription_details_metabox( $sub ) { |
| 142 | 142 | |
| 143 | - // Prepare subscription detail columns. |
|
| 144 | - $fields = apply_filters( |
|
| 145 | - 'getpaid_subscription_admin_page_fields', |
|
| 146 | - array( |
|
| 147 | - 'subscription' => __( 'Subscription', 'invoicing' ), |
|
| 148 | - 'customer' => __( 'Customer', 'invoicing' ), |
|
| 149 | - 'amount' => __( 'Amount', 'invoicing' ), |
|
| 150 | - 'start_date' => __( 'Start Date', 'invoicing' ), |
|
| 151 | - 'renews_on' => __( 'Next Payment', 'invoicing' ), |
|
| 152 | - 'renewals' => __( 'Payments', 'invoicing' ), |
|
| 153 | - 'item' => __( 'Item', 'invoicing' ), |
|
| 154 | - 'gateway' => __( 'Payment Method', 'invoicing' ), |
|
| 155 | - 'profile_id' => __( 'Profile ID', 'invoicing' ), |
|
| 156 | - 'status' => __( 'Status', 'invoicing' ), |
|
| 157 | - ) |
|
| 158 | - ); |
|
| 159 | - |
|
| 160 | - if ( ! $sub->is_active() ) { |
|
| 161 | - |
|
| 162 | - if ( isset( $fields['renews_on'] ) ) { |
|
| 163 | - unset( $fields['renews_on'] ); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - if ( isset( $fields['gateway'] ) ) { |
|
| 167 | - unset( $fields['gateway'] ); |
|
| 168 | - } |
|
| 143 | + // Prepare subscription detail columns. |
|
| 144 | + $fields = apply_filters( |
|
| 145 | + 'getpaid_subscription_admin_page_fields', |
|
| 146 | + array( |
|
| 147 | + 'subscription' => __( 'Subscription', 'invoicing' ), |
|
| 148 | + 'customer' => __( 'Customer', 'invoicing' ), |
|
| 149 | + 'amount' => __( 'Amount', 'invoicing' ), |
|
| 150 | + 'start_date' => __( 'Start Date', 'invoicing' ), |
|
| 151 | + 'renews_on' => __( 'Next Payment', 'invoicing' ), |
|
| 152 | + 'renewals' => __( 'Payments', 'invoicing' ), |
|
| 153 | + 'item' => __( 'Item', 'invoicing' ), |
|
| 154 | + 'gateway' => __( 'Payment Method', 'invoicing' ), |
|
| 155 | + 'profile_id' => __( 'Profile ID', 'invoicing' ), |
|
| 156 | + 'status' => __( 'Status', 'invoicing' ), |
|
| 157 | + ) |
|
| 158 | + ); |
|
| 159 | + |
|
| 160 | + if ( ! $sub->is_active() ) { |
|
| 161 | + |
|
| 162 | + if ( isset( $fields['renews_on'] ) ) { |
|
| 163 | + unset( $fields['renews_on'] ); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + if ( isset( $fields['gateway'] ) ) { |
|
| 167 | + unset( $fields['gateway'] ); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - } |
|
| 170 | + } |
|
| 171 | 171 | |
| 172 | - $profile_id = $sub->get_profile_id(); |
|
| 173 | - if ( empty( $profile_id ) && isset( $fields['profile_id'] ) ) { |
|
| 174 | - unset( $fields['profile_id'] ); |
|
| 175 | - } |
|
| 172 | + $profile_id = $sub->get_profile_id(); |
|
| 173 | + if ( empty( $profile_id ) && isset( $fields['profile_id'] ) ) { |
|
| 174 | + unset( $fields['profile_id'] ); |
|
| 175 | + } |
|
| 176 | 176 | |
| 177 | - ?> |
|
| 177 | + ?> |
|
| 178 | 178 | |
| 179 | 179 | <table class="table table-borderless" style="font-size: 14px;"> |
| 180 | 180 | <tbody> |
@@ -208,20 +208,20 @@ discard block |
||
| 208 | 208 | */ |
| 209 | 209 | function getpaid_admin_subscription_metabox_display_customer( $subscription ) { |
| 210 | 210 | |
| 211 | - $username = __( '(Missing User)', 'invoicing' ); |
|
| 211 | + $username = __( '(Missing User)', 'invoicing' ); |
|
| 212 | 212 | |
| 213 | - $user = get_userdata( $subscription->get_customer_id() ); |
|
| 214 | - if ( $user ) { |
|
| 213 | + $user = get_userdata( $subscription->get_customer_id() ); |
|
| 214 | + if ( $user ) { |
|
| 215 | 215 | |
| 216 | - $username = sprintf( |
|
| 217 | - '<a href="user-edit.php?user_id=%s">%s</a>', |
|
| 218 | - absint( $user->ID ), |
|
| 219 | - ! empty( $user->display_name ) ? sanitize_text_field( $user->display_name ) : sanitize_email( $user->user_email ) |
|
| 220 | - ); |
|
| 216 | + $username = sprintf( |
|
| 217 | + '<a href="user-edit.php?user_id=%s">%s</a>', |
|
| 218 | + absint( $user->ID ), |
|
| 219 | + ! empty( $user->display_name ) ? sanitize_text_field( $user->display_name ) : sanitize_email( $user->user_email ) |
|
| 220 | + ); |
|
| 221 | 221 | |
| 222 | - } |
|
| 222 | + } |
|
| 223 | 223 | |
| 224 | - echo $username; |
|
| 224 | + echo $username; |
|
| 225 | 225 | } |
| 226 | 226 | add_action( 'getpaid_subscription_admin_display_customer', 'getpaid_admin_subscription_metabox_display_customer' ); |
| 227 | 227 | |
@@ -231,8 +231,8 @@ discard block |
||
| 231 | 231 | * @param WPInv_Subscription $subscription |
| 232 | 232 | */ |
| 233 | 233 | function getpaid_admin_subscription_metabox_display_amount( $subscription ) { |
| 234 | - $amount = sanitize_text_field( getpaid_get_formatted_subscription_amount( $subscription ) ); |
|
| 235 | - echo "<span>$amount</span>"; |
|
| 234 | + $amount = sanitize_text_field( getpaid_get_formatted_subscription_amount( $subscription ) ); |
|
| 235 | + echo "<span>$amount</span>"; |
|
| 236 | 236 | } |
| 237 | 237 | add_action( 'getpaid_subscription_admin_display_amount', 'getpaid_admin_subscription_metabox_display_amount' ); |
| 238 | 238 | |
@@ -242,7 +242,7 @@ discard block |
||
| 242 | 242 | * @param WPInv_Subscription $subscription |
| 243 | 243 | */ |
| 244 | 244 | function getpaid_admin_subscription_metabox_display_id( $subscription ) { |
| 245 | - echo '#' . absint( $subscription->get_id() ); |
|
| 245 | + echo '#' . absint( $subscription->get_id() ); |
|
| 246 | 246 | } |
| 247 | 247 | add_action( 'getpaid_subscription_admin_display_subscription', 'getpaid_admin_subscription_metabox_display_id' ); |
| 248 | 248 | |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | * @param WPInv_Subscription $subscription |
| 253 | 253 | */ |
| 254 | 254 | function getpaid_admin_subscription_metabox_display_start_date( $subscription ) { |
| 255 | - echo getpaid_format_date_value( $subscription->get_date_created() ); |
|
| 255 | + echo getpaid_format_date_value( $subscription->get_date_created() ); |
|
| 256 | 256 | } |
| 257 | 257 | add_action( 'getpaid_subscription_admin_display_start_date', 'getpaid_admin_subscription_metabox_display_start_date' ); |
| 258 | 258 | |
@@ -262,7 +262,7 @@ discard block |
||
| 262 | 262 | * @param WPInv_Subscription $subscription |
| 263 | 263 | */ |
| 264 | 264 | function getpaid_admin_subscription_metabox_display_renews_on( $subscription ) { |
| 265 | - echo getpaid_format_date_value( $subscription->get_expiration() ); |
|
| 265 | + echo getpaid_format_date_value( $subscription->get_expiration() ); |
|
| 266 | 266 | } |
| 267 | 267 | add_action( 'getpaid_subscription_admin_display_renews_on', 'getpaid_admin_subscription_metabox_display_renews_on' ); |
| 268 | 268 | |
@@ -272,8 +272,8 @@ discard block |
||
| 272 | 272 | * @param WPInv_Subscription $subscription |
| 273 | 273 | */ |
| 274 | 274 | function getpaid_admin_subscription_metabox_display_renewals( $subscription ) { |
| 275 | - $max_bills = $subscription->get_bill_times(); |
|
| 276 | - echo $subscription->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "∞" : $max_bills ); |
|
| 275 | + $max_bills = $subscription->get_bill_times(); |
|
| 276 | + echo $subscription->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "∞" : $max_bills ); |
|
| 277 | 277 | } |
| 278 | 278 | add_action( 'getpaid_subscription_admin_display_renewals', 'getpaid_admin_subscription_metabox_display_renewals' ); |
| 279 | 279 | /** |
@@ -283,16 +283,16 @@ discard block |
||
| 283 | 283 | */ |
| 284 | 284 | function getpaid_admin_subscription_metabox_display_item( $subscription ) { |
| 285 | 285 | |
| 286 | - $item = get_post( $subscription->get_product_id() ); |
|
| 286 | + $item = get_post( $subscription->get_product_id() ); |
|
| 287 | 287 | |
| 288 | - if ( ! empty( $item ) ) { |
|
| 289 | - $link = get_edit_post_link( $item ); |
|
| 290 | - $link = esc_url( $link ); |
|
| 291 | - $name = esc_html( get_the_title( $item ) ); |
|
| 292 | - echo "<a href='$link'>$name</a>"; |
|
| 293 | - } else { |
|
| 294 | - echo sprintf( __( 'Item #%s', 'invoicing' ), $subscription->get_product_id() ); |
|
| 295 | - } |
|
| 288 | + if ( ! empty( $item ) ) { |
|
| 289 | + $link = get_edit_post_link( $item ); |
|
| 290 | + $link = esc_url( $link ); |
|
| 291 | + $name = esc_html( get_the_title( $item ) ); |
|
| 292 | + echo "<a href='$link'>$name</a>"; |
|
| 293 | + } else { |
|
| 294 | + echo sprintf( __( 'Item #%s', 'invoicing' ), $subscription->get_product_id() ); |
|
| 295 | + } |
|
| 296 | 296 | |
| 297 | 297 | } |
| 298 | 298 | add_action( 'getpaid_subscription_admin_display_item', 'getpaid_admin_subscription_metabox_display_item' ); |
@@ -304,13 +304,13 @@ discard block |
||
| 304 | 304 | */ |
| 305 | 305 | function getpaid_admin_subscription_metabox_display_gateway( $subscription ) { |
| 306 | 306 | |
| 307 | - $gateway = $subscription->get_gateway(); |
|
| 307 | + $gateway = $subscription->get_gateway(); |
|
| 308 | 308 | |
| 309 | - if ( ! empty( $gateway ) ) { |
|
| 310 | - echo sanitize_text_field( wpinv_get_gateway_admin_label( $gateway ) ); |
|
| 311 | - } else { |
|
| 312 | - echo "—"; |
|
| 313 | - } |
|
| 309 | + if ( ! empty( $gateway ) ) { |
|
| 310 | + echo sanitize_text_field( wpinv_get_gateway_admin_label( $gateway ) ); |
|
| 311 | + } else { |
|
| 312 | + echo "—"; |
|
| 313 | + } |
|
| 314 | 314 | |
| 315 | 315 | } |
| 316 | 316 | add_action( 'getpaid_subscription_admin_display_gateway', 'getpaid_admin_subscription_metabox_display_gateway' ); |
@@ -321,7 +321,7 @@ discard block |
||
| 321 | 321 | * @param WPInv_Subscription $subscription |
| 322 | 322 | */ |
| 323 | 323 | function getpaid_admin_subscription_metabox_display_status( $subscription ) { |
| 324 | - echo $subscription->get_status_label_html(); |
|
| 324 | + echo $subscription->get_status_label_html(); |
|
| 325 | 325 | } |
| 326 | 326 | add_action( 'getpaid_subscription_admin_display_status', 'getpaid_admin_subscription_metabox_display_status' ); |
| 327 | 327 | |
@@ -332,29 +332,29 @@ discard block |
||
| 332 | 332 | */ |
| 333 | 333 | function getpaid_admin_subscription_metabox_display_profile_id( $subscription ) { |
| 334 | 334 | |
| 335 | - $profile_id = $subscription->get_profile_id(); |
|
| 336 | - |
|
| 337 | - $input = aui()->input( |
|
| 338 | - array( |
|
| 339 | - 'type' => 'text', |
|
| 340 | - 'id' => 'wpinv_subscription_profile_id', |
|
| 341 | - 'name' => 'wpinv_subscription_profile_id', |
|
| 342 | - 'label' => __( 'Profile Id', 'invoicing' ), |
|
| 343 | - 'label_type' => 'hidden', |
|
| 344 | - 'placeholder' => __( 'Profile Id', 'invoicing' ), |
|
| 345 | - 'value' => sanitize_text_field( $profile_id ), |
|
| 346 | - 'input_group_right' => '', |
|
| 347 | - 'no_wrap' => true, |
|
| 348 | - ) |
|
| 349 | - ); |
|
| 350 | - |
|
| 351 | - echo str_ireplace( 'form-control', 'regular-text', $input ); |
|
| 352 | - |
|
| 353 | - $url = apply_filters( 'getpaid_remote_subscription_profile_url', '', $subscription ); |
|
| 354 | - if ( ! empty( $url ) ) { |
|
| 355 | - $url = esc_url_raw( $url ); |
|
| 356 | - echo ' <a href="' . $url . '" title="' . __( 'View in Gateway', 'invoicing' ) . '" target="_blank"><i class="fas fa-external-link-alt fa-xs fa-fw align-top"></i></a>'; |
|
| 357 | - } |
|
| 335 | + $profile_id = $subscription->get_profile_id(); |
|
| 336 | + |
|
| 337 | + $input = aui()->input( |
|
| 338 | + array( |
|
| 339 | + 'type' => 'text', |
|
| 340 | + 'id' => 'wpinv_subscription_profile_id', |
|
| 341 | + 'name' => 'wpinv_subscription_profile_id', |
|
| 342 | + 'label' => __( 'Profile Id', 'invoicing' ), |
|
| 343 | + 'label_type' => 'hidden', |
|
| 344 | + 'placeholder' => __( 'Profile Id', 'invoicing' ), |
|
| 345 | + 'value' => sanitize_text_field( $profile_id ), |
|
| 346 | + 'input_group_right' => '', |
|
| 347 | + 'no_wrap' => true, |
|
| 348 | + ) |
|
| 349 | + ); |
|
| 350 | + |
|
| 351 | + echo str_ireplace( 'form-control', 'regular-text', $input ); |
|
| 352 | + |
|
| 353 | + $url = apply_filters( 'getpaid_remote_subscription_profile_url', '', $subscription ); |
|
| 354 | + if ( ! empty( $url ) ) { |
|
| 355 | + $url = esc_url_raw( $url ); |
|
| 356 | + echo ' <a href="' . $url . '" title="' . __( 'View in Gateway', 'invoicing' ) . '" target="_blank"><i class="fas fa-external-link-alt fa-xs fa-fw align-top"></i></a>'; |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | 359 | } |
| 360 | 360 | add_action( 'getpaid_subscription_admin_display_profile_id', 'getpaid_admin_subscription_metabox_display_profile_id' ); |
@@ -366,39 +366,39 @@ discard block |
||
| 366 | 366 | */ |
| 367 | 367 | function getpaid_admin_subscription_update_metabox( $subscription ) { |
| 368 | 368 | |
| 369 | - ?> |
|
| 369 | + ?> |
|
| 370 | 370 | <div class="mt-3"> |
| 371 | 371 | |
| 372 | 372 | <?php |
| 373 | - echo aui()->select( |
|
| 374 | - array( |
|
| 375 | - 'options' => getpaid_get_subscription_statuses(), |
|
| 376 | - 'name' => 'subscription_status', |
|
| 377 | - 'id' => 'subscription_status_update_select', |
|
| 378 | - 'required' => true, |
|
| 379 | - 'no_wrap' => false, |
|
| 380 | - 'label' => __( 'Subscription Status', 'invoicing' ), |
|
| 381 | - 'help_text' => __( 'Updating the status will trigger related actions and hooks', 'invoicing' ), |
|
| 382 | - 'select2' => true, |
|
| 383 | - 'value' => $subscription->get_status( 'edit' ), |
|
| 384 | - ) |
|
| 385 | - ); |
|
| 386 | - ?> |
|
| 373 | + echo aui()->select( |
|
| 374 | + array( |
|
| 375 | + 'options' => getpaid_get_subscription_statuses(), |
|
| 376 | + 'name' => 'subscription_status', |
|
| 377 | + 'id' => 'subscription_status_update_select', |
|
| 378 | + 'required' => true, |
|
| 379 | + 'no_wrap' => false, |
|
| 380 | + 'label' => __( 'Subscription Status', 'invoicing' ), |
|
| 381 | + 'help_text' => __( 'Updating the status will trigger related actions and hooks', 'invoicing' ), |
|
| 382 | + 'select2' => true, |
|
| 383 | + 'value' => $subscription->get_status( 'edit' ), |
|
| 384 | + ) |
|
| 385 | + ); |
|
| 386 | + ?> |
|
| 387 | 387 | |
| 388 | 388 | <div class="mt-2 px-3 py-2 bg-light border-top" style="margin: -12px;"> |
| 389 | 389 | |
| 390 | 390 | <?php |
| 391 | - submit_button( __( 'Update', 'invoicing' ), 'primary', 'submit', false ); |
|
| 391 | + submit_button( __( 'Update', 'invoicing' ), 'primary', 'submit', false ); |
|
| 392 | 392 | |
| 393 | - $url = esc_url( wp_nonce_url( add_query_arg( 'getpaid-admin-action', 'subscription_manual_renew' ), 'getpaid-nonce', 'getpaid-nonce' ) ); |
|
| 394 | - $anchor = __( 'Renew Subscription', 'invoicing' ); |
|
| 395 | - $title = esc_attr__( 'Are you sure you want to extend the subscription and generate a new invoice that will be automatically marked as paid?', 'invoicing' ); |
|
| 393 | + $url = esc_url( wp_nonce_url( add_query_arg( 'getpaid-admin-action', 'subscription_manual_renew' ), 'getpaid-nonce', 'getpaid-nonce' ) ); |
|
| 394 | + $anchor = __( 'Renew Subscription', 'invoicing' ); |
|
| 395 | + $title = esc_attr__( 'Are you sure you want to extend the subscription and generate a new invoice that will be automatically marked as paid?', 'invoicing' ); |
|
| 396 | 396 | |
| 397 | - if ( $subscription->is_active() ) { |
|
| 398 | - echo "<a href='$url' class='float-right text-muted' onclick='return confirm(\"$title\")'>$anchor</a>"; |
|
| 399 | - } |
|
| 397 | + if ( $subscription->is_active() ) { |
|
| 398 | + echo "<a href='$url' class='float-right text-muted' onclick='return confirm(\"$title\")'>$anchor</a>"; |
|
| 399 | + } |
|
| 400 | 400 | |
| 401 | - echo '</div></div>'; |
|
| 401 | + echo '</div></div>'; |
|
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | /** |
@@ -408,33 +408,33 @@ discard block |
||
| 408 | 408 | */ |
| 409 | 409 | function getpaid_admin_subscription_invoice_details_metabox( $subscription ) { |
| 410 | 410 | |
| 411 | - $columns = apply_filters( |
|
| 412 | - 'getpaid_subscription_related_invoices_columns', |
|
| 413 | - array( |
|
| 414 | - 'invoice' => __( 'Invoice', 'invoicing' ), |
|
| 415 | - 'relationship' => __( 'Relationship', 'invoicing' ), |
|
| 416 | - 'date' => __( 'Date', 'invoicing' ), |
|
| 417 | - 'status' => __( 'Status', 'invoicing' ), |
|
| 418 | - 'total' => __( 'Total', 'invoicing' ), |
|
| 419 | - ), |
|
| 420 | - $subscription |
|
| 421 | - ); |
|
| 422 | - |
|
| 423 | - // Prepare the invoices. |
|
| 424 | - $payments = $subscription->get_child_payments( ! is_admin() ); |
|
| 425 | - $parent = $subscription->get_parent_invoice(); |
|
| 426 | - |
|
| 427 | - if ( $parent->get_id() ) { |
|
| 428 | - $payments = array_merge( array( $parent ), $payments ); |
|
| 429 | - } |
|
| 411 | + $columns = apply_filters( |
|
| 412 | + 'getpaid_subscription_related_invoices_columns', |
|
| 413 | + array( |
|
| 414 | + 'invoice' => __( 'Invoice', 'invoicing' ), |
|
| 415 | + 'relationship' => __( 'Relationship', 'invoicing' ), |
|
| 416 | + 'date' => __( 'Date', 'invoicing' ), |
|
| 417 | + 'status' => __( 'Status', 'invoicing' ), |
|
| 418 | + 'total' => __( 'Total', 'invoicing' ), |
|
| 419 | + ), |
|
| 420 | + $subscription |
|
| 421 | + ); |
|
| 422 | + |
|
| 423 | + // Prepare the invoices. |
|
| 424 | + $payments = $subscription->get_child_payments( ! is_admin() ); |
|
| 425 | + $parent = $subscription->get_parent_invoice(); |
|
| 426 | + |
|
| 427 | + if ( $parent->get_id() ) { |
|
| 428 | + $payments = array_merge( array( $parent ), $payments ); |
|
| 429 | + } |
|
| 430 | 430 | |
| 431 | - $table_class = 'w-100 bg-white'; |
|
| 431 | + $table_class = 'w-100 bg-white'; |
|
| 432 | 432 | |
| 433 | - if ( ! is_admin() ) { |
|
| 434 | - $table_class = 'table table-bordered table-striped'; |
|
| 435 | - } |
|
| 433 | + if ( ! is_admin() ) { |
|
| 434 | + $table_class = 'table table-bordered table-striped'; |
|
| 435 | + } |
|
| 436 | 436 | |
| 437 | - ?> |
|
| 437 | + ?> |
|
| 438 | 438 | <div class="m-0" style="overflow: auto;"> |
| 439 | 439 | |
| 440 | 440 | <table class="<?php echo $table_class; ?>"> |
@@ -442,13 +442,13 @@ discard block |
||
| 442 | 442 | <thead> |
| 443 | 443 | <tr> |
| 444 | 444 | <?php |
| 445 | - foreach ( $columns as $key => $label ) { |
|
| 446 | - $key = esc_attr( $key ); |
|
| 447 | - $label = sanitize_text_field( $label ); |
|
| 445 | + foreach ( $columns as $key => $label ) { |
|
| 446 | + $key = esc_attr( $key ); |
|
| 447 | + $label = sanitize_text_field( $label ); |
|
| 448 | 448 | |
| 449 | - echo "<th class='subscription-invoice-field-$key bg-light p-2 text-left color-dark font-weight-bold'>$label</th>"; |
|
| 450 | - } |
|
| 451 | - ?> |
|
| 449 | + echo "<th class='subscription-invoice-field-$key bg-light p-2 text-left color-dark font-weight-bold'>$label</th>"; |
|
| 450 | + } |
|
| 451 | + ?> |
|
| 452 | 452 | </tr> |
| 453 | 453 | </thead> |
| 454 | 454 | |
@@ -464,66 +464,66 @@ discard block |
||
| 464 | 464 | |
| 465 | 465 | <?php |
| 466 | 466 | |
| 467 | - foreach( $payments as $payment ) : |
|
| 467 | + foreach( $payments as $payment ) : |
|
| 468 | 468 | |
| 469 | - // Ensure that we have an invoice. |
|
| 470 | - $payment = new WPInv_Invoice( $payment ); |
|
| 469 | + // Ensure that we have an invoice. |
|
| 470 | + $payment = new WPInv_Invoice( $payment ); |
|
| 471 | 471 | |
| 472 | - // Abort if the invoice is invalid. |
|
| 473 | - if ( ! $payment->get_id() ) { |
|
| 474 | - continue; |
|
| 475 | - } |
|
| 472 | + // Abort if the invoice is invalid. |
|
| 473 | + if ( ! $payment->get_id() ) { |
|
| 474 | + continue; |
|
| 475 | + } |
|
| 476 | 476 | |
| 477 | - echo '<tr>'; |
|
| 477 | + echo '<tr>'; |
|
| 478 | 478 | |
| 479 | - foreach ( array_keys( $columns ) as $key ) { |
|
| 479 | + foreach ( array_keys( $columns ) as $key ) { |
|
| 480 | 480 | |
| 481 | - echo '<td class="p-2 text-left">'; |
|
| 481 | + echo '<td class="p-2 text-left">'; |
|
| 482 | 482 | |
| 483 | - switch( $key ) { |
|
| 483 | + switch( $key ) { |
|
| 484 | 484 | |
| 485 | - case 'total': |
|
| 486 | - echo '<strong>' . wpinv_price( $payment->get_total(), $payment->get_currency() ) . '</strong>'; |
|
| 487 | - break; |
|
| 485 | + case 'total': |
|
| 486 | + echo '<strong>' . wpinv_price( $payment->get_total(), $payment->get_currency() ) . '</strong>'; |
|
| 487 | + break; |
|
| 488 | 488 | |
| 489 | - case 'relationship': |
|
| 490 | - echo $payment->is_renewal() ? __( 'Renewal Invoice', 'invoicing' ) : __( 'Initial Invoice', 'invoicing' ); |
|
| 491 | - break; |
|
| 489 | + case 'relationship': |
|
| 490 | + echo $payment->is_renewal() ? __( 'Renewal Invoice', 'invoicing' ) : __( 'Initial Invoice', 'invoicing' ); |
|
| 491 | + break; |
|
| 492 | 492 | |
| 493 | - case 'date': |
|
| 494 | - echo getpaid_format_date_value( $payment->get_date_created() ); |
|
| 495 | - break; |
|
| 493 | + case 'date': |
|
| 494 | + echo getpaid_format_date_value( $payment->get_date_created() ); |
|
| 495 | + break; |
|
| 496 | 496 | |
| 497 | - case 'status': |
|
| 497 | + case 'status': |
|
| 498 | 498 | |
| 499 | - $status = $payment->get_status_nicename(); |
|
| 500 | - if ( is_admin() ) { |
|
| 501 | - $status = $payment->get_status_label_html(); |
|
| 502 | - } |
|
| 499 | + $status = $payment->get_status_nicename(); |
|
| 500 | + if ( is_admin() ) { |
|
| 501 | + $status = $payment->get_status_label_html(); |
|
| 502 | + } |
|
| 503 | 503 | |
| 504 | - echo $status; |
|
| 505 | - break; |
|
| 504 | + echo $status; |
|
| 505 | + break; |
|
| 506 | 506 | |
| 507 | - case 'invoice': |
|
| 508 | - $link = esc_url( get_edit_post_link( $payment->get_id() ) ); |
|
| 507 | + case 'invoice': |
|
| 508 | + $link = esc_url( get_edit_post_link( $payment->get_id() ) ); |
|
| 509 | 509 | |
| 510 | - if ( ! is_admin() ) { |
|
| 511 | - $link = esc_url( $payment->get_view_url() ); |
|
| 512 | - } |
|
| 510 | + if ( ! is_admin() ) { |
|
| 511 | + $link = esc_url( $payment->get_view_url() ); |
|
| 512 | + } |
|
| 513 | 513 | |
| 514 | - $invoice = sanitize_text_field( $payment->get_number() ); |
|
| 515 | - echo "<a href='$link'>$invoice</a>"; |
|
| 516 | - break; |
|
| 517 | - } |
|
| 514 | + $invoice = sanitize_text_field( $payment->get_number() ); |
|
| 515 | + echo "<a href='$link'>$invoice</a>"; |
|
| 516 | + break; |
|
| 517 | + } |
|
| 518 | 518 | |
| 519 | - echo '</td>'; |
|
| 519 | + echo '</td>'; |
|
| 520 | 520 | |
| 521 | - } |
|
| 521 | + } |
|
| 522 | 522 | |
| 523 | - echo '</tr>'; |
|
| 523 | + echo '</tr>'; |
|
| 524 | 524 | |
| 525 | - endforeach; |
|
| 526 | - ?> |
|
| 525 | + endforeach; |
|
| 526 | + ?> |
|
| 527 | 527 | |
| 528 | 528 | </tbody> |
| 529 | 529 | |
@@ -3,7 +3,7 @@ discard block |
||
| 3 | 3 | * Contains functions that display the subscriptions admin page. |
| 4 | 4 | */ |
| 5 | 5 | |
| 6 | -defined( 'ABSPATH' ) || exit; |
|
| 6 | +defined('ABSPATH') || exit; |
|
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * Render the Subscriptions page |
@@ -17,22 +17,22 @@ discard block |
||
| 17 | 17 | ?> |
| 18 | 18 | |
| 19 | 19 | <div class="wrap"> |
| 20 | - <h1><?php echo esc_html( get_admin_page_title() ); ?></h1> |
|
| 20 | + <h1><?php echo esc_html(get_admin_page_title()); ?></h1> |
|
| 21 | 21 | <div class="bsui"> |
| 22 | 22 | |
| 23 | 23 | <?php |
| 24 | 24 | |
| 25 | 25 | // Verify user permissions. |
| 26 | - if ( ! wpinv_current_user_can_manage_invoicing() ) { |
|
| 26 | + if (!wpinv_current_user_can_manage_invoicing()) { |
|
| 27 | 27 | |
| 28 | 28 | echo aui()->alert( |
| 29 | 29 | array( |
| 30 | 30 | 'type' => 'danger', |
| 31 | - 'content' => __( 'You are not permitted to view this page.', 'invoicing' ), |
|
| 31 | + 'content' => __('You are not permitted to view this page.', 'invoicing'), |
|
| 32 | 32 | ) |
| 33 | 33 | ); |
| 34 | 34 | |
| 35 | - } else if ( ! empty( $_GET['id'] ) && is_numeric( $_GET['id'] ) ) { |
|
| 35 | + } else if (!empty($_GET['id']) && is_numeric($_GET['id'])) { |
|
| 36 | 36 | |
| 37 | 37 | // Display a single subscription. |
| 38 | 38 | wpinv_recurring_subscription_details(); |
@@ -81,13 +81,13 @@ discard block |
||
| 81 | 81 | function wpinv_recurring_subscription_details() { |
| 82 | 82 | |
| 83 | 83 | // Fetch the subscription. |
| 84 | - $sub = new WPInv_Subscription( (int) $_GET['id'] ); |
|
| 85 | - if ( ! $sub->get_id() ) { |
|
| 84 | + $sub = new WPInv_Subscription((int) $_GET['id']); |
|
| 85 | + if (!$sub->get_id()) { |
|
| 86 | 86 | |
| 87 | 87 | echo aui()->alert( |
| 88 | 88 | array( |
| 89 | 89 | 'type' => 'danger', |
| 90 | - 'content' => __( 'Subscription not found.', 'invoicing' ), |
|
| 90 | + 'content' => __('Subscription not found.', 'invoicing'), |
|
| 91 | 91 | ) |
| 92 | 92 | ); |
| 93 | 93 | |
@@ -95,31 +95,31 @@ discard block |
||
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | // Use metaboxes to display the subscription details. |
| 98 | - add_meta_box( 'getpaid_admin_subscription_details_metabox', __( 'Subscription Details', 'invoicing' ), 'getpaid_admin_subscription_details_metabox', get_current_screen(), 'normal' ); |
|
| 99 | - add_meta_box( 'getpaid_admin_subscription_update_metabox', __( 'Change Status', 'invoicing' ), 'getpaid_admin_subscription_update_metabox', get_current_screen(), 'side' ); |
|
| 100 | - add_meta_box( 'getpaid_admin_subscription_invoice_details_metabox', __( 'Invoices', 'invoicing' ), 'getpaid_admin_subscription_invoice_details_metabox', get_current_screen(), 'advanced' ); |
|
| 101 | - do_action( 'getpaid_admin_single_subscription_register_metabox', $sub ); |
|
| 98 | + add_meta_box('getpaid_admin_subscription_details_metabox', __('Subscription Details', 'invoicing'), 'getpaid_admin_subscription_details_metabox', get_current_screen(), 'normal'); |
|
| 99 | + add_meta_box('getpaid_admin_subscription_update_metabox', __('Change Status', 'invoicing'), 'getpaid_admin_subscription_update_metabox', get_current_screen(), 'side'); |
|
| 100 | + add_meta_box('getpaid_admin_subscription_invoice_details_metabox', __('Invoices', 'invoicing'), 'getpaid_admin_subscription_invoice_details_metabox', get_current_screen(), 'advanced'); |
|
| 101 | + do_action('getpaid_admin_single_subscription_register_metabox', $sub); |
|
| 102 | 102 | |
| 103 | 103 | ?> |
| 104 | 104 | |
| 105 | - <form method="post" action="<?php echo admin_url( 'admin.php?page=wpinv-subscriptions&id=' . absint( $sub->get_id() ) ); ?>"> |
|
| 105 | + <form method="post" action="<?php echo admin_url('admin.php?page=wpinv-subscriptions&id=' . absint($sub->get_id())); ?>"> |
|
| 106 | 106 | |
| 107 | - <?php wp_nonce_field( 'getpaid-nonce', 'getpaid-nonce' ); ?> |
|
| 108 | - <?php wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); ?> |
|
| 109 | - <?php wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?> |
|
| 107 | + <?php wp_nonce_field('getpaid-nonce', 'getpaid-nonce'); ?> |
|
| 108 | + <?php wp_nonce_field('meta-box-order', 'meta-box-order-nonce', false); ?> |
|
| 109 | + <?php wp_nonce_field('closedpostboxes', 'closedpostboxesnonce', false); ?> |
|
| 110 | 110 | <input type="hidden" name="getpaid-admin-action" value="update_single_subscription" /> |
| 111 | - <input type="hidden" name="subscription_id" value="<?php echo (int) $sub->get_id() ;?>" /> |
|
| 111 | + <input type="hidden" name="subscription_id" value="<?php echo (int) $sub->get_id(); ?>" /> |
|
| 112 | 112 | |
| 113 | 113 | <div id="poststuff"> |
| 114 | 114 | <div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>"> |
| 115 | 115 | |
| 116 | 116 | <div id="postbox-container-1" class="postbox-container"> |
| 117 | - <?php do_meta_boxes( get_current_screen(), 'side', $sub ); ?> |
|
| 117 | + <?php do_meta_boxes(get_current_screen(), 'side', $sub); ?> |
|
| 118 | 118 | </div> |
| 119 | 119 | |
| 120 | 120 | <div id="postbox-container-2" class="postbox-container"> |
| 121 | - <?php do_meta_boxes( get_current_screen(), 'normal', $sub ); ?> |
|
| 122 | - <?php do_meta_boxes( get_current_screen(), 'advanced', $sub ); ?> |
|
| 121 | + <?php do_meta_boxes(get_current_screen(), 'normal', $sub); ?> |
|
| 122 | + <?php do_meta_boxes(get_current_screen(), 'advanced', $sub); ?> |
|
| 123 | 123 | </div> |
| 124 | 124 | |
| 125 | 125 | </div> |
@@ -138,40 +138,40 @@ discard block |
||
| 138 | 138 | * |
| 139 | 139 | * @param WPInv_Subscription $sub |
| 140 | 140 | */ |
| 141 | -function getpaid_admin_subscription_details_metabox( $sub ) { |
|
| 141 | +function getpaid_admin_subscription_details_metabox($sub) { |
|
| 142 | 142 | |
| 143 | 143 | // Prepare subscription detail columns. |
| 144 | 144 | $fields = apply_filters( |
| 145 | 145 | 'getpaid_subscription_admin_page_fields', |
| 146 | 146 | array( |
| 147 | - 'subscription' => __( 'Subscription', 'invoicing' ), |
|
| 148 | - 'customer' => __( 'Customer', 'invoicing' ), |
|
| 149 | - 'amount' => __( 'Amount', 'invoicing' ), |
|
| 150 | - 'start_date' => __( 'Start Date', 'invoicing' ), |
|
| 151 | - 'renews_on' => __( 'Next Payment', 'invoicing' ), |
|
| 152 | - 'renewals' => __( 'Payments', 'invoicing' ), |
|
| 153 | - 'item' => __( 'Item', 'invoicing' ), |
|
| 154 | - 'gateway' => __( 'Payment Method', 'invoicing' ), |
|
| 155 | - 'profile_id' => __( 'Profile ID', 'invoicing' ), |
|
| 156 | - 'status' => __( 'Status', 'invoicing' ), |
|
| 147 | + 'subscription' => __('Subscription', 'invoicing'), |
|
| 148 | + 'customer' => __('Customer', 'invoicing'), |
|
| 149 | + 'amount' => __('Amount', 'invoicing'), |
|
| 150 | + 'start_date' => __('Start Date', 'invoicing'), |
|
| 151 | + 'renews_on' => __('Next Payment', 'invoicing'), |
|
| 152 | + 'renewals' => __('Payments', 'invoicing'), |
|
| 153 | + 'item' => __('Item', 'invoicing'), |
|
| 154 | + 'gateway' => __('Payment Method', 'invoicing'), |
|
| 155 | + 'profile_id' => __('Profile ID', 'invoicing'), |
|
| 156 | + 'status' => __('Status', 'invoicing'), |
|
| 157 | 157 | ) |
| 158 | 158 | ); |
| 159 | 159 | |
| 160 | - if ( ! $sub->is_active() ) { |
|
| 160 | + if (!$sub->is_active()) { |
|
| 161 | 161 | |
| 162 | - if ( isset( $fields['renews_on'] ) ) { |
|
| 163 | - unset( $fields['renews_on'] ); |
|
| 162 | + if (isset($fields['renews_on'])) { |
|
| 163 | + unset($fields['renews_on']); |
|
| 164 | 164 | } |
| 165 | 165 | |
| 166 | - if ( isset( $fields['gateway'] ) ) { |
|
| 167 | - unset( $fields['gateway'] ); |
|
| 166 | + if (isset($fields['gateway'])) { |
|
| 167 | + unset($fields['gateway']); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | $profile_id = $sub->get_profile_id(); |
| 173 | - if ( empty( $profile_id ) && isset( $fields['profile_id'] ) ) { |
|
| 174 | - unset( $fields['profile_id'] ); |
|
| 173 | + if (empty($profile_id) && isset($fields['profile_id'])) { |
|
| 174 | + unset($fields['profile_id']); |
|
| 175 | 175 | } |
| 176 | 176 | |
| 177 | 177 | ?> |
@@ -179,16 +179,16 @@ discard block |
||
| 179 | 179 | <table class="table table-borderless" style="font-size: 14px;"> |
| 180 | 180 | <tbody> |
| 181 | 181 | |
| 182 | - <?php foreach ( $fields as $key => $label ) : ?> |
|
| 182 | + <?php foreach ($fields as $key => $label) : ?> |
|
| 183 | 183 | |
| 184 | - <tr class="getpaid-subscription-meta-<?php echo sanitize_html_class( $key ); ?>"> |
|
| 184 | + <tr class="getpaid-subscription-meta-<?php echo sanitize_html_class($key); ?>"> |
|
| 185 | 185 | |
| 186 | 186 | <th class="w-25" style="font-weight: 500;"> |
| 187 | - <?php echo sanitize_text_field( $label ); ?> |
|
| 187 | + <?php echo sanitize_text_field($label); ?> |
|
| 188 | 188 | </th> |
| 189 | 189 | |
| 190 | 190 | <td class="w-75 text-muted"> |
| 191 | - <?php do_action( 'getpaid_subscription_admin_display_' . sanitize_text_field( $key ), $sub ); ?> |
|
| 191 | + <?php do_action('getpaid_subscription_admin_display_' . sanitize_text_field($key), $sub); ?> |
|
| 192 | 192 | </td> |
| 193 | 193 | |
| 194 | 194 | </tr> |
@@ -206,131 +206,131 @@ discard block |
||
| 206 | 206 | * |
| 207 | 207 | * @param WPInv_Subscription $subscription |
| 208 | 208 | */ |
| 209 | -function getpaid_admin_subscription_metabox_display_customer( $subscription ) { |
|
| 209 | +function getpaid_admin_subscription_metabox_display_customer($subscription) { |
|
| 210 | 210 | |
| 211 | - $username = __( '(Missing User)', 'invoicing' ); |
|
| 211 | + $username = __('(Missing User)', 'invoicing'); |
|
| 212 | 212 | |
| 213 | - $user = get_userdata( $subscription->get_customer_id() ); |
|
| 214 | - if ( $user ) { |
|
| 213 | + $user = get_userdata($subscription->get_customer_id()); |
|
| 214 | + if ($user) { |
|
| 215 | 215 | |
| 216 | 216 | $username = sprintf( |
| 217 | 217 | '<a href="user-edit.php?user_id=%s">%s</a>', |
| 218 | - absint( $user->ID ), |
|
| 219 | - ! empty( $user->display_name ) ? sanitize_text_field( $user->display_name ) : sanitize_email( $user->user_email ) |
|
| 218 | + absint($user->ID), |
|
| 219 | + !empty($user->display_name) ? sanitize_text_field($user->display_name) : sanitize_email($user->user_email) |
|
| 220 | 220 | ); |
| 221 | 221 | |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | echo $username; |
| 225 | 225 | } |
| 226 | -add_action( 'getpaid_subscription_admin_display_customer', 'getpaid_admin_subscription_metabox_display_customer' ); |
|
| 226 | +add_action('getpaid_subscription_admin_display_customer', 'getpaid_admin_subscription_metabox_display_customer'); |
|
| 227 | 227 | |
| 228 | 228 | /** |
| 229 | 229 | * Displays the subscription amount. |
| 230 | 230 | * |
| 231 | 231 | * @param WPInv_Subscription $subscription |
| 232 | 232 | */ |
| 233 | -function getpaid_admin_subscription_metabox_display_amount( $subscription ) { |
|
| 234 | - $amount = sanitize_text_field( getpaid_get_formatted_subscription_amount( $subscription ) ); |
|
| 233 | +function getpaid_admin_subscription_metabox_display_amount($subscription) { |
|
| 234 | + $amount = sanitize_text_field(getpaid_get_formatted_subscription_amount($subscription)); |
|
| 235 | 235 | echo "<span>$amount</span>"; |
| 236 | 236 | } |
| 237 | -add_action( 'getpaid_subscription_admin_display_amount', 'getpaid_admin_subscription_metabox_display_amount' ); |
|
| 237 | +add_action('getpaid_subscription_admin_display_amount', 'getpaid_admin_subscription_metabox_display_amount'); |
|
| 238 | 238 | |
| 239 | 239 | /** |
| 240 | 240 | * Displays the subscription id. |
| 241 | 241 | * |
| 242 | 242 | * @param WPInv_Subscription $subscription |
| 243 | 243 | */ |
| 244 | -function getpaid_admin_subscription_metabox_display_id( $subscription ) { |
|
| 245 | - echo '#' . absint( $subscription->get_id() ); |
|
| 244 | +function getpaid_admin_subscription_metabox_display_id($subscription) { |
|
| 245 | + echo '#' . absint($subscription->get_id()); |
|
| 246 | 246 | } |
| 247 | -add_action( 'getpaid_subscription_admin_display_subscription', 'getpaid_admin_subscription_metabox_display_id' ); |
|
| 247 | +add_action('getpaid_subscription_admin_display_subscription', 'getpaid_admin_subscription_metabox_display_id'); |
|
| 248 | 248 | |
| 249 | 249 | /** |
| 250 | 250 | * Displays the subscription renewal date. |
| 251 | 251 | * |
| 252 | 252 | * @param WPInv_Subscription $subscription |
| 253 | 253 | */ |
| 254 | -function getpaid_admin_subscription_metabox_display_start_date( $subscription ) { |
|
| 255 | - echo getpaid_format_date_value( $subscription->get_date_created() ); |
|
| 254 | +function getpaid_admin_subscription_metabox_display_start_date($subscription) { |
|
| 255 | + echo getpaid_format_date_value($subscription->get_date_created()); |
|
| 256 | 256 | } |
| 257 | -add_action( 'getpaid_subscription_admin_display_start_date', 'getpaid_admin_subscription_metabox_display_start_date' ); |
|
| 257 | +add_action('getpaid_subscription_admin_display_start_date', 'getpaid_admin_subscription_metabox_display_start_date'); |
|
| 258 | 258 | |
| 259 | 259 | /** |
| 260 | 260 | * Displays the subscription renewal date. |
| 261 | 261 | * |
| 262 | 262 | * @param WPInv_Subscription $subscription |
| 263 | 263 | */ |
| 264 | -function getpaid_admin_subscription_metabox_display_renews_on( $subscription ) { |
|
| 265 | - echo getpaid_format_date_value( $subscription->get_expiration() ); |
|
| 264 | +function getpaid_admin_subscription_metabox_display_renews_on($subscription) { |
|
| 265 | + echo getpaid_format_date_value($subscription->get_expiration()); |
|
| 266 | 266 | } |
| 267 | -add_action( 'getpaid_subscription_admin_display_renews_on', 'getpaid_admin_subscription_metabox_display_renews_on' ); |
|
| 267 | +add_action('getpaid_subscription_admin_display_renews_on', 'getpaid_admin_subscription_metabox_display_renews_on'); |
|
| 268 | 268 | |
| 269 | 269 | /** |
| 270 | 270 | * Displays the subscription renewal count. |
| 271 | 271 | * |
| 272 | 272 | * @param WPInv_Subscription $subscription |
| 273 | 273 | */ |
| 274 | -function getpaid_admin_subscription_metabox_display_renewals( $subscription ) { |
|
| 274 | +function getpaid_admin_subscription_metabox_display_renewals($subscription) { |
|
| 275 | 275 | $max_bills = $subscription->get_bill_times(); |
| 276 | - echo $subscription->get_times_billed() . ' / ' . ( empty( $max_bills ) ? "∞" : $max_bills ); |
|
| 276 | + echo $subscription->get_times_billed() . ' / ' . (empty($max_bills) ? "∞" : $max_bills); |
|
| 277 | 277 | } |
| 278 | -add_action( 'getpaid_subscription_admin_display_renewals', 'getpaid_admin_subscription_metabox_display_renewals' ); |
|
| 278 | +add_action('getpaid_subscription_admin_display_renewals', 'getpaid_admin_subscription_metabox_display_renewals'); |
|
| 279 | 279 | /** |
| 280 | 280 | * Displays the subscription item. |
| 281 | 281 | * |
| 282 | 282 | * @param WPInv_Subscription $subscription |
| 283 | 283 | */ |
| 284 | -function getpaid_admin_subscription_metabox_display_item( $subscription ) { |
|
| 284 | +function getpaid_admin_subscription_metabox_display_item($subscription) { |
|
| 285 | 285 | |
| 286 | - $item = get_post( $subscription->get_product_id() ); |
|
| 286 | + $item = get_post($subscription->get_product_id()); |
|
| 287 | 287 | |
| 288 | - if ( ! empty( $item ) ) { |
|
| 289 | - $link = get_edit_post_link( $item ); |
|
| 290 | - $link = esc_url( $link ); |
|
| 291 | - $name = esc_html( get_the_title( $item ) ); |
|
| 288 | + if (!empty($item)) { |
|
| 289 | + $link = get_edit_post_link($item); |
|
| 290 | + $link = esc_url($link); |
|
| 291 | + $name = esc_html(get_the_title($item)); |
|
| 292 | 292 | echo "<a href='$link'>$name</a>"; |
| 293 | 293 | } else { |
| 294 | - echo sprintf( __( 'Item #%s', 'invoicing' ), $subscription->get_product_id() ); |
|
| 294 | + echo sprintf(__('Item #%s', 'invoicing'), $subscription->get_product_id()); |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | } |
| 298 | -add_action( 'getpaid_subscription_admin_display_item', 'getpaid_admin_subscription_metabox_display_item' ); |
|
| 298 | +add_action('getpaid_subscription_admin_display_item', 'getpaid_admin_subscription_metabox_display_item'); |
|
| 299 | 299 | |
| 300 | 300 | /** |
| 301 | 301 | * Displays the subscription gateway. |
| 302 | 302 | * |
| 303 | 303 | * @param WPInv_Subscription $subscription |
| 304 | 304 | */ |
| 305 | -function getpaid_admin_subscription_metabox_display_gateway( $subscription ) { |
|
| 305 | +function getpaid_admin_subscription_metabox_display_gateway($subscription) { |
|
| 306 | 306 | |
| 307 | 307 | $gateway = $subscription->get_gateway(); |
| 308 | 308 | |
| 309 | - if ( ! empty( $gateway ) ) { |
|
| 310 | - echo sanitize_text_field( wpinv_get_gateway_admin_label( $gateway ) ); |
|
| 309 | + if (!empty($gateway)) { |
|
| 310 | + echo sanitize_text_field(wpinv_get_gateway_admin_label($gateway)); |
|
| 311 | 311 | } else { |
| 312 | 312 | echo "—"; |
| 313 | 313 | } |
| 314 | 314 | |
| 315 | 315 | } |
| 316 | -add_action( 'getpaid_subscription_admin_display_gateway', 'getpaid_admin_subscription_metabox_display_gateway' ); |
|
| 316 | +add_action('getpaid_subscription_admin_display_gateway', 'getpaid_admin_subscription_metabox_display_gateway'); |
|
| 317 | 317 | |
| 318 | 318 | /** |
| 319 | 319 | * Displays the subscription status. |
| 320 | 320 | * |
| 321 | 321 | * @param WPInv_Subscription $subscription |
| 322 | 322 | */ |
| 323 | -function getpaid_admin_subscription_metabox_display_status( $subscription ) { |
|
| 323 | +function getpaid_admin_subscription_metabox_display_status($subscription) { |
|
| 324 | 324 | echo $subscription->get_status_label_html(); |
| 325 | 325 | } |
| 326 | -add_action( 'getpaid_subscription_admin_display_status', 'getpaid_admin_subscription_metabox_display_status' ); |
|
| 326 | +add_action('getpaid_subscription_admin_display_status', 'getpaid_admin_subscription_metabox_display_status'); |
|
| 327 | 327 | |
| 328 | 328 | /** |
| 329 | 329 | * Displays the subscription profile id. |
| 330 | 330 | * |
| 331 | 331 | * @param WPInv_Subscription $subscription |
| 332 | 332 | */ |
| 333 | -function getpaid_admin_subscription_metabox_display_profile_id( $subscription ) { |
|
| 333 | +function getpaid_admin_subscription_metabox_display_profile_id($subscription) { |
|
| 334 | 334 | |
| 335 | 335 | $profile_id = $subscription->get_profile_id(); |
| 336 | 336 | |
@@ -339,32 +339,32 @@ discard block |
||
| 339 | 339 | 'type' => 'text', |
| 340 | 340 | 'id' => 'wpinv_subscription_profile_id', |
| 341 | 341 | 'name' => 'wpinv_subscription_profile_id', |
| 342 | - 'label' => __( 'Profile Id', 'invoicing' ), |
|
| 342 | + 'label' => __('Profile Id', 'invoicing'), |
|
| 343 | 343 | 'label_type' => 'hidden', |
| 344 | - 'placeholder' => __( 'Profile Id', 'invoicing' ), |
|
| 345 | - 'value' => sanitize_text_field( $profile_id ), |
|
| 344 | + 'placeholder' => __('Profile Id', 'invoicing'), |
|
| 345 | + 'value' => sanitize_text_field($profile_id), |
|
| 346 | 346 | 'input_group_right' => '', |
| 347 | 347 | 'no_wrap' => true, |
| 348 | 348 | ) |
| 349 | 349 | ); |
| 350 | 350 | |
| 351 | - echo str_ireplace( 'form-control', 'regular-text', $input ); |
|
| 351 | + echo str_ireplace('form-control', 'regular-text', $input); |
|
| 352 | 352 | |
| 353 | - $url = apply_filters( 'getpaid_remote_subscription_profile_url', '', $subscription ); |
|
| 354 | - if ( ! empty( $url ) ) { |
|
| 355 | - $url = esc_url_raw( $url ); |
|
| 356 | - echo ' <a href="' . $url . '" title="' . __( 'View in Gateway', 'invoicing' ) . '" target="_blank"><i class="fas fa-external-link-alt fa-xs fa-fw align-top"></i></a>'; |
|
| 353 | + $url = apply_filters('getpaid_remote_subscription_profile_url', '', $subscription); |
|
| 354 | + if (!empty($url)) { |
|
| 355 | + $url = esc_url_raw($url); |
|
| 356 | + echo ' <a href="' . $url . '" title="' . __('View in Gateway', 'invoicing') . '" target="_blank"><i class="fas fa-external-link-alt fa-xs fa-fw align-top"></i></a>'; |
|
| 357 | 357 | } |
| 358 | 358 | |
| 359 | 359 | } |
| 360 | -add_action( 'getpaid_subscription_admin_display_profile_id', 'getpaid_admin_subscription_metabox_display_profile_id' ); |
|
| 360 | +add_action('getpaid_subscription_admin_display_profile_id', 'getpaid_admin_subscription_metabox_display_profile_id'); |
|
| 361 | 361 | |
| 362 | 362 | /** |
| 363 | 363 | * Displays the subscriptions update metabox. |
| 364 | 364 | * |
| 365 | 365 | * @param WPInv_Subscription $subscription |
| 366 | 366 | */ |
| 367 | -function getpaid_admin_subscription_update_metabox( $subscription ) { |
|
| 367 | +function getpaid_admin_subscription_update_metabox($subscription) { |
|
| 368 | 368 | |
| 369 | 369 | ?> |
| 370 | 370 | <div class="mt-3"> |
@@ -377,10 +377,10 @@ discard block |
||
| 377 | 377 | 'id' => 'subscription_status_update_select', |
| 378 | 378 | 'required' => true, |
| 379 | 379 | 'no_wrap' => false, |
| 380 | - 'label' => __( 'Subscription Status', 'invoicing' ), |
|
| 381 | - 'help_text' => __( 'Updating the status will trigger related actions and hooks', 'invoicing' ), |
|
| 380 | + 'label' => __('Subscription Status', 'invoicing'), |
|
| 381 | + 'help_text' => __('Updating the status will trigger related actions and hooks', 'invoicing'), |
|
| 382 | 382 | 'select2' => true, |
| 383 | - 'value' => $subscription->get_status( 'edit' ), |
|
| 383 | + 'value' => $subscription->get_status('edit'), |
|
| 384 | 384 | ) |
| 385 | 385 | ); |
| 386 | 386 | ?> |
@@ -388,13 +388,13 @@ discard block |
||
| 388 | 388 | <div class="mt-2 px-3 py-2 bg-light border-top" style="margin: -12px;"> |
| 389 | 389 | |
| 390 | 390 | <?php |
| 391 | - submit_button( __( 'Update', 'invoicing' ), 'primary', 'submit', false ); |
|
| 391 | + submit_button(__('Update', 'invoicing'), 'primary', 'submit', false); |
|
| 392 | 392 | |
| 393 | - $url = esc_url( wp_nonce_url( add_query_arg( 'getpaid-admin-action', 'subscription_manual_renew' ), 'getpaid-nonce', 'getpaid-nonce' ) ); |
|
| 394 | - $anchor = __( 'Renew Subscription', 'invoicing' ); |
|
| 395 | - $title = esc_attr__( 'Are you sure you want to extend the subscription and generate a new invoice that will be automatically marked as paid?', 'invoicing' ); |
|
| 393 | + $url = esc_url(wp_nonce_url(add_query_arg('getpaid-admin-action', 'subscription_manual_renew'), 'getpaid-nonce', 'getpaid-nonce')); |
|
| 394 | + $anchor = __('Renew Subscription', 'invoicing'); |
|
| 395 | + $title = esc_attr__('Are you sure you want to extend the subscription and generate a new invoice that will be automatically marked as paid?', 'invoicing'); |
|
| 396 | 396 | |
| 397 | - if ( $subscription->is_active() ) { |
|
| 397 | + if ($subscription->is_active()) { |
|
| 398 | 398 | echo "<a href='$url' class='float-right text-muted' onclick='return confirm(\"$title\")'>$anchor</a>"; |
| 399 | 399 | } |
| 400 | 400 | |
@@ -406,31 +406,31 @@ discard block |
||
| 406 | 406 | * |
| 407 | 407 | * @param WPInv_Subscription $subscription |
| 408 | 408 | */ |
| 409 | -function getpaid_admin_subscription_invoice_details_metabox( $subscription ) { |
|
| 409 | +function getpaid_admin_subscription_invoice_details_metabox($subscription) { |
|
| 410 | 410 | |
| 411 | 411 | $columns = apply_filters( |
| 412 | 412 | 'getpaid_subscription_related_invoices_columns', |
| 413 | 413 | array( |
| 414 | - 'invoice' => __( 'Invoice', 'invoicing' ), |
|
| 415 | - 'relationship' => __( 'Relationship', 'invoicing' ), |
|
| 416 | - 'date' => __( 'Date', 'invoicing' ), |
|
| 417 | - 'status' => __( 'Status', 'invoicing' ), |
|
| 418 | - 'total' => __( 'Total', 'invoicing' ), |
|
| 414 | + 'invoice' => __('Invoice', 'invoicing'), |
|
| 415 | + 'relationship' => __('Relationship', 'invoicing'), |
|
| 416 | + 'date' => __('Date', 'invoicing'), |
|
| 417 | + 'status' => __('Status', 'invoicing'), |
|
| 418 | + 'total' => __('Total', 'invoicing'), |
|
| 419 | 419 | ), |
| 420 | 420 | $subscription |
| 421 | 421 | ); |
| 422 | 422 | |
| 423 | 423 | // Prepare the invoices. |
| 424 | - $payments = $subscription->get_child_payments( ! is_admin() ); |
|
| 424 | + $payments = $subscription->get_child_payments(!is_admin()); |
|
| 425 | 425 | $parent = $subscription->get_parent_invoice(); |
| 426 | 426 | |
| 427 | - if ( $parent->get_id() ) { |
|
| 428 | - $payments = array_merge( array( $parent ), $payments ); |
|
| 427 | + if ($parent->get_id()) { |
|
| 428 | + $payments = array_merge(array($parent), $payments); |
|
| 429 | 429 | } |
| 430 | 430 | |
| 431 | 431 | $table_class = 'w-100 bg-white'; |
| 432 | 432 | |
| 433 | - if ( ! is_admin() ) { |
|
| 433 | + if (!is_admin()) { |
|
| 434 | 434 | $table_class = 'table table-bordered table-striped'; |
| 435 | 435 | } |
| 436 | 436 | |
@@ -442,9 +442,9 @@ discard block |
||
| 442 | 442 | <thead> |
| 443 | 443 | <tr> |
| 444 | 444 | <?php |
| 445 | - foreach ( $columns as $key => $label ) { |
|
| 446 | - $key = esc_attr( $key ); |
|
| 447 | - $label = sanitize_text_field( $label ); |
|
| 445 | + foreach ($columns as $key => $label) { |
|
| 446 | + $key = esc_attr($key); |
|
| 447 | + $label = sanitize_text_field($label); |
|
| 448 | 448 | |
| 449 | 449 | echo "<th class='subscription-invoice-field-$key bg-light p-2 text-left color-dark font-weight-bold'>$label</th>"; |
| 450 | 450 | } |
@@ -454,50 +454,50 @@ discard block |
||
| 454 | 454 | |
| 455 | 455 | <tbody> |
| 456 | 456 | |
| 457 | - <?php if ( empty( $payments ) ) : ?> |
|
| 457 | + <?php if (empty($payments)) : ?> |
|
| 458 | 458 | <tr> |
| 459 | 459 | <td colspan="<?php echo count($columns); ?>" class="p-2 text-left text-muted"> |
| 460 | - <?php _e( 'This subscription has no invoices.', 'invoicing' ); ?> |
|
| 460 | + <?php _e('This subscription has no invoices.', 'invoicing'); ?> |
|
| 461 | 461 | </td> |
| 462 | 462 | </tr> |
| 463 | 463 | <?php endif; ?> |
| 464 | 464 | |
| 465 | 465 | <?php |
| 466 | 466 | |
| 467 | - foreach( $payments as $payment ) : |
|
| 467 | + foreach ($payments as $payment) : |
|
| 468 | 468 | |
| 469 | 469 | // Ensure that we have an invoice. |
| 470 | - $payment = new WPInv_Invoice( $payment ); |
|
| 470 | + $payment = new WPInv_Invoice($payment); |
|
| 471 | 471 | |
| 472 | 472 | // Abort if the invoice is invalid. |
| 473 | - if ( ! $payment->get_id() ) { |
|
| 473 | + if (!$payment->get_id()) { |
|
| 474 | 474 | continue; |
| 475 | 475 | } |
| 476 | 476 | |
| 477 | 477 | echo '<tr>'; |
| 478 | 478 | |
| 479 | - foreach ( array_keys( $columns ) as $key ) { |
|
| 479 | + foreach (array_keys($columns) as $key) { |
|
| 480 | 480 | |
| 481 | 481 | echo '<td class="p-2 text-left">'; |
| 482 | 482 | |
| 483 | - switch( $key ) { |
|
| 483 | + switch ($key) { |
|
| 484 | 484 | |
| 485 | 485 | case 'total': |
| 486 | - echo '<strong>' . wpinv_price( $payment->get_total(), $payment->get_currency() ) . '</strong>'; |
|
| 486 | + echo '<strong>' . wpinv_price($payment->get_total(), $payment->get_currency()) . '</strong>'; |
|
| 487 | 487 | break; |
| 488 | 488 | |
| 489 | 489 | case 'relationship': |
| 490 | - echo $payment->is_renewal() ? __( 'Renewal Invoice', 'invoicing' ) : __( 'Initial Invoice', 'invoicing' ); |
|
| 490 | + echo $payment->is_renewal() ? __('Renewal Invoice', 'invoicing') : __('Initial Invoice', 'invoicing'); |
|
| 491 | 491 | break; |
| 492 | 492 | |
| 493 | 493 | case 'date': |
| 494 | - echo getpaid_format_date_value( $payment->get_date_created() ); |
|
| 494 | + echo getpaid_format_date_value($payment->get_date_created()); |
|
| 495 | 495 | break; |
| 496 | 496 | |
| 497 | 497 | case 'status': |
| 498 | 498 | |
| 499 | 499 | $status = $payment->get_status_nicename(); |
| 500 | - if ( is_admin() ) { |
|
| 500 | + if (is_admin()) { |
|
| 501 | 501 | $status = $payment->get_status_label_html(); |
| 502 | 502 | } |
| 503 | 503 | |
@@ -505,13 +505,13 @@ discard block |
||
| 505 | 505 | break; |
| 506 | 506 | |
| 507 | 507 | case 'invoice': |
| 508 | - $link = esc_url( get_edit_post_link( $payment->get_id() ) ); |
|
| 508 | + $link = esc_url(get_edit_post_link($payment->get_id())); |
|
| 509 | 509 | |
| 510 | - if ( ! is_admin() ) { |
|
| 511 | - $link = esc_url( $payment->get_view_url() ); |
|
| 510 | + if (!is_admin()) { |
|
| 511 | + $link = esc_url($payment->get_view_url()); |
|
| 512 | 512 | } |
| 513 | 513 | |
| 514 | - $invoice = sanitize_text_field( $payment->get_number() ); |
|
| 514 | + $invoice = sanitize_text_field($payment->get_number()); |
|
| 515 | 515 | echo "<a href='$link'>$invoice</a>"; |
| 516 | 516 | break; |
| 517 | 517 | } |
@@ -12,456 +12,456 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Paypal_Gateway_IPN_Handler { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Payment method id. |
|
| 17 | - * |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - protected $id = 'paypal'; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Payment method object. |
|
| 24 | - * |
|
| 25 | - * @var GetPaid_Paypal_Gateway |
|
| 26 | - */ |
|
| 27 | - protected $gateway; |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Class constructor. |
|
| 31 | - * |
|
| 32 | - * @param GetPaid_Paypal_Gateway $gateway |
|
| 33 | - */ |
|
| 34 | - public function __construct( $gateway ) { |
|
| 35 | - $this->gateway = $gateway; |
|
| 36 | - $this->verify_ipn(); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Processes ipns and marks payments as complete. |
|
| 41 | - * |
|
| 42 | - * @return void |
|
| 43 | - */ |
|
| 44 | - public function verify_ipn() { |
|
| 45 | - |
|
| 46 | - wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
| 47 | - |
|
| 48 | - // Validate the IPN. |
|
| 49 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
| 50 | - wp_die( 'PayPal IPN Request Failure', 500 ); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - // Process the IPN. |
|
| 54 | - $posted = wp_unslash( $_POST ); |
|
| 55 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
| 56 | - |
|
| 57 | - // Abort if it was not paid by our gateway. |
|
| 58 | - if ( $this->id != $invoice->get_gateway() ) { |
|
| 59 | - wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
| 60 | - wp_die( 'Invoice not paid via PayPal', 200 ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
| 64 | - $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
| 65 | - |
|
| 66 | - wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
| 67 | - wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
| 68 | - |
|
| 69 | - if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
| 70 | - call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
| 71 | - wpinv_error_log( 'Done processing IPN', false ); |
|
| 72 | - wp_die( 'Processed', 200 ); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
| 76 | - wp_die( 'Unsupported IPN type', 200 ); |
|
| 77 | - |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Retrieves IPN Invoice. |
|
| 82 | - * |
|
| 83 | - * @param array $posted |
|
| 84 | - * @return WPInv_Invoice |
|
| 85 | - */ |
|
| 86 | - protected function get_ipn_invoice( $posted ) { |
|
| 87 | - |
|
| 88 | - wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
| 89 | - |
|
| 90 | - if ( ! empty( $posted['custom'] ) ) { |
|
| 91 | - $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
| 92 | - |
|
| 93 | - if ( $invoice->exists() ) { |
|
| 94 | - wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
| 95 | - return $invoice; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
| 101 | - wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Check PayPal IPN validity. |
|
| 106 | - */ |
|
| 107 | - protected function validate_ipn() { |
|
| 108 | - |
|
| 109 | - wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
| 110 | - |
|
| 111 | - // Retrieve the associated invoice. |
|
| 112 | - $posted = wp_unslash( $_POST ); |
|
| 113 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
| 114 | - |
|
| 115 | - if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
| 116 | - wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data' ); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - // Validate the IPN. |
|
| 120 | - $posted['cmd'] = '_notify-validate'; |
|
| 121 | - |
|
| 122 | - // Send back post vars to paypal. |
|
| 123 | - $params = array( |
|
| 124 | - 'body' => $posted, |
|
| 125 | - 'timeout' => 60, |
|
| 126 | - 'httpversion' => '1.1', |
|
| 127 | - 'compress' => false, |
|
| 128 | - 'decompress' => false, |
|
| 129 | - 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
| 130 | - ); |
|
| 131 | - |
|
| 132 | - // Post back to get a response. |
|
| 133 | - $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
| 134 | - |
|
| 135 | - // Check to see if the request was valid. |
|
| 136 | - if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
| 137 | - wpinv_error_log( $response['body'], 'Received valid response from PayPal IPN' ); |
|
| 138 | - return true; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - if ( is_wp_error( $response ) ) { |
|
| 142 | - wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
| 143 | - return false; |
|
| 144 | - } |
|
| 15 | + /** |
|
| 16 | + * Payment method id. |
|
| 17 | + * |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + protected $id = 'paypal'; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Payment method object. |
|
| 24 | + * |
|
| 25 | + * @var GetPaid_Paypal_Gateway |
|
| 26 | + */ |
|
| 27 | + protected $gateway; |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Class constructor. |
|
| 31 | + * |
|
| 32 | + * @param GetPaid_Paypal_Gateway $gateway |
|
| 33 | + */ |
|
| 34 | + public function __construct( $gateway ) { |
|
| 35 | + $this->gateway = $gateway; |
|
| 36 | + $this->verify_ipn(); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Processes ipns and marks payments as complete. |
|
| 41 | + * |
|
| 42 | + * @return void |
|
| 43 | + */ |
|
| 44 | + public function verify_ipn() { |
|
| 45 | + |
|
| 46 | + wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
| 47 | + |
|
| 48 | + // Validate the IPN. |
|
| 49 | + if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
| 50 | + wp_die( 'PayPal IPN Request Failure', 500 ); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + // Process the IPN. |
|
| 54 | + $posted = wp_unslash( $_POST ); |
|
| 55 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
| 56 | + |
|
| 57 | + // Abort if it was not paid by our gateway. |
|
| 58 | + if ( $this->id != $invoice->get_gateway() ) { |
|
| 59 | + wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
| 60 | + wp_die( 'Invoice not paid via PayPal', 200 ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
| 64 | + $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
| 65 | + |
|
| 66 | + wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
| 67 | + wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
| 68 | + |
|
| 69 | + if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
| 70 | + call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
| 71 | + wpinv_error_log( 'Done processing IPN', false ); |
|
| 72 | + wp_die( 'Processed', 200 ); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
| 76 | + wp_die( 'Unsupported IPN type', 200 ); |
|
| 77 | + |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Retrieves IPN Invoice. |
|
| 82 | + * |
|
| 83 | + * @param array $posted |
|
| 84 | + * @return WPInv_Invoice |
|
| 85 | + */ |
|
| 86 | + protected function get_ipn_invoice( $posted ) { |
|
| 87 | + |
|
| 88 | + wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
| 89 | + |
|
| 90 | + if ( ! empty( $posted['custom'] ) ) { |
|
| 91 | + $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
| 92 | + |
|
| 93 | + if ( $invoice->exists() ) { |
|
| 94 | + wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
| 95 | + return $invoice; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
| 101 | + wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Check PayPal IPN validity. |
|
| 106 | + */ |
|
| 107 | + protected function validate_ipn() { |
|
| 108 | + |
|
| 109 | + wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
| 110 | + |
|
| 111 | + // Retrieve the associated invoice. |
|
| 112 | + $posted = wp_unslash( $_POST ); |
|
| 113 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
| 114 | + |
|
| 115 | + if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
| 116 | + wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data' ); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + // Validate the IPN. |
|
| 120 | + $posted['cmd'] = '_notify-validate'; |
|
| 121 | + |
|
| 122 | + // Send back post vars to paypal. |
|
| 123 | + $params = array( |
|
| 124 | + 'body' => $posted, |
|
| 125 | + 'timeout' => 60, |
|
| 126 | + 'httpversion' => '1.1', |
|
| 127 | + 'compress' => false, |
|
| 128 | + 'decompress' => false, |
|
| 129 | + 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
| 130 | + ); |
|
| 131 | + |
|
| 132 | + // Post back to get a response. |
|
| 133 | + $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
| 134 | + |
|
| 135 | + // Check to see if the request was valid. |
|
| 136 | + if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
| 137 | + wpinv_error_log( $response['body'], 'Received valid response from PayPal IPN' ); |
|
| 138 | + return true; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + if ( is_wp_error( $response ) ) { |
|
| 142 | + wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
| 143 | + return false; |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
| 147 | - return false; |
|
| 148 | - |
|
| 149 | - } |
|
| 146 | + wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
| 147 | + return false; |
|
| 148 | + |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Check currency from IPN matches the invoice. |
|
| 153 | - * |
|
| 154 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 155 | - * @param string $currency currency to validate. |
|
| 156 | - */ |
|
| 157 | - protected function validate_ipn_currency( $invoice, $currency ) { |
|
| 151 | + /** |
|
| 152 | + * Check currency from IPN matches the invoice. |
|
| 153 | + * |
|
| 154 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 155 | + * @param string $currency currency to validate. |
|
| 156 | + */ |
|
| 157 | + protected function validate_ipn_currency( $invoice, $currency ) { |
|
| 158 | 158 | |
| 159 | - if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
| 159 | + if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
| 160 | 160 | |
| 161 | - /* translators: %s: currency code. */ |
|
| 162 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
| 161 | + /* translators: %s: currency code. */ |
|
| 162 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
| 163 | 163 | |
| 164 | - wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 165 | - } |
|
| 164 | + wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 165 | + } |
|
| 166 | 166 | |
| 167 | - wpinv_error_log( $currency, 'Validated IPN Currency' ); |
|
| 168 | - } |
|
| 167 | + wpinv_error_log( $currency, 'Validated IPN Currency' ); |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Check payment amount from IPN matches the invoice. |
|
| 172 | - * |
|
| 173 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 174 | - * @param float $amount amount to validate. |
|
| 175 | - */ |
|
| 176 | - protected function validate_ipn_amount( $invoice, $amount ) { |
|
| 177 | - if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
| 170 | + /** |
|
| 171 | + * Check payment amount from IPN matches the invoice. |
|
| 172 | + * |
|
| 173 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 174 | + * @param float $amount amount to validate. |
|
| 175 | + */ |
|
| 176 | + protected function validate_ipn_amount( $invoice, $amount ) { |
|
| 177 | + if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
| 178 | 178 | |
| 179 | - /* translators: %s: Amount. */ |
|
| 180 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
| 179 | + /* translators: %s: Amount. */ |
|
| 180 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
| 181 | 181 | |
| 182 | - wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 183 | - } |
|
| 182 | + wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - wpinv_error_log( $amount, 'Validated IPN Amount' ); |
|
| 186 | - } |
|
| 185 | + wpinv_error_log( $amount, 'Validated IPN Amount' ); |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Verify receiver email from PayPal. |
|
| 190 | - * |
|
| 191 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 192 | - * @param string $receiver_email Email to validate. |
|
| 193 | - */ |
|
| 194 | - protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
| 195 | - $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
| 188 | + /** |
|
| 189 | + * Verify receiver email from PayPal. |
|
| 190 | + * |
|
| 191 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 192 | + * @param string $receiver_email Email to validate. |
|
| 193 | + */ |
|
| 194 | + protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
| 195 | + $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
| 196 | 196 | |
| 197 | - if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
| 198 | - wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
| 197 | + if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
| 198 | + wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
| 199 | 199 | |
| 200 | - /* translators: %s: email address . */ |
|
| 201 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
| 200 | + /* translators: %s: email address . */ |
|
| 201 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
| 202 | 202 | |
| 203 | - return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 204 | - } |
|
| 203 | + return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - wpinv_error_log( 'Validated PayPal Email', false ); |
|
| 207 | - } |
|
| 206 | + wpinv_error_log( 'Validated PayPal Email', false ); |
|
| 207 | + } |
|
| 208 | 208 | |
| 209 | - /** |
|
| 210 | - * Handles one time payments. |
|
| 211 | - * |
|
| 212 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 213 | - * @param array $posted Posted data. |
|
| 214 | - */ |
|
| 215 | - protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
| 216 | - |
|
| 217 | - // Collect payment details |
|
| 218 | - $payment_status = strtolower( $posted['payment_status'] ); |
|
| 219 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 220 | - |
|
| 221 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 222 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 223 | - |
|
| 224 | - // Update the transaction id. |
|
| 225 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 226 | - $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
| 227 | - $invoice->save(); |
|
| 228 | - } |
|
| 209 | + /** |
|
| 210 | + * Handles one time payments. |
|
| 211 | + * |
|
| 212 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 213 | + * @param array $posted Posted data. |
|
| 214 | + */ |
|
| 215 | + protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
| 216 | + |
|
| 217 | + // Collect payment details |
|
| 218 | + $payment_status = strtolower( $posted['payment_status'] ); |
|
| 219 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 220 | + |
|
| 221 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 222 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 223 | + |
|
| 224 | + // Update the transaction id. |
|
| 225 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
| 226 | + $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
| 227 | + $invoice->save(); |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - // Process a refund. |
|
| 231 | - if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
| 230 | + // Process a refund. |
|
| 231 | + if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
| 232 | 232 | |
| 233 | - update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
| 233 | + update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
| 234 | 234 | |
| 235 | - if ( ! $invoice->is_refunded() ) { |
|
| 236 | - $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
| 237 | - } |
|
| 235 | + if ( ! $invoice->is_refunded() ) { |
|
| 236 | + $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
| 237 | + } |
|
| 238 | 238 | |
| 239 | - return wpinv_error_log( $posted['reason_code'], false ); |
|
| 240 | - } |
|
| 239 | + return wpinv_error_log( $posted['reason_code'], false ); |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | - // Process payments. |
|
| 243 | - if ( $payment_status == 'completed' ) { |
|
| 242 | + // Process payments. |
|
| 243 | + if ( $payment_status == 'completed' ) { |
|
| 244 | 244 | |
| 245 | - if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
| 246 | - return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
| 247 | - } |
|
| 245 | + if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
| 246 | + return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
| 247 | + } |
|
| 248 | 248 | |
| 249 | - $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
| 249 | + $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
| 250 | 250 | |
| 251 | - $note = ''; |
|
| 251 | + $note = ''; |
|
| 252 | 252 | |
| 253 | - if ( ! empty( $posted['mc_fee'] ) ) { |
|
| 254 | - $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
| 255 | - } |
|
| 253 | + if ( ! empty( $posted['mc_fee'] ) ) { |
|
| 254 | + $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - if ( ! empty( $posted['payer_status'] ) ) { |
|
| 258 | - $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
| 259 | - } |
|
| 257 | + if ( ! empty( $posted['payer_status'] ) ) { |
|
| 258 | + $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
| 259 | + } |
|
| 260 | 260 | |
| 261 | - $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
| 262 | - return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
| 261 | + $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
| 262 | + return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
| 263 | 263 | |
| 264 | - } |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - // Pending payments. |
|
| 267 | - if ( $payment_status == 'pending' ) { |
|
| 266 | + // Pending payments. |
|
| 267 | + if ( $payment_status == 'pending' ) { |
|
| 268 | 268 | |
| 269 | - /* translators: %s: pending reason. */ |
|
| 270 | - $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
| 269 | + /* translators: %s: pending reason. */ |
|
| 270 | + $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
| 271 | 271 | |
| 272 | - return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
| 273 | - } |
|
| 272 | + return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - /* translators: %s: payment status. */ |
|
| 276 | - $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
| 275 | + /* translators: %s: payment status. */ |
|
| 276 | + $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
| 277 | 277 | |
| 278 | - } |
|
| 278 | + } |
|
| 279 | 279 | |
| 280 | - /** |
|
| 281 | - * Handles one time payments. |
|
| 282 | - * |
|
| 283 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 284 | - * @param array $posted Posted data. |
|
| 285 | - */ |
|
| 286 | - protected function ipn_txn_cart( $invoice, $posted ) { |
|
| 287 | - $this->ipn_txn_web_accept( $invoice, $posted ); |
|
| 288 | - } |
|
| 280 | + /** |
|
| 281 | + * Handles one time payments. |
|
| 282 | + * |
|
| 283 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 284 | + * @param array $posted Posted data. |
|
| 285 | + */ |
|
| 286 | + protected function ipn_txn_cart( $invoice, $posted ) { |
|
| 287 | + $this->ipn_txn_web_accept( $invoice, $posted ); |
|
| 288 | + } |
|
| 289 | 289 | |
| 290 | - /** |
|
| 291 | - * Handles subscription sign ups. |
|
| 292 | - * |
|
| 293 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 294 | - * @param array $posted Posted data. |
|
| 295 | - */ |
|
| 296 | - protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
| 290 | + /** |
|
| 291 | + * Handles subscription sign ups. |
|
| 292 | + * |
|
| 293 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 294 | + * @param array $posted Posted data. |
|
| 295 | + */ |
|
| 296 | + protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
| 297 | 297 | |
| 298 | - wpinv_error_log( 'Processing subscription signup', false ); |
|
| 298 | + wpinv_error_log( 'Processing subscription signup', false ); |
|
| 299 | 299 | |
| 300 | - // Make sure the invoice has a subscription. |
|
| 301 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
| 300 | + // Make sure the invoice has a subscription. |
|
| 301 | + $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
| 302 | 302 | |
| 303 | - if ( empty( $subscription ) ) { |
|
| 304 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 305 | - } |
|
| 303 | + if ( empty( $subscription ) ) { |
|
| 304 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 305 | + } |
|
| 306 | 306 | |
| 307 | - // Validate the IPN. |
|
| 308 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 309 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 310 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 307 | + // Validate the IPN. |
|
| 308 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 309 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 310 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 311 | 311 | |
| 312 | - // Activate the subscription. |
|
| 313 | - $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
| 314 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
| 315 | - $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
| 316 | - $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
| 317 | - $subscription->activate(); |
|
| 312 | + // Activate the subscription. |
|
| 313 | + $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
| 314 | + $subscription->set_date_created( current_time( 'mysql' ) ); |
|
| 315 | + $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
| 316 | + $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
| 317 | + $subscription->activate(); |
|
| 318 | 318 | |
| 319 | - // Set the transaction id. |
|
| 320 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 321 | - $invoice->set_transaction_id( $posted['txn_id'] ); |
|
| 322 | - } |
|
| 319 | + // Set the transaction id. |
|
| 320 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
| 321 | + $invoice->set_transaction_id( $posted['txn_id'] ); |
|
| 322 | + } |
|
| 323 | 323 | |
| 324 | - // Update the payment status. |
|
| 325 | - $invoice->mark_paid(); |
|
| 324 | + // Update the payment status. |
|
| 325 | + $invoice->mark_paid(); |
|
| 326 | 326 | |
| 327 | - $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 327 | + $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 328 | 328 | |
| 329 | - wpinv_error_log( 'Subscription started.', false ); |
|
| 330 | - } |
|
| 329 | + wpinv_error_log( 'Subscription started.', false ); |
|
| 330 | + } |
|
| 331 | 331 | |
| 332 | - /** |
|
| 333 | - * Handles subscription renewals. |
|
| 334 | - * |
|
| 335 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 336 | - * @param array $posted Posted data. |
|
| 337 | - */ |
|
| 338 | - protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
| 332 | + /** |
|
| 333 | + * Handles subscription renewals. |
|
| 334 | + * |
|
| 335 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 336 | + * @param array $posted Posted data. |
|
| 337 | + */ |
|
| 338 | + protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
| 339 | 339 | |
| 340 | - // Make sure the invoice has a subscription. |
|
| 341 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 340 | + // Make sure the invoice has a subscription. |
|
| 341 | + $subscription = wpinv_get_subscription( $invoice ); |
|
| 342 | 342 | |
| 343 | - if ( empty( $subscription ) ) { |
|
| 344 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 345 | - } |
|
| 343 | + if ( empty( $subscription ) ) { |
|
| 344 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 345 | + } |
|
| 346 | 346 | |
| 347 | - // Abort if this is the first payment. |
|
| 348 | - if ( ( $invoice->is_paid() && date( 'Ynd', strtotime( $invoice->get_date_completed() ) ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) || date( 'Ynd', $subscription->get_time_created() ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) { |
|
| 347 | + // Abort if this is the first payment. |
|
| 348 | + if ( ( $invoice->is_paid() && date( 'Ynd', strtotime( $invoice->get_date_completed() ) ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) || date( 'Ynd', $subscription->get_time_created() ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) { |
|
| 349 | 349 | |
| 350 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 351 | - $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
| 352 | - $invoice->mark_paid(); |
|
| 353 | - $invoice->save(); |
|
| 354 | - } |
|
| 350 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
| 351 | + $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
| 352 | + $invoice->mark_paid(); |
|
| 353 | + $invoice->save(); |
|
| 354 | + } |
|
| 355 | 355 | |
| 356 | - return; |
|
| 357 | - } |
|
| 356 | + return; |
|
| 357 | + } |
|
| 358 | 358 | |
| 359 | - wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
| 359 | + wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
| 360 | 360 | |
| 361 | - // Abort if the payment is already recorded. |
|
| 362 | - if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
| 363 | - return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - $args = array( |
|
| 367 | - 'transaction_id' => $posted['txn_id'], |
|
| 368 | - 'gateway' => $this->id, |
|
| 369 | - ); |
|
| 370 | - |
|
| 371 | - $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
| 372 | - |
|
| 373 | - if ( empty( $invoice ) ) { |
|
| 374 | - return; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
| 378 | - $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 379 | - |
|
| 380 | - $subscription->renew(); |
|
| 381 | - wpinv_error_log( 'Subscription renewed.', false ); |
|
| 382 | - |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Handles subscription cancelations. |
|
| 387 | - * |
|
| 388 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 389 | - */ |
|
| 390 | - protected function ipn_txn_subscr_cancel( $invoice ) { |
|
| 391 | - |
|
| 392 | - // Make sure the invoice has a subscription. |
|
| 393 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 394 | - |
|
| 395 | - if ( empty( $subscription ) ) { |
|
| 396 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
| 400 | - $subscription->cancel(); |
|
| 401 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 402 | - |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Handles subscription completions. |
|
| 407 | - * |
|
| 408 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 409 | - * @param array $posted Posted data. |
|
| 410 | - */ |
|
| 411 | - protected function ipn_txn_subscr_eot( $invoice ) { |
|
| 412 | - |
|
| 413 | - // Make sure the invoice has a subscription. |
|
| 414 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 361 | + // Abort if the payment is already recorded. |
|
| 362 | + if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
| 363 | + return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + $args = array( |
|
| 367 | + 'transaction_id' => $posted['txn_id'], |
|
| 368 | + 'gateway' => $this->id, |
|
| 369 | + ); |
|
| 370 | + |
|
| 371 | + $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
| 372 | + |
|
| 373 | + if ( empty( $invoice ) ) { |
|
| 374 | + return; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
| 378 | + $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 379 | + |
|
| 380 | + $subscription->renew(); |
|
| 381 | + wpinv_error_log( 'Subscription renewed.', false ); |
|
| 382 | + |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Handles subscription cancelations. |
|
| 387 | + * |
|
| 388 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 389 | + */ |
|
| 390 | + protected function ipn_txn_subscr_cancel( $invoice ) { |
|
| 391 | + |
|
| 392 | + // Make sure the invoice has a subscription. |
|
| 393 | + $subscription = wpinv_get_subscription( $invoice ); |
|
| 394 | + |
|
| 395 | + if ( empty( $subscription ) ) { |
|
| 396 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
| 400 | + $subscription->cancel(); |
|
| 401 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 402 | + |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Handles subscription completions. |
|
| 407 | + * |
|
| 408 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 409 | + * @param array $posted Posted data. |
|
| 410 | + */ |
|
| 411 | + protected function ipn_txn_subscr_eot( $invoice ) { |
|
| 412 | + |
|
| 413 | + // Make sure the invoice has a subscription. |
|
| 414 | + $subscription = wpinv_get_subscription( $invoice ); |
|
| 415 | 415 | |
| 416 | - if ( empty( $subscription ) ) { |
|
| 417 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 418 | - } |
|
| 416 | + if ( empty( $subscription ) ) { |
|
| 417 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 418 | + } |
|
| 419 | 419 | |
| 420 | - wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
| 421 | - $subscription->complete(); |
|
| 422 | - wpinv_error_log( 'Subscription completed.', false ); |
|
| 420 | + wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
| 421 | + $subscription->complete(); |
|
| 422 | + wpinv_error_log( 'Subscription completed.', false ); |
|
| 423 | 423 | |
| 424 | - } |
|
| 424 | + } |
|
| 425 | 425 | |
| 426 | - /** |
|
| 427 | - * Handles subscription fails. |
|
| 428 | - * |
|
| 429 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 430 | - * @param array $posted Posted data. |
|
| 431 | - */ |
|
| 432 | - protected function ipn_txn_subscr_failed( $invoice ) { |
|
| 433 | - |
|
| 434 | - // Make sure the invoice has a subscription. |
|
| 435 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 426 | + /** |
|
| 427 | + * Handles subscription fails. |
|
| 428 | + * |
|
| 429 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 430 | + * @param array $posted Posted data. |
|
| 431 | + */ |
|
| 432 | + protected function ipn_txn_subscr_failed( $invoice ) { |
|
| 433 | + |
|
| 434 | + // Make sure the invoice has a subscription. |
|
| 435 | + $subscription = wpinv_get_subscription( $invoice ); |
|
| 436 | 436 | |
| 437 | - if ( empty( $subscription ) ) { |
|
| 438 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 439 | - } |
|
| 437 | + if ( empty( $subscription ) ) { |
|
| 438 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 439 | + } |
|
| 440 | 440 | |
| 441 | - wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
| 442 | - $subscription->failing(); |
|
| 443 | - wpinv_error_log( 'Subscription marked as failing.', false ); |
|
| 441 | + wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
| 442 | + $subscription->failing(); |
|
| 443 | + wpinv_error_log( 'Subscription marked as failing.', false ); |
|
| 444 | 444 | |
| 445 | - } |
|
| 445 | + } |
|
| 446 | 446 | |
| 447 | - /** |
|
| 448 | - * Handles subscription suspensions. |
|
| 449 | - * |
|
| 450 | - * @param WPInv_Invoice $invoice Invoice object. |
|
| 451 | - * @param array $posted Posted data. |
|
| 452 | - */ |
|
| 453 | - protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
| 447 | + /** |
|
| 448 | + * Handles subscription suspensions. |
|
| 449 | + * |
|
| 450 | + * @param WPInv_Invoice $invoice Invoice object. |
|
| 451 | + * @param array $posted Posted data. |
|
| 452 | + */ |
|
| 453 | + protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
| 454 | 454 | |
| 455 | - // Make sure the invoice has a subscription. |
|
| 456 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 455 | + // Make sure the invoice has a subscription. |
|
| 456 | + $subscription = wpinv_get_subscription( $invoice ); |
|
| 457 | 457 | |
| 458 | - if ( empty( $subscription ) ) { |
|
| 459 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
| 463 | - $subscription->cancel(); |
|
| 464 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 465 | - } |
|
| 458 | + if ( empty( $subscription ) ) { |
|
| 459 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
| 463 | + $subscription->cancel(); |
|
| 464 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 465 | + } |
|
| 466 | 466 | |
| 467 | 467 | } |
@@ -4,7 +4,7 @@ discard block |
||
| 4 | 4 | * |
| 5 | 5 | */ |
| 6 | 6 | |
| 7 | -defined( 'ABSPATH' ) || exit; |
|
| 7 | +defined('ABSPATH') || exit; |
|
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | 10 | * Paypal Payment Gateway IPN handler class. |
@@ -31,7 +31,7 @@ discard block |
||
| 31 | 31 | * |
| 32 | 32 | * @param GetPaid_Paypal_Gateway $gateway |
| 33 | 33 | */ |
| 34 | - public function __construct( $gateway ) { |
|
| 34 | + public function __construct($gateway) { |
|
| 35 | 35 | $this->gateway = $gateway; |
| 36 | 36 | $this->verify_ipn(); |
| 37 | 37 | } |
@@ -43,37 +43,37 @@ discard block |
||
| 43 | 43 | */ |
| 44 | 44 | public function verify_ipn() { |
| 45 | 45 | |
| 46 | - wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
| 46 | + wpinv_error_log('GetPaid PayPal IPN Handler', false); |
|
| 47 | 47 | |
| 48 | 48 | // Validate the IPN. |
| 49 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
| 50 | - wp_die( 'PayPal IPN Request Failure', 500 ); |
|
| 49 | + if (empty($_POST) || !$this->validate_ipn()) { |
|
| 50 | + wp_die('PayPal IPN Request Failure', 500); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | 53 | // Process the IPN. |
| 54 | - $posted = wp_unslash( $_POST ); |
|
| 55 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
| 54 | + $posted = wp_unslash($_POST); |
|
| 55 | + $invoice = $this->get_ipn_invoice($posted); |
|
| 56 | 56 | |
| 57 | 57 | // Abort if it was not paid by our gateway. |
| 58 | - if ( $this->id != $invoice->get_gateway() ) { |
|
| 59 | - wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
| 60 | - wp_die( 'Invoice not paid via PayPal', 200 ); |
|
| 58 | + if ($this->id != $invoice->get_gateway()) { |
|
| 59 | + wpinv_error_log('Aborting, Invoice was not paid via PayPal', false); |
|
| 60 | + wp_die('Invoice not paid via PayPal', 200); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
| 64 | - $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
| 63 | + $posted['payment_status'] = isset($posted['payment_status']) ? sanitize_key(strtolower($posted['payment_status'])) : ''; |
|
| 64 | + $posted['txn_type'] = sanitize_key(strtolower($posted['txn_type'])); |
|
| 65 | 65 | |
| 66 | - wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
| 67 | - wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
| 66 | + wpinv_error_log('Payment status:' . $posted['payment_status'], false); |
|
| 67 | + wpinv_error_log('IPN Type:' . $posted['txn_type'], false); |
|
| 68 | 68 | |
| 69 | - if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
| 70 | - call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
| 71 | - wpinv_error_log( 'Done processing IPN', false ); |
|
| 72 | - wp_die( 'Processed', 200 ); |
|
| 69 | + if (method_exists($this, 'ipn_txn_' . $posted['txn_type'])) { |
|
| 70 | + call_user_func(array($this, 'ipn_txn_' . $posted['txn_type']), $invoice, $posted); |
|
| 71 | + wpinv_error_log('Done processing IPN', false); |
|
| 72 | + wp_die('Processed', 200); |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
| 76 | - wp_die( 'Unsupported IPN type', 200 ); |
|
| 75 | + wpinv_error_log('Aborting, Unsupported IPN type:' . $posted['txn_type'], false); |
|
| 76 | + wp_die('Unsupported IPN type', 200); |
|
| 77 | 77 | |
| 78 | 78 | } |
| 79 | 79 | |
@@ -83,22 +83,22 @@ discard block |
||
| 83 | 83 | * @param array $posted |
| 84 | 84 | * @return WPInv_Invoice |
| 85 | 85 | */ |
| 86 | - protected function get_ipn_invoice( $posted ) { |
|
| 86 | + protected function get_ipn_invoice($posted) { |
|
| 87 | 87 | |
| 88 | - wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
| 88 | + wpinv_error_log('Retrieving PayPal IPN Response Invoice', false); |
|
| 89 | 89 | |
| 90 | - if ( ! empty( $posted['custom'] ) ) { |
|
| 91 | - $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
| 90 | + if (!empty($posted['custom'])) { |
|
| 91 | + $invoice = new WPInv_Invoice($posted['custom']); |
|
| 92 | 92 | |
| 93 | - if ( $invoice->exists() ) { |
|
| 94 | - wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
| 93 | + if ($invoice->exists()) { |
|
| 94 | + wpinv_error_log('Found invoice #' . $invoice->get_number(), false); |
|
| 95 | 95 | return $invoice; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
| 101 | - wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
| 100 | + wpinv_error_log('Could not retrieve the associated invoice.', false); |
|
| 101 | + wp_die('Could not retrieve the associated invoice.', 200); |
|
| 102 | 102 | } |
| 103 | 103 | |
| 104 | 104 | /** |
@@ -106,14 +106,14 @@ discard block |
||
| 106 | 106 | */ |
| 107 | 107 | protected function validate_ipn() { |
| 108 | 108 | |
| 109 | - wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
| 109 | + wpinv_error_log('Validating PayPal IPN response', false); |
|
| 110 | 110 | |
| 111 | 111 | // Retrieve the associated invoice. |
| 112 | - $posted = wp_unslash( $_POST ); |
|
| 113 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
| 112 | + $posted = wp_unslash($_POST); |
|
| 113 | + $invoice = $this->get_ipn_invoice($posted); |
|
| 114 | 114 | |
| 115 | - if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
| 116 | - wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data' ); |
|
| 115 | + if ($this->gateway->is_sandbox($invoice)) { |
|
| 116 | + wpinv_error_log($posted, 'Invoice was processed in sandbox hence logging the posted data'); |
|
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | // Validate the IPN. |
@@ -130,20 +130,20 @@ discard block |
||
| 130 | 130 | ); |
| 131 | 131 | |
| 132 | 132 | // Post back to get a response. |
| 133 | - $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
| 133 | + $response = wp_safe_remote_post($this->gateway->is_sandbox($invoice) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params); |
|
| 134 | 134 | |
| 135 | 135 | // Check to see if the request was valid. |
| 136 | - if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
| 137 | - wpinv_error_log( $response['body'], 'Received valid response from PayPal IPN' ); |
|
| 136 | + if (!is_wp_error($response) && $response['response']['code'] < 300 && strstr($response['body'], 'VERIFIED')) { |
|
| 137 | + wpinv_error_log($response['body'], 'Received valid response from PayPal IPN'); |
|
| 138 | 138 | return true; |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | - if ( is_wp_error( $response ) ) { |
|
| 142 | - wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
| 141 | + if (is_wp_error($response)) { |
|
| 142 | + wpinv_error_log($response->get_error_message(), 'Received invalid response from PayPal IPN'); |
|
| 143 | 143 | return false; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | - wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
| 146 | + wpinv_error_log($response['body'], 'Received invalid response from PayPal IPN'); |
|
| 147 | 147 | return false; |
| 148 | 148 | |
| 149 | 149 | } |
@@ -154,17 +154,17 @@ discard block |
||
| 154 | 154 | * @param WPInv_Invoice $invoice Invoice object. |
| 155 | 155 | * @param string $currency currency to validate. |
| 156 | 156 | */ |
| 157 | - protected function validate_ipn_currency( $invoice, $currency ) { |
|
| 157 | + protected function validate_ipn_currency($invoice, $currency) { |
|
| 158 | 158 | |
| 159 | - if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
| 159 | + if (strtolower($invoice->get_currency()) !== strtolower($currency)) { |
|
| 160 | 160 | |
| 161 | 161 | /* translators: %s: currency code. */ |
| 162 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
| 162 | + $invoice->update_status('wpi-processing', sprintf(__('Validation error: PayPal currencies do not match (code %s).', 'invoicing'), $currency)); |
|
| 163 | 163 | |
| 164 | - wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 164 | + wpinv_error_log("Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | - wpinv_error_log( $currency, 'Validated IPN Currency' ); |
|
| 167 | + wpinv_error_log($currency, 'Validated IPN Currency'); |
|
| 168 | 168 | } |
| 169 | 169 | |
| 170 | 170 | /** |
@@ -173,16 +173,16 @@ discard block |
||
| 173 | 173 | * @param WPInv_Invoice $invoice Invoice object. |
| 174 | 174 | * @param float $amount amount to validate. |
| 175 | 175 | */ |
| 176 | - protected function validate_ipn_amount( $invoice, $amount ) { |
|
| 177 | - if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
| 176 | + protected function validate_ipn_amount($invoice, $amount) { |
|
| 177 | + if (number_format($invoice->get_total(), 2, '.', '') !== number_format($amount, 2, '.', '')) { |
|
| 178 | 178 | |
| 179 | 179 | /* translators: %s: Amount. */ |
| 180 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
| 180 | + $invoice->update_status('wpi-processing', sprintf(__('Validation error: PayPal amounts do not match (gross %s).', 'invoicing'), $amount)); |
|
| 181 | 181 | |
| 182 | - wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 182 | + wpinv_error_log("Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true); |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | - wpinv_error_log( $amount, 'Validated IPN Amount' ); |
|
| 185 | + wpinv_error_log($amount, 'Validated IPN Amount'); |
|
| 186 | 186 | } |
| 187 | 187 | |
| 188 | 188 | /** |
@@ -191,19 +191,19 @@ discard block |
||
| 191 | 191 | * @param WPInv_Invoice $invoice Invoice object. |
| 192 | 192 | * @param string $receiver_email Email to validate. |
| 193 | 193 | */ |
| 194 | - protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
| 195 | - $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
| 194 | + protected function validate_ipn_receiver_email($invoice, $receiver_email) { |
|
| 195 | + $paypal_email = wpinv_get_option('paypal_email'); |
|
| 196 | 196 | |
| 197 | - if ( strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
| 198 | - wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
| 197 | + if (strcasecmp(trim($receiver_email), trim($paypal_email)) !== 0) { |
|
| 198 | + wpinv_record_gateway_error('IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}"); |
|
| 199 | 199 | |
| 200 | 200 | /* translators: %s: email address . */ |
| 201 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
| 201 | + $invoice->update_status('wpi-processing', sprintf(__('Validation error: PayPal IPN response from a different email address (%s).', 'invoicing'), $receiver_email)); |
|
| 202 | 202 | |
| 203 | - return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
| 203 | + return wpinv_error_log("IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | - wpinv_error_log( 'Validated PayPal Email', false ); |
|
| 206 | + wpinv_error_log('Validated PayPal Email', false); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | /** |
@@ -212,68 +212,68 @@ discard block |
||
| 212 | 212 | * @param WPInv_Invoice $invoice Invoice object. |
| 213 | 213 | * @param array $posted Posted data. |
| 214 | 214 | */ |
| 215 | - protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
| 215 | + protected function ipn_txn_web_accept($invoice, $posted) { |
|
| 216 | 216 | |
| 217 | 217 | // Collect payment details |
| 218 | - $payment_status = strtolower( $posted['payment_status'] ); |
|
| 219 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 218 | + $payment_status = strtolower($posted['payment_status']); |
|
| 219 | + $business_email = isset($posted['business']) && is_email($posted['business']) ? trim($posted['business']) : trim($posted['receiver_email']); |
|
| 220 | 220 | |
| 221 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 222 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 221 | + $this->validate_ipn_receiver_email($invoice, $business_email); |
|
| 222 | + $this->validate_ipn_currency($invoice, $posted['mc_currency']); |
|
| 223 | 223 | |
| 224 | 224 | // Update the transaction id. |
| 225 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 226 | - $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
| 225 | + if (!empty($posted['txn_id'])) { |
|
| 226 | + $invoice->set_transaction_id(wpinv_clean($posted['txn_id'])); |
|
| 227 | 227 | $invoice->save(); |
| 228 | 228 | } |
| 229 | 229 | |
| 230 | 230 | // Process a refund. |
| 231 | - if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
| 231 | + if ($payment_status == 'refunded' || $payment_status == 'reversed') { |
|
| 232 | 232 | |
| 233 | - update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
| 233 | + update_post_meta($invoice->get_id(), 'refunded_remotely', 1); |
|
| 234 | 234 | |
| 235 | - if ( ! $invoice->is_refunded() ) { |
|
| 236 | - $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
| 235 | + if (!$invoice->is_refunded()) { |
|
| 236 | + $invoice->update_status('wpi-refunded', $posted['reason_code']); |
|
| 237 | 237 | } |
| 238 | 238 | |
| 239 | - return wpinv_error_log( $posted['reason_code'], false ); |
|
| 239 | + return wpinv_error_log($posted['reason_code'], false); |
|
| 240 | 240 | } |
| 241 | 241 | |
| 242 | 242 | // Process payments. |
| 243 | - if ( $payment_status == 'completed' ) { |
|
| 243 | + if ($payment_status == 'completed') { |
|
| 244 | 244 | |
| 245 | - if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
| 246 | - return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
| 245 | + if ($invoice->is_paid() && 'wpi_processing' != $invoice->get_status()) { |
|
| 246 | + return wpinv_error_log('Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false); |
|
| 247 | 247 | } |
| 248 | 248 | |
| 249 | - $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
| 249 | + $this->validate_ipn_amount($invoice, $posted['mc_gross']); |
|
| 250 | 250 | |
| 251 | 251 | $note = ''; |
| 252 | 252 | |
| 253 | - if ( ! empty( $posted['mc_fee'] ) ) { |
|
| 254 | - $note = sprintf( __( 'PayPal Transaction Fee %.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
| 253 | + if (!empty($posted['mc_fee'])) { |
|
| 254 | + $note = sprintf(__('PayPal Transaction Fee %.', 'invoicing'), sanitize_text_field($posted['mc_fee'])); |
|
| 255 | 255 | } |
| 256 | 256 | |
| 257 | - if ( ! empty( $posted['payer_status'] ) ) { |
|
| 258 | - $note = ' ' . sprintf( __( 'Buyer status %.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
| 257 | + if (!empty($posted['payer_status'])) { |
|
| 258 | + $note = ' ' . sprintf(__('Buyer status %.', 'invoicing'), sanitize_text_field($posted['payer_status'])); |
|
| 259 | 259 | } |
| 260 | 260 | |
| 261 | - $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
| 262 | - return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
| 261 | + $invoice->mark_paid((!empty($posted['txn_id']) ? sanitize_text_field($posted['txn_id']) : ''), trim($note)); |
|
| 262 | + return wpinv_error_log('Invoice marked as paid.', false); |
|
| 263 | 263 | |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | 266 | // Pending payments. |
| 267 | - if ( $payment_status == 'pending' ) { |
|
| 267 | + if ($payment_status == 'pending') { |
|
| 268 | 268 | |
| 269 | 269 | /* translators: %s: pending reason. */ |
| 270 | - $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
| 270 | + $invoice->update_status('wpi-onhold', sprintf(__('Payment pending (%s).', 'invoicing'), $posted['pending_reason'])); |
|
| 271 | 271 | |
| 272 | - return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
| 272 | + return wpinv_error_log('Invoice marked as "payment held".', false); |
|
| 273 | 273 | } |
| 274 | 274 | |
| 275 | 275 | /* translators: %s: payment status. */ |
| 276 | - $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
| 276 | + $invoice->update_status('wpi-failed', sprintf(__('Payment %s via IPN.', 'invoicing'), sanitize_text_field($posted['payment_status']))); |
|
| 277 | 277 | |
| 278 | 278 | } |
| 279 | 279 | |
@@ -283,8 +283,8 @@ discard block |
||
| 283 | 283 | * @param WPInv_Invoice $invoice Invoice object. |
| 284 | 284 | * @param array $posted Posted data. |
| 285 | 285 | */ |
| 286 | - protected function ipn_txn_cart( $invoice, $posted ) { |
|
| 287 | - $this->ipn_txn_web_accept( $invoice, $posted ); |
|
| 286 | + protected function ipn_txn_cart($invoice, $posted) { |
|
| 287 | + $this->ipn_txn_web_accept($invoice, $posted); |
|
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | /** |
@@ -293,40 +293,40 @@ discard block |
||
| 293 | 293 | * @param WPInv_Invoice $invoice Invoice object. |
| 294 | 294 | * @param array $posted Posted data. |
| 295 | 295 | */ |
| 296 | - protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
| 296 | + protected function ipn_txn_subscr_signup($invoice, $posted) { |
|
| 297 | 297 | |
| 298 | - wpinv_error_log( 'Processing subscription signup', false ); |
|
| 298 | + wpinv_error_log('Processing subscription signup', false); |
|
| 299 | 299 | |
| 300 | 300 | // Make sure the invoice has a subscription. |
| 301 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
| 301 | + $subscription = getpaid_get_invoice_subscription($invoice); |
|
| 302 | 302 | |
| 303 | - if ( empty( $subscription ) ) { |
|
| 304 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 303 | + if (empty($subscription)) { |
|
| 304 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | // Validate the IPN. |
| 308 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
| 309 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
| 310 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
| 308 | + $business_email = isset($posted['business']) && is_email($posted['business']) ? trim($posted['business']) : trim($posted['receiver_email']); |
|
| 309 | + $this->validate_ipn_receiver_email($invoice, $business_email); |
|
| 310 | + $this->validate_ipn_currency($invoice, $posted['mc_currency']); |
|
| 311 | 311 | |
| 312 | 312 | // Activate the subscription. |
| 313 | - $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
| 314 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
| 315 | - $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
| 316 | - $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
| 313 | + $duration = strtotime($subscription->get_expiration()) - strtotime($subscription->get_date_created()); |
|
| 314 | + $subscription->set_date_created(current_time('mysql')); |
|
| 315 | + $subscription->set_expiration(date('Y-m-d H:i:s', (current_time('timestamp') + $duration))); |
|
| 316 | + $subscription->set_profile_id(sanitize_text_field($posted['subscr_id'])); |
|
| 317 | 317 | $subscription->activate(); |
| 318 | 318 | |
| 319 | 319 | // Set the transaction id. |
| 320 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 321 | - $invoice->set_transaction_id( $posted['txn_id'] ); |
|
| 320 | + if (!empty($posted['txn_id'])) { |
|
| 321 | + $invoice->set_transaction_id($posted['txn_id']); |
|
| 322 | 322 | } |
| 323 | 323 | |
| 324 | 324 | // Update the payment status. |
| 325 | 325 | $invoice->mark_paid(); |
| 326 | 326 | |
| 327 | - $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 327 | + $invoice->add_note(sprintf(__('PayPal Subscription ID: %s', 'invoicing'), $posted['subscr_id']), false, false, true); |
|
| 328 | 328 | |
| 329 | - wpinv_error_log( 'Subscription started.', false ); |
|
| 329 | + wpinv_error_log('Subscription started.', false); |
|
| 330 | 330 | } |
| 331 | 331 | |
| 332 | 332 | /** |
@@ -335,20 +335,20 @@ discard block |
||
| 335 | 335 | * @param WPInv_Invoice $invoice Invoice object. |
| 336 | 336 | * @param array $posted Posted data. |
| 337 | 337 | */ |
| 338 | - protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
| 338 | + protected function ipn_txn_subscr_payment($invoice, $posted) { |
|
| 339 | 339 | |
| 340 | 340 | // Make sure the invoice has a subscription. |
| 341 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 341 | + $subscription = wpinv_get_subscription($invoice); |
|
| 342 | 342 | |
| 343 | - if ( empty( $subscription ) ) { |
|
| 344 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 343 | + if (empty($subscription)) { |
|
| 344 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | // Abort if this is the first payment. |
| 348 | - if ( ( $invoice->is_paid() && date( 'Ynd', strtotime( $invoice->get_date_completed() ) ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) || date( 'Ynd', $subscription->get_time_created() ) == date( 'Ynd', strtotime( $posted['payment_date'] ) ) ) { |
|
| 348 | + if (($invoice->is_paid() && date('Ynd', strtotime($invoice->get_date_completed())) == date('Ynd', strtotime($posted['payment_date']))) || date('Ynd', $subscription->get_time_created()) == date('Ynd', strtotime($posted['payment_date']))) { |
|
| 349 | 349 | |
| 350 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
| 351 | - $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
| 350 | + if (!empty($posted['txn_id'])) { |
|
| 351 | + $invoice->set_transaction_id(sanitize_text_field($posted['txn_id'])); |
|
| 352 | 352 | $invoice->mark_paid(); |
| 353 | 353 | $invoice->save(); |
| 354 | 354 | } |
@@ -356,11 +356,11 @@ discard block |
||
| 356 | 356 | return; |
| 357 | 357 | } |
| 358 | 358 | |
| 359 | - wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
| 359 | + wpinv_error_log('Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false); |
|
| 360 | 360 | |
| 361 | 361 | // Abort if the payment is already recorded. |
| 362 | - if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
| 363 | - return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] .' has already been processed', false ); |
|
| 362 | + if (wpinv_get_id_by_transaction_id($posted['txn_id'])) { |
|
| 363 | + return wpinv_error_log('Aborting, Transaction ' . $posted['txn_id'] . ' has already been processed', false); |
|
| 364 | 364 | } |
| 365 | 365 | |
| 366 | 366 | $args = array( |
@@ -368,17 +368,17 @@ discard block |
||
| 368 | 368 | 'gateway' => $this->id, |
| 369 | 369 | ); |
| 370 | 370 | |
| 371 | - $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
| 371 | + $invoice = wpinv_get_invoice($subscription->add_payment($args)); |
|
| 372 | 372 | |
| 373 | - if ( empty( $invoice ) ) { |
|
| 373 | + if (empty($invoice)) { |
|
| 374 | 374 | return; |
| 375 | 375 | } |
| 376 | 376 | |
| 377 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $posted['txn_id'] ), false, false, true ); |
|
| 378 | - $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $posted['subscr_id'] ), false, false, true ); |
|
| 377 | + $invoice->add_note(wp_sprintf(__('PayPal Transaction ID: %s', 'invoicing'), $posted['txn_id']), false, false, true); |
|
| 378 | + $invoice->add_note(wp_sprintf(__('PayPal Subscription ID: %s', 'invoicing'), $posted['subscr_id']), false, false, true); |
|
| 379 | 379 | |
| 380 | 380 | $subscription->renew(); |
| 381 | - wpinv_error_log( 'Subscription renewed.', false ); |
|
| 381 | + wpinv_error_log('Subscription renewed.', false); |
|
| 382 | 382 | |
| 383 | 383 | } |
| 384 | 384 | |
@@ -387,18 +387,18 @@ discard block |
||
| 387 | 387 | * |
| 388 | 388 | * @param WPInv_Invoice $invoice Invoice object. |
| 389 | 389 | */ |
| 390 | - protected function ipn_txn_subscr_cancel( $invoice ) { |
|
| 390 | + protected function ipn_txn_subscr_cancel($invoice) { |
|
| 391 | 391 | |
| 392 | 392 | // Make sure the invoice has a subscription. |
| 393 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 393 | + $subscription = wpinv_get_subscription($invoice); |
|
| 394 | 394 | |
| 395 | - if ( empty( $subscription ) ) { |
|
| 396 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 395 | + if (empty($subscription)) { |
|
| 396 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 397 | 397 | } |
| 398 | 398 | |
| 399 | - wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
| 399 | + wpinv_error_log('Processing subscription cancellation for the invoice ' . $invoice->get_id(), false); |
|
| 400 | 400 | $subscription->cancel(); |
| 401 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 401 | + wpinv_error_log('Subscription cancelled.', false); |
|
| 402 | 402 | |
| 403 | 403 | } |
| 404 | 404 | |
@@ -408,18 +408,18 @@ discard block |
||
| 408 | 408 | * @param WPInv_Invoice $invoice Invoice object. |
| 409 | 409 | * @param array $posted Posted data. |
| 410 | 410 | */ |
| 411 | - protected function ipn_txn_subscr_eot( $invoice ) { |
|
| 411 | + protected function ipn_txn_subscr_eot($invoice) { |
|
| 412 | 412 | |
| 413 | 413 | // Make sure the invoice has a subscription. |
| 414 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 414 | + $subscription = wpinv_get_subscription($invoice); |
|
| 415 | 415 | |
| 416 | - if ( empty( $subscription ) ) { |
|
| 417 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 416 | + if (empty($subscription)) { |
|
| 417 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 418 | 418 | } |
| 419 | 419 | |
| 420 | - wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
| 420 | + wpinv_error_log('Processing subscription end of life for the invoice ' . $invoice->get_id(), false); |
|
| 421 | 421 | $subscription->complete(); |
| 422 | - wpinv_error_log( 'Subscription completed.', false ); |
|
| 422 | + wpinv_error_log('Subscription completed.', false); |
|
| 423 | 423 | |
| 424 | 424 | } |
| 425 | 425 | |
@@ -429,18 +429,18 @@ discard block |
||
| 429 | 429 | * @param WPInv_Invoice $invoice Invoice object. |
| 430 | 430 | * @param array $posted Posted data. |
| 431 | 431 | */ |
| 432 | - protected function ipn_txn_subscr_failed( $invoice ) { |
|
| 432 | + protected function ipn_txn_subscr_failed($invoice) { |
|
| 433 | 433 | |
| 434 | 434 | // Make sure the invoice has a subscription. |
| 435 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 435 | + $subscription = wpinv_get_subscription($invoice); |
|
| 436 | 436 | |
| 437 | - if ( empty( $subscription ) ) { |
|
| 438 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 437 | + if (empty($subscription)) { |
|
| 438 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 439 | 439 | } |
| 440 | 440 | |
| 441 | - wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
| 441 | + wpinv_error_log('Processing subscription payment failure for the invoice ' . $invoice->get_id(), false); |
|
| 442 | 442 | $subscription->failing(); |
| 443 | - wpinv_error_log( 'Subscription marked as failing.', false ); |
|
| 443 | + wpinv_error_log('Subscription marked as failing.', false); |
|
| 444 | 444 | |
| 445 | 445 | } |
| 446 | 446 | |
@@ -450,18 +450,18 @@ discard block |
||
| 450 | 450 | * @param WPInv_Invoice $invoice Invoice object. |
| 451 | 451 | * @param array $posted Posted data. |
| 452 | 452 | */ |
| 453 | - protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
| 453 | + protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment($invoice) { |
|
| 454 | 454 | |
| 455 | 455 | // Make sure the invoice has a subscription. |
| 456 | - $subscription = wpinv_get_subscription( $invoice ); |
|
| 456 | + $subscription = wpinv_get_subscription($invoice); |
|
| 457 | 457 | |
| 458 | - if ( empty( $subscription ) ) { |
|
| 459 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
| 458 | + if (empty($subscription)) { |
|
| 459 | + return wpinv_error_log('Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false); |
|
| 460 | 460 | } |
| 461 | 461 | |
| 462 | - wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
| 462 | + wpinv_error_log('Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false); |
|
| 463 | 463 | $subscription->cancel(); |
| 464 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
| 464 | + wpinv_error_log('Subscription cancelled.', false); |
|
| 465 | 465 | } |
| 466 | 466 | |
| 467 | 467 | } |