3, 'supports' => array( 'html' => false, 'align' => array( 'wide', 'full' ), ), ); if ( $editor ) { $common['editor_script_handles'] = array( self::EDITOR ); } if ( $view && wp_style_is( self::HANDLE, 'registered' ) ) { $common['editor_style_handles'] = array( self::HANDLE ); } register_block_type( self::BOOKING, $common + array( 'title' => __( 'Booking form', 'schedulereed' ), 'category' => 'widgets', 'icon' => 'calendar-alt', 'render_callback' => array( $this, 'booking_block' ), 'attributes' => array( 'service' => array( 'type' => 'string', 'default' => '', ), 'staff' => array( 'type' => 'string', 'default' => '', ), 'location' => array( 'type' => 'string', 'default' => '', ), 'type' => array( 'type' => 'string', 'default' => '', ), 'layout' => array( 'type' => 'string', 'default' => 'inline', ), 'button' => array( 'type' => 'string', 'default' => '', ), ), ) ); register_block_type( self::PORTAL, $common + array( 'title' => __( 'My bookings', 'schedulereed' ), 'category' => 'widgets', 'icon' => 'id', 'render_callback' => array( $this, 'portal_block' ), ) ); } /** * Render the booking block. * * @param array $attrs Attributes. */ public function booking_block( array $attrs ): string { return self::placeholder( 'booking', $attrs, get_block_wrapper_attributes( array( 'class' => 'schedulereed schedulereed-booking' ) ) ); } /** * Render the [schedulereed] shortcode. * * @param array|string $attrs Attributes. */ public function booking_shortcode( $attrs ): string { $attrs = shortcode_atts( array( 'service' => '', 'staff' => '', 'location' => '', 'type' => '', 'layout' => 'inline', 'button' => '', ), is_array( $attrs ) ? $attrs : array(), 'schedulereed' ); return self::placeholder( 'booking', $attrs, 'class="schedulereed schedulereed-booking"' ); } public function portal_block(): string { return self::placeholder( 'portal', array(), get_block_wrapper_attributes( array( 'class' => 'schedulereed schedulereed-portal' ) ) ); } public function portal_shortcode(): string { return self::placeholder( 'portal', array(), 'class="schedulereed schedulereed-portal"' ); } /** * Print the element the widget script mounts on, and load the script. * * @param string $view booking or portal. * @param array $attrs Attributes. * @param string $attributes Escaped HTML attributes for the wrapper. */ private static function placeholder( string $view, array $attrs, string $attributes ): string { self::remember_page( $view ); if ( Assets::register( self::HANDLE, 'widget' ) ) { Assets::localize( self::HANDLE ); wp_enqueue_script( self::HANDLE ); wp_enqueue_style( self::HANDLE ); } $ids = static fn( $v ): array => array_values( array_filter( array_map( 'intval', explode( ',', (string) $v ) ) ) ); $config = array( 'view' => $view, 'service' => $ids( $attrs['service'] ?? '' ), 'staff' => (int) ( $attrs['staff'] ?? 0 ), 'location' => (int) ( $attrs['location'] ?? 0 ), 'type' => sanitize_key( (string) ( $attrs['type'] ?? '' ) ), 'layout' => 'button' === ( $attrs['layout'] ?? '' ) ? 'button' : 'inline', 'button' => sanitize_text_field( (string) ( $attrs['button'] ?? '' ) ), ); $style = Settings::appearance(); return sprintf( '
', $attributes, esc_attr( (string) wp_json_encode( $config ) ), esc_attr( $style['color'] ), (int) $style['radius'], esc_html__( 'Please turn on JavaScript to book online.', 'schedulereed' ) ); } /** * Note the published page showing the form or the portal, so emails link to it. * * @param string $view booking or portal. */ private static function remember_page( string $view ): void { if ( ! is_singular() ) { return; } $page = get_queried_object_id(); $option = 'portal' === $view ? Settings::PORTAL_PAGE : Settings::BOOKING_PAGE; $stored = (int) get_option( $option, 0 ); if ( $page && 'publish' === get_post_status( $page ) && $stored !== $page && ( 0 === $stored || 'publish' !== get_post_status( $stored ) ) ) { update_option( $option, $page, false ); } } }