| 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.6 |
| 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.6' ); |
| 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_script( 'ctb_editor_script', plugins_url( 'dist/editor.js', __FILE__ ), [ 'wp-blob', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-element', 'wp-html-entities', 'wp-i18n', 'wp-rich-text' ], CTB_PLUGIN_VERSION, false ); // Backend Script |
| 58 |
wp_register_style( 'ctb_editor_style', plugins_url( 'dist/editor.css', __FILE__ ), [ 'wp-edit-blocks' ], CTB_PLUGIN_VERSION ); // Backend Style |
| 59 |
wp_register_script( 'ctb_script', plugins_url( 'dist/script.js', __FILE__ ), [ 'jquery' ], CTB_PLUGIN_VERSION, true ); // Frontend Script |
| 60 |
wp_register_style( 'ctb_style', plugins_url( 'dist/style.css', __FILE__ ), [ 'wp-editor' ], CTB_PLUGIN_VERSION ); // Frontend Style |
| 61 |
|
| 62 |
register_block_type( 'ctb/countdown-time', [ |
| 63 |
'editor_script' => 'ctb_editor_script', |
| 64 |
'editor_style' => 'ctb_editor_style', |
| 65 |
'script' => 'ctb_script', |
| 66 |
'style' => 'ctb_style', |
| 67 |
'render_callback' => [$this, 'render'] |
| 68 |
] ); // Register Block |
| 69 |
|
| 70 |
wp_set_script_translations( 'ctb_editor_script', 'countdown-time', plugin_dir_path( __FILE__ ) . 'languages' ); // Translate |
| 71 |
} |
| 72 |
|
| 73 |
function render( $attributes ){ |
| 74 |
extract( $attributes ); |
| 75 |
$align = $align ?? ''; |
| 76 |
$cId = $cId ?? ''; |
| 77 |
$layout = $layout ?? 'default'; |
| 78 |
$boxIsInline = $boxIsInline ?? false; |
| 79 |
$boxPosition = $boxPosition ?? 'center'; |
| 80 |
$isLabels = $isLabels ?? true; |
| 81 |
$isDays = $isDays ?? true; |
| 82 |
$daysLabel = $daysLabel ?? 'Days'; |
| 83 |
$isHours = $isHours ?? true; |
| 84 |
$hoursLabel = $hoursLabel ?? 'Hours'; |
| 85 |
$isMinutes = $isMinutes ?? true; |
| 86 |
$minutesLabel = $minutesLabel ?? 'Minutes'; |
| 87 |
$isSeconds = $isSeconds ?? true; |
| 88 |
$secondsLabel = $secondsLabel ?? 'Seconds'; |
| 89 |
$width = $width ?? '0px'; |
| 90 |
$background = $background ?? [ 'color' => '#0000' ]; |
| 91 |
$padding = $padding ?? [ 'vertical' => '10px', 'horizontal' => '15px' ]; |
| 92 |
$shadow = $shadow ?? []; |
| 93 |
$alignment = $alignment ?? 'center'; |
| 94 |
$boxBG = $boxBG ?? [ 'type' => 'gradient', 'color' => '#4527a4', 'gradient' => 'linear-gradient(135deg, #4527a4, #8344c5)' ]; |
| 95 |
$boxWidth = $boxWidth ?? '170px'; |
| 96 |
$boxHeight = $boxHeight ?? '120px'; |
| 97 |
$boxSpace = $boxSpace ?? '30px'; |
| 98 |
$boxBorder = $boxBorder ?? [ 'radius' => '5%' ]; |
| 99 |
$boxShadow = $boxShadow ?? []; |
| 100 |
$digitTypo = $digitTypo ?? [ 'fontSize' => 48 ]; |
| 101 |
$digitColor = $digitColor ?? '#fff'; |
| 102 |
$labelTypo = $labelTypo ?? [ 'fontSize' => 18 ]; |
| 103 |
$labelColor = $labelColor ?? '#fff'; |
| 104 |
$isSep = $isSep ?? false; |
| 105 |
$sepType = $sepType ?? ':'; |
| 106 |
$sepSize = $sepSize ?? 52; |
| 107 |
$sepColor = $sepColor ?? '#4527a4'; |
| 108 |
|
| 109 |
$countdownStyle = new CTBStyleGenerator(); // Generate Styles |
| 110 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId", [ |
| 111 |
'align-items' => $alignment, |
| 112 |
'justify-content' => $alignment |
| 113 |
] ); |
| 114 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems", [ |
| 115 |
'width' => '0px' === $width || '0%' === $width || '0em' === $width ? 'auto' : $width, |
| 116 |
$background['styles'] ?? 'background-color: #0000;' => '', |
| 117 |
'padding' => $padding['styles'] ?? '10px 15px', |
| 118 |
'box-shadow' => $shadow['styles'] ?? 'none', |
| 119 |
'justify-content' => $boxPosition |
| 120 |
] ); |
| 121 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .countdownItem", [ |
| 122 |
$boxBG['styles'] ?? 'background-image: linear-gradient(135deg, #4527a4, #8344c5);' => '', |
| 123 |
'width' => $boxWidth, |
| 124 |
'height' => $boxHeight, |
| 125 |
'margin-left' => "calc( $boxSpace / 2 )", |
| 126 |
'margin-right' => "calc( $boxSpace / 2 )", |
| 127 |
$boxBorder['styles'] ?? 'border-radius: 5%;' => '', |
| 128 |
'box-shadow' => $boxShadow['styles'] ?? '0 0 0 0 #0000' |
| 129 |
] ); |
| 130 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .separator::before", [ |
| 131 |
'content' => "'$sepType'", |
| 132 |
'font-size' => $sepSize . 'px', |
| 133 |
'color' => $sepColor |
| 134 |
] ); |
| 135 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .countdownItem .digit", [ |
| 136 |
'color' => $digitColor, |
| 137 |
$digitTypo['styles'] ?? 'font-size: 48px;' => '' |
| 138 |
] ); |
| 139 |
$countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .countdownItem .label", [ |
| 140 |
'color' => $labelColor, |
| 141 |
$labelTypo['styles'] ?? 'font-size: 18px;' => '' |
| 142 |
] ); |
| 143 |
|
| 144 |
$boxClass = $boxIsInline ? 'inline' : ''; |
| 145 |
|
| 146 |
ob_start(); ?> |
| 147 |
<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 ?? ''] ) ); ?>"> |
| 148 |
<style>@import url( <?php echo esc_url( $digitTypo['googleFontLink'] ?? '' ); ?> ); @import url( <?php echo esc_url( $labelTypo['googleFontLink'] ?? '' ); ?> ); <?php echo wp_kses( $countdownStyle::renderStyle(), [] ); ?> |
| 149 |
@media screen and ( max-width: 575px ) { #ctbCountdownTime-<?php echo esc_html( $cId ); ?> .countdownItems .countdownItem{ |
| 150 |
margin-top: calc( <?php echo esc_html( $boxSpace ); ?> / 2 ); margin-bottom: calc( <?php echo esc_html( $boxSpace ); ?> / 2 ); |
| 151 |
} } |
| 152 |
</style> |
| 153 |
|
| 154 |
<div class='countdownItems'> |
| 155 |
<?php $this->box( $isDays, "countdownDays $boxClass", $isLabels, $daysLabel ); ?> |
| 156 |
<?php echo $isSep && $isDays && ( $isHours || $isMinutes || $isSeconds ) ? "<span class='separator'></span>" : ''; ?> |
| 157 |
<?php $this->box( $isHours, "countdownHours $boxClass", $isLabels, $hoursLabel ); ?> |
| 158 |
<?php echo $isSep && $isHours && ( $isMinutes || $isSeconds ) ? "<span class='separator'></span>" : ''; ?> |
| 159 |
<?php $this->box( $isMinutes, "countdownMinutes $boxClass", $isLabels, $minutesLabel ); ?> |
| 160 |
<?php echo $isSep && $isMinutes && $isSeconds ? "<span class='separator'></span>" : ''; ?> |
| 161 |
<?php $this->box( $isSeconds, "countdownSeconds $boxClass", $isLabels, $secondsLabel ); ?> |
| 162 |
</div> |
| 163 |
</div> |
| 164 |
|
| 165 |
<?php $countdownStyle::$styles = []; // Empty styles |
| 166 |
return ob_get_clean(); |
| 167 |
} // Render |
| 168 |
|
| 169 |
function box( $is, $boxClass, $isLabels, $label ){ |
| 170 |
if( $is ){ ?><div class='countdownItem <?php echo esc_attr( $boxClass ) ?>'> |
| 171 |
<span class='digit'>00</span> |
| 172 |
<?php echo $isLabels ? "<span class='label'>". wp_kses_post( $label ) ."</span>" : ''; ?> |
| 173 |
</div><?php } |
| 174 |
} // Box |
| 175 |
} |
| 176 |
CTBCountdownTimeBlock::instance(); |