@@ -12,180 +12,180 @@ 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 | - wpinv_set_error( 'invalid_gateway', __( '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 | + wpinv_set_error( 'invalid_gateway', __( '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 | - $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
165 | - $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
164 | + $address_confirmed = $submission->get_field( 'confirm-address' ); |
|
165 | + $invoice->set_address_confirmed( ! empty( $address_confirmed ) ); |
|
166 | 166 | |
167 | - if ( $submission->has_discount_code() ) { |
|
167 | + if ( $submission->has_discount_code() ) { |
|
168 | 168 | $invoice->set_discount_code( $submission->get_discount_code() ); |
169 | - } |
|
170 | - |
|
171 | - getpaid_maybe_add_default_address( $invoice ); |
|
172 | - return $invoice; |
|
173 | - } |
|
174 | - |
|
175 | - /** |
|
176 | - * Retrieves the submission's customer. |
|
177 | - * |
|
178 | - * @return int The customer id. |
|
179 | - */ |
|
180 | - protected function get_submission_customer() { |
|
181 | - $submission = $this->payment_form_submission; |
|
182 | - |
|
183 | - // If this is an existing invoice... |
|
184 | - if ( $submission->has_invoice() ) { |
|
185 | - return $submission->get_invoice()->get_user_id(); |
|
186 | - } |
|
187 | - |
|
188 | - // (Maybe) create the user. |
|
169 | + } |
|
170 | + |
|
171 | + getpaid_maybe_add_default_address( $invoice ); |
|
172 | + return $invoice; |
|
173 | + } |
|
174 | + |
|
175 | + /** |
|
176 | + * Retrieves the submission's customer. |
|
177 | + * |
|
178 | + * @return int The customer id. |
|
179 | + */ |
|
180 | + protected function get_submission_customer() { |
|
181 | + $submission = $this->payment_form_submission; |
|
182 | + |
|
183 | + // If this is an existing invoice... |
|
184 | + if ( $submission->has_invoice() ) { |
|
185 | + return $submission->get_invoice()->get_user_id(); |
|
186 | + } |
|
187 | + |
|
188 | + // (Maybe) create the user. |
|
189 | 189 | $user = get_current_user_id(); |
190 | 190 | |
191 | 191 | if ( empty( $user ) ) { |
@@ -195,11 +195,11 @@ discard block |
||
195 | 195 | if ( empty( $user ) ) { |
196 | 196 | $user = wpinv_create_user( $submission->get_billing_email() ); |
197 | 197 | |
198 | - // (Maybe) send new user notification. |
|
199 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
200 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
201 | - wp_send_new_user_notifications( $user, 'user' ); |
|
202 | - } |
|
198 | + // (Maybe) send new user notification. |
|
199 | + $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
200 | + if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
201 | + wp_send_new_user_notifications( $user, 'user' ); |
|
202 | + } |
|
203 | 203 | |
204 | 204 | } |
205 | 205 | |
@@ -209,49 +209,49 @@ discard block |
||
209 | 209 | |
210 | 210 | if ( is_numeric( $user ) ) { |
211 | 211 | return $user; |
212 | - } |
|
212 | + } |
|
213 | 213 | |
214 | - return $user->ID; |
|
214 | + return $user->ID; |
|
215 | 215 | |
216 | - } |
|
216 | + } |
|
217 | 217 | |
218 | - /** |
|
218 | + /** |
|
219 | 219 | * Prepares submission data for saving to the database. |
220 | 220 | * |
221 | - * @return array |
|
221 | + * @return array |
|
222 | 222 | */ |
223 | 223 | public function prepare_submission_data_for_saving() { |
224 | 224 | |
225 | - $submission = $this->payment_form_submission; |
|
225 | + $submission = $this->payment_form_submission; |
|
226 | 226 | |
227 | - // Prepared submission details. |
|
227 | + // Prepared submission details. |
|
228 | 228 | $prepared = array( |
229 | - 'all' => array(), |
|
230 | - 'meta' => array(), |
|
231 | - ); |
|
229 | + 'all' => array(), |
|
230 | + 'meta' => array(), |
|
231 | + ); |
|
232 | 232 | |
233 | 233 | // Raw submission details. |
234 | - $data = $submission->get_data(); |
|
234 | + $data = $submission->get_data(); |
|
235 | 235 | |
236 | - // Loop through the submitted details. |
|
236 | + // Loop through the submitted details. |
|
237 | 237 | foreach ( $submission->get_payment_form()->get_elements() as $field ) { |
238 | 238 | |
239 | - // Skip premade fields. |
|
239 | + // Skip premade fields. |
|
240 | 240 | if ( ! empty( $field['premade'] ) ) { |
241 | 241 | continue; |
242 | 242 | } |
243 | 243 | |
244 | - // Ensure address is provided. |
|
245 | - if ( $field['type'] == 'address' ) { |
|
244 | + // Ensure address is provided. |
|
245 | + if ( $field['type'] == 'address' ) { |
|
246 | 246 | $address_type = 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
247 | 247 | |
248 | - foreach ( $field['fields'] as $address_field ) { |
|
248 | + foreach ( $field['fields'] as $address_field ) { |
|
249 | 249 | |
250 | - if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { |
|
251 | - wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
252 | - } |
|
250 | + if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { |
|
251 | + wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
252 | + } |
|
253 | 253 | |
254 | - } |
|
254 | + } |
|
255 | 255 | |
256 | 256 | } |
257 | 257 | |
@@ -263,11 +263,11 @@ discard block |
||
263 | 263 | // Handle misc fields. |
264 | 264 | if ( isset( $data[ $field['id'] ] ) ) { |
265 | 265 | |
266 | - if ( $field['type'] == 'checkbox' ) { |
|
267 | - $value = isset( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
268 | - } else { |
|
269 | - $value = wp_kses_post( $data[ $field['id'] ] ); |
|
270 | - } |
|
266 | + if ( $field['type'] == 'checkbox' ) { |
|
267 | + $value = isset( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
268 | + } else { |
|
269 | + $value = wp_kses_post( $data[ $field['id'] ] ); |
|
270 | + } |
|
271 | 271 | |
272 | 272 | $label = $field['id']; |
273 | 273 | |
@@ -275,189 +275,189 @@ discard block |
||
275 | 275 | $label = $field['label']; |
276 | 276 | } |
277 | 277 | |
278 | - if ( ! empty( $field['add_meta'] ) ) { |
|
279 | - $prepared['meta'][ wpinv_clean( $label ) ] = $value; |
|
280 | - } |
|
281 | - $prepared['all'][ wpinv_clean( $label ) ] = $value; |
|
278 | + if ( ! empty( $field['add_meta'] ) ) { |
|
279 | + $prepared['meta'][ wpinv_clean( $label ) ] = $value; |
|
280 | + } |
|
281 | + $prepared['all'][ wpinv_clean( $label ) ] = $value; |
|
282 | 282 | |
283 | 283 | } |
284 | 284 | |
285 | - } |
|
285 | + } |
|
286 | 286 | |
287 | - return $prepared; |
|
287 | + return $prepared; |
|
288 | 288 | |
289 | - } |
|
289 | + } |
|
290 | 290 | |
291 | - /** |
|
291 | + /** |
|
292 | 292 | * Retrieves address details. |
293 | 293 | * |
294 | - * @return array |
|
295 | - * @param WPInv_Invoice $invoice |
|
296 | - * @param string $type |
|
294 | + * @return array |
|
295 | + * @param WPInv_Invoice $invoice |
|
296 | + * @param string $type |
|
297 | 297 | */ |
298 | 298 | public function prepare_address_details( $invoice, $type = 'billing' ) { |
299 | 299 | |
300 | - $data = $this->payment_form_submission->get_data(); |
|
301 | - $type = sanitize_key( $type ); |
|
302 | - $address = array(); |
|
303 | - $prepared = array(); |
|
300 | + $data = $this->payment_form_submission->get_data(); |
|
301 | + $type = sanitize_key( $type ); |
|
302 | + $address = array(); |
|
303 | + $prepared = array(); |
|
304 | 304 | |
305 | - if ( ! empty( $data[ $type ] ) ) { |
|
306 | - $address = $data[ $type ]; |
|
307 | - } |
|
305 | + if ( ! empty( $data[ $type ] ) ) { |
|
306 | + $address = $data[ $type ]; |
|
307 | + } |
|
308 | 308 | |
309 | - // Clean address details. |
|
310 | - foreach ( $address as $key => $value ) { |
|
311 | - $key = sanitize_key( $key ); |
|
312 | - $key = str_replace( 'wpinv_', '', $key ); |
|
313 | - $value = wpinv_clean( $value ); |
|
314 | - $prepared[ $key] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
315 | - } |
|
309 | + // Clean address details. |
|
310 | + foreach ( $address as $key => $value ) { |
|
311 | + $key = sanitize_key( $key ); |
|
312 | + $key = str_replace( 'wpinv_', '', $key ); |
|
313 | + $value = wpinv_clean( $value ); |
|
314 | + $prepared[ $key] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
315 | + } |
|
316 | 316 | |
317 | - // Filter address details. |
|
318 | - $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
317 | + // Filter address details. |
|
318 | + $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
319 | 319 | |
320 | - // Remove non-whitelisted values. |
|
321 | - return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
320 | + // Remove non-whitelisted values. |
|
321 | + return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
322 | 322 | |
323 | - } |
|
323 | + } |
|
324 | 324 | |
325 | - /** |
|
325 | + /** |
|
326 | 326 | * Prepares the billing details. |
327 | 327 | * |
328 | - * @return array |
|
329 | - * @param WPInv_Invoice $invoice |
|
328 | + * @return array |
|
329 | + * @param WPInv_Invoice $invoice |
|
330 | 330 | */ |
331 | 331 | protected function prepare_billing_info( &$invoice ) { |
332 | 332 | |
333 | - $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
333 | + $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
334 | 334 | |
335 | - // Update the invoice with the billing details. |
|
336 | - $invoice->set_props( $billing_address ); |
|
335 | + // Update the invoice with the billing details. |
|
336 | + $invoice->set_props( $billing_address ); |
|
337 | 337 | |
338 | - } |
|
338 | + } |
|
339 | 339 | |
340 | - /** |
|
340 | + /** |
|
341 | 341 | * Prepares the shipping details. |
342 | 342 | * |
343 | - * @return array |
|
344 | - * @param WPInv_Invoice $invoice |
|
343 | + * @return array |
|
344 | + * @param WPInv_Invoice $invoice |
|
345 | 345 | */ |
346 | 346 | protected function prepare_shipping_info( $invoice ) { |
347 | 347 | |
348 | - $data = $this->payment_form_submission->get_data(); |
|
348 | + $data = $this->payment_form_submission->get_data(); |
|
349 | 349 | |
350 | - if ( empty( $data['same-shipping-address'] ) ) { |
|
351 | - return $this->prepare_address_details( $invoice, 'shipping' ); |
|
352 | - } |
|
350 | + if ( empty( $data['same-shipping-address'] ) ) { |
|
351 | + return $this->prepare_address_details( $invoice, 'shipping' ); |
|
352 | + } |
|
353 | 353 | |
354 | - return $this->prepare_address_details( $invoice, 'billing' ); |
|
354 | + return $this->prepare_address_details( $invoice, 'billing' ); |
|
355 | 355 | |
356 | - } |
|
356 | + } |
|
357 | 357 | |
358 | - /** |
|
359 | - * Confirms the submission is valid and send users to the gateway. |
|
360 | - * |
|
361 | - * @param WPInv_Invoice $invoice |
|
362 | - * @param array $prepared_payment_form_data |
|
363 | - * @param array $shipping |
|
364 | - */ |
|
365 | - protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
358 | + /** |
|
359 | + * Confirms the submission is valid and send users to the gateway. |
|
360 | + * |
|
361 | + * @param WPInv_Invoice $invoice |
|
362 | + * @param array $prepared_payment_form_data |
|
363 | + * @param array $shipping |
|
364 | + */ |
|
365 | + protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
366 | 366 | |
367 | - // Ensure the invoice exists. |
|
367 | + // Ensure the invoice exists. |
|
368 | 368 | if ( ! $invoice->exists() ) { |
369 | 369 | wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
370 | 370 | } |
371 | 371 | |
372 | - // Save payment form data. |
|
373 | - $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
372 | + // Save payment form data. |
|
373 | + $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
374 | 374 | delete_post_meta( $invoice->get_id(), 'payment_form_data' ); |
375 | - delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
376 | - if ( ! empty( $prepared_payment_form_data ) ) { |
|
375 | + delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
376 | + if ( ! empty( $prepared_payment_form_data ) ) { |
|
377 | 377 | |
378 | - if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
379 | - update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
380 | - } |
|
378 | + if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
379 | + update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
380 | + } |
|
381 | 381 | |
382 | - if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
383 | - update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
384 | - } |
|
382 | + if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
383 | + update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
384 | + } |
|
385 | 385 | |
386 | - } |
|
386 | + } |
|
387 | 387 | |
388 | - // Save payment form data. |
|
388 | + // Save payment form data. |
|
389 | 389 | if ( ! empty( $shipping ) ) { |
390 | 390 | update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
391 | - } |
|
391 | + } |
|
392 | 392 | |
393 | - // Backwards compatibility. |
|
393 | + // Backwards compatibility. |
|
394 | 394 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
395 | 395 | |
396 | - $this->process_payment( $invoice ); |
|
396 | + $this->process_payment( $invoice ); |
|
397 | 397 | |
398 | 398 | // If we are here, there was an error. |
399 | - wpinv_send_back_to_checkout( $invoice ); |
|
399 | + wpinv_send_back_to_checkout( $invoice ); |
|
400 | 400 | |
401 | - } |
|
401 | + } |
|
402 | 402 | |
403 | - /** |
|
404 | - * Processes the actual payment. |
|
405 | - * |
|
406 | - * @param WPInv_Invoice $invoice |
|
407 | - */ |
|
408 | - protected function process_payment( $invoice ) { |
|
403 | + /** |
|
404 | + * Processes the actual payment. |
|
405 | + * |
|
406 | + * @param WPInv_Invoice $invoice |
|
407 | + */ |
|
408 | + protected function process_payment( $invoice ) { |
|
409 | 409 | |
410 | - // Clear any checkout errors. |
|
411 | - wpinv_clear_errors(); |
|
410 | + // Clear any checkout errors. |
|
411 | + wpinv_clear_errors(); |
|
412 | 412 | |
413 | - // No need to send free invoices to the gateway. |
|
414 | - if ( $invoice->is_free() ) { |
|
415 | - $this->process_free_payment( $invoice ); |
|
416 | - } |
|
413 | + // No need to send free invoices to the gateway. |
|
414 | + if ( $invoice->is_free() ) { |
|
415 | + $this->process_free_payment( $invoice ); |
|
416 | + } |
|
417 | 417 | |
418 | - $submission = $this->payment_form_submission; |
|
418 | + $submission = $this->payment_form_submission; |
|
419 | 419 | |
420 | - // Fires before sending to the gateway. |
|
421 | - do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
420 | + // Fires before sending to the gateway. |
|
421 | + do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
422 | 422 | |
423 | - // Allow the sumission data to be modified before it is sent to the gateway. |
|
424 | - $submission_data = $submission->get_data(); |
|
425 | - $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
426 | - $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
423 | + // Allow the sumission data to be modified before it is sent to the gateway. |
|
424 | + $submission_data = $submission->get_data(); |
|
425 | + $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
426 | + $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
427 | 427 | |
428 | - // Validate the currency. |
|
429 | - if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
430 | - wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support this currency', 'invoicing' ) ); |
|
431 | - } |
|
428 | + // Validate the currency. |
|
429 | + if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
430 | + wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support this currency', 'invoicing' ) ); |
|
431 | + } |
|
432 | 432 | |
433 | - // Check to see if we have any errors. |
|
434 | - if ( wpinv_get_errors() ) { |
|
435 | - wpinv_send_back_to_checkout( $invoice ); |
|
436 | - } |
|
433 | + // Check to see if we have any errors. |
|
434 | + if ( wpinv_get_errors() ) { |
|
435 | + wpinv_send_back_to_checkout( $invoice ); |
|
436 | + } |
|
437 | 437 | |
438 | - // Send info to the gateway for payment processing |
|
439 | - do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
438 | + // Send info to the gateway for payment processing |
|
439 | + do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
440 | 440 | |
441 | - // Backwards compatibility. |
|
442 | - wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
441 | + // Backwards compatibility. |
|
442 | + wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
443 | 443 | |
444 | - } |
|
444 | + } |
|
445 | 445 | |
446 | - /** |
|
447 | - * Marks the invoice as paid in case the checkout is free. |
|
448 | - * |
|
449 | - * @param WPInv_Invoice $invoice |
|
450 | - */ |
|
451 | - protected function process_free_payment( $invoice ) { |
|
446 | + /** |
|
447 | + * Marks the invoice as paid in case the checkout is free. |
|
448 | + * |
|
449 | + * @param WPInv_Invoice $invoice |
|
450 | + */ |
|
451 | + protected function process_free_payment( $invoice ) { |
|
452 | 452 | |
453 | - $invoice->set_gateway( 'none' ); |
|
454 | - $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
455 | - $invoice->mark_paid(); |
|
456 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
453 | + $invoice->set_gateway( 'none' ); |
|
454 | + $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
455 | + $invoice->mark_paid(); |
|
456 | + wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
457 | 457 | |
458 | - } |
|
458 | + } |
|
459 | 459 | |
460 | - /** |
|
460 | + /** |
|
461 | 461 | * Sends a redrect response to payment details. |
462 | 462 | * |
463 | 463 | */ |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | if ( ! defined( 'ABSPATH' ) ) { |
3 | - exit; |
|
3 | + exit; |
|
4 | 4 | } |
5 | 5 | |
6 | 6 | /** |
@@ -10,67 +10,67 @@ discard block |
||
10 | 10 | class GetPaid_Form_Item extends WPInv_Item { |
11 | 11 | |
12 | 12 | /** |
13 | - * Stores a custom description for the item. |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - protected $custom_description = null; |
|
18 | - |
|
19 | - /** |
|
20 | - * Stores the item quantity. |
|
21 | - * |
|
22 | - * @var float |
|
23 | - */ |
|
24 | - protected $quantity = 1; |
|
25 | - |
|
26 | - /** |
|
27 | - * Stores the item meta. |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - protected $meta = array(); |
|
32 | - |
|
33 | - /** |
|
34 | - * Is this item required? |
|
35 | - * |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - protected $is_required = true; |
|
39 | - |
|
40 | - /** |
|
41 | - * Are quantities allowed? |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - protected $allow_quantities = false; |
|
46 | - |
|
47 | - /** |
|
48 | - * Associated invoice. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - public $invoice_id = 0; |
|
53 | - |
|
54 | - /** |
|
55 | - * Item discount. |
|
56 | - * |
|
57 | - * @var float |
|
58 | - */ |
|
59 | - public $item_discount = 0; |
|
60 | - |
|
61 | - /** |
|
62 | - * Recurring item discount. |
|
63 | - * |
|
64 | - * @var float |
|
65 | - */ |
|
66 | - public $recurring_item_discount = 0; |
|
67 | - |
|
68 | - /** |
|
69 | - * Item tax. |
|
70 | - * |
|
71 | - * @var float |
|
72 | - */ |
|
73 | - public $item_tax = 0; |
|
13 | + * Stores a custom description for the item. |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + protected $custom_description = null; |
|
18 | + |
|
19 | + /** |
|
20 | + * Stores the item quantity. |
|
21 | + * |
|
22 | + * @var float |
|
23 | + */ |
|
24 | + protected $quantity = 1; |
|
25 | + |
|
26 | + /** |
|
27 | + * Stores the item meta. |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + protected $meta = array(); |
|
32 | + |
|
33 | + /** |
|
34 | + * Is this item required? |
|
35 | + * |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + protected $is_required = true; |
|
39 | + |
|
40 | + /** |
|
41 | + * Are quantities allowed? |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + protected $allow_quantities = false; |
|
46 | + |
|
47 | + /** |
|
48 | + * Associated invoice. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + public $invoice_id = 0; |
|
53 | + |
|
54 | + /** |
|
55 | + * Item discount. |
|
56 | + * |
|
57 | + * @var float |
|
58 | + */ |
|
59 | + public $item_discount = 0; |
|
60 | + |
|
61 | + /** |
|
62 | + * Recurring item discount. |
|
63 | + * |
|
64 | + * @var float |
|
65 | + */ |
|
66 | + public $recurring_item_discount = 0; |
|
67 | + |
|
68 | + /** |
|
69 | + * Item tax. |
|
70 | + * |
|
71 | + * @var float |
|
72 | + */ |
|
73 | + public $item_tax = 0; |
|
74 | 74 | |
75 | 75 | /* |
76 | 76 | |-------------------------------------------------------------------------- |
@@ -88,230 +88,230 @@ discard block |
||
88 | 88 | */ |
89 | 89 | |
90 | 90 | /** |
91 | - * Get the item name. |
|
92 | - * |
|
93 | - * @since 1.0.19 |
|
94 | - * @param string $context View or edit context. |
|
95 | - * @return string |
|
96 | - */ |
|
97 | - public function get_name( $context = 'view' ) { |
|
98 | - $name = parent::get_name( $context ); |
|
99 | - return $name . wpinv_get_item_suffix( $this ); |
|
100 | - } |
|
101 | - |
|
102 | - /** |
|
103 | - * Get the item name without a suffix. |
|
104 | - * |
|
105 | - * @since 1.0.19 |
|
106 | - * @param string $context View or edit context. |
|
107 | - * @return string |
|
108 | - */ |
|
109 | - public function get_raw_name( $context = 'view' ) { |
|
110 | - return parent::get_name( $context ); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get the item description. |
|
115 | - * |
|
116 | - * @since 1.0.19 |
|
117 | - * @param string $context View or edit context. |
|
118 | - * @return string |
|
119 | - */ |
|
120 | - public function get_description( $context = 'view' ) { |
|
121 | - |
|
122 | - if ( isset( $this->custom_description ) ) { |
|
123 | - return $this->custom_description; |
|
124 | - } |
|
125 | - |
|
126 | - return parent::get_description( $context ); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Returns the sub total. |
|
131 | - * |
|
132 | - * @since 1.0.19 |
|
133 | - * @param string $context View or edit context. |
|
134 | - * @return float |
|
135 | - */ |
|
136 | - public function get_sub_total( $context = 'view' ) { |
|
137 | - return $this->get_quantity( $context ) * $this->get_initial_price( $context ); |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Returns the recurring sub total. |
|
142 | - * |
|
143 | - * @since 1.0.19 |
|
144 | - * @param string $context View or edit context. |
|
145 | - * @return float |
|
146 | - */ |
|
147 | - public function get_recurring_sub_total( $context = 'view' ) { |
|
148 | - |
|
149 | - if ( $this->is_recurring() ) { |
|
150 | - return $this->get_quantity( $context ) * $this->get_price( $context ); |
|
151 | - } |
|
152 | - |
|
153 | - return 0; |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @deprecated |
|
158 | - */ |
|
159 | - public function get_qantity( $context = 'view' ) { |
|
160 | - return $this->get_quantity( $context ); |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Get the item quantity. |
|
165 | - * |
|
166 | - * @since 1.0.19 |
|
167 | - * @param string $context View or edit context. |
|
168 | - * @return float |
|
169 | - */ |
|
170 | - public function get_quantity( $context = 'view' ) { |
|
171 | - $quantity = (float) $this->quantity; |
|
172 | - |
|
173 | - if ( 'view' == $context ) { |
|
174 | - return apply_filters( 'getpaid_payment_form_item_quantity', $quantity, $this ); |
|
175 | - } |
|
176 | - |
|
177 | - return $quantity; |
|
178 | - |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * Get the item meta data. |
|
183 | - * |
|
184 | - * @since 1.0.19 |
|
185 | - * @param string $context View or edit context. |
|
186 | - * @return meta |
|
187 | - */ |
|
188 | - public function get_item_meta( $context = 'view' ) { |
|
189 | - $meta = $this->meta; |
|
190 | - |
|
191 | - if ( 'view' == $context ) { |
|
192 | - return apply_filters( 'getpaid_payment_form_item_meta', $meta, $this ); |
|
193 | - } |
|
194 | - |
|
195 | - return $meta; |
|
196 | - |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Returns whether or not customers can update the item quantity. |
|
201 | - * |
|
202 | - * @since 1.0.19 |
|
203 | - * @param string $context View or edit context. |
|
204 | - * @return bool |
|
205 | - */ |
|
206 | - public function get_allow_quantities( $context = 'view' ) { |
|
207 | - $allow_quantities = (bool) $this->allow_quantities; |
|
208 | - |
|
209 | - if ( 'view' == $context ) { |
|
210 | - return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this ); |
|
211 | - } |
|
212 | - |
|
213 | - return $allow_quantities; |
|
214 | - |
|
215 | - } |
|
216 | - |
|
217 | - /** |
|
218 | - * Returns whether or not the item is required. |
|
219 | - * |
|
220 | - * @since 1.0.19 |
|
221 | - * @param string $context View or edit context. |
|
222 | - * @return bool |
|
223 | - */ |
|
224 | - public function get_is_required( $context = 'view' ) { |
|
225 | - $is_required = (bool) $this->is_required; |
|
226 | - |
|
227 | - if ( 'view' == $context ) { |
|
228 | - return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this ); |
|
229 | - } |
|
230 | - |
|
231 | - return $is_required; |
|
232 | - |
|
233 | - } |
|
234 | - |
|
235 | - /** |
|
236 | - * Prepares form data for use. |
|
237 | - * |
|
238 | - * @since 1.0.19 |
|
239 | - * @return array |
|
240 | - */ |
|
241 | - public function prepare_data_for_use( $required = null ) { |
|
242 | - |
|
243 | - $required = is_null( $required ) ? $this->is_required() : $required; |
|
244 | - return array( |
|
245 | - 'title' => strip_tags( $this->get_name() ), |
|
246 | - 'id' => $this->get_id(), |
|
247 | - 'price' => $this->get_price(), |
|
248 | - 'recurring' => $this->is_recurring(), |
|
249 | - 'description' => $this->get_description(), |
|
250 | - 'allow_quantities' => $this->allows_quantities(), |
|
251 | - 'required' => $required, |
|
252 | - ); |
|
253 | - |
|
254 | - } |
|
255 | - |
|
256 | - /** |
|
257 | - * Prepares form data for ajax use. |
|
258 | - * |
|
259 | - * @since 1.0.19 |
|
260 | - * @return array |
|
261 | - */ |
|
262 | - public function prepare_data_for_invoice_edit_ajax( $currency = '', $is_renewal = false ) { |
|
263 | - |
|
264 | - $description = getpaid_item_recurring_price_help_text( $this, $currency ); |
|
265 | - |
|
266 | - if ( $description ) { |
|
267 | - $description = "<div class='getpaid-subscription-help-text'>$description</div>"; |
|
268 | - } |
|
269 | - |
|
270 | - $price = ! $is_renewal ? $this->get_price() : $this->get_recurring_price(); |
|
271 | - $subtotal = ! $is_renewal ? $this->get_sub_total() : $this->get_recurring_sub_total(); |
|
272 | - return array( |
|
273 | - 'id' => $this->get_id(), |
|
274 | - 'texts' => array( |
|
275 | - 'item-name' => sanitize_text_field( $this->get_name() ), |
|
276 | - 'item-description' => wp_kses_post( $this->get_description() ) . $description, |
|
277 | - 'item-quantity' => floatval( $this->get_quantity() ), |
|
278 | - 'item-price' => wpinv_price( $price, $currency ), |
|
279 | - 'item-total' => wpinv_price( $subtotal, $currency ), |
|
280 | - ), |
|
281 | - 'inputs' => array( |
|
282 | - 'item-id' => $this->get_id(), |
|
283 | - 'item-name' => sanitize_text_field( $this->get_name() ), |
|
284 | - 'item-description' => wp_kses_post( $this->get_description() ), |
|
285 | - 'item-quantity' => floatval( $this->get_quantity() ), |
|
286 | - 'item-price' => $price, |
|
287 | - ) |
|
288 | - ); |
|
289 | - |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * Prepares form data for saving (cart_details). |
|
294 | - * |
|
295 | - * @since 1.0.19 |
|
296 | - * @return array |
|
297 | - */ |
|
298 | - public function prepare_data_for_saving() { |
|
299 | - |
|
300 | - return array( |
|
301 | - 'post_id' => $this->invoice_id, |
|
302 | - 'item_id' => $this->get_id(), |
|
303 | - 'item_name' => sanitize_text_field( $this->get_raw_name( 'edit' ) ), |
|
304 | - 'item_description' => $this->get_description( 'edit' ), |
|
305 | - 'tax' => $this->item_tax, |
|
306 | - 'item_price' => $this->get_price( 'edit' ), |
|
307 | - 'quantity' => (float) $this->get_quantity( 'edit' ), |
|
308 | - 'discount' => $this->item_discount, |
|
309 | - 'subtotal' => $this->get_sub_total( 'edit' ), |
|
310 | - 'price' => $this->get_sub_total( 'edit' ) + $this->item_tax - $this->item_discount, |
|
311 | - 'meta' => $this->get_item_meta( 'edit' ), |
|
312 | - ); |
|
313 | - |
|
314 | - } |
|
91 | + * Get the item name. |
|
92 | + * |
|
93 | + * @since 1.0.19 |
|
94 | + * @param string $context View or edit context. |
|
95 | + * @return string |
|
96 | + */ |
|
97 | + public function get_name( $context = 'view' ) { |
|
98 | + $name = parent::get_name( $context ); |
|
99 | + return $name . wpinv_get_item_suffix( $this ); |
|
100 | + } |
|
101 | + |
|
102 | + /** |
|
103 | + * Get the item name without a suffix. |
|
104 | + * |
|
105 | + * @since 1.0.19 |
|
106 | + * @param string $context View or edit context. |
|
107 | + * @return string |
|
108 | + */ |
|
109 | + public function get_raw_name( $context = 'view' ) { |
|
110 | + return parent::get_name( $context ); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get the item description. |
|
115 | + * |
|
116 | + * @since 1.0.19 |
|
117 | + * @param string $context View or edit context. |
|
118 | + * @return string |
|
119 | + */ |
|
120 | + public function get_description( $context = 'view' ) { |
|
121 | + |
|
122 | + if ( isset( $this->custom_description ) ) { |
|
123 | + return $this->custom_description; |
|
124 | + } |
|
125 | + |
|
126 | + return parent::get_description( $context ); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Returns the sub total. |
|
131 | + * |
|
132 | + * @since 1.0.19 |
|
133 | + * @param string $context View or edit context. |
|
134 | + * @return float |
|
135 | + */ |
|
136 | + public function get_sub_total( $context = 'view' ) { |
|
137 | + return $this->get_quantity( $context ) * $this->get_initial_price( $context ); |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Returns the recurring sub total. |
|
142 | + * |
|
143 | + * @since 1.0.19 |
|
144 | + * @param string $context View or edit context. |
|
145 | + * @return float |
|
146 | + */ |
|
147 | + public function get_recurring_sub_total( $context = 'view' ) { |
|
148 | + |
|
149 | + if ( $this->is_recurring() ) { |
|
150 | + return $this->get_quantity( $context ) * $this->get_price( $context ); |
|
151 | + } |
|
152 | + |
|
153 | + return 0; |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @deprecated |
|
158 | + */ |
|
159 | + public function get_qantity( $context = 'view' ) { |
|
160 | + return $this->get_quantity( $context ); |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Get the item quantity. |
|
165 | + * |
|
166 | + * @since 1.0.19 |
|
167 | + * @param string $context View or edit context. |
|
168 | + * @return float |
|
169 | + */ |
|
170 | + public function get_quantity( $context = 'view' ) { |
|
171 | + $quantity = (float) $this->quantity; |
|
172 | + |
|
173 | + if ( 'view' == $context ) { |
|
174 | + return apply_filters( 'getpaid_payment_form_item_quantity', $quantity, $this ); |
|
175 | + } |
|
176 | + |
|
177 | + return $quantity; |
|
178 | + |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * Get the item meta data. |
|
183 | + * |
|
184 | + * @since 1.0.19 |
|
185 | + * @param string $context View or edit context. |
|
186 | + * @return meta |
|
187 | + */ |
|
188 | + public function get_item_meta( $context = 'view' ) { |
|
189 | + $meta = $this->meta; |
|
190 | + |
|
191 | + if ( 'view' == $context ) { |
|
192 | + return apply_filters( 'getpaid_payment_form_item_meta', $meta, $this ); |
|
193 | + } |
|
194 | + |
|
195 | + return $meta; |
|
196 | + |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Returns whether or not customers can update the item quantity. |
|
201 | + * |
|
202 | + * @since 1.0.19 |
|
203 | + * @param string $context View or edit context. |
|
204 | + * @return bool |
|
205 | + */ |
|
206 | + public function get_allow_quantities( $context = 'view' ) { |
|
207 | + $allow_quantities = (bool) $this->allow_quantities; |
|
208 | + |
|
209 | + if ( 'view' == $context ) { |
|
210 | + return apply_filters( 'getpaid_payment_form_item_allow_quantities', $allow_quantities, $this ); |
|
211 | + } |
|
212 | + |
|
213 | + return $allow_quantities; |
|
214 | + |
|
215 | + } |
|
216 | + |
|
217 | + /** |
|
218 | + * Returns whether or not the item is required. |
|
219 | + * |
|
220 | + * @since 1.0.19 |
|
221 | + * @param string $context View or edit context. |
|
222 | + * @return bool |
|
223 | + */ |
|
224 | + public function get_is_required( $context = 'view' ) { |
|
225 | + $is_required = (bool) $this->is_required; |
|
226 | + |
|
227 | + if ( 'view' == $context ) { |
|
228 | + return apply_filters( 'getpaid_payment_form_item_is_required', $is_required, $this ); |
|
229 | + } |
|
230 | + |
|
231 | + return $is_required; |
|
232 | + |
|
233 | + } |
|
234 | + |
|
235 | + /** |
|
236 | + * Prepares form data for use. |
|
237 | + * |
|
238 | + * @since 1.0.19 |
|
239 | + * @return array |
|
240 | + */ |
|
241 | + public function prepare_data_for_use( $required = null ) { |
|
242 | + |
|
243 | + $required = is_null( $required ) ? $this->is_required() : $required; |
|
244 | + return array( |
|
245 | + 'title' => strip_tags( $this->get_name() ), |
|
246 | + 'id' => $this->get_id(), |
|
247 | + 'price' => $this->get_price(), |
|
248 | + 'recurring' => $this->is_recurring(), |
|
249 | + 'description' => $this->get_description(), |
|
250 | + 'allow_quantities' => $this->allows_quantities(), |
|
251 | + 'required' => $required, |
|
252 | + ); |
|
253 | + |
|
254 | + } |
|
255 | + |
|
256 | + /** |
|
257 | + * Prepares form data for ajax use. |
|
258 | + * |
|
259 | + * @since 1.0.19 |
|
260 | + * @return array |
|
261 | + */ |
|
262 | + public function prepare_data_for_invoice_edit_ajax( $currency = '', $is_renewal = false ) { |
|
263 | + |
|
264 | + $description = getpaid_item_recurring_price_help_text( $this, $currency ); |
|
265 | + |
|
266 | + if ( $description ) { |
|
267 | + $description = "<div class='getpaid-subscription-help-text'>$description</div>"; |
|
268 | + } |
|
269 | + |
|
270 | + $price = ! $is_renewal ? $this->get_price() : $this->get_recurring_price(); |
|
271 | + $subtotal = ! $is_renewal ? $this->get_sub_total() : $this->get_recurring_sub_total(); |
|
272 | + return array( |
|
273 | + 'id' => $this->get_id(), |
|
274 | + 'texts' => array( |
|
275 | + 'item-name' => sanitize_text_field( $this->get_name() ), |
|
276 | + 'item-description' => wp_kses_post( $this->get_description() ) . $description, |
|
277 | + 'item-quantity' => floatval( $this->get_quantity() ), |
|
278 | + 'item-price' => wpinv_price( $price, $currency ), |
|
279 | + 'item-total' => wpinv_price( $subtotal, $currency ), |
|
280 | + ), |
|
281 | + 'inputs' => array( |
|
282 | + 'item-id' => $this->get_id(), |
|
283 | + 'item-name' => sanitize_text_field( $this->get_name() ), |
|
284 | + 'item-description' => wp_kses_post( $this->get_description() ), |
|
285 | + 'item-quantity' => floatval( $this->get_quantity() ), |
|
286 | + 'item-price' => $price, |
|
287 | + ) |
|
288 | + ); |
|
289 | + |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * Prepares form data for saving (cart_details). |
|
294 | + * |
|
295 | + * @since 1.0.19 |
|
296 | + * @return array |
|
297 | + */ |
|
298 | + public function prepare_data_for_saving() { |
|
299 | + |
|
300 | + return array( |
|
301 | + 'post_id' => $this->invoice_id, |
|
302 | + 'item_id' => $this->get_id(), |
|
303 | + 'item_name' => sanitize_text_field( $this->get_raw_name( 'edit' ) ), |
|
304 | + 'item_description' => $this->get_description( 'edit' ), |
|
305 | + 'tax' => $this->item_tax, |
|
306 | + 'item_price' => $this->get_price( 'edit' ), |
|
307 | + 'quantity' => (float) $this->get_quantity( 'edit' ), |
|
308 | + 'discount' => $this->item_discount, |
|
309 | + 'subtotal' => $this->get_sub_total( 'edit' ), |
|
310 | + 'price' => $this->get_sub_total( 'edit' ) + $this->item_tax - $this->item_discount, |
|
311 | + 'meta' => $this->get_item_meta( 'edit' ), |
|
312 | + ); |
|
313 | + |
|
314 | + } |
|
315 | 315 | |
316 | 316 | /* |
317 | 317 | |-------------------------------------------------------------------------- |
@@ -323,70 +323,70 @@ discard block |
||
323 | 323 | | object. |
324 | 324 | */ |
325 | 325 | |
326 | - /** |
|
327 | - * Set the item qantity. |
|
328 | - * |
|
329 | - * @since 1.0.19 |
|
330 | - * @param float $quantity The item quantity. |
|
331 | - */ |
|
332 | - public function set_quantity( $quantity ) { |
|
333 | - |
|
334 | - if ( ! is_numeric( $quantity ) ) { |
|
335 | - $quantity = 1; |
|
336 | - } |
|
337 | - |
|
338 | - $this->quantity = (float) $quantity; |
|
339 | - |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * Set the item meta data. |
|
344 | - * |
|
345 | - * @since 1.0.19 |
|
346 | - * @param array $meta The item meta data. |
|
347 | - */ |
|
348 | - public function set_item_meta( $meta ) { |
|
349 | - $this->meta = maybe_unserialize( $meta ); |
|
350 | - } |
|
351 | - |
|
352 | - /** |
|
353 | - * Set whether or not the quantities are allowed. |
|
354 | - * |
|
355 | - * @since 1.0.19 |
|
356 | - * @param bool $allow_quantities |
|
357 | - */ |
|
358 | - public function set_allow_quantities( $allow_quantities ) { |
|
359 | - $this->allow_quantities = (bool) $allow_quantities; |
|
360 | - } |
|
361 | - |
|
362 | - /** |
|
363 | - * Set whether or not the item is required. |
|
364 | - * |
|
365 | - * @since 1.0.19 |
|
366 | - * @param bool $is_required |
|
367 | - */ |
|
368 | - public function set_is_required( $is_required ) { |
|
369 | - $this->is_required = (bool) $is_required; |
|
370 | - } |
|
371 | - |
|
372 | - /** |
|
373 | - * Sets the custom item description. |
|
374 | - * |
|
375 | - * @since 1.0.19 |
|
376 | - * @param string $description |
|
377 | - */ |
|
378 | - public function set_custom_description( $description ) { |
|
379 | - $this->custom_description = $description; |
|
380 | - } |
|
326 | + /** |
|
327 | + * Set the item qantity. |
|
328 | + * |
|
329 | + * @since 1.0.19 |
|
330 | + * @param float $quantity The item quantity. |
|
331 | + */ |
|
332 | + public function set_quantity( $quantity ) { |
|
333 | + |
|
334 | + if ( ! is_numeric( $quantity ) ) { |
|
335 | + $quantity = 1; |
|
336 | + } |
|
337 | + |
|
338 | + $this->quantity = (float) $quantity; |
|
339 | + |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * Set the item meta data. |
|
344 | + * |
|
345 | + * @since 1.0.19 |
|
346 | + * @param array $meta The item meta data. |
|
347 | + */ |
|
348 | + public function set_item_meta( $meta ) { |
|
349 | + $this->meta = maybe_unserialize( $meta ); |
|
350 | + } |
|
351 | + |
|
352 | + /** |
|
353 | + * Set whether or not the quantities are allowed. |
|
354 | + * |
|
355 | + * @since 1.0.19 |
|
356 | + * @param bool $allow_quantities |
|
357 | + */ |
|
358 | + public function set_allow_quantities( $allow_quantities ) { |
|
359 | + $this->allow_quantities = (bool) $allow_quantities; |
|
360 | + } |
|
361 | + |
|
362 | + /** |
|
363 | + * Set whether or not the item is required. |
|
364 | + * |
|
365 | + * @since 1.0.19 |
|
366 | + * @param bool $is_required |
|
367 | + */ |
|
368 | + public function set_is_required( $is_required ) { |
|
369 | + $this->is_required = (bool) $is_required; |
|
370 | + } |
|
371 | + |
|
372 | + /** |
|
373 | + * Sets the custom item description. |
|
374 | + * |
|
375 | + * @since 1.0.19 |
|
376 | + * @param string $description |
|
377 | + */ |
|
378 | + public function set_custom_description( $description ) { |
|
379 | + $this->custom_description = $description; |
|
380 | + } |
|
381 | 381 | |
382 | 382 | /** |
383 | 383 | * We do not want to save items to the database. |
384 | 384 | * |
385 | - * @return int item id |
|
385 | + * @return int item id |
|
386 | 386 | */ |
387 | 387 | public function save( $data = array() ) { |
388 | 388 | return $this->get_id(); |
389 | - } |
|
389 | + } |
|
390 | 390 | |
391 | 391 | /* |
392 | 392 | |-------------------------------------------------------------------------- |
@@ -398,23 +398,23 @@ discard block |
||
398 | 398 | */ |
399 | 399 | |
400 | 400 | /** |
401 | - * Checks whether the item has enabled dynamic pricing. |
|
402 | - * |
|
403 | - * @since 1.0.19 |
|
404 | - * @return bool |
|
405 | - */ |
|
406 | - public function is_required() { |
|
401 | + * Checks whether the item has enabled dynamic pricing. |
|
402 | + * |
|
403 | + * @since 1.0.19 |
|
404 | + * @return bool |
|
405 | + */ |
|
406 | + public function is_required() { |
|
407 | 407 | return (bool) $this->get_is_required(); |
408 | - } |
|
409 | - |
|
410 | - /** |
|
411 | - * Checks whether users can edit the quantities. |
|
412 | - * |
|
413 | - * @since 1.0.19 |
|
414 | - * @return bool |
|
415 | - */ |
|
416 | - public function allows_quantities() { |
|
408 | + } |
|
409 | + |
|
410 | + /** |
|
411 | + * Checks whether users can edit the quantities. |
|
412 | + * |
|
413 | + * @since 1.0.19 |
|
414 | + * @return bool |
|
415 | + */ |
|
416 | + public function allows_quantities() { |
|
417 | 417 | return (bool) $this->get_allow_quantities(); |
418 | - } |
|
418 | + } |
|
419 | 419 | |
420 | 420 | } |
@@ -19,16 +19,16 @@ discard block |
||
19 | 19 | |
20 | 20 | // Define constants. |
21 | 21 | if ( ! defined( 'WPINV_PLUGIN_FILE' ) ) { |
22 | - define( 'WPINV_PLUGIN_FILE', __FILE__ ); |
|
22 | + define( 'WPINV_PLUGIN_FILE', __FILE__ ); |
|
23 | 23 | } |
24 | 24 | |
25 | 25 | if ( ! defined( 'WPINV_VERSION' ) ) { |
26 | - define( 'WPINV_VERSION', '2.5.4' ); |
|
26 | + define( 'WPINV_VERSION', '2.5.4' ); |
|
27 | 27 | } |
28 | 28 | |
29 | 29 | // Include the main Invoicing class. |
30 | 30 | if ( ! class_exists( 'WPInv_Plugin', false ) ) { |
31 | - require_once plugin_dir_path( WPINV_PLUGIN_FILE ) . 'includes/class-wpinv.php'; |
|
31 | + require_once plugin_dir_path( WPINV_PLUGIN_FILE ) . 'includes/class-wpinv.php'; |
|
32 | 32 | } |
33 | 33 | |
34 | 34 | /** |
@@ -43,7 +43,7 @@ discard block |
||
43 | 43 | $GLOBALS['invoicing'] = new WPInv_Plugin(); |
44 | 44 | } |
45 | 45 | |
46 | - return $GLOBALS['invoicing']; |
|
46 | + return $GLOBALS['invoicing']; |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |