HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/delta/wp-content/plugins/wpml-string-translation/classes/MO/Hooks/LoadTranslationFile.php
<?php

namespace WPML\ST\MO\Hooks;

use WPML\ST\MO\File\Manager;
use WPML\ST\MO\LoadedMODictionary;
use WPML_ST_Translations_File_Locale;
use function WPML\FP\partial;
use WPML\LIB\WP\WordPress;

class LoadTranslationFile
{

	private static $isHookLoaded = false;

	private static $fileReplacements = [];

	public static function replaceMoExtensionWithPhp( $mofile ) {
		if ( ! is_string( $mofile ) ) {
			return '';
		}

		return preg_replace( '/\.mo$/', '.l10n.php', $mofile );
	}

	public static function replaceTranslationFile( $domain, $mofile, $replacedMoFile ) {
		if ( ! self::$isHookLoaded ) {
			self::loadHook();
		}
		if ( ! isset( self::$fileReplacements[ $domain ] ) ) {
			self::$fileReplacements[ $domain ] = [];
		}
		self::$fileReplacements[ $domain ][ $mofile ] = $replacedMoFile;
	}


	public static function loadTranslationFile( $file, $domain ) {
		if ( isset( self::$fileReplacements[ $domain ][ $file ] ) ) {
			return self::$fileReplacements[ $domain ][ $file ];
		}
		return $file;
	}


	/**
	 * @param string $domain
	 * @param string $locale
	 * @param bool $disableVersionCheck
	 * @return null|string
	 */
	public static function getDefaultWordPressTranslationPath( $domain, $locale, $disableVersionCheck = false ) {
		if ( ! $disableVersionCheck && ! WordPress::versionCompare('>', '6.6.999') ) {
			return null;
		}
		$defaultPluginTranslation = self::checkTranslationsFolder( 'plugins', $domain, $locale );
		if ( $defaultPluginTranslation ) {
			return $defaultPluginTranslation;
		}
		$defaultThemeTranslation = self::checkTranslationsFolder( 'themes', $domain, $locale );
		if ( $defaultThemeTranslation ) {
			return $defaultThemeTranslation;
		}

		global $wp_textdomain_registry;
		if ( ! isset( $wp_textdomain_registry ) ) {
			return false;
		}
		$defaultPathDirectory =  $wp_textdomain_registry->get( $domain, $locale );

		$defaultPathFile =  "{$defaultPathDirectory}{$domain}-{$locale}.mo";

		$template_directory   = trailingslashit( get_template_directory() );
		$stylesheet_directory = trailingslashit( get_stylesheet_directory() );
		if (
			str_starts_with( $defaultPathDirectory, $template_directory ) ||
			str_starts_with( $defaultPathDirectory, $stylesheet_directory )
		) {
			$defaultPathFile = "{$defaultPathDirectory}{$locale}.mo";
		}

		$defaultPathPHP = self::replaceMoExtensionWithPhp( $defaultPathFile );
		if ( file_exists( $defaultPathFile ) ) {
			return $defaultPathFile;
		}
		if (  file_exists( $defaultPathPHP ) ) {
			return $defaultPathPHP;
		}
		return null;
	}


	private static function loadHook() {
		add_filter( 'load_translation_file', [ __CLASS__, 'loadTranslationFile' ], 10, 2 );
	}


	/**
	 * @param "plugins"|"themes" $prefix
	 * @param string $domain
	 * @param string $locale
	 * @return string|null
	 */
	private static function checkTranslationsFolder( $prefix, $domain, $locale ) {
		$defaultLegacyPath = WP_LANG_DIR . "/$prefix/$domain-$locale.mo";
		if ( file_exists( $defaultLegacyPath ) ) {
			return $defaultLegacyPath;
		}
		$defaultLegacyPathPHP = WP_LANG_DIR . "/$prefix/$domain-$locale.l10n.php";
		if ( file_exists( $defaultLegacyPathPHP ) ) {
			return $defaultLegacyPathPHP;
		}
		return null;
	}

}