first commit

This commit is contained in:
2024-11-10 21:08:49 +01:00
commit 0d932ce5ee
14455 changed files with 2567501 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
class Flexible_Checkout_Fields_Activation_Tracker {
/** @var string */
private $namespace;
/**
* Flexible_Checkout_Fields_Activation_Tracker constructor.
*
* @param $namespace string for settings
*/
public function __construct( $namespace ) {
$this->namespace = $namespace;
}
/**
* Option name for date storage
*
* @return string
*/
private function get_option_name_activation_date() {
return $this->namespace . '_activation';
}
/**
* Returns activation date and sets it if were not set before
*
* @return int unix timestamp for activation datetime
*/
public function get_activation_date() {
$activation_date
= get_option( $this->get_option_name_activation_date() );
if ( empty( $activation_date ) ) {
return $this->touch_activation_date();
}
return intval( $activation_date );
}
/**
* Was activation more than two weeks before today
*
* @return bool
*/
public function is_activated_more_than_two_weeks() {
$two_weeks = 60 * 60 * 24 * 7 * 2;
return $this->get_activation_date() + $two_weeks < time();
}
/**
* Sets activatiion date for today
*
* @return int unit timestamp for now
*/
public function touch_activation_date() {
$now = time();
update_option( $this->get_option_name_activation_date(), $now );
return $now;
}
}

View File

