prefix . 'wc_orders_meta' : $wpdb->postmeta; $vals = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT meta_value FROM {$table} WHERE meta_key = %s AND meta_value <> ''", '_aieo_pickup_location')); $names = array(); foreach ((array) $vals as $v) { $v = (string) $v; if ($v !== '') { $names[$v] = true; } } foreach (self::blocks_locations() as $bl) { if (!empty($bl['name'])) { $names[(string) $bl['name']] = true; } } return array_keys($names); } public static function get_settings() { $stored = get_option(self::OPTION_KEY, array()); if (!is_array($stored)) { $stored = array(); } $merged = array_merge(array( 'mode' => 'legacy', 'title' => '', 'blocked_countries' => array(), ), $stored); if (did_action('init') && '' === trim((string) ($merged['title'] ?? ''))) { $merged['title'] = self::i18n_title(); } return $merged; } private static function i18n_title() { return __('Store pickup', 'ai-eshop-optimizer'); } public static function mode() { $s = self::get_settings(); return ($s['mode'] === 'blocks') ? 'blocks' : 'legacy'; } public static function blocked_countries() { $s = self::get_settings(); $bc = (isset($s['blocked_countries']) && is_array($s['blocked_countries'])) ? $s['blocked_countries'] : array(); return array_values(array_unique(array_map('strtoupper', array_map('strval', $bc)))); } public static function shipping_countries() { if (!function_exists('WC') || !WC()->countries) { return array(); } $c = WC()->countries->get_shipping_countries(); return is_array($c) ? $c : array(); } public static function legacy_pickup_names() { $names = array(); if (!class_exists('WC_Shipping_Zones')) { return $names; } $zones = WC_Shipping_Zones::get_zones(); $rest = WC_Shipping_Zones::get_zone(0); if ($rest) { $zones[] = array('shipping_methods' => $rest->get_shipping_methods(false)); } foreach ($zones as $z) { foreach ((isset($z['shipping_methods']) ? $z['shipping_methods'] : array()) as $m) { if (!is_object($m) || $m->id !== 'local_pickup') { continue; } if (method_exists($m, 'is_enabled') && !$m->is_enabled()) { continue; } $title = method_exists($m, 'get_title') ? trim((string) $m->get_title()) : ''; if ($title !== '' && !in_array($title, $names, true)) { $names[] = $title; } } } return $names; } public static function legacy_pickup_instances() { $out = array(); if (!class_exists('WC_Shipping_Zones')) { return $out; } $zones = WC_Shipping_Zones::get_zones(); $rest = WC_Shipping_Zones::get_zone(0); if ($rest) { $zones[] = array('zone_name' => __('Rest of world', 'ai-eshop-optimizer'), 'shipping_methods' => $rest->get_shipping_methods(false)); } foreach ($zones as $z) { $zname = isset($z['zone_name']) ? (string) $z['zone_name'] : ''; foreach ((isset($z['shipping_methods']) ? $z['shipping_methods'] : array()) as $m) { if (!is_object($m) || $m->id !== 'local_pickup') { continue; } $iid = method_exists($m, 'get_instance_id') ? (int) $m->get_instance_id() : 0; if ($iid <= 0) { continue; } $title = method_exists($m, 'get_title') ? (string) $m->get_title() : ''; $out[] = array( 'rate_id' => 'local_pickup:' . $iid, 'title' => $title, 'label' => trim(($zname !== '' ? $zname . ' — ' : '') . $title . ' (#' . $iid . ')'), ); } } return $out; } public static function blocks_locations() { $raw = get_option('pickup_location_pickup_locations', array()); if (!is_array($raw)) { return array(); } $out = array(); foreach ($raw as $loc) { if (!is_array($loc)) { continue; } $addr = isset($loc['address']) && is_array($loc['address']) ? $loc['address'] : array(); $out[] = array( 'name' => isset($loc['name']) ? (string) $loc['name'] : '', 'enabled' => !empty($loc['enabled']), 'details' => isset($loc['details']) ? (string) $loc['details'] : '', 'review_slug' => isset($loc['review_slug']) ? (string) $loc['review_slug'] : '', 'address' => array( 'address_1' => isset($addr['address_1']) ? (string) $addr['address_1'] : '', 'city' => isset($addr['city']) ? (string) $addr['city'] : '', 'state' => isset($addr['state']) ? (string) $addr['state'] : '', 'postcode' => isset($addr['postcode']) ? (string) $addr['postcode'] : '', 'country' => (isset($addr['country']) && $addr['country'] !== '') ? (string) $addr['country'] : 'GR', ), ); } return $out; } private static function blocks_enabled() { if (class_exists('Automattic\\WooCommerce\\StoreApi\\Utilities\\LocalPickupUtils')) { return \Automattic\WooCommerce\StoreApi\Utilities\LocalPickupUtils::is_local_pickup_enabled(); } $o = get_option('woocommerce_pickup_location_settings', array()); return is_array($o) && isset($o['enabled']) && $o['enabled'] === 'yes'; } private static function load_google_reviews() { foreach (array('class-aieo-google-reviews-locations.php', 'class-aieo-google-reviews-helpers.php') as $f) { $p = (defined('AIEO_DIR') ? AIEO_DIR : '') . 'includes/dmm/google-reviews/' . $f; if ($p !== '' && file_exists($p)) { require_once $p; } } } public static function review_locations() { self::load_google_reviews(); $out = array(); if (class_exists('AIEO_Google_Reviews_Locations')) { foreach (AIEO_Google_Reviews_Locations::list_all(true) as $r) { if (!is_array($r) || empty($r['slug'])) { continue; } $out[] = array('slug' => (string) $r['slug'], 'label' => (string) (isset($r['label']) && $r['label'] !== '' ? $r['label'] : $r['slug'])); } } return $out; } public function enqueue_admin_assets() { $css = 'assets/css/aieo-lp-admin.css'; wp_enqueue_style('aieo-lp-admin', AIEO_URI . $css, array(), file_exists(AIEO_DIR . $css) ? (string) filemtime(AIEO_DIR . $css) : '1'); } public function render_subtab() { $s = self::get_settings(); $mode = self::mode(); $locations = self::blocks_locations(); $legacy = self::legacy_pickup_names(); $bp_enabled = self::blocks_enabled(); $review_locs = self::review_locations(); $nonce = wp_create_nonce(AIEO_DMM::NONCE_ACTION); $ajax = admin_url('admin-ajax.php'); $boot = wp_json_encode(array('mode' => $mode, 'title' => (string) $s['title'], 'locations' => $locations, 'reviewLocations' => $review_locs)); if (!is_string($boot)) { $boot = '{"mode":"legacy","title":"","locations":[],"reviewLocations":[]}'; } ?> $lp_name, 'enabled' => !empty($lp_l['enabled']), 'details' => (string) $lp_l['details'], 'review_slug' => (string) $lp_l['review_slug'], 'address' => array( 'address_1' => (string) $lp_a['address_1'], 'city' => (string) $lp_a['city'], 'state' => (string) $lp_a['state'], 'postcode' => (string) $lp_a['postcode'], 'country' => '' !== trim((string) $lp_a['country']) ? trim((string) $lp_a['country']) : 'GR', ), ); } $arch_form = 'aieo-card-local-pickup-pickup-architecture-form'; $locs_form = 'aieo-card-local-pickup-pickup-locations-blocks-form'; $cc_form = 'aieo-card-local-pickup-restrict-pickup-by-country-form'; rooster_admin_band_open(array('cols' => 2)); list($open, $close) = AIEO_DMM_UI::card(array( 'anchor' => 'local-pickup-pickup-architecture', 'icon' => 'workflow', 'title' => __('Pickup architecture', 'ai-eshop-optimizer'), 'description' => __('Choose how store pickup is offered at checkout. Legacy uses core WooCommerce per-zone local_pickup (good for classic checkout). Blocks uses WooCommerce\'s modern Local Pickup with stores defined once, globally — recommended for block checkouts (roosterx) because it has no shipping-zone instance-mismatch bug.', 'ai-eshop-optimizer'), 'col' => 1, 'footer' => $has_states ? \Rooster\Admin\States::save_button($arch_form) : '', )); echo $open; ob_start(); echo AIEO_Kit::row( __('Mode', 'ai-eshop-optimizer'), AIEO_Kit::checks( 'mode', array( 'legacy' => __('Legacy', 'ai-eshop-optimizer'), 'blocks' => __('Blocks (recommended for block checkout)', 'ai-eshop-optimizer'), ), array($mode), array( 'type' => 'radio', 'descriptions' => array( 'legacy' => __('core per-zone local_pickup (classic + block, with the zone-flip safety patch). No change to today\'s setup.', 'ai-eshop-optimizer'), 'blocks' => __('block checkout uses Blocks Local Pickup (stores below); classic checkout keeps legacy. Bug-free, zone-independent.', 'ai-eshop-optimizer'), ), ) ), '', array('id' => 'aieo-lp-mode', 'group' => true) ); echo AIEO_Kit::row(__('Pickup label', 'ai-eshop-optimizer'), AIEO_Kit::input('title', (string) $s['title'], array('id' => 'aieo-lp-title', 'medium' => true)), __('Shown as the "Pickup" tab label in block checkout.', 'ai-eshop-optimizer'), array('id' => 'aieo-lp-title')); ?> 'local-pickup-status', 'icon' => 'activity', 'title' => __('Status', 'ai-eshop-optimizer'), 'description' => __('Which pickup setup checkout uses today: the legacy pickup stores found in your zones, and whether Blocks Local Pickup is on.', 'ai-eshop-optimizer'), 'col' => 2, )); echo $open; ?>

