| 1 |
<?php |
| 2 |
|
| 3 |
use MailerLite\Includes\Classes\Process\CartProcess; |
| 4 |
use MailerLite\Includes\Classes\Process\OrderProcess; |
| 5 |
use MailerLite\Includes\Classes\Process\OrderSyncProcess; |
| 6 |
use MailerLite\Includes\Classes\Process\ProductProcess; |
| 7 |
use MailerLite\Includes\Classes\Settings\MailerLiteSettings; |
| 8 |
use MailerLite\Includes\Classes\Settings\SynchronizeSettings; |
| 9 |
|
| 10 |
|
| 11 |
/** |
| 12 |
* Shows the final purchase total at the bottom of the checkout page |
| 13 |
* |
| 14 |
* @return void |
| 15 |
* @since 1.5 |
| 16 |
*/ |
| 17 |
function woo_ml_checkout_label() |
| 18 |
{ |
| 19 |
if ( ! MailerLiteSettings::getInstance()->isActive()) { |
| 20 |
return; |
| 21 |
} |
| 22 |
|
| 23 |
$checkout = MailerLiteSettings::getInstance()->getMlOption('checkout', 'no'); |
| 24 |
|
| 25 |
if ('yes' != $checkout) { |
| 26 |
return; |
| 27 |
} |
| 28 |
|
| 29 |
$group = MailerLiteSettings::getInstance()->getMlOption('group'); |
| 30 |
|
| 31 |
if (empty($group)) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
$label = MailerLiteSettings::getInstance()->getMlOption('checkout_label'); |
| 36 |
$preselect = MailerLiteSettings::getInstance()->getMlOption('checkout_preselect', 'no'); |
| 37 |
$hidden = MailerLiteSettings::getInstance()->getMlOption('checkout_hide', 'no'); |
| 38 |
|
| 39 |
if ('yes' === $hidden) { |
| 40 |
?> |
| 41 |
<input name="woo_ml_subscribe" type="hidden" id="woo_ml_subscribe" value="1" checked="checked"/> |
| 42 |
<?php |
| 43 |
} else { |
| 44 |
|
| 45 |
woocommerce_form_field('woo_ml_subscribe', array( |
| 46 |
'type' => 'checkbox', |
| 47 |
'label' => __($label), |
| 48 |
), (bool) ($_COOKIE['mailerlite_accepts_marketing'] ?? 'yes' === $preselect)); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
$checkout_position = MailerLiteSettings::getInstance()->getMlOption('checkout_position', 'checkout_billing'); |
| 53 |
$checkout_position_hook = 'woocommerce_' . $checkout_position; |
| 54 |
add_action($checkout_position_hook, 'woo_ml_checkout_label', 20); |
| 55 |
|
| 56 |
/** |
| 57 |
* Remove (optional) string for ML label |
| 58 |
* |
| 59 |
*/ |
| 60 |
function remove_ml_checkout_optional_text($field, $key, $args, $value) |
| 61 |
{ |
| 62 |
|
| 63 |
if (is_checkout() && ! is_wc_endpoint_url() && strpos($field, |
| 64 |
'woo_ml_subscribe') !== false && get_option('ml_account_authenticated')) { |
| 65 |
|
| 66 |
$optional = ' <span class="optional">(' . esc_html__('optional', 'woocommerce') . ')</span>'; |
| 67 |
$field = str_replace($optional, '', $field); |
| 68 |
} |
| 69 |
|
| 70 |
return $field; |
| 71 |
} |
| 72 |
|
| 73 |
add_filter('woocommerce_form_field', 'remove_ml_checkout_optional_text', 10, 4); |
| 74 |
|
| 75 |
/** |
| 76 |
* Maybe prepare signup |
| 77 |
* |
| 78 |
* @param $order_id |
| 79 |
*/ |
| 80 |
function woo_ml_checkout_maybe_prepare_signup($order_id) |
| 81 |
{ |
| 82 |
|
| 83 |
if (isset($_POST['woo_ml_subscribe']) && '1' == $_POST['woo_ml_subscribe']) { |
| 84 |
OrderProcess::getInstance()->setOrderCustomerSubscribe($order_id); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
add_action('woocommerce_checkout_update_order_meta', 'woo_ml_checkout_maybe_prepare_signup'); |
| 89 |
|
| 90 |
/** |
| 91 |
* Process checkout completed |
| 92 |
* |
| 93 |
* @param $order_id |
| 94 |
*/ |
| 95 |
function woo_ml_process_checkout_completed($order_id) |
| 96 |
{ |
| 97 |
|
| 98 |
if ( ! get_option('ml_account_authenticated')) { |
| 99 |
|
| 100 |
return false; |
| 101 |
} |
| 102 |
|
| 103 |
OrderProcess::getInstance()->processOrderSubscription($order_id); |
| 104 |
} |
| 105 |
|
| 106 |
add_action('woocommerce_checkout_order_processed', 'woo_ml_process_checkout_completed'); |
| 107 |
|
| 108 |
/** |
| 109 |
* Process order completed (and finally paid) |
| 110 |
* |
| 111 |
* @param $order_id |
| 112 |
*/ |
| 113 |
function woo_ml_process_order_completed($order_id) |
| 114 |
{ |
| 115 |
|
| 116 |
if ( ! woo_ml_integration_setup_completed()) { |
| 117 |
woo_ml_setup_integration(); |
| 118 |
} |
| 119 |
|
| 120 |
if ( ! woo_ml_old_integration()) { |
| 121 |
OrderProcess::getInstance()->sendCompletedOrder($order_id); |
| 122 |
} |
| 123 |
|
| 124 |
OrderProcess::getInstance()->processOrderTracking($order_id); |
| 125 |
} |
| 126 |
|
| 127 |
add_action('woocommerce_order_status_completed', 'woo_ml_process_order_completed'); |
| 128 |
|
| 129 |
function woo_ml_proceed_to_checkout() |
| 130 |
{ |
| 131 |
|
| 132 |
if ( ! get_option('ml_account_authenticated')) { |
| 133 |
|
| 134 |
return false; |
| 135 |
} |
| 136 |
|
| 137 |
if ( ! woo_ml_old_integration()) { |
| 138 |
CartProcess::getInstance()->sendCart(); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
add_action('woocommerce_add_to_cart', 'woo_ml_proceed_to_checkout'); |
| 143 |
add_action('woocommerce_cart_item_removed', 'woo_ml_proceed_to_checkout'); |
| 144 |
|
| 145 |
function woo_ml_cart_action_updated($cart_updated) |
| 146 |
{ |
| 147 |
|
| 148 |
woo_ml_proceed_to_checkout(); |
| 149 |
|
| 150 |
return $cart_updated; |
| 151 |
} |
| 152 |
|
| 153 |
add_filter('woocommerce_update_cart_action_cart_updated', 'woo_ml_cart_action_updated'); |
| 154 |
|
| 155 |
function woo_ml_order_status_change($order_id) |
| 156 |
{ |
| 157 |
if ( ! woo_ml_old_integration()) { |
| 158 |
OrderProcess::getInstance()->paymentStatusProcessing($order_id); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
add_action('woocommerce_order_status_changed', 'woo_ml_order_status_change'); |
| 163 |
|
| 164 |
|
| 165 |
function woo_ml_enqueue_styles() |
| 166 |
{ |
| 167 |
|
| 168 |
if ( ! get_option('ml_account_authenticated')) { |
| 169 |
|
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
wp_enqueue_style('related-styles', plugins_url('/../public/css/style.css', __FILE__)); |
| 174 |
} |
| 175 |
|
| 176 |
add_action('wp_enqueue_scripts', 'woo_ml_enqueue_styles'); |
| 177 |
|
| 178 |
/** |
| 179 |
* WooCommerce product update |
| 180 |
* |
| 181 |
* @param $product_id |
| 182 |
*/ |
| 183 |
function woo_ml_product_update($product_id) |
| 184 |
{ |
| 185 |
|
| 186 |
ProductProcess::getInstance()->wpSyncProduct($product_id); |
| 187 |
} |
| 188 |
|
| 189 |
add_action('woocommerce_update_product', 'woo_ml_product_update', 10, 1); |
| 190 |
|
| 191 |
/** |
| 192 |
* WooCommerce order update |
| 193 |
* |
| 194 |
* @param $order_id |
| 195 |
*/ |
| 196 |
function woo_ml_order_update($order_id, $order) |
| 197 |
{ |
| 198 |
|
| 199 |
OrderSyncProcess::getInstance()->syncOrder($order_id, $order); |
| 200 |
} |
| 201 |
|
| 202 |
add_action('woocommerce_process_shop_order_meta', 'woo_ml_order_update', 10, 2); |
| 203 |
add_action('woocommerce_saved_order_items', 'woo_ml_order_update', 10, 2); |
| 204 |
|
| 205 |
/** |
| 206 |
* WooCommerce bulk order update |
| 207 |
* |
| 208 |
* @param $redirect_to |
| 209 |
* @param $action |
| 210 |
* @param $order_ids |
| 211 |
*/ |
| 212 |
function woo_ml_bulk_order_update($redirect_to, $action, $order_ids) |
| 213 |
{ |
| 214 |
|
| 215 |
OrderSyncProcess::getInstance()->syncBulkOrder($action, $order_ids); |
| 216 |
|
| 217 |
return $redirect_to; |
| 218 |
} |
| 219 |
|
| 220 |
add_filter('handle_bulk_actions-edit-shop_order', 'woo_ml_bulk_order_update', 10, 3); |
| 221 |
|
| 222 |
/** |
| 223 |
* WP Post delete |
| 224 |
* |
| 225 |
* @param $post_id |
| 226 |
*/ |
| 227 |
function woo_ml_post_delete($post_id) |
| 228 |
{ |
| 229 |
|
| 230 |
SynchronizeSettings::getInstance()->syncPostDelete($post_id); |
| 231 |
} |
| 232 |
|
| 233 |
add_action('delete_post', 'woo_ml_post_delete', 10); |
| 234 |
|
| 235 |
function woo_ml_customer_update($customer_id, $customer_data = '') |
| 236 |
{ |
| 237 |
|
| 238 |
mailerlite_wp_sync_ecommerce_customer($customer_id); |
| 239 |
} |
| 240 |
|
| 241 |
add_action('woocommerce_update_customer', 'woo_ml_customer_update', 99, 2); |
| 242 |
add_action('user_register', 'woo_ml_customer_update', 10, 1); |
| 243 |
add_action('profile_update', 'woo_ml_customer_update', 10, 2); |
| 244 |
|
| 245 |
/** |
| 246 |
* WP Product Category create/update |
| 247 |
* |
| 248 |
* @param $category_id |
| 249 |
*/ |
| 250 |
function woo_ml_product_category_sync($category_id) |
| 251 |
{ |
| 252 |
|
| 253 |
ProductProcess::getInstance()->syncProductCategory($category_id); |
| 254 |
} |
| 255 |
|
| 256 |
add_action('created_product_cat', 'woo_ml_product_category_sync', 10, 2); |
| 257 |
add_action('edited_product_cat', 'woo_ml_product_category_sync', 10, 2); |
| 258 |
|
| 259 |
function woo_ml_product_category_delete($term, $tt_id, $deleted_term, $object_ids) |
| 260 |
{ |
| 261 |
|
| 262 |
// Avoid auto save from calling API |
| 263 |
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
ProductProcess::getInstance()->deleteProductCategory($term); |
| 268 |
} |
| 269 |
|
| 270 |
add_action('delete_product_cat', 'woo_ml_product_category_delete', 10, 4); |