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

158 lines 4.0 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.0.10 Free
5 *
6 * @author FirePlugins <info@fireplugins.com>
7 * @link https://www.fireplugins.com
8 * @copyright Copyright © 2023 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 // filter the post content by setting supported block attributes
24 add_filter('render_block', [$this, 'modify_block_output'], 10, 2);
25 }
26
27 /**
28 * Filters the block output by appending out custom data attributes
29 * on our supported blocks
30 *
31 * @param string $block_content
32 * @param array $block
33 *
34 * @return string
35 */
36 public function modify_block_output($block_content, $block)
37 {
38 if (!isset($block['blockName']))
39 {
40 return $block_content;
41 }
42
43 $blockName = $block['blockName'];
44
45 $parsable_blocks = [
46 'firebox/buttons',
47 'core/buttons',
48 'core/image'
49 ];
50
51 if (!in_array($blockName, $parsable_blocks))
52 {
53 return $block_content;
54 }
55
56 /**
57 * Handle Core Buttons Block
58 */
59 if (in_array($blockName, ['core/buttons', 'firebox/buttons']))
60 {
61 $buttons = isset($block['innerBlocks']) ? $block['innerBlocks'] : [];
62 if (!$buttons)
63 {
64 return $block_content;
65 }
66
67 foreach ($buttons as $button)
68 {
69 $block_content = $this->replaceBlockAttributes('a', $button, $block_content);
70 }
71 }
72 /**
73 * Handle Core Image Block
74 */
75 else if ($blockName == 'core/image')
76 {
77 $new_block_html = $block['innerHTML'];
78
79 // check if we have an anchor element and add attributes to this elem.
80 $anchorExists = substr_count($new_block_html, '<a ');
81
82 $element = $anchorExists ? 'a' : 'figure';
83
84 $block_content = $this->replaceBlockAttributes($element, $block, $block_content);
85 }
86
87 return $block_content;
88 }
89
90 /**
91 * Adds the data attributes and class($atts) to the given element($element) within the block content($block_content)
92 *
93 * @param string $element
94 * @param array $block
95 * @param string $block_element
96 *
97 * @return string
98 */
99 private function replaceBlockAttributes($element, $block, $block_content)
100 {
101 $atts = $this->getBlockAttributes($block);
102 if (!$atts['enabled'])
103 {
104 return $block_content;
105 }
106
107 $new_block_html = $block['innerHTML'];
108
109 // box
110 if (!is_null($atts['box']) && $atts['box'] !== 'none')
111 {
112 if ($atts['box'] !== 'current')
113 {
114 $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox="' . esc_attr($atts['box']) . '" ', $new_block_html);
115 }
116
117 // cmd
118 if ($atts['cmd'])
119 {
120 $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-cmd="' . esc_attr($atts['cmd']) . '" ', $new_block_html);
121 }
122
123 // prevent default
124 if (isset($atts['preventDefault']))
125 {
126 $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-prevent="' . esc_attr((int) $atts['preventDefault']) . '" ', $new_block_html);
127 }
128 }
129
130 // class
131 if ($atts['class'])
132 {
133 $new_block_html = preg_replace('/<' . $element . '(.*)class="/', "<" . $element . "$1class=\"" . esc_attr($atts['class']) . ' ', $new_block_html);
134 }
135
136 return str_replace($block['innerHTML'], $new_block_html, $block_content);
137 }
138
139 /**
140 * Retrieves the block attributes
141 *
142 * @param array $block
143 *
144 * @return array
145 */
146 private function getBlockAttributes($block)
147 {
148 $atts = [];
149
150 $atts['enabled'] = isset($block['attrs']['dataFBoxEnabled']) ?: false;
151 $atts['box'] = isset($block['attrs']['dataFBox']) ? $block['attrs']['dataFBox'] : null;
152 $atts['cmd'] = isset($block['attrs']['dataFBoxCmd']) ? $block['attrs']['dataFBoxCmd'] : 'close';
153 $atts['class'] = isset($block['attrs']['dataFBoxClass']) ? $block['attrs']['dataFBoxClass'] : '';
154 $atts['preventDefault'] = isset($block['attrs']['dataFBoxPreventDefault']) ? $block['attrs']['dataFBoxPreventDefault'] : 1;
155
156 return $atts;
157 }
158 }