52 lines
1.4 KiB
PHP
52 lines
1.4 KiB
PHP
<?php
|
|
/**
|
|
* The editor block. Dynamic: the markup is produced by PHP at render time, so
|
|
* the events are always the ones of today and never a copy frozen in the post
|
|
* content.
|
|
*
|
|
* @package PiazzaInFesta
|
|
*/
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
/**
|
|
* Registers the block from its block.json.
|
|
*
|
|
* @return void
|
|
*/
|
|
function pinfesta_register_block() {
|
|
$type = register_block_type(
|
|
PINFESTA_DIR . 'blocks/eventi',
|
|
array( 'render_callback' => 'pinfesta_render_block' )
|
|
);
|
|
|
|
// The editor strings live in JavaScript, so they need their own JSON
|
|
// catalogue. The handle is read back from the registered block instead of
|
|
// being guessed: WordPress builds it from block.json and it can change.
|
|
if ( $type && ! empty( $type->editor_script_handles ) ) {
|
|
foreach ( $type->editor_script_handles as $handle ) {
|
|
wp_set_script_translations( $handle, 'piazza-in-festa', PINFESTA_DIR . 'languages' );
|
|
}
|
|
}
|
|
}
|
|
add_action( 'init', 'pinfesta_register_block' );
|
|
|
|
/**
|
|
* Renders the block on the front end and in the editor preview.
|
|
*
|
|
* @param array $attributes Block attributes.
|
|
* @return string HTML.
|
|
*/
|
|
function pinfesta_render_block( $attributes ) {
|
|
$args = wp_parse_args( (array) $attributes, pinfesta_defaults() );
|
|
|
|
$html = pinfesta_render_iframe( $args );
|
|
|
|
if ( '' === $html ) {
|
|
return '';
|
|
}
|
|
|
|
$wrapper = get_block_wrapper_attributes();
|
|
|
|
return '<div' . ( '' === $wrapper ? '' : ' ' . $wrapper ) . '>' . $html . '</div>';
|
|
}
|