prefix . 'aieo_dmm_branches'; } public static function matchers_table() { global $wpdb; return $wpdb->prefix . 'aieo_dmm_branch_matchers'; } public static function flush_request_cache() { self::$rows = null; } public static function all($enabled_only = false) { global $wpdb; if (self::$rows === null) { $t = self::table(); if (!function_exists('aieo_dmm_table_exists')) { $f = AIEO_DIR . 'includes/sql/sql-runtime-helpers.php'; if (is_readable($f)) { require_once $f; } } if (function_exists('aieo_dmm_table_exists') && !aieo_dmm_table_exists($t)) { self::$rows = array(); } else { $rows = $wpdb->get_results("SELECT * FROM `{$t}` ORDER BY sort_order ASC, label ASC, id ASC", ARRAY_A); self::$rows = is_array($rows) ? $rows : array(); } } if (!$enabled_only) { return self::$rows; } return array_values(array_filter(self::$rows, static function ($r) { return (int) $r['enabled'] === 1; })); } public static function labels($enabled_only = true) { $out = array(); foreach (self::all($enabled_only) as $r) { $out[(string) $r['slug']] = (string) $r['label']; } return $out; } public static function by_slug($slug) { foreach (self::all() as $r) { if ((string) $r['slug'] === (string) $slug) { return $r; } } return null; } public static function by_id($id) { foreach (self::all() as $r) { if ((int) $r['id'] === (int) $id) { return $r; } } return null; } public static function cached() { $v = get_option(self::CACHE_OPTION, null); if (is_array($v)) { return $v; } return self::sync_cache(); } public static function sync_cache() { $out = array(); foreach (self::all() as $r) { $out[] = array( 'id' => (int) $r['id'], 'slug' => (string) $r['slug'], 'label' => (string) $r['label'], 'enabled' => (int) $r['enabled'], 'sort_order' => (int) $r['sort_order'], ); } update_option(self::CACHE_OPTION, $out, true); return $out; } public static function patterns_for_row(array $row) { $raw = trim((string) ($row['match_patterns'] ?? '')); if ($raw === '') { $raw = trim((string) ($row['label'] ?? '')); } $out = array(); foreach (explode('|', $raw) as $p) { $p = trim($p); if ($p !== '' && !in_array($p, $out, true)) { $out[] = $p; } } return $out; } public static function sync_matchers() { global $wpdb; $mt = self::matchers_table(); if (!function_exists('aieo_dmm_table_exists') || !aieo_dmm_table_exists($mt)) { return 0; } $rows = array(); foreach (self::all(true) as $r) { foreach (self::patterns_for_row($r) as $p) { $esc = str_replace(array('\\', '%', '_'), array('\\\\', '\\%', '\\_'), $p); $esc = function_exists('mb_substr') ? mb_substr($esc, 0, 191) : substr($esc, 0, 191); $rows[$r['id'] . '|' . $esc] = array((int) $r['id'], $esc); } } $wpdb->query("DELETE FROM `{$mt}`"); foreach ($rows as $r) { $wpdb->insert($mt, array('branch_id' => $r[0], 'pattern' => $r[1]), array('%d', '%s')); } return count($rows); } public static function sync() { self::flush_request_cache(); self::sync_cache(); self::sync_matchers(); } public static function eshop_invoice_type() { $v = trim((string) get_option(self::OPT_ESHOP_INVOICE_TYPE, '')); return (string) apply_filters('aieo_dmm_erp_eshop_invoice_type', $v); } public static function id_for_order(WC_Order $order): ?int { return self::id_for_erp((string) $order->get_meta('soft1_branches', true)); } public static function id_for_erp($erp): ?int { $erp = trim((string) $erp); if ($erp !== '' && strpos($erp, ',') !== false) { $parts = array_filter(array_map('trim', explode(',', $erp))); $erp = (string) reset($parts); } if ($erp === '') { return null; } $map = array(); $rows = self::all(); usort($rows, static function ($a, $b) { return (int) $a['id'] <=> (int) $b['id']; }); foreach ($rows as $r) { $key = trim((string) ($r['erp_id'] ?? '')); if ($key !== '') { $map[$key] = (int) $r['id']; } } return isset($map[$erp]) ? $map[$erp] : null; } } }