Skip to content

Instantly share code, notes, and snippets.

@natac13
Last active January 17, 2022 01:41
Show Gist options
  • Save natac13/6df08fda1c570174d4bc9cfbe2c13215 to your computer and use it in GitHub Desktop.
Save natac13/6df08fda1c570174d4bc9cfbe2c13215 to your computer and use it in GitHub Desktop.
<?php
namespace Harness\WPGraphQL\Types;
use Harness\GravityForms\Addon\Translations\GFTranslationsAddOn;
use WPGraphQLGravityForms\Types\Form\Form;
use Harness\Interfaces\Hookable;
/**
* Expose Gravity Forms form URL slugs in the GraphQL API.
*/
class FormTranslations implements Hookable {
const TRANSLATION_TYPE = 'FormTranslations';
const CARD_DISPLAY_TRANSLATIONS = 'CardDisplayTranslations';
public function register_hooks() {
add_action( get_graphql_register_action(), [ $this, 'register_type' ] );
add_action( get_graphql_register_action(), [ $this, 'register_field' ] );
}
public function register_type() {
register_graphql_object_type(
self::TRANSLATION_TYPE,
[
'description' => __( 'Gravity Forms translation settings', 'harness' ),
'fields' => [
'slug' => [
'type' => 'String',
'description' => __( 'Langauge slug used for the url of the form', 'harness' ),
],
'name' => [
'type' => 'String',
'description' => __( 'Language name', 'harness' ),
],
'active' => [
'type' => 'Boolean',
'description' => __( 'Is this an active translation to make form pages for.', 'harness' ),
],
'title' => [
'type' => 'String',
'description' => __(
'Form Title
',
'harness'
),
],
'description' => [
'type' => 'String',
'description' => __( 'Form Description', 'harness' ),
],
'cardDisplay' => [
'type' => self::CARD_DISPLAY_TRANSLATIONS,
'description' => __( 'Entries List page Card Display Detail Labels', 'harness' ),
],
],
]
);
register_graphql_object_type(
self::CARD_DISPLAY_TRANSLATIONS,
[
'description' => __( 'Card Display Detail Label Translations', 'harness' ),
'fields' => [
'detail1' => [
'type' => 'String',
'description' => __( 'Detail 1 Translated Label', 'harness' ),
],
'detail2' => [
'type' => 'String',
'description' => __( 'Detail 2 Translated Label', 'harness' ),
],
'detail3' => [
'type' => 'String',
'description' => __( 'Detail 3 Translated Label', 'harness' ),
],
],
]
);
}
public function register_field() {
register_graphql_fields(
Form::$type,
[
'translations' => [
'type' => [ 'list_of' => self::TRANSLATION_TYPE ],
'description' => __( 'Form Level Translated values', 'harness' ),
'resolve' => function ( array $form ) {
$form_translations = $form[ GFTranslationsAddOn::KEY ];
$value = [];
foreach ( $form_translations as $key => $translation ) {
if ( 'en_us' !== $key ) {
$value[] = [
'slug' => strtolower( $key ),
'name' => $translation['name'],
'active' => $translation['active'] ?? false,
'title' => $translation['title'],
'description' => $translation['description'],
'submitButtonText' => $translation['submitButtonText'],
'cardDisplay' => [
'detail1' => $translation['cardDisplay']['detail1'],
'detail2' => $translation['cardDisplay']['detail2'],
'detail3' => $translation['cardDisplay']['detail3'],
],
];
}
}
return $value;
},
],
]
);
}
}
<?php
namespace Harness\GravityForms\Addon\Translations;
use GFAddOn;
use GFForms;
use Harness\GravityFormsDynamicPopulation\DynamicPopulatorUtilities;
use Harness\Interfaces\Hookable;
use Harness\WPGraphQL\Mutations\MutationTrait;
use Polylang;
GFForms::include_addon_framework();
define( 'GF_TRANSLATIONS_ADDON_VERSION', '1.0' );
class GFTranslationsAddOn extends GFAddOn implements Hookable {
const KEY = 'harness-gf-translations';
protected $_version = GF_TRANSLATIONS_ADDON_VERSION;
protected $_min_gravityforms_version = '1.9';
protected $_slug = self::KEY;
protected $_path = 'Translations/translations.php';
protected $_full_path = __FILE__;
protected $_title = 'Harness Translations Addon for Gravity Forms';
protected $_short_title = 'Translations';
private static $_instance = null;
public function register_hooks() {
$this->init();
}
/**
* Get an instance of this class.
*
* @return GFTranlationsAddOn
*/
public static function get_instance() {
if ( self::$_instance == null ) {
self::$_instance = new GFTranslationsAddOn();
}
return self::$_instance;
}
/**
* Handles hooks and loading of language files.
*/
public function init() {
parent::init();
add_action( 'gform_field_settings_tabs', [ $this, 'add_tab' ], 10, 2 );
add_action( 'gform_field_settings_tab_content_gravity-forms-translations', [ $this, 'add_tab_settings' ], 10, 2 );
add_action( 'gform_editor_js', [ $this, 'save_tab_values' ] );
}
public static function add_tab( array $tabs, array $form ) {
$tabs[] = [
'id' => 'gravity-forms-translations',
'title' => __( 'Translations', 'harness' ),
];
return $tabs;
}
public function add_tab_settings( array $form, string $tab_id ) {
$languages = self::get_all_languages();
foreach ( $languages as $key => $language ) {
if ( 'en_us' !== $key ) {
$label_key = $language['slug'] . '-label';
?>
<h3><?php echo $language['name']; ?></h3>
<li>
<label>
<?php echo 'Field Label'; ?>
</label>
<input type="text" id="<?php echo $label_key; ?>" onchange="SetFieldProperty('<?php echo esc_js( $label_key ); ?>', jQuery(this).val())" />
</li>
<?php
}
}
}
public function save_tab_values() {
$languages = self::get_all_languages();
?>
<script type='text/javascript'>
console.log({fieldSettings});
jQuery(document).on('gform_load_field_settings', function(event, field, form) {
<?php
foreach ( $languages as $key => $language ) {
$label_key = $language['slug'] . '-label';
?>
console.log({field, key: '<?php echo $label_key; ?>', value: field['<?php echo $label_key; ?>'] })
jQuery('#<?php echo $label_key; ?>').val(field['<?php echo $label_key; ?>']);
<?php
}
?>
});
</script>
<?php
}
public function get_menu_icon() {
return 'fa-language';
}
static public function get_all_languages() {
return array_filter(
pll_the_languages(
[
'raw' => 1,
'hide_if_empty' => 0,
]
),
function ( $language ) {
return 'en_us' !== $language['slug'];
}
);
}
/**
* Given a language code and name this will create the needed fields on a GF form that are needed for translations
*
* @param string $language_code
* @param string $language_name
* @return array
*/
public function make_translation_fields( string $language_code, string $language_name ) {
return
[
'title' => esc_html__( ucwords( $language_name ) . ' Translations', 'harness' ),
'fields' => [
[
'label' => esc_html__( 'Active Translation?', 'harness' ),
'type' => 'checkbox',
'name' => $language_code . '[active]',
'tooltip' => esc_html__( 'Ability to turn on and off this translation for this form.', 'harness' ),
'choices' => [
[
'name' => $language_code . '[active]',
'value' => true,
],
],
],
[
'label' => esc_html__( 'Form Title', 'harness' ),
'type' => 'text',
'name' => $language_code . '[title]',
'tooltip' => esc_html__( 'Translated form title', 'harness' ),
],
[
'label' => esc_html__( 'Form Description', 'harness' ),
'type' => 'textarea',
'name' => $language_code . '[description]',
'tooltip' => esc_html__( 'Translated form description', 'harness' ),
],
[
'label' => esc_html__( 'Submit Button Text', 'harness' ),
'type' => 'text',
'name' => $language_code . '[submitButtonText]',
'tooltip' => esc_html__( 'Translated form submit button text', 'harness' ),
],
[
'label' => esc_html__( 'Card Display - Detail 1 Label', 'harness' ),
'type' => 'text',
'name' => $language_code . '[cardDisplay][detail1]',
'tooltip' => esc_html__( 'Translated card display detail 1 label', 'harness' ),
],
[
'label' => esc_html__( 'Card Display - Detail 2 Label', 'harness' ),
'type' => 'text',
'name' => $language_code . '[cardDisplay][detail2]',
'tooltip' => esc_html__( 'Translated card display detail 2 label', 'harness' ),
],
[
'label' => esc_html__( 'Card Display - Detail 3 Label', 'harness' ),
'type' => 'text',
'name' => $language_code . '[cardDisplay][detail3]',
'tooltip' => esc_html__( 'Translated card display detail 3 label', 'harness' ),
],
],
];
}
public function form_settings_fields( $form ) {
$languages = self::get_all_languages();
$fp = fopen( plugin_dir_path( __FILE__ ) . 'filename.json', 'w' );
fwrite( $fp, json_encode( $languages ) );
fclose( $fp );
$sections = [];
foreach ( $languages as $key => $language ) {
if ( 'en_us' !== $key ) {
$sections[] = $this->make_translation_fields( $key, $language['name'] );
}
}
return $sections;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment