api_url = get_option('dutchie_analytics_api_url', 'http://localhost:3010/api'); // Try to get API key from permissions first, fallback to manual token $this->api_token = get_option('dutchie_analytics_api_token', ''); if (empty($this->api_token) && class_exists('DutchieAnalytics\API_Permissions')) { $this->api_token = API_Permissions::get_site_api_key(); } } /** * Make API request */ private function request($endpoint, $params = []) { $url = $this->api_url . $endpoint; if (!empty($params)) { $url .= '?' . http_build_query($params); } $args = [ 'headers' => [ 'X-API-Key' => $this->api_token, 'Content-Type' => 'application/json', ], 'timeout' => 30, ]; $response = wp_remote_get($url, $args); if (is_wp_error($response)) { error_log('Dutchie Analytics API Error: ' . $response->get_error_message()); return null; } $body = wp_remote_retrieve_body($response); return json_decode($body, true); } /** * Get all stores */ public function get_stores() { return $this->request('/stores'); } /** * Get single store */ public function get_store($id) { return $this->request('/stores/' . $id); } /** * Get store brands */ public function get_store_brands($store_id) { return $this->request('/stores/' . $store_id . '/brands'); } /** * Get store specials */ public function get_store_specials($store_id, $date = null) { $params = $date ? ['date' => $date] : []; return $this->request('/stores/' . $store_id . '/specials', $params); } /** * Get products * * @param array $args { * @type int $store_id Filter by store ID * @type int $category_id Filter by category ID * @type bool $in_stock Filter by stock availability * @type string $search Search query * @type int $limit Results per page (default: 50, max: 1000) * @type int $offset Pagination offset * @type string $fields Comma-separated list of fields to return * } */ public function get_products($args = []) { $defaults = [ 'limit' => 50, 'offset' => 0, ]; $params = wp_parse_args($args, $defaults); return $this->request('/products', $params); } /** * Get single product */ public function get_product($id) { return $this->request('/products/' . $id); } /** * Get categories */ public function get_categories($store_id = null) { $params = $store_id ? ['store_id' => $store_id] : []; return $this->request('/categories', $params); } /** * Get category tree */ public function get_category_tree($store_id) { return $this->request('/categories/tree', ['store_id' => $store_id]); } }