101 lines
2.5 KiB
PHP
101 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* Gutenberg: article section block (title, content, featured image).
|
|
*/
|
|
|
|
function turquesa_register_article_section_block() {
|
|
wp_register_script(
|
|
'turquesa-article-section',
|
|
get_template_directory_uri() . '/js/blocks/article-section.js',
|
|
array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n' ),
|
|
'1.0.0',
|
|
true
|
|
);
|
|
|
|
wp_register_style(
|
|
'turquesa-article-section-editor',
|
|
get_template_directory_uri() . '/css/editor-blog.css',
|
|
array( 'wp-edit-blocks' ),
|
|
'1.0.0'
|
|
);
|
|
|
|
register_block_type(
|
|
'turquesa/article-section',
|
|
array(
|
|
'api_version' => 2,
|
|
'editor_script' => 'turquesa-article-section',
|
|
'editor_style' => 'turquesa-article-section-editor',
|
|
'render_callback' => 'turquesa_render_article_section',
|
|
'attributes' => array(
|
|
'title' => array(
|
|
'type' => 'string',
|
|
'default' => '',
|
|
),
|
|
'imageId' => array(
|
|
'type' => 'number',
|
|
'default' => 0,
|
|
),
|
|
'imageUrl' => array(
|
|
'type' => 'string',
|
|
'default' => '',
|
|
),
|
|
'imageAlt' => array(
|
|
'type' => 'string',
|
|
'default' => '',
|
|
),
|
|
),
|
|
'supports' => array(
|
|
'html' => false,
|
|
'anchor' => true,
|
|
),
|
|
)
|
|
);
|
|
}
|
|
add_action( 'init', 'turquesa_register_article_section_block' );
|
|
|
|
function turquesa_render_article_section( $attributes, $content ) {
|
|
$title = isset( $attributes['title'] ) ? trim( (string) $attributes['title'] ) : '';
|
|
$image_id = isset( $attributes['imageId'] ) ? (int) $attributes['imageId'] : 0;
|
|
$alt = isset( $attributes['imageAlt'] ) ? (string) $attributes['imageAlt'] : '';
|
|
|
|
$section_id = '';
|
|
if ( $title ) {
|
|
$section_id = sanitize_title( $title );
|
|
}
|
|
|
|
ob_start();
|
|
?>
|
|
<section class="blog-section"<?php echo $section_id ? ' id="' . esc_attr( $section_id ) . '"' : ''; ?>>
|
|
<?php if ( $title ) : ?>
|
|
<h2><?php echo esc_html( $title ); ?></h2>
|
|
<?php endif; ?>
|
|
<?php
|
|
if ( $image_id ) {
|
|
echo wp_get_attachment_image(
|
|
$image_id,
|
|
'full',
|
|
false,
|
|
array(
|
|
'class' => 'blog-featured',
|
|
'alt' => $alt,
|
|
'decoding' => 'async',
|
|
)
|
|
);
|
|
}
|
|
?>
|
|
<div class="blog-section__body">
|
|
<?php echo $content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
|
|
</div>
|
|
</section>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
|
|
function turquesa_gutenberg_theme_support() {
|
|
add_theme_support( 'align-wide' );
|
|
add_theme_support( 'editor-styles' );
|
|
add_theme_support( 'responsive-embeds' );
|
|
add_editor_style( 'css/editor-blog.css' );
|
|
}
|
|
add_action( 'after_setup_theme', 'turquesa_gutenberg_theme_support' );
|