*/ private static array $stack = array(); public static function register(): void { if ( self::active() && is_admin() ) { add_action( 'init', array( self::class, 'register_strings' ), 30 ); } } public static function active(): bool { return defined( 'ICL_SITEPRESS_VERSION' ) || function_exists( 'pll_register_string' ); } /** * Offer the owner's text for translation. */ public static function register_strings(): void { foreach ( Services::bookables()->all() as $bookable ) { self::add( 'service_' . $bookable['id'] . '_name', (string) $bookable['name'] ); self::add( 'service_' . $bookable['id'] . '_description', (string) $bookable['description'], true ); } $saved = get_option( EmailTemplates::OPTION, array() ); foreach ( is_array( $saved ) ? $saved : array() as $type => $email ) { self::add( 'email_' . $type . '_subject', (string) ( $email['subject'] ?? '' ) ); self::add( 'email_' . $type . '_message', (string) ( $email['message'] ?? '' ), true ); } } /** * The text in the current language. * * @param string $value Text as typed. * @param string $name Its string name. */ public static function text( string $value, string $name ): string { if ( '' === $value || ! self::active() ) { return $value; } $lang = self::language( self::locale() ); if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { return (string) apply_filters( 'wpml_translate_single_string', $value, self::DOMAIN, $name, $lang ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's filter. } if ( function_exists( 'pll_translate_string' ) && '' !== $lang ) { return (string) pll_translate_string( $value, $lang ); } return $value; } /** * A page in the language of a locale. * * @param int $page Page. * @param string $locale Locale, '' for the current one. */ public static function page( int $page, string $locale = '' ): int { if ( $page <= 0 || ! self::active() ) { return $page; } $lang = self::language( '' !== $locale ? $locale : self::locale() ); if ( '' === $lang ) { return $page; } if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { return (int) apply_filters( 'wpml_object_id', $page, 'page', true, $lang ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's filter. } $translated = function_exists( 'pll_get_post' ) ? (int) pll_get_post( $page, $lang ) : 0; return $translated > 0 ? $translated : $page; } /** * Use a customer's language for a while, e.g. to write their email. * * @param string $locale Locale. * @return bool Whether it was switched; call restore() after. */ public static function switch_to( string $locale ): bool { if ( '' === $locale || self::locale() === $locale ) { return false; } self::$stack[] = array( $locale, switch_to_locale( $locale ) ); if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { do_action( 'wpml_switch_language', self::language( $locale ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's action. } return true; } public static function restore(): void { $last = array_pop( self::$stack ); if ( ! empty( $last[1] ) ) { restore_previous_locale(); } if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { do_action( 'wpml_switch_language', array() === self::$stack ? null : self::language( self::locale() ) ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's action. } } /** * The language text is shown in: a customer's while writing to them, * even when WordPress has no translation files for it, else the visitor's. */ public static function locale(): string { $last = end( self::$stack ); return false === $last ? determine_locale() : $last[0]; } /** * The plugin's language code for a locale, e.g. "de" for de_DE. * * @param string $locale Locale. */ public static function language( string $locale ): string { if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { foreach ( (array) apply_filters( 'wpml_active_languages', null, array( 'skip_missing' => 0 ) ) as $code => $lang ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's filter. if ( ( $lang['default_locale'] ?? '' ) === $locale ) { return (string) $code; } } return ''; } if ( function_exists( 'pll_languages_list' ) ) { $locales = (array) pll_languages_list( array( 'fields' => 'locale' ) ); $slugs = (array) pll_languages_list( array( 'fields' => 'slug' ) ); $at = array_search( $locale, $locales, true ); return false === $at ? '' : (string) ( $slugs[ $at ] ?? '' ); } return ''; } /** * Register one string. * * @param string $name Name. * @param string $value Text. * @param bool $multiline Long text. */ private static function add( string $name, string $value, bool $multiline = false ): void { if ( '' === trim( $value ) ) { return; } if ( defined( 'ICL_SITEPRESS_VERSION' ) ) { do_action( 'wpml_register_single_string', self::DOMAIN, $name, $value ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- WPML's action. } elseif ( function_exists( 'pll_register_string' ) ) { pll_register_string( $name, $value, self::DOMAIN, $multiline ); } } }