| 1 |
<?php |
| 2 |
/** |
| 3 |
* WooCommerce ↔ Form Builder bridge. |
| 4 |
* |
| 5 |
* Gates the two address field types, injects the checkout toggle on the classic |
| 6 |
* allowlist, and ships billing/shipping sub-field lists for the address blocks. |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 10 |
|
| 11 |
// Loaded by wppb_fb_load_addon_integrations(); do not include directly. |
| 12 |
|
| 13 |
/* 1. Gating — both address field types active. */ |
| 14 |
add_filter( 'wppb_fb_enabled_addon_field_types', 'wppb_in_woo_fb_enable_field_types' ); |
| 15 |
function wppb_in_woo_fb_enable_field_types( $types ) { |
| 16 |
$types[] = 'WooCommerce Customer Billing Address'; |
| 17 |
$types[] = 'WooCommerce Customer Shipping Address'; |
| 18 |
return $types; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Field types that get the "Display on WooCommerce Checkout" toggle. |
| 23 |
* Mirrors WooCheckoutFields() in assets/js/main.js (shorter list for block checkout). |
| 24 |
* Cached — block-checkout probe hits the DB and the attributes filter fires per type. |
| 25 |
*/ |
| 26 |
function wppb_in_woo_fb_checkout_field_types() { |
| 27 |
static $types = null; |
| 28 |
if ( null !== $types ) { |
| 29 |
return $types; |
| 30 |
} |
| 31 |
|
| 32 |
$is_block_checkout = function_exists( 'wppb_is_woocommerce_block_checkout' ) && wppb_is_woocommerce_block_checkout(); |
| 33 |
|
| 34 |
if ( $is_block_checkout ) { |
| 35 |
$types = array( |
| 36 |
'Input', |
| 37 |
'Number', |
| 38 |
'Phone', |
| 39 |
'Select', |
| 40 |
'Checkbox (Terms and Conditions)', |
| 41 |
); |
| 42 |
} else { |
| 43 |
$types = array( |
| 44 |
'Default - First Name', |
| 45 |
'Default - Last Name', |
| 46 |
'Default - Nickname', |
| 47 |
'Default - Biographical Info', |
| 48 |
'Default - Website', |
| 49 |
'Default - About Yourself (Heading)', |
| 50 |
'Input', |
| 51 |
'Input (Hidden)', |
| 52 |
'Textarea', |
| 53 |
'Checkbox', |
| 54 |
'Checkbox (Terms and Conditions)', |
| 55 |
'Select', |
| 56 |
'Radio', |
| 57 |
'Heading', |
| 58 |
'Datepicker', |
| 59 |
'Phone', |
| 60 |
'Number', |
| 61 |
'Avatar', |
| 62 |
'Upload', |
| 63 |
'MailChimp Subscribe', |
| 64 |
'Select (Multiple)', |
| 65 |
'WYSIWYG', |
| 66 |
'Select (Country)', |
| 67 |
'Select (Timezone)', |
| 68 |
'Select (Currency)', |
| 69 |
'Select (CPT)', |
| 70 |
'Select (User Role)', |
| 71 |
'Timepicker', |
| 72 |
'Colorpicker', |
| 73 |
'Map', |
| 74 |
'HTML', |
| 75 |
'Repeater', |
| 76 |
'Validation', |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
return $types; |
| 81 |
} |
| 82 |
|
| 83 |
/* |
| 84 |
* Classic: no checkout toggle on Repeater sub-fields. Strip on flatten; hideInsideRepeater suppresses the control. |
| 85 |
*/ |
| 86 |
add_filter( 'wppb_fb_repeater_subfield_stripped_attrs', 'wppb_in_woo_fb_strip_subfield_attrs' ); |
| 87 |
function wppb_in_woo_fb_strip_subfield_attrs( $stripped ) { |
| 88 |
$stripped[] = 'woocommerce-checkout-field'; |
| 89 |
return $stripped; |
| 90 |
} |
| 91 |
|
| 92 |
/* 2. Checkout toggle — inject into allowlisted field blocks. */ |
| 93 |
add_filter( 'wppb_fb_block_attributes', 'wppb_in_woo_fb_inject_checkout_attr', 10, 3 ); |
| 94 |
function wppb_in_woo_fb_inject_checkout_attr( $attributes, $field_type, $context ) { |
| 95 |
if ( ! in_array( $field_type, wppb_in_woo_fb_checkout_field_types(), true ) ) { |
| 96 |
return $attributes; |
| 97 |
} |
| 98 |
if ( ! isset( $attributes['woocommerce-checkout-field'] ) ) { |
| 99 |
$attributes['woocommerce-checkout-field'] = array( 'type' => 'string', 'default' => 'No' ); |
| 100 |
} |
| 101 |
return $attributes; |
| 102 |
} |
| 103 |
|
| 104 |
add_action( 'wppb_fb_enqueue_editor_assets_late', 'wppb_in_woo_fb_enqueue_editor_payload' ); |
| 105 |
function wppb_in_woo_fb_enqueue_editor_payload() { |
| 106 |
$allowed = array_intersect( wppb_in_woo_fb_checkout_field_types(), wppb_fb_all_field_type_labels() ); |
| 107 |
if ( ! empty( $allowed ) ) { |
| 108 |
$schema = array( 'woocommerce-checkout-field' => array( 'type' => 'string', 'default' => 'No' ) ); |
| 109 |
$extra = array(); |
| 110 |
foreach ( $allowed as $ft ) { |
| 111 |
$extra[ $ft ] = $schema; |
| 112 |
} |
| 113 |
wppb_fb_extra_attributes_inline_script( $extra ); |
| 114 |
} |
| 115 |
wppb_fb_register_extra_field_control( array( |
| 116 |
'attribute' => 'woocommerce-checkout-field', |
| 117 |
'label' => __( 'Display on WooCommerce Checkout', 'profile-builder' ), |
| 118 |
'help' => __( 'Add this field to the WooCommerce checkout form.', 'profile-builder' ), |
| 119 |
'control' => 'toggle', |
| 120 |
'onValue' => 'Yes', |
| 121 |
'offValue' => 'No', |
| 122 |
// Classic strips this from Repeater sub-fields; pair with stripped-attrs above. |
| 123 |
'hideInsideRepeater' => true, |
| 124 |
) ); |
| 125 |
|
| 126 |
$payload = array( |
| 127 |
'billingFields' => wppb_in_woo_fb_field_list( 'billing' ), |
| 128 |
'shippingFields' => wppb_in_woo_fb_field_list( 'shipping' ), |
| 129 |
); |
| 130 |
wp_add_inline_script( |
| 131 |
'wppb-form-editor-bundle', |
| 132 |
'window.wppbFb = window.wppbFb || {}; window.wppbFb.woocommerce = ' . wp_json_encode( $payload ) . ';', |
| 133 |
'before' |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
/* Convert legacy { key => [label, required] } to [ {key,label,required} ] for the editor. */ |
| 138 |
function wppb_in_woo_fb_field_list( $kind ) { |
| 139 |
$map = array(); |
| 140 |
if ( $kind === 'billing' && function_exists( 'wppb_in_woo_get_billing_fields' ) ) { |
| 141 |
$map = wppb_in_woo_get_billing_fields(); |
| 142 |
} elseif ( $kind === 'shipping' && function_exists( 'wppb_in_woo_get_shipping_fields' ) ) { |
| 143 |
$map = wppb_in_woo_get_shipping_fields(); |
| 144 |
} |
| 145 |
$out = array(); |
| 146 |
if ( is_array( $map ) ) { |
| 147 |
foreach ( $map as $key => $def ) { |
| 148 |
$out[] = array( |
| 149 |
'key' => (string) $key, |
| 150 |
'label' => isset( $def['label'] ) ? $def['label'] : (string) $key, |
| 151 |
'required' => isset( $def['required'] ) ? $def['required'] : 'No', |
| 152 |
); |
| 153 |
} |
| 154 |
} |
| 155 |
return $out; |
| 156 |
} |
| 157 |
|