| 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 |
); |
| 406 |
|