renderer = $renderer; } public function register(): void { add_action( 'init', array( $this, 'register_block' ) ); } public function register_block(): void { $dir = MOWL_PATH . 'build/blocks/subscribe-form'; if ( ! file_exists( $dir . '/block.json' ) ) { return; // nincs build (dev), a shortcode ettől még működik } // wp-scripts nem tud a block forrásán (src/blocks/…) kívüli fájlra // (assets/form.css) hivatkozni a block.json "style"-jában, ezért a // stíluskezelőt itt regisztráljuk és adjuk át editor_style-ként — így // a block-szerkesztő preview is megkapja a form CSS-ét. wp_register_style( 'mowl-form', MOWL_URL . 'assets/form.css', array(), MOWL_VERSION ); $block_type = register_block_type( $dir, array( 'render_callback' => array( $this, 'render' ), 'editor_style' => 'mowl-form', ) ); // A szkript fordítását a block.json "textdomain"-je regisztrálja; a JSON a // translate.wordpress.org nyelvi csomagjából (WP_LANG_DIR) töltődik. } /** A block attribútumait a shortcode-attribútum nyelvére fordítjuk. */ public function render( array $attributes ): string { $attrs = array(); if ( ! empty( $attributes['tags'] ) ) { $attrs['tags'] = implode( ',', array_map( 'intval', (array) $attributes['tags'] ) ); } $fields = array( 'email' ); if ( ! array_key_exists( 'showFirstName', $attributes ) || $attributes['showFirstName'] ) { $fields[] = 'first_name'; } if ( ! empty( $attributes['showLastName'] ) ) { $fields[] = 'last_name'; } // Csak akkor írjuk felül a beállítást, ha a block valamelyik mezőkapcsolót megadta. if ( array_key_exists( 'showFirstName', $attributes ) || array_key_exists( 'showLastName', $attributes ) ) { $attrs['fields'] = implode( ',', $fields ); } if ( array_key_exists( 'doubleOptIn', $attributes ) ) { $attrs['doi'] = $attributes['doubleOptIn'] ? '1' : '0'; } if ( array_key_exists( 'consentEnabled', $attributes ) ) { $attrs['consent'] = $attributes['consentEnabled'] ? '1' : '0'; } foreach ( array( 'consentText' => 'consent_text', 'buttonLabel' => 'button', 'successMessage' => 'success', 'pendingMessage' => 'pending', ) as $from => $to ) { if ( ! empty( $attributes[ $from ] ) ) { $attrs[ $to ] = (string) $attributes[ $from ]; } } return $this->renderer->render( $attrs, 'block' ); } }