forked from siebsie23/gravity-forms-polylang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class_GF_PLL.php
95 lines (83 loc) · 2.93 KB
/
class_GF_PLL.php
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
<?php
if (!class_exists('GravityFormsPolylang')) :
class GravityFormsPolylang
{
private array $whitelist;
private array $blacklist;
private array $registered_strings;
private array $form;
public function __construct()
{
$this->whitelist = array(
'title',
'description',
'text',
'content',
'defaultValue',
'errorMessage',
'placeholder',
'label',
'customLabel',
'value',
'subject',
'checkboxLabel',
'descriptionPlaceholder'
);
$this->blacklist = array();
$this->registered_strings = array();
}
private function isTranslatable($key, $value): bool
{
return
$key &&
in_array($key, $this->whitelist) &&
is_string($value) &&
!in_array($value, $this->registered_strings);
}
private function iterateForm(&$value, $key, $callback = null): void
{
if (!$callback && is_callable($key)) {
$callback = $key;
}
if (is_array($value) || is_object($value)) {
foreach ($value as $new_key => &$new_value) {
if (!(in_array($new_key, $this->blacklist) && !is_numeric($new_key))) {
$this->iterateForm($new_value, $new_key, $callback);
}
}
} else {
if ($this->isTranslatable($key, $value)) {
$callback($value, $key);
}
}
}
public function registerStrings(): void
{
$page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : '';
if (!str_starts_with($page, 'mlang') || !class_exists('GFAPI') || !function_exists('pll_register_string')) {
return;
}
$forms = GFAPI::get_forms();
foreach ($forms as $form) {
$this->form = $form;
$this->registered_strings = array();
$this->iterateForm($form, function ($value, $key) {
$name = 'gfpll'; // todo: suitable naming
$group = "Form #{$this->form['id']}: {$this->form['title']}";
$multiline = ( 50 < mb_strlen($value) );
pll_register_string($name, $value, $group, $multiline);
$this->registered_strings[] = $value;
});
}
}
public function translateStrings($form)
{
if (function_exists('pll__')) {
$this->iterateForm($form, function (&$value, $key) {
$value = pll__($value);
});
}
return $form;
}
}
endif;