| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 1.0.2 Free | |
| 4 | + * @version 2.1.4 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <info@fireplugins.com> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2021 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,12 +19,40 @@ | ||
| 19 | 19 | class BoxBlocksParser |
| 20 | 20 | { |
| 21 | 21 | public function __construct() |
| 22 | 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 | |
| 23 | 28 | // filter the post content by setting supported block attributes |
| 24 | - add_filter('render_block', [$this, 'modify_block_output'], 10, 2); | |
| 29 | + add_filter('render_block', [$this, 'deprecated_modify_block_output'], 10, 2); | |
| 30 | + // DEPRECATED-END | |
| 25 | 31 | } |
| 26 | 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 | + | |
| 27 | 55 | /** |
| 28 | 56 | * Filters the block output by appending out custom data attributes |
| 29 | 57 | * on our supported blocks |
| 30 | 58 | * |
| @@ -34,8 +62,14 @@ | ||
| 34 | 62 | * @return string |
| 35 | 63 | */ |
| 36 | 64 | public function modify_block_output($block_content, $block) |
| 37 | 65 | { |
| 66 | + $atts = $this->getBlockAttributes($block); | |
| 67 | + if (!$atts['utmParamsEnabled']) | |
| 68 | + { | |
| 69 | + return $block_content; | |
| 70 | + } | |
| 71 | + | |
| 38 | 72 | if (!isset($block['blockName'])) |
| 39 | 73 | { |
| 40 | 74 | return $block_content; |
| 41 | 75 | } |
| @@ -42,8 +76,73 @@ | ||
| 42 | 76 | |
| 43 | 77 | $blockName = $block['blockName']; |
| 44 | 78 | |
| 45 | 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(strip_tags($block['innerHTML'])); | |
| 99 | + } | |
| 100 | + else if ($blockName === 'firebox/button') | |
| 101 | + { | |
| 102 | + $utmContent = isset($block['attrs']['text']) ? trim(strip_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', | |
| 46 | 145 | 'core/buttons', |
| 47 | 146 | 'core/image' |
| 48 | 147 | ]; |
| 49 | 148 | |
| @@ -54,9 +153,9 @@ | ||
| 54 | 153 | |
| 55 | 154 | /** |
| 56 | 155 | * Handle Core Buttons Block |
| 57 | 156 | */ |
| 58 | - if ($blockName == 'core/buttons') | |
| 157 | + if (in_array($blockName, ['core/buttons', 'firebox/buttons'])) | |
| 59 | 158 | { |
| 60 | 159 | $buttons = isset($block['innerBlocks']) ? $block['innerBlocks'] : []; |
| 61 | 160 | if (!$buttons) |
| 62 | 161 | { |
| @@ -70,9 +169,9 @@ | ||
| 70 | 169 | } |
| 71 | 170 | /** |
| 72 | 171 | * Handle Core Image Block |
| 73 | 172 | */ |
| 74 | - else if ($blockName == 'core/image') | |
| 173 | + else if (in_array($blockName, ['core/image', 'firebox/image'])) | |
| 75 | 174 | { |
| 76 | 175 | $new_block_html = $block['innerHTML']; |
| 77 | 176 | |
| 78 | 177 | // check if we have an anchor element and add attributes to this elem. |
| @@ -97,8 +196,9 @@ | ||
| 97 | 196 | */ |
| 98 | 197 | private function replaceBlockAttributes($element, $block, $block_content) |
| 99 | 198 | { |
| 100 | 199 | $atts = $this->getBlockAttributes($block); |
| 200 | + | |
| 101 | 201 | if (!$atts['enabled']) |
| 102 | 202 | { |
| 103 | 203 | return $block_content; |
| 104 | 204 | } |
| @@ -105,24 +205,27 @@ | ||
| 105 | 205 | |
| 106 | 206 | $new_block_html = $block['innerHTML']; |
| 107 | 207 | |
| 108 | 208 | // box |
| 109 | - if ($atts['box']) | |
| 209 | + if (!is_null($atts['box']) && $atts['box'] !== 'none') | |
| 110 | 210 | { |
| 111 | - $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox="' . esc_attr($atts['box']) . '" ', $new_block_html); | |
| 112 | - } | |
| 211 | + if ($atts['box'] !== 'current') | |
| 212 | + { | |
| 213 | + $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox="' . esc_attr($atts['box']) . '" ', $new_block_html); | |
| 214 | + } | |
| 113 | 215 | |
| 114 | - // cmd | |
| 115 | - if ($atts['cmd']) | |
| 116 | - { | |
| 117 | - $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-cmd="' . esc_attr($atts['cmd']) . '" ', $new_block_html); | |
| 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); | |
| 118 | 224 | } |
| 119 | 225 | |
| 120 | - // prevent default | |
| 121 | - $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-prevent="' . esc_attr((int) $atts['preventDefault']) . '" ', $new_block_html); | |
| 122 | - | |
| 123 | 226 | // class |
| 124 | - if ($atts['class']) | |
| 227 | + if (isset($atts['class']) && !empty($atts['class'])) | |
| 125 | 228 | { |
| 126 | 229 | $new_block_html = preg_replace('/<' . $element . '(.*)class="/', "<" . $element . "$1class=\"" . esc_attr($atts['class']) . ' ', $new_block_html); |
| 127 | 230 | } |
| 128 | 231 | |
| @@ -127,9 +230,10 @@ | ||
| 127 | 230 | } |
| 128 | 231 | |
| 129 | 232 | return str_replace($block['innerHTML'], $new_block_html, $block_content); |
| 130 | 233 | } |
| 131 | - | |
| 234 | + // DEPRECATED-END | |
| 235 | + | |
| 132 | 236 | /** |
| 133 | 237 | * Retrieves the block attributes |
| 134 | 238 | * |
| 135 | 239 | * @param array $block |
| @@ -137,15 +241,30 @@ | ||
| 137 | 241 | * @return array |
| 138 | 242 | */ |
| 139 | 243 | private function getBlockAttributes($block) |
| 140 | 244 | { |
| 141 | - $atts = []; | |
| 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 | + } | |
| 142 | 262 | |
| 143 | - $atts['enabled'] = isset($block['attrs']['dataFBoxEnabled']) ?: false; | |
| 144 | - $atts['box'] = isset($block['attrs']['dataFBox']) ? $block['attrs']['dataFBox'] : null; | |
| 145 | - $atts['cmd'] = isset($block['attrs']['dataFBoxCmd']) ? $block['attrs']['dataFBoxCmd'] : 'close'; | |
| 263 | + $atts['box'] = isset($block['attrs']['dataFBox']) ? $block['attrs']['dataFBox'] : 'current'; | |
| 264 | + $atts['cmd'] = isset($block['attrs']['dataFBoxCmd']) ? $block['attrs']['dataFBoxCmd'] : 'open'; | |
| 146 | 265 | $atts['class'] = isset($block['attrs']['dataFBoxClass']) ? $block['attrs']['dataFBoxClass'] : ''; |
| 147 | - $atts['preventDefault'] = isset($block['attrs']['dataFBoxPreventDefault']) ? $block['attrs']['dataFBoxPreventDefault'] : 1; | |
| 266 | + // DEPRECATED-END | |
| 148 | 267 | |
| 149 | 268 | return $atts; |
| 150 | 269 | } |
| 151 | 270 | } |