@@ -0,0 +1,478 @@
<?php
class Flexible_Checkout_Fields_Disaplay_Options {
const HOOK_PRIORITY_LAST = 999999;
const DISPLAY_ON_ADDRESS = 'display_on_address';
const DISPLAY_ON_THANK_YOU = 'display_on_thank_you';
const DISPLAY_ON_ORDER = 'display_on_order';
const DISPLAY_ON_EMAILS = 'display_on_emails';
protected $plugin;
protected $current_address_type = 'shipping';
protected $in_email_address = false;
/**
* Flexible_Checkout_Fields_Disaplay_Options constructor.
*
* @param Flexible_Checkout_Fields_Plugin $plugin
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
$this->hooks();
}
/**
*
*/
protected function hooks() {
add_filter( 'woocommerce_localisation_address_formats', array( $this, 'woocommerce_localisation_address_formats_filter' ), self::HOOK_PRIORITY_LAST );
add_filter( 'woocommerce_formatted_address_replacements', array( $this, 'woocommerce_formatted_address_replacements' ), self::HOOK_PRIORITY_LAST, 2 );
add_filter( 'woocommerce_order_formatted_billing_address', array( $this, 'woocommerce_order_formatted_billing_address' ), self::HOOK_PRIORITY_LAST, 2 );
add_filter( 'woocommerce_order_formatted_shipping_address', array( $this, 'woocommerce_order_formatted_shipping_address' ), self::HOOK_PRIORITY_LAST, 2 );
// addresses in my account
add_filter( 'woocommerce_my_account_my_address_formatted_address', array( $this, 'woocommerce_my_account_my_address_formatted_address' ), 10, 3 );
add_action( 'woocommerce_billing_fields', array($this, 'woocommerce_billing_fields'), 19999 );
add_action( 'woocommerce_shipping_fields', array($this, 'woocommerce_shipping_fields'), 19999 );
add_action( 'woocommerce_email_customer_details', array( $this, 'woocommerce_email_customer_details_start' ), 10 );
add_action( 'woocommerce_email_customer_details', array( $this, 'woocommerce_email_customer_details_end' ), 10000 );
// additional fields
add_action( 'woocommerce_thankyou', array( $this, 'additional_information_fields' ), 75 );
add_action( 'woocommerce_email_order_meta', array( $this, 'email_additional_information_fields' ), 195 );
add_action( 'woocommerce_view_order', array( $this, 'additional_information_fields' ), 195 );
add_action( 'woocommerce_edit_account_form', array( $this, 'woocommerce_edit_account_form' ) );
}
public function woocommerce_edit_account_form() {
}
public function email_additional_information_fields( $order ) {
$this->in_email_address = true;
$this->additional_information_fields( wpdesk_get_order_id( $order ) );
$this->in_email_address = false;
}
/**
* Displays additional fields.
*
* @param int $order_id Order id.
*/
public function additional_information_fields( $order_id ) {
$settings = $this->plugin->getCheckoutFields( $this->plugin->get_settings() );
$checkout_field_type = $this->plugin->get_fields();
if ( ! empty( $settings ) && is_array( $settings ) ) {
$return = array();
foreach ( $settings as $key => $type ) {
if ( in_array( $key, array( 'billing', 'shipping' ), true ) ) {
continue;
}
if ( isset( $type ) && is_array( $type ) ) {
foreach ( $type as $field ) {
if ( isset( $field['visible'] ) && 0 === intval( $field['visible'] ) && isset( $field['custom_field'] ) && 1 === intval( $field['custom_field'] ) ) {
$value = wpdesk_get_order_meta( $order_id, '_' . $field['name'], true );
if ( $this->is_field_displayable( $field ) && '' !== $value ) {
if ( ! empty( $checkout_field_type[ $field['type'] ]['has_options'] ) ) {
$options = $field['options'];
if ( isset( $options[ $value ] ) ) {
$value = $options[ $value ];
}
}
$value = apply_filters( 'flexible_checkout_fields_print_value', $value, $field );
if ( '' !== $value ) {
$return[] = strip_tags( wpdesk__( $field['label'], 'flexible-checkout-fields' ) ) . ': ' . esc_html( $value );
}
}
}
}
}
}
if ( count( $return ) > 0 ) {
echo '<div class="inspire_checkout_fields_additional_information">';
echo '<h3>' . __( 'Additional Information', 'flexible-checkout-fields' ) . '</h3>';
echo '<p>' . implode( '<br />', $return ) . '</p>';
echo '</div>';
}
}
}
public function woocommerce_email_customer_details_start() {
$this->in_email_address = true;
}
public function woocommerce_email_customer_details_end() {
$this->in_email_address = false;
}
public function woocommerce_my_account_my_address_formatted_address( $address, $customer_id, $address_type ) {
$checkout_field_type = $this->plugin->get_fields();
$this->current_address_type = $address_type;
WC()->countries->address_formats = '';
$cf_fields = $this->getCheckoutFields( array(), $address_type );
$is_empty_address = $this->check_if_address_is_empty ( $address );
foreach ( $cf_fields as $field_key => $field ) {
$fcf_field = new Flexible_Checkout_Fields_Field( $field, $this->plugin);
if ( !isset( $address[$field['name']] ) ) {
$val = '';
if ( $fcf_field->is_custom_field() && $fcf_field->get_display_on_option_show_label() === '1' ) {
$val .= strip_tags( wpdesk__( $field['label'], 'flexible-checkout-fields' ) ) . ': ';
}
$meta_value = get_user_meta( $customer_id, $field_key, true );
$meta_value = apply_filters( 'flexible_checkout_fields_user_meta_display_value', $meta_value, $field );
$val .= $meta_value;
if ( $is_empty_address && ( $meta_value === '' ) ) {
$val = '';
}
$address[$field['name']] = $val;
$address[$this->replace_only_first( $address_type . '_', '', $field['name'] )] = $val;
}
}
return $address;
}
public function getCheckoutFields( $fields, $request_type = null ) {
return $this->plugin->getCheckoutFields( $fields, $request_type );
}
/**
* Checks if all values in address array are empty.
*
* @param string[] $address Array keys are field names and values are field values.
*
* @return bool Status if all values are empty string.
*/
private function check_if_address_is_empty( array $address ) {
foreach ( $address as $field_key => $field_value ) {
if ( $field_value !== '' ) {
return false;
}
}
return true;
}
/**
* Append field to address format.
*
* @param string $format
* @param string $field_key
* @param array $field
*
* @return string
*/
private function append_field_to_address_format( $format, $field_key, $field ) {
if ( ( $this->is_thankyou_page() || $this->is_in_email() || $this->is_order_page() )
&& in_array( $field_key, array( 'billing_phone', 'billing_email' ) )
) {
return $format;
}
$fcf_field = new Flexible_Checkout_Fields_Field( $field, $this->plugin );
if ( isset( $field['type'] ) && in_array( $field['type'], array( 'heading', 'info', 'paragraph', 'image' ) ) ) {
return $format;
}
if ( $this->is_field_displayable( $field ) ) {
if ( $format != '' ) {
if ( $fcf_field->get_display_on_option_new_line_before() === '1' ) {
$format .= "\n";
} elseif ( ! $fcf_field->get_display_comma_before() ) {
$format .= ' ';
}
}
if ( $fcf_field->get_display_comma_before() ) {
$format .= ', ';
}
$field_name = $fcf_field->get_name_for_address_format();
$format .= '{' . $this->replace_only_first( $this->current_address_type . '_', '', $field_name . '}' );
}
return $format;
}
/**
* Localisation address formats - woocommerce filter.
*
* @param array $formats
*
* @return array
*/
public function woocommerce_localisation_address_formats_filter( $formats ) {
$fields = $this->getCheckoutFields( array(), $this->current_address_type );
if ( empty( $fields ) ) {
return $formats;
}
foreach ( $formats as $format_key => $format ) {
if ( $this->is_edit_address_page()
|| $this->is_order_page()
|| $this->is_in_email()
|| $this->is_thankyou_page()
) {
$formats[ $format_key ] = '';
foreach ( $fields as $field_key => $field ) {
$formats[ $format_key ] = $this->append_field_to_address_format( $formats[ $format_key ], $field_key, $field );
}
}
}
return $formats;
}
private function is_field_displayable( $field ) {
$displayable = true;
$fcf_field = new Flexible_Checkout_Fields_Field( $field, $this->plugin );
if ( $this->is_edit_address_page() ) {
$displayable = !isset( $field[self::DISPLAY_ON_ADDRESS] ) || $field[self::DISPLAY_ON_ADDRESS] == '1';
if ( $fcf_field->is_field_excluded_for_user() ) {
$displayable = false;
}
}
if ( $this->is_order_page() ) {
$displayable = !isset( $field[self::DISPLAY_ON_ORDER] ) || $field[self::DISPLAY_ON_ORDER] == '1';
}
if ( $this->is_in_email() ) {
$displayable = !isset( $field[self::DISPLAY_ON_EMAILS] ) || $field[self::DISPLAY_ON_EMAILS] == '1';
}
if ( $this->is_thankyou_page() ) {
$displayable = !isset( $field[self::DISPLAY_ON_THANK_YOU] ) || $field[self::DISPLAY_ON_THANK_YOU] == '1';
}
return $displayable;
}
public function is_admin_edit_order() {
$admin_edit_order = false;
if ( is_admin() ) {
$admin_edit_order = true;
}
return $admin_edit_order;
}
public function is_edit_address_page() {
global $wp;
$edit_address_page = false;
if ( is_account_page() ) {
if ( isset( $wp->query_vars['edit-address'] ) ) {
$edit_address_page = true;
}
}
return $edit_address_page;
}
public function is_order_page() {
global $wp;
$order_page = false;
if ( is_account_page() ) {
if ( isset( $wp->query_vars['view-order'] ) ) {
$order_page = true;
}
}
return $order_page;
}
public function is_in_email() {
$in_email = false;
if ( $this->in_email_address ) {
$in_email = true;
}
return $in_email;
}
public function is_thankyou_page() {
global $wp;
$thankyou_page = false;
if ( is_checkout() ) {
if ( isset( $wp->query_vars['order-received'] ) ) {
$thankyou_page = true;
}
}
return $thankyou_page;
}
public function woocommerce_formatted_address_replacements( $fields, $args ) {
foreach ( $args as $arg_key => $arg ) {
if ( !isset( $fields['{' . $arg_key . '}'] ) ) {
$fields['{' . $arg_key . '}'] = $arg;
}
}
return $fields;
}
public function woocommerce_order_formatted_billing_address( $fields, $order ) {
$this->current_address_type = 'billing';
WC()->countries->address_formats = '';
return $this->woocommerce_order_formatted_address( $fields, $order, 'billing' );
}
/**
* @param array $fields
* @param WC_Order $order
* @param string $address_type
*
* @return mixed
*/
public function woocommerce_order_formatted_address( $fields, $order, $address_type ) {
$cf_fields = $this->getCheckoutFields( array(), $address_type );
$checkout_field_type = $this->plugin->get_fields();
foreach ( $cf_fields as $field_key => $field ) {
$val = wpdesk_get_order_meta( $order, '_' . $field_key, true );
if ( empty( $val ) && isset( $fields[ $field_key ] ) ) {
$val = $fields[ $field_key ];
}
$fcf_field = new Flexible_Checkout_Fields_Field( $field, $this->plugin );
if ( (isset( $field['custom_field'] ) && $field['custom_field'] == '1')) {
$val = '';
if ( $fcf_field->is_custom_field() && $fcf_field->get_display_on_option_show_label() === '1' ) {
$val = strip_tags( wpdesk__( $field['label'], 'flexible-checkout-fields' ) ) . ': ';
}
$meta_value = wpdesk_get_order_meta( $order, '_' . $field_key, true );
$meta_value = apply_filters( 'flexible_checkout_fields_print_value', $meta_value, $field );
$val .= $meta_value;
}
$val = $this->flexible_invoices_ask_field_integration($val, $field, $field_key, $fields);
$val = wp_kses_post( $val );
$fields[$field['name']] = $val;
$fields[$this->replace_only_first( $address_type . '_', '', $field['name'] )] = $val;
}
return $fields;
}
/**
* Similar to str_replace but replaces only the first occurrence.
*
* @param string $needle search for it.
* @param string $replace change the needle to this value.
* @param string $haystack here we are searching
*
* @return string
*/
private function replace_only_first($needle, $replace, $haystack) {
$pos = strpos($haystack, $needle);
if ($pos !== false) {
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
return $haystack;
}
/**
* Return value for invoice ask field prepared by FI plugin. If can't then fallback.
*
* @param string $val Prepared by FCF field value.
* @param array $field FCF field def.
* @param string $field_key Field key that is currently processed. Needed to check if val should be replaced.
* @param array $fields Prepared by WC field values
*
* @return string New field value
*/
private function flexible_invoices_ask_field_integration( $val, $field, $field_key, $fields ) {
if ( apply_filters( 'flexible_checkout_fields_invoices_integration_enabled', true ) ) {
// FI ask field integration
if ( $field_key === 'invoice_ask' && ! empty( $fields['invoice_ask_field'] ) ) {
return $fields['invoice_ask_field'];
}
// wFirma/Fakturownia/iFirma/inFakt ask field integration
$supported_ask_fields = array(
'billing_faktura',
'billing_invoice',
'billing_rachunek'
);
if ( in_array( $field_key, $supported_ask_fields, true ) ) {
$wc_meta_key_definitions = apply_filters( 'woocommerce_customer_meta_fields', [] );
$label = strip_tags( wpdesk__( $field['label'], 'flexible-checkout-fields' ) );
// original plugin is probably(?) disabled if the field is not accessible
if ( isset( $wc_meta_key_definitions[ $this->current_address_type ]['fields'][ $field_key ] ) ) {
$wc_field_def = $wc_meta_key_definitions[ $this->current_address_type ]['fields'][ $field_key ];
// if field exists and is defined as select we can use this data. If not then better do not touch as it's probably optional checkbox
if ( isset( $wc_field_def['options'] ) ) {
$select_options = $wc_meta_key_definitions[ $this->current_address_type ]['fields'][ $field_key ]['options'];
$option_val = isset( $select_options[ $val ] ) ? $select_options[ $val ] : '';
return $label . ': ' . $option_val;
} elseif ( (int) $val === 1 ) {
return $label;
}
}
}
}
return $val;
}
/**
* Mainly injects FCF data values into WC formatted shipping address.
* Also changes current_address_type indicator and do some shady stuff to WC()->countries.
*
* @param array|string $fields Fields can be string when shipment is disabled
* @param WC_Order $order
*
* @return array|string
*/
public function woocommerce_order_formatted_shipping_address( $fields, $order ) {
$this->current_address_type = 'shipping';
WC()->countries->address_formats = '';
if ( ! is_array( $fields ) ) {
return $fields;
}
return $this->woocommerce_order_formatted_address( $fields, $order, 'shipping' );
}
public function woocommerce_billing_fields( $fields ) {
return $this->woocommerce_fields( $fields, 'billing' );
}
public function woocommerce_shipping_fields( $fields ) {
return $this->woocommerce_fields( $fields, 'shipping' );
}
protected function woocommerce_fields( $fields, $section ) {
global $wp;
$cf_fields = $this->getCheckoutFields( array(), $section );
$edit_address_page = false;
if ( is_account_page() ) {
if ( isset( $wp->query_vars['edit-address'] ) ) {
$edit_address_page = true;
}
}
foreach ( $cf_fields as $cf_field_key => $cf_field ) {
$remove_field = false;
if ( $edit_address_page ) {
$remove_field = true;
if ( !isset( $cf_field[self::DISPLAY_ON_ADDRESS] ) || $cf_field[self::DISPLAY_ON_ADDRESS] == '1' ) {
$remove_field = false;
}
}
if ( $remove_field ) {
unset( $fields[$cf_field_key] );
}
}
return $fields;
}
}

View File

@@ -0,0 +1,49 @@
<?php
/**
* Field type settings.
*/
class Flexible_Checkout_Fields_Field_Type_Settings {
const FIELD_TYPE_TEXT = 'text';
const FIELD_TYPE_TEXTAREA = 'textarea';
/**
* Field type data.
*
* @var array
*/
private $field_type_settings;
public function __construct( array $field_type_settings ) {
$this->field_type_settings = $field_type_settings;
}
/**
* .
*
* @return bool
*/
public function has_options() {
return isset( $this->field_type_settings['has_options'] ) && $this->field_type_settings['has_options'];
}
/**
* .
*
* @return bool
*/
public function has_default_value() {
return isset( $this->field_type_settings['has_default_value'] ) && $this->field_type_settings['has_default_value'];
}
/**
* Is pro indicator set?
*
* @return bool
*/
public function is_pro() {
return isset( $this->field_type_settings['pro'] ) ? intval( $this->field_type_settings['pro'] ) === 1 : false;
}
}

View File

@@ -0,0 +1,272 @@
<?php
/**
* Checkout field settings.
*/
class Flexible_Checkout_Fields_Field {
const FIELD_TYPE = 'type';
const FIELD_CUSTOM_FIELD = 'custom_field';
const FIELD_VISIBLE = 'visible';
const FIELD_DEFAULT = 'default';
const FIELD_DISPLAY_ON_OPTION_NEW_LINE_BEFORE = 'display_on_option_new_line_before';
const FIELD_DISPLAY_ON_OPTION_SHOW_LABEL = 'display_on_option_show_label';
const FIELD_TYPE_EXCLUDE_IN_ADMIN = 'exclude_in_admin';
const FIELD_TYPE_EXCLUDE_FOR_USER = 'exclude_for_user';
const DEFAULT_FIELD_TYPE = Flexible_Checkout_Fields_Field_Type_Settings::FIELD_TYPE_TEXT;
const FIELD_TYPE_STATE = 'state';
const DISPLAY_OPTION_STATE_CODE = 'state_code';
const DISPLAY_OPTION_STATE_COMMA_BEFORE = 'state_code_comma_before';
/**
* Field data.
*
* @var array
*/
private $field_data;
/**
* Plugin.
*
* @var Flexible_Checkout_Fields_Plugin
*/
private $plugin;
/**
* Flexible_Checkout_Fields_Field constructor.
*
* @param array $field_data Field data.
* @param Flexible_Checkout_Fields_Plugin $plugin Plugin.
*/
public function __construct( array $field_data, $plugin ) {
$this->plugin = $plugin;
$this->field_data = $field_data;
}
/**
* @param array $field_data Field data.
* @param array $field_settings .
* @param Flexible_Checkout_Fields_Plugin $plugin Plugin.
*
* @return Flexible_Checkout_Fields_Field
*/
public static function create_with_settings( $field_data, $field_settings, $plugin ) {
$fcf_field = new self( $field_data, $plugin );
$fcf_field->add_field_settings( $field_settings );
return $fcf_field;
}
/**
* Add settings to field.
*
* @param array $field_settings .
*/
public function add_field_settings( array $field_settings ) {
foreach ( $field_settings as $key => $setting ) {
$this->field_data[ $key ] = $setting;
}
}
/**
* Get field setting.
*
* @param string $setting_name .
* @param null|string|array $default_value .
*
* @return array|string|null
*/
public function get_field_setting( $setting_name, $default_value = null ) {
if ( $setting_name === self::FIELD_DISPLAY_ON_OPTION_SHOW_LABEL ) {
return $this->get_display_on_option_show_label();
}
if ( $setting_name === self::FIELD_DISPLAY_ON_OPTION_NEW_LINE_BEFORE ) {
return $this->get_display_on_option_new_line_before();
}
if ( isset( $this->field_data[ $setting_name ] ) ) {
return $this->field_data[ $setting_name ];
} else {
return $default_value;
}
}
/**
* Get field types from plugin.
*
* @return array
*/
private function get_field_types_from_plugin() {
return $this->plugin->get_fields();
}
/**
* Get field type settings.
*
* @return array
*/
private function get_field_type_settings() {
$default_values = array(
self::FIELD_TYPE_EXCLUDE_IN_ADMIN => false,
self::FIELD_TYPE_EXCLUDE_FOR_USER => false,
);
$field_types = $this->get_field_types_from_plugin();
if ( isset( $this->field_data[ self::FIELD_TYPE ] ) && isset( $field_types[ $this->field_data[ self::FIELD_TYPE ] ] ) ) {
$field_type_settings = $field_types[ $this->field_data[ self::FIELD_TYPE ] ];
$field_type_settings = array_merge( $default_values, $field_type_settings );
return $field_type_settings;
}
return $default_values;
}
/**
* Is visible?
*
* @return bool
*/
public function is_custom_field() {
if ( isset( $this->field_data[ self::FIELD_CUSTOM_FIELD ] ) && 1 === intval( $this->field_data[ self::FIELD_CUSTOM_FIELD ] ) ) {
return true;
}
return false;
}
/**
* Is visible?
*
* @return bool
*/
public function is_visible() {
if ( isset( $this->field_data[ self::FIELD_VISIBLE ] ) && 0 === intval( $this->field_data[ self::FIELD_VISIBLE ] ) ) {
return false;
}
return true;
}
/**
* Is field excluded for user?
* Field is excluded from user when is custom field and is not visible or field type is excluded for user.
*
* @return bool
*/
public function is_field_excluded_for_user() {
if ( ! $this->is_custom_field() ) {
return false;
}
$field_type_settings = $this->get_field_type_settings();
if ( true === $field_type_settings[ self::FIELD_TYPE_EXCLUDE_FOR_USER ] ) {
return true;
}
return false;
}
/**
* Is field excluded in admin?
*
* @return bool
*/
public function is_field_excluded_in_admin() {
if ( ! $this->is_custom_field() ) {
return false;
}
$field_type_settings = $this->get_field_type_settings();
if ( true === $field_type_settings[ self::FIELD_TYPE_EXCLUDE_IN_ADMIN ] ) {
return true;
}
return false;
}
/**
* .
*
* @return string
*/
public function get_display_on_option_new_line_before() {
if ( isset( $this->field_data[ self::FIELD_DISPLAY_ON_OPTION_NEW_LINE_BEFORE ] ) ) {
return $this->field_data[ self::FIELD_DISPLAY_ON_OPTION_NEW_LINE_BEFORE ];
} else {
return '1';
}
}
/**
* .
*
* @return string
*/
public function get_display_on_option_show_label() {
if ( isset( $this->field_data[ self::FIELD_DISPLAY_ON_OPTION_SHOW_LABEL ] ) ) {
return $this->field_data[ self::FIELD_DISPLAY_ON_OPTION_SHOW_LABEL ];
} else {
if ( $this->is_custom_field() ) {
return '1';
} else {
return '0';
}
}
}
/**
* Set field type.
*
* @param string $type .
*/
public function set_type( $type ) {
$this->field_data[ self::FIELD_TYPE ] = $type;
}
/**
* Get field type.
*
* @return string
*/
public function get_type() {
return isset( $this->field_data[ self::FIELD_TYPE ] ) ? $this->field_data[ self::FIELD_TYPE ] : self::DEFAULT_FIELD_TYPE;
}
/**
* Get default value.
*
* @return string
*/
public function get_default() {
return isset( $this->field_data[ self::FIELD_DEFAULT ] ) ? $this->field_data[ self::FIELD_DEFAULT ] : '';
}
/**
* Prepare display_on option name.
*
* @param string $display_on
*
* @return string
*/
public function prepare_display_on_option_name( $display_on ) {
return 'display_on_option_' . $display_on;
}
/**
* Get field name for formatted address.
*/
public function get_name_for_address_format() {
$name = $this->field_data['name'];
if ( isset( $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_CODE ) ] )
&& 1 === intval( $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_CODE ) ] )
) {
$name = 'state_code';
}
return $name;
}
/**
* Get display comma before field.
* Currently used only on state/county field.
*/
public function get_display_comma_before() {
return isset( $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_COMMA_BEFORE ) ] )
? $this->field_data[ $this->prepare_display_on_option_name( self::DISPLAY_OPTION_STATE_COMMA_BEFORE ) ] : '0';
}
}

