PluginProbe ʕ •ᴥ•ʔ
Flexible Checkout Fields for WooCommerce – WooCommerce Checkout Manager / 3.4.3
Flexible Checkout Fields for WooCommerce – WooCommerce Checkout Manager v3.4.3
4.1.38 trunk 1.2 1.2.1 1.2.2 1.2.3 1.3 1.3.1 1.3.2 1.3.3 1.4 1.5 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6 1.6.1 1.6.10 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7 1.7.1 1.7.2 1.8 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 1.9.2 1.9.3 2.0.0 2.0.1 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.4 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.6.0 2.7.0 2.7.1 2.8.0 2.9.0 2.9.1 2.9.2 3.0.0 3.0.1 3.0.10 3.0.11 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.10 3.5.11 3.5.12 3.5.13 3.5.14 3.5.15 3.5.16 3.5.17 3.5.18 3.5.19 3.5.2 3.5.3 3.5.4 3.5.5 3.5.6 3.5.7 3.5.8 3.5.9 3.6.0 4.0.0 4.1.0 4.1.1 4.1.10 4.1.11 4.1.12 4.1.13 4.1.14 4.1.15 4.1.16 4.1.17 4.1.18 4.1.19 4.1.2 4.1.20 4.1.21 4.1.22 4.1.23 4.1.24 4.1.25 4.1.26 4.1.27 4.1.28 4.1.29 4.1.3 4.1.30 4.1.31 4.1.32 4.1.34 4.1.35 4.1.36 4.1.37 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9
flexible-checkout-fields / classes / plugin.php
flexible-checkout-fields / classes Last commit date
activation-tracker.php 3 years ago display-options.php 3 years ago field-type-settings.php 3 years ago field.php 3 years ago filed-validation.php 3 years ago myaccount-edit-address.php 3 years ago myaccount-field-processor.php 3 years ago plugin.php 3 years ago settings.php 3 years ago tracker.php 3 years ago user-meta-checkout.php 3 years ago user-meta.php 3 years ago user-profile.php 3 years ago
plugin.php
950 lines
1 <?php
2
3 use WPDesk\FCF\Free\Field\Type\FileType;
4 use WPDesk\FCF\Free\Field\Type\MultiCheckboxType;
5 use WPDesk\FCF\Free\Field\Type\MultiSelectType;
6 use WPDesk\FCF\Free\Field\Type\TextareaType;
7 use WPDesk\FCF\Free\Plugin as PluginFree;
8
9 /**
10 * Class Plugin
11 *
12 * @package WPDesk\WooCommerceFakturownia
13 */
14 class Flexible_Checkout_Fields_Plugin extends \FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin {
15
16 /** @see validate_checkout method https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-checkout.php#L719 */
17 const FIELDS_REQUIREMENT_CONTROLLED_BY_WOOCOMMERCE = array(
18 'billing_country',
19 'shipping_country',
20 'billing_state',
21 'shipping_state',
22 'billing_postcode',
23 'shipping_postcode',
24 );
25
26 /**
27 * Scripts version.
28 *
29 * @var string
30 */
31 private $scripts_version = FLEXIBLE_CHECKOUT_FIELDS_VERSION . '.19';
32
33 protected $fields = array();
34
35 public $sections = array();
36
37 public $all_sections = array();
38
39 public $page_size = array();
40
41 public $field_validation;
42
43 /**
44 * Plugin path.
45 *
46 * @var string
47 */
48 private $plugin_path;
49
50 /**
51 * Plugin namespaces
52 *
53 * Fot backward compatibility
54 *
55 * @var string
56 */
57 public $plugin_namespace = 'inspire_checkout_fields';
58
59 /**
60 * Instance of new version main class of plugin.
61 *
62 * @var PluginFree
63 */
64 private $plugin_free;
65
66
67 /**
68 * Plugin constructor.
69 *
70 * @param \WPDesk_Plugin_Info $plugin_info Plugin info.
71 */
72 public function __construct( \FcfVendor\WPDesk_Plugin_Info $plugin_info ) {
73 parent::__construct( $plugin_info );
74 $this->plugin_info = $plugin_info;
75 $this->plugin_free = new PluginFree( $plugin_info, $this );
76 }
77
78 /**
79 * Init base variables for plugin
80 */
81 public function init_base_variables() {
82 $this->plugin_url = $this->plugin_info->get_plugin_url();
83 $this->plugin_path = $this->plugin_info->get_plugin_dir();
84 $this->template_path = $this->plugin_info->get_text_domain();
85 $this->settings_url = admin_url( 'admin.php?page=wc-settings&tab=integration&section=integration-fakturownia' );
86 $this->default_view_args = [ 'plugin_url' => $this->get_plugin_url() ];
87 $this->plugin_has_settings = false;
88 $this->plugin_namespace = 'inspire_checkout_fields';
89 }
90
91 /**
92 * Init.
93 */
94 public function init() {
95 $this->plugin_free->init();
96 $this->init_base_variables();
97 $this->load_dependencies();
98 $this->hooks();
99 }
100
101 /**
102 * Load dependencies.
103 */
104 private function load_dependencies() {
105 new WPDesk_Flexible_Checkout_Fields_Tracker();
106 require_once __DIR__ . '/settings.php';
107 }
108
109 /**
110 * Hooks.
111 */
112 public function hooks() {
113 $this->plugin_free->hooks();
114 parent::hooks();
115
116 $this->settings = new Flexible_Checkout_Fields_Settings( $this, self::FIELDS_REQUIREMENT_CONTROLLED_BY_WOOCOMMERCE );
117
118 add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 100 );
119
120 add_action( 'woocommerce_checkout_fields', array( $this, 'changeCheckoutFields' ), 9999 );
121 add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'updateCheckoutFields' ), 9, 2 );
122
123 add_action( 'woocommerce_admin_order_data_after_billing_address', array(
124 $this,
125 'addCustomBillingFieldsToAdmin'
126 ) );
127 add_action( 'woocommerce_admin_order_data_after_shipping_address', array(
128 $this,
129 'addCustomShippingFieldsToAdmin'
130 ) );
131 add_action( 'woocommerce_admin_order_data_after_shipping_address', array(
132 $this,
133 'addCustomOrderFieldsToAdmin'
134 ) );
135
136 add_action( 'woocommerce_billing_fields', array( $this, 'addCustomFieldsBillingFields' ), 9999 );
137 add_action( 'woocommerce_shipping_fields', array( $this, 'addCustomFieldsShippingFields' ), 9999 );
138 add_action( 'woocommerce_order_fields', array( $this, 'addCustomFieldsOrderFields' ), 9999 );
139
140
141 add_action( 'woocommerce_before_checkout_form', array( $this, 'woocommerce_before_checkout_form' ), 10 );
142 add_action( 'woocommerce_before_edit_address_form_shipping', array(
143 $this,
144 'woocommerce_before_checkout_form'
145 ), 10 );
146 add_action( 'woocommerce_before_edit_address_form_billing', array(
147 $this,
148 'woocommerce_before_checkout_form'
149 ), 10 );
150
151 add_filter( 'flexible_chekout_fields_fields', array( $this, 'getCheckoutFields' ), 10, 2 );
152
153 add_action( 'woocommerce_default_address_fields', array( $this, 'woocommerce_default_address_fields' ), 9999 );
154 add_filter( 'woocommerce_get_country_locale', array( $this, 'woocommerce_get_country_locale' ), 9999 );
155 add_filter( 'woocommerce_get_country_locale_base', array( $this, 'woocommerce_get_country_locale_base' ), 9999 );
156
157 add_action( 'woocommerce_get_country_locale_default', array( $this, 'woocommerce_get_country_locale_default' ), 11 );
158
159 add_filter( 'woocommerce_screen_ids', array( $this, 'add_woocommerce_screen_ids' ) );
160
161 new Flexible_Checkout_Fields_Disaplay_Options( $this );
162
163 $user_meta = new Flexible_Checkout_Fields_User_Meta( $this );
164
165 $user_profile = new Flexible_Checkout_Fields_User_Profile( $this, $user_meta );
166 $user_profile->hooks();
167
168 $user_meta = new Flexible_Checkout_Fields_User_Meta_Checkout( $this, $user_meta );
169 $user_meta->hooks();
170
171 $this->field_validation = new Flexible_Checkout_Fields_Field_Validation( $this );
172 $this->field_validation->hooks();
173
174 $my_account_fields_processor = new Flexible_Checkout_Fields_Myaccount_Field_Processor( $this );
175 $my_account_fields_processor->hooks();
176
177 $my_account_edit_address = new Flexible_Checkout_Fields_Myaccount_Edit_Address( $this );
178 $my_account_edit_address->hooks();
179
180 $plugin = $this;
181 add_filter( 'flexible_checkout_fields', static function() use( $plugin ) {
182 return $plugin;
183 });
184 }
185
186 /**
187 * Get plugin path.
188 *
189 * @return string
190 */
191 public function get_plugin_path() {
192 return $this->plugin_path;
193 }
194
195
196 /**
197 * Load plugin textdomain
198 */
199 public function load_plugin_text_domain() {
200 load_plugin_textdomain( 'wpdesk-plugin', false, $this->get_text_domain() . '/classes/wpdesk/lang/' );
201 load_plugin_textdomain( $this->get_text_domain(), false, $this->get_text_domain() . '/lang/' );
202 }
203
204 public function plugins_loaded() {
205 $this->init_fields();
206 //do użycia dla pola miasto, kod pocztowy i stan
207 $this->init_sections();
208 }
209
210 /**
211 * Get setting value.
212 *
213 * @param string $name Setting name.
214 * @param mixed $default Default setting value.
215 *
216 * @return mixed|void
217 */
218 public function get_setting_value( $name, $default = null ) {
219 return get_option( $this->get_namespace() . '_' . $name, $default );
220 }
221
222 /**
223 * Change params used by js locale woocommerce/assets/js/frontend/address-i18n.js so it would not overwrite backend settings.
224 *
225 * This is a locale for default country.
226 *
227 * @param array $base Local base.
228 *
229 * @return array
230 */
231 public function woocommerce_get_country_locale_base( $base ) {
232 $settings = $this->get_settings();
233
234 foreach ( $base as $key => $field ) {
235 unset( $base[ $key ]['placeholder'] );
236 unset( $base[ $key ]['label'] );
237 if ( version_compare( WC()->version, '4.4.1', '>=' ) ) {
238 unset( $base[ $key ]['class'] );
239 }
240
241 // field is force-required for given locale when FCF have shipping or billing field required
242 $shipping_key = 'shipping_' . $key;
243 $billing_key = 'billing_' . $key;
244 if ( ( isset( $settings['shipping'][ $shipping_key ] ) && $settings['shipping'][ $shipping_key ]['required'] )
245 || ( isset( $settings['billing'][ $billing_key ] ) && $settings['billing'][ $billing_key ]['required'] ) ) {
246 $base [ $key ]['required'] = true;
247 }
248 }
249
250 return $base;
251 }
252
253 /**
254 * Change params used by js locale woocommerce/assets/js/frontend/address-i18n.js so it would not overwrite backend settings
255 *
256 * @param array $locale Table of field settings per locale
257 *
258 * @return array
259 */
260 public function woocommerce_get_country_locale( $locale ) {
261 if ( is_checkout() || is_account_page() ) {
262 foreach ( $locale as $country => $fields ) {
263 foreach ( $fields as $field => &$settings ) {
264 unset( $locale[ $country ][ $field ]['priority'] );
265 unset( $locale[ $country ][ $field ]['label'] );
266 unset( $locale[ $country ][ $field ]['placeholder'] );
267 }
268 }
269 }
270
271 return $locale;
272 }
273
274 /**
275 * Remove priority from default address field
276 *
277 * @param array $fields Fields.
278 *
279 * @return array
280 */
281 public function woocommerce_default_address_fields( $fields ) {
282 if ( is_checkout() || is_account_page() ) {
283 foreach ( $fields as $key => $field ) {
284 unset( $fields[ $key ]['priority'] );
285 }
286 }
287
288 return $fields;
289 }
290
291 /**
292 * Init sections.
293 */
294 public function init_sections() {
295 $sections = array(
296 'billing' => array(
297 'section' => 'billing',
298 'tab' => 'fields_billing',
299 'tab_title' => __( 'Billing', 'flexible-checkout-fields' ),
300 'custom_section' => false,
301 'user_meta' => true,
302 ),
303 'shipping' => array(
304 'section' => 'shipping',
305 'tab' => 'fields_shipping',
306 'tab_title' => __( 'Shipping', 'flexible-checkout-fields' ),
307 'custom_section' => false,
308 'user_meta' => true,
309 ),
310 'order' => array(
311 'section' => 'order',
312 'tab' => 'fields_order',
313 'tab_title' => __( 'Order', 'flexible-checkout-fields' ),
314 'custom_section' => false,
315 'user_meta' => false,
316 ),
317 );
318
319 $all_sections = unserialize( serialize( $sections ) );
320
321 $this->sections = apply_filters( 'flexible_checkout_fields_sections', $sections );
322
323 $this->all_sections = apply_filters( 'flexible_checkout_fields_all_sections', $all_sections );
324 }
325
326 private function init_fields() {
327 $field_types = apply_filters( 'flexible_checkout_fields/field_types', [] );
328 foreach ( $field_types as $field_type ) {
329 if ( $field_type['is_hidden'] ) {
330 continue;
331 }
332
333 $this->fields[ $field_type['type'] ] = [
334 'name' => $field_type['label'],
335 ];
336 }
337 }
338
339 public function get_fields() {
340 return apply_filters( 'flexible_checkout_fields_fields', $this->fields );
341 }
342
343
344 /**
345 * Remove unavailable sections from settings.
346 * Removes sections added by PRO plugin, after PRO plugin disable.
347 *
348 * @param array $settings Settings.
349 *
350 * @return array
351 */
352 private function get_settings_for_available_sections( array $settings ) {
353 $this->init_sections();
354 if ( is_array( $settings ) && is_array( $this->sections ) ) {
355 foreach ( $settings as $section => $section_settings ) {
356 $unset = true;
357 foreach ( $this->sections as $section_data ) {
358 if ( $section_data['section'] === $section ) {
359 $unset = false;
360 }
361 }
362 if ( $unset ) {
363 unset( $settings[ $section ] );
364 }
365 }
366 }
367
368 return $settings;
369 }
370
371 /**
372 * Get settings.
373 *
374 * @return array
375 */
376 public function get_settings() {
377 $settings = get_option( 'inspire_checkout_fields_settings', array() );
378 if ( ! is_array( $settings ) ) {
379 $settings = array();
380 }
381
382 return $this->get_settings_for_available_sections( $settings );
383 }
384
385 public function woocommerce_before_checkout_form() {
386 WC()->session->set( 'checkout-fields', array() );
387 $settings = $this->get_settings();
388 $args = array( 'settings' => $settings );
389 include $this->plugin_path . '/views/before-checkout-form.php';
390 }
391
392
393 /**
394 * @param array $settings
395 * @param array $fields
396 * @param array $new
397 * @param null|string $request_type
398 *
399 * @return array
400 */
401 private function append_other_plugins_fields_to_checkout_fields( $settings, $fields, $new, $request_type ) {
402 if ( $request_type === null ) {
403 if ( ! empty( $fields ) && is_array( $fields ) ) {
404 foreach ( $fields as $section => $section_fields ) {
405 if ( ! empty( $section_fields ) && is_array( $section_fields ) ) {
406 foreach ( $section_fields as $key => $field ) {
407 if ( empty( $settings[ $section ][ $key ] ) ) {
408 $new[ $section ][ $key ] = $field;
409 }
410 }
411 }
412 }
413 }
414 } else {
415 foreach ( $fields as $key => $field ) {
416 if ( empty( $settings[ $request_type ][ $key ] ) ) {
417 $new[ $request_type ][ $key ] = $field;
418 }
419 }
420 }
421
422 return $new;
423 }
424
425 /**
426 * Is field requirement controlled by woocommerce.
427 *
428 * @param string $field_name .
429 *
430 * @return bool
431 */
432 private function is_field_requirement_controlled_by_woocommerce( $field_name ) {
433 return in_array( $field_name, self::FIELDS_REQUIREMENT_CONTROLLED_BY_WOOCOMMERCE, true );
434 }
435
436 /**
437 * @param array $fields
438 * @param null|string $request_type
439 *
440 * @return array
441 */
442 public function getCheckoutFields( $fields, $request_type = null ) {
443 $settings = $this->get_settings();
444
445 $checkout_field_type = $this->get_fields();
446 if ( ! empty( $settings ) ) {
447 $new = array();
448 if ( isset( $fields['account'] ) ) {
449 $new['account'] = array();
450 }
451 $priority = 0;
452 foreach ( $settings as $key => $type ) {
453
454 if ( $key !== 'billing' && $key !== 'shipping' && $key !== 'order' ) {
455 if ( get_option( 'inspire_checkout_fields_' . $key, '0' ) == '0' ) {
456 continue;
457 }
458 }
459 if ( ! is_array( $type ) ) {
460 continue;
461 }
462 if ( $request_type == null || $request_type == $key ) {
463 if ( ! isset( $new[ $key ] ) ) {
464 $new[ $key ] = array();
465 }
466 $fields_found = true;
467 foreach ( $type as $field_name => $field ) {
468 if ( apply_filters( 'flexible_checkout_fields_condition', true, $field ) ) {
469 if ( $field['visible'] == 0 or
470 ( ( isset( $_GET['page'] ) && $_GET['page'] == 'inspire_checkout_fields_settings' ) && $field['visible'] == 1 ) || $field['name'] == 'billing_country' || $field['name'] == 'shipping_country' ) {
471 $fcf_field = new Flexible_Checkout_Fields_Field( $field, $this );
472 $custom_field = $fcf_field->is_custom_field();
473 if ( isset( $fields[ $key ][ $field['name'] ] ) ) {
474 $new[ $key ][ $field['name'] ] = $fields[ $key ][ $field['name'] ];
475 } else {
476 $new[ $key ][ $field['name'] ] = $type[ $field['name'] ];
477 }
478
479 if ( ! $this->is_field_requirement_controlled_by_woocommerce( $field_name ) ) {
480 if ( 1 === intval( $field['required'] ?? 0 ) ) {
481 $new[ $key ][ $field['name'] ]['required'] = true;
482 } else {
483 $new[ $key ][ $field['name'] ]['required'] = false;
484 if ( isset( $new[ $key ][ $field['name'] ]['validate'] ) ) {
485 unset( $new[ $key ][ $field['name'] ]['validate'] );
486 }
487 }
488 } else {
489 if ( isset( $fields[ $key ][ $field['name'] ] ) ) {
490 $new[ $key ][ $field['name'] ]['required'] = $fields[ $key ][ $field['name'] ]['required'];
491 }
492 }
493 if ( isset( $field['label'] ) ) {
494 $new[ $key ][ $field['name'] ]['label'] = stripcslashes( wpdesk__( $field['label'], 'flexible-checkout-fields' ) );
495
496 // Support for fields rendered by WooCommerce
497 if ( isset( $field['type'] ) && in_array( $field['type'], array( 'text', 'textarea', 'select' ), true ) ) {
498 $new[ $key ][ $field['name'] ]['label'] = wp_kses_post( $new[ $key ][ $field['name'] ]['label'] );
499 }
500 }
501 if ( isset( $field['placeholder'] ) ) {
502 $new[ $key ][ $field['name'] ]['placeholder'] = wpdesk__( $field['placeholder'], 'flexible-checkout-fields' );
503 } else {
504 $new[ $key ][ $field['name'] ]['placeholder'] = '';
505 }
506 if ( isset( $field['class'] ) && ! is_array( $field['class'] ) ) {
507 $new[ $key ][ $field['name'] ]['class'] = explode( ' ', $field['class'] );
508 }
509 if ( ( $field['name'] == 'billing_country' || $field['name'] == 'shipping_country' ) && $field['visible'] == 1 ) {
510 $new[ $key ][ $field['name'] ]['class'][1] = "inspire_checkout_fields_hide";
511 }
512 if ( ! $custom_field ) {
513 if ( isset( $field['validation'] ) && $field['validation'] != '' ) {
514 if ( $field['validation'] == 'none' ) {
515 unset( $new[ $key ][ $field['name'] ]['validate'] );
516 } else {
517 $new[ $key ][ $field['name'] ]['validate'] = array( $field['validation'] );
518 }
519 }
520 } else {
521 if ( isset( $field['validation'] ) && $field['validation'] != 'none' ) {
522 $new[ $key ][ $field['name'] ]['validate'] = array( $field['validation'] );
523 }
524 }
525
526 if ( ! empty( $field['type'] ) ) {
527 $new[ $key ][ $field['name'] ]['type'] = $field['type'];
528 }
529
530 if ( $custom_field ) {
531 $new[ $key ][ $field['name'] ]['type'] = $field['type'] ?? '';
532 }
533
534 if ( '' !== $fcf_field->get_default() ) {
535 $new[ $key ][ $field['name'] ]['default'] = wpdesk__( $fcf_field->get_default(), 'flexible-checkout-fields' );
536 } elseif ( $field['options'] ?? [] ) {
537 $default = [];
538 foreach ( $field['options'] as $option ) {
539 if ( $option['default_checked'] ?? false ) {
540 $default[] = $option['key'];
541 }
542 }
543 $new[ $key ][ $field['name'] ]['default'] = $default;
544 }
545 }
546 }
547 }
548 }
549 }
550
551 $new = $this->append_other_plugins_fields_to_checkout_fields( $settings, $fields, $new, $request_type );
552
553 foreach ( $new as $type => $new_fields ) {
554 $priority = 0;
555 foreach ( $new_fields as $key => $field ) {
556 $priority += 10;
557 $new[ $type ][ $key ]['priority'] = $priority;
558 }
559 }
560
561 if ( $request_type == null ) {
562 if ( ! empty( $fields['account'] ) ) {
563 $new['account'] = $fields['account'];
564 }
565
566 $new = $this->restore_default_city_validation( $new, $_POST, 'billing' );
567 $new = $this->restore_default_city_validation( $new, $_POST, 'shipping' );
568
569 return $new;
570 }
571 if ( isset( $new[ $request_type ] ) ) {
572 $new = $this->restore_default_city_validation( $new, $_POST, $request_type );
573
574 return $new[ $request_type ];
575 } else {
576 return array();
577 }
578 } else {
579 return $fields;
580 }
581 }
582
583 /**
584 * Restores the default validation for the city
585 *
586 * @param array $fields Fields.
587 * @param array|null $request Request.
588 * @param string $request_type the type of shipping address (billing or shipping).
589 *
590 * @return array
591 */
592 private function restore_default_city_validation( array $fields, $request, $request_type ) {
593
594 if ( null === $request ) {
595 $request = array();
596 }
597
598 $city = $request_type . '_city';
599 $country = $request_type . '_country';
600
601 if ( isset( $fields[ $request_type ][ $city ]['required'] ) && isset( $request[ $country ] ) ) {
602 $slug = $request[ $country ];
603 $countries = new WC_Countries();
604 $locales = $countries->get_country_locale();
605 if ( isset( $locales[ $slug ]['city']['required'] ) ) {
606 $required = $locales[ $slug ]['city']['required'];
607 if ( ! $required ) {
608 $fields[ $request_type ][ $city ]['required'] = 0;
609 $fields[ $request_type ][ $city ]['hidden'] = 1;
610 }
611 }
612 }
613
614 return $fields;
615 }
616
617 public function getCheckoutUserFields( $fields, $request_type = null ) {
618 $settings = $this->get_settings();
619
620 $checkout_field_type = $this->get_fields();
621
622 $priority = 0;
623
624 if ( ! empty( $settings[ $request_type ] ) ) {
625 foreach ( $settings[ $request_type ] as $key => $field ) {
626
627 if ( $field['visible'] == 0 || $field['name'] === 'billing_country' || $field['name'] === 'shipping_country' || ( isset( $_GET['page'] ) && $_GET['page'] === 'inspire_checkout_fields_settings' && $field['visible'] == 1 ) ) {
628 if ( ! empty( $fields[ $key ] ) ) {
629 $new[ $key ] = $fields[ $key ];
630 }
631
632 if ( ! $this->is_field_requirement_controlled_by_woocommerce( $key ) ) {
633 if ( ( $field['required'] ?? 0 ) == 1 ) {
634 $new[ $key ]['required'] = true;
635 } else {
636 $new[ $key ]['required'] = false;
637 }
638 }
639
640 if ( isset( $field['label'] ) ) {
641 $new[ $key ]['label'] = wpdesk__( $field['label'], 'flexible-checkout-fields' );
642 }
643
644 if ( isset( $field['placeholder'] ) ) {
645 $new[ $key ]['placeholder'] = wpdesk__( $field['placeholder'], 'flexible-checkout-fields' );
646 } else {
647 $new[ $key ]['placeholder'] = '';
648 }
649
650 if ( isset( $field['class'] ) ) {
651 if ( is_array( $field['class'] ) ) {
652 $new[ $key ]['class'] = explode( ' ', esc_attr( implode( ' ', $field['class'] ) ) );
653 } else {
654 $new[ $key ]['class'] = explode( ' ', esc_attr( $field['class'] ) );
655 }
656 }
657
658 if ( ! empty( $field['name'] ) ) {
659 if ( ( $field['name'] === 'billing_country' || $field['name'] === 'shipping_country' ) && $field['visible'] == 1 ) {
660 $new[ $key ]['class'][] = "inspire_checkout_fields_hide";
661 }
662 }
663
664 if ( ! empty( $field['type'] ) ) {
665 $new[ $key ]['type'] = $field['type'];
666 }
667
668 $new[ $key ]['custom_attributes'] = apply_filters(
669 'flexible_checkout_fields_custom_attributes',
670 $field['custom_attributes'] ?? [],
671 $field
672 );
673 }
674 }
675
676 if ( count( $fields ) ) {
677 foreach ( $new as $key => $field ) {
678 if ( empty( $fields[ $key ] ) ) {
679 $new[ $key ]['custom_field'] = 1;
680 }
681 }
682 }
683
684 foreach ( $new as $key => $field ) {
685 $priority += 10;
686 $new[ $key ]['priority'] = $priority;
687 }
688
689 return $new;
690 } else {
691 return $fields;
692 }
693 }
694
695 public function printCheckoutFields( $order, $request_type = null ) {
696
697 $settings = $this->getCheckoutFields( $this->get_settings() );
698
699 $checkout_field_type = $this->get_fields();
700
701 if ( ! empty( $settings ) ) {
702 foreach ( $settings as $key => $type ) {
703 if ( $request_type == null || $request_type == $key ) {
704 $return = [];
705 foreach ( $type as $field ) {
706 if ( ( isset( $field['custom_field'] ) && $field['custom_field'] == 1 )
707 && ( empty( $field['type'] ) || ( ! empty( $checkout_field_type[ $field['type'] ] ) && empty( $checkout_field_type[ $field['type'] ]['exclude_in_admin'] ) ) )
708 ) {
709 if ( $value = wpdesk_get_order_meta( $order, '_' . $field['name'], true ) ) {
710 if ( isset( $field['type'] ) ) {
711 $value = apply_filters( 'flexible_checkout_fields_print_value', nl2br( $value ), $field );
712 }
713
714 $return[] = sprintf(
715 '<strong>%1$s</strong>: %2$s',
716 strip_tags( $field['label'] ),
717 wp_kses_post( $value )
718 );
719 }
720 }
721 }
722 }
723 }
724
725 if ( ! empty( $return ) ) {
726 echo '<div class="address_flexible_checkout_fields"><p class="form-field form-field-wide">' . implode( '<br />', $return ) . '</p></div>';
727 }
728 }
729 }
730
731 public function changeCheckoutFields( $fields ) {
732 return $this->getCheckoutFields( $fields );
733 }
734
735 public function changeShippingFields( $fields ) {
736 return $this->getCheckoutFields( $fields, 'shipping' );
737 }
738
739 public function changeBillingFields( $fields ) {
740 return $this->getCheckoutFields( $fields, 'billing' );
741 }
742
743 public function changeOrderFields( $fields ) {
744 return $this->getCheckoutFields( $fields, 'order' );
745 }
746
747 public function addCustomBillingFieldsToAdmin( $order ) {
748 $this->printCheckoutFields( $order, 'billing' );
749 }
750
751 public function addCustomShippingFieldsToAdmin( $order ) {
752 $this->printCheckoutFields( $order, 'shipping' );
753 }
754
755 public function addCustomOrderFieldsToAdmin( $order ) {
756 $this->printCheckoutFields( $order, 'order' );
757 }
758
759 public function addCustomFieldsBillingFields( $fields ) {
760 return $this->getCheckoutUserFields( $fields, 'billing' );
761 }
762
763 public function addCustomFieldsShippingFields( $fields ) {
764 return $this->getCheckoutUserFields( $fields, 'shipping' );
765 }
766
767 public function addCustomFieldsOrderFields( $fields ) {
768 return $this->getCheckoutUserFields( $fields, 'order' );
769 }
770
771 /**
772 * Update fields on checkout.
773 *
774 * @param int $order_id Order id.
775 * @param array $data Posted data.
776 */
777 function updateCheckoutFields( $order_id, $data ) {
778 $settings = $this->get_settings();
779 if ( ! empty( $settings ) ) {
780 $fields = [];
781 foreach ( $settings as $section_fields ) {
782 $fields += $section_fields;
783 }
784
785 foreach ( $data as $key => $value ) {
786 if ( isset( $fields[ $key ] ) ) {
787 $fcf_field = new Flexible_Checkout_Fields_Field( $fields[ $key ], $this );
788 if ( $fcf_field->is_custom_field() ) {
789 if ( in_array( $fcf_field->get_type(), [ TextareaType::FIELD_TYPE ] ) ) {
790 update_post_meta( $order_id, '_' . $key, sanitize_textarea_field( wp_unslash( $value ) ) );
791 } elseif ( in_array( $fcf_field->get_type(), [ MultiCheckboxType::FIELD_TYPE, MultiSelectType::FIELD_TYPE, FileType::FIELD_TYPE ] ) ) {
792 update_post_meta( $order_id, '_' . $key, json_encode( wp_unslash( $value ) ) );
793 } else {
794 update_post_meta( $order_id, '_' . $key, sanitize_text_field( wp_unslash( $value ) ) );
795 }
796 }
797 }
798 }
799 }
800
801 do_action( 'flexible_checkout_fields_checkout_update_order_meta', $order_id, $data );
802 }
803
804 public static function flexible_checkout_fields_section_settings( $key, $settings ) {
805 echo 1;
806 }
807
808 public function woocommerce_get_country_locale_default( $address_fields ) {
809 return $address_fields;
810 }
811
812 /**
813 * Add woocommerce screen ids.
814 *
815 * @param array $screen_ids Screen ids.
816 *
817 * @return array
818 */
819 public function add_woocommerce_screen_ids( $screen_ids ) {
820 $screen_ids[] = 'woocommerce_page_inspire_checkout_fields_settings';
821
822 return $screen_ids;
823 }
824
825 /**
826 * Admin enqueue scripts.
827 */
828 public function admin_enqueue_scripts() {
829 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
830 if (function_exists('get_current_screen')) {
831 $current_screen = get_current_screen();
832 }
833
834 $deps = array(
835 'jquery',
836 'jquery-ui-sortable',
837 'jquery-ui-tooltip',
838 'jquery-ui-datepicker',
839 );
840 wp_enqueue_script( 'inspire_checkout_fields_admin_js', trailingslashit( $this->get_plugin_assets_url() ) . 'js/admin' . $suffix . '.js', $deps, $this->scripts_version );
841 }
842
843 /**
844 * Frontend enqueue scripts.
845 */
846 public function wp_enqueue_scripts() {
847 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
848 if ( is_checkout() || is_account_page() ) {
849 if ( $this->get_setting_value( 'css_disable' ) != 1 ) {
850 wp_enqueue_style( 'jquery-ui-style', trailingslashit( $this->get_plugin_assets_url() ) . 'css/jquery-ui' . $suffix . '.css', array(), $this->scripts_version );
851 }
852
853 wp_enqueue_style( 'inspire_checkout_fields_public_style', trailingslashit( $this->get_plugin_assets_url() ) . 'css/front' . $suffix . '.css', array(), $this->scripts_version );
854 }
855 if ( is_checkout() || is_account_page() ) {
856 add_action( 'wp_enqueue_scripts', array( $this, 'wp_localize_jquery_ui_datepicker' ), 1000 );
857
858 $deps = array(
859 'jquery',
860 'jquery-ui-datepicker',
861 );
862 wp_register_script( 'inspire_checkout_fields_checkout_js', trailingslashit( $this->get_plugin_assets_url() ) . 'js/checkout' . $suffix . '.js', $deps, $this->scripts_version );
863 $translation_array = array(
864 'uploading' => __( 'Uploading file...', 'flexible-checkout-fields' ),
865 );
866 wp_localize_script( 'inspire_checkout_fields_checkout_js', 'words', $translation_array );
867 wp_enqueue_script( 'inspire_checkout_fields_checkout_js' );
868 wp_enqueue_script( 'jquery-ui-datepicker' );
869 }
870 }
871
872
873 function wp_localize_jquery_ui_datepicker() {
874 global $wp_locale;
875 global $wp_version;
876
877 if ( ! wp_script_is( 'jquery-ui-datepicker', 'enqueued' ) || version_compare( $wp_version, '4.6' ) != - 1 ) {
878 return;
879 }
880
881 // Convert the PHP date format into jQuery UI's format.
882 $datepicker_date_format = str_replace(
883 array(
884 'd',
885 'j',
886 'l',
887 'z', // Day.
888 'F',
889 'M',
890 'n',
891 'm', // Month.
892 'Y',
893 'y' // Year.
894 ),
895 array(
896 'dd',
897 'd',
898 'DD',
899 'o',
900 'MM',
901 'M',
902 'm',
903 'mm',
904 'yy',
905 'y'
906 ),
907 get_option( 'date_format' )
908 );
909
910 $datepicker_defaults = wp_json_encode( array(
911 'closeText' => __( 'Close' ),
912 'currentText' => __( 'Today' ),
913 'monthNames' => array_values( $wp_locale->month ),
914 'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
915 'nextText' => __( 'Next' ),
916 'prevText' => __( 'Previous' ),
917 'dayNames' => array_values( $wp_locale->weekday ),
918 'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ),
919 'dayNamesMin' => array_values( $wp_locale->weekday_initial ),
920 'dateFormat' => $datepicker_date_format,
921 'firstDay' => absint( get_option( 'start_of_week' ) ),
922 'isRTL' => $wp_locale->is_rtl(),
923 ) );
924
925 wp_add_inline_script( 'jquery-ui-datepicker', "jQuery(document).ready(function(jQuery){jQuery.datepicker.setDefaults({$datepicker_defaults});});" );
926 }
927
928 /**
929 * Links filter.
930 *
931 * @param array $links Links.
932 *
933 * @return array
934 */
935 public function links_filter( $links ) {
936 $plugin_links = array(
937 '<a href="' . admin_url( 'admin.php?page=inspire_checkout_fields_settings' ) . '">' . __( 'Settings', 'flexible-checkout-fields' ) . '</a>',
938 '<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-settings-row-action-docs' ) ) . '" target="_blank">' . __( 'Docs', 'flexible-checkout-fields' ) . '</a>',
939 '<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-settings-row-action-support' ) ) . '" target="_blank">' . __( 'Support', 'flexible-checkout-fields' ) . '</a>',
940 );
941
942 if ( ! wpdesk_is_plugin_active( 'flexible-checkout-fields-pro/flexible-checkout-fields-pro.php' ) ) {
943 $plugin_links[] = '<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-settings-row-action-upgrade' ) ) . '" target="_blank" style="color:#d64e07;font-weight:bold;">' . __( 'Upgrade', 'flexible-checkout-fields' ) . '</a>';
944 }
945
946 return array_merge( $plugin_links, $links );
947 }
948
949 }
950