renderer = $renderer; } /** * Registers the block type. */ public function register(): void { if ( ! function_exists( 'register_block_type' ) || \WP_Block_Type_Registry::get_instance()->is_registered( self::NAME ) ) { return; } wp_register_script( 'fxd-form-block', FORENSIX_URL . 'blocks/form/editor.js', array( 'wp-blocks', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-i18n' ), FORENSIX_VERSION, true ); wp_set_script_translations( 'fxd-form-block', 'forensix-digital', FORENSIX_PATH . 'languages' ); $forms = array(); if ( is_admin() ) { foreach ( ( new FormStore() )->all() as $form ) { $forms[] = array( 'id' => $form['id'], 'title' => $form['title'], ); } } wp_add_inline_script( 'fxd-form-block', 'window.forensixFormBlock = ' . wp_json_encode( array( 'forms' => $forms ) ) . ';', 'before' ); register_block_type( FORENSIX_PATH . 'blocks/form', array( 'editor_script' => 'fxd-form-block', 'render_callback' => array( $this, 'render' ), ) ); } /** * Unregisters the block type. */ public function unregister(): void { if ( function_exists( 'unregister_block_type' ) && \WP_Block_Type_Registry::get_instance()->is_registered( self::NAME ) ) { unregister_block_type( self::NAME ); } } /** * Server-side output. * * @param array $attributes Block attributes. * @phpstan-param array $attributes */ public function render( array $attributes ): string { $form_id = absint( $attributes['formId'] ?? 0 ); return $form_id > 0 ? ( $this->renderer )( $form_id ) : ''; } }