| 1 |
<?php |
| 2 |
|
| 3 |
class WooMailerLitePluginController extends WooMailerLiteController |
| 4 |
{ |
| 5 |
protected $position = ''; |
| 6 |
protected $label = ''; |
| 7 |
protected $preselect = ''; |
| 8 |
protected $hidden = ''; |
| 9 |
protected $pluginEnabled = false; |
| 10 |
protected $checkoutEnabled = ''; |
| 11 |
protected $group = false; |
| 12 |
|
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
$this->position = WooMailerLiteOptions::get('settings.selectedCheckoutPosition', 'checkout_billing'); |
| 16 |
$this->label = strip_tags(stripslashes(WooMailerLiteOptions::get('settings.checkoutLabel'))); |
| 17 |
$this->hidden = WooMailerLiteOptions::get('settings.checkoutHidden'); |
| 18 |
$this->preselect = WooMailerLiteOptions::get('settings.checkoutPreselect'); |
| 19 |
$this->pluginEnabled = WooMailerLiteOptions::get('enabled'); |
| 20 |
$this->checkoutEnabled = WooMailerLiteOptions::get('settings.subscribeOnCheckout'); |
| 21 |
$this->group = WooMailerLiteOptions::get('group' , false); |
| 22 |
} |
| 23 |
public function addMlSubscribeCheckbox() |
| 24 |
{ |
| 25 |
if (!$this->pluginEnabled || !$this->checkoutEnabled || ($this->position == 'checkout_billing_email') || !$this->group) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
if ($this->hidden) { |
| 30 |
?> |
| 31 |
<input name="woo_ml_subscribe" type="hidden" id="woo_ml_subscribe" value="1" checked="checked"/> |
| 32 |
<?php |
| 33 |
} else { |
| 34 |
woocommerce_form_field('woo_ml_subscribe', array( |
| 35 |
'type' => 'checkbox', |
| 36 |
'label' => __($this->label, 'woo-mailerlite'), |
| 37 |
'checked' => $this->preselect ? 'checked' : '', |
| 38 |
'label_class' => ['woocommerce-form__label-for-checkbox'], |
| 39 |
), (bool) $this->preselect); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
public function addBillingCheckoutFields($fields) |
| 44 |
{ |
| 45 |
if (!$this->pluginEnabled || !$this->checkoutEnabled || !$this->group || ($this->position !== 'checkout_billing_email')) { |
| 46 |
return $fields; |
| 47 |
} |
| 48 |
$new_billing_fields = []; |
| 49 |
|
| 50 |
foreach ($fields['billing'] as $key => $field) { |
| 51 |
$new_billing_fields[$key] = $field; |
| 52 |
if ($key === 'billing_email') { |
| 53 |
|
| 54 |
$new_billing_fields['woo_ml_subscribe'] = [ |
| 55 |
'type' => (!$this->hidden) ? 'checkbox' : 'hidden', |
| 56 |
'default' => $this->preselect, |
| 57 |
'required' => false, |
| 58 |
'label_class' => ['woocommerce-form__label-for-checkbox'], |
| 59 |
]; |
| 60 |
|
| 61 |
if (!$this->hidden) { |
| 62 |
$new_billing_fields['woo_ml_subscribe']['label'] = __($this->label, 'woo-mailerlite'); |
| 63 |
} else { |
| 64 |
$new_billing_fields['woo_ml_subscribe']['custom_attributes'] = [ |
| 65 |
'checked' => 'checked', |
| 66 |
]; |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
$fields['billing'] = $new_billing_fields; |
| 72 |
|
| 73 |
return $fields; |
| 74 |
} |
| 75 |
|
| 76 |
public function reloadCheckout() |
| 77 |
{ |
| 78 |
try { |
| 79 |
if (!function_exists('WC') || !is_object(WC()->session)) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
if ($this->requestHas('ml_checkout') && (ctype_digit($this->request['ml_checkout']) || wp_is_uuid($this->request['ml_checkout']))) { |
| 84 |
$escaped = db()->esc_like($this->request['ml_checkout']); |
| 85 |
$escaped = '%checkout_id":"' . addcslashes($escaped, '%_') . '"%'; |
| 86 |
$cart = WooMailerLiteCart::where('data', 'like', $escaped)->first(); |
| 87 |
if ($cart && $cart->exists()) { |
| 88 |
$cartData = $cart->data; |
| 89 |
unset($cartData['checkout_id']); |
| 90 |
WC()->session->set('cart', $cartData); |
| 91 |
} |
| 92 |
} |
| 93 |
} catch (Throwable $e) { |
| 94 |
WooMailerLiteLog()->error('Error restoring cart from checkout ID: ' . $e->getMessage(), ['trace' => $e->getTraceAsString()]); |
| 95 |
} |
| 96 |
return true; |
| 97 |
} |
| 98 |
} |
| 99 |
|