PluginProbe
Countdown Timer Block – build urgency for events and launches / 1.0.8
Countdown Timer Block – build urgency for events and launches v1.0.8
1.3.3 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 All 32 releases
countdown-time / plugin.php

plugin.php in Countdown Timer Block – build urgency for events and launches 1.0.8, at plugin.php

130 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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.8
6 * Author: bPlugins LLC
7 * Author URI: http://bplugins.com
8 * License: GPLv3
9 * License URI: https://www.gnu.org/licenses/gpl-3.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.8' );
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 function __construct(){
44 add_action( 'init', [$this, 'onInit'] );
45 }
46
47 function onInit() {
48 wp_register_style( 'ctb-countdown-time-editor-style', plugins_url( 'dist/editor.css', __FILE__ ), [ 'wp-edit-blocks' ], CTB_PLUGIN_VERSION ); // Backend Style
49 wp_register_style( 'ctb-countdown-time-style', plugins_url( 'dist/style.css', __FILE__ ), [ 'wp-editor' ], CTB_PLUGIN_VERSION ); // Frontend Style
50
51 register_block_type( __DIR__, [
52 'editor_style' => 'ctb-countdown-time-editor-style',
53 'style' => 'ctb-countdown-time-style',
54 'render_callback' => [$this, 'render']
55 ] ); // Register Block
56
57 wp_set_script_translations( 'ctb-countdown-time-editor-script', 'countdown-time', plugin_dir_path( __FILE__ ) . 'languages' ); // Translate
58 }
59
60 function render( $attributes ){
61 extract( $attributes );
62
63 $countdownStyle = new CTBStyleGenerator(); // Generate Styles
64 $countdownStyle::addStyle( "#ctbCountdownTime-$cId", [
65 'align-items' => $alignment,
66 'justify-content' => $alignment
67 ] );
68 $countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems", [
69 'width' => '0px' === $width || '0%' === $width || '0em' === $width ? 'auto' : $width,
70 $background['styles'] ?? 'background-color: #0000;' => '',
71 'padding' => $padding['styles'] ?? '10px 15px',
72 'box-shadow' => $shadow['styles'] ?? 'none',
73 'justify-content' => $boxPosition
74 ] );
75 $countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .countdownItem", [
76 $boxBG['styles'] ?? 'background-image: linear-gradient(135deg, #4527a4, #8344c5);' => '',
77 'width' => $boxWidth,
78 'height' => $boxHeight,
79 'margin-left' => "calc( $boxSpace / 2 )",
80 'margin-right' => "calc( $boxSpace / 2 )",
81 $boxBorder['styles'] ?? 'border-radius: 5%;' => '',
82 'box-shadow' => $boxShadow['styles'] ?? '0 0 0 0 #0000'
83 ] );
84 $countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .separator::before", [
85 'content' => "'$sepType'",
86 'font-size' => $sepSize . 'px',
87 'color' => $sepColor
88 ] );
89 $countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .countdownItem .digit", [
90 'color' => $digitColor,
91 $digitTypo['styles'] ?? 'font-size: 48px;' => ''
92 ] );
93 $countdownStyle::addStyle( "#ctbCountdownTime-$cId .countdownItems .countdownItem .label", [
94 'color' => $labelColor,
95 $labelTypo['styles'] ?? 'font-size: 18px;' => ''
96 ] );
97
98 $boxClass = $boxIsInline ? 'inline' : '';
99
100 ob_start(); ?>
101 <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 ] ) ); ?>">
102 <style>@import url( <?php echo esc_url( $digitTypo['googleFontLink'] ?? '' ); ?> ); @import url( <?php echo esc_url( $labelTypo['googleFontLink'] ?? '' ); ?> ); <?php echo wp_kses( $countdownStyle::renderStyle(), [] ); ?>
103 @media screen and ( max-width: 575px ) { #ctbCountdownTime-<?php echo esc_html( $cId ); ?> .countdownItems .countdownItem{
104 margin-top: calc( <?php echo esc_html( $boxSpace ); ?> / 2 ); margin-bottom: calc( <?php echo esc_html( $boxSpace ); ?> / 2 );
105 } }
106 </style>
107
108 <div class='countdownItems'>
109 <?php $this->box( $isDays, "countdownDays $boxClass", $isLabels, $daysLabel ); ?>
110 <?php echo $isSep && $isDays && ( $isHours || $isMinutes || $isSeconds ) ? "<span class='separator'></span>" : ''; ?>
111 <?php $this->box( $isHours, "countdownHours $boxClass", $isLabels, $hoursLabel ); ?>
112 <?php echo $isSep && $isHours && ( $isMinutes || $isSeconds ) ? "<span class='separator'></span>" : ''; ?>
113 <?php $this->box( $isMinutes, "countdownMinutes $boxClass", $isLabels, $minutesLabel ); ?>
114 <?php echo $isSep && $isMinutes && $isSeconds ? "<span class='separator'></span>" : ''; ?>
115 <?php $this->box( $isSeconds, "countdownSeconds $boxClass", $isLabels, $secondsLabel ); ?>
116 </div>
117 </div>
118
119 <?php $countdownStyle::$styles = []; // Empty styles
120 return ob_get_clean();
121 } // Render
122
123 function box( $is, $boxClass, $isLabels, $label ){
124 if( $is ){ ?><div class='countdownItem <?php echo esc_attr( $boxClass ) ?>'>
125 <span class='digit'>00</span>
126 <?php echo $isLabels ? "<span class='label'>". wp_kses_post( $label ) ."</span>" : ''; ?>
127 </div><?php }
128 } // Box
129 }
130 new CTBCountdownTimeBlock;