$codes, 'source' => $source, ); } /** * Same guarded, fall-through-on-anything-unexpected philosophy as * from_multilingual_plugin() above, and checked in the same order — * WPML and Polylang confirmed working in production, WP Multilang/ * TranslatePress/Weglot best-effort (their options/functions read * here are real and documented, but none of those three * integrations has been run against an actual install of its plugin * yet). Returns null when no multilingual plugin is active or none * exposes a language list we recognize — active_languages() already * handles that case. */ private static function active_languages_from_multilingual_plugin() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- see from_multilingual_plugin() above. $wpml = apply_filters( 'wpml_active_languages', null ); if ( is_array( $wpml ) && ! empty( $wpml ) ) { return array( 'codes' => self::normalize_codes( array_keys( $wpml ) ), 'source' => 'WPML', ); } if ( function_exists( 'pll_languages_list' ) ) { $pll = pll_languages_list(); if ( is_array( $pll ) && ! empty( $pll ) ) { return array( 'codes' => self::normalize_codes( $pll ), 'source' => 'Polylang', ); } } // WP Multilang: wpm_get_languages() returns every *enabled* // language as an associative array keyed by that language's own // short code (confirmed directly from its real source, // WPM_Setup::get_languages() in class-wpm-setup.php) — the keys // are exactly what active_languages() needs, same shape as // Polylang's own flat code list above. if ( function_exists( 'wpm_get_languages' ) ) { $wpm = wpm_get_languages(); if ( is_array( $wpm ) && ! empty( $wpm ) ) { return array( 'codes' => self::normalize_codes( array_keys( $wpm ) ), 'source' => 'WP Multilang', ); } } // TranslatePress stores its own settings (including which // languages a site translates into) under this option name — // no public function wraps it, so this reads the option // directly. Untested against a real install; a missing/renamed // option just returns false and falls through, same as no // multilingual plugin at all. $trp_settings = get_option( 'trp_settings' ); if ( is_array( $trp_settings ) && ! empty( $trp_settings['translation-languages'] ) && is_array( $trp_settings['translation-languages'] ) ) { return array( 'codes' => self::normalize_codes( $trp_settings['translation-languages'] ), 'source' => 'TranslatePress', ); } // Weglot stores its source language and its list of destination // languages as two plain options — also untested against a real // install; missing options just return false and fall through. $weglot_from = get_option( 'weglot_language_from' ); $weglot_to = get_option( 'weglot_language_to' ); if ( is_string( $weglot_from ) && '' !== $weglot_from ) { $codes = array_merge( array( $weglot_from ), is_array( $weglot_to ) ? $weglot_to : array() ); return array( 'codes' => self::normalize_codes( $codes ), 'source' => 'Weglot', ); } return null; } private static function normalize_codes( $codes ) { $codes = array_map( array( __CLASS__, 'bucket' ), $codes ); return array_values( array_unique( array_filter( $codes ) ) ); } // ISO 639-1 codes for languages conventionally written right-to- // left — kept hand-in-sync with widget/alpineconsent.js's own // RTL_LANGS (same reasoning as this project's other cross-language // ports: no shared build step between the WordPress plugin and the // widget, so this is the pragmatic choice). Independent of // AlpineConsent_Strings coverage — a language can be RTL with no // curated translation yet (e.g. Urdu) and still needs its editing // fields/banner mirrored correctly. const RTL_LANGUAGES = array( 'ar', 'he', 'fa', 'ur', 'ps', 'sd', 'yi', 'ku', 'dv' ); public static function is_rtl( $code ) { return in_array( self::bucket( $code ), self::RTL_LANGUAGES, true ); } }