| 1 |
<?php |
| 2 |
/** |
| 3 |
* Checkout markup. |
| 4 |
* |
| 5 |
* @package cartflows |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Checkout Markup |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
class Cartflows_Optin_Fields { |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
/** |
| 22 |
* Member Variable |
| 23 |
* |
| 24 |
* @var object instance |
| 25 |
*/ |
| 26 |
private static $instance; |
| 27 |
|
| 28 |
/** |
| 29 |
* Initiator |
| 30 |
*/ |
| 31 |
public static function get_instance() { |
| 32 |
if ( ! isset( self::$instance ) ) { |
| 33 |
self::$instance = new self(); |
| 34 |
} |
| 35 |
return self::$instance; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor |
| 40 |
*/ |
| 41 |
public function __construct() { |
| 42 |
|
| 43 |
add_filter( 'cartflows_billing_optin_fields', array( $this, 'optin_billing_fields_customization' ), 10, 3 ); |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Is custom optin? |
| 49 |
* |
| 50 |
* @param int $optin_id optin ID. |
| 51 |
* @since 1.0.0 |
| 52 |
*/ |
| 53 |
public function is_wcf_optin_custom_fields( $optin_id ) { |
| 54 |
|
| 55 |
$is_custom = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-optin-enable-custom-fields' ); |
| 56 |
|
| 57 |
if ( 'yes' === $is_custom ) { |
| 58 |
|
| 59 |
return true; |
| 60 |
} |
| 61 |
|
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Billing field customization. |
| 67 |
* |
| 68 |
* @param array $fields fields data. |
| 69 |
* @param string $country country name. |
| 70 |
* @param int $optin_id checkout id. |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
public function optin_billing_fields_customization( $fields, $country, $optin_id ) { |
| 74 |
|
| 75 |
if ( ! $this->is_wcf_optin_custom_fields( $optin_id ) ) { |
| 76 |
return $fields; |
| 77 |
} |
| 78 |
|
| 79 |
if ( is_wc_endpoint_url( 'edit-address' ) ) { |
| 80 |
return $fields; |
| 81 |
} |
| 82 |
|
| 83 |
$saved_fields = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-optin-fields-billing' ); |
| 84 |
|
| 85 |
if ( ! is_array( $saved_fields ) ) { |
| 86 |
return $fields; |
| 87 |
} |
| 88 |
|
| 89 |
return $this->prepare_custom_fields( $saved_fields, $country, $optin_id, $fields, 'billing' ); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* Prepare custom fields. |
| 95 |
* |
| 96 |
* @param array $fieldset fieldset data. |
| 97 |
* @param string $country country name. |
| 98 |
* @param int $optin_id checkout ID. |
| 99 |
* @param bool $original_fieldset is original fieldset. |
| 100 |
* @param string $type address type. |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
public function prepare_custom_fields( $fieldset, $country, $optin_id, $original_fieldset = false, $type = 'billing' ) { |
| 104 |
|
| 105 |
if ( is_array( $fieldset ) && ! empty( $fieldset ) ) { |
| 106 |
|
| 107 |
$priority = 0; |
| 108 |
|
| 109 |
$all_original_fields = array_merge( $original_fieldset, $fieldset ); |
| 110 |
|
| 111 |
$original_fieldset = $this->prepare_custom_fields_data( $fieldset, $all_original_fields, $optin_id ); |
| 112 |
|
| 113 |
if ( ! empty( $original_fieldset ) ) { |
| 114 |
foreach ( $original_fieldset as $fieldset_key => $fieldset_value ) { |
| 115 |
if ( ! isset( $fieldset_value['priority'] ) ) { |
| 116 |
$new_priority = $priority + 10; |
| 117 |
$original_fieldset[ $fieldset_key ]['priority'] = $new_priority; |
| 118 |
$priority = $new_priority; |
| 119 |
} else { |
| 120 |
$priority = $fieldset_value['priority']; |
| 121 |
} |
| 122 |
|
| 123 |
if ( isset( $fieldset_value['type'] ) && 'radio' === $fieldset_value['type'] ) { |
| 124 |
$original_fieldset[ $fieldset_key ]['label_class'][] = 'input-radio'; |
| 125 |
$original_fieldset[ $fieldset_key ]['class'][] = 'wcf-input-radio-field-wrapper'; |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return $original_fieldset; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Prepare checkout fields. |
| 136 |
* |
| 137 |
* @param array $fields fields data. |
| 138 |
* @param bool $original_fields is original fields. |
| 139 |
* @param int $optin_id checkout ID. |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
public function prepare_custom_fields_data( $fields, $original_fields, $optin_id ) { |
| 143 |
|
| 144 |
if ( is_array( $fields ) && ! empty( $fields ) ) { |
| 145 |
|
| 146 |
$order_optin_fields = wcf()->options->get_optin_meta_value( $optin_id, 'wcf-optin-fields-billing' ); |
| 147 |
|
| 148 |
foreach ( $fields as $name => $field ) { |
| 149 |
|
| 150 |
// Backword compatibility with field enabled. |
| 151 |
if ( isset( $order_optin_fields[ $name ]['enabled'] ) ) { |
| 152 |
$is_enabled = $order_optin_fields[ $name ]['enabled']; |
| 153 |
} |
| 154 |
|
| 155 |
// Backword compatibility with field width. |
| 156 |
if ( isset( $order_optin_fields[ $name ]['width'] ) ) { |
| 157 |
$field_widths = $order_optin_fields[ $name ]['width']; |
| 158 |
} |
| 159 |
|
| 160 |
// Set/Unset field if checked/unchecked. |
| 161 |
if ( ! $is_enabled ) { |
| 162 |
unset( $original_fields[ $name ] ); |
| 163 |
unset( $fields[ $name ] ); |
| 164 |
} else { |
| 165 |
if ( ! isset( $original_fields[ $name ] ) ) { |
| 166 |
$original_fields[ $name ] = $field; |
| 167 |
} |
| 168 |
|
| 169 |
$field_widths = apply_filters( 'cartflows_optin_billing_fields_width', $field_widths, $name ); |
| 170 |
|
| 171 |
// Add Custom class if set. |
| 172 |
if ( '' != $field_widths ) { |
| 173 |
$original_fields[ $name ]['class'][] = 'wcf-column-' . $field_widths; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
return $original_fields; |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Kicking this off by calling 'get_instance()' method |
| 186 |
*/ |
| 187 |
Cartflows_Optin_Fields::get_instance(); |
| 188 |
|