self::forms_for_editor() ] ); if ( is_string( $forms_json ) ) { wp_add_inline_script( self::EDITOR_SCRIPT, 'window.booklineBlockEditor = ' . $forms_json . ';', 'before' ); } register_block_type( self::NAME, [ 'api_version' => 3, 'title' => __( 'Buildler booking form', 'buildler' ), 'description' => __( 'Place a Buildler booking form on this page.', 'buildler' ), 'category' => 'widgets', 'icon' => 'calendar-alt', 'keywords' => [ 'booking', 'appointment', 'calendar', 'buildler' ], 'textdomain' => 'buildler', 'editor_script' => self::EDITOR_SCRIPT, 'render_callback' => [ self::class, 'render' ], 'supports' => [ 'html' => false, 'align' => [ 'wide', 'full' ], ], 'attributes' => [ 'formId' => [ 'type' => 'number', 'default' => 0, ], 'service' => [ 'type' => 'number', 'default' => 0, ], 'agent' => [ 'type' => 'number', 'default' => 0, ], 'location' => [ 'type' => 'number', 'default' => 0, ], 'mode' => [ 'type' => 'string', 'default' => 'inline', ], ], ] ); } /** * Form list for the block inspector. */ public static function localize_editor(): void { if ( ! wp_script_is( self::EDITOR_SCRIPT, 'registered' ) ) { return; } wp_localize_script( self::EDITOR_SCRIPT, 'booklineBlockEditor', [ 'forms' => self::forms_for_editor(), ] ); } /** * Load widget assets when this block is in the post. * * @param bool $enqueue Current filter value. */ public static function enqueue_when_block_present( bool $enqueue ): bool { if ( $enqueue ) { return true; } if ( ! is_singular() ) { return false; } $post = get_post(); if ( ! $post instanceof \WP_Post ) { return false; } return has_block( self::NAME, $post ); } /** * Front-end markup. Same mount point as `[bookline_booking]`. * * @param array $attributes Block attributes. */ public static function render( array $attributes ): string { $form_id = isset( $attributes['formId'] ) ? (int) $attributes['formId'] : 0; $atts = [ 'form' => (string) max( 1, $form_id ), 'service' => self::optional_id( $attributes, 'service' ), 'agent' => self::optional_id( $attributes, 'agent' ), 'location' => self::optional_id( $attributes, 'location' ), 'mode' => isset( $attributes['mode'] ) && 'inline' !== $attributes['mode'] ? sanitize_key( (string) $attributes['mode'] ) : 'inline', ]; return BookingShortcode::render( $atts ); } /** * Positive id attribute as a string, or empty. * * @param array $attributes Block attributes. * @param string $key Attribute name. */ private static function optional_id( array $attributes, string $key ): string { $id = isset( $attributes[ $key ] ) ? (int) $attributes[ $key ] : 0; return $id > 0 ? (string) $id : ''; } /** * Compact form list for the editor select. * * @return list */ private static function forms_for_editor(): array { try { $repo = Plugin::container()->get( BookingFormRepository::class ); if ( ! $repo instanceof BookingFormRepository ) { return []; } $list = $repo->list_for_admin( 100, 0 ); $out = []; foreach ( $list['items'] as $row ) { $id = (int) ( $row['id'] ?? 0 ); if ( $id < 1 ) { continue; } $out[] = [ 'id' => $id, 'name' => (string) ( $row['name'] ?? '' ), 'status' => (string) ( $row['status'] ?? '' ), ]; } return $out; } catch ( \Throwable ) { return []; } } }