View File

@@ -0,0 +1,104 @@
<?php
use WPDesk\FCF\Free\Field\Type\FileType;
use WPDesk\FCF\Free\Field\Type\MultiCheckboxType;
use WPDesk\FCF\Free\Field\Type\MultiSelectType;
use WPDesk\FCF\Free\Field\Type\TextareaType;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class Flexible_Checkout_Fields_Field_Validation {
/**
* @var Flexible_Checkout_Fields_Plugin
*/
protected $plugin;
/**
* Flexible_Checkout_Fields_Field_Validation constructor.
*
* @param Flexible_Checkout_Fields_Plugin $plugin
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
}
/**
*
*/
public function hooks() {
add_action( 'woocommerce_after_checkout_validation', array( $this, 'woocommerce_after_checkout_validation_action' ) );
add_filter( 'woocommerce_checkout_required_field_notice', array( $this, 'woocommerce_checkout_required_field_notice_filter' ), 10, 2 );
}
public function woocommerce_checkout_required_field_notice_filter( $notice, $field_label ) {
$field_label = strip_tags( $field_label );
$notice = sprintf( __( '%s is a required field.', 'woocommerce' ), '<strong>' . $field_label . '</strong>' );
return $notice;
}
/**
* @param $data
*/
public function woocommerce_after_checkout_validation_action( $data ) {
foreach ( $data as $field => $value ) {
do_action( 'flexible_checkout_fields_validate_' . $field, $value );
}
$settings = $this->plugin->get_settings();
$custom_validations = $this->get_custom_validations();
foreach ( $settings as $section => $fields ) {
foreach ( $fields as $field_key => $field ) {
if ( isset( $_POST[$field_key] ) && !empty( $field['validation'] ) && array_key_exists( $field['validation'], $custom_validations ) ) {
call_user_func( $custom_validations[$field['validation']]['callback'], $field['label'], sanitize_textarea_field($_POST[$field_key]), $field );
}
if ( ! ( $field['custom_field'] ?? false ) ) {
continue;
}
if ( in_array( $field['type'], [ TextareaType::FIELD_TYPE ] ) ) {
$value = sanitize_textarea_field( wp_unslash( $_POST[ $field_key ] ?? '' ) );
} elseif ( in_array( $field['type'], [ MultiCheckboxType::FIELD_TYPE, MultiSelectType::FIELD_TYPE, FileType::FIELD_TYPE ] ) ) {
$value = json_encode( wp_unslash( $_POST[ $field_key ] ?? [] ) );
} else {
$value = sanitize_text_field( wp_unslash( $_POST[ $field_key ] ?? '' ) );
}
do_action( 'flexible_checkout_fields_validate_' . $field['type'], $value, $field );
}
}
}
/**
* Get custom validations.
*
* @param string $section .
* @return array
*/
public function get_custom_validations( $section = '' ) {
return apply_filters( 'flexible_checkout_fields_custom_validation', array(), $section );
}
/**
* Get validation options.
*
* @param string $section .
* @return array
*/
public function get_validation_options( $section = '' ) {
$validation_options = array(
'' => __( 'Default', 'flexible-checkout-fields' ),
'none' => __( 'None', 'flexible-checkout-fields' ),
'email' => __( 'Email', 'flexible-checkout-fields' ),
'phone' => __( 'Phone', 'flexible-checkout-fields' ),
);
if ( in_array( $section, array( 'billing', 'shipping' ), true ) ) {
$validation_options['postcode'] = __( 'Postcode', 'flexible-checkout-fields' );
}
$custom_validations = $this->get_custom_validations( $section );
foreach ( $custom_validations as $custom_validation_key => $custom_validation ) {
$validation_options[ $custom_validation_key ] = $custom_validation['label'];
}
return $validation_options;
}
}

View File

@@ -0,0 +1,50 @@
<?php
/**
* Class Flexible_Checkout_Fields_Myaccount_Edit_Address
*/
class Flexible_Checkout_Fields_Myaccount_Edit_Address {
/**
* Plugin.
*
* @var Flexible_Checkout_Fields_Plugin
*/
protected $plugin;
/**
* Flexible_Checkout_Fields_Myaccount_Edit_Address constructor.
*
* @param Flexible_Checkout_Fields_Plugin $plugin Plugin.
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
}
/**
* Hooks.
*/
public function hooks() {
add_filter( 'woocommerce_address_to_edit', array( $this, 'filter_edit_address_fields' ), 10, 2 );
}
/**
* Filter edit address fields.
*
* @param array $fields Fields.
* @param string $section Section.
*
* @return array
*/
public function filter_edit_address_fields( array $fields, $section ) {
foreach ( $fields as $key => $field ) {
$fcf_field = new Flexible_Checkout_Fields_Field( $field, $this->plugin );
if ( $fcf_field->is_field_excluded_for_user() ) {
unset( $fields[ $key ] );
}
}
return $fields;
}
}

View File

@@ -0,0 +1,91 @@
<?php
use WPDesk\FCF\Free\Field\Type\FileType;
use WPDesk\FCF\Free\Field\Type\MultiCheckboxType;
use WPDesk\FCF\Free\Field\Type\MultiSelectType;
use WPDesk\FCF\Free\Field\Type\TextareaType;
/**
* Class Flexible_Checkout_Fields_Myaccount_Field_Processor
*/
class Flexible_Checkout_Fields_Myaccount_Field_Processor {
/**
* @var Flexible_Checkout_Fields_Plugin
*/
protected $plugin;
/**
* Flexible_Checkout_Fields_Myaccount_Field_Processor constructor.
*
* @param Flexible_Checkout_Fields_Plugin $plugin Plugin.
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
}
/**
* Is custom field?
*
* @param array $field Field.
*
* @return bool
*/
private function is_custom_field( $field ) {
if ( isset( $field['custom_field'] ) && 1 === intval( $field['custom_field'] ) ) {
return true;
}
return false;
}
/**
* Hooks.
*/
public function hooks() {
$settings = $this->plugin->get_settings();
foreach ( $settings as $section ) {
if ( is_array( $section ) ) {
foreach ( $section as $key => $field ) {
if ( ! $this->is_custom_field( $field ) ) {
continue;
}
if ( in_array( $field['type'], [ TextareaType::FIELD_TYPE ] ) ) {
add_filter( 'woocommerce_process_myaccount_field_' . $key, [ $this, 'sanitize_textarea_value' ] );
} else if ( in_array( $field['type'], [ MultiCheckboxType::FIELD_TYPE, MultiSelectType::FIELD_TYPE, FileType::FIELD_TYPE ] ) ) {
add_filter( 'woocommerce_process_myaccount_field_' . $key, [ $this, 'sanitize_array_value' ] );
} else {
add_filter( 'woocommerce_process_myaccount_field_' . $key, [ $this, 'sanitize_text_value' ] );
}
}
}
}
}
/**
* @param string|null $value .
*
* @return string
*/
public function sanitize_textarea_value( $value ) {
return sanitize_textarea_field( wp_unslash( $value ) );
}
/**
* @param string|null $value .
*
* @return string
*/
public function sanitize_array_value( $value ) {
return json_encode( wp_unslash( $value ) );
}
/**
* @param string|null $value .
*
* @return string
*/
public function sanitize_text_value( $value ) {
return sanitize_text_field( wp_unslash( $value ) );
}
}

View File

