Skip to content

Commit

Permalink
gpi-enable-wpml-for-custom-messages.php: Added snippet to use WPML …
Browse files Browse the repository at this point in the history
…String Translate for custom GPI messages.
  • Loading branch information
barthc authored Nov 4, 2024
1 parent 355dccb commit b7802b1
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions gp-inventory/gpi-enable-wpml-for-custom-messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Gravity Perks // Limit Submissions // Enable `WPML String Translate` for Custom Insufficient,
* Exhausted and Available Inventory Messages.
* https://gravitywiz.com/documentation/gravity-forms-inventory/
*/
add_filter( 'gform_pre_render', function( $form ) {
if ( ! function_exists( 'icl_register_string' ) || ! function_exists( 'gp_inventory' ) ) {
return $form;
}

$form_id = $form['id'];
foreach ( $form['fields'] as $field ) {
if ( rgar( $field, 'gpiMessageInventoryInsufficient', false ) ) {
$name = "inventory-insufficient-message-{$form_id}-{$field->id}";
icl_register_string( 'gp-inventory', $name, $field['gpiMessageInventoryInsufficient'] );
add_filter( "gpi_inventory_insufficient_message_{$form_id}_{$field->id}", function( $message ) use ( $name ) {
return apply_filters( 'wpml_translate_single_string', $message, 'gp-inventory', $name );
} );
}
if ( rgar( $field, 'gpiMessageInventoryExhausted', false ) ) {
$name = "inventory-exhausted-message-{$form_id}-{$field->id}";
icl_register_string( 'gp-inventory', $name, $field['gpiMessageInventoryExhausted'] );
add_filter( "gpi_inventory_exhausted_message_{$form_id}_{$field->id}", function( $message ) use ( $name ) {
return apply_filters( 'wpml_translate_single_string', $message, 'gp-inventory', $name );
} );
}
if ( rgar( $field, 'gpiMessageInventoryAvailable', false ) ) {
$name = "inventory-available-message-{$form_id}-{$field->id}";
icl_register_string( 'gp-inventory', $name, $field['gpiMessageInventoryAvailable'] );
add_filter( "gpi_inventory_available_message_{$form_id}_{$field->id}", function( $message ) use ( $name ) {
return apply_filters( 'wpml_translate_single_string', $message, 'gp-inventory', $name );
} );
}
}

return $form;
} );

0 comments on commit b7802b1

Please sign in to comment.