title, callback, position (lower first), cap, nav (show in top bar). * Extensions add screens through `tableengine_admin_pages` and render them with Admin::page_shell(). */ public function pages() { $pages = apply_filters( 'tableengine_admin_pages', array( 'tableengine' => array( 'title' => __( 'Reservations', 'tableengine' ), 'callback' => array( $this, 'page_reservations' ), 'position' => 10, ), 'tableengine-floorplans' => array( 'title' => __( 'Floor plans', 'tableengine' ), 'callback' => array( $this, 'page_floorplans' ), 'position' => 20, 'cap' => Plugin::cap( 'floorplans' ), ), 'tableengine-settings' => array( 'title' => __( 'Settings', 'tableengine' ), 'callback' => array( $this, 'page_settings' ), 'position' => 90, 'cap' => Plugin::cap( 'settings' ), ), ), $this ); foreach ( $pages as $slug => $p ) { $pages[ $slug ] = wp_parse_args( $p, array( 'title' => $slug, 'callback' => '__return_null', 'position' => 50, 'cap' => Plugin::cap(), 'nav' => true, ) ); } uasort( $pages, function ( $a, $b ) { return $a['position'] - $b['position']; } ); return $pages; } /** The logo mark as a data URI, so WordPress recolours it to match the admin colour scheme. */ public static function menu_icon() { $svg = file_get_contents( TABLEENGINE_DIR . 'assets/img/tableengine-menu-icon.svg' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents return $svg ? 'data:image/svg+xml;base64,' . base64_encode( $svg ) : 'dashicons-food'; // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode } public function menu() { $cap = Plugin::cap(); $pending = $this->pending_count(); $bubble = $pending ? ' ' . number_format_i18n( $pending ) . '' : ''; /** Admin menu name and icon (e.g. white label). */ $title = (string) apply_filters( 'tableengine_menu_title', __( 'TableEngine', 'tableengine' ) ); add_menu_page( $title, $title . $bubble, $cap, 'tableengine', array( $this, 'page_reservations' ), (string) apply_filters( 'tableengine_menu_icon', self::menu_icon() ), 26 ); foreach ( $this->pages() as $slug => $p ) { add_submenu_page( 'tableengine', $p['title'], $p['title'], $p['cap'], $slug, $p['callback'] ); } } /** * Top bar for server-rendered screens added by extensions. Wrap the page in * `
` and the content in `
` for the standard spacing. */ public function page_header( $title, $actions = '' ) { echo '

' . esc_html( $title ) . '

'; $this->header( $title, $actions ); } /** * Standard wrapper (top bar + app mount point) for screens added by extensions. */ public function page_shell( $title, $mount_id, $actions = '' ) { echo '

' . esc_html( $title ) . '

'; $this->header( $title, $actions ); echo '
'; } private function pending_count() { global $wpdb; $t = Install::table( 'reservations' ); return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$t} WHERE status = 'pending' AND start_at >= %s" . Reservations::scope_sql(), wp_date( 'Y-m-d 00:00:00' ) ) ); // phpcs:ignore WordPress.DB, PluginCheck.Security.DirectDB -- Plugin's own custom table: the name is $wpdb->prefix plus a fixed suffix and values are prepared, so there is no core API or cache to use. } private function screen_is_tableengine() { $page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification return 0 === strpos( $page, 'tableengine' ) ? $page : false; } public function body_class( $classes ) { return $this->screen_is_tableengine() ? $classes . ' tableengine-admin' : $classes; } public function assets() { $page = $this->screen_is_tableengine(); if ( ! $page ) { return; } wp_enqueue_style( 'tableengine-admin', TABLEENGINE_URL . 'assets/css/admin.css', array(), Plugin::ver( 'assets/css/admin.css' ) ); /** Data available to every TableEngine admin screen as window.TableEngineAdmin. */ $common = apply_filters( 'tableengine_admin_config', array( 'rest' => esc_url_raw( rest_url( 'tableengine/v1/' ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'settings' => Settings::public_config(), 'statuses' => Reservations::statuses(), 'payments' => Reservations::payment_statuses(), 'today' => wp_date( 'Y-m-d' ), 'currency' => Settings::get( 'currency' ), 'bookingUrl' => Settings::get( 'booking_page' ) ? get_permalink( (int) Settings::get( 'booking_page' ) ) : '', 'premium' => Licensing::is_premium(), // What the current user may do, so screens can hide actions they'd be refused. 'can' => array_combine( Plugin::AREAS, array_map( array( 'TableEngine\\Plugin', 'can' ), Plugin::AREAS ) ), 'nonBooking' => Reservations::non_booking_statuses(), 'upgradeUrl' => Licensing::show_upsell() ? Licensing::upgrade_url() : '', ), $page ); Plugin::register_shared_scripts(); // Every TableEngine screen gets the shared hooks bus and config, including screens added by extensions. wp_enqueue_script( 'tableengine-hooks' ); // JSON, not wp_localize_script(): that turns top-level numbers and booleans into strings ("0" is truthy in JS). wp_add_inline_script( 'tableengine-hooks', 'var TableEngineAdmin = ' . wp_json_encode( $common ) . ';', 'before' ); // Top bar: fits the navigation into the space left (a "More" menu for the rest). wp_enqueue_script( 'tableengine-topbar', TABLEENGINE_URL . 'assets/js/admin-topbar.js', array(), Plugin::ver( 'assets/js/admin-topbar.js' ), true ); if ( 'tableengine-floorplans' === $page ) { wp_enqueue_style( 'tableengine-builder', TABLEENGINE_URL . 'assets/css/builder.css', array( 'tableengine-admin' ), Plugin::ver( 'assets/css/builder.css' ) ); wp_enqueue_script( 'tableengine-builder', TABLEENGINE_URL . 'assets/js/builder.js', Plugin::script_deps( 'tableengine-builder', array( 'tableengine-render' ) ), Plugin::ver( 'assets/js/builder.js' ), true ); } elseif ( 'tableengine' === $page ) { wp_enqueue_script( 'tableengine-reservations', TABLEENGINE_URL . 'assets/js/admin-reservations.js', Plugin::script_deps( 'tableengine-reservations', array( 'tableengine-render' ) ), Plugin::ver( 'assets/js/admin-reservations.js' ), true ); } elseif ( 'tableengine-settings' === $page ) { wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_script( 'tableengine-settings', TABLEENGINE_URL . 'assets/js/admin-settings.js', Plugin::script_deps( 'tableengine-settings', array( 'wp-color-picker', 'jquery', 'tableengine-hooks' ) ), Plugin::ver( 'assets/js/admin-settings.js' ), true ); } /** Enqueue assets for a TableEngine screen. Depend on 'tableengine-hooks' to use window.tableengine.hooks. */ do_action( 'tableengine_admin_enqueue', $page, $common ); } private function header( $title, $actions = '' ) { /** Extra top-bar controls on every TableEngine screen (e.g. a location switcher). */ $actions = apply_filters( 'tableengine_admin_header_actions', $actions, $title ); ?>
/

true, 'id' => true, 'title' => true, 'hidden' => true, 'role' => true, 'aria-label' => true, 'aria-hidden' => true, 'aria-live' => true, 'data-*' => true, ); $allowed = array( 'a' => $common + array( 'href' => true, 'target' => true, 'rel' => true, ), 'button' => $common + array( 'type' => true, 'name' => true, 'value' => true, 'disabled' => true, ), 'span' => $common, 'div' => $common, 'label' => $common + array( 'for' => true ), 'form' => $common + array( 'method' => true, 'action' => true, ), 'input' => $common + array( 'type' => true, 'name' => true, 'value' => true, ), 'select' => $common + array( 'name' => true, 'disabled' => true, ), 'option' => array( 'value' => true, 'selected' => true, 'disabled' => true, ), 'noscript' => array(), ); /** Markup allowed in the admin top-bar actions (wp_kses() policy). */ return (array) apply_filters( 'tableengine_admin_header_actions_allowed_html', $allowed ); } public function page_reservations() { echo '

' . esc_html__( 'Reservations', 'tableengine' ) . '

'; $this->header( __( 'Reservations', 'tableengine' ), '' ); echo '
'; } public function page_floorplans() { echo '

' . esc_html__( 'Floor plans', 'tableengine' ) . '

'; $this->header( __( 'Floor plan builder', 'tableengine' ), '' ); echo '
'; } // --------------------------------------------------------------------- // Settings // --------------------------------------------------------------------- public function save_settings() { if ( ! Plugin::can( 'settings' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'tableengine' ) ); } check_admin_referer( 'tableengine_settings' ); $reset = ! empty( $_POST['tableengine_reset'] ); // Sanitised here, field by field against the settings schema, before anything else sees it. $clean = Settings::sanitize( isset( $_POST['tableengine'] ) && is_array( $_POST['tableengine'] ) ? $_POST['tableengine'] : array() ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- Settings::sanitize() unslashes and sanitises every field by its schema type. /** Return true to store the (already sanitised) settings elsewhere, e.g. as a location's overrides. */ if ( apply_filters( 'tableengine_pre_save_settings', false, $clean, $reset ) ) { // Handled. } elseif ( $reset ) { update_option( Settings::OPTION, Settings::defaults() ); } else { update_option( Settings::OPTION, $clean ); } Settings::flush(); $tab = isset( $_POST['tableengine_tab'] ) ? sanitize_key( wp_unslash( $_POST['tableengine_tab'] ) ) : 'general'; wp_safe_redirect( apply_filters( 'tableengine_settings_redirect', admin_url( 'admin.php?page=tableengine-settings&saved=1' ) ) . '#' . $tab ); exit; } public function page_settings() { $schema = Settings::schema(); $values = Settings::all(); $saved = isset( $_GET['saved'] ); // phpcs:ignore WordPress.Security.NonceVerification ?>

header( __( 'Settings', 'tableengine' ) ); ?>
$group ) : ?>

$section ) : ?>

$field ) { $this->render_field( $key, $field, isset( $values[ $key ] ) ? $values[ $key ] : null ); } ?>
'; if ( 'toggle' === $type ) { ?> '; return; } if ( $label ) { printf( '', esc_attr( $id ), esc_html( $label ) ); } echo '
'; switch ( $type ) { case 'select': printf( ''; break; case 'number': printf( 'print_attrs( $attrs ); echo '>'; break; case 'decimal': printf( '
%s
', esc_html( Settings::get( 'currency' ) ), esc_attr( $id ), esc_attr( $name ), esc_attr( $value ) ); break; case 'color': printf( '', esc_attr( $id ), esc_attr( $name ), esc_attr( $value ), esc_attr( $f['default'] ) ); break; case 'textarea': case 'code': printf( ''; break; case 'password': $masked = $value ? str_repeat( '•', 16 ) : ''; printf( '
print_attrs( $attrs ); printf( '>%s
', $value ? 'is-set' : '', $value ? esc_html__( 'Saved', 'tableengine' ) : esc_html__( 'Not set', 'tableengine' ) ); break; case 'page': wp_dropdown_pages( array( 'name' => esc_attr( $name ), 'id' => esc_attr( $id ), 'selected' => (int) $value, 'show_option_none' => esc_html__( '— Select a page —', 'tableengine' ), 'option_none_value' => 0, ) ); break; case 'weekly_hours': $this->render_weekly_hours( $name, (array) $value ); break; case 'webhook_info': ?>

print_attrs( $attrs ); echo '>'; } if ( ! empty( $f['help'] ) ) { printf( '

%s

', esc_html( $f['help'] ) ); } echo '
'; } /** * Print extra HTML attributes, each name and value escaped here. * * @param array $attrs Attribute name => raw value. */ private function print_attrs( array $attrs ) { foreach ( $attrs as $attr => $val ) { $attr = preg_replace( '/[^a-z0-9_-]/', '', strtolower( (string) $attr ) ); if ( '' !== $attr ) { echo ' ' . esc_attr( $attr ) . '="' . esc_attr( $val ) . '"'; } } } /** * A single, quiet upgrade card in the settings sidebar. Shown only to free * users when Freemius is configured; nothing in the free plugin is locked. */ private function upsell_card() { if ( ! Licensing::show_upsell() ) { return; } ?>

'; for ( $i = 0; $i < 7; $i++ ) { $d = ( $start + $i ) % 7; $ranges = isset( $value[ (string) $d ] ) ? $value[ (string) $d ] : ''; $open = '' !== trim( $ranges ); ?>
'; } }