Skip to content

Commit bb85a99

Browse files
committed
added timezone, escaped attrs, add external thumbnail
1 parent d36dfe4 commit bb85a99

File tree

6 files changed

+82
-34
lines changed

6 files changed

+82
-34
lines changed

src/block/countdown/editor.scss

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.ugb-panel--countdown .components-datetime__time-wrapper {
1+
:is(.ugb-panel--countdown, .ugb-panel--video-popup) .components-datetime__time-wrapper {
22
.components-base-control {
33
margin-bottom: 0px !important;
44
}
@@ -8,7 +8,7 @@
88
}
99
}
1010

11-
.ugb-panel--countdown .components-datetime__date {
11+
:is(.ugb-panel--countdown, .ugb-panel--video-popup) .components-datetime__date {
1212
.components-datetime__date__day[aria-label*="Selected" i] {
1313
color: #fff;
1414
&:hover {

src/block/countdown/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import metadata from './block.json'
1717
import deprecated from './deprecated'
1818
import substitute from './substitute'
1919

20+
export { timezones } from './timezones'
21+
2022
export const settings = {
2123
...metadata,
2224
icon: CountdownIcon,

src/block/video-popup/edit.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
AdvancedToggleControl,
1919
useBlockCssGenerator,
2020
ControlSeparator,
21+
AdvancedSelectControl,
2122
} from '~stackable/components'
2223
import {
2324
BlockDiv,
@@ -39,6 +40,8 @@ import {
3940
withQueryLoopContext,
4041
} from '~stackable/higher-order'
4142

43+
import { timezones as TIMEZONE_OPTIONS } from '../countdown'
44+
4245
/**
4346
* WordPress dependencies
4447
*/
@@ -47,7 +50,8 @@ import { InnerBlocks } from '@wordpress/block-editor'
4750
import { __ } from '@wordpress/i18n'
4851
import { addFilter } from '@wordpress/hooks'
4952
import { memo } from '@wordpress/element'
50-
import { DatePicker } from '@wordpress/components'
53+
import { DateTimePicker } from '@wordpress/components'
54+
import { getSettings as getDateSettings } from '@wordpress/date'
5155

5256
export const defaultIcon = '<svg data-prefix="fas" data-icon="play" class="svg-inline--fa fa-play fa-w-14" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" aria-hidden="true"><path fill="currentColor" d="M424.4 214.7L72.4 6.6C43.8-10.3 0 6.1 0 47.9V464c0 37.5 40.7 60.1 72.4 41.3l352-208c31.4-18.5 31.5-64.1 0-82.6z"></path></svg>'
5357

@@ -109,6 +113,7 @@ const Edit = props => {
109113
videoName={ attributes.videoName }
110114
videoUploadDate={ attributes.videoUploadDate }
111115
videoDescription={ attributes.videoDescription }
116+
videoUploadDateTimezone={ attributes.videoUploadDateTimezone }
112117
/>
113118

114119
{ blockCss && <style key="block-css">{ blockCss }</style> }
@@ -133,6 +138,28 @@ const Edit = props => {
133138
}
134139

135140
const InspectorControls = memo( props => {
141+
const getUploadDate = ( uploadDate, timezone ) => {
142+
// If it uses local timezone, get offset from WordPress settings
143+
if ( ! timezone ) {
144+
const { timezone: localTimezone } = getDateSettings()
145+
const offset = Number( localTimezone.offset )
146+
const hours = Math.floor( Math.abs( offset ) )
147+
const minutes = Math.round( ( Math.abs( offset ) % 1 ) * 60 )
148+
149+
return uploadDate + ( offset >= 0 ? '+' : '-' ) +
150+
String( hours ).padStart( 2, '0' ) + ':' +
151+
String( minutes ).padStart( 2, '0' )
152+
}
153+
154+
const date = new Date( uploadDate )
155+
const offset = new Intl.DateTimeFormat( 'en-US', {
156+
timeZone: timezone,
157+
timeZoneName: 'longOffset',
158+
} ).format( date ).slice( -6 )
159+
160+
return uploadDate + offset
161+
}
162+
136163
return (
137164
<>
138165
<InspectorTabs hasLayoutPanel={ false } />
@@ -221,15 +248,21 @@ const InspectorControls = memo( props => {
221248
// This text control allows users to see if a date has been set/removed
222249
className="stk-components-datetime__date-input"
223250
label={ __( 'Video upload date', i18n ) }
224-
value={ props.videoUploadDate ? new Date( props.videoUploadDate ).toISOString().slice( 0, 10 ) : '' }
251+
value={ props.videoUploadDate ? getUploadDate( props.videoUploadDate, props.videoUploadDateTimezone ) : '' }
252+
onChange={ videoUploadDate => props.setAttributes( { videoUploadDate } ) }
225253
inputType="date"
226254
readOnly={ true }
227255
/>
228-
<DatePicker
256+
<DateTimePicker
229257
currentDate={ props.videoUploadDate }
230-
onChange={ videoUploadDate => {
231-
props.setAttributes( { videoUploadDate } )
232-
} }
258+
is12Hour={ true }
259+
onChange={ videoUploadDate => props.setAttributes( { videoUploadDate } ) }
260+
/>
261+
<AdvancedSelectControl
262+
label={ __( 'Timezone', i18n ) }
263+
options={ TIMEZONE_OPTIONS }
264+
attribute="videoUploadDateTimezone"
265+
allowReset={ false }
233266
/>
234267
</> }
235268

src/block/video-popup/editor.scss

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,7 @@
2828
.editor-styles-wrapper .stk-block-video-popup [data-block] {
2929
max-width: none;
3030
}
31-
.ugb-panel--video-popup .components-datetime__date {
32-
.components-datetime__date__day[aria-label*="Selected" i] {
33-
color: #fff !important;
34-
&:hover {
35-
color: #fff !important;
36-
}
37-
}
38-
:last-child {
39-
row-gap: 8px;
40-
column-gap: 4px;
41-
div:nth-of-type(7) {
42-
justify-self: auto;
43-
}
44-
}
45-
}
31+
4632
.stk-components-datetime__date-input .components-text-control__input {
4733
background-color: #fff;
4834
}

src/block/video-popup/index.php

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,38 +36,61 @@ public function print_video_popup_schema() {
3636
}
3737
}
3838

39+
public function get_upload_date_timezone( $timezone_name ) {
40+
// If it uses local timezone, get offset from WordPress settings
41+
if ( ! $timezone_name ) {
42+
$offset = (float) get_option( 'gmt_offset' );
43+
$hours = (int) $offset;
44+
$minutes = ( $offset - $hours );
45+
$sign = ( $offset < 0 ) ? '-' : '+';
46+
$abs_hour = abs( $hours );
47+
$abs_mins = abs( $minutes * 60 );
48+
49+
return sprintf( '%s%02d:%02d', $sign, $abs_hour, $abs_mins );
50+
}
51+
52+
$timezone = new DateTimeZone( $timezone_name );
53+
$datetime = new DateTime('now', $timezone);
54+
return $datetime->format('P');
55+
}
56+
3957
public function render_block_video_popup_schema( $block_content, $block ) {
4058
// Initialize video schema
41-
$video_schema = array();
42-
$video_schema[ '@context' ] = 'https://schema.org';
43-
$video_schema[ '@type' ] = 'VideoObject';
59+
$video_schema = array(
60+
'@context' => 'https://schema.org',
61+
'@type' => 'VideoObject'
62+
);
4463

4564
// Get video schema properties from block attributes
4665
$attributes = $block[ 'attrs' ];
4766

4867
// Get video name from the title of the post if not set
4968
$name = isset( $attributes[ 'videoName' ] ) ? $attributes[ 'videoName' ] : ( get_the_title() ?? '');
5069
// Get video upload date from the date of the post if not set
51-
$upload_date = isset( $attributes[ 'videoUploadDate' ] ) ? $attributes[ 'videoUploadDate' ] : ( get_the_date( 'c' ) || '');
70+
$upload_date_timezone = $this->get_upload_date_timezone( isset( $attributes[ 'videoUploadDateTimezone' ] ) ? $attributes[ 'videoUploadDateTimezone' ] : false );
71+
$upload_date = isset( $attributes[ 'videoUploadDate' ] ) ? $attributes[ 'videoUploadDate' ] . $upload_date_timezone : ( get_the_date( 'c' ) ?? '');
5272
$description = isset( $attributes[ 'videoDescription' ] ) ? $attributes[ 'videoDescription' ] : '';
5373
$content_url = isset( $attributes[ 'videoLink' ] ) ? $attributes[ 'videoLink' ] : '';
5474

55-
$video_schema[ 'name' ] = $name;
56-
$video_schema[ 'description' ] = $description;
57-
$video_schema[ 'uploadDate' ] = $upload_date;
58-
$video_schema[ 'contentUrl' ] = $content_url;
75+
error_log( $upload_date );
76+
77+
$video_schema[ 'name' ] = esc_attr( $name );
78+
$video_schema[ 'description' ] = esc_attr( $description );
79+
$video_schema[ 'uploadDate' ] = esc_attr( $upload_date );
80+
$video_schema[ 'contentUrl' ] = esc_url( $content_url );
5981

6082
// Get thumbnail URL from the image block if it exists
6183
if ( isset( $block[ 'innerBlocks' ] )
6284
&& count( $block[ 'innerBlocks' ] ) === 2
6385
&& $block[ 'innerBlocks' ][ 1 ][ 'blockName' ] === 'stackable/image'
6486
) {
6587
$image_attributes = $block[ 'innerBlocks' ][ 1 ][ 'attrs' ];
66-
$thumbnail_url = isset( $image_attributes[ 'imageUrl' ] ) ? $image_attributes[ 'imageUrl' ] : '';
67-
$video_schema[ 'thumbnailUrl' ] = $thumbnail_url;
88+
$thumbnail_url = isset( $image_attributes[ 'imageUrl' ] ) ? $image_attributes[ 'imageUrl' ]
89+
: ( isset( $image_attributes[ 'imageExternalUrl' ] ) ? $image_attributes[ 'imageExternalUrl' ] : '' );
90+
$video_schema[ 'thumbnailUrl' ] = esc_url( $thumbnail_url );
6891
}
6992

70-
$video_schema_json = json_encode( $video_schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES );
93+
$video_schema_json = wp_json_encode( $video_schema, JSON_UNESCAPED_SLASHES );
7194
$this->video_entities[] = $video_schema_json;
7295

7396
return $block_content;

src/block/video-popup/schema.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ export const attributes = ( version = VERSION ) => {
7474
type: 'string',
7575
default: '',
7676
},
77+
videoUploadDateTimezone: {
78+
type: 'string',
79+
default: '',
80+
},
7781
},
7882
versionAdded: '3.0.0',
7983
versionDeprecated: '',

0 commit comments

Comments
 (0)