modules = $modules; } public function register(): void { add_action('admin_menu', [$this, 'addMenuPage']); add_action('admin_init', [$this, 'registerSettings']); add_action('admin_enqueue_scripts', [$this, 'enqueueAssets']); add_action('admin_notices', [$this, 'adminNotices']); } // Branding private function getMenuIcon(): string { $svg = '' . '' . ''; return 'data:image/svg+xml;base64,' . base64_encode($svg); } // Menu & settings registration public function addMenuPage(): void { add_menu_page( __('Aikanen', 'aikanen'), __('Aikanen', 'aikanen'), 'manage_woocommerce', 'aikanen', [$this, 'render'], $this->getMenuIcon(), 56 ); } public function registerSettings(): void { // Connection (API and Webhook) register_setting('aikanen', 'aikanen_api_token', [ // API 'type' => 'string', 'sanitize_callback' => function ($value) { $value = sanitize_text_field($value); return $value !== '' ? $value : get_option('aikanen_api_token', ''); // Don't update on empty value }, ]); register_setting('aikanen', 'aikanen_webhook_secret', [ // Webhook 'type' => 'string', 'sanitize_callback' => function ($value) { $value = trim($value); return $value !== '' ? $value : get_option('aikanen_webhook_secret', ''); // Keep existing value if empty }, ]); // VAT mapping register_setting('aikanen', ProductImportMapper::OPTION_VAT_MAP, [ 'type' => 'array', 'sanitize_callback' => [$this, 'sanitizeVatMap'], ]); // Module toggles foreach ($this->modules as $key => $module) { register_setting('aikanen', "aikanen_module_{$key}_enabled", [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ]); } // Product auto export selection if (isset($this->modules['product_export'])) { register_setting('aikanen', 'aikanen_auto_export_product', [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ]); } // Product auto import selection if (isset($this->modules['product_import'])) { register_setting('aikanen', 'aikanen_auto_import_product', [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ]); } // Category auto export selection if (isset($this->modules['category_export'])) { register_setting('aikanen', 'aikanen_auto_export_category', [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ]); } // Category auto import selection if (isset($this->modules['category_import'])) { register_setting('aikanen', 'aikanen_auto_import_category', [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ]); } // Sync stock immediately on stock update events if (isset($this->modules['stock_sync'])) { register_setting('aikanen', 'aikanen_auto_stock_sync', [ 'type' => 'boolean', 'default' => false, 'sanitize_callback' => 'rest_sanitize_boolean', ]); } // Full stock sync settings register_setting('aikanen', 'aikanen_stock_source_of_truth', [ // Direction where sync is done, from Aikanen to Woo, or Woo to Aikanen 'type' => 'string', 'default' => 'aikanen', 'sanitize_callback' => fn ($v) => in_array($v, ['aikanen', 'woocommerce', 'bidirectional']) ? $v : 'aikanen', ]); register_setting('aikanen', 'aikanen_stock_full_sync_interval', [ // Frequency for full stock sync 'type' => 'string', 'default' => 'hourly', 'sanitize_callback' => fn($v) => in_array($v, ['disabled', 'hourly', 'daily'], true) ? $v : 'disabled', ]); } public function sanitizeVatMap(mixed $input): array { if (!is_array($input)) { return ProductImportMapper::DEFAULT_VAT_MAP; } $map = []; foreach ([1, 2, 3, 4] as $vatId) { $value = $input[$vatId] ?? ProductImportMapper::DEFAULT_VAT_MAP[$vatId]; $map[$vatId] = wc_format_decimal($value, 2); } return $map; } // Assets public function enqueueAssets(string $hook): void { if ($hook !== 'toplevel_page_aikanen') return; wp_enqueue_style('aikanen-admin', plugins_url('assets/css/admin.css', AIKANEN_PLUGIN_FILE), [], AIKANEN_VERSION); // Get styles wp_enqueue_script('aikanen-admin', plugins_url('assets/js/admin.js', AIKANEN_PLUGIN_FILE), [], AIKANEN_VERSION, true); // Get scripts wp_add_inline_script('aikanen-admin', ' (function () { var interval = document.querySelector("[name=\"aikanen_stock_full_sync_interval\"]"); var sourceRow = document.getElementById("aikanen-stock-source-row"); if (!interval || !sourceRow) return; interval.addEventListener("change", function () { sourceRow.hidden = this.value === "disabled"; }); })(); document.getElementById("aikanen-select-all")?.addEventListener("change", function () { document.querySelectorAll("input[name=\"services[]\"]").forEach(function (cb) { cb.checked = this.checked; }, this); }); '); } // Admin notices public function adminNotices(): void { $notice = get_transient('aikanen_notice'); if (!$notice) { return; } delete_transient('aikanen_notice'); printf( '

%s

', esc_attr($notice['type']), esc_html($notice['message']) ); } // Render settings public function render(): void { if (!current_user_can('manage_woocommerce')) { wp_die(esc_html(__('You do not have permission to access this page.', 'aikanen'))); } $connected = $this->testConnection(); $importModule = $this->modules['product_import'] ?? null; $previewData = $importModule ? get_transient('aikanen_preview') : false; $importedCount = $importModule ? $importModule->getImportedCount() : 0; $categoryExportModule = $this->modules['category_export'] ?? null; $categoryImportModule = $this->modules['category_import'] ?? null; $categoryMappingData = get_transient('aikanen_fetched_groups') ?: null; include __DIR__ . '/views/settings.php'; } // Test connection/credentials to Aikanen private function testConnection(): array { $token = get_option('aikanen_api_token'); if (!$token) { return ['status' => 'disconnected', 'company' => null]; } try { (new ApiClient($token))->get('/test'); return ['status' => 'connected']; } catch (\Exception $e) { return ['status' => 'error', 'message' => $e->getMessage()]; } } }