| @@ -1,405 +1,296 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -namespace Getwid\Blocks; | |
| 4 | - | |
| 5 | -class Countdown extends \Getwid\Blocks\AbstractBlock { | |
| 6 | - | |
| 7 | - protected static $blockName = 'getwid/countdown'; | |
| 8 | - | |
| 9 | - public function __construct() { | |
| 10 | - | |
| 11 | - parent::__construct( self::$blockName ); | |
| 12 | - | |
| 13 | - //Set default date + 1 day | |
| 14 | - $current_date = new \DateTime(current_time('Y-m-d H:i:s')); | |
| 15 | - $current_date->add(new \DateInterval('P1D')); | |
| 16 | - $default_date = $current_date->format('Y-m-d H:i:s'); | |
| 17 | - | |
| 18 | - register_block_type( | |
| 19 | - 'getwid/countdown', | |
| 20 | - array( | |
| 21 | - 'attributes' => array( | |
| 22 | - 'dateTime' => array( | |
| 23 | - 'type' => 'string', | |
| 24 | - 'default' => $default_date, | |
| 25 | - ), | |
| 26 | - 'years' => array( | |
| 27 | - 'type' => 'boolean', | |
| 28 | - 'default' => false, | |
| 29 | - ), | |
| 30 | - 'months' => array( | |
| 31 | - 'type' => 'boolean', | |
| 32 | - 'default' => false, | |
| 33 | - ), | |
| 34 | - 'weeks' => array( | |
| 35 | - 'type' => 'boolean', | |
| 36 | - 'default' => false, | |
| 37 | - ), | |
| 38 | - 'days' => array( | |
| 39 | - 'type' => 'boolean', | |
| 40 | - 'default' => true, | |
| 41 | - ), | |
| 42 | - 'hours' => array( | |
| 43 | - 'type' => 'boolean', | |
| 44 | - 'default' => true, | |
| 45 | - ), | |
| 46 | - 'minutes' => array( | |
| 47 | - 'type' => 'boolean', | |
| 48 | - 'default' => true, | |
| 49 | - ), | |
| 50 | - 'seconds' => array( | |
| 51 | - 'type' => 'boolean', | |
| 52 | - 'default' => true, | |
| 53 | - ), | |
| 54 | - 'backgroundColor' => array( | |
| 55 | - 'type' => 'string', | |
| 56 | - ), | |
| 57 | - 'textColor' => array( | |
| 58 | - 'type' => 'string', | |
| 59 | - ), | |
| 60 | - 'customTextColor' => array( | |
| 61 | - 'type' => 'string', | |
| 62 | - ), | |
| 63 | - | |
| 64 | - 'fontGroupID' => array( | |
| 65 | - 'type' => 'string', | |
| 66 | - 'default' => '', | |
| 67 | - ), | |
| 68 | - 'fontFamily' => array( | |
| 69 | - 'type' => 'string', | |
| 70 | - 'default' => '', | |
| 71 | - ), | |
| 72 | - 'fontSize' => array( | |
| 73 | - 'type' => 'string', | |
| 74 | - ), | |
| 75 | - 'fontSizeTablet' => array( | |
| 76 | - 'type' => 'string', | |
| 77 | - 'default' => 'fs-tablet-100', | |
| 78 | - ), | |
| 79 | - 'fontSizeMobile' => array( | |
| 80 | - 'type' => 'string', | |
| 81 | - 'default' => 'fs-mobile-100', | |
| 82 | - ), | |
| 83 | - 'fontWeight' => array( | |
| 84 | - 'type' => 'string', | |
| 85 | - ), | |
| 86 | - 'fontStyle' => array( | |
| 87 | - 'type' => 'string', | |
| 88 | - ), | |
| 89 | - 'textTransform' => array( | |
| 90 | - 'type' => 'string', | |
| 91 | - ), | |
| 92 | - 'lineHeight' => array( | |
| 93 | - 'type' => 'string', | |
| 94 | - ), | |
| 95 | - 'letterSpacing' => array( | |
| 96 | - 'type' => 'string', | |
| 97 | - ), | |
| 98 | - | |
| 99 | - 'align' => array( | |
| 100 | - 'type' => 'string', | |
| 101 | - ), | |
| 102 | - 'textAlignment' => array( | |
| 103 | - 'type' => 'string', | |
| 104 | - ), | |
| 105 | - 'innerPadding' => array( | |
| 106 | - 'type' => 'string', | |
| 107 | - 'default' => 'default', | |
| 108 | - ), | |
| 109 | - 'innerSpacings' => array( | |
| 110 | - 'type' => 'string', | |
| 111 | - 'default' => 'none', | |
| 112 | - ), | |
| 113 | - | |
| 114 | - 'className' => array( | |
| 115 | - 'type' => 'string', | |
| 116 | - ), | |
| 117 | - ), | |
| 118 | - 'render_callback' => [ $this, 'render_callback' ] | |
| 119 | - ) | |
| 120 | - ); | |
| 121 | - | |
| 122 | - if ( $this->isEnabled() ) { | |
| 123 | - | |
| 124 | - add_filter( 'getwid/editor_blocks_js/dependencies', [ $this, 'block_editor_scripts'] ); | |
| 125 | - | |
| 126 | - //Register JS/CSS assets | |
| 127 | - wp_register_script( | |
| 128 | - 'jquery-plugin', | |
| 129 | - getwid_get_plugin_url( 'vendors/jquery.countdown/jquery.plugin.min.js' ), | |
| 130 | - [ 'jquery' ], | |
| 131 | - '1.0', | |
| 132 | - true | |
| 133 | - ); | |
| 134 | - | |
| 135 | - wp_register_script( | |
| 136 | - 'jquery-countdown', | |
| 137 | - getwid_get_plugin_url( 'vendors/jquery.countdown/jquery.countdown.min.js' ), | |
| 138 | - [ 'jquery', 'jquery-plugin' ], | |
| 139 | - '2.1.0', | |
| 140 | - true | |
| 141 | - ); | |
| 142 | - | |
| 143 | - preg_match( '/^(.*)_/', get_locale(), $current_locale ); | |
| 144 | - $locale_prefix = isset( $current_locale[ 1 ] ) && $current_locale[ 1 ] !='en' ? $current_locale[ 1 ] : ''; | |
| 145 | - | |
| 146 | - if ( $locale_prefix != '' ) { | |
| 147 | - $locale_path = 'vendors/jquery.countdown/localization/jquery.countdown-' . $locale_prefix . '.js'; | |
| 148 | - | |
| 149 | - if ( file_exists( getwid_get_plugin_path( $locale_path ) ) ) { | |
| 150 | - wp_register_script( | |
| 151 | - 'jquery-countdown-' . $locale_prefix, | |
| 152 | - getwid_get_plugin_url( $locale_path ), | |
| 153 | - [ 'jquery-countdown' ], | |
| 154 | - '2.1.0', | |
| 155 | - true | |
| 156 | - ); | |
| 157 | - } | |
| 158 | - } | |
| 159 | - } | |
| 160 | - } | |
| 161 | - | |
| 162 | - public function getLabel() { | |
| 163 | - return __('Countdown', 'getwid'); | |
| 164 | - } | |
| 165 | - | |
| 166 | - public function block_editor_scripts($scripts) { | |
| 167 | - | |
| 168 | - preg_match( '/^(.*)_/', get_locale(), $current_locale ); | |
| 169 | - $locale_prefix = isset( $current_locale[ 1 ] ) && $current_locale[ 1 ] !='en' ? $current_locale[ 1 ] : ''; | |
| 170 | - | |
| 171 | - if ( $locale_prefix != '' ) { | |
| 172 | - $locale_path = 'vendors/jquery.countdown/localization/jquery.countdown-' . $locale_prefix . '.js'; | |
| 173 | - | |
| 174 | - if ( file_exists( getwid_get_plugin_path( $locale_path ) ) ) { | |
| 175 | - wp_enqueue_script('jquery-countdown-' . $locale_prefix); | |
| 176 | - } | |
| 177 | - } | |
| 178 | - | |
| 179 | - //jquery.countdown.min.js | |
| 180 | - if ( ! in_array( 'jquery-countdown', $scripts ) ) { | |
| 181 | - array_push( $scripts, 'jquery-countdown' ); | |
| 182 | - } | |
| 183 | - | |
| 184 | - return $scripts; | |
| 185 | - } | |
| 186 | - | |
| 187 | - public function block_frontend_assets() { | |
| 188 | - | |
| 189 | - if ( is_admin() ) { | |
| 190 | - return; | |
| 191 | - } | |
| 192 | - | |
| 193 | - //jquery.countdown.min.js | |
| 194 | - if ( ! wp_script_is( 'jquery-countdown', 'enqueued' ) ) { | |
| 195 | - wp_enqueue_script('jquery-countdown'); | |
| 196 | - } | |
| 197 | - | |
| 198 | - preg_match( '/^(.*)_/', get_locale(), $current_locale ); | |
| 199 | - $locale_prefix = isset( $current_locale[ 1 ] ) && $current_locale[ 1 ] !='en' ? $current_locale[ 1 ] : ''; | |
| 200 | - | |
| 201 | - if ( $locale_prefix != '' ) { | |
| 202 | - $locale_path = 'vendors/jquery.countdown/localization/jquery.countdown-' . $locale_prefix . '.js'; | |
| 203 | - | |
| 204 | - if ( file_exists( getwid_get_plugin_path( $locale_path ) ) ) { | |
| 205 | - wp_enqueue_script('jquery-countdown-' . $locale_prefix); | |
| 206 | - } | |
| 207 | - } | |
| 208 | - | |
| 209 | - if ( FALSE == getwid()->assetsOptimization()->load_assets_on_demand() ) { | |
| 210 | - return; | |
| 211 | - } | |
| 212 | - | |
| 213 | - add_filter( 'getwid/optimize/assets', | |
| 214 | - function ( $assets ) { | |
| 215 | - $assets[] = getwid()->settings()->getPrefix() . '-blocks-common'; | |
| 216 | - | |
| 217 | - return $assets; | |
| 218 | - } | |
| 219 | - ); | |
| 220 | - | |
| 221 | - add_filter( 'getwid/optimize/should_load_common_css', '__return_true' ); | |
| 222 | - | |
| 223 | - $rtl = is_rtl() ? '.rtl' : ''; | |
| 224 | - | |
| 225 | - wp_enqueue_style( | |
| 226 | - self::$blockName, | |
| 227 | - getwid_get_plugin_url( 'assets/blocks/countdown/style' . $rtl . '.css' ), | |
| 228 | - [], | |
| 229 | - getwid()->settings()->getVersion() | |
| 230 | - ); | |
| 231 | - | |
| 232 | - wp_enqueue_script( | |
| 233 | - self::$blockName, | |
| 234 | - getwid_get_plugin_url( 'assets/blocks/countdown/frontend.js' ), | |
| 235 | - [ 'jquery', 'jquery-countdown' ], | |
| 236 | - getwid()->settings()->getVersion(), | |
| 237 | - true | |
| 238 | - ); | |
| 239 | - | |
| 240 | - } | |
| 241 | - | |
| 242 | - public function render_callback( $attributes, $content ) { | |
| 243 | - | |
| 244 | - if ( isset( $attributes['fontWeight'] ) && | |
| 245 | - ( $attributes['fontWeight'] == 'regular' || $attributes['fontWeight'] == 'normal') ) { | |
| 246 | - | |
| 247 | - $attributes['fontWeight'] = '400'; | |
| 248 | - } | |
| 249 | - | |
| 250 | - $should_load_gf = $this->shouldLoadGoogleFont( $attributes ); | |
| 251 | - | |
| 252 | - if ( $should_load_gf ) { | |
| 253 | - | |
| 254 | - $fontFamily = $attributes['fontFamily']; | |
| 255 | - | |
| 256 | - $fontFamilyHandle = strtolower( preg_replace( '/\s+/', '_', $fontFamily ) ); | |
| 257 | - | |
| 258 | - $fontWeight = ''; | |
| 259 | - $fontWeightHandle = ''; | |
| 260 | - $fontWeightPart = ''; | |
| 261 | - if ( isset( $attributes['fontWeight'] ) && $attributes['fontWeight'] != '400' ) { | |
| 262 | - $fontWeight = $attributes['fontWeight']; | |
| 263 | - $fontWeightHandle = '_' . $fontWeight; | |
| 264 | - $fontWeightPart = ':' . $fontWeight; | |
| 265 | - } | |
| 266 | - | |
| 267 | - wp_enqueue_style( | |
| 268 | - 'google-font-' . esc_attr( $fontFamilyHandle ) . esc_attr( $fontWeightHandle ), | |
| 269 | - 'https://fonts.googleapis.com/css?family=' . esc_attr( $fontFamily ) . esc_attr( $fontWeightPart ), | |
| 270 | - null, | |
| 271 | - 'all' | |
| 272 | - ); | |
| 273 | - } | |
| 274 | - | |
| 275 | - $block_name = 'wp-block-getwid-countdown'; | |
| 276 | - $class = $block_name; | |
| 277 | - | |
| 278 | - //Classes | |
| 279 | - if ( isset( $attributes['className'] ) ) { | |
| 280 | - $class .= ' ' . $attributes['className']; | |
| 281 | - } | |
| 282 | - if ( isset( $attributes['align'] ) ) { | |
| 283 | - $class .= ' align' . $attributes['align']; | |
| 284 | - } | |
| 285 | - if ( isset( $attributes['textAlignment'] ) ) { | |
| 286 | - $class .= ' has-horizontal-alignment-' . $attributes['textAlignment']; | |
| 287 | - } | |
| 288 | - | |
| 289 | - if ( isset( $attributes['innerPadding'] ) && $attributes['innerPadding'] != 'default' ) { | |
| 290 | - $class .= ' has-inner-paddings-' . $attributes['innerPadding']; | |
| 291 | - } | |
| 292 | - if ( isset( $attributes['innerSpacings'] ) && $attributes['innerSpacings'] != 'none' ) { | |
| 293 | - $class .= ' has-spacing-' . $attributes['innerSpacings']; | |
| 294 | - } | |
| 295 | - | |
| 296 | - $wrapper_class = $block_name . '__content'; | |
| 297 | - $content_class = $block_name . '__wrapper'; | |
| 298 | - | |
| 299 | - if ( isset( $attributes['fontSizeTablet'] ) && $attributes['fontSizeTablet'] != 'fs-tablet-100' ) { | |
| 300 | - $content_class .= ' ' . $attributes['fontSizeTablet']; | |
| 301 | - } | |
| 302 | - if ( isset( $attributes['fontSizeMobile'] ) && $attributes['fontSizeMobile'] != 'fs-mobile-100' ) { | |
| 303 | - $content_class .= ' ' . $attributes['fontSizeMobile']; | |
| 304 | - } | |
| 305 | - if ( isset( $attributes['fontSize'] ) && $attributes['fontSize'] != '' ) { | |
| 306 | - $content_class .= ' has-custom-font-size'; | |
| 307 | - } | |
| 308 | - | |
| 309 | - $content_style = ''; | |
| 310 | - //Style | |
| 311 | - if ( isset( $attributes['fontSize'] ) ) { | |
| 312 | - $content_style .= 'font-size: ' . $attributes['fontSize'] . ';'; | |
| 313 | - } | |
| 314 | - | |
| 315 | - if ( isset( $attributes['fontFamily'] ) && $attributes['fontFamily'] != '' ) { | |
| 316 | - $content_style .= 'font-family: ' . $attributes['fontFamily'] . ';'; | |
| 317 | - } | |
| 318 | - if ( isset( $attributes['fontWeight'] ) ) { | |
| 319 | - $content_style .= 'font-weight: ' . $attributes['fontWeight'] . ';'; | |
| 320 | - } | |
| 321 | - if ( isset( $attributes['fontStyle'] ) ) { | |
| 322 | - $content_style .= 'font-style: ' . $attributes['fontStyle'] . ';'; | |
| 323 | - } | |
| 324 | - if ( isset( $attributes['textTransform'] ) && $attributes['textTransform'] != 'default' ) { | |
| 325 | - $content_style .= 'text-transform: ' . $attributes['textTransform'] . ';'; | |
| 326 | - } | |
| 327 | - if ( isset( $attributes['lineHeight'] ) ) { | |
| 328 | - $content_style .= 'line-height: ' . $attributes['lineHeight'] . ';'; | |
| 329 | - } | |
| 330 | - if ( isset( $attributes['letterSpacing'] ) ) { | |
| 331 | - $content_style .= 'letter-spacing: ' . $attributes['letterSpacing'] . ';'; | |
| 332 | - } | |
| 333 | - | |
| 334 | - $is_back_end = getwid_is_block_editor(); | |
| 335 | - | |
| 336 | - //Color style & class | |
| 337 | - getwid_custom_color_style_and_class( $content_style, $content_class, $attributes, 'color', $is_back_end ); | |
| 338 | - | |
| 339 | - try { | |
| 340 | - $target_date = new \DateTime( $attributes['dateTime'] ); | |
| 341 | - } catch ( \Exception $e ) { | |
| 342 | - return esc_html__( 'Invalid date.', 'getwid' ); | |
| 343 | - } | |
| 344 | - | |
| 345 | - $current_date = new \DateTime(current_time('Y-m-d H:i:s')); //Server time | |
| 346 | - | |
| 347 | - if ( $current_date < $target_date ) { | |
| 348 | - $dateTime_until = $current_date->diff( $target_date )->format( "+%yy +%mo +%dd +%hh +%im +%ss" ); | |
| 349 | - } else { | |
| 350 | - $dateTime_until = 'negative'; | |
| 351 | - } | |
| 352 | - | |
| 353 | - $countdown_options = array( | |
| 354 | - ( ! empty( $attributes['backgroundColor'] ) ? 'data-bg-color="' . esc_attr( $attributes['backgroundColor'] ) . '"' : '' ), | |
| 355 | - ( ! empty( $attributes['years'] ) ? 'data-years="' . esc_attr( $attributes['years'] ) . '"' : '' ), | |
| 356 | - ( ! empty( $attributes['months'] ) ? 'data-months="' . esc_attr( $attributes['months'] ) . '"' : '' ), | |
| 357 | - ( ! empty( $attributes['weeks'] ) ? 'data-weeks="' . esc_attr( $attributes['weeks'] ) . '"' : '' ), | |
| 358 | - ( ! empty( $attributes['days'] ) ? 'data-days="' . esc_attr( $attributes['days'] ) . '"' : '' ), | |
| 359 | - ( ! empty( $attributes['hours'] ) ? 'data-hours="' . esc_attr( $attributes['hours'] ) . '"' : '' ), | |
| 360 | - ( ! empty( $attributes['minutes'] ) ? 'data-minutes="' . esc_attr( $attributes['minutes'] ) . '"' : '' ), | |
| 361 | - ( ! empty( $attributes['seconds'] ) ? 'data-seconds="' . esc_attr( $attributes['seconds'] ) . '"' : '' ), | |
| 362 | - ); | |
| 363 | - | |
| 364 | - $countdown_options_str = implode( ' ', $countdown_options ); | |
| 365 | - | |
| 366 | - ob_start(); | |
| 367 | - ?> | |
| 368 | - | |
| 369 | - <div class="<?php echo esc_attr( $class ); ?>"> | |
| 370 | - <div class="<?php echo esc_attr( $content_class ); ?>" <?php if ( ! empty( $content_style ) ) { ?> style="<?php echo esc_attr( $content_style ); ?>"<?php } ?>> | |
| 371 | - <div class="<?php echo esc_attr( $wrapper_class ); ?>" | |
| 372 | - data-datetime="<?php echo esc_attr( !empty( $dateTime_until ) ? $dateTime_until : '' ); ?>" <?php echo $countdown_options_str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> | |
| 373 | - </div> | |
| 374 | - </div> | |
| 375 | - </div> | |
| 376 | - | |
| 377 | - <?php | |
| 378 | - $result = ob_get_clean(); | |
| 379 | - | |
| 380 | - $this->block_frontend_assets(); | |
| 381 | - | |
| 382 | - return $result; | |
| 383 | - } | |
| 384 | - | |
| 385 | - private function shouldLoadGoogleFont( $attributes ) { | |
| 386 | - $should_load = false; | |
| 387 | - | |
| 388 | - // if fontFamily set maybe GF should be loaded | |
| 389 | - if ( isset( $attributes['fontFamily'] ) && !empty( $attributes['fontFamily'] ) ) { | |
| 390 | - $should_load = true; | |
| 391 | - } | |
| 392 | - | |
| 393 | - // if fontFamily isset (condition above) check fontGroupID | |
| 394 | - // if fontGroupID isset but not equal to 'google-fonts' or ''(for old plugin versions) it shouldn't be loaded | |
| 395 | - if ( $should_load && isset( $attributes['fontGroupID'] ) && !in_array( $attributes['fontGroupID'], ['', 'google-fonts'] ) ) { | |
| 396 | - $should_load = false; | |
| 397 | - } | |
| 398 | - | |
| 399 | - return $should_load; | |
| 400 | - } | |
| 401 | -} | |
| 402 | - | |
| 403 | -getwid()->blocksManager()->addBlock( | |
| 404 | - new \Getwid\Blocks\Countdown() | |
| 405 | -); | |
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace Getwid\Blocks; | |
| 4 | + | |
| 5 | +class Countdown extends AbstractBlock { | |
| 6 | + | |
| 7 | + public function __construct() { | |
| 8 | + | |
| 9 | + parent::__construct( 'getwid/countdown' ); | |
| 10 | + | |
| 11 | + $this->register_vendor_scripts(); | |
| 12 | + | |
| 13 | + add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) ); | |
| 14 | + | |
| 15 | + register_block_type( | |
| 16 | + getwid_get_plugin_path( 'assets/blocks/countdown' ), | |
| 17 | + array( | |
| 18 | + 'render_callback' => array( $this, 'render_callback' ), | |
| 19 | + ) | |
| 20 | + ); | |
| 21 | + } | |
| 22 | + | |
| 23 | + public function get_label() { | |
| 24 | + return __( 'Countdown', 'getwid' ); | |
| 25 | + } | |
| 26 | + | |
| 27 | + private function get_default_date() { | |
| 28 | + | |
| 29 | + $current_date = new \DateTime( current_time( 'Y-m-d H:i:s' ) ); | |
| 30 | + $current_date->add( new \DateInterval( 'P1D' ) ); | |
| 31 | + | |
| 32 | + return $current_date->format( 'Y-m-d H:i:s' ); | |
| 33 | + } | |
| 34 | + | |
| 35 | + | |
| 36 | + private function get_locale_prefix() { | |
| 37 | + | |
| 38 | + preg_match( '/^(.*)_/', get_locale(), $current_locale ); | |
| 39 | + | |
| 40 | + return isset( $current_locale[1] ) && 'en' !== $current_locale[1] ? $current_locale[1] : ''; | |
| 41 | + } | |
| 42 | + | |
| 43 | + private function register_vendor_scripts() { | |
| 44 | + | |
| 45 | + wp_register_script( | |
| 46 | + 'jquery-plugin', | |
| 47 | + getwid_get_plugin_url( 'vendors/jquery.countdown/jquery.plugin.min.js' ), | |
| 48 | + array( 'jquery' ), | |
| 49 | + '1.0', | |
| 50 | + true | |
| 51 | + ); | |
| 52 | + | |
| 53 | + wp_register_script( | |
| 54 | + 'jquery-countdown', | |
| 55 | + getwid_get_plugin_url( 'vendors/jquery.countdown/jquery.countdown.min.js' ), | |
| 56 | + array( 'jquery', 'jquery-plugin' ), | |
| 57 | + '2.1.0', | |
| 58 | + true | |
| 59 | + ); | |
| 60 | + | |
| 61 | + $locale_prefix = $this->get_locale_prefix(); | |
| 62 | + | |
| 63 | + if ( '' !== $locale_prefix ) { | |
| 64 | + $locale_path = 'vendors/jquery.countdown/localization/jquery.countdown-' . $locale_prefix . '.js'; | |
| 65 | + | |
| 66 | + if ( file_exists( getwid_get_plugin_path( $locale_path ) ) ) { | |
| 67 | + wp_register_script( | |
| 68 | + 'jquery-countdown-' . $locale_prefix, | |
| 69 | + getwid_get_plugin_url( $locale_path ), | |
| 70 | + array( 'jquery-countdown' ), | |
| 71 | + '2.1.0', | |
| 72 | + true | |
| 73 | + ); | |
| 74 | + } | |
| 75 | + } | |
| 76 | + } | |
| 77 | + | |
| 78 | + public function enqueue_editor_assets() { | |
| 79 | + | |
| 80 | + wp_enqueue_script( 'jquery-countdown' ); | |
| 81 | + | |
| 82 | + $locale_prefix = $this->get_locale_prefix(); | |
| 83 | + | |
| 84 | + if ( '' !== $locale_prefix ) { | |
| 85 | + $locale_path = 'vendors/jquery.countdown/localization/jquery.countdown-' . $locale_prefix . '.js'; | |
| 86 | + | |
| 87 | + if ( file_exists( getwid_get_plugin_path( $locale_path ) ) ) { | |
| 88 | + wp_enqueue_script( 'jquery-countdown-' . $locale_prefix ); | |
| 89 | + } | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 93 | + public function block_frontend_assets() { | |
| 94 | + | |
| 95 | + if ( is_admin() ) { | |
| 96 | + return; | |
| 97 | + } | |
| 98 | + | |
| 99 | + wp_enqueue_script( 'jquery-countdown' ); | |
| 100 | + | |
| 101 | + $locale_prefix = $this->get_locale_prefix(); | |
| 102 | + | |
| 103 | + if ( '' !== $locale_prefix ) { | |
| 104 | + $locale_path = 'vendors/jquery.countdown/localization/jquery.countdown-' . $locale_prefix . '.js'; | |
| 105 | + | |
| 106 | + if ( file_exists( getwid_get_plugin_path( $locale_path ) ) ) { | |
| 107 | + wp_enqueue_script( 'jquery-countdown-' . $locale_prefix ); | |
| 108 | + } | |
| 109 | + } | |
| 110 | + } | |
| 111 | + | |
| 112 | + public function render_callback( $attributes, $content ) { | |
| 113 | + | |
| 114 | + $attributes = wp_parse_args( | |
| 115 | + $attributes, | |
| 116 | + array( | |
| 117 | + 'dateTime' => $this->get_default_date(), | |
| 118 | + 'years' => false, | |
| 119 | + 'months' => false, | |
| 120 | + 'weeks' => false, | |
| 121 | + 'days' => true, | |
| 122 | + 'hours' => true, | |
| 123 | + 'minutes' => true, | |
| 124 | + 'seconds' => true, | |
| 125 | + 'fontGroupID' => '', | |
| 126 | + 'fontFamily' => '', | |
| 127 | + 'fontSizeTablet' => 'fs-tablet-100', | |
| 128 | + 'fontSizeMobile' => 'fs-mobile-100', | |
| 129 | + 'innerPadding' => 'default', | |
| 130 | + 'innerSpacings' => 'none', | |
| 131 | + ) | |
| 132 | + ); | |
| 133 | + | |
| 134 | + if ( | |
| 135 | + isset( $attributes['fontWeight'] ) && | |
| 136 | + ( 'regular' === $attributes['fontWeight'] || 'normal' === $attributes['fontWeight'] ) | |
| 137 | + ) { | |
| 138 | + $attributes['fontWeight'] = '400'; | |
| 139 | + } | |
| 140 | + | |
| 141 | + if ( $this->should_load_google_font( $attributes ) ) { | |
| 142 | + $font_family = $attributes['fontFamily']; | |
| 143 | + $font_family_handle = strtolower( preg_replace( '/\s+/', '_', $font_family ) ); | |
| 144 | + $font_weight = ''; | |
| 145 | + $font_weight_handle = ''; | |
| 146 | + $font_weight_part = ''; | |
| 147 | + | |
| 148 | + if ( isset( $attributes['fontWeight'] ) && '400' !== $attributes['fontWeight'] ) { | |
| 149 | + $font_weight = $attributes['fontWeight']; | |
| 150 | + $font_weight_handle = '_' . $font_weight; | |
| 151 | + $font_weight_part = ':' . $font_weight; | |
| 152 | + } | |
| 153 | + | |
| 154 | + wp_enqueue_style( | |
| 155 | + 'google-font-' . esc_attr( $font_family_handle ) . esc_attr( $font_weight_handle ), | |
| 156 | + 'https://fonts.googleapis.com/css?family=' . esc_attr( $font_family ) . esc_attr( $font_weight_part ), | |
| 157 | + null, | |
| 158 | + 'all' | |
| 159 | + ); | |
| 160 | + } | |
| 161 | + | |
| 162 | + $block_name = 'wp-block-getwid-countdown'; | |
| 163 | + $class = $block_name; | |
| 164 | + | |
| 165 | + if ( isset( $attributes['className'] ) ) { | |
| 166 | + $class .= ' ' . $attributes['className']; | |
| 167 | + } | |
| 168 | + if ( isset( $attributes['align'] ) ) { | |
| 169 | + $class .= ' align' . $attributes['align']; | |
| 170 | + } | |
| 171 | + if ( isset( $attributes['textAlignment'] ) ) { | |
| 172 | + $class .= ' has-horizontal-alignment-' . $attributes['textAlignment']; | |
| 173 | + } | |
| 174 | + if ( isset( $attributes['innerPadding'] ) && 'default' !== $attributes['innerPadding'] ) { | |
| 175 | + $class .= ' has-inner-paddings-' . $attributes['innerPadding']; | |
| 176 | + } | |
| 177 | + if ( isset( $attributes['innerSpacings'] ) && 'none' !== $attributes['innerSpacings'] ) { | |
| 178 | + $class .= ' has-spacing-' . $attributes['innerSpacings']; | |
| 179 | + } | |
| 180 | + | |
| 181 | + $wrapper_class = $block_name . '__content'; | |
| 182 | + $content_class = $block_name . '__wrapper'; | |
| 183 | + | |
| 184 | + if ( isset( $attributes['fontSizeTablet'] ) && 'fs-tablet-100' !== $attributes['fontSizeTablet'] ) { | |
| 185 | + $content_class .= ' ' . $attributes['fontSizeTablet']; | |
| 186 | + } | |
| 187 | + if ( isset( $attributes['fontSizeMobile'] ) && 'fs-mobile-100' !== $attributes['fontSizeMobile'] ) { | |
| 188 | + $content_class .= ' ' . $attributes['fontSizeMobile']; | |
| 189 | + } | |
| 190 | + if ( isset( $attributes['fontSize'] ) && '' !== $attributes['fontSize'] ) { | |
| 191 | + $content_class .= ' has-custom-font-size'; | |
| 192 | + } | |
| 193 | + | |
| 194 | + $content_style = ''; | |
| 195 | + | |
| 196 | + if ( isset( $attributes['fontSize'] ) ) { | |
| 197 | + $content_style .= 'font-size: ' . $attributes['fontSize'] . ';'; | |
| 198 | + } | |
| 199 | + if ( isset( $attributes['fontFamily'] ) && '' !== $attributes['fontFamily'] ) { | |
| 200 | + $content_style .= 'font-family: ' . $attributes['fontFamily'] . ';'; | |
| 201 | + } | |
| 202 | + if ( isset( $attributes['fontWeight'] ) ) { | |
| 203 | + $content_style .= 'font-weight: ' . $attributes['fontWeight'] . ';'; | |
| 204 | + } | |
| 205 | + if ( isset( $attributes['fontStyle'] ) ) { | |
| 206 | + $content_style .= 'font-style: ' . $attributes['fontStyle'] . ';'; | |
| 207 | + } | |
| 208 | + if ( isset( $attributes['textTransform'] ) && 'default' !== $attributes['textTransform'] ) { | |
| 209 | + $content_style .= 'text-transform: ' . $attributes['textTransform'] . ';'; | |
| 210 | + } | |
| 211 | + if ( isset( $attributes['lineHeight'] ) ) { | |
| 212 | + $content_style .= 'line-height: ' . $attributes['lineHeight'] . ';'; | |
| 213 | + } | |
| 214 | + if ( isset( $attributes['letterSpacing'] ) ) { | |
| 215 | + $content_style .= 'letter-spacing: ' . $attributes['letterSpacing'] . ';'; | |
| 216 | + } | |
| 217 | + | |
| 218 | + getwid_custom_color_style_and_class( | |
| 219 | + $content_style, | |
| 220 | + $content_class, | |
| 221 | + $attributes, | |
| 222 | + 'color', | |
| 223 | + getwid_is_block_editor() | |
| 224 | + ); | |
| 225 | + | |
| 226 | + try { | |
| 227 | + $target_date = new \DateTime( $attributes['dateTime'] ); | |
| 228 | + } catch ( \Exception $e ) { | |
| 229 | + return esc_html__( 'Invalid date.', 'getwid' ); | |
| 230 | + } | |
| 231 | + | |
| 232 | + $current_date = new \DateTime( current_time( 'Y-m-d H:i:s' ) ); | |
| 233 | + | |
| 234 | + if ( $current_date < $target_date ) { | |
| 235 | + $date_time_until = $current_date->diff( $target_date )->format( '+%yy +%mo +%dd +%hh +%im +%ss' ); | |
| 236 | + } else { | |
| 237 | + $date_time_until = 'negative'; | |
| 238 | + } | |
| 239 | + | |
| 240 | + $countdown_options = array( | |
| 241 | + ! empty( $attributes['backgroundColor'] ) ? 'data-bg-color="' . esc_attr( $attributes['backgroundColor'] ) . '"' : '', | |
| 242 | + ! empty( $attributes['years'] ) ? 'data-years="' . esc_attr( $attributes['years'] ) . '"' : '', | |
| 243 | + ! empty( $attributes['months'] ) ? 'data-months="' . esc_attr( $attributes['months'] ) . '"' : '', | |
| 244 | + ! empty( $attributes['weeks'] ) ? 'data-weeks="' . esc_attr( $attributes['weeks'] ) . '"' : '', | |
| 245 | + ! empty( $attributes['days'] ) ? 'data-days="' . esc_attr( $attributes['days'] ) . '"' : '', | |
| 246 | + ! empty( $attributes['hours'] ) ? 'data-hours="' . esc_attr( $attributes['hours'] ) . '"' : '', | |
| 247 | + ! empty( $attributes['minutes'] ) ? 'data-minutes="' . esc_attr( $attributes['minutes'] ) . '"' : '', | |
| 248 | + ! empty( $attributes['seconds'] ) ? 'data-seconds="' . esc_attr( $attributes['seconds'] ) . '"' : '', | |
| 249 | + ); | |
| 250 | + | |
| 251 | + $countdown_options_str = implode( ' ', $countdown_options ); | |
| 252 | + | |
| 253 | + ob_start(); | |
| 254 | + ?> | |
| 255 | + <div class="<?php echo esc_attr( $class ); ?>"> | |
| 256 | + <div class="<?php echo esc_attr( $content_class ); ?>" | |
| 257 | + <?php | |
| 258 | + if ( ! empty( $content_style ) ) { | |
| 259 | + ?> | |
| 260 | + style="<?php echo esc_attr( $content_style ); ?>"<?php } ?>> | |
| 261 | + <div class="<?php echo esc_attr( $wrapper_class ); ?>" | |
| 262 | + data-datetime="<?php echo esc_attr( ! empty( $date_time_until ) ? $date_time_until : '' ); ?>" <?php echo $countdown_options_str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>> | |
| 263 | + </div> | |
| 264 | + </div> | |
| 265 | + </div> | |
| 266 | + <?php | |
| 267 | + $result = ob_get_clean(); | |
| 268 | + | |
| 269 | + $this->block_frontend_assets(); | |
| 270 | + | |
| 271 | + return $result; | |
| 272 | + } | |
| 273 | + | |
| 274 | + private function should_load_google_font( $attributes ) { | |
| 275 | + | |
| 276 | + $should_load = false; | |
| 277 | + | |
| 278 | + if ( isset( $attributes['fontFamily'] ) && ! empty( $attributes['fontFamily'] ) ) { | |
| 279 | + $should_load = true; | |
| 280 | + } | |
| 281 | + | |
| 282 | + if ( | |
| 283 | + $should_load && | |
| 284 | + isset( $attributes['fontGroupID'] ) && | |
| 285 | + ! in_array( $attributes['fontGroupID'], array( '', 'google-fonts' ), true ) | |
| 286 | + ) { | |
| 287 | + $should_load = false; | |
| 288 | + } | |
| 289 | + | |
| 290 | + return $should_load; | |
| 291 | + } | |
| 292 | +} | |
| 293 | + | |
| 294 | +getwid()->blocksManager()->addBlock( | |
| 295 | + new Countdown() | |
| 296 | +); | |