@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | */ |
9 | 9 | |
10 | 10 | if ( ! defined( 'ABSPATH' ) ) { |
11 | - exit; // Exit if accessed directly |
|
11 | + exit; // Exit if accessed directly |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | /** |
@@ -16,84 +16,84 @@ discard block |
||
16 | 16 | */ |
17 | 17 | class GetPaid_Meta_Box_Invoice_Address { |
18 | 18 | |
19 | - /** |
|
20 | - * Output the metabox. |
|
21 | - * |
|
22 | - * @param WP_Post $post |
|
23 | - */ |
|
24 | - public static function output( $post ) { |
|
25 | - |
|
26 | - // Prepare the invoice. |
|
27 | - $invoice = new WPInv_Invoice( $post ); |
|
28 | - $customer = $invoice->exists() ? $invoice->get_user_id( 'edit' ) : get_current_user_id(); |
|
29 | - $customer = new WP_User( $customer ); |
|
30 | - $display = sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'invoicing' ), $customer->display_name, $customer->user_email ); |
|
31 | - wp_nonce_field( 'getpaid_meta_nonce', 'getpaid_meta_nonce' ); |
|
32 | - |
|
33 | - // Address fields. |
|
34 | - $address_fields = array( |
|
35 | - 'first_name' => array( |
|
36 | - 'label' => __( 'First Name', 'invoicing' ), |
|
37 | - 'type' => 'text', |
|
38 | - ), |
|
39 | - 'last_name' => array( |
|
40 | - 'label' => __( 'Last Name', 'invoicing' ), |
|
41 | - 'type' => 'text', |
|
42 | - ), |
|
43 | - 'company' => array( |
|
44 | - 'label' => __( 'Company', 'invoicing' ), |
|
45 | - 'type' => 'text', |
|
46 | - 'class' => 'getpaid-recalculate-prices-on-change', |
|
47 | - ), |
|
48 | - 'vat_number' => array( |
|
49 | - 'label' => __( 'VAT Number', 'invoicing' ), |
|
50 | - 'type' => 'text', |
|
51 | - ), |
|
52 | - 'address' => array( |
|
53 | - 'label' => __( 'Address', 'invoicing' ), |
|
54 | - 'type' => 'text', |
|
55 | - ), |
|
56 | - 'city' => array( |
|
57 | - 'label' => __( 'City', 'invoicing' ), |
|
58 | - 'type' => 'text', |
|
59 | - ), |
|
60 | - 'country' => array( |
|
61 | - 'label' => __( 'Country', 'invoicing' ), |
|
62 | - 'type' => 'select', |
|
63 | - 'class' => 'getpaid-recalculate-prices-on-change', |
|
64 | - 'options' => wpinv_get_country_list(), |
|
65 | - 'placeholder' => __( 'Choose a country', 'invoicing' ), |
|
66 | - ), |
|
67 | - 'state' => array( |
|
68 | - 'label' => __( 'State', 'invoicing' ), |
|
69 | - 'type' => 'text', |
|
70 | - 'class' => 'getpaid-recalculate-prices-on-change', |
|
71 | - ), |
|
72 | - 'zip' => array( |
|
73 | - 'label' => __( 'Zip', 'invoicing' ), |
|
74 | - 'type' => 'text', |
|
75 | - ), |
|
76 | - 'phone' => array( |
|
77 | - 'label' => __( 'Phone', 'invoicing' ), |
|
78 | - 'type' => 'text', |
|
79 | - ), |
|
80 | - ); |
|
81 | - |
|
82 | - $states = wpinv_get_country_states( $invoice->get_country( 'edit' ) ); |
|
83 | - |
|
84 | - if ( ! empty( $states ) ) { |
|
85 | - $address_fields['state']['type'] = 'select'; |
|
86 | - $address_fields['state']['options'] = $states; |
|
87 | - $address_fields['state']['placeholder'] = __( 'Choose a state', 'invoicing' ); |
|
88 | - } |
|
89 | - |
|
90 | - // Maybe remove the VAT field. |
|
91 | - if ( ! wpinv_use_taxes() ) { |
|
92 | - unset( $address_fields['vat_number'] ); |
|
93 | - } |
|
94 | - |
|
95 | - $address_fields = apply_filters( 'getpaid_admin_edit_invoice_address_fields', $address_fields, $invoice ); |
|
96 | - ?> |
|
19 | + /** |
|
20 | + * Output the metabox. |
|
21 | + * |
|
22 | + * @param WP_Post $post |
|
23 | + */ |
|
24 | + public static function output( $post ) { |
|
25 | + |
|
26 | + // Prepare the invoice. |
|
27 | + $invoice = new WPInv_Invoice( $post ); |
|
28 | + $customer = $invoice->exists() ? $invoice->get_user_id( 'edit' ) : get_current_user_id(); |
|
29 | + $customer = new WP_User( $customer ); |
|
30 | + $display = sprintf( _x( '%1$s (%2$s)', 'user dropdown', 'invoicing' ), $customer->display_name, $customer->user_email ); |
|
31 | + wp_nonce_field( 'getpaid_meta_nonce', 'getpaid_meta_nonce' ); |
|
32 | + |
|
33 | + // Address fields. |
|
34 | + $address_fields = array( |
|
35 | + 'first_name' => array( |
|
36 | + 'label' => __( 'First Name', 'invoicing' ), |
|
37 | + 'type' => 'text', |
|
38 | + ), |
|
39 | + 'last_name' => array( |
|
40 | + 'label' => __( 'Last Name', 'invoicing' ), |
|
41 | + 'type' => 'text', |
|
42 | + ), |
|
43 | + 'company' => array( |
|
44 | + 'label' => __( 'Company', 'invoicing' ), |
|
45 | + 'type' => 'text', |
|
46 | + 'class' => 'getpaid-recalculate-prices-on-change', |
|
47 | + ), |
|
48 | + 'vat_number' => array( |
|
49 | + 'label' => __( 'VAT Number', 'invoicing' ), |
|
50 | + 'type' => 'text', |
|
51 | + ), |
|
52 | + 'address' => array( |
|
53 | + 'label' => __( 'Address', 'invoicing' ), |
|
54 | + 'type' => 'text', |
|
55 | + ), |
|
56 | + 'city' => array( |
|
57 | + 'label' => __( 'City', 'invoicing' ), |
|
58 | + 'type' => 'text', |
|
59 | + ), |
|
60 | + 'country' => array( |
|
61 | + 'label' => __( 'Country', 'invoicing' ), |
|
62 | + 'type' => 'select', |
|
63 | + 'class' => 'getpaid-recalculate-prices-on-change', |
|
64 | + 'options' => wpinv_get_country_list(), |
|
65 | + 'placeholder' => __( 'Choose a country', 'invoicing' ), |
|
66 | + ), |
|
67 | + 'state' => array( |
|
68 | + 'label' => __( 'State', 'invoicing' ), |
|
69 | + 'type' => 'text', |
|
70 | + 'class' => 'getpaid-recalculate-prices-on-change', |
|
71 | + ), |
|
72 | + 'zip' => array( |
|
73 | + 'label' => __( 'Zip', 'invoicing' ), |
|
74 | + 'type' => 'text', |
|
75 | + ), |
|
76 | + 'phone' => array( |
|
77 | + 'label' => __( 'Phone', 'invoicing' ), |
|
78 | + 'type' => 'text', |
|
79 | + ), |
|
80 | + ); |
|
81 | + |
|
82 | + $states = wpinv_get_country_states( $invoice->get_country( 'edit' ) ); |
|
83 | + |
|
84 | + if ( ! empty( $states ) ) { |
|
85 | + $address_fields['state']['type'] = 'select'; |
|
86 | + $address_fields['state']['options'] = $states; |
|
87 | + $address_fields['state']['placeholder'] = __( 'Choose a state', 'invoicing' ); |
|
88 | + } |
|
89 | + |
|
90 | + // Maybe remove the VAT field. |
|
91 | + if ( ! wpinv_use_taxes() ) { |
|
92 | + unset( $address_fields['vat_number'] ); |
|
93 | + } |
|
94 | + |
|
95 | + $address_fields = apply_filters( 'getpaid_admin_edit_invoice_address_fields', $address_fields, $invoice ); |
|
96 | + ?> |
|
97 | 97 | |
98 | 98 | <style> |
99 | 99 | #wpinv-address label { |
@@ -118,19 +118,19 @@ discard block |
||
118 | 118 | <div id="getpaid-invoice-email-wrapper" class="d-none"> |
119 | 119 | <input type="hidden" id="getpaid-invoice-create-new-user" name="wpinv_new_user" value="" /> |
120 | 120 | <?php |
121 | - aui()->input( |
|
122 | - array( |
|
123 | - 'type' => 'text', |
|
124 | - 'id' => 'getpaid-invoice-new-user-email', |
|
125 | - 'name' => 'wpinv_email', |
|
126 | - 'label' => __( 'Email', 'invoicing' ) . '<span class="required">*</span>', |
|
127 | - 'label_type' => 'vertical', |
|
128 | - 'placeholder' => '[email protected]', |
|
129 | - 'class' => 'form-control-sm', |
|
130 | - ), |
|
131 | - true |
|
132 | - ); |
|
133 | - ?> |
|
121 | + aui()->input( |
|
122 | + array( |
|
123 | + 'type' => 'text', |
|
124 | + 'id' => 'getpaid-invoice-new-user-email', |
|
125 | + 'name' => 'wpinv_email', |
|
126 | + 'label' => __( 'Email', 'invoicing' ) . '<span class="required">*</span>', |
|
127 | + 'label_type' => 'vertical', |
|
128 | + 'placeholder' => '[email protected]', |
|
129 | + 'class' => 'form-control-sm', |
|
130 | + ), |
|
131 | + true |
|
132 | + ); |
|
133 | + ?> |
|
134 | 134 | </div> |
135 | 135 | </div> |
136 | 136 | <div class="col-12 col-sm-6 form-group mb-3 mt-sm-4"> |
@@ -154,39 +154,39 @@ discard block |
||
154 | 154 | <div class="col-12 col-sm-6 getpaid-invoice-address-field__<?php echo esc_attr( $key ); ?>--wrapper"> |
155 | 155 | <?php |
156 | 156 | |
157 | - if ( 'select' === $field['type'] ) { |
|
158 | - aui()->select( |
|
159 | - array( |
|
160 | - 'id' => 'wpinv_' . $key, |
|
161 | - 'name' => 'wpinv_' . $key, |
|
162 | - 'label' => $field['label'], |
|
163 | - 'label_type' => 'vertical', |
|
164 | - 'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '', |
|
165 | - 'class' => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ), |
|
166 | - 'value' => $invoice->get( $key, 'edit' ), |
|
167 | - 'options' => $field['options'], |
|
168 | - 'data-allow-clear' => 'false', |
|
169 | - 'select2' => true, |
|
170 | - ), |
|
171 | - true |
|
172 | - ); |
|
173 | - } else { |
|
174 | - aui()->input( |
|
175 | - array( |
|
176 | - 'type' => $field['type'], |
|
177 | - 'id' => 'wpinv_' . $key, |
|
178 | - 'name' => 'wpinv_' . $key, |
|
179 | - 'label' => $field['label'], |
|
180 | - 'label_type' => 'vertical', |
|
181 | - 'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '', |
|
182 | - 'class' => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ), |
|
183 | - 'value' => $invoice->get( $key, 'edit' ), |
|
184 | - ), |
|
185 | - true |
|
186 | - ); |
|
187 | - } |
|
188 | - |
|
189 | - ?> |
|
157 | + if ( 'select' === $field['type'] ) { |
|
158 | + aui()->select( |
|
159 | + array( |
|
160 | + 'id' => 'wpinv_' . $key, |
|
161 | + 'name' => 'wpinv_' . $key, |
|
162 | + 'label' => $field['label'], |
|
163 | + 'label_type' => 'vertical', |
|
164 | + 'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '', |
|
165 | + 'class' => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ), |
|
166 | + 'value' => $invoice->get( $key, 'edit' ), |
|
167 | + 'options' => $field['options'], |
|
168 | + 'data-allow-clear' => 'false', |
|
169 | + 'select2' => true, |
|
170 | + ), |
|
171 | + true |
|
172 | + ); |
|
173 | + } else { |
|
174 | + aui()->input( |
|
175 | + array( |
|
176 | + 'type' => $field['type'], |
|
177 | + 'id' => 'wpinv_' . $key, |
|
178 | + 'name' => 'wpinv_' . $key, |
|
179 | + 'label' => $field['label'], |
|
180 | + 'label_type' => 'vertical', |
|
181 | + 'placeholder' => isset( $field['placeholder'] ) ? $field['placeholder'] : '', |
|
182 | + 'class' => 'form-control-sm ' . ( isset( $field['class'] ) ? $field['class'] : '' ), |
|
183 | + 'value' => $invoice->get( $key, 'edit' ), |
|
184 | + ), |
|
185 | + true |
|
186 | + ); |
|
187 | + } |
|
188 | + |
|
189 | + ?> |
|
190 | 190 | </div> |
191 | 191 | <?php endforeach; ?> |
192 | 192 | </div> |
@@ -197,48 +197,48 @@ discard block |
||
197 | 197 | <div class="row"> |
198 | 198 | <div class="col-12 col-sm-6"> |
199 | 199 | <?php |
200 | - aui()->select( |
|
201 | - array( |
|
202 | - 'id' => 'wpinv_template', |
|
203 | - 'name' => 'wpinv_template', |
|
204 | - 'label' => __( 'Template', 'invoicing' ), |
|
205 | - 'label_type' => 'vertical', |
|
206 | - 'placeholder' => __( 'Choose a template', 'invoicing' ), |
|
207 | - 'class' => 'form-control-sm', |
|
208 | - 'value' => $invoice->get_template( 'edit' ), |
|
209 | - 'options' => array( |
|
210 | - 'quantity' => __( 'Quantity', 'invoicing' ), |
|
211 | - 'hours' => __( 'Hours', 'invoicing' ), |
|
212 | - ), |
|
213 | - 'data-allow-clear' => 'false', |
|
214 | - 'select2' => true, |
|
215 | - ), |
|
216 | - true |
|
217 | - ); |
|
218 | - ?> |
|
200 | + aui()->select( |
|
201 | + array( |
|
202 | + 'id' => 'wpinv_template', |
|
203 | + 'name' => 'wpinv_template', |
|
204 | + 'label' => __( 'Template', 'invoicing' ), |
|
205 | + 'label_type' => 'vertical', |
|
206 | + 'placeholder' => __( 'Choose a template', 'invoicing' ), |
|
207 | + 'class' => 'form-control-sm', |
|
208 | + 'value' => $invoice->get_template( 'edit' ), |
|
209 | + 'options' => array( |
|
210 | + 'quantity' => __( 'Quantity', 'invoicing' ), |
|
211 | + 'hours' => __( 'Hours', 'invoicing' ), |
|
212 | + ), |
|
213 | + 'data-allow-clear' => 'false', |
|
214 | + 'select2' => true, |
|
215 | + ), |
|
216 | + true |
|
217 | + ); |
|
218 | + ?> |
|
219 | 219 | </div> |
220 | 220 | <div class="col-12 col-sm-6"> |
221 | 221 | <?php |
222 | 222 | |
223 | - // Set currency. |
|
224 | - aui()->select( |
|
225 | - array( |
|
226 | - 'id' => 'wpinv_currency', |
|
227 | - 'name' => 'wpinv_currency', |
|
228 | - 'label' => __( 'Currency', 'invoicing' ), |
|
229 | - 'label_type' => 'vertical', |
|
230 | - 'placeholder' => __( 'Select Invoice Currency', 'invoicing' ), |
|
231 | - 'class' => 'form-control-sm getpaid-recalculate-prices-on-change', |
|
232 | - 'value' => $invoice->get_currency( 'edit' ), |
|
233 | - 'required' => false, |
|
234 | - 'data-allow-clear' => 'false', |
|
235 | - 'select2' => true, |
|
236 | - 'options' => wpinv_get_currencies(), |
|
237 | - ), |
|
238 | - true |
|
239 | - ); |
|
240 | - |
|
241 | - ?> |
|
223 | + // Set currency. |
|
224 | + aui()->select( |
|
225 | + array( |
|
226 | + 'id' => 'wpinv_currency', |
|
227 | + 'name' => 'wpinv_currency', |
|
228 | + 'label' => __( 'Currency', 'invoicing' ), |
|
229 | + 'label_type' => 'vertical', |
|
230 | + 'placeholder' => __( 'Select Invoice Currency', 'invoicing' ), |
|
231 | + 'class' => 'form-control-sm getpaid-recalculate-prices-on-change', |
|
232 | + 'value' => $invoice->get_currency( 'edit' ), |
|
233 | + 'required' => false, |
|
234 | + 'data-allow-clear' => 'false', |
|
235 | + 'select2' => true, |
|
236 | + 'options' => wpinv_get_currencies(), |
|
237 | + ), |
|
238 | + true |
|
239 | + ); |
|
240 | + |
|
241 | + ?> |
|
242 | 242 | </div> |
243 | 243 | </div> |
244 | 244 | |
@@ -248,123 +248,123 @@ discard block |
||
248 | 248 | <div class="row"> |
249 | 249 | <div class="col-12 col-sm-6"> |
250 | 250 | <?php |
251 | - aui()->input( |
|
252 | - array( |
|
253 | - 'type' => 'text', |
|
254 | - 'id' => 'wpinv_company_id', |
|
255 | - 'name' => 'wpinv_company_id', |
|
256 | - 'label' => __( 'Company ID', 'invoicing' ), |
|
257 | - 'label_type' => 'vertical', |
|
258 | - 'placeholder' => '', |
|
259 | - 'class' => 'form-control-sm', |
|
260 | - 'value' => $invoice->get_company_id( 'edit' ), |
|
261 | - ), |
|
262 | - true |
|
263 | - ); |
|
264 | - ?> |
|
251 | + aui()->input( |
|
252 | + array( |
|
253 | + 'type' => 'text', |
|
254 | + 'id' => 'wpinv_company_id', |
|
255 | + 'name' => 'wpinv_company_id', |
|
256 | + 'label' => __( 'Company ID', 'invoicing' ), |
|
257 | + 'label_type' => 'vertical', |
|
258 | + 'placeholder' => '', |
|
259 | + 'class' => 'form-control-sm', |
|
260 | + 'value' => $invoice->get_company_id( 'edit' ), |
|
261 | + ), |
|
262 | + true |
|
263 | + ); |
|
264 | + ?> |
|
265 | 265 | </div> |
266 | 266 | </div> |
267 | 267 | |
268 | 268 | <?php do_action( 'getpaid_after_metabox_invoice_address', $invoice ); ?> |
269 | 269 | </div> |
270 | 270 | <?php |
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Save meta box data. |
|
275 | - * |
|
276 | - * @param int $post_id |
|
277 | - * @param array $posted the posted data. |
|
278 | - */ |
|
279 | - public static function save( $post_id, $posted ) { |
|
280 | - |
|
281 | - // Prepare the invoice. |
|
282 | - $invoice = new WPInv_Invoice( $post_id ); |
|
283 | - |
|
284 | - // Load new data. |
|
285 | - $invoice->set_props( |
|
286 | - array( |
|
287 | - 'template' => isset( $posted['wpinv_template'] ) ? wpinv_clean( $posted['wpinv_template'] ) : null, |
|
288 | - 'email_cc' => isset( $posted['wpinv_cc'] ) ? wpinv_clean( $posted['wpinv_cc'] ) : null, |
|
289 | - 'disable_taxes' => ! empty( $posted['disable_taxes'] ), |
|
290 | - 'currency' => isset( $posted['wpinv_currency'] ) ? wpinv_clean( $posted['wpinv_currency'] ) : null, |
|
291 | - 'gateway' => ( $invoice->needs_payment() && isset( $posted['wpinv_gateway'] ) ) ? wpinv_clean( $posted['wpinv_gateway'] ) : null, |
|
292 | - 'address' => isset( $posted['wpinv_address'] ) ? wpinv_clean( $posted['wpinv_address'] ) : null, |
|
293 | - 'vat_number' => isset( $posted['wpinv_vat_number'] ) ? wpinv_clean( $posted['wpinv_vat_number'] ) : null, |
|
294 | - 'company' => isset( $posted['wpinv_company'] ) ? wpinv_clean( $posted['wpinv_company'] ) : null, |
|
295 | - 'company_id' => isset( $posted['wpinv_company_id'] ) ? wpinv_clean( $posted['wpinv_company_id'] ) : null, |
|
296 | - 'zip' => isset( $posted['wpinv_zip'] ) ? wpinv_clean( $posted['wpinv_zip'] ) : null, |
|
297 | - 'state' => isset( $posted['wpinv_state'] ) ? wpinv_clean( $posted['wpinv_state'] ) : null, |
|
298 | - 'city' => isset( $posted['wpinv_city'] ) ? wpinv_clean( $posted['wpinv_city'] ) : null, |
|
299 | - 'country' => isset( $posted['wpinv_country'] ) ? wpinv_clean( $posted['wpinv_country'] ) : null, |
|
300 | - 'phone' => isset( $posted['wpinv_phone'] ) ? wpinv_clean( $posted['wpinv_phone'] ) : null, |
|
301 | - 'first_name' => isset( $posted['wpinv_first_name'] ) ? wpinv_clean( $posted['wpinv_first_name'] ) : null, |
|
302 | - 'last_name' => isset( $posted['wpinv_last_name'] ) ? wpinv_clean( $posted['wpinv_last_name'] ) : null, |
|
303 | - 'author' => isset( $posted['post_author_override'] ) ? wpinv_clean( $posted['post_author_override'] ) : null, |
|
304 | - 'date_created' => isset( $posted['date_created'] ) ? wpinv_clean( $posted['date_created'] ) : null, |
|
305 | - 'date_completed' => isset( $posted['wpinv_date_completed'] ) ? wpinv_clean( $posted['wpinv_date_completed'] ) : null, |
|
306 | - 'due_date' => isset( $posted['wpinv_due_date'] ) ? wpinv_clean( $posted['wpinv_due_date'] ) : null, |
|
307 | - 'number' => isset( $posted['wpinv_number'] ) ? wpinv_clean( $posted['wpinv_number'] ) : null, |
|
308 | - 'status' => isset( $posted['wpinv_status'] ) ? wpinv_clean( $posted['wpinv_status'] ) : null, |
|
309 | - ) |
|
310 | - ); |
|
311 | - |
|
312 | - // Discount code. |
|
313 | - if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
|
314 | - |
|
315 | - if ( isset( $posted['wpinv_discount_code'] ) ) { |
|
316 | - $invoice->set_discount_code( wpinv_clean( $posted['wpinv_discount_code'] ) ); |
|
317 | - } |
|
318 | - |
|
319 | - $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
|
320 | - if ( $discount->exists() ) { |
|
321 | - $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
|
322 | - } else { |
|
323 | - $invoice->remove_discount( 'discount_code' ); |
|
324 | - } |
|
325 | - |
|
326 | - // Recalculate totals. |
|
327 | - $invoice->recalculate_total(); |
|
328 | - |
|
329 | - } |
|
330 | - |
|
331 | - // If we're creating a new user... |
|
332 | - if ( ! empty( $posted['wpinv_new_user'] ) && is_email( stripslashes( $posted['wpinv_email'] ) ) ) { |
|
333 | - |
|
334 | - // Attempt to create the user. |
|
335 | - $user = wpinv_create_user( sanitize_email( stripslashes( $posted['wpinv_email'] ) ), $invoice->get_first_name() . $invoice->get_last_name() ); |
|
336 | - |
|
337 | - // If successful, update the invoice author. |
|
338 | - if ( is_numeric( $user ) ) { |
|
339 | - $invoice->set_author( $user ); |
|
340 | - } else { |
|
341 | - wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ ); |
|
342 | - } |
|
343 | - } |
|
344 | - |
|
345 | - // Do not send new invoice notifications. |
|
346 | - $GLOBALS['wpinv_skip_invoice_notification'] = true; |
|
347 | - |
|
348 | - // Save the invoice. |
|
349 | - $invoice->save(); |
|
350 | - |
|
351 | - // Save the user address. |
|
352 | - getpaid_save_invoice_user_address( $invoice ); |
|
353 | - |
|
354 | - // Undo do not send new invoice notifications. |
|
355 | - $GLOBALS['wpinv_skip_invoice_notification'] = false; |
|
356 | - |
|
357 | - // (Maybe) send new user notification. |
|
358 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
359 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) { |
|
360 | - wp_send_new_user_notifications( $user, 'user' ); |
|
361 | - } |
|
362 | - |
|
363 | - if ( ! empty( $posted['send_to_customer'] ) && ! $invoice->is_draft() ) { |
|
364 | - getpaid()->get( 'invoice_emails' )->user_invoice( $invoice, true ); |
|
365 | - } |
|
366 | - |
|
367 | - // Fires after an invoice is saved. |
|
368 | - do_action( 'wpinv_invoice_metabox_saved', $invoice ); |
|
369 | - } |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Save meta box data. |
|
275 | + * |
|
276 | + * @param int $post_id |
|
277 | + * @param array $posted the posted data. |
|
278 | + */ |
|
279 | + public static function save( $post_id, $posted ) { |
|
280 | + |
|
281 | + // Prepare the invoice. |
|
282 | + $invoice = new WPInv_Invoice( $post_id ); |
|
283 | + |
|
284 | + // Load new data. |
|
285 | + $invoice->set_props( |
|
286 | + array( |
|
287 | + 'template' => isset( $posted['wpinv_template'] ) ? wpinv_clean( $posted['wpinv_template'] ) : null, |
|
288 | + 'email_cc' => isset( $posted['wpinv_cc'] ) ? wpinv_clean( $posted['wpinv_cc'] ) : null, |
|
289 | + 'disable_taxes' => ! empty( $posted['disable_taxes'] ), |
|
290 | + 'currency' => isset( $posted['wpinv_currency'] ) ? wpinv_clean( $posted['wpinv_currency'] ) : null, |
|
291 | + 'gateway' => ( $invoice->needs_payment() && isset( $posted['wpinv_gateway'] ) ) ? wpinv_clean( $posted['wpinv_gateway'] ) : null, |
|
292 | + 'address' => isset( $posted['wpinv_address'] ) ? wpinv_clean( $posted['wpinv_address'] ) : null, |
|
293 | + 'vat_number' => isset( $posted['wpinv_vat_number'] ) ? wpinv_clean( $posted['wpinv_vat_number'] ) : null, |
|
294 | + 'company' => isset( $posted['wpinv_company'] ) ? wpinv_clean( $posted['wpinv_company'] ) : null, |
|
295 | + 'company_id' => isset( $posted['wpinv_company_id'] ) ? wpinv_clean( $posted['wpinv_company_id'] ) : null, |
|
296 | + 'zip' => isset( $posted['wpinv_zip'] ) ? wpinv_clean( $posted['wpinv_zip'] ) : null, |
|
297 | + 'state' => isset( $posted['wpinv_state'] ) ? wpinv_clean( $posted['wpinv_state'] ) : null, |
|
298 | + 'city' => isset( $posted['wpinv_city'] ) ? wpinv_clean( $posted['wpinv_city'] ) : null, |
|
299 | + 'country' => isset( $posted['wpinv_country'] ) ? wpinv_clean( $posted['wpinv_country'] ) : null, |
|
300 | + 'phone' => isset( $posted['wpinv_phone'] ) ? wpinv_clean( $posted['wpinv_phone'] ) : null, |
|
301 | + 'first_name' => isset( $posted['wpinv_first_name'] ) ? wpinv_clean( $posted['wpinv_first_name'] ) : null, |
|
302 | + 'last_name' => isset( $posted['wpinv_last_name'] ) ? wpinv_clean( $posted['wpinv_last_name'] ) : null, |
|
303 | + 'author' => isset( $posted['post_author_override'] ) ? wpinv_clean( $posted['post_author_override'] ) : null, |
|
304 | + 'date_created' => isset( $posted['date_created'] ) ? wpinv_clean( $posted['date_created'] ) : null, |
|
305 | + 'date_completed' => isset( $posted['wpinv_date_completed'] ) ? wpinv_clean( $posted['wpinv_date_completed'] ) : null, |
|
306 | + 'due_date' => isset( $posted['wpinv_due_date'] ) ? wpinv_clean( $posted['wpinv_due_date'] ) : null, |
|
307 | + 'number' => isset( $posted['wpinv_number'] ) ? wpinv_clean( $posted['wpinv_number'] ) : null, |
|
308 | + 'status' => isset( $posted['wpinv_status'] ) ? wpinv_clean( $posted['wpinv_status'] ) : null, |
|
309 | + ) |
|
310 | + ); |
|
311 | + |
|
312 | + // Discount code. |
|
313 | + if ( ! $invoice->is_paid() && ! $invoice->is_refunded() ) { |
|
314 | + |
|
315 | + if ( isset( $posted['wpinv_discount_code'] ) ) { |
|
316 | + $invoice->set_discount_code( wpinv_clean( $posted['wpinv_discount_code'] ) ); |
|
317 | + } |
|
318 | + |
|
319 | + $discount = new WPInv_Discount( $invoice->get_discount_code() ); |
|
320 | + if ( $discount->exists() ) { |
|
321 | + $invoice->add_discount( getpaid_calculate_invoice_discount( $invoice, $discount ) ); |
|
322 | + } else { |
|
323 | + $invoice->remove_discount( 'discount_code' ); |
|
324 | + } |
|
325 | + |
|
326 | + // Recalculate totals. |
|
327 | + $invoice->recalculate_total(); |
|
328 | + |
|
329 | + } |
|
330 | + |
|
331 | + // If we're creating a new user... |
|
332 | + if ( ! empty( $posted['wpinv_new_user'] ) && is_email( stripslashes( $posted['wpinv_email'] ) ) ) { |
|
333 | + |
|
334 | + // Attempt to create the user. |
|
335 | + $user = wpinv_create_user( sanitize_email( stripslashes( $posted['wpinv_email'] ) ), $invoice->get_first_name() . $invoice->get_last_name() ); |
|
336 | + |
|
337 | + // If successful, update the invoice author. |
|
338 | + if ( is_numeric( $user ) ) { |
|
339 | + $invoice->set_author( $user ); |
|
340 | + } else { |
|
341 | + wpinv_error_log( $user->get_error_message(), __( 'Invoice add new user', 'invoicing' ), __FILE__, __LINE__ ); |
|
342 | + } |
|
343 | + } |
|
344 | + |
|
345 | + // Do not send new invoice notifications. |
|
346 | + $GLOBALS['wpinv_skip_invoice_notification'] = true; |
|
347 | + |
|
348 | + // Save the invoice. |
|
349 | + $invoice->save(); |
|
350 | + |
|
351 | + // Save the user address. |
|
352 | + getpaid_save_invoice_user_address( $invoice ); |
|
353 | + |
|
354 | + // Undo do not send new invoice notifications. |
|
355 | + $GLOBALS['wpinv_skip_invoice_notification'] = false; |
|
356 | + |
|
357 | + // (Maybe) send new user notification. |
|
358 | + $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
359 | + if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ) ) ) { |
|
360 | + wp_send_new_user_notifications( $user, 'user' ); |
|
361 | + } |
|
362 | + |
|
363 | + if ( ! empty( $posted['send_to_customer'] ) && ! $invoice->is_draft() ) { |
|
364 | + getpaid()->get( 'invoice_emails' )->user_invoice( $invoice, true ); |
|
365 | + } |
|
366 | + |
|
367 | + // Fires after an invoice is saved. |
|
368 | + do_action( 'wpinv_invoice_metabox_saved', $invoice ); |
|
369 | + } |
|
370 | 370 | } |
@@ -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; |
|
133 | 125 | |
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 | - } |
|
126 | + if ( ! $submission->has_invoice() ) { |
|
127 | + $invoice = new WPInv_Invoice(); |
|
128 | + $invoice->set_created_via( 'payment_form' ); |
|
129 | + return $invoice; |
|
130 | + } |
|
138 | 131 | |
139 | - return $invoice; |
|
140 | - } |
|
132 | + $invoice = $submission->get_invoice(); |
|
141 | 133 | |
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 ) { |
|
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 | + } |
|
150 | 138 | |
151 | - $submission = $this->payment_form_submission; |
|
139 | + return $invoice; |
|
140 | + } |
|
152 | 141 | |
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() ) ); |
|
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 | + |
|
151 | + $submission = $this->payment_form_submission; |
|
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() ) ); |
|
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 ) ) { |
@@ -197,16 +197,16 @@ discard block |
||
197 | 197 | } |
198 | 198 | |
199 | 199 | if ( empty( $user ) ) { |
200 | - $name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) ); |
|
201 | - $name = implode( '', array_filter( $name ) ); |
|
200 | + $name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) ); |
|
201 | + $name = implode( '', array_filter( $name ) ); |
|
202 | 202 | $user = wpinv_create_user( $submission->get_billing_email(), $name ); |
203 | 203 | |
204 | - // (Maybe) send new user notification. |
|
205 | - $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
206 | - if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
207 | - wp_send_new_user_notifications( $user, 'user' ); |
|
208 | - } |
|
209 | - } |
|
204 | + // (Maybe) send new user notification. |
|
205 | + $should_send_notification = wpinv_get_option( 'disable_new_user_emails' ); |
|
206 | + if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) { |
|
207 | + wp_send_new_user_notifications( $user, 'user' ); |
|
208 | + } |
|
209 | + } |
|
210 | 210 | |
211 | 211 | if ( is_wp_error( $user ) ) { |
212 | 212 | wp_send_json_error( $user->get_error_message() ); |
@@ -214,49 +214,49 @@ discard block |
||
214 | 214 | |
215 | 215 | if ( is_numeric( $user ) ) { |
216 | 216 | return $user; |
217 | - } |
|
217 | + } |
|
218 | 218 | |
219 | - return $user->ID; |
|
219 | + return $user->ID; |
|
220 | 220 | |
221 | - } |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
223 | + /** |
|
224 | 224 | * Prepares submission data for saving to the database. |
225 | 225 | * |
226 | - * @return array |
|
226 | + * @return array |
|
227 | 227 | */ |
228 | 228 | public function prepare_submission_data_for_saving() { |
229 | 229 | |
230 | - $submission = $this->payment_form_submission; |
|
230 | + $submission = $this->payment_form_submission; |
|
231 | 231 | |
232 | - // Prepared submission details. |
|
232 | + // Prepared submission details. |
|
233 | 233 | $prepared = array( |
234 | - 'all' => array(), |
|
235 | - 'meta' => array(), |
|
236 | - ); |
|
234 | + 'all' => array(), |
|
235 | + 'meta' => array(), |
|
236 | + ); |
|
237 | 237 | |
238 | 238 | // Raw submission details. |
239 | - $data = $submission->get_data(); |
|
239 | + $data = $submission->get_data(); |
|
240 | 240 | |
241 | - // Loop through the submitted details. |
|
241 | + // Loop through the submitted details. |
|
242 | 242 | foreach ( $submission->get_payment_form()->get_elements() as $field ) { |
243 | 243 | |
244 | - // Skip premade fields. |
|
244 | + // Skip premade fields. |
|
245 | 245 | if ( ! empty( $field['premade'] ) ) { |
246 | 246 | continue; |
247 | 247 | } |
248 | 248 | |
249 | - // Ensure address is provided. |
|
250 | - if ( 'address' === $field['type'] ) { |
|
249 | + // Ensure address is provided. |
|
250 | + if ( 'address' === $field['type'] ) { |
|
251 | 251 | $address_type = isset( $field['address_type'] ) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
252 | 252 | |
253 | - foreach ( $field['fields'] as $address_field ) { |
|
253 | + foreach ( $field['fields'] as $address_field ) { |
|
254 | 254 | |
255 | - if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
256 | - wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
257 | - } |
|
258 | - } |
|
259 | - } |
|
255 | + if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
|
256 | + wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
|
257 | + } |
|
258 | + } |
|
259 | + } |
|
260 | 260 | |
261 | 261 | // If it is required and not set, abort. |
262 | 262 | if ( ! $submission->is_required_field_set( $field ) ) { |
@@ -266,31 +266,31 @@ discard block |
||
266 | 266 | // Handle misc fields. |
267 | 267 | if ( isset( $data[ $field['id'] ] ) ) { |
268 | 268 | |
269 | - // Uploads. |
|
270 | - if ( 'file_upload' === $field['type'] ) { |
|
271 | - $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
269 | + // Uploads. |
|
270 | + if ( 'file_upload' === $field['type'] ) { |
|
271 | + $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
|
272 | 272 | |
273 | - if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
274 | - wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
275 | - } |
|
273 | + if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
|
274 | + wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
|
275 | + } |
|
276 | 276 | |
277 | - $value = array(); |
|
277 | + $value = array(); |
|
278 | 278 | |
279 | - foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
280 | - $value[] = sprintf( |
|
281 | - '<a href="%s" target="_blank">%s</a>', |
|
282 | - esc_url_raw( $url ), |
|
283 | - esc_html( $name ) |
|
284 | - ); |
|
285 | - } |
|
279 | + foreach ( $data[ $field['id'] ] as $url => $name ) { |
|
280 | + $value[] = sprintf( |
|
281 | + '<a href="%s" target="_blank">%s</a>', |
|
282 | + esc_url_raw( $url ), |
|
283 | + esc_html( $name ) |
|
284 | + ); |
|
285 | + } |
|
286 | 286 | |
287 | - $value = implode( ' | ', $value ); |
|
287 | + $value = implode( ' | ', $value ); |
|
288 | 288 | |
289 | - } elseif ( 'checkbox' === $field['type'] ) { |
|
290 | - $value = ! empty( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
291 | - } else { |
|
292 | - $value = wp_kses_post( $data[ $field['id'] ] ); |
|
293 | - } |
|
289 | + } elseif ( 'checkbox' === $field['type'] ) { |
|
290 | + $value = ! empty( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
|
291 | + } else { |
|
292 | + $value = wp_kses_post( $data[ $field['id'] ] ); |
|
293 | + } |
|
294 | 294 | |
295 | 295 | $label = $field['id']; |
296 | 296 | |
@@ -298,192 +298,192 @@ discard block |
||
298 | 298 | $label = $field['label']; |
299 | 299 | } |
300 | 300 | |
301 | - if ( ! empty( $field['add_meta'] ) ) { |
|
302 | - $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
303 | - } |
|
304 | - $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
301 | + if ( ! empty( $field['add_meta'] ) ) { |
|
302 | + $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
303 | + } |
|
304 | + $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value ); |
|
305 | 305 | |
306 | 306 | } |
307 | - } |
|
307 | + } |
|
308 | 308 | |
309 | - return $prepared; |
|
309 | + return $prepared; |
|
310 | 310 | |
311 | - } |
|
311 | + } |
|
312 | 312 | |
313 | - /** |
|
313 | + /** |
|
314 | 314 | * Retrieves address details. |
315 | 315 | * |
316 | - * @return array |
|
317 | - * @param WPInv_Invoice $invoice |
|
318 | - * @param string $type |
|
316 | + * @return array |
|
317 | + * @param WPInv_Invoice $invoice |
|
318 | + * @param string $type |
|
319 | 319 | */ |
320 | 320 | public function prepare_address_details( $invoice, $type = 'billing' ) { |
321 | 321 | |
322 | - $data = $this->payment_form_submission->get_data(); |
|
323 | - $type = sanitize_key( $type ); |
|
324 | - $address = array(); |
|
325 | - $prepared = array(); |
|
322 | + $data = $this->payment_form_submission->get_data(); |
|
323 | + $type = sanitize_key( $type ); |
|
324 | + $address = array(); |
|
325 | + $prepared = array(); |
|
326 | 326 | |
327 | - if ( ! empty( $data[ $type ] ) ) { |
|
328 | - $address = $data[ $type ]; |
|
329 | - } |
|
327 | + if ( ! empty( $data[ $type ] ) ) { |
|
328 | + $address = $data[ $type ]; |
|
329 | + } |
|
330 | 330 | |
331 | - // Clean address details. |
|
332 | - foreach ( $address as $key => $value ) { |
|
333 | - $key = sanitize_key( $key ); |
|
334 | - $key = str_replace( 'wpinv_', '', $key ); |
|
335 | - $value = wpinv_clean( $value ); |
|
336 | - $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
337 | - } |
|
331 | + // Clean address details. |
|
332 | + foreach ( $address as $key => $value ) { |
|
333 | + $key = sanitize_key( $key ); |
|
334 | + $key = str_replace( 'wpinv_', '', $key ); |
|
335 | + $value = wpinv_clean( $value ); |
|
336 | + $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice ); |
|
337 | + } |
|
338 | 338 | |
339 | - // Filter address details. |
|
340 | - $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
339 | + // Filter address details. |
|
340 | + $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
|
341 | 341 | |
342 | - // Remove non-whitelisted values. |
|
343 | - return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
342 | + // Remove non-whitelisted values. |
|
343 | + return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
|
344 | 344 | |
345 | - } |
|
345 | + } |
|
346 | 346 | |
347 | - /** |
|
347 | + /** |
|
348 | 348 | * Prepares the billing details. |
349 | 349 | * |
350 | - * @return array |
|
351 | - * @param WPInv_Invoice $invoice |
|
350 | + * @return array |
|
351 | + * @param WPInv_Invoice $invoice |
|
352 | 352 | */ |
353 | 353 | protected function prepare_billing_info( &$invoice ) { |
354 | 354 | |
355 | - $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
355 | + $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
|
356 | 356 | |
357 | - // Update the invoice with the billing details. |
|
358 | - $invoice->set_props( $billing_address ); |
|
357 | + // Update the invoice with the billing details. |
|
358 | + $invoice->set_props( $billing_address ); |
|
359 | 359 | |
360 | - } |
|
360 | + } |
|
361 | 361 | |
362 | - /** |
|
362 | + /** |
|
363 | 363 | * Prepares the shipping details. |
364 | 364 | * |
365 | - * @return array |
|
366 | - * @param WPInv_Invoice $invoice |
|
365 | + * @return array |
|
366 | + * @param WPInv_Invoice $invoice |
|
367 | 367 | */ |
368 | 368 | protected function prepare_shipping_info( $invoice ) { |
369 | 369 | |
370 | - $data = $this->payment_form_submission->get_data(); |
|
370 | + $data = $this->payment_form_submission->get_data(); |
|
371 | 371 | |
372 | - if ( empty( $data['same-shipping-address'] ) ) { |
|
373 | - return $this->prepare_address_details( $invoice, 'shipping' ); |
|
374 | - } |
|
372 | + if ( empty( $data['same-shipping-address'] ) ) { |
|
373 | + return $this->prepare_address_details( $invoice, 'shipping' ); |
|
374 | + } |
|
375 | 375 | |
376 | - return $this->prepare_address_details( $invoice, 'billing' ); |
|
376 | + return $this->prepare_address_details( $invoice, 'billing' ); |
|
377 | 377 | |
378 | - } |
|
378 | + } |
|
379 | 379 | |
380 | - /** |
|
381 | - * Confirms the submission is valid and send users to the gateway. |
|
382 | - * |
|
383 | - * @param WPInv_Invoice $invoice |
|
384 | - * @param array $prepared_payment_form_data |
|
385 | - * @param array $shipping |
|
386 | - */ |
|
387 | - protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
380 | + /** |
|
381 | + * Confirms the submission is valid and send users to the gateway. |
|
382 | + * |
|
383 | + * @param WPInv_Invoice $invoice |
|
384 | + * @param array $prepared_payment_form_data |
|
385 | + * @param array $shipping |
|
386 | + */ |
|
387 | + protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) { |
|
388 | 388 | |
389 | - // Ensure the invoice exists. |
|
389 | + // Ensure the invoice exists. |
|
390 | 390 | if ( ! $invoice->exists() ) { |
391 | 391 | wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
392 | 392 | } |
393 | 393 | |
394 | - // Save payment form data. |
|
395 | - $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
394 | + // Save payment form data. |
|
395 | + $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
|
396 | 396 | delete_post_meta( $invoice->get_id(), 'payment_form_data' ); |
397 | - delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
398 | - if ( ! empty( $prepared_payment_form_data ) ) { |
|
397 | + delete_post_meta( $invoice->get_id(), 'additional_meta_data' ); |
|
398 | + if ( ! empty( $prepared_payment_form_data ) ) { |
|
399 | 399 | |
400 | - if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
401 | - update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
402 | - } |
|
400 | + if ( ! empty( $prepared_payment_form_data['all'] ) ) { |
|
401 | + update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] ); |
|
402 | + } |
|
403 | 403 | |
404 | - if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
405 | - update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
406 | - } |
|
407 | - } |
|
404 | + if ( ! empty( $prepared_payment_form_data['meta'] ) ) { |
|
405 | + update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] ); |
|
406 | + } |
|
407 | + } |
|
408 | 408 | |
409 | - // Save payment form data. |
|
410 | - $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
409 | + // Save payment form data. |
|
410 | + $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
|
411 | 411 | if ( ! empty( $shipping ) ) { |
412 | 412 | update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
413 | - } |
|
413 | + } |
|
414 | 414 | |
415 | - // Backwards compatibility. |
|
415 | + // Backwards compatibility. |
|
416 | 416 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
417 | 417 | |
418 | - try { |
|
419 | - $this->process_payment( $invoice ); |
|
420 | - } catch ( Exception $e ) { |
|
421 | - wpinv_set_error( 'payment_error', $e->getMessage() ); |
|
422 | - } |
|
418 | + try { |
|
419 | + $this->process_payment( $invoice ); |
|
420 | + } catch ( Exception $e ) { |
|
421 | + wpinv_set_error( 'payment_error', $e->getMessage() ); |
|
422 | + } |
|
423 | 423 | |
424 | 424 | // If we are here, there was an error. |
425 | - wpinv_send_back_to_checkout( $invoice ); |
|
425 | + wpinv_send_back_to_checkout( $invoice ); |
|
426 | 426 | |
427 | - } |
|
427 | + } |
|
428 | 428 | |
429 | - /** |
|
430 | - * Processes the actual payment. |
|
431 | - * |
|
432 | - * @param WPInv_Invoice $invoice |
|
433 | - */ |
|
434 | - protected function process_payment( $invoice ) { |
|
429 | + /** |
|
430 | + * Processes the actual payment. |
|
431 | + * |
|
432 | + * @param WPInv_Invoice $invoice |
|
433 | + */ |
|
434 | + protected function process_payment( $invoice ) { |
|
435 | 435 | |
436 | - // Clear any checkout errors. |
|
437 | - wpinv_clear_errors(); |
|
436 | + // Clear any checkout errors. |
|
437 | + wpinv_clear_errors(); |
|
438 | 438 | |
439 | - // No need to send free invoices to the gateway. |
|
440 | - if ( $invoice->is_free() ) { |
|
441 | - $this->process_free_payment( $invoice ); |
|
442 | - } |
|
439 | + // No need to send free invoices to the gateway. |
|
440 | + if ( $invoice->is_free() ) { |
|
441 | + $this->process_free_payment( $invoice ); |
|
442 | + } |
|
443 | 443 | |
444 | - $submission = $this->payment_form_submission; |
|
444 | + $submission = $this->payment_form_submission; |
|
445 | 445 | |
446 | - // Fires before sending to the gateway. |
|
447 | - do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
446 | + // Fires before sending to the gateway. |
|
447 | + do_action( 'getpaid_checkout_before_gateway', $invoice, $submission ); |
|
448 | 448 | |
449 | - // Allow the sumission data to be modified before it is sent to the gateway. |
|
450 | - $submission_data = $submission->get_data(); |
|
451 | - $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
452 | - $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
449 | + // Allow the sumission data to be modified before it is sent to the gateway. |
|
450 | + $submission_data = $submission->get_data(); |
|
451 | + $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice ); |
|
452 | + $submission_data = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice ); |
|
453 | 453 | |
454 | - // Validate the currency. |
|
455 | - if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
456 | - wpinv_set_error( 'invalid_currency' ); |
|
457 | - } |
|
454 | + // Validate the currency. |
|
455 | + if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) { |
|
456 | + wpinv_set_error( 'invalid_currency' ); |
|
457 | + } |
|
458 | 458 | |
459 | - // Check to see if we have any errors. |
|
460 | - if ( wpinv_get_errors() ) { |
|
461 | - wpinv_send_back_to_checkout( $invoice ); |
|
462 | - } |
|
459 | + // Check to see if we have any errors. |
|
460 | + if ( wpinv_get_errors() ) { |
|
461 | + wpinv_send_back_to_checkout( $invoice ); |
|
462 | + } |
|
463 | 463 | |
464 | - // Send info to the gateway for payment processing |
|
465 | - do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
464 | + // Send info to the gateway for payment processing |
|
465 | + do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission ); |
|
466 | 466 | |
467 | - // Backwards compatibility. |
|
468 | - wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
467 | + // Backwards compatibility. |
|
468 | + wpinv_send_to_gateway( $submission_gateway, $invoice ); |
|
469 | 469 | |
470 | - } |
|
470 | + } |
|
471 | 471 | |
472 | - /** |
|
473 | - * Marks the invoice as paid in case the checkout is free. |
|
474 | - * |
|
475 | - * @param WPInv_Invoice $invoice |
|
476 | - */ |
|
477 | - protected function process_free_payment( $invoice ) { |
|
472 | + /** |
|
473 | + * Marks the invoice as paid in case the checkout is free. |
|
474 | + * |
|
475 | + * @param WPInv_Invoice $invoice |
|
476 | + */ |
|
477 | + protected function process_free_payment( $invoice ) { |
|
478 | 478 | |
479 | - $invoice->set_gateway( 'none' ); |
|
480 | - $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
481 | - $invoice->mark_paid(); |
|
482 | - wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
479 | + $invoice->set_gateway( 'none' ); |
|
480 | + $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
|
481 | + $invoice->mark_paid(); |
|
482 | + wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
|
483 | 483 | |
484 | - } |
|
484 | + } |
|
485 | 485 | |
486 | - /** |
|
486 | + /** |
|
487 | 487 | * Sends a redrect response to payment details. |
488 | 488 | * |
489 | 489 | */ |
@@ -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,199 +10,199 @@ discard block |
||
10 | 10 | class GetPaid_Payment_Form_Submission { |
11 | 11 | |
12 | 12 | /** |
13 | - * Submission ID |
|
14 | - * |
|
15 | - * @var string |
|
16 | - */ |
|
17 | - public $id = null; |
|
18 | - |
|
19 | - /** |
|
20 | - * The raw submission data. |
|
21 | - * |
|
22 | - * @var array |
|
23 | - */ |
|
24 | - protected $data = null; |
|
25 | - |
|
26 | - /** |
|
27 | - * Submission totals |
|
28 | - * |
|
29 | - * @var array |
|
30 | - */ |
|
31 | - protected $totals = array( |
|
32 | - |
|
33 | - 'subtotal' => array( |
|
34 | - 'initial' => 0, |
|
35 | - 'recurring' => 0, |
|
36 | - ), |
|
37 | - |
|
38 | - 'discount' => array( |
|
39 | - 'initial' => 0, |
|
40 | - 'recurring' => 0, |
|
41 | - ), |
|
42 | - |
|
43 | - 'fees' => array( |
|
44 | - 'initial' => 0, |
|
45 | - 'recurring' => 0, |
|
46 | - ), |
|
47 | - |
|
48 | - 'taxes' => array( |
|
49 | - 'initial' => 0, |
|
50 | - 'recurring' => 0, |
|
51 | - ), |
|
52 | - |
|
53 | - 'shipping' => array( |
|
54 | - 'initial' => 0, |
|
55 | - 'recurring' => 0, |
|
56 | - ), |
|
57 | - |
|
58 | - ); |
|
59 | - |
|
60 | - /** |
|
61 | - * Sets the associated payment form. |
|
62 | - * |
|
63 | - * @var GetPaid_Payment_Form |
|
64 | - */ |
|
13 | + * Submission ID |
|
14 | + * |
|
15 | + * @var string |
|
16 | + */ |
|
17 | + public $id = null; |
|
18 | + |
|
19 | + /** |
|
20 | + * The raw submission data. |
|
21 | + * |
|
22 | + * @var array |
|
23 | + */ |
|
24 | + protected $data = null; |
|
25 | + |
|
26 | + /** |
|
27 | + * Submission totals |
|
28 | + * |
|
29 | + * @var array |
|
30 | + */ |
|
31 | + protected $totals = array( |
|
32 | + |
|
33 | + 'subtotal' => array( |
|
34 | + 'initial' => 0, |
|
35 | + 'recurring' => 0, |
|
36 | + ), |
|
37 | + |
|
38 | + 'discount' => array( |
|
39 | + 'initial' => 0, |
|
40 | + 'recurring' => 0, |
|
41 | + ), |
|
42 | + |
|
43 | + 'fees' => array( |
|
44 | + 'initial' => 0, |
|
45 | + 'recurring' => 0, |
|
46 | + ), |
|
47 | + |
|
48 | + 'taxes' => array( |
|
49 | + 'initial' => 0, |
|
50 | + 'recurring' => 0, |
|
51 | + ), |
|
52 | + |
|
53 | + 'shipping' => array( |
|
54 | + 'initial' => 0, |
|
55 | + 'recurring' => 0, |
|
56 | + ), |
|
57 | + |
|
58 | + ); |
|
59 | + |
|
60 | + /** |
|
61 | + * Sets the associated payment form. |
|
62 | + * |
|
63 | + * @var GetPaid_Payment_Form |
|
64 | + */ |
|
65 | 65 | protected $payment_form = null; |
66 | 66 | |
67 | 67 | /** |
68 | - * The country for the submission. |
|
69 | - * |
|
70 | - * @var string |
|
71 | - */ |
|
72 | - public $country = null; |
|
73 | - |
|
74 | - /** |
|
75 | - * The state for the submission. |
|
76 | - * |
|
77 | - * @since 1.0.19 |
|
78 | - * @var string |
|
79 | - */ |
|
80 | - public $state = null; |
|
81 | - |
|
82 | - /** |
|
83 | - * The invoice associated with the submission. |
|
84 | - * |
|
85 | - * @var WPInv_Invoice |
|
86 | - */ |
|
87 | - protected $invoice = null; |
|
88 | - |
|
89 | - /** |
|
90 | - * The recurring item for the submission. |
|
91 | - * |
|
92 | - * @var int |
|
93 | - */ |
|
94 | - public $has_recurring = 0; |
|
95 | - |
|
96 | - /** |
|
97 | - * An array of fees for the submission. |
|
98 | - * |
|
99 | - * @var array |
|
100 | - */ |
|
101 | - protected $fees = array(); |
|
102 | - |
|
103 | - /** |
|
104 | - * An array of discounts for the submission. |
|
105 | - * |
|
106 | - * @var array |
|
107 | - */ |
|
108 | - protected $discounts = array(); |
|
109 | - |
|
110 | - /** |
|
111 | - * An array of taxes for the submission. |
|
112 | - * |
|
113 | - * @var array |
|
114 | - */ |
|
115 | - protected $taxes = array(); |
|
116 | - |
|
117 | - /** |
|
118 | - * An array of items for the submission. |
|
119 | - * |
|
120 | - * @var GetPaid_Form_Item[] |
|
121 | - */ |
|
122 | - protected $items = array(); |
|
123 | - |
|
124 | - /** |
|
125 | - * The last error. |
|
126 | - * |
|
127 | - * @var string |
|
128 | - */ |
|
129 | - public $last_error = null; |
|
130 | - |
|
131 | - /** |
|
132 | - * The last error code. |
|
133 | - * |
|
134 | - * @var string |
|
135 | - */ |
|
136 | - public $last_error_code = null; |
|
137 | - |
|
138 | - /** |
|
139 | - * Class constructor. |
|
140 | - * |
|
141 | - */ |
|
142 | - public function __construct() { |
|
143 | - |
|
144 | - // Set the state and country to the default state and country. |
|
145 | - $this->country = wpinv_default_billing_country(); |
|
146 | - $this->state = wpinv_get_default_state(); |
|
147 | - |
|
148 | - // Do we have an actual submission? |
|
149 | - if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
150 | - $this->load_data( wp_kses_post_deep( wp_unslash( $_POST ) ) ); |
|
151 | - } |
|
152 | - |
|
153 | - } |
|
154 | - |
|
155 | - /** |
|
156 | - * Loads submission data. |
|
157 | - * |
|
158 | - * @param array $data |
|
159 | - */ |
|
160 | - public function load_data( $data ) { |
|
161 | - |
|
162 | - // Allow plugins to filter the data. |
|
163 | - $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
164 | - |
|
165 | - // Cache it... |
|
166 | - $this->data = $data; |
|
167 | - |
|
168 | - // Then generate a unique id from the data. |
|
169 | - $this->id = md5( wp_json_encode( $data ) ); |
|
170 | - |
|
171 | - // Finally, process the submission. |
|
172 | - try { |
|
173 | - |
|
174 | - // Each process is passed an instance of the class (with reference) |
|
175 | - // and should throw an Exception whenever it encounters one. |
|
176 | - $processors = apply_filters( |
|
177 | - 'getpaid_payment_form_submission_processors', |
|
178 | - array( |
|
179 | - array( $this, 'process_payment_form' ), |
|
180 | - array( $this, 'process_invoice' ), |
|
181 | - array( $this, 'process_fees' ), |
|
182 | - array( $this, 'process_items' ), |
|
183 | - array( $this, 'process_discount' ), |
|
184 | - array( $this, 'process_taxes' ), |
|
185 | - ), |
|
186 | - $this |
|
187 | - ); |
|
188 | - |
|
189 | - foreach ( $processors as $processor ) { |
|
190 | - call_user_func_array( $processor, array( &$this ) ); |
|
191 | - } |
|
192 | - } catch ( GetPaid_Payment_Exception $e ) { |
|
193 | - $this->last_error = $e->getMessage(); |
|
194 | - $this->last_error_code = $e->getErrorCode(); |
|
195 | - } catch ( Exception $e ) { |
|
196 | - $this->last_error = $e->getMessage(); |
|
197 | - $this->last_error_code = $e->getCode(); |
|
198 | - } |
|
199 | - |
|
200 | - // Fired when we are done processing a submission. |
|
201 | - do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
|
202 | - |
|
203 | - } |
|
204 | - |
|
205 | - /* |
|
68 | + * The country for the submission. |
|
69 | + * |
|
70 | + * @var string |
|
71 | + */ |
|
72 | + public $country = null; |
|
73 | + |
|
74 | + /** |
|
75 | + * The state for the submission. |
|
76 | + * |
|
77 | + * @since 1.0.19 |
|
78 | + * @var string |
|
79 | + */ |
|
80 | + public $state = null; |
|
81 | + |
|
82 | + /** |
|
83 | + * The invoice associated with the submission. |
|
84 | + * |
|
85 | + * @var WPInv_Invoice |
|
86 | + */ |
|
87 | + protected $invoice = null; |
|
88 | + |
|
89 | + /** |
|
90 | + * The recurring item for the submission. |
|
91 | + * |
|
92 | + * @var int |
|
93 | + */ |
|
94 | + public $has_recurring = 0; |
|
95 | + |
|
96 | + /** |
|
97 | + * An array of fees for the submission. |
|
98 | + * |
|
99 | + * @var array |
|
100 | + */ |
|
101 | + protected $fees = array(); |
|
102 | + |
|
103 | + /** |
|
104 | + * An array of discounts for the submission. |
|
105 | + * |
|
106 | + * @var array |
|
107 | + */ |
|
108 | + protected $discounts = array(); |
|
109 | + |
|
110 | + /** |
|
111 | + * An array of taxes for the submission. |
|
112 | + * |
|
113 | + * @var array |
|
114 | + */ |
|
115 | + protected $taxes = array(); |
|
116 | + |
|
117 | + /** |
|
118 | + * An array of items for the submission. |
|
119 | + * |
|
120 | + * @var GetPaid_Form_Item[] |
|
121 | + */ |
|
122 | + protected $items = array(); |
|
123 | + |
|
124 | + /** |
|
125 | + * The last error. |
|
126 | + * |
|
127 | + * @var string |
|
128 | + */ |
|
129 | + public $last_error = null; |
|
130 | + |
|
131 | + /** |
|
132 | + * The last error code. |
|
133 | + * |
|
134 | + * @var string |
|
135 | + */ |
|
136 | + public $last_error_code = null; |
|
137 | + |
|
138 | + /** |
|
139 | + * Class constructor. |
|
140 | + * |
|
141 | + */ |
|
142 | + public function __construct() { |
|
143 | + |
|
144 | + // Set the state and country to the default state and country. |
|
145 | + $this->country = wpinv_default_billing_country(); |
|
146 | + $this->state = wpinv_get_default_state(); |
|
147 | + |
|
148 | + // Do we have an actual submission? |
|
149 | + if ( isset( $_POST['getpaid_payment_form_submission'] ) ) { |
|
150 | + $this->load_data( wp_kses_post_deep( wp_unslash( $_POST ) ) ); |
|
151 | + } |
|
152 | + |
|
153 | + } |
|
154 | + |
|
155 | + /** |
|
156 | + * Loads submission data. |
|
157 | + * |
|
158 | + * @param array $data |
|
159 | + */ |
|
160 | + public function load_data( $data ) { |
|
161 | + |
|
162 | + // Allow plugins to filter the data. |
|
163 | + $data = apply_filters( 'getpaid_submission_data', $data, $this ); |
|
164 | + |
|
165 | + // Cache it... |
|
166 | + $this->data = $data; |
|
167 | + |
|
168 | + // Then generate a unique id from the data. |
|
169 | + $this->id = md5( wp_json_encode( $data ) ); |
|
170 | + |
|
171 | + // Finally, process the submission. |
|
172 | + try { |
|
173 | + |
|
174 | + // Each process is passed an instance of the class (with reference) |
|
175 | + // and should throw an Exception whenever it encounters one. |
|
176 | + $processors = apply_filters( |
|
177 | + 'getpaid_payment_form_submission_processors', |
|
178 | + array( |
|
179 | + array( $this, 'process_payment_form' ), |
|
180 | + array( $this, 'process_invoice' ), |
|
181 | + array( $this, 'process_fees' ), |
|
182 | + array( $this, 'process_items' ), |
|
183 | + array( $this, 'process_discount' ), |
|
184 | + array( $this, 'process_taxes' ), |
|
185 | + ), |
|
186 | + $this |
|
187 | + ); |
|
188 | + |
|
189 | + foreach ( $processors as $processor ) { |
|
190 | + call_user_func_array( $processor, array( &$this ) ); |
|
191 | + } |
|
192 | + } catch ( GetPaid_Payment_Exception $e ) { |
|
193 | + $this->last_error = $e->getMessage(); |
|
194 | + $this->last_error_code = $e->getErrorCode(); |
|
195 | + } catch ( Exception $e ) { |
|
196 | + $this->last_error = $e->getMessage(); |
|
197 | + $this->last_error_code = $e->getCode(); |
|
198 | + } |
|
199 | + |
|
200 | + // Fired when we are done processing a submission. |
|
201 | + do_action_ref_array( 'getpaid_process_submission', array( &$this ) ); |
|
202 | + |
|
203 | + } |
|
204 | + |
|
205 | + /* |
|
206 | 206 | |-------------------------------------------------------------------------- |
207 | 207 | | Payment Forms. |
208 | 208 | |-------------------------------------------------------------------------- |
@@ -211,39 +211,39 @@ discard block |
||
211 | 211 | | submission has an active payment form etc. |
212 | 212 | */ |
213 | 213 | |
214 | - /** |
|
215 | - * Prepares the submission's payment form. |
|
216 | - * |
|
217 | - * @since 1.0.19 |
|
218 | - */ |
|
219 | - public function process_payment_form() { |
|
214 | + /** |
|
215 | + * Prepares the submission's payment form. |
|
216 | + * |
|
217 | + * @since 1.0.19 |
|
218 | + */ |
|
219 | + public function process_payment_form() { |
|
220 | 220 | |
221 | - // Every submission needs an active payment form. |
|
222 | - if ( empty( $this->data['form_id'] ) ) { |
|
223 | - throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
|
224 | - } |
|
221 | + // Every submission needs an active payment form. |
|
222 | + if ( empty( $this->data['form_id'] ) ) { |
|
223 | + throw new Exception( __( 'Missing payment form', 'invoicing' ) ); |
|
224 | + } |
|
225 | 225 | |
226 | - // Fetch the payment form. |
|
227 | - $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
226 | + // Fetch the payment form. |
|
227 | + $this->payment_form = new GetPaid_Payment_Form( $this->data['form_id'] ); |
|
228 | 228 | |
229 | - if ( ! $this->payment_form->is_active() ) { |
|
230 | - throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
231 | - } |
|
229 | + if ( ! $this->payment_form->is_active() ) { |
|
230 | + throw new Exception( __( 'Payment form not active', 'invoicing' ) ); |
|
231 | + } |
|
232 | 232 | |
233 | - do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
234 | - } |
|
233 | + do_action_ref_array( 'getpaid_submissions_process_payment_form', array( &$this ) ); |
|
234 | + } |
|
235 | 235 | |
236 | 236 | /** |
237 | - * Returns the payment form. |
|
238 | - * |
|
239 | - * @since 1.0.19 |
|
240 | - * @return GetPaid_Payment_Form |
|
241 | - */ |
|
242 | - public function get_payment_form() { |
|
243 | - return $this->payment_form; |
|
244 | - } |
|
237 | + * Returns the payment form. |
|
238 | + * |
|
239 | + * @since 1.0.19 |
|
240 | + * @return GetPaid_Payment_Form |
|
241 | + */ |
|
242 | + public function get_payment_form() { |
|
243 | + return $this->payment_form; |
|
244 | + } |
|
245 | 245 | |
246 | - /* |
|
246 | + /* |
|
247 | 247 | |-------------------------------------------------------------------------- |
248 | 248 | | Invoices. |
249 | 249 | |-------------------------------------------------------------------------- |
@@ -252,95 +252,95 @@ discard block |
||
252 | 252 | | might be for an existing invoice. |
253 | 253 | */ |
254 | 254 | |
255 | - /** |
|
256 | - * Prepares the submission's invoice. |
|
257 | - * |
|
258 | - * @since 1.0.19 |
|
259 | - */ |
|
260 | - public function process_invoice() { |
|
261 | - |
|
262 | - // Abort if there is no invoice. |
|
263 | - if ( empty( $this->data['invoice_id'] ) ) { |
|
264 | - |
|
265 | - // Check if we are resuming a payment. |
|
266 | - if ( empty( $this->data['maybe_use_invoice'] ) ) { |
|
267 | - return; |
|
268 | - } |
|
269 | - |
|
270 | - $invoice = wpinv_get_invoice( $this->data['maybe_use_invoice'] ); |
|
271 | - if ( empty( $invoice ) || ! $invoice->has_status( 'draft, auto-draft, wpi-pending' ) ) { |
|
272 | - return; |
|
273 | - } |
|
274 | - } |
|
275 | - |
|
276 | - // If the submission is for an existing invoice, ensure that it exists |
|
277 | - // and that it is not paid for. |
|
278 | - if ( empty( $invoice ) ) { |
|
279 | - $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
|
280 | - } |
|
255 | + /** |
|
256 | + * Prepares the submission's invoice. |
|
257 | + * |
|
258 | + * @since 1.0.19 |
|
259 | + */ |
|
260 | + public function process_invoice() { |
|
261 | + |
|
262 | + // Abort if there is no invoice. |
|
263 | + if ( empty( $this->data['invoice_id'] ) ) { |
|
264 | + |
|
265 | + // Check if we are resuming a payment. |
|
266 | + if ( empty( $this->data['maybe_use_invoice'] ) ) { |
|
267 | + return; |
|
268 | + } |
|
269 | + |
|
270 | + $invoice = wpinv_get_invoice( $this->data['maybe_use_invoice'] ); |
|
271 | + if ( empty( $invoice ) || ! $invoice->has_status( 'draft, auto-draft, wpi-pending' ) ) { |
|
272 | + return; |
|
273 | + } |
|
274 | + } |
|
275 | + |
|
276 | + // If the submission is for an existing invoice, ensure that it exists |
|
277 | + // and that it is not paid for. |
|
278 | + if ( empty( $invoice ) ) { |
|
279 | + $invoice = wpinv_get_invoice( $this->data['invoice_id'] ); |
|
280 | + } |
|
281 | 281 | |
282 | 282 | if ( empty( $invoice ) ) { |
283 | - throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
284 | - } |
|
285 | - |
|
286 | - if ( $invoice->is_paid() ) { |
|
287 | - throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
288 | - } |
|
289 | - |
|
290 | - $this->payment_form->invoice = $invoice; |
|
291 | - if ( ! $this->payment_form->is_default() ) { |
|
292 | - |
|
293 | - $items = array(); |
|
294 | - $item_ids = array(); |
|
295 | - |
|
296 | - foreach ( $invoice->get_items() as $item ) { |
|
297 | - if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
298 | - $item_ids[] = $item->get_id(); |
|
299 | - $items[] = $item; |
|
300 | - } |
|
301 | - } |
|
302 | - |
|
303 | - foreach ( $this->payment_form->get_items() as $item ) { |
|
304 | - if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
305 | - $item_ids[] = $item->get_id(); |
|
306 | - $items[] = $item; |
|
307 | - } |
|
308 | - } |
|
309 | - |
|
310 | - $this->payment_form->set_items( $items ); |
|
311 | - |
|
312 | - } else { |
|
313 | - $this->payment_form->set_items( $invoice->get_items() ); |
|
314 | - } |
|
315 | - |
|
316 | - $this->country = $invoice->get_country(); |
|
317 | - $this->state = $invoice->get_state(); |
|
318 | - $this->invoice = $invoice; |
|
319 | - |
|
320 | - do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
|
321 | - } |
|
322 | - |
|
323 | - /** |
|
324 | - * Returns the associated invoice. |
|
325 | - * |
|
326 | - * @since 1.0.19 |
|
327 | - * @return WPInv_Invoice |
|
328 | - */ |
|
329 | - public function get_invoice() { |
|
330 | - return $this->invoice; |
|
331 | - } |
|
332 | - |
|
333 | - /** |
|
334 | - * Checks whether there is an invoice associated with this submission. |
|
335 | - * |
|
336 | - * @since 1.0.19 |
|
337 | - * @return bool |
|
338 | - */ |
|
339 | - public function has_invoice() { |
|
340 | - return ! empty( $this->invoice ); |
|
341 | - } |
|
342 | - |
|
343 | - /* |
|
283 | + throw new Exception( __( 'Invalid invoice', 'invoicing' ) ); |
|
284 | + } |
|
285 | + |
|
286 | + if ( $invoice->is_paid() ) { |
|
287 | + throw new Exception( __( 'This invoice is already paid for.', 'invoicing' ) ); |
|
288 | + } |
|
289 | + |
|
290 | + $this->payment_form->invoice = $invoice; |
|
291 | + if ( ! $this->payment_form->is_default() ) { |
|
292 | + |
|
293 | + $items = array(); |
|
294 | + $item_ids = array(); |
|
295 | + |
|
296 | + foreach ( $invoice->get_items() as $item ) { |
|
297 | + if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
298 | + $item_ids[] = $item->get_id(); |
|
299 | + $items[] = $item; |
|
300 | + } |
|
301 | + } |
|
302 | + |
|
303 | + foreach ( $this->payment_form->get_items() as $item ) { |
|
304 | + if ( ! in_array( $item->get_id(), $item_ids ) ) { |
|
305 | + $item_ids[] = $item->get_id(); |
|
306 | + $items[] = $item; |
|
307 | + } |
|
308 | + } |
|
309 | + |
|
310 | + $this->payment_form->set_items( $items ); |
|
311 | + |
|
312 | + } else { |
|
313 | + $this->payment_form->set_items( $invoice->get_items() ); |
|
314 | + } |
|
315 | + |
|
316 | + $this->country = $invoice->get_country(); |
|
317 | + $this->state = $invoice->get_state(); |
|
318 | + $this->invoice = $invoice; |
|
319 | + |
|
320 | + do_action_ref_array( 'getpaid_submissions_process_invoice', array( &$this ) ); |
|
321 | + } |
|
322 | + |
|
323 | + /** |
|
324 | + * Returns the associated invoice. |
|
325 | + * |
|
326 | + * @since 1.0.19 |
|
327 | + * @return WPInv_Invoice |
|
328 | + */ |
|
329 | + public function get_invoice() { |
|
330 | + return $this->invoice; |
|
331 | + } |
|
332 | + |
|
333 | + /** |
|
334 | + * Checks whether there is an invoice associated with this submission. |
|
335 | + * |
|
336 | + * @since 1.0.19 |
|
337 | + * @return bool |
|
338 | + */ |
|
339 | + public function has_invoice() { |
|
340 | + return ! empty( $this->invoice ); |
|
341 | + } |
|
342 | + |
|
343 | + /* |
|
344 | 344 | |-------------------------------------------------------------------------- |
345 | 345 | | Items. |
346 | 346 | |-------------------------------------------------------------------------- |
@@ -349,129 +349,129 @@ discard block |
||
349 | 349 | | recurring item. But can have an unlimited number of non-recurring items. |
350 | 350 | */ |
351 | 351 | |
352 | - /** |
|
353 | - * Prepares the submission's items. |
|
354 | - * |
|
355 | - * @since 1.0.19 |
|
356 | - */ |
|
357 | - public function process_items() { |
|
358 | - |
|
359 | - $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
360 | - |
|
361 | - foreach ( $processor->items as $item ) { |
|
362 | - $this->add_item( $item ); |
|
363 | - } |
|
364 | - |
|
365 | - do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Adds an item to the submission. |
|
370 | - * |
|
371 | - * @since 1.0.19 |
|
372 | - * @param GetPaid_Form_Item $item |
|
373 | - */ |
|
374 | - public function add_item( $item ) { |
|
375 | - |
|
376 | - // Make sure that it is available for purchase. |
|
377 | - if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
|
378 | - return; |
|
379 | - } |
|
380 | - |
|
381 | - // Each submission can only contain one recurring item. |
|
382 | - if ( $item->is_recurring() ) { |
|
383 | - $this->has_recurring = $item->get_id(); |
|
384 | - } |
|
385 | - |
|
386 | - // Update the items and totals. |
|
387 | - $this->items[ $item->get_id() ] = $item; |
|
388 | - $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
|
389 | - $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
|
390 | - |
|
391 | - } |
|
392 | - |
|
393 | - /** |
|
394 | - * Removes a specific item. |
|
395 | - * |
|
396 | - * You should not call this method after the discounts and taxes |
|
397 | - * have been calculated. |
|
398 | - * |
|
399 | - * @since 1.0.19 |
|
400 | - */ |
|
401 | - public function remove_item( $item_id ) { |
|
402 | - |
|
403 | - if ( isset( $this->items[ $item_id ] ) ) { |
|
404 | - $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
|
405 | - $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
|
406 | - |
|
407 | - if ( $this->items[ $item_id ]->is_recurring() ) { |
|
408 | - $this->has_recurring = 0; |
|
409 | - } |
|
410 | - |
|
411 | - unset( $this->items[ $item_id ] ); |
|
412 | - } |
|
413 | - |
|
414 | - } |
|
415 | - |
|
416 | - /** |
|
417 | - * Returns the subtotal. |
|
418 | - * |
|
419 | - * @since 1.0.19 |
|
420 | - */ |
|
421 | - public function get_subtotal() { |
|
422 | - |
|
423 | - if ( wpinv_prices_include_tax() ) { |
|
424 | - return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
|
425 | - } |
|
426 | - |
|
427 | - return $this->totals['subtotal']['initial']; |
|
428 | - } |
|
429 | - |
|
430 | - /** |
|
431 | - * Returns the recurring subtotal. |
|
432 | - * |
|
433 | - * @since 1.0.19 |
|
434 | - */ |
|
435 | - public function get_recurring_subtotal() { |
|
436 | - |
|
437 | - if ( wpinv_prices_include_tax() ) { |
|
438 | - return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
|
439 | - } |
|
440 | - |
|
441 | - return $this->totals['subtotal']['recurring']; |
|
442 | - } |
|
443 | - |
|
444 | - /** |
|
445 | - * Returns all items. |
|
446 | - * |
|
447 | - * @since 1.0.19 |
|
448 | - * @return GetPaid_Form_Item[] |
|
449 | - */ |
|
450 | - public function get_items() { |
|
451 | - return $this->items; |
|
452 | - } |
|
453 | - |
|
454 | - /** |
|
455 | - * Checks if there's a single subscription group in the submission. |
|
456 | - * |
|
457 | - * @since 2.3.0 |
|
458 | - * @return bool |
|
459 | - */ |
|
460 | - public function has_subscription_group() { |
|
461 | - return $this->has_recurring && getpaid_should_group_subscriptions( $this ) && 1 == count( getpaid_get_subscription_groups( $this ) ); |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Checks if there are multipe subscription groups in the submission. |
|
466 | - * |
|
467 | - * @since 2.3.0 |
|
468 | - * @return bool |
|
469 | - */ |
|
470 | - public function has_multiple_subscription_groups() { |
|
471 | - return $this->has_recurring && 1 < count( getpaid_get_subscription_groups( $this ) ); |
|
472 | - } |
|
473 | - |
|
474 | - /* |
|
352 | + /** |
|
353 | + * Prepares the submission's items. |
|
354 | + * |
|
355 | + * @since 1.0.19 |
|
356 | + */ |
|
357 | + public function process_items() { |
|
358 | + |
|
359 | + $processor = new GetPaid_Payment_Form_Submission_Items( $this ); |
|
360 | + |
|
361 | + foreach ( $processor->items as $item ) { |
|
362 | + $this->add_item( $item ); |
|
363 | + } |
|
364 | + |
|
365 | + do_action_ref_array( 'getpaid_submissions_process_items', array( &$this ) ); |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Adds an item to the submission. |
|
370 | + * |
|
371 | + * @since 1.0.19 |
|
372 | + * @param GetPaid_Form_Item $item |
|
373 | + */ |
|
374 | + public function add_item( $item ) { |
|
375 | + |
|
376 | + // Make sure that it is available for purchase. |
|
377 | + if ( ! $item->can_purchase() || isset( $this->items[ $item->get_id() ] ) ) { |
|
378 | + return; |
|
379 | + } |
|
380 | + |
|
381 | + // Each submission can only contain one recurring item. |
|
382 | + if ( $item->is_recurring() ) { |
|
383 | + $this->has_recurring = $item->get_id(); |
|
384 | + } |
|
385 | + |
|
386 | + // Update the items and totals. |
|
387 | + $this->items[ $item->get_id() ] = $item; |
|
388 | + $this->totals['subtotal']['initial'] += $item->get_sub_total(); |
|
389 | + $this->totals['subtotal']['recurring'] += $item->get_recurring_sub_total(); |
|
390 | + |
|
391 | + } |
|
392 | + |
|
393 | + /** |
|
394 | + * Removes a specific item. |
|
395 | + * |
|
396 | + * You should not call this method after the discounts and taxes |
|
397 | + * have been calculated. |
|
398 | + * |
|
399 | + * @since 1.0.19 |
|
400 | + */ |
|
401 | + public function remove_item( $item_id ) { |
|
402 | + |
|
403 | + if ( isset( $this->items[ $item_id ] ) ) { |
|
404 | + $this->totals['subtotal']['initial'] -= $this->items[ $item_id ]->get_sub_total(); |
|
405 | + $this->totals['subtotal']['recurring'] -= $this->items[ $item_id ]->get_recurring_sub_total(); |
|
406 | + |
|
407 | + if ( $this->items[ $item_id ]->is_recurring() ) { |
|
408 | + $this->has_recurring = 0; |
|
409 | + } |
|
410 | + |
|
411 | + unset( $this->items[ $item_id ] ); |
|
412 | + } |
|
413 | + |
|
414 | + } |
|
415 | + |
|
416 | + /** |
|
417 | + * Returns the subtotal. |
|
418 | + * |
|
419 | + * @since 1.0.19 |
|
420 | + */ |
|
421 | + public function get_subtotal() { |
|
422 | + |
|
423 | + if ( wpinv_prices_include_tax() ) { |
|
424 | + return $this->totals['subtotal']['initial'] - $this->totals['taxes']['initial']; |
|
425 | + } |
|
426 | + |
|
427 | + return $this->totals['subtotal']['initial']; |
|
428 | + } |
|
429 | + |
|
430 | + /** |
|
431 | + * Returns the recurring subtotal. |
|
432 | + * |
|
433 | + * @since 1.0.19 |
|
434 | + */ |
|
435 | + public function get_recurring_subtotal() { |
|
436 | + |
|
437 | + if ( wpinv_prices_include_tax() ) { |
|
438 | + return $this->totals['subtotal']['recurring'] - $this->totals['taxes']['recurring']; |
|
439 | + } |
|
440 | + |
|
441 | + return $this->totals['subtotal']['recurring']; |
|
442 | + } |
|
443 | + |
|
444 | + /** |
|
445 | + * Returns all items. |
|
446 | + * |
|
447 | + * @since 1.0.19 |
|
448 | + * @return GetPaid_Form_Item[] |
|
449 | + */ |
|
450 | + public function get_items() { |
|
451 | + return $this->items; |
|
452 | + } |
|
453 | + |
|
454 | + /** |
|
455 | + * Checks if there's a single subscription group in the submission. |
|
456 | + * |
|
457 | + * @since 2.3.0 |
|
458 | + * @return bool |
|
459 | + */ |
|
460 | + public function has_subscription_group() { |
|
461 | + return $this->has_recurring && getpaid_should_group_subscriptions( $this ) && 1 == count( getpaid_get_subscription_groups( $this ) ); |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Checks if there are multipe subscription groups in the submission. |
|
466 | + * |
|
467 | + * @since 2.3.0 |
|
468 | + * @return bool |
|
469 | + */ |
|
470 | + public function has_multiple_subscription_groups() { |
|
471 | + return $this->has_recurring && 1 < count( getpaid_get_subscription_groups( $this ) ); |
|
472 | + } |
|
473 | + |
|
474 | + /* |
|
475 | 475 | |-------------------------------------------------------------------------- |
476 | 476 | | Taxes |
477 | 477 | |-------------------------------------------------------------------------- |
@@ -480,128 +480,128 @@ discard block |
||
480 | 480 | | or only one-time. |
481 | 481 | */ |
482 | 482 | |
483 | - /** |
|
484 | - * Prepares the submission's taxes. |
|
485 | - * |
|
486 | - * @since 1.0.19 |
|
487 | - */ |
|
488 | - public function process_taxes() { |
|
489 | - |
|
490 | - // Abort if we're not using taxes. |
|
491 | - if ( ! $this->use_taxes() ) { |
|
492 | - return; |
|
493 | - } |
|
494 | - |
|
495 | - // If a custom country && state has been passed in, use it to calculate taxes. |
|
496 | - $country = $this->get_field( 'wpinv_country', 'billing' ); |
|
497 | - if ( ! empty( $country ) ) { |
|
498 | - $this->country = $country; |
|
499 | - } |
|
500 | - |
|
501 | - $state = $this->get_field( 'wpinv_state', 'billing' ); |
|
502 | - if ( ! empty( $state ) ) { |
|
503 | - $this->state = $state; |
|
504 | - } |
|
505 | - |
|
506 | - // Confirm if the provided country and the ip country are similar. |
|
507 | - $address_confirmed = $this->get_field( 'confirm-address' ); |
|
508 | - if ( isset( $_POST['billing']['country'] ) && wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
|
509 | - throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
|
510 | - } |
|
511 | - |
|
512 | - // Abort if the country is not taxable. |
|
513 | - if ( ! wpinv_is_country_taxable( $this->country ) ) { |
|
514 | - return; |
|
515 | - } |
|
516 | - |
|
517 | - $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
|
518 | - |
|
519 | - foreach ( $processor->taxes as $tax ) { |
|
520 | - $this->add_tax( $tax ); |
|
521 | - } |
|
522 | - |
|
523 | - do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
|
524 | - } |
|
525 | - |
|
526 | - /** |
|
527 | - * Adds a tax to the submission. |
|
528 | - * |
|
529 | - * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
|
530 | - * @since 1.0.19 |
|
531 | - */ |
|
532 | - public function add_tax( $tax ) { |
|
533 | - |
|
534 | - if ( wpinv_round_tax_per_tax_rate() ) { |
|
535 | - $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
|
536 | - $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
|
537 | - } |
|
538 | - |
|
539 | - $this->taxes[ $tax['name'] ] = $tax; |
|
540 | - $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
541 | - $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
|
542 | - |
|
543 | - } |
|
544 | - |
|
545 | - /** |
|
546 | - * Removes a specific tax. |
|
547 | - * |
|
548 | - * @since 1.0.19 |
|
549 | - */ |
|
550 | - public function remove_tax( $tax_name ) { |
|
551 | - |
|
552 | - if ( isset( $this->taxes[ $tax_name ] ) ) { |
|
553 | - $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
|
554 | - $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
|
555 | - unset( $this->taxes[ $tax_name ] ); |
|
556 | - } |
|
557 | - |
|
558 | - } |
|
559 | - |
|
560 | - /** |
|
561 | - * Whether or not we'll use taxes for the submission. |
|
562 | - * |
|
563 | - * @since 1.0.19 |
|
564 | - */ |
|
565 | - public function use_taxes() { |
|
566 | - |
|
567 | - $use_taxes = wpinv_use_taxes(); |
|
568 | - |
|
569 | - if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
|
570 | - $use_taxes = false; |
|
571 | - } |
|
572 | - |
|
573 | - return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
574 | - |
|
575 | - } |
|
576 | - |
|
577 | - /** |
|
578 | - * Returns the tax. |
|
579 | - * |
|
580 | - * @since 1.0.19 |
|
581 | - */ |
|
582 | - public function get_tax() { |
|
583 | - return $this->totals['taxes']['initial']; |
|
584 | - } |
|
585 | - |
|
586 | - /** |
|
587 | - * Returns the recurring tax. |
|
588 | - * |
|
589 | - * @since 1.0.19 |
|
590 | - */ |
|
591 | - public function get_recurring_tax() { |
|
592 | - return $this->totals['taxes']['recurring']; |
|
593 | - } |
|
594 | - |
|
595 | - /** |
|
596 | - * Returns all taxes. |
|
597 | - * |
|
598 | - * @since 1.0.19 |
|
599 | - */ |
|
600 | - public function get_taxes() { |
|
601 | - return $this->taxes; |
|
602 | - } |
|
603 | - |
|
604 | - /* |
|
483 | + /** |
|
484 | + * Prepares the submission's taxes. |
|
485 | + * |
|
486 | + * @since 1.0.19 |
|
487 | + */ |
|
488 | + public function process_taxes() { |
|
489 | + |
|
490 | + // Abort if we're not using taxes. |
|
491 | + if ( ! $this->use_taxes() ) { |
|
492 | + return; |
|
493 | + } |
|
494 | + |
|
495 | + // If a custom country && state has been passed in, use it to calculate taxes. |
|
496 | + $country = $this->get_field( 'wpinv_country', 'billing' ); |
|
497 | + if ( ! empty( $country ) ) { |
|
498 | + $this->country = $country; |
|
499 | + } |
|
500 | + |
|
501 | + $state = $this->get_field( 'wpinv_state', 'billing' ); |
|
502 | + if ( ! empty( $state ) ) { |
|
503 | + $this->state = $state; |
|
504 | + } |
|
505 | + |
|
506 | + // Confirm if the provided country and the ip country are similar. |
|
507 | + $address_confirmed = $this->get_field( 'confirm-address' ); |
|
508 | + if ( isset( $_POST['billing']['country'] ) && wpinv_should_validate_vat_number() && getpaid_get_ip_country() != $this->country && empty( $address_confirmed ) ) { |
|
509 | + throw new Exception( __( 'The country of your current location must be the same as the country of your billing location or you must confirm the billing address is your home country.', 'invoicing' ) ); |
|
510 | + } |
|
511 | + |
|
512 | + // Abort if the country is not taxable. |
|
513 | + if ( ! wpinv_is_country_taxable( $this->country ) ) { |
|
514 | + return; |
|
515 | + } |
|
516 | + |
|
517 | + $processor = new GetPaid_Payment_Form_Submission_Taxes( $this ); |
|
518 | + |
|
519 | + foreach ( $processor->taxes as $tax ) { |
|
520 | + $this->add_tax( $tax ); |
|
521 | + } |
|
522 | + |
|
523 | + do_action_ref_array( 'getpaid_submissions_process_taxes', array( &$this ) ); |
|
524 | + } |
|
525 | + |
|
526 | + /** |
|
527 | + * Adds a tax to the submission. |
|
528 | + * |
|
529 | + * @param array $tax An array of tax details. name, initial_tax, and recurring_tax are required. |
|
530 | + * @since 1.0.19 |
|
531 | + */ |
|
532 | + public function add_tax( $tax ) { |
|
533 | + |
|
534 | + if ( wpinv_round_tax_per_tax_rate() ) { |
|
535 | + $tax['initial_tax'] = wpinv_round_amount( $tax['initial_tax'] ); |
|
536 | + $tax['recurring_tax'] = wpinv_round_amount( $tax['recurring_tax'] ); |
|
537 | + } |
|
538 | + |
|
539 | + $this->taxes[ $tax['name'] ] = $tax; |
|
540 | + $this->totals['taxes']['initial'] += wpinv_sanitize_amount( $tax['initial_tax'] ); |
|
541 | + $this->totals['taxes']['recurring'] += wpinv_sanitize_amount( $tax['recurring_tax'] ); |
|
542 | + |
|
543 | + } |
|
544 | + |
|
545 | + /** |
|
546 | + * Removes a specific tax. |
|
547 | + * |
|
548 | + * @since 1.0.19 |
|
549 | + */ |
|
550 | + public function remove_tax( $tax_name ) { |
|
551 | + |
|
552 | + if ( isset( $this->taxes[ $tax_name ] ) ) { |
|
553 | + $this->totals['taxes']['initial'] -= $this->taxes[ $tax_name ]['initial_tax']; |
|
554 | + $this->totals['taxes']['recurring'] -= $this->taxes[ $tax_name ]['recurring_tax']; |
|
555 | + unset( $this->taxes[ $tax_name ] ); |
|
556 | + } |
|
557 | + |
|
558 | + } |
|
559 | + |
|
560 | + /** |
|
561 | + * Whether or not we'll use taxes for the submission. |
|
562 | + * |
|
563 | + * @since 1.0.19 |
|
564 | + */ |
|
565 | + public function use_taxes() { |
|
566 | + |
|
567 | + $use_taxes = wpinv_use_taxes(); |
|
568 | + |
|
569 | + if ( $this->has_invoice() && ! $this->invoice->is_taxable() ) { |
|
570 | + $use_taxes = false; |
|
571 | + } |
|
572 | + |
|
573 | + return apply_filters( 'getpaid_submission_use_taxes', $use_taxes, $this ); |
|
574 | + |
|
575 | + } |
|
576 | + |
|
577 | + /** |
|
578 | + * Returns the tax. |
|
579 | + * |
|
580 | + * @since 1.0.19 |
|
581 | + */ |
|
582 | + public function get_tax() { |
|
583 | + return $this->totals['taxes']['initial']; |
|
584 | + } |
|
585 | + |
|
586 | + /** |
|
587 | + * Returns the recurring tax. |
|
588 | + * |
|
589 | + * @since 1.0.19 |
|
590 | + */ |
|
591 | + public function get_recurring_tax() { |
|
592 | + return $this->totals['taxes']['recurring']; |
|
593 | + } |
|
594 | + |
|
595 | + /** |
|
596 | + * Returns all taxes. |
|
597 | + * |
|
598 | + * @since 1.0.19 |
|
599 | + */ |
|
600 | + public function get_taxes() { |
|
601 | + return $this->taxes; |
|
602 | + } |
|
603 | + |
|
604 | + /* |
|
605 | 605 | |-------------------------------------------------------------------------- |
606 | 606 | | Discounts |
607 | 607 | |-------------------------------------------------------------------------- |
@@ -610,99 +610,99 @@ discard block |
||
610 | 610 | | or only one-time. They also do not have to come from a discount code. |
611 | 611 | */ |
612 | 612 | |
613 | - /** |
|
614 | - * Prepares the submission's discount. |
|
615 | - * |
|
616 | - * @since 1.0.19 |
|
617 | - */ |
|
618 | - public function process_discount() { |
|
619 | - |
|
620 | - $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
|
621 | - $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
|
622 | - $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
|
623 | - |
|
624 | - foreach ( $processor->discounts as $discount ) { |
|
625 | - $this->add_discount( $discount ); |
|
626 | - } |
|
627 | - |
|
628 | - do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
|
629 | - } |
|
630 | - |
|
631 | - /** |
|
632 | - * Adds a discount to the submission. |
|
633 | - * |
|
634 | - * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
|
635 | - * @since 1.0.19 |
|
636 | - */ |
|
637 | - public function add_discount( $discount ) { |
|
638 | - $this->discounts[ $discount['name'] ] = $discount; |
|
639 | - $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
|
640 | - $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
|
641 | - } |
|
642 | - |
|
643 | - /** |
|
644 | - * Removes a discount from the submission. |
|
645 | - * |
|
646 | - * @since 1.0.19 |
|
647 | - */ |
|
648 | - public function remove_discount( $name ) { |
|
649 | - |
|
650 | - if ( isset( $this->discounts[ $name ] ) ) { |
|
651 | - $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
|
652 | - $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
|
653 | - unset( $this->discounts[ $name ] ); |
|
654 | - } |
|
655 | - |
|
656 | - } |
|
657 | - |
|
658 | - /** |
|
659 | - * Checks whether there is a discount code associated with this submission. |
|
660 | - * |
|
661 | - * @since 1.0.19 |
|
662 | - * @return bool |
|
663 | - */ |
|
664 | - public function has_discount_code() { |
|
665 | - return ! empty( $this->discounts['discount_code'] ); |
|
666 | - } |
|
667 | - |
|
668 | - /** |
|
669 | - * Returns the discount code. |
|
670 | - * |
|
671 | - * @since 1.0.19 |
|
672 | - * @return string |
|
673 | - */ |
|
674 | - public function get_discount_code() { |
|
675 | - return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : ''; |
|
676 | - } |
|
677 | - |
|
678 | - /** |
|
679 | - * Returns the discount. |
|
680 | - * |
|
681 | - * @since 1.0.19 |
|
682 | - */ |
|
683 | - public function get_discount() { |
|
684 | - return $this->totals['discount']['initial']; |
|
685 | - } |
|
686 | - |
|
687 | - /** |
|
688 | - * Returns the recurring discount. |
|
689 | - * |
|
690 | - * @since 1.0.19 |
|
691 | - */ |
|
692 | - public function get_recurring_discount() { |
|
693 | - return $this->totals['discount']['recurring']; |
|
694 | - } |
|
695 | - |
|
696 | - /** |
|
697 | - * Returns all discounts. |
|
698 | - * |
|
699 | - * @since 1.0.19 |
|
700 | - */ |
|
701 | - public function get_discounts() { |
|
702 | - return $this->discounts; |
|
703 | - } |
|
704 | - |
|
705 | - /* |
|
613 | + /** |
|
614 | + * Prepares the submission's discount. |
|
615 | + * |
|
616 | + * @since 1.0.19 |
|
617 | + */ |
|
618 | + public function process_discount() { |
|
619 | + |
|
620 | + $initial_total = $this->get_subtotal() + $this->get_fee() + $this->get_tax(); |
|
621 | + $recurring_total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax(); |
|
622 | + $processor = new GetPaid_Payment_Form_Submission_Discount( $this, $initial_total, $recurring_total ); |
|
623 | + |
|
624 | + foreach ( $processor->discounts as $discount ) { |
|
625 | + $this->add_discount( $discount ); |
|
626 | + } |
|
627 | + |
|
628 | + do_action_ref_array( 'getpaid_submissions_process_discounts', array( &$this ) ); |
|
629 | + } |
|
630 | + |
|
631 | + /** |
|
632 | + * Adds a discount to the submission. |
|
633 | + * |
|
634 | + * @param array $discount An array of discount details. name, initial_discount, and recurring_discount are required. Include discount_code if the discount is from a discount code. |
|
635 | + * @since 1.0.19 |
|
636 | + */ |
|
637 | + public function add_discount( $discount ) { |
|
638 | + $this->discounts[ $discount['name'] ] = $discount; |
|
639 | + $this->totals['discount']['initial'] += wpinv_sanitize_amount( $discount['initial_discount'] ); |
|
640 | + $this->totals['discount']['recurring'] += wpinv_sanitize_amount( $discount['recurring_discount'] ); |
|
641 | + } |
|
642 | + |
|
643 | + /** |
|
644 | + * Removes a discount from the submission. |
|
645 | + * |
|
646 | + * @since 1.0.19 |
|
647 | + */ |
|
648 | + public function remove_discount( $name ) { |
|
649 | + |
|
650 | + if ( isset( $this->discounts[ $name ] ) ) { |
|
651 | + $this->totals['discount']['initial'] -= $this->discounts[ $name ]['initial_discount']; |
|
652 | + $this->totals['discount']['recurring'] -= $this->discounts[ $name ]['recurring_discount']; |
|
653 | + unset( $this->discounts[ $name ] ); |
|
654 | + } |
|
655 | + |
|
656 | + } |
|
657 | + |
|
658 | + /** |
|
659 | + * Checks whether there is a discount code associated with this submission. |
|
660 | + * |
|
661 | + * @since 1.0.19 |
|
662 | + * @return bool |
|
663 | + */ |
|
664 | + public function has_discount_code() { |
|
665 | + return ! empty( $this->discounts['discount_code'] ); |
|
666 | + } |
|
667 | + |
|
668 | + /** |
|
669 | + * Returns the discount code. |
|
670 | + * |
|
671 | + * @since 1.0.19 |
|
672 | + * @return string |
|
673 | + */ |
|
674 | + public function get_discount_code() { |
|
675 | + return $this->has_discount_code() ? $this->discounts['discount_code']['discount_code'] : ''; |
|
676 | + } |
|
677 | + |
|
678 | + /** |
|
679 | + * Returns the discount. |
|
680 | + * |
|
681 | + * @since 1.0.19 |
|
682 | + */ |
|
683 | + public function get_discount() { |
|
684 | + return $this->totals['discount']['initial']; |
|
685 | + } |
|
686 | + |
|
687 | + /** |
|
688 | + * Returns the recurring discount. |
|
689 | + * |
|
690 | + * @since 1.0.19 |
|
691 | + */ |
|
692 | + public function get_recurring_discount() { |
|
693 | + return $this->totals['discount']['recurring']; |
|
694 | + } |
|
695 | + |
|
696 | + /** |
|
697 | + * Returns all discounts. |
|
698 | + * |
|
699 | + * @since 1.0.19 |
|
700 | + */ |
|
701 | + public function get_discounts() { |
|
702 | + return $this->discounts; |
|
703 | + } |
|
704 | + |
|
705 | + /* |
|
706 | 706 | |-------------------------------------------------------------------------- |
707 | 707 | | Fees |
708 | 708 | |-------------------------------------------------------------------------- |
@@ -712,100 +712,100 @@ discard block |
||
712 | 712 | | fees. |
713 | 713 | */ |
714 | 714 | |
715 | - /** |
|
716 | - * Prepares the submission's fees. |
|
717 | - * |
|
718 | - * @since 1.0.19 |
|
719 | - */ |
|
720 | - public function process_fees() { |
|
721 | - |
|
722 | - $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
|
723 | - |
|
724 | - foreach ( $fees_processor->fees as $fee ) { |
|
725 | - $this->add_fee( $fee ); |
|
726 | - } |
|
727 | - |
|
728 | - do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
|
729 | - } |
|
730 | - |
|
731 | - /** |
|
732 | - * Adds a fee to the submission. |
|
733 | - * |
|
734 | - * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
|
735 | - * @since 1.0.19 |
|
736 | - */ |
|
737 | - public function add_fee( $fee ) { |
|
738 | - |
|
739 | - if ( $fee['name'] == 'shipping' ) { |
|
740 | - $this->totals['shipping']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
741 | - $this->totals['shipping']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
742 | - return; |
|
743 | - } |
|
744 | - |
|
745 | - $this->fees[ $fee['name'] ] = $fee; |
|
746 | - $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
747 | - $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
748 | - |
|
749 | - } |
|
750 | - |
|
751 | - /** |
|
752 | - * Removes a fee from the submission. |
|
753 | - * |
|
754 | - * @since 1.0.19 |
|
755 | - */ |
|
756 | - public function remove_fee( $name ) { |
|
757 | - |
|
758 | - if ( isset( $this->fees[ $name ] ) ) { |
|
759 | - $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
|
760 | - $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
|
761 | - unset( $this->fees[ $name ] ); |
|
762 | - } |
|
763 | - |
|
764 | - if ( 'shipping' == $name ) { |
|
765 | - $this->totals['shipping']['initial'] = 0; |
|
766 | - $this->totals['shipping']['recurring'] = 0; |
|
767 | - } |
|
768 | - |
|
769 | - } |
|
770 | - |
|
771 | - /** |
|
772 | - * Returns the fees. |
|
773 | - * |
|
774 | - * @since 1.0.19 |
|
775 | - */ |
|
776 | - public function get_fee() { |
|
777 | - return $this->totals['fees']['initial']; |
|
778 | - } |
|
779 | - |
|
780 | - /** |
|
781 | - * Returns the recurring fees. |
|
782 | - * |
|
783 | - * @since 1.0.19 |
|
784 | - */ |
|
785 | - public function get_recurring_fee() { |
|
786 | - return $this->totals['fees']['recurring']; |
|
787 | - } |
|
788 | - |
|
789 | - /** |
|
790 | - * Returns all fees. |
|
791 | - * |
|
792 | - * @since 1.0.19 |
|
793 | - */ |
|
794 | - public function get_fees() { |
|
795 | - return $this->fees; |
|
796 | - } |
|
797 | - |
|
798 | - /** |
|
799 | - * Checks if there are any fees for the form. |
|
800 | - * |
|
801 | - * @return bool |
|
802 | - * @since 1.0.19 |
|
803 | - */ |
|
804 | - public function has_fees() { |
|
805 | - return count( $this->fees ) !== 0; |
|
806 | - } |
|
807 | - |
|
808 | - /* |
|
715 | + /** |
|
716 | + * Prepares the submission's fees. |
|
717 | + * |
|
718 | + * @since 1.0.19 |
|
719 | + */ |
|
720 | + public function process_fees() { |
|
721 | + |
|
722 | + $fees_processor = new GetPaid_Payment_Form_Submission_Fees( $this ); |
|
723 | + |
|
724 | + foreach ( $fees_processor->fees as $fee ) { |
|
725 | + $this->add_fee( $fee ); |
|
726 | + } |
|
727 | + |
|
728 | + do_action_ref_array( 'getpaid_submissions_process_fees', array( &$this ) ); |
|
729 | + } |
|
730 | + |
|
731 | + /** |
|
732 | + * Adds a fee to the submission. |
|
733 | + * |
|
734 | + * @param array $fee An array of fee details. name, initial_fee, and recurring_fee are required. |
|
735 | + * @since 1.0.19 |
|
736 | + */ |
|
737 | + public function add_fee( $fee ) { |
|
738 | + |
|
739 | + if ( $fee['name'] == 'shipping' ) { |
|
740 | + $this->totals['shipping']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
741 | + $this->totals['shipping']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
742 | + return; |
|
743 | + } |
|
744 | + |
|
745 | + $this->fees[ $fee['name'] ] = $fee; |
|
746 | + $this->totals['fees']['initial'] += wpinv_sanitize_amount( $fee['initial_fee'] ); |
|
747 | + $this->totals['fees']['recurring'] += wpinv_sanitize_amount( $fee['recurring_fee'] ); |
|
748 | + |
|
749 | + } |
|
750 | + |
|
751 | + /** |
|
752 | + * Removes a fee from the submission. |
|
753 | + * |
|
754 | + * @since 1.0.19 |
|
755 | + */ |
|
756 | + public function remove_fee( $name ) { |
|
757 | + |
|
758 | + if ( isset( $this->fees[ $name ] ) ) { |
|
759 | + $this->totals['fees']['initial'] -= $this->fees[ $name ]['initial_fee']; |
|
760 | + $this->totals['fees']['recurring'] -= $this->fees[ $name ]['recurring_fee']; |
|
761 | + unset( $this->fees[ $name ] ); |
|
762 | + } |
|
763 | + |
|
764 | + if ( 'shipping' == $name ) { |
|
765 | + $this->totals['shipping']['initial'] = 0; |
|
766 | + $this->totals['shipping']['recurring'] = 0; |
|
767 | + } |
|
768 | + |
|
769 | + } |
|
770 | + |
|
771 | + /** |
|
772 | + * Returns the fees. |
|
773 | + * |
|
774 | + * @since 1.0.19 |
|
775 | + */ |
|
776 | + public function get_fee() { |
|
777 | + return $this->totals['fees']['initial']; |
|
778 | + } |
|
779 | + |
|
780 | + /** |
|
781 | + * Returns the recurring fees. |
|
782 | + * |
|
783 | + * @since 1.0.19 |
|
784 | + */ |
|
785 | + public function get_recurring_fee() { |
|
786 | + return $this->totals['fees']['recurring']; |
|
787 | + } |
|
788 | + |
|
789 | + /** |
|
790 | + * Returns all fees. |
|
791 | + * |
|
792 | + * @since 1.0.19 |
|
793 | + */ |
|
794 | + public function get_fees() { |
|
795 | + return $this->fees; |
|
796 | + } |
|
797 | + |
|
798 | + /** |
|
799 | + * Checks if there are any fees for the form. |
|
800 | + * |
|
801 | + * @return bool |
|
802 | + * @since 1.0.19 |
|
803 | + */ |
|
804 | + public function has_fees() { |
|
805 | + return count( $this->fees ) !== 0; |
|
806 | + } |
|
807 | + |
|
808 | + /* |
|
809 | 809 | |-------------------------------------------------------------------------- |
810 | 810 | | MISC |
811 | 811 | |-------------------------------------------------------------------------- |
@@ -813,147 +813,147 @@ discard block |
||
813 | 813 | | Extra submission functions. |
814 | 814 | */ |
815 | 815 | |
816 | - /** |
|
817 | - * Returns the shipping amount. |
|
818 | - * |
|
819 | - * @since 1.0.19 |
|
820 | - */ |
|
821 | - public function get_shipping() { |
|
822 | - return $this->totals['shipping']['initial']; |
|
823 | - } |
|
824 | - |
|
825 | - /** |
|
826 | - * Returns the recurring shipping. |
|
827 | - * |
|
828 | - * @since 1.0.19 |
|
829 | - */ |
|
830 | - public function get_recurring_shipping() { |
|
831 | - return $this->totals['shipping']['recurring']; |
|
832 | - } |
|
833 | - |
|
834 | - /** |
|
835 | - * Checks if there are any shipping fees for the form. |
|
836 | - * |
|
837 | - * @return bool |
|
838 | - * @since 1.0.19 |
|
839 | - */ |
|
840 | - public function has_shipping() { |
|
841 | - return apply_filters( 'getpaid_payment_form_has_shipping', false, $this ); |
|
842 | - } |
|
843 | - |
|
844 | - /** |
|
845 | - * Checks if this is the initial fetch. |
|
846 | - * |
|
847 | - * @return bool |
|
848 | - * @since 1.0.19 |
|
849 | - */ |
|
850 | - public function is_initial_fetch() { |
|
851 | - return empty( $this->data['initial_state'] ); |
|
852 | - } |
|
853 | - |
|
854 | - /** |
|
855 | - * Returns the total amount to collect for this submission. |
|
856 | - * |
|
857 | - * @since 1.0.19 |
|
858 | - */ |
|
859 | - public function get_total() { |
|
860 | - $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() + $this->get_shipping() - $this->get_discount(); |
|
861 | - return max( $total, 0 ); |
|
862 | - } |
|
863 | - |
|
864 | - /** |
|
865 | - * Returns the recurring total amount to collect for this submission. |
|
866 | - * |
|
867 | - * @since 1.0.19 |
|
868 | - */ |
|
869 | - public function get_recurring_total() { |
|
870 | - $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() + $this->get_recurring_shipping() - $this->get_recurring_discount(); |
|
871 | - return max( $total, 0 ); |
|
872 | - } |
|
873 | - |
|
874 | - /** |
|
875 | - * Whether payment details should be collected for this submission. |
|
876 | - * |
|
877 | - * @since 1.0.19 |
|
878 | - */ |
|
879 | - public function should_collect_payment_details() { |
|
880 | - $initial = $this->get_total(); |
|
881 | - $recurring = $this->get_recurring_total(); |
|
882 | - |
|
883 | - if ( $this->has_recurring == 0 ) { |
|
884 | - $recurring = 0; |
|
885 | - } |
|
886 | - |
|
887 | - $collect = $initial > 0 || $recurring > 0; |
|
888 | - return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
|
889 | - } |
|
890 | - |
|
891 | - /** |
|
892 | - * Returns the billing email of the user. |
|
893 | - * |
|
894 | - * @since 1.0.19 |
|
895 | - */ |
|
896 | - public function get_billing_email() { |
|
897 | - return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
|
898 | - } |
|
899 | - |
|
900 | - /** |
|
901 | - * Checks if the submitter has a billing email. |
|
902 | - * |
|
903 | - * @since 1.0.19 |
|
904 | - */ |
|
905 | - public function has_billing_email() { |
|
906 | - $billing_email = $this->get_billing_email(); |
|
907 | - return ! empty( $billing_email ) && is_email( $billing_email ); |
|
908 | - } |
|
909 | - |
|
910 | - /** |
|
911 | - * Returns the appropriate currency for the submission. |
|
912 | - * |
|
913 | - * @since 1.0.19 |
|
914 | - * @return string |
|
915 | - */ |
|
916 | - public function get_currency() { |
|
917 | - return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency(); |
|
918 | - } |
|
919 | - |
|
920 | - /** |
|
921 | - * Returns the raw submission data. |
|
922 | - * |
|
923 | - * @since 1.0.19 |
|
924 | - * @return array |
|
925 | - */ |
|
926 | - public function get_data() { |
|
927 | - return $this->data; |
|
928 | - } |
|
929 | - |
|
930 | - /** |
|
931 | - * Returns a field from the submission data |
|
932 | - * |
|
933 | - * @param string $field |
|
934 | - * @since 1.0.19 |
|
935 | - * @return mixed|null |
|
936 | - */ |
|
937 | - public function get_field( $field, $sub_array_key = null ) { |
|
938 | - return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
|
939 | - } |
|
940 | - |
|
941 | - /** |
|
942 | - * Checks if a required field is set. |
|
943 | - * |
|
944 | - * @since 1.0.19 |
|
945 | - */ |
|
946 | - public function is_required_field_set( $field ) { |
|
947 | - return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
948 | - } |
|
949 | - |
|
950 | - /** |
|
951 | - * Formats an amount |
|
952 | - * |
|
953 | - * @since 1.0.19 |
|
954 | - */ |
|
955 | - public function format_amount( $amount ) { |
|
956 | - return wpinv_price( $amount, $this->get_currency() ); |
|
957 | - } |
|
816 | + /** |
|
817 | + * Returns the shipping amount. |
|
818 | + * |
|
819 | + * @since 1.0.19 |
|
820 | + */ |
|
821 | + public function get_shipping() { |
|
822 | + return $this->totals['shipping']['initial']; |
|
823 | + } |
|
824 | + |
|
825 | + /** |
|
826 | + * Returns the recurring shipping. |
|
827 | + * |
|
828 | + * @since 1.0.19 |
|
829 | + */ |
|
830 | + public function get_recurring_shipping() { |
|
831 | + return $this->totals['shipping']['recurring']; |
|
832 | + } |
|
833 | + |
|
834 | + /** |
|
835 | + * Checks if there are any shipping fees for the form. |
|
836 | + * |
|
837 | + * @return bool |
|
838 | + * @since 1.0.19 |
|
839 | + */ |
|
840 | + public function has_shipping() { |
|
841 | + return apply_filters( 'getpaid_payment_form_has_shipping', false, $this ); |
|
842 | + } |
|
843 | + |
|
844 | + /** |
|
845 | + * Checks if this is the initial fetch. |
|
846 | + * |
|
847 | + * @return bool |
|
848 | + * @since 1.0.19 |
|
849 | + */ |
|
850 | + public function is_initial_fetch() { |
|
851 | + return empty( $this->data['initial_state'] ); |
|
852 | + } |
|
853 | + |
|
854 | + /** |
|
855 | + * Returns the total amount to collect for this submission. |
|
856 | + * |
|
857 | + * @since 1.0.19 |
|
858 | + */ |
|
859 | + public function get_total() { |
|
860 | + $total = $this->get_subtotal() + $this->get_fee() + $this->get_tax() + $this->get_shipping() - $this->get_discount(); |
|
861 | + return max( $total, 0 ); |
|
862 | + } |
|
863 | + |
|
864 | + /** |
|
865 | + * Returns the recurring total amount to collect for this submission. |
|
866 | + * |
|
867 | + * @since 1.0.19 |
|
868 | + */ |
|
869 | + public function get_recurring_total() { |
|
870 | + $total = $this->get_recurring_subtotal() + $this->get_recurring_fee() + $this->get_recurring_tax() + $this->get_recurring_shipping() - $this->get_recurring_discount(); |
|
871 | + return max( $total, 0 ); |
|
872 | + } |
|
873 | + |
|
874 | + /** |
|
875 | + * Whether payment details should be collected for this submission. |
|
876 | + * |
|
877 | + * @since 1.0.19 |
|
878 | + */ |
|
879 | + public function should_collect_payment_details() { |
|
880 | + $initial = $this->get_total(); |
|
881 | + $recurring = $this->get_recurring_total(); |
|
882 | + |
|
883 | + if ( $this->has_recurring == 0 ) { |
|
884 | + $recurring = 0; |
|
885 | + } |
|
886 | + |
|
887 | + $collect = $initial > 0 || $recurring > 0; |
|
888 | + return apply_filters( 'getpaid_submission_should_collect_payment_details', $collect, $this ); |
|
889 | + } |
|
890 | + |
|
891 | + /** |
|
892 | + * Returns the billing email of the user. |
|
893 | + * |
|
894 | + * @since 1.0.19 |
|
895 | + */ |
|
896 | + public function get_billing_email() { |
|
897 | + return apply_filters( 'getpaid_get_submission_billing_email', $this->get_field( 'billing_email' ), $this ); |
|
898 | + } |
|
899 | + |
|
900 | + /** |
|
901 | + * Checks if the submitter has a billing email. |
|
902 | + * |
|
903 | + * @since 1.0.19 |
|
904 | + */ |
|
905 | + public function has_billing_email() { |
|
906 | + $billing_email = $this->get_billing_email(); |
|
907 | + return ! empty( $billing_email ) && is_email( $billing_email ); |
|
908 | + } |
|
909 | + |
|
910 | + /** |
|
911 | + * Returns the appropriate currency for the submission. |
|
912 | + * |
|
913 | + * @since 1.0.19 |
|
914 | + * @return string |
|
915 | + */ |
|
916 | + public function get_currency() { |
|
917 | + return $this->has_invoice() ? $this->invoice->get_currency() : wpinv_get_currency(); |
|
918 | + } |
|
919 | + |
|
920 | + /** |
|
921 | + * Returns the raw submission data. |
|
922 | + * |
|
923 | + * @since 1.0.19 |
|
924 | + * @return array |
|
925 | + */ |
|
926 | + public function get_data() { |
|
927 | + return $this->data; |
|
928 | + } |
|
929 | + |
|
930 | + /** |
|
931 | + * Returns a field from the submission data |
|
932 | + * |
|
933 | + * @param string $field |
|
934 | + * @since 1.0.19 |
|
935 | + * @return mixed|null |
|
936 | + */ |
|
937 | + public function get_field( $field, $sub_array_key = null ) { |
|
938 | + return getpaid_get_array_field( $this->data, $field, $sub_array_key ); |
|
939 | + } |
|
940 | + |
|
941 | + /** |
|
942 | + * Checks if a required field is set. |
|
943 | + * |
|
944 | + * @since 1.0.19 |
|
945 | + */ |
|
946 | + public function is_required_field_set( $field ) { |
|
947 | + return empty( $field['required'] ) || ! empty( $this->data[ $field['id'] ] ); |
|
948 | + } |
|
949 | + |
|
950 | + /** |
|
951 | + * Formats an amount |
|
952 | + * |
|
953 | + * @since 1.0.19 |
|
954 | + */ |
|
955 | + public function format_amount( $amount ) { |
|
956 | + return wpinv_price( $amount, $this->get_currency() ); |
|
957 | + } |
|
958 | 958 | |
959 | 959 | } |
@@ -26,71 +26,71 @@ discard block |
||
26 | 26 | |
27 | 27 | <?php |
28 | 28 | |
29 | - // Fires before printing a line item column. |
|
30 | - do_action( "getpaid_form_cart_item_before_$key", $item, $form ); |
|
29 | + // Fires before printing a line item column. |
|
30 | + do_action( "getpaid_form_cart_item_before_$key", $item, $form ); |
|
31 | 31 | |
32 | - // Item name. |
|
33 | - if ( 'name' === $key ) { |
|
32 | + // Item name. |
|
33 | + if ( 'name' === $key ) { |
|
34 | 34 | |
35 | 35 | |
36 | - ob_start(); |
|
36 | + ob_start(); |
|
37 | 37 | |
38 | - // Add an optional description. |
|
39 | - $description = $item->get_description(); |
|
38 | + // Add an optional description. |
|
39 | + $description = $item->get_description(); |
|
40 | 40 | |
41 | - if ( ! empty( $description ) ) { |
|
42 | - echo "<small class='form-text text-muted pr-2 m-0'>" . wp_kses_post( $description ) . '</small>'; |
|
43 | - } |
|
41 | + if ( ! empty( $description ) ) { |
|
42 | + echo "<small class='form-text text-muted pr-2 m-0'>" . wp_kses_post( $description ) . '</small>'; |
|
43 | + } |
|
44 | 44 | |
45 | - // Price help text. |
|
46 | - $description = getpaid_item_recurring_price_help_text( $item, $currency ); |
|
47 | - if ( $description ) { |
|
48 | - echo "<small class='getpaid-form-item-price-desc form-text text-muted font-italic pr-2 m-0'>" . wp_kses_post( $description ) . '</small>'; |
|
49 | - } |
|
45 | + // Price help text. |
|
46 | + $description = getpaid_item_recurring_price_help_text( $item, $currency ); |
|
47 | + if ( $description ) { |
|
48 | + echo "<small class='getpaid-form-item-price-desc form-text text-muted font-italic pr-2 m-0'>" . wp_kses_post( $description ) . '</small>'; |
|
49 | + } |
|
50 | 50 | |
51 | - do_action( 'getpaid_payment_form_cart_item_description', $item, $form ); |
|
51 | + do_action( 'getpaid_payment_form_cart_item_description', $item, $form ); |
|
52 | 52 | |
53 | - if ( wpinv_current_user_can_manage_invoicing() ) { |
|
53 | + if ( wpinv_current_user_can_manage_invoicing() ) { |
|
54 | 54 | |
55 | - edit_post_link( |
|
56 | - __( 'Edit this item.', 'invoicing' ), |
|
57 | - '<small class="form-text text-muted">', |
|
58 | - '</small>', |
|
59 | - $item->get_id(), |
|
60 | - 'text-danger' |
|
61 | - ); |
|
55 | + edit_post_link( |
|
56 | + __( 'Edit this item.', 'invoicing' ), |
|
57 | + '<small class="form-text text-muted">', |
|
58 | + '</small>', |
|
59 | + $item->get_id(), |
|
60 | + 'text-danger' |
|
61 | + ); |
|
62 | 62 | |
63 | - } |
|
63 | + } |
|
64 | 64 | |
65 | - $description = ob_get_clean(); |
|
65 | + $description = ob_get_clean(); |
|
66 | 66 | |
67 | - // Display the name. |
|
68 | - $tootip = empty( $description ) ? '' : ' <i class="fas fa-xs fa-info gp-tooltip d-sm-none text-muted"></i>'; |
|
67 | + // Display the name. |
|
68 | + $tootip = empty( $description ) ? '' : ' <i class="fas fa-xs fa-info gp-tooltip d-sm-none text-muted"></i>'; |
|
69 | 69 | |
70 | - $has_featured_image = has_post_thumbnail( $item->get_id() ); |
|
70 | + $has_featured_image = has_post_thumbnail( $item->get_id() ); |
|
71 | 71 | |
72 | - if ( $has_featured_image ) { |
|
73 | - echo '<div class="d-flex align-items-center getpaid-form-item-has-featured-image">'; |
|
74 | - echo '<div class="getpaid-form-item-image-container mr-2">'; |
|
75 | - echo get_the_post_thumbnail( $item->get_id(), 'thumbnail', array( 'class' => 'getpaid-form-item-image mb-0' ) ); |
|
76 | - echo '</div>'; |
|
77 | - echo '<div class="getpaid-form-item-name-container">'; |
|
78 | - } |
|
72 | + if ( $has_featured_image ) { |
|
73 | + echo '<div class="d-flex align-items-center getpaid-form-item-has-featured-image">'; |
|
74 | + echo '<div class="getpaid-form-item-image-container mr-2">'; |
|
75 | + echo get_the_post_thumbnail( $item->get_id(), 'thumbnail', array( 'class' => 'getpaid-form-item-image mb-0' ) ); |
|
76 | + echo '</div>'; |
|
77 | + echo '<div class="getpaid-form-item-name-container">'; |
|
78 | + } |
|
79 | 79 | |
80 | - echo '<div class="mb-1 font-weight-bold">' . esc_html( $item->get_name() ) . wp_kses_post( $tootip ) . '</div>'; |
|
80 | + echo '<div class="mb-1 font-weight-bold">' . esc_html( $item->get_name() ) . wp_kses_post( $tootip ) . '</div>'; |
|
81 | 81 | |
82 | - if ( ! empty( $description ) ) { |
|
83 | - printf( '<span class="d-none d-sm-block getpaid-item-desc">%s</span>', wp_kses_post( $description ) ); |
|
84 | - } |
|
82 | + if ( ! empty( $description ) ) { |
|
83 | + printf( '<span class="d-none d-sm-block getpaid-item-desc">%s</span>', wp_kses_post( $description ) ); |
|
84 | + } |
|
85 | 85 | |
86 | - if ( $item->allows_quantities() ) { |
|
87 | - printf( |
|
88 | - '<small class="d-sm-none text-muted form-text">%s</small>', |
|
89 | - sprintf( |
|
90 | - // translators: %s is the item quantity. |
|
91 | - esc_html__( 'Qty %s', 'invoicing' ), |
|
92 | - sprintf( |
|
93 | - '<input |
|
86 | + if ( $item->allows_quantities() ) { |
|
87 | + printf( |
|
88 | + '<small class="d-sm-none text-muted form-text">%s</small>', |
|
89 | + sprintf( |
|
90 | + // translators: %s is the item quantity. |
|
91 | + esc_html__( 'Qty %s', 'invoicing' ), |
|
92 | + sprintf( |
|
93 | + '<input |
|
94 | 94 | type="number" |
95 | 95 | step="0.01" |
96 | 96 | style="width: 48px;" |
@@ -99,62 +99,62 @@ discard block |
||
99 | 99 | min="1" |
100 | 100 | max="%s" |
101 | 101 | >', |
102 | - (float) $item->get_quantity() == 0 ? 1 : (float) $item->get_quantity(), |
|
103 | - floatval( null !== $max_qty ? $max_qty : 1000000000000 ) |
|
104 | - ) |
|
105 | - ) |
|
106 | - ); |
|
107 | - } else { |
|
108 | - printf( |
|
109 | - '<small class="d-sm-none text-muted form-text">%s</small>', |
|
110 | - sprintf( |
|
111 | - // translators: %s is the item quantity. |
|
112 | - esc_html__( 'Qty %s', 'invoicing' ), |
|
113 | - (float) $item->get_quantity() |
|
114 | - ) |
|
115 | - ); |
|
116 | - } |
|
117 | - |
|
118 | - if ( $has_featured_image ) { |
|
119 | - echo '</div>'; |
|
120 | - echo '</div>'; |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - // Item price. |
|
125 | - if ( 'price' === $key ) { |
|
126 | - |
|
127 | - // Set the currency position. |
|
128 | - $position = wpinv_currency_position(); |
|
129 | - |
|
130 | - if ( 'left_space' === $position ) { |
|
131 | - $position = 'left'; |
|
132 | - } |
|
133 | - |
|
134 | - if ( 'right_space' === $position ) { |
|
135 | - $position = 'right'; |
|
136 | - } |
|
137 | - |
|
138 | - if ( $item->user_can_set_their_price() ) { |
|
139 | - $price = max( (float) $item->get_price(), (float) $item->get_minimum_price() ); |
|
140 | - $minimum = (float) $item->get_minimum_price(); |
|
141 | - $validate_minimum = ''; |
|
142 | - $class = ''; |
|
143 | - $data_minimum = ''; |
|
144 | - |
|
145 | - if ( $minimum > 0 ) { |
|
146 | - $validate_minimum = sprintf( |
|
147 | - // translators: %s is the minimum price. |
|
148 | - esc_attr__( 'The minimum allowed amount is %s', 'invoicing' ), |
|
149 | - wp_strip_all_tags( wpinv_price( $minimum, $currency ) ) |
|
150 | - ); |
|
151 | - |
|
152 | - $class = 'getpaid-validate-minimum-amount'; |
|
153 | - |
|
154 | - $data_minimum = "data-minimum-amount='" . esc_attr( getpaid_unstandardize_amount( $minimum ) ) . "'"; |
|
155 | - } |
|
156 | - |
|
157 | - ?> |
|
102 | + (float) $item->get_quantity() == 0 ? 1 : (float) $item->get_quantity(), |
|
103 | + floatval( null !== $max_qty ? $max_qty : 1000000000000 ) |
|
104 | + ) |
|
105 | + ) |
|
106 | + ); |
|
107 | + } else { |
|
108 | + printf( |
|
109 | + '<small class="d-sm-none text-muted form-text">%s</small>', |
|
110 | + sprintf( |
|
111 | + // translators: %s is the item quantity. |
|
112 | + esc_html__( 'Qty %s', 'invoicing' ), |
|
113 | + (float) $item->get_quantity() |
|
114 | + ) |
|
115 | + ); |
|
116 | + } |
|
117 | + |
|
118 | + if ( $has_featured_image ) { |
|
119 | + echo '</div>'; |
|
120 | + echo '</div>'; |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + // Item price. |
|
125 | + if ( 'price' === $key ) { |
|
126 | + |
|
127 | + // Set the currency position. |
|
128 | + $position = wpinv_currency_position(); |
|
129 | + |
|
130 | + if ( 'left_space' === $position ) { |
|
131 | + $position = 'left'; |
|
132 | + } |
|
133 | + |
|
134 | + if ( 'right_space' === $position ) { |
|
135 | + $position = 'right'; |
|
136 | + } |
|
137 | + |
|
138 | + if ( $item->user_can_set_their_price() ) { |
|
139 | + $price = max( (float) $item->get_price(), (float) $item->get_minimum_price() ); |
|
140 | + $minimum = (float) $item->get_minimum_price(); |
|
141 | + $validate_minimum = ''; |
|
142 | + $class = ''; |
|
143 | + $data_minimum = ''; |
|
144 | + |
|
145 | + if ( $minimum > 0 ) { |
|
146 | + $validate_minimum = sprintf( |
|
147 | + // translators: %s is the minimum price. |
|
148 | + esc_attr__( 'The minimum allowed amount is %s', 'invoicing' ), |
|
149 | + wp_strip_all_tags( wpinv_price( $minimum, $currency ) ) |
|
150 | + ); |
|
151 | + |
|
152 | + $class = 'getpaid-validate-minimum-amount'; |
|
153 | + |
|
154 | + $data_minimum = "data-minimum-amount='" . esc_attr( getpaid_unstandardize_amount( $minimum ) ) . "'"; |
|
155 | + } |
|
156 | + |
|
157 | + ?> |
|
158 | 158 | <div class="input-group input-group-sm"> |
159 | 159 | <?php if ( 'left' === $position ) : ?> |
160 | 160 | <?php if ( empty( $GLOBALS['aui_bs5'] ) ) : ?> |
@@ -195,46 +195,46 @@ discard block |
||
195 | 195 | |
196 | 196 | <?php |
197 | 197 | |
198 | - } else { |
|
199 | - ?> |
|
198 | + } else { |
|
199 | + ?> |
|
200 | 200 | <span class="getpaid-items-<?php echo (int) $item->get_id(); ?>-view-price"> |
201 | 201 | <?php echo wp_kses_post( wpinv_price( $item->get_price(), $currency ) ); ?> |
202 | 202 | </span> |
203 | 203 | <input name='getpaid-items[<?php echo (int) $item->get_id(); ?>][price]' type='hidden' class='getpaid-item-price-input' value='<?php echo esc_attr( $item->get_price() ); ?>'> |
204 | 204 | <?php |
205 | - } |
|
205 | + } |
|
206 | 206 | |
207 | - printf( |
|
207 | + printf( |
|
208 | 208 | '<small class="d-sm-none text-muted form-text getpaid-mobile-item-subtotal">%s</small>', |
209 | - // translators: %s is the item subtotal. |
|
209 | + // translators: %s is the item subtotal. |
|
210 | 210 | sprintf( esc_html__( 'Subtotal: %s', 'invoicing' ), wp_kses_post( wpinv_price( $item->get_sub_total(), $currency ) ) ) |
211 | 211 | ); |
212 | - } |
|
212 | + } |
|
213 | 213 | |
214 | - // Item quantity. |
|
215 | - if ( 'quantity' === $key ) { |
|
214 | + // Item quantity. |
|
215 | + if ( 'quantity' === $key ) { |
|
216 | 216 | |
217 | - if ( $item->allows_quantities() ) { |
|
218 | - ?> |
|
217 | + if ( $item->allows_quantities() ) { |
|
218 | + ?> |
|
219 | 219 | <input name='getpaid-items[<?php echo (int) $item->get_id(); ?>][quantity]' type="number" step="any" style='width: 64px; line-height: 1; min-height: 35px;' class='getpaid-item-quantity-input p-1 align-middle font-weight-normal shadow-none m-0 rounded-0 text-center border' value='<?php echo (float) $item->get_quantity() == 0 ? 1 : (float) $item->get_quantity(); ?>' min='1' <?php echo null !== $max_qty ? 'max="' . (float) $max_qty . '"' : ''; ?> required> |
220 | 220 | <?php |
221 | - } else { |
|
222 | - ?> |
|
221 | + } else { |
|
222 | + ?> |
|
223 | 223 | <span class="getpaid-items-<?php echo (int) $item->get_id(); ?>-view-quantity"> |
224 | 224 | <?php echo (float) $item->get_quantity(); ?> |
225 | 225 | </span> |
226 | 226 | <input type='hidden' name='getpaid-items[<?php echo (int) $item->get_id(); ?>][quantity]' class='getpaid-item-quantity-input' value='<?php echo (float) $item->get_quantity(); ?>'> |
227 | 227 | <?php |
228 | - } |
|
228 | + } |
|
229 | 229 | } |
230 | 230 | |
231 | - // Item sub total. |
|
232 | - if ( 'subtotal' === $key ) { |
|
233 | - echo wp_kses_post( wpinv_price( $item->get_sub_total(), $currency ) ); |
|
234 | - } |
|
231 | + // Item sub total. |
|
232 | + if ( 'subtotal' === $key ) { |
|
233 | + echo wp_kses_post( wpinv_price( $item->get_sub_total(), $currency ) ); |
|
234 | + } |
|
235 | 235 | |
236 | - do_action( "getpaid_payment_form_cart_item_$key", $item, $form ); |
|
237 | - ?> |
|
236 | + do_action( "getpaid_payment_form_cart_item_$key", $item, $form ); |
|
237 | + ?> |
|
238 | 238 | |
239 | 239 | </div> |
240 | 240 |
@@ -12,51 +12,51 @@ 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 | - $invoice = $submission->get_invoice(); |
|
31 | - $force_prices = array(); |
|
32 | - |
|
33 | - // Prepare the selected items. |
|
34 | - $selected_items = array(); |
|
35 | - if ( ! empty( $data['getpaid-items'] ) ) { |
|
36 | - $selected_items = wpinv_clean( $data['getpaid-items'] ); |
|
37 | - |
|
38 | - if ( ! empty( $invoice ) && $submission->is_initial_fetch() ) { |
|
39 | - foreach ( $invoice->get_items() as $invoice_item ) { |
|
40 | - if ( isset( $selected_items[ $invoice_item->get_id() ] ) ) { |
|
41 | - $selected_items[ $invoice_item->get_id() ]['quantity'] = $invoice_item->get_quantity(); |
|
42 | - $selected_items[ $invoice_item->get_id() ]['price'] = $invoice_item->get_price(); |
|
43 | - |
|
44 | - $force_prices[ $invoice_item->get_id() ] = $invoice_item->get_price(); |
|
45 | - } |
|
46 | - } |
|
47 | - } |
|
48 | - } |
|
49 | - |
|
50 | - // (Maybe) set form items. |
|
51 | - if ( isset( $data['getpaid-form-items'] ) ) { |
|
52 | - |
|
53 | - // Confirm items key. |
|
54 | - $form_items = wpinv_clean( $data['getpaid-form-items'] ); |
|
55 | - if ( ! isset( $data['getpaid-form-items-key'] ) || md5( NONCE_KEY . AUTH_KEY . $form_items ) !== $data['getpaid-form-items-key'] ) { |
|
56 | - throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) ); |
|
57 | - } |
|
58 | - |
|
59 | - $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 | + $invoice = $submission->get_invoice(); |
|
31 | + $force_prices = array(); |
|
32 | + |
|
33 | + // Prepare the selected items. |
|
34 | + $selected_items = array(); |
|
35 | + if ( ! empty( $data['getpaid-items'] ) ) { |
|
36 | + $selected_items = wpinv_clean( $data['getpaid-items'] ); |
|
37 | + |
|
38 | + if ( ! empty( $invoice ) && $submission->is_initial_fetch() ) { |
|
39 | + foreach ( $invoice->get_items() as $invoice_item ) { |
|
40 | + if ( isset( $selected_items[ $invoice_item->get_id() ] ) ) { |
|
41 | + $selected_items[ $invoice_item->get_id() ]['quantity'] = $invoice_item->get_quantity(); |
|
42 | + $selected_items[ $invoice_item->get_id() ]['price'] = $invoice_item->get_price(); |
|
43 | + |
|
44 | + $force_prices[ $invoice_item->get_id() ] = $invoice_item->get_price(); |
|
45 | + } |
|
46 | + } |
|
47 | + } |
|
48 | + } |
|
49 | + |
|
50 | + // (Maybe) set form items. |
|
51 | + if ( isset( $data['getpaid-form-items'] ) ) { |
|
52 | + |
|
53 | + // Confirm items key. |
|
54 | + $form_items = wpinv_clean( $data['getpaid-form-items'] ); |
|
55 | + if ( ! isset( $data['getpaid-form-items-key'] ) || md5( NONCE_KEY . AUTH_KEY . $form_items ) !== $data['getpaid-form-items-key'] ) { |
|
56 | + throw new Exception( __( 'We could not validate the form items. Please reload the page and try again.', 'invoicing' ) ); |
|
57 | + } |
|
58 | + |
|
59 | + $items = array(); |
|
60 | 60 | $item_ids = array(); |
61 | 61 | |
62 | 62 | foreach ( getpaid_convert_items_to_array( $form_items ) as $item_id => $qty ) { |
@@ -69,10 +69,10 @@ discard block |
||
69 | 69 | $item->set_is_required( false ); |
70 | 70 | } |
71 | 71 | |
72 | - if ( ! $item->user_can_set_their_price() && isset( $force_prices[ $item_id ] ) ) { |
|
73 | - $item->set_is_dynamic_pricing( true ); |
|
74 | - $item->set_minimum_price( 0 ); |
|
75 | - } |
|
72 | + if ( ! $item->user_can_set_their_price() && isset( $force_prices[ $item_id ] ) ) { |
|
73 | + $item->set_is_dynamic_pricing( true ); |
|
74 | + $item->set_minimum_price( 0 ); |
|
75 | + } |
|
76 | 76 | |
77 | 77 | $item_ids[] = $item->get_id(); |
78 | 78 | $items[] = $item; |
@@ -87,61 +87,61 @@ discard block |
||
87 | 87 | $items[] = $item; |
88 | 88 | } |
89 | 89 | } |
90 | - } |
|
90 | + } |
|
91 | 91 | |
92 | 92 | $payment_form->set_items( $items ); |
93 | 93 | |
94 | - } |
|
95 | - |
|
96 | - // Process each individual item. |
|
97 | - foreach ( $payment_form->get_items() as $item ) { |
|
98 | - $this->process_item( $item, $selected_items, $submission ); |
|
99 | - } |
|
100 | - |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Process a single item. |
|
105 | - * |
|
106 | - * @param GetPaid_Form_Item $item |
|
107 | - * @param array $selected_items |
|
108 | - * @param GetPaid_Payment_Form_Submission $submission |
|
109 | - */ |
|
110 | - public function process_item( $item, $selected_items, $submission ) { |
|
94 | + } |
|
111 | 95 | |
112 | - // Abort if this is an optional item and it has not been selected. |
|
113 | - if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
|
114 | - return; |
|
115 | - } |
|
96 | + // Process each individual item. |
|
97 | + foreach ( $payment_form->get_items() as $item ) { |
|
98 | + $this->process_item( $item, $selected_items, $submission ); |
|
99 | + } |
|
116 | 100 | |
117 | - // (maybe) let customers change the quantities and prices. |
|
118 | - if ( isset( $selected_items[ $item->get_id() ] ) ) { |
|
101 | + } |
|
119 | 102 | |
120 | - // Maybe change the quantities. |
|
121 | - if ( $item->allows_quantities() ) { |
|
122 | - $item->set_quantity( (float) $selected_items[ $item->get_id() ]['quantity'] ); |
|
123 | - } |
|
103 | + /** |
|
104 | + * Process a single item. |
|
105 | + * |
|
106 | + * @param GetPaid_Form_Item $item |
|
107 | + * @param array $selected_items |
|
108 | + * @param GetPaid_Payment_Form_Submission $submission |
|
109 | + */ |
|
110 | + public function process_item( $item, $selected_items, $submission ) { |
|
111 | + |
|
112 | + // Abort if this is an optional item and it has not been selected. |
|
113 | + if ( ! $item->is_required() && ! isset( $selected_items[ $item->get_id() ] ) ) { |
|
114 | + return; |
|
115 | + } |
|
116 | + |
|
117 | + // (maybe) let customers change the quantities and prices. |
|
118 | + if ( isset( $selected_items[ $item->get_id() ] ) ) { |
|
119 | + |
|
120 | + // Maybe change the quantities. |
|
121 | + if ( $item->allows_quantities() ) { |
|
122 | + $item->set_quantity( (float) $selected_items[ $item->get_id() ]['quantity'] ); |
|
123 | + } |
|
124 | 124 | |
125 | - // Maybe change the price. |
|
126 | - if ( $item->user_can_set_their_price() ) { |
|
127 | - $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
|
125 | + // Maybe change the price. |
|
126 | + if ( $item->user_can_set_their_price() ) { |
|
127 | + $price = (float) wpinv_sanitize_amount( $selected_items[ $item->get_id() ]['price'] ); |
|
128 | 128 | |
129 | - if ( $item->get_minimum_price() > $price ) { |
|
130 | - throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $item->get_minimum_price() ) ) ); |
|
131 | - } |
|
129 | + if ( $item->get_minimum_price() > $price ) { |
|
130 | + throw new Exception( sprintf( __( 'The minimum allowed amount is %s', 'invoicing' ), getpaid_unstandardize_amount( $item->get_minimum_price() ) ) ); |
|
131 | + } |
|
132 | 132 | |
133 | - $item->set_price( $price ); |
|
133 | + $item->set_price( $price ); |
|
134 | 134 | |
135 | - } |
|
136 | - } |
|
135 | + } |
|
136 | + } |
|
137 | 137 | |
138 | - if ( 0 == $item->get_quantity() ) { |
|
139 | - return; |
|
140 | - } |
|
138 | + if ( 0 == $item->get_quantity() ) { |
|
139 | + return; |
|
140 | + } |
|
141 | 141 | |
142 | - // Save the item. |
|
143 | - $this->items[] = apply_filters( 'getpaid_payment_form_submission_processed_item', $item, $submission ); |
|
142 | + // Save the item. |
|
143 | + $this->items[] = apply_filters( 'getpaid_payment_form_submission_processed_item', $item, $submission ); |
|
144 | 144 | |
145 | - } |
|
145 | + } |
|
146 | 146 | |
147 | 147 | } |
@@ -12,305 +12,305 @@ |
||
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 | - $payment_form = $submission->get_payment_form(); |
|
37 | - if ( ! empty( $payment_form->invoice ) ) { |
|
38 | - $this->response['invoice'] = $payment_form->invoice->get_id(); |
|
39 | - } |
|
40 | - |
|
41 | - $this->add_totals( $submission ); |
|
42 | - $this->add_texts( $submission ); |
|
43 | - $this->add_items( $submission ); |
|
44 | - $this->add_fees( $submission ); |
|
45 | - $this->add_discounts( $submission ); |
|
46 | - $this->add_taxes( $submission ); |
|
47 | - $this->add_gateways( $submission ); |
|
48 | - $this->add_data( $submission ); |
|
49 | - |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Adds totals to a response for submission refresh prices. |
|
54 | - * |
|
55 | - * @param GetPaid_Payment_Form_Submission $submission |
|
56 | - */ |
|
57 | - public function add_totals( $submission ) { |
|
58 | - |
|
59 | - $this->response = array_merge( |
|
60 | - $this->response, |
|
61 | - array( |
|
62 | - |
|
63 | - 'totals' => array( |
|
64 | - 'subtotal' => $submission->format_amount( $submission->get_subtotal() ), |
|
65 | - 'discount' => $submission->format_amount( $submission->get_discount() ), |
|
66 | - 'fees' => $submission->format_amount( $submission->get_fee() ), |
|
67 | - 'tax' => $submission->format_amount( $submission->get_tax() ), |
|
68 | - 'total' => $submission->format_amount( $submission->get_total() ), |
|
69 | - 'raw_total' => html_entity_decode( sanitize_text_field( $submission->format_amount( $submission->get_total() ) ), ENT_QUOTES ), |
|
70 | - ), |
|
71 | - |
|
72 | - 'recurring' => array( |
|
73 | - 'subtotal' => $submission->format_amount( $submission->get_recurring_subtotal() ), |
|
74 | - 'discount' => $submission->format_amount( $submission->get_recurring_discount() ), |
|
75 | - 'fees' => $submission->format_amount( $submission->get_recurring_fee() ), |
|
76 | - 'tax' => $submission->format_amount( $submission->get_recurring_tax() ), |
|
77 | - 'total' => $submission->format_amount( $submission->get_recurring_total() ), |
|
78 | - ), |
|
79 | - |
|
80 | - 'initial_amt' => wpinv_round_amount( $submission->get_total(), null, true ), |
|
81 | - 'currency' => $submission->get_currency(), |
|
82 | - |
|
83 | - ) |
|
84 | - ); |
|
85 | - |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Adds texts to a response for submission refresh prices. |
|
90 | - * |
|
91 | - * @param GetPaid_Payment_Form_Submission $submission |
|
92 | - */ |
|
93 | - public function add_texts( $submission ) { |
|
94 | - |
|
95 | - $payable = $submission->format_amount( $submission->get_total() ); |
|
96 | - $groups = getpaid_get_subscription_groups( $submission ); |
|
97 | - |
|
98 | - if ( $submission->has_recurring && 2 > count( $groups ) ) { |
|
99 | - |
|
100 | - $recurring = new WPInv_Item( $submission->has_recurring ); |
|
101 | - $period = getpaid_get_subscription_period_label( $recurring->get_recurring_period( true ), $recurring->get_recurring_interval(), '' ); |
|
102 | - $main_item = reset( $groups ); |
|
103 | - |
|
104 | - if ( $submission->get_total() == $submission->get_recurring_total() ) { |
|
105 | - $payable = "$payable / $period"; |
|
106 | - } elseif ( $main_item ) { |
|
107 | - |
|
108 | - $main_item = reset( $main_item ); |
|
109 | - |
|
110 | - // Calculate the next renewal date. |
|
111 | - $_period = $main_item->get_recurring_period( true ); |
|
112 | - $_interval = $main_item->get_recurring_interval(); |
|
113 | - |
|
114 | - // If the subscription item has a trial period... |
|
115 | - if ( $main_item->has_free_trial() ) { |
|
116 | - $_period = $main_item->get_trial_period( true ); |
|
117 | - $_interval = $main_item->get_trial_interval(); |
|
118 | - } |
|
119 | - |
|
120 | - $payable = sprintf( |
|
121 | - __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
122 | - $submission->format_amount( $submission->get_total() ), |
|
123 | - $submission->format_amount( $submission->get_recurring_total() ), |
|
124 | - $period |
|
125 | - ); |
|
126 | - |
|
127 | - $payable .= sprintf( |
|
128 | - '<small class="text-muted form-text">%s</small>', |
|
129 | - sprintf( |
|
130 | - __( 'First renewal on %s', 'invoicing' ), |
|
131 | - getpaid_format_date( date( 'Y-m-d H:i:s', strtotime( "+$_interval $_period", current_time( 'timestamp' ) ) ) ) |
|
132 | - ) |
|
133 | - ); |
|
134 | - |
|
135 | - } else { |
|
136 | - $payable = sprintf( |
|
137 | - __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
138 | - $submission->format_amount( $submission->get_total() ), |
|
139 | - $submission->format_amount( $submission->get_recurring_total() ), |
|
140 | - $period |
|
141 | - ); |
|
142 | - } |
|
143 | -} |
|
34 | + ); |
|
35 | + |
|
36 | + $payment_form = $submission->get_payment_form(); |
|
37 | + if ( ! empty( $payment_form->invoice ) ) { |
|
38 | + $this->response['invoice'] = $payment_form->invoice->get_id(); |
|
39 | + } |
|
40 | + |
|
41 | + $this->add_totals( $submission ); |
|
42 | + $this->add_texts( $submission ); |
|
43 | + $this->add_items( $submission ); |
|
44 | + $this->add_fees( $submission ); |
|
45 | + $this->add_discounts( $submission ); |
|
46 | + $this->add_taxes( $submission ); |
|
47 | + $this->add_gateways( $submission ); |
|
48 | + $this->add_data( $submission ); |
|
144 | 49 | |
145 | - $texts = array( |
|
146 | - '.getpaid-checkout-total-payable' => $payable, |
|
147 | - ); |
|
50 | + } |
|
148 | 51 | |
149 | - foreach ( $submission->get_items() as $item ) { |
|
150 | - $item_id = $item->get_id(); |
|
151 | - $initial_price = $submission->format_amount( $item->get_sub_total() - $item->item_discount ); |
|
152 | - $recurring_price = $submission->format_amount( $item->get_recurring_sub_total() - $item->recurring_item_discount ); |
|
153 | - $texts[ ".item-$item_id .getpaid-form-item-price-desc" ] = getpaid_item_recurring_price_help_text( $item, $submission->get_currency(), $initial_price, $recurring_price ); |
|
154 | - $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = sprintf( __( 'Subtotal: %s', 'invoicing' ), $submission->format_amount( $item->get_sub_total() ) ); |
|
52 | + /** |
|
53 | + * Adds totals to a response for submission refresh prices. |
|
54 | + * |
|
55 | + * @param GetPaid_Payment_Form_Submission $submission |
|
56 | + */ |
|
57 | + public function add_totals( $submission ) { |
|
58 | + |
|
59 | + $this->response = array_merge( |
|
60 | + $this->response, |
|
61 | + array( |
|
62 | + |
|
63 | + 'totals' => array( |
|
64 | + 'subtotal' => $submission->format_amount( $submission->get_subtotal() ), |
|
65 | + 'discount' => $submission->format_amount( $submission->get_discount() ), |
|
66 | + 'fees' => $submission->format_amount( $submission->get_fee() ), |
|
67 | + 'tax' => $submission->format_amount( $submission->get_tax() ), |
|
68 | + 'total' => $submission->format_amount( $submission->get_total() ), |
|
69 | + 'raw_total' => html_entity_decode( sanitize_text_field( $submission->format_amount( $submission->get_total() ) ), ENT_QUOTES ), |
|
70 | + ), |
|
71 | + |
|
72 | + 'recurring' => array( |
|
73 | + 'subtotal' => $submission->format_amount( $submission->get_recurring_subtotal() ), |
|
74 | + 'discount' => $submission->format_amount( $submission->get_recurring_discount() ), |
|
75 | + 'fees' => $submission->format_amount( $submission->get_recurring_fee() ), |
|
76 | + 'tax' => $submission->format_amount( $submission->get_recurring_tax() ), |
|
77 | + 'total' => $submission->format_amount( $submission->get_recurring_total() ), |
|
78 | + ), |
|
79 | + |
|
80 | + 'initial_amt' => wpinv_round_amount( $submission->get_total(), null, true ), |
|
81 | + 'currency' => $submission->get_currency(), |
|
82 | + |
|
83 | + ) |
|
84 | + ); |
|
85 | + |
|
86 | + } |
|
155 | 87 | |
156 | - if ( $item->get_quantity() == 1 ) { |
|
157 | - $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = ''; |
|
158 | - } |
|
88 | + /** |
|
89 | + * Adds texts to a response for submission refresh prices. |
|
90 | + * |
|
91 | + * @param GetPaid_Payment_Form_Submission $submission |
|
92 | + */ |
|
93 | + public function add_texts( $submission ) { |
|
94 | + |
|
95 | + $payable = $submission->format_amount( $submission->get_total() ); |
|
96 | + $groups = getpaid_get_subscription_groups( $submission ); |
|
97 | + |
|
98 | + if ( $submission->has_recurring && 2 > count( $groups ) ) { |
|
99 | + |
|
100 | + $recurring = new WPInv_Item( $submission->has_recurring ); |
|
101 | + $period = getpaid_get_subscription_period_label( $recurring->get_recurring_period( true ), $recurring->get_recurring_interval(), '' ); |
|
102 | + $main_item = reset( $groups ); |
|
103 | + |
|
104 | + if ( $submission->get_total() == $submission->get_recurring_total() ) { |
|
105 | + $payable = "$payable / $period"; |
|
106 | + } elseif ( $main_item ) { |
|
107 | + |
|
108 | + $main_item = reset( $main_item ); |
|
109 | + |
|
110 | + // Calculate the next renewal date. |
|
111 | + $_period = $main_item->get_recurring_period( true ); |
|
112 | + $_interval = $main_item->get_recurring_interval(); |
|
113 | + |
|
114 | + // If the subscription item has a trial period... |
|
115 | + if ( $main_item->has_free_trial() ) { |
|
116 | + $_period = $main_item->get_trial_period( true ); |
|
117 | + $_interval = $main_item->get_trial_interval(); |
|
118 | + } |
|
119 | + |
|
120 | + $payable = sprintf( |
|
121 | + __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
122 | + $submission->format_amount( $submission->get_total() ), |
|
123 | + $submission->format_amount( $submission->get_recurring_total() ), |
|
124 | + $period |
|
125 | + ); |
|
126 | + |
|
127 | + $payable .= sprintf( |
|
128 | + '<small class="text-muted form-text">%s</small>', |
|
129 | + sprintf( |
|
130 | + __( 'First renewal on %s', 'invoicing' ), |
|
131 | + getpaid_format_date( date( 'Y-m-d H:i:s', strtotime( "+$_interval $_period", current_time( 'timestamp' ) ) ) ) |
|
132 | + ) |
|
133 | + ); |
|
134 | + |
|
135 | + } else { |
|
136 | + $payable = sprintf( |
|
137 | + __( '%1$s (renews at %2$s / %3$s)', 'invoicing' ), |
|
138 | + $submission->format_amount( $submission->get_total() ), |
|
139 | + $submission->format_amount( $submission->get_recurring_total() ), |
|
140 | + $period |
|
141 | + ); |
|
142 | + } |
|
159 | 143 | } |
160 | 144 | |
161 | - $this->response = array_merge( $this->response, array( 'texts' => $texts ) ); |
|
145 | + $texts = array( |
|
146 | + '.getpaid-checkout-total-payable' => $payable, |
|
147 | + ); |
|
162 | 148 | |
163 | - } |
|
149 | + foreach ( $submission->get_items() as $item ) { |
|
150 | + $item_id = $item->get_id(); |
|
151 | + $initial_price = $submission->format_amount( $item->get_sub_total() - $item->item_discount ); |
|
152 | + $recurring_price = $submission->format_amount( $item->get_recurring_sub_total() - $item->recurring_item_discount ); |
|
153 | + $texts[ ".item-$item_id .getpaid-form-item-price-desc" ] = getpaid_item_recurring_price_help_text( $item, $submission->get_currency(), $initial_price, $recurring_price ); |
|
154 | + $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = sprintf( __( 'Subtotal: %s', 'invoicing' ), $submission->format_amount( $item->get_sub_total() ) ); |
|
155 | + |
|
156 | + if ( $item->get_quantity() == 1 ) { |
|
157 | + $texts[ ".item-$item_id .getpaid-mobile-item-subtotal" ] = ''; |
|
158 | + } |
|
159 | +} |
|
164 | 160 | |
165 | - /** |
|
166 | - * Adds items to a response for submission refresh prices. |
|
167 | - * |
|
168 | - * @param GetPaid_Payment_Form_Submission $submission |
|
169 | - */ |
|
170 | - public function add_items( $submission ) { |
|
161 | + $this->response = array_merge( $this->response, array( 'texts' => $texts ) ); |
|
171 | 162 | |
172 | - // Add items. |
|
173 | - $items = array(); |
|
174 | - $selected_items = array(); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Adds items to a response for submission refresh prices. |
|
167 | + * |
|
168 | + * @param GetPaid_Payment_Form_Submission $submission |
|
169 | + */ |
|
170 | + public function add_items( $submission ) { |
|
171 | + |
|
172 | + // Add items. |
|
173 | + $items = array(); |
|
174 | + $selected_items = array(); |
|
175 | 175 | |
176 | 176 | foreach ( $submission->get_items() as $item ) { |
177 | - $item_id = $item->get_id(); |
|
178 | - $items[ "$item_id" ] = $submission->format_amount( $item->get_sub_total() ); |
|
179 | - |
|
180 | - $selected_items[ "$item_id" ] = array( |
|
181 | - 'quantity' => $item->get_quantity(), |
|
182 | - 'price' => $item->get_price(), |
|
183 | - 'price_fmt' => $submission->format_amount( $item->get_price() ), |
|
184 | - ); |
|
185 | - } |
|
186 | - |
|
187 | - $this->response = array_merge( |
|
188 | - $this->response, |
|
189 | - array( |
|
190 | - 'items' => $items, |
|
191 | - 'selected_items' => $selected_items, |
|
192 | - ) |
|
193 | - ); |
|
194 | - |
|
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Adds fees to a response for submission refresh prices. |
|
199 | - * |
|
200 | - * @param GetPaid_Payment_Form_Submission $submission |
|
201 | - */ |
|
202 | - public function add_fees( $submission ) { |
|
203 | - |
|
204 | - $fees = array(); |
|
177 | + $item_id = $item->get_id(); |
|
178 | + $items[ "$item_id" ] = $submission->format_amount( $item->get_sub_total() ); |
|
179 | + |
|
180 | + $selected_items[ "$item_id" ] = array( |
|
181 | + 'quantity' => $item->get_quantity(), |
|
182 | + 'price' => $item->get_price(), |
|
183 | + 'price_fmt' => $submission->format_amount( $item->get_price() ), |
|
184 | + ); |
|
185 | + } |
|
186 | + |
|
187 | + $this->response = array_merge( |
|
188 | + $this->response, |
|
189 | + array( |
|
190 | + 'items' => $items, |
|
191 | + 'selected_items' => $selected_items, |
|
192 | + ) |
|
193 | + ); |
|
194 | + |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Adds fees to a response for submission refresh prices. |
|
199 | + * |
|
200 | + * @param GetPaid_Payment_Form_Submission $submission |
|
201 | + */ |
|
202 | + public function add_fees( $submission ) { |
|
203 | + |
|
204 | + $fees = array(); |
|
205 | 205 | |
206 | 206 | foreach ( $submission->get_fees() as $name => $data ) { |
207 | - $fees[ $name ] = $submission->format_amount( $data['initial_fee'] ); |
|
208 | - } |
|
207 | + $fees[ $name ] = $submission->format_amount( $data['initial_fee'] ); |
|
208 | + } |
|
209 | 209 | |
210 | - $this->response = array_merge( |
|
211 | - $this->response, |
|
212 | - array( 'fees' => $fees ) |
|
213 | - ); |
|
210 | + $this->response = array_merge( |
|
211 | + $this->response, |
|
212 | + array( 'fees' => $fees ) |
|
213 | + ); |
|
214 | 214 | |
215 | - } |
|
215 | + } |
|
216 | 216 | |
217 | - /** |
|
218 | - * Adds discounts to a response for submission refresh prices. |
|
219 | - * |
|
220 | - * @param GetPaid_Payment_Form_Submission $submission |
|
221 | - */ |
|
222 | - public function add_discounts( $submission ) { |
|
217 | + /** |
|
218 | + * Adds discounts to a response for submission refresh prices. |
|
219 | + * |
|
220 | + * @param GetPaid_Payment_Form_Submission $submission |
|
221 | + */ |
|
222 | + public function add_discounts( $submission ) { |
|
223 | 223 | |
224 | - $discounts = array(); |
|
224 | + $discounts = array(); |
|
225 | 225 | |
226 | 226 | foreach ( $submission->get_discounts() as $name => $data ) { |
227 | - $discounts[ $name ] = $submission->format_amount( $data['initial_discount'] ); |
|
228 | - } |
|
227 | + $discounts[ $name ] = $submission->format_amount( $data['initial_discount'] ); |
|
228 | + } |
|
229 | 229 | |
230 | - $this->response = array_merge( |
|
231 | - $this->response, |
|
232 | - array( 'discounts' => $discounts ) |
|
233 | - ); |
|
230 | + $this->response = array_merge( |
|
231 | + $this->response, |
|
232 | + array( 'discounts' => $discounts ) |
|
233 | + ); |
|
234 | 234 | |
235 | - } |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Adds taxes to a response for submission refresh prices. |
|
239 | - * |
|
240 | - * @param GetPaid_Payment_Form_Submission $submission |
|
241 | - */ |
|
242 | - public function add_taxes( $submission ) { |
|
243 | - |
|
244 | - $taxes = array(); |
|
245 | - $markup = ''; |
|
237 | + /** |
|
238 | + * Adds taxes to a response for submission refresh prices. |
|
239 | + * |
|
240 | + * @param GetPaid_Payment_Form_Submission $submission |
|
241 | + */ |
|
242 | + public function add_taxes( $submission ) { |
|
243 | + |
|
244 | + $taxes = array(); |
|
245 | + $markup = ''; |
|
246 | 246 | foreach ( $submission->get_taxes() as $name => $data ) { |
247 | - $name = sanitize_text_field( $name ); |
|
248 | - $amount = $submission->format_amount( $data['initial_tax'] ); |
|
249 | - $taxes[ $name ] = $amount; |
|
250 | - $markup .= "<small class='form-text'>$name : $amount</small>"; |
|
251 | - } |
|
252 | - |
|
253 | - if ( wpinv_display_individual_tax_rates() && ! empty( $taxes ) ) { |
|
254 | - $this->response['texts']['.getpaid-form-cart-totals-total-tax'] = $markup; |
|
255 | - } |
|
256 | - |
|
257 | - $this->response = array_merge( |
|
258 | - $this->response, |
|
259 | - array( 'taxes' => $taxes ) |
|
260 | - ); |
|
261 | - |
|
262 | - } |
|
263 | - |
|
264 | - /** |
|
265 | - * Adds gateways to a response for submission refresh prices. |
|
266 | - * |
|
267 | - * @param GetPaid_Payment_Form_Submission $submission |
|
268 | - */ |
|
269 | - public function add_gateways( $submission ) { |
|
270 | - |
|
271 | - $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
272 | - |
|
273 | - if ( $this->response['has_recurring'] ) { |
|
274 | - |
|
275 | - foreach ( $gateways as $i => $gateway ) { |
|
276 | - |
|
277 | - if ( |
|
278 | - ! getpaid_payment_gateway_supports( $gateway, 'subscription' ) |
|
279 | - || ( $this->response['has_subscription_group'] && ! getpaid_payment_gateway_supports( $gateway, 'single_subscription_group' ) ) |
|
280 | - || ( $this->response['has_multiple_subscription_groups'] && ! getpaid_payment_gateway_supports( $gateway, 'multiple_subscription_groups' ) ) ) { |
|
281 | - unset( $gateways[ $i ] ); |
|
282 | - } |
|
247 | + $name = sanitize_text_field( $name ); |
|
248 | + $amount = $submission->format_amount( $data['initial_tax'] ); |
|
249 | + $taxes[ $name ] = $amount; |
|
250 | + $markup .= "<small class='form-text'>$name : $amount</small>"; |
|
251 | + } |
|
252 | + |
|
253 | + if ( wpinv_display_individual_tax_rates() && ! empty( $taxes ) ) { |
|
254 | + $this->response['texts']['.getpaid-form-cart-totals-total-tax'] = $markup; |
|
255 | + } |
|
256 | + |
|
257 | + $this->response = array_merge( |
|
258 | + $this->response, |
|
259 | + array( 'taxes' => $taxes ) |
|
260 | + ); |
|
261 | + |
|
262 | + } |
|
263 | + |
|
264 | + /** |
|
265 | + * Adds gateways to a response for submission refresh prices. |
|
266 | + * |
|
267 | + * @param GetPaid_Payment_Form_Submission $submission |
|
268 | + */ |
|
269 | + public function add_gateways( $submission ) { |
|
270 | + |
|
271 | + $gateways = array_keys( wpinv_get_enabled_payment_gateways() ); |
|
272 | + |
|
273 | + if ( $this->response['has_recurring'] ) { |
|
274 | + |
|
275 | + foreach ( $gateways as $i => $gateway ) { |
|
276 | + |
|
277 | + if ( |
|
278 | + ! getpaid_payment_gateway_supports( $gateway, 'subscription' ) |
|
279 | + || ( $this->response['has_subscription_group'] && ! getpaid_payment_gateway_supports( $gateway, 'single_subscription_group' ) ) |
|
280 | + || ( $this->response['has_multiple_subscription_groups'] && ! getpaid_payment_gateway_supports( $gateway, 'multiple_subscription_groups' ) ) ) { |
|
281 | + unset( $gateways[ $i ] ); |
|
282 | + } |
|
283 | 283 | } |
284 | 284 | } |
285 | 285 | |
286 | - $gateways = apply_filters( 'getpaid_submission_gateways', $gateways, $submission ); |
|
287 | - $this->response = array_merge( |
|
288 | - $this->response, |
|
289 | - array( 'gateways' => $gateways ) |
|
290 | - ); |
|
291 | - |
|
292 | - } |
|
293 | - |
|
294 | - /** |
|
295 | - * Adds data to a response for submission refresh prices. |
|
296 | - * |
|
297 | - * @param GetPaid_Payment_Form_Submission $submission |
|
298 | - */ |
|
299 | - public function add_data( $submission ) { |
|
300 | - |
|
301 | - $this->response = array_merge( |
|
302 | - $this->response, |
|
303 | - array( |
|
304 | - 'js_data' => apply_filters( |
|
305 | - 'getpaid_submission_js_data', |
|
306 | - array( |
|
307 | - 'is_recurring' => $this->response['has_recurring'], |
|
308 | - ), |
|
309 | - $submission |
|
310 | - ), |
|
311 | - ) |
|
312 | - ); |
|
313 | - |
|
314 | - } |
|
286 | + $gateways = apply_filters( 'getpaid_submission_gateways', $gateways, $submission ); |
|
287 | + $this->response = array_merge( |
|
288 | + $this->response, |
|
289 | + array( 'gateways' => $gateways ) |
|
290 | + ); |
|
291 | + |
|
292 | + } |
|
293 | + |
|
294 | + /** |
|
295 | + * Adds data to a response for submission refresh prices. |
|
296 | + * |
|
297 | + * @param GetPaid_Payment_Form_Submission $submission |
|
298 | + */ |
|
299 | + public function add_data( $submission ) { |
|
300 | + |
|
301 | + $this->response = array_merge( |
|
302 | + $this->response, |
|
303 | + array( |
|
304 | + 'js_data' => apply_filters( |
|
305 | + 'getpaid_submission_js_data', |
|
306 | + array( |
|
307 | + 'is_recurring' => $this->response['has_recurring'], |
|
308 | + ), |
|
309 | + $submission |
|
310 | + ), |
|
311 | + ) |
|
312 | + ); |
|
313 | + |
|
314 | + } |
|
315 | 315 | |
316 | 316 | } |
@@ -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' => wp_strip_all_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' => wp_strip_all_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.7.0' ); |
|
26 | + define( 'WPINV_VERSION', '2.7.0' ); |
|
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 | /** |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | * Bail if we are not in WP. |
14 | 14 | */ |
15 | 15 | if ( ! defined( 'ABSPATH' ) ) { |
16 | - exit; |
|
16 | + exit; |
|
17 | 17 | } |
18 | 18 | |
19 | 19 | /** |
@@ -21,362 +21,362 @@ discard block |
||
21 | 21 | */ |
22 | 22 | if ( ! class_exists( 'WP_Font_Awesome_Settings' ) ) { |
23 | 23 | |
24 | - /** |
|
25 | - * A Class to be able to change settings for Font Awesome. |
|
26 | - * |
|
27 | - * Class WP_Font_Awesome_Settings |
|
28 | - * @since 1.0.10 Now able to pass wp.org theme check. |
|
29 | - * @since 1.0.11 Font Awesome Pro now supported. |
|
30 | - * @since 1.0.11 Font Awesome Kits now supported. |
|
31 | - * @since 1.0.13 RTL language support added. |
|
32 | - * @since 1.0.14 Warning added for v6 pro requires kit and will now not work if official FA plugin installed. |
|
33 | - * @since 1.0.15 Font Awesome will now load in the FSE if enable din the backend. |
|
34 | - * @since 1.1.0 Option added to load FontAwesome locally. |
|
35 | - * @since 1.1.1 Requires to re-save settings to load locally when option does not exists - FIXED. |
|
36 | - * @since 1.1.2 Bumped the latest version to 6.3.0 - CHANGED. |
|
37 | - * @ver 1.0.15 |
|
38 | - * @todo decide how to implement textdomain |
|
39 | - */ |
|
40 | - class WP_Font_Awesome_Settings { |
|
41 | - |
|
42 | - /** |
|
43 | - * Class version version. |
|
44 | - * |
|
45 | - * @var string |
|
46 | - */ |
|
47 | - public $version = '1.1.2'; |
|
48 | - |
|
49 | - /** |
|
50 | - * Class textdomain. |
|
51 | - * |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - public $textdomain = 'font-awesome-settings'; |
|
55 | - |
|
56 | - /** |
|
57 | - * Latest version of Font Awesome at time of publish published. |
|
58 | - * |
|
59 | - * @var string |
|
60 | - */ |
|
61 | - public $latest = "6.3.0"; |
|
62 | - |
|
63 | - /** |
|
64 | - * The title. |
|
65 | - * |
|
66 | - * @var string |
|
67 | - */ |
|
68 | - public $name = 'Font Awesome'; |
|
69 | - |
|
70 | - /** |
|
71 | - * Holds the settings values. |
|
72 | - * |
|
73 | - * @var array |
|
74 | - */ |
|
75 | - private $settings; |
|
76 | - |
|
77 | - /** |
|
78 | - * WP_Font_Awesome_Settings instance. |
|
79 | - * |
|
80 | - * @access private |
|
81 | - * @since 1.0.0 |
|
82 | - * @var WP_Font_Awesome_Settings There can be only one! |
|
83 | - */ |
|
84 | - private static $instance = null; |
|
85 | - |
|
86 | - /** |
|
87 | - * Main WP_Font_Awesome_Settings Instance. |
|
88 | - * |
|
89 | - * Ensures only one instance of WP_Font_Awesome_Settings is loaded or can be loaded. |
|
90 | - * |
|
91 | - * @since 1.0.0 |
|
92 | - * @static |
|
93 | - * @return WP_Font_Awesome_Settings - Main instance. |
|
94 | - */ |
|
95 | - public static function instance() { |
|
96 | - if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WP_Font_Awesome_Settings ) ) { |
|
97 | - self::$instance = new WP_Font_Awesome_Settings; |
|
98 | - |
|
99 | - add_action( 'init', array( self::$instance, 'init' ) ); // set settings |
|
100 | - |
|
101 | - if ( is_admin() ) { |
|
102 | - add_action( 'admin_menu', array( self::$instance, 'menu_item' ) ); |
|
103 | - add_action( 'admin_init', array( self::$instance, 'register_settings' ) ); |
|
104 | - add_action( 'admin_notices', array( self::$instance, 'admin_notices' ) ); |
|
105 | - } |
|
106 | - |
|
107 | - do_action( 'wp_font_awesome_settings_loaded' ); |
|
108 | - } |
|
109 | - |
|
110 | - return self::$instance; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Initiate the settings and add the required action hooks. |
|
115 | - * |
|
116 | - * @since 1.0.8 Settings name wrong - FIXED |
|
117 | - */ |
|
118 | - public function init() { |
|
119 | - // Download fontawesome locally. |
|
120 | - add_action( 'add_option_wp-font-awesome-settings', array( $this, 'add_option_wp_font_awesome_settings' ), 10, 2 ); |
|
121 | - add_action( 'update_option_wp-font-awesome-settings', array( $this, 'update_option_wp_font_awesome_settings' ), 10, 2 ); |
|
122 | - |
|
123 | - $this->settings = $this->get_settings(); |
|
124 | - |
|
125 | - // check if the official plugin is active and use that instead if so. |
|
126 | - if ( ! defined( 'FONTAWESOME_PLUGIN_FILE' ) ) { |
|
127 | - |
|
128 | - if ( $this->settings['type'] == 'CSS' ) { |
|
129 | - |
|
130 | - if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) { |
|
131 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 ); |
|
132 | - } |
|
133 | - |
|
134 | - if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) { |
|
135 | - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 ); |
|
136 | - add_filter( 'block_editor_settings_all', array( $this, 'enqueue_editor_styles' ), 10, 2 ); |
|
137 | - } |
|
138 | - |
|
139 | - } else { |
|
140 | - |
|
141 | - if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) { |
|
142 | - add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
|
143 | - } |
|
144 | - |
|
145 | - if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) { |
|
146 | - add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
|
147 | - add_filter( 'block_editor_settings_all', array( $this, 'enqueue_editor_scripts' ), 10, 2 ); |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - // remove font awesome if set to do so |
|
152 | - if ( $this->settings['dequeue'] == '1' ) { |
|
153 | - add_action( 'clean_url', array( $this, 'remove_font_awesome' ), 5000, 3 ); |
|
154 | - } |
|
155 | - } |
|
156 | - |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * Add FA to the FSE. |
|
161 | - * |
|
162 | - * @param $editor_settings |
|
163 | - * @param $block_editor_context |
|
164 | - * |
|
165 | - * @return array |
|
166 | - */ |
|
167 | - public function enqueue_editor_styles( $editor_settings, $block_editor_context ){ |
|
168 | - |
|
169 | - if ( ! empty( $editor_settings['__unstableResolvedAssets']['styles'] ) ) { |
|
170 | - $url = $this->get_url(); |
|
171 | - $editor_settings['__unstableResolvedAssets']['styles'] .= "<link rel='stylesheet' id='font-awesome-css' href='$url' media='all' />"; |
|
172 | - } |
|
173 | - |
|
174 | - return $editor_settings; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Add FA to the FSE. |
|
179 | - * |
|
180 | - * @param $editor_settings |
|
181 | - * @param $block_editor_context |
|
182 | - * |
|
183 | - * @return array |
|
184 | - */ |
|
185 | - public function enqueue_editor_scripts( $editor_settings, $block_editor_context ){ |
|
186 | - |
|
187 | - $url = $this->get_url(); |
|
188 | - $editor_settings['__unstableResolvedAssets']['scripts'] .= "<script src='$url' id='font-awesome-js'></script>"; |
|
189 | - |
|
190 | - return $editor_settings; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Adds the Font Awesome styles. |
|
195 | - */ |
|
196 | - public function enqueue_style() { |
|
197 | - // build url |
|
198 | - $url = $this->get_url(); |
|
199 | - $version = ! empty( $this->settings['local'] ) && empty( $this->settings['pro'] ) ? strip_tags( $this->settings['local_version'] ) : null; |
|
200 | - |
|
201 | - wp_deregister_style( 'font-awesome' ); // deregister in case its already there |
|
202 | - wp_register_style( 'font-awesome', $url, array(), $version ); |
|
203 | - wp_enqueue_style( 'font-awesome' ); |
|
204 | - |
|
205 | - // RTL language support CSS. |
|
206 | - if ( is_rtl() ) { |
|
207 | - wp_add_inline_style( 'font-awesome', $this->rtl_inline_css() ); |
|
208 | - } |
|
209 | - |
|
210 | - if ( $this->settings['shims'] ) { |
|
211 | - $url = $this->get_url( true ); |
|
212 | - wp_deregister_style( 'font-awesome-shims' ); // deregister in case its already there |
|
213 | - wp_register_style( 'font-awesome-shims', $url, array(), $version ); |
|
214 | - wp_enqueue_style( 'font-awesome-shims' ); |
|
215 | - } |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Adds the Font Awesome JS. |
|
220 | - */ |
|
221 | - public function enqueue_scripts() { |
|
222 | - // build url |
|
223 | - $url = $this->get_url(); |
|
224 | - |
|
225 | - $deregister_function = 'wp' . '_' . 'deregister' . '_' . 'script'; |
|
226 | - call_user_func( $deregister_function, 'font-awesome' ); // deregister in case its already there |
|
227 | - wp_register_script( 'font-awesome', $url, array(), null ); |
|
228 | - wp_enqueue_script( 'font-awesome' ); |
|
229 | - |
|
230 | - if ( $this->settings['shims'] ) { |
|
231 | - $url = $this->get_url( true ); |
|
232 | - call_user_func( $deregister_function, 'font-awesome-shims' ); // deregister in case its already there |
|
233 | - wp_register_script( 'font-awesome-shims', $url, array(), null ); |
|
234 | - wp_enqueue_script( 'font-awesome-shims' ); |
|
235 | - } |
|
236 | - } |
|
237 | - |
|
238 | - /** |
|
239 | - * Get the url of the Font Awesome files. |
|
240 | - * |
|
241 | - * @param bool $shims If this is a shim file or not. |
|
242 | - * @param bool $local Load locally if allowed. |
|
243 | - * |
|
244 | - * @return string The url to the file. |
|
245 | - */ |
|
246 | - public function get_url( $shims = false, $local = true ) { |
|
247 | - $script = $shims ? 'v4-shims' : 'all'; |
|
248 | - $sub = $this->settings['pro'] ? 'pro' : 'use'; |
|
249 | - $type = $this->settings['type']; |
|
250 | - $version = $this->settings['version']; |
|
251 | - $kit_url = $this->settings['kit-url'] ? esc_url( $this->settings['kit-url'] ) : ''; |
|
252 | - $url = ''; |
|
253 | - |
|
254 | - if ( $type == 'KIT' && $kit_url ) { |
|
255 | - if ( $shims ) { |
|
256 | - // if its a kit then we don't add shims here |
|
257 | - return ''; |
|
258 | - } |
|
259 | - $url .= $kit_url; // CDN |
|
260 | - $url .= "?wpfas=true"; // set our var so our version is not removed |
|
261 | - } else { |
|
262 | - $v = ''; |
|
263 | - // Check and load locally. |
|
264 | - if ( $local && $this->has_local() ) { |
|
265 | - $script .= ".min"; |
|
266 | - $v .= '&ver=' . strip_tags( $this->settings['local_version'] ); |
|
267 | - $url .= $this->get_fonts_url(); // Local fonts url. |
|
268 | - } else { |
|
269 | - $url .= "https://$sub.fontawesome.com/releases/"; // CDN |
|
270 | - $url .= ! empty( $version ) ? "v" . $version . '/' : "v" . $this->get_latest_version() . '/'; // version |
|
271 | - } |
|
272 | - $url .= $type == 'CSS' ? 'css/' : 'js/'; // type |
|
273 | - $url .= $type == 'CSS' ? $script . '.css' : $script . '.js'; // type |
|
274 | - $url .= "?wpfas=true" . $v; // set our var so our version is not removed |
|
275 | - } |
|
276 | - |
|
277 | - return $url; |
|
278 | - } |
|
279 | - |
|
280 | - /** |
|
281 | - * Try and remove any other versions of Font Awesome added by other plugins/themes. |
|
282 | - * |
|
283 | - * Uses the clean_url filter to try and remove any other Font Awesome files added, it can also add pseudo-elements flag for the JS version. |
|
284 | - * |
|
285 | - * @param $url |
|
286 | - * @param $original_url |
|
287 | - * @param $_context |
|
288 | - * |
|
289 | - * @return string The filtered url. |
|
290 | - */ |
|
291 | - public function remove_font_awesome( $url, $original_url, $_context ) { |
|
292 | - |
|
293 | - if ( $_context == 'display' |
|
294 | - && ( strstr( $url, "fontawesome" ) !== false || strstr( $url, "font-awesome" ) !== false ) |
|
295 | - && ( strstr( $url, ".js" ) !== false || strstr( $url, ".css" ) !== false ) |
|
296 | - ) {// it's a font-awesome-url (probably) |
|
297 | - |
|
298 | - if ( strstr( $url, "wpfas=true" ) !== false ) { |
|
299 | - if ( $this->settings['type'] == 'JS' ) { |
|
300 | - if ( $this->settings['js-pseudo'] ) { |
|
301 | - $url .= "' data-search-pseudo-elements defer='defer"; |
|
302 | - } else { |
|
303 | - $url .= "' defer='defer"; |
|
304 | - } |
|
305 | - } |
|
306 | - } else { |
|
307 | - $url = ''; // removing the url removes the file |
|
308 | - } |
|
309 | - |
|
310 | - } |
|
311 | - |
|
312 | - return $url; |
|
313 | - } |
|
314 | - |
|
315 | - /** |
|
316 | - * Register the database settings with WordPress. |
|
317 | - */ |
|
318 | - public function register_settings() { |
|
319 | - register_setting( 'wp-font-awesome-settings', 'wp-font-awesome-settings' ); |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * Add the WordPress settings menu item. |
|
324 | - * @since 1.0.10 Calling function name direct will fail theme check so we don't. |
|
325 | - */ |
|
326 | - public function menu_item() { |
|
327 | - $menu_function = 'add' . '_' . 'options' . '_' . 'page'; // won't pass theme check if function name present in theme |
|
328 | - call_user_func( $menu_function, $this->name, $this->name, 'manage_options', 'wp-font-awesome-settings', array( |
|
329 | - $this, |
|
330 | - 'settings_page' |
|
331 | - ) ); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * Get the current Font Awesome output settings. |
|
336 | - * |
|
337 | - * @return array The array of settings. |
|
338 | - */ |
|
339 | - public function get_settings() { |
|
340 | - $db_settings = get_option( 'wp-font-awesome-settings' ); |
|
341 | - |
|
342 | - $defaults = array( |
|
343 | - 'type' => 'CSS', // type to use, CSS or JS or KIT |
|
344 | - 'version' => '', // latest |
|
345 | - 'enqueue' => '', // front and backend |
|
346 | - 'shims' => '0', // default OFF now in 2020 |
|
347 | - 'js-pseudo' => '0', // if the pseudo elements flag should be set (CPU intensive) |
|
348 | - 'dequeue' => '0', // if we should try to remove other versions added by other plugins/themes |
|
349 | - 'pro' => '0', // if pro CDN url should be used |
|
350 | - 'local' => '0', // Store fonts locally. |
|
351 | - 'local_version' => '', // Local fonts version. |
|
352 | - 'kit-url' => '', // the kit url |
|
353 | - ); |
|
354 | - |
|
355 | - $settings = wp_parse_args( $db_settings, $defaults ); |
|
356 | - |
|
357 | - /** |
|
358 | - * Filter the Font Awesome settings. |
|
359 | - * |
|
360 | - * @todo if we add this filer people might use it and then it defeates the purpose of this class :/ |
|
361 | - */ |
|
362 | - return $this->settings = apply_filters( 'wp-font-awesome-settings', $settings, $db_settings, $defaults ); |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * The settings page html output. |
|
367 | - */ |
|
368 | - public function settings_page() { |
|
369 | - if ( ! current_user_can( 'manage_options' ) ) { |
|
370 | - wp_die( __( 'You do not have sufficient permissions to access this page.', 'font-awesome-settings' ) ); |
|
371 | - } |
|
372 | - |
|
373 | - // a hidden way to force the update of the version number via api instead of waiting the 48 hours |
|
374 | - if ( isset( $_REQUEST['force-version-check'] ) ) { |
|
375 | - $this->get_latest_version( $force_api = true ); |
|
376 | - } |
|
377 | - |
|
378 | - if ( ! defined( 'FONTAWESOME_PLUGIN_FILE' ) ) { |
|
379 | - ?> |
|
24 | + /** |
|
25 | + * A Class to be able to change settings for Font Awesome. |
|
26 | + * |
|
27 | + * Class WP_Font_Awesome_Settings |
|
28 | + * @since 1.0.10 Now able to pass wp.org theme check. |
|
29 | + * @since 1.0.11 Font Awesome Pro now supported. |
|
30 | + * @since 1.0.11 Font Awesome Kits now supported. |
|
31 | + * @since 1.0.13 RTL language support added. |
|
32 | + * @since 1.0.14 Warning added for v6 pro requires kit and will now not work if official FA plugin installed. |
|
33 | + * @since 1.0.15 Font Awesome will now load in the FSE if enable din the backend. |
|
34 | + * @since 1.1.0 Option added to load FontAwesome locally. |
|
35 | + * @since 1.1.1 Requires to re-save settings to load locally when option does not exists - FIXED. |
|
36 | + * @since 1.1.2 Bumped the latest version to 6.3.0 - CHANGED. |
|
37 | + * @ver 1.0.15 |
|
38 | + * @todo decide how to implement textdomain |
|
39 | + */ |
|
40 | + class WP_Font_Awesome_Settings { |
|
41 | + |
|
42 | + /** |
|
43 | + * Class version version. |
|
44 | + * |
|
45 | + * @var string |
|
46 | + */ |
|
47 | + public $version = '1.1.2'; |
|
48 | + |
|
49 | + /** |
|
50 | + * Class textdomain. |
|
51 | + * |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + public $textdomain = 'font-awesome-settings'; |
|
55 | + |
|
56 | + /** |
|
57 | + * Latest version of Font Awesome at time of publish published. |
|
58 | + * |
|
59 | + * @var string |
|
60 | + */ |
|
61 | + public $latest = "6.3.0"; |
|
62 | + |
|
63 | + /** |
|
64 | + * The title. |
|
65 | + * |
|
66 | + * @var string |
|
67 | + */ |
|
68 | + public $name = 'Font Awesome'; |
|
69 | + |
|
70 | + /** |
|
71 | + * Holds the settings values. |
|
72 | + * |
|
73 | + * @var array |
|
74 | + */ |
|
75 | + private $settings; |
|
76 | + |
|
77 | + /** |
|
78 | + * WP_Font_Awesome_Settings instance. |
|
79 | + * |
|
80 | + * @access private |
|
81 | + * @since 1.0.0 |
|
82 | + * @var WP_Font_Awesome_Settings There can be only one! |
|
83 | + */ |
|
84 | + private static $instance = null; |
|
85 | + |
|
86 | + /** |
|
87 | + * Main WP_Font_Awesome_Settings Instance. |
|
88 | + * |
|
89 | + * Ensures only one instance of WP_Font_Awesome_Settings is loaded or can be loaded. |
|
90 | + * |
|
91 | + * @since 1.0.0 |
|
92 | + * @static |
|
93 | + * @return WP_Font_Awesome_Settings - Main instance. |
|
94 | + */ |
|
95 | + public static function instance() { |
|
96 | + if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WP_Font_Awesome_Settings ) ) { |
|
97 | + self::$instance = new WP_Font_Awesome_Settings; |
|
98 | + |
|
99 | + add_action( 'init', array( self::$instance, 'init' ) ); // set settings |
|
100 | + |
|
101 | + if ( is_admin() ) { |
|
102 | + add_action( 'admin_menu', array( self::$instance, 'menu_item' ) ); |
|
103 | + add_action( 'admin_init', array( self::$instance, 'register_settings' ) ); |
|
104 | + add_action( 'admin_notices', array( self::$instance, 'admin_notices' ) ); |
|
105 | + } |
|
106 | + |
|
107 | + do_action( 'wp_font_awesome_settings_loaded' ); |
|
108 | + } |
|
109 | + |
|
110 | + return self::$instance; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Initiate the settings and add the required action hooks. |
|
115 | + * |
|
116 | + * @since 1.0.8 Settings name wrong - FIXED |
|
117 | + */ |
|
118 | + public function init() { |
|
119 | + // Download fontawesome locally. |
|
120 | + add_action( 'add_option_wp-font-awesome-settings', array( $this, 'add_option_wp_font_awesome_settings' ), 10, 2 ); |
|
121 | + add_action( 'update_option_wp-font-awesome-settings', array( $this, 'update_option_wp_font_awesome_settings' ), 10, 2 ); |
|
122 | + |
|
123 | + $this->settings = $this->get_settings(); |
|
124 | + |
|
125 | + // check if the official plugin is active and use that instead if so. |
|
126 | + if ( ! defined( 'FONTAWESOME_PLUGIN_FILE' ) ) { |
|
127 | + |
|
128 | + if ( $this->settings['type'] == 'CSS' ) { |
|
129 | + |
|
130 | + if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) { |
|
131 | + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 ); |
|
132 | + } |
|
133 | + |
|
134 | + if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) { |
|
135 | + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_style' ), 5000 ); |
|
136 | + add_filter( 'block_editor_settings_all', array( $this, 'enqueue_editor_styles' ), 10, 2 ); |
|
137 | + } |
|
138 | + |
|
139 | + } else { |
|
140 | + |
|
141 | + if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'frontend' ) { |
|
142 | + add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
|
143 | + } |
|
144 | + |
|
145 | + if ( $this->settings['enqueue'] == '' || $this->settings['enqueue'] == 'backend' ) { |
|
146 | + add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 5000 ); |
|
147 | + add_filter( 'block_editor_settings_all', array( $this, 'enqueue_editor_scripts' ), 10, 2 ); |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + // remove font awesome if set to do so |
|
152 | + if ( $this->settings['dequeue'] == '1' ) { |
|
153 | + add_action( 'clean_url', array( $this, 'remove_font_awesome' ), 5000, 3 ); |
|
154 | + } |
|
155 | + } |
|
156 | + |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * Add FA to the FSE. |
|
161 | + * |
|
162 | + * @param $editor_settings |
|
163 | + * @param $block_editor_context |
|
164 | + * |
|
165 | + * @return array |
|
166 | + */ |
|
167 | + public function enqueue_editor_styles( $editor_settings, $block_editor_context ){ |
|
168 | + |
|
169 | + if ( ! empty( $editor_settings['__unstableResolvedAssets']['styles'] ) ) { |
|
170 | + $url = $this->get_url(); |
|
171 | + $editor_settings['__unstableResolvedAssets']['styles'] .= "<link rel='stylesheet' id='font-awesome-css' href='$url' media='all' />"; |
|
172 | + } |
|
173 | + |
|
174 | + return $editor_settings; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Add FA to the FSE. |
|
179 | + * |
|
180 | + * @param $editor_settings |
|
181 | + * @param $block_editor_context |
|
182 | + * |
|
183 | + * @return array |
|
184 | + */ |
|
185 | + public function enqueue_editor_scripts( $editor_settings, $block_editor_context ){ |
|
186 | + |
|
187 | + $url = $this->get_url(); |
|
188 | + $editor_settings['__unstableResolvedAssets']['scripts'] .= "<script src='$url' id='font-awesome-js'></script>"; |
|
189 | + |
|
190 | + return $editor_settings; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Adds the Font Awesome styles. |
|
195 | + */ |
|
196 | + public function enqueue_style() { |
|
197 | + // build url |
|
198 | + $url = $this->get_url(); |
|
199 | + $version = ! empty( $this->settings['local'] ) && empty( $this->settings['pro'] ) ? strip_tags( $this->settings['local_version'] ) : null; |
|
200 | + |
|
201 | + wp_deregister_style( 'font-awesome' ); // deregister in case its already there |
|
202 | + wp_register_style( 'font-awesome', $url, array(), $version ); |
|
203 | + wp_enqueue_style( 'font-awesome' ); |
|
204 | + |
|
205 | + // RTL language support CSS. |
|
206 | + if ( is_rtl() ) { |
|
207 | + wp_add_inline_style( 'font-awesome', $this->rtl_inline_css() ); |
|
208 | + } |
|
209 | + |
|
210 | + if ( $this->settings['shims'] ) { |
|
211 | + $url = $this->get_url( true ); |
|
212 | + wp_deregister_style( 'font-awesome-shims' ); // deregister in case its already there |
|
213 | + wp_register_style( 'font-awesome-shims', $url, array(), $version ); |
|
214 | + wp_enqueue_style( 'font-awesome-shims' ); |
|
215 | + } |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Adds the Font Awesome JS. |
|
220 | + */ |
|
221 | + public function enqueue_scripts() { |
|
222 | + // build url |
|
223 | + $url = $this->get_url(); |
|
224 | + |
|
225 | + $deregister_function = 'wp' . '_' . 'deregister' . '_' . 'script'; |
|
226 | + call_user_func( $deregister_function, 'font-awesome' ); // deregister in case its already there |
|
227 | + wp_register_script( 'font-awesome', $url, array(), null ); |
|
228 | + wp_enqueue_script( 'font-awesome' ); |
|
229 | + |
|
230 | + if ( $this->settings['shims'] ) { |
|
231 | + $url = $this->get_url( true ); |
|
232 | + call_user_func( $deregister_function, 'font-awesome-shims' ); // deregister in case its already there |
|
233 | + wp_register_script( 'font-awesome-shims', $url, array(), null ); |
|
234 | + wp_enqueue_script( 'font-awesome-shims' ); |
|
235 | + } |
|
236 | + } |
|
237 | + |
|
238 | + /** |
|
239 | + * Get the url of the Font Awesome files. |
|
240 | + * |
|
241 | + * @param bool $shims If this is a shim file or not. |
|
242 | + * @param bool $local Load locally if allowed. |
|
243 | + * |
|
244 | + * @return string The url to the file. |
|
245 | + */ |
|
246 | + public function get_url( $shims = false, $local = true ) { |
|
247 | + $script = $shims ? 'v4-shims' : 'all'; |
|
248 | + $sub = $this->settings['pro'] ? 'pro' : 'use'; |
|
249 | + $type = $this->settings['type']; |
|
250 | + $version = $this->settings['version']; |
|
251 | + $kit_url = $this->settings['kit-url'] ? esc_url( $this->settings['kit-url'] ) : ''; |
|
252 | + $url = ''; |
|
253 | + |
|
254 | + if ( $type == 'KIT' && $kit_url ) { |
|
255 | + if ( $shims ) { |
|
256 | + // if its a kit then we don't add shims here |
|
257 | + return ''; |
|
258 | + } |
|
259 | + $url .= $kit_url; // CDN |
|
260 | + $url .= "?wpfas=true"; // set our var so our version is not removed |
|
261 | + } else { |
|
262 | + $v = ''; |
|
263 | + // Check and load locally. |
|
264 | + if ( $local && $this->has_local() ) { |
|
265 | + $script .= ".min"; |
|
266 | + $v .= '&ver=' . strip_tags( $this->settings['local_version'] ); |
|
267 | + $url .= $this->get_fonts_url(); // Local fonts url. |
|
268 | + } else { |
|
269 | + $url .= "https://$sub.fontawesome.com/releases/"; // CDN |
|
270 | + $url .= ! empty( $version ) ? "v" . $version . '/' : "v" . $this->get_latest_version() . '/'; // version |
|
271 | + } |
|
272 | + $url .= $type == 'CSS' ? 'css/' : 'js/'; // type |
|
273 | + $url .= $type == 'CSS' ? $script . '.css' : $script . '.js'; // type |
|
274 | + $url .= "?wpfas=true" . $v; // set our var so our version is not removed |
|
275 | + } |
|
276 | + |
|
277 | + return $url; |
|
278 | + } |
|
279 | + |
|
280 | + /** |
|
281 | + * Try and remove any other versions of Font Awesome added by other plugins/themes. |
|
282 | + * |
|
283 | + * Uses the clean_url filter to try and remove any other Font Awesome files added, it can also add pseudo-elements flag for the JS version. |
|
284 | + * |
|
285 | + * @param $url |
|
286 | + * @param $original_url |
|
287 | + * @param $_context |
|
288 | + * |
|
289 | + * @return string The filtered url. |
|
290 | + */ |
|
291 | + public function remove_font_awesome( $url, $original_url, $_context ) { |
|
292 | + |
|
293 | + if ( $_context == 'display' |
|
294 | + && ( strstr( $url, "fontawesome" ) !== false || strstr( $url, "font-awesome" ) !== false ) |
|
295 | + && ( strstr( $url, ".js" ) !== false || strstr( $url, ".css" ) !== false ) |
|
296 | + ) {// it's a font-awesome-url (probably) |
|
297 | + |
|
298 | + if ( strstr( $url, "wpfas=true" ) !== false ) { |
|
299 | + if ( $this->settings['type'] == 'JS' ) { |
|
300 | + if ( $this->settings['js-pseudo'] ) { |
|
301 | + $url .= "' data-search-pseudo-elements defer='defer"; |
|
302 | + } else { |
|
303 | + $url .= "' defer='defer"; |
|
304 | + } |
|
305 | + } |
|
306 | + } else { |
|
307 | + $url = ''; // removing the url removes the file |
|
308 | + } |
|
309 | + |
|
310 | + } |
|
311 | + |
|
312 | + return $url; |
|
313 | + } |
|
314 | + |
|
315 | + /** |
|
316 | + * Register the database settings with WordPress. |
|
317 | + */ |
|
318 | + public function register_settings() { |
|
319 | + register_setting( 'wp-font-awesome-settings', 'wp-font-awesome-settings' ); |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * Add the WordPress settings menu item. |
|
324 | + * @since 1.0.10 Calling function name direct will fail theme check so we don't. |
|
325 | + */ |
|
326 | + public function menu_item() { |
|
327 | + $menu_function = 'add' . '_' . 'options' . '_' . 'page'; // won't pass theme check if function name present in theme |
|
328 | + call_user_func( $menu_function, $this->name, $this->name, 'manage_options', 'wp-font-awesome-settings', array( |
|
329 | + $this, |
|
330 | + 'settings_page' |
|
331 | + ) ); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * Get the current Font Awesome output settings. |
|
336 | + * |
|
337 | + * @return array The array of settings. |
|
338 | + */ |
|
339 | + public function get_settings() { |
|
340 | + $db_settings = get_option( 'wp-font-awesome-settings' ); |
|
341 | + |
|
342 | + $defaults = array( |
|
343 | + 'type' => 'CSS', // type to use, CSS or JS or KIT |
|
344 | + 'version' => '', // latest |
|
345 | + 'enqueue' => '', // front and backend |
|
346 | + 'shims' => '0', // default OFF now in 2020 |
|
347 | + 'js-pseudo' => '0', // if the pseudo elements flag should be set (CPU intensive) |
|
348 | + 'dequeue' => '0', // if we should try to remove other versions added by other plugins/themes |
|
349 | + 'pro' => '0', // if pro CDN url should be used |
|
350 | + 'local' => '0', // Store fonts locally. |
|
351 | + 'local_version' => '', // Local fonts version. |
|
352 | + 'kit-url' => '', // the kit url |
|
353 | + ); |
|
354 | + |
|
355 | + $settings = wp_parse_args( $db_settings, $defaults ); |
|
356 | + |
|
357 | + /** |
|
358 | + * Filter the Font Awesome settings. |
|
359 | + * |
|
360 | + * @todo if we add this filer people might use it and then it defeates the purpose of this class :/ |
|
361 | + */ |
|
362 | + return $this->settings = apply_filters( 'wp-font-awesome-settings', $settings, $db_settings, $defaults ); |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * The settings page html output. |
|
367 | + */ |
|
368 | + public function settings_page() { |
|
369 | + if ( ! current_user_can( 'manage_options' ) ) { |
|
370 | + wp_die( __( 'You do not have sufficient permissions to access this page.', 'font-awesome-settings' ) ); |
|
371 | + } |
|
372 | + |
|
373 | + // a hidden way to force the update of the version number via api instead of waiting the 48 hours |
|
374 | + if ( isset( $_REQUEST['force-version-check'] ) ) { |
|
375 | + $this->get_latest_version( $force_api = true ); |
|
376 | + } |
|
377 | + |
|
378 | + if ( ! defined( 'FONTAWESOME_PLUGIN_FILE' ) ) { |
|
379 | + ?> |
|
380 | 380 | <style> |
381 | 381 | .wpfas-kit-show { |
382 | 382 | display: none; |
@@ -402,16 +402,16 @@ discard block |
||
402 | 402 | <h1><?php echo $this->name; ?></h1> |
403 | 403 | <form method="post" action="options.php" class="fas-settings-form"> |
404 | 404 | <?php |
405 | - settings_fields( 'wp-font-awesome-settings' ); |
|
406 | - do_settings_sections( 'wp-font-awesome-settings' ); |
|
407 | - $table_class = ''; |
|
408 | - if ( $this->settings['type'] ) { |
|
409 | - $table_class .= 'wpfas-' . sanitize_html_class( strtolower( $this->settings['type'] ) ) . '-set'; |
|
410 | - } |
|
411 | - if ( ! empty( $this->settings['pro'] ) ) { |
|
412 | - $table_class .= ' wpfas-has-pro'; |
|
413 | - } |
|
414 | - ?> |
|
405 | + settings_fields( 'wp-font-awesome-settings' ); |
|
406 | + do_settings_sections( 'wp-font-awesome-settings' ); |
|
407 | + $table_class = ''; |
|
408 | + if ( $this->settings['type'] ) { |
|
409 | + $table_class .= 'wpfas-' . sanitize_html_class( strtolower( $this->settings['type'] ) ) . '-set'; |
|
410 | + } |
|
411 | + if ( ! empty( $this->settings['pro'] ) ) { |
|
412 | + $table_class .= ' wpfas-has-pro'; |
|
413 | + } |
|
414 | + ?> |
|
415 | 415 | <?php if ( $this->settings['type'] != 'KIT' && ! empty( $this->settings['local'] ) && empty( $this->settings['pro'] ) ) { ?> |
416 | 416 | <?php if ( $this->has_local() ) { ?> |
417 | 417 | <div class="notice notice-info"><p><strong><?php _e( 'Font Awesome fonts are loading locally.', 'font-awesome-settings' ); ?></strong></p></div> |
@@ -436,12 +436,12 @@ discard block |
||
436 | 436 | <td> |
437 | 437 | <input class="regular-text" id="wpfas-kit-url" type="url" name="wp-font-awesome-settings[kit-url]" value="<?php echo esc_attr( $this->settings['kit-url'] ); ?>" placeholder="<?php echo 'https://kit.font';echo 'awesome.com/123abc.js'; // this won't pass theme check :(?>"/> |
438 | 438 | <span><?php |
439 | - echo sprintf( |
|
440 | - __( 'Requires a free account with Font Awesome. %sGet kit url%s', 'font-awesome-settings' ), |
|
441 | - '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/kits"><i class="fas fa-external-link-alt"></i>', |
|
442 | - '</a>' |
|
443 | - ); |
|
444 | - ?></span> |
|
439 | + echo sprintf( |
|
440 | + __( 'Requires a free account with Font Awesome. %sGet kit url%s', 'font-awesome-settings' ), |
|
441 | + '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/kits"><i class="fas fa-external-link-alt"></i>', |
|
442 | + '</a>' |
|
443 | + ); |
|
444 | + ?></span> |
|
445 | 445 | </td> |
446 | 446 | </tr> |
447 | 447 | |
@@ -482,14 +482,14 @@ discard block |
||
482 | 482 | <input type="hidden" name="wp-font-awesome-settings[pro]" value="0"/> |
483 | 483 | <input type="checkbox" name="wp-font-awesome-settings[pro]" value="1" <?php checked( $this->settings['pro'], '1' ); ?> id="wpfas-pro" onchange="if(jQuery(this).is(':checked')){jQuery('.wpfas-table-settings').addClass('wpfas-has-pro')}else{jQuery('.wpfas-table-settings').removeClass('wpfas-has-pro')}"/> |
484 | 484 | <span><?php |
485 | - echo wp_sprintf( |
|
486 | - __( 'Requires a subscription. %sLearn more%s %sManage my allowed domains%s', 'font-awesome-settings' ), |
|
487 | - '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/referral?a=c9b89e1418">', |
|
488 | - ' <i class="fas fa-external-link-alt"></i></a>', |
|
489 | - '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/account/cdn">', |
|
490 | - ' <i class="fas fa-external-link-alt"></i></a>' |
|
491 | - ); |
|
492 | - ?></span> |
|
485 | + echo wp_sprintf( |
|
486 | + __( 'Requires a subscription. %sLearn more%s %sManage my allowed domains%s', 'font-awesome-settings' ), |
|
487 | + '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/referral?a=c9b89e1418">', |
|
488 | + ' <i class="fas fa-external-link-alt"></i></a>', |
|
489 | + '<a rel="noopener noreferrer" target="_blank" href="https://fontawesome.com/account/cdn">', |
|
490 | + ' <i class="fas fa-external-link-alt"></i></a>' |
|
491 | + ); |
|
492 | + ?></span> |
|
493 | 493 | </td> |
494 | 494 | </tr> |
495 | 495 | |
@@ -543,8 +543,8 @@ discard block |
||
543 | 543 | </table> |
544 | 544 | <div class="fas-buttons"> |
545 | 545 | <?php |
546 | - submit_button(); |
|
547 | - ?> |
|
546 | + submit_button(); |
|
547 | + ?> |
|
548 | 548 | <p class="submit"><a href="https://fontawesome.com/referral?a=c9b89e1418" class="button button-secondary"><?php _e('Get 14,000+ more icons with Font Awesome Pro','font-awesome-settings'); ?> <i class="fas fa-external-link-alt"></i></a></p> |
549 | 549 | |
550 | 550 | </div> |
@@ -553,392 +553,392 @@ discard block |
||
553 | 553 | <div id="wpfas-version"><?php echo sprintf(__( 'Version: %s (affiliate links provided)', 'font-awesome-settings' ), $this->version ); ?></div> |
554 | 554 | </div> |
555 | 555 | <?php |
556 | - } |
|
557 | - } |
|
558 | - |
|
559 | - /** |
|
560 | - * Check a version number is valid and if so return it or else return an empty string. |
|
561 | - * |
|
562 | - * @param $version string The version number to check. |
|
563 | - * |
|
564 | - * @since 1.0.6 |
|
565 | - * |
|
566 | - * @return string Either a valid version number or an empty string. |
|
567 | - */ |
|
568 | - public function validate_version_number( $version ) { |
|
569 | - |
|
570 | - if ( version_compare( $version, '0.0.1', '>=' ) >= 0 ) { |
|
571 | - // valid |
|
572 | - } else { |
|
573 | - $version = '';// not validated |
|
574 | - } |
|
575 | - |
|
576 | - return $version; |
|
577 | - } |
|
578 | - |
|
579 | - |
|
580 | - /** |
|
581 | - * Get the latest version of Font Awesome. |
|
582 | - * |
|
583 | - * We check for a cached version and if none we will check for a live version via API and then cache it for 48 hours. |
|
584 | - * |
|
585 | - * @since 1.0.7 |
|
586 | - * @return mixed|string The latest version number found. |
|
587 | - */ |
|
588 | - public function get_latest_version( $force_api = false ) { |
|
589 | - $latest_version = $this->latest; |
|
590 | - |
|
591 | - $cache = get_transient( 'wp-font-awesome-settings-version' ); |
|
592 | - |
|
593 | - if ( $cache === false || $force_api ) { // its not set |
|
594 | - $api_ver = $this->get_latest_version_from_api(); |
|
595 | - if ( version_compare( $api_ver, $this->latest, '>=' ) >= 0 ) { |
|
596 | - $latest_version = $api_ver; |
|
597 | - set_transient( 'wp-font-awesome-settings-version', $api_ver, 48 * HOUR_IN_SECONDS ); |
|
598 | - } |
|
599 | - } elseif ( $this->validate_version_number( $cache ) ) { |
|
600 | - if ( version_compare( $cache, $this->latest, '>=' ) >= 0 ) { |
|
601 | - $latest_version = $cache; |
|
602 | - } |
|
603 | - } |
|
604 | - |
|
605 | - // Check and auto download fonts locally. |
|
606 | - if ( empty( $this->settings['pro'] ) && empty( $this->settings['version'] ) && $this->settings['type'] != 'KIT' && ! empty( $this->settings['local'] ) && ! empty( $this->settings['local_version'] ) && ! empty( $latest_version ) ) { |
|
607 | - if ( version_compare( $latest_version, $this->settings['local_version'], '>' ) && is_admin() && ! wp_doing_ajax() ) { |
|
608 | - $this->download_package( $latest_version ); |
|
609 | - } |
|
610 | - } |
|
611 | - |
|
612 | - return $latest_version; |
|
613 | - } |
|
614 | - |
|
615 | - /** |
|
616 | - * Get the latest Font Awesome version from the github API. |
|
617 | - * |
|
618 | - * @since 1.0.7 |
|
619 | - * @return string The latest version number or `0` on API fail. |
|
620 | - */ |
|
621 | - public function get_latest_version_from_api() { |
|
622 | - $version = "0"; |
|
623 | - $response = wp_remote_get( "https://api.github.com/repos/FortAwesome/Font-Awesome/releases/latest" ); |
|
624 | - if ( ! is_wp_error( $response ) && is_array( $response ) ) { |
|
625 | - $api_response = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
626 | - if ( isset( $api_response['tag_name'] ) && version_compare( $api_response['tag_name'], $this->latest, '>=' ) >= 0 && empty( $api_response['prerelease'] ) ) { |
|
627 | - $version = $api_response['tag_name']; |
|
628 | - } |
|
629 | - } |
|
630 | - |
|
631 | - return $version; |
|
632 | - } |
|
633 | - |
|
634 | - /** |
|
635 | - * Inline CSS for RTL language support. |
|
636 | - * |
|
637 | - * @since 1.0.13 |
|
638 | - * @return string Inline CSS. |
|
639 | - */ |
|
640 | - public function rtl_inline_css() { |
|
641 | - $inline_css = '[dir=rtl] .fa-address,[dir=rtl] .fa-address-card,[dir=rtl] .fa-adjust,[dir=rtl] .fa-alarm-clock,[dir=rtl] .fa-align-left,[dir=rtl] .fa-align-right,[dir=rtl] .fa-analytics,[dir=rtl] .fa-angle-double-left,[dir=rtl] .fa-angle-double-right,[dir=rtl] .fa-angle-left,[dir=rtl] .fa-angle-right,[dir=rtl] .fa-arrow-alt-circle-left,[dir=rtl] .fa-arrow-alt-circle-right,[dir=rtl] .fa-arrow-alt-from-left,[dir=rtl] .fa-arrow-alt-from-right,[dir=rtl] .fa-arrow-alt-left,[dir=rtl] .fa-arrow-alt-right,[dir=rtl] .fa-arrow-alt-square-left,[dir=rtl] .fa-arrow-alt-square-right,[dir=rtl] .fa-arrow-alt-to-left,[dir=rtl] .fa-arrow-alt-to-right,[dir=rtl] .fa-arrow-circle-left,[dir=rtl] .fa-arrow-circle-right,[dir=rtl] .fa-arrow-from-left,[dir=rtl] .fa-arrow-from-right,[dir=rtl] .fa-arrow-left,[dir=rtl] .fa-arrow-right,[dir=rtl] .fa-arrow-square-left,[dir=rtl] .fa-arrow-square-right,[dir=rtl] .fa-arrow-to-left,[dir=rtl] .fa-arrow-to-right,[dir=rtl] .fa-balance-scale-left,[dir=rtl] .fa-balance-scale-right,[dir=rtl] .fa-bed,[dir=rtl] .fa-bed-bunk,[dir=rtl] .fa-bed-empty,[dir=rtl] .fa-border-left,[dir=rtl] .fa-border-right,[dir=rtl] .fa-calendar-check,[dir=rtl] .fa-caret-circle-left,[dir=rtl] .fa-caret-circle-right,[dir=rtl] .fa-caret-left,[dir=rtl] .fa-caret-right,[dir=rtl] .fa-caret-square-left,[dir=rtl] .fa-caret-square-right,[dir=rtl] .fa-cart-arrow-down,[dir=rtl] .fa-cart-plus,[dir=rtl] .fa-chart-area,[dir=rtl] .fa-chart-bar,[dir=rtl] .fa-chart-line,[dir=rtl] .fa-chart-line-down,[dir=rtl] .fa-chart-network,[dir=rtl] .fa-chart-pie,[dir=rtl] .fa-chart-pie-alt,[dir=rtl] .fa-chart-scatter,[dir=rtl] .fa-check-circle,[dir=rtl] .fa-check-square,[dir=rtl] .fa-chevron-circle-left,[dir=rtl] .fa-chevron-circle-right,[dir=rtl] .fa-chevron-double-left,[dir=rtl] .fa-chevron-double-right,[dir=rtl] .fa-chevron-left,[dir=rtl] .fa-chevron-right,[dir=rtl] .fa-chevron-square-left,[dir=rtl] .fa-chevron-square-right,[dir=rtl] .fa-clock,[dir=rtl] .fa-file,[dir=rtl] .fa-file-alt,[dir=rtl] .fa-file-archive,[dir=rtl] .fa-file-audio,[dir=rtl] .fa-file-chart-line,[dir=rtl] .fa-file-chart-pie,[dir=rtl] .fa-file-code,[dir=rtl] .fa-file-excel,[dir=rtl] .fa-file-image,[dir=rtl] .fa-file-pdf,[dir=rtl] .fa-file-powerpoint,[dir=rtl] .fa-file-video,[dir=rtl] .fa-file-word,[dir=rtl] .fa-flag,[dir=rtl] .fa-folder,[dir=rtl] .fa-folder-open,[dir=rtl] .fa-hand-lizard,[dir=rtl] .fa-hand-point-down,[dir=rtl] .fa-hand-point-left,[dir=rtl] .fa-hand-point-right,[dir=rtl] .fa-hand-point-up,[dir=rtl] .fa-hand-scissors,[dir=rtl] .fa-image,[dir=rtl] .fa-long-arrow-alt-left,[dir=rtl] .fa-long-arrow-alt-right,[dir=rtl] .fa-long-arrow-left,[dir=rtl] .fa-long-arrow-right,[dir=rtl] .fa-luggage-cart,[dir=rtl] .fa-moon,[dir=rtl] .fa-pencil,[dir=rtl] .fa-pencil-alt,[dir=rtl] .fa-play-circle,[dir=rtl] .fa-project-diagram,[dir=rtl] .fa-quote-left,[dir=rtl] .fa-quote-right,[dir=rtl] .fa-shopping-cart,[dir=rtl] .fa-thumbs-down,[dir=rtl] .fa-thumbs-up,[dir=rtl] .fa-user-chart{filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=0, mirror=1);transform:scale(-1,1)}[dir=rtl] .fa-spin{animation-direction:reverse}'; |
|
642 | - |
|
643 | - return $inline_css; |
|
644 | - } |
|
645 | - |
|
646 | - /** |
|
647 | - * Show any warnings as an admin notice. |
|
648 | - * |
|
649 | - * @return void |
|
650 | - */ |
|
651 | - public function admin_notices() { |
|
652 | - $settings = $this->settings; |
|
653 | - |
|
654 | - if ( defined( 'FONTAWESOME_PLUGIN_FILE' ) ) { |
|
655 | - if ( ! empty( $_REQUEST['page'] ) && $_REQUEST['page'] == 'wp-font-awesome-settings' ) { |
|
656 | - ?> |
|
556 | + } |
|
557 | + } |
|
558 | + |
|
559 | + /** |
|
560 | + * Check a version number is valid and if so return it or else return an empty string. |
|
561 | + * |
|
562 | + * @param $version string The version number to check. |
|
563 | + * |
|
564 | + * @since 1.0.6 |
|
565 | + * |
|
566 | + * @return string Either a valid version number or an empty string. |
|
567 | + */ |
|
568 | + public function validate_version_number( $version ) { |
|
569 | + |
|
570 | + if ( version_compare( $version, '0.0.1', '>=' ) >= 0 ) { |
|
571 | + // valid |
|
572 | + } else { |
|
573 | + $version = '';// not validated |
|
574 | + } |
|
575 | + |
|
576 | + return $version; |
|
577 | + } |
|
578 | + |
|
579 | + |
|
580 | + /** |
|
581 | + * Get the latest version of Font Awesome. |
|
582 | + * |
|
583 | + * We check for a cached version and if none we will check for a live version via API and then cache it for 48 hours. |
|
584 | + * |
|
585 | + * @since 1.0.7 |
|
586 | + * @return mixed|string The latest version number found. |
|
587 | + */ |
|
588 | + public function get_latest_version( $force_api = false ) { |
|
589 | + $latest_version = $this->latest; |
|
590 | + |
|
591 | + $cache = get_transient( 'wp-font-awesome-settings-version' ); |
|
592 | + |
|
593 | + if ( $cache === false || $force_api ) { // its not set |
|
594 | + $api_ver = $this->get_latest_version_from_api(); |
|
595 | + if ( version_compare( $api_ver, $this->latest, '>=' ) >= 0 ) { |
|
596 | + $latest_version = $api_ver; |
|
597 | + set_transient( 'wp-font-awesome-settings-version', $api_ver, 48 * HOUR_IN_SECONDS ); |
|
598 | + } |
|
599 | + } elseif ( $this->validate_version_number( $cache ) ) { |
|
600 | + if ( version_compare( $cache, $this->latest, '>=' ) >= 0 ) { |
|
601 | + $latest_version = $cache; |
|
602 | + } |
|
603 | + } |
|
604 | + |
|
605 | + // Check and auto download fonts locally. |
|
606 | + if ( empty( $this->settings['pro'] ) && empty( $this->settings['version'] ) && $this->settings['type'] != 'KIT' && ! empty( $this->settings['local'] ) && ! empty( $this->settings['local_version'] ) && ! empty( $latest_version ) ) { |
|
607 | + if ( version_compare( $latest_version, $this->settings['local_version'], '>' ) && is_admin() && ! wp_doing_ajax() ) { |
|
608 | + $this->download_package( $latest_version ); |
|
609 | + } |
|
610 | + } |
|
611 | + |
|
612 | + return $latest_version; |
|
613 | + } |
|
614 | + |
|
615 | + /** |
|
616 | + * Get the latest Font Awesome version from the github API. |
|
617 | + * |
|
618 | + * @since 1.0.7 |
|
619 | + * @return string The latest version number or `0` on API fail. |
|
620 | + */ |
|
621 | + public function get_latest_version_from_api() { |
|
622 | + $version = "0"; |
|
623 | + $response = wp_remote_get( "https://api.github.com/repos/FortAwesome/Font-Awesome/releases/latest" ); |
|
624 | + if ( ! is_wp_error( $response ) && is_array( $response ) ) { |
|
625 | + $api_response = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
626 | + if ( isset( $api_response['tag_name'] ) && version_compare( $api_response['tag_name'], $this->latest, '>=' ) >= 0 && empty( $api_response['prerelease'] ) ) { |
|
627 | + $version = $api_response['tag_name']; |
|
628 | + } |
|
629 | + } |
|
630 | + |
|
631 | + return $version; |
|
632 | + } |
|
633 | + |
|
634 | + /** |
|
635 | + * Inline CSS for RTL language support. |
|
636 | + * |
|
637 | + * @since 1.0.13 |
|
638 | + * @return string Inline CSS. |
|
639 | + */ |
|
640 | + public function rtl_inline_css() { |
|
641 | + $inline_css = '[dir=rtl] .fa-address,[dir=rtl] .fa-address-card,[dir=rtl] .fa-adjust,[dir=rtl] .fa-alarm-clock,[dir=rtl] .fa-align-left,[dir=rtl] .fa-align-right,[dir=rtl] .fa-analytics,[dir=rtl] .fa-angle-double-left,[dir=rtl] .fa-angle-double-right,[dir=rtl] .fa-angle-left,[dir=rtl] .fa-angle-right,[dir=rtl] .fa-arrow-alt-circle-left,[dir=rtl] .fa-arrow-alt-circle-right,[dir=rtl] .fa-arrow-alt-from-left,[dir=rtl] .fa-arrow-alt-from-right,[dir=rtl] .fa-arrow-alt-left,[dir=rtl] .fa-arrow-alt-right,[dir=rtl] .fa-arrow-alt-square-left,[dir=rtl] .fa-arrow-alt-square-right,[dir=rtl] .fa-arrow-alt-to-left,[dir=rtl] .fa-arrow-alt-to-right,[dir=rtl] .fa-arrow-circle-left,[dir=rtl] .fa-arrow-circle-right,[dir=rtl] .fa-arrow-from-left,[dir=rtl] .fa-arrow-from-right,[dir=rtl] .fa-arrow-left,[dir=rtl] .fa-arrow-right,[dir=rtl] .fa-arrow-square-left,[dir=rtl] .fa-arrow-square-right,[dir=rtl] .fa-arrow-to-left,[dir=rtl] .fa-arrow-to-right,[dir=rtl] .fa-balance-scale-left,[dir=rtl] .fa-balance-scale-right,[dir=rtl] .fa-bed,[dir=rtl] .fa-bed-bunk,[dir=rtl] .fa-bed-empty,[dir=rtl] .fa-border-left,[dir=rtl] .fa-border-right,[dir=rtl] .fa-calendar-check,[dir=rtl] .fa-caret-circle-left,[dir=rtl] .fa-caret-circle-right,[dir=rtl] .fa-caret-left,[dir=rtl] .fa-caret-right,[dir=rtl] .fa-caret-square-left,[dir=rtl] .fa-caret-square-right,[dir=rtl] .fa-cart-arrow-down,[dir=rtl] .fa-cart-plus,[dir=rtl] .fa-chart-area,[dir=rtl] .fa-chart-bar,[dir=rtl] .fa-chart-line,[dir=rtl] .fa-chart-line-down,[dir=rtl] .fa-chart-network,[dir=rtl] .fa-chart-pie,[dir=rtl] .fa-chart-pie-alt,[dir=rtl] .fa-chart-scatter,[dir=rtl] .fa-check-circle,[dir=rtl] .fa-check-square,[dir=rtl] .fa-chevron-circle-left,[dir=rtl] .fa-chevron-circle-right,[dir=rtl] .fa-chevron-double-left,[dir=rtl] .fa-chevron-double-right,[dir=rtl] .fa-chevron-left,[dir=rtl] .fa-chevron-right,[dir=rtl] .fa-chevron-square-left,[dir=rtl] .fa-chevron-square-right,[dir=rtl] .fa-clock,[dir=rtl] .fa-file,[dir=rtl] .fa-file-alt,[dir=rtl] .fa-file-archive,[dir=rtl] .fa-file-audio,[dir=rtl] .fa-file-chart-line,[dir=rtl] .fa-file-chart-pie,[dir=rtl] .fa-file-code,[dir=rtl] .fa-file-excel,[dir=rtl] .fa-file-image,[dir=rtl] .fa-file-pdf,[dir=rtl] .fa-file-powerpoint,[dir=rtl] .fa-file-video,[dir=rtl] .fa-file-word,[dir=rtl] .fa-flag,[dir=rtl] .fa-folder,[dir=rtl] .fa-folder-open,[dir=rtl] .fa-hand-lizard,[dir=rtl] .fa-hand-point-down,[dir=rtl] .fa-hand-point-left,[dir=rtl] .fa-hand-point-right,[dir=rtl] .fa-hand-point-up,[dir=rtl] .fa-hand-scissors,[dir=rtl] .fa-image,[dir=rtl] .fa-long-arrow-alt-left,[dir=rtl] .fa-long-arrow-alt-right,[dir=rtl] .fa-long-arrow-left,[dir=rtl] .fa-long-arrow-right,[dir=rtl] .fa-luggage-cart,[dir=rtl] .fa-moon,[dir=rtl] .fa-pencil,[dir=rtl] .fa-pencil-alt,[dir=rtl] .fa-play-circle,[dir=rtl] .fa-project-diagram,[dir=rtl] .fa-quote-left,[dir=rtl] .fa-quote-right,[dir=rtl] .fa-shopping-cart,[dir=rtl] .fa-thumbs-down,[dir=rtl] .fa-thumbs-up,[dir=rtl] .fa-user-chart{filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=0, mirror=1);transform:scale(-1,1)}[dir=rtl] .fa-spin{animation-direction:reverse}'; |
|
642 | + |
|
643 | + return $inline_css; |
|
644 | + } |
|
645 | + |
|
646 | + /** |
|
647 | + * Show any warnings as an admin notice. |
|
648 | + * |
|
649 | + * @return void |
|
650 | + */ |
|
651 | + public function admin_notices() { |
|
652 | + $settings = $this->settings; |
|
653 | + |
|
654 | + if ( defined( 'FONTAWESOME_PLUGIN_FILE' ) ) { |
|
655 | + if ( ! empty( $_REQUEST['page'] ) && $_REQUEST['page'] == 'wp-font-awesome-settings' ) { |
|
656 | + ?> |
|
657 | 657 | <div class="notice notice-error is-dismissible"> |
658 | 658 | <p><?php _e( 'The Official Font Awesome Plugin is active, please adjust your settings there.', 'font-awesome-settings' ); ?></p> |
659 | 659 | </div> |
660 | 660 | <?php |
661 | - } |
|
662 | - } else { |
|
663 | - if ( ! empty( $settings ) ) { |
|
664 | - if ( $settings['type'] != 'KIT' && $settings['pro'] && ( $settings['version'] == '' || version_compare( $settings['version'], '6', '>=' ) ) ) { |
|
665 | - $link = admin_url('options-general.php?page=wp-font-awesome-settings'); |
|
666 | - ?> |
|
661 | + } |
|
662 | + } else { |
|
663 | + if ( ! empty( $settings ) ) { |
|
664 | + if ( $settings['type'] != 'KIT' && $settings['pro'] && ( $settings['version'] == '' || version_compare( $settings['version'], '6', '>=' ) ) ) { |
|
665 | + $link = admin_url('options-general.php?page=wp-font-awesome-settings'); |
|
666 | + ?> |
|
667 | 667 | <div class="notice notice-error is-dismissible"> |
668 | 668 | <p><?php echo sprintf( __( 'Font Awesome Pro v6 requires the use of a kit, please setup your kit in %ssettings.%s', 'font-awesome-settings' ),"<a href='". esc_url_raw( $link )."'>","</a>" ); ?></p> |
669 | 669 | </div> |
670 | 670 | <?php |
671 | - } |
|
672 | - } |
|
673 | - } |
|
674 | - } |
|
675 | - |
|
676 | - /** |
|
677 | - * Handle fontawesome add settings to download fontawesome to store locally. |
|
678 | - * |
|
679 | - * @since 1.1.1 |
|
680 | - * |
|
681 | - * @param string $option The option name. |
|
682 | - * @param mixed $value The option value. |
|
683 | - */ |
|
684 | - public function add_option_wp_font_awesome_settings( $option, $value ) { |
|
685 | - // Do nothing if WordPress is being installed. |
|
686 | - if ( wp_installing() ) { |
|
687 | - return; |
|
688 | - } |
|
689 | - |
|
690 | - if ( ! empty( $value['local'] ) && empty( $value['pro'] ) && ! ( ! empty( $value['type'] ) && $value['type'] == 'KIT' ) ) { |
|
691 | - $version = isset( $value['version'] ) && $value['version'] ? $value['version'] : $this->get_latest_version(); |
|
692 | - |
|
693 | - if ( ! empty( $version ) ) { |
|
694 | - $response = $this->download_package( $version, $value ); |
|
695 | - |
|
696 | - if ( is_wp_error( $response ) ) { |
|
697 | - add_settings_error( 'general', 'fontawesome_download', __( 'ERROR:', 'font-awesome-settings' ) . ' ' . $response->get_error_message(), 'error' ); |
|
698 | - } |
|
699 | - } |
|
700 | - } |
|
701 | - } |
|
702 | - |
|
703 | - /** |
|
704 | - * Handle fontawesome update settings to download fontawesome to store locally. |
|
705 | - * |
|
706 | - * @since 1.1.0 |
|
707 | - * |
|
708 | - * @param mixed $old_value The old option value. |
|
709 | - * @param mixed $value The new option value. |
|
710 | - */ |
|
711 | - public function update_option_wp_font_awesome_settings( $old_value, $new_value ) { |
|
712 | - // Do nothing if WordPress is being installed. |
|
713 | - if ( wp_installing() ) { |
|
714 | - return; |
|
715 | - } |
|
716 | - |
|
717 | - if ( ! empty( $new_value['local'] ) && empty( $new_value['pro'] ) && ! ( ! empty( $new_value['type'] ) && $new_value['type'] == 'KIT' ) ) { |
|
718 | - // Old values |
|
719 | - $old_version = isset( $old_value['version'] ) && $old_value['version'] ? $old_value['version'] : ( isset( $old_value['local_version'] ) ? $old_value['local_version'] : '' ); |
|
720 | - $old_local = isset( $old_value['local'] ) ? (int) $old_value['local'] : 0; |
|
721 | - |
|
722 | - // New values |
|
723 | - $new_version = isset( $new_value['version'] ) && $new_value['version'] ? $new_value['version'] : $this->get_latest_version(); |
|
724 | - |
|
725 | - if ( empty( $old_local ) || $old_version !== $new_version || ! file_exists( $this->get_fonts_dir() . 'css' . DIRECTORY_SEPARATOR . 'all.css' ) ) { |
|
726 | - $response = $this->download_package( $new_version, $new_value ); |
|
727 | - |
|
728 | - if ( is_wp_error( $response ) ) { |
|
729 | - add_settings_error( 'general', 'fontawesome_download', __( 'ERROR:', 'font-awesome-settings' ) . ' ' . $response->get_error_message(), 'error' ); |
|
730 | - } |
|
731 | - } |
|
732 | - } |
|
733 | - } |
|
734 | - |
|
735 | - /** |
|
736 | - * Get the fonts directory local path. |
|
737 | - * |
|
738 | - * @since 1.1.0 |
|
739 | - * |
|
740 | - * @param string Fonts directory local path. |
|
741 | - */ |
|
742 | - public function get_fonts_dir() { |
|
743 | - $upload_dir = wp_upload_dir( null, false ); |
|
744 | - |
|
745 | - return $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'ayefonts' . DIRECTORY_SEPARATOR . 'fa' . DIRECTORY_SEPARATOR; |
|
746 | - } |
|
747 | - |
|
748 | - /** |
|
749 | - * Get the fonts directory local url. |
|
750 | - * |
|
751 | - * @since 1.1.0 |
|
752 | - * |
|
753 | - * @param string Fonts directory local url. |
|
754 | - */ |
|
755 | - public function get_fonts_url() { |
|
756 | - $upload_dir = wp_upload_dir( null, false ); |
|
757 | - |
|
758 | - return $upload_dir['baseurl'] . '/ayefonts/fa/'; |
|
759 | - } |
|
760 | - |
|
761 | - /** |
|
762 | - * Check whether load locally active. |
|
763 | - * |
|
764 | - * @since 1.1.0 |
|
765 | - * |
|
766 | - * @return bool True if active else false. |
|
767 | - */ |
|
768 | - public function has_local() { |
|
769 | - if ( ! empty( $this->settings['local'] ) && empty( $this->settings['pro'] ) && file_exists( $this->get_fonts_dir() . 'css' . DIRECTORY_SEPARATOR . 'all.css' ) ) { |
|
770 | - return true; |
|
771 | - } |
|
772 | - |
|
773 | - return false; |
|
774 | - } |
|
775 | - |
|
776 | - /** |
|
777 | - * Get the WP Filesystem access. |
|
778 | - * |
|
779 | - * @since 1.1.0 |
|
780 | - * |
|
781 | - * @return object The WP Filesystem. |
|
782 | - */ |
|
783 | - public function get_wp_filesystem() { |
|
784 | - if ( ! function_exists( 'get_filesystem_method' ) ) { |
|
785 | - require_once( ABSPATH . "/wp-admin/includes/file.php" ); |
|
786 | - } |
|
787 | - |
|
788 | - $access_type = get_filesystem_method(); |
|
789 | - |
|
790 | - if ( $access_type === 'direct' ) { |
|
791 | - /* You can safely run request_filesystem_credentials() without any issues and don't need to worry about passing in a URL */ |
|
792 | - $creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() ); |
|
793 | - |
|
794 | - /* Initialize the API */ |
|
795 | - if ( ! WP_Filesystem( $creds ) ) { |
|
796 | - /* Any problems and we exit */ |
|
797 | - return false; |
|
798 | - } |
|
799 | - |
|
800 | - global $wp_filesystem; |
|
801 | - |
|
802 | - return $wp_filesystem; |
|
803 | - /* Do our file manipulations below */ |
|
804 | - } else if ( defined( 'FTP_USER' ) ) { |
|
805 | - $creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() ); |
|
806 | - |
|
807 | - /* Initialize the API */ |
|
808 | - if ( ! WP_Filesystem( $creds ) ) { |
|
809 | - /* Any problems and we exit */ |
|
810 | - return false; |
|
811 | - } |
|
812 | - |
|
813 | - global $wp_filesystem; |
|
814 | - |
|
815 | - return $wp_filesystem; |
|
816 | - } else { |
|
817 | - /* Don't have direct write access. Prompt user with our notice */ |
|
818 | - return false; |
|
819 | - } |
|
820 | - } |
|
821 | - |
|
822 | - /** |
|
823 | - * Download the fontawesome package file. |
|
824 | - * |
|
825 | - * @since 1.1.0 |
|
826 | - * |
|
827 | - * @param mixed $version The font awesome. |
|
828 | - * @param array $option Fontawesome settings. |
|
829 | - * @return WP_ERROR|bool Error on fail and true on success. |
|
830 | - */ |
|
831 | - public function download_package( $version, $option = array() ) { |
|
832 | - $filename = 'fontawesome-free-' . $version . '-web'; |
|
833 | - $url = 'https://use.fontawesome.com/releases/v' . $version . '/' . $filename . '.zip'; |
|
834 | - |
|
835 | - if ( ! function_exists( 'wp_handle_upload' ) ) { |
|
836 | - require_once ABSPATH . 'wp-admin/includes/file.php'; |
|
837 | - } |
|
838 | - |
|
839 | - $download_file = download_url( esc_url_raw( $url ) ); |
|
840 | - |
|
841 | - if ( is_wp_error( $download_file ) ) { |
|
842 | - return new WP_Error( 'fontawesome_download_failed', __( $download_file->get_error_message(), 'font-awesome-settings' ) ); |
|
843 | - } else if ( empty( $download_file ) ) { |
|
844 | - return new WP_Error( 'fontawesome_download_failed', __( 'Something went wrong in downloading the font awesome to store locally.', 'font-awesome-settings' ) ); |
|
845 | - } |
|
846 | - |
|
847 | - $response = $this->extract_package( $download_file, $filename, true ); |
|
848 | - |
|
849 | - // Update local version. |
|
850 | - if ( is_wp_error( $response ) ) { |
|
851 | - return $response; |
|
852 | - } else if ( $response ) { |
|
853 | - if ( empty( $option ) ) { |
|
854 | - $option = get_option( 'wp-font-awesome-settings' ); |
|
855 | - } |
|
856 | - |
|
857 | - $option['local_version'] = $version; |
|
858 | - |
|
859 | - // Remove action to prevent looping. |
|
860 | - remove_action( 'update_option_wp-font-awesome-settings', array( $this, 'update_option_wp_font_awesome_settings' ), 10, 2 ); |
|
861 | - |
|
862 | - update_option( 'wp-font-awesome-settings', $option ); |
|
863 | - |
|
864 | - return true; |
|
865 | - } |
|
866 | - |
|
867 | - return false; |
|
868 | - } |
|
869 | - |
|
870 | - /** |
|
871 | - * Extract the fontawesome package file. |
|
872 | - * |
|
873 | - * @since 1.1.0 |
|
874 | - * |
|
875 | - * @param string $package The package file path. |
|
876 | - * @param string $dirname Package file name. |
|
877 | - * @param bool $delete_package Delete temp file or not. |
|
878 | - * @return WP_Error|bool True on success WP_Error on fail. |
|
879 | - */ |
|
880 | - public function extract_package( $package, $dirname = '', $delete_package = false ) { |
|
881 | - global $wp_filesystem; |
|
882 | - |
|
883 | - $wp_filesystem = $this->get_wp_filesystem(); |
|
884 | - |
|
885 | - if ( empty( $wp_filesystem ) && isset( $wp_filesystem->errors ) && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { |
|
886 | - return new WP_Error( 'fontawesome_filesystem_error', __( $wp_filesystem->errors->get_error_message(), 'font-awesome-settings' ) ); |
|
887 | - } else if ( empty( $wp_filesystem ) ) { |
|
888 | - return new WP_Error( 'fontawesome_filesystem_error', __( 'Failed to initialise WP_Filesystem while trying to download the Font Awesome package.', 'font-awesome-settings' ) ); |
|
889 | - } |
|
890 | - |
|
891 | - $fonts_dir = $this->get_fonts_dir(); |
|
892 | - $fonts_tmp_dir = dirname( $fonts_dir ) . DIRECTORY_SEPARATOR . 'fa-tmp' . DIRECTORY_SEPARATOR; |
|
893 | - |
|
894 | - if ( $wp_filesystem->is_dir( $fonts_tmp_dir ) ) { |
|
895 | - $wp_filesystem->delete( $fonts_tmp_dir, true ); |
|
896 | - } |
|
897 | - |
|
898 | - // Unzip package to working directory. |
|
899 | - $result = unzip_file( $package, $fonts_tmp_dir ); |
|
900 | - |
|
901 | - if ( is_wp_error( $result ) ) { |
|
902 | - $wp_filesystem->delete( $fonts_tmp_dir, true ); |
|
903 | - |
|
904 | - if ( 'incompatible_archive' === $result->get_error_code() ) { |
|
905 | - return new WP_Error( 'fontawesome_incompatible_archive', __( $result->get_error_message(), 'font-awesome-settings' ) ); |
|
906 | - } |
|
907 | - |
|
908 | - return $result; |
|
909 | - } |
|
910 | - |
|
911 | - if ( $wp_filesystem->is_dir( $fonts_dir ) ) { |
|
912 | - $wp_filesystem->delete( $fonts_dir, true ); |
|
913 | - } |
|
914 | - |
|
915 | - $extract_dir = $fonts_tmp_dir; |
|
916 | - |
|
917 | - if ( $dirname && $wp_filesystem->is_dir( $extract_dir . $dirname . DIRECTORY_SEPARATOR ) ) { |
|
918 | - $extract_dir .= $dirname . DIRECTORY_SEPARATOR; |
|
919 | - } |
|
920 | - |
|
921 | - try { |
|
922 | - $return = $wp_filesystem->move( $extract_dir, $fonts_dir, true ); |
|
923 | - } catch ( Exception $e ) { |
|
924 | - $return = new WP_Error( 'fontawesome_move_package', __( 'Fail to move font awesome package!', 'font-awesome-settings' ) ); |
|
925 | - } |
|
926 | - |
|
927 | - if ( $wp_filesystem->is_dir( $fonts_tmp_dir ) ) { |
|
928 | - $wp_filesystem->delete( $fonts_tmp_dir, true ); |
|
929 | - } |
|
930 | - |
|
931 | - // Once extracted, delete the package if required. |
|
932 | - if ( $delete_package ) { |
|
933 | - unlink( $package ); |
|
934 | - } |
|
935 | - |
|
936 | - return $return; |
|
937 | - } |
|
938 | - } |
|
939 | - |
|
940 | - /** |
|
941 | - * Run the class if found. |
|
942 | - */ |
|
943 | - WP_Font_Awesome_Settings::instance(); |
|
671 | + } |
|
672 | + } |
|
673 | + } |
|
674 | + } |
|
675 | + |
|
676 | + /** |
|
677 | + * Handle fontawesome add settings to download fontawesome to store locally. |
|
678 | + * |
|
679 | + * @since 1.1.1 |
|
680 | + * |
|
681 | + * @param string $option The option name. |
|
682 | + * @param mixed $value The option value. |
|
683 | + */ |
|
684 | + public function add_option_wp_font_awesome_settings( $option, $value ) { |
|
685 | + // Do nothing if WordPress is being installed. |
|
686 | + if ( wp_installing() ) { |
|
687 | + return; |
|
688 | + } |
|
689 | + |
|
690 | + if ( ! empty( $value['local'] ) && empty( $value['pro'] ) && ! ( ! empty( $value['type'] ) && $value['type'] == 'KIT' ) ) { |
|
691 | + $version = isset( $value['version'] ) && $value['version'] ? $value['version'] : $this->get_latest_version(); |
|
692 | + |
|
693 | + if ( ! empty( $version ) ) { |
|
694 | + $response = $this->download_package( $version, $value ); |
|
695 | + |
|
696 | + if ( is_wp_error( $response ) ) { |
|
697 | + add_settings_error( 'general', 'fontawesome_download', __( 'ERROR:', 'font-awesome-settings' ) . ' ' . $response->get_error_message(), 'error' ); |
|
698 | + } |
|
699 | + } |
|
700 | + } |
|
701 | + } |
|
702 | + |
|
703 | + /** |
|
704 | + * Handle fontawesome update settings to download fontawesome to store locally. |
|
705 | + * |
|
706 | + * @since 1.1.0 |
|
707 | + * |
|
708 | + * @param mixed $old_value The old option value. |
|
709 | + * @param mixed $value The new option value. |
|
710 | + */ |
|
711 | + public function update_option_wp_font_awesome_settings( $old_value, $new_value ) { |
|
712 | + // Do nothing if WordPress is being installed. |
|
713 | + if ( wp_installing() ) { |
|
714 | + return; |
|
715 | + } |
|
716 | + |
|
717 | + if ( ! empty( $new_value['local'] ) && empty( $new_value['pro'] ) && ! ( ! empty( $new_value['type'] ) && $new_value['type'] == 'KIT' ) ) { |
|
718 | + // Old values |
|
719 | + $old_version = isset( $old_value['version'] ) && $old_value['version'] ? $old_value['version'] : ( isset( $old_value['local_version'] ) ? $old_value['local_version'] : '' ); |
|
720 | + $old_local = isset( $old_value['local'] ) ? (int) $old_value['local'] : 0; |
|
721 | + |
|
722 | + // New values |
|
723 | + $new_version = isset( $new_value['version'] ) && $new_value['version'] ? $new_value['version'] : $this->get_latest_version(); |
|
724 | + |
|
725 | + if ( empty( $old_local ) || $old_version !== $new_version || ! file_exists( $this->get_fonts_dir() . 'css' . DIRECTORY_SEPARATOR . 'all.css' ) ) { |
|
726 | + $response = $this->download_package( $new_version, $new_value ); |
|
727 | + |
|
728 | + if ( is_wp_error( $response ) ) { |
|
729 | + add_settings_error( 'general', 'fontawesome_download', __( 'ERROR:', 'font-awesome-settings' ) . ' ' . $response->get_error_message(), 'error' ); |
|
730 | + } |
|
731 | + } |
|
732 | + } |
|
733 | + } |
|
734 | + |
|
735 | + /** |
|
736 | + * Get the fonts directory local path. |
|
737 | + * |
|
738 | + * @since 1.1.0 |
|
739 | + * |
|
740 | + * @param string Fonts directory local path. |
|
741 | + */ |
|
742 | + public function get_fonts_dir() { |
|
743 | + $upload_dir = wp_upload_dir( null, false ); |
|
744 | + |
|
745 | + return $upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'ayefonts' . DIRECTORY_SEPARATOR . 'fa' . DIRECTORY_SEPARATOR; |
|
746 | + } |
|
747 | + |
|
748 | + /** |
|
749 | + * Get the fonts directory local url. |
|
750 | + * |
|
751 | + * @since 1.1.0 |
|
752 | + * |
|
753 | + * @param string Fonts directory local url. |
|
754 | + */ |
|
755 | + public function get_fonts_url() { |
|
756 | + $upload_dir = wp_upload_dir( null, false ); |
|
757 | + |
|
758 | + return $upload_dir['baseurl'] . '/ayefonts/fa/'; |
|
759 | + } |
|
760 | + |
|
761 | + /** |
|
762 | + * Check whether load locally active. |
|
763 | + * |
|
764 | + * @since 1.1.0 |
|
765 | + * |
|
766 | + * @return bool True if active else false. |
|
767 | + */ |
|
768 | + public function has_local() { |
|
769 | + if ( ! empty( $this->settings['local'] ) && empty( $this->settings['pro'] ) && file_exists( $this->get_fonts_dir() . 'css' . DIRECTORY_SEPARATOR . 'all.css' ) ) { |
|
770 | + return true; |
|
771 | + } |
|
772 | + |
|
773 | + return false; |
|
774 | + } |
|
775 | + |
|
776 | + /** |
|
777 | + * Get the WP Filesystem access. |
|
778 | + * |
|
779 | + * @since 1.1.0 |
|
780 | + * |
|
781 | + * @return object The WP Filesystem. |
|
782 | + */ |
|
783 | + public function get_wp_filesystem() { |
|
784 | + if ( ! function_exists( 'get_filesystem_method' ) ) { |
|
785 | + require_once( ABSPATH . "/wp-admin/includes/file.php" ); |
|
786 | + } |
|
787 | + |
|
788 | + $access_type = get_filesystem_method(); |
|
789 | + |
|
790 | + if ( $access_type === 'direct' ) { |
|
791 | + /* You can safely run request_filesystem_credentials() without any issues and don't need to worry about passing in a URL */ |
|
792 | + $creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() ); |
|
793 | + |
|
794 | + /* Initialize the API */ |
|
795 | + if ( ! WP_Filesystem( $creds ) ) { |
|
796 | + /* Any problems and we exit */ |
|
797 | + return false; |
|
798 | + } |
|
799 | + |
|
800 | + global $wp_filesystem; |
|
801 | + |
|
802 | + return $wp_filesystem; |
|
803 | + /* Do our file manipulations below */ |
|
804 | + } else if ( defined( 'FTP_USER' ) ) { |
|
805 | + $creds = request_filesystem_credentials( trailingslashit( site_url() ) . 'wp-admin/', '', false, false, array() ); |
|
806 | + |
|
807 | + /* Initialize the API */ |
|
808 | + if ( ! WP_Filesystem( $creds ) ) { |
|
809 | + /* Any problems and we exit */ |
|
810 | + return false; |
|
811 | + } |
|
812 | + |
|
813 | + global $wp_filesystem; |
|
814 | + |
|
815 | + return $wp_filesystem; |
|
816 | + } else { |
|
817 | + /* Don't have direct write access. Prompt user with our notice */ |
|
818 | + return false; |
|
819 | + } |
|
820 | + } |
|
821 | + |
|
822 | + /** |
|
823 | + * Download the fontawesome package file. |
|
824 | + * |
|
825 | + * @since 1.1.0 |
|
826 | + * |
|
827 | + * @param mixed $version The font awesome. |
|
828 | + * @param array $option Fontawesome settings. |
|
829 | + * @return WP_ERROR|bool Error on fail and true on success. |
|
830 | + */ |
|
831 | + public function download_package( $version, $option = array() ) { |
|
832 | + $filename = 'fontawesome-free-' . $version . '-web'; |
|
833 | + $url = 'https://use.fontawesome.com/releases/v' . $version . '/' . $filename . '.zip'; |
|
834 | + |
|
835 | + if ( ! function_exists( 'wp_handle_upload' ) ) { |
|
836 | + require_once ABSPATH . 'wp-admin/includes/file.php'; |
|
837 | + } |
|
838 | + |
|
839 | + $download_file = download_url( esc_url_raw( $url ) ); |
|
840 | + |
|
841 | + if ( is_wp_error( $download_file ) ) { |
|
842 | + return new WP_Error( 'fontawesome_download_failed', __( $download_file->get_error_message(), 'font-awesome-settings' ) ); |
|
843 | + } else if ( empty( $download_file ) ) { |
|
844 | + return new WP_Error( 'fontawesome_download_failed', __( 'Something went wrong in downloading the font awesome to store locally.', 'font-awesome-settings' ) ); |
|
845 | + } |
|
846 | + |
|
847 | + $response = $this->extract_package( $download_file, $filename, true ); |
|
848 | + |
|
849 | + // Update local version. |
|
850 | + if ( is_wp_error( $response ) ) { |
|
851 | + return $response; |
|
852 | + } else if ( $response ) { |
|
853 | + if ( empty( $option ) ) { |
|
854 | + $option = get_option( 'wp-font-awesome-settings' ); |
|
855 | + } |
|
856 | + |
|
857 | + $option['local_version'] = $version; |
|
858 | + |
|
859 | + // Remove action to prevent looping. |
|
860 | + remove_action( 'update_option_wp-font-awesome-settings', array( $this, 'update_option_wp_font_awesome_settings' ), 10, 2 ); |
|
861 | + |
|
862 | + update_option( 'wp-font-awesome-settings', $option ); |
|
863 | + |
|
864 | + return true; |
|
865 | + } |
|
866 | + |
|
867 | + return false; |
|
868 | + } |
|
869 | + |
|
870 | + /** |
|
871 | + * Extract the fontawesome package file. |
|
872 | + * |
|
873 | + * @since 1.1.0 |
|
874 | + * |
|
875 | + * @param string $package The package file path. |
|
876 | + * @param string $dirname Package file name. |
|
877 | + * @param bool $delete_package Delete temp file or not. |
|
878 | + * @return WP_Error|bool True on success WP_Error on fail. |
|
879 | + */ |
|
880 | + public function extract_package( $package, $dirname = '', $delete_package = false ) { |
|
881 | + global $wp_filesystem; |
|
882 | + |
|
883 | + $wp_filesystem = $this->get_wp_filesystem(); |
|
884 | + |
|
885 | + if ( empty( $wp_filesystem ) && isset( $wp_filesystem->errors ) && is_wp_error( $wp_filesystem->errors ) && $wp_filesystem->errors->get_error_code() ) { |
|
886 | + return new WP_Error( 'fontawesome_filesystem_error', __( $wp_filesystem->errors->get_error_message(), 'font-awesome-settings' ) ); |
|
887 | + } else if ( empty( $wp_filesystem ) ) { |
|
888 | + return new WP_Error( 'fontawesome_filesystem_error', __( 'Failed to initialise WP_Filesystem while trying to download the Font Awesome package.', 'font-awesome-settings' ) ); |
|
889 | + } |
|
890 | + |
|
891 | + $fonts_dir = $this->get_fonts_dir(); |
|
892 | + $fonts_tmp_dir = dirname( $fonts_dir ) . DIRECTORY_SEPARATOR . 'fa-tmp' . DIRECTORY_SEPARATOR; |
|
893 | + |
|
894 | + if ( $wp_filesystem->is_dir( $fonts_tmp_dir ) ) { |
|
895 | + $wp_filesystem->delete( $fonts_tmp_dir, true ); |
|
896 | + } |
|
897 | + |
|
898 | + // Unzip package to working directory. |
|
899 | + $result = unzip_file( $package, $fonts_tmp_dir ); |
|
900 | + |
|
901 | + if ( is_wp_error( $result ) ) { |
|
902 | + $wp_filesystem->delete( $fonts_tmp_dir, true ); |
|
903 | + |
|
904 | + if ( 'incompatible_archive' === $result->get_error_code() ) { |
|
905 | + return new WP_Error( 'fontawesome_incompatible_archive', __( $result->get_error_message(), 'font-awesome-settings' ) ); |
|
906 | + } |
|
907 | + |
|
908 | + return $result; |
|
909 | + } |
|
910 | + |
|
911 | + if ( $wp_filesystem->is_dir( $fonts_dir ) ) { |
|
912 | + $wp_filesystem->delete( $fonts_dir, true ); |
|
913 | + } |
|
914 | + |
|
915 | + $extract_dir = $fonts_tmp_dir; |
|
916 | + |
|
917 | + if ( $dirname && $wp_filesystem->is_dir( $extract_dir . $dirname . DIRECTORY_SEPARATOR ) ) { |
|
918 | + $extract_dir .= $dirname . DIRECTORY_SEPARATOR; |
|
919 | + } |
|
920 | + |
|
921 | + try { |
|
922 | + $return = $wp_filesystem->move( $extract_dir, $fonts_dir, true ); |
|
923 | + } catch ( Exception $e ) { |
|
924 | + $return = new WP_Error( 'fontawesome_move_package', __( 'Fail to move font awesome package!', 'font-awesome-settings' ) ); |
|
925 | + } |
|
926 | + |
|
927 | + if ( $wp_filesystem->is_dir( $fonts_tmp_dir ) ) { |
|
928 | + $wp_filesystem->delete( $fonts_tmp_dir, true ); |
|
929 | + } |
|
930 | + |
|
931 | + // Once extracted, delete the package if required. |
|
932 | + if ( $delete_package ) { |
|
933 | + unlink( $package ); |
|
934 | + } |
|
935 | + |
|
936 | + return $return; |
|
937 | + } |
|
938 | + } |
|
939 | + |
|
940 | + /** |
|
941 | + * Run the class if found. |
|
942 | + */ |
|
943 | + WP_Font_Awesome_Settings::instance(); |
|
944 | 944 | } |