Advertisement
sarahn

fixed:gravity_forms_multilingual.class.php

Oct 22nd, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3.  
  4. class Gravity_forms_multilingual{
  5.  
  6.  
  7. function __construct($ext = false){
  8. add_action('init', array($this,'init'));
  9. }
  10.  
  11. function __destruct(){
  12. return;
  13. }
  14.  
  15. function init(){
  16. add_filter('WPML_get_translatable_types', array($this,'get_translatable_types'));
  17. add_filter('WPML_get_translatable_items', array($this,'get_translatable_items'), 10, 3);
  18. add_filter('WPML_get_translatable_item', array($this,'get_translatable_item'), 10, 2);
  19. add_filter('WPML_get_link', array($this,'get_link'), 10, 4);
  20.  
  21. add_filter('gform_pre_render', array($this, 'gform_pre_render'));
  22. add_filter('gform_pre_submission_filter', array($this, 'gform_pre_render'));
  23. add_filter('gform_confirmation', array($this, 'gform_confirmation'), 10, 4);
  24.  
  25. }
  26.  
  27.  
  28. function get_translatable_types($types) {
  29. // Tell WPML that we want gravity forms translated
  30. $types['gravity_form'] = 'Gravity form';
  31. return $types;
  32. }
  33.  
  34. function _get_form_strings($form_id) {
  35.  
  36. $form = RGFormsModel::get_form_meta($form_id, true);
  37. $form = RGFormsModel::add_default_properties($form);
  38.  
  39. $string_data = array();
  40.  
  41. $form_keys = array('title', 'description', 'limitEntriesMessage',
  42. 'scheduleMessage',
  43. 'postTitleTemplate',
  44. 'postContentTemplate',
  45. 'confirmation-message',
  46. 'autoResponder-subject',
  47. 'autoResponder-message',
  48. 'button-text'
  49. );
  50.  
  51. foreach ($form_keys as $key) {
  52. $parts = explode('-', $key);
  53. if (sizeof($parts) == 1) {
  54. if (isset($form[$key]) && $form[$key] != '') {
  55. $string_data[$key] = $form[$key];
  56. }
  57. } else {
  58. if (isset($form[$parts[0]][$parts[1]]) && $form[$parts[0]][$parts[1]] != '') {
  59. $string_data[$key] = $form[$parts[0]][$parts[1]];
  60. }
  61. }
  62. }
  63.  
  64.  
  65. ///- Paging Page Names - $form["pagination"]["pages"][i]
  66.  
  67. $keys = array('label', 'description', 'defaultValue', 'errorMessage');
  68.  
  69. foreach ($form['fields'] as $id => $field) {
  70.  
  71. foreach ($keys as $key) {
  72. if (isset($field[$key]) && $field[$key] != '') {
  73. $string_data['field-' . $field['id'] . '-' . $key] = $field[$key];
  74. }
  75. }
  76.  
  77. switch ($field['type']) {
  78. case 'text':
  79. case 'textarea':
  80. case 'email':
  81. case 'number':
  82. case 'section':
  83. break;
  84.  
  85. case 'html':
  86. $string_data['field-' . $field['id'] . '-content'] = $field['content'];
  87. break;
  88.  
  89. case 'page':
  90. $string_data['field-' . $field['id'] . '-nextButton'] = $field['nextButton']['text'];
  91. $string_data['field-' . $field['id'] . '-previousButton'] = $field['previousButton']['text'];
  92. break;
  93.  
  94. case 'select':
  95. case 'checkbox':
  96. case 'radio':
  97. case 'product':
  98. if(isset($field['choices']) && is_array($field['choices']))
  99. foreach ($field['choices'] as $index => $choice) {
  100. $string_data['field-' . $field['id'] . '-choice-' . $choice['value']] = $choice['text'];
  101. }
  102. break;
  103.  
  104. case 'post_custom_field':
  105. $string_data['field-' . $field['id'] . '-customFieldTemplate'] = $field["customFieldTemplate"];
  106. break;
  107.  
  108. }
  109.  
  110. }
  111.  
  112. return $string_data;
  113.  
  114. }
  115.  
  116. function gform_pre_render($form) {
  117.  
  118. if (function_exists('icl_t')) {
  119.  
  120. $form_keys = array('title', 'description', 'limitEntriesMessage',
  121. 'scheduleMessage',
  122. 'postTitleTemplate',
  123. 'postContentTemplate',
  124. 'confirmation-message',
  125. 'autoResponder-subject',
  126. 'autoResponder-message',
  127. 'button-text'
  128. );
  129.  
  130. foreach ($form_keys as $key) {
  131. $parts = explode('-', $key);
  132. if (sizeof($parts) == 1) {
  133. if (isset($form[$key]) && $form[$key] != '') {
  134. $form[$key] = icl_t('gravity_form', $form['id'] . '_' . $key, $form[$key]);
  135. }
  136. } else {
  137. if (isset($form[$parts[0]][$parts[1]]) && $form[$parts[0]][$parts[1]] != '') {
  138. $form[$parts[0]][$parts[1]] = icl_t('gravity_form', $form['id'] . '_' . $key, $form[$parts[0]][$parts[1]]);
  139. }
  140. }
  141. }
  142.  
  143.  
  144. $keys = array('label', 'description', 'defaultValue', 'errorMessage');
  145.  
  146. foreach ($form['fields'] as $id => $field) {
  147.  
  148. foreach ($keys as $key) {
  149. if (isset($field[$key]) && $field[$key] != '') {
  150. $form['fields'][$id][$key] = icl_t('gravity_form', $form['id'] . '_field-' . $field['id'] . '-' . $key, $field[$key]);
  151. }
  152. }
  153.  
  154.  
  155.  
  156. switch ($field['type']) {
  157. case 'text':
  158. case 'textarea':
  159. case 'email':
  160. case 'number':
  161. case 'section':
  162. break;
  163.  
  164. case 'html':
  165. $form['fields'][$id]['content'] = icl_t('gravity_form', $form['id'] . '_field-' . $field['id'] . '-content', $field['content']);
  166. break;
  167.  
  168. case 'page':
  169. $form['fields'][$id]['nextButton']['text'] = icl_t('gravity_form', $form['id'] . '_field-' . $field['id'] . '-nextButton', $field['nextButton']['text']);
  170. $form['fields'][$id]['previousButton']['text'] = icl_t('gravity_form', $form['id'] . '_field-' . $field['id'] . '-previousButton', $field['previousButton']['text']);
  171. break;
  172.  
  173. case 'select':
  174. case 'checkbox':
  175. case 'radio':
  176. case 'product':
  177. if (is_array($field['choices'])) {
  178. foreach ($field['choices'] as $index => $choice) {
  179. $string_name = substr($form['id'] . '_field-' . $field['id'] . '-choice-' . $choice['value'], 0, 160); // limit to 160 chars
  180. $form['fields'][$id]['choices'][$index]['text'] = icl_t('gravity_form', $string_name, $choice['text']);
  181. }
  182. }
  183. break;
  184.  
  185. }
  186.  
  187. }
  188. }
  189.  
  190. return $form;
  191. }
  192.  
  193. function gform_confirmation($confirmation, $form, $lead, $ajax){
  194.  
  195. if (function_exists('icl_t')) {
  196. if (isset($form['confirmation']['message']) && $form['confirmation']['message'] != '') {
  197. if (is_string($confirmation)) {
  198. $original_message = $form['confirmation']['message'];
  199. $translation = icl_t('gravity_form', $form['id'] . '_confirmation-message', $original_message);
  200.  
  201. if ($translation != $original_message) {
  202. $confirmation = str_replace($original_message, $translation, $confirmation);
  203. }
  204. }
  205. } else if(!empty($form["confirmation"]["pageId"])){
  206. $url = get_permalink(icl_object_id($form["confirmation"]["pageId"],'page',true));
  207. $confirmation = array("redirect" => $url);
  208. }
  209. }
  210. return $confirmation;
  211. }
  212.  
  213. function get_translatable_items($items, $type, $filter) {
  214.  
  215. if (function_exists('icl_st_is_registered_string')) {
  216. // Only return items if string translation is available.
  217.  
  218. global $sitepress, $wpdb;
  219.  
  220. if ($type == 'gravity_form') {
  221.  
  222. $default_lang = $sitepress->get_default_language();
  223. $languages = $sitepress->get_active_languages();
  224.  
  225. global $wpdb;
  226. $g_forms = $wpdb->get_results($wpdb->prepare("
  227. SELECT *
  228. FROM {$wpdb->prefix}rg_form
  229. "));
  230. foreach($g_forms as $k=>$v){
  231. $new_item = new stdClass();
  232.  
  233. $new_item->external_type = true;
  234. $new_item->type = 'gravity_form';
  235. $new_item->id = $v->id;
  236. $new_item->post_type = 'gravity_form';
  237. $new_item->post_id = 'external_' . $new_item->post_type . '_' . $v->id;
  238. $new_item->post_date = $v->date_created;
  239. $new_item->post_status = $v->is_active ? __('Active', 'gravity-forms-ml') : __('Inactive', 'gravity-forms-ml');
  240. $new_item->post_title = $v->title;
  241. $new_item->is_translation = false;
  242.  
  243. $new_item->string_data = $this->_get_form_strings($v->id);
  244.  
  245. // add to the translation table if required
  246. $post_trid = $sitepress->get_element_trid($new_item->id, 'post_' . $new_item->post_type);
  247. if (!$post_trid) {
  248. $sitepress->set_element_language_details($new_item->id, 'post_' . $new_item->post_type, false, $default_lang, null, false);
  249. }
  250.  
  251. // register the strings with WPML
  252.  
  253. if (function_exists('icl_st_is_registered_string')) {
  254. foreach ($new_item->string_data as $key => $value) {
  255. if (!icl_st_is_registered_string('gravity_form', $new_item->id . '_' . $key)) {
  256. icl_register_string('gravity_form', $new_item->id . '_' . $key, $value);
  257. }
  258. }
  259. }
  260.  
  261. $post_trid = $sitepress->get_element_trid($new_item->id, 'post_' . $new_item->post_type);
  262. $post_translations = $sitepress->get_element_translations($post_trid, 'post_' . $new_item->post_type);
  263.  
  264. global $iclTranslationManagement;
  265.  
  266. $md5 = $iclTranslationManagement->post_md5($new_item);
  267.  
  268. foreach ($post_translations as $lang => $translation) {
  269. $res = $wpdb->get_row("SELECT status, needs_update, md5 FROM {$wpdb->prefix}icl_translation_status WHERE translation_id={$translation->translation_id}");
  270. if ($res) {
  271. if (!$res->needs_update) {
  272. // see if the md5 has changed.
  273. if ($md5 != $res->md5) {
  274. $res->needs_update = 1;
  275. $wpdb->update($wpdb->prefix.'icl_translation_status', array('needs_update'=>1), array('translation_id'=>$translation->translation_id));
  276. }
  277. }
  278. $_suffix = str_replace('-','_',$lang);
  279. $index = 'status_' . $_suffix;
  280. $new_item->$index = $res->status;
  281. $index = 'needs_update_' . $_suffix;
  282. $new_item->$index = $res->needs_update;
  283. }
  284. }
  285.  
  286. $items[] = $new_item;
  287.  
  288. }
  289.  
  290. }
  291. }
  292. return $items;
  293. }
  294.  
  295. function get_translatable_item($item, $id) {
  296. if ($item == null) {
  297. $parts = explode('_', $id);
  298. if ($parts[0] == 'external') {
  299. $id = array_pop($parts);
  300.  
  301. unset($parts[0]);
  302.  
  303. $type = implode('_', $parts);
  304.  
  305. if ($type == 'gravity_form') {
  306. // this is ours.
  307.  
  308. global $wpdb;
  309. $g_form = $wpdb->get_row($wpdb->prepare("
  310. SELECT *
  311. FROM {$wpdb->prefix}rg_form
  312. WHERE id = %d
  313. ", (int)$id));
  314.  
  315. $item = new stdClass();
  316.  
  317. $item->external_type = true;
  318. $item->type = 'gravity_form';
  319. $item->id = $g_form->id;
  320. $item->ID = $g_form->id;
  321. $item->post_type = 'gravity_form';
  322. $item->post_id = 'external_' . $item->post_type . '_' . $item->id;
  323. $item->post_date = $g_form->modified;
  324. $item->post_status = $g_form->is_active ? __('Active', 'gravity-forms-ml') : __('Inactive', 'gravity-forms-ml');
  325. $item->post_title = $g_form->title;
  326. $item->is_translation = false;
  327.  
  328. $item->string_data = $this->_get_form_strings($item->id);
  329.  
  330. }
  331. }
  332. }
  333.  
  334. return $item;
  335.  
  336. }
  337.  
  338. function get_link($item, $id, $anchor, $hide_empty) {
  339. if ($item == "") {
  340. $parts = explode('_', $id);
  341. if ($parts[0] == 'external') {
  342. $id = array_pop($parts);
  343.  
  344. unset($parts[0]);
  345.  
  346. $type = implode('_', $parts);
  347.  
  348. if ($type == 'gravity_form') {
  349. // this is ours.
  350.  
  351. if (false === $anchor) {
  352. global $wpdb;
  353. $g_form = $wpdb->get_row($wpdb->prepare("
  354. SELECT *
  355. FROM {$wpdb->prefix}rg_form
  356. WHERE id = %d
  357. ", (int)$id));
  358. $anchor = $g_form->title;
  359. }
  360.  
  361. $item = sprintf('<a href="%s">%s</a>', 'admin.php?page=gf_edit_forms&id=' . $id, $anchor);
  362. }
  363. }
  364. }
  365. return $item;
  366. }
  367.  
  368.  
  369. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement