| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 3.1.13 Free | |
| 4 | + * @version 2.1.14 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <info@fireplugins.com> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2026 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2024 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,46 +19,18 @@ | ||
| 19 | 19 | class BoxBlocksParser |
| 20 | 20 | { |
| 21 | 21 | public function __construct() |
| 22 | 22 | { |
| 23 | - add_filter('render_block', [$this, 'parse_block'], 10, 2); | |
| 24 | - } | |
| 23 | + add_filter('render_block', [$this, 'maybe_load_block_extension_script'], 10, 2); | |
| 25 | 24 | |
| 26 | - /** | |
| 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 | - * @param string $block_content | |
| 32 | - * @param array $block | |
| 33 | - * | |
| 34 | - * @return string | |
| 35 | - */ | |
| 36 | - public function parse_block($block_content, $block) | |
| 37 | - { | |
| 38 | - if (empty($block['attrs']) || !$this->hasFireBoxAttrs($block['attrs'])) | |
| 39 | - { | |
| 40 | - return $block_content; | |
| 41 | - } | |
| 25 | + add_filter('render_block', [$this, 'modify_block_output'], 10, 2); | |
| 42 | 26 | |
| 43 | - $block_content = $this->maybe_load_block_extension_script($block_content, $block); | |
| 44 | - $block_content = $this->modify_block_output($block_content, $block); | |
| 45 | - | |
| 46 | - return $block_content; | |
| 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 | |
| 47 | 31 | } |
| 48 | 32 | |
| 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 | - } | |
| 60 | - | |
| 61 | 33 | public function maybe_load_block_extension_script($block_content, $block) |
| 62 | 34 | { |
| 63 | 35 | if (isset($block['attrs']['dataFBoxOnClick']) && in_array($block['attrs']['dataFBoxOnClick'], ['copy_clipboard', 'download_file'])) |
| 64 | 36 | { |
| @@ -82,12 +54,12 @@ | ||
| 82 | 54 | |
| 83 | 55 | /** |
| 84 | 56 | * Filters the block output by appending out custom data attributes |
| 85 | 57 | * on our supported blocks |
| 86 | - * | |
| 58 | + * | |
| 87 | 59 | * @param string $block_content |
| 88 | 60 | * @param array $block |
| 89 | - * | |
| 61 | + * | |
| 90 | 62 | * @return string |
| 91 | 63 | */ |
| 92 | 64 | public function modify_block_output($block_content, $block) |
| 93 | 65 | { |
| @@ -147,9 +119,122 @@ | ||
| 147 | 119 | |
| 148 | 120 | return $block_content; |
| 149 | 121 | } |
| 150 | 122 | |
| 123 | + // DEPRECATED-START - Remove on 2025-01-01 | |
| 151 | 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 | + /** | |
| 152 | 237 | * Retrieves the block attributes |
| 153 | 238 | * |
| 154 | 239 | * @param array $block |
| 155 | 240 | * |
| @@ -164,8 +249,22 @@ | ||
| 164 | 249 | if (isset($block['attrs']['dataFBoxOnClickURLAddParameters']) && $block['attrs']['dataFBoxOnClickURLAddParameters']) |
| 165 | 250 | { |
| 166 | 251 | $atts['utmParamsEnabled'] = true; |
| 167 | 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 | |
| 168 | 267 | |
| 169 | 268 | return $atts; |
| 170 | 269 | } |
| 171 | 270 | } |