__('Status', 'ai-eshop-optimizer'))); ?> 'local-pickup-pickup-locations-blocks', 'icon' => 'building-2', 'title' => __('Pickup locations (Blocks)', 'ai-eshop-optimizer'), 'description' => __('Stores customers can collect from when they choose Pickup in block checkout. Used by Blocks mode.', 'ai-eshop-optimizer'), 'meta' => rooster_admin_badge(_x('Off', 'a card whose settings are not in use', 'ai-eshop-optimizer'), 'mono', array('class' => 'aieo-lp-off-badge')), 'footer' => $has_states ? \Rooster\Admin\States::save_button($locs_form) : '', )); echo $open; ob_start(); ?>

'aieo-lp-import-status')); ?>

locations_grid($locations, $review_locs); ?> 'local-pickup-restrict-pickup-by-country', 'icon' => 'globe', 'title' => __('Restrict pickup by country', 'ai-eshop-optimizer'), 'description' => __('Hide store pickup (both Blocks and legacy) when the customer\'s shipping country is ticked below. Use it to offer pickup for Greece only — e.g. tick Cyprus so Cypriot orders never see a pickup option. Applies to block AND classic checkout.', 'ai-eshop-optimizer'), 'footer' => ($has_states && !empty($ship_cc)) ? \Rooster\Admin\States::save_button($cc_form) : '', )); echo $open; ob_start(); ?> __('No shipping countries are configured in WooCommerce → Settings → Shipping yet.', 'ai-eshop-optimizer'))); ?>
$name) : ?>
render_legacy_migration(); $this->render_rename(); AIEO_Table::engine(); ?> '— —'); foreach ($review_locs as $r) { $reviews[(string) $r['slug']] = (string) $r['label']; } $columns = array( array('key' => 'enabled', 'label' => __('Enabled', 'ai-eshop-optimizer'), 'type' => 'html', 'width' => 'min', 'align' => 'center', 'always' => true), array('key' => 'name', 'label' => __('Name', 'ai-eshop-optimizer'), 'type' => 'html', 'always' => true), array('key' => 'address_1', 'label' => __('Address', 'ai-eshop-optimizer'), 'type' => 'html'), array('key' => 'city', 'label' => __('City', 'ai-eshop-optimizer'), 'type' => 'html', 'width' => 'sm'), array('key' => 'postcode', 'label' => __('Postcode', 'ai-eshop-optimizer'), 'type' => 'html', 'width' => 'xs'), array('key' => 'country', 'label' => __('Country', 'ai-eshop-optimizer'), 'type' => 'html', 'width' => 'xs'), array('key' => 'details', 'label' => __('Details', 'ai-eshop-optimizer'), 'type' => 'html'), array('key' => 'review', 'label' => __('Google location', 'ai-eshop-optimizer'), 'type' => 'html', 'width' => 'md'), array('key' => 'remove', 'label' => __('Actions', 'ai-eshop-optimizer'), 'type' => 'actions'), ); $row = static function ($id, array $loc) use ($reviews) { $a = isset($loc['address']) && is_array($loc['address']) ? $loc['address'] : array(); $field = static function ($class, $value, $label) { return array('html' => \Rooster\Admin\Ui::input('', (string) $value, array('class' => $class, 'aria-label' => $label))); }; $slug = (string) ($loc['review_slug'] ?? ''); return array( '_id' => (string) $id, '_name' => (string) ($loc['name'] ?? ''), 'enabled' => array('html' => \Rooster\Admin\Ui::toggle('', false !== ($loc['enabled'] ?? true), array('bare' => true, 'hidden' => false, 'label' => __('Enabled', 'ai-eshop-optimizer'), 'attrs' => array('data-lp' => 'enabled')))), 'name' => $field('lp-name', $loc['name'] ?? '', __('Name', 'ai-eshop-optimizer')), 'address_1' => $field('lp-address_1', $a['address_1'] ?? '', __('Address', 'ai-eshop-optimizer')), 'city' => $field('lp-city', $a['city'] ?? '', __('City', 'ai-eshop-optimizer')), 'postcode' => $field('lp-postcode', $a['postcode'] ?? '', __('Postcode', 'ai-eshop-optimizer')), 'country' => $field('lp-country', '' !== (string) ($a['country'] ?? '') ? $a['country'] : 'GR', __('Country', 'ai-eshop-optimizer')), 'details' => array('html' => \Rooster\Admin\Ui::input('', (string) ($loc['details'] ?? ''), array('class' => 'lp-details', 'aria-label' => __('Details', 'ai-eshop-optimizer'))) . ''), 'review' => array('html' => \Rooster\Admin\Ui::select('', $reviews, isset($reviews[$slug]) ? $slug : '', array('aria-label' => __('Google location', 'ai-eshop-optimizer'), 'attrs' => array('data-lp' => 'review')))), 'remove' => array( array( 'label' => __('Remove', 'ai-eshop-optimizer'), 'destructive' => true, 'attrs' => array('data-lp-remove' => ''), ), ), ); }; $rows = array(); foreach (array_values($locations) as $i => $loc) { $rows[] = $row($i + 1, (array) $loc); } if (!$rows) { $rows[] = $row(1, array('address' => array('country' => 'GR'))); } $html = AIEO_Table::local('aieo-lp-table', $columns, $rows, array( 'local' => array( 'form' => true, 'none' => esc_html__('No pickup locations yet. Use “+ Add location” to create one.', 'ai-eshop-optimizer'), ), 'size' => 0, 'caption' => __('Pickup locations', 'ai-eshop-optimizer'), 'chooser' => true, )); return $html . ''; } private function legacy_map_grid(array $instances, array $new_names, array $map) { if (!class_exists('\Rooster\Admin\Ui')) { return ''; } $choices = array('' => '— —'); foreach ($new_names as $nn) { $choices[(string) $nn] = (string) $nn; } $rows = array(); foreach ($instances as $inst) { $rate = (string) $inst['rate_id']; $sel = isset($map[$rate]) ? (string) $map[$rate] : ''; $rows[] = array( '_id' => $rate, 'legacy' => (string) $inst['label'], 'new' => array('html' => \Rooster\Admin\Ui::select('', $choices, isset($choices[$sel]) ? $sel : '', array('aria-label' => sprintf( __('New pickup location for %s', 'ai-eshop-optimizer'), (string) $inst['label']), 'attrs' => array('data-rateid' => $rate)))), '_s' => array( 'legacy' => (string) $inst['label'], 'new' => $sel, ), ); } return AIEO_Table::local('aieo-lpm-table', array( array('key' => 'legacy', 'label' => __('Legacy pickup (zone — store)', 'ai-eshop-optimizer'), 'sort_type' => 'text'), array('key' => 'new', 'label' => __('New pickup location', 'ai-eshop-optimizer'), 'type' => 'html', 'width' => 'lg', 'sort_type' => 'text'), ), $rows, array( 'prefix' => 'lpm_', 'local' => array('form' => true), 'size' => 0, 'caption' => __('Legacy methods → pickup locations', 'ai-eshop-optimizer'), )); } private function render_legacy_migration() { $instances = self::legacy_pickup_instances(); if (empty($instances)) { return; } $new_names = array(); foreach (self::blocks_locations() as $bl) { if (!empty($bl['name'])) { $new_names[] = (string) $bl['name']; } } $map = get_option('aieo_pickup_legacy_map', array()); if (!is_array($map)) { $map = array(); } $nonce = wp_create_nonce(AIEO_DMM::NONCE_ACTION); $ajax = admin_url('admin-ajax.php'); ?> 'local-pickup-migrate-legacy-pickup-orders-new-locations', 'icon' => 'import', 'title' => __('Legacy methods → pickup locations', 'ai-eshop-optimizer'), 'description' => __('Map each legacy per-zone pickup method to a new pickup location. Old orders placed with the legacy pickup then resolve to the new location (for the Google Maps link in emails / order pages). Legacy stores were duplicated across two zones, so expect two rows per store — point both at the same new location.', 'ai-eshop-optimizer'), )); echo $open; ?> legacy_map_grid($instances, $new_names, $map); ?>