@@ -0,0 +1,949 @@
<?php
use WPDesk\FCF\Free\Field\Type\FileType;
use WPDesk\FCF\Free\Field\Type\MultiCheckboxType;
use WPDesk\FCF\Free\Field\Type\MultiSelectType;
use WPDesk\FCF\Free\Field\Type\TextareaType;
use WPDesk\FCF\Free\Plugin as PluginFree;
/**
* Class Plugin
*
* @package WPDesk\WooCommerceFakturownia
*/
class Flexible_Checkout_Fields_Plugin extends \FcfVendor\WPDesk\PluginBuilder\Plugin\AbstractPlugin {
/** @see validate_checkout method https://github.com/woocommerce/woocommerce/blob/master/includes/class-wc-checkout.php#L719 */
const FIELDS_REQUIREMENT_CONTROLLED_BY_WOOCOMMERCE = array(
'billing_country',
'shipping_country',
'billing_state',
'shipping_state',
'billing_postcode',
'shipping_postcode',
);
/**
* Scripts version.
*
* @var string
*/
private $scripts_version = FLEXIBLE_CHECKOUT_FIELDS_VERSION . '.19';
protected $fields = array();
public $sections = array();
public $all_sections = array();
public $page_size = array();
public $field_validation;
/**
* Plugin path.
*
* @var string
*/
private $plugin_path;
/**
* Plugin namespaces
*
* Fot backward compatibility
*
* @var string
*/
public $plugin_namespace = 'inspire_checkout_fields';
/**
* Instance of new version main class of plugin.
*
* @var PluginFree
*/
private $plugin_free;
/**
* Plugin constructor.
*
* @param \WPDesk_Plugin_Info $plugin_info Plugin info.
*/
public function __construct( \FcfVendor\WPDesk_Plugin_Info $plugin_info ) {
parent::__construct( $plugin_info );
$this->plugin_info = $plugin_info;
$this->plugin_free = new PluginFree( $plugin_info, $this );
}
/**
* Init base variables for plugin
*/
public function init_base_variables() {
$this->plugin_url = $this->plugin_info->get_plugin_url();
$this->plugin_path = $this->plugin_info->get_plugin_dir();
$this->template_path = $this->plugin_info->get_text_domain();
$this->settings_url = admin_url( 'admin.php?page=wc-settings&tab=integration&section=integration-fakturownia' );
$this->default_view_args = [ 'plugin_url' => $this->get_plugin_url() ];
$this->plugin_has_settings = false;
$this->plugin_namespace = 'inspire_checkout_fields';
}
/**
* Init.
*/
public function init() {
$this->plugin_free->init();
$this->init_base_variables();
$this->load_dependencies();
$this->hooks();
}
/**
* Load dependencies.
*/
private function load_dependencies() {
new WPDesk_Flexible_Checkout_Fields_Tracker();
require_once __DIR__ . '/settings.php';
}
/**
* Hooks.
*/
public function hooks() {
$this->plugin_free->hooks();
parent::hooks();
$this->settings = new Flexible_Checkout_Fields_Settings( $this, self::FIELDS_REQUIREMENT_CONTROLLED_BY_WOOCOMMERCE );
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 100 );
add_action( 'woocommerce_checkout_fields', array( $this, 'changeCheckoutFields' ), 9999 );
add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'updateCheckoutFields' ), 9, 2 );
add_action( 'woocommerce_admin_order_data_after_billing_address', array(
$this,
'addCustomBillingFieldsToAdmin'
) );
add_action( 'woocommerce_admin_order_data_after_shipping_address', array(
$this,
'addCustomShippingFieldsToAdmin'
) );
add_action( 'woocommerce_admin_order_data_after_shipping_address', array(
$this,
'addCustomOrderFieldsToAdmin'
) );
add_action( 'woocommerce_billing_fields', array( $this, 'addCustomFieldsBillingFields' ), 9999 );
add_action( 'woocommerce_shipping_fields', array( $this, 'addCustomFieldsShippingFields' ), 9999 );
add_action( 'woocommerce_order_fields', array( $this, 'addCustomFieldsOrderFields' ), 9999 );
add_action( 'woocommerce_before_checkout_form', array( $this, 'woocommerce_before_checkout_form' ), 10 );
add_action( 'woocommerce_before_edit_address_form_shipping', array(
$this,
'woocommerce_before_checkout_form'
), 10 );
add_action( 'woocommerce_before_edit_address_form_billing', array(
$this,
'woocommerce_before_checkout_form'
), 10 );
add_filter( 'flexible_chekout_fields_fields', array( $this, 'getCheckoutFields' ), 10, 2 );
add_action( 'woocommerce_default_address_fields', array( $this, 'woocommerce_default_address_fields' ), 9999 );
add_filter( 'woocommerce_get_country_locale', array( $this, 'woocommerce_get_country_locale' ), 9999 );
add_filter( 'woocommerce_get_country_locale_base', array( $this, 'woocommerce_get_country_locale_base' ), 9999 );
add_action( 'woocommerce_get_country_locale_default', array( $this, 'woocommerce_get_country_locale_default' ), 11 );
add_filter( 'woocommerce_screen_ids', array( $this, 'add_woocommerce_screen_ids' ) );
new Flexible_Checkout_Fields_Disaplay_Options( $this );
$user_meta = new Flexible_Checkout_Fields_User_Meta( $this );
$user_profile = new Flexible_Checkout_Fields_User_Profile( $this, $user_meta );
$user_profile->hooks();
$user_meta = new Flexible_Checkout_Fields_User_Meta_Checkout( $this, $user_meta );
$user_meta->hooks();
$this->field_validation = new Flexible_Checkout_Fields_Field_Validation( $this );
$this->field_validation->hooks();
$my_account_fields_processor = new Flexible_Checkout_Fields_Myaccount_Field_Processor( $this );
$my_account_fields_processor->hooks();
$my_account_edit_address = new Flexible_Checkout_Fields_Myaccount_Edit_Address( $this );
$my_account_edit_address->hooks();
$plugin = $this;
add_filter( 'flexible_checkout_fields', static function() use( $plugin ) {
return $plugin;
});
}
/**
* Get plugin path.
*
* @return string
*/
public function get_plugin_path() {
return $this->plugin_path;
}
/**
* Load plugin textdomain
*/
public function load_plugin_text_domain() {
load_plugin_textdomain( 'wpdesk-plugin', false, $this->get_text_domain() . '/classes/wpdesk/lang/' );
load_plugin_textdomain( $this->get_text_domain(), false, $this->get_text_domain() . '/lang/' );
}
public function plugins_loaded() {
$this->init_fields();
//do użycia dla pola miasto, kod pocztowy i stan
$this->init_sections();
}
/**
* Get setting value.
*
* @param string $name Setting name.
* @param mixed $default Default setting value.
*
* @return mixed|void
*/
public function get_setting_value( $name, $default = null ) {
return get_option( $this->get_namespace() . '_' . $name, $default );
}
/**
* Change params used by js locale woocommerce/assets/js/frontend/address-i18n.js so it would not overwrite backend settings.
*
* This is a locale for default country.
*
* @param array $base Local base.
*
* @return array
*/
public function woocommerce_get_country_locale_base( $base ) {
$settings = $this->get_settings();
foreach ( $base as $key => $field ) {
unset( $base[ $key ]['placeholder'] );
unset( $base[ $key ]['label'] );
if ( version_compare( WC()->version, '4.4.1', '>=' ) ) {
unset( $base[ $key ]['class'] );
}
// field is force-required for given locale when FCF have shipping or billing field required
$shipping_key = 'shipping_' . $key;
$billing_key = 'billing_' . $key;
if ( ( isset( $settings['shipping'][ $shipping_key ] ) && $settings['shipping'][ $shipping_key ]['required'] )
|| ( isset( $settings['billing'][ $billing_key ] ) && $settings['billing'][ $billing_key ]['required'] ) ) {
$base [ $key ]['required'] = true;
}
}
return $base;
}
/**
* Change params used by js locale woocommerce/assets/js/frontend/address-i18n.js so it would not overwrite backend settings
*
* @param array $locale Table of field settings per locale
*
* @return array
*/
public function woocommerce_get_country_locale( $locale ) {
if ( is_checkout() || is_account_page() ) {
foreach ( $locale as $country => $fields ) {
foreach ( $fields as $field => &$settings ) {
unset( $locale[ $country ][ $field ]['priority'] );
unset( $locale[ $country ][ $field ]['label'] );
unset( $locale[ $country ][ $field ]['placeholder'] );
}
}
}
return $locale;
}
/**
* Remove priority from default address field
*
* @param array $fields Fields.
*
* @return array
*/
public function woocommerce_default_address_fields( $fields ) {
if ( is_checkout() || is_account_page() ) {
foreach ( $fields as $key => $field ) {
unset( $fields[ $key ]['priority'] );
}
}
return $fields;
}
/**
* Init sections.
*/
public function init_sections() {
$sections = array(
'billing' => array(
'section' => 'billing',
'tab' => 'fields_billing',
'tab_title' => __( 'Billing', 'flexible-checkout-fields' ),
'custom_section' => false,
'user_meta' => true,
),
'shipping' => array(
'section' => 'shipping',
'tab' => 'fields_shipping',
'tab_title' => __( 'Shipping', 'flexible-checkout-fields' ),
'custom_section' => false,
'user_meta' => true,
),
'order' => array(
'section' => 'order',
'tab' => 'fields_order',
'tab_title' => __( 'Order', 'flexible-checkout-fields' ),
'custom_section' => false,
'user_meta' => false,
),
);
$all_sections = unserialize( serialize( $sections ) );
$this->sections = apply_filters( 'flexible_checkout_fields_sections', $sections );
$this->all_sections = apply_filters( 'flexible_checkout_fields_all_sections', $all_sections );
}
private function init_fields() {
$field_types = apply_filters( 'flexible_checkout_fields/field_types', [] );
foreach ( $field_types as $field_type ) {
if ( $field_type['is_hidden'] ) {
continue;
}
$this->fields[ $field_type['type'] ] = [
'name' => $field_type['label'],
];
}
}
public function get_fields() {
return apply_filters( 'flexible_checkout_fields_fields', $this->fields );
}
/**
* Remove unavailable sections from settings.
* Removes sections added by PRO plugin, after PRO plugin disable.
*
* @param array $settings Settings.
*
* @return array
*/
private function get_settings_for_available_sections( array $settings ) {
$this->init_sections();
if ( is_array( $settings ) && is_array( $this->sections ) ) {
foreach ( $settings as $section => $section_settings ) {
$unset = true;
foreach ( $this->sections as $section_data ) {
if ( $section_data['section'] === $section ) {
$unset = false;
}
}
if ( $unset ) {
unset( $settings[ $section ] );
}
}
}
return $settings;
}
/**
* Get settings.
*
* @return array
*/
public function get_settings() {
$settings = get_option( 'inspire_checkout_fields_settings', array() );
if ( ! is_array( $settings ) ) {
$settings = array();
}
return $this->get_settings_for_available_sections( $settings );
}
public function woocommerce_before_checkout_form() {
WC()->session->set( 'checkout-fields', array() );
$settings = $this->get_settings();
$args = array( 'settings' => $settings );
include $this->plugin_path . '/views/before-checkout-form.php';
}
/**
* @param array $settings
* @param array $fields
* @param array $new
* @param null|string $request_type
*
* @return array
*/
private function append_other_plugins_fields_to_checkout_fields( $settings, $fields, $new, $request_type ) {
if ( $request_type === null ) {
if ( ! empty( $fields ) && is_array( $fields ) ) {
foreach ( $fields as $section => $section_fields ) {
if ( ! empty( $section_fields ) && is_array( $section_fields ) ) {
foreach ( $section_fields as $key => $field ) {
if ( empty( $settings[ $section ][ $key ] ) ) {
$new[ $section ][ $key ] = $field;
}
}
}
}
}
} else {
foreach ( $fields as $key => $field ) {
if ( empty( $settings[ $request_type ][ $key ] ) ) {
$new[ $request_type ][ $key ] = $field;
}
}
}
return $new;
}
/**
* Is field requirement controlled by woocommerce.
*
* @param string $field_name .
*
* @return bool
*/
private function is_field_requirement_controlled_by_woocommerce( $field_name ) {
return in_array( $field_name, self::FIELDS_REQUIREMENT_CONTROLLED_BY_WOOCOMMERCE, true );
}
/**
* @param array $fields
* @param null|string $request_type
*
* @return array
*/
public function getCheckoutFields( $fields, $request_type = null ) {
$settings = $this->get_settings();
$checkout_field_type = $this->get_fields();
if ( ! empty( $settings ) ) {
$new = array();
if ( isset( $fields['account'] ) ) {
$new['account'] = array();
}
$priority = 0;
foreach ( $settings as $key => $type ) {
if ( $key !== 'billing' && $key !== 'shipping' && $key !== 'order' ) {
if ( get_option( 'inspire_checkout_fields_' . $key, '0' ) == '0' ) {
continue;
}
}
if ( ! is_array( $type ) ) {
continue;
}
if ( $request_type == null || $request_type == $key ) {
if ( ! isset( $new[ $key ] ) ) {
$new[ $key ] = array();
}
$fields_found = true;
foreach ( $type as $field_name => $field ) {
if ( apply_filters( 'flexible_checkout_fields_condition', true, $field ) ) {
if ( $field['visible'] == 0 or
( ( isset( $_GET['page'] ) && $_GET['page'] == 'inspire_checkout_fields_settings' ) && $field['visible'] == 1 ) || $field['name'] == 'billing_country' || $field['name'] == 'shipping_country' ) {
$fcf_field = new Flexible_Checkout_Fields_Field( $field, $this );
$custom_field = $fcf_field->is_custom_field();
if ( isset( $fields[ $key ][ $field['name'] ] ) ) {
$new[ $key ][ $field['name'] ] = $fields[ $key ][ $field['name'] ];
} else {
$new[ $key ][ $field['name'] ] = $type[ $field['name'] ];
}
if ( ! $this->is_field_requirement_controlled_by_woocommerce( $field_name ) ) {
if ( 1 === intval( $field['required'] ?? 0 ) ) {
$new[ $key ][ $field['name'] ]['required'] = true;
} else {
$new[ $key ][ $field['name'] ]['required'] = false;
if ( isset( $new[ $key ][ $field['name'] ]['validate'] ) ) {
unset( $new[ $key ][ $field['name'] ]['validate'] );
}
}
} else {
if ( isset( $fields[ $key ][ $field['name'] ] ) ) {
$new[ $key ][ $field['name'] ]['required'] = $fields[ $key ][ $field['name'] ]['required'];
}
}
if ( isset( $field['label'] ) ) {
$new[ $key ][ $field['name'] ]['label'] = stripcslashes( wpdesk__( $field['label'], 'flexible-checkout-fields' ) );
// Support for fields rendered by WooCommerce
if ( isset( $field['type'] ) && in_array( $field['type'], array( 'text', 'textarea', 'select' ), true ) ) {
$new[ $key ][ $field['name'] ]['label'] = wp_kses_post( $new[ $key ][ $field['name'] ]['label'] );
}
}
if ( isset( $field['placeholder'] ) ) {
$new[ $key ][ $field['name'] ]['placeholder'] = wpdesk__( $field['placeholder'], 'flexible-checkout-fields' );
} else {
$new[ $key ][ $field['name'] ]['placeholder'] = '';
}
if ( isset( $field['class'] ) && ! is_array( $field['class'] ) ) {
$new[ $key ][ $field['name'] ]['class'] = explode( ' ', $field['class'] );
}
if ( ( $field['name'] == 'billing_country' || $field['name'] == 'shipping_country' ) && $field['visible'] == 1 ) {
$new[ $key ][ $field['name'] ]['class'][1] = "inspire_checkout_fields_hide";
}
if ( ! $custom_field ) {
if ( isset( $field['validation'] ) && $field['validation'] != '' ) {
if ( $field['validation'] == 'none' ) {
unset( $new[ $key ][ $field['name'] ]['validate'] );
} else {
$new[ $key ][ $field['name'] ]['validate'] = array( $field['validation'] );
}
}
} else {
if ( isset( $field['validation'] ) && $field['validation'] != 'none' ) {
$new[ $key ][ $field['name'] ]['validate'] = array( $field['validation'] );
}
}
if ( ! empty( $field['type'] ) ) {
$new[ $key ][ $field['name'] ]['type'] = $field['type'];
}
if ( $custom_field ) {
$new[ $key ][ $field['name'] ]['type'] = $field['type'] ?? '';
}
if ( '' !== $fcf_field->get_default() ) {
$new[ $key ][ $field['name'] ]['default'] = wpdesk__( $fcf_field->get_default(), 'flexible-checkout-fields' );
} elseif ( $field['options'] ?? [] ) {
$default = [];
foreach ( $field['options'] as $option ) {
if ( $option['default_checked'] ?? false ) {
$default[] = $option['key'];
}
}
$new[ $key ][ $field['name'] ]['default'] = $default;
}
}
}
}
}
}
$new = $this->append_other_plugins_fields_to_checkout_fields( $settings, $fields, $new, $request_type );
foreach ( $new as $type => $new_fields ) {
$priority = 0;
foreach ( $new_fields as $key => $field ) {
$priority += 10;
$new[ $type ][ $key ]['priority'] = $priority;
}
}
if ( $request_type == null ) {
if ( ! empty( $fields['account'] ) ) {
$new['account'] = $fields['account'];
}
$new = $this->restore_default_city_validation( $new, $_POST, 'billing' );
$new = $this->restore_default_city_validation( $new, $_POST, 'shipping' );
return $new;
}
if ( isset( $new[ $request_type ] ) ) {
$new = $this->restore_default_city_validation( $new, $_POST, $request_type );
return $new[ $request_type ];
} else {
return array();
}
} else {
return $fields;
}
}
/**
* Restores the default validation for the city
*
* @param array $fields Fields.
* @param array|null $request Request.
* @param string $request_type the type of shipping address (billing or shipping).
*
* @return array
*/
private function restore_default_city_validation( array $fields, $request, $request_type ) {
if ( null === $request ) {
$request = array();
}
$city = $request_type . '_city';
$country = $request_type . '_country';
if ( isset( $fields[ $request_type ][ $city ]['required'] ) && isset( $request[ $country ] ) ) {
$slug = $request[ $country ];
$countries = new WC_Countries();
$locales = $countries->get_country_locale();
if ( isset( $locales[ $slug ]['city']['required'] ) ) {
$required = $locales[ $slug ]['city']['required'];
if ( ! $required ) {
$fields[ $request_type ][ $city ]['required'] = 0;
$fields[ $request_type ][ $city ]['hidden'] = 1;
}
}
}
return $fields;
}
public function getCheckoutUserFields( $fields, $request_type = null ) {
$settings = $this->get_settings();
$checkout_field_type = $this->get_fields();
$priority = 0;
if ( ! empty( $settings[ $request_type ] ) ) {
foreach ( $settings[ $request_type ] as $key => $field ) {
if ( $field['visible'] == 0 || $field['name'] === 'billing_country' || $field['name'] === 'shipping_country' || ( isset( $_GET['page'] ) && $_GET['page'] === 'inspire_checkout_fields_settings' && $field['visible'] == 1 ) ) {
if ( ! empty( $fields[ $key ] ) ) {
$new[ $key ] = $fields[ $key ];
}
if ( ! $this->is_field_requirement_controlled_by_woocommerce( $key ) ) {
if ( ( $field['required'] ?? 0 ) == 1 ) {
$new[ $key ]['required'] = true;
} else {
$new[ $key ]['required'] = false;
}
}
if ( isset( $field['label'] ) ) {
$new[ $key ]['label'] = wpdesk__( $field['label'], 'flexible-checkout-fields' );
}
if ( isset( $field['placeholder'] ) ) {
$new[ $key ]['placeholder'] = wpdesk__( $field['placeholder'], 'flexible-checkout-fields' );
} else {
$new[ $key ]['placeholder'] = '';
}
if ( isset( $field['class'] ) ) {
if ( is_array( $field['class'] ) ) {
$new[ $key ]['class'] = explode( ' ', esc_attr( implode( ' ', $field['class'] ) ) );
} else {
$new[ $key ]['class'] = explode( ' ', esc_attr( $field['class'] ) );
}
}
if ( ! empty( $field['name'] ) ) {
if ( ( $field['name'] === 'billing_country' || $field['name'] === 'shipping_country' ) && $field['visible'] == 1 ) {
$new[ $key ]['class'][] = "inspire_checkout_fields_hide";
}
}
if ( ! empty( $field['type'] ) ) {
$new[ $key ]['type'] = $field['type'];
}
$new[ $key ]['custom_attributes'] = apply_filters(
'flexible_checkout_fields_custom_attributes',
$field['custom_attributes'] ?? [],
$field
);
}
}
if ( count( $fields ) ) {
foreach ( $new as $key => $field ) {
if ( empty( $fields[ $key ] ) ) {
$new[ $key ]['custom_field'] = 1;
}
}
}
foreach ( $new as $key => $field ) {
$priority += 10;
$new[ $key ]['priority'] = $priority;
}
return $new;
} else {
return $fields;
}
}
public function printCheckoutFields( $order, $request_type = null ) {
$settings = $this->getCheckoutFields( $this->get_settings() );
$checkout_field_type = $this->get_fields();
if ( ! empty( $settings ) ) {
foreach ( $settings as $key => $type ) {
if ( $request_type == null || $request_type == $key ) {
$return = [];
foreach ( $type as $field ) {
if ( ( isset( $field['custom_field'] ) && $field['custom_field'] == 1 )
&& ( empty( $field['type'] ) || ( ! empty( $checkout_field_type[ $field['type'] ] ) && empty( $checkout_field_type[ $field['type'] ]['exclude_in_admin'] ) ) )
) {
if ( $value = wpdesk_get_order_meta( $order, '_' . $field['name'], true ) ) {
if ( isset( $field['type'] ) ) {
$value = apply_filters( 'flexible_checkout_fields_print_value', nl2br( $value ), $field );
}
$return[] = sprintf(
'<strong>%1$s</strong>: %2$s',
strip_tags( $field['label'] ),
wp_kses_post( $value )
);
}
}
}
}
}
if ( ! empty( $return ) ) {
echo '<div class="address_flexible_checkout_fields"><p class="form-field form-field-wide">' . implode( '<br />', $return ) . '</p></div>';
}
}
}
public function changeCheckoutFields( $fields ) {
return $this->getCheckoutFields( $fields );
}
public function changeShippingFields( $fields ) {
return $this->getCheckoutFields( $fields, 'shipping' );
}
public function changeBillingFields( $fields ) {
return $this->getCheckoutFields( $fields, 'billing' );
}
public function changeOrderFields( $fields ) {
return $this->getCheckoutFields( $fields, 'order' );
}
public function addCustomBillingFieldsToAdmin( $order ) {
$this->printCheckoutFields( $order, 'billing' );
}
public function addCustomShippingFieldsToAdmin( $order ) {
$this->printCheckoutFields( $order, 'shipping' );
}
public function addCustomOrderFieldsToAdmin( $order ) {
$this->printCheckoutFields( $order, 'order' );
}
public function addCustomFieldsBillingFields( $fields ) {
return $this->getCheckoutUserFields( $fields, 'billing' );
}
public function addCustomFieldsShippingFields( $fields ) {
return $this->getCheckoutUserFields( $fields, 'shipping' );
}
public function addCustomFieldsOrderFields( $fields ) {
return $this->getCheckoutUserFields( $fields, 'order' );
}
/**
* Update fields on checkout.
*
* @param int $order_id Order id.
* @param array $data Posted data.
*/
function updateCheckoutFields( $order_id, $data ) {
$settings = $this->get_settings();
if ( ! empty( $settings ) ) {
$fields = [];
foreach ( $settings as $section_fields ) {
$fields += $section_fields;
}
foreach ( $data as $key => $value ) {
if ( isset( $fields[ $key ] ) ) {
$fcf_field = new Flexible_Checkout_Fields_Field( $fields[ $key ], $this );
if ( $fcf_field->is_custom_field() ) {
if ( in_array( $fcf_field->get_type(), [ TextareaType::FIELD_TYPE ] ) ) {
update_post_meta( $order_id, '_' . $key, sanitize_textarea_field( wp_unslash( $value ) ) );
} elseif ( in_array( $fcf_field->get_type(), [ MultiCheckboxType::FIELD_TYPE, MultiSelectType::FIELD_TYPE, FileType::FIELD_TYPE ] ) ) {
update_post_meta( $order_id, '_' . $key, json_encode( wp_unslash( $value ) ) );
} else {
update_post_meta( $order_id, '_' . $key, sanitize_text_field( wp_unslash( $value ) ) );
}
}
}
}
}
do_action( 'flexible_checkout_fields_checkout_update_order_meta', $order_id, $data );
}
public static function flexible_checkout_fields_section_settings( $key, $settings ) {
echo 1;
}
public function woocommerce_get_country_locale_default( $address_fields ) {
return $address_fields;
}
/**
* Add woocommerce screen ids.
*
* @param array $screen_ids Screen ids.
*
* @return array
*/
public function add_woocommerce_screen_ids( $screen_ids ) {
$screen_ids[] = 'woocommerce_page_inspire_checkout_fields_settings';
return $screen_ids;
}
/**
* Admin enqueue scripts.
*/
public function admin_enqueue_scripts() {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
if (function_exists('get_current_screen')) {
$current_screen = get_current_screen();
}
$deps = array(
'jquery',
'jquery-ui-sortable',
'jquery-ui-tooltip',
'jquery-ui-datepicker',
);
wp_enqueue_script( 'inspire_checkout_fields_admin_js', trailingslashit( $this->get_plugin_assets_url() ) . 'js/admin' . $suffix . '.js', $deps, $this->scripts_version );
}
/**
* Frontend enqueue scripts.
*/
public function wp_enqueue_scripts() {
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
if ( is_checkout() || is_account_page() ) {
if ( $this->get_setting_value( 'css_disable' ) != 1 ) {
wp_enqueue_style( 'jquery-ui-style', trailingslashit( $this->get_plugin_assets_url() ) . 'css/jquery-ui' . $suffix . '.css', array(), $this->scripts_version );
}
wp_enqueue_style( 'inspire_checkout_fields_public_style', trailingslashit( $this->get_plugin_assets_url() ) . 'css/front' . $suffix . '.css', array(), $this->scripts_version );
}
if ( is_checkout() || is_account_page() ) {
add_action( 'wp_enqueue_scripts', array( $this, 'wp_localize_jquery_ui_datepicker' ), 1000 );
$deps = array(
'jquery',
'jquery-ui-datepicker',
);
wp_register_script( 'inspire_checkout_fields_checkout_js', trailingslashit( $this->get_plugin_assets_url() ) . 'js/checkout' . $suffix . '.js', $deps, $this->scripts_version );
$translation_array = array(
'uploading' => __( 'Uploading file...', 'flexible-checkout-fields' ),
);
wp_localize_script( 'inspire_checkout_fields_checkout_js', 'words', $translation_array );
wp_enqueue_script( 'inspire_checkout_fields_checkout_js' );
wp_enqueue_script( 'jquery-ui-datepicker' );
}
}
function wp_localize_jquery_ui_datepicker() {
global $wp_locale;
global $wp_version;
if ( ! wp_script_is( 'jquery-ui-datepicker', 'enqueued' ) || version_compare( $wp_version, '4.6' ) != - 1 ) {
return;
}
// Convert the PHP date format into jQuery UI's format.
$datepicker_date_format = str_replace(
array(
'd',
'j',
'l',
'z', // Day.
'F',
'M',
'n',
'm', // Month.
'Y',
'y' // Year.
),
array(
'dd',
'd',
'DD',
'o',
'MM',
'M',
'm',
'mm',
'yy',
'y'
),
get_option( 'date_format' )
);
$datepicker_defaults = wp_json_encode( array(
'closeText' => __( 'Close' ),
'currentText' => __( 'Today' ),
'monthNames' => array_values( $wp_locale->month ),
'monthNamesShort' => array_values( $wp_locale->month_abbrev ),
'nextText' => __( 'Next' ),
'prevText' => __( 'Previous' ),
'dayNames' => array_values( $wp_locale->weekday ),
'dayNamesShort' => array_values( $wp_locale->weekday_abbrev ),
'dayNamesMin' => array_values( $wp_locale->weekday_initial ),
'dateFormat' => $datepicker_date_format,
'firstDay' => absint( get_option( 'start_of_week' ) ),
'isRTL' => $wp_locale->is_rtl(),
) );
wp_add_inline_script( 'jquery-ui-datepicker', "jQuery(document).ready(function(jQuery){jQuery.datepicker.setDefaults({$datepicker_defaults});});" );
}
/**
* Links filter.
*
* @param array $links Links.
*
* @return array
*/
public function links_filter( $links ) {
$plugin_links = array(
'<a href="' . admin_url( 'admin.php?page=inspire_checkout_fields_settings' ) . '">' . __( 'Settings', 'flexible-checkout-fields' ) . '</a>',
'<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-settings-row-action-docs' ) ) . '" target="_blank">' . __( 'Docs', 'flexible-checkout-fields' ) . '</a>',
'<a href="' . esc_url( apply_filters( 'flexible_checkout_fields/short_url', '#', 'fcf-settings-row-action-support' ) ) . '" target="_blank">' . __( 'Support', 'flexible-checkout-fields' ) . '</a>',
);
if ( ! wpdesk_is_plugin_active( 'flexible-checkout-fields-pro/flexible-checkout-fields-pro.php' ) ) {
$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>';
}
return array_merge( $plugin_links, $links );
}
}

