| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Checkout ACF Extra Fields widget. |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Widget_Base; |
| 12 |
use King_Addons\Core; |
| 13 |
use King_Addons\Woo_Builder\Context as Woo_Context; |
| 14 |
|
| 15 |
if (!defined('ABSPATH')) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Placeholder ACF fields for checkout (Pro-only rendering). |
| 21 |
*/ |
| 22 |
class Woo_Checkout_ACF_Fields extends Widget_Base |
| 23 |
{ |
| 24 |
/** |
| 25 |
* Register Pro-only controls (placeholder). |
| 26 |
* |
| 27 |
* Pro overrides this method to add premium controls without using parent::register_controls(). |
| 28 |
* |
| 29 |
* @return void |
| 30 |
*/ |
| 31 |
public function register_pro_controls(): void |
| 32 |
{ |
| 33 |
// Intentionally empty in Free. |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Widget slug. |
| 38 |
* |
| 39 |
* @return string |
| 40 |
*/ |
| 41 |
public function get_name(): string |
| 42 |
{ |
| 43 |
return 'woo_checkout_acf_fields'; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Widget title. |
| 48 |
* |
| 49 |
* @return string |
| 50 |
*/ |
| 51 |
public function get_title(): string |
| 52 |
{ |
| 53 |
return esc_html__('Checkout ACF Extra Fields', 'king-addons'); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Widget icon. |
| 58 |
* |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
public function get_icon(): string |
| 62 |
{ |
| 63 |
return 'eicon-field-text'; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Styles. |
| 68 |
* |
| 69 |
* @return array<int,string> |
| 70 |
*/ |
| 71 |
public function get_style_depends(): array |
| 72 |
{ |
| 73 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-checkout-acf-fields-style']; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Widget categories. |
| 78 |
* |
| 79 |
* @return array<int,string> |
| 80 |
*/ |
| 81 |
public function get_categories(): array |
| 82 |
{ |
| 83 |
return ['king-addons-woo-builder']; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Register controls. |
| 88 |
* |
| 89 |
* @return void |
| 90 |
*/ |
| 91 |
public function get_custom_help_url() |
| 92 |
{ |
| 93 |
return 'mailto:[email protected]?subject=Bug Report - King Addons&body=Please describe the issue'; |
| 94 |
} |
| 95 |
|
| 96 |
protected function register_controls(): void |
| 97 |
{ |
| 98 |
$this->start_controls_section( |
| 99 |
'section_content', |
| 100 |
[ |
| 101 |
'label' => esc_html__('Content', 'king-addons'), |
| 102 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 103 |
] |
| 104 |
); |
| 105 |
|
| 106 |
$this->add_control( |
| 107 |
'heading', |
| 108 |
[ |
| 109 |
'label' => esc_html__('Heading', 'king-addons'), |
| 110 |
'type' => Controls_Manager::TEXT, |
| 111 |
'default' => esc_html__('Extra Information', 'king-addons'), |
| 112 |
] |
| 113 |
); |
| 114 |
|
| 115 |
$this->add_control( |
| 116 |
'field_keys', |
| 117 |
[ |
| 118 |
'label' => esc_html__('ACF Field Keys (Pro)', 'king-addons'), |
| 119 |
'type' => Controls_Manager::TEXT, |
| 120 |
'placeholder' => 'field_123abc, my_field', |
| 121 |
] |
| 122 |
); |
| 123 |
|
| 124 |
$this->add_control( |
| 125 |
'placement', |
| 126 |
[ |
| 127 |
'label' => esc_html__('Placement', 'king-addons'), |
| 128 |
'type' => Controls_Manager::SELECT, |
| 129 |
'options' => [ |
| 130 |
'before_billing' => esc_html__('Before billing', 'king-addons'), |
| 131 |
'after_billing' => esc_html__('After billing', 'king-addons'), |
| 132 |
'before_shipping' => esc_html__('Before shipping', 'king-addons'), |
| 133 |
'after_shipping' => esc_html__('After shipping', 'king-addons'), |
| 134 |
'before_order' => esc_html__('Before order notes', 'king-addons'), |
| 135 |
'after_order' => esc_html__('After order notes', 'king-addons'), |
| 136 |
], |
| 137 |
'default' => 'after_order', |
| 138 |
] |
| 139 |
); |
| 140 |
|
| 141 |
$this->add_control( |
| 142 |
'required_notice', |
| 143 |
[ |
| 144 |
'label' => sprintf(__('Show required mark %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 145 |
'type' => Controls_Manager::SWITCHER, |
| 146 |
'return_value' => 'yes', |
| 147 |
] |
| 148 |
); |
| 149 |
|
| 150 |
$this->end_controls_section(); |
| 151 |
|
| 152 |
$this->start_controls_section( |
| 153 |
'section_style', |
| 154 |
[ |
| 155 |
'label' => esc_html__('Style', 'king-addons'), |
| 156 |
'tab' => Controls_Manager::TAB_STYLE, |
| 157 |
] |
| 158 |
); |
| 159 |
|
| 160 |
$this->add_control( |
| 161 |
'gap', |
| 162 |
[ |
| 163 |
'label' => esc_html__('Fields gap', 'king-addons'), |
| 164 |
'type' => Controls_Manager::SLIDER, |
| 165 |
'range' => [ |
| 166 |
'px' => ['min' => 0, 'max' => 40], |
| 167 |
], |
| 168 |
'selectors' => [ |
| 169 |
'{{WRAPPER}} .ka-woo-checkout-acf-fields' => 'gap: {{SIZE}}{{UNIT}};', |
| 170 |
], |
| 171 |
] |
| 172 |
); |
| 173 |
|
| 174 |
$this->add_control( |
| 175 |
'label_color', |
| 176 |
[ |
| 177 |
'label' => esc_html__('Label color', 'king-addons'), |
| 178 |
'type' => Controls_Manager::COLOR, |
| 179 |
'selectors' => [ |
| 180 |
'{{WRAPPER}} .ka-woo-checkout-acf-fields label' => 'color: {{VALUE}};', |
| 181 |
], |
| 182 |
] |
| 183 |
); |
| 184 |
|
| 185 |
$this->add_control( |
| 186 |
'required_color', |
| 187 |
[ |
| 188 |
'label' => esc_html__('Required mark color', 'king-addons'), |
| 189 |
'type' => Controls_Manager::COLOR, |
| 190 |
'selectors' => [ |
| 191 |
'{{WRAPPER}} .ka-woo-checkout-acf-fields .required' => 'color: {{VALUE}};', |
| 192 |
], |
| 193 |
] |
| 194 |
); |
| 195 |
|
| 196 |
$this->end_controls_section(); |
| 197 |
|
| 198 |
// Allow Pro to add additional controls without using parent::register_controls(). |
| 199 |
$this->register_pro_controls(); |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Render widget output (free notice). |
| 204 |
* |
| 205 |
* @return void |
| 206 |
*/ |
| 207 |
protected function render(): void |
| 208 |
{ |
| 209 |
if (!Woo_Context::maybe_render_context_notice('checkout')) { |
| 210 |
return; |
| 211 |
} |
| 212 |
|
| 213 |
$in_builder = class_exists('King_Addons\\Woo_Builder\\Context') && Woo_Context::is_editing_template_type('checkout'); |
| 214 |
if (!function_exists('is_checkout') || (!is_checkout() && !$in_builder) || (function_exists('is_order_received_page') && is_order_received_page())) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
if (!king_addons_can_use_pro()) { |
| 219 |
if (Woo_Context::is_editor()) { |
| 220 |
echo '<div class="king-addons-woo-builder-notice">'; |
| 221 |
echo esc_html__('ACF extra fields for checkout are available in Pro.', 'king-addons'); |
| 222 |
echo '</div>'; |
| 223 |
} |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
$settings = $this->get_settings_for_display(); |
| 228 |
$placement = $settings['placement'] ?? 'after_order'; |
| 229 |
$required = !empty($settings['required_notice']); |
| 230 |
$fields_raw = $settings['field_keys'] ?? ''; |
| 231 |
$fields = []; |
| 232 |
if (!empty($fields_raw)) { |
| 233 |
$parts = explode(',', $fields_raw); |
| 234 |
$fields = array_filter(array_map('trim', $parts)); |
| 235 |
} |
| 236 |
|
| 237 |
$wrap = '<div class="ka-woo-checkout-acf-fields" data-ka-acf="1" data-placement="' . esc_attr($placement) . '" data-required="' . ($required ? '1' : '0') . '">'; |
| 238 |
if (!empty($settings['heading'])) { |
| 239 |
$wrap .= '<h4 class="ka-woo-checkout-acf-fields__heading">' . esc_html($settings['heading']) . '</h4>'; |
| 240 |
} |
| 241 |
/** |
| 242 |
* Render ACF fields at checkout. |
| 243 |
* |
| 244 |
* Developers can hook into this action to output ACF forms/fields. |
| 245 |
*/ |
| 246 |
ob_start(); |
| 247 |
do_action('king_addons_checkout_acf_fields', $fields, $required, $placement); |
| 248 |
$wrap .= ob_get_clean(); |
| 249 |
$wrap .= '</div>'; |
| 250 |
|
| 251 |
if ('before_billing' === $placement) { |
| 252 |
add_action('woocommerce_before_checkout_billing_form', static function () use ($wrap): void { |
| 253 |
echo $wrap; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 254 |
}); |
| 255 |
return; |
| 256 |
} |
| 257 |
if ('after_billing' === $placement) { |
| 258 |
add_action('woocommerce_after_checkout_billing_form', static function () use ($wrap): void { |
| 259 |
echo $wrap; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 260 |
}); |
| 261 |
return; |
| 262 |
} |
| 263 |
if ('before_shipping' === $placement) { |
| 264 |
add_action('woocommerce_before_checkout_shipping_form', static function () use ($wrap): void { |
| 265 |
echo $wrap; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 266 |
}); |
| 267 |
return; |
| 268 |
} |
| 269 |
if ('after_shipping' === $placement) { |
| 270 |
add_action('woocommerce_after_checkout_shipping_form', static function () use ($wrap): void { |
| 271 |
echo $wrap; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 272 |
}); |
| 273 |
return; |
| 274 |
} |
| 275 |
if ('before_order' === $placement) { |
| 276 |
add_action('woocommerce_before_order_notes', static function () use ($wrap): void { |
| 277 |
echo $wrap; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 278 |
}); |
| 279 |
return; |
| 280 |
} |
| 281 |
// default after_order |
| 282 |
add_action('woocommerce_after_order_notes', static function () use ($wrap): void { |
| 283 |
echo $wrap; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 284 |
}); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|