block-api
3 days ago
form
3 days ago
import
3 days ago
popup
3 days ago
templates
3 days ago
class-gutenberg-block-styles.php
3 days ago
class-gutenberg-cache-util.php
3 days ago
class-gutenberg-conditions-controller.php
3 days ago
class-gutenberg-controller.php
3 days ago
class-gutenberg-dynamic-content-controller.php
3 days ago
class-gutenberg-enhancements-controller.php
3 days ago
class-gutenberg-social-icons-controller.php
3 days ago
class-gutenberg-sticky-controller.php
3 days ago
class-gutenberg-z-index-controller.php
3 days ago
class-gutenberg-z-index-controller.php
61 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Gutenberg\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | use WP_HTML_Tag_Processor; |
| 8 | |
| 9 | class GutenbergZIndexController |
| 10 | { |
| 11 | const Z_INDEX_MIN = -100; |
| 12 | const Z_INDEX_MAX = 999; |
| 13 | |
| 14 | public static function Initialize() |
| 15 | { |
| 16 | add_filter('render_block', array(__CLASS__, 'FilterZIndexRender'), 9, 2); |
| 17 | } |
| 18 | |
| 19 | public static function FilterZIndexRender($block_content, $block) |
| 20 | { |
| 21 | if (empty($block_content)) { |
| 22 | return $block_content; |
| 23 | } |
| 24 | |
| 25 | $attrs = isset($block['attrs']) && is_array($block['attrs']) ? $block['attrs'] : array(); |
| 26 | if (!isset($attrs['spbaddZIndex']) || !is_numeric($attrs['spbaddZIndex'])) { |
| 27 | return $block_content; |
| 28 | } |
| 29 | |
| 30 | $zindex = intval($attrs['spbaddZIndex']); |
| 31 | if ($zindex < self::Z_INDEX_MIN) { |
| 32 | $zindex = self::Z_INDEX_MIN; |
| 33 | } elseif ($zindex > self::Z_INDEX_MAX) { |
| 34 | $zindex = self::Z_INDEX_MAX; |
| 35 | } |
| 36 | |
| 37 | $processor = new WP_HTML_Tag_Processor($block_content); |
| 38 | if (!$processor->next_tag()) { |
| 39 | return $block_content; |
| 40 | } |
| 41 | |
| 42 | $existing_style = $processor->get_attribute('style'); |
| 43 | $existing_style = is_string($existing_style) ? $existing_style : ''; |
| 44 | $existing_style_lower = strtolower($existing_style); |
| 45 | |
| 46 | $addition = 'z-index:' . $zindex . ';'; |
| 47 | // z-index only applies to positioned elements. Add position:relative as a |
| 48 | // safe default when the block hasn't already set position explicitly. |
| 49 | if (strpos($existing_style_lower, 'position:') === false) { |
| 50 | $addition .= 'position:relative;'; |
| 51 | } |
| 52 | |
| 53 | if ($existing_style !== '' && substr($existing_style, -1) !== ';') { |
| 54 | $existing_style .= ';'; |
| 55 | } |
| 56 | $processor->set_attribute('style', $existing_style . $addition); |
| 57 | |
| 58 | return $processor->get_updated_html(); |
| 59 | } |
| 60 | } |
| 61 |