'aieo-lpm-status')); ?>

'aieo-lpm-apply-status')); ?>

$name) { $rid = sanitize_text_field((string) $rid); $name = sanitize_text_field((string) $name); if ($rid === '' || $name === '') { continue; } if (!preg_match('/^local_pickup:\d+$/', $rid)) { continue; } if (!in_array($name, $valid_names, true)) { continue; } $map[$rid] = $name; if (!empty($titles[$rid])) { $map[$titles[$rid]] = $name; } } } } update_option('aieo_pickup_legacy_map', $map, false); wp_send_json_success(array('message' => __('Migration map saved.', 'ai-eshop-optimizer'), 'count' => count($map))); } public function ajax_apply_orders() { AIEO_DMM::verify_ajax_request(); if (!class_exists('AIEO_Runtime_Pickup_Maps')) { wp_send_json_error(array('message' => __('Pickup runtime not available.', 'ai-eshop-optimizer'))); } if (function_exists('set_time_limit')) { @set_time_limit(0); } global $wpdb; $batch = 100; $offset = isset($_POST['offset']) ? max(0, (int) $_POST['offset']) : 0; $oi = $wpdb->prefix . 'woocommerce_order_items'; $oim = $wpdb->prefix . 'woocommerce_order_itemmeta'; $ids = $wpdb->get_col($wpdb->prepare( "SELECT DISTINCT oi.order_id FROM {$oi} oi INNER JOIN {$oim} oim ON oim.order_item_id = oi.order_item_id WHERE oi.order_item_type = 'shipping' AND oim.meta_key = 'method_id' AND oim.meta_value = 'local_pickup' ORDER BY oi.order_id DESC LIMIT %d OFFSET %d", $batch, $offset )); $processed = is_array($ids) ? count($ids) : 0; $updated = 0; foreach ((array) $ids as $oid) { $order = wc_get_order((int) $oid); if ($order && AIEO_Runtime_Pickup_Maps::stamp_order_pickup($order, true)) { $updated++; } } wp_send_json_success(array( 'processed' => $processed, 'updated' => $updated, 'next' => $offset + $processed, 'done' => ($processed < $batch), )); } private function render_rename() { $current = array(); foreach (self::blocks_locations() as $bl) { if (!empty($bl['name'])) { $current[] = (string) $bl['name']; } } $known = self::known_pickup_names(); if (empty($known) && empty($current)) { return; } $nonce = wp_create_nonce(AIEO_DMM::NONCE_ACTION); $ajax = admin_url('admin-ajax.php'); ?> 'local-pickup-rename-merge-a-pickup-name', 'icon' => 'pencil-line', 'title' => __('Rename / merge pickup locations', 'ai-eshop-optimizer'), 'description' => __('Rename a pickup location everywhere at once: the location itself, all past orders (the statistics field + the shipping line), and the dependent settings (cheque-on-pickup selection + migration map). Use it to fix a name that drifted, or to merge two names into one. Edits historical orders.', 'ai-eshop-optimizer'), 'footer' => '' . ' ' . \Rooster\Admin\States::outcome('', '', array('id' => 'aieo-lpr-status')), )); echo $open; $aieo_lpr_from = array('' => '— —'); foreach ($known as $n) { $aieo_lpr_from[(string) $n] = in_array($n, $current, true) ? (string) $n : $n . ' (' . __('stale — not a current location', 'ai-eshop-optimizer') . ')'; } $aieo_lpr_list = ''; foreach ($current as $n) { $aieo_lpr_list .= ''; } $aieo_lpr_list .= ''; echo AIEO_Kit::row(__('From (existing name)', 'ai-eshop-optimizer'), AIEO_Kit::select('', $aieo_lpr_from, '', array('id' => 'aieo-lpr-from')), '', array('id' => 'aieo-lpr-from')); echo AIEO_Kit::row( __('To (new name)', 'ai-eshop-optimizer'), AIEO_Kit::input('', '', array('id' => 'aieo-lpr-to', 'medium' => true, 'placeholder' => __('Pick a current location or type a new name', 'ai-eshop-optimizer'), 'attrs' => array('list' => 'aieo-lpr-list'))) . $aieo_lpr_list, __('If "From" is a current location, it gets renamed to this. Otherwise the orders/settings are just re-pointed to this name.', 'ai-eshop-optimizer'), array('id' => 'aieo-lpr-to') ); echo $close; ?> __('Pick a source name and a different new name.', 'ai-eshop-optimizer'))); } $raw = get_option('pickup_location_pickup_locations', array()); $to_loc = null; if (is_array($raw)) { $changed = false; foreach ($raw as $i => $l) { if (is_array($l) && isset($l['name']) && (string) $l['name'] === $from) { $raw[$i]['name'] = $to; $changed = true; } } if ($changed) { update_option('pickup_location_pickup_locations', $raw, false); } foreach ($raw as $l) { if (is_array($l) && isset($l['name']) && (string) $l['name'] === $to) { $to_loc = $l; break; } } } $to_addr = ''; if (is_array($to_loc) && !empty($to_loc['address']) && is_array($to_loc['address']) && function_exists('WC') && WC()->countries) { $to_addr = (string) WC()->countries->get_formatted_address($to_loc['address'], ', '); } $to_details = (is_array($to_loc) && isset($to_loc['details'])) ? (string) $to_loc['details'] : ''; $to_maps = (is_array($to_loc) && isset($to_loc['maps_url'])) ? (string) $to_loc['maps_url'] : ''; $updated = 0; for ($pass = 0; $pass < 500; $pass++) { $ids = wc_get_orders(array( 'limit' => 100, 'return' => 'ids', 'meta_query' => array(array('key' => '_aieo_pickup_location', 'value' => $from)), )); if (empty($ids)) { break; } foreach ($ids as $oid) { $order = wc_get_order($oid); if (!$order) { continue; } $order->update_meta_data('_aieo_pickup_location', $to); if ($to_maps !== '') { $order->update_meta_data('_aieo_pickup_maps_url', $to_maps); } foreach ($order->get_shipping_methods() as $item) { $pl = (string) $item->get_meta('pickup_location'); $mtitle = method_exists($item, 'get_method_title') ? (string) $item->get_method_title() : ''; if ($pl === $from || $mtitle === $from) { $item->update_meta_data('pickup_location', $to); if ($to_addr !== '') { $item->update_meta_data('pickup_address', $to_addr); } if ($to_details !== '') { $item->update_meta_data('pickup_details', $to_details); } if (method_exists($item, 'set_method_title')) { $item->set_method_title($to); } $item->save(); } } $order->save(); $updated++; } } $pg = get_option('aieo_dmm_payment_gating', array()); if (is_array($pg) && isset($pg['pickup_locations']) && is_array($pg['pickup_locations'])) { $remapped = array(); foreach ($pg['pickup_locations'] as $n) { $remapped[] = ((string) $n === $from) ? $to : (string) $n; } $pg['pickup_locations'] = array_values(array_unique($remapped)); update_option('aieo_dmm_payment_gating', $pg, false); } $lm = get_option('aieo_pickup_legacy_map', array()); if (is_array($lm)) { $lm_changed = false; foreach ($lm as $k => $v) { if ((string) $v === $from) { $lm[$k] = $to; $lm_changed = true; } } if ($lm_changed) { update_option('aieo_pickup_legacy_map', $lm, false); } } if (class_exists('WC_Cache_Helper')) { WC_Cache_Helper::get_transient_version('shipping', true); } wp_send_json_success(array( 'message' => sprintf( __('Renamed "%1$s" → "%2$s". Orders updated: %3$d.', 'ai-eshop-optimizer'), $from, $to, $updated ), 'updated' => $updated, )); } public function ajax_import_legacy() { AIEO_DMM::verify_ajax_request(); $rows = array(); foreach (self::legacy_pickup_names() as $name) { $rows[] = array( 'name' => $name, 'enabled' => true, 'details' => '', 'address' => array('address_1' => '', 'city' => '', 'state' => '', 'postcode' => '', 'country' => 'GR'), ); } wp_send_json_success(array('locations' => $rows)); } public function ajax_save() { AIEO_DMM::verify_ajax_request(); $mode = (isset($_POST['mode']) && sanitize_key(wp_unslash($_POST['mode'])) === 'blocks') ? 'blocks' : 'legacy'; $title = isset($_POST['title']) ? sanitize_text_field(wp_unslash($_POST['title'])) : ''; $locations = array(); if (isset($_POST['locations_json'])) { self::load_google_reviews(); $decoded = json_decode((string) wp_unslash($_POST['locations_json']), true); if (is_array($decoded)) { foreach ($decoded as $loc) { if (!is_array($loc)) { continue; } $name = isset($loc['name']) ? sanitize_text_field((string) $loc['name']) : ''; if ($name === '') { continue; } $addr = (isset($loc['address']) && is_array($loc['address'])) ? $loc['address'] : array(); $country = isset($addr['country']) ? sanitize_text_field((string) $addr['country']) : 'GR'; if ($country === '') { $country = 'GR'; } $review_slug = isset($loc['review_slug']) ? sanitize_key((string) $loc['review_slug']) : ''; $maps_url = ''; if ($review_slug !== '' && class_exists('AIEO_Google_Reviews_Locations') && class_exists('AIEO_Google_Reviews_Helpers')) { $rl = AIEO_Google_Reviews_Locations::get_by_slug($review_slug); if (is_array($rl)) { $maps_url = (string) AIEO_Google_Reviews_Helpers::maps_url($rl); } else { $review_slug = ''; } } $locations[] = array( 'name' => $name, 'enabled' => !empty($loc['enabled']), 'details' => isset($loc['details']) ? sanitize_text_field((string) $loc['details']) : '', 'review_slug' => $review_slug, 'maps_url' => $maps_url, 'address' => array( 'address_1' => isset($addr['address_1']) ? sanitize_text_field((string) $addr['address_1']) : '', 'city' => isset($addr['city']) ? sanitize_text_field((string) $addr['city']) : '', 'state' => isset($addr['state']) ? sanitize_text_field((string) $addr['state']) : '', 'postcode' => isset($addr['postcode']) ? sanitize_text_field((string) $addr['postcode']) : '', 'country' => $country, ), ); } } } $blocked_cc = array(); if (isset($_POST['blocked_countries_json'])) { $decoded = json_decode((string) wp_unslash($_POST['blocked_countries_json']), true); if (is_array($decoded)) { $valid = array_change_key_case(self::shipping_countries(), CASE_UPPER); foreach ($decoded as $cc) { $cc = strtoupper(sanitize_text_field((string) $cc)); if ($cc !== '' && isset($valid[$cc]) && !in_array($cc, $blocked_cc, true)) { $blocked_cc[] = $cc; } } } } update_option(self::OPTION_KEY, array('mode' => $mode, 'title' => $title, 'blocked_countries' => $blocked_cc), false); update_option('pickup_location_pickup_locations', $locations); update_option('woocommerce_pickup_location_settings', array( 'enabled' => ($mode === 'blocks') ? 'yes' : 'no', 'title' => $title, 'cost' => '', 'tax_status' => 'none', )); if (class_exists('AIEO_DMM_Payment_Gating')) { $pg = get_option(AIEO_DMM_Payment_Gating::OPTION_KEY, array()); if (is_array($pg)) { $tok = isset($pg['shipping_match_token']) ? (string) $pg['shipping_match_token'] : ''; if ($mode === 'blocks' && ($tok === 'local_pickup' || $tok === 'pickup_location')) { $pg['shipping_match_token'] = AIEO_DMM_Payment_Gating::PICKUP_ANY_TOKEN; update_option(AIEO_DMM_Payment_Gating::OPTION_KEY, $pg, false); } } } wp_send_json_success(array( 'message' => __('Local pickup settings saved.', 'ai-eshop-optimizer'), 'mode' => $mode, 'count' => count($locations), )); } }