This repository has been archived by the owner on Oct 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
class_GF_PLL.php
105 lines (72 loc) · 1.98 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
96
97
98
99
100
101
102
103
104
105
<?php
if(!class_exists('GF_PLL')) :
class GF_PLL {
private $whitelist;
private $blacklist;
private $registered_strings;
private $form;
public function __construct() {
$this->whitelist = array(
'title',
'description',
'text',
'content',
'message',
'defaultValue',
'errorMessage',
'placeholder',
'label',
'customLabel',
'value',
'subject'
);
$this->blacklist = array();
$this->registered_strings = array();
}
private function is_translatable($key, $value) {
return
$key &&
in_array($key, $this->whitelist) &&
is_string($value) &&
!in_array($value, $this->registered_strings);
}
private function iterate_form(&$value, $key, $callback = null) {
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->iterate_form($new_value, $new_key, $callback);
}
}
} else {
if($this->is_translatable($key, $value)) {
$callback($value, $key);
}
}
}
public function register_strings() {
if(!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->iterate_form($form, function($value, $key) {
$name = ''; // todo: suitable naming
$group = "Form #{$this->form['id']}: {$this->form['title']}";
pll_register_string($name, $value, $group);
$this->registered_strings[] = $value;
});
}
}
public function translate_strings($form) {
if(function_exists('pll__')) {
$this->iterate_form($form, function(&$value, $key) {
$value = pll__($value);
});
}
return $form;
}
}
endif;