PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 3.1.14
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v3.1.14
3.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 All 123 releases
← All changes | Inc/Core/FB/BoxBlocksParser.php +99 -82 1.0.8 → 3.1.14 View file →
@@ -1,12 +1,12 @@
1 1 <?php
2 2 /**
3 3 * @package FireBox
4 - * @version 1.0.8 Free
4 + * @version 3.1.14 Free
5 5 *
6 6 * @author FirePlugins <[email protected]>
7 7 * @link https://www.fireplugins.com
8 - * @copyright Copyright © 2021 FirePlugins All Rights Reserved
8 + * @copyright Copyright © 2026 FirePlugins All Rights Reserved
9 9 * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later
10 10 */
11 11
12 12 namespace FireBox\Core\FB;
@@ -19,69 +19,63 @@
19 19 class BoxBlocksParser
20 20 {
21 21 public function __construct()
22 22 {
23 - // filter the post content by setting supported block attributes
24 - add_filter('render_block', [$this, 'modify_block_output'], 10, 2);
23 + add_filter('render_block', [$this, 'parse_block'], 10, 2);
25 24 }
26 25
27 26 /**
28 - * Filters the block output by appending out custom data attributes
29 - * on our supported blocks
30 - *
27 + * Single entry point for all render_block work on this class. Bails immediately
28 + * when the block carries none of our FireBox attributes, before any preg_ /
29 + * get_post_type() work runs for every block on the page.
30 + *
31 31 * @param string $block_content
32 32 * @param array $block
33 - *
33 + *
34 34 * @return string
35 35 */
36 - public function modify_block_output($block_content, $block)
36 + public function parse_block($block_content, $block)
37 37 {
38 - if (!isset($block['blockName']))
38 + if (empty($block['attrs']) || !$this->hasFireBoxAttrs($block['attrs']))
39 39 {
40 40 return $block_content;
41 41 }
42 42
43 - $blockName = $block['blockName'];
43 + $block_content = $this->maybe_load_block_extension_script($block_content, $block);
44 + $block_content = $this->modify_block_output($block_content, $block);
44 45
45 - $parsable_blocks = [
46 - 'core/buttons',
47 - 'core/image'
48 - ];
49 -
50 - if (!in_array($blockName, $parsable_blocks))
51 - {
52 - return $block_content;
53 - }
46 + return $block_content;
47 + }
54 48
55 - /**
56 - * Handle Core Buttons Block
57 - */
58 - if ($blockName == 'core/buttons')
59 - {
60 - $buttons = isset($block['innerBlocks']) ? $block['innerBlocks'] : [];
61 - if (!$buttons)
62 - {
63 - return $block_content;
64 - }
49 + /**
50 + * Whether the block's attributes contain any FireBox-specific key.
51 + *
52 + * @param array $attrs
53 + *
54 + * @return bool
55 + */
56 + private function hasFireBoxAttrs($attrs)
57 + {
58 + return isset($attrs['dataFBoxOnClick']) || isset($attrs['dataFBoxOnClickURLAddParameters']);
59 + }
65 60
66 - foreach ($buttons as $button)
67 - {
68 - $block_content = $this->replaceBlockAttributes('a', $button, $block_content);
69 - }
70 - }
71 - /**
72 - * Handle Core Image Block
73 - */
74 - else if ($blockName == 'core/image')
61 + public function maybe_load_block_extension_script($block_content, $block)
62 + {
63 + if (isset($block['attrs']['dataFBoxOnClick']) && in_array($block['attrs']['dataFBoxOnClick'], ['copy_clipboard', 'download_file']))
75 64 {
76 - $new_block_html = $block['innerHTML'];
77 -
78 - // check if we have an anchor element and add attributes to this elem.
79 - $anchorExists = substr_count($new_block_html, '<a ');
80 -
81 - $element = $anchorExists ? 'a' : 'figure';
82 -
83 - $block_content = $this->replaceBlockAttributes($element, $block, $block_content);
65 + wp_enqueue_style(
66 + 'firebox-block-extensions',
67 + FBOX_MEDIA_PUBLIC_URL . 'css/block_extensions.css',
68 + [],
69 + FBOX_VERSION
70 + );
71 + wp_enqueue_script(
72 + 'firebox-block-extensions',
73 + FBOX_MEDIA_PUBLIC_URL . 'js/block_extensions.js',
74 + [],
75 + FBOX_VERSION,
76 + true
77 + );
84 78 }
85 79
86 80 return $block_content;
87 81 }
@@ -86,53 +80,75 @@
86 80 return $block_content;
87 81 }
88 82
89 83 /**
90 - * Adds the data attributes and class($atts) to the given element($element) within the block content($block_content)
91 - *
92 - * @param string $element
84 + * Filters the block output by appending out custom data attributes
85 + * on our supported blocks
86 + *
87 + * @param string $block_content
93 88 * @param array $block
94 - * @param string $block_element
95 - *
89 + *
96 90 * @return string
97 91 */
98 - private function replaceBlockAttributes($element, $block, $block_content)
92 + public function modify_block_output($block_content, $block)
99 93 {
100 94 $atts = $this->getBlockAttributes($block);
101 - if (!$atts['enabled'])
95 + if (!$atts['utmParamsEnabled'])
102 96 {
103 97 return $block_content;
104 98 }
105 99
106 - $new_block_html = $block['innerHTML'];
107 -
108 - // box
109 - if (!is_null($atts['box']) && $atts['box'] !== 'none')
100 + if (!isset($block['blockName']))
110 101 {
111 - $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox="' . esc_attr($atts['box']) . '" ', $new_block_html);
102 + return $block_content;
103 + }
112 104
113 - // cmd
114 - if ($atts['cmd'])
115 - {
116 - $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-cmd="' . esc_attr($atts['cmd']) . '" ', $new_block_html);
117 - }
118 -
119 - // prevent default
120 - if ($atts['preventDefault'])
121 - {
122 - $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-prevent="' . esc_attr((int) $atts['preventDefault']) . '" ', $new_block_html);
123 - }
105 + $blockName = $block['blockName'];
106 +
107 + $parsable_blocks = [
108 + 'firebox/button',
109 + 'firebox/image',
110 + 'core/button',
111 + 'core/image'
112 + ];
113 +
114 + if (!in_array($blockName, $parsable_blocks))
115 + {
116 + return $block_content;
124 117 }
125 118
126 - // class
127 - if ($atts['class'])
119 + // Check if href attribute contains ? then prefix is & else prefix is ?, use regex
120 + $utmPrefix = preg_match('/href="([^"]*)\?/', $block_content) ? '&' : '?';
121 +
122 + // Find utm_content
123 + $utmContent = '';
124 + if ($blockName === 'core/button')
128 125 {
129 - $new_block_html = preg_replace('/<' . $element . '(.*)class="/', "<" . $element . "$1class=\"" . esc_attr($atts['class']) . ' ', $new_block_html);
126 + $utmContent = trim(wp_strip_all_tags($block['innerHTML']));
130 127 }
128 + else if ($blockName === 'firebox/button')
129 + {
130 + $utmContent = isset($block['attrs']['text']) ? trim(wp_strip_all_tags($block['attrs']['text'])) : '';
131 + }
132 + else if ($blockName === 'core/image')
133 + {
134 + // Get the src attribute value from $block['innerHTML'] using regex
135 + $utmContent = preg_match('/src="([^"]*)"/', $block['innerHTML'], $matches) ? $matches[1] : '';
136 + }
137 + else if ($blockName === 'firebox/image')
138 + {
139 + $utmContent = isset($block['attrs']['image']['desktop']['url']) ? $block['attrs']['image']['desktop']['url'] : '';
140 + }
141 +
142 + // Get the UTM Parameters
143 + $utmParams = $utmPrefix . 'utm_source=' . get_post_type() . '&utm_medium=' . $blockName . '&utm_campaign=' . get_the_title() . '&utm_content=' . $utmContent;
131 144
132 - return str_replace($block['innerHTML'], $new_block_html, $block_content);
145 + // Find the href attribute and append to it the utm params
146 + $block_content = preg_replace('/href="([^"]*)"/', 'href="$1' . $utmParams . '"', $block_content);
147 +
148 + return $block_content;
133 149 }
134 -
150 +
135 151 /**
136 152 * Retrieves the block attributes
137 153 *
138 154 * @param array $block
@@ -140,15 +156,16 @@
140 156 * @return array
141 157 */
142 158 private function getBlockAttributes($block)
143 159 {
144 - $atts = [];
145 -
146 - $atts['enabled'] = isset($block['attrs']['dataFBoxEnabled']) ?: false;
147 - $atts['box'] = isset($block['attrs']['dataFBox']) ? $block['attrs']['dataFBox'] : null;
148 - $atts['cmd'] = isset($block['attrs']['dataFBoxCmd']) ? $block['attrs']['dataFBoxCmd'] : 'close';
149 - $atts['class'] = isset($block['attrs']['dataFBoxClass']) ? $block['attrs']['dataFBoxClass'] : '';
150 - $atts['preventDefault'] = isset($block['attrs']['dataFBoxPreventDefault']) ? $block['attrs']['dataFBoxPreventDefault'] : 1;
160 + $atts = [
161 + 'utmParamsEnabled' => false
162 + ];
163 +
164 + if (isset($block['attrs']['dataFBoxOnClickURLAddParameters']) && $block['attrs']['dataFBoxOnClickURLAddParameters'])
165 + {
166 + $atts['utmParamsEnabled'] = true;
167 + }
151 168
152 169 return $atts;
153 170 }
154 171 }