|
<?php |
|
|
|
if ( ! class_exists('SitePress') ) { |
|
return; |
|
} |
|
|
|
/** |
|
* Filter: Replace WPML merge tags with their respective values |
|
* |
|
* @used-by Filter: GravityForms\'gform_replace_merge_tags' |
|
* @uses WPML\SitePress::get_current_language() |
|
* @uses WPML\SitePress::get_default_language() |
|
* @global SitePress $sitepress |
|
* |
|
* @param string $text The current text in which merge tags are being replaced. |
|
* @param array $form The current form. |
|
* @param array $entry The current entry. |
|
* @param boolean $url_encode Whether or not to encode any URLs found in the replaced value. |
|
* @param boolean $esc_html Whether or not to encode HTML found in the replaced value. |
|
* @param boolean $nl2br Whether or not to convert newlines to break tags. |
|
* @param string $format Default "html"; determines how the value should be formatted. |
|
*/ |
|
|
|
function gf_wpml_replace_language_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) |
|
{ |
|
global $sitepress; |
|
|
|
$merge_tag = '{wpml_current_language}'; |
|
|
|
if ( strpos( $text, $merge_tag ) === false || empty( $entry ) || empty( $form ) ) { |
|
return $text; |
|
} |
|
|
|
$text = str_replace( $merge_tag, $sitepress->get_current_language(), $text ); |
|
|
|
$merge_tag = '{wpml_default_language}'; |
|
|
|
if ( strpos( $text, $merge_tag ) === false || empty( $entry ) || empty( $form ) ) { |
|
return $text; |
|
} |
|
|
|
$text = str_replace( $merge_tag, $sitepress->get_default_language(), $text ); |
|
|
|
return $text; |
|
} |
|
|
|
add_filter( 'gform_replace_merge_tags', 'gf_wpml_replace_language_merge_tags', 10, 7 ); |
|
|
|
/** |
|
* Action: Adds a JavaScript filter to add language merge tags using WPML. |
|
* |
|
* Merge Tags: |
|
* - `{wpml_current_language}` |
|
* - `{wpml_default_language}` |
|
* |
|
* @used-by Action: GravityForms\'gform_admin_pre_render' |
|
* |
|
* @param array $form The current form to be filtered. |
|
*/ |
|
|
|
function gf_wpml_filter_merge_tags( $form ) |
|
{ |
|
?> |
|
<script> |
|
/** |
|
* Filter: Adds language merge tags using WPML to the Merge Tag drop-down in the admin. |
|
* |
|
* Ideally integrated through the PHP 'gform_admin_pre_render' action. |
|
* |
|
* Merge Tags: |
|
* - `{wpml_current_language}` |
|
* - `{wpml_default_language}` |
|
* |
|
* @link http://www.gravityhelp.com/documentation/gravity-forms/extending-gravity-forms/hooks/javascript/gform_merge_tags/ |
|
* @used-by Filter: GravityForms\'gform_merge_tags' |
|
* |
|
* @param {array} mergeTags - The current merge tags |
|
* @param {strign} elementId - The name of the current field. When selecting the Merge Tag |
|
* drop down on the Advanced tab of a form field, the value will |
|
* always be "field_default_value". The confirmation and notification |
|
* admin pages will return specific field names so you may target |
|
* adding a merge tag to a specific field. |
|
* @param {string} hideAllFields - Indicates whether to include the "All Submitted Fields" choice |
|
* in the Merge Tag drop down. |
|
* @param {array} excludeFieldTypes - Indicates which field types should be ignored. |
|
* @param {boolean} isPrepop - Indicates whether to include form fields in the Merge Tag drop down. |
|
* @param {string} option - Internal use by Gravity Forms. |
|
* @param {object} gfMergeTagsObj - Reference to the MergeTags object. |
|
*/ |
|
|
|
function gf_wpml_add_merge_tags( mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option, mergeTagsObj ) |
|
{ |
|
mergeTags['other'].tags.push({ tag: '{wpml_current_language}', label: '<?php _e('Current Language', 'upa'); ?>' }); |
|
mergeTags['other'].tags.push({ tag: '{wpml_default_language}', label: '<?php _e('Default Language', 'upa'); ?>' }); |
|
|
|
return mergeTags; |
|
} |
|
|
|
gform.addFilter( 'gform_merge_tags', 'gf_wpml_add_merge_tags' ); |
|
</script> |
|
<?php |
|
|
|
return $form; |
|
} |
|
|
|
add_action( 'gform_admin_pre_render', 'gf_wpml_filter_merge_tags' ); |