| 1 |
<?php |
| 2 |
class BBlocksCountdown extends BBlocks{ |
| 3 |
public function __construct(){ |
| 4 |
add_action( 'init', [$this, 'register'] ); |
| 5 |
} |
| 6 |
|
| 7 |
function register(){ |
| 8 |
$this::registerScripts( 'countdown', array( |
| 9 |
'render_callback' => [$this, 'render'] |
| 10 |
) ); |
| 11 |
} |
| 12 |
|
| 13 |
function render( $attributes ){ |
| 14 |
extract( $attributes ); |
| 15 |
$align = $align ?? ''; |
| 16 |
$cId = $cId ?? ''; |
| 17 |
$layout = $layout ?? 'default'; |
| 18 |
$boxIsInline = $boxIsInline ?? false; |
| 19 |
$boxPosition = $boxPosition ?? 'center'; |
| 20 |
$isLabels = $isLabels ?? true; |
| 21 |
$isDays = $isDays ?? true; |
| 22 |
$daysLabel = $daysLabel ?? 'Days'; |
| 23 |
$isHours = $isHours ?? true; |
| 24 |
$hoursLabel = $hoursLabel ?? 'Hours'; |
| 25 |
$isMinutes = $isMinutes ?? true; |
| 26 |
$minutesLabel = $minutesLabel ?? 'Minutes'; |
| 27 |
$isSeconds = $isSeconds ?? true; |
| 28 |
$secondsLabel = $secondsLabel ?? 'Seconds'; |
| 29 |
$width = $width ?? '0px'; |
| 30 |
$background = $background ?? array( 'color' => '#0000' ); |
| 31 |
$padding = $padding ?? array( 'vertical' => '10px', 'horizontal' => '15px' ); |
| 32 |
$shadow = $shadow ?? array(); |
| 33 |
$alignment = $alignment ?? 'center'; |
| 34 |
$boxBG = $boxBG ?? array( 'type' => 'gradient', 'color' => '#4527a4', 'gradient' => 'linear-gradient(135deg, #4527a4, #8344c5)' ); |
| 35 |
$boxWidth = $boxWidth ?? '170px'; |
| 36 |
$boxHeight = $boxHeight ?? '120px'; |
| 37 |
$boxSpace = $boxSpace ?? '30px'; |
| 38 |
$boxBorder = $boxBorder ?? array( 'radius' => '5%' ); |
| 39 |
$boxShadow = $boxShadow ?? array(); |
| 40 |
$digitTypo = $digitTypo ?? array( 'fontSize' => 48 ); |
| 41 |
$digitColor = $digitColor ?? '#fff'; |
| 42 |
$labelTypo = $labelTypo ?? array( 'fontSize' => 18 ); |
| 43 |
$labelColor = $labelColor ?? '#fff'; |
| 44 |
$isSep = $isSep ?? false; |
| 45 |
$sepType = $sepType ?? ':'; |
| 46 |
$sepSize = $sepSize ?? 52; |
| 47 |
$sepColor = $sepColor ?? '#4527a4'; |
| 48 |
|
| 49 |
// Generate Styles |
| 50 |
$countdownStyle = new BBlocksStyleGenerator(); |
| 51 |
$countdownStyle::addStyle( "#bBlocksCountdown-$cId", array( |
| 52 |
'align-items' => $alignment, |
| 53 |
'justify-content' => $alignment |
| 54 |
) ); |
| 55 |
$countdownStyle::addStyle( "#bBlocksCountdown-$cId .countdownItems", array( |
| 56 |
'width' => '0px' === $width || '0%' === $width || '0em' === $width ? 'auto' : $width, |
| 57 |
$background['styles'] ?? 'background-color: #0000;' => '', |
| 58 |
'padding' => $padding['styles'] ?? '10px 15px', |
| 59 |
'box-shadow' => $shadow['styles'] ?? 'none', |
| 60 |
'justify-content' => $boxPosition |
| 61 |
) ); |
| 62 |
$countdownStyle::addStyle( "#bBlocksCountdown-$cId .countdownItems .countdownItem", array( |
| 63 |
$boxBG['styles'] ?? 'background-image: linear-gradient(135deg, #4527a4, #8344c5);' => '', |
| 64 |
'width' => $boxWidth, |
| 65 |
'height' => $boxHeight, |
| 66 |
'margin-left' => "calc($boxSpace / 2)", |
| 67 |
'margin-right' => "calc($boxSpace / 2)", |
| 68 |
$boxBorder['styles'] ?? 'border-radius: 5%;' => '', |
| 69 |
'box-shadow' => $boxShadow['styles'] ?? '0 0 0 0 #0000' |
| 70 |
) ); |
| 71 |
$countdownStyle::addStyle( "#bBlocksCountdown-$cId .countdownItems .separator::before", array( |
| 72 |
'content' => "'$sepType'", |
| 73 |
'font-size' => $sepSize . 'px', |
| 74 |
'color' => $sepColor |
| 75 |
) ); |
| 76 |
$countdownStyle::addStyle( "#bBlocksCountdown-$cId .countdownItems .countdownItem .digit", array( |
| 77 |
'color' => $digitColor, |
| 78 |
$digitTypo['styles'] ?? 'font-size: 48px;' => '' |
| 79 |
) ); |
| 80 |
$countdownStyle::addStyle( "#bBlocksCountdown-$cId .countdownItems .countdownItem .label", array( |
| 81 |
'color' => $labelColor, |
| 82 |
$labelTypo['styles'] ?? 'font-size: 18px;' => '' |
| 83 |
) ); |
| 84 |
|
| 85 |
$boxClass = $boxIsInline ? 'inline' : ''; |
| 86 |
|
| 87 |
ob_start(); ?> |
| 88 |
<div class='wp-block-b-blocks-countdown <?php echo 'align' . esc_attr( $align ); ?>' id='bBlocksCountdown-<?php echo esc_attr( $cId ); ?>' data-date="<?php echo esc_attr( json_encode( array( 'destDate' => $destDate ?? '' ) ) ); ?>"> |
| 89 |
<style> |
| 90 |
@import url(<?php echo esc_url( $digitTypo['googleFontLink'] ?? '' ); ?>); |
| 91 |
@import url(<?php echo esc_url( $labelTypo['googleFontLink'] ?? '' ); ?>); |
| 92 |
<?php echo wp_kses( $countdownStyle::renderStyle(), array() ); ?> |
| 93 |
@media (max-width: 575px) { #bBlocksCountdown-<?php echo esc_html( $cId ); ?> .countdownItems .countdownItem{ |
| 94 |
margin-top: calc(<?php echo esc_html( $boxSpace ); ?> / 2); |
| 95 |
margin-bottom: calc(<?php echo esc_html( $boxSpace ); ?> / 2); |
| 96 |
} } |
| 97 |
</style> |
| 98 |
|
| 99 |
<div class='countdownItems'> |
| 100 |
<?php $this->box( $isDays, "countdownDays $boxClass", $isLabels, $daysLabel ); ?> |
| 101 |
<?php echo $isSep && $isDays && ( $isHours || $isMinutes || $isSeconds ) ? "<span class='separator'></span>" : ''; ?> |
| 102 |
<?php $this->box( $isHours, "countdownHours $boxClass", $isLabels, $hoursLabel ); ?> |
| 103 |
<?php echo $isSep && $isHours && ( $isMinutes || $isSeconds ) ? "<span class='separator'></span>" : ''; ?> |
| 104 |
<?php $this->box( $isMinutes, "countdownMinutes $boxClass", $isLabels, $minutesLabel ); ?> |
| 105 |
<?php echo $isSep && $isMinutes && $isSeconds ? "<span class='separator'></span>" : ''; ?> |
| 106 |
<?php $this->box( $isSeconds, "countdownSeconds $boxClass", $isLabels, $secondsLabel ); ?> |
| 107 |
</div> |
| 108 |
</div> |
| 109 |
|
| 110 |
<?php $countdownStyle::$styles = array(); // Empty styles |
| 111 |
return ob_get_clean(); |
| 112 |
} |
| 113 |
|
| 114 |
function box( $is, $boxClass, $isLabels, $label ){ |
| 115 |
if( $is ){ ?><div class='countdownItem <?php echo esc_attr( $boxClass ); ?>'> |
| 116 |
<span class='digit'>00</span> |
| 117 |
<?php echo $isLabels ? "<span class='label'>". wp_kses_post( $label ) ."</span>" : ''; ?> |
| 118 |
</div><?php } |
| 119 |
} // Box |
| 120 |
} |
| 121 |
new BBlocksCountdown(); |