PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.49
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.49
51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 51.1.46 51.1.47 51.1.49 All 37 releases
king-addons / includes / widgets / TB_Post_Navigation / TB_Post_Navigation.php

TB_Post_Navigation.php in King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder 51.1.49, at includes/widgets/TB_Post_Navigation/TB_Post_Navigation.php

389 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Theme Builder Post Navigation widget (Free).
4 *
5 * @package King_Addons
6 */
7
8 namespace King_Addons;
9
10 use Elementor\Controls_Manager;
11 use Elementor\Group_Control_Typography;
12 use Elementor\Widget_Base;
13
14 if (!defined('ABSPATH')) {
15 exit;
16 }
17
18 /**
19 * Shows previous/next post navigation.
20 */
21 class TB_Post_Navigation extends Widget_Base
22 {
23 /**
24 * Widget slug.
25 *
26 * @return string
27 */
28 public function get_name(): string
29 {
30 return 'king-addons-tb-post-navigation';
31 }
32
33 /**
34 * Widget title.
35 *
36 * @return string
37 */
38 public function get_title(): string
39 {
40 return esc_html__('TB - Post Navigation', 'king-addons');
41 }
42
43 /**
44 * Widget icon.
45 *
46 * @return string
47 */
48 public function get_icon(): string
49 {
50 return 'eicon-arrow-right';
51 }
52
53 /**
54 * Style dependencies.
55 *
56 * @return array<int, string>
57 */
58 public function get_style_depends(): array
59 {
60 return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-post-navigation-style'];
61 }
62
63 /**
64 * Script dependencies.
65 *
66 * @return array<int, string>
67 */
68 public function get_script_depends(): array
69 {
70 return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-tb-post-navigation-script'];
71 }
72
73 /**
74 * Categories.
75 *
76 * @return array<int, string>
77 */
78 public function get_categories(): array
79 {
80 return ['king-addons'];
81 }
82
83 /**
84 * Keywords.
85 *
86 * @return array<int, string>
87 */
88 public function get_keywords(): array
89 {
90 return ['navigation', 'previous', 'next', 'post', 'king-addons'];
91 }
92
93 /**
94 * Register controls.
95 *
96 * @return void
97 */
98 public function register_controls(): void
99 {
100 $this->register_content_controls(false);
101 $this->register_style_controls(false);
102 $this->register_pro_notice_controls();
103 }
104
105 /**
106 * Render output.
107 *
108 * @return void
109 */
110 public function render(): void
111 {
112 $settings = $this->get_settings_for_display();
113 $this->render_output($settings, false);
114 }
115
116 /**
117 * Content controls.
118 *
119 * @param bool $is_pro Whether Pro controls are enabled.
120 *
121 * @return void
122 */
123 protected function register_content_controls(bool $is_pro): void
124 {
125 $this->start_controls_section(
126 'kng_content_section',
127 [
128 'label' => KING_ADDONS_ELEMENTOR_ICON . esc_html__('Content', 'king-addons'),
129 'tab' => Controls_Manager::TAB_CONTENT,
130 ]
131 );
132
133 $this->add_control(
134 'kng_show',
135 [
136 'label' => esc_html__('Show', 'king-addons'),
137 'type' => Controls_Manager::SELECT,
138 'default' => 'both',
139 'options' => [
140 'previous' => esc_html__('Previous Only', 'king-addons'),
141 'next' => esc_html__('Next Only', 'king-addons'),
142 'both' => esc_html__('Both', 'king-addons'),
143 ],
144 ]
145 );
146
147 $this->add_control(
148 'kng_prev_label',
149 [
150 'label' => esc_html__('Previous Label', 'king-addons'),
151 'type' => Controls_Manager::TEXT,
152 'default' => esc_html__('Previous', 'king-addons'),
153 ]
154 );
155
156 $this->add_control(
157 'kng_next_label',
158 [
159 'label' => esc_html__('Next Label', 'king-addons'),
160 'type' => Controls_Manager::TEXT,
161 'default' => esc_html__('Next', 'king-addons'),
162 ]
163 );
164
165 $this->add_control(
166 'kng_show_titles',
167 [
168 'label' => esc_html__('Show Post Titles', 'king-addons'),
169 'type' => Controls_Manager::SWITCHER,
170 'return_value' => 'yes',
171 'default' => 'yes',
172 ]
173 );
174
175 $this->add_control(
176 'kng_same_tax',
177 [
178 'label' => $is_pro ?
179 esc_html__('Restrict to Same Category', 'king-addons') :
180 sprintf(__('Restrict to Same Category %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
181 'type' => Controls_Manager::SWITCHER,
182 'return_value' => 'yes',
183 'default' => '',
184 'classes' => $is_pro ? '' : 'king-addons-pro-control',
185 ]
186 );
187
188 $this->add_control(
189 'kng_layout',
190 [
191 'label' => esc_html__('Layout', 'king-addons'),
192 'type' => Controls_Manager::SELECT,
193 'default' => 'inline',
194 'options' => [
195 'inline' => esc_html__('Inline', 'king-addons'),
196 'columns' => esc_html__('Two Columns', 'king-addons'),
197 'stacked' => esc_html__('Stacked', 'king-addons'),
198 ],
199 ]
200 );
201
202 $this->end_controls_section();
203 }
204
205 /**
206 * Style controls.
207 *
208 * @param bool $is_pro Whether Pro controls are enabled.
209 *
210 * @return void
211 */
212 protected function register_style_controls(bool $is_pro): void
213 {
214 $this->start_controls_section(
215 'kng_style_section',
216 [
217 'label' => esc_html__('Style', 'king-addons'),
218 'tab' => Controls_Manager::TAB_STYLE,
219 ]
220 );
221
222 $this->add_group_control(
223 Group_Control_Typography::get_type(),
224 [
225 'name' => 'kng_label_typography',
226 'label' => esc_html__('Label Typography', 'king-addons'),
227 'selector' => '{{WRAPPER}} .king-addons-tb-post-navigation__label',
228 ]
229 );
230
231 $this->add_group_control(
232 Group_Control_Typography::get_type(),
233 [
234 'name' => 'kng_title_typography',
235 'label' => esc_html__('Title Typography', 'king-addons'),
236 'selector' => '{{WRAPPER}} .king-addons-tb-post-navigation__title',
237 ]
238 );
239
240 $this->add_control(
241 'kng_text_color',
242 [
243 'label' => esc_html__('Text Color', 'king-addons'),
244 'type' => Controls_Manager::COLOR,
245 'selectors' => [
246 '{{WRAPPER}} .king-addons-tb-post-navigation' => 'color: {{VALUE}};',
247 '{{WRAPPER}} .king-addons-tb-post-navigation a' => 'color: {{VALUE}};',
248 ],
249 ]
250 );
251
252 $this->add_control(
253 'kng_text_color_hover',
254 [
255 'label' => esc_html__('Hover Color', 'king-addons'),
256 'type' => Controls_Manager::COLOR,
257 'selectors' => [
258 '{{WRAPPER}} .king-addons-tb-post-navigation a:hover' => 'color: {{VALUE}};',
259 ],
260 ]
261 );
262
263 $this->add_responsive_control(
264 'kng_gap',
265 [
266 'label' => esc_html__('Gap', 'king-addons'),
267 'type' => Controls_Manager::SLIDER,
268 'size_units' => ['px'],
269 'range' => [
270 'px' => ['min' => 0, 'max' => 60],
271 ],
272 'default' => [
273 'size' => 12,
274 'unit' => 'px',
275 ],
276 'selectors' => [
277 '{{WRAPPER}} .king-addons-tb-post-navigation' => 'gap: {{SIZE}}{{UNIT}};',
278 ],
279 ]
280 );
281
282 $this->end_controls_section();
283 }
284
285 /**
286 * Pro upsell.
287 *
288 * @return void
289 */
290 protected function register_pro_notice_controls(): void
291 {
292 if (king_addons_freemius()->can_use_premium_code__premium_only()) {
293 return;
294 }
295
296 Core::renderProFeaturesSection(
297 $this,
298 '',
299 Controls_Manager::RAW_HTML,
300 'tb-post-navigation',
301 [
302 'Restrict navigation to same category',
303 'Arrow icons and advanced layout skins',
304 ]
305 );
306 }
307
308 /**
309 * Render output helper.
310 *
311 * @param array<string, mixed> $settings Widget settings.
312 * @param bool $is_pro Whether Pro is enabled.
313 *
314 * @return void
315 */
316 protected function render_output(array $settings, bool $is_pro): void
317 {
318 $post = get_post();
319 if (!$post instanceof \WP_Post) {
320 return;
321 }
322
323 $same = $is_pro && ('yes' === ($settings['kng_same_tax'] ?? '')) ? true : false;
324 $previous = get_previous_post($same);
325 $next = get_next_post($same);
326
327 $show = $settings['kng_show'] ?? 'both';
328 $items = [];
329
330 if (('previous' === $show || 'both' === $show) && $previous instanceof \WP_Post) {
331 $items[] = $this->build_item(
332 $settings['kng_prev_label'] ?? '',
333 $previous,
334 'prev',
335 $settings
336 );
337 }
338
339 if (('next' === $show || 'both' === $show) && $next instanceof \WP_Post) {
340 $items[] = $this->build_item(
341 $settings['kng_next_label'] ?? '',
342 $next,
343 'next',
344 $settings
345 );
346 }
347
348 if (empty($items)) {
349 return;
350 }
351
352 $layout = $settings['kng_layout'] ?? 'inline';
353 $classes = ['king-addons-tb-post-navigation', 'king-addons-tb-post-navigation--' . $layout];
354
355 echo '<div class="' . esc_attr(implode(' ', $classes)) . '">';
356 foreach ($items as $item) {
357 echo $item; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
358 }
359 echo '</div>';
360 }
361
362 /**
363 * Build item markup.
364 *
365 * @param string $label Label text.
366 * @param \WP_Post $post Target post.
367 * @param string $type Item type prev/next.
368 * @param array $settings Settings.
369 *
370 * @return string
371 */
372 protected function build_item(string $label, \WP_Post $post, string $type, array $settings): string
373 {
374 $title = ('yes' === ($settings['kng_show_titles'] ?? 'yes')) ? get_the_title($post) : '';
375
376 $html = '<div class="king-addons-tb-post-navigation__item king-addons-tb-post-navigation__item--' . esc_attr($type) . '">';
377 $html .= '<a href="' . esc_url(get_permalink($post)) . '">';
378 if ($label) {
379 $html .= '<span class="king-addons-tb-post-navigation__label">' . esc_html($label) . '</span>';
380 }
381 if ($title) {
382 $html .= '<span class="king-addons-tb-post-navigation__title">' . esc_html($title) . '</span>';
383 }
384 $html .= '</a></div>';
385
386 return $html;
387 }
388 }
389