# king-addons/51.1.44/includes/helpers/Woo_Builder/Context.php

King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder, version 51.1.44. 219 lines.

- Page: https://pluginprobe.com/plugins/king-addons/51.1.44/code/includes/helpers/Woo_Builder/Context.php
- Raw: https://pluginprobe.com/plugins/king-addons/51.1.44/raw/includes/helpers/Woo_Builder/Context.php
- Modified: 2025-12-27T05:19:20+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/king-addons/51.1.44/code/includes/helpers/Woo_Builder/Context.php#L10-L20`.

```php
<?php
/**
 * Woo Builder context helpers.
 *
 * @package King_Addons
 */

namespace King_Addons\Woo_Builder;

use WC_Product;

if (!defined('ABSPATH')) {
    exit;
}

/**
 * Utility helpers for Woo Builder.
 */
class Context
{
    /**
     * Detect current WooCommerce context.
     *
     * Cart, Checkout, and My Account pages may not pass is_woocommerce()
     * so we check them separately.
     *
     * @return string|null
     */
    public static function detect_context(): ?string
    {
        if (!function_exists('is_woocommerce')) {
            return null;
        }

        // Check cart page first (may not pass is_woocommerce())
        if (function_exists('is_cart') && is_cart()) {
            return 'cart';
        }

        // Check checkout page (may not pass is_woocommerce())
        if (function_exists('is_checkout') && is_checkout() && !is_order_received_page()) {
            return 'checkout';
        }

        // Check My Account page (may not pass is_woocommerce())
        if (function_exists('is_account_page') && is_account_page()) {
            return 'my_account';
        }

        // For product and archive pages, is_woocommerce() should be true
        if (!is_woocommerce()) {
            return null;
        }

        if (is_product()) {
            return 'single_product';
        }

        if (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy()) {
            return 'product_archive';
        }

        return null;
    }

    /**
    * Get current product ID.
    *
    * @return int|null
    */
    public static function get_product_id(): ?int
    {
        global $product, $post;

        if ($product instanceof WC_Product) {
            return (int) $product->get_id();
        }

        if (!empty($post->ID) && 'product' === get_post_type($post->ID)) {
            return (int) $post->ID;
        }

        return null;
    }

    /**
    * Match conditions array against current page context.
    *
    * @param string               $context    Page context.
    * @param array<string,mixed>  $conditions Conditions array.
    *
    * @return bool
    */
    public static function match_conditions(string $context, array $conditions): bool
    {
        if (empty($conditions['rules']) || !is_array($conditions['rules'])) {
            return true;
        }

        $product_id = self::get_product_id();

        foreach ($conditions['rules'] as $rule) {
            $type = $rule['type'] ?? '';
            $values = is_array($rule['values'] ?? null) ? $rule['values'] : [];

            if (!self::match_rule($context, $type, $values, $product_id)) {
                return false;
            }
        }

        return true;
    }

    /**
    * Match a single rule.
    *
    * @param string    $context    Context.
    * @param string    $type       Rule type.
    * @param array     $values     Rule values.
    * @param int|null  $product_id Product ID.
    *
    * @return bool
    */
    private static function match_rule(string $context, string $type, array $values, ?int $product_id): bool
    {
        switch ($type) {
            case 'all_products':
                return 'single_product' === $context;
            case 'product_in':
            case 'products':
                return ('single_product' === $context && $product_id && in_array($product_id, $values, true));
            case 'product_cat_in':
            case 'product_categories':
                if ('single_product' !== $context || !$product_id) {
                    return false;
                }
                $terms = wp_get_post_terms($product_id, 'product_cat', ['fields' => 'ids']);
                return !empty(array_intersect($values, $terms));
            case 'product_tag_in':
            case 'product_tags':
                if ('single_product' !== $context || !$product_id) {
                    return false;
                }
                $terms = wp_get_post_terms($product_id, 'product_tag', ['fields' => 'ids']);
                return !empty(array_intersect($values, $terms));
            case 'product_type_in':
            case 'product_types':
                if ('single_product' !== $context || !$product_id) {
                    return false;
                }
                $types = wp_get_object_terms($product_id, 'product_type', ['fields' => 'slugs']);
                return !empty(array_intersect($values, $types));
            case 'is_shop':
            case 'shop':
                return 'product_archive' === $context && is_shop();
            case 'product_cat_archive_in':
            case 'product_cat_archives':
                if ('product_archive' !== $context) {
                    return false;
                }
                if (is_product_category()) {
                    $term = get_queried_object();
                    if (!empty($term->term_id)) {
                        return in_array((int) $term->term_id, $values, true);
                    }
                }
                return false;
            case 'cart':
                return 'cart' === $context;
            case 'checkout':
                return 'checkout' === $context;
            case 'my_account':
                return 'my_account' === $context;
            case 'always':
                return true;
            default:
                return true;
        }
    }

    /**
     * Render an editor-only notice if widget is not in the expected Woo context.
     *
     * @param string $expected_context Expected context slug.
     *
     * @return bool True when context matches or not in edit mode, false when notice rendered.
     */
    public static function maybe_render_context_notice(string $expected_context): bool
    {
        if (!class_exists('\Elementor\Plugin') || !\Elementor\Plugin::$instance->editor->is_edit_mode()) {
            return true;
        }

        $current = self::detect_context();
        if ($current === $expected_context) {
            return true;
        }

        $labels = [
            'single_product' => esc_html__('This widget works on Single Product pages.', 'king-addons'),
            'product_archive' => esc_html__('This widget works on Product Archive pages.', 'king-addons'),
            'cart' => esc_html__('This widget works on the Cart page.', 'king-addons'),
            'checkout' => esc_html__('This widget works on the Checkout page.', 'king-addons'),
            'my_account' => esc_html__('This widget works on My Account pages.', 'king-addons'),
        ];

        $message = $labels[$expected_context] ?? esc_html__('This widget is only available on WooCommerce pages.', 'king-addons');
        echo '<div class="king-addons-woo-builder-notice">' . esc_html($message) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        return false;
    }
}








```
