This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathqtranslate-support-for-gravityforms.php
155 lines (134 loc) · 5.44 KB
/
qtranslate-support-for-gravityforms.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
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
<?php
/*
Plugin Name: qTranslate support for GravityForms
Plugin URI: https://github.com/mweimerskirch/wordpress-qtranslate-support-for-gravityforms
Description: Makes qTranslate work with GravityForms
Version: 1.1.2
Author: Michel Weimerskirch
Author URI: http://michel.weimerskirch.net
License: MIT
*/
class qTranslateSupportForGravityforms
{
public function __construct()
{
add_filter('gform_pre_render', array($this, 'gform_pre_render'));
add_filter('gform_pre_submission_filter', array($this, 'gform_pre_render'));
add_filter('gform_polls_form_pre_results', array($this, 'gform_pre_render'));
add_filter('gform_form_tag', array($this, 'gform_form_tag'));
add_filter("gform_confirmation", array($this, "gform_confirmation"), 10, 4);
add_filter("gform_pre_send_email", array($this, "gform_pre_send_email"));
}
private function isEnabled()
{
return (function_exists('qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage') || function_exists('qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage'));
}
private function translate($text)
{
if (function_exists('qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage')) {
return qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage($text);
} else {
return qtrans_useCurrentLanguageIfNotFoundUseDefaultLanguage($text);
}
}
private function convertURL($url, $lang)
{
if (function_exists('qtranxf_convertURL')) {
return qtranxf_convertURL($url, $lang);
} else {
return qtrans_convertURL($url, $lang);
}
}
public function gform_pre_render($form)
{
if (!$this->isEnabled()) return $form;
if (isset($form['button']['text'])) {
$form['button']['text'] = $this->translate($form['button']['text']);
}
if (isset($form['title'])) {
$form['title'] = $this->translate($form['title']);
}
if (isset($form['description'])) {
$form['description'] = $this->translate($form['description']);
}
if (isset($form['confirmation']['message'])) {
$form['confirmation']['message'] = $this->translate($form['confirmation']['message']);
}
if (isset($form['fields'])) {
foreach ($form['fields'] as $id => $field) {
$form['fields'][$id]->label = $this->translate($form['fields'][$id]->label);
$form['fields'][$id]->placeholder = $this->translate($form['fields'][$id]->placeholder);
$form['fields'][$id]->content = $this->translate($form['fields'][$id]->content);
$form['fields'][$id]->description = $this->translate($form['fields'][$id]->description);
$form['fields'][$id]->defaultValue = $this->translate($form['fields'][$id]->defaultValue);
$form['fields'][$id]->errorMessage = $this->translate($form['fields'][$id]->errorMessage);
$form['fields'][$id]->validation_message = $this->translate($form['fields'][$id]->validation_message);
$form['fields'][$id]->choices = $this->translate($form['fields'][$id]->choices);
if (isset($form['fields'][$id]->conditionalLogic['rules'])) {
foreach ($form['fields'][$id]->conditionalLogic['rules'] as $value => $key) {
foreach ($key as $value2 => $key2) {
$form['fields'][$id]->conditionalLogic['rules'][$value][$value2] = $this->translate($key2);
}
}
}
// Translate sub-labels
if (isset($form['fields'][$id]->inputs) && $form['fields'][$id]->inputs) {
foreach ($form['fields'][$id]->inputs as $input_id => $input) {
if(isset($input['customLabel'])) {
$form['fields'][$id]->inputs[$input_id]['customLabel'] = $this->translate($input['customLabel']);
}
if(isset($input['placeholder'])) {
$form['fields'][$id]->inputs[$input_id]['placeholder'] = $this->translate($input['placeholder']);
}
}
}
// Support for the poll add-on
if (isset($form['fields'][$id]->choices) && $form['fields'][$id]->choices) {
foreach ($form['fields'][$id]->choices as $value => $key) {
$form['fields'][$id]['choices'][$value]['text'] = $this->translate($key['text']);
}
}
if (isset($form['fields'][$id]->nextButton) && $form['fields'][$id]->nextButton) {
$form['fields'][$id]->nextButton['text'] = $this->translate($form['fields'][$id]->nextButton['text']);
}
if (isset($form['fields'][$id]->previousButton) && $form['fields'][$id]->previousButton) {
$form['fields'][$id]->previousButton['text'] = $this->translate($form['fields'][$id]->previousButton['text']);
}
}
}
if (isset($form['lastPageButton'])) {
$form['lastPageButton'] = $this->translate($form['lastPageButton']);
}
if (isset($form['pagination'])) {
foreach ($form['pagination']['pages'] as $id => $title) {
$form['pagination']['pages'][$id] = $this->translate($form['pagination']['pages'][$id]);
}
}
return $form;
}
public function gform_form_action_attribute($matches)
{
global $q_config;
return 'action="' . $this->convertURL($matches[1], $q_config['language']) . '"';
}
public function gform_form_tag($tag)
{
if (!$this->isEnabled()) return $tag;
$tag = preg_replace_callback("|action='([^']+)'|", array(&$this, 'gform_form_action_attribute'), $tag);
return $tag;
}
public function gform_confirmation($confirmation, $form, $lead, $ajax)
{
if (!$this->isEnabled()) return $confirmation;
$confirmation = $this->translate($confirmation);
return $confirmation;
}
public function gform_pre_send_email($email)
{
if (!$this->isEnabled()) return $email;
$email["message"] = $this->translate($email["message"]);
$email["subject"] = $this->translate($email["subject"]);
return $email;
}
}
new qTranslateSupportForGravityforms();