settings = $settings; $this->router = $router; } public function register(): void { add_shortcode( 'translateplus_switcher', array( $this, 'shortcode' ) ); add_action( 'init', array( $this, 'register_block' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) ); add_action( 'wp_footer', array( $this, 'floating' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'redirect_script' ), 1 ); add_filter( 'wp_nav_menu_objects', array( $this, 'menu_items' ), 20 ); add_action( 'admin_head-nav-menus.php', array( $this, 'menu_metabox' ) ); } /** * Data for every enabled language on the current page. * * @return array */ public function items(): array { $items = array(); foreach ( Languages::all() as $code ) { $items[] = array( 'code' => $code, 'name' => Languages::name( $code ), 'native' => Languages::name( $code, true ), 'url' => $this->router->language_url( $code ), 'current' => Languages::current() === $code, ); } return (array) apply_filters( 'translateplus_switcher_items', $items ); } /** * Render the switcher. * * @param array $args layout: dropdown|list, display: flags-names|names|codes, class. */ public function render( array $args = array() ): string { if ( ! Languages::targets() ) { return ''; } $args = wp_parse_args( $args, array( 'layout' => 'dropdown', 'display' => $this->settings->get( 'switcher' )['style'] ?? 'flags-names', 'class' => '', ) ); $items = $this->items(); $current = current( wp_list_filter( $items, array( 'current' => true ) ) ) ?: $items[0]; $classes = trim( 'tp-switcher tp-switcher--' . sanitize_html_class( $args['layout'] ) . ' tp-switcher--' . sanitize_html_class( $args['display'] ) . ' ' . $args['class'] ); $list = ''; foreach ( $items as $item ) { $list .= sprintf( '
  • %5$s
  • ', esc_url( $item['url'] ), esc_attr( Languages::bcp47( $item['code'] ) ), esc_attr( $item['code'] ), $item['current'] ? ' aria-current="true" class="is-current"' : '', $this->label( $item, $args['display'] ) ); } if ( 'list' === $args['layout'] ) { return ''; } return ''; } private function label( array $item, string $display ): string { $code = ''; $name = '' . esc_html( $item['native'] ) . ''; switch ( $display ) { case 'names': return $name; case 'codes': return $code . '' . esc_html( $item['native'] ) . ''; default: return $code . $name; } } public function shortcode( $atts ): string { $atts = shortcode_atts( array( 'layout' => 'dropdown', 'display' => '', 'class' => '', ), (array) $atts, 'translateplus_switcher' ); return $this->render( array_filter( $atts ) ); } public function register_block(): void { wp_register_script( 'translateplus-switcher-block', TRANSLATEPLUS_URL . 'assets/js/switcher-block.js', array( 'wp-blocks', 'wp-element', 'wp-block-editor', 'wp-components', 'wp-server-side-render', 'wp-i18n' ), TRANSLATEPLUS_VERSION, true ); wp_add_inline_script( 'translateplus-switcher-block', 'window.translateplusLogo = ' . wp_json_encode( TRANSLATEPLUS_URL . 'assets/images/logo.png' ) . ';', 'before' ); register_block_type( TRANSLATEPLUS_DIR . 'blocks/language-switcher', array( 'editor_script' => 'translateplus-switcher-block', 'render_callback' => function ( $attrs ) { return $this->render( array( 'layout' => $attrs['layout'] ?? 'dropdown', 'display' => $attrs['display'] ?? 'flags-names', 'class' => $attrs['className'] ?? '', ) ); }, ) ); } public function enqueue(): void { wp_enqueue_style( 'translateplus-switcher', TRANSLATEPLUS_URL . 'assets/css/switcher.css', array(), TRANSLATEPLUS_VERSION ); wp_enqueue_script( 'translateplus-switcher', TRANSLATEPLUS_URL . 'assets/js/switcher.js', array(), TRANSLATEPLUS_VERSION, array( 'strategy' => 'defer', 'in_footer' => true ) ); } public function floating(): void { $cfg = (array) $this->settings->get( 'switcher' ); if ( empty( $cfg['floating'] ) || ! Languages::targets() ) { return; } echo '
    ' . $this->render( array( 'layout' => 'dropdown' ) ) . '
    '; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } /** * Browser-language redirect, done client-side so it works behind full-page * caches. Runs once per visitor (cookie) and never for crawlers. */ public function redirect_script(): void { if ( ! $this->settings->get( 'browser_redirect' ) || ! Languages::targets() || ! Languages::is_source_current() || is_user_logged_in() ) { return; } $urls = array(); foreach ( $this->items() as $item ) { $urls[ strtolower( $item['code'] ) ] = $item['url']; } // In the head and not deferred, so the redirect happens before the page paints. wp_enqueue_script( 'translateplus-redirect', TRANSLATEPLUS_URL . 'assets/js/redirect.js', array(), TRANSLATEPLUS_VERSION, false ); wp_add_inline_script( 'translateplus-redirect', 'window.translateplusRedirect = ' . wp_json_encode( array( 'urls' => $urls, 'source' => Languages::source(), 'sourceBase' => strtolower( (string) strtok( Languages::source(), '-' ) ), 'cookie' => Router::COOKIE, ) ) . ';', 'before' ); } /** * Expand the "Language switcher" menu item into one item per language. * * @param \WP_Post[] $items Menu items. * @return \WP_Post[] */ public function menu_items( $items ) { $out = array(); foreach ( $items as $item ) { if ( self::MENU_URL !== $item->url ) { $out[] = $item; continue; } $langs = $this->items(); $current = current( wp_list_filter( $langs, array( 'current' => true ) ) ) ?: $langs[0]; $parent = clone $item; $parent->title = esc_html( $current['native'] ); $parent->url = $current['url']; $parent->classes = array_merge( (array) $item->classes, array( 'tp-menu-switcher', 'menu-item-has-children' ) ); $parent->attr_title = __( 'Change language', 'translateplus' ); $out[] = $parent; foreach ( $langs as $i => $lang ) { if ( $lang['current'] ) { continue; } $child = clone $item; $child->ID = (int) $item->ID * 1000 + $i + 1; $child->db_id = $child->ID; $child->menu_item_parent = (string) $item->db_id; $child->title = esc_html( $lang['native'] ); $child->url = $lang['url']; $child->classes = array( 'tp-menu-lang', 'tp-menu-lang-' . sanitize_html_class( $lang['code'] ) ); $child->xfn = 'alternate'; $out[] = $child; } } return $out; } public function menu_metabox(): void { add_meta_box( 'translateplus-menu-switcher', __( 'Language switcher', 'translateplus' ), static function () { ?>