View File

@@ -0,0 +1,77 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class Flexible_Checkout_Fields_Settings {
/**
* Flexible_Checkout_Fields_Settings constructor.
*
* @param Flexible_Checkout_Fields_Plugin $plugin .
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
add_action( 'init', array($this, 'init_polylang') );
add_action( 'admin_init', array($this, 'init_wpml') );
}
function init_polylang() {
if ( function_exists( 'pll_register_string' ) ) {
$settings = get_option('inspire_checkout_fields_settings', array() );
foreach ( $settings as $section ) {
if ( is_array( $section ) ) {
foreach ( $section as $field ) {
if ( isset( $field['label'] ) && $field['label'] !== '' ) {
pll_register_string( $field['label'], $field['label'], __('Flexible Checkout Fields', 'flexible-checkout-fields' ) );
}
if ( isset( $field['placeholder'] ) && $field['placeholder'] !== '' ) {
pll_register_string( $field['placeholder'], $field['placeholder'], __('Flexible Checkout Fields', 'flexible-checkout-fields' ) );
}
if ( isset( $field['default'] ) && $field['default'] !== '' ) {
pll_register_string( $field['default'], $field['default'], __('Flexible Checkout Fields', 'flexible-checkout-fields' ) );
}
if ( isset( $field['options'] ) ) {
foreach ( $field['options'] as $option_data ) {
pll_register_string( $option_data['value'], $option_data['value'], __('Flexible Checkout Fields', 'flexible-checkout-fields' ) );
}
}
if ( isset( $field['regex_message'] ) && $field['regex_message'] !== '' ) {
pll_register_string( $field['regex_message'], $field['regex_message'], __('Flexible Checkout Fields', 'flexible-checkout-fields' ) );
}
}
}
}
}
}
function init_wpml() {
if ( function_exists( 'icl_register_string' ) ) {
$icl_language_code = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : get_bloginfo('language');
$settings = get_option('inspire_checkout_fields_settings', array() );
foreach ( $settings as $section ) {
if ( is_array( $section ) ) {
foreach ( $section as $field ) {
if ( isset( $field['label'] ) && $field['label'] !== '' ) {
icl_register_string( 'flexible-checkout-fields', $field['label'], $field['label'], false, $icl_language_code );
}
if ( isset( $field['placeholder'] ) && $field['placeholder'] !== '' ) {
icl_register_string( 'flexible-checkout-fields', $field['placeholder'], $field['placeholder'], false, $icl_language_code );
}
if ( isset( $field['default'] ) && $field['default'] !== '' ) {
icl_register_string( 'flexible-checkout-fields', $field['default'], $field['default'], false, $icl_language_code );
}
if ( isset( $field['options'] ) ) {
foreach ( $field['options'] as $option_data ) {
icl_register_string( 'flexible-checkout-fields', $option_data['value'], $option_data['value'], false, $icl_language_code );
}
}
if ( isset( $field['regex_message'] ) && $field['regex_message'] !== '' ) {
icl_register_string( 'flexible-checkout-fields', $field['regex_message'], $field['regex_message'], false, $icl_language_code );
}
}
}
}
}
}
}

View File

@@ -0,0 +1,368 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'WPDesk_Flexible_Checkout_Fields_Tracker' ) ) {
class WPDesk_Flexible_Checkout_Fields_Tracker {
public static $script_version = '11';
public function __construct() {
$this->hooks();
}
public function hooks() {
add_filter( 'wpdesk_tracker_data', array( $this, 'wpdesk_tracker_data' ), 11 );
add_filter( 'wpdesk_tracker_notice_screens', array( $this, 'wpdesk_tracker_notice_screens' ) );
add_filter( 'plugin_action_links_flexible-checkout-fields/flexible-checkout-fields.php', array( $this, 'plugin_action_links' ) );
add_action( 'activated_plugin', array( $this, 'activated_plugin' ), 10, 2 );
}
public function wpdesk_tracker_data( $data ) {
$sections = array(
'billing',
'shipping',
'order',
'before_customer_details',
'after_customer_details',
'before_checkout_billing_form',
'after_checkout_billing_form',
'before_checkout_shipping_form',
'after_checkout_shipping_form',
'before_checkout_registration_form',
'after_checkout_registration_form',
'before_order_notes',
'after_order_notes',
'review_order_before_submit',
'review_order_after_submit',
);
$settings_fields = get_option('inspire_checkout_fields_settings', array() );
if ( ! is_array( $settings_fields ) ) {
$settings_fields = array();
}
$plugin_data = [
'sections' => $this->get_sections_data( $sections, $settings_fields ),
'fields_names' => $this->get_fields_names( $settings_fields ),
'options' => array(
'css_disable' => get_option( 'inspire_checkout_fields_css_disable', '0' )
),
'pro_version' => array(
'is_active' => is_flexible_checkout_fields_pro_active() ? '1' : '0',
'is_activated' => ( get_option( 'api_flexible-checkout-fields-pro_activated', '' ) === 'Activated' ) ? '1' : '0',
),
];
$data['flexible_checkout_fields'] = $plugin_data;
return $data;
}
private function get_fields_names( $settings_fields ) {
$items = array();
foreach ( $settings_fields as $section_key => $fields ) {
foreach ( $fields as $field_name => $field ) {
$name = str_replace( $section_key . '_', '', $field_name );
if ( !isset( $items[ $name ] ) ) {
$items[ $name ] = 0;
}
$items[ $name ]++;
}
}
return $items;
}
private function get_sections_data( $sections, $settings_fields ) {
$settings_sections = get_option('inspire_checkout_fields_section_settings', array() );
if ( ! is_array( $settings_sections ) ) {
$settings_sections = array();
}
$default_data = array(
'enabled' => 0,
'has_title' => 0,
'has_css' => 0,
'fields' => array(),
'fields_count' => 0,
);
$data = array();
foreach ( $sections as $section ) {
$data[$section] = $default_data;
if ( in_array( $section, array( 'billing', 'shipping', 'order' ) )
|| get_option( 'inspire_checkout_fields_' . $section, '0' ) ) {
$data[$section]['enabled'] = '1';
}
if ( isset( $settings_sections[ $section ] ) && ! empty( $settings_sections[ $section ]['section_title'] ) ) {
$data[$section]['has_title'] = '1';
}
if ( isset( $settings_sections[ $section ] ) && ! empty( $settings_sections[ $section ]['section_css'] ) ) {
$data[$section]['has_css'] = '1';
}
$data[$section]['fields'] = $this->get_fields_data( $section, $settings_fields );
if ( isset( $settings_fields[ $section ] ) ) {
$data[$section]['fields_count'] = count( $settings_fields[ $section ] );
}
}
return $data;
}
private function get_fields_data( $section, $settings ) {
if ( ! isset( $settings[ $section ] ) ) {
return array();
}
$default_data = array(
'count' => 0,
'enabled' => 0,
'required' => 0,
'validation' => array(),
'default_value' => 0,
'placeholder' => 0,
'display_on' => array(
'thank_you' => 0,
'on_address' => 0,
'on_order' => 0,
'on_emails' => 0,
'option_new_line_before' => 0,
'option_show_label' => 0,
),
'conditional_logic' => array(),
'pricing' => array(
'enabled' => 0,
'types' => array(),
'values' => array(),
'tax_classes' => array(),
),
);
$data = array();
foreach ( $settings[ $section ] as $field ) {
$field_type = ( isset( $field['type'] ) ) ? $field['type'] : '_null_';
if ( ! isset( $data[ $field_type ] ) ) {
$data[ $field_type ] = $default_data;
}
$data[ $field_type ]['count']++;
if ( isset( $field['visible'] ) && ! $field['visible'] ) {
$data[ $field_type ]['enabled']++;
}
if ( isset( $field['required'] ) && $field['required'] ) {
$data[ $field_type ]['required']++;
}
if ( isset( $field['validation'] ) && $field['validation'] ) {
if ( ! isset( $data[ $field_type ]['validation'][ $field['validation'] ] ) ) {
$data[ $field_type ]['validation'][ $field['validation'] ] = 0;
}
$data[ $field_type ]['validation'][ $field['validation'] ]++;
}
if ( isset( $field['default'] ) && $field['default'] ) {
$data[ $field_type ]['default_value']++;
}
if ( isset( $field['placeholder'] ) && $field['placeholder'] ) {
$data[ $field_type ]['placeholder']++;
}
if ( isset( $field['display_on_thank_you'] ) && $field['display_on_thank_you'] ) {
$data[ $field_type ]['display_on']['thank_you']++;
}
if ( isset( $field['display_on_address'] ) && $field['display_on_address'] ) {
$data[ $field_type ]['display_on']['on_address']++;
}
if ( isset( $field['display_on_order'] ) && $field['display_on_order'] ) {
$data[ $field_type ]['display_on']['on_order']++;
}
if ( isset( $field['display_on_emails'] ) && $field['display_on_emails'] ) {
$data[ $field_type ]['display_on']['on_emails']++;
}
if ( isset( $field['display_on_option_new_line_before'] ) && $field['display_on_option_new_line_before'] ) {
$data[ $field_type ]['display_on']['option_new_line_before']++;
}
if ( isset( $field['display_on_option_show_label'] ) && $field['display_on_option_show_label'] ) {
$data[ $field_type ]['display_on']['option_show_label']++;
}
if ( isset( $field['pricing_enabled'] ) && $field['pricing_enabled'] ) {
$data[ $field_type ]['pricing']['enabled']++;
}
if ( isset( $field['pricing_values'] ) && $field['pricing_values'] ) {
foreach ( $field['pricing_values'] as $pricing_value ) {
if ( ! isset( $data[ $field_type ]['pricing']['types'][ $pricing_value['type'] ] ) ) {
$data[ $field_type ]['pricing']['types'][ $pricing_value['type'] ] = 0;
}
$data[ $field_type ]['pricing']['types'][ $pricing_value['type'] ]++;
}
foreach ( $field['pricing_values'] as $pricing_value ) {
if ( ! isset( $data[ $field_type ]['pricing']['values'][ $pricing_value['value'] ] ) ) {
$data[ $field_type ]['pricing']['values'][ $pricing_value['value'] ] = 0;
}
$data[ $field_type ]['pricing']['values'][ $pricing_value['value'] ]++;
}
foreach ( $field['pricing_values'] as $pricing_value ) {
if ( ! isset( $data[ $field_type ]['pricing']['tax_classes'][ $pricing_value['tax_class'] ] ) ) {
$data[ $field_type ]['pricing']['tax_classes'][ $pricing_value['tax_class'] ] = 0;
}
$data[ $field_type ]['pricing']['tax_classes'][ $pricing_value['tax_class'] ]++;
}
}
$data[ $field_type ]['conditional_logic'] = $this->get_conditional_logic_data(
$field,
$data[ $field_type ]['conditional_logic']
);
}
return $data;
}
private function get_conditional_logic_data( $field, $current_data ) {
$default_data = array(
'enabled' => 0,
'action' => array(),
'operator' => array(),
'rule_options' => array(),
'rule_operators' => array(),
);
$data = ( $current_data )
? $current_data
: array(
'products' => $default_data,
'fields' => $default_data,
'shipping' => $default_data,
)
;
if ( isset( $field['conditional_logic'] ) && $field['conditional_logic'] ) {
$data['products']['enabled']++;
}
if ( isset( $field['conditional_logic_action'] ) && $field['conditional_logic_action'] ) {
if ( ! isset( $data['products']['action'][ $field['conditional_logic_action'] ] ) ) {
$data['products']['action'][ $field['conditional_logic_action'] ] = 0;
}
$data['products']['action'][ $field['conditional_logic_action'] ]++;
}
if ( isset( $field['conditional_logic_operator'] ) && $field['conditional_logic_operator'] ) {
if ( ! isset( $data['products']['operator'][ $field['conditional_logic_operator'] ] ) ) {
$data['products']['operator'][ $field['conditional_logic_operator'] ] = 0;
}
$data['products']['operator'][ $field['conditional_logic_operator'] ]++;
}
if ( isset( $field['conditional_logic_rules'] ) && $field['conditional_logic_rules'] ) {
foreach ( $field['conditional_logic_rules'] as $rule ) {
if ( ! isset( $data['products']['rule_options'][ $rule['condition'] ] ) ) {
$data['products']['rule_options'][ $rule['condition'] ] = 0;
}
$data['products']['rule_options'][ $rule['condition'] ]++;
if ( ! isset( $data['products']['rule_operators'][ $rule['what'] ] ) ) {
$data['products']['rule_operators'][ $rule['what'] ] = 0;
}
$data['products']['rule_operators'][ $rule['what'] ]++;
}
}
if ( isset( $field['conditional_logic_fields'] ) && $field['conditional_logic_fields'] ) {
$data['fields']['enabled']++;
}
if ( isset( $field['conditional_logic_fields_action'] ) && $field['conditional_logic_fields_action'] ) {
if ( ! isset( $data['fields']['action'][ $field['conditional_logic_fields_action'] ] ) ) {
$data['fields']['action'][ $field['conditional_logic_fields_action'] ] = 0;
}
$data['fields']['action'][ $field['conditional_logic_fields_action'] ]++;
}
if ( isset( $field['conditional_logic_fields_operator'] ) && $field['conditional_logic_fields_operator'] ) {
if ( ! isset( $data['fields']['operator'][ $field['conditional_logic_fields_operator'] ] ) ) {
$data['fields']['operator'][ $field['conditional_logic_fields_operator'] ] = 0;
}
$data['fields']['operator'][ $field['conditional_logic_fields_operator'] ]++;
}
if ( isset( $field['conditional_logic_fields_rules'] ) && $field['conditional_logic_fields_rules'] ) {
foreach ( $field['conditional_logic_fields_rules'] as $rule ) {
if ( ! isset( $data['fields']['rule_operators'][ $rule['condition'] ] ) ) {
$data['fields']['rule_operators'][ $rule['condition'] ] = 0;
}
$data['fields']['rule_operators'][ $rule['condition'] ]++;
}
}
if ( isset( $field['conditional_logic_shipping_fields'] ) && $field['conditional_logic_shipping_fields'] ) {
$data['shipping']['enabled']++;
}
if ( isset( $field['conditional_logic_shipping_fields_action'] ) && $field['conditional_logic_shipping_fields_action'] ) {
if ( ! isset( $data['shipping']['action'][ $field['conditional_logic_shipping_fields_action'] ] ) ) {
$data['shipping']['action'][ $field['conditional_logic_shipping_fields_action'] ] = 0;
}
$data['shipping']['action'][ $field['conditional_logic_shipping_fields_action'] ]++;
}
if ( isset( $field['conditional_logic_shipping_fields_operator'] ) && $field['conditional_logic_shipping_fields_operator'] ) {
if ( ! isset( $data['shipping']['operator'][ $field['conditional_logic_shipping_fields_operator'] ] ) ) {
$data['shipping']['operator'][ $field['conditional_logic_shipping_fields_operator'] ] = 0;
}
$data['shipping']['operator'][ $field['conditional_logic_shipping_fields_operator'] ]++;
}
return $data;
}
public function wpdesk_tracker_notice_screens( $screens ) {
$current_screen = get_current_screen();
if ( $current_screen->id == 'woocommerce_page_inspire_checkout_fields_settings' ) {
$screens[] = $current_screen->id;
}
return $screens;
}
public function plugin_action_links( $links ) {
if ( !wpdesk_tracker_enabled() || apply_filters( 'wpdesk_tracker_do_not_ask', false ) ) {
return $links;
}
$options = get_option('wpdesk_helper_options', array() );
if ( !is_array( $options )) {
$options = array();
}
if ( empty( $options['wpdesk_tracker_agree'] ) ) {
$options['wpdesk_tracker_agree'] = '0';
}
$plugin_links = array();
if ( $options['wpdesk_tracker_agree'] == '0' ) {
$opt_in_link = admin_url( 'admin.php?page=wpdesk_tracker&plugin=flexible-checkout-fields/flexible-checkout-fields.php' );
$plugin_links[] = '<a href="' . $opt_in_link . '">' . __( 'Opt-in', 'flexible-checkout-fields' ) . '</a>';
}
else {
$opt_in_link = admin_url( 'plugins.php?wpdesk_tracker_opt_out=1&plugin=flexible-checkout-fields/flexible-checkout-fields.php' );
$plugin_links[] = '<a href="' . $opt_in_link . '">' . __( 'Opt-out', 'flexible-checkout-fields' ) . '</a>';
}
return array_merge( $plugin_links, $links );
}
public function activated_plugin( $plugin, $network_wide ) {
if ( $network_wide ) {
return;
}
if ( defined( 'WP_CLI' ) && WP_CLI ) {
return;
}
if ( !wpdesk_tracker_enabled() ) {
return;
}
if ( $plugin == 'flexible-checkout-fields/flexible-checkout-fields.php' ) {
$options = get_option('wpdesk_helper_options', array() );
if ( empty( $options ) ) {
$options = array();
}
if ( empty( $options['wpdesk_tracker_agree'] ) ) {
$options['wpdesk_tracker_agree'] = '0';
}
$wpdesk_tracker_skip_plugin = get_option( 'wpdesk_tracker_skip_flexible_checkout_fields', '0' );
if ( $options['wpdesk_tracker_agree'] == '0' && $wpdesk_tracker_skip_plugin == '0' ) {
update_option( 'wpdesk_tracker_notice', '1' );
update_option( 'wpdesk_tracker_skip_flexible_checkout_fields', '1' );
if ( !apply_filters( 'wpdesk_tracker_do_not_ask', false ) ) {
wp_redirect( admin_url( 'admin.php?page=wpdesk_tracker&plugin=flexible-checkout-fields/flexible-checkout-fields.php' ) );
exit;
}
}
}
}
}
}

View File

@@ -0,0 +1,54 @@
<?php
/**
* User meta hooks.
*
* @package Flexible Checkout Fields
*/
/**
* Handles user meta on checkout.
*/
class Flexible_Checkout_Fields_User_Meta_Checkout {
/**
* Plugin.
*
* @var Flexible_Checkout_Fields_Plugin
*/
protected $plugin;
/**
* .
*
* @var Flexible_Checkout_Fields_User_Meta
*/
private $user_meta;
/**
* Flexible_Checkout_Fields_User_Profile constructor.
*
* @param Flexible_Checkout_Fields_Plugin $plugin Plugin.
* @param Flexible_Checkout_Fields_User_Meta $user_meta .
*/
public function __construct( Flexible_Checkout_Fields_Plugin $plugin, Flexible_Checkout_Fields_User_Meta $user_meta ) {
$this->plugin = $plugin;
$this->user_meta = $user_meta;
}
/**
* Hooks.
*/
public function hooks() {
add_filter( 'woocommerce_checkout_update_user_meta', array( $this, 'update_customer_meta_fields_on_checkout' ), 10, 2 );
}
/**
* Update customer meta data on checkout.
*
* @param int $customer_id Customer ID.
* @param array $data Posted checkout data.
*/
public function update_customer_meta_fields_on_checkout( $customer_id, $data ) {
$this->user_meta->update_customer_meta_fields( $customer_id, $data );
}
}

View File

@@ -0,0 +1,99 @@
<?php
/**
* User meta.
*
* @package Flexible Checkout Fields
*/
use WPDesk\FCF\Free\Field\Type\FileType;
use WPDesk\FCF\Free\Field\Type\MultiCheckboxType;
use WPDesk\FCF\Free\Field\Type\MultiSelectType;
use WPDesk\FCF\Free\Field\Type\TextareaType;
/**
* Can update user meta.
*/
class Flexible_Checkout_Fields_User_Meta {
/**
* Plugin.
*
* @var Flexible_Checkout_Fields_Plugin
*/
protected $plugin;
/**
* @param Flexible_Checkout_Fields_Plugin $plugin Plugin.
*/
public function __construct( $plugin ) {
$this->plugin = $plugin;
}
/**
* Is flexible checkout fields section?
*
* @param string $settings_section .
*
* @return bool
*/
public function is_fcf_section( $settings_section ) {
$sections = $this->plugin->sections;
foreach ( $sections as $section ) {
if ( isset( $section['section'] ) && $section['section'] === $settings_section ) {
return true;
}
}
return false;
}
/**
* Is allowed section to be stored in User Metadata?
*
* @param string $settings_section Key of fields section.
*
* @return bool Status of section, returns true if no data.
*/
public function is_section_allowed_for_usermeta( $settings_section ) {
$sections = $this->plugin->sections;
foreach ( $sections as $section ) {
if ( isset( $section['section'] ) && $section['section'] === $settings_section ) {
return ( ! isset( $section['user_meta'] ) || $section['user_meta'] );
}
}
return true;
}
/**
* Update customer meta data.
*
* @param int $customer_id Customer ID.
* @param array $data Posted checkout data.
*/
public function update_customer_meta_fields( $customer_id, $data ) {
$settings = $this->plugin->get_settings();
if ( ! empty( $settings ) ) {
foreach ( $settings as $key => $type ) {
if ( ! $this->is_fcf_section( $key ) || ! $this->is_section_allowed_for_usermeta( $key ) ) {
continue;
}
foreach ( $type as $field ) {
$field_name = $field['name'];
$fcf_field = new Flexible_Checkout_Fields_Field( $field, $this->plugin );
if ( ! $fcf_field->is_field_excluded_for_user() ) {
$value = '';
if ( isset( $data[ $field_name ] ) ) {
if ( in_array( $fcf_field->get_type(), [ TextareaType::FIELD_TYPE ] ) ) {
$value = sanitize_textarea_field( wp_unslash( $data[ $field_name ] ) );
} elseif ( in_array( $fcf_field->get_type(), [ MultiCheckboxType::FIELD_TYPE, MultiSelectType::FIELD_TYPE, FileType::FIELD_TYPE ] ) ) {
$value = json_encode( wp_unslash( $data[ $field_name ] ) );
} else {
$value = sanitize_text_field( wp_unslash( $data[ $field_name ] ) );
}
}
update_user_meta( $customer_id, $field_name, $value );
}
}
}
}
}
}

View File

@@ -0,0 +1,114 @@
<?php
/**
* User profile hooks.
*
* @package Flexible Checkout Fields
*/
/**
* User profile hooks.
*
* Class Flexible_Checkout_Fields_User_Profile
*/
class Flexible_Checkout_Fields_User_Profile {
const FIELD_TYPE = 'type';
const FIELD_TYPE_SELECT = 'select';
const FIELD_TYPE_INSPIRECHECKBOX = 'inspirecheckbox';
const FIELD_TYPE_INSPIRERADIO = 'inspireradio';
const FIELD_COPY_BILLING = 'copy_billing';
/**
* Plugin.
*
* @var Flexible_Checkout_Fields_Plugin
*/
protected $plugin;
/**
* .
*
* @var Flexible_Checkout_Fields_User_Meta
*/
private $user_meta;
/**
* Flexible_Checkout_Fields_User_Profile constructor.
*
* @param Flexible_Checkout_Fields_Plugin $plugin Plugin.
* @param Flexible_Checkout_Fields_User_Meta $user_meta .
*/
public function __construct( Flexible_Checkout_Fields_Plugin $plugin, Flexible_Checkout_Fields_User_Meta $user_meta ) {
$this->plugin = $plugin;
$this->user_meta = $user_meta;
}
/**
* Hooks.
*/
public function hooks() {
add_action( 'show_user_profile', [ $this, 'add_custom_user_fields_admin' ], 75 );
add_action( 'edit_user_profile', [ $this, 'add_custom_user_fields_admin' ], 75 );
add_action( 'personal_options_update', [ $this, 'save_custom_user_fields_admin' ] );
add_action( 'edit_user_profile_update', [ $this, 'save_custom_user_fields_admin' ] );
}
/**
* Add custom fields to edit user admin /wp-admin/profile.php.
*
* @param mixed $user .
*
* @return void
*/
public function add_custom_user_fields_admin( $user ) {
$settings = $this->plugin->get_settings();
$sections = $this->plugin->sections;
if ( ! $settings ) {
return;
}
foreach ( $settings as $key => $type ) {
if ( ! $this->user_meta->is_fcf_section( $key )
|| ! $this->user_meta->is_section_allowed_for_usermeta( $key )
|| ! is_array( $type ) ) {
continue;
}
$section_data = $sections[ $key ] ?? ( $sections[ 'woocommerce_' . $key ] ?? null );
if ( $section_data === null ) {
continue;
}
echo '<h3>' . esc_html( $section_data['tab_title'] ) . '</h3>';
echo '<div class="fcf-admin-fields postbox">';
echo '<div class="inside">';
foreach ( $type as $field ) {
if ( isset( $field['visible'] ) && 0 === intval( $field['visible'] )
&& ( isset( $field['custom_field'] ) && 1 === intval( $field['custom_field'] ) ) ) {
$field_value = htmlspecialchars_decode( get_the_author_meta( $field['name'], $user->ID ) );
echo apply_filters( 'flexible_checkout_fields_form_field', '', $field['name'], $field, $field_value );
}
}
echo '</div>';
echo '</div>';
}
}
/**
* Save custom user fields in admin.
*
* @param int $user_id User ID.
*/
public function save_custom_user_fields_admin( $user_id ) {
if ( ! current_user_can( 'edit_user', $user_id ) ) {
return;
}
if ( wp_verify_nonce( $_POST['_wpnonce'], 'update-user_' . $user_id ) ) { // phpcs:ignore
$this->user_meta->update_customer_meta_fields( $user_id, $_POST );
}
}
}