3, 'title' => __( 'Nodebook form', 'nodebook' ), 'category' => 'widgets', 'editor_script' => 'nodebook-blocks', 'style' => 'nodebook-form', 'attributes' => array( 'phone' => array( 'type' => 'boolean', 'default' => false, ), 'company' => array( 'type' => 'boolean', 'default' => false, ), 'message' => array( 'type' => 'boolean', 'default' => true, ), 'button' => array( 'type' => 'string', 'default' => '', ), 'success' => array( 'type' => 'string', 'default' => '', ), 'newsletter' => array( 'type' => 'boolean', 'default' => true, ), 'newsletterText' => array( 'type' => 'string', 'default' => '', ), 'source' => array( 'type' => 'string', 'default' => '', ), ), 'render_callback' => array( __CLASS__, 'render_form' ), ) ); register_block_type( 'nodebook/signup', array( 'api_version' => 3, 'title' => __( 'Newsletter sign-up', 'nodebook' ), 'category' => 'widgets', 'editor_script' => 'nodebook-blocks', 'style' => 'nodebook-form', 'attributes' => array( 'name' => array( 'type' => 'boolean', 'default' => true, ), 'button' => array( 'type' => 'string', 'default' => '', ), 'promise' => array( 'type' => 'string', 'default' => '', ), 'success' => array( 'type' => 'string', 'default' => '', ), ), 'render_callback' => array( __CLASS__, 'render_signup' ), ) ); } /** Shortcode attributes from block attributes; empty strings fall back to the shortcode's defaults. */ private static function atts( array $map ) { return array_filter( $map, static function ( $v ) { return '' !== $v && null !== $v; } ); } /** * The contact form block. * * @param array $a Block attributes. */ public static function render_form( $a ) { $fields = array( 'name', 'email' ); foreach ( array( 'phone', 'company', 'message' ) as $f ) { if ( ! empty( $a[ $f ] ) ) { $fields[] = $f; } } return '
' . Nodebook_Shortcode::render( self::atts( array( 'fields' => implode( ',', $fields ), 'button' => (string) ( $a['button'] ?? '' ), 'success' => (string) ( $a['success'] ?? '' ), 'source' => (string) ( $a['source'] ?? '' ), 'subscribe' => ! empty( $a['newsletter'] ) ? 'checkbox' : '', 'subscribe_label' => (string) ( $a['newsletterText'] ?? '' ), ) ) ) . '
'; } /** * The newsletter sign-up block: sending it is the opt-in, and the promise is shown under the button. * * @param array $a Block attributes. */ public static function render_signup( $a ) { return '
' . Nodebook_Shortcode::render( self::atts( array( 'fields' => ! empty( $a['name'] ) ? 'name,email' : 'email', 'button' => '' !== (string) ( $a['button'] ?? '' ) ? (string) $a['button'] : __( 'Subscribe', 'nodebook' ), 'success' => '' !== (string) ( $a['success'] ?? '' ) ? (string) $a['success'] : __( 'You are on the list. Thank you!', 'nodebook' ), 'source' => __( 'Newsletter sign-up', 'nodebook' ), 'subscribe' => 'yes', 'subscribe_label' => '' !== (string) ( $a['promise'] ?? '' ) ? (string) $a['promise'] : __( 'One email a month, no spam. Unsubscribe any time.', 'nodebook' ), ) ) ) . '
'; } }