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