block-api
2 days ago
form
2 days ago
import
2 days ago
popup
2 days ago
templates
2 days ago
class-gutenberg-block-styles.php
2 days ago
class-gutenberg-cache-util.php
2 days ago
class-gutenberg-conditions-controller.php
2 days ago
class-gutenberg-controller.php
2 days ago
class-gutenberg-dynamic-content-controller.php
2 days ago
class-gutenberg-enhancements-controller.php
2 days ago
class-gutenberg-social-icons-controller.php
2 days ago
class-gutenberg-sticky-controller.php
2 days ago
class-gutenberg-z-index-controller.php
2 days ago
class-gutenberg-sticky-controller.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Gutenberg\Controllers; |
| 4 | |
| 5 | defined('ABSPATH') || exit(); |
| 6 | |
| 7 | use WP_HTML_Tag_Processor; |
| 8 | |
| 9 | class GutenbergStickyController |
| 10 | { |
| 11 | const FRONTEND_SCRIPT_HANDLE = 'superbaddons-sticky'; |
| 12 | |
| 13 | public static function Initialize() |
| 14 | { |
| 15 | add_filter('render_block', array(__CLASS__, 'FilterStickyRender'), 9, 2); |
| 16 | } |
| 17 | |
| 18 | public static function FilterStickyRender($block_content, $block) |
| 19 | { |
| 20 | if (empty($block_content)) { |
| 21 | return $block_content; |
| 22 | } |
| 23 | |
| 24 | if (isset($block['blockName']) && $block['blockName'] === 'superb-addons/popup') { |
| 25 | return $block_content; |
| 26 | } |
| 27 | |
| 28 | $attrs = isset($block['attrs']) && is_array($block['attrs']) ? $block['attrs'] : array(); |
| 29 | if (empty($attrs['spbaddStickyEnabled'])) { |
| 30 | return $block_content; |
| 31 | } |
| 32 | |
| 33 | $position = isset($attrs['spbaddStickyPosition']) ? $attrs['spbaddStickyPosition'] : 'top'; |
| 34 | if (!in_array($position, array('top', 'bottom'), true)) { |
| 35 | $position = 'top'; |
| 36 | } |
| 37 | |
| 38 | $scope = isset($attrs['spbaddStickyScope']) ? $attrs['spbaddStickyScope'] : 'screen'; |
| 39 | if (!in_array($scope, array('screen', 'parent', 'top-level'), true)) { |
| 40 | $scope = 'screen'; |
| 41 | } |
| 42 | |
| 43 | $offset = self::SanitizeOffset(isset($attrs['spbaddStickyOffset']) ? $attrs['spbaddStickyOffset'] : '0px'); |
| 44 | |
| 45 | $disable_on = array(); |
| 46 | if (isset($attrs['spbaddStickyDisableOn']) && is_array($attrs['spbaddStickyDisableOn'])) { |
| 47 | $disable_on = array_values(array_intersect( |
| 48 | $attrs['spbaddStickyDisableOn'], |
| 49 | array('desktop', 'tablet', 'mobile') |
| 50 | )); |
| 51 | } |
| 52 | |
| 53 | $processor = new WP_HTML_Tag_Processor($block_content); |
| 54 | if (!$processor->next_tag()) { |
| 55 | return $block_content; |
| 56 | } |
| 57 | |
| 58 | $processor->set_attribute('data-spbadd-sticky', 'true'); |
| 59 | $processor->set_attribute('data-spbadd-sticky-position', $position); |
| 60 | $processor->set_attribute('data-spbadd-sticky-scope', $scope); |
| 61 | |
| 62 | if (!empty($disable_on)) { |
| 63 | $processor->set_attribute('data-spbadd-sticky-disable', implode(',', $disable_on)); |
| 64 | } |
| 65 | |
| 66 | $existing_style = $processor->get_attribute('style'); |
| 67 | $existing_style = is_string($existing_style) ? $existing_style : ''; |
| 68 | if ($existing_style !== '' && substr($existing_style, -1) !== ';') { |
| 69 | $existing_style .= ';'; |
| 70 | } |
| 71 | $existing_style .= '--spbadd-sticky-offset:' . $offset . ';--spbadd-sticky-z:10;'; |
| 72 | $processor->set_attribute('style', $existing_style); |
| 73 | |
| 74 | wp_enqueue_script( |
| 75 | self::FRONTEND_SCRIPT_HANDLE, |
| 76 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/sticky.js', |
| 77 | array(), |
| 78 | SUPERBADDONS_VERSION, |
| 79 | true |
| 80 | ); |
| 81 | |
| 82 | return $processor->get_updated_html(); |
| 83 | } |
| 84 | |
| 85 | private static function SanitizeOffset($raw) |
| 86 | { |
| 87 | $raw = is_string($raw) ? trim($raw) : (string) $raw; |
| 88 | if ($raw === '') { |
| 89 | return '0px'; |
| 90 | } |
| 91 | if (preg_match('/^-?\d*\.?\d+$/', $raw)) { |
| 92 | return $raw . 'px'; |
| 93 | } |
| 94 | if (preg_match('/^-?\d*\.?\d+(px|rem|em|vh|%)$/i', $raw)) { |
| 95 | return strtolower($raw); |
| 96 | } |
| 97 | return '0px'; |
| 98 | } |
| 99 | } |
| 100 |