1; } /** * Active languages as code => display name. * * @return array */ public static function languages() { static $cache = null; if ( null !== $cache ) { return $cache; } $languages = array(); if ( function_exists( 'pll_languages_list' ) ) { $slugs = pll_languages_list( array( 'fields' => 'slug' ) ); $names = pll_languages_list( array( 'fields' => 'name' ) ); foreach ( (array) $slugs as $index => $slug ) { $languages[ (string) $slug ] = isset( $names[ $index ] ) ? (string) $names[ $index ] : (string) $slug; } } if ( empty( $languages ) && has_filter( 'wpml_active_languages' ) ) { // WPML's own read API; the prefix rule is about hooks we own. $active = apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 0 ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Third-party API. foreach ( (array) $active as $code => $entry ) { $languages[ (string) $code ] = isset( $entry['translated_name'] ) ? (string) $entry['translated_name'] : (string) $code; } } /** * Filter the language list Oorank groups content by. * * Supply this to support a translation plugin Oorank does not know. * * @param array $languages Code => display name. */ $cache = apply_filters( 'oorank_languages', $languages ); return $cache; } /** * The language code a post belongs to. * * @param WP_Post|int $post Post or ID. * @return string Code, or '' when unknown or monolingual. */ public static function post_language( $post ) { $post_id = ( $post instanceof WP_Post ) ? $post->ID : absint( $post ); if ( ! $post_id ) { return ''; } if ( function_exists( 'pll_get_post_language' ) ) { return (string) pll_get_post_language( $post_id, 'slug' ); } if ( has_filter( 'wpml_post_language_details' ) ) { $details = apply_filters( 'wpml_post_language_details', null, $post_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Third-party API. if ( is_array( $details ) && ! empty( $details['language_code'] ) ) { return (string) $details['language_code']; } } return ''; } /** * Whether a code is one of the site's languages. * * @param string $code Language code. * @return bool */ public static function is_language( $code ) { return '' !== $code && isset( self::languages()[ $code ] ); } /** * Display name for a code, falling back to the code itself. * * @param string $code Language code. * @return string */ public static function label( $code ) { $languages = self::languages(); return isset( $languages[ $code ] ) ? $languages[ $code ] : $code; } /** * The URL of a language's own llms.txt. * * @param string $code Language code. * @param string $variant index|full. * @return string */ public static function endpoint( $code, $variant = 'index' ) { $file = ( 'full' === $variant ) ? 'llms-full.txt' : 'llms.txt'; if ( get_option( 'permalink_structure' ) ) { return home_url( '/' . $code . '/' . $file ); } return add_query_arg( 'oorank_lang', $code, home_url( '/' . $file ) ); } }