@@ -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 | } |
@@ -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 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | 3 | if ( ! defined( 'ABSPATH' ) ) { |
4 | - exit; // Exit if accessed directly |
|
4 | + exit; // Exit if accessed directly |
|
5 | 5 | } |
6 | 6 | |
7 | 7 | /** |
@@ -11,1283 +11,1283 @@ discard block |
||
11 | 11 | */ |
12 | 12 | class AUI_Component_Input { |
13 | 13 | |
14 | - /** |
|
15 | - * Build the component. |
|
16 | - * |
|
17 | - * @param array $args |
|
18 | - * |
|
19 | - * @return string The rendered component. |
|
20 | - */ |
|
21 | - public static function input( $args = array() ) { |
|
22 | - global $aui_bs5; |
|
23 | - |
|
24 | - $defaults = array( |
|
25 | - 'type' => 'text', |
|
26 | - 'name' => '', |
|
27 | - 'class' => '', |
|
28 | - 'wrap_class' => '', |
|
29 | - 'id' => '', |
|
30 | - 'placeholder' => '', |
|
31 | - 'title' => '', |
|
32 | - 'value' => '', |
|
33 | - 'required' => false, |
|
34 | - 'size' => '', // sm, lg, small, large |
|
35 | - 'clear_icon' => '', // true will show a clear icon, can't be used with input_group_right |
|
36 | - 'with_hidden' => false, // Append hidden field for single checkbox. |
|
37 | - 'label' => '', |
|
38 | - 'label_after' => false, |
|
39 | - 'label_class' => '', |
|
40 | - 'label_col' => '2', |
|
41 | - 'label_type' => '', // top, horizontal, empty = hidden |
|
42 | - 'label_force_left' => false, // used to force checkbox label left when using horizontal |
|
43 | - // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
44 | - 'help_text' => '', |
|
45 | - 'validation_text' => '', |
|
46 | - 'validation_pattern' => '', |
|
47 | - 'no_wrap' => false, |
|
48 | - 'input_group_right' => '', |
|
49 | - 'input_group_left' => '', |
|
50 | - 'input_group_right_inside' => false, |
|
51 | - // forces the input group inside the input |
|
52 | - 'input_group_left_inside' => false, |
|
53 | - // forces the input group inside the input |
|
54 | - 'form_group_class' => '', |
|
55 | - 'step' => '', |
|
56 | - 'switch' => false, |
|
57 | - // to show checkbox as a switch |
|
58 | - 'checked' => false, |
|
59 | - // set a checkbox or radio as selected |
|
60 | - 'password_toggle' => true, |
|
61 | - // toggle view/hide password |
|
62 | - 'element_require' => '', |
|
63 | - // [%element_id%] == "1" |
|
64 | - 'extra_attributes' => array(), |
|
65 | - // an array of extra attributes |
|
66 | - 'wrap_attributes' => array() |
|
67 | - ); |
|
68 | - |
|
69 | - /** |
|
70 | - * Parse incoming $args into an array and merge it with $defaults |
|
71 | - */ |
|
72 | - $args = wp_parse_args( $args, $defaults ); |
|
73 | - $output = ''; |
|
74 | - if ( ! empty( $args['type'] ) ) { |
|
75 | - // hidden label option needs to be empty |
|
76 | - $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
77 | - |
|
78 | - $type = sanitize_html_class( $args['type'] ); |
|
79 | - |
|
80 | - $help_text = ''; |
|
81 | - $label = ''; |
|
82 | - $label_after = $args['label_after']; |
|
83 | - $label_args = array( |
|
84 | - 'title' => $args['label'], |
|
85 | - 'for' => $args['id'], |
|
86 | - 'class' => $args['label_class'] . " ", |
|
87 | - 'label_type' => $args['label_type'], |
|
88 | - 'label_col' => $args['label_col'] |
|
89 | - ); |
|
90 | - |
|
91 | - // floating labels need label after |
|
92 | - if ( $args['label_type'] == 'floating' && $type != 'checkbox' ) { |
|
93 | - $label_after = true; |
|
94 | - $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
95 | - } |
|
96 | - |
|
97 | - // size |
|
98 | - $size = ''; |
|
99 | - if ( $args['size'] == 'lg' || $args['size'] == 'large' ) { |
|
100 | - $size = 'lg'; |
|
101 | - $args['class'] .= ' form-control-lg'; |
|
102 | - }elseif ( $args['size'] == 'sm' || $args['size'] == 'small' ) { |
|
103 | - $size = 'sm'; |
|
104 | - $args['class'] .= ' form-control-sm'; |
|
105 | - } |
|
106 | - |
|
107 | - // clear function |
|
108 | - $clear_function = 'jQuery(this).parent().parent().find(\'input\').val(\'\');'; |
|
109 | - |
|
110 | - // Some special sauce for files |
|
111 | - if ( $type == 'file' ) { |
|
112 | - $label_after = true; // if type file we need the label after |
|
113 | - $args['class'] .= ' custom-file-input '; |
|
114 | - } elseif ( $type == 'checkbox' ) { |
|
115 | - $label_after = true; // if type file we need the label after |
|
116 | - $args['class'] .= $aui_bs5 ? ' form-check-input' : ' custom-control-input '; |
|
117 | - } elseif ( $type == 'datepicker' || $type == 'timepicker' ) { |
|
118 | - $orig_type = $type; |
|
119 | - $type = 'text'; |
|
120 | - $args['class'] .= ' bg-initial '; // @todo not sure why we have this? |
|
121 | - $clear_function .= "jQuery(this).parent().parent().find('input[name=\'" . esc_attr( $args['name'] ) . "\']').trigger('change');"; |
|
122 | - |
|
123 | - $args['extra_attributes']['data-aui-init'] = 'flatpickr'; |
|
124 | - |
|
125 | - // Disable native datetime inputs. |
|
126 | - if ( ( $orig_type == 'timepicker' || ! empty( $args['extra_attributes']['data-enable-time'] ) ) && ! isset( $args['extra_attributes']['data-disable-mobile'] ) ) { |
|
127 | - $args['extra_attributes']['data-disable-mobile'] = 'true'; |
|
128 | - } |
|
129 | - |
|
130 | - // set a way to clear field if empty |
|
131 | - if ( $args['input_group_right'] === '' && $args['clear_icon'] !== false ) { |
|
132 | - $args['input_group_right_inside'] = true; |
|
133 | - $args['clear_icon'] = true; |
|
134 | - } |
|
135 | - |
|
136 | - // enqueue the script |
|
137 | - $aui_settings = AyeCode_UI_Settings::instance(); |
|
138 | - $aui_settings->enqueue_flatpickr(); |
|
139 | - } elseif ( $type == 'iconpicker' ) { |
|
140 | - $type = 'text'; |
|
141 | - //$args['class'] .= ' aui-flatpickr bg-initial '; |
|
14 | + /** |
|
15 | + * Build the component. |
|
16 | + * |
|
17 | + * @param array $args |
|
18 | + * |
|
19 | + * @return string The rendered component. |
|
20 | + */ |
|
21 | + public static function input( $args = array() ) { |
|
22 | + global $aui_bs5; |
|
23 | + |
|
24 | + $defaults = array( |
|
25 | + 'type' => 'text', |
|
26 | + 'name' => '', |
|
27 | + 'class' => '', |
|
28 | + 'wrap_class' => '', |
|
29 | + 'id' => '', |
|
30 | + 'placeholder' => '', |
|
31 | + 'title' => '', |
|
32 | + 'value' => '', |
|
33 | + 'required' => false, |
|
34 | + 'size' => '', // sm, lg, small, large |
|
35 | + 'clear_icon' => '', // true will show a clear icon, can't be used with input_group_right |
|
36 | + 'with_hidden' => false, // Append hidden field for single checkbox. |
|
37 | + 'label' => '', |
|
38 | + 'label_after' => false, |
|
39 | + 'label_class' => '', |
|
40 | + 'label_col' => '2', |
|
41 | + 'label_type' => '', // top, horizontal, empty = hidden |
|
42 | + 'label_force_left' => false, // used to force checkbox label left when using horizontal |
|
43 | + // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
44 | + 'help_text' => '', |
|
45 | + 'validation_text' => '', |
|
46 | + 'validation_pattern' => '', |
|
47 | + 'no_wrap' => false, |
|
48 | + 'input_group_right' => '', |
|
49 | + 'input_group_left' => '', |
|
50 | + 'input_group_right_inside' => false, |
|
51 | + // forces the input group inside the input |
|
52 | + 'input_group_left_inside' => false, |
|
53 | + // forces the input group inside the input |
|
54 | + 'form_group_class' => '', |
|
55 | + 'step' => '', |
|
56 | + 'switch' => false, |
|
57 | + // to show checkbox as a switch |
|
58 | + 'checked' => false, |
|
59 | + // set a checkbox or radio as selected |
|
60 | + 'password_toggle' => true, |
|
61 | + // toggle view/hide password |
|
62 | + 'element_require' => '', |
|
63 | + // [%element_id%] == "1" |
|
64 | + 'extra_attributes' => array(), |
|
65 | + // an array of extra attributes |
|
66 | + 'wrap_attributes' => array() |
|
67 | + ); |
|
68 | + |
|
69 | + /** |
|
70 | + * Parse incoming $args into an array and merge it with $defaults |
|
71 | + */ |
|
72 | + $args = wp_parse_args( $args, $defaults ); |
|
73 | + $output = ''; |
|
74 | + if ( ! empty( $args['type'] ) ) { |
|
75 | + // hidden label option needs to be empty |
|
76 | + $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
77 | + |
|
78 | + $type = sanitize_html_class( $args['type'] ); |
|
79 | + |
|
80 | + $help_text = ''; |
|
81 | + $label = ''; |
|
82 | + $label_after = $args['label_after']; |
|
83 | + $label_args = array( |
|
84 | + 'title' => $args['label'], |
|
85 | + 'for' => $args['id'], |
|
86 | + 'class' => $args['label_class'] . " ", |
|
87 | + 'label_type' => $args['label_type'], |
|
88 | + 'label_col' => $args['label_col'] |
|
89 | + ); |
|
90 | + |
|
91 | + // floating labels need label after |
|
92 | + if ( $args['label_type'] == 'floating' && $type != 'checkbox' ) { |
|
93 | + $label_after = true; |
|
94 | + $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
95 | + } |
|
96 | + |
|
97 | + // size |
|
98 | + $size = ''; |
|
99 | + if ( $args['size'] == 'lg' || $args['size'] == 'large' ) { |
|
100 | + $size = 'lg'; |
|
101 | + $args['class'] .= ' form-control-lg'; |
|
102 | + }elseif ( $args['size'] == 'sm' || $args['size'] == 'small' ) { |
|
103 | + $size = 'sm'; |
|
104 | + $args['class'] .= ' form-control-sm'; |
|
105 | + } |
|
106 | + |
|
107 | + // clear function |
|
108 | + $clear_function = 'jQuery(this).parent().parent().find(\'input\').val(\'\');'; |
|
109 | + |
|
110 | + // Some special sauce for files |
|
111 | + if ( $type == 'file' ) { |
|
112 | + $label_after = true; // if type file we need the label after |
|
113 | + $args['class'] .= ' custom-file-input '; |
|
114 | + } elseif ( $type == 'checkbox' ) { |
|
115 | + $label_after = true; // if type file we need the label after |
|
116 | + $args['class'] .= $aui_bs5 ? ' form-check-input' : ' custom-control-input '; |
|
117 | + } elseif ( $type == 'datepicker' || $type == 'timepicker' ) { |
|
118 | + $orig_type = $type; |
|
119 | + $type = 'text'; |
|
120 | + $args['class'] .= ' bg-initial '; // @todo not sure why we have this? |
|
121 | + $clear_function .= "jQuery(this).parent().parent().find('input[name=\'" . esc_attr( $args['name'] ) . "\']').trigger('change');"; |
|
122 | + |
|
123 | + $args['extra_attributes']['data-aui-init'] = 'flatpickr'; |
|
124 | + |
|
125 | + // Disable native datetime inputs. |
|
126 | + if ( ( $orig_type == 'timepicker' || ! empty( $args['extra_attributes']['data-enable-time'] ) ) && ! isset( $args['extra_attributes']['data-disable-mobile'] ) ) { |
|
127 | + $args['extra_attributes']['data-disable-mobile'] = 'true'; |
|
128 | + } |
|
129 | + |
|
130 | + // set a way to clear field if empty |
|
131 | + if ( $args['input_group_right'] === '' && $args['clear_icon'] !== false ) { |
|
132 | + $args['input_group_right_inside'] = true; |
|
133 | + $args['clear_icon'] = true; |
|
134 | + } |
|
135 | + |
|
136 | + // enqueue the script |
|
137 | + $aui_settings = AyeCode_UI_Settings::instance(); |
|
138 | + $aui_settings->enqueue_flatpickr(); |
|
139 | + } elseif ( $type == 'iconpicker' ) { |
|
140 | + $type = 'text'; |
|
141 | + //$args['class'] .= ' aui-flatpickr bg-initial '; |
|
142 | 142 | // $args['class'] .= ' bg-initial '; |
143 | 143 | |
144 | - $args['extra_attributes']['data-aui-init'] = 'iconpicker'; |
|
145 | - $args['extra_attributes']['data-placement'] = 'bottomRight'; |
|
144 | + $args['extra_attributes']['data-aui-init'] = 'iconpicker'; |
|
145 | + $args['extra_attributes']['data-placement'] = 'bottomRight'; |
|
146 | 146 | |
147 | - $args['input_group_right'] = '<span class="input-group-addon input-group-text c-pointer"></span>'; |
|
147 | + $args['input_group_right'] = '<span class="input-group-addon input-group-text c-pointer"></span>'; |
|
148 | 148 | // $args['input_group_right_inside'] = true; |
149 | - // enqueue the script |
|
150 | - $aui_settings = AyeCode_UI_Settings::instance(); |
|
151 | - $aui_settings->enqueue_iconpicker(); |
|
152 | - } |
|
153 | - |
|
154 | - if ( $type == 'checkbox' && ( ( ! empty( $args['name'] ) && strpos( $args['name'], '[' ) === false ) || ! empty( $args['with_hidden'] ) ) ) { |
|
155 | - $output .= '<input type="hidden" name="' . esc_attr( $args['name'] ) . '" value="0" />'; |
|
156 | - } |
|
157 | - |
|
158 | - // allow clear icon |
|
159 | - if ( $args['input_group_right'] === '' && $args['clear_icon'] ) { |
|
160 | - $font_size = $size == 'sm' ? '1.3' : ( $size == 'lg' ? '1.65' : '1.5' ); |
|
161 | - $args['input_group_right_inside'] = true; |
|
162 | - $align_class = $aui_bs5 ? ' h-100 py-0' : ''; |
|
163 | - $args['input_group_right'] = '<span class="input-group-text aui-clear-input c-pointer bg-initial border-0 px-2 d-none ' . $align_class . '" onclick="' . $clear_function . '"><span style="font-size: ' . $font_size . 'rem" aria-hidden="true" class="' . ( $aui_bs5 ? 'btn-close' : 'close' ) . '">' . ( $aui_bs5 ? '' : '×' ) . '</span></span>'; |
|
164 | - } |
|
165 | - |
|
166 | - // open/type |
|
167 | - $output .= '<input type="' . $type . '" '; |
|
168 | - |
|
169 | - // name |
|
170 | - if ( ! empty( $args['name'] ) ) { |
|
171 | - $output .= ' name="' . esc_attr( $args['name'] ) . '" '; |
|
172 | - } |
|
173 | - |
|
174 | - // id |
|
175 | - if ( ! empty( $args['id'] ) ) { |
|
176 | - $output .= ' id="' . sanitize_html_class( $args['id'] ) . '" '; |
|
177 | - } |
|
178 | - |
|
179 | - // placeholder |
|
180 | - if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] ) { |
|
181 | - $output .= ' placeholder="' . esc_attr( $args['placeholder'] ) . '" '; |
|
182 | - } |
|
183 | - |
|
184 | - // title |
|
185 | - if ( ! empty( $args['title'] ) ) { |
|
186 | - $output .= ' title="' . esc_attr( $args['title'] ) . '" '; |
|
187 | - } |
|
188 | - |
|
189 | - // value |
|
190 | - if ( ! empty( $args['value'] ) ) { |
|
191 | - $output .= AUI_Component_Helper::value( $args['value'] ); |
|
192 | - } |
|
193 | - |
|
194 | - // checked, for radio and checkboxes |
|
195 | - if ( ( $type == 'checkbox' || $type == 'radio' ) && $args['checked'] ) { |
|
196 | - $output .= ' checked '; |
|
197 | - } |
|
198 | - |
|
199 | - // validation text |
|
200 | - if ( ! empty( $args['validation_text'] ) ) { |
|
201 | - $output .= ' oninvalid="setCustomValidity(\'' . esc_attr( $args['validation_text'] ) . '\')" '; |
|
202 | - $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
203 | - } |
|
204 | - |
|
205 | - // validation_pattern |
|
206 | - if ( ! empty( $args['validation_pattern'] ) ) { |
|
207 | - $output .= ' pattern="' . esc_attr( $args['validation_pattern'] ) . '" '; |
|
208 | - } |
|
209 | - |
|
210 | - // step (for numbers) |
|
211 | - if ( ! empty( $args['step'] ) ) { |
|
212 | - $output .= ' step="' . $args['step'] . '" '; |
|
213 | - } |
|
214 | - |
|
215 | - // required |
|
216 | - if ( ! empty( $args['required'] ) ) { |
|
217 | - $output .= ' required '; |
|
218 | - } |
|
219 | - |
|
220 | - // class |
|
221 | - $class = ! empty( $args['class'] ) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
222 | - $output .= $aui_bs5 && $type == 'checkbox' ? ' class="' . $class . '" ' : ' class="form-control ' . $class . '" '; |
|
223 | - |
|
224 | - // data-attributes |
|
225 | - $output .= AUI_Component_Helper::data_attributes( $args ); |
|
226 | - |
|
227 | - // extra attributes |
|
228 | - if ( ! empty( $args['extra_attributes'] ) ) { |
|
229 | - $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
230 | - } |
|
231 | - |
|
232 | - // close |
|
233 | - $output .= ' >'; |
|
234 | - |
|
235 | - // help text |
|
236 | - if ( ! empty( $args['help_text'] ) ) { |
|
237 | - $help_text = AUI_Component_Helper::help_text( $args['help_text'] ); |
|
238 | - } |
|
239 | - |
|
240 | - // label |
|
241 | - if ( ! empty( $args['label'] ) ) { |
|
242 | - $label_base_class = ''; |
|
243 | - if ( $type == 'file' ) { |
|
244 | - $label_base_class = ' custom-file-label'; |
|
245 | - } elseif ( $type == 'checkbox' ) { |
|
246 | - if ( ! empty( $args['label_force_left'] ) ) { |
|
247 | - $label_args['title'] = wp_kses_post( $args['help_text'] ); |
|
248 | - $help_text = ''; |
|
249 | - //$label_args['class'] .= ' d-inline '; |
|
250 | - $args['wrap_class'] .= ' align-items-center '; |
|
251 | - }else{ |
|
252 | - |
|
253 | - } |
|
254 | - |
|
255 | - $label_base_class = $aui_bs5 ? ' form-check-label' : ' custom-control-label'; |
|
256 | - } |
|
257 | - $label_args['class'] .= $label_base_class; |
|
258 | - $temp_label_args = $label_args; |
|
259 | - if(! empty( $args['label_force_left'] )){$temp_label_args['class'] = $label_base_class." text-muted";} |
|
260 | - $label = self::label( $temp_label_args, $type ); |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - |
|
265 | - |
|
266 | - // set help text in the correct position |
|
267 | - if ( $label_after ) { |
|
268 | - $output .= $label . $help_text; |
|
269 | - } |
|
270 | - |
|
271 | - // some input types need a separate wrap |
|
272 | - if ( $type == 'file' ) { |
|
273 | - $output = self::wrap( array( |
|
274 | - 'content' => $output, |
|
275 | - 'class' => $aui_bs5 ? 'mb-3 custom-file' : 'form-group custom-file' |
|
276 | - ) ); |
|
277 | - } elseif ( $type == 'checkbox' ) { |
|
278 | - |
|
279 | - $label_args['title'] = $args['label']; |
|
280 | - $label_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'label' ); |
|
281 | - $label = !empty( $args['label_force_left'] ) ? self::label( $label_args, 'cb' ) : '<div class="' . $label_col . ' col-form-label"></div>'; |
|
282 | - $switch_size_class = $args['switch'] && !is_bool( $args['switch'] ) ? ' custom-switch-'.esc_attr( $args['switch'] ) : ''; |
|
283 | - if ( $aui_bs5 ) { |
|
284 | - $wrap_class = $args['switch'] ? 'form-check form-switch' . $switch_size_class : 'form-check'; |
|
285 | - }else{ |
|
286 | - $wrap_class = $args['switch'] ? 'custom-switch' . $switch_size_class : 'custom-checkbox' ; |
|
287 | - } |
|
288 | - if ( ! empty( $args['label_force_left'] ) ) { |
|
289 | - $wrap_class .= $aui_bs5 ? '' : ' d-flex align-content-center'; |
|
290 | - $label = str_replace(array("form-check-label","custom-control-label"),"", self::label( $label_args, 'cb' ) ); |
|
291 | - } |
|
292 | - $output = self::wrap( array( |
|
293 | - 'content' => $output, |
|
294 | - 'class' => $aui_bs5 ? $wrap_class : 'custom-control ' . $wrap_class |
|
295 | - ) ); |
|
296 | - |
|
297 | - if ( $args['label_type'] == 'horizontal' ) { |
|
298 | - $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
299 | - $output = $label . '<div class="' . $input_col . '">' . $output . '</div>'; |
|
300 | - } |
|
301 | - } elseif ( $type == 'password' && $args['password_toggle'] && ! $args['input_group_right'] ) { |
|
302 | - |
|
303 | - |
|
304 | - // allow password field to toggle view |
|
305 | - $args['input_group_right'] = '<span class="input-group-text c-pointer px-3" |
|
149 | + // enqueue the script |
|
150 | + $aui_settings = AyeCode_UI_Settings::instance(); |
|
151 | + $aui_settings->enqueue_iconpicker(); |
|
152 | + } |
|
153 | + |
|
154 | + if ( $type == 'checkbox' && ( ( ! empty( $args['name'] ) && strpos( $args['name'], '[' ) === false ) || ! empty( $args['with_hidden'] ) ) ) { |
|
155 | + $output .= '<input type="hidden" name="' . esc_attr( $args['name'] ) . '" value="0" />'; |
|
156 | + } |
|
157 | + |
|
158 | + // allow clear icon |
|
159 | + if ( $args['input_group_right'] === '' && $args['clear_icon'] ) { |
|
160 | + $font_size = $size == 'sm' ? '1.3' : ( $size == 'lg' ? '1.65' : '1.5' ); |
|
161 | + $args['input_group_right_inside'] = true; |
|
162 | + $align_class = $aui_bs5 ? ' h-100 py-0' : ''; |
|
163 | + $args['input_group_right'] = '<span class="input-group-text aui-clear-input c-pointer bg-initial border-0 px-2 d-none ' . $align_class . '" onclick="' . $clear_function . '"><span style="font-size: ' . $font_size . 'rem" aria-hidden="true" class="' . ( $aui_bs5 ? 'btn-close' : 'close' ) . '">' . ( $aui_bs5 ? '' : '×' ) . '</span></span>'; |
|
164 | + } |
|
165 | + |
|
166 | + // open/type |
|
167 | + $output .= '<input type="' . $type . '" '; |
|
168 | + |
|
169 | + // name |
|
170 | + if ( ! empty( $args['name'] ) ) { |
|
171 | + $output .= ' name="' . esc_attr( $args['name'] ) . '" '; |
|
172 | + } |
|
173 | + |
|
174 | + // id |
|
175 | + if ( ! empty( $args['id'] ) ) { |
|
176 | + $output .= ' id="' . sanitize_html_class( $args['id'] ) . '" '; |
|
177 | + } |
|
178 | + |
|
179 | + // placeholder |
|
180 | + if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] ) { |
|
181 | + $output .= ' placeholder="' . esc_attr( $args['placeholder'] ) . '" '; |
|
182 | + } |
|
183 | + |
|
184 | + // title |
|
185 | + if ( ! empty( $args['title'] ) ) { |
|
186 | + $output .= ' title="' . esc_attr( $args['title'] ) . '" '; |
|
187 | + } |
|
188 | + |
|
189 | + // value |
|
190 | + if ( ! empty( $args['value'] ) ) { |
|
191 | + $output .= AUI_Component_Helper::value( $args['value'] ); |
|
192 | + } |
|
193 | + |
|
194 | + // checked, for radio and checkboxes |
|
195 | + if ( ( $type == 'checkbox' || $type == 'radio' ) && $args['checked'] ) { |
|
196 | + $output .= ' checked '; |
|
197 | + } |
|
198 | + |
|
199 | + // validation text |
|
200 | + if ( ! empty( $args['validation_text'] ) ) { |
|
201 | + $output .= ' oninvalid="setCustomValidity(\'' . esc_attr( $args['validation_text'] ) . '\')" '; |
|
202 | + $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
203 | + } |
|
204 | + |
|
205 | + // validation_pattern |
|
206 | + if ( ! empty( $args['validation_pattern'] ) ) { |
|
207 | + $output .= ' pattern="' . esc_attr( $args['validation_pattern'] ) . '" '; |
|
208 | + } |
|
209 | + |
|
210 | + // step (for numbers) |
|
211 | + if ( ! empty( $args['step'] ) ) { |
|
212 | + $output .= ' step="' . $args['step'] . '" '; |
|
213 | + } |
|
214 | + |
|
215 | + // required |
|
216 | + if ( ! empty( $args['required'] ) ) { |
|
217 | + $output .= ' required '; |
|
218 | + } |
|
219 | + |
|
220 | + // class |
|
221 | + $class = ! empty( $args['class'] ) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
222 | + $output .= $aui_bs5 && $type == 'checkbox' ? ' class="' . $class . '" ' : ' class="form-control ' . $class . '" '; |
|
223 | + |
|
224 | + // data-attributes |
|
225 | + $output .= AUI_Component_Helper::data_attributes( $args ); |
|
226 | + |
|
227 | + // extra attributes |
|
228 | + if ( ! empty( $args['extra_attributes'] ) ) { |
|
229 | + $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
230 | + } |
|
231 | + |
|
232 | + // close |
|
233 | + $output .= ' >'; |
|
234 | + |
|
235 | + // help text |
|
236 | + if ( ! empty( $args['help_text'] ) ) { |
|
237 | + $help_text = AUI_Component_Helper::help_text( $args['help_text'] ); |
|
238 | + } |
|
239 | + |
|
240 | + // label |
|
241 | + if ( ! empty( $args['label'] ) ) { |
|
242 | + $label_base_class = ''; |
|
243 | + if ( $type == 'file' ) { |
|
244 | + $label_base_class = ' custom-file-label'; |
|
245 | + } elseif ( $type == 'checkbox' ) { |
|
246 | + if ( ! empty( $args['label_force_left'] ) ) { |
|
247 | + $label_args['title'] = wp_kses_post( $args['help_text'] ); |
|
248 | + $help_text = ''; |
|
249 | + //$label_args['class'] .= ' d-inline '; |
|
250 | + $args['wrap_class'] .= ' align-items-center '; |
|
251 | + }else{ |
|
252 | + |
|
253 | + } |
|
254 | + |
|
255 | + $label_base_class = $aui_bs5 ? ' form-check-label' : ' custom-control-label'; |
|
256 | + } |
|
257 | + $label_args['class'] .= $label_base_class; |
|
258 | + $temp_label_args = $label_args; |
|
259 | + if(! empty( $args['label_force_left'] )){$temp_label_args['class'] = $label_base_class." text-muted";} |
|
260 | + $label = self::label( $temp_label_args, $type ); |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + |
|
265 | + |
|
266 | + // set help text in the correct position |
|
267 | + if ( $label_after ) { |
|
268 | + $output .= $label . $help_text; |
|
269 | + } |
|
270 | + |
|
271 | + // some input types need a separate wrap |
|
272 | + if ( $type == 'file' ) { |
|
273 | + $output = self::wrap( array( |
|
274 | + 'content' => $output, |
|
275 | + 'class' => $aui_bs5 ? 'mb-3 custom-file' : 'form-group custom-file' |
|
276 | + ) ); |
|
277 | + } elseif ( $type == 'checkbox' ) { |
|
278 | + |
|
279 | + $label_args['title'] = $args['label']; |
|
280 | + $label_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'label' ); |
|
281 | + $label = !empty( $args['label_force_left'] ) ? self::label( $label_args, 'cb' ) : '<div class="' . $label_col . ' col-form-label"></div>'; |
|
282 | + $switch_size_class = $args['switch'] && !is_bool( $args['switch'] ) ? ' custom-switch-'.esc_attr( $args['switch'] ) : ''; |
|
283 | + if ( $aui_bs5 ) { |
|
284 | + $wrap_class = $args['switch'] ? 'form-check form-switch' . $switch_size_class : 'form-check'; |
|
285 | + }else{ |
|
286 | + $wrap_class = $args['switch'] ? 'custom-switch' . $switch_size_class : 'custom-checkbox' ; |
|
287 | + } |
|
288 | + if ( ! empty( $args['label_force_left'] ) ) { |
|
289 | + $wrap_class .= $aui_bs5 ? '' : ' d-flex align-content-center'; |
|
290 | + $label = str_replace(array("form-check-label","custom-control-label"),"", self::label( $label_args, 'cb' ) ); |
|
291 | + } |
|
292 | + $output = self::wrap( array( |
|
293 | + 'content' => $output, |
|
294 | + 'class' => $aui_bs5 ? $wrap_class : 'custom-control ' . $wrap_class |
|
295 | + ) ); |
|
296 | + |
|
297 | + if ( $args['label_type'] == 'horizontal' ) { |
|
298 | + $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
299 | + $output = $label . '<div class="' . $input_col . '">' . $output . '</div>'; |
|
300 | + } |
|
301 | + } elseif ( $type == 'password' && $args['password_toggle'] && ! $args['input_group_right'] ) { |
|
302 | + |
|
303 | + |
|
304 | + // allow password field to toggle view |
|
305 | + $args['input_group_right'] = '<span class="input-group-text c-pointer px-3" |
|
306 | 306 | onclick="var $el = jQuery(this).find(\'i\');$el.toggleClass(\'fa-eye fa-eye-slash\'); |
307 | 307 | var $eli = jQuery(this).parent().parent().find(\'input\'); |
308 | 308 | if($el.hasClass(\'fa-eye\')) |
309 | 309 | {$eli.attr(\'type\',\'text\');} |
310 | 310 | else{$eli.attr(\'type\',\'password\');}" |
311 | 311 | ><i class="far fa-fw fa-eye-slash"></i></span>'; |
312 | - } |
|
313 | - |
|
314 | - // input group wraps |
|
315 | - if ( $args['input_group_left'] || $args['input_group_right'] ) { |
|
316 | - $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; |
|
317 | - $group_size = $size == 'lg' ? ' input-group-lg' : ''; |
|
318 | - $group_size = !$group_size && $size == 'sm' ? ' input-group-sm' : $group_size; |
|
319 | - |
|
320 | - if ( $args['input_group_left'] ) { |
|
321 | - $output = self::wrap( array( |
|
322 | - 'content' => $output, |
|
323 | - 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 . $group_size : 'input-group' . $group_size, |
|
324 | - 'input_group_left' => $args['input_group_left'], |
|
325 | - 'input_group_left_inside' => $args['input_group_left_inside'] |
|
326 | - ) ); |
|
327 | - } |
|
328 | - if ( $args['input_group_right'] ) { |
|
329 | - $output = self::wrap( array( |
|
330 | - 'content' => $output, |
|
331 | - 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 . $group_size : 'input-group' . $group_size, |
|
332 | - 'input_group_right' => $args['input_group_right'], |
|
333 | - 'input_group_right_inside' => $args['input_group_right_inside'] |
|
334 | - ) ); |
|
335 | - } |
|
336 | - |
|
337 | - } |
|
338 | - |
|
339 | - if ( ! $label_after ) { |
|
340 | - $output .= $help_text; |
|
341 | - } |
|
342 | - |
|
343 | - |
|
344 | - if ( $args['label_type'] == 'horizontal' && $type != 'checkbox' ) { |
|
345 | - $output = self::wrap( array( |
|
346 | - 'content' => $output, |
|
347 | - 'class' => AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ) |
|
348 | - ) ); |
|
349 | - } |
|
350 | - |
|
351 | - if ( ! $label_after ) { |
|
352 | - $output = $label . $output; |
|
353 | - } |
|
354 | - |
|
355 | - // wrap |
|
356 | - if ( ! $args['no_wrap'] ) { |
|
357 | - if ( ! empty( $args['form_group_class'] ) ) { |
|
358 | - $fg_class = esc_attr( $args['form_group_class'] ); |
|
359 | - }else{ |
|
360 | - $fg_class = $aui_bs5 ? 'mb-3' : 'form-group'; |
|
361 | - } |
|
362 | - $form_group_class = $args['label_type'] == 'floating' && $type != 'checkbox' ? 'form-label-group' : $fg_class; |
|
363 | - $wrap_class = $args['label_type'] == 'horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
364 | - $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
365 | - $output = self::wrap( array( |
|
366 | - 'content' => $output, |
|
367 | - 'class' => $wrap_class, |
|
368 | - 'element_require' => $args['element_require'], |
|
369 | - 'argument_id' => $args['id'], |
|
370 | - 'wrap_attributes' => $args['wrap_attributes'], |
|
371 | - ) ); |
|
372 | - } |
|
373 | - } |
|
374 | - |
|
375 | - return $output; |
|
376 | - } |
|
377 | - |
|
378 | - public static function label( $args = array(), $type = '' ) { |
|
379 | - global $aui_bs5; |
|
380 | - //<label for="exampleInputEmail1">Email address</label> |
|
381 | - $defaults = array( |
|
382 | - 'title' => 'div', |
|
383 | - 'for' => '', |
|
384 | - 'class' => '', |
|
385 | - 'label_type' => '', // empty = hidden, top, horizontal |
|
386 | - 'label_col' => '', |
|
387 | - ); |
|
388 | - |
|
389 | - /** |
|
390 | - * Parse incoming $args into an array and merge it with $defaults |
|
391 | - */ |
|
392 | - $args = wp_parse_args( $args, $defaults ); |
|
393 | - $output = ''; |
|
394 | - |
|
395 | - if ( $args['title'] ) { |
|
396 | - |
|
397 | - // maybe hide labels //@todo set a global option for visibility class |
|
398 | - if ( $type == 'file' || $type == 'checkbox' || $type == 'radio' || ! empty( $args['label_type'] ) ) { |
|
399 | - $class = $args['class']; |
|
400 | - } else { |
|
401 | - $class = 'sr-only ' . $args['class']; |
|
402 | - } |
|
403 | - |
|
404 | - // maybe horizontal |
|
405 | - if ( $args['label_type'] == 'horizontal' && $type != 'checkbox' ) { |
|
406 | - $class .= ' ' . AUI_Component_Helper::get_column_class( $args['label_col'], 'label' ) . ' col-form-label '.$type; |
|
407 | - } |
|
408 | - |
|
409 | - if( $aui_bs5 ){ $class .= ' form-label'; } |
|
410 | - |
|
411 | - // open |
|
412 | - $output .= '<label '; |
|
413 | - |
|
414 | - // for |
|
415 | - if ( ! empty( $args['for'] ) ) { |
|
416 | - $output .= ' for="' . esc_attr( $args['for'] ) . '" '; |
|
417 | - } |
|
418 | - |
|
419 | - // class |
|
420 | - $class = $class ? AUI_Component_Helper::esc_classes( $class ) : ''; |
|
421 | - $output .= ' class="' . $class . '" '; |
|
422 | - |
|
423 | - // close |
|
424 | - $output .= '>'; |
|
425 | - |
|
426 | - |
|
427 | - // title, don't escape fully as can contain html |
|
428 | - if ( ! empty( $args['title'] ) ) { |
|
429 | - $output .= wp_kses_post( $args['title'] ); |
|
430 | - } |
|
431 | - |
|
432 | - // close wrap |
|
433 | - $output .= '</label>'; |
|
434 | - |
|
435 | - |
|
436 | - } |
|
437 | - |
|
438 | - |
|
439 | - return $output; |
|
440 | - } |
|
441 | - |
|
442 | - /** |
|
443 | - * Wrap some content in a HTML wrapper. |
|
444 | - * |
|
445 | - * @param array $args |
|
446 | - * |
|
447 | - * @return string |
|
448 | - */ |
|
449 | - public static function wrap( $args = array() ) { |
|
450 | - global $aui_bs5; |
|
451 | - $defaults = array( |
|
452 | - 'type' => 'div', |
|
453 | - 'class' => $aui_bs5 ? 'mb-3' : 'form-group', |
|
454 | - 'content' => '', |
|
455 | - 'input_group_left' => '', |
|
456 | - 'input_group_right' => '', |
|
457 | - 'input_group_left_inside' => false, |
|
458 | - 'input_group_right_inside' => false, |
|
459 | - 'element_require' => '', |
|
460 | - 'argument_id' => '', |
|
461 | - 'wrap_attributes' => array() |
|
462 | - ); |
|
463 | - |
|
464 | - /** |
|
465 | - * Parse incoming $args into an array and merge it with $defaults |
|
466 | - */ |
|
467 | - $args = wp_parse_args( $args, $defaults ); |
|
468 | - $output = ''; |
|
469 | - if ( $args['type'] ) { |
|
470 | - |
|
471 | - // open |
|
472 | - $output .= '<' . sanitize_html_class( $args['type'] ); |
|
473 | - |
|
474 | - // element require |
|
475 | - if ( ! empty( $args['element_require'] ) ) { |
|
476 | - $output .= AUI_Component_Helper::element_require( $args['element_require'] ); |
|
477 | - $args['class'] .= " aui-conditional-field"; |
|
478 | - } |
|
479 | - |
|
480 | - // argument_id |
|
481 | - if ( ! empty( $args['argument_id'] ) ) { |
|
482 | - $output .= ' data-argument="' . esc_attr( $args['argument_id'] ) . '"'; |
|
483 | - } |
|
484 | - |
|
485 | - // class |
|
486 | - $class = ! empty( $args['class'] ) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
487 | - $output .= ' class="' . $class . '" '; |
|
488 | - |
|
489 | - // Attributes |
|
490 | - if ( ! empty( $args['wrap_attributes'] ) ) { |
|
491 | - $output .= AUI_Component_Helper::extra_attributes( $args['wrap_attributes'] ); |
|
492 | - } |
|
493 | - |
|
494 | - // close wrap |
|
495 | - $output .= ' >'; |
|
496 | - |
|
497 | - |
|
498 | - // Input group left |
|
499 | - if ( ! empty( $args['input_group_left'] ) ) { |
|
500 | - $position_class = ! empty( $args['input_group_left_inside'] ) ? 'position-absolute h-100' : ''; |
|
501 | - $input_group_left = strpos( $args['input_group_left'], '<' ) !== false ? $args['input_group_left'] : '<span class="input-group-text">' . $args['input_group_left'] . '</span>'; |
|
502 | - $output .= $aui_bs5 ? $input_group_left : '<div class="input-group-prepend ' . $position_class . '">' . $input_group_left . '</div>'; |
|
312 | + } |
|
313 | + |
|
314 | + // input group wraps |
|
315 | + if ( $args['input_group_left'] || $args['input_group_right'] ) { |
|
316 | + $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; |
|
317 | + $group_size = $size == 'lg' ? ' input-group-lg' : ''; |
|
318 | + $group_size = !$group_size && $size == 'sm' ? ' input-group-sm' : $group_size; |
|
319 | + |
|
320 | + if ( $args['input_group_left'] ) { |
|
321 | + $output = self::wrap( array( |
|
322 | + 'content' => $output, |
|
323 | + 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 . $group_size : 'input-group' . $group_size, |
|
324 | + 'input_group_left' => $args['input_group_left'], |
|
325 | + 'input_group_left_inside' => $args['input_group_left_inside'] |
|
326 | + ) ); |
|
327 | + } |
|
328 | + if ( $args['input_group_right'] ) { |
|
329 | + $output = self::wrap( array( |
|
330 | + 'content' => $output, |
|
331 | + 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 . $group_size : 'input-group' . $group_size, |
|
332 | + 'input_group_right' => $args['input_group_right'], |
|
333 | + 'input_group_right_inside' => $args['input_group_right_inside'] |
|
334 | + ) ); |
|
335 | + } |
|
336 | + |
|
337 | + } |
|
338 | + |
|
339 | + if ( ! $label_after ) { |
|
340 | + $output .= $help_text; |
|
341 | + } |
|
342 | + |
|
343 | + |
|
344 | + if ( $args['label_type'] == 'horizontal' && $type != 'checkbox' ) { |
|
345 | + $output = self::wrap( array( |
|
346 | + 'content' => $output, |
|
347 | + 'class' => AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ) |
|
348 | + ) ); |
|
349 | + } |
|
350 | + |
|
351 | + if ( ! $label_after ) { |
|
352 | + $output = $label . $output; |
|
353 | + } |
|
354 | + |
|
355 | + // wrap |
|
356 | + if ( ! $args['no_wrap'] ) { |
|
357 | + if ( ! empty( $args['form_group_class'] ) ) { |
|
358 | + $fg_class = esc_attr( $args['form_group_class'] ); |
|
359 | + }else{ |
|
360 | + $fg_class = $aui_bs5 ? 'mb-3' : 'form-group'; |
|
361 | + } |
|
362 | + $form_group_class = $args['label_type'] == 'floating' && $type != 'checkbox' ? 'form-label-group' : $fg_class; |
|
363 | + $wrap_class = $args['label_type'] == 'horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
364 | + $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
365 | + $output = self::wrap( array( |
|
366 | + 'content' => $output, |
|
367 | + 'class' => $wrap_class, |
|
368 | + 'element_require' => $args['element_require'], |
|
369 | + 'argument_id' => $args['id'], |
|
370 | + 'wrap_attributes' => $args['wrap_attributes'], |
|
371 | + ) ); |
|
372 | + } |
|
373 | + } |
|
374 | + |
|
375 | + return $output; |
|
376 | + } |
|
377 | + |
|
378 | + public static function label( $args = array(), $type = '' ) { |
|
379 | + global $aui_bs5; |
|
380 | + //<label for="exampleInputEmail1">Email address</label> |
|
381 | + $defaults = array( |
|
382 | + 'title' => 'div', |
|
383 | + 'for' => '', |
|
384 | + 'class' => '', |
|
385 | + 'label_type' => '', // empty = hidden, top, horizontal |
|
386 | + 'label_col' => '', |
|
387 | + ); |
|
388 | + |
|
389 | + /** |
|
390 | + * Parse incoming $args into an array and merge it with $defaults |
|
391 | + */ |
|
392 | + $args = wp_parse_args( $args, $defaults ); |
|
393 | + $output = ''; |
|
394 | + |
|
395 | + if ( $args['title'] ) { |
|
396 | + |
|
397 | + // maybe hide labels //@todo set a global option for visibility class |
|
398 | + if ( $type == 'file' || $type == 'checkbox' || $type == 'radio' || ! empty( $args['label_type'] ) ) { |
|
399 | + $class = $args['class']; |
|
400 | + } else { |
|
401 | + $class = 'sr-only ' . $args['class']; |
|
402 | + } |
|
403 | + |
|
404 | + // maybe horizontal |
|
405 | + if ( $args['label_type'] == 'horizontal' && $type != 'checkbox' ) { |
|
406 | + $class .= ' ' . AUI_Component_Helper::get_column_class( $args['label_col'], 'label' ) . ' col-form-label '.$type; |
|
407 | + } |
|
408 | + |
|
409 | + if( $aui_bs5 ){ $class .= ' form-label'; } |
|
410 | + |
|
411 | + // open |
|
412 | + $output .= '<label '; |
|
413 | + |
|
414 | + // for |
|
415 | + if ( ! empty( $args['for'] ) ) { |
|
416 | + $output .= ' for="' . esc_attr( $args['for'] ) . '" '; |
|
417 | + } |
|
418 | + |
|
419 | + // class |
|
420 | + $class = $class ? AUI_Component_Helper::esc_classes( $class ) : ''; |
|
421 | + $output .= ' class="' . $class . '" '; |
|
422 | + |
|
423 | + // close |
|
424 | + $output .= '>'; |
|
425 | + |
|
426 | + |
|
427 | + // title, don't escape fully as can contain html |
|
428 | + if ( ! empty( $args['title'] ) ) { |
|
429 | + $output .= wp_kses_post( $args['title'] ); |
|
430 | + } |
|
431 | + |
|
432 | + // close wrap |
|
433 | + $output .= '</label>'; |
|
434 | + |
|
435 | + |
|
436 | + } |
|
437 | + |
|
438 | + |
|
439 | + return $output; |
|
440 | + } |
|
441 | + |
|
442 | + /** |
|
443 | + * Wrap some content in a HTML wrapper. |
|
444 | + * |
|
445 | + * @param array $args |
|
446 | + * |
|
447 | + * @return string |
|
448 | + */ |
|
449 | + public static function wrap( $args = array() ) { |
|
450 | + global $aui_bs5; |
|
451 | + $defaults = array( |
|
452 | + 'type' => 'div', |
|
453 | + 'class' => $aui_bs5 ? 'mb-3' : 'form-group', |
|
454 | + 'content' => '', |
|
455 | + 'input_group_left' => '', |
|
456 | + 'input_group_right' => '', |
|
457 | + 'input_group_left_inside' => false, |
|
458 | + 'input_group_right_inside' => false, |
|
459 | + 'element_require' => '', |
|
460 | + 'argument_id' => '', |
|
461 | + 'wrap_attributes' => array() |
|
462 | + ); |
|
463 | + |
|
464 | + /** |
|
465 | + * Parse incoming $args into an array and merge it with $defaults |
|
466 | + */ |
|
467 | + $args = wp_parse_args( $args, $defaults ); |
|
468 | + $output = ''; |
|
469 | + if ( $args['type'] ) { |
|
470 | + |
|
471 | + // open |
|
472 | + $output .= '<' . sanitize_html_class( $args['type'] ); |
|
473 | + |
|
474 | + // element require |
|
475 | + if ( ! empty( $args['element_require'] ) ) { |
|
476 | + $output .= AUI_Component_Helper::element_require( $args['element_require'] ); |
|
477 | + $args['class'] .= " aui-conditional-field"; |
|
478 | + } |
|
479 | + |
|
480 | + // argument_id |
|
481 | + if ( ! empty( $args['argument_id'] ) ) { |
|
482 | + $output .= ' data-argument="' . esc_attr( $args['argument_id'] ) . '"'; |
|
483 | + } |
|
484 | + |
|
485 | + // class |
|
486 | + $class = ! empty( $args['class'] ) ? AUI_Component_Helper::esc_classes( $args['class'] ) : ''; |
|
487 | + $output .= ' class="' . $class . '" '; |
|
488 | + |
|
489 | + // Attributes |
|
490 | + if ( ! empty( $args['wrap_attributes'] ) ) { |
|
491 | + $output .= AUI_Component_Helper::extra_attributes( $args['wrap_attributes'] ); |
|
492 | + } |
|
493 | + |
|
494 | + // close wrap |
|
495 | + $output .= ' >'; |
|
496 | + |
|
497 | + |
|
498 | + // Input group left |
|
499 | + if ( ! empty( $args['input_group_left'] ) ) { |
|
500 | + $position_class = ! empty( $args['input_group_left_inside'] ) ? 'position-absolute h-100' : ''; |
|
501 | + $input_group_left = strpos( $args['input_group_left'], '<' ) !== false ? $args['input_group_left'] : '<span class="input-group-text">' . $args['input_group_left'] . '</span>'; |
|
502 | + $output .= $aui_bs5 ? $input_group_left : '<div class="input-group-prepend ' . $position_class . '">' . $input_group_left . '</div>'; |
|
503 | 503 | // $output .= '<div class="input-group-prepend ' . $position_class . '">' . $input_group_left . '</div>'; |
504 | - } |
|
504 | + } |
|
505 | 505 | |
506 | - // content |
|
507 | - $output .= $args['content']; |
|
506 | + // content |
|
507 | + $output .= $args['content']; |
|
508 | 508 | |
509 | - // Input group right |
|
510 | - if ( ! empty( $args['input_group_right'] ) ) { |
|
511 | - $position_class = ! empty( $args['input_group_right_inside'] ) ? 'position-absolute h-100' : ''; |
|
512 | - $input_group_right = strpos( $args['input_group_right'], '<' ) !== false ? $args['input_group_right'] : '<span class="input-group-text">' . $args['input_group_right'] . '</span>'; |
|
513 | - $output .= $aui_bs5 ? str_replace( 'input-group-text','input-group-text top-0 end-0', $input_group_right ) : '<div class="input-group-append ' . $position_class . '" style="top:0;right:0;">' . $input_group_right . '</div>'; |
|
509 | + // Input group right |
|
510 | + if ( ! empty( $args['input_group_right'] ) ) { |
|
511 | + $position_class = ! empty( $args['input_group_right_inside'] ) ? 'position-absolute h-100' : ''; |
|
512 | + $input_group_right = strpos( $args['input_group_right'], '<' ) !== false ? $args['input_group_right'] : '<span class="input-group-text">' . $args['input_group_right'] . '</span>'; |
|
513 | + $output .= $aui_bs5 ? str_replace( 'input-group-text','input-group-text top-0 end-0', $input_group_right ) : '<div class="input-group-append ' . $position_class . '" style="top:0;right:0;">' . $input_group_right . '</div>'; |
|
514 | 514 | // $output .= '<div class="input-group-append ' . $position_class . '" style="top:0;right:0;">' . $input_group_right . '</div>'; |
515 | - } |
|
516 | - |
|
517 | - |
|
518 | - // close wrap |
|
519 | - $output .= '</' . sanitize_html_class( $args['type'] ) . '>'; |
|
520 | - |
|
521 | - |
|
522 | - } else { |
|
523 | - $output = $args['content']; |
|
524 | - } |
|
525 | - |
|
526 | - return $output; |
|
527 | - } |
|
528 | - |
|
529 | - /** |
|
530 | - * Build the component. |
|
531 | - * |
|
532 | - * @param array $args |
|
533 | - * |
|
534 | - * @return string The rendered component. |
|
535 | - */ |
|
536 | - public static function textarea( $args = array() ) { |
|
537 | - global $aui_bs5; |
|
538 | - |
|
539 | - $defaults = array( |
|
540 | - 'name' => '', |
|
541 | - 'class' => '', |
|
542 | - 'wrap_class' => '', |
|
543 | - 'id' => '', |
|
544 | - 'placeholder' => '', |
|
545 | - 'title' => '', |
|
546 | - 'value' => '', |
|
547 | - 'required' => false, |
|
548 | - 'label' => '', |
|
549 | - 'label_after' => false, |
|
550 | - 'label_class' => '', |
|
551 | - 'label_type' => '', |
|
552 | - 'label_col' => '', |
|
553 | - // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
554 | - 'input_group_right' => '', |
|
555 | - 'input_group_left' => '', |
|
556 | - 'input_group_right_inside' => false, |
|
557 | - 'form_group_class' => '', |
|
558 | - 'help_text' => '', |
|
559 | - 'validation_text' => '', |
|
560 | - 'validation_pattern' => '', |
|
561 | - 'no_wrap' => false, |
|
562 | - 'rows' => '', |
|
563 | - 'wysiwyg' => false, |
|
564 | - 'allow_tags' => false, |
|
565 | - // Allow HTML tags |
|
566 | - 'element_require' => '', |
|
567 | - // [%element_id%] == "1" |
|
568 | - 'extra_attributes' => array(), |
|
569 | - // an array of extra attributes |
|
570 | - 'wrap_attributes' => array(), |
|
571 | - ); |
|
572 | - |
|
573 | - /** |
|
574 | - * Parse incoming $args into an array and merge it with $defaults |
|
575 | - */ |
|
576 | - $args = wp_parse_args( $args, $defaults ); |
|
577 | - $output = ''; |
|
578 | - $label = ''; |
|
579 | - |
|
580 | - // hidden label option needs to be empty |
|
581 | - $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
582 | - |
|
583 | - // floating labels don't work with wysiwyg so set it as top |
|
584 | - if ( $args['label_type'] == 'floating' && ! empty( $args['wysiwyg'] ) ) { |
|
585 | - $args['label_type'] = 'top'; |
|
586 | - } |
|
587 | - |
|
588 | - $label_after = $args['label_after']; |
|
589 | - |
|
590 | - // floating labels need label after |
|
591 | - if ( $args['label_type'] == 'floating' && empty( $args['wysiwyg'] ) ) { |
|
592 | - $label_after = true; |
|
593 | - $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
594 | - } |
|
595 | - |
|
596 | - // label |
|
597 | - if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
598 | - } elseif ( ! empty( $args['label'] ) && ! $label_after ) { |
|
599 | - $label_args = array( |
|
600 | - 'title' => $args['label'], |
|
601 | - 'for' => $args['id'], |
|
602 | - 'class' => $args['label_class'] . " ", |
|
603 | - 'label_type' => $args['label_type'], |
|
604 | - 'label_col' => $args['label_col'] |
|
605 | - ); |
|
606 | - $label .= self::label( $label_args ); |
|
607 | - } |
|
608 | - |
|
609 | - // maybe horizontal label |
|
610 | - if ( $args['label_type'] == 'horizontal' ) { |
|
611 | - $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
612 | - $label .= '<div class="' . $input_col . '">'; |
|
613 | - } |
|
614 | - |
|
615 | - if ( ! empty( $args['wysiwyg'] ) ) { |
|
616 | - ob_start(); |
|
617 | - $content = $args['value']; |
|
618 | - $editor_id = ! empty( $args['id'] ) ? sanitize_html_class( $args['id'] ) : 'wp_editor'; |
|
619 | - $settings = array( |
|
620 | - 'textarea_rows' => ! empty( absint( $args['rows'] ) ) ? absint( $args['rows'] ) : 4, |
|
621 | - 'quicktags' => false, |
|
622 | - 'media_buttons' => false, |
|
623 | - 'editor_class' => 'form-control', |
|
624 | - 'textarea_name' => ! empty( $args['name'] ) ? sanitize_html_class( $args['name'] ) : sanitize_html_class( $args['id'] ), |
|
625 | - 'teeny' => true, |
|
626 | - ); |
|
627 | - |
|
628 | - // maybe set settings if array |
|
629 | - if ( is_array( $args['wysiwyg'] ) ) { |
|
630 | - $settings = wp_parse_args( $args['wysiwyg'], $settings ); |
|
631 | - } |
|
632 | - |
|
633 | - wp_editor( $content, $editor_id, $settings ); |
|
634 | - $output .= ob_get_clean(); |
|
635 | - } else { |
|
636 | - |
|
637 | - // open |
|
638 | - $output .= '<textarea '; |
|
639 | - |
|
640 | - // name |
|
641 | - if ( ! empty( $args['name'] ) ) { |
|
642 | - $output .= ' name="' . esc_attr( $args['name'] ) . '" '; |
|
643 | - } |
|
644 | - |
|
645 | - // id |
|
646 | - if ( ! empty( $args['id'] ) ) { |
|
647 | - $output .= ' id="' . sanitize_html_class( $args['id'] ) . '" '; |
|
648 | - } |
|
649 | - |
|
650 | - // placeholder |
|
651 | - if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] ) { |
|
652 | - $output .= ' placeholder="' . esc_attr( $args['placeholder'] ) . '" '; |
|
653 | - } |
|
654 | - |
|
655 | - // title |
|
656 | - if ( ! empty( $args['title'] ) ) { |
|
657 | - $output .= ' title="' . esc_attr( $args['title'] ) . '" '; |
|
658 | - } |
|
659 | - |
|
660 | - // validation text |
|
661 | - if ( ! empty( $args['validation_text'] ) ) { |
|
662 | - $output .= ' oninvalid="setCustomValidity(\'' . esc_attr( $args['validation_text'] ) . '\')" '; |
|
663 | - $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
664 | - } |
|
665 | - |
|
666 | - // validation_pattern |
|
667 | - if ( ! empty( $args['validation_pattern'] ) ) { |
|
668 | - $output .= ' pattern="' . esc_attr( $args['validation_pattern'] ) . '" '; |
|
669 | - } |
|
670 | - |
|
671 | - // required |
|
672 | - if ( ! empty( $args['required'] ) ) { |
|
673 | - $output .= ' required '; |
|
674 | - } |
|
675 | - |
|
676 | - // rows |
|
677 | - if ( ! empty( $args['rows'] ) ) { |
|
678 | - $output .= ' rows="' . absint( $args['rows'] ) . '" '; |
|
679 | - } |
|
680 | - |
|
681 | - |
|
682 | - // class |
|
683 | - $class = ! empty( $args['class'] ) ? $args['class'] : ''; |
|
684 | - $output .= ' class="form-control ' . $class . '" '; |
|
685 | - |
|
686 | - // extra attributes |
|
687 | - if ( ! empty( $args['extra_attributes'] ) ) { |
|
688 | - $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
689 | - } |
|
690 | - |
|
691 | - // close tag |
|
692 | - $output .= ' >'; |
|
693 | - |
|
694 | - // value |
|
695 | - if ( ! empty( $args['value'] ) ) { |
|
696 | - if ( ! empty( $args['allow_tags'] ) ) { |
|
697 | - $output .= AUI_Component_Helper::sanitize_html_field( $args['value'], $args ); // Sanitize HTML. |
|
698 | - } else { |
|
699 | - $output .= AUI_Component_Helper::sanitize_textarea_field( $args['value'] ); |
|
700 | - } |
|
701 | - } |
|
702 | - |
|
703 | - // closing tag |
|
704 | - $output .= '</textarea>'; |
|
705 | - |
|
706 | - |
|
707 | - // input group wraps |
|
708 | - if ( $args['input_group_left'] || $args['input_group_right'] ) { |
|
709 | - $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; |
|
710 | - if ( $args['input_group_left'] ) { |
|
711 | - $output = self::wrap( array( |
|
712 | - 'content' => $output, |
|
713 | - 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
714 | - 'input_group_left' => $args['input_group_left'], |
|
715 | - 'input_group_left_inside' => $args['input_group_left_inside'] |
|
716 | - ) ); |
|
717 | - } |
|
718 | - if ( $args['input_group_right'] ) { |
|
719 | - $output = self::wrap( array( |
|
720 | - 'content' => $output, |
|
721 | - 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
722 | - 'input_group_right' => $args['input_group_right'], |
|
723 | - 'input_group_right_inside' => $args['input_group_right_inside'] |
|
724 | - ) ); |
|
725 | - } |
|
726 | - |
|
727 | - } |
|
728 | - |
|
729 | - |
|
730 | - } |
|
731 | - |
|
732 | - if ( ! empty( $args['label'] ) && $label_after ) { |
|
733 | - $label_args = array( |
|
734 | - 'title' => $args['label'], |
|
735 | - 'for' => $args['id'], |
|
736 | - 'class' => $args['label_class'] . " ", |
|
737 | - 'label_type' => $args['label_type'], |
|
738 | - 'label_col' => $args['label_col'] |
|
739 | - ); |
|
740 | - $output .= self::label( $label_args ); |
|
741 | - } |
|
742 | - |
|
743 | - // help text |
|
744 | - if ( ! empty( $args['help_text'] ) ) { |
|
745 | - $output .= AUI_Component_Helper::help_text( $args['help_text'] ); |
|
746 | - } |
|
747 | - |
|
748 | - if ( ! $label_after ) { |
|
749 | - $output = $label . $output; |
|
750 | - } |
|
751 | - |
|
752 | - // maybe horizontal label |
|
753 | - if ( $args['label_type'] == 'horizontal' ) { |
|
754 | - $output .= '</div>'; |
|
755 | - } |
|
756 | - |
|
757 | - |
|
758 | - // wrap |
|
759 | - if ( ! $args['no_wrap'] ) { |
|
760 | - if ( ! empty( $args['form_group_class'] ) ) { |
|
761 | - $fg_class = esc_attr( $args['form_group_class'] ); |
|
762 | - }else{ |
|
763 | - $fg_class = $aui_bs5 ? 'mb-3' : 'form-group'; |
|
764 | - } |
|
765 | - $form_group_class = $args['label_type'] == 'floating' ? 'form-label-group' : $fg_class; |
|
766 | - $wrap_class = $args['label_type'] == 'horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
767 | - $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
768 | - $output = self::wrap( array( |
|
769 | - 'content' => $output, |
|
770 | - 'class' => $wrap_class, |
|
771 | - 'element_require' => $args['element_require'], |
|
772 | - 'argument_id' => $args['id'], |
|
773 | - 'wrap_attributes' => $args['wrap_attributes'], |
|
774 | - ) ); |
|
775 | - } |
|
776 | - |
|
777 | - |
|
778 | - return $output; |
|
779 | - } |
|
780 | - |
|
781 | - /** |
|
782 | - * Build the component. |
|
783 | - * |
|
784 | - * @param array $args |
|
785 | - * |
|
786 | - * @return string The rendered component. |
|
787 | - */ |
|
788 | - public static function select( $args = array() ) { |
|
789 | - global $aui_bs5; |
|
790 | - $defaults = array( |
|
791 | - 'class' => '', |
|
792 | - 'wrap_class' => '', |
|
793 | - 'id' => '', |
|
794 | - 'title' => '', |
|
795 | - 'value' => '', |
|
796 | - // can be an array or a string |
|
797 | - 'required' => false, |
|
798 | - 'label' => '', |
|
799 | - 'label_after' => false, |
|
800 | - 'label_type' => '', |
|
801 | - 'label_col' => '', |
|
802 | - // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
803 | - 'label_class' => '', |
|
804 | - 'help_text' => '', |
|
805 | - 'placeholder' => '', |
|
806 | - 'options' => array(), |
|
807 | - // array or string |
|
808 | - 'icon' => '', |
|
809 | - 'multiple' => false, |
|
810 | - 'select2' => false, |
|
811 | - 'no_wrap' => false, |
|
812 | - 'input_group_right' => '', |
|
813 | - 'input_group_left' => '', |
|
814 | - 'input_group_right_inside' => false, // forces the input group inside the input |
|
815 | - 'input_group_left_inside' => false, // forces the input group inside the input |
|
816 | - 'form_group_class' => '', |
|
817 | - 'element_require' => '', |
|
818 | - // [%element_id%] == "1" |
|
819 | - 'extra_attributes' => array(), |
|
820 | - // an array of extra attributes |
|
821 | - 'wrap_attributes' => array(), |
|
822 | - ); |
|
823 | - |
|
824 | - /** |
|
825 | - * Parse incoming $args into an array and merge it with $defaults |
|
826 | - */ |
|
827 | - $args = wp_parse_args( $args, $defaults ); |
|
828 | - $output = ''; |
|
829 | - |
|
830 | - // for now lets hide floating labels |
|
831 | - if ( $args['label_type'] == 'floating' ) { |
|
832 | - $args['label_type'] = 'hidden'; |
|
833 | - } |
|
834 | - |
|
835 | - // hidden label option needs to be empty |
|
836 | - $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
837 | - |
|
838 | - |
|
839 | - $label_after = $args['label_after']; |
|
840 | - |
|
841 | - // floating labels need label after |
|
842 | - if ( $args['label_type'] == 'floating' ) { |
|
843 | - $label_after = true; |
|
844 | - $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
845 | - } |
|
846 | - |
|
847 | - // Maybe setup select2 |
|
848 | - $is_select2 = false; |
|
849 | - if ( ! empty( $args['select2'] ) ) { |
|
850 | - $args['class'] .= ' aui-select2'; |
|
851 | - $is_select2 = true; |
|
852 | - } elseif ( strpos( $args['class'], 'aui-select2' ) !== false ) { |
|
853 | - $is_select2 = true; |
|
854 | - } |
|
855 | - |
|
856 | - // select2 tags |
|
857 | - if ( ! empty( $args['select2'] ) && $args['select2'] === 'tags' ) { // triple equals needed here for some reason |
|
858 | - $args['data-tags'] = 'true'; |
|
859 | - $args['data-token-separators'] = "[',']"; |
|
860 | - $args['multiple'] = true; |
|
861 | - } |
|
862 | - |
|
863 | - // select2 placeholder |
|
864 | - if ( $is_select2 && isset( $args['placeholder'] ) && '' != $args['placeholder'] && empty( $args['data-placeholder'] ) ) { |
|
865 | - $args['data-placeholder'] = esc_attr( $args['placeholder'] ); |
|
866 | - $args['data-allow-clear'] = isset( $args['data-allow-clear'] ) ? (bool) $args['data-allow-clear'] : true; |
|
867 | - } |
|
868 | - |
|
869 | - // Set hidden input to save empty value for multiselect. |
|
870 | - if ( ! empty( $args['multiple'] ) && ! empty( $args['name'] ) ) { |
|
871 | - $output .= '<input type="hidden" ' . AUI_Component_Helper::name( $args['name'] ) . ' value="" data-ignore-rule/>'; |
|
872 | - } |
|
873 | - |
|
874 | - // open/type |
|
875 | - $output .= '<select '; |
|
876 | - |
|
877 | - // style |
|
878 | - if ( $is_select2 && !($args['input_group_left'] || $args['input_group_right'])) { |
|
879 | - $output .= " style='width:100%;' "; |
|
880 | - } |
|
881 | - |
|
882 | - // element require |
|
883 | - if ( ! empty( $args['element_require'] ) ) { |
|
884 | - $output .= AUI_Component_Helper::element_require( $args['element_require'] ); |
|
885 | - $args['class'] .= " aui-conditional-field"; |
|
886 | - } |
|
887 | - |
|
888 | - // class |
|
889 | - $class = ! empty( $args['class'] ) ? $args['class'] : ''; |
|
890 | - $select_class = $aui_bs5 ? 'form-select ' : 'custom-select '; |
|
891 | - $output .= AUI_Component_Helper::class_attr( $select_class . $class ); |
|
892 | - |
|
893 | - // name |
|
894 | - if ( ! empty( $args['name'] ) ) { |
|
895 | - $output .= AUI_Component_Helper::name( $args['name'], $args['multiple'] ); |
|
896 | - } |
|
897 | - |
|
898 | - // id |
|
899 | - if ( ! empty( $args['id'] ) ) { |
|
900 | - $output .= AUI_Component_Helper::id( $args['id'] ); |
|
901 | - } |
|
902 | - |
|
903 | - // title |
|
904 | - if ( ! empty( $args['title'] ) ) { |
|
905 | - $output .= AUI_Component_Helper::title( $args['title'] ); |
|
906 | - } |
|
907 | - |
|
908 | - // data-attributes |
|
909 | - $output .= AUI_Component_Helper::data_attributes( $args ); |
|
910 | - |
|
911 | - // aria-attributes |
|
912 | - $output .= AUI_Component_Helper::aria_attributes( $args ); |
|
913 | - |
|
914 | - // extra attributes |
|
915 | - if ( ! empty( $args['extra_attributes'] ) ) { |
|
916 | - $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
917 | - } |
|
918 | - |
|
919 | - // required |
|
920 | - if ( ! empty( $args['required'] ) ) { |
|
921 | - $output .= ' required '; |
|
922 | - } |
|
923 | - |
|
924 | - // multiple |
|
925 | - if ( ! empty( $args['multiple'] ) ) { |
|
926 | - $output .= ' multiple '; |
|
927 | - } |
|
928 | - |
|
929 | - // close opening tag |
|
930 | - $output .= ' >'; |
|
931 | - |
|
932 | - // placeholder |
|
933 | - if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] && ! $is_select2 ) { |
|
934 | - $output .= '<option value="" disabled selected hidden>' . esc_attr( $args['placeholder'] ) . '</option>'; |
|
935 | - } elseif ( $is_select2 && ! empty( $args['placeholder'] ) ) { |
|
936 | - $output .= "<option></option>"; // select2 needs an empty select to fill the placeholder |
|
937 | - } |
|
938 | - |
|
939 | - // Options |
|
940 | - if ( ! empty( $args['options'] ) ) { |
|
941 | - |
|
942 | - if ( ! is_array( $args['options'] ) ) { |
|
943 | - $output .= $args['options']; // not the preferred way but an option |
|
944 | - } else { |
|
945 | - foreach ( $args['options'] as $val => $name ) { |
|
946 | - $selected = ''; |
|
947 | - if ( is_array( $name ) ) { |
|
948 | - if ( isset( $name['optgroup'] ) && ( $name['optgroup'] == 'start' || $name['optgroup'] == 'end' ) ) { |
|
949 | - $option_label = isset( $name['label'] ) ? $name['label'] : ''; |
|
950 | - |
|
951 | - $output .= $name['optgroup'] == 'start' ? '<optgroup label="' . esc_attr( $option_label ) . '">' : '</optgroup>'; |
|
952 | - } else { |
|
953 | - $option_label = isset( $name['label'] ) ? $name['label'] : ''; |
|
954 | - $option_value = isset( $name['value'] ) ? $name['value'] : ''; |
|
955 | - $extra_attributes = !empty($name['extra_attributes']) ? AUI_Component_Helper::extra_attributes( $name['extra_attributes'] ) : ''; |
|
956 | - if ( ! empty( $args['multiple'] ) && ! empty( $args['value'] ) && is_array( $args['value'] ) ) { |
|
957 | - $selected = in_array( $option_value, stripslashes_deep( $args['value'] ) ) ? "selected" : ""; |
|
958 | - } elseif ( ! empty( $args['value'] ) ) { |
|
959 | - $selected = selected( $option_value, stripslashes_deep( $args['value'] ), false ); |
|
960 | - } elseif ( empty( $args['value'] ) && $args['value'] === $option_value ) { |
|
961 | - $selected = selected( $option_value, $args['value'], false ); |
|
962 | - } |
|
963 | - |
|
964 | - $output .= '<option value="' . esc_attr( $option_value ) . '" ' . $selected . ' '.$extra_attributes .'>' . $option_label . '</option>'; |
|
965 | - } |
|
966 | - } else { |
|
967 | - if ( ! empty( $args['value'] ) ) { |
|
968 | - if ( is_array( $args['value'] ) ) { |
|
969 | - $selected = in_array( $val, $args['value'] ) ? 'selected="selected"' : ''; |
|
970 | - } elseif ( ! empty( $args['value'] ) ) { |
|
971 | - $selected = selected( $args['value'], $val, false ); |
|
972 | - } |
|
973 | - } elseif ( $args['value'] === $val ) { |
|
974 | - $selected = selected( $args['value'], $val, false ); |
|
975 | - } |
|
976 | - $output .= '<option value="' . esc_attr( $val ) . '" ' . $selected . '>' . esc_attr( $name ) . '</option>'; |
|
977 | - } |
|
978 | - } |
|
979 | - } |
|
980 | - |
|
981 | - } |
|
982 | - |
|
983 | - // closing tag |
|
984 | - $output .= '</select>'; |
|
985 | - |
|
986 | - $label = ''; |
|
987 | - $help_text = ''; |
|
988 | - // label |
|
989 | - if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
990 | - } elseif ( ! empty( $args['label'] ) && ! $label_after ) { |
|
991 | - $label_args = array( |
|
992 | - 'title' => $args['label'], |
|
993 | - 'for' => $args['id'], |
|
994 | - 'class' => $args['label_class'] . " ", |
|
995 | - 'label_type' => $args['label_type'], |
|
996 | - 'label_col' => $args['label_col'] |
|
997 | - ); |
|
998 | - $label = self::label( $label_args ); |
|
999 | - } |
|
1000 | - |
|
1001 | - // help text |
|
1002 | - if ( ! empty( $args['help_text'] ) ) { |
|
1003 | - $help_text = AUI_Component_Helper::help_text( $args['help_text'] ); |
|
1004 | - } |
|
1005 | - |
|
1006 | - // input group wraps |
|
1007 | - if ( $args['input_group_left'] || $args['input_group_right'] ) { |
|
1008 | - $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; |
|
1009 | - if ( $args['input_group_left'] ) { |
|
1010 | - $output = self::wrap( array( |
|
1011 | - 'content' => $output, |
|
1012 | - 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
1013 | - 'input_group_left' => $args['input_group_left'], |
|
1014 | - 'input_group_left_inside' => $args['input_group_left_inside'] |
|
1015 | - ) ); |
|
1016 | - } |
|
1017 | - if ( $args['input_group_right'] ) { |
|
1018 | - $output = self::wrap( array( |
|
1019 | - 'content' => $output, |
|
1020 | - 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
1021 | - 'input_group_right' => $args['input_group_right'], |
|
1022 | - 'input_group_right_inside' => $args['input_group_right_inside'] |
|
1023 | - ) ); |
|
1024 | - } |
|
1025 | - |
|
1026 | - } |
|
1027 | - |
|
1028 | - if ( ! $label_after ) { |
|
1029 | - $output .= $help_text; |
|
1030 | - } |
|
1031 | - |
|
1032 | - |
|
1033 | - if ( $args['label_type'] == 'horizontal' ) { |
|
1034 | - $output = self::wrap( array( |
|
1035 | - 'content' => $output, |
|
1036 | - 'class' => AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ) |
|
1037 | - ) ); |
|
1038 | - } |
|
1039 | - |
|
1040 | - if ( ! $label_after ) { |
|
1041 | - $output = $label . $output; |
|
1042 | - } |
|
1043 | - |
|
1044 | - // maybe horizontal label |
|
515 | + } |
|
516 | + |
|
517 | + |
|
518 | + // close wrap |
|
519 | + $output .= '</' . sanitize_html_class( $args['type'] ) . '>'; |
|
520 | + |
|
521 | + |
|
522 | + } else { |
|
523 | + $output = $args['content']; |
|
524 | + } |
|
525 | + |
|
526 | + return $output; |
|
527 | + } |
|
528 | + |
|
529 | + /** |
|
530 | + * Build the component. |
|
531 | + * |
|
532 | + * @param array $args |
|
533 | + * |
|
534 | + * @return string The rendered component. |
|
535 | + */ |
|
536 | + public static function textarea( $args = array() ) { |
|
537 | + global $aui_bs5; |
|
538 | + |
|
539 | + $defaults = array( |
|
540 | + 'name' => '', |
|
541 | + 'class' => '', |
|
542 | + 'wrap_class' => '', |
|
543 | + 'id' => '', |
|
544 | + 'placeholder' => '', |
|
545 | + 'title' => '', |
|
546 | + 'value' => '', |
|
547 | + 'required' => false, |
|
548 | + 'label' => '', |
|
549 | + 'label_after' => false, |
|
550 | + 'label_class' => '', |
|
551 | + 'label_type' => '', |
|
552 | + 'label_col' => '', |
|
553 | + // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
554 | + 'input_group_right' => '', |
|
555 | + 'input_group_left' => '', |
|
556 | + 'input_group_right_inside' => false, |
|
557 | + 'form_group_class' => '', |
|
558 | + 'help_text' => '', |
|
559 | + 'validation_text' => '', |
|
560 | + 'validation_pattern' => '', |
|
561 | + 'no_wrap' => false, |
|
562 | + 'rows' => '', |
|
563 | + 'wysiwyg' => false, |
|
564 | + 'allow_tags' => false, |
|
565 | + // Allow HTML tags |
|
566 | + 'element_require' => '', |
|
567 | + // [%element_id%] == "1" |
|
568 | + 'extra_attributes' => array(), |
|
569 | + // an array of extra attributes |
|
570 | + 'wrap_attributes' => array(), |
|
571 | + ); |
|
572 | + |
|
573 | + /** |
|
574 | + * Parse incoming $args into an array and merge it with $defaults |
|
575 | + */ |
|
576 | + $args = wp_parse_args( $args, $defaults ); |
|
577 | + $output = ''; |
|
578 | + $label = ''; |
|
579 | + |
|
580 | + // hidden label option needs to be empty |
|
581 | + $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
582 | + |
|
583 | + // floating labels don't work with wysiwyg so set it as top |
|
584 | + if ( $args['label_type'] == 'floating' && ! empty( $args['wysiwyg'] ) ) { |
|
585 | + $args['label_type'] = 'top'; |
|
586 | + } |
|
587 | + |
|
588 | + $label_after = $args['label_after']; |
|
589 | + |
|
590 | + // floating labels need label after |
|
591 | + if ( $args['label_type'] == 'floating' && empty( $args['wysiwyg'] ) ) { |
|
592 | + $label_after = true; |
|
593 | + $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
594 | + } |
|
595 | + |
|
596 | + // label |
|
597 | + if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
598 | + } elseif ( ! empty( $args['label'] ) && ! $label_after ) { |
|
599 | + $label_args = array( |
|
600 | + 'title' => $args['label'], |
|
601 | + 'for' => $args['id'], |
|
602 | + 'class' => $args['label_class'] . " ", |
|
603 | + 'label_type' => $args['label_type'], |
|
604 | + 'label_col' => $args['label_col'] |
|
605 | + ); |
|
606 | + $label .= self::label( $label_args ); |
|
607 | + } |
|
608 | + |
|
609 | + // maybe horizontal label |
|
610 | + if ( $args['label_type'] == 'horizontal' ) { |
|
611 | + $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
612 | + $label .= '<div class="' . $input_col . '">'; |
|
613 | + } |
|
614 | + |
|
615 | + if ( ! empty( $args['wysiwyg'] ) ) { |
|
616 | + ob_start(); |
|
617 | + $content = $args['value']; |
|
618 | + $editor_id = ! empty( $args['id'] ) ? sanitize_html_class( $args['id'] ) : 'wp_editor'; |
|
619 | + $settings = array( |
|
620 | + 'textarea_rows' => ! empty( absint( $args['rows'] ) ) ? absint( $args['rows'] ) : 4, |
|
621 | + 'quicktags' => false, |
|
622 | + 'media_buttons' => false, |
|
623 | + 'editor_class' => 'form-control', |
|
624 | + 'textarea_name' => ! empty( $args['name'] ) ? sanitize_html_class( $args['name'] ) : sanitize_html_class( $args['id'] ), |
|
625 | + 'teeny' => true, |
|
626 | + ); |
|
627 | + |
|
628 | + // maybe set settings if array |
|
629 | + if ( is_array( $args['wysiwyg'] ) ) { |
|
630 | + $settings = wp_parse_args( $args['wysiwyg'], $settings ); |
|
631 | + } |
|
632 | + |
|
633 | + wp_editor( $content, $editor_id, $settings ); |
|
634 | + $output .= ob_get_clean(); |
|
635 | + } else { |
|
636 | + |
|
637 | + // open |
|
638 | + $output .= '<textarea '; |
|
639 | + |
|
640 | + // name |
|
641 | + if ( ! empty( $args['name'] ) ) { |
|
642 | + $output .= ' name="' . esc_attr( $args['name'] ) . '" '; |
|
643 | + } |
|
644 | + |
|
645 | + // id |
|
646 | + if ( ! empty( $args['id'] ) ) { |
|
647 | + $output .= ' id="' . sanitize_html_class( $args['id'] ) . '" '; |
|
648 | + } |
|
649 | + |
|
650 | + // placeholder |
|
651 | + if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] ) { |
|
652 | + $output .= ' placeholder="' . esc_attr( $args['placeholder'] ) . '" '; |
|
653 | + } |
|
654 | + |
|
655 | + // title |
|
656 | + if ( ! empty( $args['title'] ) ) { |
|
657 | + $output .= ' title="' . esc_attr( $args['title'] ) . '" '; |
|
658 | + } |
|
659 | + |
|
660 | + // validation text |
|
661 | + if ( ! empty( $args['validation_text'] ) ) { |
|
662 | + $output .= ' oninvalid="setCustomValidity(\'' . esc_attr( $args['validation_text'] ) . '\')" '; |
|
663 | + $output .= ' onchange="try{setCustomValidity(\'\')}catch(e){}" '; |
|
664 | + } |
|
665 | + |
|
666 | + // validation_pattern |
|
667 | + if ( ! empty( $args['validation_pattern'] ) ) { |
|
668 | + $output .= ' pattern="' . esc_attr( $args['validation_pattern'] ) . '" '; |
|
669 | + } |
|
670 | + |
|
671 | + // required |
|
672 | + if ( ! empty( $args['required'] ) ) { |
|
673 | + $output .= ' required '; |
|
674 | + } |
|
675 | + |
|
676 | + // rows |
|
677 | + if ( ! empty( $args['rows'] ) ) { |
|
678 | + $output .= ' rows="' . absint( $args['rows'] ) . '" '; |
|
679 | + } |
|
680 | + |
|
681 | + |
|
682 | + // class |
|
683 | + $class = ! empty( $args['class'] ) ? $args['class'] : ''; |
|
684 | + $output .= ' class="form-control ' . $class . '" '; |
|
685 | + |
|
686 | + // extra attributes |
|
687 | + if ( ! empty( $args['extra_attributes'] ) ) { |
|
688 | + $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
689 | + } |
|
690 | + |
|
691 | + // close tag |
|
692 | + $output .= ' >'; |
|
693 | + |
|
694 | + // value |
|
695 | + if ( ! empty( $args['value'] ) ) { |
|
696 | + if ( ! empty( $args['allow_tags'] ) ) { |
|
697 | + $output .= AUI_Component_Helper::sanitize_html_field( $args['value'], $args ); // Sanitize HTML. |
|
698 | + } else { |
|
699 | + $output .= AUI_Component_Helper::sanitize_textarea_field( $args['value'] ); |
|
700 | + } |
|
701 | + } |
|
702 | + |
|
703 | + // closing tag |
|
704 | + $output .= '</textarea>'; |
|
705 | + |
|
706 | + |
|
707 | + // input group wraps |
|
708 | + if ( $args['input_group_left'] || $args['input_group_right'] ) { |
|
709 | + $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; |
|
710 | + if ( $args['input_group_left'] ) { |
|
711 | + $output = self::wrap( array( |
|
712 | + 'content' => $output, |
|
713 | + 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
714 | + 'input_group_left' => $args['input_group_left'], |
|
715 | + 'input_group_left_inside' => $args['input_group_left_inside'] |
|
716 | + ) ); |
|
717 | + } |
|
718 | + if ( $args['input_group_right'] ) { |
|
719 | + $output = self::wrap( array( |
|
720 | + 'content' => $output, |
|
721 | + 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
722 | + 'input_group_right' => $args['input_group_right'], |
|
723 | + 'input_group_right_inside' => $args['input_group_right_inside'] |
|
724 | + ) ); |
|
725 | + } |
|
726 | + |
|
727 | + } |
|
728 | + |
|
729 | + |
|
730 | + } |
|
731 | + |
|
732 | + if ( ! empty( $args['label'] ) && $label_after ) { |
|
733 | + $label_args = array( |
|
734 | + 'title' => $args['label'], |
|
735 | + 'for' => $args['id'], |
|
736 | + 'class' => $args['label_class'] . " ", |
|
737 | + 'label_type' => $args['label_type'], |
|
738 | + 'label_col' => $args['label_col'] |
|
739 | + ); |
|
740 | + $output .= self::label( $label_args ); |
|
741 | + } |
|
742 | + |
|
743 | + // help text |
|
744 | + if ( ! empty( $args['help_text'] ) ) { |
|
745 | + $output .= AUI_Component_Helper::help_text( $args['help_text'] ); |
|
746 | + } |
|
747 | + |
|
748 | + if ( ! $label_after ) { |
|
749 | + $output = $label . $output; |
|
750 | + } |
|
751 | + |
|
752 | + // maybe horizontal label |
|
753 | + if ( $args['label_type'] == 'horizontal' ) { |
|
754 | + $output .= '</div>'; |
|
755 | + } |
|
756 | + |
|
757 | + |
|
758 | + // wrap |
|
759 | + if ( ! $args['no_wrap'] ) { |
|
760 | + if ( ! empty( $args['form_group_class'] ) ) { |
|
761 | + $fg_class = esc_attr( $args['form_group_class'] ); |
|
762 | + }else{ |
|
763 | + $fg_class = $aui_bs5 ? 'mb-3' : 'form-group'; |
|
764 | + } |
|
765 | + $form_group_class = $args['label_type'] == 'floating' ? 'form-label-group' : $fg_class; |
|
766 | + $wrap_class = $args['label_type'] == 'horizontal' ? $form_group_class . ' row' : $form_group_class; |
|
767 | + $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
768 | + $output = self::wrap( array( |
|
769 | + 'content' => $output, |
|
770 | + 'class' => $wrap_class, |
|
771 | + 'element_require' => $args['element_require'], |
|
772 | + 'argument_id' => $args['id'], |
|
773 | + 'wrap_attributes' => $args['wrap_attributes'], |
|
774 | + ) ); |
|
775 | + } |
|
776 | + |
|
777 | + |
|
778 | + return $output; |
|
779 | + } |
|
780 | + |
|
781 | + /** |
|
782 | + * Build the component. |
|
783 | + * |
|
784 | + * @param array $args |
|
785 | + * |
|
786 | + * @return string The rendered component. |
|
787 | + */ |
|
788 | + public static function select( $args = array() ) { |
|
789 | + global $aui_bs5; |
|
790 | + $defaults = array( |
|
791 | + 'class' => '', |
|
792 | + 'wrap_class' => '', |
|
793 | + 'id' => '', |
|
794 | + 'title' => '', |
|
795 | + 'value' => '', |
|
796 | + // can be an array or a string |
|
797 | + 'required' => false, |
|
798 | + 'label' => '', |
|
799 | + 'label_after' => false, |
|
800 | + 'label_type' => '', |
|
801 | + 'label_col' => '', |
|
802 | + // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
803 | + 'label_class' => '', |
|
804 | + 'help_text' => '', |
|
805 | + 'placeholder' => '', |
|
806 | + 'options' => array(), |
|
807 | + // array or string |
|
808 | + 'icon' => '', |
|
809 | + 'multiple' => false, |
|
810 | + 'select2' => false, |
|
811 | + 'no_wrap' => false, |
|
812 | + 'input_group_right' => '', |
|
813 | + 'input_group_left' => '', |
|
814 | + 'input_group_right_inside' => false, // forces the input group inside the input |
|
815 | + 'input_group_left_inside' => false, // forces the input group inside the input |
|
816 | + 'form_group_class' => '', |
|
817 | + 'element_require' => '', |
|
818 | + // [%element_id%] == "1" |
|
819 | + 'extra_attributes' => array(), |
|
820 | + // an array of extra attributes |
|
821 | + 'wrap_attributes' => array(), |
|
822 | + ); |
|
823 | + |
|
824 | + /** |
|
825 | + * Parse incoming $args into an array and merge it with $defaults |
|
826 | + */ |
|
827 | + $args = wp_parse_args( $args, $defaults ); |
|
828 | + $output = ''; |
|
829 | + |
|
830 | + // for now lets hide floating labels |
|
831 | + if ( $args['label_type'] == 'floating' ) { |
|
832 | + $args['label_type'] = 'hidden'; |
|
833 | + } |
|
834 | + |
|
835 | + // hidden label option needs to be empty |
|
836 | + $args['label_type'] = $args['label_type'] == 'hidden' ? '' : $args['label_type']; |
|
837 | + |
|
838 | + |
|
839 | + $label_after = $args['label_after']; |
|
840 | + |
|
841 | + // floating labels need label after |
|
842 | + if ( $args['label_type'] == 'floating' ) { |
|
843 | + $label_after = true; |
|
844 | + $args['placeholder'] = ' '; // set the placeholder not empty so the floating label works. |
|
845 | + } |
|
846 | + |
|
847 | + // Maybe setup select2 |
|
848 | + $is_select2 = false; |
|
849 | + if ( ! empty( $args['select2'] ) ) { |
|
850 | + $args['class'] .= ' aui-select2'; |
|
851 | + $is_select2 = true; |
|
852 | + } elseif ( strpos( $args['class'], 'aui-select2' ) !== false ) { |
|
853 | + $is_select2 = true; |
|
854 | + } |
|
855 | + |
|
856 | + // select2 tags |
|
857 | + if ( ! empty( $args['select2'] ) && $args['select2'] === 'tags' ) { // triple equals needed here for some reason |
|
858 | + $args['data-tags'] = 'true'; |
|
859 | + $args['data-token-separators'] = "[',']"; |
|
860 | + $args['multiple'] = true; |
|
861 | + } |
|
862 | + |
|
863 | + // select2 placeholder |
|
864 | + if ( $is_select2 && isset( $args['placeholder'] ) && '' != $args['placeholder'] && empty( $args['data-placeholder'] ) ) { |
|
865 | + $args['data-placeholder'] = esc_attr( $args['placeholder'] ); |
|
866 | + $args['data-allow-clear'] = isset( $args['data-allow-clear'] ) ? (bool) $args['data-allow-clear'] : true; |
|
867 | + } |
|
868 | + |
|
869 | + // Set hidden input to save empty value for multiselect. |
|
870 | + if ( ! empty( $args['multiple'] ) && ! empty( $args['name'] ) ) { |
|
871 | + $output .= '<input type="hidden" ' . AUI_Component_Helper::name( $args['name'] ) . ' value="" data-ignore-rule/>'; |
|
872 | + } |
|
873 | + |
|
874 | + // open/type |
|
875 | + $output .= '<select '; |
|
876 | + |
|
877 | + // style |
|
878 | + if ( $is_select2 && !($args['input_group_left'] || $args['input_group_right'])) { |
|
879 | + $output .= " style='width:100%;' "; |
|
880 | + } |
|
881 | + |
|
882 | + // element require |
|
883 | + if ( ! empty( $args['element_require'] ) ) { |
|
884 | + $output .= AUI_Component_Helper::element_require( $args['element_require'] ); |
|
885 | + $args['class'] .= " aui-conditional-field"; |
|
886 | + } |
|
887 | + |
|
888 | + // class |
|
889 | + $class = ! empty( $args['class'] ) ? $args['class'] : ''; |
|
890 | + $select_class = $aui_bs5 ? 'form-select ' : 'custom-select '; |
|
891 | + $output .= AUI_Component_Helper::class_attr( $select_class . $class ); |
|
892 | + |
|
893 | + // name |
|
894 | + if ( ! empty( $args['name'] ) ) { |
|
895 | + $output .= AUI_Component_Helper::name( $args['name'], $args['multiple'] ); |
|
896 | + } |
|
897 | + |
|
898 | + // id |
|
899 | + if ( ! empty( $args['id'] ) ) { |
|
900 | + $output .= AUI_Component_Helper::id( $args['id'] ); |
|
901 | + } |
|
902 | + |
|
903 | + // title |
|
904 | + if ( ! empty( $args['title'] ) ) { |
|
905 | + $output .= AUI_Component_Helper::title( $args['title'] ); |
|
906 | + } |
|
907 | + |
|
908 | + // data-attributes |
|
909 | + $output .= AUI_Component_Helper::data_attributes( $args ); |
|
910 | + |
|
911 | + // aria-attributes |
|
912 | + $output .= AUI_Component_Helper::aria_attributes( $args ); |
|
913 | + |
|
914 | + // extra attributes |
|
915 | + if ( ! empty( $args['extra_attributes'] ) ) { |
|
916 | + $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
917 | + } |
|
918 | + |
|
919 | + // required |
|
920 | + if ( ! empty( $args['required'] ) ) { |
|
921 | + $output .= ' required '; |
|
922 | + } |
|
923 | + |
|
924 | + // multiple |
|
925 | + if ( ! empty( $args['multiple'] ) ) { |
|
926 | + $output .= ' multiple '; |
|
927 | + } |
|
928 | + |
|
929 | + // close opening tag |
|
930 | + $output .= ' >'; |
|
931 | + |
|
932 | + // placeholder |
|
933 | + if ( isset( $args['placeholder'] ) && '' != $args['placeholder'] && ! $is_select2 ) { |
|
934 | + $output .= '<option value="" disabled selected hidden>' . esc_attr( $args['placeholder'] ) . '</option>'; |
|
935 | + } elseif ( $is_select2 && ! empty( $args['placeholder'] ) ) { |
|
936 | + $output .= "<option></option>"; // select2 needs an empty select to fill the placeholder |
|
937 | + } |
|
938 | + |
|
939 | + // Options |
|
940 | + if ( ! empty( $args['options'] ) ) { |
|
941 | + |
|
942 | + if ( ! is_array( $args['options'] ) ) { |
|
943 | + $output .= $args['options']; // not the preferred way but an option |
|
944 | + } else { |
|
945 | + foreach ( $args['options'] as $val => $name ) { |
|
946 | + $selected = ''; |
|
947 | + if ( is_array( $name ) ) { |
|
948 | + if ( isset( $name['optgroup'] ) && ( $name['optgroup'] == 'start' || $name['optgroup'] == 'end' ) ) { |
|
949 | + $option_label = isset( $name['label'] ) ? $name['label'] : ''; |
|
950 | + |
|
951 | + $output .= $name['optgroup'] == 'start' ? '<optgroup label="' . esc_attr( $option_label ) . '">' : '</optgroup>'; |
|
952 | + } else { |
|
953 | + $option_label = isset( $name['label'] ) ? $name['label'] : ''; |
|
954 | + $option_value = isset( $name['value'] ) ? $name['value'] : ''; |
|
955 | + $extra_attributes = !empty($name['extra_attributes']) ? AUI_Component_Helper::extra_attributes( $name['extra_attributes'] ) : ''; |
|
956 | + if ( ! empty( $args['multiple'] ) && ! empty( $args['value'] ) && is_array( $args['value'] ) ) { |
|
957 | + $selected = in_array( $option_value, stripslashes_deep( $args['value'] ) ) ? "selected" : ""; |
|
958 | + } elseif ( ! empty( $args['value'] ) ) { |
|
959 | + $selected = selected( $option_value, stripslashes_deep( $args['value'] ), false ); |
|
960 | + } elseif ( empty( $args['value'] ) && $args['value'] === $option_value ) { |
|
961 | + $selected = selected( $option_value, $args['value'], false ); |
|
962 | + } |
|
963 | + |
|
964 | + $output .= '<option value="' . esc_attr( $option_value ) . '" ' . $selected . ' '.$extra_attributes .'>' . $option_label . '</option>'; |
|
965 | + } |
|
966 | + } else { |
|
967 | + if ( ! empty( $args['value'] ) ) { |
|
968 | + if ( is_array( $args['value'] ) ) { |
|
969 | + $selected = in_array( $val, $args['value'] ) ? 'selected="selected"' : ''; |
|
970 | + } elseif ( ! empty( $args['value'] ) ) { |
|
971 | + $selected = selected( $args['value'], $val, false ); |
|
972 | + } |
|
973 | + } elseif ( $args['value'] === $val ) { |
|
974 | + $selected = selected( $args['value'], $val, false ); |
|
975 | + } |
|
976 | + $output .= '<option value="' . esc_attr( $val ) . '" ' . $selected . '>' . esc_attr( $name ) . '</option>'; |
|
977 | + } |
|
978 | + } |
|
979 | + } |
|
980 | + |
|
981 | + } |
|
982 | + |
|
983 | + // closing tag |
|
984 | + $output .= '</select>'; |
|
985 | + |
|
986 | + $label = ''; |
|
987 | + $help_text = ''; |
|
988 | + // label |
|
989 | + if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
990 | + } elseif ( ! empty( $args['label'] ) && ! $label_after ) { |
|
991 | + $label_args = array( |
|
992 | + 'title' => $args['label'], |
|
993 | + 'for' => $args['id'], |
|
994 | + 'class' => $args['label_class'] . " ", |
|
995 | + 'label_type' => $args['label_type'], |
|
996 | + 'label_col' => $args['label_col'] |
|
997 | + ); |
|
998 | + $label = self::label( $label_args ); |
|
999 | + } |
|
1000 | + |
|
1001 | + // help text |
|
1002 | + if ( ! empty( $args['help_text'] ) ) { |
|
1003 | + $help_text = AUI_Component_Helper::help_text( $args['help_text'] ); |
|
1004 | + } |
|
1005 | + |
|
1006 | + // input group wraps |
|
1007 | + if ( $args['input_group_left'] || $args['input_group_right'] ) { |
|
1008 | + $w100 = strpos( $args['class'], 'w-100' ) !== false ? ' w-100' : ''; |
|
1009 | + if ( $args['input_group_left'] ) { |
|
1010 | + $output = self::wrap( array( |
|
1011 | + 'content' => $output, |
|
1012 | + 'class' => $args['input_group_left_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
1013 | + 'input_group_left' => $args['input_group_left'], |
|
1014 | + 'input_group_left_inside' => $args['input_group_left_inside'] |
|
1015 | + ) ); |
|
1016 | + } |
|
1017 | + if ( $args['input_group_right'] ) { |
|
1018 | + $output = self::wrap( array( |
|
1019 | + 'content' => $output, |
|
1020 | + 'class' => $args['input_group_right_inside'] ? 'input-group-inside position-relative' . $w100 : 'input-group', |
|
1021 | + 'input_group_right' => $args['input_group_right'], |
|
1022 | + 'input_group_right_inside' => $args['input_group_right_inside'] |
|
1023 | + ) ); |
|
1024 | + } |
|
1025 | + |
|
1026 | + } |
|
1027 | + |
|
1028 | + if ( ! $label_after ) { |
|
1029 | + $output .= $help_text; |
|
1030 | + } |
|
1031 | + |
|
1032 | + |
|
1033 | + if ( $args['label_type'] == 'horizontal' ) { |
|
1034 | + $output = self::wrap( array( |
|
1035 | + 'content' => $output, |
|
1036 | + 'class' => AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ) |
|
1037 | + ) ); |
|
1038 | + } |
|
1039 | + |
|
1040 | + if ( ! $label_after ) { |
|
1041 | + $output = $label . $output; |
|
1042 | + } |
|
1043 | + |
|
1044 | + // maybe horizontal label |
|
1045 | 1045 | // if ( $args['label_type'] == 'horizontal' ) { |
1046 | 1046 | // $output .= '</div>'; |
1047 | 1047 | // } |
1048 | 1048 | |
1049 | 1049 | |
1050 | - // wrap |
|
1051 | - if ( ! $args['no_wrap'] ) { |
|
1052 | - if ( ! empty( $args['form_group_class'] ) ) { |
|
1053 | - $fg_class = esc_attr( $args['form_group_class'] ); |
|
1054 | - }else{ |
|
1055 | - $fg_class = $aui_bs5 ? 'mb-3' : 'form-group'; |
|
1056 | - } |
|
1057 | - $wrap_class = $args['label_type'] == 'horizontal' ? $fg_class . ' row' : $fg_class; |
|
1058 | - $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
1059 | - $output = self::wrap( array( |
|
1060 | - 'content' => $output, |
|
1061 | - 'class' => $wrap_class, |
|
1062 | - 'element_require' => $args['element_require'], |
|
1063 | - 'argument_id' => $args['id'], |
|
1064 | - 'wrap_attributes' => $args['wrap_attributes'], |
|
1065 | - ) ); |
|
1066 | - } |
|
1067 | - |
|
1068 | - |
|
1069 | - return $output; |
|
1070 | - } |
|
1071 | - |
|
1072 | - /** |
|
1073 | - * Build the component. |
|
1074 | - * |
|
1075 | - * @param array $args |
|
1076 | - * |
|
1077 | - * @return string The rendered component. |
|
1078 | - */ |
|
1079 | - public static function radio( $args = array() ) { |
|
1080 | - global $aui_bs5; |
|
1081 | - |
|
1082 | - $defaults = array( |
|
1083 | - 'class' => '', |
|
1084 | - 'wrap_class' => '', |
|
1085 | - 'id' => '', |
|
1086 | - 'title' => '', |
|
1087 | - 'horizontal' => false, |
|
1088 | - // sets the lable horizontal |
|
1089 | - 'value' => '', |
|
1090 | - 'label' => '', |
|
1091 | - 'label_class' => '', |
|
1092 | - 'label_type' => '', |
|
1093 | - 'label_col' => '', |
|
1094 | - // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
1095 | - 'help_text' => '', |
|
1096 | - 'inline' => true, |
|
1097 | - 'required' => false, |
|
1098 | - 'options' => array(), |
|
1099 | - 'icon' => '', |
|
1100 | - 'no_wrap' => false, |
|
1101 | - 'element_require' => '', |
|
1102 | - // [%element_id%] == "1" |
|
1103 | - 'extra_attributes' => array(), |
|
1104 | - // an array of extra attributes |
|
1105 | - 'wrap_attributes' => array() |
|
1106 | - ); |
|
1107 | - |
|
1108 | - /** |
|
1109 | - * Parse incoming $args into an array and merge it with $defaults |
|
1110 | - */ |
|
1111 | - $args = wp_parse_args( $args, $defaults ); |
|
1112 | - |
|
1113 | - // for now lets use horizontal for floating |
|
1114 | - if ( $args['label_type'] == 'floating' ) { |
|
1115 | - $args['label_type'] = 'horizontal'; |
|
1116 | - } |
|
1117 | - |
|
1118 | - $label_args = array( |
|
1119 | - 'title' => $args['label'], |
|
1120 | - 'class' => $args['label_class'] . " pt-0 ", |
|
1121 | - 'label_type' => $args['label_type'], |
|
1122 | - 'label_col' => $args['label_col'] |
|
1123 | - ); |
|
1124 | - |
|
1125 | - $output = ''; |
|
1126 | - |
|
1127 | - |
|
1128 | - // label before |
|
1129 | - if ( ! empty( $args['label'] ) ) { |
|
1130 | - $output .= self::label( $label_args, 'radio' ); |
|
1131 | - } |
|
1132 | - |
|
1133 | - // maybe horizontal label |
|
1134 | - if ( $args['label_type'] == 'horizontal' ) { |
|
1135 | - $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
1136 | - $output .= '<div class="' . $input_col . '">'; |
|
1137 | - } |
|
1138 | - |
|
1139 | - if ( ! empty( $args['options'] ) ) { |
|
1140 | - $count = 0; |
|
1141 | - foreach ( $args['options'] as $value => $label ) { |
|
1142 | - $option_args = $args; |
|
1143 | - $option_args['value'] = $value; |
|
1144 | - $option_args['label'] = $label; |
|
1145 | - $option_args['checked'] = $value == $args['value'] ? true : false; |
|
1146 | - $output .= self::radio_option( $option_args, $count ); |
|
1147 | - $count ++; |
|
1148 | - } |
|
1149 | - } |
|
1150 | - |
|
1151 | - // help text |
|
1152 | - $help_text = ! empty( $args['help_text'] ) ? AUI_Component_Helper::help_text( $args['help_text'] ) : ''; |
|
1153 | - $output .= $help_text; |
|
1154 | - |
|
1155 | - // maybe horizontal label |
|
1156 | - if ( $args['label_type'] == 'horizontal' ) { |
|
1157 | - $output .= '</div>'; |
|
1158 | - } |
|
1159 | - |
|
1160 | - // wrap |
|
1161 | - $fg_class = $aui_bs5 ? 'mb-3' : 'form-group'; |
|
1162 | - $wrap_class = $args['label_type'] == 'horizontal' ? $fg_class . ' row' : $fg_class; |
|
1163 | - $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
1164 | - $output = self::wrap( array( |
|
1165 | - 'content' => $output, |
|
1166 | - 'class' => $wrap_class, |
|
1167 | - 'element_require' => $args['element_require'], |
|
1168 | - 'argument_id' => $args['id'], |
|
1169 | - 'wrap_attributes' => $args['wrap_attributes'], |
|
1170 | - ) ); |
|
1171 | - |
|
1172 | - |
|
1173 | - return $output; |
|
1174 | - } |
|
1175 | - |
|
1176 | - /** |
|
1177 | - * Build the component. |
|
1178 | - * |
|
1179 | - * @param array $args |
|
1180 | - * |
|
1181 | - * @return string The rendered component. |
|
1182 | - */ |
|
1183 | - public static function radio_option( $args = array(), $count = '' ) { |
|
1184 | - $defaults = array( |
|
1185 | - 'class' => '', |
|
1186 | - 'id' => '', |
|
1187 | - 'title' => '', |
|
1188 | - 'value' => '', |
|
1189 | - 'required' => false, |
|
1190 | - 'inline' => true, |
|
1191 | - 'label' => '', |
|
1192 | - 'options' => array(), |
|
1193 | - 'icon' => '', |
|
1194 | - 'no_wrap' => false, |
|
1195 | - 'extra_attributes' => array() // an array of extra attributes |
|
1196 | - ); |
|
1197 | - |
|
1198 | - /** |
|
1199 | - * Parse incoming $args into an array and merge it with $defaults |
|
1200 | - */ |
|
1201 | - $args = wp_parse_args( $args, $defaults ); |
|
1202 | - |
|
1203 | - $output = ''; |
|
1204 | - |
|
1205 | - // open/type |
|
1206 | - $output .= '<input type="radio"'; |
|
1207 | - |
|
1208 | - // class |
|
1209 | - $output .= ' class="form-check-input" '; |
|
1210 | - |
|
1211 | - // name |
|
1212 | - if ( ! empty( $args['name'] ) ) { |
|
1213 | - $output .= AUI_Component_Helper::name( $args['name'] ); |
|
1214 | - } |
|
1215 | - |
|
1216 | - // id |
|
1217 | - if ( ! empty( $args['id'] ) ) { |
|
1218 | - $output .= AUI_Component_Helper::id( $args['id'] . $count ); |
|
1219 | - } |
|
1220 | - |
|
1221 | - // title |
|
1222 | - if ( ! empty( $args['title'] ) ) { |
|
1223 | - $output .= AUI_Component_Helper::title( $args['title'] ); |
|
1224 | - } |
|
1225 | - |
|
1226 | - // value |
|
1227 | - if ( isset( $args['value'] ) ) { |
|
1228 | - $output .= AUI_Component_Helper::value( $args['value'] ); |
|
1229 | - } |
|
1230 | - |
|
1231 | - // checked, for radio and checkboxes |
|
1232 | - if ( $args['checked'] ) { |
|
1233 | - $output .= ' checked '; |
|
1234 | - } |
|
1235 | - |
|
1236 | - // data-attributes |
|
1237 | - $output .= AUI_Component_Helper::data_attributes( $args ); |
|
1238 | - |
|
1239 | - // aria-attributes |
|
1240 | - $output .= AUI_Component_Helper::aria_attributes( $args ); |
|
1241 | - |
|
1242 | - // extra attributes |
|
1243 | - if ( ! empty( $args['extra_attributes'] ) ) { |
|
1244 | - $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
1245 | - } |
|
1246 | - |
|
1247 | - // required |
|
1248 | - if ( ! empty( $args['required'] ) ) { |
|
1249 | - $output .= ' required '; |
|
1250 | - } |
|
1251 | - |
|
1252 | - // close opening tag |
|
1253 | - $output .= ' >'; |
|
1254 | - |
|
1255 | - // label |
|
1256 | - if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
1257 | - } elseif ( ! empty( $args['label'] ) ) { |
|
1258 | - $output .= self::label( array( |
|
1259 | - 'title' => $args['label'], |
|
1260 | - 'for' => $args['id'] . $count, |
|
1261 | - 'class' => 'form-check-label' |
|
1262 | - ), 'radio' ); |
|
1263 | - } |
|
1264 | - |
|
1265 | - // wrap |
|
1266 | - if ( ! $args['no_wrap'] ) { |
|
1267 | - $wrap_class = $args['inline'] ? 'form-check form-check-inline' : 'form-check'; |
|
1268 | - |
|
1269 | - // Unique wrap class |
|
1270 | - $uniq_class = 'fwrap'; |
|
1271 | - if ( ! empty( $args['name'] ) ) { |
|
1272 | - $uniq_class .= '-' . $args['name']; |
|
1273 | - } else if ( ! empty( $args['id'] ) ) { |
|
1274 | - $uniq_class .= '-' . $args['id']; |
|
1275 | - } |
|
1276 | - |
|
1277 | - if ( isset( $args['value'] ) || $args['value'] !== "" ) { |
|
1278 | - $uniq_class .= '-' . $args['value']; |
|
1279 | - } else { |
|
1280 | - $uniq_class .= '-' . $count; |
|
1281 | - } |
|
1282 | - $wrap_class .= ' ' . sanitize_html_class( $uniq_class ); |
|
1283 | - |
|
1284 | - $output = self::wrap( array( |
|
1285 | - 'content' => $output, |
|
1286 | - 'class' => $wrap_class |
|
1287 | - ) ); |
|
1288 | - } |
|
1289 | - |
|
1290 | - return $output; |
|
1291 | - } |
|
1050 | + // wrap |
|
1051 | + if ( ! $args['no_wrap'] ) { |
|
1052 | + if ( ! empty( $args['form_group_class'] ) ) { |
|
1053 | + $fg_class = esc_attr( $args['form_group_class'] ); |
|
1054 | + }else{ |
|
1055 | + $fg_class = $aui_bs5 ? 'mb-3' : 'form-group'; |
|
1056 | + } |
|
1057 | + $wrap_class = $args['label_type'] == 'horizontal' ? $fg_class . ' row' : $fg_class; |
|
1058 | + $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
1059 | + $output = self::wrap( array( |
|
1060 | + 'content' => $output, |
|
1061 | + 'class' => $wrap_class, |
|
1062 | + 'element_require' => $args['element_require'], |
|
1063 | + 'argument_id' => $args['id'], |
|
1064 | + 'wrap_attributes' => $args['wrap_attributes'], |
|
1065 | + ) ); |
|
1066 | + } |
|
1067 | + |
|
1068 | + |
|
1069 | + return $output; |
|
1070 | + } |
|
1071 | + |
|
1072 | + /** |
|
1073 | + * Build the component. |
|
1074 | + * |
|
1075 | + * @param array $args |
|
1076 | + * |
|
1077 | + * @return string The rendered component. |
|
1078 | + */ |
|
1079 | + public static function radio( $args = array() ) { |
|
1080 | + global $aui_bs5; |
|
1081 | + |
|
1082 | + $defaults = array( |
|
1083 | + 'class' => '', |
|
1084 | + 'wrap_class' => '', |
|
1085 | + 'id' => '', |
|
1086 | + 'title' => '', |
|
1087 | + 'horizontal' => false, |
|
1088 | + // sets the lable horizontal |
|
1089 | + 'value' => '', |
|
1090 | + 'label' => '', |
|
1091 | + 'label_class' => '', |
|
1092 | + 'label_type' => '', |
|
1093 | + 'label_col' => '', |
|
1094 | + // sets the label type, default: hidden. Options: hidden, top, horizontal, floating |
|
1095 | + 'help_text' => '', |
|
1096 | + 'inline' => true, |
|
1097 | + 'required' => false, |
|
1098 | + 'options' => array(), |
|
1099 | + 'icon' => '', |
|
1100 | + 'no_wrap' => false, |
|
1101 | + 'element_require' => '', |
|
1102 | + // [%element_id%] == "1" |
|
1103 | + 'extra_attributes' => array(), |
|
1104 | + // an array of extra attributes |
|
1105 | + 'wrap_attributes' => array() |
|
1106 | + ); |
|
1107 | + |
|
1108 | + /** |
|
1109 | + * Parse incoming $args into an array and merge it with $defaults |
|
1110 | + */ |
|
1111 | + $args = wp_parse_args( $args, $defaults ); |
|
1112 | + |
|
1113 | + // for now lets use horizontal for floating |
|
1114 | + if ( $args['label_type'] == 'floating' ) { |
|
1115 | + $args['label_type'] = 'horizontal'; |
|
1116 | + } |
|
1117 | + |
|
1118 | + $label_args = array( |
|
1119 | + 'title' => $args['label'], |
|
1120 | + 'class' => $args['label_class'] . " pt-0 ", |
|
1121 | + 'label_type' => $args['label_type'], |
|
1122 | + 'label_col' => $args['label_col'] |
|
1123 | + ); |
|
1124 | + |
|
1125 | + $output = ''; |
|
1126 | + |
|
1127 | + |
|
1128 | + // label before |
|
1129 | + if ( ! empty( $args['label'] ) ) { |
|
1130 | + $output .= self::label( $label_args, 'radio' ); |
|
1131 | + } |
|
1132 | + |
|
1133 | + // maybe horizontal label |
|
1134 | + if ( $args['label_type'] == 'horizontal' ) { |
|
1135 | + $input_col = AUI_Component_Helper::get_column_class( $args['label_col'], 'input' ); |
|
1136 | + $output .= '<div class="' . $input_col . '">'; |
|
1137 | + } |
|
1138 | + |
|
1139 | + if ( ! empty( $args['options'] ) ) { |
|
1140 | + $count = 0; |
|
1141 | + foreach ( $args['options'] as $value => $label ) { |
|
1142 | + $option_args = $args; |
|
1143 | + $option_args['value'] = $value; |
|
1144 | + $option_args['label'] = $label; |
|
1145 | + $option_args['checked'] = $value == $args['value'] ? true : false; |
|
1146 | + $output .= self::radio_option( $option_args, $count ); |
|
1147 | + $count ++; |
|
1148 | + } |
|
1149 | + } |
|
1150 | + |
|
1151 | + // help text |
|
1152 | + $help_text = ! empty( $args['help_text'] ) ? AUI_Component_Helper::help_text( $args['help_text'] ) : ''; |
|
1153 | + $output .= $help_text; |
|
1154 | + |
|
1155 | + // maybe horizontal label |
|
1156 | + if ( $args['label_type'] == 'horizontal' ) { |
|
1157 | + $output .= '</div>'; |
|
1158 | + } |
|
1159 | + |
|
1160 | + // wrap |
|
1161 | + $fg_class = $aui_bs5 ? 'mb-3' : 'form-group'; |
|
1162 | + $wrap_class = $args['label_type'] == 'horizontal' ? $fg_class . ' row' : $fg_class; |
|
1163 | + $wrap_class = ! empty( $args['wrap_class'] ) ? $wrap_class . " " . $args['wrap_class'] : $wrap_class; |
|
1164 | + $output = self::wrap( array( |
|
1165 | + 'content' => $output, |
|
1166 | + 'class' => $wrap_class, |
|
1167 | + 'element_require' => $args['element_require'], |
|
1168 | + 'argument_id' => $args['id'], |
|
1169 | + 'wrap_attributes' => $args['wrap_attributes'], |
|
1170 | + ) ); |
|
1171 | + |
|
1172 | + |
|
1173 | + return $output; |
|
1174 | + } |
|
1175 | + |
|
1176 | + /** |
|
1177 | + * Build the component. |
|
1178 | + * |
|
1179 | + * @param array $args |
|
1180 | + * |
|
1181 | + * @return string The rendered component. |
|
1182 | + */ |
|
1183 | + public static function radio_option( $args = array(), $count = '' ) { |
|
1184 | + $defaults = array( |
|
1185 | + 'class' => '', |
|
1186 | + 'id' => '', |
|
1187 | + 'title' => '', |
|
1188 | + 'value' => '', |
|
1189 | + 'required' => false, |
|
1190 | + 'inline' => true, |
|
1191 | + 'label' => '', |
|
1192 | + 'options' => array(), |
|
1193 | + 'icon' => '', |
|
1194 | + 'no_wrap' => false, |
|
1195 | + 'extra_attributes' => array() // an array of extra attributes |
|
1196 | + ); |
|
1197 | + |
|
1198 | + /** |
|
1199 | + * Parse incoming $args into an array and merge it with $defaults |
|
1200 | + */ |
|
1201 | + $args = wp_parse_args( $args, $defaults ); |
|
1202 | + |
|
1203 | + $output = ''; |
|
1204 | + |
|
1205 | + // open/type |
|
1206 | + $output .= '<input type="radio"'; |
|
1207 | + |
|
1208 | + // class |
|
1209 | + $output .= ' class="form-check-input" '; |
|
1210 | + |
|
1211 | + // name |
|
1212 | + if ( ! empty( $args['name'] ) ) { |
|
1213 | + $output .= AUI_Component_Helper::name( $args['name'] ); |
|
1214 | + } |
|
1215 | + |
|
1216 | + // id |
|
1217 | + if ( ! empty( $args['id'] ) ) { |
|
1218 | + $output .= AUI_Component_Helper::id( $args['id'] . $count ); |
|
1219 | + } |
|
1220 | + |
|
1221 | + // title |
|
1222 | + if ( ! empty( $args['title'] ) ) { |
|
1223 | + $output .= AUI_Component_Helper::title( $args['title'] ); |
|
1224 | + } |
|
1225 | + |
|
1226 | + // value |
|
1227 | + if ( isset( $args['value'] ) ) { |
|
1228 | + $output .= AUI_Component_Helper::value( $args['value'] ); |
|
1229 | + } |
|
1230 | + |
|
1231 | + // checked, for radio and checkboxes |
|
1232 | + if ( $args['checked'] ) { |
|
1233 | + $output .= ' checked '; |
|
1234 | + } |
|
1235 | + |
|
1236 | + // data-attributes |
|
1237 | + $output .= AUI_Component_Helper::data_attributes( $args ); |
|
1238 | + |
|
1239 | + // aria-attributes |
|
1240 | + $output .= AUI_Component_Helper::aria_attributes( $args ); |
|
1241 | + |
|
1242 | + // extra attributes |
|
1243 | + if ( ! empty( $args['extra_attributes'] ) ) { |
|
1244 | + $output .= AUI_Component_Helper::extra_attributes( $args['extra_attributes'] ); |
|
1245 | + } |
|
1246 | + |
|
1247 | + // required |
|
1248 | + if ( ! empty( $args['required'] ) ) { |
|
1249 | + $output .= ' required '; |
|
1250 | + } |
|
1251 | + |
|
1252 | + // close opening tag |
|
1253 | + $output .= ' >'; |
|
1254 | + |
|
1255 | + // label |
|
1256 | + if ( ! empty( $args['label'] ) && is_array( $args['label'] ) ) { |
|
1257 | + } elseif ( ! empty( $args['label'] ) ) { |
|
1258 | + $output .= self::label( array( |
|
1259 | + 'title' => $args['label'], |
|
1260 | + 'for' => $args['id'] . $count, |
|
1261 | + 'class' => 'form-check-label' |
|
1262 | + ), 'radio' ); |
|
1263 | + } |
|
1264 | + |
|
1265 | + // wrap |
|
1266 | + if ( ! $args['no_wrap'] ) { |
|
1267 | + $wrap_class = $args['inline'] ? 'form-check form-check-inline' : 'form-check'; |
|
1268 | + |
|
1269 | + // Unique wrap class |
|
1270 | + $uniq_class = 'fwrap'; |
|
1271 | + if ( ! empty( $args['name'] ) ) { |
|
1272 | + $uniq_class .= '-' . $args['name']; |
|
1273 | + } else if ( ! empty( $args['id'] ) ) { |
|
1274 | + $uniq_class .= '-' . $args['id']; |
|
1275 | + } |
|
1276 | + |
|
1277 | + if ( isset( $args['value'] ) || $args['value'] !== "" ) { |
|
1278 | + $uniq_class .= '-' . $args['value']; |
|
1279 | + } else { |
|
1280 | + $uniq_class .= '-' . $count; |
|
1281 | + } |
|
1282 | + $wrap_class .= ' ' . sanitize_html_class( $uniq_class ); |
|
1283 | + |
|
1284 | + $output = self::wrap( array( |
|
1285 | + 'content' => $output, |
|
1286 | + 'class' => $wrap_class |
|
1287 | + ) ); |
|
1288 | + } |
|
1289 | + |
|
1290 | + return $output; |
|
1291 | + } |
|
1292 | 1292 | |
1293 | 1293 | } |
1294 | 1294 | \ No newline at end of file |
@@ -14,57 +14,57 @@ |
||
14 | 14 | * @return array|mixed|string|string[] |
15 | 15 | */ |
16 | 16 | function aui_bs_convert_sd_output( $output, $instance = '', $args = '', $sd = '' ) { |
17 | - global $aui_bs5; |
|
17 | + global $aui_bs5; |
|
18 | 18 | |
19 | - if ( $aui_bs5 ) { |
|
19 | + if ( $aui_bs5 ) { |
|
20 | 20 | |
21 | - $convert = array( |
|
22 | - 'ml-' => 'ms-', |
|
23 | - 'mr-' => 'me-', |
|
24 | - 'pl-' => 'ps-', |
|
25 | - 'pr-' => 'pe-', |
|
26 | - ' form-row' => ' row', |
|
27 | - ' embed-responsive-item' => '', |
|
28 | - ' embed-responsive' => ' ratio', |
|
29 | - '-1by1' => '-1x1', |
|
30 | - '-4by3' => '-4x3', |
|
31 | - '-16by9' => '-16x9', |
|
32 | - '-21by9' => '-21x9', |
|
33 | - 'geodir-lightbox-image' => 'aui-lightbox-image', |
|
34 | - ' badge-' => ' text-bg-', |
|
35 | - 'form-group' => 'mb-3', |
|
36 | - 'custom-select' => 'form-select', |
|
37 | - 'float-left' => 'float-start', |
|
38 | - 'float-right' => 'float-end', |
|
39 | - 'text-left' => 'text-start', |
|
40 | - 'text-right' => 'text-end', |
|
41 | - 'border-right' => 'border-end', |
|
42 | - 'border-left' => 'border-start', |
|
43 | - 'font-weight-' => 'fw-', |
|
44 | - 'btn-block' => 'w-100', |
|
45 | - 'rounded-left' => 'rounded-start', |
|
46 | - 'rounded-right' => 'rounded-end', |
|
21 | + $convert = array( |
|
22 | + 'ml-' => 'ms-', |
|
23 | + 'mr-' => 'me-', |
|
24 | + 'pl-' => 'ps-', |
|
25 | + 'pr-' => 'pe-', |
|
26 | + ' form-row' => ' row', |
|
27 | + ' embed-responsive-item' => '', |
|
28 | + ' embed-responsive' => ' ratio', |
|
29 | + '-1by1' => '-1x1', |
|
30 | + '-4by3' => '-4x3', |
|
31 | + '-16by9' => '-16x9', |
|
32 | + '-21by9' => '-21x9', |
|
33 | + 'geodir-lightbox-image' => 'aui-lightbox-image', |
|
34 | + ' badge-' => ' text-bg-', |
|
35 | + 'form-group' => 'mb-3', |
|
36 | + 'custom-select' => 'form-select', |
|
37 | + 'float-left' => 'float-start', |
|
38 | + 'float-right' => 'float-end', |
|
39 | + 'text-left' => 'text-start', |
|
40 | + 'text-right' => 'text-end', |
|
41 | + 'border-right' => 'border-end', |
|
42 | + 'border-left' => 'border-start', |
|
43 | + 'font-weight-' => 'fw-', |
|
44 | + 'btn-block' => 'w-100', |
|
45 | + 'rounded-left' => 'rounded-start', |
|
46 | + 'rounded-right' => 'rounded-end', |
|
47 | 47 | |
48 | 48 | // 'custom-control custom-checkbox' => 'form-check', |
49 | - // data |
|
50 | - ' data-toggle=' => ' data-bs-toggle=', |
|
51 | - 'data-ride=' => 'data-bs-ride=', |
|
52 | - 'data-controlnav=' => 'data-bs-controlnav=', |
|
53 | - 'data-slide=' => 'data-bs-slide=', |
|
54 | - 'data-slide-to=' => 'data-bs-slide-to=', |
|
55 | - 'data-target=' => 'data-bs-target=', |
|
56 | - 'data-dismiss="modal"' => 'data-bs-dismiss="modal"', |
|
57 | - 'class="close"' => 'class="btn-close"', |
|
58 | - '<span aria-hidden="true">×</span>' => '', |
|
59 | - ); |
|
60 | - $output = str_replace( |
|
61 | - array_keys( $convert ), |
|
62 | - array_values( $convert ), |
|
63 | - $output |
|
64 | - ); |
|
65 | - } |
|
49 | + // data |
|
50 | + ' data-toggle=' => ' data-bs-toggle=', |
|
51 | + 'data-ride=' => 'data-bs-ride=', |
|
52 | + 'data-controlnav=' => 'data-bs-controlnav=', |
|
53 | + 'data-slide=' => 'data-bs-slide=', |
|
54 | + 'data-slide-to=' => 'data-bs-slide-to=', |
|
55 | + 'data-target=' => 'data-bs-target=', |
|
56 | + 'data-dismiss="modal"' => 'data-bs-dismiss="modal"', |
|
57 | + 'class="close"' => 'class="btn-close"', |
|
58 | + '<span aria-hidden="true">×</span>' => '', |
|
59 | + ); |
|
60 | + $output = str_replace( |
|
61 | + array_keys( $convert ), |
|
62 | + array_values( $convert ), |
|
63 | + $output |
|
64 | + ); |
|
65 | + } |
|
66 | 66 | |
67 | - return $output; |
|
67 | + return $output; |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | add_filter( 'wp_super_duper_widget_output', 'aui_bs_convert_sd_output', 10, 4 ); //$output, $instance, $args, $this |
@@ -12,473 +12,473 @@ |
||
12 | 12 | */ |
13 | 13 | class GetPaid_Paypal_Gateway_IPN_Handler { |
14 | 14 | |
15 | - /** |
|
16 | - * Payment method id. |
|
17 | - * |
|
18 | - * @var string |
|
19 | - */ |
|
20 | - protected $id = 'paypal'; |
|
21 | - |
|
22 | - /** |
|
23 | - * Payment method object. |
|
24 | - * |
|
25 | - * @var GetPaid_Paypal_Gateway |
|
26 | - */ |
|
27 | - protected $gateway; |
|
28 | - |
|
29 | - /** |
|
30 | - * Class constructor. |
|
31 | - * |
|
32 | - * @param GetPaid_Paypal_Gateway $gateway |
|
33 | - */ |
|
34 | - public function __construct( $gateway ) { |
|
35 | - $this->gateway = $gateway; |
|
36 | - $this->verify_ipn(); |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * Processes ipns and marks payments as complete. |
|
41 | - * |
|
42 | - * @return void |
|
43 | - */ |
|
44 | - public function verify_ipn() { |
|
45 | - |
|
46 | - wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
47 | - |
|
48 | - // Validate the IPN. |
|
49 | - if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
50 | - wp_die( 'PayPal IPN Request Failure', 500 ); |
|
51 | - } |
|
52 | - |
|
53 | - // Process the IPN. |
|
54 | - $posted = wp_unslash( $_POST ); |
|
55 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
56 | - |
|
57 | - // Abort if it was not paid by our gateway. |
|
58 | - if ( $this->id != $invoice->get_gateway() ) { |
|
59 | - wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
60 | - wp_die( 'Invoice not paid via PayPal', 200 ); |
|
61 | - } |
|
62 | - |
|
63 | - $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
64 | - $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
65 | - |
|
66 | - wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
67 | - wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
68 | - |
|
69 | - if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
70 | - call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
71 | - wpinv_error_log( 'Done processing IPN', false ); |
|
72 | - wp_die( 'Processed', 200 ); |
|
73 | - } |
|
74 | - |
|
75 | - wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
76 | - wp_die( 'Unsupported IPN type', 200 ); |
|
77 | - |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Retrieves IPN Invoice. |
|
82 | - * |
|
83 | - * @param array $posted |
|
84 | - * @return WPInv_Invoice |
|
85 | - */ |
|
86 | - protected function get_ipn_invoice( $posted ) { |
|
87 | - |
|
88 | - wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
89 | - |
|
90 | - if ( ! empty( $posted['custom'] ) ) { |
|
91 | - $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
92 | - |
|
93 | - if ( $invoice->exists() ) { |
|
94 | - wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
95 | - return $invoice; |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
100 | - wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * Check PayPal IPN validity. |
|
105 | - */ |
|
106 | - protected function validate_ipn() { |
|
107 | - |
|
108 | - wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
109 | - |
|
110 | - // Retrieve the associated invoice. |
|
111 | - $posted = wp_unslash( $_POST ); |
|
112 | - $invoice = $this->get_ipn_invoice( $posted ); |
|
113 | - |
|
114 | - if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
115 | - wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data', false ); |
|
116 | - } |
|
117 | - |
|
118 | - // Validate the IPN. |
|
119 | - $posted['cmd'] = '_notify-validate'; |
|
120 | - |
|
121 | - // Send back post vars to paypal. |
|
122 | - $params = array( |
|
123 | - 'body' => $posted, |
|
124 | - 'timeout' => 60, |
|
125 | - 'httpversion' => '1.1', |
|
126 | - 'compress' => false, |
|
127 | - 'decompress' => false, |
|
128 | - 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
129 | - ); |
|
130 | - |
|
131 | - // Post back to get a response. |
|
132 | - $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
133 | - |
|
134 | - // Check to see if the request was valid. |
|
135 | - if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
136 | - wpinv_error_log( 'Received valid response from PayPal IPN: ' . $response['body'], false ); |
|
137 | - return true; |
|
138 | - } |
|
139 | - |
|
140 | - if ( is_wp_error( $response ) ) { |
|
141 | - wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
142 | - return false; |
|
143 | - } |
|
144 | - |
|
145 | - wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
146 | - return false; |
|
147 | - |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Check currency from IPN matches the invoice. |
|
152 | - * |
|
153 | - * @param WPInv_Invoice $invoice Invoice object. |
|
154 | - * @param string $currency currency to validate. |
|
155 | - */ |
|
156 | - protected function validate_ipn_currency( $invoice, $currency ) { |
|
15 | + /** |
|
16 | + * Payment method id. |
|
17 | + * |
|
18 | + * @var string |
|
19 | + */ |
|
20 | + protected $id = 'paypal'; |
|
21 | + |
|
22 | + /** |
|
23 | + * Payment method object. |
|
24 | + * |
|
25 | + * @var GetPaid_Paypal_Gateway |
|
26 | + */ |
|
27 | + protected $gateway; |
|
28 | + |
|
29 | + /** |
|
30 | + * Class constructor. |
|
31 | + * |
|
32 | + * @param GetPaid_Paypal_Gateway $gateway |
|
33 | + */ |
|
34 | + public function __construct( $gateway ) { |
|
35 | + $this->gateway = $gateway; |
|
36 | + $this->verify_ipn(); |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * Processes ipns and marks payments as complete. |
|
41 | + * |
|
42 | + * @return void |
|
43 | + */ |
|
44 | + public function verify_ipn() { |
|
45 | + |
|
46 | + wpinv_error_log( 'GetPaid PayPal IPN Handler', false ); |
|
47 | + |
|
48 | + // Validate the IPN. |
|
49 | + if ( empty( $_POST ) || ! $this->validate_ipn() ) { |
|
50 | + wp_die( 'PayPal IPN Request Failure', 500 ); |
|
51 | + } |
|
52 | + |
|
53 | + // Process the IPN. |
|
54 | + $posted = wp_unslash( $_POST ); |
|
55 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
56 | + |
|
57 | + // Abort if it was not paid by our gateway. |
|
58 | + if ( $this->id != $invoice->get_gateway() ) { |
|
59 | + wpinv_error_log( 'Aborting, Invoice was not paid via PayPal', false ); |
|
60 | + wp_die( 'Invoice not paid via PayPal', 200 ); |
|
61 | + } |
|
62 | + |
|
63 | + $posted['payment_status'] = isset( $posted['payment_status'] ) ? sanitize_key( strtolower( $posted['payment_status'] ) ) : ''; |
|
64 | + $posted['txn_type'] = sanitize_key( strtolower( $posted['txn_type'] ) ); |
|
65 | + |
|
66 | + wpinv_error_log( 'Payment status:' . $posted['payment_status'], false ); |
|
67 | + wpinv_error_log( 'IPN Type:' . $posted['txn_type'], false ); |
|
68 | + |
|
69 | + if ( method_exists( $this, 'ipn_txn_' . $posted['txn_type'] ) ) { |
|
70 | + call_user_func( array( $this, 'ipn_txn_' . $posted['txn_type'] ), $invoice, $posted ); |
|
71 | + wpinv_error_log( 'Done processing IPN', false ); |
|
72 | + wp_die( 'Processed', 200 ); |
|
73 | + } |
|
74 | + |
|
75 | + wpinv_error_log( 'Aborting, Unsupported IPN type:' . $posted['txn_type'], false ); |
|
76 | + wp_die( 'Unsupported IPN type', 200 ); |
|
77 | + |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Retrieves IPN Invoice. |
|
82 | + * |
|
83 | + * @param array $posted |
|
84 | + * @return WPInv_Invoice |
|
85 | + */ |
|
86 | + protected function get_ipn_invoice( $posted ) { |
|
87 | + |
|
88 | + wpinv_error_log( 'Retrieving PayPal IPN Response Invoice', false ); |
|
89 | + |
|
90 | + if ( ! empty( $posted['custom'] ) ) { |
|
91 | + $invoice = new WPInv_Invoice( $posted['custom'] ); |
|
92 | + |
|
93 | + if ( $invoice->exists() ) { |
|
94 | + wpinv_error_log( 'Found invoice #' . $invoice->get_number(), false ); |
|
95 | + return $invoice; |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + wpinv_error_log( 'Could not retrieve the associated invoice.', false ); |
|
100 | + wp_die( 'Could not retrieve the associated invoice.', 200 ); |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * Check PayPal IPN validity. |
|
105 | + */ |
|
106 | + protected function validate_ipn() { |
|
107 | + |
|
108 | + wpinv_error_log( 'Validating PayPal IPN response', false ); |
|
109 | + |
|
110 | + // Retrieve the associated invoice. |
|
111 | + $posted = wp_unslash( $_POST ); |
|
112 | + $invoice = $this->get_ipn_invoice( $posted ); |
|
113 | + |
|
114 | + if ( $this->gateway->is_sandbox( $invoice ) ) { |
|
115 | + wpinv_error_log( $posted, 'Invoice was processed in sandbox hence logging the posted data', false ); |
|
116 | + } |
|
117 | + |
|
118 | + // Validate the IPN. |
|
119 | + $posted['cmd'] = '_notify-validate'; |
|
120 | + |
|
121 | + // Send back post vars to paypal. |
|
122 | + $params = array( |
|
123 | + 'body' => $posted, |
|
124 | + 'timeout' => 60, |
|
125 | + 'httpversion' => '1.1', |
|
126 | + 'compress' => false, |
|
127 | + 'decompress' => false, |
|
128 | + 'user-agent' => 'GetPaid/' . WPINV_VERSION, |
|
129 | + ); |
|
130 | + |
|
131 | + // Post back to get a response. |
|
132 | + $response = wp_safe_remote_post( $this->gateway->is_sandbox( $invoice ) ? 'https://www.sandbox.paypal.com/cgi-bin/webscr' : 'https://www.paypal.com/cgi-bin/webscr', $params ); |
|
133 | + |
|
134 | + // Check to see if the request was valid. |
|
135 | + if ( ! is_wp_error( $response ) && $response['response']['code'] < 300 && strstr( $response['body'], 'VERIFIED' ) ) { |
|
136 | + wpinv_error_log( 'Received valid response from PayPal IPN: ' . $response['body'], false ); |
|
137 | + return true; |
|
138 | + } |
|
139 | + |
|
140 | + if ( is_wp_error( $response ) ) { |
|
141 | + wpinv_error_log( $response->get_error_message(), 'Received invalid response from PayPal IPN' ); |
|
142 | + return false; |
|
143 | + } |
|
144 | + |
|
145 | + wpinv_error_log( $response['body'], 'Received invalid response from PayPal IPN' ); |
|
146 | + return false; |
|
147 | + |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Check currency from IPN matches the invoice. |
|
152 | + * |
|
153 | + * @param WPInv_Invoice $invoice Invoice object. |
|
154 | + * @param string $currency currency to validate. |
|
155 | + */ |
|
156 | + protected function validate_ipn_currency( $invoice, $currency ) { |
|
157 | 157 | |
158 | - if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
158 | + if ( strtolower( $invoice->get_currency() ) !== strtolower( $currency ) ) { |
|
159 | 159 | |
160 | - /* translators: %s: currency code. */ |
|
161 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
160 | + /* translators: %s: currency code. */ |
|
161 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal currencies do not match (code %s).', 'invoicing' ), $currency ) ); |
|
162 | 162 | |
163 | - wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
164 | - } |
|
163 | + wpinv_error_log( "Currencies do not match: {$currency} instead of {$invoice->get_currency()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
164 | + } |
|
165 | 165 | |
166 | - wpinv_error_log( $currency, 'Validated IPN Currency', false ); |
|
167 | - } |
|
166 | + wpinv_error_log( $currency, 'Validated IPN Currency', false ); |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * Check payment amount from IPN matches the invoice. |
|
171 | - * |
|
172 | - * @param WPInv_Invoice $invoice Invoice object. |
|
173 | - * @param float $amount amount to validate. |
|
174 | - */ |
|
175 | - protected function validate_ipn_amount( $invoice, $amount ) { |
|
176 | - if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
169 | + /** |
|
170 | + * Check payment amount from IPN matches the invoice. |
|
171 | + * |
|
172 | + * @param WPInv_Invoice $invoice Invoice object. |
|
173 | + * @param float $amount amount to validate. |
|
174 | + */ |
|
175 | + protected function validate_ipn_amount( $invoice, $amount ) { |
|
176 | + if ( number_format( $invoice->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) { |
|
177 | 177 | |
178 | - /* translators: %s: Amount. */ |
|
179 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
178 | + /* translators: %s: Amount. */ |
|
179 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal amounts do not match (gross %s).', 'invoicing' ), $amount ) ); |
|
180 | 180 | |
181 | - wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
182 | - } |
|
181 | + wpinv_error_log( "Amounts do not match: {$amount} instead of {$invoice->get_total()}", 'IPN Error', __FILE__, __LINE__, true ); |
|
182 | + } |
|
183 | 183 | |
184 | - wpinv_error_log( $amount, 'Validated IPN Amount', false ); |
|
185 | - } |
|
184 | + wpinv_error_log( $amount, 'Validated IPN Amount', false ); |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Verify receiver email from PayPal. |
|
189 | - * |
|
190 | - * @param WPInv_Invoice $invoice Invoice object. |
|
191 | - * @param string $receiver_email Email to validate. |
|
192 | - */ |
|
193 | - protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
194 | - $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
187 | + /** |
|
188 | + * Verify receiver email from PayPal. |
|
189 | + * |
|
190 | + * @param WPInv_Invoice $invoice Invoice object. |
|
191 | + * @param string $receiver_email Email to validate. |
|
192 | + */ |
|
193 | + protected function validate_ipn_receiver_email( $invoice, $receiver_email ) { |
|
194 | + $paypal_email = wpinv_get_option( 'paypal_email' ); |
|
195 | 195 | |
196 | - if ( $receiver_email && strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
197 | - wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
196 | + if ( $receiver_email && strcasecmp( trim( $receiver_email ), trim( $paypal_email ) ) !== 0 ) { |
|
197 | + wpinv_record_gateway_error( 'IPN Error', "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}" ); |
|
198 | 198 | |
199 | - /* translators: %s: email address . */ |
|
200 | - $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
199 | + /* translators: %s: email address . */ |
|
200 | + $invoice->update_status( 'wpi-processing', sprintf( __( 'Validation error: PayPal IPN response from a different email address (%s).', 'invoicing' ), $receiver_email ) ); |
|
201 | 201 | |
202 | - return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
203 | - } |
|
202 | + return wpinv_error_log( "IPN Response is for another account: {$receiver_email}. Your email is {$paypal_email}", 'IPN Error', __FILE__, __LINE__, true ); |
|
203 | + } |
|
204 | 204 | |
205 | - wpinv_error_log( 'Validated PayPal Email', false ); |
|
206 | - } |
|
205 | + wpinv_error_log( 'Validated PayPal Email', false ); |
|
206 | + } |
|
207 | 207 | |
208 | - /** |
|
209 | - * Handles one time payments. |
|
210 | - * |
|
211 | - * @param WPInv_Invoice $invoice Invoice object. |
|
212 | - * @param array $posted Posted data. |
|
213 | - */ |
|
214 | - protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
208 | + /** |
|
209 | + * Handles one time payments. |
|
210 | + * |
|
211 | + * @param WPInv_Invoice $invoice Invoice object. |
|
212 | + * @param array $posted Posted data. |
|
213 | + */ |
|
214 | + protected function ipn_txn_web_accept( $invoice, $posted ) { |
|
215 | 215 | |
216 | - // Collect payment details |
|
217 | - $payment_status = strtolower( $posted['payment_status'] ); |
|
218 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
216 | + // Collect payment details |
|
217 | + $payment_status = strtolower( $posted['payment_status'] ); |
|
218 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
219 | 219 | |
220 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
221 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
220 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
221 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
222 | 222 | |
223 | - // Update the transaction id. |
|
224 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
225 | - $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
226 | - $invoice->save(); |
|
227 | - } |
|
223 | + // Update the transaction id. |
|
224 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
225 | + $invoice->set_transaction_id( wpinv_clean( $posted['txn_id'] ) ); |
|
226 | + $invoice->save(); |
|
227 | + } |
|
228 | 228 | |
229 | - $invoice->add_system_note( __( 'Processing invoice IPN', 'invoicing' ) ); |
|
229 | + $invoice->add_system_note( __( 'Processing invoice IPN', 'invoicing' ) ); |
|
230 | 230 | |
231 | - // Process a refund. |
|
232 | - if ( 'refunded' === $payment_status || 'reversed' === $payment_status ) { |
|
231 | + // Process a refund. |
|
232 | + if ( 'refunded' === $payment_status || 'reversed' === $payment_status ) { |
|
233 | 233 | |
234 | - update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
234 | + update_post_meta( $invoice->get_id(), 'refunded_remotely', 1 ); |
|
235 | 235 | |
236 | - if ( ! $invoice->is_refunded() ) { |
|
237 | - $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
238 | - } |
|
236 | + if ( ! $invoice->is_refunded() ) { |
|
237 | + $invoice->update_status( 'wpi-refunded', $posted['reason_code'] ); |
|
238 | + } |
|
239 | 239 | |
240 | - return wpinv_error_log( $posted['reason_code'], false ); |
|
241 | - } |
|
240 | + return wpinv_error_log( $posted['reason_code'], false ); |
|
241 | + } |
|
242 | 242 | |
243 | - // Process payments. |
|
244 | - if ( 'completed' === $payment_status ) { |
|
243 | + // Process payments. |
|
244 | + if ( 'completed' === $payment_status ) { |
|
245 | 245 | |
246 | - if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
247 | - return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
248 | - } |
|
246 | + if ( $invoice->is_paid() && 'wpi_processing' != $invoice->get_status() ) { |
|
247 | + return wpinv_error_log( 'Aborting, Invoice #' . $invoice->get_number() . ' is already paid.', false ); |
|
248 | + } |
|
249 | 249 | |
250 | - $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
250 | + $this->validate_ipn_amount( $invoice, $posted['mc_gross'] ); |
|
251 | 251 | |
252 | - $note = ''; |
|
252 | + $note = ''; |
|
253 | 253 | |
254 | - if ( ! empty( $posted['mc_fee'] ) ) { |
|
255 | - $note = sprintf( __( 'PayPal Transaction Fee %s.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
256 | - } |
|
254 | + if ( ! empty( $posted['mc_fee'] ) ) { |
|
255 | + $note = sprintf( __( 'PayPal Transaction Fee %s.', 'invoicing' ), sanitize_text_field( $posted['mc_fee'] ) ); |
|
256 | + } |
|
257 | 257 | |
258 | - if ( ! empty( $posted['payer_status'] ) ) { |
|
259 | - $note = ' ' . sprintf( __( 'Buyer status %s.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
260 | - } |
|
258 | + if ( ! empty( $posted['payer_status'] ) ) { |
|
259 | + $note = ' ' . sprintf( __( 'Buyer status %s.', 'invoicing' ), sanitize_text_field( $posted['payer_status'] ) ); |
|
260 | + } |
|
261 | 261 | |
262 | - $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
263 | - return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
262 | + $invoice->mark_paid( ( ! empty( $posted['txn_id'] ) ? sanitize_text_field( $posted['txn_id'] ) : '' ), trim( $note ) ); |
|
263 | + return wpinv_error_log( 'Invoice marked as paid.', false ); |
|
264 | 264 | |
265 | - } |
|
265 | + } |
|
266 | 266 | |
267 | - // Pending payments. |
|
268 | - if ( 'pending' === $payment_status ) { |
|
267 | + // Pending payments. |
|
268 | + if ( 'pending' === $payment_status ) { |
|
269 | 269 | |
270 | - /* translators: %s: pending reason. */ |
|
271 | - $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
270 | + /* translators: %s: pending reason. */ |
|
271 | + $invoice->update_status( 'wpi-onhold', sprintf( __( 'Payment pending (%s).', 'invoicing' ), $posted['pending_reason'] ) ); |
|
272 | 272 | |
273 | - return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
274 | - } |
|
273 | + return wpinv_error_log( 'Invoice marked as "payment held".', false ); |
|
274 | + } |
|
275 | 275 | |
276 | - /* translators: %s: payment status. */ |
|
277 | - $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
276 | + /* translators: %s: payment status. */ |
|
277 | + $invoice->update_status( 'wpi-failed', sprintf( __( 'Payment %s via IPN.', 'invoicing' ), sanitize_text_field( $posted['payment_status'] ) ) ); |
|
278 | 278 | |
279 | - } |
|
279 | + } |
|
280 | 280 | |
281 | - /** |
|
282 | - * Handles one time payments. |
|
283 | - * |
|
284 | - * @param WPInv_Invoice $invoice Invoice object. |
|
285 | - * @param array $posted Posted data. |
|
286 | - */ |
|
287 | - protected function ipn_txn_cart( $invoice, $posted ) { |
|
288 | - $this->ipn_txn_web_accept( $invoice, $posted ); |
|
289 | - } |
|
281 | + /** |
|
282 | + * Handles one time payments. |
|
283 | + * |
|
284 | + * @param WPInv_Invoice $invoice Invoice object. |
|
285 | + * @param array $posted Posted data. |
|
286 | + */ |
|
287 | + protected function ipn_txn_cart( $invoice, $posted ) { |
|
288 | + $this->ipn_txn_web_accept( $invoice, $posted ); |
|
289 | + } |
|
290 | 290 | |
291 | - /** |
|
292 | - * Handles subscription sign ups. |
|
293 | - * |
|
294 | - * @param WPInv_Invoice $invoice Invoice object. |
|
295 | - * @param array $posted Posted data. |
|
296 | - */ |
|
297 | - protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
291 | + /** |
|
292 | + * Handles subscription sign ups. |
|
293 | + * |
|
294 | + * @param WPInv_Invoice $invoice Invoice object. |
|
295 | + * @param array $posted Posted data. |
|
296 | + */ |
|
297 | + protected function ipn_txn_subscr_signup( $invoice, $posted ) { |
|
298 | 298 | |
299 | - wpinv_error_log( 'Processing subscription signup', false ); |
|
299 | + wpinv_error_log( 'Processing subscription signup', false ); |
|
300 | 300 | |
301 | - // Make sure the invoice has a subscription. |
|
302 | - $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
301 | + // Make sure the invoice has a subscription. |
|
302 | + $subscription = getpaid_get_invoice_subscription( $invoice ); |
|
303 | 303 | |
304 | - if ( empty( $subscription ) ) { |
|
305 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
306 | - } |
|
304 | + if ( empty( $subscription ) ) { |
|
305 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
306 | + } |
|
307 | 307 | |
308 | - wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
308 | + wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
309 | 309 | |
310 | - // Validate the IPN. |
|
311 | - $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
312 | - $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
313 | - $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
310 | + // Validate the IPN. |
|
311 | + $business_email = isset( $posted['business'] ) && is_email( $posted['business'] ) ? trim( $posted['business'] ) : trim( $posted['receiver_email'] ); |
|
312 | + $this->validate_ipn_receiver_email( $invoice, $business_email ); |
|
313 | + $this->validate_ipn_currency( $invoice, $posted['mc_currency'] ); |
|
314 | 314 | |
315 | - // Activate the subscription. |
|
316 | - $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
317 | - $subscription->set_date_created( current_time( 'mysql' ) ); |
|
318 | - $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
319 | - $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
320 | - $subscription->activate(); |
|
315 | + // Activate the subscription. |
|
316 | + $duration = strtotime( $subscription->get_expiration() ) - strtotime( $subscription->get_date_created() ); |
|
317 | + $subscription->set_date_created( current_time( 'mysql' ) ); |
|
318 | + $subscription->set_expiration( date( 'Y-m-d H:i:s', ( current_time( 'timestamp' ) + $duration ) ) ); |
|
319 | + $subscription->set_profile_id( sanitize_text_field( $posted['subscr_id'] ) ); |
|
320 | + $subscription->activate(); |
|
321 | 321 | |
322 | - // Set the transaction id. |
|
323 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
324 | - $invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), $posted['txn_id'] ), false, false, true ); |
|
325 | - $invoice->set_transaction_id( $posted['txn_id'] ); |
|
326 | - } |
|
322 | + // Set the transaction id. |
|
323 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
324 | + $invoice->add_note( sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), $posted['txn_id'] ), false, false, true ); |
|
325 | + $invoice->set_transaction_id( $posted['txn_id'] ); |
|
326 | + } |
|
327 | 327 | |
328 | - // Update the payment status. |
|
329 | - $invoice->mark_paid(); |
|
328 | + // Update the payment status. |
|
329 | + $invoice->mark_paid(); |
|
330 | 330 | |
331 | - $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ), $posted['subscr_id'] ), false, false, true ); |
|
331 | + $invoice->add_note( sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ), $posted['subscr_id'] ), false, false, true ); |
|
332 | 332 | |
333 | - wpinv_error_log( 'Subscription started.', false ); |
|
334 | - } |
|
333 | + wpinv_error_log( 'Subscription started.', false ); |
|
334 | + } |
|
335 | 335 | |
336 | - /** |
|
337 | - * Handles subscription renewals. |
|
338 | - * |
|
339 | - * @param WPInv_Invoice $invoice Invoice object. |
|
340 | - * @param array $posted Posted data. |
|
341 | - */ |
|
342 | - protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
336 | + /** |
|
337 | + * Handles subscription renewals. |
|
338 | + * |
|
339 | + * @param WPInv_Invoice $invoice Invoice object. |
|
340 | + * @param array $posted Posted data. |
|
341 | + */ |
|
342 | + protected function ipn_txn_subscr_payment( $invoice, $posted ) { |
|
343 | 343 | |
344 | - // Make sure the invoice has a subscription. |
|
345 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
344 | + // Make sure the invoice has a subscription. |
|
345 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
346 | 346 | |
347 | - if ( empty( $subscription ) ) { |
|
348 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
349 | - } |
|
347 | + if ( empty( $subscription ) ) { |
|
348 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
349 | + } |
|
350 | 350 | |
351 | - wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
351 | + wpinv_error_log( 'Found subscription #' . $subscription->get_id(), false ); |
|
352 | 352 | |
353 | - // PayPal sends a subscr_payment for the first payment too. |
|
354 | - $date_completed = getpaid_format_date( $invoice->get_date_completed() ); |
|
355 | - $date_created = getpaid_format_date( $invoice->get_date_created() ); |
|
356 | - $today_date = getpaid_format_date( current_time( 'mysql' ) ); |
|
357 | - $payment_date = getpaid_format_date( $posted['payment_date'] ); |
|
358 | - $subscribe_date = getpaid_format_date( $subscription->get_date_created() ); |
|
359 | - $dates = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) ); |
|
353 | + // PayPal sends a subscr_payment for the first payment too. |
|
354 | + $date_completed = getpaid_format_date( $invoice->get_date_completed() ); |
|
355 | + $date_created = getpaid_format_date( $invoice->get_date_created() ); |
|
356 | + $today_date = getpaid_format_date( current_time( 'mysql' ) ); |
|
357 | + $payment_date = getpaid_format_date( $posted['payment_date'] ); |
|
358 | + $subscribe_date = getpaid_format_date( $subscription->get_date_created() ); |
|
359 | + $dates = array_filter( compact( 'date_completed', 'date_created', 'subscribe_date' ) ); |
|
360 | 360 | |
361 | - foreach ( $dates as $date ) { |
|
361 | + foreach ( $dates as $date ) { |
|
362 | 362 | |
363 | - if ( $date !== $today_date && $date !== $payment_date ) { |
|
364 | - continue; |
|
365 | - } |
|
363 | + if ( $date !== $today_date && $date !== $payment_date ) { |
|
364 | + continue; |
|
365 | + } |
|
366 | 366 | |
367 | - if ( ! empty( $posted['txn_id'] ) ) { |
|
368 | - $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
369 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), sanitize_text_field( $posted['txn_id'] ) ), false, false, true ); |
|
370 | - } |
|
367 | + if ( ! empty( $posted['txn_id'] ) ) { |
|
368 | + $invoice->set_transaction_id( sanitize_text_field( $posted['txn_id'] ) ); |
|
369 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), sanitize_text_field( $posted['txn_id'] ) ), false, false, true ); |
|
370 | + } |
|
371 | 371 | |
372 | - return $invoice->mark_paid(); |
|
373 | - |
|
374 | - } |
|
372 | + return $invoice->mark_paid(); |
|
373 | + |
|
374 | + } |
|
375 | 375 | |
376 | - wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
377 | - |
|
378 | - // Abort if the payment is already recorded. |
|
379 | - if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
380 | - return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] . ' has already been processed', false ); |
|
381 | - } |
|
382 | - |
|
383 | - $args = array( |
|
384 | - 'transaction_id' => $posted['txn_id'], |
|
385 | - 'gateway' => $this->id, |
|
386 | - ); |
|
387 | - |
|
388 | - $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
376 | + wpinv_error_log( 'Processing subscription renewal payment for the invoice ' . $invoice->get_id(), false ); |
|
377 | + |
|
378 | + // Abort if the payment is already recorded. |
|
379 | + if ( wpinv_get_id_by_transaction_id( $posted['txn_id'] ) ) { |
|
380 | + return wpinv_error_log( 'Aborting, Transaction ' . $posted['txn_id'] . ' has already been processed', false ); |
|
381 | + } |
|
382 | + |
|
383 | + $args = array( |
|
384 | + 'transaction_id' => $posted['txn_id'], |
|
385 | + 'gateway' => $this->id, |
|
386 | + ); |
|
387 | + |
|
388 | + $invoice = wpinv_get_invoice( $subscription->add_payment( $args ) ); |
|
389 | 389 | |
390 | - if ( empty( $invoice ) ) { |
|
391 | - return; |
|
392 | - } |
|
390 | + if ( empty( $invoice ) ) { |
|
391 | + return; |
|
392 | + } |
|
393 | 393 | |
394 | - $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), $posted['txn_id'] ), false, false, true ); |
|
395 | - $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ), $posted['subscr_id'] ), false, false, true ); |
|
394 | + $invoice->add_note( wp_sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ), $posted['txn_id'] ), false, false, true ); |
|
395 | + $invoice->add_note( wp_sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ), $posted['subscr_id'] ), false, false, true ); |
|
396 | 396 | |
397 | - $subscription->renew(); |
|
398 | - wpinv_error_log( 'Subscription renewed.', false ); |
|
397 | + $subscription->renew(); |
|
398 | + wpinv_error_log( 'Subscription renewed.', false ); |
|
399 | 399 | |
400 | - } |
|
400 | + } |
|
401 | 401 | |
402 | - /** |
|
403 | - * Handles subscription cancelations. |
|
404 | - * |
|
405 | - * @param WPInv_Invoice $invoice Invoice object. |
|
406 | - */ |
|
407 | - protected function ipn_txn_subscr_cancel( $invoice ) { |
|
402 | + /** |
|
403 | + * Handles subscription cancelations. |
|
404 | + * |
|
405 | + * @param WPInv_Invoice $invoice Invoice object. |
|
406 | + */ |
|
407 | + protected function ipn_txn_subscr_cancel( $invoice ) { |
|
408 | 408 | |
409 | - // Make sure the invoice has a subscription. |
|
410 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
411 | - |
|
412 | - if ( empty( $subscription ) ) { |
|
413 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
414 | - } |
|
415 | - |
|
416 | - wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
417 | - $subscription->cancel(); |
|
418 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
409 | + // Make sure the invoice has a subscription. |
|
410 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
411 | + |
|
412 | + if ( empty( $subscription ) ) { |
|
413 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
414 | + } |
|
415 | + |
|
416 | + wpinv_error_log( 'Processing subscription cancellation for the invoice ' . $invoice->get_id(), false ); |
|
417 | + $subscription->cancel(); |
|
418 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
419 | 419 | |
420 | - } |
|
420 | + } |
|
421 | 421 | |
422 | - /** |
|
423 | - * Handles subscription completions. |
|
424 | - * |
|
425 | - * @param WPInv_Invoice $invoice Invoice object. |
|
426 | - * @param array $posted Posted data. |
|
427 | - */ |
|
428 | - protected function ipn_txn_subscr_eot( $invoice ) { |
|
422 | + /** |
|
423 | + * Handles subscription completions. |
|
424 | + * |
|
425 | + * @param WPInv_Invoice $invoice Invoice object. |
|
426 | + * @param array $posted Posted data. |
|
427 | + */ |
|
428 | + protected function ipn_txn_subscr_eot( $invoice ) { |
|
429 | 429 | |
430 | - // Make sure the invoice has a subscription. |
|
431 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
430 | + // Make sure the invoice has a subscription. |
|
431 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
432 | 432 | |
433 | - if ( empty( $subscription ) ) { |
|
434 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
435 | - } |
|
433 | + if ( empty( $subscription ) ) { |
|
434 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
435 | + } |
|
436 | 436 | |
437 | - wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
438 | - $subscription->complete(); |
|
439 | - wpinv_error_log( 'Subscription completed.', false ); |
|
437 | + wpinv_error_log( 'Processing subscription end of life for the invoice ' . $invoice->get_id(), false ); |
|
438 | + $subscription->complete(); |
|
439 | + wpinv_error_log( 'Subscription completed.', false ); |
|
440 | 440 | |
441 | - } |
|
441 | + } |
|
442 | 442 | |
443 | - /** |
|
444 | - * Handles subscription fails. |
|
445 | - * |
|
446 | - * @param WPInv_Invoice $invoice Invoice object. |
|
447 | - * @param array $posted Posted data. |
|
448 | - */ |
|
449 | - protected function ipn_txn_subscr_failed( $invoice ) { |
|
443 | + /** |
|
444 | + * Handles subscription fails. |
|
445 | + * |
|
446 | + * @param WPInv_Invoice $invoice Invoice object. |
|
447 | + * @param array $posted Posted data. |
|
448 | + */ |
|
449 | + protected function ipn_txn_subscr_failed( $invoice ) { |
|
450 | 450 | |
451 | - // Make sure the invoice has a subscription. |
|
452 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
451 | + // Make sure the invoice has a subscription. |
|
452 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
453 | 453 | |
454 | - if ( empty( $subscription ) ) { |
|
455 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
456 | - } |
|
454 | + if ( empty( $subscription ) ) { |
|
455 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
456 | + } |
|
457 | 457 | |
458 | - wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
459 | - $subscription->failing(); |
|
460 | - wpinv_error_log( 'Subscription marked as failing.', false ); |
|
458 | + wpinv_error_log( 'Processing subscription payment failure for the invoice ' . $invoice->get_id(), false ); |
|
459 | + $subscription->failing(); |
|
460 | + wpinv_error_log( 'Subscription marked as failing.', false ); |
|
461 | 461 | |
462 | - } |
|
462 | + } |
|
463 | 463 | |
464 | - /** |
|
465 | - * Handles subscription suspensions. |
|
466 | - * |
|
467 | - * @param WPInv_Invoice $invoice Invoice object. |
|
468 | - * @param array $posted Posted data. |
|
469 | - */ |
|
470 | - protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
464 | + /** |
|
465 | + * Handles subscription suspensions. |
|
466 | + * |
|
467 | + * @param WPInv_Invoice $invoice Invoice object. |
|
468 | + * @param array $posted Posted data. |
|
469 | + */ |
|
470 | + protected function ipn_txn_recurring_payment_suspended_due_to_max_failed_payment( $invoice ) { |
|
471 | 471 | |
472 | - // Make sure the invoice has a subscription. |
|
473 | - $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
472 | + // Make sure the invoice has a subscription. |
|
473 | + $subscription = getpaid_subscriptions()->get_invoice_subscription( $invoice ); |
|
474 | 474 | |
475 | - if ( empty( $subscription ) ) { |
|
476 | - return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
477 | - } |
|
478 | - |
|
479 | - wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
480 | - $subscription->cancel(); |
|
481 | - wpinv_error_log( 'Subscription cancelled.', false ); |
|
482 | - } |
|
475 | + if ( empty( $subscription ) ) { |
|
476 | + return wpinv_error_log( 'Aborting, Subscription for the invoice ' . $invoice->get_id() . ' not found', false ); |
|
477 | + } |
|
478 | + |
|
479 | + wpinv_error_log( 'Processing subscription cancellation due to max failed payment for the invoice ' . $invoice->get_id(), false ); |
|
480 | + $subscription->cancel(); |
|
481 | + wpinv_error_log( 'Subscription cancelled.', false ); |
|
482 | + } |
|
483 | 483 | |
484 | 484 | } |
@@ -13,464 +13,464 @@ discard block |
||
13 | 13 | */ |
14 | 14 | abstract class GetPaid_Payment_Gateway { |
15 | 15 | |
16 | - /** |
|
17 | - * Set if the place checkout button should be renamed on selection. |
|
18 | - * |
|
19 | - * @var string |
|
20 | - */ |
|
21 | - public $checkout_button_text; |
|
22 | - |
|
23 | - /** |
|
24 | - * Boolean whether the method is enabled. |
|
25 | - * |
|
26 | - * @var bool |
|
27 | - */ |
|
28 | - public $enabled = true; |
|
29 | - |
|
30 | - /** |
|
31 | - * Payment method id. |
|
32 | - * |
|
33 | - * @var string |
|
34 | - */ |
|
35 | - public $id; |
|
36 | - |
|
37 | - /** |
|
38 | - * Payment method order. |
|
39 | - * |
|
40 | - * @var int |
|
41 | - */ |
|
42 | - public $order = 10; |
|
43 | - |
|
44 | - /** |
|
45 | - * Payment method title for the frontend. |
|
46 | - * |
|
47 | - * @var string |
|
48 | - */ |
|
49 | - public $title; |
|
50 | - |
|
51 | - /** |
|
52 | - * Payment method description for the frontend. |
|
53 | - * |
|
54 | - * @var string |
|
55 | - */ |
|
56 | - public $description; |
|
57 | - |
|
58 | - /** |
|
59 | - * Gateway title. |
|
60 | - * |
|
61 | - * @var string |
|
62 | - */ |
|
63 | - public $method_title = ''; |
|
64 | - |
|
65 | - /** |
|
66 | - * Gateway description. |
|
67 | - * |
|
68 | - * @var string |
|
69 | - */ |
|
70 | - public $method_description = ''; |
|
71 | - |
|
72 | - /** |
|
73 | - * Countries this gateway is allowed for. |
|
74 | - * |
|
75 | - * @var array |
|
76 | - */ |
|
77 | - public $countries; |
|
78 | - |
|
79 | - /** |
|
80 | - * Currencies this gateway is allowed for. |
|
81 | - * |
|
82 | - * @var array |
|
83 | - */ |
|
84 | - public $currencies; |
|
85 | - |
|
86 | - /** |
|
87 | - * Currencies this gateway is not allowed for. |
|
88 | - * |
|
89 | - * @var array |
|
90 | - */ |
|
91 | - public $exclude_currencies; |
|
92 | - |
|
93 | - /** |
|
94 | - * Maximum transaction amount, zero does not define a maximum. |
|
95 | - * |
|
96 | - * @var int |
|
97 | - */ |
|
98 | - public $max_amount = 0; |
|
99 | - |
|
100 | - /** |
|
101 | - * Optional URL to view a transaction. |
|
102 | - * |
|
103 | - * @var string |
|
104 | - */ |
|
105 | - public $view_transaction_url = ''; |
|
106 | - |
|
107 | - /** |
|
108 | - * Optional URL to view a subscription. |
|
109 | - * |
|
110 | - * @var string |
|
111 | - */ |
|
112 | - public $view_subscription_url = ''; |
|
113 | - |
|
114 | - /** |
|
115 | - * Optional label to show for "new payment method" in the payment |
|
116 | - * method/token selection radio selection. |
|
117 | - * |
|
118 | - * @var string |
|
119 | - */ |
|
120 | - public $new_method_label = ''; |
|
121 | - |
|
122 | - /** |
|
123 | - * Contains a user's saved tokens for this gateway. |
|
124 | - * |
|
125 | - * @var array |
|
126 | - */ |
|
127 | - protected $tokens = array(); |
|
128 | - |
|
129 | - /** |
|
130 | - * An array of features that this gateway supports. |
|
131 | - * |
|
132 | - * @var array |
|
133 | - */ |
|
134 | - protected $supports = array(); |
|
135 | - |
|
136 | - /** |
|
137 | - * Class constructor. |
|
138 | - */ |
|
139 | - public function __construct() { |
|
140 | - |
|
141 | - do_action( 'getpaid_before_init_payment_gateway_' . $this->id, $this ); |
|
142 | - |
|
143 | - // Register gateway. |
|
144 | - add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
145 | - |
|
146 | - $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
147 | - |
|
148 | - // Add support for various features. |
|
149 | - foreach ( $this->supports as $feature ) { |
|
150 | - add_filter( "wpinv_{$this->id}_support_{$feature}", '__return_true' ); |
|
151 | - add_filter( "getpaid_{$this->id}_support_{$feature}", '__return_true' ); |
|
152 | - add_filter( "getpaid_{$this->id}_supports_{$feature}", '__return_true' ); |
|
153 | - } |
|
154 | - |
|
155 | - // Invoice addons. |
|
156 | - if ( $this->supports( 'addons' ) ) { |
|
157 | - add_action( "getpaid_process_{$this->id}_invoice_addons", array( $this, 'process_addons' ), 10, 2 ); |
|
158 | - } |
|
159 | - |
|
160 | - // Gateway settings. |
|
161 | - add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
162 | - |
|
163 | - // Gateway checkout fiellds. |
|
164 | - add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
165 | - |
|
166 | - // Process payment. |
|
167 | - add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
168 | - |
|
169 | - // Change the checkout button text. |
|
170 | - if ( ! empty( $this->checkout_button_text ) ) { |
|
171 | - add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
172 | - } |
|
173 | - |
|
174 | - // Check if a gateway is valid for a given currency. |
|
175 | - add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
176 | - |
|
177 | - // Generate the transaction url. |
|
178 | - add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
179 | - |
|
180 | - // Generate the subscription url. |
|
181 | - add_filter( 'getpaid_remote_subscription_profile_url', array( $this, 'generate_subscription_url' ), 10, 2 ); |
|
182 | - |
|
183 | - // Confirm payments. |
|
184 | - add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
185 | - |
|
186 | - // Verify IPNs. |
|
187 | - add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
188 | - |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Checks if this gateway is a given gateway. |
|
193 | - * |
|
194 | - * @since 1.0.19 |
|
195 | - * @return bool |
|
196 | - */ |
|
197 | - public function is( $gateway ) { |
|
198 | - return $gateway == $this->id; |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Returns a users saved tokens for this gateway. |
|
203 | - * |
|
204 | - * @since 1.0.19 |
|
205 | - * @return array |
|
206 | - */ |
|
207 | - public function get_tokens( $sandbox = null ) { |
|
208 | - |
|
209 | - if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
210 | - $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
211 | - |
|
212 | - if ( is_array( $tokens ) ) { |
|
213 | - $this->tokens = $tokens; |
|
214 | - } |
|
16 | + /** |
|
17 | + * Set if the place checkout button should be renamed on selection. |
|
18 | + * |
|
19 | + * @var string |
|
20 | + */ |
|
21 | + public $checkout_button_text; |
|
22 | + |
|
23 | + /** |
|
24 | + * Boolean whether the method is enabled. |
|
25 | + * |
|
26 | + * @var bool |
|
27 | + */ |
|
28 | + public $enabled = true; |
|
29 | + |
|
30 | + /** |
|
31 | + * Payment method id. |
|
32 | + * |
|
33 | + * @var string |
|
34 | + */ |
|
35 | + public $id; |
|
36 | + |
|
37 | + /** |
|
38 | + * Payment method order. |
|
39 | + * |
|
40 | + * @var int |
|
41 | + */ |
|
42 | + public $order = 10; |
|
43 | + |
|
44 | + /** |
|
45 | + * Payment method title for the frontend. |
|
46 | + * |
|
47 | + * @var string |
|
48 | + */ |
|
49 | + public $title; |
|
50 | + |
|
51 | + /** |
|
52 | + * Payment method description for the frontend. |
|
53 | + * |
|
54 | + * @var string |
|
55 | + */ |
|
56 | + public $description; |
|
57 | + |
|
58 | + /** |
|
59 | + * Gateway title. |
|
60 | + * |
|
61 | + * @var string |
|
62 | + */ |
|
63 | + public $method_title = ''; |
|
64 | + |
|
65 | + /** |
|
66 | + * Gateway description. |
|
67 | + * |
|
68 | + * @var string |
|
69 | + */ |
|
70 | + public $method_description = ''; |
|
71 | + |
|
72 | + /** |
|
73 | + * Countries this gateway is allowed for. |
|
74 | + * |
|
75 | + * @var array |
|
76 | + */ |
|
77 | + public $countries; |
|
78 | + |
|
79 | + /** |
|
80 | + * Currencies this gateway is allowed for. |
|
81 | + * |
|
82 | + * @var array |
|
83 | + */ |
|
84 | + public $currencies; |
|
85 | + |
|
86 | + /** |
|
87 | + * Currencies this gateway is not allowed for. |
|
88 | + * |
|
89 | + * @var array |
|
90 | + */ |
|
91 | + public $exclude_currencies; |
|
92 | + |
|
93 | + /** |
|
94 | + * Maximum transaction amount, zero does not define a maximum. |
|
95 | + * |
|
96 | + * @var int |
|
97 | + */ |
|
98 | + public $max_amount = 0; |
|
99 | + |
|
100 | + /** |
|
101 | + * Optional URL to view a transaction. |
|
102 | + * |
|
103 | + * @var string |
|
104 | + */ |
|
105 | + public $view_transaction_url = ''; |
|
106 | + |
|
107 | + /** |
|
108 | + * Optional URL to view a subscription. |
|
109 | + * |
|
110 | + * @var string |
|
111 | + */ |
|
112 | + public $view_subscription_url = ''; |
|
113 | + |
|
114 | + /** |
|
115 | + * Optional label to show for "new payment method" in the payment |
|
116 | + * method/token selection radio selection. |
|
117 | + * |
|
118 | + * @var string |
|
119 | + */ |
|
120 | + public $new_method_label = ''; |
|
121 | + |
|
122 | + /** |
|
123 | + * Contains a user's saved tokens for this gateway. |
|
124 | + * |
|
125 | + * @var array |
|
126 | + */ |
|
127 | + protected $tokens = array(); |
|
128 | + |
|
129 | + /** |
|
130 | + * An array of features that this gateway supports. |
|
131 | + * |
|
132 | + * @var array |
|
133 | + */ |
|
134 | + protected $supports = array(); |
|
135 | + |
|
136 | + /** |
|
137 | + * Class constructor. |
|
138 | + */ |
|
139 | + public function __construct() { |
|
140 | + |
|
141 | + do_action( 'getpaid_before_init_payment_gateway_' . $this->id, $this ); |
|
142 | + |
|
143 | + // Register gateway. |
|
144 | + add_filter( 'wpinv_payment_gateways', array( $this, 'register_gateway' ) ); |
|
145 | + |
|
146 | + $this->enabled = wpinv_is_gateway_active( $this->id ); |
|
147 | + |
|
148 | + // Add support for various features. |
|
149 | + foreach ( $this->supports as $feature ) { |
|
150 | + add_filter( "wpinv_{$this->id}_support_{$feature}", '__return_true' ); |
|
151 | + add_filter( "getpaid_{$this->id}_support_{$feature}", '__return_true' ); |
|
152 | + add_filter( "getpaid_{$this->id}_supports_{$feature}", '__return_true' ); |
|
153 | + } |
|
154 | + |
|
155 | + // Invoice addons. |
|
156 | + if ( $this->supports( 'addons' ) ) { |
|
157 | + add_action( "getpaid_process_{$this->id}_invoice_addons", array( $this, 'process_addons' ), 10, 2 ); |
|
158 | + } |
|
159 | + |
|
160 | + // Gateway settings. |
|
161 | + add_filter( "wpinv_gateway_settings_{$this->id}", array( $this, 'admin_settings' ) ); |
|
162 | + |
|
163 | + // Gateway checkout fiellds. |
|
164 | + add_action( "wpinv_{$this->id}_cc_form", array( $this, 'payment_fields' ), 10, 2 ); |
|
165 | + |
|
166 | + // Process payment. |
|
167 | + add_action( "getpaid_gateway_{$this->id}", array( $this, 'process_payment' ), 10, 3 ); |
|
168 | + |
|
169 | + // Change the checkout button text. |
|
170 | + if ( ! empty( $this->checkout_button_text ) ) { |
|
171 | + add_filter( "getpaid_gateway_{$this->id}_checkout_button_label", array( $this, 'rename_checkout_button' ) ); |
|
172 | + } |
|
173 | + |
|
174 | + // Check if a gateway is valid for a given currency. |
|
175 | + add_filter( "getpaid_gateway_{$this->id}_is_valid_for_currency", array( $this, 'validate_currency' ), 10, 2 ); |
|
176 | + |
|
177 | + // Generate the transaction url. |
|
178 | + add_filter( "getpaid_gateway_{$this->id}_transaction_url", array( $this, 'filter_transaction_url' ), 10, 2 ); |
|
179 | + |
|
180 | + // Generate the subscription url. |
|
181 | + add_filter( 'getpaid_remote_subscription_profile_url', array( $this, 'generate_subscription_url' ), 10, 2 ); |
|
182 | + |
|
183 | + // Confirm payments. |
|
184 | + add_filter( "wpinv_payment_confirm_{$this->id}", array( $this, 'confirm_payment' ), 10, 2 ); |
|
185 | + |
|
186 | + // Verify IPNs. |
|
187 | + add_action( "wpinv_verify_{$this->id}_ipn", array( $this, 'verify_ipn' ) ); |
|
188 | + |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Checks if this gateway is a given gateway. |
|
193 | + * |
|
194 | + * @since 1.0.19 |
|
195 | + * @return bool |
|
196 | + */ |
|
197 | + public function is( $gateway ) { |
|
198 | + return $gateway == $this->id; |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Returns a users saved tokens for this gateway. |
|
203 | + * |
|
204 | + * @since 1.0.19 |
|
205 | + * @return array |
|
206 | + */ |
|
207 | + public function get_tokens( $sandbox = null ) { |
|
208 | + |
|
209 | + if ( is_user_logged_in() && $this->supports( 'tokens' ) && 0 == count( $this->tokens ) ) { |
|
210 | + $tokens = get_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", true ); |
|
211 | + |
|
212 | + if ( is_array( $tokens ) ) { |
|
213 | + $this->tokens = $tokens; |
|
214 | + } |
|
215 | 215 | } |
216 | 216 | |
217 | - if ( ! is_bool( $sandbox ) ) { |
|
218 | - return $this->tokens; |
|
219 | - } |
|
220 | - |
|
221 | - // Filter tokens. |
|
222 | - $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
223 | - return wp_list_filter( $this->tokens, $args ); |
|
224 | - |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * Saves a token for this gateway. |
|
229 | - * |
|
230 | - * @since 1.0.19 |
|
231 | - */ |
|
232 | - public function save_token( $token ) { |
|
233 | - |
|
234 | - $tokens = $this->get_tokens(); |
|
235 | - $tokens[] = $token; |
|
236 | - |
|
237 | - update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
238 | - |
|
239 | - $this->tokens = $tokens; |
|
240 | - |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Return the title for admin screens. |
|
245 | - * |
|
246 | - * @return string |
|
247 | - */ |
|
248 | - public function get_method_title() { |
|
249 | - return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
250 | - } |
|
251 | - |
|
252 | - /** |
|
253 | - * Return the description for admin screens. |
|
254 | - * |
|
255 | - * @return string |
|
256 | - */ |
|
257 | - public function get_method_description() { |
|
258 | - return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
259 | - } |
|
260 | - |
|
261 | - /** |
|
262 | - * Get the success url. |
|
263 | - * |
|
264 | - * @param WPInv_Invoice $invoice Invoice object. |
|
265 | - * @return string |
|
266 | - */ |
|
267 | - public function get_return_url( $invoice ) { |
|
268 | - |
|
269 | - // Payment success url |
|
270 | - $return_url = add_query_arg( |
|
271 | - array( |
|
272 | - 'payment-confirm' => $this->id, |
|
273 | - 'invoice_key' => $invoice->get_key(), |
|
274 | - 'utm_nooverride' => 1, |
|
275 | - ), |
|
276 | - wpinv_get_success_page_uri() |
|
277 | - ); |
|
278 | - |
|
279 | - return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * Confirms payments when rendering the success page. |
|
284 | - * |
|
285 | - * @param string $content Success page content. |
|
286 | - * @return string |
|
287 | - */ |
|
288 | - public function confirm_payment( $content ) { |
|
289 | - |
|
290 | - // Retrieve the invoice. |
|
291 | - $invoice_id = getpaid_get_current_invoice_id(); |
|
292 | - $invoice = wpinv_get_invoice( $invoice_id ); |
|
293 | - |
|
294 | - // Ensure that it exists and that it is pending payment. |
|
295 | - if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
296 | - return $content; |
|
297 | - } |
|
298 | - |
|
299 | - // Can the user view this invoice?? |
|
300 | - if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
301 | - return $content; |
|
302 | - } |
|
303 | - |
|
304 | - // Show payment processing indicator. |
|
305 | - return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
306 | - } |
|
307 | - |
|
308 | - /** |
|
309 | - * Processes ipns and marks payments as complete. |
|
310 | - * |
|
311 | - * @return void |
|
312 | - */ |
|
313 | - public function verify_ipn() {} |
|
314 | - |
|
315 | - /** |
|
316 | - * Processes invoice addons. |
|
317 | - * |
|
318 | - * @param WPInv_Invoice $invoice |
|
319 | - * @param GetPaid_Form_Item[] $items |
|
320 | - * @return WPInv_Invoice |
|
321 | - */ |
|
322 | - public function process_addons( $invoice, $items ) { |
|
323 | - |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
328 | - * |
|
329 | - * @param string $transaction_url transaction url. |
|
330 | - * @param WPInv_Invoice $invoice Invoice object. |
|
331 | - * @return string transaction URL, or empty string. |
|
332 | - */ |
|
333 | - public function filter_transaction_url( $transaction_url, $invoice ) { |
|
334 | - |
|
335 | - $transaction_id = $invoice->get_transaction_id(); |
|
336 | - |
|
337 | - if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
338 | - $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
339 | - $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
340 | - $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
341 | - } |
|
342 | - |
|
343 | - return $transaction_url; |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
348 | - * |
|
349 | - * @param string $subscription_url transaction url. |
|
350 | - * @param WPInv_Subscription $subscription Subscription objectt. |
|
351 | - * @return string subscription URL, or empty string. |
|
352 | - */ |
|
353 | - public function generate_subscription_url( $subscription_url, $subscription ) { |
|
354 | - |
|
355 | - $profile_id = $subscription->get_profile_id(); |
|
356 | - |
|
357 | - if ( $this->id == $subscription->get_gateway() && ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
358 | - |
|
359 | - $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
360 | - $replace = $this->is_sandbox( $subscription->get_parent_invoice() ) ? 'sandbox' : ''; |
|
361 | - $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
362 | - |
|
363 | - } |
|
364 | - |
|
365 | - return $subscription_url; |
|
366 | - } |
|
367 | - |
|
368 | - /** |
|
369 | - * Check if the gateway is available for use. |
|
370 | - * |
|
371 | - * @return bool |
|
372 | - */ |
|
373 | - public function is_available() { |
|
374 | - return ! empty( $this->enabled ); |
|
375 | - } |
|
376 | - |
|
377 | - /** |
|
378 | - * Return the gateway's title. |
|
379 | - * |
|
380 | - * @return string |
|
381 | - */ |
|
382 | - public function get_title() { |
|
383 | - return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * Return the gateway's description. |
|
388 | - * |
|
389 | - * @return string |
|
390 | - */ |
|
391 | - public function get_description() { |
|
392 | - return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
393 | - } |
|
394 | - |
|
395 | - /** |
|
396 | - * Process Payment. |
|
397 | - * |
|
398 | - * |
|
399 | - * @param WPInv_Invoice $invoice Invoice. |
|
400 | - * @param array $submission_data Posted checkout fields. |
|
401 | - * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
402 | - * @return void |
|
403 | - */ |
|
404 | - public function process_payment( $invoice, $submission_data, $submission ) { |
|
405 | - // Process the payment then either redirect to the success page or the gateway. |
|
406 | - do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
407 | - } |
|
408 | - |
|
409 | - /** |
|
410 | - * Process refund. |
|
411 | - * |
|
412 | - * If the gateway declares 'refunds' support, this will allow it to refund. |
|
413 | - * a passed in amount. |
|
414 | - * |
|
415 | - * @param WPInv_Invoice $invoice Invoice. |
|
416 | - * @param float $amount Refund amount. |
|
417 | - * @param string $reason Refund reason. |
|
418 | - * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
419 | - */ |
|
420 | - public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
421 | - return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * Displays the payment fields, credit cards etc. |
|
426 | - * |
|
427 | - * @param int $invoice_id 0 or invoice id. |
|
428 | - * @param GetPaid_Payment_Form $form Current payment form. |
|
429 | - */ |
|
430 | - public function payment_fields( $invoice_id, $form ) { |
|
431 | - do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Filters the gateway settings. |
|
436 | - * |
|
437 | - * @param array $admin_settings |
|
438 | - */ |
|
439 | - public function admin_settings( $admin_settings ) { |
|
440 | - return $admin_settings; |
|
441 | - } |
|
442 | - |
|
443 | - /** |
|
444 | - * Retrieves the value of a gateway setting. |
|
445 | - * |
|
446 | - * @param string $option |
|
447 | - */ |
|
448 | - public function get_option( $option, $default = false ) { |
|
449 | - return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
450 | - } |
|
451 | - |
|
452 | - /** |
|
453 | - * Check if a gateway supports a given feature. |
|
454 | - * |
|
455 | - * Gateways should override this to declare support (or lack of support) for a feature. |
|
456 | - * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
457 | - * |
|
458 | - * @param string $feature string The name of a feature to test support for. |
|
459 | - * @return bool True if the gateway supports the feature, false otherwise. |
|
460 | - * @since 1.0.19 |
|
461 | - */ |
|
462 | - public function supports( $feature ) { |
|
463 | - return getpaid_payment_gateway_supports( $this->id, $feature ); |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * Returns the credit card form html. |
|
468 | - * |
|
469 | - * @param bool $save whether or not to display the save button. |
|
470 | - */ |
|
217 | + if ( ! is_bool( $sandbox ) ) { |
|
218 | + return $this->tokens; |
|
219 | + } |
|
220 | + |
|
221 | + // Filter tokens. |
|
222 | + $args = array( 'type' => $sandbox ? 'sandbox' : 'live' ); |
|
223 | + return wp_list_filter( $this->tokens, $args ); |
|
224 | + |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * Saves a token for this gateway. |
|
229 | + * |
|
230 | + * @since 1.0.19 |
|
231 | + */ |
|
232 | + public function save_token( $token ) { |
|
233 | + |
|
234 | + $tokens = $this->get_tokens(); |
|
235 | + $tokens[] = $token; |
|
236 | + |
|
237 | + update_user_meta( get_current_user_id(), "getpaid_{$this->id}_tokens", $tokens ); |
|
238 | + |
|
239 | + $this->tokens = $tokens; |
|
240 | + |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Return the title for admin screens. |
|
245 | + * |
|
246 | + * @return string |
|
247 | + */ |
|
248 | + public function get_method_title() { |
|
249 | + return apply_filters( 'getpaid_gateway_method_title', $this->method_title, $this ); |
|
250 | + } |
|
251 | + |
|
252 | + /** |
|
253 | + * Return the description for admin screens. |
|
254 | + * |
|
255 | + * @return string |
|
256 | + */ |
|
257 | + public function get_method_description() { |
|
258 | + return apply_filters( 'getpaid_gateway_method_description', $this->method_description, $this ); |
|
259 | + } |
|
260 | + |
|
261 | + /** |
|
262 | + * Get the success url. |
|
263 | + * |
|
264 | + * @param WPInv_Invoice $invoice Invoice object. |
|
265 | + * @return string |
|
266 | + */ |
|
267 | + public function get_return_url( $invoice ) { |
|
268 | + |
|
269 | + // Payment success url |
|
270 | + $return_url = add_query_arg( |
|
271 | + array( |
|
272 | + 'payment-confirm' => $this->id, |
|
273 | + 'invoice_key' => $invoice->get_key(), |
|
274 | + 'utm_nooverride' => 1, |
|
275 | + ), |
|
276 | + wpinv_get_success_page_uri() |
|
277 | + ); |
|
278 | + |
|
279 | + return apply_filters( 'getpaid_gateway_success_url', $return_url, $invoice, $this ); |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * Confirms payments when rendering the success page. |
|
284 | + * |
|
285 | + * @param string $content Success page content. |
|
286 | + * @return string |
|
287 | + */ |
|
288 | + public function confirm_payment( $content ) { |
|
289 | + |
|
290 | + // Retrieve the invoice. |
|
291 | + $invoice_id = getpaid_get_current_invoice_id(); |
|
292 | + $invoice = wpinv_get_invoice( $invoice_id ); |
|
293 | + |
|
294 | + // Ensure that it exists and that it is pending payment. |
|
295 | + if ( empty( $invoice_id ) || ! $invoice->needs_payment() ) { |
|
296 | + return $content; |
|
297 | + } |
|
298 | + |
|
299 | + // Can the user view this invoice?? |
|
300 | + if ( ! wpinv_user_can_view_invoice( $invoice ) ) { |
|
301 | + return $content; |
|
302 | + } |
|
303 | + |
|
304 | + // Show payment processing indicator. |
|
305 | + return wpinv_get_template_html( 'wpinv-payment-processing.php', compact( 'invoice' ) ); |
|
306 | + } |
|
307 | + |
|
308 | + /** |
|
309 | + * Processes ipns and marks payments as complete. |
|
310 | + * |
|
311 | + * @return void |
|
312 | + */ |
|
313 | + public function verify_ipn() {} |
|
314 | + |
|
315 | + /** |
|
316 | + * Processes invoice addons. |
|
317 | + * |
|
318 | + * @param WPInv_Invoice $invoice |
|
319 | + * @param GetPaid_Form_Item[] $items |
|
320 | + * @return WPInv_Invoice |
|
321 | + */ |
|
322 | + public function process_addons( $invoice, $items ) { |
|
323 | + |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * Get a link to the transaction on the 3rd party gateway site (if applicable). |
|
328 | + * |
|
329 | + * @param string $transaction_url transaction url. |
|
330 | + * @param WPInv_Invoice $invoice Invoice object. |
|
331 | + * @return string transaction URL, or empty string. |
|
332 | + */ |
|
333 | + public function filter_transaction_url( $transaction_url, $invoice ) { |
|
334 | + |
|
335 | + $transaction_id = $invoice->get_transaction_id(); |
|
336 | + |
|
337 | + if ( ! empty( $this->view_transaction_url ) && ! empty( $transaction_id ) ) { |
|
338 | + $transaction_url = sprintf( $this->view_transaction_url, $transaction_id ); |
|
339 | + $replace = $this->is_sandbox( $invoice ) ? 'sandbox' : ''; |
|
340 | + $transaction_url = str_replace( '{sandbox}', $replace, $transaction_url ); |
|
341 | + } |
|
342 | + |
|
343 | + return $transaction_url; |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Get a link to the subscription on the 3rd party gateway site (if applicable). |
|
348 | + * |
|
349 | + * @param string $subscription_url transaction url. |
|
350 | + * @param WPInv_Subscription $subscription Subscription objectt. |
|
351 | + * @return string subscription URL, or empty string. |
|
352 | + */ |
|
353 | + public function generate_subscription_url( $subscription_url, $subscription ) { |
|
354 | + |
|
355 | + $profile_id = $subscription->get_profile_id(); |
|
356 | + |
|
357 | + if ( $this->id == $subscription->get_gateway() && ! empty( $this->view_subscription_url ) && ! empty( $profile_id ) ) { |
|
358 | + |
|
359 | + $subscription_url = sprintf( $this->view_subscription_url, $profile_id ); |
|
360 | + $replace = $this->is_sandbox( $subscription->get_parent_invoice() ) ? 'sandbox' : ''; |
|
361 | + $subscription_url = str_replace( '{sandbox}', $replace, $subscription_url ); |
|
362 | + |
|
363 | + } |
|
364 | + |
|
365 | + return $subscription_url; |
|
366 | + } |
|
367 | + |
|
368 | + /** |
|
369 | + * Check if the gateway is available for use. |
|
370 | + * |
|
371 | + * @return bool |
|
372 | + */ |
|
373 | + public function is_available() { |
|
374 | + return ! empty( $this->enabled ); |
|
375 | + } |
|
376 | + |
|
377 | + /** |
|
378 | + * Return the gateway's title. |
|
379 | + * |
|
380 | + * @return string |
|
381 | + */ |
|
382 | + public function get_title() { |
|
383 | + return apply_filters( 'getpaid_gateway_title', $this->title, $this ); |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * Return the gateway's description. |
|
388 | + * |
|
389 | + * @return string |
|
390 | + */ |
|
391 | + public function get_description() { |
|
392 | + return apply_filters( 'getpaid_gateway_description', $this->description, $this ); |
|
393 | + } |
|
394 | + |
|
395 | + /** |
|
396 | + * Process Payment. |
|
397 | + * |
|
398 | + * |
|
399 | + * @param WPInv_Invoice $invoice Invoice. |
|
400 | + * @param array $submission_data Posted checkout fields. |
|
401 | + * @param GetPaid_Payment_Form_Submission $submission Checkout submission. |
|
402 | + * @return void |
|
403 | + */ |
|
404 | + public function process_payment( $invoice, $submission_data, $submission ) { |
|
405 | + // Process the payment then either redirect to the success page or the gateway. |
|
406 | + do_action( 'getpaid_process_invoice_payment_' . $this->id, $invoice, $submission_data, $submission ); |
|
407 | + } |
|
408 | + |
|
409 | + /** |
|
410 | + * Process refund. |
|
411 | + * |
|
412 | + * If the gateway declares 'refunds' support, this will allow it to refund. |
|
413 | + * a passed in amount. |
|
414 | + * |
|
415 | + * @param WPInv_Invoice $invoice Invoice. |
|
416 | + * @param float $amount Refund amount. |
|
417 | + * @param string $reason Refund reason. |
|
418 | + * @return WP_Error|bool True or false based on success, or a WP_Error object. |
|
419 | + */ |
|
420 | + public function process_refund( $invoice, $amount = null, $reason = '' ) { |
|
421 | + return apply_filters( 'getpaid_process_invoice_refund_' . $this->id, false, $invoice, $amount, $reason ); |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * Displays the payment fields, credit cards etc. |
|
426 | + * |
|
427 | + * @param int $invoice_id 0 or invoice id. |
|
428 | + * @param GetPaid_Payment_Form $form Current payment form. |
|
429 | + */ |
|
430 | + public function payment_fields( $invoice_id, $form ) { |
|
431 | + do_action( 'getpaid_getpaid_gateway_payment_fields_' . $this->id, $invoice_id, $form ); |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Filters the gateway settings. |
|
436 | + * |
|
437 | + * @param array $admin_settings |
|
438 | + */ |
|
439 | + public function admin_settings( $admin_settings ) { |
|
440 | + return $admin_settings; |
|
441 | + } |
|
442 | + |
|
443 | + /** |
|
444 | + * Retrieves the value of a gateway setting. |
|
445 | + * |
|
446 | + * @param string $option |
|
447 | + */ |
|
448 | + public function get_option( $option, $default = false ) { |
|
449 | + return wpinv_get_option( $this->id . '_' . $option, $default ); |
|
450 | + } |
|
451 | + |
|
452 | + /** |
|
453 | + * Check if a gateway supports a given feature. |
|
454 | + * |
|
455 | + * Gateways should override this to declare support (or lack of support) for a feature. |
|
456 | + * For backward compatibility, gateways support 'products' by default, but nothing else. |
|
457 | + * |
|
458 | + * @param string $feature string The name of a feature to test support for. |
|
459 | + * @return bool True if the gateway supports the feature, false otherwise. |
|
460 | + * @since 1.0.19 |
|
461 | + */ |
|
462 | + public function supports( $feature ) { |
|
463 | + return getpaid_payment_gateway_supports( $this->id, $feature ); |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * Returns the credit card form html. |
|
468 | + * |
|
469 | + * @param bool $save whether or not to display the save button. |
|
470 | + */ |
|
471 | 471 | public function get_cc_form( $save = false ) { |
472 | 472 | |
473 | - ob_start(); |
|
473 | + ob_start(); |
|
474 | 474 | |
475 | 475 | $id_prefix = esc_attr( uniqid( $this->id ) ); |
476 | 476 | |
@@ -488,7 +488,7 @@ discard block |
||
488 | 488 | '11' => __( 'November', 'invoicing' ), |
489 | 489 | '12' => __( 'December', 'invoicing' ), |
490 | 490 | ); |
491 | - $months = apply_filters( 'getpaid_cc_months', $months, $this ); |
|
491 | + $months = apply_filters( 'getpaid_cc_months', $months, $this ); |
|
492 | 492 | |
493 | 493 | $year = (int) current_time( 'Y' ); |
494 | 494 | $years = array(); |
@@ -497,7 +497,7 @@ discard block |
||
497 | 497 | $years[ $year + $i ] = $year + $i; |
498 | 498 | } |
499 | 499 | |
500 | - $years = apply_filters( 'getpaid_cc_years', $years, $this ); |
|
500 | + $years = apply_filters( 'getpaid_cc_years', $years, $this ); |
|
501 | 501 | |
502 | 502 | ?> |
503 | 503 | <div class="<?php echo esc_attr( $this->id ); ?>-cc-form getpaid-cc-form mt-1"> |
@@ -539,7 +539,7 @@ discard block |
||
539 | 539 | |
540 | 540 | <?php |
541 | 541 | foreach ( $months as $key => $month ) { |
542 | - echo "<option value='" . esc_attr( $key ) . "'>" . esc_html( $month ) . '</option>'; |
|
542 | + echo "<option value='" . esc_attr( $key ) . "'>" . esc_html( $month ) . '</option>'; |
|
543 | 543 | } |
544 | 544 | ?> |
545 | 545 | |
@@ -552,7 +552,7 @@ discard block |
||
552 | 552 | |
553 | 553 | <?php |
554 | 554 | foreach ( $years as $key => $year ) { |
555 | - echo "<option value='" . esc_attr( $key ) . "'>" . esc_html( $year ) . '</option>'; |
|
555 | + echo "<option value='" . esc_attr( $key ) . "'>" . esc_html( $year ) . '</option>'; |
|
556 | 556 | } |
557 | 557 | ?> |
558 | 558 | |
@@ -570,13 +570,13 @@ discard block |
||
570 | 570 | 'name' => $this->id . '[cc_cvv2]', |
571 | 571 | 'id' => "$id_prefix-cc-cvv2", |
572 | 572 | 'label' => __( 'CCV', 'invoicing' ), |
573 | - 'label_type' => 'vertical', |
|
574 | - 'class' => 'form-control-sm', |
|
575 | - 'extra_attributes' => array( |
|
576 | - 'autocomplete' => 'cc-csc', |
|
577 | - ), |
|
573 | + 'label_type' => 'vertical', |
|
574 | + 'class' => 'form-control-sm', |
|
575 | + 'extra_attributes' => array( |
|
576 | + 'autocomplete' => 'cc-csc', |
|
577 | + ), |
|
578 | 578 | ), |
579 | - true |
|
579 | + true |
|
580 | 580 | ); |
581 | 581 | ?> |
582 | 582 | </div> |
@@ -585,192 +585,192 @@ discard block |
||
585 | 585 | |
586 | 586 | <?php |
587 | 587 | |
588 | - if ( $save ) { |
|
589 | - $this->save_payment_method_checkbox(); |
|
590 | - } |
|
588 | + if ( $save ) { |
|
589 | + $this->save_payment_method_checkbox(); |
|
590 | + } |
|
591 | 591 | |
592 | - ?> |
|
592 | + ?> |
|
593 | 593 | </div> |
594 | 594 | |
595 | 595 | </div> |
596 | 596 | <?php |
597 | 597 | |
598 | - return ob_get_clean(); |
|
598 | + return ob_get_clean(); |
|
599 | + |
|
600 | + } |
|
601 | + |
|
602 | + /** |
|
603 | + * Displays a new payment method entry form. |
|
604 | + * |
|
605 | + * @since 1.0.19 |
|
606 | + */ |
|
607 | + public function new_payment_method_entry( $form ) { |
|
608 | + echo "<div class='getpaid-new-payment-method-form' style='display:none;'> " . wp_kses( $form, getpaid_allowed_html() ) . '</div>'; |
|
609 | + } |
|
610 | + |
|
611 | + /** |
|
612 | + * Grab and display our saved payment methods. |
|
613 | + * |
|
614 | + * @since 1.0.19 |
|
615 | + */ |
|
616 | + public function saved_payment_methods() { |
|
617 | + echo '<ul class="getpaid-saved-payment-methods list-unstyled m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
618 | + |
|
619 | + foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
620 | + $this->get_saved_payment_method_option_html( $token ); |
|
621 | + } |
|
622 | + |
|
623 | + $this->get_new_payment_method_option_html(); |
|
624 | + echo '</ul>'; |
|
599 | 625 | |
600 | 626 | } |
601 | 627 | |
602 | - /** |
|
603 | - * Displays a new payment method entry form. |
|
604 | - * |
|
605 | - * @since 1.0.19 |
|
606 | - */ |
|
607 | - public function new_payment_method_entry( $form ) { |
|
608 | - echo "<div class='getpaid-new-payment-method-form' style='display:none;'> " . wp_kses( $form, getpaid_allowed_html() ) . '</div>'; |
|
609 | - } |
|
610 | - |
|
611 | - /** |
|
612 | - * Grab and display our saved payment methods. |
|
613 | - * |
|
614 | - * @since 1.0.19 |
|
615 | - */ |
|
616 | - public function saved_payment_methods() { |
|
617 | - echo '<ul class="getpaid-saved-payment-methods list-unstyled m-0 mt-2" data-count="' . esc_attr( count( $this->get_tokens( $this->is_sandbox() ) ) ) . '">'; |
|
618 | - |
|
619 | - foreach ( $this->get_tokens( $this->is_sandbox() ) as $token ) { |
|
620 | - $this->get_saved_payment_method_option_html( $token ); |
|
621 | - } |
|
622 | - |
|
623 | - $this->get_new_payment_method_option_html(); |
|
624 | - echo '</ul>'; |
|
625 | - |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * Gets saved payment method HTML from a token. |
|
630 | - * |
|
631 | - * @since 1.0.19 |
|
632 | - * @param array $token Payment Token. |
|
633 | - * @return string Generated payment method HTML |
|
634 | - */ |
|
635 | - public function get_saved_payment_method_option_html( $token ) { |
|
636 | - |
|
637 | - printf( |
|
638 | - '<li class="getpaid-payment-method form-group mb-3"> |
|
628 | + /** |
|
629 | + * Gets saved payment method HTML from a token. |
|
630 | + * |
|
631 | + * @since 1.0.19 |
|
632 | + * @param array $token Payment Token. |
|
633 | + * @return string Generated payment method HTML |
|
634 | + */ |
|
635 | + public function get_saved_payment_method_option_html( $token ) { |
|
636 | + |
|
637 | + printf( |
|
638 | + '<li class="getpaid-payment-method form-group mb-3"> |
|
639 | 639 | <label> |
640 | 640 | <input name="getpaid-%1$s-payment-method" type="radio" value="%2$s" data-currency="%5$s" style="width:auto;" class="getpaid-saved-payment-method-token-input" %4$s /> |
641 | 641 | <span>%3$s</span> |
642 | 642 | </label> |
643 | 643 | </li>', |
644 | - esc_attr( $this->id ), |
|
645 | - esc_attr( $token['id'] ), |
|
646 | - esc_html( $token['name'] ), |
|
647 | - checked( empty( $token['default'] ), false, false ), |
|
648 | - empty( $token['currency'] ) ? 'none' : esc_attr( $token['currency'] ) |
|
649 | - ); |
|
650 | - |
|
651 | - } |
|
652 | - |
|
653 | - /** |
|
654 | - * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
655 | - * |
|
656 | - * @since 1.0.19 |
|
657 | - */ |
|
658 | - public function get_new_payment_method_option_html() { |
|
659 | - |
|
660 | - $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
661 | - |
|
662 | - printf( |
|
663 | - '<li class="getpaid-new-payment-method"> |
|
644 | + esc_attr( $this->id ), |
|
645 | + esc_attr( $token['id'] ), |
|
646 | + esc_html( $token['name'] ), |
|
647 | + checked( empty( $token['default'] ), false, false ), |
|
648 | + empty( $token['currency'] ) ? 'none' : esc_attr( $token['currency'] ) |
|
649 | + ); |
|
650 | + |
|
651 | + } |
|
652 | + |
|
653 | + /** |
|
654 | + * Displays a radio button for entering a new payment method (new CC details) instead of using a saved method. |
|
655 | + * |
|
656 | + * @since 1.0.19 |
|
657 | + */ |
|
658 | + public function get_new_payment_method_option_html() { |
|
659 | + |
|
660 | + $label = apply_filters( 'getpaid_new_payment_method_label', $this->new_method_label ? $this->new_method_label : __( 'Use a new payment method', 'invoicing' ), $this ); |
|
661 | + |
|
662 | + printf( |
|
663 | + '<li class="getpaid-new-payment-method"> |
|
664 | 664 | <label> |
665 | 665 | <input name="getpaid-%1$s-payment-method" type="radio" data-currency="none" value="new" style="width:auto;" /> |
666 | 666 | <span>%2$s</span> |
667 | 667 | </label> |
668 | 668 | </li>', |
669 | - esc_attr( $this->id ), |
|
670 | - esc_html( $label ) |
|
671 | - ); |
|
672 | - |
|
673 | - } |
|
674 | - |
|
675 | - /** |
|
676 | - * Outputs a checkbox for saving a new payment method to the database. |
|
677 | - * |
|
678 | - * @since 1.0.19 |
|
679 | - */ |
|
680 | - public function save_payment_method_checkbox() { |
|
681 | - |
|
682 | - aui()->input( |
|
683 | - array( |
|
684 | - 'type' => 'checkbox', |
|
685 | - 'name' => esc_attr( "getpaid-$this->id-new-payment-method" ), |
|
686 | - 'id' => esc_attr( uniqid( $this->id ) ), |
|
687 | - 'required' => false, |
|
688 | - 'label' => esc_html__( 'Save payment method', 'invoicing' ), |
|
689 | - 'value' => 'true', |
|
690 | - 'checked' => true, |
|
691 | - 'wrap_class' => 'getpaid-save-payment-method pt-1 pb-1', |
|
692 | - ), |
|
693 | - true |
|
694 | - ); |
|
695 | - |
|
696 | - } |
|
697 | - |
|
698 | - /** |
|
699 | - * Registers the gateway. |
|
700 | - * |
|
701 | - * @return array |
|
702 | - */ |
|
703 | - public function register_gateway( $gateways ) { |
|
704 | - |
|
705 | - $gateways[ $this->id ] = array( |
|
706 | - |
|
707 | - 'admin_label' => $this->method_title, |
|
669 | + esc_attr( $this->id ), |
|
670 | + esc_html( $label ) |
|
671 | + ); |
|
672 | + |
|
673 | + } |
|
674 | + |
|
675 | + /** |
|
676 | + * Outputs a checkbox for saving a new payment method to the database. |
|
677 | + * |
|
678 | + * @since 1.0.19 |
|
679 | + */ |
|
680 | + public function save_payment_method_checkbox() { |
|
681 | + |
|
682 | + aui()->input( |
|
683 | + array( |
|
684 | + 'type' => 'checkbox', |
|
685 | + 'name' => esc_attr( "getpaid-$this->id-new-payment-method" ), |
|
686 | + 'id' => esc_attr( uniqid( $this->id ) ), |
|
687 | + 'required' => false, |
|
688 | + 'label' => esc_html__( 'Save payment method', 'invoicing' ), |
|
689 | + 'value' => 'true', |
|
690 | + 'checked' => true, |
|
691 | + 'wrap_class' => 'getpaid-save-payment-method pt-1 pb-1', |
|
692 | + ), |
|
693 | + true |
|
694 | + ); |
|
695 | + |
|
696 | + } |
|
697 | + |
|
698 | + /** |
|
699 | + * Registers the gateway. |
|
700 | + * |
|
701 | + * @return array |
|
702 | + */ |
|
703 | + public function register_gateway( $gateways ) { |
|
704 | + |
|
705 | + $gateways[ $this->id ] = array( |
|
706 | + |
|
707 | + 'admin_label' => $this->method_title, |
|
708 | 708 | 'checkout_label' => $this->title, |
709 | - 'ordering' => $this->order, |
|
709 | + 'ordering' => $this->order, |
|
710 | 710 | |
711 | - ); |
|
711 | + ); |
|
712 | 712 | |
713 | - return $gateways; |
|
713 | + return $gateways; |
|
714 | 714 | |
715 | - } |
|
715 | + } |
|
716 | 716 | |
717 | - /** |
|
718 | - * Checks whether or not this is a sandbox request. |
|
719 | - * |
|
720 | - * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
721 | - * @return bool |
|
722 | - */ |
|
723 | - public function is_sandbox( $invoice = null ) { |
|
717 | + /** |
|
718 | + * Checks whether or not this is a sandbox request. |
|
719 | + * |
|
720 | + * @param WPInv_Invoice|null $invoice Invoice object or null. |
|
721 | + * @return bool |
|
722 | + */ |
|
723 | + public function is_sandbox( $invoice = null ) { |
|
724 | 724 | |
725 | - if ( is_a( $invoice, 'WPInv_Invoice' ) && ! $invoice->needs_payment() ) { |
|
726 | - return $invoice->get_mode() === 'test'; |
|
727 | - } |
|
725 | + if ( is_a( $invoice, 'WPInv_Invoice' ) && ! $invoice->needs_payment() ) { |
|
726 | + return $invoice->get_mode() === 'test'; |
|
727 | + } |
|
728 | 728 | |
729 | - return wpinv_is_test_mode( $this->id ); |
|
729 | + return wpinv_is_test_mode( $this->id ); |
|
730 | 730 | |
731 | - } |
|
731 | + } |
|
732 | 732 | |
733 | - /** |
|
734 | - * Renames the checkout button |
|
735 | - * |
|
736 | - * @return string |
|
737 | - */ |
|
738 | - public function rename_checkout_button() { |
|
739 | - return $this->checkout_button_text; |
|
740 | - } |
|
733 | + /** |
|
734 | + * Renames the checkout button |
|
735 | + * |
|
736 | + * @return string |
|
737 | + */ |
|
738 | + public function rename_checkout_button() { |
|
739 | + return $this->checkout_button_text; |
|
740 | + } |
|
741 | 741 | |
742 | - /** |
|
743 | - * Validate gateway currency |
|
744 | - * |
|
745 | - * @return bool |
|
746 | - */ |
|
747 | - public function validate_currency( $validation, $currency ) { |
|
742 | + /** |
|
743 | + * Validate gateway currency |
|
744 | + * |
|
745 | + * @return bool |
|
746 | + */ |
|
747 | + public function validate_currency( $validation, $currency ) { |
|
748 | 748 | |
749 | - // Required currencies. |
|
750 | - if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
751 | - return false; |
|
752 | - } |
|
749 | + // Required currencies. |
|
750 | + if ( ! empty( $this->currencies ) && ! in_array( $currency, $this->currencies ) ) { |
|
751 | + return false; |
|
752 | + } |
|
753 | 753 | |
754 | - // Excluded currencies. |
|
755 | - if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
756 | - return false; |
|
757 | - } |
|
754 | + // Excluded currencies. |
|
755 | + if ( ! empty( $this->exclude_currencies ) && in_array( $currency, $this->exclude_currencies ) ) { |
|
756 | + return false; |
|
757 | + } |
|
758 | 758 | |
759 | - return $validation; |
|
760 | - } |
|
759 | + return $validation; |
|
760 | + } |
|
761 | 761 | |
762 | - /** |
|
763 | - * Displays an error |
|
764 | - * |
|
765 | - */ |
|
766 | - public function show_error( $code, $message, $type ) { |
|
762 | + /** |
|
763 | + * Displays an error |
|
764 | + * |
|
765 | + */ |
|
766 | + public function show_error( $code, $message, $type ) { |
|
767 | 767 | |
768 | - if ( is_admin() ) { |
|
769 | - getpaid_admin()->{"show_$type"}( $message ); |
|
770 | - } |
|
768 | + if ( is_admin() ) { |
|
769 | + getpaid_admin()->{"show_$type"}( $message ); |
|
770 | + } |
|
771 | 771 | |
772 | - wpinv_set_error( $code, $message, $type ); |
|
772 | + wpinv_set_error( $code, $message, $type ); |
|
773 | 773 | |
774 | - } |
|
774 | + } |
|
775 | 775 | |
776 | 776 | } |