| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gutenberg block support. |
| 4 |
* |
| 5 |
* @package ERecht24LegalText |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace ERecht24LegalText\Frontend; |
| 9 |
|
| 10 |
use ERecht24LegalText\Settings; |
| 11 |
|
| 12 |
defined( 'ABSPATH' ) || exit; |
| 13 |
|
| 14 |
/** |
| 15 |
* Registers a dynamic Gutenberg block that delegates rendering to the shortcode. |
| 16 |
*/ |
| 17 |
final class Blocks { |
| 18 |
|
| 19 |
/** |
| 20 |
* Attribute schema shared by both registered block names. Must stay in |
| 21 |
* sync with the block.json files below blocks/ — used only as fallback |
| 22 |
* when a metadata file is unreadable. |
| 23 |
* |
| 24 |
* @var array<string,array<string,mixed>> |
| 25 |
*/ |
| 26 |
private const BLOCK_ATTRIBUTES = array( |
| 27 |
'type' => array( |
| 28 |
'type' => 'string', |
| 29 |
'default' => 'imprint', |
| 30 |
), |
| 31 |
'lang' => array( |
| 32 |
'type' => 'string', |
| 33 |
'default' => 'de', |
| 34 |
), |
| 35 |
'strip_title' => array( |
| 36 |
'type' => 'boolean', |
| 37 |
'default' => false, |
| 38 |
), |
| 39 |
); |
| 40 |
|
| 41 |
/** |
| 42 |
* Shortcode renderer. |
| 43 |
* |
| 44 |
* @var Shortcodes |
| 45 |
*/ |
| 46 |
private $shortcodes; |
| 47 |
|
| 48 |
/** |
| 49 |
* Settings service. |
| 50 |
* |
| 51 |
* @var Settings |
| 52 |
*/ |
| 53 |
private $settings; |
| 54 |
|
| 55 |
/** |
| 56 |
* Constructor. |
| 57 |
* |
| 58 |
* @param Shortcodes $shortcodes Shortcode renderer. |
| 59 |
* @param Settings $settings Settings service. |
| 60 |
*/ |
| 61 |
public function __construct( Shortcodes $shortcodes, Settings $settings ) { |
| 62 |
$this->shortcodes = $shortcodes; |
| 63 |
$this->settings = $settings; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Register block hooks. |
| 68 |
*/ |
| 69 |
public function register_hooks(): void { |
| 70 |
add_action( 'init', array( $this, 'register_block' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Register dynamic block. |
| 75 |
*/ |
| 76 |
public function register_block(): void { |
| 77 |
static $script_asset_version = null; |
| 78 |
|
| 79 |
if ( null === $script_asset_version ) { |
| 80 |
$script_file = ERECHT24_LEGAL_TEXT_PATH . 'assets/js/block.js'; |
| 81 |
$script_asset_version = is_readable( $script_file ) |
| 82 |
? (string) filemtime( $script_file ) |
| 83 |
: ERECHT24_LEGAL_TEXT_VERSION; |
| 84 |
} |
| 85 |
|
| 86 |
wp_register_script( |
| 87 |
'erecht24-legal-texts-block', |
| 88 |
ERECHT24_LEGAL_TEXT_URL . 'assets/js/block.js', |
| 89 |
array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-i18n', 'wp-server-side-render' ), |
| 90 |
$script_asset_version, |
| 91 |
true |
| 92 |
); |
| 93 |
|
| 94 |
wp_register_style( |
| 95 |
'erecht24-legal-texts-block-editor', |
| 96 |
ERECHT24_LEGAL_TEXT_URL . 'assets/css/block-editor.css', |
| 97 |
array(), |
| 98 |
ERECHT24_LEGAL_TEXT_VERSION |
| 99 |
); |
| 100 |
|
| 101 |
wp_localize_script( |
| 102 |
'erecht24-legal-texts-block', |
| 103 |
'eRecht24LegalTextBlock', |
| 104 |
array( |
| 105 |
'canRender' => true, |
| 106 |
'canSyncRemote' => $this->settings->can_render_documents(), |
| 107 |
'message' => __( 'Remote-Synchronisierung ist deaktiviert, bis ein gültiger eRecht24 API-Schlüssel gespeichert wurde. Lokale Rechtstexte können weiterhin ausgegeben werden.', 'erecht24' ), |
| 108 |
) |
| 109 |
); |
| 110 |
|
| 111 |
wp_set_script_translations( 'erecht24-legal-texts-block', 'erecht24', ERECHT24_LEGAL_TEXT_PATH . 'languages' ); |
| 112 |
|
| 113 |
$this->register_block_variant( 'legal-text', 'erecht24/legal-text' ); |
| 114 |
|
| 115 |
// Legacy alias for blocks saved by the v3 plugin as 'erecht24/erecht24'. |
| 116 |
$this->register_block_variant( 'legal-text-legacy', 'erecht24/erecht24' ); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Register one block name, preferring block.json metadata (Block API v3). |
| 121 |
* |
| 122 |
* @param string $folder Metadata folder below blocks/. |
| 123 |
* @param string $block_name Full block name, used for the metadata-less fallback. |
| 124 |
*/ |
| 125 |
private function register_block_variant( string $folder, string $block_name ): void { |
| 126 |
$settings = array( 'render_callback' => array( $this, 'render_block' ) ); |
| 127 |
$metadata_dir = ERECHT24_LEGAL_TEXT_PATH . 'blocks/' . $folder; |
| 128 |
|
| 129 |
if ( is_readable( $metadata_dir . '/block.json' ) ) { |
| 130 |
register_block_type( $metadata_dir, $settings ); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
// Fallback: array-based registration as in plugin <= 4.0.5. |
| 135 |
if ( 'erecht24/erecht24' === $block_name ) { |
| 136 |
$settings['supports'] = array( 'inserter' => false ); |
| 137 |
} |
| 138 |
|
| 139 |
register_block_type( |
| 140 |
$block_name, |
| 141 |
array_merge( |
| 142 |
$settings, |
| 143 |
array( |
| 144 |
'api_version' => 3, |
| 145 |
'editor_script' => 'erecht24-legal-texts-block', |
| 146 |
'editor_style' => 'erecht24-legal-texts-block-editor', |
| 147 |
'attributes' => self::BLOCK_ATTRIBUTES, |
| 148 |
) |
| 149 |
) |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Render dynamic block. |
| 155 |
* |
| 156 |
* @param array<string,mixed> $attributes Block attributes. |
| 157 |
*/ |
| 158 |
public function render_block( array $attributes ): string { |
| 159 |
return $this->shortcodes->render( $attributes ); |
| 160 |
} |
| 161 |
} |
| 162 |
|