Files

99 lines
3.1 KiB
PHP

<?php
namespace Smilia\Core;
use WP_Block_Editor_Context;
use WP_Post;
defined( 'ABSPATH' ) || exit;
final class Blocks {
private const DIR = SMILIA_CORE_DIR . '/build/blocks';
public function register(): void {
add_action( 'init', array( $this, 'register_blocks' ) );
add_filter( 'block_type_metadata', array( $this, 'asset_version' ) );
add_filter( 'block_categories_all', array( $this, 'add_category' ) );
add_filter( 'block_editor_settings_all', array( $this, 'editor_settings' ), 10, 2 );
add_filter( 'should_load_remote_block_patterns', '__return_false' );
add_filter( 'wp_content_img_tag', array( $this, 'keep_wide_sizes' ), 20 );
add_action( 'enqueue_block_editor_assets', array( $this, 'editor_data' ) );
remove_action( 'enqueue_block_editor_assets', 'wp_enqueue_editor_block_directory_assets' );
}
public function register_blocks(): void {
if ( is_readable( self::DIR . '/blocks-manifest.php' ) ) {
wp_register_block_types_from_metadata_collection( self::DIR, self::DIR . '/blocks-manifest.php' );
}
foreach ( \WP_Block_Type_Registry::get_instance()->get_all_registered() as $name => $type ) {
if ( ! str_starts_with( $name, 'smilia/' ) ) {
continue;
}
foreach ( (array) $type->editor_script_handles as $handle ) {
wp_set_script_translations( $handle, 'smilia-core', SMILIA_CORE_DIR . '/languages' );
}
}
}
public function keep_wide_sizes( string $image ): string {
if ( false === strpos( $image, 'service-page__method-photo' ) ) {
return $image;
}
$processor = new \WP_HTML_Tag_Processor( $image );
if ( $processor->next_tag( 'img' ) ) {
$sizes = (string) $processor->get_attribute( 'sizes' );
if ( str_starts_with( $sizes, 'auto,' ) ) {
$processor->set_attribute( 'sizes', trim( substr( $sizes, 5 ) ) );
}
}
return $processor->get_updated_html();
}
public function asset_version( array $metadata ): array {
if ( empty( $metadata['file'] ) || ! str_starts_with( wp_normalize_path( $metadata['file'] ), wp_normalize_path( self::DIR ) ) ) {
return $metadata;
}
$asset = dirname( $metadata['file'] ) . '/index.asset.php';
if ( is_readable( $asset ) ) {
$data = include $asset;
if ( is_array( $data ) && ! empty( $data['version'] ) ) {
$metadata['version'] = (string) $data['version'];
}
}
return $metadata;
}
public function editor_data(): void {
wp_add_inline_script(
'wp-blocks',
'window.smiliaBlocks = ' . wp_json_encode(
array(
'images' => plugins_url( 'assets/img/', SMILIA_CORE_FILE ),
'admin' => admin_url(),
)
) . ';'
);
}
public function add_category( array $categories ): array {
array_unshift(
$categories,
array(
'slug' => 'smilia',
'title' => __( 'Smilia', 'smilia-core' ),
'icon' => null,
)
);
return $categories;
}
public function editor_settings( array $settings, WP_Block_Editor_Context $context ): array {
$settings['enableOpenverseMediaCategory'] = false;
if ( $context->post instanceof WP_Post && 'page' === $context->post->post_type ) {
$settings['templateLock'] = current_user_can( 'manage_options' ) ? 'all' : 'contentOnly';
}
return $settings;
}
}