detect_page_type(); $context = [ 'page' => $page['type'], 'page_url' => $this->get_current_url(), 'language' => get_locale(), 'country' => $this->get_visitor_country(), 'device' => wp_is_mobile() ? 'mobile' : 'desktop', 'search_term' => $this->get_search_term(), 'cart' => $this->get_cart_product_ids(), 'catalog_categories' => $this->get_catalog_categories(), ]; if (!empty($page['product_id'])) { $context['product_id'] = $page['product_id']; $context['product'] = $this->get_product_metadata((int) $page['product_id']); } if (!empty($page['category'])) { $context['category'] = $page['category']; } if (!empty($page['category_id'])) { $context['category_id'] = $page['category_id']; } if (is_user_logged_in()) { $context['customer_id'] = get_current_user_id(); } return $context; } /** * Detect the current page type and related identifiers. * * @return array{type: string, product_id?: int, category?: string, category_id?: int} */ private function detect_page_type() { if (function_exists('is_product') && is_product()) { $product_id = get_queried_object_id(); $category = ''; if ($product_id && function_exists('wc_get_product')) { $terms = get_the_terms($product_id, 'product_cat'); if (!empty($terms) && !is_wp_error($terms)) { $category = $terms[0]->name; } } return [ 'type' => 'product', 'product_id' => $product_id, 'category' => $category, ]; } if (function_exists('is_product_category') && is_product_category()) { $term = get_queried_object(); return [ 'type' => 'category', 'category' => ($term && isset($term->name)) ? $term->name : '', 'category_id' => ($term && isset($term->term_id)) ? (int) $term->term_id : 0, ]; } if (function_exists('is_shop') && is_shop()) { return ['type' => 'shop']; } if (function_exists('is_cart') && is_cart()) { return ['type' => 'cart']; } if (function_exists('is_checkout') && is_checkout()) { return ['type' => 'checkout']; } if (function_exists('is_search') && is_search()) { return ['type' => 'search']; } if (is_front_page() || is_home()) { return ['type' => 'home']; } if (is_singular('post')) { return ['type' => 'blog']; } if (is_page()) { return ['type' => 'page']; } return ['type' => 'other']; } /** * @param int $product_id * @return array */ private function get_product_metadata($product_id) { if (!function_exists('wc_get_product') || $product_id <= 0) { return []; } $product = wc_get_product($product_id); if (!$product) { return []; } $categories = []; $terms = get_the_terms($product_id, 'product_cat'); if (!empty($terms) && !is_wp_error($terms)) { foreach ($terms as $term) { $categories[] = $term->name; } } $tags = []; $tag_terms = get_the_terms($product_id, 'product_tag'); if (!empty($tag_terms) && !is_wp_error($tag_terms)) { foreach ($tag_terms as $term) { $tags[] = $term->name; } } $brands = []; if (taxonomy_exists('product_brand')) { $brand_terms = get_the_terms($product_id, 'product_brand'); if (!empty($brand_terms) && !is_wp_error($brand_terms)) { foreach ($brand_terms as $term) { $brands[] = $term->name; } } } return [ 'id' => $product_id, 'name' => $product->get_name(), 'price' => (float) $product->get_price(), 'categories' => $categories, 'tags' => $tags, 'brands' => $brands, 'in_stock' => $product->is_in_stock(), 'on_sale' => $product->is_on_sale(), 'sku' => $product->get_sku(), 'has_description' => $this->product_has_description($product), 'has_variations' => $product->is_type('variable'), 'options' => $this->get_product_options($product), ]; } /** * @return string[] */ private function get_catalog_categories() { if (!taxonomy_exists('product_cat')) { return []; } $terms = get_terms([ 'taxonomy' => 'product_cat', 'hide_empty' => true, 'number' => 30, 'orderby' => 'name', 'order' => 'ASC', ]); if (empty($terms) || is_wp_error($terms)) { return []; } $categories = []; foreach ($terms as $term) { $name = sanitize_text_field($term->name); if ($name !== '') { $categories[] = $name; } } return array_values(array_unique($categories)); } /** * @param WC_Product $product * @return bool */ private function product_has_description($product) { $description = trim(wp_strip_all_tags((string) $product->get_description())); if ($description !== '') { return true; } $short_description = trim(wp_strip_all_tags((string) $product->get_short_description())); return $short_description !== ''; } /** * @param WC_Product $product * @return array */ private function get_product_options($product) { $options = []; if (!$product->is_type('variable')) { return $options; } foreach ($product->get_variation_attributes() as $attribute_name => $values) { $label = sanitize_text_field(wc_attribute_label($attribute_name)); $normalized_values = array_values(array_filter(array_map('sanitize_text_field', (array) $values))); if ($label !== '' && count($normalized_values) > 1) { $options[$label] = $normalized_values; } } return $options; } /** * @return int[] */ private function get_cart_product_ids() { if (!function_exists('WC') || !WC()->cart) { return []; } $ids = []; foreach (WC()->cart->get_cart() as $item) { if (!empty($item['product_id'])) { $ids[] = (int) $item['product_id']; } } return array_values(array_unique($ids)); } /** * @return string */ private function get_search_term() { if (function_exists('is_search') && is_search()) { return sanitize_text_field(get_search_query()); } $search_query = filter_input(INPUT_GET, 's', FILTER_SANITIZE_FULL_SPECIAL_CHARS); if (is_string($search_query) && $search_query !== '') { return sanitize_text_field($search_query); } return ''; } /** * @return string */ private function get_visitor_country() { if (function_exists('wc_get_customer_default_location')) { $location = wc_get_customer_default_location(); if (!empty($location['country'])) { return sanitize_text_field($location['country']); } } return ''; } /** * @return string */ private function get_current_url() { if (!empty($_SERVER['HTTP_HOST']) && !empty($_SERVER['REQUEST_URI'])) { $scheme = is_ssl() ? 'https' : 'http'; return esc_url_raw($scheme . '://' . wp_unslash($_SERVER['HTTP_HOST']) . wp_unslash($_SERVER['REQUEST_URI'])); } return home_url(add_query_arg([])); } }