| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 1.0.13 Free | |
| 4 | + * @version 2.1.39 Free | |
| 5 | 5 | * |
| 6 | 6 | * @author FirePlugins <info@fireplugins.com> |
| 7 | 7 | * @link https://www.fireplugins.com |
| 8 | - * @copyright Copyright © 2022 FirePlugins All Rights Reserved | |
| 8 | + * @copyright Copyright © 2025 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,35 @@ | ||
| 19 | 19 | class BoxBlocksParser |
| 20 | 20 | { |
| 21 | 21 | public function __construct() |
| 22 | 22 | { |
| 23 | - // filter the post content by setting supported block attributes | |
| 23 | + add_filter('render_block', [$this, 'maybe_load_block_extension_script'], 10, 2); | |
| 24 | + | |
| 24 | 25 | add_filter('render_block', [$this, 'modify_block_output'], 10, 2); |
| 25 | 26 | } |
| 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 | + | |
| 27 | 50 | /** |
| 28 | 51 | * Filters the block output by appending out custom data attributes |
| 29 | 52 | * on our supported blocks |
| 30 | 53 | * |
| @@ -34,8 +57,14 @@ | ||
| 34 | 57 | * @return string |
| 35 | 58 | */ |
| 36 | 59 | public function modify_block_output($block_content, $block) |
| 37 | 60 | { |
| 61 | + $atts = $this->getBlockAttributes($block); | |
| 62 | + if (!$atts['utmParamsEnabled']) | |
| 63 | + { | |
| 64 | + return $block_content; | |
| 65 | + } | |
| 66 | + | |
| 38 | 67 | if (!isset($block['blockName'])) |
| 39 | 68 | { |
| 40 | 69 | return $block_content; |
| 41 | 70 | } |
| @@ -42,100 +71,51 @@ | ||
| 42 | 71 | |
| 43 | 72 | $blockName = $block['blockName']; |
| 44 | 73 | |
| 45 | 74 | $parsable_blocks = [ |
| 46 | - 'core/buttons', | |
| 75 | + 'firebox/button', | |
| 76 | + 'firebox/image', | |
| 77 | + 'core/button', | |
| 47 | 78 | 'core/image' |
| 48 | 79 | ]; |
| 49 | - | |
| 80 | + | |
| 50 | 81 | if (!in_array($blockName, $parsable_blocks)) |
| 51 | 82 | { |
| 52 | 83 | return $block_content; |
| 53 | 84 | } |
| 54 | 85 | |
| 55 | - /** | |
| 56 | - * Handle Core Buttons Block | |
| 57 | - */ | |
| 58 | - if ($blockName == 'core/buttons') | |
| 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') | |
| 59 | 92 | { |
| 60 | - $buttons = isset($block['innerBlocks']) ? $block['innerBlocks'] : []; | |
| 61 | - if (!$buttons) | |
| 62 | - { | |
| 63 | - return $block_content; | |
| 64 | - } | |
| 65 | - | |
| 66 | - foreach ($buttons as $button) | |
| 67 | - { | |
| 68 | - $block_content = $this->replaceBlockAttributes('a', $button, $block_content); | |
| 69 | - } | |
| 93 | + $utmContent = trim(wp_strip_all_tags($block['innerHTML'])); | |
| 70 | 94 | } |
| 71 | - /** | |
| 72 | - * Handle Core Image Block | |
| 73 | - */ | |
| 74 | - else if ($blockName == 'core/image') | |
| 95 | + else if ($blockName === 'firebox/button') | |
| 75 | 96 | { |
| 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); | |
| 97 | + $utmContent = isset($block['attrs']['text']) ? trim(wp_strip_all_tags($block['attrs']['text'])) : ''; | |
| 84 | 98 | } |
| 85 | - | |
| 86 | - return $block_content; | |
| 87 | - } | |
| 88 | - | |
| 89 | - /** | |
| 90 | - * Adds the data attributes and class($atts) to the given element($element) within the block content($block_content) | |
| 91 | - * | |
| 92 | - * @param string $element | |
| 93 | - * @param array $block | |
| 94 | - * @param string $block_element | |
| 95 | - * | |
| 96 | - * @return string | |
| 97 | - */ | |
| 98 | - private function replaceBlockAttributes($element, $block, $block_content) | |
| 99 | - { | |
| 100 | - $atts = $this->getBlockAttributes($block); | |
| 101 | - if (!$atts['enabled']) | |
| 99 | + else if ($blockName === 'core/image') | |
| 102 | 100 | { |
| 103 | - return $block_content; | |
| 101 | + // Get the src attribute value from $block['innerHTML'] using regex | |
| 102 | + $utmContent = preg_match('/src="([^"]*)"/', $block['innerHTML'], $matches) ? $matches[1] : ''; | |
| 104 | 103 | } |
| 105 | - | |
| 106 | - $new_block_html = $block['innerHTML']; | |
| 107 | - | |
| 108 | - // box | |
| 109 | - if (!is_null($atts['box']) && $atts['box'] !== 'none') | |
| 104 | + else if ($blockName === 'firebox/image') | |
| 110 | 105 | { |
| 111 | - if ($atts['box'] !== 'current') | |
| 112 | - { | |
| 113 | - $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox="' . esc_attr($atts['box']) . '" ', $new_block_html); | |
| 114 | - } | |
| 115 | - | |
| 116 | - // cmd | |
| 117 | - if ($atts['cmd']) | |
| 118 | - { | |
| 119 | - $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-cmd="' . esc_attr($atts['cmd']) . '" ', $new_block_html); | |
| 120 | - } | |
| 121 | - | |
| 122 | - // prevent default | |
| 123 | - if ($atts['preventDefault']) | |
| 124 | - { | |
| 125 | - $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-prevent="' . esc_attr((int) $atts['preventDefault']) . '" ', $new_block_html); | |
| 126 | - } | |
| 106 | + $utmContent = isset($block['attrs']['image']['desktop']['url']) ? $block['attrs']['image']['desktop']['url'] : ''; | |
| 127 | 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; | |
| 128 | 111 | |
| 129 | - // class | |
| 130 | - if ($atts['class']) | |
| 131 | - { | |
| 132 | - $new_block_html = preg_replace('/<' . $element . '(.*)class="/', "<" . $element . "$1class=\"" . esc_attr($atts['class']) . ' ', $new_block_html); | |
| 133 | - } | |
| 112 | + // Find the href attribute and append to it the utm params | |
| 113 | + $block_content = preg_replace('/href="([^"]*)"/', 'href="$1' . $utmParams . '"', $block_content); | |
| 134 | 114 | |
| 135 | - return str_replace($block['innerHTML'], $new_block_html, $block_content); | |
| 115 | + return $block_content; | |
| 136 | 116 | } |
| 137 | - | |
| 117 | + | |
| 138 | 118 | /** |
| 139 | 119 | * Retrieves the block attributes |
| 140 | 120 | * |
| 141 | 121 | * @param array $block |
| @@ -143,15 +123,16 @@ | ||
| 143 | 123 | * @return array |
| 144 | 124 | */ |
| 145 | 125 | private function getBlockAttributes($block) |
| 146 | 126 | { |
| 147 | - $atts = []; | |
| 148 | - | |
| 149 | - $atts['enabled'] = isset($block['attrs']['dataFBoxEnabled']) ?: false; | |
| 150 | - $atts['box'] = isset($block['attrs']['dataFBox']) ? $block['attrs']['dataFBox'] : null; | |
| 151 | - $atts['cmd'] = isset($block['attrs']['dataFBoxCmd']) ? $block['attrs']['dataFBoxCmd'] : 'close'; | |
| 152 | - $atts['class'] = isset($block['attrs']['dataFBoxClass']) ? $block['attrs']['dataFBoxClass'] : ''; | |
| 153 | - $atts['preventDefault'] = isset($block['attrs']['dataFBoxPreventDefault']) ? $block['attrs']['dataFBoxPreventDefault'] : 1; | |
| 127 | + $atts = [ | |
| 128 | + 'utmParamsEnabled' => false | |
| 129 | + ]; | |
| 130 | + | |
| 131 | + if (isset($block['attrs']['dataFBoxOnClickURLAddParameters']) && $block['attrs']['dataFBoxOnClickURLAddParameters']) | |
| 132 | + { | |
| 133 | + $atts['utmParamsEnabled'] = true; | |
| 134 | + } | |
| 154 | 135 | |
| 155 | 136 | return $atts; |
| 156 | 137 | } |
| 157 | 138 | } |