159 lines
4.4 KiB
PHP
159 lines
4.4 KiB
PHP
<?php
|
|
/**
|
|
* Demo scaffolding: a language switcher, standing in for WPML.
|
|
*
|
|
* The real site resolves the menu through WPML, which is not installed here and
|
|
* is not what the demo is arguing about. But the argument *is* about languages
|
|
* — the claim is that one tree per language renders correctly without anyone
|
|
* cutting a bitmap — so the demo has to be able to show fifteen of them.
|
|
*
|
|
* So the fifteen imported trees are all real menus, and `?lang=xx` picks which
|
|
* one the `primary` location resolves to. Under a port this file goes away and
|
|
* WPML's own resolution takes over; nothing else in the theme knows about it.
|
|
*
|
|
* @package TLIG
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* The languages the demo has a menu for, in the order they are offered.
|
|
*
|
|
* @return array<string,string> Code => endonym.
|
|
*/
|
|
function tlig_demo_languages(): array {
|
|
return [
|
|
'en' => 'English',
|
|
'fr' => 'Français',
|
|
'de' => 'Deutsch',
|
|
'es' => 'Español',
|
|
'it' => 'Italiano',
|
|
'pt' => 'Português',
|
|
'nl' => 'Nederlands',
|
|
'da' => 'Dansk',
|
|
'sv' => 'Svenska',
|
|
'pl' => 'Polski',
|
|
'cs' => 'Čeština',
|
|
'bg' => 'Български',
|
|
'ru' => 'Русский',
|
|
'el' => 'Ελληνικά',
|
|
'ar' => 'العربية',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* The language currently being shown.
|
|
*/
|
|
function tlig_demo_lang(): string {
|
|
$want = isset( $_GET['lang'] ) ? sanitize_key( wp_unslash( $_GET['lang'] ) ) : 'en';
|
|
|
|
return isset( tlig_demo_languages()[ $want ] ) ? $want : 'en';
|
|
}
|
|
|
|
/**
|
|
* Arabic is the reason the whole layout is written in logical properties.
|
|
*/
|
|
function tlig_demo_is_rtl(): bool {
|
|
return 'ar' === tlig_demo_lang();
|
|
}
|
|
|
|
/**
|
|
* Points the primary location at the menu for the chosen language.
|
|
*
|
|
* @param array<string,mixed> $args wp_nav_menu arguments.
|
|
* @return array<string,mixed>
|
|
*/
|
|
function tlig_demo_pick_menu( $args ) {
|
|
if ( ( $args['theme_location'] ?? '' ) !== 'primary' ) {
|
|
return $args;
|
|
}
|
|
|
|
$menu = wp_get_nav_menu_object( 'TLIG ' . strtoupper( tlig_demo_lang() ) );
|
|
|
|
if ( $menu ) {
|
|
$args['menu'] = $menu;
|
|
}
|
|
|
|
return $args;
|
|
}
|
|
add_filter( 'wp_nav_menu_args', 'tlig_demo_pick_menu' );
|
|
|
|
/**
|
|
* Flips the document for Arabic.
|
|
*
|
|
* @param string $output The <html> attributes.
|
|
* @return string
|
|
*/
|
|
function tlig_demo_html_dir( $output ) {
|
|
if ( ! tlig_demo_is_rtl() ) {
|
|
return $output;
|
|
}
|
|
|
|
return preg_replace( '/\bdir="[^"]*"/', '', $output ) . ' dir="rtl" lang="ar"';
|
|
}
|
|
add_filter( 'language_attributes', 'tlig_demo_html_dir' );
|
|
|
|
/**
|
|
* The switcher itself.
|
|
*/
|
|
function tlig_demo_language_bar(): void {
|
|
$current = tlig_demo_lang();
|
|
|
|
echo '<div class="lang-bar"><div class="lang-bar__inner">';
|
|
echo '<span class="filter-bar__label">' . esc_html__( 'Language', 'tlig' ) . '</span>';
|
|
echo '<ul class="filter-bar">';
|
|
|
|
foreach ( tlig_demo_languages() as $code => $name ) {
|
|
$is_current = ( $code === $current );
|
|
|
|
printf(
|
|
'<li><a class="%s" href="%s" hreflang="%s"%s>%s</a></li>',
|
|
esc_attr( $is_current ? 'filter filter--sm is-active' : 'filter filter--sm' ),
|
|
esc_url( add_query_arg( 'lang', $code, home_url( '/' ) ) ),
|
|
esc_attr( $code ),
|
|
$is_current ? ' aria-current="page"' : '',
|
|
esc_html( $name )
|
|
);
|
|
}
|
|
|
|
echo '</ul></div></div>';
|
|
}
|
|
|
|
/**
|
|
* Demo scaffolding: force panels open, for screenshots and for the pitch.
|
|
*
|
|
* `?open=3` holds the third panel open; `?open=all` drops every panel into the
|
|
* flow, one under another. The second is the argument in one picture — seven
|
|
* panels of seven different heights, all drawn from the same 328px parchment
|
|
* file, where the old site needs a separately cut bitmap for each.
|
|
*
|
|
* It is a stylesheet, not a change to the markup, so nothing about the menu
|
|
* itself depends on it.
|
|
*/
|
|
function tlig_demo_open_panels(): void {
|
|
$open = isset( $_GET['open'] ) ? sanitize_key( wp_unslash( $_GET['open'] ) ) : '';
|
|
|
|
if ( '' === $open ) {
|
|
return;
|
|
}
|
|
|
|
if ( 'all' === $open ) {
|
|
$css = '.site-nav__bar{display:block}'
|
|
. '.site-nav__item{display:block}'
|
|
. '.site-nav__panel{display:block;position:static;margin-block:0 1.5rem}';
|
|
} else {
|
|
$n = max( 1, (int) $open );
|
|
$css = sprintf(
|
|
// The bar too: below the tablet breakpoint it is hidden until the
|
|
// toggle opens it, and a screenshot cannot press the toggle.
|
|
'.site-nav__bar{display:flex}.site-nav__item:nth-child(%d) > .site-nav__panel{display:block}',
|
|
$n
|
|
);
|
|
}
|
|
|
|
printf( "<style id=\"tlig-demo-open\">%s</style>\n", $css );
|
|
}
|
|
add_action( 'wp_head', 'tlig_demo_open_panels', 99 );
|