| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Countdown Time |
| 4 |
* Description: Display your events date into a timer to your visitor with countdown time block |
| 5 |
* Version: 1.0.7 |
| 6 |
* Author: bPlugins LLC |
| 7 |
* Author URI: http://bplugins.com |
| 8 |
* License: GPLv3 |
| 9 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt |
| 10 |
* Text Domain: countdown-time |
| 11 |
*/ |
| 12 |
|
| 13 |
// ABS PATH |
| 14 |
if ( !defined( 'ABSPATH' ) ) { exit; } |
| 15 |
|
| 16 |
// Constant |
| 17 |
define( 'CTB_PLUGIN_VERSION', 'localhost' === $_SERVER['HTTP_HOST'] ? time() : '1.0.7' ); |
| 18 |
define( 'CTB_ASSETS_DIR', plugin_dir_url( __FILE__ ) . 'assets/' ); |
| 19 |
|
| 20 |
// Generate Styles |
| 21 |
class CTBStyleGenerator { |
| 22 |
public static $styles = []; |
| 23 |
public static function addStyle( $selector, $styles ){ |
| 24 |
if( array_key_exists( $selector, self::$styles ) ){ |
| 25 |
self::$styles[$selector] = wp_parse_args( self::$styles[$selector], $styles ); |
| 26 |
}else { self::$styles[$selector] = $styles; } |
| 27 |
} |
| 28 |
public static function renderStyle(){ |
| 29 |
$output = ''; |
| 30 |
foreach( self::$styles as $selector => $style ){ |
| 31 |
$new = ''; |
| 32 |
foreach( $style as $property => $value ){ |
| 33 |
if( $value == '' ){ $new .= $property; }else { $new .= " $property: $value;"; } |
| 34 |
} |
| 35 |
$output .= "$selector { $new }"; |
| 36 |
} |
| 37 |
return $output; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
// Countdown Time |
| 42 |
class CTBCountdownTimeBlock{ |
| 43 |
protected static $_instance = null; |
| 44 |
|
| 45 |
function __construct(){ |
| 46 |
add_action( 'init', [$this, 'register'] ); |
| 47 |
} |
| 48 |
|
| 49 |
public static function instance(){ |
| 50 |
if( self::$_instance === null ){ |
| 51 |
self::$_instance = new self(); |
| 52 |
} |
| 53 |
return self::$_instance; |
| 54 |
} |
| 55 |
|
| 56 |
function register() { |
| 57 |
wp_register_style( 'ctb-countdown-time-editor-style', plugins_url( 'dist/editor.css', __FILE__ ), [ 'wp-edit-blocks' ], CTB_PLUGIN_VERSION ); // Backend Style |
| 58 |
wp_register_style( 'ctb-countdown-time-style', plugins_url( 'dist/style.css', __FILE__ ), [ 'wp-editor' ], CTB_PLUGIN_VERSION ); // Frontend Style |
| 59 |
|
| 60 |
register_block_type( __DIR__, [ |
| 61 |
'editor_style' => 'ctb-countdown-time-editor-style', |
| 62 |
'style' => 'ctb-countdown-time-style', |
| 63 |
'render_callback' => [$this, 'render'] |
| 64 |
] ); // Register Block |
| 65 |
|
| 66 |
wp_set_script_translations( 'ctb-countdown-time-editor-script', 'countdown-time', plugin_dir_path( __FILE__ ) . 'languages' ); // Translate |
| 67 |
} |
| 68 |
|
| 69 |
function render( $attributes ){ |
| 70 |
extract( $attributes ); |
| 71 |
|
| 72 |
$countdownStyle = new CTBStyleGenerator(); // Generate Styles |
| 73 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId", [ |
| 74 |
'align-items' => $alignment, |
| 75 |
'justify-content' => $alignment |
| 76 |
] ); |
| 77 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems", [ |
| 78 |
'width' => '0px' === $width || '0%' === $width || '0em' === $width ? 'auto' : $width, |
| 79 |
$background['styles'] ?? 'background-color: #0000;' => '', |
| 80 |
'padding' => $padding['styles'] ?? '10px 15px', |
| 81 |
'box-shadow' => $shadow['styles'] ?? 'none', |
| 82 |
'justify-content' => $boxPosition |
| 83 |
] ); |
| 84 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .countdownItem", [ |
| 85 |
$boxBG['styles'] ?? 'background-image: linear-gradient(135deg, #4527a4, #8344c5);' => '', |
| 86 |
'width' => $boxWidth, |
| 87 |
'height' => $boxHeight, |
| 88 |
'margin-left' => "calc( $boxSpace / 2 )", |
| 89 |
'margin-right' => "calc( $boxSpace / 2 )", |
| 90 |
$boxBorder['styles'] ?? 'border-radius: 5%;' => '', |
| 91 |
'box-shadow' => $boxShadow['styles'] ?? '0 0 0 0 #0000' |
| 92 |
] ); |
| 93 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .separator::before", [ |
| 94 |
'content' => "'$sepType'", |
| 95 |
'font-size' => $sepSize . 'px', |
| 96 |
'color' => $sepColor |
| 97 |
] ); |
| 98 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .countdownItem .digit", [ |
| 99 |
'color' => $digitColor, |
| 100 |
$digitTypo['styles'] ?? 'font-size: 48px;' => '' |
| 101 |
] ); |
| 102 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .countdownItem .label", [ |
| 103 |
'color' => $labelColor, |
| 104 |
$labelTypo['styles'] ?? 'font-size: 18px;' => '' |
| 105 |
] ); |
| 106 |
|
| 107 |
$boxClass = $boxIsInline ? 'inline' : ''; |
| 108 |
|
| 109 |
ob_start(); ?> |
| 110 |
<div class='wp-block-ctb-countdown-time <?php echo 'align' . esc_attr( $align ); ?>' id='ctbCountdownTime-<?php echo esc_attr( $cId ) ?>' data-date="<?php echo esc_attr( wp_json_encode( [ 'destDate' => $destDate ] ) ); ?>"> |
| 111 |
<style>@import url( <?php echo esc_url( $digitTypo['googleFontLink'] ?? '' ); ?> ); @import url( <?php echo esc_url( $labelTypo['googleFontLink'] ?? '' ); ?> ); <?php echo wp_kses( $countdownStyle::renderStyle(), [] ); ?> |
| 112 |
@media screen and ( max-width: 575px ) { #ctbCountdownTime-<?php echo esc_html( $cId ); ?> .countdownItems .countdownItem{ |
| 113 |
margin-top: calc( <?php echo esc_html( $boxSpace ); ?> / 2 ); margin-bottom: calc( <?php echo esc_html( $boxSpace ); ?> / 2 ); |
| 114 |
} } |
| 115 |
</style> |
| 116 |
|
| 117 |
<div class='countdownItems'> |
| 118 |
<?php $this->box( $isDays, "countdownDays $boxClass", $isLabels, $daysLabel ); ?> |
| 119 |
<?php echo $isSep && $isDays && ( $isHours || $isMinutes || $isSeconds ) ? "<span class='separator'></span>" : ''; ?> |
| 120 |
<?php $this->box( $isHours, "countdownHours $boxClass", $isLabels, $hoursLabel ); ?> |
| 121 |
<?php echo $isSep && $isHours && ( $isMinutes || $isSeconds ) ? "<span class='separator'></span>" : ''; ?> |
| 122 |
<?php $this->box( $isMinutes, "countdownMinutes $boxClass", $isLabels, $minutesLabel ); ?> |
| 123 |
<?php echo $isSep && $isMinutes && $isSeconds ? "<span class='separator'></span>" : ''; ?> |
| 124 |
<?php $this->box( $isSeconds, "countdownSeconds $boxClass", $isLabels, $secondsLabel ); ?> |
| 125 |
</div> |
| 126 |
</div> |
| 127 |
|
| 128 |
<?php $countdownStyle::$styles = []; // Empty styles |
| 129 |
return ob_get_clean(); |
| 130 |
} // Render |
| 131 |
|
| 132 |
function box( $is, $boxClass, $isLabels, $label ){ |
| 133 |
if( $is ){ ?><div class='countdownItem <?php echo esc_attr( $boxClass ) ?>'> |
| 134 |
<span class='digit'>00</span> |
| 135 |
<?php echo $isLabels ? "<span class='label'>". wp_kses_post( $label ) ."</span>" : ''; ?> |
| 136 |
</div><?php } |
| 137 |
} // Box |
| 138 |
} |
| 139 |
CTBCountdownTimeBlock::instance(); |