| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 2.0.16 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 |
'firebox/image', |
| 48 |
'core/buttons', |
| 49 |
'core/image' |
| 50 |
]; |
| 51 |
|
| 52 |
if (!in_array($blockName, $parsable_blocks)) |
| 53 |
{ |
| 54 |
return $block_content; |
| 55 |
} |
| 56 |
|
| 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 |
} |
| 67 |
|
| 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'])) |
| 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); |
| 86 |
} |
| 87 |
|
| 88 |
return $block_content; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Adds the data attributes and class($atts) to the given element($element) within the block content($block_content) |
| 93 |
* |
| 94 |
* @param string $element |
| 95 |
* @param array $block |
| 96 |
* @param string $block_element |
| 97 |
* |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
private function replaceBlockAttributes($element, $block, $block_content) |
| 101 |
{ |
| 102 |
$atts = $this->getBlockAttributes($block); |
| 103 |
if (!$atts['enabled']) |
| 104 |
{ |
| 105 |
return $block_content; |
| 106 |
} |
| 107 |
|
| 108 |
$new_block_html = $block['innerHTML']; |
| 109 |
|
| 110 |
// box |
| 111 |
if (!is_null($atts['box']) && $atts['box'] !== 'none') |
| 112 |
{ |
| 113 |
if ($atts['box'] !== 'current') |
| 114 |
{ |
| 115 |
$new_block_html = str_replace('<' . $element . ' ', '<' . $element . ' data-fbox="' . esc_attr($atts['box']) . '" ', $new_block_html); |
| 116 |
} |
| 117 |
|
| 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); |
| 126 |
} |
| 127 |
|
| 128 |
// class |
| 129 |
if (isset($atts['class']) && !empty($atts['class'])) |
| 130 |
{ |
| 131 |
$new_block_html = preg_replace('/<' . $element . '(.*)class="/', "<" . $element . "$1class=\"" . esc_attr($atts['class']) . ' ', $new_block_html); |
| 132 |
} |
| 133 |
|
| 134 |
return str_replace($block['innerHTML'], $new_block_html, $block_content); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Retrieves the block attributes |
| 139 |
* |
| 140 |
* @param array $block |
| 141 |
* |
| 142 |
* @return array |
| 143 |
*/ |
| 144 |
private function getBlockAttributes($block) |
| 145 |
{ |
| 146 |
$atts = []; |
| 147 |
|
| 148 |
$atts['enabled'] = isset($block['attrs']['dataFBoxEnabled']) ?: false; |
| 149 |
|
| 150 |
if (!$atts['enabled']) |
| 151 |
{ |
| 152 |
return $atts; |
| 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'] : ''; |
| 158 |
|
| 159 |
return $atts; |
| 160 |
} |
| 161 |
} |