PluginProbe
HubSpot All-In-One Marketing – Forms, Popups, Live Chat / 11.3.75
HubSpot All-In-One Marketing – Forms, Popups, Live Chat v11.3.75
11.3.75 11.3.73 11.3.71 11.3.70 11.3.69 11.3.64 11.3.65 11.3.62 11.3.61 11.3.56 11.3.58 11.0.31 11.0.52 11.0.54 11.0.56 11.0.58 11.0.7 11.1.10 11.1.11 11.1.13 11.1.14 11.1.15 11.1.2 11.1.20 11.1.21 All 73 releases
leadin / public / utils / class-shortcoderenderutils.php

class-shortcoderenderutils.php in HubSpot All-In-One Marketing – Forms, Popups, Live Chat 11.3.75, at public/utils/class-shortcoderenderutils.php

205 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Leadin\utils;
4
5 use Leadin\data\Filters;
6 use Leadin\AssetsManager;
7
8 /**
9 * Util Class responsible for rendering shortcodes.
10 */
11 class ShortcodeRenderUtils {
12
13 /**
14 * Render leadin shortcodes output
15 *
16 * @param array $shortcode_attrs Shortcode attributes.
17 */
18 public static function render_shortcode( $shortcode_attrs ) {
19 $parsed_attributes = shortcode_atts( array( 'type' => null ), $shortcode_attrs );
20
21 $type = $parsed_attributes['type'];
22 if ( ! isset( $type ) ) {
23 return;
24 }
25
26 switch ( $type ) {
27 case 'form':
28 return self::render_form( $shortcode_attrs );
29 case 'cta':
30 return self::render_cta( $shortcode_attrs );
31 case 'meeting':
32 return self::render_meeting( $shortcode_attrs );
33 }
34 }
35
36 /**
37 * Render form leadin shortcodes
38 *
39 * @param array $attrs Shortcode attributes.
40 */
41 private static function render_form( $attrs ) {
42 $parsed_attributes = shortcode_atts(
43 array(
44 'portal' => null,
45 'id' => null,
46 'version' => null,
47 ),
48 $attrs
49 );
50
51 $portal_id = $parsed_attributes['portal'];
52 $id = $parsed_attributes['id'];
53 $version = $parsed_attributes['version'];
54 if ( ! isset( $portal_id ) || ! isset( $id ) || ! is_numeric( $portal_id ) ||
55 ! ( self::is_valid_uuid( $id ) || is_numeric( $id ) ) ) {
56 return;
57 }
58
59 $form_div_uuid = self::generate_div_uuid();
60 $hublet = Filters::apply_hublet_filters();
61
62 if ( isset( $version ) && 'v4' === $version ) {
63 AssetsManager::enqueue_form_v4_script( $portal_id );
64 return '
65 <div
66 class="hs-form-frame"
67 data-region="' . $hublet . '"
68 data-form-id="' . $id . '"
69 data-portal-id="' . $portal_id . '"
70 ' . Filters::apply_forms_v4_payload_filters() . ' >
71 </div>
72 ';
73 } else {
74 AssetsManager::enqueue_forms_script();
75 return '
76 <script>
77 window.hsFormsOnReady = window.hsFormsOnReady || [];
78 window.hsFormsOnReady.push(()=>{
79 hbspt.forms.create({
80 portalId: ' . $portal_id . ',
81 formId: "' . $id . '",
82 target: "#hbspt-form-' . $form_div_uuid . '",
83 region: "' . $hublet . '",
84 ' . Filters::apply_forms_payload_filters() . '
85 })});
86 </script>
87 <div class="hbspt-form" id="hbspt-form-' . $form_div_uuid . '"></div>';
88 }
89 }
90
91 /**
92 * Render cta leadin shortcodes
93 *
94 * @param array $attrs Shortcode attributes.
95 */
96 private static function render_cta( $attrs ) {
97 $parsed_attributes = shortcode_atts(
98 array(
99 'portal' => null,
100 'id' => null,
101 ),
102 $attrs
103 );
104
105 $portal_id = $parsed_attributes['portal'];
106 $id = $parsed_attributes['id'];
107
108 if ( ! isset( $portal_id ) || ! isset( $id ) || ! is_numeric( $portal_id ) ||
109 ! ( self::is_valid_uuid( $id ) || is_numeric( $id ) ) ) {
110 return;
111 }
112
113 return '
114 <!--HubSpot Call-to-Action Code -->
115 <span class="hs-cta-wrapper" id="hs-cta-wrapper-' . $id . '">
116 <span class="hs-cta-node hs-cta-' . $id . '" id="' . $id . '">
117 <!--[if lte IE 8]>
118 <div id="hs-cta-ie-element"></div>
119 <![endif]-->
120 <a href="https://cta-redirect.hubspot.com/cta/redirect/' . $portal_id . '/' . $id . '" >
121 <img class="hs-cta-img" id="hs-cta-img-' . $id . '" style="border-width:0px;" src="https://no-cache.hubspot.com/cta/default/' . $portal_id . '/' . $id . '.png" alt="New call-to-action"/>
122 </a>
123 </span>
124 <' . 'script charset="utf-8" src="//js.hubspot.com/cta/current.js"></script>
125 <script>
126 hbspt.cta.load(' . $portal_id . ', \'' . $id . '\', {});
127 </script>
128 </span>
129 <!-- end HubSpot Call-to-Action Code -->
130 ';
131 }
132
133 /**
134 * Render meeting leadin shortcodes
135 *
136 * @param array $attrs Shortcode attributes.
137 */
138 private static function render_meeting( $attrs ) {
139 $parsed_attributes = shortcode_atts(
140 array(
141 'url' => null,
142 ),
143 $attrs
144 );
145
146 $url = esc_url_raw( $parsed_attributes['url'] );
147
148 if ( filter_var( $url, FILTER_VALIDATE_URL ) === false ) {
149 return;
150 }
151
152 AssetsManager::enqueue_meetings_script();
153
154 return '
155 <div class="meetings-iframe-container" data-src="' . $url . '?embed=true"></div>
156 <script>
157 if(window.hbspt && window.hbspt.meetings){
158 window.hbspt.meetings.create(".meetings-iframe-container");
159 }
160 </script>
161 ';
162 }
163
164 /**
165 * Wraps an embed in a pointer-events:none container for the Elementor editor,
166 * so the iframe cannot swallow the click Elementor needs to select the widget.
167 *
168 * @param string $embed The embed HTML to wrap.
169 */
170 public static function wrap_embed_for_elementor_editor( $embed ) {
171 return '<div class="hubspot-embed-editor-preview" style="pointer-events:none;">' . $embed . '</div>';
172 }
173
174 /**
175 * Generates 10 characters long string with random values
176 */
177 private static function get_random_number_string() {
178 $result = '';
179 for ( $i = 0; $i < 10; $i++ ) {
180 $result .= wp_rand( 0, 9 );
181 }
182 return $result;
183 }
184
185 /**
186 * Generates a unique uuid
187 */
188 private static function generate_div_uuid() {
189 return time() * 1000 . '-' . self::get_random_number_string();
190 }
191
192 /**
193 * Checks if input is a valid uuid.
194 *
195 * @param String $uuid input to validate.
196 */
197 private static function is_valid_uuid( $uuid ) {
198 if ( ! is_string( $uuid ) || ( preg_match( '/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i', $uuid ) !== 1 ) ) {
199 return false;
200 }
201 return true;
202 }
203
204 }
205