PluginProbe
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment / 3.0.1
FireBox – WooCommerce Popup Builder, Exit Intent Popup, Email Optin & Cart Abandonment v3.0.1
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 3.0.1, at Inc/Core/FB/BoxBlocksParser.php

138 lines 3.4 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 3.0.1 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2025 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
28 public function maybe_load_block_extension_script($block_content, $block)
29 {
30 if (isset($block['attrs']['dataFBoxOnClick']) && in_array($block['attrs']['dataFBoxOnClick'], ['copy_clipboard', 'download_file']))
31 {
32 wp_enqueue_style(
33 'firebox-block-extensions',
34 FBOX_MEDIA_PUBLIC_URL . 'css/block_extensions.css',
35 [],
36 FBOX_VERSION
37 );
38 wp_enqueue_script(
39 'firebox-block-extensions',
40 FBOX_MEDIA_PUBLIC_URL . 'js/block_extensions.js',
41 [],
42 FBOX_VERSION,
43 true
44 );
45 }
46
47 return $block_content;
48 }
49
50 /**
51 * Filters the block output by appending out custom data attributes
52 * on our supported blocks
53 *
54 * @param string $block_content
55 * @param array $block
56 *
57 * @return string
58 */
59 public function modify_block_output($block_content, $block)
60 {
61 $atts = $this->getBlockAttributes($block);
62 if (!$atts['utmParamsEnabled'])
63 {
64 return $block_content;
65 }
66
67 if (!isset($block['blockName']))
68 {
69 return $block_content;
70 }
71
72 $blockName = $block['blockName'];
73
74 $parsable_blocks = [
75 'firebox/button',
76 'firebox/image',
77 'core/button',
78 'core/image'
79 ];
80
81 if (!in_array($blockName, $parsable_blocks))
82 {
83 return $block_content;
84 }
85
86 // Check if href attribute contains ? then prefix is & else prefix is ?, use regex
87 $utmPrefix = preg_match('/href="([^"]*)\?/', $block_content) ? '&' : '?';
88
89 // Find utm_content
90 $utmContent = '';
91 if ($blockName === 'core/button')
92 {
93 $utmContent = trim(wp_strip_all_tags($block['innerHTML']));
94 }
95 else if ($blockName === 'firebox/button')
96 {
97 $utmContent = isset($block['attrs']['text']) ? trim(wp_strip_all_tags($block['attrs']['text'])) : '';
98 }
99 else if ($blockName === 'core/image')
100 {
101 // Get the src attribute value from $block['innerHTML'] using regex
102 $utmContent = preg_match('/src="([^"]*)"/', $block['innerHTML'], $matches) ? $matches[1] : '';
103 }
104 else if ($blockName === 'firebox/image')
105 {
106 $utmContent = isset($block['attrs']['image']['desktop']['url']) ? $block['attrs']['image']['desktop']['url'] : '';
107 }
108
109 // Get the UTM Parameters
110 $utmParams = $utmPrefix . 'utm_source=' . get_post_type() . '&utm_medium=' . $blockName . '&utm_campaign=' . get_the_title() . '&utm_content=' . $utmContent;
111
112 // Find the href attribute and append to it the utm params
113 $block_content = preg_replace('/href="([^"]*)"/', 'href="$1' . $utmParams . '"', $block_content);
114
115 return $block_content;
116 }
117
118 /**
119 * Retrieves the block attributes
120 *
121 * @param array $block
122 *
123 * @return array
124 */
125 private function getBlockAttributes($block)
126 {
127 $atts = [
128 'utmParamsEnabled' => false
129 ];
130
131 if (isset($block['attrs']['dataFBoxOnClickURLAddParameters']) && $block['attrs']['dataFBoxOnClickURLAddParameters'])
132 {
133 $atts['utmParamsEnabled'] = true;
134 }
135
136 return $atts;
137 }
138 }