activation-tracker.php
3 weeks ago
display-options.php
3 weeks ago
field-type-settings.php
3 weeks ago
field.php
3 weeks ago
filed-validation.php
3 weeks ago
myaccount-edit-address.php
3 weeks ago
myaccount-field-processor.php
3 weeks ago
plugin.php
3 weeks ago
settings.php
3 weeks ago
tracker.php
3 weeks ago
user-meta-checkout.php
3 weeks ago
user-meta.php
3 weeks ago
user-profile.php
3 weeks ago
display-options.php
496 lines
| 1 | <?php |
| 2 | |
| 3 | class Flexible_Checkout_Fields_Disaplay_Options { |
| 4 | |
| 5 | const HOOK_PRIORITY_LAST = 999999; |
| 6 | |
| 7 | const DISPLAY_ON_ADDRESS = 'display_on_address'; |
| 8 | const DISPLAY_ON_THANK_YOU = 'display_on_thank_you'; |
| 9 | const DISPLAY_ON_ORDER = 'display_on_order'; |
| 10 | const DISPLAY_ON_EMAILS = 'display_on_emails'; |
| 11 | |
| 12 | protected $plugin; |
| 13 | |
| 14 | protected $current_address_type = 'shipping'; |
| 15 | |
| 16 | protected $in_email_address = false; |
| 17 | |
| 18 | /** |
| 19 | * @var WC_Order |
| 20 | */ |
| 21 | protected $order = null; |
| 22 | |
| 23 | /** |
| 24 | * Flexible_Checkout_Fields_Disaplay_Options constructor. |
| 25 | * |
| 26 | * @param Flexible_Checkout_Fields_Plugin $plugin |
| 27 | */ |
| 28 | public function __construct( $plugin ) { |
| 29 | $this->plugin = $plugin; |
| 30 | $this->hooks(); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * |
| 35 | */ |
| 36 | protected function hooks() { |
| 37 | add_filter( 'woocommerce_localisation_address_formats', [ $this, 'woocommerce_localisation_address_formats_filter' ], self::HOOK_PRIORITY_LAST ); |
| 38 | add_filter( 'woocommerce_formatted_address_replacements', [ $this, 'woocommerce_formatted_address_replacements' ], self::HOOK_PRIORITY_LAST, 2 ); |
| 39 | add_filter( 'woocommerce_order_formatted_billing_address', [ $this, 'woocommerce_order_formatted_billing_address' ], self::HOOK_PRIORITY_LAST, 2 ); |
| 40 | add_filter( 'woocommerce_order_formatted_shipping_address', [ $this, 'woocommerce_order_formatted_shipping_address' ], self::HOOK_PRIORITY_LAST, 2 ); |
| 41 | |
| 42 | // addresses in my account |
| 43 | add_filter( 'woocommerce_my_account_my_address_formatted_address', [ $this, 'woocommerce_my_account_my_address_formatted_address' ], 10, 3 ); |
| 44 | |
| 45 | add_action( 'woocommerce_billing_fields', [ $this, 'woocommerce_billing_fields' ], 19999 ); |
| 46 | add_action( 'woocommerce_shipping_fields', [ $this, 'woocommerce_shipping_fields' ], 19999 ); |
| 47 | |
| 48 | add_action( 'woocommerce_email_customer_details', [ $this, 'woocommerce_email_customer_details_start' ], 10 ); |
| 49 | |
| 50 | add_action( 'woocommerce_email_customer_details', [ $this, 'woocommerce_email_customer_details_end' ], 10000 ); |
| 51 | |
| 52 | // additional fields |
| 53 | add_action( 'woocommerce_thankyou', [ $this, 'additional_information_fields' ], 75 ); |
| 54 | add_action( 'woocommerce_email_order_meta', [ $this, 'email_additional_information_fields' ], 195 ); |
| 55 | add_action( 'woocommerce_view_order', [ $this, 'additional_information_fields' ], 195 ); |
| 56 | } |
| 57 | |
| 58 | public function email_additional_information_fields( $order ) { |
| 59 | $this->in_email_address = true; |
| 60 | $this->additional_information_fields( wpdesk_get_order_id( $order ) ); |
| 61 | $this->in_email_address = false; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Displays additional fields. |
| 66 | * |
| 67 | * @param int $order_id Order id. |
| 68 | */ |
| 69 | public function additional_information_fields( $order_id ) { |
| 70 | |
| 71 | $settings = $this->plugin->getCheckoutFields( $this->plugin->get_settings() ); |
| 72 | |
| 73 | $checkout_field_type = $this->plugin->get_fields(); |
| 74 | |
| 75 | if ( ! empty( $settings ) && is_array( $settings ) ) { |
| 76 | $return = []; |
| 77 | foreach ( $settings as $key => $type ) { |
| 78 | if ( in_array( $key, [ 'billing', 'shipping' ], true ) ) { |
| 79 | continue; |
| 80 | } |
| 81 | if ( isset( $type ) && is_array( $type ) ) { |
| 82 | foreach ( $type as $field ) { |
| 83 | if ( isset( $field['visible'] ) && 0 === intval( $field['visible'] ) && isset( $field['custom_field'] ) && 1 === intval( $field['custom_field'] ) ) { |
| 84 | $value = wpdesk_get_order_meta( $order_id, '_' . $field['name'], true ); |
| 85 | if ( $this->is_field_displayable( $field ) && '' !== $value ) { |
| 86 | if ( ! empty( $checkout_field_type[ $field['type'] ]['has_options'] ) ) { |
| 87 | $options = $field['options']; |
| 88 | if ( isset( $options[ $value ] ) ) { |
| 89 | $value = $options[ $value ]; |
| 90 | } |
| 91 | } |
| 92 | $value = apply_filters( 'flexible_checkout_fields_print_value', $value, $field ); |
| 93 | if ( '' !== $value ) { |
| 94 | $return[] = strip_tags( wpdesk__( $field['label'], 'flexible-checkout-fields' ) ) . ': ' . \nl2br( esc_html( $value ) ); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | if ( count( $return ) > 0 ) { |
| 102 | echo '<div class="inspire_checkout_fields_additional_information">'; |
| 103 | echo '<h3>' . __( 'Additional Information', 'flexible-checkout-fields' ) . '</h3>'; |
| 104 | echo '<p>' . implode( '<br />', $return ) . '</p>'; |
| 105 | echo '</div>'; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | public function woocommerce_email_customer_details_start() { |
| 112 | $this->in_email_address = true; |
| 113 | } |
| 114 | |
| 115 | public function woocommerce_email_customer_details_end() { |
| 116 | $this->in_email_address = false; |
| 117 | } |
| 118 | |
| 119 | public function woocommerce_my_account_my_address_formatted_address( $address, $customer_id, $address_type ) { |
| 120 | $this->current_address_type = $address_type; |
| 121 | WC()->countries->address_formats = ''; |
| 122 | $cf_fields = $this->getCheckoutFields( [], $address_type ); |
| 123 | $is_empty_address = $this->check_if_address_is_empty( $address ); |
| 124 | foreach ( $cf_fields as $field_key => $field ) { |
| 125 | $fcf_field = new Flexible_Checkout_Fields_Field( $field, $this->plugin ); |
| 126 | if ( ! isset( $address[ $field['name'] ] ) ) { |
| 127 | $val = ''; |
| 128 | if ( $fcf_field->is_custom_field() && $fcf_field->get_display_on_option_show_label() === '1' ) { |
| 129 | $val .= strip_tags( wpdesk__( $field['label'], 'flexible-checkout-fields' ) ) . ': '; |
| 130 | } |
| 131 | |
| 132 | $meta_value = get_user_meta( $customer_id, $field_key, true ); |
| 133 | $meta_value = apply_filters( 'flexible_checkout_fields_user_meta_display_value', $meta_value, $field ); |
| 134 | $val .= $meta_value; |
| 135 | if ( $is_empty_address && ( $meta_value === '' ) ) { |
| 136 | $val = ''; |
| 137 | } |
| 138 | |
| 139 | $address[ $field['name'] ] = $val; |
| 140 | $address[ $this->replace_only_first( $address_type . '_', '', $field['name'] ) ] = $val; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | return $address; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Get checkout fields for display (after order is created) |
| 149 | * |
| 150 | * @param array<string, array<string,mixed>> $fields |
| 151 | * @param string $section_name (e.g. 'billing'). |
| 152 | * @param \WC_Order $order |
| 153 | * @return array<string, array<string,mixed>> |
| 154 | */ |
| 155 | public function getCheckoutFields( $fields, $section_name = null, $order = null ) { |
| 156 | $fields = $this->plugin->getCheckoutFields( $fields, $section_name ); |
| 157 | |
| 158 | /** |
| 159 | * Filter checkout fields for display. |
| 160 | * |
| 161 | * @since 4.1.5 |
| 162 | * |
| 163 | * @param array<string, array<string,mixed>> $fields |
| 164 | * @param string $section_name |
| 165 | * @param \WC_Order $order |
| 166 | * |
| 167 | * @return array<string, array<string,mixed>> |
| 168 | */ |
| 169 | return apply_filters( 'flexible_checkout_fields/display/get_checkout_fields', $fields, $section_name, $order ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Checks if all values in address array are empty. |
| 174 | * |
| 175 | * @param string[] $address Array keys are field names and values are field values. |
| 176 | * |
| 177 | * @return bool Status if all values are empty string. |
| 178 | */ |
| 179 | private function check_if_address_is_empty( array $address ) { |
| 180 | foreach ( $address as $field_key => $field_value ) { |
| 181 | if ( $field_value !== '' ) { |
| 182 | return false; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return true; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Append field to address format. |
| 191 | * |
| 192 | * @param string $format |
| 193 | * @param string $field_key |
| 194 | * @param array $field |
| 195 | * |
| 196 | * @return string |
| 197 | */ |
| 198 | private function append_field_to_address_format( $format, $field_key, $field ) { |
| 199 | if ( ( $this->is_thankyou_page() || $this->is_in_email() || $this->is_order_page() ) |
| 200 | && in_array( $field_key, [ 'billing_phone', 'billing_email' ] ) |
| 201 | ) { |
| 202 | return $format; |
| 203 | } |
| 204 | $fcf_field = new Flexible_Checkout_Fields_Field( $field, $this->plugin ); |
| 205 | if ( isset( $field['type'] ) && in_array( $field['type'], [ 'heading', 'info', 'paragraph', 'image' ] ) ) { |
| 206 | return $format; |
| 207 | } |
| 208 | if ( $this->is_field_displayable( $field ) ) { |
| 209 | if ( $format != '' ) { |
| 210 | if ( $fcf_field->get_display_on_option_new_line_before() === '1' ) { |
| 211 | $format .= "\n"; |
| 212 | } elseif ( ! $fcf_field->get_display_comma_before() ) { |
| 213 | $format .= ' '; |
| 214 | } |
| 215 | } |
| 216 | if ( $fcf_field->get_display_comma_before() ) { |
| 217 | $format .= ', '; |
| 218 | } |
| 219 | $field_name = $fcf_field->get_name_for_address_format(); |
| 220 | $format .= '{' . $this->replace_only_first( $this->current_address_type . '_', '', $field_name . '}' ); |
| 221 | } |
| 222 | |
| 223 | return $format; |
| 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Localisation address formats - woocommerce filter. |
| 228 | * |
| 229 | * @param array $formats |
| 230 | * |
| 231 | * @return array |
| 232 | */ |
| 233 | public function woocommerce_localisation_address_formats_filter( $formats ) { |
| 234 | $fields = $this->getCheckoutFields( [], $this->current_address_type, $this->order ); |
| 235 | if ( empty( $fields ) ) { |
| 236 | return $formats; |
| 237 | } |
| 238 | |
| 239 | foreach ( $formats as $format_key => $format ) { |
| 240 | if ( $this->is_edit_address_page() |
| 241 | || $this->is_order_page() |
| 242 | || $this->is_in_email() |
| 243 | || $this->is_thankyou_page() |
| 244 | ) { |
| 245 | $formats[ $format_key ] = ''; |
| 246 | foreach ( $fields as $field_key => $field ) { |
| 247 | $formats[ $format_key ] = $this->append_field_to_address_format( $formats[ $format_key ], $field_key, $field ); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return $formats; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Checks if field should be displayed on a specific page. |
| 257 | * |
| 258 | * @param array $field |
| 259 | * |
| 260 | * @return bool |
| 261 | */ |
| 262 | private function is_field_displayable( $field ) { |
| 263 | if ( $this->is_edit_address_page() ) { |
| 264 | $fcf_field = new Flexible_Checkout_Fields_Field( $field, $this->plugin ); |
| 265 | if ( $fcf_field->is_field_excluded_for_user() ) { |
| 266 | return false; |
| 267 | } |
| 268 | return isset( $field[ self::DISPLAY_ON_ADDRESS ] ) && $field[ self::DISPLAY_ON_ADDRESS ] == '1'; |
| 269 | } |
| 270 | if ( $this->is_order_page() ) { |
| 271 | return isset( $field[ self::DISPLAY_ON_ORDER ] ) && $field[ self::DISPLAY_ON_ORDER ] == '1'; |
| 272 | } |
| 273 | if ( $this->is_in_email() ) { |
| 274 | return isset( $field[ self::DISPLAY_ON_EMAILS ] ) && $field[ self::DISPLAY_ON_EMAILS ] == '1'; |
| 275 | } |
| 276 | if ( $this->is_thankyou_page() ) { |
| 277 | return isset( $field[ self::DISPLAY_ON_THANK_YOU ] ) && $field[ self::DISPLAY_ON_THANK_YOU ] == '1'; |
| 278 | } |
| 279 | // default. |
| 280 | return true; |
| 281 | } |
| 282 | |
| 283 | public function is_admin_edit_order() { |
| 284 | $admin_edit_order = false; |
| 285 | if ( is_admin() ) { |
| 286 | $admin_edit_order = true; |
| 287 | } |
| 288 | return $admin_edit_order; |
| 289 | } |
| 290 | |
| 291 | public function is_edit_address_page() { |
| 292 | global $wp; |
| 293 | $edit_address_page = false; |
| 294 | if ( is_account_page() ) { |
| 295 | if ( isset( $wp->query_vars['edit-address'] ) ) { |
| 296 | $edit_address_page = true; |
| 297 | } |
| 298 | } |
| 299 | return $edit_address_page; |
| 300 | } |
| 301 | |
| 302 | public function is_order_page() { |
| 303 | global $wp; |
| 304 | $order_page = false; |
| 305 | if ( is_account_page() ) { |
| 306 | if ( isset( $wp->query_vars['view-order'] ) ) { |
| 307 | $order_page = true; |
| 308 | } |
| 309 | } |
| 310 | return $order_page; |
| 311 | } |
| 312 | |
| 313 | public function is_in_email() { |
| 314 | $in_email = false; |
| 315 | if ( $this->in_email_address ) { |
| 316 | $in_email = true; |
| 317 | } |
| 318 | return $in_email; |
| 319 | } |
| 320 | |
| 321 | public function is_thankyou_page() { |
| 322 | global $wp; |
| 323 | $thankyou_page = false; |
| 324 | if ( is_checkout() ) { |
| 325 | if ( isset( $wp->query_vars['order-received'] ) ) { |
| 326 | $thankyou_page = true; |
| 327 | } |
| 328 | } |
| 329 | return $thankyou_page; |
| 330 | } |
| 331 | |
| 332 | public function woocommerce_formatted_address_replacements( $fields, $args ) { |
| 333 | foreach ( $args as $arg_key => $arg ) { |
| 334 | if ( ! isset( $fields[ '{' . $arg_key . '}' ] ) ) { |
| 335 | $fields[ '{' . $arg_key . '}' ] = $arg; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | return $fields; |
| 340 | } |
| 341 | |
| 342 | public function woocommerce_order_formatted_billing_address( $fields, $order ) { |
| 343 | $this->order = $order; |
| 344 | $this->current_address_type = 'billing'; |
| 345 | WC()->countries->address_formats = ''; |
| 346 | return $this->woocommerce_order_formatted_address( $fields, $order, 'billing' ); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * @param array $fields |
| 351 | * @param WC_Order $order |
| 352 | * @param string $address_type |
| 353 | * |
| 354 | * @return mixed |
| 355 | */ |
| 356 | public function woocommerce_order_formatted_address( $fields, $order, $address_type ) { |
| 357 | |
| 358 | $cf_fields = $this->getCheckoutFields( [], $address_type, $order ); |
| 359 | |
| 360 | foreach ( $cf_fields as $field_key => $field ) { |
| 361 | if ( ! isset( $field['name'] ) ) { |
| 362 | continue; |
| 363 | } |
| 364 | |
| 365 | $val = wpdesk_get_order_meta( $order, '_' . $field_key, true ); |
| 366 | if ( empty( $val ) && isset( $fields[ $field_key ] ) ) { |
| 367 | $val = $fields[ $field_key ]; |
| 368 | } |
| 369 | |
| 370 | $fcf_field = new Flexible_Checkout_Fields_Field( $field, $this->plugin ); |
| 371 | if ( ( isset( $field['custom_field'] ) && $field['custom_field'] == '1' ) ) { |
| 372 | $val = ''; |
| 373 | if ( $fcf_field->is_custom_field() && $fcf_field->get_display_on_option_show_label() === '1' ) { |
| 374 | $val = strip_tags( wpdesk__( $field['label'], 'flexible-checkout-fields' ) ) . ': '; |
| 375 | } |
| 376 | |
| 377 | $meta_value = wpdesk_get_order_meta( $order, '_' . $field_key, true ); |
| 378 | $meta_value = apply_filters( 'flexible_checkout_fields_print_value', $meta_value, $field ); |
| 379 | $val .= $meta_value; |
| 380 | } |
| 381 | |
| 382 | $val = $this->flexible_invoices_ask_field_integration( $val, $field, $field_key, $fields ); |
| 383 | $val = wp_kses_post( $val ); |
| 384 | |
| 385 | $fields[ $field['name'] ] = $val; |
| 386 | $fields[ $this->replace_only_first( $address_type . '_', '', $field['name'] ) ] = $val; |
| 387 | } |
| 388 | |
| 389 | return $fields; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Similar to str_replace but replaces only the first occurrence. |
| 394 | * |
| 395 | * @param string $needle search for it. |
| 396 | * @param string $replace change the needle to this value. |
| 397 | * @param string $haystack here we are searching |
| 398 | * |
| 399 | * @return string |
| 400 | */ |
| 401 | private function replace_only_first( $needle, $replace, $haystack ) { |
| 402 | $pos = strpos( $haystack, $needle ); |
| 403 | if ( $pos !== false ) { |
| 404 | return substr_replace( $haystack, $replace, $pos, strlen( $needle ) ); |
| 405 | } |
| 406 | return $haystack; |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Return value for invoice ask field prepared by FI plugin. If can't then fallback. |
| 411 | * |
| 412 | * @param string $val Prepared by FCF field value. |
| 413 | * @param array $field FCF field def. |
| 414 | * @param string $field_key Field key that is currently processed. Needed to check if val should be replaced. |
| 415 | * @param array $fields Prepared by WC field values |
| 416 | * |
| 417 | * @return string New field value |
| 418 | */ |
| 419 | private function flexible_invoices_ask_field_integration( $val, $field, $field_key, $fields ) { |
| 420 | if ( apply_filters( 'flexible_checkout_fields_invoices_integration_enabled', true ) ) { |
| 421 | |
| 422 | // FI ask field integration |
| 423 | if ( $field_key === 'invoice_ask' && ! empty( $fields['invoice_ask_field'] ) ) { |
| 424 | return $fields['invoice_ask_field']; |
| 425 | } |
| 426 | |
| 427 | // wFirma/Fakturownia/iFirma/inFakt ask field integration |
| 428 | $supported_ask_fields = [ |
| 429 | 'billing_faktura', |
| 430 | 'billing_invoice', |
| 431 | 'billing_rachunek', |
| 432 | ]; |
| 433 | if ( in_array( $field_key, $supported_ask_fields, true ) ) { |
| 434 | $wc_meta_key_definitions = apply_filters( 'woocommerce_customer_meta_fields', [] ); |
| 435 | |
| 436 | $label = strip_tags( wpdesk__( $field['label'], 'flexible-checkout-fields' ) ); |
| 437 | |
| 438 | // original plugin is probably(?) disabled if the field is not accessible |
| 439 | if ( isset( $wc_meta_key_definitions[ $this->current_address_type ]['fields'][ $field_key ] ) ) { |
| 440 | $wc_field_def = $wc_meta_key_definitions[ $this->current_address_type ]['fields'][ $field_key ]; |
| 441 | |
| 442 | // 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 |
| 443 | if ( isset( $wc_field_def['options'] ) ) { |
| 444 | $select_options = $wc_meta_key_definitions[ $this->current_address_type ]['fields'][ $field_key ]['options']; |
| 445 | $option_val = isset( $select_options[ $val ] ) ? $select_options[ $val ] : ''; |
| 446 | |
| 447 | return $label . ': ' . $option_val; |
| 448 | } elseif ( (int) $val === 1 ) { |
| 449 | return $label; |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | return $val; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * Mainly injects FCF data values into WC formatted shipping address. |
| 460 | * Also changes current_address_type indicator and do some shady stuff to WC()->countries. |
| 461 | * |
| 462 | * @param array|string $fields Fields can be string when shipment is disabled |
| 463 | * @param WC_Order $order |
| 464 | * |
| 465 | * @return array|string |
| 466 | */ |
| 467 | public function woocommerce_order_formatted_shipping_address( $fields, $order ) { |
| 468 | $this->order = $order; |
| 469 | $this->current_address_type = 'shipping'; |
| 470 | WC()->countries->address_formats = ''; |
| 471 | if ( ! is_array( $fields ) ) { |
| 472 | return $fields; |
| 473 | } |
| 474 | |
| 475 | return $this->woocommerce_order_formatted_address( $fields, $order, 'shipping' ); |
| 476 | } |
| 477 | |
| 478 | public function woocommerce_billing_fields( $fields ) { |
| 479 | return $this->woocommerce_fields( $fields, 'billing' ); |
| 480 | } |
| 481 | |
| 482 | public function woocommerce_shipping_fields( $fields ) { |
| 483 | return $this->woocommerce_fields( $fields, 'shipping' ); |
| 484 | } |
| 485 | |
| 486 | protected function woocommerce_fields( $fields, $section ) { |
| 487 | $cf_fields = $this->getCheckoutFields( [], $section ); |
| 488 | foreach ( $cf_fields as $cf_field_key => $cf_field ) { |
| 489 | if ( ! $this->is_field_displayable( $cf_field ) ) { |
| 490 | unset( $fields[ $cf_field_key ] ); |
| 491 | } |
| 492 | } |
| 493 | return $fields; |
| 494 | } |
| 495 | } |
| 496 |