Skip to content

Commit

Permalink
Events: Convert event-list timestamp.
Browse files Browse the repository at this point in the history
Convert server-side Unix timestamps to a human-readable format and client-side timezone for the block WordPress Event List.
  • Loading branch information
renintw committed Dec 6, 2023
1 parent 673c20c commit c7f5f95
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
}
},
"editorScript": "file:./index.js",
"style": "file:./style-index.css"
"style": "file:./style-index.css",
"viewScript": "file:./view.js"

This comment has been minimized.

Copy link
@iandunn

iandunn Dec 7, 2023

Member

🤔 I'm not seeing this enqueued in my local env or on production. Not sure why, though, it looks correct.

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function render( $attributes, $content, $block ) {
$content .= '<li class="wporg-marker-list-item">';
$content .= '<h3 class="wporg-marker-list-item__title"><a class="external-link" href="' . esc_url( $event->url ) . ' ">' . esc_html( $event->title ) . '</a></h3>';
$content .= '<div class="wporg-marker-list-item__location">' . esc_html( $event->location ) . '</div>';
$content .= '<div class="wporg-marker-list-item__date-time">' . esc_html( $event->timestamp ) . '</div>';
$content .= '<div class="wporg-marker-list-item__date-time" data-wc-events-list-timestamp="' . esc_html( $event->timestamp ) . '"></div>';

This comment has been minimized.

Copy link
@iandunn

iandunn Dec 7, 2023

Member

Hey @renintw 👋🏻 esc_attr() would fit best here, since it's an attribute. esc_html() would be good if you wanted to escape the inner content, though, like the line above is doing.

Do you mind updating it? Sometimes using the escaping function for a different context can introduce security bugs.

This comment has been minimized.

Copy link
@renintw

renintw Dec 7, 2023

Author Contributor

Ah, thanks @iandunn. It's fine now as I've already reverted this entire commit in yesterday's PR. I will be mindful of this in the future!

$content .= '</li>';
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Converts server-side Unix timestamps to a human-readable format and client-side timezone.
window.addEventListener( 'load', () => {
const timeElements = document.querySelectorAll( '.wporg-marker-list__container .wporg-marker-list-item__date-time' );

timeElements.forEach( ( element ) => {
const timestamp = element.getAttribute( 'data-wc-events-list-timestamp' );
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
};
const localDate = new Date( parseInt( timestamp ) * 1000 ).toLocaleString( 'en-US', options );
element.textContent = localDate;
} );
} );

0 comments on commit c7f5f95

Please sign in to comment.