class-author-box-controller.php
2 days ago
class-dynamic-block-assets.php
2 days ago
class-recent-posts-controller.php
2 days ago
class-table-of-contents-controller.php
2 days ago
class-dynamic-block-assets.php
197 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Gutenberg\BlocksAPI\Controllers; |
| 4 | |
| 5 | use SuperbAddons\Data\Utils\ScriptTranslations; |
| 6 | use SuperbAddons\Gutenberg\Form\FormSettings; |
| 7 | |
| 8 | defined('ABSPATH') || exit(); |
| 9 | |
| 10 | class DynamicBlockAssets |
| 11 | { |
| 12 | public static function EnqueueAnimatedHeader($attr, $content) |
| 13 | { |
| 14 | wp_enqueue_script( |
| 15 | 'superbaddons-animated-heading', |
| 16 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/animated-heading.js', |
| 17 | [], |
| 18 | SUPERBADDONS_VERSION, |
| 19 | true |
| 20 | ); |
| 21 | return $content; |
| 22 | } |
| 23 | |
| 24 | public static function EnqueueRevealButton($attr, $content) |
| 25 | { |
| 26 | wp_enqueue_script( |
| 27 | 'superbaddons-reveal-button', |
| 28 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/reveal-button.js', |
| 29 | [], |
| 30 | SUPERBADDONS_VERSION, |
| 31 | true |
| 32 | ); |
| 33 | return $content; |
| 34 | } |
| 35 | |
| 36 | public static function EnqueueAccordion($attr, $content) |
| 37 | { |
| 38 | wp_enqueue_script( |
| 39 | 'superbaddons-accordion', |
| 40 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/accordion.js', |
| 41 | [], |
| 42 | SUPERBADDONS_VERSION, |
| 43 | true |
| 44 | ); |
| 45 | return $content; |
| 46 | } |
| 47 | |
| 48 | public static function EnqueueCountdown($attr, $content) |
| 49 | { |
| 50 | wp_enqueue_script( |
| 51 | 'superbaddons-countdown', |
| 52 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/countdown.js', |
| 53 | [], |
| 54 | SUPERBADDONS_VERSION, |
| 55 | true |
| 56 | ); |
| 57 | |
| 58 | // Hide countdown until JS initializes to prevent flash of uninitialized state |
| 59 | $processor = new \WP_HTML_Tag_Processor($content); |
| 60 | if ($processor->next_tag()) { |
| 61 | $existing = $processor->get_attribute('style'); |
| 62 | $existing = isset($existing) ? $existing : ''; |
| 63 | if (!empty($existing) && substr($existing, -1) !== ';') { |
| 64 | $existing .= ';'; |
| 65 | } |
| 66 | $processor->set_attribute('style', $existing . 'visibility:hidden;'); |
| 67 | $content = $processor->get_updated_html(); |
| 68 | } |
| 69 | |
| 70 | return $content; |
| 71 | } |
| 72 | |
| 73 | public static function EnqueueProgressBar($attr, $content) |
| 74 | { |
| 75 | wp_enqueue_script( |
| 76 | 'superbaddons-progress-bar', |
| 77 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/progress-bar.js', |
| 78 | [], |
| 79 | SUPERBADDONS_VERSION, |
| 80 | true |
| 81 | ); |
| 82 | |
| 83 | // Hide block until JS initializes to prevent flash of un-animated state |
| 84 | $processor = new \WP_HTML_Tag_Processor($content); |
| 85 | if ($processor->next_tag()) { |
| 86 | $existing = $processor->get_attribute('style'); |
| 87 | if (!is_string($existing)) { |
| 88 | $existing = ''; |
| 89 | } |
| 90 | if (!empty($existing) && substr($existing, -1) !== ';') { |
| 91 | $existing .= ';'; |
| 92 | } |
| 93 | $processor->set_attribute('style', $existing . 'visibility:hidden;'); |
| 94 | $content = $processor->get_updated_html(); |
| 95 | } |
| 96 | |
| 97 | return $content; |
| 98 | } |
| 99 | |
| 100 | public static function EnqueueCarousel($attr, $content) |
| 101 | { |
| 102 | wp_enqueue_script( |
| 103 | 'superbaddons-carousel', |
| 104 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/carousel.js', |
| 105 | ['wp-i18n'], |
| 106 | SUPERBADDONS_VERSION, |
| 107 | true |
| 108 | ); |
| 109 | ScriptTranslations::Set('superbaddons-carousel'); |
| 110 | return $content; |
| 111 | } |
| 112 | |
| 113 | public static function EnqueuePopup($attr, $content) |
| 114 | { |
| 115 | wp_enqueue_script( |
| 116 | 'superbaddons-popup', |
| 117 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/popup.js', |
| 118 | [], |
| 119 | SUPERBADDONS_VERSION, |
| 120 | true |
| 121 | ); |
| 122 | return $content; |
| 123 | } |
| 124 | |
| 125 | public static function EnqueueForm($attr, $content, $block = null) |
| 126 | { |
| 127 | if (!$block || empty($block->inner_blocks)) { |
| 128 | $isMultistep = $block && isset($block->name) && $block->name === 'superb-addons/multistep-form'; |
| 129 | return $isMultistep |
| 130 | ? '<!-- Superb Multi-Step Form: not rendered because the form has no steps. Please add steps in the block editor. -->' |
| 131 | : '<!-- Superb Form: not rendered because the form has no fields. Please add fields in the block editor. -->'; |
| 132 | } |
| 133 | |
| 134 | wp_enqueue_script( |
| 135 | 'superbaddons-form', |
| 136 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/form.js', |
| 137 | array('wp-i18n'), |
| 138 | SUPERBADDONS_VERSION, |
| 139 | true |
| 140 | ); |
| 141 | ScriptTranslations::Set('superbaddons-form'); |
| 142 | |
| 143 | static $localized = false; |
| 144 | if (!$localized) { |
| 145 | $localized = true; |
| 146 | $config = array('restUrl' => \get_rest_url(null, 'superbaddons/')); |
| 147 | $siteKeys = array( |
| 148 | 'hcaptchaSiteKey' => FormSettings::Get(FormSettings::OPTION_HCAPTCHA_SITE_KEY), |
| 149 | 'recaptchaSiteKey' => FormSettings::Get(FormSettings::OPTION_RECAPTCHA_SITE_KEY), |
| 150 | 'turnstileSiteKey' => FormSettings::Get(FormSettings::OPTION_TURNSTILE_SITE_KEY), |
| 151 | ); |
| 152 | foreach ($siteKeys as $key => $value) { |
| 153 | if (!empty($value)) { |
| 154 | $config[$key] = $value; |
| 155 | } |
| 156 | } |
| 157 | wp_localize_script('superbaddons-form', 'superbFormsConfig', $config); |
| 158 | } |
| 159 | |
| 160 | return $content; |
| 161 | } |
| 162 | |
| 163 | public static function EnqueueAddToCart($attr, $content = '') |
| 164 | { |
| 165 | if (!function_exists('WC')) { |
| 166 | return $content; |
| 167 | } |
| 168 | |
| 169 | wp_enqueue_script( |
| 170 | 'superbaddons-add-to-cart', |
| 171 | SUPERBADDONS_ASSETS_PATH . '/js/dynamic-blocks/add-to-cart.js', |
| 172 | array('wp-i18n'), |
| 173 | SUPERBADDONS_VERSION, |
| 174 | true |
| 175 | ); |
| 176 | ScriptTranslations::Set('superbaddons-add-to-cart'); |
| 177 | |
| 178 | static $localized = false; |
| 179 | if (!$localized) { |
| 180 | $localized = true; |
| 181 | wp_localize_script('superbaddons-add-to-cart', 'superbAddToCartConfig', array( |
| 182 | 'restUrl' => \get_rest_url(null, 'superbaddons/'), |
| 183 | 'nonce' => wp_create_nonce('wp_rest'), |
| 184 | 'cartUrl' => esc_url_raw((string) wc_get_cart_url()), |
| 185 | 'checkoutUrl' => esc_url_raw((string) wc_get_checkout_url()), |
| 186 | 'currencySymbol' => html_entity_decode(get_woocommerce_currency_symbol(), ENT_QUOTES, 'UTF-8'), |
| 187 | 'currencyPosition' => (string) get_option('woocommerce_currency_pos', 'left'), |
| 188 | 'decimalSeparator' => function_exists('wc_get_price_decimal_separator') ? (string) wc_get_price_decimal_separator() : '.', |
| 189 | 'thousandSeparator' => function_exists('wc_get_price_thousand_separator') ? (string) wc_get_price_thousand_separator() : ',', |
| 190 | 'decimals' => function_exists('wc_get_price_decimals') ? intval(wc_get_price_decimals()) : 2, |
| 191 | )); |
| 192 | } |
| 193 | |
| 194 | return $content; |
| 195 | } |
| 196 | } |
| 197 |