| 1 |
<?php |
| 2 |
|
| 3 |
class WooMailerLiteAdmin |
| 4 |
{ |
| 5 |
public static $instance; |
| 6 |
|
| 7 |
public static function instance() |
| 8 |
{ |
| 9 |
if (!empty(static::$instance)) { |
| 10 |
return static::$instance; |
| 11 |
} |
| 12 |
static::$instance = new self(); |
| 13 |
return static::$instance; |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
public function addPluginAdminMenu() |
| 18 |
{ |
| 19 |
add_submenu_page('woocommerce', 'MailerLite', 'MailerLite', 'manage_options', 'mailerlite', |
| 20 |
[$this, 'wooMailerLiteSettingsPageCallback']); |
| 21 |
} |
| 22 |
|
| 23 |
public function enqueueScripts($hook) |
| 24 |
{ |
| 25 |
if ($hook === 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] === 'product') { |
| 26 |
wp_enqueue_script('woo-mailerlite-quick-edit', plugin_dir_url(__FILE__) . '../admin/assets/js/ml-quick-edit.js', ['jquery', 'inline-edit-post'], null, true); |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
if ($hook !== 'woocommerce_page_mailerlite') { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
wp_dequeue_script('select2'); |
| 35 |
wp_deregister_script('select2'); |
| 36 |
|
| 37 |
wp_enqueue_script('woo-mailerlite-vue-cdn', 'https://cdn.jsdelivr.net/npm/vue@3.5.13/dist/vue.global.prod.js', [], null, true); |
| 38 |
wp_localize_script('woo-mailerlite-admin', 'woo_mailerlite_admin_data', array( |
| 39 |
'ajax_url' => admin_url('admin-ajax.php'), |
| 40 |
'language' => get_locale() |
| 41 |
)); |
| 42 |
|
| 43 |
wp_enqueue_script('woo-mailerlite-select2', plugin_dir_url(__FILE__) . 'assets/js/lib/select2.min.js', ['jquery'], WOO_MAILERLITE_VERSION, true); |
| 44 |
wp_enqueue_script('woo-mailerlite-admin', plugin_dir_url(__FILE__) . '../admin/assets/js/ml-app.js', ['jquery', 'woo-mailerlite-vue-cdn', 'woo-mailerlite-select2'], null, true); |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
public function enqueueStyles($hook) { |
| 49 |
if ($hook !== 'woocommerce_page_mailerlite') { |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
wp_dequeue_style('select2'); |
| 54 |
wp_deregister_style('select2'); |
| 55 |
|
| 56 |
wp_enqueue_style('woo-mailerlite-admin-css', plugin_dir_url( __FILE__ ) . '../admin/assets/css/admin.css', false, WOO_MAILERLITE_VERSION); |
| 57 |
wp_enqueue_style('woo-mailerlite-select2-css', plugin_dir_url( __FILE__ ) . 'assets/css/lib/select2.min.css', false, WOO_MAILERLITE_VERSION); |
| 58 |
wp_enqueue_style('woo-mailerlite-select2-theme', plugin_dir_url( __FILE__ ) . '../public/css/mailerlite-select2.css', ['woo-mailerlite-select2-css'], WOO_MAILERLITE_VERSION); |
| 59 |
} |
| 60 |
|
| 61 |
public function removeConflictingSelect2($hook) |
| 62 |
{ |
| 63 |
if ($hook !== 'woocommerce_page_mailerlite') { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
global $wp_scripts, $wp_styles; |
| 68 |
|
| 69 |
$scriptPatterns = ['select2', 'selectWoo', 'wc-enhanced-select']; |
| 70 |
|
| 71 |
foreach ($wp_scripts->registered as $handle => $script) { |
| 72 |
if ($handle === 'woo-mailerlite-select2') { |
| 73 |
continue; |
| 74 |
} |
| 75 |
foreach ($scriptPatterns as $pattern) { |
| 76 |
if (strpos($script->src, $pattern) !== false) { |
| 77 |
wp_dequeue_script($handle); |
| 78 |
wp_deregister_script($handle); |
| 79 |
break; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$stylePatterns = ['select2', 'selectWoo']; |
| 85 |
|
| 86 |
foreach ($wp_styles->registered as $handle => $style) { |
| 87 |
if ($handle === 'woo-mailerlite-select2-css' || $handle === 'woo-mailerlite-select2-theme') { |
| 88 |
continue; |
| 89 |
} |
| 90 |
foreach ($stylePatterns as $pattern) { |
| 91 |
if (strpos($style->src, $pattern) !== false) { |
| 92 |
wp_dequeue_style($handle); |
| 93 |
wp_deregister_style($handle); |
| 94 |
break; |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
public function wooMailerLiteSettingsPageCallback() |
| 101 |
{ |
| 102 |
$this->migrateProductVariantsOrIgnore(); |
| 103 |
$falseApi = false; |
| 104 |
// if (!WooMailerLiteCache::get('valid_api')) { |
| 105 |
// $response = WooMailerLiteApi::client()->ping(); |
| 106 |
// if ($response->status === 401) { |
| 107 |
// $falseApi = true; |
| 108 |
//// WooMailerLiteOptions::deleteAll(); |
| 109 |
// } else { |
| 110 |
// WooMailerLiteCache::set('valid_api', true, 86400); |
| 111 |
// } |
| 112 |
// } |
| 113 |
$untrackedResources = WooMailerLiteProduct::getUntrackedProductsCount() + WooMailerLiteCategory::getUntrackedCategoriesCount() + WooMailerLiteCustomer::getUntrackedCustomersCount(); |
| 114 |
|
| 115 |
wp_localize_script('woo-mailerlite-vue-cdn', 'woo_mailerlite_admin_data', [ |
| 116 |
'ajaxUrl' => admin_url('admin-ajax.php'), |
| 117 |
'productsUrl' => esc_url(admin_url('edit.php?post_type=product')), |
| 118 |
'currentStep' => WooMailerLiteOptions::get('wizardStep', 0), |
| 119 |
'account' => [ |
| 120 |
'isConnected' => false, |
| 121 |
'accountName' => WooMailerLiteOptions::get('accountName', ''), |
| 122 |
'platform' => WooMailerLiteApi::getApiType() ?? 1, |
| 123 |
], |
| 124 |
'selectedGroup' => WooMailerLiteOptions::get('group', []), |
| 125 |
'woo_mailerlite_admin_nonce' => wp_create_nonce( 'woo_mailerlite_admin' ), |
| 126 |
'ignoredProducts' => (array)WooMailerLiteOptions::get('ignored_products', []), |
| 127 |
'sync' => [ |
| 128 |
'lastCustomerSync' => 0, |
| 129 |
'newMLSync' => true, |
| 130 |
'syncInProgress' => WooMailerLiteCache::get('manual_sync', false), |
| 131 |
'totalUntrackedResources' => $untrackedResources, |
| 132 |
'totalTrackedResources' => 0 |
| 133 |
], |
| 134 |
'settings' => WooMailerLiteOptions::get('settings', []), |
| 135 |
'syncFields' => WooMailerLiteOptions::get('syncFields'), |
| 136 |
// Need better solution for async sync, adding true for now and force sync (works) |
| 137 |
'asyncSync' => WooMailerLiteCache::get('scheduled_jobs') && $untrackedResources, |
| 138 |
'falseApi' => $falseApi, |
| 139 |
'debugMode' => WooMailerLiteOptions::get('debugMode') |
| 140 |
]); |
| 141 |
|
| 142 |
require_once __DIR__ . '/../views/mailerlite-app.php'; |
| 143 |
} |
| 144 |
|
| 145 |
private function migrateProductVariantsOrIgnore() |
| 146 |
{ |
| 147 |
if (WooMailerLiteOptions::get('productVariantsMigrated', false)) { |
| 148 |
return; |
| 149 |
} |
| 150 |
|
| 151 |
if (!WooMailerLiteOptions::get('apiKey') || !WooMailerLiteOptions::get('shopId')) { |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
WooMailerLiteOptions::update('productVariantsMigrated', true); |
| 156 |
WooMailerLiteProductVariantsMigrationJob::dispatch(); |
| 157 |
|
| 158 |
WooMailerLiteLog()->info('variants:migration:started'); |
| 159 |
} |
| 160 |
|
| 161 |
public function addModuleTypeScript($tag, $handle, $src) |
| 162 |
{ |
| 163 |
if ('woo-mailerlite-admin' === $handle) { |
| 164 |
// modify the <script> tag to include type="module" |
| 165 |
$tag = '<script type="module" src="' . esc_url($src) . '"></script>'; |
| 166 |
} |
| 167 |
|
| 168 |
return $tag; |
| 169 |
} |
| 170 |
|
| 171 |
public function ignoreProductBlock() |
| 172 |
{ |
| 173 |
echo '<div class="inline-edit-group">'; |
| 174 |
woocommerce_wp_checkbox(array( |
| 175 |
'id' => 'ml_ignore_product', |
| 176 |
'label' => __('MailerLite e-commerce automations', 'woo-mailerlite'), |
| 177 |
'description' => __('Ignore product', 'woo-mailerlite'), |
| 178 |
'value' => '', |
| 179 |
'desc_tip' => false |
| 180 |
)); |
| 181 |
echo '</div>'; |
| 182 |
} |
| 183 |
|
| 184 |
public function populateIgnoreProductBlock($column, $post_id) |
| 185 |
{ |
| 186 |
|
| 187 |
switch ($column) { |
| 188 |
case 'name' : |
| 189 |
$ignoredProducts = WooMailerLiteOptions::get('ignored_products', []); |
| 190 |
?> |
| 191 |
<div class="hidden ml_ignore_product_inline" id="ml_ignore_product_inline_<?=intval($post_id) ?>"> |
| 192 |
<div class="_ml_ignore_product"><?php echo array_key_exists($post_id, $ignoredProducts) ? 'yes' : 'no' ?></div> |
| 193 |
</div> |
| 194 |
<?php |
| 195 |
|
| 196 |
break; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
public function abss() |
| 201 |
{ |
| 202 |
global $post; |
| 203 |
?> |
| 204 |
<div id="woo-ml-product-data" class="panel woocommerce_options_panel"> |
| 205 |
<?php |
| 206 |
|
| 207 |
$ignoredProducts = WooMailerLiteOptions::get('ignored_products', []); |
| 208 |
|
| 209 |
woocommerce_wp_checkbox(array( |
| 210 |
'id' => 'ml_ignore_product', |
| 211 |
'label' => __('Ignore Product', 'woo-mailerlite'), |
| 212 |
'description' => __('Select if you do not wish to trigger any e-commerce automations for this product', |
| 213 |
'woo-mailerlite'), |
| 214 |
'value' => array_key_exists($post->ID, $ignoredProducts) ? 'yes' : 'no', |
| 215 |
'desc_tip' => true, |
| 216 |
)); |
| 217 |
?> |
| 218 |
</div> |
| 219 |
<?php |
| 220 |
} |
| 221 |
|
| 222 |
public function woo_ml_product_data_tab($product_data_tabs) |
| 223 |
{ |
| 224 |
$product_data_tabs['woo-ml'] = array( |
| 225 |
'label' => __('MailerLite', 'woo-mailerlite'), |
| 226 |
'target' => 'woo-ml-product-data', |
| 227 |
'class' => 'woo_mailerlite_product_tab', |
| 228 |
'priority' => 99 |
| 229 |
); |
| 230 |
|
| 231 |
return $product_data_tabs; |
| 232 |
} |
| 233 |
|
| 234 |
public function handleCustomProductQuery( $query, $query_vars ) { |
| 235 |
|
| 236 |
if ( isset( $query_vars['metaQuery'] ) ) { |
| 237 |
$query['meta_query'][] = $query_vars['metaQuery']; |
| 238 |
} |
| 239 |
|
| 240 |
return $query; |
| 241 |
} |
| 242 |
|
| 243 |
public function addSettingsOptionInPluginList($links) |
| 244 |
{ |
| 245 |
$settings_url = admin_url('admin.php?page=mailerlite'); |
| 246 |
$settings_link = '<a href="' . esc_url($settings_url) . '">Settings</a>'; |
| 247 |
array_unshift($links, $settings_link); |
| 248 |
return $links; |
| 249 |
} |
| 250 |
} |
| 251 |
|