array( 'class' => true, 'aria-label' => true, ), 'ul' => array( 'class' => true ), 'li' => array( 'class' => true ), 'a' => array( 'class' => true, 'href' => true, 'hreflang' => true, 'aria-current' => true, ), 'span' => array( 'lang' => true ), ); /** * Hook into WordPress. * * @return void */ public function register(): void { add_shortcode( 'vabelon_switcher', array( $this, 'shortcode' ) ); add_action( 'vabelon_switcher', array( $this, 'output' ) ); } /** * The links to show. * * @param int|null $post_id Post being viewed, or null on a page that is not one post. * @param string $active Language of the page being viewed. * @return array */ public function model( ?int $post_id, string $active ): array { $translations = null !== $post_id ? Plugin::translations()->translations_of( $post_id, array( 'publish' ) ) : array(); $items = array(); foreach ( Plugin::languages()->all() as $language ) { $slug = $language->slug; if ( null !== $post_id && ! isset( $translations[ $slug ] ) ) { continue; } $items[] = array( 'slug' => $slug, 'label' => $language->native_name, 'hreflang' => $language->hreflang, 'url' => null !== $post_id ? (string) get_permalink( $translations[ $slug ] ) : Plugin::urls()->home( $slug ), 'is_current' => $slug === $active, ); } return $items; } /** * The switcher's markup; empty when there is nothing to switch to. * * @param array $items Links. * @return string */ public function render( array $items ): string { if ( 2 > count( $items ) ) { return ''; } $html = ''; } /** * The switcher for the page being viewed. * * @return string */ public function shortcode(): string { $post_id = is_singular() ? get_queried_object_id() : 0; $post_id = 0 < $post_id && Plugin::translations()->supports( $post_id ) ? $post_id : null; $active = null !== $post_id ? Plugin::translations()->effective_language( $post_id ) : Plugin::request()->language(); return $this->render( $this->model( $post_id, $active ) ); } /** * Print the switcher for the page being viewed. * * @return void */ public function output(): void { echo wp_kses( $this->shortcode(), self::ALLOWED ); } }