@@ -12,290 +12,290 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Payment_Form_Submission_Refresh_Prices { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Contains the response for refreshing prices. |
|
| 17 | - * @var array |
|
| 18 | - */ |
|
| 19 | - public $response = array(); |
|
| 15 | + /** |
|
| 16 | + * Contains the response for refreshing prices. |
|
| 17 | + * @var array |
|
| 18 | + */ |
|
| 19 | + public $response = array(); |
|
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | - * Class constructor |
|
| 23 | - * |
|
| 24 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 25 | - */ |
|
| 26 | - public function __construct( $submission ) { |
|
| 27 | - |
|
| 28 | - $this->response = array( |
|
| 29 | - 'submission_id' => $submission->id, |
|
| 22 | + * Class constructor |
|
| 23 | + * |
|
| 24 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 25 | + */ |
|
| 26 | + public function __construct( $submission ) { |
|
| 27 | + |
|
| 28 | + $this->response = array( |
|
| 29 | + 'submission_id' => $submission->id, |
|
| 30 | 30 | 'has_recurring' => $submission->has_recurring, |
| 31 | - 'has_subscription_group' => $submission->has_subscription_group(), |
|
| 32 | - 'has_multiple_subscription_groups' => $submission->has_multiple_subscription_groups(), |
|
| 31 | + 'has_subscription_group' => $submission->has_subscription_group(), |
|
| 32 | + 'has_multiple_subscription_groups' => $submission->has_multiple_subscription_groups(), |
|
| 33 | 33 | 'is_free' => ! $submission->should_collect_payment_details(), |
| 34 | - ); |
|
| 35 | - |
|
| 36 | - $this->add_totals( $submission ); |
|
| 37 | - $this->add_texts( $submission ); |
|
| 38 | - $this->add_items( $submission ); |
|
| 39 | - $this->add_fees( $submission ); |
|
| 40 | - $this->add_discounts( $submission ); |
|
| 41 | - $this->add_taxes( $submission ); |
|
| 42 | - $this->add_gateways( $submission ); |
|
| 43 | - $this->add_data( $submission ); |
|
| 44 | - |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Adds totals to a response for submission refresh prices. |
|
| 49 | - * |
|
| 50 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 51 | - */ |
|
| 52 | - public function add_totals( $submission ) { |
|
| 53 | - |
|
| 54 | - $this->response = array_merge( |
|
| 55 | - $this->response, |
|
| 56 | - array( |
|
| 57 | - |
|
| 58 | - 'totals' => array( |
|
| 59 | - 'subtotal' => $submission->format_amount( $submission->get_subtotal() ), |
|
| 60 | - 'discount' => $submission->format_amount( $submission->get_discount() ), |
|
| 61 | - 'fees' => $submission->format_amount( $submission->get_fee() ), |
|
| 62 | - 'tax' => $submission->format_amount( $submission->get_tax() ), |
|
| 63 | - 'total' => $submission->format_amount( $submission->get_total() ), |
|
| 64 | - 'raw_total' => html_entity_decode( sanitize_text_field( $submission->format_amount( $submission->get_total() ) ), ENT_QUOTES ), |
|
| 65 | - ), |
|
| 66 | - |
|
| 67 | - 'recurring' => array( |
|
| 68 | - 'subtotal' => $submission->format_amount( $submission->get_recurring_subtotal() ), |
|
| 69 | - 'discount' => $submission->format_amount( $submission->get_recurring_discount() ), |
|
| 70 | - 'fees' => $submission->format_amount( $submission->get_recurring_fee() ), |
|
| 71 | - 'tax' => $submission->format_amount( $submission->get_recurring_tax() ), |
|
| 72 | - 'total' => $submission->format_amount( $submission->get_recurring_total() ), |
|
| 73 | - ), |
|
| 74 | - |
|
| 75 | - 'initial_amt' => wpinv_round_amount( $submission->get_total(), null, true ), |
|
| 76 | - 'currency' => $submission->get_currency(), |
|
| 77 | - |
|
| 78 | - ) |
|
| 79 | - ); |
|
| 80 | - |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Adds texts to a response for submission refresh prices. |
|
| 85 | - * |
|
| 86 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 87 | - */ |
|
| 88 | - public function add_texts( $submission ) { |
|
| 89 | - |
|
| 90 | - $payable = $submission->format_amount( $submission->get_total() ); |
|
| 91 | - $groups = getpaid_get_subscription_groups( $submission ); |
|
| 92 | - |
|
| 93 | - if ( $submission->has_recurring && 2 > count( $groups ) ) { |
|
| 94 | - |
|
| 95 | - $recurring = new WPInv_Item( $submission->has_recurring ); |
|
| 96 | - $period = getpaid_get_subscription_period_label( $recurring->get_recurring_period( true ), $recurring->get_recurring_interval(), '' ); |
|
| 97 | - $main_item = reset( $groups ); |
|
| 98 | - |
|
| 99 | - if ( $submission->get_total() == $submission->get_recurring_total() ) { |
|
| 100 | - $payable = "$payable / $period"; |
|
| 101 | - } elseif ( $main_item ) { |
|
| 102 | - |
|
| 103 | - $main_item = reset( $main_item ); |
|
| 104 | - |
|
| 105 | - // Calculate the next renewal date. |
|
| 106 | - $_period = $main_item->get_recurring_period( true ); |
|
| 107 | - $_interval = $main_item->get_recurring_interval(); |
|
| 108 | - |
|
| 109 | - // If the subscription item has a trial period... |
|
| 110 | - if ( $main_item->has_free_trial() ) { |
|
| 111 | - $_period = $main_item->get_trial_period( true ); |
|
| 112 | - $_interval = $main_item->get_trial_interval(); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - $payable = sprintf( |
|
| 116 | - __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
| 117 | - $submission->format_amount( $submission->get_total() ), |
|
| 118 | - $submission->format_amount( $submission->get_recurring_total() ), |
|
| 119 | - $period |
|
| 120 | - ); |
|
| 121 | - |
|
| 122 | - $payable .= sprintf( |
|
| 123 | - '<small class="text-muted form-text">%s</small>', |
|
| 124 | - sprintf( |
|
| 125 | - __( 'First renewal on %s', 'invoicing' ), |
|
| 126 | - getpaid_format_date( date( 'Y-m-d H:i:s', strtotime( "+$_interval $_period", current_time( 'timestamp' ) ) ) ) |
|
| 127 | - ) |
|
| 128 | - ); |
|
| 129 | - |
|
| 130 | - } else { |
|
| 131 | - $payable = sprintf( |
|
| 132 | - __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
| 133 | - $submission->format_amount( $submission->get_total() ), |
|
| 134 | - $submission->format_amount( $submission->get_recurring_total() ), |
|
| 135 | - $period |
|
| 136 | - ); |
|
| 137 | - } |
|
| 138 | -} |
|
| 34 | + ); |
|
| 139 | 35 | |
| 140 | - $texts = array( |
|
| 141 | - '.getpaid-checkout-total-payable' => $payable, |
|
| 142 | - ); |
|
| 36 | + $this->add_totals( $submission ); |
|
| 37 | + $this->add_texts( $submission ); |
|
| 38 | + $this->add_items( $submission ); |
|
| 39 | + $this->add_fees( $submission ); |
|
| 40 | + $this->add_discounts( $submission ); |
|
| 41 | + $this->add_taxes( $submission ); |
|
| 42 | + $this->add_gateways( $submission ); |
|
| 43 | + $this->add_data( $submission ); |
|
| 143 | 44 | |
| 144 | - foreach ( $submission->get_items() as $item ) { |
|
| 145 | - $item_id = $item->get_id(); |
|
| 146 | - $initial_price = $submission->format_amount( $item->get_sub_total() - $item->item_discount ); |
|
| 147 | - $recurring_price = $submission->format_amount( $item->get_recurring_sub_total() - $item->recurring_item_discount ); |
|
| 148 | - $texts[ ".item-$item_id .getpaid-form-item-price-desc" ] = getpaid_item_recurring_price_help_text( $item, $submission->get_currency(), $initial_price, $recurring_price ); |
|
| 149 | - $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = sprintf( __( 'Subtotal: %s', 'invoicing' ), $submission->format_amount( $item->get_sub_total() ) ); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Adds totals to a response for submission refresh prices. |
|
| 49 | + * |
|
| 50 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 51 | + */ |
|
| 52 | + public function add_totals( $submission ) { |
|
| 53 | + |
|
| 54 | + $this->response = array_merge( |
|
| 55 | + $this->response, |
|
| 56 | + array( |
|
| 57 | + |
|
| 58 | + 'totals' => array( |
|
| 59 | + 'subtotal' => $submission->format_amount( $submission->get_subtotal() ), |
|
| 60 | + 'discount' => $submission->format_amount( $submission->get_discount() ), |
|
| 61 | + 'fees' => $submission->format_amount( $submission->get_fee() ), |
|
| 62 | + 'tax' => $submission->format_amount( $submission->get_tax() ), |
|
| 63 | + 'total' => $submission->format_amount( $submission->get_total() ), |
|
| 64 | + 'raw_total' => html_entity_decode( sanitize_text_field( $submission->format_amount( $submission->get_total() ) ), ENT_QUOTES ), |
|
| 65 | + ), |
|
| 66 | + |
|
| 67 | + 'recurring' => array( |
|
| 68 | + 'subtotal' => $submission->format_amount( $submission->get_recurring_subtotal() ), |
|
| 69 | + 'discount' => $submission->format_amount( $submission->get_recurring_discount() ), |
|
| 70 | + 'fees' => $submission->format_amount( $submission->get_recurring_fee() ), |
|
| 71 | + 'tax' => $submission->format_amount( $submission->get_recurring_tax() ), |
|
| 72 | + 'total' => $submission->format_amount( $submission->get_recurring_total() ), |
|
| 73 | + ), |
|
| 74 | + |
|
| 75 | + 'initial_amt' => wpinv_round_amount( $submission->get_total(), null, true ), |
|
| 76 | + 'currency' => $submission->get_currency(), |
|
| 77 | + |
|
| 78 | + ) |
|
| 79 | + ); |
|
| 80 | + |
|
| 81 | + } |
|
| 150 | 82 | |
| 151 | - if ( $item->get_quantity() == 1 ) { |
|
| 152 | - $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = ''; |
|
| 153 | - } |
|
| 83 | + /** |
|
| 84 | + * Adds texts to a response for submission refresh prices. |
|
| 85 | + * |
|
| 86 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 87 | + */ |
|
| 88 | + public function add_texts( $submission ) { |
|
| 89 | + |
|
| 90 | + $payable = $submission->format_amount( $submission->get_total() ); |
|
| 91 | + $groups = getpaid_get_subscription_groups( $submission ); |
|
| 92 | + |
|
| 93 | + if ( $submission->has_recurring && 2 > count( $groups ) ) { |
|
| 94 | + |
|
| 95 | + $recurring = new WPInv_Item( $submission->has_recurring ); |
|
| 96 | + $period = getpaid_get_subscription_period_label( $recurring->get_recurring_period( true ), $recurring->get_recurring_interval(), '' ); |
|
| 97 | + $main_item = reset( $groups ); |
|
| 98 | + |
|
| 99 | + if ( $submission->get_total() == $submission->get_recurring_total() ) { |
|
| 100 | + $payable = "$payable / $period"; |
|
| 101 | + } elseif ( $main_item ) { |
|
| 102 | + |
|
| 103 | + $main_item = reset( $main_item ); |
|
| 104 | + |
|
| 105 | + // Calculate the next renewal date. |
|
| 106 | + $_period = $main_item->get_recurring_period( true ); |
|
| 107 | + $_interval = $main_item->get_recurring_interval(); |
|
| 108 | + |
|
| 109 | + // If the subscription item has a trial period... |
|
| 110 | + if ( $main_item->has_free_trial() ) { |
|
| 111 | + $_period = $main_item->get_trial_period( true ); |
|
| 112 | + $_interval = $main_item->get_trial_interval(); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + $payable = sprintf( |
|
| 116 | + __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
| 117 | + $submission->format_amount( $submission->get_total() ), |
|
| 118 | + $submission->format_amount( $submission->get_recurring_total() ), |
|
| 119 | + $period |
|
| 120 | + ); |
|
| 121 | + |
|
| 122 | + $payable .= sprintf( |
|
| 123 | + '<small class="text-muted form-text">%s</small>', |
|
| 124 | + sprintf( |
|
| 125 | + __( 'First renewal on %s', 'invoicing' ), |
|
| 126 | + getpaid_format_date( date( 'Y-m-d H:i:s', strtotime( "+$_interval $_period", current_time( 'timestamp' ) ) ) ) |
|
| 127 | + ) |
|
| 128 | + ); |
|
| 129 | + |
|
| 130 | + } else { |
|
| 131 | + $payable = sprintf( |
|
| 132 | + __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
| 133 | + $submission->format_amount( $submission->get_total() ), |
|
| 134 | + $submission->format_amount( $submission->get_recurring_total() ), |
|
| 135 | + $period |
|
| 136 | + ); |
|
| 137 | + } |
|
| 154 | 138 | } |
| 155 | 139 | |
| 156 | - $this->response = array_merge( $this->response, array( 'texts' => $texts ) ); |
|
| 140 | + $texts = array( |
|
| 141 | + '.getpaid-checkout-total-payable' => $payable, |
|
| 142 | + ); |
|
| 157 | 143 | |
| 158 | - } |
|
| 144 | + foreach ( $submission->get_items() as $item ) { |
|
| 145 | + $item_id = $item->get_id(); |
|
| 146 | + $initial_price = $submission->format_amount( $item->get_sub_total() - $item->item_discount ); |
|
| 147 | + $recurring_price = $submission->format_amount( $item->get_recurring_sub_total() - $item->recurring_item_discount ); |
|
| 148 | + $texts[ ".item-$item_id .getpaid-form-item-price-desc" ] = getpaid_item_recurring_price_help_text( $item, $submission->get_currency(), $initial_price, $recurring_price ); |
|
| 149 | + $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = sprintf( __( 'Subtotal: %s', 'invoicing' ), $submission->format_amount( $item->get_sub_total() ) ); |
|
| 150 | + |
|
| 151 | + if ( $item->get_quantity() == 1 ) { |
|
| 152 | + $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = ''; |
|
| 153 | + } |
|
| 154 | +} |
|
| 159 | 155 | |
| 160 | - /** |
|
| 161 | - * Adds items to a response for submission refresh prices. |
|
| 162 | - * |
|
| 163 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 164 | - */ |
|
| 165 | - public function add_items( $submission ) { |
|
| 156 | + $this->response = array_merge( $this->response, array( 'texts' => $texts ) ); |
|
| 166 | 157 | |
| 167 | - // Add items. |
|
| 168 | - $items = array(); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Adds items to a response for submission refresh prices. |
|
| 162 | + * |
|
| 163 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 164 | + */ |
|
| 165 | + public function add_items( $submission ) { |
|
| 166 | + |
|
| 167 | + // Add items. |
|
| 168 | + $items = array(); |
|
| 169 | 169 | |
| 170 | 170 | foreach ( $submission->get_items() as $item ) { |
| 171 | - $item_id = $item->get_id(); |
|
| 172 | - $items[ "$item_id" ] = $submission->format_amount( $item->get_sub_total() ); |
|
| 173 | - } |
|
| 171 | + $item_id = $item->get_id(); |
|
| 172 | + $items[ "$item_id" ] = $submission->format_amount( $item->get_sub_total() ); |
|
| 173 | + } |
|
| 174 | 174 | |
| 175 | - $this->response = array_merge( |
|
| 176 | - $this->response, |
|
| 177 | - array( 'items' => $items ) |
|
| 178 | - ); |
|
| 175 | + $this->response = array_merge( |
|
| 176 | + $this->response, |
|
| 177 | + array( 'items' => $items ) |
|
| 178 | + ); |
|
| 179 | 179 | |
| 180 | - } |
|
| 180 | + } |
|
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Adds fees to a response for submission refresh prices. |
|
| 184 | - * |
|
| 185 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 186 | - */ |
|
| 187 | - public function add_fees( $submission ) { |
|
| 182 | + /** |
|
| 183 | + * Adds fees to a response for submission refresh prices. |
|
| 184 | + * |
|
| 185 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 186 | + */ |
|
| 187 | + public function add_fees( $submission ) { |
|
| 188 | 188 | |
| 189 | - $fees = array(); |
|
| 189 | + $fees = array(); |
|
| 190 | 190 | |
| 191 | 191 | foreach ( $submission->get_fees() as $name => $data ) { |
| 192 | - $fees[ $name ] = $submission->format_amount( $data['initial_fee'] ); |
|
| 193 | - } |
|
| 192 | + $fees[ $name ] = $submission->format_amount( $data['initial_fee'] ); |
|
| 193 | + } |
|
| 194 | 194 | |
| 195 | - $this->response = array_merge( |
|
| 196 | - $this->response, |
|
| 197 | - array( 'fees' => $fees ) |
|
| 198 | - ); |
|
| 195 | + $this->response = array_merge( |
|
| 196 | + $this->response, |
|
| 197 | + array( 'fees' => $fees ) |
|
| 198 | + ); |
|
| 199 | 199 | |
| 200 | - } |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * Adds discounts to a response for submission refresh prices. |
|
| 204 | - * |
|
| 205 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 206 | - */ |
|
| 207 | - public function add_discounts( $submission ) { |
|
| 202 | + /** |
|
| 203 | + * Adds discounts to a response for submission refresh prices. |
|
| 204 | + * |
|
| 205 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 206 | + */ |
|
| 207 | + public function add_discounts( $submission ) { |
|
| 208 | 208 | |
| 209 | - $discounts = array(); |
|
| 209 | + $discounts = array(); |
|
| 210 | 210 | |
| 211 | 211 | foreach ( $submission->get_discounts() as $name => $data ) { |
| 212 | - $discounts[ $name ] = $submission->format_amount( $data['initial_discount'] ); |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - $this->response = array_merge( |
|
| 216 | - $this->response, |
|
| 217 | - array( 'discounts' => $discounts ) |
|
| 218 | - ); |
|
| 212 | + $discounts[ $name ] = $submission->format_amount( $data['initial_discount'] ); |
|
| 213 | + } |
|
| 219 | 214 | |
| 220 | - } |
|
| 215 | + $this->response = array_merge( |
|
| 216 | + $this->response, |
|
| 217 | + array( 'discounts' => $discounts ) |
|
| 218 | + ); |
|
| 221 | 219 | |
| 222 | - /** |
|
| 223 | - * Adds taxes to a response for submission refresh prices. |
|
| 224 | - * |
|
| 225 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 226 | - */ |
|
| 227 | - public function add_taxes( $submission ) { |
|
| 220 | + } |
|
| 228 | 221 | |
| 229 | - $taxes = array(); |
|
| 230 | - $markup = ''; |
|
| 222 | + /** |
|
| 223 | + * Adds taxes to a response for submission refresh prices. |
|
| 224 | + * |
|
| 225 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 226 | + */ |
|
| 227 | + public function add_taxes( $submission ) { |
|
| 228 | + |
|
| 229 | + $taxes = array(); |
|
| 230 | + $markup = ''; |
|
| 231 | 231 | foreach ( $submission->get_taxes() as $name => $data ) { |
| 232 | - $name = sanitize_text_field( $name ); |
|
| 233 | - $amount = $submission->format_amount( $data['initial_tax'] ); |
|
| 234 | - $taxes[ $name ] = $amount; |
|
| 235 | - $markup .= "<small class='form-text'>$name : $amount</small>"; |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - if ( wpinv_display_individual_tax_rates() && ! empty( $taxes ) ) { |
|
| 239 | - $this->response['texts']['.getpaid-form-cart-totals-total-tax'] = $markup; |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - $this->response = array_merge( |
|
| 243 | - $this->response, |
|
| 244 | - array( 'taxes' => $taxes ) |
|
| 245 | - ); |
|
| 246 | - |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * Adds gateways to a response for submission refresh prices. |
|
| 251 | - * |
|
| 252 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 253 | - */ |
|
| 254 | - public function add_gateways( $submission ) { |
|
| 255 | - |
|
| 256 | - $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
| 257 | - |
|
| 258 | - if ( $this->response['has_recurring'] ) { |
|
| 259 | - |
|
| 260 | - foreach ( $gateways as $i => $gateway ) { |
|
| 261 | - |
|
| 262 | - if ( |
|
| 263 | - ! getpaid_payment_gateway_supports( $gateway, 'subscription' ) |
|
| 264 | - || ( $this->response['has_subscription_group'] && ! getpaid_payment_gateway_supports( $gateway, 'single_subscription_group' ) ) |
|
| 265 | - || ( $this->response['has_multiple_subscription_groups'] && ! getpaid_payment_gateway_supports( $gateway, 'multiple_subscription_groups' ) ) ) { |
|
| 266 | - unset( $gateways[ $i ] ); |
|
| 267 | - } |
|
| 232 | + $name = sanitize_text_field( $name ); |
|
| 233 | + $amount = $submission->format_amount( $data['initial_tax'] ); |
|
| 234 | + $taxes[ $name ] = $amount; |
|
| 235 | + $markup .= "<small class='form-text'>$name : $amount</small>"; |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + if ( wpinv_display_individual_tax_rates() && ! empty( $taxes ) ) { |
|
| 239 | + $this->response['texts']['.getpaid-form-cart-totals-total-tax'] = $markup; |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + $this->response = array_merge( |
|
| 243 | + $this->response, |
|
| 244 | + array( 'taxes' => $taxes ) |
|
| 245 | + ); |
|
| 246 | + |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * Adds gateways to a response for submission refresh prices. |
|
| 251 | + * |
|
| 252 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 253 | + */ |
|
| 254 | + public function add_gateways( $submission ) { |
|
| 255 | + |
|
| 256 | + $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
| 257 | + |
|
| 258 | + if ( $this->response['has_recurring'] ) { |
|
| 259 | + |
|
| 260 | + foreach ( $gateways as $i => $gateway ) { |
|
| 261 | + |
|
| 262 | + if ( |
|
| 263 | + ! getpaid_payment_gateway_supports( $gateway, 'subscription' ) |
|
| 264 | + || ( $this->response['has_subscription_group'] && ! getpaid_payment_gateway_supports( $gateway, 'single_subscription_group' ) ) |
|
| 265 | + || ( $this->response['has_multiple_subscription_groups'] && ! getpaid_payment_gateway_supports( $gateway, 'multiple_subscription_groups' ) ) ) { |
|
| 266 | + unset( $gateways[ $i ] ); |
|
| 267 | + } |
|
| 268 | 268 | } |
| 269 | 269 | } |
| 270 | 270 | |
| 271 | - $gateways = apply_filters( 'getpaid_submission_gateways', $gateways, $submission ); |
|
| 272 | - $this->response = array_merge( |
|
| 273 | - $this->response, |
|
| 274 | - array( 'gateways' => $gateways ) |
|
| 275 | - ); |
|
| 276 | - |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * Adds data to a response for submission refresh prices. |
|
| 281 | - * |
|
| 282 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 283 | - */ |
|
| 284 | - public function add_data( $submission ) { |
|
| 285 | - |
|
| 286 | - $this->response = array_merge( |
|
| 287 | - $this->response, |
|
| 288 | - array( |
|
| 289 | - 'js_data' => apply_filters( |
|
| 290 | - 'getpaid_submission_js_data', |
|
| 291 | - array( |
|
| 292 | - 'is_recurring' => $this->response['has_recurring'], |
|
| 293 | - ), |
|
| 294 | - $submission |
|
| 295 | - ), |
|
| 296 | - ) |
|
| 297 | - ); |
|
| 298 | - |
|
| 299 | - } |
|
| 271 | + $gateways = apply_filters( 'getpaid_submission_gateways', $gateways, $submission ); |
|
| 272 | + $this->response = array_merge( |
|
| 273 | + $this->response, |
|
| 274 | + array( 'gateways' => $gateways ) |
|
| 275 | + ); |
|
| 276 | + |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * Adds data to a response for submission refresh prices. |
|
| 281 | + * |
|
| 282 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 283 | + */ |
|
| 284 | + public function add_data( $submission ) { |
|
| 285 | + |
|
| 286 | + $this->response = array_merge( |
|
| 287 | + $this->response, |
|
| 288 | + array( |
|
| 289 | + 'js_data' => apply_filters( |
|
| 290 | + 'getpaid_submission_js_data', |
|
| 291 | + array( |
|
| 292 | + 'is_recurring' => $this->response['has_recurring'], |
|
| 293 | + ), |
|
| 294 | + $submission |
|
| 295 | + ), |
|
| 296 | + ) |
|
| 297 | + ); |
|
| 298 | + |
|
| 299 | + } |
|
| 300 | 300 | |
| 301 | 301 | } |
@@ -12,184 +12,184 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Checkout { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var GetPaid_Payment_Form_Submission |
|
| 17 | - */ |
|
| 18 | - protected $payment_form_submission; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Class constructor. |
|
| 22 | - * |
|
| 23 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 24 | - */ |
|
| 25 | - public function __construct( $submission ) { |
|
| 26 | - $this->payment_form_submission = $submission; |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - /** |
|
| 30 | - * Processes the checkout. |
|
| 31 | - * |
|
| 32 | - */ |
|
| 33 | - public function process_checkout() { |
|
| 34 | - |
|
| 35 | - // Validate the submission. |
|
| 36 | - $this->validate_submission(); |
|
| 37 | - |
|
| 38 | - // Prepare the invoice. |
|
| 39 | - $items = $this->get_submission_items(); |
|
| 40 | - $invoice = $this->get_submission_invoice(); |
|
| 41 | - $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
| 42 | - $prepared = $this->prepare_submission_data_for_saving(); |
|
| 43 | - |
|
| 44 | - $this->prepare_billing_info( $invoice ); |
|
| 45 | - |
|
| 46 | - $shipping = $this->prepare_shipping_info( $invoice ); |
|
| 47 | - |
|
| 48 | - // Save the invoice. |
|
| 49 | - $invoice->set_is_viewed( true ); |
|
| 50 | - $invoice->recalculate_total(); |
|
| 15 | + /** |
|
| 16 | + * @var GetPaid_Payment_Form_Submission |
|
| 17 | + */ |
|
| 18 | + protected $payment_form_submission; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Class constructor. |
|
| 22 | + * |
|
| 23 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 24 | + */ |
|
| 25 | + public function __construct( $submission ) { |
|
| 26 | + $this->payment_form_submission = $submission; |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + /** |
|
| 30 | + * Processes the checkout. |
|
| 31 | + * |
|
| 32 | + */ |
|
| 33 | + public function process_checkout() { |
|
| 34 | + |
|
| 35 | + // Validate the submission. |
|
| 36 | + $this->validate_submission(); |
|
| 37 | + |
|
| 38 | + // Prepare the invoice. |
|
| 39 | + $items = $this->get_submission_items(); |
|
| 40 | + $invoice = $this->get_submission_invoice(); |
|
| 41 | + $invoice = $this->process_submission_invoice( $invoice, $items ); |
|
| 42 | + $prepared = $this->prepare_submission_data_for_saving(); |
|
| 43 | + |
|
| 44 | + $this->prepare_billing_info( $invoice ); |
|
| 45 | + |
|
| 46 | + $shipping = $this->prepare_shipping_info( $invoice ); |
|
| 47 | + |
|
| 48 | + // Save the invoice. |
|
| 49 | + $invoice->set_is_viewed( true ); |
|
| 50 | + $invoice->recalculate_total(); |
|
| 51 | 51 | $invoice->save(); |
| 52 | 52 | |
| 53 | - do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
| 53 | + do_action( 'getpaid_checkout_invoice_updated', $invoice ); |
|
| 54 | 54 | |
| 55 | - // Send to the gateway. |
|
| 56 | - $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
| 57 | - } |
|
| 55 | + // Send to the gateway. |
|
| 56 | + $this->post_process_submission( $invoice, $prepared, $shipping ); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * Validates the submission. |
|
| 61 | - * |
|
| 62 | - */ |
|
| 63 | - protected function validate_submission() { |
|
| 59 | + /** |
|
| 60 | + * Validates the submission. |
|
| 61 | + * |
|
| 62 | + */ |
|
| 63 | + protected function validate_submission() { |
|
| 64 | 64 | |
| 65 | - $submission = $this->payment_form_submission; |
|
| 66 | - $data = $submission->get_data(); |
|
| 65 | + $submission = $this->payment_form_submission; |
|
| 66 | + $data = $submission->get_data(); |
|
| 67 | 67 | |
| 68 | - // Do we have an error? |
|
| 68 | + // Do we have an error? |
|
| 69 | 69 | if ( ! empty( $submission->last_error ) ) { |
| 70 | - wp_send_json_error( $submission->last_error ); |
|
| 70 | + wp_send_json_error( $submission->last_error ); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | - // We need a billing email. |
|
| 73 | + // We need a billing email. |
|
| 74 | 74 | if ( ! $submission->has_billing_email() ) { |
| 75 | 75 | wp_send_json_error( __( 'Provide a valid billing email.', 'invoicing' ) ); |
| 76 | - } |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - // Non-recurring gateways should not be allowed to process recurring invoices. |
|
| 79 | - if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
| 80 | - wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
| 81 | - } |
|
| 78 | + // Non-recurring gateways should not be allowed to process recurring invoices. |
|
| 79 | + if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) { |
|
| 80 | + wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) ); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - // Ensure the gateway is active. |
|
| 84 | - if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
| 85 | - wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
| 86 | - } |
|
| 83 | + // Ensure the gateway is active. |
|
| 84 | + if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) { |
|
| 85 | + wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) ); |
|
| 86 | + } |
|
| 87 | 87 | |
| 88 | - // Clear any existing errors. |
|
| 89 | - wpinv_clear_errors(); |
|
| 88 | + // Clear any existing errors. |
|
| 89 | + wpinv_clear_errors(); |
|
| 90 | 90 | |
| 91 | - // Allow themes and plugins to hook to errors |
|
| 92 | - do_action( 'getpaid_checkout_error_checks', $submission ); |
|
| 91 | + // Allow themes and plugins to hook to errors |
|
| 92 | + do_action( 'getpaid_checkout_error_checks', $submission ); |
|
| 93 | 93 | |
| 94 | - // Do we have any errors? |
|
| 94 | + // Do we have any errors? |
|
| 95 | 95 | if ( wpinv_get_errors() ) { |
| 96 | 96 | wp_send_json_error( getpaid_get_errors_html() ); |
| 97 | - } |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - } |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * Retrieves submission items. |
|
| 103 | - * |
|
| 104 | - * @return GetPaid_Form_Item[] |
|
| 105 | - */ |
|
| 106 | - protected function get_submission_items() { |
|
| 101 | + /** |
|
| 102 | + * Retrieves submission items. |
|
| 103 | + * |
|
| 104 | + * @return GetPaid_Form_Item[] |
|
| 105 | + */ |
|
| 106 | + protected function get_submission_items() { |
|
| 107 | 107 | |
| 108 | - $items = $this->payment_form_submission->get_items(); |
|
| 108 | + $items = $this->payment_form_submission->get_items(); |
|
| 109 | 109 | |
| 110 | 110 | // Ensure that we have items. |
| 111 | 111 | if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) { |
| 112 | 112 | wp_send_json_error( __( 'Please provide at least one item or amount.', 'invoicing' ) ); |
| 113 | - } |
|
| 114 | - |
|
| 115 | - return $items; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Retrieves submission invoice. |
|
| 120 | - * |
|
| 121 | - * @return WPInv_Invoice |
|
| 122 | - */ |
|
| 123 | - protected function get_submission_invoice() { |
|
| 124 | - $submission = $this->payment_form_submission; |
|
| 125 | - |
|
| 126 | - if ( ! $submission->has_invoice() ) { |
|
| 127 | - $invoice = new WPInv_Invoice(); |
|
| 128 | - $invoice->set_created_via( 'payment_form' ); |
|
| 129 | - return $invoice; |
|
| 130 | 113 | } |
| 131 | 114 | |
| 132 | - $invoice = $submission->get_invoice(); |
|
| 115 | + return $items; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Retrieves submission invoice. |
|
| 120 | + * |
|
| 121 | + * @return WPInv_Invoice |
|
| 122 | + */ |
|
| 123 | + protected function get_submission_invoice() { |
|
| 124 | + $submission = $this->payment_form_submission; |
|
| 125 | + |
|
| 126 | + if ( ! $submission->has_invoice() ) { |
|
| 127 | + $invoice = new WPInv_Invoice(); |
|
| 128 | + $invoice->set_created_via( 'payment_form' ); |
|
| 129 | + return $invoice; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + $invoice = $submission->get_invoice(); |
|
| 133 | 133 | |
| 134 | - // Make sure that it is neither paid or refunded. |
|
| 135 | - if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
| 136 | - wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
| 137 | - } |
|
| 134 | + // Make sure that it is neither paid or refunded. |
|
| 135 | + if ( $invoice->is_paid() || $invoice->is_refunded() ) { |
|
| 136 | + wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) ); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | - return $invoice; |
|
| 140 | - } |
|
| 139 | + return $invoice; |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - /** |
|
| 143 | - * Processes the submission invoice. |
|
| 144 | - * |
|
| 145 | - * @param WPInv_Invoice $invoice |
|
| 146 | - * @param GetPaid_Form_Item[] $items |
|
| 147 | - * @return WPInv_Invoice |
|
| 148 | - */ |
|
| 149 | - protected function process_submission_invoice( $invoice, $items ) { |
|
| 142 | + /** |
|
| 143 | + * Processes the submission invoice. |
|
| 144 | + * |
|
| 145 | + * @param WPInv_Invoice $invoice |
|
| 146 | + * @param GetPaid_Form_Item[] $items |
|
| 147 | + * @return WPInv_Invoice |
|
| 148 | + */ |
|
| 149 | + protected function process_submission_invoice( $invoice, $items ) { |
|
| 150 | 150 | |
| 151 | - $submission = $this->payment_form_submission; |
|
| 151 | + $submission = $this->payment_form_submission; |
|
| 152 | 152 | |
| 153 | - // Set-up the invoice details. |
|
| 154 | - $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
| 155 | - $invoice->set_user_id( $this->get_submission_customer() ); |
|
| 156 | - $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
| 153 | + // Set-up the invoice details. |
|
| 154 | + $invoice->set_email( sanitize_email( $submission->get_billing_email() ) ); |
|
| 155 | + $invoice->set_user_id( $this->get_submission_customer() ); |
|
| 156 | + $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) ); |
|
| 157 | 157 | $invoice->set_items( $items ); |
| 158 | 158 | $invoice->set_fees( $submission->get_fees() ); |
| 159 | 159 | $invoice->set_taxes( $submission->get_taxes() ); |
| 160 | - $invoice->set_discounts( $submission->get_discounts() ); |
|
| 161 | - $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
| 162 | - $invoice->set_currency( $submission->get_currency() ); |
|
| 160 | + $invoice->set_discounts( $submission->get_discounts() ); |
|
| 161 | + $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) ); |
|
| 162 | + $invoice->set_currency( $submission->get_currency() ); |
|
| 163 | 163 | |
| 164 | - if ( $submission->has_shipping() ) { |
|
| 165 | - $invoice->set_shipping( $submission->get_shipping() ); |
|
| 166 | - } |
|
| 164 | + if ( $submission->has_shipping() ) { |
|
| 165 | + $invoice->set_shipping( $submission->get_shipping() ); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
| 169 | - $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
| 168 | + $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
| 169 | + $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
| 170 | 170 | |
| 171 | - if ( $submission->has_discount_code() ) { |
|
| 171 | + if ( $submission->has_discount_code() ) { |
|
| 172 | 172 | $invoice->set_discount_code( $submission->get_discount_code() ); |
| 173 | - } |
|
| 174 | - |
|
| 175 | - getpaid_maybe_add_default_address( $invoice ); |
|
| 176 | - return $invoice; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Retrieves the submission's customer. |
|
| 181 | - * |
|
| 182 | - * @return int The customer id. |
|
| 183 | - */ |
|
| 184 | - protected function get_submission_customer() { |
|
| 185 | - $submission = $this->payment_form_submission; |
|
| 186 | - |
|
| 187 | - // If this is an existing invoice... |
|
| 188 | - if ( $submission->has_invoice() ) { |
|
| 189 | - return $submission->get_invoice()->get_user_id(); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - // (Maybe) create the user. |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + getpaid_maybe_add_default_address( $invoice ); |
|
| 176 | + return $invoice; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Retrieves the submission's customer. |
|
| 181 | + * |
|
| 182 | + * @return int The customer id. |
|
| 183 | + */ |
|
| 184 | + protected function get_submission_customer() { |
|
| 185 | + $submission = $this->payment_form_submission; |
|
| 186 | + |
|
| 187 | + // If this is an existing invoice... |
|
| 188 | + if ( $submission->has_invoice() ) { |
|
| 189 | + return $submission->get_invoice()->get_user_id(); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + // (Maybe) create the user. |
|
| 193 | 193 | $user = get_current_user_id(); |
| 194 | 194 | |
| 195 | 195 | if ( empty( $user ) ) { |
@@ -199,11 +199,11 @@ discard block |
||
| 199 | 199 | if ( empty( $user ) ) { |
| 200 | 200 | $user = wpinv_create_user( $submission->get_billing_email() ); |
| 201 | 201 | |
| 202 | - // (Maybe) send new user notification. |
|
| 203 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
| 204 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
| 205 | - wp_send_new_user_notifications( $user, 'user' ); |
|
| 206 | - } |
|
| 202 | + // (Maybe) send new user notification. |
|
| 203 | + $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
| 204 | + if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
| 205 | + wp_send_new_user_notifications( $user, 'user' ); |
|
| 206 | + } |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | if ( is_wp_error( $user ) ) { |
@@ -212,47 +212,47 @@ discard block |
||
| 212 | 212 | |
| 213 | 213 | if ( is_numeric( $user ) ) { |
| 214 | 214 | return $user; |
| 215 | - } |
|
| 215 | + } |
|
| 216 | 216 | |
| 217 | - return $user->ID; |
|
| 217 | + return $user->ID; |
|
| 218 | 218 | |
| 219 | - } |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - /** |
|
| 221 | + /** |
|
| 222 | 222 | * Prepares submission data for saving to the database. |
| 223 | 223 | * |
| 224 | - * @return array |
|
| 224 | + * @return array |
|
| 225 | 225 | */ |
| 226 | 226 | public function prepare_submission_data_for_saving() { |
| 227 | 227 | |
| 228 | - $submission = $this->payment_form_submission; |
|
| 228 | + $submission = $this->payment_form_submission; |
|
| 229 | 229 | |
| 230 | - // Prepared submission details. |
|
| 230 | + // Prepared submission details. |
|
| 231 | 231 | $prepared = array( |
| 232 | - 'all' => array(), |
|
| 233 | - 'meta' => array(), |
|
| 234 | - ); |
|
| 232 | + 'all' => array(), |
|
| 233 | + 'meta' => array(), |
|
| 234 | + ); |
|
| 235 | 235 | |
| 236 | 236 | // Raw submission details. |
| 237 | - $data = $submission->get_data(); |
|
| 237 | + $data = $submission->get_data(); |
|
| 238 | 238 | |
| 239 | - // Loop through the submitted details. |
|
| 239 | + // Loop through the submitted details. |
|
| 240 | 240 | foreach ( $submission->get_payment_form()->get_elements() as $field ) { |
| 241 | 241 | |
| 242 | - // Skip premade fields. |
|
| 242 | + // Skip premade fields. |
|
| 243 | 243 | if ( ! empty( $field['premade'] ) ) { |
| 244 | 244 | continue; |
| 245 | 245 | } |
| 246 | 246 | |
| 247 | - // Ensure address is provided. |
|
| 248 | - if ( $field['type'] == 'address' ) { |
|
| 247 | + // Ensure address is provided. |
|
| 248 | + if ( $field['type'] == 'address' ) { |
|
| 249 | 249 | $address_type = isset( $field['address_type'] ) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
| 250 | 250 | |
| 251 | - foreach ( $field['fields'] as $address_field ) { |
|
| 251 | + foreach ( $field['fields'] as $address_field ) { |
|
| 252 | 252 | |
| 253 | - if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { |
|
| 254 | - wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
| 255 | - } |
|
| 253 | + if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { |
|
| 254 | + wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
| 255 | + } |
|
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | |
@@ -264,31 +264,31 @@ discard block |
||
| 264 | 264 | // Handle misc fields. |
| 265 | 265 | if ( isset( $data[ $field['id'] ] ) ) { |
| 266 | 266 | |
| 267 | - // Uploads. |
|
| 268 | - if ( $field['type'] == 'file_upload' ) { |
|
| 269 | - $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
| 267 | + // Uploads. |
|
| 268 | + if ( $field['type'] == 'file_upload' ) { |
|
| 269 | + $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
| 270 | 270 | |
| 271 | - if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
| 272 | - wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
| 273 | - } |
|
| 271 | + if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
| 272 | + wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | - $value = array(); |
|
| 275 | + $value = array(); |
|
| 276 | 276 | |
| 277 | - foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
| 278 | - $value[] = sprintf( |
|
| 279 | - '<a href="%s" target="_blank">%s</a>', |
|
| 280 | - esc_url_raw( $url ), |
|
| 281 | - esc_html( $name ) |
|
| 282 | - ); |
|
| 283 | - } |
|
| 277 | + foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
| 278 | + $value[] = sprintf( |
|
| 279 | + '<a href="%s" target="_blank">%s</a>', |
|
| 280 | + esc_url_raw( $url ), |
|
| 281 | + esc_html( $name ) |
|
| 282 | + ); |
|
| 283 | + } |
|
| 284 | 284 | |
| 285 | - $value = implode( ' | ', $value ); |
|
| 285 | + $value = implode( ' | ', $value ); |
|
| 286 | 286 | |
| 287 | - } elseif ( $field['type'] == 'checkbox' ) { |
|
| 288 | - $value = isset( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
| 289 | - } else { |
|
| 290 | - $value = wp_kses_post( $data[ $field['id'] ] ); |
|
| 291 | - } |
|
| 287 | + } elseif ( $field['type'] == 'checkbox' ) { |
|
| 288 | + $value = isset( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
| 289 | + } else { |
|
| 290 | + $value = wp_kses_post( $data[ $field['id'] ] ); |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | 293 | $label = $field['id']; |
| 294 | 294 | |
@@ -296,188 +296,188 @@ discard block |
||
| 296 | 296 | $label = $field['label']; |
| 297 | 297 | } |
| 298 | 298 | |
| 299 | - if ( ! empty( $field['add_meta'] ) ) { |
|
| 300 | - $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
| 301 | - } |
|
| 302 | - $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
| 299 | + if ( ! empty( $field['add_meta'] ) ) { |
|
| 300 | + $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
| 301 | + } |
|
| 302 | + $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
| 303 | 303 | |
| 304 | 304 | } |
| 305 | 305 | } |
| 306 | 306 | |
| 307 | - return $prepared; |
|
| 307 | + return $prepared; |
|
| 308 | 308 | |
| 309 | - } |
|
| 309 | + } |
|
| 310 | 310 | |
| 311 | - /** |
|
| 311 | + /** |
|
| 312 | 312 | * Retrieves address details. |
| 313 | 313 | * |
| 314 | - * @return array |
|
| 315 | - * @param WPInv_Invoice $invoice |
|
| 316 | - * @param string $type |
|
| 314 | + * @return array |
|
| 315 | + * @param WPInv_Invoice $invoice |
|
| 316 | + * @param string $type |
|
| 317 | 317 | */ |
| 318 | 318 | public function prepare_address_details( $invoice, $type = 'billing' ) { |
| 319 | 319 | |
| 320 | - $data = $this->payment_form_submission->get_data(); |
|
| 321 | - $type = sanitize_key( $type ); |
|
| 322 | - $address = array(); |
|
| 323 | - $prepared = array(); |
|
| 320 | + $data = $this->payment_form_submission->get_data(); |
|
| 321 | + $type = sanitize_key( $type ); |
|
| 322 | + $address = array(); |
|
| 323 | + $prepared = array(); |
|
| 324 | 324 | |
| 325 | - if ( ! empty( $data[ $type ] ) ) { |
|
| 326 | - $address = $data[ $type ]; |
|
| 327 | - } |
|
| 325 | + if ( ! empty( $data[ $type ] ) ) { |
|
| 326 | + $address = $data[ $type ]; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - // Clean address details. |
|
| 330 | - foreach ( $address as $key => $value ) { |
|
| 331 | - $key = sanitize_key( $key ); |
|
| 332 | - $key = str_replace( 'wpinv_', '', $key ); |
|
| 333 | - $value = wpinv_clean( $value ); |
|
| 334 | - $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
| 335 | - } |
|
| 329 | + // Clean address details. |
|
| 330 | + foreach ( $address as $key => $value ) { |
|
| 331 | + $key = sanitize_key( $key ); |
|
| 332 | + $key = str_replace( 'wpinv_', '', $key ); |
|
| 333 | + $value = wpinv_clean( $value ); |
|
| 334 | + $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
| 335 | + } |
|
| 336 | 336 | |
| 337 | - // Filter address details. |
|
| 338 | - $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
| 337 | + // Filter address details. |
|
| 338 | + $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
| 339 | 339 | |
| 340 | - // Remove non-whitelisted values. |
|
| 341 | - return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
| 340 | + // Remove non-whitelisted values. |
|
| 341 | + return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
| 342 | 342 | |
| 343 | - } |
|
| 343 | + } |
|
| 344 | 344 | |
| 345 | - /** |
|
| 345 | + /** |
|
| 346 | 346 | * Prepares the billing details. |
| 347 | 347 | * |
| 348 | - * @return array |
|
| 349 | - * @param WPInv_Invoice $invoice |
|
| 348 | + * @return array |
|
| 349 | + * @param WPInv_Invoice $invoice |
|
| 350 | 350 | */ |
| 351 | 351 | protected function prepare_billing_info( &$invoice ) { |
| 352 | 352 | |
| 353 | - $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
| 353 | + $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
| 354 | 354 | |
| 355 | - // Update the invoice with the billing details. |
|
| 356 | - $invoice->set_props( $billing_address ); |
|
| 355 | + // Update the invoice with the billing details. |
|
| 356 | + $invoice->set_props( $billing_address ); |
|
| 357 | 357 | |
| 358 | - } |
|
| 358 | + } |
|
| 359 | 359 | |
| 360 | - /** |
|
| 360 | + /** |
|
| 361 | 361 | * Prepares the shipping details. |
| 362 | 362 | * |
| 363 | - * @return array |
|
| 364 | - * @param WPInv_Invoice $invoice |
|
| 363 | + * @return array |
|
| 364 | + * @param WPInv_Invoice $invoice |
|
| 365 | 365 | */ |
| 366 | 366 | protected function prepare_shipping_info( $invoice ) { |
| 367 | 367 | |
| 368 | - $data = $this->payment_form_submission->get_data(); |
|
| 368 | + $data = $this->payment_form_submission->get_data(); |
|
| 369 | 369 | |
| 370 | - if ( empty( $data['same-shipping-address'] ) ) { |
|
| 371 | - return $this->prepare_address_details( $invoice, 'shipping' ); |
|
| 372 | - } |
|
| 370 | + if ( empty( $data['same-shipping-address'] ) ) { |
|
| 371 | + return $this->prepare_address_details( $invoice, 'shipping' ); |
|
| 372 | + } |
|
| 373 | 373 | |
| 374 | - return $this->prepare_address_details( $invoice, 'billing' ); |
|
| 374 | + return $this->prepare_address_details( $invoice, 'billing' ); |
|
| 375 | 375 | |
| 376 | - } |
|
| 376 | + } |
|
| 377 | 377 | |
| 378 | - /** |
|
| 379 | - * Confirms the submission is valid and send users to the gateway. |
|
| 380 | - * |
|
| 381 | - * @param WPInv_Invoice $invoice |
|
| 382 | - * @param array $prepared_payment_form_data |
|
| 383 | - * @param array $shipping |
|
| 384 | - */ |
|
| 385 | - protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
| 378 | + /** |
|
| 379 | + * Confirms the submission is valid and send users to the gateway. |
|
| 380 | + * |
|
| 381 | + * @param WPInv_Invoice $invoice |
|
| 382 | + * @param array $prepared_payment_form_data |
|
| 383 | + * @param array $shipping |
|
| 384 | + */ |
|
| 385 | + protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
| 386 | 386 | |
| 387 | - // Ensure the invoice exists. |
|
| 387 | + // Ensure the invoice exists. |
|
| 388 | 388 | if ( ! $invoice->exists() ) { |
| 389 | 389 | wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
| 390 | 390 | } |
| 391 | 391 | |
| 392 | - // Save payment form data. |
|
| 393 | - $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
| 392 | + // Save payment form data. |
|
| 393 | + $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
| 394 | 394 | delete_post_meta( $invoice->get_id(), 'payment_form_data' ); |
| 395 | - delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
| 396 | - if ( ! empty( $prepared_payment_form_data ) ) { |
|
| 395 | + delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
| 396 | + if ( ! empty( $prepared_payment_form_data ) ) { |
|
| 397 | 397 | |
| 398 | - if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
| 399 | - update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
| 400 | - } |
|
| 398 | + if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
| 399 | + update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
| 400 | + } |
|
| 401 | 401 | |
| 402 | - if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
| 403 | - update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
| 404 | - } |
|
| 402 | + if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
| 403 | + update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
| 404 | + } |
|
| 405 | 405 | } |
| 406 | 406 | |
| 407 | - // Save payment form data. |
|
| 408 | - $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
| 407 | + // Save payment form data. |
|
| 408 | + $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
| 409 | 409 | if ( ! empty( $shipping ) ) { |
| 410 | 410 | update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
| 411 | - } |
|
| 411 | + } |
|
| 412 | 412 | |
| 413 | - // Backwards compatibility. |
|
| 413 | + // Backwards compatibility. |
|
| 414 | 414 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
| 415 | 415 | |
| 416 | - $this->process_payment( $invoice ); |
|
| 416 | + $this->process_payment( $invoice ); |
|
| 417 | 417 | |
| 418 | 418 | // If we are here, there was an error. |
| 419 | - wpinv_send_back_to_checkout( $invoice ); |
|
| 419 | + wpinv_send_back_to_checkout( $invoice ); |
|
| 420 | 420 | |
| 421 | - } |
|
| 421 | + } |
|
| 422 | 422 | |
| 423 | - /** |
|
| 424 | - * Processes the actual payment. |
|
| 425 | - * |
|
| 426 | - * @param WPInv_Invoice $invoice |
|
| 427 | - */ |
|
| 428 | - protected function process_payment( $invoice ) { |
|
| 423 | + /** |
|
| 424 | + * Processes the actual payment. |
|
| 425 | + * |
|
| 426 | + * @param WPInv_Invoice $invoice |
|
| 427 | + */ |
|
| 428 | + protected function process_payment( $invoice ) { |
|
| 429 | 429 | |
| 430 | - // Clear any checkout errors. |
|
| 431 | - wpinv_clear_errors(); |
|
| 430 | + // Clear any checkout errors. |
|
| 431 | + wpinv_clear_errors(); |
|
| 432 | 432 | |
| 433 | - // No need to send free invoices to the gateway. |
|
| 434 | - if ( $invoice->is_free() ) { |
|
| 435 | - $this->process_free_payment( $invoice ); |
|
| 436 | - } |
|
| 433 | + // No need to send free invoices to the gateway. |
|
| 434 | + if ( $invoice->is_free() ) { |
|
| 435 | + $this->process_free_payment( $invoice ); |
|
| 436 | + } |
|
| 437 | 437 | |
| 438 | - $submission = $this->payment_form_submission; |
|
| 438 | + $submission = $this->payment_form_submission; |
|
| 439 | 439 | |
| 440 | - // Fires before sending to the gateway. |
|
| 441 | - do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
| 440 | + // Fires before sending to the gateway. |
|
| 441 | + do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
| 442 | 442 | |
| 443 | - // Allow the sumission data to be modified before it is sent to the gateway. |
|
| 444 | - $submission_data = $submission->get_data(); |
|
| 445 | - $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
| 446 | - $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
| 443 | + // Allow the sumission data to be modified before it is sent to the gateway. |
|
| 444 | + $submission_data = $submission->get_data(); |
|
| 445 | + $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
| 446 | + $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
| 447 | 447 | |
| 448 | - // Validate the currency. |
|
| 449 | - if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
| 450 | - wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support this currency', 'invoicing' ) ); |
|
| 451 | - } |
|
| 448 | + // Validate the currency. |
|
| 449 | + if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
| 450 | + wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support this currency', 'invoicing' ) ); |
|
| 451 | + } |
|
| 452 | 452 | |
| 453 | - // Check to see if we have any errors. |
|
| 454 | - if ( wpinv_get_errors() ) { |
|
| 455 | - wpinv_send_back_to_checkout( $invoice ); |
|
| 456 | - } |
|
| 453 | + // Check to see if we have any errors. |
|
| 454 | + if ( wpinv_get_errors() ) { |
|
| 455 | + wpinv_send_back_to_checkout( $invoice ); |
|
| 456 | + } |
|
| 457 | 457 | |
| 458 | - // Send info to the gateway for payment processing |
|
| 459 | - do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
| 458 | + // Send info to the gateway for payment processing |
|
| 459 | + do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
| 460 | 460 | |
| 461 | - // Backwards compatibility. |
|
| 462 | - wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
| 461 | + // Backwards compatibility. |
|
| 462 | + wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
| 463 | 463 | |
| 464 | - } |
|
| 464 | + } |
|
| 465 | 465 | |
| 466 | - /** |
|
| 467 | - * Marks the invoice as paid in case the checkout is free. |
|
| 468 | - * |
|
| 469 | - * @param WPInv_Invoice $invoice |
|
| 470 | - */ |
|
| 471 | - protected function process_free_payment( $invoice ) { |
|
| 466 | + /** |
|
| 467 | + * Marks the invoice as paid in case the checkout is free. |
|
| 468 | + * |
|
| 469 | + * @param WPInv_Invoice $invoice |
|
| 470 | + */ |
|
| 471 | + protected function process_free_payment( $invoice ) { |
|
| 472 | 472 | |
| 473 | - $invoice->set_gateway( 'none' ); |
|
| 474 | - $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
| 475 | - $invoice->mark_paid(); |
|
| 476 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
| 473 | + $invoice->set_gateway( 'none' ); |
|
| 474 | + $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
| 475 | + $invoice->mark_paid(); |
|
| 476 | + wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
| 477 | 477 | |
| 478 | - } |
|
| 478 | + } |
|
| 479 | 479 | |
| 480 | - /** |
|
| 480 | + /** |
|
| 481 | 481 | * Sends a redrect response to payment details. |
| 482 | 482 | * |
| 483 | 483 | */ |
@@ -12,38 +12,38 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Payment_Form_Submission_Items { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Submission items. |
|
| 17 | - * @var GetPaid_Form_Item[] |
|
| 18 | - */ |
|
| 19 | - public $items = array(); |
|
| 15 | + /** |
|
| 16 | + * Submission items. |
|
| 17 | + * @var GetPaid_Form_Item[] |
|
| 18 | + */ |
|
| 19 | + public $items = array(); |
|
| 20 | 20 | |
| 21 | 21 | /** |
| 22 | - * Class constructor |
|
| 23 | - * |
|
| 24 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 25 | - */ |
|
| 26 | - public function __construct( $submission ) { |
|
| 27 | - |
|
| 28 | - $data = $submission->get_data(); |
|
| 29 | - $payment_form = $submission->get_payment_form(); |
|
| 30 | - |
|
| 31 | - // Prepare the selected items. |
|
| 32 | - $selected_items = array(); |
|
| 33 | - if ( ! empty( $data['getpaid-items'] ) ) { |
|
| 34 | - $selected_items = wpinv_clean( $data['getpaid-items'] ); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - // (Maybe) set form items. |
|
| 38 | - if ( isset( $data['getpaid-form-items'] ) ) { |
|
| 39 | - |
|
| 40 | - // Confirm items key. |
|
| 41 | - $form_items = wpinv_clean( $data['getpaid-form-items'] ); |
|
| 42 | - if ( ! isset( $data['getpaid-form-items-key'] ) || $data['getpaid-form-items-key'] !== md5( NONCE_KEY . AUTH_KEY . $form_items ) ) { |
|
| 43 | - throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) ); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - $items = array(); |
|
| 22 | + * Class constructor |
|
| 23 | + * |
|
| 24 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 25 | + */ |
|
| 26 | + public function __construct( $submission ) { |
|
| 27 | + |
|
| 28 | + $data = $submission->get_data(); |
|
| 29 | + $payment_form = $submission->get_payment_form(); |
|
| 30 | + |
|
| 31 | + // Prepare the selected items. |
|
| 32 | + $selected_items = array(); |
|
| 33 | + if ( ! empty( $data['getpaid-items'] ) ) { |
|
| 34 | + $selected_items = wpinv_clean( $data['getpaid-items'] ); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + // (Maybe) set form items. |
|
| 38 | + if ( isset( $data['getpaid-form-items'] ) ) { |
|
| 39 | + |
|
| 40 | + // Confirm items key. |
|
| 41 | + $form_items = wpinv_clean( $data['getpaid-form-items'] ); |
|
| 42 | + if ( ! isset( $data['getpaid-form-items-key'] ) || $data['getpaid-form-items-key'] !== md5( NONCE_KEY . AUTH_KEY . $form_items ) ) { |
|
| 43 | + throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) ); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + $items = array(); |
|
| 47 | 47 | $item_ids = array(); |
| 48 | 48 | |
| 49 | 49 | foreach ( getpaid_convert_items_to_array( $form_items ) as $item_id => $qty ) { |
@@ -73,57 +73,57 @@ discard block |
||
| 73 | 73 | |
| 74 | 74 | $payment_form->set_items( $items ); |
| 75 | 75 | |
| 76 | - } |
|
| 77 | - |
|
| 78 | - // Process each individual item. |
|
| 79 | - foreach ( $payment_form->get_items() as $item ) { |
|
| 80 | - $this->process_item( $item, $selected_items, $submission ); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - } |
|
| 76 | + } |
|
| 84 | 77 | |
| 85 | - /** |
|
| 86 | - * Process a single item. |
|
| 87 | - * |
|
| 88 | - * @param GetPaid_Form_Item $item |
|
| 89 | - * @param array $selected_items |
|
| 90 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 91 | - */ |
|
| 92 | - public function process_item( $item, $selected_items, $submission ) { |
|
| 78 | + // Process each individual item. |
|
| 79 | + foreach ( $payment_form->get_items() as $item ) { |
|
| 80 | + $this->process_item( $item, $selected_items, $submission ); |
|
| 81 | + } |
|
| 93 | 82 | |
| 94 | - // Abort if this is an optional item and it has not been selected. |
|
| 95 | - if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
|
| 96 | - return; |
|
| 97 | - } |
|
| 83 | + } |
|
| 98 | 84 | |
| 99 | - // (maybe) let customers change the quantities and prices. |
|
| 100 | - if ( isset( $selected_items[ $item->get_id() ] ) ) { |
|
| 101 | - |
|
| 102 | - // Maybe change the quantities. |
|
| 103 | - if ( $item->allows_quantities() ) { |
|
| 104 | - $item->set_quantity( (float) $selected_items[ $item->get_id() ]['quantity'] ); |
|
| 105 | - } |
|
| 85 | + /** |
|
| 86 | + * Process a single item. |
|
| 87 | + * |
|
| 88 | + * @param GetPaid_Form_Item $item |
|
| 89 | + * @param array $selected_items |
|
| 90 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 91 | + */ |
|
| 92 | + public function process_item( $item, $selected_items, $submission ) { |
|
| 93 | + |
|
| 94 | + // Abort if this is an optional item and it has not been selected. |
|
| 95 | + if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
|
| 96 | + return; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + // (maybe) let customers change the quantities and prices. |
|
| 100 | + if ( isset( $selected_items[ $item->get_id() ] ) ) { |
|
| 101 | + |
|
| 102 | + // Maybe change the quantities. |
|
| 103 | + if ( $item->allows_quantities() ) { |
|
| 104 | + $item->set_quantity( (float) $selected_items[ $item->get_id() ]['quantity'] ); |
|
| 105 | + } |
|
| 106 | 106 | |
| 107 | - // Maybe change the price. |
|
| 108 | - if ( $item->user_can_set_their_price() ) { |
|
| 109 | - $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
|
| 107 | + // Maybe change the price. |
|
| 108 | + if ( $item->user_can_set_their_price() ) { |
|
| 109 | + $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
|
| 110 | 110 | |
| 111 | - if ( $item->get_minimum_price() > $price ) { |
|
| 112 | - throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $item->get_minimum_price() ) ) ); |
|
| 113 | - } |
|
| 111 | + if ( $item->get_minimum_price() > $price ) { |
|
| 112 | + throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $item->get_minimum_price() ) ) ); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - $item->set_price( $price ); |
|
| 115 | + $item->set_price( $price ); |
|
| 116 | 116 | |
| 117 | - } |
|
| 117 | + } |
|
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - if ( 0 == $item->get_quantity() ) { |
|
| 121 | - return; |
|
| 122 | - } |
|
| 120 | + if ( 0 == $item->get_quantity() ) { |
|
| 121 | + return; |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - // Save the item. |
|
| 125 | - $this->items[] = apply_filters( 'getpaid_payment_form_submission_processed_item', $item, $submission ); |
|
| 124 | + // Save the item. |
|
| 125 | + $this->items[] = apply_filters( 'getpaid_payment_form_submission_processed_item', $item, $submission ); |
|
| 126 | 126 | |
| 127 | - } |
|
| 127 | + } |
|
| 128 | 128 | |
| 129 | 129 | } |
@@ -12,227 +12,227 @@ |
||
| 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 | + $item->item_tax += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
| 71 | + |
|
| 72 | + if ( ! isset( $this->taxes[ $name ] ) ) { |
|
| 73 | + $this->taxes[ $name ] = $tax; |
|
| 74 | + continue; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
|
| 78 | + $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
|
| 79 | + |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + } |
|
| 26 | 83 | |
| 27 | 84 | /** |
| 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 | - $item->item_tax += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
| 71 | - |
|
| 72 | - if ( ! isset( $this->taxes[ $name ] ) ) { |
|
| 73 | - $this->taxes[ $name ] = $tax; |
|
| 74 | - continue; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - $this->taxes[ $name ]['initial_tax'] += $tax['initial_tax']; |
|
| 78 | - $this->taxes[ $name ]['recurring_tax'] += $tax['recurring_tax']; |
|
| 79 | - |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * Checks if the submission has a digital item. |
|
| 86 | - * |
|
| 87 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 88 | - * @since 1.0.19 |
|
| 89 | - * @return bool |
|
| 90 | - */ |
|
| 91 | - public function has_digital_item( $submission ) { |
|
| 92 | - |
|
| 93 | - foreach ( $submission->get_items() as $item ) { |
|
| 94 | - |
|
| 95 | - if ( 'digital' == $item->get_vat_rule() ) { |
|
| 96 | - return true; |
|
| 97 | - } |
|
| 85 | + * Checks if the submission has a digital item. |
|
| 86 | + * |
|
| 87 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 88 | + * @since 1.0.19 |
|
| 89 | + * @return bool |
|
| 90 | + */ |
|
| 91 | + public function has_digital_item( $submission ) { |
|
| 92 | + |
|
| 93 | + foreach ( $submission->get_items() as $item ) { |
|
| 94 | + |
|
| 95 | + if ( 'digital' == $item->get_vat_rule() ) { |
|
| 96 | + return true; |
|
| 97 | + } |
|
| 98 | 98 | } |
| 99 | 99 | |
| 100 | - return false; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Checks if this is an eu store. |
|
| 105 | - * |
|
| 106 | - * @since 1.0.19 |
|
| 107 | - * @return bool |
|
| 108 | - */ |
|
| 109 | - public static function is_eu_store() { |
|
| 110 | - return self::is_eu_country( wpinv_get_default_country() ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Checks if this is an eu country. |
|
| 115 | - * |
|
| 116 | - * @param string $country |
|
| 117 | - * @since 1.0.19 |
|
| 118 | - * @return bool |
|
| 119 | - */ |
|
| 120 | - public static function is_eu_country( $country ) { |
|
| 121 | - return getpaid_is_eu_state( $country ); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Checks if this is an eu purchase. |
|
| 126 | - * |
|
| 127 | - * @param string $customer_country |
|
| 128 | - * @since 1.0.19 |
|
| 129 | - * @return bool |
|
| 130 | - */ |
|
| 131 | - public static function is_eu_transaction( $customer_country ) { |
|
| 132 | - return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
|
| 133 | - } |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * Retrieves the vat number. |
|
| 137 | - * |
|
| 138 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 139 | - * @since 1.0.19 |
|
| 140 | - * @return string |
|
| 141 | - */ |
|
| 142 | - public function get_vat_number( $submission ) { |
|
| 143 | - |
|
| 144 | - // Retrieve from the posted number. |
|
| 145 | - $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
|
| 146 | - if ( ! is_null( $vat_number ) ) { |
|
| 147 | - return wpinv_clean( $vat_number ); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Retrieves the company. |
|
| 155 | - * |
|
| 156 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 157 | - * @since 1.0.19 |
|
| 158 | - * @return string |
|
| 159 | - */ |
|
| 160 | - public function get_company( $submission ) { |
|
| 161 | - |
|
| 162 | - // Retrieve from the posted data. |
|
| 163 | - $company = $submission->get_field( 'wpinv_company', 'billing' ); |
|
| 164 | - if ( ! empty( $company ) ) { |
|
| 165 | - return wpinv_clean( $company ); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - // Retrieve from the invoice. |
|
| 169 | - return $submission->has_invoice() ? $submission->get_invoice()->get_company() : ''; |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * Checks if we require a VAT number. |
|
| 174 | - * |
|
| 175 | - * @param bool $ip_in_eu Whether the customer IP is from the EU |
|
| 176 | - * @param bool $country_in_eu Whether the customer country is from the EU |
|
| 177 | - * @since 1.0.19 |
|
| 178 | - * @return string |
|
| 179 | - */ |
|
| 180 | - public function requires_vat( $ip_in_eu, $country_in_eu ) { |
|
| 181 | - |
|
| 182 | - $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' ); |
|
| 183 | - $prevent_b2c = ! empty( $prevent_b2c ); |
|
| 184 | - $is_eu = $ip_in_eu || $country_in_eu; |
|
| 185 | - |
|
| 186 | - return $prevent_b2c && $is_eu; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Validate VAT data. |
|
| 191 | - * |
|
| 192 | - * @param GetPaid_Payment_Form_Submission $submission |
|
| 193 | - * @since 1.0.19 |
|
| 194 | - */ |
|
| 195 | - public function validate_vat( $submission ) { |
|
| 196 | - |
|
| 197 | - $in_eu = $this->is_eu_transaction( $submission->country ); |
|
| 198 | - |
|
| 199 | - // Abort if we are not validating vat numbers. |
|
| 200 | - if ( ! $in_eu ) { |
|
| 100 | + return false; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Checks if this is an eu store. |
|
| 105 | + * |
|
| 106 | + * @since 1.0.19 |
|
| 107 | + * @return bool |
|
| 108 | + */ |
|
| 109 | + public static function is_eu_store() { |
|
| 110 | + return self::is_eu_country( wpinv_get_default_country() ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Checks if this is an eu country. |
|
| 115 | + * |
|
| 116 | + * @param string $country |
|
| 117 | + * @since 1.0.19 |
|
| 118 | + * @return bool |
|
| 119 | + */ |
|
| 120 | + public static function is_eu_country( $country ) { |
|
| 121 | + return getpaid_is_eu_state( $country ); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Checks if this is an eu purchase. |
|
| 126 | + * |
|
| 127 | + * @param string $customer_country |
|
| 128 | + * @since 1.0.19 |
|
| 129 | + * @return bool |
|
| 130 | + */ |
|
| 131 | + public static function is_eu_transaction( $customer_country ) { |
|
| 132 | + return self::is_eu_country( $customer_country ) && self::is_eu_store(); |
|
| 133 | + } |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * Retrieves the vat number. |
|
| 137 | + * |
|
| 138 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 139 | + * @since 1.0.19 |
|
| 140 | + * @return string |
|
| 141 | + */ |
|
| 142 | + public function get_vat_number( $submission ) { |
|
| 143 | + |
|
| 144 | + // Retrieve from the posted number. |
|
| 145 | + $vat_number = $submission->get_field( 'wpinv_vat_number', 'billing' ); |
|
| 146 | + if ( ! is_null( $vat_number ) ) { |
|
| 147 | + return wpinv_clean( $vat_number ); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return $submission->has_invoice() ? $submission->get_invoice()->get_vat_number() : ''; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Retrieves the company. |
|
| 155 | + * |
|
| 156 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 157 | + * @since 1.0.19 |
|
| 158 | + * @return string |
|
| 159 | + */ |
|
| 160 | + public function get_company( $submission ) { |
|
| 161 | + |
|
| 162 | + // Retrieve from the posted data. |
|
| 163 | + $company = $submission->get_field( 'wpinv_company', 'billing' ); |
|
| 164 | + if ( ! empty( $company ) ) { |
|
| 165 | + return wpinv_clean( $company ); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + // Retrieve from the invoice. |
|
| 169 | + return $submission->has_invoice() ? $submission->get_invoice()->get_company() : ''; |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * Checks if we require a VAT number. |
|
| 174 | + * |
|
| 175 | + * @param bool $ip_in_eu Whether the customer IP is from the EU |
|
| 176 | + * @param bool $country_in_eu Whether the customer country is from the EU |
|
| 177 | + * @since 1.0.19 |
|
| 178 | + * @return string |
|
| 179 | + */ |
|
| 180 | + public function requires_vat( $ip_in_eu, $country_in_eu ) { |
|
| 181 | + |
|
| 182 | + $prevent_b2c = wpinv_get_option( 'vat_prevent_b2c_purchase' ); |
|
| 183 | + $prevent_b2c = ! empty( $prevent_b2c ); |
|
| 184 | + $is_eu = $ip_in_eu || $country_in_eu; |
|
| 185 | + |
|
| 186 | + return $prevent_b2c && $is_eu; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Validate VAT data. |
|
| 191 | + * |
|
| 192 | + * @param GetPaid_Payment_Form_Submission $submission |
|
| 193 | + * @since 1.0.19 |
|
| 194 | + */ |
|
| 195 | + public function validate_vat( $submission ) { |
|
| 196 | + |
|
| 197 | + $in_eu = $this->is_eu_transaction( $submission->country ); |
|
| 198 | + |
|
| 199 | + // Abort if we are not validating vat numbers. |
|
| 200 | + if ( ! $in_eu ) { |
|
| 201 | 201 | return; |
| 202 | - } |
|
| 202 | + } |
|
| 203 | 203 | |
| 204 | - // Prepare variables. |
|
| 205 | - $vat_number = $this->get_vat_number( $submission ); |
|
| 206 | - $ip_country = getpaid_get_ip_country(); |
|
| 204 | + // Prepare variables. |
|
| 205 | + $vat_number = $this->get_vat_number( $submission ); |
|
| 206 | + $ip_country = getpaid_get_ip_country(); |
|
| 207 | 207 | $is_eu = $this->is_eu_country( $submission->country ); |
| 208 | 208 | $is_ip_eu = $this->is_eu_country( $ip_country ); |
| 209 | 209 | |
| 210 | - // Maybe abort early for initial fetches. |
|
| 211 | - if ( $submission->is_initial_fetch() && empty( $vat_number ) ) { |
|
| 212 | - return; |
|
| 213 | - } |
|
| 210 | + // Maybe abort early for initial fetches. |
|
| 211 | + if ( $submission->is_initial_fetch() && empty( $vat_number ) ) { |
|
| 212 | + return; |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - // If we're preventing business to consumer purchases, |
|
| 216 | - if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) { |
|
| 215 | + // If we're preventing business to consumer purchases, |
|
| 216 | + if ( $this->requires_vat( $is_ip_eu, $is_eu ) && empty( $vat_number ) ) { |
|
| 217 | 217 | |
| 218 | - // Ensure that a vat number has been specified. |
|
| 219 | - throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) ); |
|
| 218 | + // Ensure that a vat number has been specified. |
|
| 219 | + throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Please enter your VAT number to verify your purchase is by an EU business.', 'invoicing' ) ); |
|
| 220 | 220 | |
| 221 | - } |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - if ( empty( $vat_number ) ) { |
|
| 224 | - return; |
|
| 225 | - } |
|
| 223 | + if ( empty( $vat_number ) ) { |
|
| 224 | + return; |
|
| 225 | + } |
|
| 226 | 226 | |
| 227 | - if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) { |
|
| 228 | - throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Your VAT number is invalid', 'invoicing' ) ); |
|
| 229 | - } |
|
| 227 | + if ( wpinv_should_validate_vat_number() && ! wpinv_validate_vat_number( $vat_number, $submission->country ) ) { |
|
| 228 | + throw new GetPaid_Payment_Exception( '.getpaid-error-billingwpinv_vat_number.getpaid-custom-payment-form-errors', __( 'Your VAT number is invalid', 'invoicing' ) ); |
|
| 229 | + } |
|
| 230 | 230 | |
| 231 | - if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) { |
|
| 232 | - return; |
|
| 233 | - } |
|
| 231 | + if ( wpinv_default_billing_country() == $submission->country && 'vat_too' == wpinv_get_option( 'vat_same_country_rule', 'vat_too' ) ) { |
|
| 232 | + return; |
|
| 233 | + } |
|
| 234 | 234 | |
| 235 | - $this->skip_taxes = true; |
|
| 236 | - } |
|
| 235 | + $this->skip_taxes = true; |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | 238 | } |
@@ -12,69 +12,69 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | abstract class GetPaid_Reports_Abstract_Report { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - public $stats; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Class constructor. |
|
| 22 | - * |
|
| 23 | - */ |
|
| 24 | - public function __construct() { |
|
| 25 | - $this->prepare_stats(); |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * Retrieves the current range. |
|
| 30 | - * |
|
| 31 | - */ |
|
| 32 | - public function get_range() { |
|
| 33 | - $valid_ranges = $this->get_periods(); |
|
| 34 | - |
|
| 35 | - if ( isset( $_GET['date_range'] ) && array_key_exists( $_GET['date_range'], $valid_ranges ) ) { |
|
| 36 | - return sanitize_key( $_GET['date_range'] ); |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - return '7_days'; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Returns an array of date ranges. |
|
| 44 | - * |
|
| 45 | - * @return array |
|
| 46 | - */ |
|
| 47 | - public function get_periods() { |
|
| 48 | - |
|
| 49 | - $periods = array( |
|
| 15 | + /** |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + public $stats; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Class constructor. |
|
| 22 | + * |
|
| 23 | + */ |
|
| 24 | + public function __construct() { |
|
| 25 | + $this->prepare_stats(); |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * Retrieves the current range. |
|
| 30 | + * |
|
| 31 | + */ |
|
| 32 | + public function get_range() { |
|
| 33 | + $valid_ranges = $this->get_periods(); |
|
| 34 | + |
|
| 35 | + if ( isset( $_GET['date_range'] ) && array_key_exists( $_GET['date_range'], $valid_ranges ) ) { |
|
| 36 | + return sanitize_key( $_GET['date_range'] ); |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + return '7_days'; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Returns an array of date ranges. |
|
| 44 | + * |
|
| 45 | + * @return array |
|
| 46 | + */ |
|
| 47 | + public function get_periods() { |
|
| 48 | + |
|
| 49 | + $periods = array( |
|
| 50 | 50 | 'today' => __( 'Today', 'invoicing' ), |
| 51 | 51 | 'yesterday' => __( 'Yesterday', 'invoicing' ), |
| 52 | 52 | '7_days' => __( 'Last 7 days', 'invoicing' ), |
| 53 | - '30_days' => __( 'Last 30 days', 'invoicing' ), |
|
| 54 | - '60_days' => __( 'Last 60 days', 'invoicing' ), |
|
| 55 | - '90_days' => __( 'Last 90 days', 'invoicing' ), |
|
| 56 | - '180_days' => __( 'Last 180 days', 'invoicing' ), |
|
| 57 | - '360_days' => __( 'Last 360 days', 'invoicing' ), |
|
| 58 | - ); |
|
| 59 | - |
|
| 60 | - return apply_filters( 'getpaid_earning_periods', $periods ); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Retrieves the current range's sql. |
|
| 65 | - * |
|
| 66 | - */ |
|
| 67 | - public function get_range_sql( $range, $date = 'CAST(meta.completed_date AS DATE)', $datetime = 'meta.comlpeted_date' ) { |
|
| 53 | + '30_days' => __( 'Last 30 days', 'invoicing' ), |
|
| 54 | + '60_days' => __( 'Last 60 days', 'invoicing' ), |
|
| 55 | + '90_days' => __( 'Last 90 days', 'invoicing' ), |
|
| 56 | + '180_days' => __( 'Last 180 days', 'invoicing' ), |
|
| 57 | + '360_days' => __( 'Last 360 days', 'invoicing' ), |
|
| 58 | + ); |
|
| 59 | + |
|
| 60 | + return apply_filters( 'getpaid_earning_periods', $periods ); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Retrieves the current range's sql. |
|
| 65 | + * |
|
| 66 | + */ |
|
| 67 | + public function get_range_sql( $range, $date = 'CAST(meta.completed_date AS DATE)', $datetime = 'meta.comlpeted_date' ) { |
|
| 68 | 68 | |
| 69 | 69 | // Prepare durations. |
| 70 | 70 | $today = current_time( 'Y-m-d' ); |
| 71 | - $yesterday = date( 'Y-m-d', strtotime( '-1 day', current_time( 'timestamp' ) ) ); |
|
| 72 | - $seven_days_ago = date( 'Y-m-d', strtotime( '-7 days', current_time( 'timestamp' ) ) ); |
|
| 73 | - $thirty_days_ago = date( 'Y-m-d', strtotime( '-30 days', current_time( 'timestamp' ) ) ); |
|
| 74 | - $ninety_days_ago = date( 'Y-m-d', strtotime( '-90 days', current_time( 'timestamp' ) ) ); |
|
| 75 | - $sixty_days_ago = date( 'Y-m-d', strtotime( '-60 days', current_time( 'timestamp' ) ) ); |
|
| 76 | - $one_eighty_days_ago = date( 'Y-m-d', strtotime( '-180 days', current_time( 'timestamp' ) ) ); |
|
| 77 | - $three_sixty_days_ago = date( 'Y-m-d', strtotime( '-360 days', current_time( 'timestamp' ) ) ); |
|
| 71 | + $yesterday = date( 'Y-m-d', strtotime( '-1 day', current_time( 'timestamp' ) ) ); |
|
| 72 | + $seven_days_ago = date( 'Y-m-d', strtotime( '-7 days', current_time( 'timestamp' ) ) ); |
|
| 73 | + $thirty_days_ago = date( 'Y-m-d', strtotime( '-30 days', current_time( 'timestamp' ) ) ); |
|
| 74 | + $ninety_days_ago = date( 'Y-m-d', strtotime( '-90 days', current_time( 'timestamp' ) ) ); |
|
| 75 | + $sixty_days_ago = date( 'Y-m-d', strtotime( '-60 days', current_time( 'timestamp' ) ) ); |
|
| 76 | + $one_eighty_days_ago = date( 'Y-m-d', strtotime( '-180 days', current_time( 'timestamp' ) ) ); |
|
| 77 | + $three_sixty_days_ago = date( 'Y-m-d', strtotime( '-360 days', current_time( 'timestamp' ) ) ); |
|
| 78 | 78 | |
| 79 | 79 | $ranges = array( |
| 80 | 80 | |
@@ -91,130 +91,130 @@ discard block |
||
| 91 | 91 | '7_days' => array( |
| 92 | 92 | "DATE($datetime)", |
| 93 | 93 | "$date BETWEEN '$seven_days_ago' AND '$today'", |
| 94 | - ), |
|
| 94 | + ), |
|
| 95 | 95 | |
| 96 | - '30_days' => array( |
|
| 96 | + '30_days' => array( |
|
| 97 | 97 | "DATE($datetime)", |
| 98 | 98 | "$date BETWEEN '$thirty_days_ago' AND '$today'", |
| 99 | - ), |
|
| 99 | + ), |
|
| 100 | 100 | |
| 101 | - '60_days' => array( |
|
| 101 | + '60_days' => array( |
|
| 102 | 102 | "DATE($datetime)", |
| 103 | 103 | "$date BETWEEN '$sixty_days_ago' AND '$today'", |
| 104 | - ), |
|
| 104 | + ), |
|
| 105 | 105 | |
| 106 | - '90_days' => array( |
|
| 106 | + '90_days' => array( |
|
| 107 | 107 | "WEEK($datetime)", |
| 108 | 108 | "$date BETWEEN '$ninety_days_ago' AND '$today'", |
| 109 | - ), |
|
| 109 | + ), |
|
| 110 | 110 | |
| 111 | - '180_days' => array( |
|
| 111 | + '180_days' => array( |
|
| 112 | 112 | "WEEK($datetime)", |
| 113 | 113 | "$date BETWEEN '$one_eighty_days_ago' AND '$today'", |
| 114 | - ), |
|
| 114 | + ), |
|
| 115 | 115 | |
| 116 | - '360_days' => array( |
|
| 116 | + '360_days' => array( |
|
| 117 | 117 | "WEEK($datetime)", |
| 118 | 118 | "$date BETWEEN '$three_sixty_days_ago' AND '$today'", |
| 119 | 119 | ), |
| 120 | 120 | |
| 121 | 121 | ); |
| 122 | 122 | |
| 123 | - $sql = isset( $ranges[ $range ] ) ? $ranges[ $range ] : $ranges['7_days']; |
|
| 124 | - return apply_filters( 'getpaid_earning_graphs_get_range_sql', $sql, $range ); |
|
| 125 | - |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Retrieves the hours in a day |
|
| 130 | - * |
|
| 131 | - */ |
|
| 132 | - public function get_hours_in_a_day() { |
|
| 133 | - |
|
| 134 | - return array( |
|
| 135 | - '12AM' => __( '12 AM', 'invoicing' ), |
|
| 136 | - '1AM' => __( '1 AM', 'invoicing' ), |
|
| 137 | - '2AM' => __( '2 AM', 'invoicing' ), |
|
| 138 | - '3AM' => __( '3 AM', 'invoicing' ), |
|
| 139 | - '4AM' => __( '4 AM', 'invoicing' ), |
|
| 140 | - '5AM' => __( '5 AM', 'invoicing' ), |
|
| 141 | - '6AM' => __( '6 AM', 'invoicing' ), |
|
| 142 | - '7AM' => __( '7 AM', 'invoicing' ), |
|
| 143 | - '8AM' => __( '8 AM', 'invoicing' ), |
|
| 144 | - '9AM' => __( '9 AM', 'invoicing' ), |
|
| 145 | - '10AM' => __( '10 AM', 'invoicing' ), |
|
| 146 | - '11AM' => __( '11 AM', 'invoicing' ), |
|
| 147 | - '12pm' => __( '12 PM', 'invoicing' ), |
|
| 148 | - '1PM' => __( '1 PM', 'invoicing' ), |
|
| 149 | - '2PM' => __( '2 PM', 'invoicing' ), |
|
| 150 | - '3PM' => __( '3 PM', 'invoicing' ), |
|
| 151 | - '4PM' => __( '4 PM', 'invoicing' ), |
|
| 152 | - '5PM' => __( '5 PM', 'invoicing' ), |
|
| 153 | - '6PM' => __( '6 PM', 'invoicing' ), |
|
| 154 | - '7PM' => __( '7 PM', 'invoicing' ), |
|
| 155 | - '8PM' => __( '8 PM', 'invoicing' ), |
|
| 156 | - '9PM' => __( '9 PM', 'invoicing' ), |
|
| 157 | - '10PM' => __( '10 PM', 'invoicing' ), |
|
| 158 | - '11PM' => __( '11 PM', 'invoicing' ), |
|
| 159 | - ); |
|
| 160 | - |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * Retrieves the days in a period |
|
| 165 | - * |
|
| 166 | - */ |
|
| 167 | - public function get_days_in_period( $days ) { |
|
| 168 | - |
|
| 169 | - $return = array(); |
|
| 170 | - $format = 'Y-m-d'; |
|
| 171 | - |
|
| 172 | - if ( $days < 8 ) { |
|
| 173 | - $format = 'D'; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - if ( $days < 32 ) { |
|
| 177 | - $format = 'M j'; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - while ( $days > 0 ) { |
|
| 181 | - |
|
| 182 | - $key = date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
| 183 | - $label = date_i18n( $format, strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
| 184 | - $return[ $key ] = $label; |
|
| 185 | - $days--; |
|
| 186 | - |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - return $return; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - /** |
|
| 193 | - * Retrieves the weeks in a period |
|
| 194 | - * |
|
| 195 | - */ |
|
| 196 | - public function get_weeks_in_period( $days ) { |
|
| 197 | - |
|
| 198 | - $return = array(); |
|
| 199 | - |
|
| 200 | - while ( $days > 0 ) { |
|
| 201 | - |
|
| 202 | - $key = date( 'W', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
| 203 | - $label = date_i18n( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
| 204 | - $return[ $key ] = $label; |
|
| 205 | - $days--; |
|
| 206 | - |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - return $return; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Displays the report card. |
|
| 214 | - * |
|
| 215 | - */ |
|
| 216 | - public function display() { |
|
| 217 | - ?> |
|
| 123 | + $sql = isset( $ranges[ $range ] ) ? $ranges[ $range ] : $ranges['7_days']; |
|
| 124 | + return apply_filters( 'getpaid_earning_graphs_get_range_sql', $sql, $range ); |
|
| 125 | + |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Retrieves the hours in a day |
|
| 130 | + * |
|
| 131 | + */ |
|
| 132 | + public function get_hours_in_a_day() { |
|
| 133 | + |
|
| 134 | + return array( |
|
| 135 | + '12AM' => __( '12 AM', 'invoicing' ), |
|
| 136 | + '1AM' => __( '1 AM', 'invoicing' ), |
|
| 137 | + '2AM' => __( '2 AM', 'invoicing' ), |
|
| 138 | + '3AM' => __( '3 AM', 'invoicing' ), |
|
| 139 | + '4AM' => __( '4 AM', 'invoicing' ), |
|
| 140 | + '5AM' => __( '5 AM', 'invoicing' ), |
|
| 141 | + '6AM' => __( '6 AM', 'invoicing' ), |
|
| 142 | + '7AM' => __( '7 AM', 'invoicing' ), |
|
| 143 | + '8AM' => __( '8 AM', 'invoicing' ), |
|
| 144 | + '9AM' => __( '9 AM', 'invoicing' ), |
|
| 145 | + '10AM' => __( '10 AM', 'invoicing' ), |
|
| 146 | + '11AM' => __( '11 AM', 'invoicing' ), |
|
| 147 | + '12pm' => __( '12 PM', 'invoicing' ), |
|
| 148 | + '1PM' => __( '1 PM', 'invoicing' ), |
|
| 149 | + '2PM' => __( '2 PM', 'invoicing' ), |
|
| 150 | + '3PM' => __( '3 PM', 'invoicing' ), |
|
| 151 | + '4PM' => __( '4 PM', 'invoicing' ), |
|
| 152 | + '5PM' => __( '5 PM', 'invoicing' ), |
|
| 153 | + '6PM' => __( '6 PM', 'invoicing' ), |
|
| 154 | + '7PM' => __( '7 PM', 'invoicing' ), |
|
| 155 | + '8PM' => __( '8 PM', 'invoicing' ), |
|
| 156 | + '9PM' => __( '9 PM', 'invoicing' ), |
|
| 157 | + '10PM' => __( '10 PM', 'invoicing' ), |
|
| 158 | + '11PM' => __( '11 PM', 'invoicing' ), |
|
| 159 | + ); |
|
| 160 | + |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * Retrieves the days in a period |
|
| 165 | + * |
|
| 166 | + */ |
|
| 167 | + public function get_days_in_period( $days ) { |
|
| 168 | + |
|
| 169 | + $return = array(); |
|
| 170 | + $format = 'Y-m-d'; |
|
| 171 | + |
|
| 172 | + if ( $days < 8 ) { |
|
| 173 | + $format = 'D'; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + if ( $days < 32 ) { |
|
| 177 | + $format = 'M j'; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + while ( $days > 0 ) { |
|
| 181 | + |
|
| 182 | + $key = date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
| 183 | + $label = date_i18n( $format, strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
| 184 | + $return[ $key ] = $label; |
|
| 185 | + $days--; |
|
| 186 | + |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + return $return; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + /** |
|
| 193 | + * Retrieves the weeks in a period |
|
| 194 | + * |
|
| 195 | + */ |
|
| 196 | + public function get_weeks_in_period( $days ) { |
|
| 197 | + |
|
| 198 | + $return = array(); |
|
| 199 | + |
|
| 200 | + while ( $days > 0 ) { |
|
| 201 | + |
|
| 202 | + $key = date( 'W', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
| 203 | + $label = date_i18n( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ); |
|
| 204 | + $return[ $key ] = $label; |
|
| 205 | + $days--; |
|
| 206 | + |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + return $return; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Displays the report card. |
|
| 214 | + * |
|
| 215 | + */ |
|
| 216 | + public function display() { |
|
| 217 | + ?> |
|
| 218 | 218 | |
| 219 | 219 | <div class="row"> |
| 220 | 220 | <div class="col-12"> |
@@ -228,20 +228,20 @@ discard block |
||
| 228 | 228 | |
| 229 | 229 | <?php |
| 230 | 230 | |
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * Prepares the report stats. |
|
| 235 | - * |
|
| 236 | - * Extend this in child classes. |
|
| 237 | - */ |
|
| 238 | - abstract public function prepare_stats(); |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * Displays the actual report. |
|
| 242 | - * |
|
| 243 | - * Extend this in child classes. |
|
| 244 | - */ |
|
| 245 | - abstract public function display_stats(); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * Prepares the report stats. |
|
| 235 | + * |
|
| 236 | + * Extend this in child classes. |
|
| 237 | + */ |
|
| 238 | + abstract public function prepare_stats(); |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * Displays the actual report. |
|
| 242 | + * |
|
| 243 | + * Extend this in child classes. |
|
| 244 | + */ |
|
| 245 | + abstract public function display_stats(); |
|
| 246 | 246 | |
| 247 | 247 | } |
@@ -12,23 +12,23 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Reports_Report_Items extends GetPaid_Reports_Abstract_Report { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - public $field = 'item_name'; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Retrieves the earning sql. |
|
| 22 | - * |
|
| 23 | - */ |
|
| 24 | - public function get_sql( $range ) { |
|
| 25 | - global $wpdb; |
|
| 26 | - |
|
| 27 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 28 | - $table2 = $wpdb->prefix . 'getpaid_invoice_items'; |
|
| 29 | - $clauses = $this->get_range_sql( $range ); |
|
| 30 | - |
|
| 31 | - $sql = "SELECT |
|
| 15 | + /** |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + public $field = 'item_name'; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Retrieves the earning sql. |
|
| 22 | + * |
|
| 23 | + */ |
|
| 24 | + public function get_sql( $range ) { |
|
| 25 | + global $wpdb; |
|
| 26 | + |
|
| 27 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 28 | + $table2 = $wpdb->prefix . 'getpaid_invoice_items'; |
|
| 29 | + $clauses = $this->get_range_sql( $range ); |
|
| 30 | + |
|
| 31 | + $sql = "SELECT |
|
| 32 | 32 | item.item_name AS item_name, |
| 33 | 33 | item.item_id AS item_id, |
| 34 | 34 | SUM(price) as total |
@@ -43,91 +43,91 @@ discard block |
||
| 43 | 43 | ORDER BY total DESC |
| 44 | 44 | "; |
| 45 | 45 | |
| 46 | - return apply_filters( 'getpaid_items_graphs_get_sql', $sql, $range ); |
|
| 46 | + return apply_filters( 'getpaid_items_graphs_get_sql', $sql, $range ); |
|
| 47 | 47 | |
| 48 | - } |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Prepares the report stats. |
|
| 52 | - * |
|
| 53 | - */ |
|
| 54 | - public function prepare_stats() { |
|
| 55 | - global $wpdb; |
|
| 56 | - $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
| 57 | - $this->stats = $this->normalize_stats( $this->stats ); |
|
| 58 | - } |
|
| 50 | + /** |
|
| 51 | + * Prepares the report stats. |
|
| 52 | + * |
|
| 53 | + */ |
|
| 54 | + public function prepare_stats() { |
|
| 55 | + global $wpdb; |
|
| 56 | + $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
| 57 | + $this->stats = $this->normalize_stats( $this->stats ); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - /** |
|
| 61 | - * Normalizes the report stats. |
|
| 62 | - * |
|
| 63 | - */ |
|
| 64 | - public function normalize_stats( $stats ) { |
|
| 65 | - $normalized = array(); |
|
| 66 | - $others = 0; |
|
| 67 | - $did = 0; |
|
| 60 | + /** |
|
| 61 | + * Normalizes the report stats. |
|
| 62 | + * |
|
| 63 | + */ |
|
| 64 | + public function normalize_stats( $stats ) { |
|
| 65 | + $normalized = array(); |
|
| 66 | + $others = 0; |
|
| 67 | + $did = 0; |
|
| 68 | 68 | |
| 69 | - foreach ( $stats as $stat ) { |
|
| 69 | + foreach ( $stats as $stat ) { |
|
| 70 | 70 | |
| 71 | - if ( $did > 4 ) { |
|
| 71 | + if ( $did > 4 ) { |
|
| 72 | 72 | |
| 73 | - $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ); |
|
| 73 | + $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ); |
|
| 74 | 74 | |
| 75 | - } else { |
|
| 75 | + } else { |
|
| 76 | 76 | |
| 77 | - $normalized[] = array( |
|
| 78 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
| 79 | - 'item_name' => strip_tags( $stat->item_name ), |
|
| 80 | - ); |
|
| 77 | + $normalized[] = array( |
|
| 78 | + 'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
| 79 | + 'item_name' => strip_tags( $stat->item_name ), |
|
| 80 | + ); |
|
| 81 | 81 | |
| 82 | - } |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - $did++; |
|
| 85 | - } |
|
| 84 | + $did++; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - if ( $others > 0 ) { |
|
| 87 | + if ( $others > 0 ) { |
|
| 88 | 88 | |
| 89 | - $normalized[] = array( |
|
| 90 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
| 91 | - 'item_name' => esc_html__( 'Others', 'invoicing' ), |
|
| 92 | - ); |
|
| 89 | + $normalized[] = array( |
|
| 90 | + 'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
| 91 | + 'item_name' => esc_html__( 'Others', 'invoicing' ), |
|
| 92 | + ); |
|
| 93 | 93 | |
| 94 | - } |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - return $normalized; |
|
| 97 | - } |
|
| 96 | + return $normalized; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * Retrieves report data. |
|
| 101 | - * |
|
| 102 | - */ |
|
| 103 | - public function get_data() { |
|
| 99 | + /** |
|
| 100 | + * Retrieves report data. |
|
| 101 | + * |
|
| 102 | + */ |
|
| 103 | + public function get_data() { |
|
| 104 | 104 | |
| 105 | - $data = wp_list_pluck( $this->stats, 'total' ); |
|
| 106 | - $colors = array( '#009688', '#4caf50', '#8bc34a', '#00bcd4', '#03a9f4', '#2196f3' ); |
|
| 105 | + $data = wp_list_pluck( $this->stats, 'total' ); |
|
| 106 | + $colors = array( '#009688', '#4caf50', '#8bc34a', '#00bcd4', '#03a9f4', '#2196f3' ); |
|
| 107 | 107 | |
| 108 | - shuffle( $colors ); |
|
| 108 | + shuffle( $colors ); |
|
| 109 | 109 | |
| 110 | - return array( |
|
| 111 | - 'data' => $data, |
|
| 112 | - 'backgroundColor' => $colors, |
|
| 113 | - ); |
|
| 110 | + return array( |
|
| 111 | + 'data' => $data, |
|
| 112 | + 'backgroundColor' => $colors, |
|
| 113 | + ); |
|
| 114 | 114 | |
| 115 | - } |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Retrieves report labels. |
|
| 119 | - * |
|
| 120 | - */ |
|
| 121 | - public function get_labels() { |
|
| 122 | - return wp_list_pluck( $this->stats, 'item_name' ); |
|
| 123 | - } |
|
| 117 | + /** |
|
| 118 | + * Retrieves report labels. |
|
| 119 | + * |
|
| 120 | + */ |
|
| 121 | + public function get_labels() { |
|
| 122 | + return wp_list_pluck( $this->stats, 'item_name' ); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - /** |
|
| 126 | - * Displays the actual report. |
|
| 127 | - * |
|
| 128 | - */ |
|
| 129 | - public function display_stats() { |
|
| 130 | - ?> |
|
| 125 | + /** |
|
| 126 | + * Displays the actual report. |
|
| 127 | + * |
|
| 128 | + */ |
|
| 129 | + public function display_stats() { |
|
| 130 | + ?> |
|
| 131 | 131 | |
| 132 | 132 | <canvas id="getpaid-chartjs-earnings-items"></canvas> |
| 133 | 133 | |
@@ -156,6 +156,6 @@ discard block |
||
| 156 | 156 | </script> |
| 157 | 157 | |
| 158 | 158 | <?php |
| 159 | - } |
|
| 159 | + } |
|
| 160 | 160 | |
| 161 | 161 | } |
@@ -12,47 +12,47 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Reports_Export { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Displays the reports tab. |
|
| 17 | - * |
|
| 18 | - */ |
|
| 19 | - public function display() { |
|
| 20 | - |
|
| 21 | - echo "<div class='row mt-4' style='max-width: 920px;' >"; |
|
| 22 | - foreach ( array_keys( getpaid_get_invoice_post_types() ) as $post_type ) { |
|
| 23 | - $this->display_post_type_export( $post_type ); |
|
| 24 | - } |
|
| 25 | - $this->display_subscription_export(); |
|
| 26 | - echo '</div>'; |
|
| 27 | - |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Retrieves the download url. |
|
| 32 | - * |
|
| 33 | - */ |
|
| 34 | - public function get_download_url( $post_type ) { |
|
| 35 | - |
|
| 36 | - return wp_nonce_url( |
|
| 37 | - add_query_arg( |
|
| 38 | - array( |
|
| 39 | - 'getpaid-admin-action' => 'export_invoices', |
|
| 40 | - 'post_type' => urlencode( $post_type ), |
|
| 41 | - ) |
|
| 42 | - ), |
|
| 43 | - 'getpaid-nonce', |
|
| 44 | - 'getpaid-nonce' |
|
| 45 | - ); |
|
| 46 | - |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Displays a single post type export card. |
|
| 51 | - * |
|
| 52 | - */ |
|
| 53 | - public function display_post_type_export( $post_type ) { |
|
| 54 | - |
|
| 55 | - ?> |
|
| 15 | + /** |
|
| 16 | + * Displays the reports tab. |
|
| 17 | + * |
|
| 18 | + */ |
|
| 19 | + public function display() { |
|
| 20 | + |
|
| 21 | + echo "<div class='row mt-4' style='max-width: 920px;' >"; |
|
| 22 | + foreach ( array_keys( getpaid_get_invoice_post_types() ) as $post_type ) { |
|
| 23 | + $this->display_post_type_export( $post_type ); |
|
| 24 | + } |
|
| 25 | + $this->display_subscription_export(); |
|
| 26 | + echo '</div>'; |
|
| 27 | + |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Retrieves the download url. |
|
| 32 | + * |
|
| 33 | + */ |
|
| 34 | + public function get_download_url( $post_type ) { |
|
| 35 | + |
|
| 36 | + return wp_nonce_url( |
|
| 37 | + add_query_arg( |
|
| 38 | + array( |
|
| 39 | + 'getpaid-admin-action' => 'export_invoices', |
|
| 40 | + 'post_type' => urlencode( $post_type ), |
|
| 41 | + ) |
|
| 42 | + ), |
|
| 43 | + 'getpaid-nonce', |
|
| 44 | + 'getpaid-nonce' |
|
| 45 | + ); |
|
| 46 | + |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Displays a single post type export card. |
|
| 51 | + * |
|
| 52 | + */ |
|
| 53 | + public function display_post_type_export( $post_type ) { |
|
| 54 | + |
|
| 55 | + ?> |
|
| 56 | 56 | |
| 57 | 57 | <div class="col-12 col-md-6"> |
| 58 | 58 | <div class="card m-0 p-0" style="max-width:100%"> |
@@ -60,11 +60,11 @@ discard block |
||
| 60 | 60 | <div class="card-header"> |
| 61 | 61 | <strong> |
| 62 | 62 | <?php |
| 63 | - printf( |
|
| 64 | - __( 'Export %s', 'invoicing' ), |
|
| 65 | - sanitize_text_field( getpaid_get_post_type_label( $post_type ) ) |
|
| 66 | - ); |
|
| 67 | - ?> |
|
| 63 | + printf( |
|
| 64 | + __( 'Export %s', 'invoicing' ), |
|
| 65 | + sanitize_text_field( getpaid_get_post_type_label( $post_type ) ) |
|
| 66 | + ); |
|
| 67 | + ?> |
|
| 68 | 68 | </strong> |
| 69 | 69 | </div> |
| 70 | 70 | |
@@ -73,12 +73,12 @@ discard block |
||
| 73 | 73 | <form method="post" action="<?php echo esc_url( $this->get_download_url( $post_type ) ); ?>"> |
| 74 | 74 | |
| 75 | 75 | <?php |
| 76 | - $this->display_markup( $this->generate_from_date( $post_type ) ); |
|
| 77 | - $this->display_markup( $this->generate_to_date( $post_type ) ); |
|
| 78 | - $this->display_markup( $this->generate_post_status_select( $post_type ) ); |
|
| 79 | - $this->display_markup( $this->generate_file_type_select( $post_type ) ); |
|
| 80 | - submit_button( __( 'Download', 'invoicing' ) ); |
|
| 81 | - ?> |
|
| 76 | + $this->display_markup( $this->generate_from_date( $post_type ) ); |
|
| 77 | + $this->display_markup( $this->generate_to_date( $post_type ) ); |
|
| 78 | + $this->display_markup( $this->generate_post_status_select( $post_type ) ); |
|
| 79 | + $this->display_markup( $this->generate_file_type_select( $post_type ) ); |
|
| 80 | + submit_button( __( 'Download', 'invoicing' ) ); |
|
| 81 | + ?> |
|
| 82 | 82 | |
| 83 | 83 | </form> |
| 84 | 84 | |
@@ -89,122 +89,122 @@ discard block |
||
| 89 | 89 | |
| 90 | 90 | <?php |
| 91 | 91 | |
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Generates the from date input field. |
|
| 96 | - * |
|
| 97 | - */ |
|
| 98 | - public function generate_from_date( $post_type ) { |
|
| 99 | - |
|
| 100 | - return aui()->input( |
|
| 101 | - array( |
|
| 102 | - 'name' => 'from_date', |
|
| 103 | - 'id' => esc_attr( "$post_type-from_date" ), |
|
| 104 | - 'placeholder' => 'yy-mm-dd', |
|
| 105 | - 'label' => __( 'From Date', 'invoicing' ), |
|
| 106 | - 'label_type' => 'vertical', |
|
| 107 | - 'label_class' => 'd-block', |
|
| 108 | - 'type' => 'datepicker', |
|
| 109 | - ) |
|
| 110 | - ); |
|
| 111 | - |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Generates the to date input field. |
|
| 116 | - * |
|
| 117 | - */ |
|
| 118 | - public function generate_to_date( $post_type ) { |
|
| 119 | - |
|
| 120 | - return aui()->input( |
|
| 121 | - array( |
|
| 122 | - 'name' => 'to_date', |
|
| 123 | - 'id' => esc_attr( "$post_type-to_date" ), |
|
| 124 | - 'placeholder' => 'yy-mm-dd', |
|
| 125 | - 'label' => __( 'To Date', 'invoicing' ), |
|
| 126 | - 'label_type' => 'vertical', |
|
| 127 | - 'label_class' => 'd-block', |
|
| 128 | - 'type' => 'datepicker', |
|
| 129 | - ) |
|
| 130 | - ); |
|
| 131 | - |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Generates the to post status select field. |
|
| 136 | - * |
|
| 137 | - */ |
|
| 138 | - public function generate_post_status_select( $post_type ) { |
|
| 139 | - |
|
| 140 | - if ( 'subscriptions' === $post_type ) { |
|
| 141 | - $options = getpaid_get_subscription_statuses(); |
|
| 142 | - } else { |
|
| 143 | - $options = wpinv_get_invoice_statuses( true, false, $post_type ); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - return aui()->select( |
|
| 147 | - array( |
|
| 148 | - 'name' => 'status', |
|
| 149 | - 'id' => esc_attr( "$post_type-status" ), |
|
| 150 | - 'placeholder' => __( 'All Statuses', 'invoicing' ), |
|
| 151 | - 'label' => __( 'Status', 'invoicing' ), |
|
| 152 | - 'label_type' => 'vertical', |
|
| 153 | - 'label_class' => 'd-block', |
|
| 154 | - 'options' => $options, |
|
| 155 | - ) |
|
| 156 | - ); |
|
| 157 | - |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * Generates the to file type select field. |
|
| 162 | - * |
|
| 163 | - */ |
|
| 164 | - public function generate_file_type_select( $post_type ) { |
|
| 165 | - |
|
| 166 | - return aui()->select( |
|
| 167 | - array( |
|
| 168 | - 'name' => 'file_type', |
|
| 169 | - 'id' => esc_attr( "$post_type-file_type" ), |
|
| 170 | - 'placeholder' => __( 'Select File Type', 'invoicing' ), |
|
| 171 | - 'label' => __( 'Export File', 'invoicing' ), |
|
| 172 | - 'label_type' => 'vertical', |
|
| 173 | - 'label_class' => 'd-block', |
|
| 174 | - 'options' => array( |
|
| 175 | - 'csv' => __( 'CSV', 'invoicing' ), |
|
| 176 | - 'xml' => __( 'XML', 'invoicing' ), |
|
| 177 | - 'json' => __( 'JSON', 'invoicing' ), |
|
| 178 | - ), |
|
| 179 | - ) |
|
| 180 | - ); |
|
| 181 | - |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * Displays a field's markup. |
|
| 186 | - * |
|
| 187 | - */ |
|
| 188 | - public function display_markup( $markup ) { |
|
| 189 | - |
|
| 190 | - echo str_replace( |
|
| 191 | - array( |
|
| 192 | - 'form-control', |
|
| 193 | - 'custom-select', |
|
| 194 | - ), |
|
| 195 | - 'regular-text', |
|
| 196 | - $markup |
|
| 197 | - ); |
|
| 198 | - |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - /** |
|
| 202 | - * Displays a subscription export card. |
|
| 203 | - * |
|
| 204 | - */ |
|
| 205 | - public function display_subscription_export() { |
|
| 206 | - |
|
| 207 | - ?> |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Generates the from date input field. |
|
| 96 | + * |
|
| 97 | + */ |
|
| 98 | + public function generate_from_date( $post_type ) { |
|
| 99 | + |
|
| 100 | + return aui()->input( |
|
| 101 | + array( |
|
| 102 | + 'name' => 'from_date', |
|
| 103 | + 'id' => esc_attr( "$post_type-from_date" ), |
|
| 104 | + 'placeholder' => 'yy-mm-dd', |
|
| 105 | + 'label' => __( 'From Date', 'invoicing' ), |
|
| 106 | + 'label_type' => 'vertical', |
|
| 107 | + 'label_class' => 'd-block', |
|
| 108 | + 'type' => 'datepicker', |
|
| 109 | + ) |
|
| 110 | + ); |
|
| 111 | + |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Generates the to date input field. |
|
| 116 | + * |
|
| 117 | + */ |
|
| 118 | + public function generate_to_date( $post_type ) { |
|
| 119 | + |
|
| 120 | + return aui()->input( |
|
| 121 | + array( |
|
| 122 | + 'name' => 'to_date', |
|
| 123 | + 'id' => esc_attr( "$post_type-to_date" ), |
|
| 124 | + 'placeholder' => 'yy-mm-dd', |
|
| 125 | + 'label' => __( 'To Date', 'invoicing' ), |
|
| 126 | + 'label_type' => 'vertical', |
|
| 127 | + 'label_class' => 'd-block', |
|
| 128 | + 'type' => 'datepicker', |
|
| 129 | + ) |
|
| 130 | + ); |
|
| 131 | + |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Generates the to post status select field. |
|
| 136 | + * |
|
| 137 | + */ |
|
| 138 | + public function generate_post_status_select( $post_type ) { |
|
| 139 | + |
|
| 140 | + if ( 'subscriptions' === $post_type ) { |
|
| 141 | + $options = getpaid_get_subscription_statuses(); |
|
| 142 | + } else { |
|
| 143 | + $options = wpinv_get_invoice_statuses( true, false, $post_type ); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + return aui()->select( |
|
| 147 | + array( |
|
| 148 | + 'name' => 'status', |
|
| 149 | + 'id' => esc_attr( "$post_type-status" ), |
|
| 150 | + 'placeholder' => __( 'All Statuses', 'invoicing' ), |
|
| 151 | + 'label' => __( 'Status', 'invoicing' ), |
|
| 152 | + 'label_type' => 'vertical', |
|
| 153 | + 'label_class' => 'd-block', |
|
| 154 | + 'options' => $options, |
|
| 155 | + ) |
|
| 156 | + ); |
|
| 157 | + |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * Generates the to file type select field. |
|
| 162 | + * |
|
| 163 | + */ |
|
| 164 | + public function generate_file_type_select( $post_type ) { |
|
| 165 | + |
|
| 166 | + return aui()->select( |
|
| 167 | + array( |
|
| 168 | + 'name' => 'file_type', |
|
| 169 | + 'id' => esc_attr( "$post_type-file_type" ), |
|
| 170 | + 'placeholder' => __( 'Select File Type', 'invoicing' ), |
|
| 171 | + 'label' => __( 'Export File', 'invoicing' ), |
|
| 172 | + 'label_type' => 'vertical', |
|
| 173 | + 'label_class' => 'd-block', |
|
| 174 | + 'options' => array( |
|
| 175 | + 'csv' => __( 'CSV', 'invoicing' ), |
|
| 176 | + 'xml' => __( 'XML', 'invoicing' ), |
|
| 177 | + 'json' => __( 'JSON', 'invoicing' ), |
|
| 178 | + ), |
|
| 179 | + ) |
|
| 180 | + ); |
|
| 181 | + |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * Displays a field's markup. |
|
| 186 | + * |
|
| 187 | + */ |
|
| 188 | + public function display_markup( $markup ) { |
|
| 189 | + |
|
| 190 | + echo str_replace( |
|
| 191 | + array( |
|
| 192 | + 'form-control', |
|
| 193 | + 'custom-select', |
|
| 194 | + ), |
|
| 195 | + 'regular-text', |
|
| 196 | + $markup |
|
| 197 | + ); |
|
| 198 | + |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + /** |
|
| 202 | + * Displays a subscription export card. |
|
| 203 | + * |
|
| 204 | + */ |
|
| 205 | + public function display_subscription_export() { |
|
| 206 | + |
|
| 207 | + ?> |
|
| 208 | 208 | |
| 209 | 209 | <div class="col-12 col-md-6"> |
| 210 | 210 | <div class="card m-0 p-0" style="max-width:100%"> |
@@ -220,12 +220,12 @@ discard block |
||
| 220 | 220 | <form method="post" action="<?php echo esc_url( $this->get_download_url( 'subscriptions' ) ); ?>"> |
| 221 | 221 | |
| 222 | 222 | <?php |
| 223 | - $this->display_markup( $this->generate_from_date( 'subscriptions' ) ); |
|
| 224 | - $this->display_markup( $this->generate_to_date( 'subscriptions' ) ); |
|
| 225 | - $this->display_markup( $this->generate_post_status_select( 'subscriptions' ) ); |
|
| 226 | - $this->display_markup( $this->generate_file_type_select( 'subscriptions' ) ); |
|
| 227 | - submit_button( __( 'Download', 'invoicing' ) ); |
|
| 228 | - ?> |
|
| 223 | + $this->display_markup( $this->generate_from_date( 'subscriptions' ) ); |
|
| 224 | + $this->display_markup( $this->generate_to_date( 'subscriptions' ) ); |
|
| 225 | + $this->display_markup( $this->generate_post_status_select( 'subscriptions' ) ); |
|
| 226 | + $this->display_markup( $this->generate_file_type_select( 'subscriptions' ) ); |
|
| 227 | + submit_button( __( 'Download', 'invoicing' ) ); |
|
| 228 | + ?> |
|
| 229 | 229 | |
| 230 | 230 | </form> |
| 231 | 231 | |
@@ -236,6 +236,6 @@ discard block |
||
| 236 | 236 | |
| 237 | 237 | <?php |
| 238 | 238 | |
| 239 | - } |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | 241 | } |
@@ -12,43 +12,43 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Reports_Report_Earnings extends GetPaid_Reports_Abstract_Report { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * Retrieves the earning graphs. |
|
| 17 | - * |
|
| 18 | - */ |
|
| 19 | - public function get_graphs() { |
|
| 15 | + /** |
|
| 16 | + * Retrieves the earning graphs. |
|
| 17 | + * |
|
| 18 | + */ |
|
| 19 | + public function get_graphs() { |
|
| 20 | 20 | |
| 21 | - $graphs = array( |
|
| 21 | + $graphs = array( |
|
| 22 | 22 | |
| 23 | 23 | 'total' => __( 'Earnings', 'invoicing' ), |
| 24 | 24 | 'discount' => __( 'Discount', 'invoicing' ), |
| 25 | - 'fees_total' => __( 'Fees', 'invoicing' ), |
|
| 26 | - 'tax' => __( 'Tax', 'invoicing' ), |
|
| 25 | + 'fees_total' => __( 'Fees', 'invoicing' ), |
|
| 26 | + 'tax' => __( 'Tax', 'invoicing' ), |
|
| 27 | 27 | |
| 28 | - ); |
|
| 28 | + ); |
|
| 29 | 29 | |
| 30 | - return apply_filters( 'getpaid_earning_graphs', $graphs ); |
|
| 30 | + return apply_filters( 'getpaid_earning_graphs', $graphs ); |
|
| 31 | 31 | |
| 32 | - } |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Retrieves the earning sql. |
|
| 36 | - * |
|
| 37 | - */ |
|
| 38 | - public function get_sql( $range ) { |
|
| 39 | - global $wpdb; |
|
| 34 | + /** |
|
| 35 | + * Retrieves the earning sql. |
|
| 36 | + * |
|
| 37 | + */ |
|
| 38 | + public function get_sql( $range ) { |
|
| 39 | + global $wpdb; |
|
| 40 | 40 | |
| 41 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 42 | - $clauses = $this->get_range_sql( $range ); |
|
| 43 | - $graphs = array_keys( $this->get_graphs() ); |
|
| 44 | - $graphs_sql = array(); |
|
| 41 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 42 | + $clauses = $this->get_range_sql( $range ); |
|
| 43 | + $graphs = array_keys( $this->get_graphs() ); |
|
| 44 | + $graphs_sql = array(); |
|
| 45 | 45 | |
| 46 | - foreach ( $graphs as $graph ) { |
|
| 47 | - $graphs_sql[] = "SUM( meta.$graph ) AS $graph"; |
|
| 48 | - } |
|
| 46 | + foreach ( $graphs as $graph ) { |
|
| 47 | + $graphs_sql[] = "SUM( meta.$graph ) AS $graph"; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - $graphs_sql = implode( ', ', $graphs_sql ); |
|
| 51 | - $sql = "SELECT {$clauses[0]} AS completed_date, $graphs_sql |
|
| 50 | + $graphs_sql = implode( ', ', $graphs_sql ); |
|
| 51 | + $sql = "SELECT {$clauses[0]} AS completed_date, $graphs_sql |
|
| 52 | 52 | FROM $wpdb->posts |
| 53 | 53 | LEFT JOIN $table as meta ON meta.post_id = $wpdb->posts.ID |
| 54 | 54 | WHERE meta.post_id IS NOT NULL |
@@ -58,94 +58,94 @@ discard block |
||
| 58 | 58 | GROUP BY {$clauses[0]} |
| 59 | 59 | "; |
| 60 | 60 | |
| 61 | - return apply_filters( 'getpaid_earning_graphs_get_sql', $sql, $range ); |
|
| 62 | - |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Prepares the report stats. |
|
| 67 | - * |
|
| 68 | - */ |
|
| 69 | - public function prepare_stats() { |
|
| 70 | - global $wpdb; |
|
| 71 | - $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * Retrieves report labels. |
|
| 76 | - * |
|
| 77 | - */ |
|
| 78 | - public function get_labels( $range ) { |
|
| 79 | - |
|
| 80 | - $labels = array( |
|
| 81 | - 'today' => $this->get_hours_in_a_day(), |
|
| 82 | - 'yesterday' => $this->get_hours_in_a_day(), |
|
| 83 | - '7_days' => $this->get_days_in_period( 7 ), |
|
| 84 | - '30_days' => $this->get_days_in_period( 30 ), |
|
| 85 | - '60_days' => $this->get_days_in_period( 60 ), |
|
| 86 | - '90_days' => $this->get_weeks_in_period( 90 ), |
|
| 87 | - '180_days' => $this->get_weeks_in_period( 180 ), |
|
| 88 | - '360_days' => $this->get_weeks_in_period( 360 ), |
|
| 89 | - ); |
|
| 90 | - |
|
| 91 | - $label = isset( $labels[ $range ] ) ? $labels[ $range ] : $labels['7_days']; |
|
| 92 | - return apply_filters( 'getpaid_earning_graphs_get_labels', $label, $range ); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Retrieves report datasets. |
|
| 97 | - * |
|
| 98 | - */ |
|
| 99 | - public function get_datasets( $labels ) { |
|
| 100 | - |
|
| 101 | - $datasets = array(); |
|
| 102 | - |
|
| 103 | - foreach ( $this->get_graphs() as $key => $label ) { |
|
| 104 | - $datasets[ $key ] = array( |
|
| 105 | - 'label' => $label, |
|
| 106 | - 'data' => $this->get_data( $key, $labels ), |
|
| 107 | - ); |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - return apply_filters( 'getpaid_earning_graphs_get_datasets', $datasets, $labels ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - /** |
|
| 114 | - * Retrieves report data. |
|
| 115 | - * |
|
| 116 | - */ |
|
| 117 | - public function get_data( $key, $labels ) { |
|
| 118 | - |
|
| 119 | - $data = wp_list_pluck( $this->stats, $key, 'completed_date' ); |
|
| 120 | - $prepared = array(); |
|
| 121 | - |
|
| 122 | - foreach ( $labels as $label ) { |
|
| 123 | - |
|
| 124 | - $value = 0; |
|
| 125 | - if ( isset( $data[ $label ] ) ) { |
|
| 126 | - $value = wpinv_round_amount( wpinv_sanitize_amount( $data[ $label ] ) ); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - $prepared[] = $value; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - return apply_filters( 'getpaid_earning_graphs_get_data', $prepared, $key, $labels ); |
|
| 133 | - |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Displays the report card. |
|
| 138 | - * |
|
| 139 | - */ |
|
| 140 | - public function display() { |
|
| 141 | - |
|
| 142 | - $labels = $this->get_labels( $this->get_range() ); |
|
| 143 | - $chart_data = array( |
|
| 144 | - 'labels' => array_values( $labels ), |
|
| 145 | - 'datasets' => $this->get_datasets( array_keys( $labels ) ), |
|
| 146 | - ); |
|
| 147 | - |
|
| 148 | - ?> |
|
| 61 | + return apply_filters( 'getpaid_earning_graphs_get_sql', $sql, $range ); |
|
| 62 | + |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Prepares the report stats. |
|
| 67 | + * |
|
| 68 | + */ |
|
| 69 | + public function prepare_stats() { |
|
| 70 | + global $wpdb; |
|
| 71 | + $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * Retrieves report labels. |
|
| 76 | + * |
|
| 77 | + */ |
|
| 78 | + public function get_labels( $range ) { |
|
| 79 | + |
|
| 80 | + $labels = array( |
|
| 81 | + 'today' => $this->get_hours_in_a_day(), |
|
| 82 | + 'yesterday' => $this->get_hours_in_a_day(), |
|
| 83 | + '7_days' => $this->get_days_in_period( 7 ), |
|
| 84 | + '30_days' => $this->get_days_in_period( 30 ), |
|
| 85 | + '60_days' => $this->get_days_in_period( 60 ), |
|
| 86 | + '90_days' => $this->get_weeks_in_period( 90 ), |
|
| 87 | + '180_days' => $this->get_weeks_in_period( 180 ), |
|
| 88 | + '360_days' => $this->get_weeks_in_period( 360 ), |
|
| 89 | + ); |
|
| 90 | + |
|
| 91 | + $label = isset( $labels[ $range ] ) ? $labels[ $range ] : $labels['7_days']; |
|
| 92 | + return apply_filters( 'getpaid_earning_graphs_get_labels', $label, $range ); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Retrieves report datasets. |
|
| 97 | + * |
|
| 98 | + */ |
|
| 99 | + public function get_datasets( $labels ) { |
|
| 100 | + |
|
| 101 | + $datasets = array(); |
|
| 102 | + |
|
| 103 | + foreach ( $this->get_graphs() as $key => $label ) { |
|
| 104 | + $datasets[ $key ] = array( |
|
| 105 | + 'label' => $label, |
|
| 106 | + 'data' => $this->get_data( $key, $labels ), |
|
| 107 | + ); |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + return apply_filters( 'getpaid_earning_graphs_get_datasets', $datasets, $labels ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + /** |
|
| 114 | + * Retrieves report data. |
|
| 115 | + * |
|
| 116 | + */ |
|
| 117 | + public function get_data( $key, $labels ) { |
|
| 118 | + |
|
| 119 | + $data = wp_list_pluck( $this->stats, $key, 'completed_date' ); |
|
| 120 | + $prepared = array(); |
|
| 121 | + |
|
| 122 | + foreach ( $labels as $label ) { |
|
| 123 | + |
|
| 124 | + $value = 0; |
|
| 125 | + if ( isset( $data[ $label ] ) ) { |
|
| 126 | + $value = wpinv_round_amount( wpinv_sanitize_amount( $data[ $label ] ) ); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + $prepared[] = $value; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + return apply_filters( 'getpaid_earning_graphs_get_data', $prepared, $key, $labels ); |
|
| 133 | + |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Displays the report card. |
|
| 138 | + * |
|
| 139 | + */ |
|
| 140 | + public function display() { |
|
| 141 | + |
|
| 142 | + $labels = $this->get_labels( $this->get_range() ); |
|
| 143 | + $chart_data = array( |
|
| 144 | + 'labels' => array_values( $labels ), |
|
| 145 | + 'datasets' => $this->get_datasets( array_keys( $labels ) ), |
|
| 146 | + ); |
|
| 147 | + |
|
| 148 | + ?> |
|
| 149 | 149 | |
| 150 | 150 | <?php foreach ( $chart_data['datasets'] as $key => $dataset ) : ?> |
| 151 | 151 | <div class="row mb-4"> |
@@ -165,15 +165,15 @@ discard block |
||
| 165 | 165 | |
| 166 | 166 | <?php |
| 167 | 167 | |
| 168 | - } |
|
| 168 | + } |
|
| 169 | 169 | |
| 170 | - /** |
|
| 171 | - * Displays the actual report. |
|
| 172 | - * |
|
| 173 | - */ |
|
| 174 | - public function display_graph( $key, $dataset, $labels ) { |
|
| 170 | + /** |
|
| 171 | + * Displays the actual report. |
|
| 172 | + * |
|
| 173 | + */ |
|
| 174 | + public function display_graph( $key, $dataset, $labels ) { |
|
| 175 | 175 | |
| 176 | - ?> |
|
| 176 | + ?> |
|
| 177 | 177 | |
| 178 | 178 | <canvas id="getpaid-chartjs-earnings-<?php echo esc_attr( $key ); ?>"></canvas> |
| 179 | 179 | |
@@ -223,20 +223,20 @@ discard block |
||
| 223 | 223 | </script> |
| 224 | 224 | |
| 225 | 225 | <?php |
| 226 | - } |
|
| 226 | + } |
|
| 227 | 227 | |
| 228 | - /** |
|
| 229 | - * Displays the actual report. |
|
| 230 | - * |
|
| 231 | - */ |
|
| 232 | - public function display_stats() {} |
|
| 228 | + /** |
|
| 229 | + * Displays the actual report. |
|
| 230 | + * |
|
| 231 | + */ |
|
| 232 | + public function display_stats() {} |
|
| 233 | 233 | |
| 234 | - /** |
|
| 235 | - * Displays the range selector. |
|
| 236 | - * |
|
| 237 | - */ |
|
| 238 | - public function display_range_selector() { |
|
| 234 | + /** |
|
| 235 | + * Displays the range selector. |
|
| 236 | + * |
|
| 237 | + */ |
|
| 238 | + public function display_range_selector() { |
|
| 239 | 239 | |
| 240 | - } |
|
| 240 | + } |
|
| 241 | 241 | |
| 242 | 242 | } |
@@ -12,22 +12,22 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class GetPaid_Reports_Report_Discounts extends GetPaid_Reports_Abstract_Report { |
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * @var string |
|
| 17 | - */ |
|
| 18 | - public $field = 'discount_code'; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Retrieves the discounts sql. |
|
| 22 | - * |
|
| 23 | - */ |
|
| 24 | - public function get_sql( $range ) { |
|
| 25 | - global $wpdb; |
|
| 26 | - |
|
| 27 | - $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 28 | - $clauses = $this->get_range_sql( $range ); |
|
| 29 | - |
|
| 30 | - $sql = "SELECT |
|
| 15 | + /** |
|
| 16 | + * @var string |
|
| 17 | + */ |
|
| 18 | + public $field = 'discount_code'; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Retrieves the discounts sql. |
|
| 22 | + * |
|
| 23 | + */ |
|
| 24 | + public function get_sql( $range ) { |
|
| 25 | + global $wpdb; |
|
| 26 | + |
|
| 27 | + $table = $wpdb->prefix . 'getpaid_invoices'; |
|
| 28 | + $clauses = $this->get_range_sql( $range ); |
|
| 29 | + |
|
| 30 | + $sql = "SELECT |
|
| 31 | 31 | meta.discount_code AS discount_code, |
| 32 | 32 | SUM(total) as total |
| 33 | 33 | FROM $wpdb->posts |
@@ -41,91 +41,91 @@ discard block |
||
| 41 | 41 | ORDER BY total DESC |
| 42 | 42 | "; |
| 43 | 43 | |
| 44 | - return apply_filters( 'getpaid_discounts_graphs_get_sql', $sql, $range ); |
|
| 44 | + return apply_filters( 'getpaid_discounts_graphs_get_sql', $sql, $range ); |
|
| 45 | 45 | |
| 46 | - } |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * Prepares the report stats. |
|
| 50 | - * |
|
| 51 | - */ |
|
| 52 | - public function prepare_stats() { |
|
| 53 | - global $wpdb; |
|
| 54 | - $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
| 55 | - $this->stats = $this->normalize_stats( $this->stats ); |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * Prepares the report stats. |
|
| 50 | + * |
|
| 51 | + */ |
|
| 52 | + public function prepare_stats() { |
|
| 53 | + global $wpdb; |
|
| 54 | + $this->stats = $wpdb->get_results( $this->get_sql( $this->get_range() ) ); |
|
| 55 | + $this->stats = $this->normalize_stats( $this->stats ); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Normalizes the report stats. |
|
| 60 | - * |
|
| 61 | - */ |
|
| 62 | - public function normalize_stats( $stats ) { |
|
| 63 | - $normalized = array(); |
|
| 64 | - $others = 0; |
|
| 65 | - $did = 0; |
|
| 58 | + /** |
|
| 59 | + * Normalizes the report stats. |
|
| 60 | + * |
|
| 61 | + */ |
|
| 62 | + public function normalize_stats( $stats ) { |
|
| 63 | + $normalized = array(); |
|
| 64 | + $others = 0; |
|
| 65 | + $did = 0; |
|
| 66 | 66 | |
| 67 | - foreach ( $stats as $stat ) { |
|
| 67 | + foreach ( $stats as $stat ) { |
|
| 68 | 68 | |
| 69 | - if ( $did > 4 ) { |
|
| 69 | + if ( $did > 4 ) { |
|
| 70 | 70 | |
| 71 | - $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ); |
|
| 71 | + $others += wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ); |
|
| 72 | 72 | |
| 73 | - } else { |
|
| 73 | + } else { |
|
| 74 | 74 | |
| 75 | - $normalized[] = array( |
|
| 76 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
| 77 | - 'discount_code' => strip_tags( $stat->discount_code ), |
|
| 78 | - ); |
|
| 75 | + $normalized[] = array( |
|
| 76 | + 'total' => wpinv_round_amount( wpinv_sanitize_amount( $stat->total ) ), |
|
| 77 | + 'discount_code' => strip_tags( $stat->discount_code ), |
|
| 78 | + ); |
|
| 79 | 79 | |
| 80 | - } |
|
| 80 | + } |
|
| 81 | 81 | |
| 82 | - $did++; |
|
| 83 | - } |
|
| 82 | + $did++; |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - if ( $others > 0 ) { |
|
| 85 | + if ( $others > 0 ) { |
|
| 86 | 86 | |
| 87 | - $normalized[] = array( |
|
| 88 | - 'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
| 89 | - 'discount_code' => esc_html__( 'Others', 'invoicing' ), |
|
| 90 | - ); |
|
| 87 | + $normalized[] = array( |
|
| 88 | + 'total' => wpinv_round_amount( wpinv_sanitize_amount( $others ) ), |
|
| 89 | + 'discount_code' => esc_html__( 'Others', 'invoicing' ), |
|
| 90 | + ); |
|
| 91 | 91 | |
| 92 | - } |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - return $normalized; |
|
| 95 | - } |
|
| 94 | + return $normalized; |
|
| 95 | + } |
|
| 96 | 96 | |
| 97 | - /** |
|
| 98 | - * Retrieves report data. |
|
| 99 | - * |
|
| 100 | - */ |
|
| 101 | - public function get_data() { |
|
| 97 | + /** |
|
| 98 | + * Retrieves report data. |
|
| 99 | + * |
|
| 100 | + */ |
|
| 101 | + public function get_data() { |
|
| 102 | 102 | |
| 103 | - $data = wp_list_pluck( $this->stats, 'total' ); |
|
| 104 | - $colors = array( '#009688', '#4caf50', '#8bc34a', '#00bcd4', '#03a9f4', '#2196f3' ); |
|
| 103 | + $data = wp_list_pluck( $this->stats, 'total' ); |
|
| 104 | + $colors = array( '#009688', '#4caf50', '#8bc34a', '#00bcd4', '#03a9f4', '#2196f3' ); |
|
| 105 | 105 | |
| 106 | - shuffle( $colors ); |
|
| 106 | + shuffle( $colors ); |
|
| 107 | 107 | |
| 108 | - return array( |
|
| 109 | - 'data' => $data, |
|
| 110 | - 'backgroundColor' => $colors, |
|
| 111 | - ); |
|
| 108 | + return array( |
|
| 109 | + 'data' => $data, |
|
| 110 | + 'backgroundColor' => $colors, |
|
| 111 | + ); |
|
| 112 | 112 | |
| 113 | - } |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Retrieves report labels. |
|
| 117 | - * |
|
| 118 | - */ |
|
| 119 | - public function get_labels() { |
|
| 120 | - return wp_list_pluck( $this->stats, 'discount_code' ); |
|
| 121 | - } |
|
| 115 | + /** |
|
| 116 | + * Retrieves report labels. |
|
| 117 | + * |
|
| 118 | + */ |
|
| 119 | + public function get_labels() { |
|
| 120 | + return wp_list_pluck( $this->stats, 'discount_code' ); |
|
| 121 | + } |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Displays the actual report. |
|
| 125 | - * |
|
| 126 | - */ |
|
| 127 | - public function display_stats() { |
|
| 128 | - ?> |
|
| 123 | + /** |
|
| 124 | + * Displays the actual report. |
|
| 125 | + * |
|
| 126 | + */ |
|
| 127 | + public function display_stats() { |
|
| 128 | + ?> |
|
| 129 | 129 | |
| 130 | 130 | <canvas id="getpaid-chartjs-earnings-discount_code"></canvas> |
| 131 | 131 | |
@@ -154,6 +154,6 @@ discard block |
||
| 154 | 154 | </script> |
| 155 | 155 | |
| 156 | 156 | <?php |
| 157 | - } |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | 159 | } |