| @@ -543,8 +543,96 @@ | ||
| 543 | 543 | |
| 544 | 544 | echo '<div class="king-addons-woo-builder-notice">' . esc_html($message) . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 545 | 545 | return false; |
| 546 | 546 | } |
| 547 | + | |
| 548 | + /** | |
| 549 | + * Whether the active Woo Builder template for a context contains a widget. | |
| 550 | + * | |
| 551 | + * Used so companion widgets can hide the native WooCommerce copy of the | |
| 552 | + * same block (login, coupon, payment, variable add-to-cart, and so on). | |
| 553 | + * | |
| 554 | + * @param string $widget_type Elementor widget name. | |
| 555 | + * @param string|null $context Woo Builder context. Detected when omitted. | |
| 556 | + * | |
| 557 | + * @return bool | |
| 558 | + */ | |
| 559 | + public static function template_has_widget(string $widget_type, ?string $context = null): bool | |
| 560 | + { | |
| 561 | + static $cache = []; | |
| 562 | + | |
| 563 | + $widget_type = sanitize_key($widget_type); | |
| 564 | + if ('' === $widget_type) { | |
| 565 | + return false; | |
| 566 | + } | |
| 567 | + | |
| 568 | + $context = $context ?: (self::detect_context() ?? self::get_editor_template_type()); | |
| 569 | + if (!$context) { | |
| 570 | + return false; | |
| 571 | + } | |
| 572 | + | |
| 573 | + $key = $context . '|' . $widget_type; | |
| 574 | + if (array_key_exists($key, $cache)) { | |
| 575 | + return $cache[$key]; | |
| 576 | + } | |
| 577 | + | |
| 578 | + $widgets = self::find_template_widgets($widget_type, $context); | |
| 579 | + $cache[$key] = !empty($widgets); | |
| 580 | + return $cache[$key]; | |
| 581 | + } | |
| 582 | + | |
| 583 | + /** | |
| 584 | + * Widget instances of a type on the current Woo Builder template. | |
| 585 | + * | |
| 586 | + * @param string $widget_type Elementor widget name. | |
| 587 | + * @param string|null $context Woo Builder context. Detected when omitted. | |
| 588 | + * | |
| 589 | + * @return array<int,array{id:string,settings:array<string,mixed>}> | |
| 590 | + */ | |
| 591 | + public static function find_template_widgets(string $widget_type, ?string $context = null): array | |
| 592 | + { | |
| 593 | + $widget_type = sanitize_key($widget_type); | |
| 594 | + if ('' === $widget_type) { | |
| 595 | + return []; | |
| 596 | + } | |
| 597 | + | |
| 598 | + $context = $context ?: (self::detect_context() ?? self::get_editor_template_type()); | |
| 599 | + if (!$context) { | |
| 600 | + return []; | |
| 601 | + } | |
| 602 | + | |
| 603 | + $template_id = (int) apply_filters('king_addons/woo_builder/current_template_id', 0, $context); | |
| 604 | + if ($template_id < 1 && self::is_editor()) { | |
| 605 | + $template_id = (int) self::get_editor_post_id(); | |
| 606 | + } | |
| 607 | + | |
| 608 | + if ($template_id < 1) { | |
| 609 | + return []; | |
| 610 | + } | |
| 611 | + | |
| 612 | + $data = json_decode((string) get_post_meta($template_id, '_elementor_data', true), true); | |
| 613 | + if (!is_array($data)) { | |
| 614 | + return []; | |
| 615 | + } | |
| 616 | + | |
| 617 | + $found = []; | |
| 618 | + $walk = static function ($els) use (&$walk, &$found, $widget_type): void { | |
| 619 | + foreach ($els as $el) { | |
| 620 | + if (($el['elType'] ?? '') === 'widget' && ($el['widgetType'] ?? '') === $widget_type) { | |
| 621 | + $found[] = [ | |
| 622 | + 'id' => (string) ($el['id'] ?? ''), | |
| 623 | + 'settings' => is_array($el['settings'] ?? null) ? $el['settings'] : [], | |
| 624 | + ]; | |
| 625 | + } | |
| 626 | + if (!empty($el['elements']) && is_array($el['elements'])) { | |
| 627 | + $walk($el['elements']); | |
| 628 | + } | |
| 629 | + } | |
| 630 | + }; | |
| 631 | + $walk($data); | |
| 632 | + | |
| 633 | + return $found; | |
| 634 | + } | |
| 547 | 635 | } |
| 548 | 636 | |
| 549 | 637 | |
| 550 | 638 | |