Skip to content

Instantly share code, notes, and snippets.

@mcaskill
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcaskill/c0aae410f9515b9053e5 to your computer and use it in GitHub Desktop.
Save mcaskill/c0aae410f9515b9053e5 to your computer and use it in GitHub Desktop.
WordPress \ Polylang : Adds "current" and "default" language merge tags for Gravity Forms
<?php
if ( ! class_exists('Polylang') ) {
return;
}
/**
* Filter: Replace Polylang merge tags with their respective values
*
* @used-by Filter: GravityForms\'gform_replace_merge_tags'
* @uses Polylang\pll_current_language()
* @uses Polylang\pll_default_language()
*
* @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_pll_replace_language_merge_tags( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format )
{
$merge_tag = '{pll_current_language}';
if ( strpos( $text, $merge_tag ) === false || empty( $entry ) || empty( $form ) ) {
return $text;
}
$text = str_replace( $merge_tag, pll_current_language(), $text );
$merge_tag = '{pll_default_language}';
if ( strpos( $text, $merge_tag ) === false || empty( $entry ) || empty( $form ) ) {
return $text;
}
$text = str_replace( $merge_tag, pll_default_language(), $text );
return $text;
}
add_filter( 'gform_replace_merge_tags', 'gf_pll_replace_language_merge_tags', 10, 7 );
/**
* Action: Adds a JavaScript filter to add language merge tags using Polylang.
*
* Merge Tags:
* - `{pll_current_language}`
* - `{pll_default_language}`
*
* @used-by Action: GravityForms\'gform_admin_pre_render'
*
* @param array $form The current form to be filtered.
*/
function gf_pll_filter_merge_tags( $form )
{
?>
<script>
/**
* Filter: Adds language merge tags using Polylang to the Merge Tag drop-down in the admin.
*
* Ideally integrated through the PHP 'gform_admin_pre_render' action.
*
* Merge Tags:
* - `{pll_current_language}`
* - `{pll_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_pll_add_merge_tags( mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option, mergeTagsObj )
{
mergeTags['other'].tags.push({ tag: '{pll_current_language}', label: '<?php _e('Current Language', 'upa'); ?>' });
mergeTags['other'].tags.push({ tag: '{pll_default_language}', label: '<?php _e('Default Language', 'upa'); ?>' });
return mergeTags;
}
gform.addFilter( 'gform_merge_tags', 'gf_pll_add_merge_tags' );
</script>
<?php
return $form;
}
add_action( 'gform_admin_pre_render', 'gf_pll_filter_merge_tags' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment