'', 'widget_look' => '', // Attribute used by [gtranslate] shortcodes. 'flags' => '', 'native' => '', ), (array) $atts ); $args = array(); $map = array( 'dropdown_with_flags' => 'dropdown_flags', 'flags_dropdown' => 'dropdown_flags', 'popup_search' => 'popup', 'lang_names' => 'names', 'lang_codes' => 'codes', 'float' => 'dropdown_flags', ); $style = $atts['style'] ? $atts['style'] : $atts['widget_look']; if ( $style ) { $args['style'] = $map[ $style ] ?? $style; } if ( '' !== $atts['flags'] ) { $args['show_flags'] = filter_var( $atts['flags'], FILTER_VALIDATE_BOOLEAN ); } if ( '' !== $atts['native'] ) { $args['native_names'] = filter_var( $atts['native'], FILTER_VALIDATE_BOOLEAN ); } return self::render( $args ); } /** * Render a switcher. Returns '' when the site has no target languages. * * @param array $args style, show_flags, native_names, flag_shape, theme, floating(position), class. */ public static function render( array $args = array() ) { if ( ! Settings::is_active() ) { return ''; } $config = Settings::effective(); $sw = array_merge( $config['switcher'], $args ); $style = in_array( $sw['style'], Settings::SWITCHER_STYLES, true ) ? $sw['style'] : 'dropdown_flags'; $router = Router::instance(); $cur = $router->current(); self::$assets_needed = true; $flags_for_style = in_array( $style, array( 'flags', 'flags_name', 'flags_code', 'dropdown_flags', 'popup', 'globe' ), true ); $show_flags = $flags_for_style && ! empty( $sw['show_flags'] ); if ( 'flags' === $style ) { $show_flags = true; } $items = array(); foreach ( Settings::all_languages() as $lang ) { $items[] = array( 'lang' => $lang, 'url' => $router->current_url_in( $lang ), 'label' => Languages::name( $lang, ! empty( $sw['native_names'] ) ), 'active' => $lang === $cur, ); } $classes = array( 'lf-switcher', 'lf-style-' . $style, 'lf-theme-' . ( in_array( $sw['theme'], array( 'dark', 'auto' ), true ) ? $sw['theme'] : 'light' ), 'notranslate' ); if ( 'round' === $sw['flag_shape'] ) { $classes[] = 'lf-flags-round'; } if ( ! empty( $sw['floating'] ) ) { $classes[] = 'lf-floating'; $classes[] = 'lf-pos-' . sanitize_html_class( $sw['floating'] ); } if ( ! empty( $sw['class'] ) ) { $classes[] = sanitize_html_class( $sw['class'] ); } $current_item = $items[0]; foreach ( $items as $item ) { if ( $item['active'] ) { $current_item = $item; } } $label = esc_attr__( 'Select language', 'langferry' ); $html = '
'; switch ( $style ) { case 'dropdown_flags': case 'dropdown': case 'globe': // What each entry shows besides its flag: the name, or the code or nothing when an // inline style floats (see floating()). $labels = in_array( $sw['labels'] ?? '', array( 'none', 'code' ), true ) ? $sw['labels'] : 'name'; $trigger = 'globe' === $style ? self::globe_icon() . '' . esc_html( strtoupper( Languages::url_slug( $current_item['lang'] ) ) ) . '' : self::item_inner( $current_item, $show_flags, $labels ); $html .= ''; $html .= ''; break; case 'popup': $html .= ''; $html .= ''; break; default: $mode = array( 'flags' => 'none', 'flags_name' => 'name', 'flags_code' => 'code', 'names' => 'name', 'codes' => 'code', )[ $style ]; $html .= ''; } return $html . '
'; } /** * The tags and attributes render() writes, for printing it through wp_kses(). Only these survive, * so markup added to render() must be added here too (tests/engine-input.php checks that every * style comes through unchanged). * * @return array> */ public static function allowed_html() { $aria = array( 'aria-label' => true, 'aria-hidden' => true, ); return array( 'div' => array( 'class' => true, 'translate' => true, 'data-*' => true, 'role' => true, 'aria-modal' => true, 'hidden' => true, ) + $aria, 'button' => array( 'type' => true, 'class' => true, 'aria-haspopup' => true, 'aria-expanded' => true, ) + $aria, 'ul' => array( 'class' => true, 'role' => true, 'hidden' => true, ), 'li' => array( 'class' => true ), 'a' => array( 'href' => true, 'hreflang' => true, 'lang' => true, 'data-*' => true, 'aria-current' => true, 'class' => true, 'title' => true, ), 'img' => array( 'class' => true, 'src' => true, 'width' => true, 'height' => true, 'alt' => true, 'loading' => true, 'decoding' => true, ), 'span' => array( 'class' => true ), 'input' => array( 'type' => true, 'class' => true, 'placeholder' => true, ) + $aria, 'svg' => array( 'class' => true, 'width' => true, 'height' => true, 'viewbox' => true, 'focusable' => true, ) + $aria, 'g' => array( 'fill' => true, 'stroke' => true, 'stroke-width' => true, ), 'path' => array( 'd' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true, ), 'circle' => array( 'cx' => true, 'cy' => true, 'r' => true, ), ); } private static function list_items( array $items, $show_flags, $mode ) { $out = ''; foreach ( $items as $item ) { $attrs = sprintf( 'href="%s" hreflang="%s" lang="%s" data-lf-lang="%s"%s', esc_url( $item['url'] ), esc_attr( Languages::hreflang( $item['lang'] ) ), esc_attr( Languages::hreflang( $item['lang'] ) ), esc_attr( $item['lang'] ), $item['active'] ? ' aria-current="true" class="lf-active"' : '' ); if ( 'none' === $mode ) { $attrs .= ' title="' . esc_attr( $item['label'] ) . '"'; } $out .= '
  • ' . self::item_inner( $item, $show_flags, $mode ) . '
  • '; } return $out; } private static function item_inner( array $item, $show_flags, $mode ) { $out = ''; if ( $show_flags ) { $alt = 'none' === $mode ? $item['label'] : ''; $out .= '' . esc_attr( $alt ) . ''; } if ( 'name' === $mode ) { $out .= '' . esc_html( $item['label'] ) . ''; } elseif ( 'code' === $mode ) { $out .= '' . esc_html( strtoupper( Languages::url_slug( $item['lang'] ) ) ) . ''; } return $out; } private static function chevron() { return ''; } private static function globe_icon() { return ''; } /* ------------------------------------------------------------------ */ /* Placement */ /* ------------------------------------------------------------------ */ public static function floating() { $config = Settings::effective(); if ( ! Settings::is_active() || 'inline' === $config['switcher']['position'] ) { return; } $args = array( 'style' => $config['switcher']['style'], 'floating' => $config['switcher']['position'], ); // A row of up to 100 flags or names can't float, so inline styles float as a dropdown that // shows what the style shows: flags, names or codes. $inline = array( 'flags' => array( 'dropdown_flags', 'none' ), 'flags_name' => array( 'dropdown_flags', 'name' ), 'flags_code' => array( 'dropdown_flags', 'code' ), 'names' => array( 'dropdown', 'name' ), 'codes' => array( 'dropdown', 'code' ), ); if ( isset( $inline[ $args['style'] ] ) ) { list( $args['style'], $args['labels'] ) = $inline[ $args['style'] ]; if ( 'none' === $args['labels'] ) { $args['show_flags'] = true; // "Flags" always shows them, as it does inline. } } echo wp_kses( self::render( $args ), self::allowed_html() ); } public static function menu_items( $items, $args ) { $location = Settings::effective()['switcher']['menu_location']; if ( ! $location || empty( $args->theme_location ) || $args->theme_location !== $location ) { return $items; } return $items . ''; } /** * Block themes: add the switcher to the site's Navigation block when the * menu location is set to "navigation". */ public static function navigation_block( $content, $block ) { static $done = false; if ( $done || 'navigation' !== Settings::effective()['switcher']['menu_location'] ) { return $content; } $pos = strrpos( $content, '' ); if ( false === $pos ) { return $content; } $done = true; $item = '
  • ' . self::render( array( 'class' => 'lf-in-menu' ) ) . '
  • '; return substr_replace( $content, $item . '', $pos, 5 ); } public static function register_widget() { register_widget( Widget::class ); } public static function register_block() { wp_register_script( 'langferry-block-editor', LANGFERRY_URL . 'assets/js/block.js', array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-server-side-render', 'wp-i18n' ), LANGFERRY_VERSION, true ); register_block_type( 'langferry/language-switcher', array( 'api_version' => 3, 'title' => __( 'Language Switcher', 'langferry' ), 'category' => 'widgets', 'icon' => 'translation', 'editor_script' => 'langferry-block-editor', 'editor_style' => 'langferry-switcher', 'attributes' => array( 'style' => array( 'type' => 'string', 'default' => '', ), ), 'render_callback' => static function ( $attributes ) { $args = array(); if ( ! empty( $attributes['style'] ) ) { $args['style'] = $attributes['style']; } $html = self::render( $args ); if ( '' === $html && defined( 'REST_REQUEST' ) && REST_REQUEST ) { return '

    ' . esc_html__( 'Language switcher — connect Langferry and choose languages to display it.', 'langferry' ) . '

    '; } return $html; }, ) ); wp_register_style( 'langferry-switcher', LANGFERRY_URL . 'assets/css/switcher.css', array(), LANGFERRY_VERSION ); } public static function enqueue() { if ( ! Settings::is_active() ) { return; } $config = Settings::effective(); wp_enqueue_style( 'langferry-switcher', LANGFERRY_URL . 'assets/css/switcher.css', array(), LANGFERRY_VERSION ); wp_enqueue_script( 'langferry-switcher', LANGFERRY_URL . 'assets/js/switcher.js', array(), LANGFERRY_VERSION, array( 'strategy' => 'defer', 'in_footer' => true ) ); $router = Router::instance(); $alternate = array(); if ( $config['auto_redirect'] && $router->is_default() ) { foreach ( $config['languages'] as $lang ) { $alternate[ $lang ] = $router->current_url_in( $lang ); } } wp_add_inline_script( 'langferry-switcher', 'window.langferry=' . wp_json_encode( array( 'current' => $router->current(), 'default' => $config['default_language'], 'autoRedirect' => (bool) $config['auto_redirect'], 'alternates' => (object) $alternate, ) ) . ';', 'before' ); } }