| 1 |
<?php |
| 2 |
/** |
| 3 |
* Facet Meta/ACF filter widget (placeholder). |
| 4 |
* |
| 5 |
* @package King_Addons |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace King_Addons; |
| 9 |
|
| 10 |
use Elementor\Controls_Manager; |
| 11 |
use Elementor\Widget_Base; |
| 12 |
use King_Addons\Woo_Builder\Context as Woo_Context; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) { |
| 15 |
exit; |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Renders meta/ACF filter controls for faceted filters. |
| 20 |
*/ |
| 21 |
class Facet_Meta extends Widget_Base |
| 22 |
{ |
| 23 |
public function get_name(): string |
| 24 |
{ |
| 25 |
return 'facet_meta'; |
| 26 |
} |
| 27 |
|
| 28 |
public function get_title(): string |
| 29 |
{ |
| 30 |
return esc_html__('Custom Field Filter', 'king-addons'); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Keywords. |
| 35 |
* |
| 36 |
* @return array<int, string> |
| 37 |
*/ |
| 38 |
public function get_keywords(): array |
| 39 |
{ |
| 40 |
return ['shop', 'product', 'filter', 'filters', 'ajax', 'woocommerce', 'acf', 'meta', 'sku', 'faceted', 'smart filters']; |
| 41 |
} |
| 42 |
|
| 43 |
public function get_icon(): string |
| 44 |
{ |
| 45 |
return 'king-addons-icon king-addons-facet-meta'; |
| 46 |
} |
| 47 |
|
| 48 |
public function get_categories(): array |
| 49 |
{ |
| 50 |
return ['king-addons']; |
| 51 |
} |
| 52 |
|
| 53 |
public function get_script_depends(): array |
| 54 |
{ |
| 55 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-facet-meta-script']; |
| 56 |
} |
| 57 |
|
| 58 |
public function get_style_depends(): array |
| 59 |
{ |
| 60 |
return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-facet-meta-style']; |
| 61 |
} |
| 62 |
|
| 63 |
public function get_custom_help_url() |
| 64 |
{ |
| 65 |
return 'mailto:[email protected]?subject=Bug Report - King Addons&body=Please describe the issue'; |
| 66 |
} |
| 67 |
|
| 68 |
protected function register_controls(): void |
| 69 |
{ |
| 70 |
$this->start_controls_section( |
| 71 |
'section_content', |
| 72 |
[ |
| 73 |
'label' => esc_html__('Content', 'king-addons'), |
| 74 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 75 |
] |
| 76 |
); |
| 77 |
|
| 78 |
Core::renderUpgradeProSection($this, Controls_Manager::RAW_HTML, 'facet-meta', 'facet_meta_pro_notice'); |
| 79 |
|
| 80 |
$this->add_control( |
| 81 |
'query_id', |
| 82 |
[ |
| 83 |
'label' => esc_html__('Shop Filters ID', 'king-addons'), |
| 84 |
'type' => Controls_Manager::TEXT, |
| 85 |
'description' => esc_html__('Must match the Shop Filters ID on the product grid.', 'king-addons'), |
| 86 |
] |
| 87 |
); |
| 88 |
|
| 89 |
$this->add_control( |
| 90 |
'meta_key', |
| 91 |
[ |
| 92 |
'label' => esc_html__('Meta/ACF key', 'king-addons'), |
| 93 |
'type' => Controls_Manager::TEXT, |
| 94 |
'placeholder' => 'custom_field', |
| 95 |
] |
| 96 |
); |
| 97 |
|
| 98 |
$this->add_control( |
| 99 |
'control_type', |
| 100 |
[ |
| 101 |
'label' => esc_html__('Control type', 'king-addons'), |
| 102 |
'type' => Controls_Manager::SELECT, |
| 103 |
'options' => [ |
| 104 |
'text' => esc_html__('Text', 'king-addons'), |
| 105 |
'select' => esc_html__('Select', 'king-addons'), |
| 106 |
'range' => esc_html__('Range', 'king-addons'), |
| 107 |
], |
| 108 |
'default' => 'text', |
| 109 |
] |
| 110 |
); |
| 111 |
|
| 112 |
$this->add_control( |
| 113 |
'placeholder', |
| 114 |
[ |
| 115 |
'label' => esc_html__('Placeholder', 'king-addons'), |
| 116 |
'type' => Controls_Manager::TEXT, |
| 117 |
] |
| 118 |
); |
| 119 |
|
| 120 |
$this->end_controls_section(); |
| 121 |
|
| 122 |
require_once KING_ADDONS_PATH . 'includes/helpers/Faceted/Style_Controls.php'; |
| 123 |
Facet_Style_Controls::fields($this, '{{WRAPPER}} .ka-facet-meta__input, {{WRAPPER}} .ka-facet-meta__select, {{WRAPPER}} .ka-facet-meta__range input'); |
| 124 |
} |
| 125 |
|
| 126 |
protected function render(): void |
| 127 |
{ |
| 128 |
if (!function_exists('king_addons_can_use_pro') || !king_addons_can_use_pro()) { |
| 129 |
if (class_exists(Woo_Context::class) && Woo_Context::is_editor()) { |
| 130 |
echo '<div class="king-addons-woo-builder-notice">' . esc_html__('Custom Field Filter is available in Pro.', 'king-addons') . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 131 |
} |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$settings = $this->get_settings_for_display(); |
| 136 |
$query_id = sanitize_title($settings['query_id'] ?? ''); |
| 137 |
$meta_key = sanitize_key($settings['meta_key'] ?? ''); |
| 138 |
if (empty($query_id) || empty($meta_key)) { |
| 139 |
if (class_exists(Woo_Context::class) && Woo_Context::is_editor()) { |
| 140 |
echo '<div class="king-addons-woo-builder-notice">' . esc_html__('Set Query ID and Meta key.', 'king-addons') . '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 141 |
} |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
$type = $settings['control_type'] ?? 'text'; |
| 146 |
$placeholder = $settings['placeholder'] ?? ''; |
| 147 |
if ('_sku' === $meta_key) { |
| 148 |
$meta_label = __('SKU', 'king-addons'); |
| 149 |
} else { |
| 150 |
$meta_label = ucwords(str_replace(['_', '-'], ' ', (string) preg_replace('/^ka_/', '', $meta_key))); |
| 151 |
} |
| 152 |
$shared = ' data-ka-filters-query-id="' . esc_attr($query_id) . '" data-ka-filter-type="meta" data-ka-meta-key="' . esc_attr($meta_key) . '" data-ka-meta-label="' . esc_attr($meta_label) . '"'; |
| 153 |
|
| 154 |
echo '<div class="ka-facet-meta">'; |
| 155 |
if ('text' === $type) { |
| 156 |
echo '<input type="text" class="ka-facet-meta__input"' . $shared . ' data-ka-meta-role="equals" placeholder="' . esc_attr($placeholder) . '" />'; |
| 157 |
} elseif ('select' === $type) { |
| 158 |
$ph = $placeholder ?: __('Any', 'king-addons'); |
| 159 |
echo '<select class="ka-facet-meta__select"' . $shared . ' data-ka-meta-role="equals" data-placeholder="' . esc_attr($ph) . '"><option value="">' . esc_html($ph) . '</option></select>'; |
| 160 |
} elseif ('range' === $type) { |
| 161 |
echo '<div class="ka-facet-meta__range">'; |
| 162 |
echo '<input type="number" class="ka-facet-meta__range-min"' . $shared . ' data-ka-meta-role="min" placeholder="' . esc_attr__('Min', 'king-addons') . '" />'; |
| 163 |
echo '<input type="number" class="ka-facet-meta__range-max"' . $shared . ' data-ka-meta-role="max" placeholder="' . esc_attr__('Max', 'king-addons') . '" />'; |
| 164 |
echo '</div>'; |
| 165 |
} |
| 166 |
echo '</div>'; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|