Файловый менеджер - Редактировать - /var/www/xthruster/html/wp-content/uploads/flags/gettext-hooks.tar
Назад
Hooks.php 0000644 00000010032 14721504410 0006332 0 ustar 00 <?php /** * WPML\ST\Gettext\Hooks class file. * * @package WPML\ST */ namespace WPML\ST\Gettext; use SitePress; use function WPML\Container\make; use WPML\ST\Gettext\Filters\IFilter; use WPML\ST\StringsFilter\Provider; /** * Class WPML\ST\Gettext\Hooks */ class Hooks implements \IWPML_Action { /** @var Filters\IFilter[] $filters */ private $filters = []; /** * @var SitePress SitePress */ private $sitepress; /** * @var array */ private $string_cache = []; /** * @var string|null */ private $lang; public function __construct( SitePress $sitepress ) { $this->sitepress = $sitepress; } /** * Init hooks. */ public function add_hooks() { add_action( 'plugins_loaded', array( $this, 'init_gettext_hooks' ), 2 ); } public function addFilter( IFilter $filter ) { $this->filters[] = $filter; } public function clearFilters() { $this->filters = []; } /** * @param string $lang */ public function switch_language_hook( $lang ) { $this->lang = $lang; } /** * @throws \WPML\Auryn\InjectionException * @deprecated since WPML ST 3.0.0 */ public function clear_filters() { // @todo: This is used in WCML tests, we will have to adjust there accordingly. /** @var Provider $filter_provider */ $filter_provider = make( Provider::class ); $filter_provider->clearFilters(); } /** * Init gettext hooks. */ public function init_gettext_hooks() { add_filter( 'gettext', [ $this, 'gettext_filter' ], 9, 3 ); add_filter( 'gettext_with_context', [ $this, 'gettext_with_context_filter' ], 1, 4 ); add_filter( 'ngettext', [ $this, 'ngettext_filter' ], 9, 5 ); add_filter( 'ngettext_with_context', [ $this, 'ngettext_with_context_filter' ], 9, 6 ); add_action( 'wpml_language_has_switched', [ $this, 'switch_language_hook' ] ); } /** * @param string $translation * @param string $text * @param string|array $domain * @param string|false $name Deprecated since WPML ST 3.0.0 (the name should be automatically created as a hash) * * @return string */ public function gettext_filter( $translation, $text, $domain, $name = false ) { if ( is_array( $domain ) ) { $domain_key = implode( '', $domain ); } else { $domain_key = $domain; } if ( ! $name ) { $name = md5( $text ); } if ( ! $this->lang ) { $this->lang = $this->sitepress->get_current_language(); } $key = $translation . $text . $domain_key . $name . $this->lang; if ( isset( $this->string_cache[ $key ] ) ) { return $this->string_cache[ $key ]; } foreach ( $this->filters as $filter ) { $translation = $filter->filter( $translation, $text, $domain, $name ); } $this->string_cache[ $key ] = $translation; return $translation; } /** * @param string $translation * @param string $text * @param string|false $context * @param string $domain * * @return string */ public function gettext_with_context_filter( $translation, $text, $context, $domain ) { if ( $context ) { $domain = [ 'domain' => $domain, 'context' => $context, ]; } return $this->gettext_filter( $translation, $text, $domain ); } /** * @param string $translation * @param string $single * @param string $plural * @param string $number * @param string $domain * @param string|false $context * * @return string */ public function ngettext_filter( $translation, $single, $plural, $number, $domain, $context = false ) { if ( $number == 1 ) { $string_to_translate = $single; } else { $string_to_translate = $plural; } return $this->gettext_with_context_filter( $translation, $string_to_translate, $context, $domain ); } /** * @param string $translation * @param string $single * @param string $plural * @param string $number * @param string $context * @param string $domain * * @return string */ public function ngettext_with_context_filter( $translation, $single, $plural, $number, $context, $domain ) { return $this->ngettext_filter( $translation, $single, $plural, $number, $domain, $context ); } } Settings.php 0000644 00000002637 14721504410 0007063 0 ustar 00 <?php /** * @author OnTheGo Systems */ namespace WPML\ST\Gettext; use SitePress; class Settings { /** @var SitePress $sitepress */ private $sitepress; /** @var AutoRegisterSettings $auto_register_settings */ private $auto_register_settings; public function __construct( SitePress $sitepress, AutoRegisterSettings $auto_register_settings ) { $this->sitepress = $sitepress; $this->auto_register_settings = $auto_register_settings; } /** @return bool */ public function isTrackStringsEnabled() { return (bool) $this->getSTSetting( 'track_strings', false ); } /** @return string */ public function getTrackStringColor() { return (string) $this->getSTSetting( 'hl_color', '' ); } /** @return bool */ public function isAutoRegistrationEnabled() { return (bool) $this->auto_register_settings->isEnabled(); } /** * @param string|array $domain * * @return bool */ public function isDomainRegistrationExcluded( $domain ) { if ( is_array( $domain ) && array_key_exists( 'domain', $domain ) ) { $domain = $domain[ 'domain' ]; } return (bool) $this->auto_register_settings->isExcludedDomain( $domain ); } /** * @param string $key * @param mixed $default * * @return mixed|null */ private function getSTSetting( $key, $default = null ) { $settings = $this->sitepress->get_setting( 'st' ); return isset( $settings[ $key ] ) ? $settings[ $key ] : $default; } } filters/StringHighlighting.php 0000644 00000002344 14721504410 0012522 0 ustar 00 <?php namespace WPML\ST\Gettext\Filters; use WPML\ST\Gettext\HooksFactory; use WPML\ST\Gettext\Settings; class StringHighlighting implements IFilter { /** @var Settings $settings */ private $settings; public function __construct( Settings $settings ) { $this->settings = $settings; } /** * @param string $translation * @param string $text * @param string|array $domain * @param string|false $name * * @return string */ public function filter( $translation, $text, $domain, $name = false ) { if ( is_array( $domain ) ) { $domain = $domain['domain']; } if ( $this->isHighlighting( $domain, $text ) ) { $translation = '<span style="background-color:' . esc_attr( $this->settings->getTrackStringColor() ) . '">' . $translation . '</span>'; } return $translation; } /** * @param string $domain * @param string $text * * @return bool */ private function isHighlighting( $domain, $text ) { return isset( $_GET[ HooksFactory::TRACK_PARAM_TEXT ], $_GET[ HooksFactory::TRACK_PARAM_DOMAIN ] ) && stripslashes( $_GET[ HooksFactory::TRACK_PARAM_DOMAIN ] ) === $domain && stripslashes( $_GET[ HooksFactory::TRACK_PARAM_TEXT ] ) === $text; } } filters/StringTranslation.php 0000644 00000002076 14721504410 0012415 0 ustar 00 <?php namespace WPML\ST\Gettext\Filters; use WPML\ST\Gettext\Settings; class StringTranslation implements IFilter { /** @var Settings $settings */ private $settings; public function __construct( Settings $settings ) { $this->settings = $settings; } /** * @param string $translation * @param string $text * @param string|array $domain * @param string|false $name * * @return string */ public function filter( $translation, $text, $domain, $name = false ) { if ( $this->settings->isDomainRegistrationExcluded( $domain ) ) { return $translation; } if ( ! defined( 'ICL_STRING_TRANSLATION_DYNAMIC_CONTEXT' ) ) { define( 'ICL_STRING_TRANSLATION_DYNAMIC_CONTEXT', 'wpml_string' ); } if ( $domain === ICL_STRING_TRANSLATION_DYNAMIC_CONTEXT ) { icl_register_string( $domain, (string) $name, $text ); } $has_translation = null; $found_translation = icl_translate( $domain, (string) $name, $text, false, $has_translation ); if ( $has_translation ) { return $found_translation; } else { return $translation; } } } filters/StringTracking.php 0000644 00000001123 14721504410 0011651 0 ustar 00 <?php namespace WPML\ST\Gettext\Filters; class StringTracking implements IFilter { /** * @param string $translation * @param string $text * @param string|array $domain * @param string|false $name * * @return string */ public function filter( $translation, $text, $domain, $name = false ) { if ( $this->canTrackStrings() ) { icl_st_track_string( $text, $domain, ICL_STRING_TRANSLATION_STRING_TRACKING_TYPE_PAGE ); } return $translation; } /** * @return bool */ public function canTrackStrings() { return did_action( 'after_setup_theme' ); } } filters/IFilter.php 0000644 00000000517 14721504410 0010264 0 ustar 00 <?php /** * @author OnTheGo Systems */ namespace WPML\ST\Gettext\Filters; interface IFilter { /** * @param string $translation * @param string $text * @param string|array $domain * @param string|false $name * * @return string */ public function filter( $translation, $text, $domain, $name = false ); } HooksFactory.php 0000644 00000003557 14721504410 0007700 0 ustar 00 <?php /** * @author OnTheGo Systems */ namespace WPML\ST\Gettext; use function WPML\Container\make; use WPML_ST_Upgrade; class HooksFactory implements \IWPML_Backend_Action_Loader, \IWPML_Frontend_Action_Loader { const TRACK_PARAM_TEXT = 'icl_string_track_value'; const TRACK_PARAM_DOMAIN = 'icl_string_track_context'; /** * @return \IWPML_Action|Hooks|null * @throws \WPML\Auryn\InjectionException */ public function create() { /** * @deprecated this global should not be used anymore. * * @var Hooks $st_gettext_hooks */ global $st_gettext_hooks; $st_gettext_hooks = null; $filters = $this->getFilters(); if ( ! $filters ) { return $st_gettext_hooks; } /** @var Hooks $st_gettext_hooks */ $st_gettext_hooks = make( Hooks::class ); $st_gettext_hooks->clearFilters(); foreach ( $filters as $filter ) { $st_gettext_hooks->addFilter( $filter ); } return $st_gettext_hooks; } /** * @return Filters\IFilter[] * @throws \WPML\Auryn\InjectionException */ private function getFilters() { $filters = []; /** @var Settings $settings */ $settings = make( Settings::class ); if ( $settings->isAutoRegistrationEnabled() ) { $filters[] = make( Filters\StringTranslation::class ); } if ( $this->isTrackingStrings( $settings ) ) { $filters[] = make( Filters\StringTracking::class ); } if ( $this->isHighlightingStrings() ) { $filters[] = make( Filters\StringHighlighting::class ); } return $filters; } /** * @param Settings $settings * * @return bool */ private function isTrackingStrings( Settings $settings ) { return $settings->isTrackStringsEnabled() && current_user_can( 'edit_others_posts' ) && ! is_admin(); } /** * @return bool */ private function isHighlightingStrings() { return isset( $_GET[ self::TRACK_PARAM_TEXT ], $_GET[ self::TRACK_PARAM_DOMAIN ] ); } } AutoRegisterSettings.php 0000644 00000016757 14721504410 0011431 0 ustar 00 <?php namespace WPML\ST\Gettext; use wpdb; use WPML\FP\Obj; use WPML\ST\MO\Hooks\PreloadThemeMoFile; use WPML\ST\Package\Domains; use function wpml_collect; use WPML_ST_Settings; class AutoRegisterSettings { const KEY_EXCLUDED_DOMAINS = 'wpml_st_auto_reg_excluded_contexts'; const KEY_ENABLED = 'auto_register_enabled'; const RESET_AUTOLOAD_TIMEOUT = 2 * HOUR_IN_SECONDS; /** * @var wpdb $wpdb */ protected $wpdb; /** * @var WPML_ST_Settings */ private $settings; /** * @var Domains */ private $package_domains; /** @var \WPML_Localization */ private $localization; /** * @var array */ private $excluded_domains; public function __construct( wpdb $wpdb, WPML_ST_Settings $settings, Domains $package_domains, \WPML_Localization $localization ) { $this->wpdb = $wpdb; $this->settings = $settings; $this->package_domains = $package_domains; $this->localization = $localization; } /** @return bool */ public function isEnabled() { $setting = $this->getSetting( self::KEY_ENABLED, [ 'enabled' => false ] ); if ( $setting['enabled'] ) { $elapsed_time = time() - $setting['time']; $isStillEnabled = self::RESET_AUTOLOAD_TIMEOUT > $elapsed_time; $setting['enabled'] = $isStillEnabled; if ( ! $isStillEnabled ) { $this->setEnabled( false ); } } return $setting['enabled']; } /** * @param bool $isEnabled */ public function setEnabled( $isEnabled ) { $setting = $this->getSetting( self::KEY_ENABLED, [ 'enabled' => false ] ); $setting['enabled'] = ( $isEnabled && $this->getTimeToAutoDisable() > 0 ); $this->settings->update_setting( self::KEY_ENABLED, $setting, true ); $this->setWpmlSettingForThemeLocalization( $isEnabled ); } /** * @return int number of seconds before auto-disable */ public function getTimeToAutoDisable() { $setting = $this->getSetting( self::KEY_ENABLED, [ 'enabled' => false ] ); if ( isset( $setting['time'] ) ) { $elapsed_time = time() - $setting['time']; $time_to_auto_disable = self::RESET_AUTOLOAD_TIMEOUT - $elapsed_time; if ( $time_to_auto_disable > 0 ) { return $time_to_auto_disable; } } return 0; } /** * @param string $key * @param mixed $default * * @return mixed|null */ private function getSetting( $key, $default = null ) { $setting = $this->settings->get_setting( $key ); return null !== $setting ? $setting : $default; } /** * @return array */ public function getExcludedDomains() { if ( ! $this->excluded_domains ) { $excluded = $this->getSetting( self::KEY_EXCLUDED_DOMAINS, [] ); $this->excluded_domains = wpml_collect( $excluded ) ->reject( [ $this, 'isAdminOrPackageDomain' ] ) ->toArray(); } return $this->excluded_domains; } /** * @param string $domain * * @return bool */ public function isExcludedDomain( $domain ) { return in_array( $domain, $this->getExcludedDomains(), true ); } /** * @return array * @todo: Remove this method, looks like dead code. */ public function get_included_contexts() { return array_values( array_diff( $this->getAllDomains(), $this->getExcludedDomains() ) ); } /** * @return array */ public function getAllDomains() { $sql = " SELECT DISTINCT context FROM {$this->wpdb->prefix}icl_strings "; return wpml_collect( $this->wpdb->get_col( $sql ) ) ->reject( [ $this, 'isAdminOrPackageDomain' ] ) ->merge( wpml_collect( $this->getExcludedDomains() ) ) ->unique() ->toArray(); } /** * @param string $domain * * @return bool */ public function isAdminOrPackageDomain( $domain ) { return 0 === strpos( $domain, \WPML_Admin_Texts::DOMAIN_NAME_PREFIX ) || $this->package_domains->isPackage( $domain ); } /** * @return array */ public function getDomainsAndTheirExcludeStatus() { $contexts = $this->getAllDomains(); $excluded = $this->getExcludedDomains(); $result = array(); foreach ( $contexts as $context ) { $result[ $context ] = in_array( $context, $excluded ); } return $result; } public function saveExcludedContexts() { $nonce = isset( $_POST['nonce'] ) ? $_POST['nonce'] : ''; $is_valid = wp_verify_nonce( $nonce, 'wpml-st-cancel-button' ); if ( $is_valid ) { $is_enabled = [ 'enabled' => false ]; $excluded_contexts = []; if ( isset( $_POST[ self::KEY_EXCLUDED_DOMAINS ] ) && is_array( $_POST[ self::KEY_EXCLUDED_DOMAINS ] ) ) { $excluded_contexts = array_map( 'stripslashes', $_POST[ self::KEY_EXCLUDED_DOMAINS ] ); } $this->settings->update_setting( self::KEY_EXCLUDED_DOMAINS, $excluded_contexts, true ); if ( isset( $_POST[ self::KEY_ENABLED ] ) && 'true' === $_POST[ self::KEY_ENABLED ] ) { $is_enabled = [ 'enabled' => true, 'time' => time(), ]; } $this->settings->update_setting( self::KEY_ENABLED, $is_enabled, true ); $this->setWpmlSettingForThemeLocalization( $is_enabled[ 'enabled' ] ); wp_send_json_success(); } else { wp_send_json_error( __( 'Nonce value is invalid', 'wpml-string-translation' ) ); } } /** @return string */ public function getFeatureEnabledDescription() { return '<span class="icon otgs-ico-warning"></span> ' . __( "Automatic string registration will remain active for %s. Please visit the site's front-end to allow WPML to find strings for translation.", 'wpml-string-translation' ); } /** @return string */ public function getFeatureDisabledDescription() { return __( '* This feature is only intended for sites that are in development. It will significantly slow down the site, but help you find strings that WPML cannot detect in the PHP code.', 'wpml-string-translation' ); } public function getDomainsWithStringsTranslationData() { $excluded = $this->getExcludedDomains(); $domains = wpml_collect( $this->getAllDomains() )->merge( $excluded )->unique()->toArray(); $stats = $this->localization->get_domain_stats( $domains, 'default', false, true ); $result = []; foreach ( $domains as $domain ) { $completed_strings_count = (int) Obj::path( [ $domain, 'complete' ], $stats ); $incomplete_strings_count = (int) Obj::path( [ $domain, 'incomplete' ], $stats ); $result[ $domain ] = [ 'name' => $domain, 'translated_strings_count' => $completed_strings_count, 'total_strings_count' => $incomplete_strings_count + $completed_strings_count, 'is_blocked' => in_array( $domain, $excluded ), ]; } return $result; } private function setWpmlSettingForThemeLocalization( $isEnabled ) { if ( $isEnabled ) { $this->enableWpmlSettingForThemeLocalizationIfNecessary(); } else { $this->disableWpmlSettingForThemeLocalizationIfNecessary(); } } private function enableWpmlSettingForThemeLocalizationIfNecessary() { $previousLoadThemeSetting = (int) apply_filters( 'wpml_get_setting', 0, PreloadThemeMoFile::SETTING_KEY ); if ( $previousLoadThemeSetting === PreloadThemeMoFile::SETTING_DISABLED ) { do_action( 'wpml_set_setting', PreloadThemeMoFile::SETTING_KEY, PreloadThemeMoFile::SETTING_ENABLED_FOR_LOAD_TEXT_DOMAIN, true ); } } private function disableWpmlSettingForThemeLocalizationIfNecessary() { $previousLoadThemeSetting = (int) apply_filters( 'wpml_get_setting', 0, PreloadThemeMoFile::SETTING_KEY ); if ( $previousLoadThemeSetting === PreloadThemeMoFile::SETTING_ENABLED_FOR_LOAD_TEXT_DOMAIN ) { do_action( 'wpml_set_setting', PreloadThemeMoFile::SETTING_KEY, PreloadThemeMoFile::SETTING_DISABLED, true ); } } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.3-4ubuntu2.24 | Генерация страницы: 0 |
proxy
|
phpinfo
|
Настройка