language = $language; $this->settings = $settings; $this->links = $links; $this->post_model = $links->model->post; $this->id = (int) $language->term_id; $this->slug = $language->slug; $this->locale = $language->get_locale( 'display' ); $this->direction = $language->is_rtl ? 'rtl' : 'ltr'; $this->order = (int) $language->term_group; $this->has_translations = true; $this->is_empty = $language->get_tax_prop( 'language', 'count' ) <= 0; $this->is_current = $this->get_current_language_slug() === $language->slug; $this->item_classes = array_merge( array( 'lang-item', "lang-item-{$language->term_id}", "lang-item-{$language->slug}" ), $settings->item_classes ); $this->link_classes = $settings->link_classes; if ( $this->is_current ) { $this->item_classes[] = 'current-lang'; } if ( $this->settings->show_labels ) { $this->label = 'codes' === $this->settings->show_labels ? strtoupper( $this->language->slug ) : $this->language->name; } $this->set_flag(); $this->set_url(); } /** * Returns the markup of a row. * * @since 3.9 * * @return string */ abstract public function get(): string; /** * Returns the markup of the label of a row. * * @since 3.9 * * @return string */ public function get_label(): string { return esc_html( $this->label ); } /** * Returns the current language code. * * @since 3.9 * * @return string */ private function get_current_language_slug(): string { if ( ! empty( $this->links->curlang ) ) { return $this->links->curlang->slug; } return $this->links->options['default_lang']; } /** * Sets the flag of the element. * * @since 3.9 * * @return void */ private function set_flag(): void { if ( ! $this->settings->show_flags ) { return; } $this->flag = $this->language->get_display_flag( $this->settings->show_labels ? 'no-alt' : 'alt' ); if ( empty( $this->flag ) ) { return; } $flag = new WP_HTML_Tag_Processor( $this->flag ); if ( ! $flag->next_tag( array( 'tag_name' => 'img' ) ) ) { return; } // Replaces the size set by `PLL_Language::get_flag_html()`. Keep in sync with `css/src/lib/switcher-flags.css`. if ( ! $flag->set_attribute( 'style', 'display:block;object-fit:cover;object-position:center;width:100%;height:100%' ) ) { return; } $this->flag = $flag->get_updated_html(); } /** * Sets the URL of the element. * * @since 3.9 * * @return void */ private function set_url(): void { $url = $this->get_original_url(); if ( empty( $url ) ) { $this->has_translations = false; $this->item_classes[] = 'no-translation'; } /** * Filter the link in the language switcher. * * @since 0.7 * @since 3.9 Return an empty string instead of `null`. * * @param string $url The link URL, an empty string if no translation was found. * @param string $slug The language code. * @param string $locale The language locale. */ $url = apply_filters( 'pll_the_language_link', $url, $this->language->slug, $this->language->locale ); if ( empty( $url ) || ! is_string( $url ) || $this->settings->force_home ) { $this->url = $this->links->get_home_url( $this->language ); } else { $this->url = $url; } } /** * Returns the original URL of the element. * * @since 3.9 * * @return string */ private function get_original_url(): string { global $post; // Priority to the post passed in parameters. if ( $this->settings->post_id ) { $tr_id = $this->post_model->get( $this->settings->post_id, $this->language ); if ( $tr_id && $this->post_model->current_user_can_read( $tr_id ) ) { return (string) get_permalink( $tr_id ); } } // If we are on frontend. if ( $this->links instanceof PLL_Frontend_Links ) { return $this->links->get_translation_url( $this->language ); } // For blocks in posts in REST requests. if ( $post instanceof WP_Post ) { $tr_id = $this->post_model->get( $post->ID, $this->language ); if ( $tr_id && $this->post_model->current_user_can_read( $tr_id ) ) { return (string) get_permalink( $tr_id ); } } return ''; } }