PluginProbe
Getwid – Gutenberg Blocks / 3.0.2
Getwid – Gutenberg Blocks v3.0.2
3.0.2 3.0.1 3.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.2.0 trunk 1.8.7 1.9.1 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9
getwid / includes / blocks / countdown.php

countdown.php in Getwid – Gutenberg Blocks 3.0.2, at includes/blocks/countdown.php

297 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Getwid\Blocks;
4
5 class Countdown extends AbstractBlock {
6
7 public function __construct() {
8
9 parent::__construct( 'getwid/countdown' );
10
11 $this->register_vendor_scripts();
12
13 add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_editor_assets' ) );
14
15 register_block_type(
16 getwid_get_plugin_path( 'assets/blocks/countdown' ),
17 array(
18 'render_callback' => array( $this, 'render_callback' ),
19 )
20 );
21 }
22
23 public function get_label() {
24 return __( 'Countdown', 'getwid' );
25 }
26
27 private function get_default_date() {
28
29 $current_date = new \DateTime( current_time( 'Y-m-d H:i:s' ) );
30 $current_date->add( new \DateInterval( 'P1D' ) );
31
32 return $current_date->format( 'Y-m-d H:i:s' );
33 }
34
35
36 private function get_locale_prefix() {
37
38 preg_match( '/^(.*)_/', get_locale(), $current_locale );
39
40 return isset( $current_locale[1] ) && 'en' !== $current_locale[1] ? $current_locale[1] : '';
41 }
42
43 private function register_vendor_scripts() {
44
45 wp_register_script(
46 'jquery-plugin',
47 getwid_get_plugin_url( 'vendors/jquery.countdown/jquery.plugin.min.js' ),
48 array( 'jquery' ),
49 '1.0',
50 true
51 );
52
53 wp_register_script(
54 'jquery-countdown',
55 getwid_get_plugin_url( 'vendors/jquery.countdown/jquery.countdown.min.js' ),
56 array( 'jquery', 'jquery-plugin' ),
57 '2.1.0',
58 true
59 );
60
61 $locale_prefix = $this->get_locale_prefix();
62
63 if ( '' !== $locale_prefix ) {
64 $locale_path = 'vendors/jquery.countdown/localization/jquery.countdown-' . $locale_prefix . '.js';
65
66 if ( file_exists( getwid_get_plugin_path( $locale_path ) ) ) {
67 wp_register_script(
68 'jquery-countdown-' . $locale_prefix,
69 getwid_get_plugin_url( $locale_path ),
70 array( 'jquery-countdown' ),
71 '2.1.0',
72 true
73 );
74 }
75 }
76 }
77
78 public function enqueue_editor_assets() {
79
80 wp_enqueue_script( 'jquery-countdown' );
81
82 $locale_prefix = $this->get_locale_prefix();
83
84 if ( '' !== $locale_prefix ) {
85 $locale_path = 'vendors/jquery.countdown/localization/jquery.countdown-' . $locale_prefix . '.js';
86
87 if ( file_exists( getwid_get_plugin_path( $locale_path ) ) ) {
88 wp_enqueue_script( 'jquery-countdown-' . $locale_prefix );
89 }
90 }
91 }
92
93 public function block_frontend_assets() {
94
95 if ( is_admin() ) {
96 return;
97 }
98
99 wp_enqueue_script( 'jquery-countdown' );
100
101 $locale_prefix = $this->get_locale_prefix();
102
103 if ( '' !== $locale_prefix ) {
104 $locale_path = 'vendors/jquery.countdown/localization/jquery.countdown-' . $locale_prefix . '.js';
105
106 if ( file_exists( getwid_get_plugin_path( $locale_path ) ) ) {
107 wp_enqueue_script( 'jquery-countdown-' . $locale_prefix );
108 }
109 }
110 }
111
112 public function render_callback( $attributes, $content ) {
113
114 $attributes = wp_parse_args(
115 $attributes,
116 array(
117 'dateTime' => $this->get_default_date(),
118 'years' => false,
119 'months' => false,
120 'weeks' => false,
121 'days' => true,
122 'hours' => true,
123 'minutes' => true,
124 'seconds' => true,
125 'fontGroupID' => '',
126 'fontFamily' => '',
127 'fontSizeTablet' => 'fs-tablet-100',
128 'fontSizeMobile' => 'fs-mobile-100',
129 'innerPadding' => 'default',
130 'innerSpacings' => 'none',
131 )
132 );
133
134 if (
135 isset( $attributes['fontWeight'] ) &&
136 ( 'regular' === $attributes['fontWeight'] || 'normal' === $attributes['fontWeight'] )
137 ) {
138 $attributes['fontWeight'] = '400';
139 }
140
141 if ( $this->should_load_google_font( $attributes ) ) {
142 $font_family = $attributes['fontFamily'];
143 $font_family_handle = strtolower( preg_replace( '/\s+/', '_', $font_family ) );
144 $font_weight = '';
145 $font_weight_handle = '';
146 $font_weight_part = '';
147
148 if ( isset( $attributes['fontWeight'] ) && '400' !== $attributes['fontWeight'] ) {
149 $font_weight = $attributes['fontWeight'];
150 $font_weight_handle = '_' . $font_weight;
151 $font_weight_part = ':' . $font_weight;
152 }
153
154 wp_enqueue_style(
155 'google-font-' . esc_attr( $font_family_handle ) . esc_attr( $font_weight_handle ),
156 'https://fonts.googleapis.com/css?family=' . esc_attr( $font_family ) . esc_attr( $font_weight_part ),
157 null,
158 'all'
159 );
160 }
161
162 $block_name = 'wp-block-getwid-countdown';
163 $class = $block_name;
164
165 if ( isset( $attributes['className'] ) ) {
166 $class .= ' ' . $attributes['className'];
167 }
168 if ( isset( $attributes['align'] ) ) {
169 $class .= ' align' . $attributes['align'];
170 }
171 if ( isset( $attributes['textAlignment'] ) ) {
172 $class .= ' has-horizontal-alignment-' . $attributes['textAlignment'];
173 }
174 if ( isset( $attributes['innerPadding'] ) && 'default' !== $attributes['innerPadding'] ) {
175 $class .= ' has-inner-paddings-' . $attributes['innerPadding'];
176 }
177 if ( isset( $attributes['innerSpacings'] ) && 'none' !== $attributes['innerSpacings'] ) {
178 $class .= ' has-spacing-' . $attributes['innerSpacings'];
179 }
180
181 $wrapper_class = $block_name . '__content';
182 $content_class = $block_name . '__wrapper';
183
184 if ( isset( $attributes['fontSizeTablet'] ) && 'fs-tablet-100' !== $attributes['fontSizeTablet'] ) {
185 $content_class .= ' ' . $attributes['fontSizeTablet'];
186 }
187 if ( isset( $attributes['fontSizeMobile'] ) && 'fs-mobile-100' !== $attributes['fontSizeMobile'] ) {
188 $content_class .= ' ' . $attributes['fontSizeMobile'];
189 }
190 if ( isset( $attributes['fontSize'] ) && '' !== $attributes['fontSize'] ) {
191 $content_class .= ' has-custom-font-size';
192 }
193
194 $content_style = '';
195
196 if ( isset( $attributes['fontSize'] ) ) {
197 $content_style .= 'font-size: ' . $attributes['fontSize'] . ';';
198 }
199 if ( isset( $attributes['fontFamily'] ) && '' !== $attributes['fontFamily'] ) {
200 $content_style .= 'font-family: ' . $attributes['fontFamily'] . ';';
201 }
202 if ( isset( $attributes['fontWeight'] ) ) {
203 $content_style .= 'font-weight: ' . $attributes['fontWeight'] . ';';
204 }
205 if ( isset( $attributes['fontStyle'] ) ) {
206 $content_style .= 'font-style: ' . $attributes['fontStyle'] . ';';
207 }
208 if ( isset( $attributes['textTransform'] ) && 'default' !== $attributes['textTransform'] ) {
209 $content_style .= 'text-transform: ' . $attributes['textTransform'] . ';';
210 }
211 if ( isset( $attributes['lineHeight'] ) ) {
212 $content_style .= 'line-height: ' . $attributes['lineHeight'] . ';';
213 }
214 if ( isset( $attributes['letterSpacing'] ) ) {
215 $content_style .= 'letter-spacing: ' . $attributes['letterSpacing'] . ';';
216 }
217
218 getwid_custom_color_style_and_class(
219 $content_style,
220 $content_class,
221 $attributes,
222 'color',
223 getwid_is_block_editor()
224 );
225
226 try {
227 $target_date = new \DateTime( $attributes['dateTime'] );
228 } catch ( \Exception $e ) {
229 return esc_html__( 'Invalid date.', 'getwid' );
230 }
231
232 $current_date = new \DateTime( current_time( 'Y-m-d H:i:s' ) );
233
234 if ( $current_date < $target_date ) {
235 $date_time_until = $current_date->diff( $target_date )->format( '+%yy +%mo +%dd +%hh +%im +%ss' );
236 } else {
237 $date_time_until = 'negative';
238 }
239
240 $countdown_options = array(
241 ! empty( $attributes['backgroundColor'] ) ? 'data-bg-color="' . esc_attr( $attributes['backgroundColor'] ) . '"' : '',
242 ! empty( $attributes['years'] ) ? 'data-years="' . esc_attr( $attributes['years'] ) . '"' : '',
243 ! empty( $attributes['months'] ) ? 'data-months="' . esc_attr( $attributes['months'] ) . '"' : '',
244 ! empty( $attributes['weeks'] ) ? 'data-weeks="' . esc_attr( $attributes['weeks'] ) . '"' : '',
245 ! empty( $attributes['days'] ) ? 'data-days="' . esc_attr( $attributes['days'] ) . '"' : '',
246 ! empty( $attributes['hours'] ) ? 'data-hours="' . esc_attr( $attributes['hours'] ) . '"' : '',
247 ! empty( $attributes['minutes'] ) ? 'data-minutes="' . esc_attr( $attributes['minutes'] ) . '"' : '',
248 ! empty( $attributes['seconds'] ) ? 'data-seconds="' . esc_attr( $attributes['seconds'] ) . '"' : '',
249 );
250
251 $countdown_options_str = implode( ' ', $countdown_options );
252
253 ob_start();
254 ?>
255 <div class="<?php echo esc_attr( $class ); ?>">
256 <div class="<?php echo esc_attr( $content_class ); ?>"
257 <?php
258 if ( ! empty( $content_style ) ) {
259 ?>
260 style="<?php echo esc_attr( $content_style ); ?>"<?php } ?>>
261 <div class="<?php echo esc_attr( $wrapper_class ); ?>"
262 data-datetime="<?php echo esc_attr( ! empty( $date_time_until ) ? $date_time_until : '' ); ?>" <?php echo $countdown_options_str; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
263 </div>
264 </div>
265 </div>
266 <?php
267 $result = ob_get_clean();
268
269 $this->block_frontend_assets();
270
271 return $result;
272 }
273
274 private function should_load_google_font( $attributes ) {
275
276 $should_load = false;
277
278 if ( isset( $attributes['fontFamily'] ) && ! empty( $attributes['fontFamily'] ) ) {
279 $should_load = true;
280 }
281
282 if (
283 $should_load &&
284 isset( $attributes['fontGroupID'] ) &&
285 ! in_array( $attributes['fontGroupID'], array( '', 'google-fonts' ), true )
286 ) {
287 $should_load = false;
288 }
289
290 return $should_load;
291 }
292 }
293
294 getwid()->blocksManager()->addBlock(
295 new Countdown()
296 );
297