| @@ -1,12 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * @package FireBox |
| 4 | - * @version 3.1.13 Free | |
| 4 | + * @version 2.0.12 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 © 2023 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,63 +19,71 @@ | ||
| 19 | 19 | class BoxBlocksParser |
| 20 | 20 | { |
| 21 | 21 | public function __construct() |
| 22 | 22 | { |
| 23 | - add_filter('render_block', [$this, 'parse_block'], 10, 2); | |
| 23 | + // filter the post content by setting supported block attributes | |
| 24 | + add_filter('render_block', [$this, 'modify_block_output'], 10, 2); | |
| 24 | 25 | } |
| 25 | 26 | |
| 26 | 27 | /** |
| 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 | - * | |
| 28 | + * Filters the block output by appending out custom data attributes | |
| 29 | + * on our supported blocks | |
| 30 | + * | |
| 31 | 31 | * @param string $block_content |
| 32 | 32 | * @param array $block |
| 33 | - * | |
| 33 | + * | |
| 34 | 34 | * @return string |
| 35 | 35 | */ |
| 36 | - public function parse_block($block_content, $block) | |
| 36 | + public function modify_block_output($block_content, $block) | |
| 37 | 37 | { |
| 38 | - if (empty($block['attrs']) || !$this->hasFireBoxAttrs($block['attrs'])) | |
| 38 | + if (!isset($block['blockName'])) | |
| 39 | 39 | { |
| 40 | 40 | return $block_content; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | - $block_content = $this->maybe_load_block_extension_script($block_content, $block); | |
| 44 | - $block_content = $this->modify_block_output($block_content, $block); | |
| 43 | + $blockName = $block['blockName']; | |
| 45 | 44 | |
| 46 | - return $block_content; | |
| 47 | - } | |
| 45 | + $parsable_blocks = [ | |
| 46 | + 'firebox/buttons', | |
| 47 | + 'firebox/image', | |
| 48 | + 'core/buttons', | |
| 49 | + 'core/image' | |
| 50 | + ]; | |
| 51 | + | |
| 52 | + if (!in_array($blockName, $parsable_blocks)) | |
| 53 | + { | |
| 54 | + return $block_content; | |
| 55 | + } | |
| 48 | 56 | |
| 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 | - } | |
| 57 | + /** | |
| 58 | + * Handle Core Buttons Block | |
| 59 | + */ | |
| 60 | + if (in_array($blockName, ['core/buttons', 'firebox/buttons'])) | |
| 61 | + { | |
| 62 | + $buttons = isset($block['innerBlocks']) ? $block['innerBlocks'] : []; | |
| 63 | + if (!$buttons) | |
| 64 | + { | |
| 65 | + return $block_content; | |
| 66 | + } | |
| 60 | 67 | |
| 61 | - public function maybe_load_block_extension_script($block_content, $block) | |
| 62 | - { | |
| 63 | - if (isset($block['attrs']['dataFBoxOnClick']) && in_array($block['attrs']['dataFBoxOnClick'], ['copy_clipboard', 'download_file'])) | |
| 68 | + foreach ($buttons as $button) | |
| 69 | + { | |
| 70 | + $block_content = $this->replaceBlockAttributes('a', $button, $block_content); | |
| 71 | + } | |
| 72 | + } | |
| 73 | + /** | |
| 74 | + * Handle Core Image Block | |
| 75 | + */ | |
| 76 | + else if (in_array($blockName, ['core/image', 'firebox/image'])) | |
| 64 | 77 | { |
| 65 | - wp_enqueue_style( | |
| 66 | - 'firebox-block-extensions', | |
| 67 | - FBOX_MEDIA_PUBLIC_URL . 'css/block_extensions.css', | |
| 68 | - [], | |
| 69 | - FBOX_VERSION | |
| 70 | - ); | |
| 71 | - wp_enqueue_script( | |
| 72 | - 'firebox-block-extensions', | |
| 73 | - FBOX_MEDIA_PUBLIC_URL . 'js/block_extensions.js', | |
| 74 | - [], | |
| 75 | - FBOX_VERSION, | |
| 76 | - true | |
| 77 | - ); | |
| 78 | + $new_block_html = $block['innerHTML']; | |
| 79 | + | |
| 80 | + // check if we have an anchor element and add attributes to this elem. | |
| 81 | + $anchorExists = substr_count($new_block_html, '<a '); | |
| 82 | + | |
| 83 | + $element = $anchorExists ? 'a' : 'figure'; | |
| 84 | + | |
| 85 | + $block_content = $this->replaceBlockAttributes($element, $block, $block_content); | |
| 78 | 86 | } |
| 79 | 87 | |
| 80 | 88 | return $block_content; |
| 81 | 89 | } |
| @@ -80,75 +88,53 @@ | ||
| 80 | 88 | return $block_content; |
| 81 | 89 | } |
| 82 | 90 | |
| 83 | 91 | /** |
| 84 | - * Filters the block output by appending out custom data attributes | |
| 85 | - * on our supported blocks | |
| 86 | - * | |
| 87 | - * @param string $block_content | |
| 92 | + * Adds the data attributes and class($atts) to the given element($element) within the block content($block_content) | |
| 93 | + * | |
| 94 | + * @param string $element | |
| 88 | 95 | * @param array $block |
| 89 | - * | |
| 96 | + * @param string $block_element | |
| 97 | + * | |
| 90 | 98 | * @return string |
| 91 | 99 | */ |
| 92 | - public function modify_block_output($block_content, $block) | |
| 100 | + private function replaceBlockAttributes($element, $block, $block_content) | |
| 93 | 101 | { |
| 94 | 102 | $atts = $this->getBlockAttributes($block); |
| 95 | - if (!$atts['utmParamsEnabled']) | |
| 103 | + if (!$atts['enabled']) | |
| 96 | 104 | { |
| 97 | 105 | return $block_content; |
| 98 | 106 | } |
| 99 | 107 | |
| 100 | - if (!isset($block['blockName'])) | |
| 108 | + $new_block_html = $block['innerHTML']; | |
| 109 | + | |
| 110 | + // box | |
| 111 | + if (!is_null($atts['box']) && $atts['box'] !== 'none') | |
| 101 | 112 | { |
| 102 | - return $block_content; | |
| 103 | - } | |
| 113 | + if ($atts['box'] !== 'current') | |
| 114 | + { | |
| 115 | + $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox="' . esc_attr($atts['box']) . '" ', $new_block_html); | |
| 116 | + } | |
| 104 | 117 | |
| 105 | - $blockName = $block['blockName']; | |
| 106 | - | |
| 107 | - $parsable_blocks = [ | |
| 108 | - 'firebox/button', | |
| 109 | - 'firebox/image', | |
| 110 | - 'core/button', | |
| 111 | - 'core/image' | |
| 112 | - ]; | |
| 113 | - | |
| 114 | - if (!in_array($blockName, $parsable_blocks)) | |
| 115 | - { | |
| 116 | - return $block_content; | |
| 118 | + // cmd | |
| 119 | + if ($atts['cmd']) | |
| 120 | + { | |
| 121 | + $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-cmd="' . esc_attr($atts['cmd']) . '" ', $new_block_html); | |
| 122 | + } | |
| 123 | + | |
| 124 | + // prevent default | |
| 125 | + $new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox-prevent="1" ', $new_block_html); | |
| 117 | 126 | } |
| 118 | 127 | |
| 119 | - // Check if href attribute contains ? then prefix is & else prefix is ?, use regex | |
| 120 | - $utmPrefix = preg_match('/href="([^"]*)\?/', $block_content) ? '&' : '?'; | |
| 121 | - | |
| 122 | - // Find utm_content | |
| 123 | - $utmContent = ''; | |
| 124 | - if ($blockName === 'core/button') | |
| 128 | + // class | |
| 129 | + if (isset($atts['class']) && !empty($atts['class'])) | |
| 125 | 130 | { |
| 126 | - $utmContent = trim(wp_strip_all_tags($block['innerHTML'])); | |
| 131 | + $new_block_html = preg_replace('/<' . $element . '(.*)class="/', "<" . $element . "$1class=\"" . esc_attr($atts['class']) . ' ', $new_block_html); | |
| 127 | 132 | } |
| 128 | - else if ($blockName === 'firebox/button') | |
| 129 | - { | |
| 130 | - $utmContent = isset($block['attrs']['text']) ? trim(wp_strip_all_tags($block['attrs']['text'])) : ''; | |
| 131 | - } | |
| 132 | - else if ($blockName === 'core/image') | |
| 133 | - { | |
| 134 | - // Get the src attribute value from $block['innerHTML'] using regex | |
| 135 | - $utmContent = preg_match('/src="([^"]*)"/', $block['innerHTML'], $matches) ? $matches[1] : ''; | |
| 136 | - } | |
| 137 | - else if ($blockName === 'firebox/image') | |
| 138 | - { | |
| 139 | - $utmContent = isset($block['attrs']['image']['desktop']['url']) ? $block['attrs']['image']['desktop']['url'] : ''; | |
| 140 | - } | |
| 141 | - | |
| 142 | - // Get the UTM Parameters | |
| 143 | - $utmParams = $utmPrefix . 'utm_source=' . get_post_type() . '&utm_medium=' . $blockName . '&utm_campaign=' . get_the_title() . '&utm_content=' . $utmContent; | |
| 144 | 133 | |
| 145 | - // Find the href attribute and append to it the utm params | |
| 146 | - $block_content = preg_replace('/href="([^"]*)"/', 'href="$1' . $utmParams . '"', $block_content); | |
| 147 | - | |
| 148 | - return $block_content; | |
| 134 | + return str_replace($block['innerHTML'], $new_block_html, $block_content); | |
| 149 | 135 | } |
| 150 | - | |
| 136 | + | |
| 151 | 137 | /** |
| 152 | 138 | * Retrieves the block attributes |
| 153 | 139 | * |
| 154 | 140 | * @param array $block |
| @@ -156,16 +142,20 @@ | ||
| 156 | 142 | * @return array |
| 157 | 143 | */ |
| 158 | 144 | private function getBlockAttributes($block) |
| 159 | 145 | { |
| 160 | - $atts = [ | |
| 161 | - 'utmParamsEnabled' => false | |
| 162 | - ]; | |
| 146 | + $atts = []; | |
| 163 | 147 | |
| 164 | - if (isset($block['attrs']['dataFBoxOnClickURLAddParameters']) && $block['attrs']['dataFBoxOnClickURLAddParameters']) | |
| 148 | + $atts['enabled'] = isset($block['attrs']['dataFBoxEnabled']) ?: false; | |
| 149 | + | |
| 150 | + if (!$atts['enabled']) | |
| 165 | 151 | { |
| 166 | - $atts['utmParamsEnabled'] = true; | |
| 152 | + return $atts; | |
| 167 | 153 | } |
| 154 | + | |
| 155 | + $atts['box'] = isset($block['attrs']['dataFBox']) ? $block['attrs']['dataFBox'] : null; | |
| 156 | + $atts['cmd'] = isset($block['attrs']['dataFBoxCmd']) ? $block['attrs']['dataFBoxCmd'] : 'open'; | |
| 157 | + $atts['class'] = isset($block['attrs']['dataFBoxClass']) ? $block['attrs']['dataFBoxClass'] : ''; | |
| 168 | 158 | |
| 169 | 159 | return $atts; |
| 170 | 160 | } |
| 171 | 161 | } |