'string', 'default' => '' ); }; register_block_type( 'menugps/menu', array( 'editor_script' => 'menugps-block', 'editor_style' => 'menugps-menu', 'render_callback' => array( __CLASS__, 'render_block' ), 'attributes' => array( 'venue' => $text(), 'theme' => $text(), 'columns' => $text(), 'section' => $text(), 'photos' => $text(), 'sold_out' => $text(), 'nav' => $text(), ), ) ); } public static function render_block( $attributes ) { // Only what the block actually set; the rest falls through to the site defaults. $given = array_filter( (array) $attributes, static function ( $v ) { return is_string( $v ) && '' !== $v; } ); return self::render( $given + self::site_defaults() ); } /** * [menugps] — or [menugps venue="slug" theme="cards" columns="2" section="drinks" * prices="no" descriptions="no" photos="no" sold_out="show" nav="no"]. */ public static function shortcode( $atts ) { $atts = shortcode_atts( array( 'venue' => '', 'section' => '' ) + self::site_defaults(), $atts, 'menugps' ); return self::render( $atts ); } /** @return array */ private static function site_defaults() { return class_exists( 'MenuGPS_Appearance' ) ? MenuGPS_Appearance::defaults() : array( 'columns' => '1', 'prices' => 'yes', 'descriptions' => 'yes', 'tags' => 'yes', 'theme' => 'classic', 'photos' => 'yes', 'sold_out' => 'hide', 'nav' => 'auto', 'picker' => 'yes' ); } private static function render( array $atts ) { $slug = ! empty( $atts['venue'] ) ? sanitize_title( $atts['venue'] ) : MenuGPS_API::venue(); if ( ! $slug ) { // Never shout at a visitor about configuration. The owner sees the warning in // wp-admin; the public page simply shows nothing. return current_user_can( 'manage_options' ) ? '

' . esc_html__( 'MenuGPS: choose a venue in Settings to show your menu here.', 'menugps' ) . '

' : ''; } $site = self::locale(); $menu = self::fetch( $slug, $site ); if ( is_wp_error( $menu ) || empty( $menu['categories'] ) ) { return current_user_can( 'manage_options' ) && is_wp_error( $menu ) ? '

' . esc_html( $menu->get_error_message() ) . '

' : ''; } /* * The picker is layered ON TOP of the default render, never instead of it. * * The first fetch above is always the site's language: that is the copy the transient * caches, the one search engines index, and the one a visitor who never touches the * picker sees. Only an explicit ?menu_lang= — validated against the languages the SERVER * says this venue has — causes a second fetch. An arbitrary value cannot reach the API * or create a cache entry. */ $options = self::language_options( $menu ); $chosen = self::chosen_locale( $options ); if ( $chosen && $chosen !== $site ) { $switched = self::fetch( $slug, $chosen ); if ( ! is_wp_error( $switched ) && ! empty( $switched['categories'] ) ) { $menu = $switched; } else { $chosen = ''; } } wp_enqueue_style( 'menugps-menu', MENUGPS_PLUGIN_URL . 'assets/menu.css', array(), MENUGPS_VERSION ); return self::picker( $options, $chosen ? $chosen : $site ) . self::markup( $menu, $atts ); } /** * The languages this venue's menu may be switched into, or none. * * Empty for a free venue (the server sends no list, because there is nothing to switch to) * and empty when the owner has turned the picker off. Both checks live here so the caller * cannot accidentally offer one without the other. * * @return array> */ private static function language_options( array $menu ) { $plan = (array) ( isset( $menu['plan'] ) ? $menu['plan'] : array() ); if ( empty( $plan['languages'] ) ) { return array(); } if ( class_exists( 'MenuGPS_Appearance' ) && ! MenuGPS_Appearance::language_picker() ) { return array(); } return (array) ( isset( $plan['language_options'] ) ? $plan['language_options'] : array() ); } /** * The language the visitor asked for in the URL, if it is one this venue actually has. * * Checked against the server's list rather than a pattern: a two-letter regex would accept * `zz` and let anyone mint cache entries and API calls a letter at a time. */ private static function chosen_locale( array $options ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only display choice, no side effects. if ( ! $options || empty( $_GET['menu_lang'] ) ) { return ''; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- as above. $asked = strtolower( substr( sanitize_text_field( wp_unslash( $_GET['menu_lang'] ) ), 0, 2 ) ); foreach ( $options as $option ) { if ( isset( $option['code'] ) && $option['code'] === $asked ) { return $asked; } } return ''; } /** * The language list shown above the menu. * * Plain links with a query parameter, not a script: it works with JavaScript switched off, it * keeps every language a real URL somebody can send to a friend, and the existing per-locale * transient means a switched page is served from cache like any other. * * Names are endonyms — "Deutsch", not "German" — because the person hunting for their * language by definition cannot read the one the page is in. For the same reason there are no * flags: languages are not countries, several of the choices would be wrong or offensive, and * Windows browsers draw flag emoji as a two-letter box anyway. * * `rel="nofollow"` on every link, so 53 near-duplicate URLs never enter an index. The page's * own canonical is untouched and still points at the permalink. */ private static function picker( array $options, $current ) { if ( count( $options ) < 2 ) { return ''; } $links = ''; foreach ( $options as $option ) { $code = isset( $option['code'] ) ? (string) $option['code'] : ''; $name = isset( $option['name'] ) ? (string) $option['name'] : ''; if ( '' === $code || '' === $name ) { continue; } $links .= sprintf( '
  • %4$s
  • ', $code === $current ? ' is-current' : '', esc_url( add_query_arg( 'menu_lang', $code ) ), esc_attr( $code ), esc_html( $name ) ); } if ( '' === $links ) { return ''; } // Enhancement only: it lifts the visitor's own languages out of the list and into the row // above it. Without it the full list is still one click away and fully usable. wp_enqueue_script( 'menugps-picker', MENUGPS_PLUGIN_URL . 'assets/picker.js', array(), MENUGPS_VERSION, true ); return sprintf( '', esc_attr__( 'Menu language', 'menugps' ), esc_html( sprintf( /* translators: %d: number of languages the menu is available in. */ _n( '%d language', '%d languages', count( $options ), 'menugps' ), count( $options ) ) ), $links ); } /** * What this venue's subscription allows, for the settings screen. * * Read from the same cached menu response the front end uses, so checking it costs no extra * request and cannot drift from what visitors are actually served. * * @return array */ public static function plan( $slug = '' ) { $slug = $slug ? $slug : MenuGPS_API::venue(); if ( ! $slug ) { return array(); } $menu = self::fetch( $slug, self::locale() ); return is_wp_error( $menu ) ? array() : (array) ( isset( $menu['plan'] ) ? $menu['plan'] : array() ); } /** * Which language to ask for. * * The SITE's language, not the visitor's. That is deliberate: it is one value per site, so * the cache below stays one entry per language rather than one per reader, and the menu that * search engines index is the same one humans land on. * * A visitor who wants another language says so through the picker, which is a separate and * explicit choice — see picker() — rather than something smuggled in here off a browser * header, which would vary this page per reader and defeat both the cache and the index. * * On a WPML or Polylang site get_locale() already follows the page being viewed, so a * translated page gets a translated menu with no extra work. */ private static function locale() { $locale = function_exists( 'determine_locale' ) ? determine_locale() : get_locale(); return strtolower( substr( (string) $locale, 0, 2 ) ); } /** * The menu, from cache where possible. * * The locale is part of the key. Without it the first visitor's language would be handed to * everyone else for the next five minutes, which is the sort of bug that only shows up on a * site that actually has two languages. */ private static function fetch( $slug, $locale = '' ) { $v = (int) get_option( 'menugps_cache_v', 1 ); $key = 'menugps_rendered_' . md5( $v . '|' . (string) $slug . '|' . (string) $locale ); $cached = get_transient( $key ); if ( is_array( $cached ) ) { return $cached; } $query = array( 'venue' => $slug ); if ( $locale ) { // Ignored by the server unless the venue subscribes; free venues are served the // language they wrote the menu in. The plugin holds no rule about that on purpose. $query['locale'] = $locale; } $menu = MenuGPS_API::get( 'wordpress/menu', $query ); if ( ! is_wp_error( $menu ) ) { set_transient( $key, $menu, self::CACHE_SECONDS ); } return $menu; } /** The looks a wrapper can take; "classic" is the layout-only default and adds no class. */ const THEMES = array( 'classic', 'modern', 'cards', 'elegant', 'board' ); /** * Semantic, class-named markup and nothing else. * * Every colour and font is left to the theme: a menu should look like the rest of the site it * is on, and a plugin that imposes its own typeface is a plugin people fight with. The bundled * stylesheet handles layout and the chosen look, and every class is prefixed so a theme can * override any of it. * * The class names and structure match the MenuGPS Wix widget on purpose: the same menu on two * of a restaurant's channels should not look like two different menus. */ private static function markup( array $menu, array $atts ) { $show_prices = 'no' !== strtolower( (string) ( $atts['prices'] ?? 'yes' ) ); $show_desc = 'no' !== strtolower( (string) ( $atts['descriptions'] ?? 'yes' ) ); $show_tags = 'no' !== strtolower( (string) ( $atts['tags'] ?? 'yes' ) ); $show_photos = 'no' !== strtolower( (string) ( $atts['photos'] ?? 'yes' ) ); $show_sold_out = 'show' === strtolower( (string) ( $atts['sold_out'] ?? 'hide' ) ); $nav = strtolower( (string) ( $atts['nav'] ?? 'auto' ) ); $columns = max( 1, min( 3, (int) ( $atts['columns'] ?? 1 ) ) ); $theme = strtolower( (string) ( $atts['theme'] ?? 'classic' ) ); $theme = in_array( $theme, self::THEMES, true ) ? $theme : 'classic'; $accent = class_exists( 'MenuGPS_Appearance' ) ? MenuGPS_Appearance::accent() : ''; $categories = self::sections_to_show( (array) $menu['categories'], (string) ( $atts['section'] ?? '' ), $show_sold_out ); if ( ! $categories ) { return ''; } // Unique on the page even when two shortcodes show two venues, and the same on every // render so a link to a section can be shared. $prefix = 'menugps-' . sanitize_title( (string) ( $menu['venue']['slug'] ?? 'menu' ) ); $classes = array( 'menugps-menu', 'menugps-cols-' . $columns ); if ( 'classic' !== $theme ) { $classes[] = 'menugps-theme-' . $theme; } if ( ! $show_photos ) { $classes[] = 'menugps-no-photos'; } if ( ! $show_tags ) { $classes[] = 'menugps-no-tags'; } $out = sprintf( '
    ', esc_attr( implode( ' ', $classes ) ), // A custom property on the wrapper, so a theme can still override it and nothing // leaks into the rest of the page. $accent ? ' style="--menugps-accent: ' . esc_attr( $accent ) . '"' : '' ); $out .= self::occasion_line( $menu ); // Section tabs: asked for, or a long menu left on "auto". if ( 'yes' === $nav || ( 'auto' === $nav && count( $categories ) >= 4 ) ) { $out .= ''; } foreach ( $categories as $category ) { $out .= sprintf( ''; } $out .= '
    '; return $out; } /** * The sections to print, each holding only the dishes to print. * * A dish outside its serving hours is never shown: `is_available` is false then, and the * platform hides it from the menu on the table too. A sold-out dish is hidden as well unless * the owner chose to show it greyed — `sold_out` tells the two cases apart. * * `$only` narrows to named sections — "drinks", or "starters,mains" — matched on the name * the way a slug would be, so "From the grill" is `from-the-grill`. An id works too. * * @return array> */ private static function sections_to_show( array $categories, $only, $show_sold_out ) { $wanted = array_filter( array_map( 'sanitize_title', array_map( 'trim', explode( ',', (string) $only ) ) ) ); $shown = array(); foreach ( $categories as $category ) { if ( $wanted ) { $name = sanitize_title( (string) ( $category['name'] ?? '' ) ); $id = (string) (int) ( $category['id'] ?? 0 ); if ( ! in_array( $name, $wanted, true ) && ! in_array( $id, $wanted, true ) ) { continue; } } $items = array_values( array_filter( (array) ( $category['items'] ?? array() ), static function ( $item ) use ( $show_sold_out ) { if ( ! empty( $item['is_available'] ) ) { return true; } return $show_sold_out && ! empty( $item['sold_out'] ); } ) ); if ( ! $items ) { continue; } $category['items'] = $items; $shown[] = $category; } return $shown; } /** "Christmas Eve · until 23:00", when an occasion menu has replaced the usual one. */ private static function occasion_line( array $menu ) { $occasion = isset( $menu['occasion'] ) && is_array( $menu['occasion'] ) ? $menu['occasion'] : array(); if ( empty( $occasion['title'] ) ) { return ''; } $line = '' . esc_html( $occasion['title'] ) . ''; if ( ! empty( $occasion['until'] ) ) { $line .= ' · ' . esc_html( sprintf( /* translators: %s: a time, e.g. 23:00 */ __( 'until %s', 'menugps' ), $occasion['until'] ) ); } return ''; } private static function item_markup( array $item, $show_prices, $show_desc, $show_photos ) { $sold_out = empty( $item['is_available'] ) && ! empty( $item['sold_out'] ); $variants = $show_prices && ! empty( $item['variants'] ) && is_array( $item['variants'] ) ? $item['variants'] : array(); $out = ''; return $out; } }