Skip to content

Commit

Permalink
Remove usage of register_block_type as this isn't a block
Browse files Browse the repository at this point in the history
  • Loading branch information
adamwoodnz committed Jul 16, 2023
1 parent be048ab commit 8c87c3f
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions mu-plugins/blocks/time/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,26 @@
add_action( 'init', __NAMESPACE__ . '\init' );

/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
* Enqueues the script for the Time format in the block editor using the metadata loaded from the `block.json` file,
* and adds the filter for transforming the time blocks.
*/
function init() {
register_block_type( __DIR__ . '/build' );
add_filter('the_content', __NAMESPACE__ . '\transform_time_blocks', 99, 1);
// Add the JS script to add the Time formatting option to the toolbar. The dependencies are autogenerated in block.json,
// and can be read with `wp_json_file_decode` & `register_block_script_handle.
$metadata_file = __DIR__ . '/build/block.json';
$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
$metadata['file'] = $metadata_file;
$editor_script_handle = register_block_script_handle( $metadata, 'editorScript', 0 );
add_action(
'enqueue_block_assets',
function() use ( $editor_script_handle ) {
if ( wp_should_load_block_editor_scripts_and_styles() && is_admin() ) {
wp_enqueue_script( $editor_script_handle );
}
}
);

add_filter( 'the_content', __NAMESPACE__ . '\transform_time_blocks', 99, 1 );
}

/**
Expand All @@ -32,7 +43,7 @@ function init() {
* @param string $content Post content.
* @return string Content with display times reformatted.
*/
function transform_time_blocks( $content) {
function transform_time_blocks( $content ) {
if ( empty( $content ) ) {
return $content;
}
Expand All @@ -42,15 +53,15 @@ function transform_time_blocks( $content) {
$dom->loadHTML( $content );
$xpath = new \DOMXPath( $dom );
$time_elements = $xpath->query( "//*[contains(concat(' ', normalize-space(@class), ' '), ' wporg-time ')]" );

foreach ( $time_elements as $time_element ) {
$time_content = $time_element->nodeValue;
$parsed_time = parse_time( $time_content );

if ( $parsed_time === null ) {
continue;
}

// Build the link and abbr microformat.
$time_element->setAttribute( 'href', 'https://www.timeanddate.com/worldclock/fixedtime.html?iso=' . gmdate( 'Ymd\THi', $parsed_time ) );

Expand All @@ -77,13 +88,13 @@ function transform_time_blocks( $content) {
function parse_time( $content ) {
// Replace non-breaking spaces with a regular white space.
$gmtcontent = preg_replace( '/\xC2\xA0| /', ' ', $content );

// PHP understands "GMT" better than "UTC" for timezones.
$gmtcontent = str_replace( 'UTC', 'GMT', $gmtcontent );

// Remove the word "at" from the string, if present. Allows strings like "Monday, April 6 at 19:00 UTC" to work.
$gmtcontent = str_replace( ' at ', ' ', $gmtcontent );

// Try to parse the time, relative to the post time.
$time = strtotime( $gmtcontent, get_the_date( 'U' ) );

Expand Down

0 comments on commit 8c87c3f

Please sign in to comment.