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 );
?>
';
}
/**
* 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;
}
?>