PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 2.1.14
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v2.1.14
3.1.13 3.1.12 3.1.11 3.1.10 3.1.9 3.1.8 3.1.7 trunk 1.0.0 1.0.1 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 All 122 releases
firebox / Inc / Core / FB / BoxBlocksParser.php

BoxBlocksParser.php in FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment 2.1.14, at Inc/Core/FB/BoxBlocksParser.php

270 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package FireBox
4 * @version 2.1.14 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2024 FirePlugins All Rights Reserved
9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 */
11
12 namespace FireBox\Core\FB;
13
14 if (!defined('ABSPATH'))
15 {
16 exit; // Exit if accessed directly.
17 }
18
19 class BoxBlocksParser
20 {
21 public function __construct()
22 {
23 add_filter('render_block', [$this, 'maybe_load_block_extension_script'], 10, 2);
24
25 add_filter('render_block', [$this, 'modify_block_output'], 10, 2);
26
27 // DEPRECATED-START - Remove on 2024-01-01
28 // filter the post content by setting supported block attributes
29 add_filter('render_block', [$this, 'deprecated_modify_block_output'], 10, 2);
30 // DEPRECATED-END
31 }
32
33 public function maybe_load_block_extension_script($block_content, $block)
34 {
35 if (isset($block['attrs']['dataFBoxOnClick']) && in_array($block['attrs']['dataFBoxOnClick'], ['copy_clipboard', 'download_file']))
36 {
37 wp_enqueue_style(
38 'firebox-block-extensions',
39 FBOX_MEDIA_PUBLIC_URL . 'css/block_extensions.css',
40 [],
41 FBOX_VERSION
42 );
43 wp_enqueue_script(
44 'firebox-block-extensions',
45 FBOX_MEDIA_PUBLIC_URL . 'js/block_extensions.js',
46 [],
47 FBOX_VERSION,
48 true
49 );
50 }
51
52 return $block_content;
53 }
54
55 /**
56 * Filters the block output by appending out custom data attributes
57 * on our supported blocks
58 *
59 * @param string $block_content
60 * @param array $block
61 *
62 * @return string
63 */
64 public function modify_block_output($block_content, $block)
65 {
66 $atts = $this->getBlockAttributes($block);
67 if (!$atts['utmParamsEnabled'])
68 {
69 return $block_content;
70 }
71
72 if (!isset($block['blockName']))
73 {
74 return $block_content;
75 }
76
77 $blockName = $block['blockName'];
78
79 $parsable_blocks = [
80 'firebox/button',
81 'firebox/image',
82 'core/button',
83 'core/image'
84 ];
85
86 if (!in_array($blockName, $parsable_blocks))
87 {
88 return $block_content;
89 }
90
91 // Check if href attribute contains ? then prefix is & else prefix is ?, use regex
92 $utmPrefix = preg_match('/href="([^"]*)\?/', $block_content) ? '&' : '?';
93
94 // Find utm_content
95 $utmContent = '';
96 if ($blockName === 'core/button')
97 {
98 $utmContent = trim(wp_strip_all_tags($block['innerHTML']));
99 }
100 else if ($blockName === 'firebox/button')
101 {
102 $utmContent = isset($block['attrs']['text']) ? trim(wp_strip_all_tags($block['attrs']['text'])) : '';
103 }
104 else if ($blockName === 'core/image')
105 {
106 // Get the src attribute value from $block['innerHTML'] using regex
107 $utmContent = preg_match('/src="([^"]*)"/', $block['innerHTML'], $matches) ? $matches[1] : '';
108 }
109 else if ($blockName === 'firebox/image')
110 {
111 $utmContent = isset($block['attrs']['image']['desktop']['url']) ? $block['attrs']['image']['desktop']['url'] : '';
112 }
113
114 // Get the UTM Parameters
115 $utmParams = $utmPrefix . 'utm_source=' . get_post_type() . '&utm_medium=' . $blockName . '&utm_campaign=' . get_the_title() . '&utm_content=' . $utmContent;
116
117 // Find the href attribute and append to it the utm params
118 $block_content = preg_replace('/href="([^"]*)"/', 'href="$1' . $utmParams . '"', $block_content);
119
120 return $block_content;
121 }
122
123 // DEPRECATED-START - Remove on 2025-01-01
124 /**
125 * Filters the block output by appending out custom data attributes
126 * on our supported blocks
127 *
128 * @param string $block_content
129 * @param array $block
130 *
131 * @return string
132 */
133 public function deprecated_modify_block_output($block_content, $block)
134 {
135 if (!isset($block['blockName']))
136 {
137 return $block_content;
138 }
139
140 $blockName = $block['blockName'];
141
142 $parsable_blocks = [
143 'firebox/buttons',
144 'firebox/image',
145 'core/buttons',
146 'core/image'
147 ];
148
149 if (!in_array($blockName, $parsable_blocks))
150 {
151 return $block_content;
152 }
153
154 /**
155 * Handle Core Buttons Block
156 */
157 if (in_array($blockName, ['core/buttons', 'firebox/buttons']))
158 {
159 $buttons = isset($block['innerBlocks']) ? $block['innerBlocks'] : [];
160 if (!$buttons)
161 {
162 return $block_content;
163 }
164
165 foreach ($buttons as $button)
166 {
167 $block_content = $this->replaceBlockAttributes('a', $button, $block_content);
168 }
169 }
170 /**
171 * Handle Core Image Block
172 */
173 else if (in_array($blockName, ['core/image', 'firebox/image']))
174 {
175 $new_block_html = $block['innerHTML'];
176
177 // check if we have an anchor element and add attributes to this elem.
178 $anchorExists = substr_count($new_block_html, '<a ');
179
180 $element = $anchorExists ? 'a' : 'figure';
181
182 $block_content = $this->replaceBlockAttributes($element, $block, $block_content);
183 }
184
185 return $block_content;
186 }
187
188 /**
189 * Adds the data attributes and class($atts) to the given element($element) within the block content($block_content)
190 *
191 * @param string $element
192 * @param array $block
193 * @param string $block_element
194 *
195 * @return string
196 */
197 private function replaceBlockAttributes($element, $block, $block_content)
198 {
199 $atts = $this->getBlockAttributes($block);
200
201 if (!$atts['enabled'])
202 {
203 return $block_content;
204 }
205
206 $new_block_html = $block['innerHTML'];
207
208 // box
209 if (!is_null($atts['box']) && $atts['box'] !== 'none')
210 {
211 if ($atts['box'] !== 'current')
212 {
213 $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox="' . esc_attr($atts['box']) . '" ', $new_block_html);
214 }
215
216 // cmd
217 if ($atts['cmd'])
218 {
219 $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-cmd="' . esc_attr($atts['cmd']) . '" ', $new_block_html);
220 }
221
222 // prevent default
223 $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-prevent="1" ', $new_block_html);
224 }
225
226 // class
227 if (isset($atts['class']) && !empty($atts['class']))
228 {
229 $new_block_html = preg_replace('/<' . $element . '(.*)class="/', "<" . $element . "$1class=\"" . esc_attr($atts['class']) . ' ', $new_block_html);
230 }
231
232 return str_replace($block['innerHTML'], $new_block_html, $block_content);
233 }
234 // DEPRECATED-END
235
236 /**
237 * Retrieves the block attributes
238 *
239 * @param array $block
240 *
241 * @return array
242 */
243 private function getBlockAttributes($block)
244 {
245 $atts = [
246 'utmParamsEnabled' => false
247 ];
248
249 if (isset($block['attrs']['dataFBoxOnClickURLAddParameters']) && $block['attrs']['dataFBoxOnClickURLAddParameters'])
250 {
251 $atts['utmParamsEnabled'] = true;
252 }
253
254 // DEPRECATED-START
255 // FireBox settings
256 $atts['enabled'] = isset($block['attrs']['dataFBoxEnabled']) && $block['attrs']['dataFBoxEnabled'];
257
258 if (!$atts['enabled'])
259 {
260 return $atts;
261 }
262
263 $atts['box'] = isset($block['attrs']['dataFBox']) ? $block['attrs']['dataFBox'] : 'current';
264 $atts['cmd'] = isset($block['attrs']['dataFBoxCmd']) ? $block['attrs']['dataFBoxCmd'] : 'open';
265 $atts['class'] = isset($block['attrs']['dataFBoxClass']) ? $block['attrs']['dataFBoxClass'] : '';
266 // DEPRECATED-END
267
268 return $atts;
269 }
270 }