PluginProbe ʕ •ᴥ•ʔ
Zenchef widget integration / 1.3.0
Zenchef widget integration v1.3.0
1.3.0 1.0.1 1.2.0 trunk 1.0.0
zenchef-widget-integration / src / Widget / register_shortcode.php
zenchef-widget-integration / src / Widget Last commit date
Backoffice 2 months ago build_widget_data_attributes.php 2 months ago enqueue_widget_sdk.php 2 weeks ago get_widget_settings.php 2 months ago load_script_file.php 2 weeks ago load_script_template_file.php 2 weeks ago load_translations.php 2 years ago register_consent_api.php 2 weeks ago register_shortcode.php 2 weeks ago sanitize_restaurant_id.php 2 months ago sanitize_widget_settings.php 2 months ago
register_shortcode.php
93 lines
1 <?php
2
3 namespace Zenchef\Widget\Widget;
4
5 use function add_shortcode;
6 use function is_array;
7 use function ob_get_clean;
8 use function ob_start;
9 use function shortcode_atts;
10 use function Zenchef\Widget\View\render;
11
12 /**
13 * @return void
14 */
15 function register_widget_shortcode()
16 {
17 add_shortcode('zenchef_widget', __NAMESPACE__ . '\\render_widget_shortcode');
18 }
19
20 /**
21 * Tracks whether the shortcode rendered at least one widget on this request, so
22 * the global wp_footer injector can avoid emitting a duplicate widget on the page.
23 *
24 * @param bool|null $mark Pass true to mark as rendered; null to read current state.
25 * @return bool
26 */
27 function widget_shortcode_was_rendered($mark = null)
28 {
29 static $rendered = false;
30 if ($mark === true) {
31 $rendered = true;
32 }
33 return $rendered;
34 }
35
36 /**
37 * @param array|string $atts
38 * @return string
39 */
40 function render_widget_shortcode($atts)
41 {
42 $defaults = get_widget_settings();
43
44 $atts = shortcode_atts(
45 [
46 'restaurant_id' => $defaults['restaurant_id'],
47 'language' => $defaults['language'],
48 'primary_color' => $defaults['primary_color'],
49 'use_default_color' => $defaults['use_default_color'],
50 'position' => $defaults['position'],
51 'auto_open' => $defaults['auto_open'],
52 'open_delay' => $defaults['open_delay'],
53 'hide_button' => $defaults['hide_button'],
54 'disable_gtm' => $defaults['disable_gtm'],
55 'disable_ga4' => $defaults['disable_ga4'],
56 ],
57 is_array($atts) ? $atts : [],
58 'zenchef_widget'
59 );
60
61 $restaurant_id = sanitize_restaurant_id($atts['restaurant_id']);
62 if ($restaurant_id === null || $restaurant_id === '') {
63 return '';
64 }
65
66 if (!widget_should_load()) {
67 return '';
68 }
69
70 $settings = [
71 'restaurant_id' => $restaurant_id,
72 'language' => sanitize_language($atts['language']),
73 'primary_color' => sanitize_primary_color($atts['primary_color']),
74 'use_default_color' => sanitize_boolean($atts['use_default_color']),
75 'position' => sanitize_position($atts['position']),
76 'auto_open' => sanitize_boolean($atts['auto_open']),
77 'open_delay' => sanitize_open_delay($atts['open_delay']),
78 'hide_button' => sanitize_boolean($atts['hide_button']),
79 'disable_gtm' => sanitize_boolean($atts['disable_gtm']),
80 'disable_ga4' => sanitize_boolean($atts['disable_ga4']),
81 ];
82
83 widget_shortcode_was_rendered(true);
84 enqueue_widget_sdk();
85
86 ob_start();
87 render('main', [
88 'restaurant_id' => $restaurant_id,
89 'data_attributes' => build_widget_data_attributes($settings),
90 ]);
91 return (string) ob_get_clean();
92 }
93