get_rest_url(), // Static preview image displayed in the block inserter. 'cmplz_tc_preview' => cmplz_tc_url . 'assets/images/gutenberg-preview.png', ) ); // Register JS translations so i18n strings in the bundle are localised. wp_set_script_translations( 'cmplz-tc-block', 'complianz-terms-conditions', cmplz_tc_path . '/languages' ); } /** * Fires when block editor scripts and styles are enqueued. * * @since 1.0.0 */ add_action( 'enqueue_block_editor_assets', 'cmplz_tc_editor_assets' ); /** * Renders the Terms & Conditions block on the front end. * * Called by the block editor's server-side rendering pipeline whenever a page * containing the `complianztc/terms-conditions` block is displayed. When the * block's `documentSyncStatus` attribute is `'unlink'` and a `customDocument` * attribute is present the saved custom HTML is returned as-is, allowing the * user's manual edits to the document to be preserved. In all other cases the * live document HTML is generated fresh from the wizard configuration via * `cmplz_tc_document::get_document_html()`. * * @since 1.0.0 * @access public * * @see cmplz_tc_document::get_document_html() Generates the live T&C HTML. * @see register_block_type() Where this callback is registered. * * @param array $attributes Block attributes set in the editor, including: * - `documentSyncStatus` (string) 'sync' | 'unlink' — whether to * use the live document or a saved custom version. * - `customDocument` (string) Raw HTML of the user-edited * document; only used when `documentSyncStatus` is 'unlink'. * @param string $content Inner block content (unused for this dynamic block). * @return string The HTML string to render in place of the block. */ function cmplz_tc_render_document_block( $attributes, $content ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed -- $content required by block render_callback signature. if ( isset( $attributes['documentSyncStatus'] ) && 'unlink' === $attributes['documentSyncStatus'] && isset( $attributes['customDocument'] ) ) { // Block is unlinked from the wizard: return the user's saved custom HTML. $html = $attributes['customDocument']; } else { // Block is synced: generate fresh HTML from the wizard-configured document. $type = 'terms-conditions'; $html = COMPLIANZ_TC::$document->get_document_html( $type ); } return $html; } /** * Registers the Terms & Conditions Gutenberg block with a server-side render callback. * * The `render_callback` replaces the static `save()` output with dynamically * generated document HTML so the block always reflects the latest wizard * settings without requiring manual re-saving of each post. * * @since 1.0.0 */ register_block_type( 'complianztc/terms-conditions', array( 'render_callback' => 'cmplz_tc_render_document_block', ) );