Plugin Directory

Changeset 3833155


Ignore:
Timestamp:
11/29/2026 02:18:53 PM (less than one hour ago)
Author:
finesfinest
Message:

Update to version 1.5.0 from GitHub

Location:
fines-finest-booking
Files:
18 edited
1 copied

Legend:

Unmodified
Added
Removed
  • fines-finest-booking/tags/1.5.0/assets/ffb-admin.css

    r3831478 r3833155  
    237237        }
    238238}
     239
     240/* ---------- Termintyp: Geschaeftszeiten uebernehmen und Vorschau ---------- */
     241.ffb-admin .ffb-copy-hours {
     242        display: flex;
     243        align-items: center;
     244        flex-wrap: wrap;
     245        gap: .4em .8em;
     246        margin: 0 0 1em;
     247}
     248
     249.ffb-admin .ffb-preview__list {
     250        max-width: 46em;
     251        margin: .75em 0 1em;
     252        padding: .75em 1em;
     253        background: #fff;
     254        border: 1px solid #dcdcde;
     255        border-radius: 2px;
     256}
     257
     258.ffb-admin .ffb-preview__list[aria-busy="true"] { opacity: .6; }
     259
     260.ffb-admin .ffb-preview__days {
     261        display: grid;
     262        grid-template-columns: max-content 1fr;
     263        gap: .35em 1.2em;
     264        margin: 0;
     265}
     266
     267.ffb-admin .ffb-preview__days dt { font-weight: 600; }
     268
     269.ffb-admin .ffb-preview__days dd { margin: 0; }
     270
     271.ffb-admin .ffb-preview__note { margin: 0 0 .6em; color: #996800; }
     272
     273.ffb-admin .ffb-preview__empty { margin: 0; }
     274
     275.ffb-admin .ffb-preview__error { margin: 0; color: #d63638; }
     276
     277@media (max-width: 600px) {
     278        .ffb-admin .ffb-preview__days { grid-template-columns: 1fr; }
     279        .ffb-admin .ffb-preview__days dd { margin-bottom: .5em; }
     280}
  • fines-finest-booking/tags/1.5.0/assets/ffb-admin.js

    r3831478 r3833155  
    216216                }, true );
    217217        }() );
     218
     219        // Termintyp: Geschaeftszeiten uebernehmen und Vorschau der freien Zeiten.
     220        ( function () {
     221                var preview = document.querySelector( '[data-ffb-preview]' );
     222                var form = preview ? preview.closest( 'form' ) : null;
     223                var copyButton = document.querySelector( '[data-ffb-copy-hours]' );
     224                var timer = null;
     225                var request = 0;
     226
     227                function select( name ) {
     228                        return form ? form.querySelector( 'select[name="ffb[' + name + ']"]' ) : null;
     229                }
     230
     231                if ( copyButton && form ) {
     232                        copyButton.addEventListener( 'click', function () {
     233                                var hours;
     234                                try {
     235                                        hours = JSON.parse( copyButton.getAttribute( 'data-ffb-copy-hours' ) || '{}' );
     236                                } catch ( e ) {
     237                                        return;
     238                                }
     239                                for ( var day = 1; day <= 7; day++ ) {
     240                                        var windows = hours[ String( day ) ] || [];
     241                                        for ( var i = 0; i < 2; i++ ) {
     242                                                var von = select( 'day_' + day + '_von_' + i );
     243                                                var bis = select( 'day_' + day + '_bis_' + i );
     244                                                if ( von ) {
     245                                                        von.value = windows[ i ] ? windows[ i ][ 0 ] : '';
     246                                                }
     247                                                if ( bis ) {
     248                                                        bis.value = windows[ i ] ? windows[ i ][ 1 ] : '';
     249                                                }
     250                                        }
     251                                }
     252                                var status = document.querySelector( '[data-ffb-copy-hours-status]' );
     253                                if ( status ) {
     254                                        status.textContent = texte.hoursCopied || '';
     255                                }
     256                                schedule( 0 );
     257                        } );
     258                }
     259
     260                if ( ! preview || ! form || ! window.fetch || ! window.FormData ) {
     261                        return;
     262                }
     263
     264                var list = preview.querySelector( '[data-ffb-preview-list]' );
     265
     266                function message( text, isError ) {
     267                        list.innerHTML = '';
     268                        var p = document.createElement( 'p' );
     269                        p.className = isError ? 'ffb-preview__error' : 'ffb-preview__empty';
     270                        p.textContent = text;
     271                        list.appendChild( p );
     272                }
     273
     274                function render( data ) {
     275                        list.innerHTML = '';
     276                        ( data.notes || [] ).forEach( function ( note ) {
     277                                var p = document.createElement( 'p' );
     278                                p.className = 'ffb-preview__note';
     279                                p.textContent = note;
     280                                list.appendChild( p );
     281                        } );
     282                        if ( ! data.days || ! data.days.length ) {
     283                                var leer = document.createElement( 'p' );
     284                                leer.className = 'ffb-preview__empty';
     285                                leer.textContent = data.empty || '';
     286                                list.appendChild( leer );
     287                                return;
     288                        }
     289                        var dl = document.createElement( 'dl' );
     290                        dl.className = 'ffb-preview__days';
     291                        data.days.forEach( function ( day ) {
     292                                var dt = document.createElement( 'dt' );
     293                                dt.textContent = day.label;
     294                                var dd = document.createElement( 'dd' );
     295                                dd.textContent = day.slots.join( ', ' );
     296                                dl.appendChild( dt );
     297                                dl.appendChild( dd );
     298                        } );
     299                        list.appendChild( dl );
     300                }
     301
     302                function load() {
     303                        var body = new FormData( form );
     304                        body.delete( 'ffbooking_action' );
     305                        body.delete( '_wpnonce' );
     306                        body.append( 'action', 'ffbooking_preview_slots' );
     307                        body.append( 'nonce', texte.previewNonce || '' );
     308
     309                        var mine = ++request;
     310                        list.setAttribute( 'aria-busy', 'true' );
     311                        if ( ! list.firstChild ) {
     312                                message( texte.previewLoading || '', false );
     313                        }
     314
     315                        fetch( texte.ajaxUrl, { method: 'POST', credentials: 'same-origin', body: body } )
     316                                .then( function ( response ) {
     317                                        return response.json().catch( function () {
     318                                                return null;
     319                                        } );
     320                                } )
     321                                .then( function ( result ) {
     322                                        // Eine aeltere Antwort darf eine neuere nicht ueberschreiben.
     323                                        if ( mine !== request ) {
     324                                                return;
     325                                        }
     326                                        list.removeAttribute( 'aria-busy' );
     327                                        if ( result && result.success ) {
     328                                                render( result.data );
     329                                        } else {
     330                                                message( ( result && result.data && result.data.message ) || texte.previewError || '', true );
     331                                        }
     332                                } )
     333                                .catch( function () {
     334                                        if ( mine === request ) {
     335                                                list.removeAttribute( 'aria-busy' );
     336                                                message( texte.previewError || '', true );
     337                                        }
     338                                } );
     339                }
     340
     341                function schedule( delay ) {
     342                        window.clearTimeout( timer );
     343                        timer = window.setTimeout( load, delay );
     344                }
     345
     346                form.addEventListener( 'change', function () {
     347                        schedule( 400 );
     348                } );
     349                form.addEventListener( 'input', function ( event ) {
     350                        if ( 'number' === event.target.type || 'ffb-blackouts' === event.target.id ) {
     351                                schedule( 800 );
     352                        }
     353                } );
     354
     355                load();
     356        }() );
    218357}() );
  • fines-finest-booking/tags/1.5.0/assets/ffb-frontend.css

    r3818359 r3833155  
    494494        }
    495495}
     496
     497/* Vorschau aus dem Backend */
     498.ffb .ffb-preview-note {
     499        margin: 0 0 1rem;
     500        border-left: 3px solid var(--ffb-accent);
     501}
  • fines-finest-booking/tags/1.5.0/assets/ffb-frontend.js

    r3818359 r3833155  
    8484                this.days = parseInt( root.getAttribute( 'data-days' ), 10 ) || 45;
    8585                this.quelle = root.getAttribute( 'data-quelle' ) || '';
     86                // Vorschau aus dem Backend: zeigt alles wie fuer Kund*innen, bucht aber nichts.
     87                this.preview = '1' === root.getAttribute( 'data-preview' );
    8688                this.data = null;
    8789                this.selectedDate = null;
    … …  
    103105                this.root.appendChild( el( 'div', 'ffb-loading', t( 'loading', 'Loading appointments …' ) ) );
    104106
    105                 getSession().catch( function () {} );
    106 
    107                 api( '/slots?event=' + encodeURIComponent( this.slug ) + '&days=' + this.days )
     107                var path = '/slots?event=' + encodeURIComponent( this.slug ) + '&days=' + this.days;
     108                var options;
     109
     110                if ( this.preview ) {
     111                        path += '&preview=1';
     112                        options = { credentials: 'same-origin', headers: { 'X-WP-Nonce': config.nonce || '' } };
     113                } else {
     114                        getSession().catch( function () {} );
     115                }
     116
     117                api( path, options )
    108118                        .then( function ( data ) {
    109119                                self.data = data;
    … …  
    269279                var submit = el( 'button', 'ffb-btn ffb-btn--primary', t( 'submit', 'Book appointment' ) );
    270280                submit.type = 'submit';
     281                if ( this.preview ) {
     282                        submit.disabled = true;
     283                        submit.textContent = t( 'previewSubmit', 'Preview: booking is switched off' );
     284                }
    271285                form.appendChild( submit );
    272286
    … …  
    277291                form.addEventListener( 'submit', function ( event ) {
    278292                        event.preventDefault();
     293                        if ( self.preview ) {
     294                                return;
     295                        }
    279296                        self.submit( form, submit, message );
    280297                } );
  • fines-finest-booking/tags/1.5.0/fines-finest-booking.php

    r3831478 r3833155  
    44 * Plugin URI:        https://github.com/FinesFinest/fines-finest-booking
    55 * Description:       Appointment booking in your own design, with Google Calendar sync, Google Meet, confirmation emails and Brevo. All data stays in your own WordPress database.
    6  * Version:           1.4.2
     6 * Version:           1.5.0
    77 * Requires at least: 6.0
    88 * Requires PHP:      7.4
    … …  
    1717}
    1818
    19 define( 'FFBOOKING_VERSION', '1.4.2' );
     19define( 'FFBOOKING_VERSION', '1.5.0' );
    2020define( 'FFBOOKING_FILE', __FILE__ );
    2121define( 'FFBOOKING_DIR', plugin_dir_path( __FILE__ ) );
  • fines-finest-booking/tags/1.5.0/includes/class-ffb-admin.php

    r3831478 r3833155  
    3939                add_action( 'admin_init', array( __CLASS__, 'handle_actions' ) );
    4040                add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) );
     41                add_action( 'wp_ajax_ffbooking_preview_slots', array( __CLASS__, 'ajax_preview_slots' ) );
    4142                add_action( 'admin_notices', array( __CLASS__, 'activation_notice' ) );
    4243                add_action( 'admin_notices', array( 'FFBooking_Install', 'admin_notices' ) );
    … …  
    132133                        'FFBOOKING_ADMIN',
    133134                        array(
    134                                 'copied'      => __( 'Copied', 'fines-finest-booking' ),
     135                                'copied'         => __( 'Copied', 'fines-finest-booking' ),
    135136                                /* translators: %s: the ending of every Google client ID */
    136                                 'idSuffix'    => sprintf( __( 'The client ID ends with %s. Copy it again in full.', 'fines-finest-booking' ), FFBooking_Google_Check::CLIENT_SUFFIX ),
    137                                 'idSwapped'   => __( 'This looks like the client secret. Enter the client ID here and the secret in the field below.', 'fines-finest-booking' ),
    138                                 'secretIsId'  => __( 'This looks like the client ID. Enter the client secret here, it usually starts with GOCSPX-.', 'fines-finest-booking' ),
    139                                 'spaces'      => __( 'There is a space or line break in it. Copy the value again.', 'fines-finest-booking' ),
     137                                'idSuffix'       => sprintf( __( 'The client ID ends with %s. Copy it again in full.', 'fines-finest-booking' ), FFBooking_Google_Check::CLIENT_SUFFIX ),
     138                                'idSwapped'      => __( 'This looks like the client secret. Enter the client ID here and the secret in the field below.', 'fines-finest-booking' ),
     139                                'secretIsId'     => __( 'This looks like the client ID. Enter the client secret here, it usually starts with GOCSPX-.', 'fines-finest-booking' ),
     140                                'spaces'         => __( 'There is a space or line break in it. Copy the value again.', 'fines-finest-booking' ),
     141                                'ajaxUrl'        => admin_url( 'admin-ajax.php' ),
     142                                'previewNonce'   => wp_create_nonce( 'ffbooking_preview' ),
     143                                'previewLoading' => __( 'Calculating free times …', 'fines-finest-booking' ),
     144                                'previewError'   => __( 'The preview could not be loaded.', 'fines-finest-booking' ),
     145                                'hoursCopied'    => __( 'Business hours copied. Save to keep them.', 'fines-finest-booking' ),
    140146                        )
    141147                );
    … …  
    480486                $id    = isset( $_POST['ffbooking_id'] ) ? (int) $_POST['ffbooking_id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce prueft handle_actions().
    481487
     488                $result = FFBooking_Event_Types::save( self::event_data_from_input( (array) $input ), $id );
     489
     490                if ( is_wp_error( $result ) ) {
     491                        set_transient( 'ffbooking_admin_error', $result->get_error_message(), 60 );
     492                        wp_safe_redirect( admin_url( 'admin.php?page=ffbooking-event-types&ffbooking_notice=error' ) );
     493                        exit;
     494                }
     495
     496                wp_safe_redirect( admin_url( 'admin.php?page=ffbooking-event-types&edit=' . (int) $result . '&ffbooking_notice=saved' ) );
     497                exit;
     498        }
     499
     500        /**
     501         * Macht aus den Feldern des Termintyp-Formulars die Daten fuer FFBooking_Event_Types::save().
     502         *
     503         * Speichern und Vorschau nutzen dieselbe Umwandlung. So zeigt die Vorschau genau das,
     504         * was nach dem Speichern gilt.
     505         *
     506         * @param array $input Rohes Formular-Array, bereits ohne Schraegstriche.
     507         * @return array
     508         */
     509        public static function event_data_from_input( $input ) {
    482510                $availability = array();
    483511                foreach ( array_keys( self::weekdays() ) as $day ) {
    … …  
    493521                $questions = self::parse_questions( isset( $input['questions'] ) ? (string) $input['questions'] : '' );
    494522
    495                 $data = array(
     523                return array(
    496524                        'title'            => $input['title'] ?? '',
    497525                        'slug'             => $input['slug'] ?? '',
    … …  
    514542                        'sort_order'       => $input['sort_order'] ?? 0,
    515543                );
    516 
    517                 $result = FFBooking_Event_Types::save( $data, $id );
    518 
    519                 if ( is_wp_error( $result ) ) {
    520                         set_transient( 'ffbooking_admin_error', $result->get_error_message(), 60 );
    521                         wp_safe_redirect( admin_url( 'admin.php?page=ffbooking-event-types&ffbooking_notice=error' ) );
    522                         exit;
    523                 }
    524 
    525                 wp_safe_redirect( admin_url( 'admin.php?page=ffbooking-event-types&edit=' . (int) $result . '&ffbooking_notice=saved' ) );
    526                 exit;
     544        }
     545
     546        /**
     547         * Vorschau der freien Zeiten aus dem noch nicht gespeicherten Formular (admin-ajax).
     548         *
     549         * Rechnet mit denselben Regeln wie die Buchungsseite: Geschaeftszeiten, Vorlauf, Puffer,
     550         * Sperrtage, belegte Zeiten aus Google und bestehende Buchungen. Speichert nichts.
     551         */
     552        public static function ajax_preview_slots() {
     553                check_ajax_referer( 'ffbooking_preview', 'nonce' );
     554                if ( ! current_user_can( 'manage_options' ) ) {
     555                        wp_send_json_error( array( 'message' => __( 'You are not allowed to do this.', 'fines-finest-booking' ) ), 403 );
     556                }
     557                if ( ! FFBooking_Install::ready() ) {
     558                        wp_send_json_error( array( 'message' => __( 'The database tables of the plugin are missing.', 'fines-finest-booking' ) ), 500 );
     559                }
     560
     561                $input = isset( $_POST['ffb'] ) ? wp_unslash( $_POST['ffb'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- Die Werte bereinigen event_data_from_input() und preview_event().
     562                $id    = isset( $_POST['ffbooking_id'] ) ? (int) $_POST['ffbooking_id'] : 0;
     563
     564                $event = self::preview_event( self::event_data_from_input( (array) $input ), $id );
     565                $days  = 14;
     566                $data  = FFBooking_Availability::get_slots( $event, '', $days );
     567
     568                if ( is_wp_error( $data ) ) {
     569                        wp_send_json_error( array( 'message' => __( 'The busy times from Google Calendar could not be loaded, so no preview is possible right now. The reason is in the error log in the settings.', 'fines-finest-booking' ) ), 503 );
     570                }
     571
     572                $out = array();
     573                foreach ( $data['days'] as $day ) {
     574                        $date   = new DateTimeImmutable( $day['date'] . ' 12:00:00', ffbooking_timezone() );
     575                        $labels = array();
     576                        foreach ( $day['slots'] as $slot ) {
     577                                $labels[] = $slot['label'];
     578                        }
     579                        $out[] = array(
     580                                /* translators: date format for the slot preview, see https://www.php.net/manual/datetime.format.php */
     581                                'label' => wp_date( __( 'D, M j', 'fines-finest-booking' ), $date->getTimestamp() ),
     582                                'slots' => $labels,
     583                        );
     584                }
     585
     586                $notes = array();
     587                if ( ! FFBooking_Google::is_connected() ) {
     588                        $notes[] = __( 'Google Calendar is not connected. Busy times from your calendar are not taken into account.', 'fines-finest-booking' );
     589                }
     590                if ( empty( $event['active'] ) ) {
     591                        $notes[] = __( 'This appointment type is not bookable at the moment. On the booking page nobody sees these times yet.', 'fines-finest-booking' );
     592                }
     593
     594                wp_send_json_success(
     595                        array(
     596                                'days'  => $out,
     597                                'empty' => empty( $out ) ? self::preview_empty_reason( $event ) : '',
     598                                'notes' => $notes,
     599                        )
     600                );
     601        }
     602
     603        /**
     604         * Baut aus den Formulardaten einen Termintyp, wie ihn FFBooking_Event_Types::get() liefern wuerde.
     605         *
     606         * @param array $data Ergebnis von event_data_from_input().
     607         * @param int   $id   ID des Termintyps, 0 fuer neu.
     608         * @return array
     609         */
     610        public static function preview_event( $data, $id ) {
     611                // Gleiche Untergrenzen wie FFBooking_Event_Types::save().
     612                $zahl = static function ( $value, $min ) {
     613                        return max( $min, (int) $value );
     614                };
     615
     616                return array(
     617                        'id'            => (int) $id,
     618                        'slug'          => '',
     619                        'title'         => sanitize_text_field( (string) $data['title'] ),
     620                        'duration'      => $zahl( $data['duration'], 5 ),
     621                        'slot_step'     => $zahl( $data['slot_step'], 5 ),
     622                        'buffer_before' => $zahl( $data['buffer_before'], 0 ),
     623                        'buffer_after'  => $zahl( $data['buffer_after'], 0 ),
     624                        'min_notice'    => $zahl( $data['min_notice'], 0 ),
     625                        'max_days'      => $zahl( $data['max_days'], 1 ),
     626                        'daily_limit'   => $zahl( $data['daily_limit'], 0 ),
     627                        'availability'  => FFBooking_Event_Types::normalize_availability( $data['availability'] ),
     628                        'blackouts'     => FFBooking_Event_Types::clean_blackouts( $data['blackouts'] ),
     629                        'active'        => empty( $data['active'] ) ? 0 : 1,
     630                );
     631        }
     632
     633        /**
     634         * Nennt den wahrscheinlichsten Grund, warum die Vorschau keine Zeiten zeigt.
     635         *
     636         * @param array $event Termintyp aus preview_event().
     637         * @return string
     638         */
     639        public static function preview_empty_reason( $event ) {
     640                $has_windows = false;
     641                foreach ( $event['availability'] as $windows ) {
     642                        if ( ! empty( $windows ) ) {
     643                                $has_windows = true;
     644                                break;
     645                        }
     646                }
     647                if ( ! $has_windows ) {
     648                        return __( 'No free times in the next 14 days: no time windows are set for any weekday.', 'fines-finest-booking' );
     649                }
     650
     651                $global = FFBooking_Availability::global_limits();
     652                if ( $global['enforce'] ) {
     653                        $overlap = false;
     654                        foreach ( $event['availability'] as $day => $windows ) {
     655                                $hours = isset( $global['hours'][ $day ] ) ? $global['hours'][ $day ] : array();
     656                                if ( FFBooking_Availability::intersect_windows( $windows, $hours ) ) {
     657                                        $overlap = true;
     658                                        break;
     659                                }
     660                        }
     661                        if ( ! $overlap ) {
     662                                return __( 'No free times in the next 14 days: the time windows lie completely outside the business hours from the settings.', 'fines-finest-booking' );
     663                        }
     664                }
     665
     666                return __( 'No free times in the next 14 days. Check the minimum notice, the blocked days and your Google Calendar.', 'fines-finest-booking' );
     667        }
     668
     669        /**
     670         * Geschaeftszeiten aus den Einstellungen fuer den Knopf "Geschaeftszeiten uebernehmen".
     671         *
     672         * @return array Wochentag 1 bis 7 => Liste von [von, bis]. Leer, wenn keine eingetragen sind.
     673         */
     674        public static function business_hours() {
     675                $hours = array();
     676                $any   = false;
     677                for ( $day = 1; $day <= 7; $day++ ) {
     678                        $hours[ (string) $day ] = array_slice( self::parse_windows( ffbooking_get_setting( 'hours_' . $day, '' ) ), 0, 2 );
     679                        if ( $hours[ (string) $day ] ) {
     680                                $any = true;
     681                        }
     682                }
     683
     684                return $any ? $hours : array();
     685        }
     686
     687        /**
     688         * Adresse, unter der Fine den Buchungskalender eines Termintyps so sieht wie Kund*innen.
     689         *
     690         * Ist eine Buchungsseite eingetragen, zeigt sie die Vorschau im Design der Website.
     691         * Sonst liefert das Plugin eine schlanke eigene Seite.
     692         *
     693         * @param int $id ID des Termintyps.
     694         * @return string
     695         */
     696        public static function preview_url( $id ) {
     697                $base = (string) ffbooking_get_setting( 'booking_page_url' );
     698                if ( '' === $base ) {
     699                        $base = home_url( '/' );
     700                }
     701
     702                return add_query_arg( 'ffbooking_preview', (int) $id, $base );
    527703        }
    528704
    … …  
    13111487                                                <th scope="row"><?php esc_html_e( 'Time windows', 'fines-finest-booking' ); ?></th>
    13121488                                                <td>
     1489                                                        <p class="description" style="margin:0 0 1em;"><?php esc_html_e( 'When this appointment type can be booked. The business hours in the settings apply to all appointment types on top of this.', 'fines-finest-booking' ); ?></p>
     1490                                                        <?php $ffbooking_hours = self::business_hours(); ?>
     1491                                                        <p class="ffb-copy-hours">
     1492                                                                <button type="button" class="button" data-ffb-copy-hours="<?php echo esc_attr( wp_json_encode( $ffbooking_hours ) ); ?>" <?php disabled( empty( $ffbooking_hours ) ); ?>><?php esc_html_e( 'Copy business hours', 'fines-finest-booking' ); ?></button>
     1493                                                                <span class="description" data-ffb-copy-hours-status role="status">
     1494                                                                <?php
     1495                                                                if ( empty( $ffbooking_hours ) ) {
     1496                                                                        echo wp_kses(
     1497                                                                                sprintf(
     1498                                                                                        /* translators: %s: link "Times and limits" to the settings tab */
     1499                                                                                        __( 'No business hours are set yet. You can enter them under %s.', 'fines-finest-booking' ),
     1500                                                                                        '<a href="' . esc_url( admin_url( 'admin.php?page=ffbooking-settings#ffb-tab-limits' ) ) . '">' . esc_html__( 'Times and limits', 'fines-finest-booking' ) . '</a>'
     1501                                                                                ),
     1502                                                                                array( 'a' => array( 'href' => array() ) )
     1503                                                                        );
     1504                                                                } else {
     1505                                                                        esc_html_e( 'Fills in the time windows below with the business hours from the settings. You can adjust them afterwards.', 'fines-finest-booking' );
     1506                                                                }
     1507                                                                ?>
     1508                                                                </span>
     1509                                                        </p>
    13131510                                                        <?php foreach ( self::weekdays() as $num => $label ) : ?>
    13141511                                                                <p style="margin:0 0 .5em;">
    … …  
    14021599                                                        &nbsp;&nbsp;
    14031600                                                        <label><?php esc_html_e( 'Order', 'fines-finest-booking' ); ?> <input name="ffb[sort_order]" type="number" value="<?php echo (int) $event['sort_order']; ?>" class="small-text"></label>
     1601                                                </td>
     1602                                        </tr>
     1603                                        <tr>
     1604                                                <th scope="row"><?php esc_html_e( 'Preview', 'fines-finest-booking' ); ?></th>
     1605                                                <td>
     1606                                                        <div class="ffb-preview" data-ffb-preview>
     1607                                                                <p class="description"><?php esc_html_e( 'Free times of the next 14 days with the values in this form, even before saving. Business hours, buffers, minimum notice, blocked days, your Google Calendar and existing bookings are taken into account.', 'fines-finest-booking' ); ?></p>
     1608                                                                <div class="ffb-preview__list" data-ffb-preview-list aria-live="polite"></div>
     1609                                                                <p>
     1610                                                                        <?php if ( $id ) : ?>
     1611                                                                                <a class="button" href="<?php echo esc_url( self::preview_url( $id ) ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'View booking calendar', 'fines-finest-booking' ); ?></a>
     1612                                                                                <span class="description"><?php esc_html_e( 'Opens a new tab with the booking calendar as visitors see it, in the saved state. Nothing can be booked there.', 'fines-finest-booking' ); ?></span>
     1613                                                                        <?php else : ?>
     1614                                                                                <span class="description"><?php esc_html_e( 'After the first save you can view the booking calendar here as visitors see it.', 'fines-finest-booking' ); ?></span>
     1615                                                                        <?php endif; ?>
     1616                                                                </p>
     1617                                                        </div>
    14041618                                                </td>
    14051619                                        </tr>
    … …  
    16331847                                <h2><?php esc_html_e( 'Limits for all appointment types', 'fines-finest-booking' ); ?></h2>
    16341848                                <p class="description" style="max-width:46em;"><?php esc_html_e( 'These values apply in addition to the settings of the individual appointment types and can only restrict further, never extend. No appointment type can override them.', 'fines-finest-booking' ); ?></p>
     1849                                <p class="description" style="max-width:46em;"><?php esc_html_e( 'In each appointment type, the button "Copy business hours" takes over these times as its time windows with one click.', 'fines-finest-booking' ); ?></p>
    16351850                                <table class="form-table" role="presentation">
    16361851                                        <tr>
  • fines-finest-booking/tags/1.5.0/includes/class-ffb-rest.php

    r3818359 r3833155  
    210210                $event = FFBooking_Event_Types::get_by_slug( (string) $request->get_param( 'event' ) );
    211211
    212                 if ( ! $event || ! $event['active'] ) {
     212                // Die Vorschau aus dem Backend zeigt auch Termintypen, die noch nicht buchbar sind.
     213                // Das gilt nur fuer angemeldete Admins, die Anfrage traegt dafuer den REST-Nonce.
     214                $preview = $request->get_param( 'preview' ) && current_user_can( 'manage_options' );
     215
     216                if ( ! $event || ( ! $event['active'] && ! $preview ) ) {
    213217                        return new WP_Error( 'ffbooking_unknown_event', __( 'This appointment type does not exist.', 'fines-finest-booking' ), array( 'status' => 404 ) );
    214218                }
  • fines-finest-booking/tags/1.5.0/includes/class-ffb-shortcode.php

    r3818359 r3833155  
    3333                // Prioritaet 1: vor den Weiterleitungen von WordPress, die einen alten Link sonst umbiegen oder verlieren.
    3434                add_action( 'template_redirect', array( __CLASS__, 'maybe_standalone_cancel' ), 1 );
     35                add_action( 'template_redirect', array( __CLASS__, 'maybe_standalone_preview' ), 1 );
     36        }
     37
     38        /**
     39         * Termintyp fuer die Vorschau aus dem Backend, nur fuer angemeldete Admins.
     40         *
     41         * @return array|null
     42         */
     43        public static function preview_event() {
     44                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nur Anzeige, geschuetzt durch die Rechtepruefung.
     45                $id = isset( $_GET['ffbooking_preview'] ) ? absint( wp_unslash( $_GET['ffbooking_preview'] ) ) : 0;
     46                if ( ! $id || ! current_user_can( 'manage_options' ) || ! FFBooking_Install::ready() ) {
     47                        return null;
     48                }
     49
     50                return FFBooking_Event_Types::get( $id );
     51        }
     52
     53        /**
     54         * Vorschau-Adresse ausserhalb der Buchungsseite: eigene, schlanke Seite.
     55         * Auf der Buchungsseite zeigt der Shortcode die Vorschau im Design der Website.
     56         */
     57        public static function maybe_standalone_preview() {
     58                if ( is_admin() ) {
     59                        return;
     60                }
     61                $event = self::preview_event();
     62                if ( ! $event || ( self::is_booking_page_request() && ! post_password_required() ) ) {
     63                        return;
     64                }
     65
     66                nocache_headers();
     67                status_header( 200 );
     68                header( 'Content-Type: text/html; charset=utf-8' );
     69                ?>
     70<!DOCTYPE html>
     71<html <?php language_attributes(); ?>>
     72<head>
     73        <meta charset="<?php bloginfo( 'charset' ); ?>">
     74        <meta name="viewport" content="width=device-width, initial-scale=1">
     75        <meta name="robots" content="noindex, nofollow">
     76        <title>
     77        <?php
     78        /* translators: %s: name of the appointment type */
     79        echo esc_html( sprintf( __( 'Preview: %s', 'fines-finest-booking' ), $event['title'] ) );
     80        ?>
     81        </title>
     82        <?php
     83        self::enqueue();
     84        wp_add_inline_style( 'ffbooking-frontend', 'body{margin:0;padding:3rem 1.25rem;background:' . ffbooking_get_color( 'color_bg_deep' ) . ';}.ffb{max-width:760px;margin:0 auto;}' );
     85        wp_print_styles( array( 'ffbooking-frontend' ) );
     86        ?>
     87</head>
     88<body>
     89        <?php echo self::render_widget( $event, 45, '', true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render_widget() escaped jede Ausgabe selbst. ?>
     90        <?php wp_print_scripts( array( 'ffbooking-frontend' ) ); ?>
     91</body>
     92</html>
     93                <?php
     94                exit;
    3595        }
    3696
    … …  
    258318                }
    259319
    260                 return array(
     320                $config = array(
    261321                        'root'          => esc_url_raw( rest_url( FFBooking_Rest::NS ) ),
    262322                        'consentText'   => ffbooking_get_setting( 'consent_text' ),
    … …  
    300360                                /* translators: 1: weekday name, 2: day of month, 3: month name */
    301361                                'longDate'        => __( '%1$s, %3$s %2$d', 'fines-finest-booking' ),
     362                                'previewSubmit'   => __( 'Preview: booking is switched off', 'fines-finest-booking' ),
    302363                        ),
    303364                );
     365
     366                // Nur in der Vorschau: Mit dem REST-Nonce erkennt die Schnittstelle den angemeldeten Admin.
     367                if ( self::preview_event() ) {
     368                        $config['nonce'] = wp_create_nonce( 'wp_rest' );
     369                }
     370
     371                return $config;
    304372        }
    305373
    … …  
    361429                if ( $cancel_token ) {
    362430                        return self::render_cancel( $cancel_token );
     431                }
     432
     433                // Vorschau aus dem Backend: zeigt den gewaehlten Termintyp, auch wenn er noch nicht buchbar ist.
     434                $preview = self::preview_event();
     435                if ( $preview ) {
     436                        return self::render_widget( $preview, (int) $atts['days'], '', true );
    363437                }
    364438
    … …  
    446520         * @param int    $days   Vorschau-Tage.
    447521         * @param string $quelle Herkunft aus dem Shortcode.
     522         * @param bool   $preview Vorschau aus dem Backend: zeigt einen Hinweis, Buchen ist gesperrt.
    448523         * @return string
    449524         */
    450         protected static function render_widget( $event, $days, $quelle = '' ) {
     525        protected static function render_widget( $event, $days, $quelle = '', $preview = false ) {
    451526                $notify = (string) ffbooking_get_setting( 'notify_email' );
    452527                $link   = '<a href="mailto:' . esc_attr( $notify ) . '">' . esc_html( $notify ) . '</a>';
    … …  
    455530                ?>
    456531                <div class="ffb">
    457                         <div class="ffb-widget" data-ffb-widget data-event="<?php echo esc_attr( $event['slug'] ); ?>" data-days="<?php echo (int) max( 7, $days ); ?>" data-quelle="<?php echo esc_attr( self::resolve_source( $quelle ) ); ?>">
     532                        <?php if ( $preview ) : ?>
     533                                <p class="ffb-note ffb-preview-note">
     534                                <?php
     535                                echo esc_html(
     536                                        $event['active']
     537                                                ? __( 'Preview for you as admin. Visitors see the same calendar. Nothing can be booked here.', 'fines-finest-booking' )
     538                                                : __( 'Preview for you as admin. This appointment type is not bookable yet, visitors do not see it. Nothing can be booked here.', 'fines-finest-booking' )
     539                                );
     540                                ?>
     541                                </p>
     542                        <?php endif; ?>
     543                        <div class="ffb-widget" data-ffb-widget data-event="<?php echo esc_attr( $event['slug'] ); ?>" data-days="<?php echo (int) max( 7, $days ); ?>" data-quelle="<?php echo esc_attr( self::resolve_source( $quelle ) ); ?>"<?php echo $preview ? ' data-preview="1"' : ''; ?>>
    458544                                <div class="ffb-loading"><?php echo esc_html__( 'Loading appointments …', 'fines-finest-booking' ); ?></div>
    459545                        </div>
  • fines-finest-booking/tags/1.5.0/readme.txt

    r3831478 r3833155  
    66Tested up to: 7.2
    77Requires PHP: 7.4
    8 Stable tag: 1.4.2
     8Stable tag: 1.5.0
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    8989
    9090== Changelog ==
     91
     92= 1.5.0 =
     93* New preview when editing an appointment type: the free times of the next 14 days, calculated from the values in the form before saving, with business hours, buffers, minimum notice, blocked days, Google Calendar and existing bookings.
     94* New button "View booking calendar": opens the booking calendar as visitors see it, also for appointment types that are not bookable yet. Only admins can open it and nothing can be booked there.
     95* New button "Copy business hours": fills the time windows of an appointment type with the business hours from the settings in one click.
     96* Clearer explanation of how business hours and the time windows of an appointment type work together.
    9197
    9298= 1.4.2 =
  • fines-finest-booking/trunk/assets/ffb-admin.css

    r3831478 r3833155  
    237237        }
    238238}
     239
     240/* ---------- Termintyp: Geschaeftszeiten uebernehmen und Vorschau ---------- */
     241.ffb-admin .ffb-copy-hours {
     242        display: flex;
     243        align-items: center;
     244        flex-wrap: wrap;
     245        gap: .4em .8em;
     246        margin: 0 0 1em;
     247}
     248
     249.ffb-admin .ffb-preview__list {
     250        max-width: 46em;
     251        margin: .75em 0 1em;
     252        padding: .75em 1em;
     253        background: #fff;
     254        border: 1px solid #dcdcde;
     255        border-radius: 2px;
     256}
     257
     258.ffb-admin .ffb-preview__list[aria-busy="true"] { opacity: .6; }
     259
     260.ffb-admin .ffb-preview__days {
     261        display: grid;
     262        grid-template-columns: max-content 1fr;
     263        gap: .35em 1.2em;
     264        margin: 0;
     265}
     266
     267.ffb-admin .ffb-preview__days dt { font-weight: 600; }
     268
     269.ffb-admin .ffb-preview__days dd { margin: 0; }
     270
     271.ffb-admin .ffb-preview__note { margin: 0 0 .6em; color: #996800; }
     272
     273.ffb-admin .ffb-preview__empty { margin: 0; }
     274
     275.ffb-admin .ffb-preview__error { margin: 0; color: #d63638; }
     276
     277@media (max-width: 600px) {
     278        .ffb-admin .ffb-preview__days { grid-template-columns: 1fr; }
     279        .ffb-admin .ffb-preview__days dd { margin-bottom: .5em; }
     280}
  • fines-finest-booking/trunk/assets/ffb-admin.js

    r3831478 r3833155  
    216216                }, true );
    217217        }() );
     218
     219        // Termintyp: Geschaeftszeiten uebernehmen und Vorschau der freien Zeiten.
     220        ( function () {
     221                var preview = document.querySelector( '[data-ffb-preview]' );
     222                var form = preview ? preview.closest( 'form' ) : null;
     223                var copyButton = document.querySelector( '[data-ffb-copy-hours]' );
     224                var timer = null;
     225                var request = 0;
     226
     227                function select( name ) {
     228                        return form ? form.querySelector( 'select[name="ffb[' + name + ']"]' ) : null;
     229                }
     230
     231                if ( copyButton && form ) {
     232                        copyButton.addEventListener( 'click', function () {
     233                                var hours;
     234                                try {
     235                                        hours = JSON.parse( copyButton.getAttribute( 'data-ffb-copy-hours' ) || '{}' );
     236                                } catch ( e ) {
     237                                        return;
     238                                }
     239                                for ( var day = 1; day <= 7; day++ ) {
     240                                        var windows = hours[ String( day ) ] || [];
     241                                        for ( var i = 0; i < 2; i++ ) {
     242                                                var von = select( 'day_' + day + '_von_' + i );
     243                                                var bis = select( 'day_' + day + '_bis_' + i );
     244                                                if ( von ) {
     245                                                        von.value = windows[ i ] ? windows[ i ][ 0 ] : '';
     246                                                }
     247                                                if ( bis ) {
     248                                                        bis.value = windows[ i ] ? windows[ i ][ 1 ] : '';
     249                                                }
     250                                        }
     251                                }
     252                                var status = document.querySelector( '[data-ffb-copy-hours-status]' );
     253                                if ( status ) {
     254                                        status.textContent = texte.hoursCopied || '';
     255                                }
     256                                schedule( 0 );
     257                        } );
     258                }
     259
     260                if ( ! preview || ! form || ! window.fetch || ! window.FormData ) {
     261                        return;
     262                }
     263
     264                var list = preview.querySelector( '[data-ffb-preview-list]' );
     265
     266                function message( text, isError ) {
     267                        list.innerHTML = '';
     268                        var p = document.createElement( 'p' );
     269                        p.className = isError ? 'ffb-preview__error' : 'ffb-preview__empty';
     270                        p.textContent = text;
     271                        list.appendChild( p );
     272                }
     273
     274                function render( data ) {
     275                        list.innerHTML = '';
     276                        ( data.notes || [] ).forEach( function ( note ) {
     277                                var p = document.createElement( 'p' );
     278                                p.className = 'ffb-preview__note';
     279                                p.textContent = note;
     280                                list.appendChild( p );
     281                        } );
     282                        if ( ! data.days || ! data.days.length ) {
     283                                var leer = document.createElement( 'p' );
     284                                leer.className = 'ffb-preview__empty';
     285                                leer.textContent = data.empty || '';
     286                                list.appendChild( leer );
     287                                return;
     288                        }
     289                        var dl = document.createElement( 'dl' );
     290                        dl.className = 'ffb-preview__days';
     291                        data.days.forEach( function ( day ) {
     292                                var dt = document.createElement( 'dt' );
     293                                dt.textContent = day.label;
     294                                var dd = document.createElement( 'dd' );
     295                                dd.textContent = day.slots.join( ', ' );
     296                                dl.appendChild( dt );
     297                                dl.appendChild( dd );
     298                        } );
     299                        list.appendChild( dl );
     300                }
     301
     302                function load() {
     303                        var body = new FormData( form );
     304                        body.delete( 'ffbooking_action' );
     305                        body.delete( '_wpnonce' );
     306                        body.append( 'action', 'ffbooking_preview_slots' );
     307                        body.append( 'nonce', texte.previewNonce || '' );
     308
     309                        var mine = ++request;
     310                        list.setAttribute( 'aria-busy', 'true' );
     311                        if ( ! list.firstChild ) {
     312                                message( texte.previewLoading || '', false );
     313                        }
     314
     315                        fetch( texte.ajaxUrl, { method: 'POST', credentials: 'same-origin', body: body } )
     316                                .then( function ( response ) {
     317                                        return response.json().catch( function () {
     318                                                return null;
     319                                        } );
     320                                } )
     321                                .then( function ( result ) {
     322                                        // Eine aeltere Antwort darf eine neuere nicht ueberschreiben.
     323                                        if ( mine !== request ) {
     324                                                return;
     325                                        }
     326                                        list.removeAttribute( 'aria-busy' );
     327                                        if ( result && result.success ) {
     328                                                render( result.data );
     329                                        } else {
     330                                                message( ( result && result.data && result.data.message ) || texte.previewError || '', true );
     331                                        }
     332                                } )
     333                                .catch( function () {
     334                                        if ( mine === request ) {
     335                                                list.removeAttribute( 'aria-busy' );
     336                                                message( texte.previewError || '', true );
     337                                        }
     338                                } );
     339                }
     340
     341                function schedule( delay ) {
     342                        window.clearTimeout( timer );
     343                        timer = window.setTimeout( load, delay );
     344                }
     345
     346                form.addEventListener( 'change', function () {
     347                        schedule( 400 );
     348                } );
     349                form.addEventListener( 'input', function ( event ) {
     350                        if ( 'number' === event.target.type || 'ffb-blackouts' === event.target.id ) {
     351                                schedule( 800 );
     352                        }
     353                } );
     354
     355                load();
     356        }() );
    218357}() );
  • fines-finest-booking/trunk/assets/ffb-frontend.css

    r3818359 r3833155  
    494494        }
    495495}
     496
     497/* Vorschau aus dem Backend */
     498.ffb .ffb-preview-note {
     499        margin: 0 0 1rem;
     500        border-left: 3px solid var(--ffb-accent);
     501}
  • fines-finest-booking/trunk/assets/ffb-frontend.js

    r3818359 r3833155  
    8484                this.days = parseInt( root.getAttribute( 'data-days' ), 10 ) || 45;
    8585                this.quelle = root.getAttribute( 'data-quelle' ) || '';
     86                // Vorschau aus dem Backend: zeigt alles wie fuer Kund*innen, bucht aber nichts.
     87                this.preview = '1' === root.getAttribute( 'data-preview' );
    8688                this.data = null;
    8789                this.selectedDate = null;
    … …  
    103105                this.root.appendChild( el( 'div', 'ffb-loading', t( 'loading', 'Loading appointments …' ) ) );
    104106
    105                 getSession().catch( function () {} );
    106 
    107                 api( '/slots?event=' + encodeURIComponent( this.slug ) + '&days=' + this.days )
     107                var path = '/slots?event=' + encodeURIComponent( this.slug ) + '&days=' + this.days;
     108                var options;
     109
     110                if ( this.preview ) {
     111                        path += '&preview=1';
     112                        options = { credentials: 'same-origin', headers: { 'X-WP-Nonce': config.nonce || '' } };
     113                } else {
     114                        getSession().catch( function () {} );
     115                }
     116
     117                api( path, options )
    108118                        .then( function ( data ) {
    109119                                self.data = data;
    … …  
    269279                var submit = el( 'button', 'ffb-btn ffb-btn--primary', t( 'submit', 'Book appointment' ) );
    270280                submit.type = 'submit';
     281                if ( this.preview ) {
     282                        submit.disabled = true;
     283                        submit.textContent = t( 'previewSubmit', 'Preview: booking is switched off' );
     284                }
    271285                form.appendChild( submit );
    272286
    … …  
    277291                form.addEventListener( 'submit', function ( event ) {
    278292                        event.preventDefault();
     293                        if ( self.preview ) {
     294                                return;
     295                        }
    279296                        self.submit( form, submit, message );
    280297                } );
  • fines-finest-booking/trunk/fines-finest-booking.php

    r3831478 r3833155  
    44 * Plugin URI:        https://github.com/FinesFinest/fines-finest-booking
    55 * Description:       Appointment booking in your own design, with Google Calendar sync, Google Meet, confirmation emails and Brevo. All data stays in your own WordPress database.
    6  * Version:           1.4.2
     6 * Version:           1.5.0
    77 * Requires at least: 6.0
    88 * Requires PHP:      7.4
    … …  
    1717}
    1818
    19 define( 'FFBOOKING_VERSION', '1.4.2' );
     19define( 'FFBOOKING_VERSION', '1.5.0' );
    2020define( 'FFBOOKING_FILE', __FILE__ );
    2121define( 'FFBOOKING_DIR', plugin_dir_path( __FILE__ ) );
  • fines-finest-booking/trunk/includes/class-ffb-admin.php

    r3831478 r3833155  
    3939                add_action( 'admin_init', array( __CLASS__, 'handle_actions' ) );
    4040                add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) );
     41                add_action( 'wp_ajax_ffbooking_preview_slots', array( __CLASS__, 'ajax_preview_slots' ) );
    4142                add_action( 'admin_notices', array( __CLASS__, 'activation_notice' ) );
    4243                add_action( 'admin_notices', array( 'FFBooking_Install', 'admin_notices' ) );
    … …  
    132133                        'FFBOOKING_ADMIN',
    133134                        array(
    134                                 'copied'      => __( 'Copied', 'fines-finest-booking' ),
     135                                'copied'         => __( 'Copied', 'fines-finest-booking' ),
    135136                                /* translators: %s: the ending of every Google client ID */
    136                                 'idSuffix'    => sprintf( __( 'The client ID ends with %s. Copy it again in full.', 'fines-finest-booking' ), FFBooking_Google_Check::CLIENT_SUFFIX ),
    137                                 'idSwapped'   => __( 'This looks like the client secret. Enter the client ID here and the secret in the field below.', 'fines-finest-booking' ),
    138                                 'secretIsId'  => __( 'This looks like the client ID. Enter the client secret here, it usually starts with GOCSPX-.', 'fines-finest-booking' ),
    139                                 'spaces'      => __( 'There is a space or line break in it. Copy the value again.', 'fines-finest-booking' ),
     137                                'idSuffix'       => sprintf( __( 'The client ID ends with %s. Copy it again in full.', 'fines-finest-booking' ), FFBooking_Google_Check::CLIENT_SUFFIX ),
     138                                'idSwapped'      => __( 'This looks like the client secret. Enter the client ID here and the secret in the field below.', 'fines-finest-booking' ),
     139                                'secretIsId'     => __( 'This looks like the client ID. Enter the client secret here, it usually starts with GOCSPX-.', 'fines-finest-booking' ),
     140                                'spaces'         => __( 'There is a space or line break in it. Copy the value again.', 'fines-finest-booking' ),
     141                                'ajaxUrl'        => admin_url( 'admin-ajax.php' ),
     142                                'previewNonce'   => wp_create_nonce( 'ffbooking_preview' ),
     143                                'previewLoading' => __( 'Calculating free times …', 'fines-finest-booking' ),
     144                                'previewError'   => __( 'The preview could not be loaded.', 'fines-finest-booking' ),
     145                                'hoursCopied'    => __( 'Business hours copied. Save to keep them.', 'fines-finest-booking' ),
    140146                        )
    141147                );
    … …  
    480486                $id    = isset( $_POST['ffbooking_id'] ) ? (int) $_POST['ffbooking_id'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce prueft handle_actions().
    481487
     488                $result = FFBooking_Event_Types::save( self::event_data_from_input( (array) $input ), $id );
     489
     490                if ( is_wp_error( $result ) ) {
     491                        set_transient( 'ffbooking_admin_error', $result->get_error_message(), 60 );
     492                        wp_safe_redirect( admin_url( 'admin.php?page=ffbooking-event-types&ffbooking_notice=error' ) );
     493                        exit;
     494                }
     495
     496                wp_safe_redirect( admin_url( 'admin.php?page=ffbooking-event-types&edit=' . (int) $result . '&ffbooking_notice=saved' ) );
     497                exit;
     498        }
     499
     500        /**
     501         * Macht aus den Feldern des Termintyp-Formulars die Daten fuer FFBooking_Event_Types::save().
     502         *
     503         * Speichern und Vorschau nutzen dieselbe Umwandlung. So zeigt die Vorschau genau das,
     504         * was nach dem Speichern gilt.
     505         *
     506         * @param array $input Rohes Formular-Array, bereits ohne Schraegstriche.
     507         * @return array
     508         */
     509        public static function event_data_from_input( $input ) {
    482510                $availability = array();
    483511                foreach ( array_keys( self::weekdays() ) as $day ) {
    … …  
    493521                $questions = self::parse_questions( isset( $input['questions'] ) ? (string) $input['questions'] : '' );
    494522
    495                 $data = array(
     523                return array(
    496524                        'title'            => $input['title'] ?? '',
    497525                        'slug'             => $input['slug'] ?? '',
    … …  
    514542                        'sort_order'       => $input['sort_order'] ?? 0,
    515543                );
    516 
    517                 $result = FFBooking_Event_Types::save( $data, $id );
    518 
    519                 if ( is_wp_error( $result ) ) {
    520                         set_transient( 'ffbooking_admin_error', $result->get_error_message(), 60 );
    521                         wp_safe_redirect( admin_url( 'admin.php?page=ffbooking-event-types&ffbooking_notice=error' ) );
    522                         exit;
    523                 }
    524 
    525                 wp_safe_redirect( admin_url( 'admin.php?page=ffbooking-event-types&edit=' . (int) $result . '&ffbooking_notice=saved' ) );
    526                 exit;
     544        }
     545
     546        /**
     547         * Vorschau der freien Zeiten aus dem noch nicht gespeicherten Formular (admin-ajax).
     548         *
     549         * Rechnet mit denselben Regeln wie die Buchungsseite: Geschaeftszeiten, Vorlauf, Puffer,
     550         * Sperrtage, belegte Zeiten aus Google und bestehende Buchungen. Speichert nichts.
     551         */
     552        public static function ajax_preview_slots() {
     553                check_ajax_referer( 'ffbooking_preview', 'nonce' );
     554                if ( ! current_user_can( 'manage_options' ) ) {
     555                        wp_send_json_error( array( 'message' => __( 'You are not allowed to do this.', 'fines-finest-booking' ) ), 403 );
     556                }
     557                if ( ! FFBooking_Install::ready() ) {
     558                        wp_send_json_error( array( 'message' => __( 'The database tables of the plugin are missing.', 'fines-finest-booking' ) ), 500 );
     559                }
     560
     561                $input = isset( $_POST['ffb'] ) ? wp_unslash( $_POST['ffb'] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- Die Werte bereinigen event_data_from_input() und preview_event().
     562                $id    = isset( $_POST['ffbooking_id'] ) ? (int) $_POST['ffbooking_id'] : 0;
     563
     564                $event = self::preview_event( self::event_data_from_input( (array) $input ), $id );
     565                $days  = 14;
     566                $data  = FFBooking_Availability::get_slots( $event, '', $days );
     567
     568                if ( is_wp_error( $data ) ) {
     569                        wp_send_json_error( array( 'message' => __( 'The busy times from Google Calendar could not be loaded, so no preview is possible right now. The reason is in the error log in the settings.', 'fines-finest-booking' ) ), 503 );
     570                }
     571
     572                $out = array();
     573                foreach ( $data['days'] as $day ) {
     574                        $date   = new DateTimeImmutable( $day['date'] . ' 12:00:00', ffbooking_timezone() );
     575                        $labels = array();
     576                        foreach ( $day['slots'] as $slot ) {
     577                                $labels[] = $slot['label'];
     578                        }
     579                        $out[] = array(
     580                                /* translators: date format for the slot preview, see https://www.php.net/manual/datetime.format.php */
     581                                'label' => wp_date( __( 'D, M j', 'fines-finest-booking' ), $date->getTimestamp() ),
     582                                'slots' => $labels,
     583                        );
     584                }
     585
     586                $notes = array();
     587                if ( ! FFBooking_Google::is_connected() ) {
     588                        $notes[] = __( 'Google Calendar is not connected. Busy times from your calendar are not taken into account.', 'fines-finest-booking' );
     589                }
     590                if ( empty( $event['active'] ) ) {
     591                        $notes[] = __( 'This appointment type is not bookable at the moment. On the booking page nobody sees these times yet.', 'fines-finest-booking' );
     592                }
     593
     594                wp_send_json_success(
     595                        array(
     596                                'days'  => $out,
     597                                'empty' => empty( $out ) ? self::preview_empty_reason( $event ) : '',
     598                                'notes' => $notes,
     599                        )
     600                );
     601        }
     602
     603        /**
     604         * Baut aus den Formulardaten einen Termintyp, wie ihn FFBooking_Event_Types::get() liefern wuerde.
     605         *
     606         * @param array $data Ergebnis von event_data_from_input().
     607         * @param int   $id   ID des Termintyps, 0 fuer neu.
     608         * @return array
     609         */
     610        public static function preview_event( $data, $id ) {
     611                // Gleiche Untergrenzen wie FFBooking_Event_Types::save().
     612                $zahl = static function ( $value, $min ) {
     613                        return max( $min, (int) $value );
     614                };
     615
     616                return array(
     617                        'id'            => (int) $id,
     618                        'slug'          => '',
     619                        'title'         => sanitize_text_field( (string) $data['title'] ),
     620                        'duration'      => $zahl( $data['duration'], 5 ),
     621                        'slot_step'     => $zahl( $data['slot_step'], 5 ),
     622                        'buffer_before' => $zahl( $data['buffer_before'], 0 ),
     623                        'buffer_after'  => $zahl( $data['buffer_after'], 0 ),
     624                        'min_notice'    => $zahl( $data['min_notice'], 0 ),
     625                        'max_days'      => $zahl( $data['max_days'], 1 ),
     626                        'daily_limit'   => $zahl( $data['daily_limit'], 0 ),
     627                        'availability'  => FFBooking_Event_Types::normalize_availability( $data['availability'] ),
     628                        'blackouts'     => FFBooking_Event_Types::clean_blackouts( $data['blackouts'] ),
     629                        'active'        => empty( $data['active'] ) ? 0 : 1,
     630                );
     631        }
     632
     633        /**
     634         * Nennt den wahrscheinlichsten Grund, warum die Vorschau keine Zeiten zeigt.
     635         *
     636         * @param array $event Termintyp aus preview_event().
     637         * @return string
     638         */
     639        public static function preview_empty_reason( $event ) {
     640                $has_windows = false;
     641                foreach ( $event['availability'] as $windows ) {
     642                        if ( ! empty( $windows ) ) {
     643                                $has_windows = true;
     644                                break;
     645                        }
     646                }
     647                if ( ! $has_windows ) {
     648                        return __( 'No free times in the next 14 days: no time windows are set for any weekday.', 'fines-finest-booking' );
     649                }
     650
     651                $global = FFBooking_Availability::global_limits();
     652                if ( $global['enforce'] ) {
     653                        $overlap = false;
     654                        foreach ( $event['availability'] as $day => $windows ) {
     655                                $hours = isset( $global['hours'][ $day ] ) ? $global['hours'][ $day ] : array();
     656                                if ( FFBooking_Availability::intersect_windows( $windows, $hours ) ) {
     657                                        $overlap = true;
     658                                        break;
     659                                }
     660                        }
     661                        if ( ! $overlap ) {
     662                                return __( 'No free times in the next 14 days: the time windows lie completely outside the business hours from the settings.', 'fines-finest-booking' );
     663                        }
     664                }
     665
     666                return __( 'No free times in the next 14 days. Check the minimum notice, the blocked days and your Google Calendar.', 'fines-finest-booking' );
     667        }
     668
     669        /**
     670         * Geschaeftszeiten aus den Einstellungen fuer den Knopf "Geschaeftszeiten uebernehmen".
     671         *
     672         * @return array Wochentag 1 bis 7 => Liste von [von, bis]. Leer, wenn keine eingetragen sind.
     673         */
     674        public static function business_hours() {
     675                $hours = array();
     676                $any   = false;
     677                for ( $day = 1; $day <= 7; $day++ ) {
     678                        $hours[ (string) $day ] = array_slice( self::parse_windows( ffbooking_get_setting( 'hours_' . $day, '' ) ), 0, 2 );
     679                        if ( $hours[ (string) $day ] ) {
     680                                $any = true;
     681                        }
     682                }
     683
     684                return $any ? $hours : array();
     685        }
     686
     687        /**
     688         * Adresse, unter der Fine den Buchungskalender eines Termintyps so sieht wie Kund*innen.
     689         *
     690         * Ist eine Buchungsseite eingetragen, zeigt sie die Vorschau im Design der Website.
     691         * Sonst liefert das Plugin eine schlanke eigene Seite.
     692         *
     693         * @param int $id ID des Termintyps.
     694         * @return string
     695         */
     696        public static function preview_url( $id ) {
     697                $base = (string) ffbooking_get_setting( 'booking_page_url' );
     698                if ( '' === $base ) {
     699                        $base = home_url( '/' );
     700                }
     701
     702                return add_query_arg( 'ffbooking_preview', (int) $id, $base );
    527703        }
    528704
    … …  
    13111487                                                <th scope="row"><?php esc_html_e( 'Time windows', 'fines-finest-booking' ); ?></th>
    13121488                                                <td>
     1489                                                        <p class="description" style="margin:0 0 1em;"><?php esc_html_e( 'When this appointment type can be booked. The business hours in the settings apply to all appointment types on top of this.', 'fines-finest-booking' ); ?></p>
     1490                                                        <?php $ffbooking_hours = self::business_hours(); ?>
     1491                                                        <p class="ffb-copy-hours">
     1492                                                                <button type="button" class="button" data-ffb-copy-hours="<?php echo esc_attr( wp_json_encode( $ffbooking_hours ) ); ?>" <?php disabled( empty( $ffbooking_hours ) ); ?>><?php esc_html_e( 'Copy business hours', 'fines-finest-booking' ); ?></button>
     1493                                                                <span class="description" data-ffb-copy-hours-status role="status">
     1494                                                                <?php
     1495                                                                if ( empty( $ffbooking_hours ) ) {
     1496                                                                        echo wp_kses(
     1497                                                                                sprintf(
     1498                                                                                        /* translators: %s: link "Times and limits" to the settings tab */
     1499                                                                                        __( 'No business hours are set yet. You can enter them under %s.', 'fines-finest-booking' ),
     1500                                                                                        '<a href="' . esc_url( admin_url( 'admin.php?page=ffbooking-settings#ffb-tab-limits' ) ) . '">' . esc_html__( 'Times and limits', 'fines-finest-booking' ) . '</a>'
     1501                                                                                ),
     1502                                                                                array( 'a' => array( 'href' => array() ) )
     1503                                                                        );
     1504                                                                } else {
     1505                                                                        esc_html_e( 'Fills in the time windows below with the business hours from the settings. You can adjust them afterwards.', 'fines-finest-booking' );
     1506                                                                }
     1507                                                                ?>
     1508                                                                </span>
     1509                                                        </p>
    13131510                                                        <?php foreach ( self::weekdays() as $num => $label ) : ?>
    13141511                                                                <p style="margin:0 0 .5em;">
    … …  
    14021599                                                        &nbsp;&nbsp;
    14031600                                                        <label><?php esc_html_e( 'Order', 'fines-finest-booking' ); ?> <input name="ffb[sort_order]" type="number" value="<?php echo (int) $event['sort_order']; ?>" class="small-text"></label>
     1601                                                </td>
     1602                                        </tr>
     1603                                        <tr>
     1604                                                <th scope="row"><?php esc_html_e( 'Preview', 'fines-finest-booking' ); ?></th>
     1605                                                <td>
     1606                                                        <div class="ffb-preview" data-ffb-preview>
     1607                                                                <p class="description"><?php esc_html_e( 'Free times of the next 14 days with the values in this form, even before saving. Business hours, buffers, minimum notice, blocked days, your Google Calendar and existing bookings are taken into account.', 'fines-finest-booking' ); ?></p>
     1608                                                                <div class="ffb-preview__list" data-ffb-preview-list aria-live="polite"></div>
     1609                                                                <p>
     1610                                                                        <?php if ( $id ) : ?>
     1611                                                                                <a class="button" href="<?php echo esc_url( self::preview_url( $id ) ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'View booking calendar', 'fines-finest-booking' ); ?></a>
     1612                                                                                <span class="description"><?php esc_html_e( 'Opens a new tab with the booking calendar as visitors see it, in the saved state. Nothing can be booked there.', 'fines-finest-booking' ); ?></span>
     1613                                                                        <?php else : ?>
     1614                                                                                <span class="description"><?php esc_html_e( 'After the first save you can view the booking calendar here as visitors see it.', 'fines-finest-booking' ); ?></span>
     1615                                                                        <?php endif; ?>
     1616                                                                </p>
     1617                                                        </div>
    14041618                                                </td>
    14051619                                        </tr>
    … …  
    16331847                                <h2><?php esc_html_e( 'Limits for all appointment types', 'fines-finest-booking' ); ?></h2>
    16341848                                <p class="description" style="max-width:46em;"><?php esc_html_e( 'These values apply in addition to the settings of the individual appointment types and can only restrict further, never extend. No appointment type can override them.', 'fines-finest-booking' ); ?></p>
     1849                                <p class="description" style="max-width:46em;"><?php esc_html_e( 'In each appointment type, the button "Copy business hours" takes over these times as its time windows with one click.', 'fines-finest-booking' ); ?></p>
    16351850                                <table class="form-table" role="presentation">
    16361851                                        <tr>
  • fines-finest-booking/trunk/includes/class-ffb-rest.php

    r3818359 r3833155  
    210210                $event = FFBooking_Event_Types::get_by_slug( (string) $request->get_param( 'event' ) );
    211211
    212                 if ( ! $event || ! $event['active'] ) {
     212                // Die Vorschau aus dem Backend zeigt auch Termintypen, die noch nicht buchbar sind.
     213                // Das gilt nur fuer angemeldete Admins, die Anfrage traegt dafuer den REST-Nonce.
     214                $preview = $request->get_param( 'preview' ) && current_user_can( 'manage_options' );
     215
     216                if ( ! $event || ( ! $event['active'] && ! $preview ) ) {
    213217                        return new WP_Error( 'ffbooking_unknown_event', __( 'This appointment type does not exist.', 'fines-finest-booking' ), array( 'status' => 404 ) );
    214218                }
  • fines-finest-booking/trunk/includes/class-ffb-shortcode.php

    r3818359 r3833155  
    3333                // Prioritaet 1: vor den Weiterleitungen von WordPress, die einen alten Link sonst umbiegen oder verlieren.
    3434                add_action( 'template_redirect', array( __CLASS__, 'maybe_standalone_cancel' ), 1 );
     35                add_action( 'template_redirect', array( __CLASS__, 'maybe_standalone_preview' ), 1 );
     36        }
     37
     38        /**
     39         * Termintyp fuer die Vorschau aus dem Backend, nur fuer angemeldete Admins.
     40         *
     41         * @return array|null
     42         */
     43        public static function preview_event() {
     44                // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nur Anzeige, geschuetzt durch die Rechtepruefung.
     45                $id = isset( $_GET['ffbooking_preview'] ) ? absint( wp_unslash( $_GET['ffbooking_preview'] ) ) : 0;
     46                if ( ! $id || ! current_user_can( 'manage_options' ) || ! FFBooking_Install::ready() ) {
     47                        return null;
     48                }
     49
     50                return FFBooking_Event_Types::get( $id );
     51        }
     52
     53        /**
     54         * Vorschau-Adresse ausserhalb der Buchungsseite: eigene, schlanke Seite.
     55         * Auf der Buchungsseite zeigt der Shortcode die Vorschau im Design der Website.
     56         */
     57        public static function maybe_standalone_preview() {
     58                if ( is_admin() ) {
     59                        return;
     60                }
     61                $event = self::preview_event();
     62                if ( ! $event || ( self::is_booking_page_request() && ! post_password_required() ) ) {
     63                        return;
     64                }
     65
     66                nocache_headers();
     67                status_header( 200 );
     68                header( 'Content-Type: text/html; charset=utf-8' );
     69                ?>
     70<!DOCTYPE html>
     71<html <?php language_attributes(); ?>>
     72<head>
     73        <meta charset="<?php bloginfo( 'charset' ); ?>">
     74        <meta name="viewport" content="width=device-width, initial-scale=1">
     75        <meta name="robots" content="noindex, nofollow">
     76        <title>
     77        <?php
     78        /* translators: %s: name of the appointment type */
     79        echo esc_html( sprintf( __( 'Preview: %s', 'fines-finest-booking' ), $event['title'] ) );
     80        ?>
     81        </title>
     82        <?php
     83        self::enqueue();
     84        wp_add_inline_style( 'ffbooking-frontend', 'body{margin:0;padding:3rem 1.25rem;background:' . ffbooking_get_color( 'color_bg_deep' ) . ';}.ffb{max-width:760px;margin:0 auto;}' );
     85        wp_print_styles( array( 'ffbooking-frontend' ) );
     86        ?>
     87</head>
     88<body>
     89        <?php echo self::render_widget( $event, 45, '', true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- render_widget() escaped jede Ausgabe selbst. ?>
     90        <?php wp_print_scripts( array( 'ffbooking-frontend' ) ); ?>
     91</body>
     92</html>
     93                <?php
     94                exit;
    3595        }
    3696
    … …  
    258318                }
    259319
    260                 return array(
     320                $config = array(
    261321                        'root'          => esc_url_raw( rest_url( FFBooking_Rest::NS ) ),
    262322                        'consentText'   => ffbooking_get_setting( 'consent_text' ),
    … …  
    300360                                /* translators: 1: weekday name, 2: day of month, 3: month name */
    301361                                'longDate'        => __( '%1$s, %3$s %2$d', 'fines-finest-booking' ),
     362                                'previewSubmit'   => __( 'Preview: booking is switched off', 'fines-finest-booking' ),
    302363                        ),
    303364                );
     365
     366                // Nur in der Vorschau: Mit dem REST-Nonce erkennt die Schnittstelle den angemeldeten Admin.
     367                if ( self::preview_event() ) {
     368                        $config['nonce'] = wp_create_nonce( 'wp_rest' );
     369                }
     370
     371                return $config;
    304372        }
    305373
    … …  
    361429                if ( $cancel_token ) {
    362430                        return self::render_cancel( $cancel_token );
     431                }
     432
     433                // Vorschau aus dem Backend: zeigt den gewaehlten Termintyp, auch wenn er noch nicht buchbar ist.
     434                $preview = self::preview_event();
     435                if ( $preview ) {
     436                        return self::render_widget( $preview, (int) $atts['days'], '', true );
    363437                }
    364438
    … …  
    446520         * @param int    $days   Vorschau-Tage.
    447521         * @param string $quelle Herkunft aus dem Shortcode.
     522         * @param bool   $preview Vorschau aus dem Backend: zeigt einen Hinweis, Buchen ist gesperrt.
    448523         * @return string
    449524         */
    450         protected static function render_widget( $event, $days, $quelle = '' ) {
     525        protected static function render_widget( $event, $days, $quelle = '', $preview = false ) {
    451526                $notify = (string) ffbooking_get_setting( 'notify_email' );
    452527                $link   = '<a href="mailto:' . esc_attr( $notify ) . '">' . esc_html( $notify ) . '</a>';
    … …  
    455530                ?>
    456531                <div class="ffb">
    457                         <div class="ffb-widget" data-ffb-widget data-event="<?php echo esc_attr( $event['slug'] ); ?>" data-days="<?php echo (int) max( 7, $days ); ?>" data-quelle="<?php echo esc_attr( self::resolve_source( $quelle ) ); ?>">
     532                        <?php if ( $preview ) : ?>
     533                                <p class="ffb-note ffb-preview-note">
     534                                <?php
     535                                echo esc_html(
     536                                        $event['active']
     537                                                ? __( 'Preview for you as admin. Visitors see the same calendar. Nothing can be booked here.', 'fines-finest-booking' )
     538                                                : __( 'Preview for you as admin. This appointment type is not bookable yet, visitors do not see it. Nothing can be booked here.', 'fines-finest-booking' )
     539                                );
     540                                ?>
     541                                </p>
     542                        <?php endif; ?>
     543                        <div class="ffb-widget" data-ffb-widget data-event="<?php echo esc_attr( $event['slug'] ); ?>" data-days="<?php echo (int) max( 7, $days ); ?>" data-quelle="<?php echo esc_attr( self::resolve_source( $quelle ) ); ?>"<?php echo $preview ? ' data-preview="1"' : ''; ?>>
    458544                                <div class="ffb-loading"><?php echo esc_html__( 'Loading appointments …', 'fines-finest-booking' ); ?></div>
    459545                        </div>
  • fines-finest-booking/trunk/readme.txt

    r3831478 r3833155  
    66Tested up to: 7.2
    77Requires PHP: 7.4
    8 Stable tag: 1.4.2
     8Stable tag: 1.5.0
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    8989
    9090== Changelog ==
     91
     92= 1.5.0 =
     93* New preview when editing an appointment type: the free times of the next 14 days, calculated from the values in the form before saving, with business hours, buffers, minimum notice, blocked days, Google Calendar and existing bookings.
     94* New button "View booking calendar": opens the booking calendar as visitors see it, also for appointment types that are not bookable yet. Only admins can open it and nothing can be booked there.
     95* New button "Copy business hours": fills the time windows of an appointment type with the business hours from the settings in one click.
     96* Clearer explanation of how business hours and the time windows of an appointment type work together.
    9197
    9298= 1.4.2 =
Note: See TracChangeset for help on using the changeset viewer.