TranslationHandler.php
6.89 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace WPML\ST\Shortcode;
use WPML\Collect\Support\Traits\Macroable;
use WPML\FP\Fns;
use WPML\FP\Lst;
use WPML\FP\Obj;
use WPML\FP\Str;
use function WPML\FP\curryN;
use function WPML\FP\pipe;
use function WPML\FP\spreadArgs;
/**
* Class TranslationHandler
*
* @package WPML\ST\Shortcode
*
* @method static callable|mixed appendId(callable ...$getStringRowByItsDomainAndValue, mixed ...$fieldData) - Curried :: (string->string->array)->mixed->mixed
*
* It appends "id" attribute to [wpml-string] shortcode.
*
* $getStringRowByItsDomainAndValue :: string->string->array
*
* @method static callable|mixed registerStringTranslation(callable ...$lens, mixed ...$data, callable ...$getTargetLanguage) - Curried :: callable->mixed->(mixed->string)->mixed
*
* It detects all [wpml-string] shortcodes in $jobData and registers string translations
*
* $getTargetLanguage :: mixed->string
*
* @method static callable|mixed restoreOriginalShortcodes(callable ...$getStringById, callable ...$lens, mixed ...$data) - Curried :: (int->string)->callable->mixed->mixed
*
* It detects all [wpml-string] shortcodes in $jobData and
* - removes "id" attribute
* - replaces translated inner text by its original value
*
* $getStringById :: int->array
*/
class TranslationHandler {
use Macroable;
const SHORTCODE_PATTERN = '/\[wpml-string.*?\[\/wpml-string\]/';
public static function init() {
self::macro(
'appendId',
curryN(
2,
function ( callable $getStringRowByItsDomainAndValue, $fieldData ) {
// $getStringId :: [ domain, value ] → id
$getStringId = spreadArgs( pipe( $getStringRowByItsDomainAndValue, Obj::prop( 'id' ) ) );
// appendStringId :: [ _, domain, value ] → [ _, domain, value, id ]
$appendStringId = self::appendToData( pipe( Lst::takeLast( 2 ), $getStringId ) );
// $getShortcode :: [shortcode, domain, id] -> shortcode
$getShortcode = Lst::nth( 0 );
// $getShortcode :: [shortcode, domain, id] -> id
/** @var callable $getId */
$getId = Lst::last();
// $extractDomain :: string -> string
$extractDomain = self::firstMatchingGroup( '/context="(.*?)"/', 'wpml-shortcode' );
// $forceRegisterShortcodeString :: string -> void
$forceRegisterShortcodeString = pipe( $getShortcode, 'do_shortcode' );
// $newShortcode :: [shortcode, domain, id] -> string
$newShortcode = function ( $data ) use ( $getId, $getShortcode ) {
$pattern = '/\[wpml-string(.*)\]/';
$replace = '[wpml-string id="' . $getId( $data ) . '"${1}]';
return preg_replace( $pattern, $replace, $getShortcode( $data ) );
};
// $updateSingleShortcode :: [shortcode, domain, id] -> [shortcode, newShortcode]
$updateSingleShortcode = Fns::converge( Lst::makePair(), [ $getShortcode, $newShortcode ] );
// $updateFieldData :: string, [shortcode, newShortcode] -> string
$updateFieldData = function ( $fieldData, $shortCodePairs ) {
list( $shortcode, $newShortcode ) = $shortCodePairs;
return str_replace( $shortcode, $newShortcode, $fieldData );
};
return \wpml_collect( Str::matchAll( self::SHORTCODE_PATTERN, $fieldData ) )
->map( self::appendToData( pipe( $getShortcode, $extractDomain ) ) )
->map( self::appendToData( pipe( $getShortcode, self::extractInnerText() ) ) )
->each( $forceRegisterShortcodeString )
->map( $appendStringId )
->filter( $getId )
->map( $updateSingleShortcode )
->reduce( $updateFieldData, $fieldData );
}
)
);
self::macro(
'registerStringTranslation',
curryN(
3,
function ( callable $lens, $data, callable $getTargetLanguage ) {
$targetLanguage = $getTargetLanguage( $data );
if ( ! $targetLanguage ) {
return $data;
}
$registerStringTranslation = curryN( 4, 'icl_add_string_translation' );
// $registerStringTranslation :: stringId, translationValue -> translationId
$registerStringTranslation = $registerStringTranslation(
Fns::__,
$targetLanguage,
Fns::__,
ICL_STRING_TRANSLATION_COMPLETE
);
// $getStringIdAndTranslations :: [shortcode, id, translation] -> [id, translation]
$getStringIdAndTranslations = Lst::drop( 1 );
// $registerTranslationOfSingleString :: [shortcode, id, translation] -> void
$registerTranslationOfSingleString = pipe(
$getStringIdAndTranslations,
spreadArgs( $registerStringTranslation )
);
// $registerStringsFromFieldData :: string -> void
$registerStringsFromFieldData = pipe(
self::findShortcodesInJobData(),
Fns::each( $registerTranslationOfSingleString )
);
Fns::each( $registerStringsFromFieldData, Obj::view( $lens, $data ) );
return $data;
}
)
);
self::macro(
'restoreOriginalShortcodes',
curryN(
3,
function ( callable $getStringById, callable $lens, $data ) {
// $getOriginalStringValue :: int -> string
$getOriginalStringValue = pipe( $getStringById, Obj::prop( 'value' ) );
// $restoreSingleShortcode :: string, [string, id] -> string
$restoreSingleShortcode = function ( $fieldData, $shortcodeMatches ) use ( $getOriginalStringValue ) {
list( , $stringId ) = $shortcodeMatches;
$pattern = '/\[wpml-string id="' . $stringId . '"(.*?)\](.*?)\[\/wpml-string\]/';
$replacement = '[wpml-string${1}]' . $getOriginalStringValue( $stringId ) . '[/wpml-string]';
return preg_replace( $pattern, $replacement, $fieldData );
};
// $updateFieldData :: string -> string
$updateFieldData = Fns::map(
Fns::converge(
Fns::reduce( $restoreSingleShortcode ),
[
Fns::identity(),
self::findShortcodesInJobData(),
]
)
);
return Obj::over( $lens, $updateFieldData, $data );
}
)
);
}
// findShortcodesInJobData :: void → ( string → [shortcode, id, translation] )
private static function findShortcodesInJobData() {
return function ( $str ) {
/** @var callable $getId */
$getId = Lst::nth( 1 );
$extractId = self::firstMatchingGroup( '/id="(.*?)"/' );
return \wpml_collect( Str::matchAll( self::SHORTCODE_PATTERN, $str ) )
->map( self::appendToData( pipe( Lst::nth( 0 ), $extractId ) ) )
->map( self::appendToData( pipe( Lst::nth( 0 ), self::extractInnerText() ) ) )
->filter( $getId );
};
}
// appendToData :: callable -> ( array -> array )
private static function appendToData( callable $fn ) {
return Fns::converge( Lst::append(), [ $fn, Fns::identity() ] );
}
// firstMatchingGroup :: string, string|null -> ( string -> string )
private static function firstMatchingGroup( $pattern, $fallback = null ) {
return function ( $str ) use ( $pattern, $fallback ) {
return Lst::nth( 1, Str::match( $pattern, $str ) ) ?: $fallback;
};
}
// extractInnerText :: void -> ( string -> string )
private static function extractInnerText() {
return self::firstMatchingGroup( '/\](.*)\[/' );
}
}
TranslationHandler::init();