Hooks.php
4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?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 );
}
}