-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelementor-kb-section-widget.php
More file actions
211 lines (186 loc) · 6.92 KB
/
elementor-kb-section-widget.php
File metadata and controls
211 lines (186 loc) · 6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Elementor KB Section Widget
*
* Provides an Elementor container widget for designating KB sections.
*/
namespace SmartCloud\WPSuite\AiKit;
if (!defined('ABSPATH')) {
exit;
}
/**
* Register Elementor KB Section Widget
*/
class ElementorKBSectionWidget
{
/**
* Initialize the widget registration
*/
public static function init(): void
{
// Only register if Elementor is active
if (!did_action('elementor/loaded')) {
return;
}
// Add KB Section controls to existing Section/Container elements
add_action('elementor/element/section/section_advanced/after_section_end', [__CLASS__, 'add_kb_controls'], 10, 2);
add_action('elementor/element/container/section_layout/after_section_end', [__CLASS__, 'add_kb_controls'], 10, 2);
// Add frontend rendering
add_action('elementor/frontend/section/before_render', [__CLASS__, 'before_render_section']);
add_action('elementor/frontend/container/before_render', [__CLASS__, 'before_render_section']);
}
/**
* Add KB Section controls to Section/Container elements
*/
public static function add_kb_controls($element, $args): void
{
$element->start_controls_section(
'kb_section_settings',
[
'label' => __('KB Section', 'smartcloud-ai-kit'),
'tab' => \Elementor\Controls_Manager::TAB_ADVANCED,
]
);
$element->add_control(
'kb_enabled',
[
'label' => __('Enable KB Section', 'smartcloud-ai-kit'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'default' => '',
'description' => __('Mark this section as a Knowledge Base section', 'smartcloud-ai-kit'),
]
);
$element->add_control(
'kb_mode',
[
'label' => __('Mode', 'smartcloud-ai-kit'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'inherit',
'options' => [
'inherit' => __('Inherit (part of base doc)', 'smartcloud-ai-kit'),
'separate_doc' => __('Separate Document', 'smartcloud-ai-kit'),
// phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_exclude -- This is an Elementor control option value, not a query parameter
'exclude' => __('Exclude from KB', 'smartcloud-ai-kit'),
],
'condition' => [
'kb_enabled' => 'yes',
],
]
);
$element->add_control(
'kb_doc_key',
[
'label' => __('Document Key', 'smartcloud-ai-kit'),
'type' => \Elementor\Controls_Manager::TEXT,
'placeholder' => __('e.g., pricing, faq', 'smartcloud-ai-kit'),
'description' => __('Unique identifier for this document', 'smartcloud-ai-kit'),
'condition' => [
'kb_enabled' => 'yes',
'kb_mode' => 'separate_doc',
],
]
);
$element->add_control(
'kb_doc_title',
[
'label' => __('Document Title', 'smartcloud-ai-kit'),
'type' => \Elementor\Controls_Manager::TEXT,
'placeholder' => __('e.g., Pricing Information', 'smartcloud-ai-kit'),
'description' => __('Title for this separate document', 'smartcloud-ai-kit'),
'condition' => [
'kb_enabled' => 'yes',
'kb_mode' => 'separate_doc',
],
]
);
$element->add_control(
'kb_section_key',
[
'label' => __('Section Key', 'smartcloud-ai-kit'),
'type' => \Elementor\Controls_Manager::TEXT,
'placeholder' => __('Optional custom identifier', 'smartcloud-ai-kit'),
'description' => __('Leave empty to auto-generate', 'smartcloud-ai-kit'),
'condition' => [
'kb_enabled' => 'yes',
],
]
);
$element->add_control(
'kb_title',
[
'label' => __('Title Override', 'smartcloud-ai-kit'),
'type' => \Elementor\Controls_Manager::TEXT,
'condition' => [
'kb_enabled' => 'yes',
'kb_mode!' => 'separate_doc',
],
]
);
$element->add_control(
'kb_category',
[
'label' => __('Category', 'smartcloud-ai-kit'),
'type' => \Elementor\Controls_Manager::TEXT,
'condition' => [
'kb_enabled' => 'yes',
],
]
);
$element->add_control(
'kb_subcategory',
[
'label' => __('Subcategory', 'smartcloud-ai-kit'),
'type' => \Elementor\Controls_Manager::TEXT,
'condition' => [
'kb_enabled' => 'yes',
],
]
);
$element->add_control(
'kb_tags',
[
'label' => __('Tags', 'smartcloud-ai-kit'),
'type' => \Elementor\Controls_Manager::TEXTAREA,
'placeholder' => __('tag1, tag2, tag3', 'smartcloud-ai-kit'),
'condition' => [
'kb_enabled' => 'yes',
],
]
);
$element->add_control(
'kb_priority',
[
'label' => __('Priority', 'smartcloud-ai-kit'),
'type' => \Elementor\Controls_Manager::NUMBER,
'condition' => [
'kb_enabled' => 'yes',
],
]
);
$element->end_controls_section();
}
/**
* Add data attributes before rendering
*/
public static function before_render_section($element): void
{
$settings = $element->get_settings();
if (empty($settings['kb_enabled']) || $settings['kb_enabled'] !== 'yes') {
return;
}
$mode = $settings['kb_mode'] ?? 'inherit';
$element->add_render_attribute('_wrapper', 'class', 'wpsaik-kb-section');
$element->add_render_attribute('_wrapper', 'data-mode', $mode);
if (!empty($settings['kb_doc_key'])) {
$element->add_render_attribute('_wrapper', 'data-doc-key', $settings['kb_doc_key']);
}
if (!empty($settings['kb_section_key'])) {
$element->add_render_attribute('_wrapper', 'data-section-key', $settings['kb_section_key']);
}
if (!empty($settings['kb_doc_title'])) {
$element->add_render_attribute('_wrapper', 'data-doc-title', $settings['kb_doc_title']);
}
}
}
// Initialize
ElementorKBSectionWidget::init();