renderer = $renderer; } /** * Registers the hooks. */ public function register_hooks() { add_action( 'init', array( $this, 'register' ) ); } /** * Registers the block from its built metadata. */ public function register() { $dir = PRIMIKO_DIR . 'build/block'; if ( ! is_readable( $dir . '/block.json' ) ) { return; } register_block_type( $dir, array( 'render_callback' => array( $this, 'render' ), ) ); wp_set_script_translations( 'primiko-form-editor-script', 'primiko', PRIMIKO_DIR . 'languages' ); } /** * Renders the block. * * @param array $attributes Block attributes. * @return string */ public function render( $attributes ) { $html = $this->renderer->render( isset( $attributes['formId'] ) ? absint( $attributes['formId'] ) : 0, isset( $attributes['headingLevel'] ) ? (int) $attributes['headingLevel'] : 3 ); if ( '' === $html ) { return ''; } $style = ''; $color = isset( $attributes['accentColor'] ) ? sanitize_hex_color( $attributes['accentColor'] ) : ''; if ( $color ) { $style .= '--primiko-accent:' . $color . ';'; } if ( isset( $attributes['radius'] ) && is_numeric( $attributes['radius'] ) ) { $style .= '--primiko-radius:' . max( 0, min( 24, (int) $attributes['radius'] ) ) . 'px;'; } if ( isset( $attributes['width'] ) && 'full' === $attributes['width'] ) { $style .= '--primiko-width:none;'; } // The variables sit on the wrapper and reach the form through inheritance. $extra = $style ? array( 'style' => $style ) : array(); if ( ! empty( $attributes['columns'] ) ) { $extra['class'] = 'primiko-columns'; } $wrapper = get_block_wrapper_attributes( $extra ); $anchor = isset( $attributes['anchor'] ) ? sanitize_html_class( $attributes['anchor'] ) : ''; // WordPress 7.0 sets the HTML anchor of dynamic blocks itself. Before, it is added here, once. if ( '' !== $anchor && false === strpos( $wrapper, ' id="' ) && 0 !== strpos( $wrapper, 'id="' ) ) { $wrapper .= ' id="' . esc_attr( $anchor ) . '"'; } return '
' . $html . '
'; } }