Skip to content

Commit

Permalink
added support for registration field label translation with WPML
Browse files Browse the repository at this point in the history
  • Loading branch information
itthinx committed Apr 15, 2020
1 parent 66d9cc7 commit 6a3f97a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
3 changes: 3 additions & 0 deletions affiliates.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
if ( !defined( 'AFFILIATES_CORE_URL' ) ) {
define( 'AFFILIATES_CORE_URL', WP_PLUGIN_URL . '/affiliates' );
}
if ( !defined( 'AFFILIATES_WPML' ) ) {
define( 'AFFILIATES_WPML', true );
}
require_once AFFILIATES_CORE_LIB . '/constants.php';
require_once AFFILIATES_CORE_LIB . '/wp-init.php';
}
2 changes: 2 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* WordPress 5.4 compatible.
* Fixed a notice related to the deprecated contextual_help action.
* Added the affiliates_admin_help_show_screen filter.
* Added support for registration field label translation with WPML, requires WPML >= 3.2 and WPML String Translation.
* Added the AFFILIATES_WPML constant that can be used to enable/disable support for translation with WPML (enabled by default).

= 4.5.0 =
* WordPress 5.3 compatibility retested.
Expand Down
42 changes: 41 additions & 1 deletion lib/core/class-affiliates-registration.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ public static function init() {

// delete affiliate when user is deleted
add_action( 'deleted_user', array( __CLASS__, 'deleted_user' ) );

add_action( 'init', array( __CLASS__, 'wp_init' ) );
}

/**
* Registers registration field labels with WPML.
*
* @link https://wpml.org/documentation/support/translation-for-texts-by-other-plugins-and-themes/
*/
public static function wp_init() {
if ( defined( 'AFFILIATES_WPML' ) && AFFILIATES_WPML ) {
require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings.php';
require_once AFFILIATES_CORE_LIB . '/class-affiliates-settings-registration.php';
$registration_fields = Affiliates_Settings_Registration::get_fields();
if ( is_array( $registration_fields ) ) {
foreach( $registration_fields as $name => $field ) {
// context, name, value
do_action( 'wpml_register_single_string', 'affiliates', self::get_wpml_string_name( $name ), $field['label'] );
}
}
}
}

/**
* Returns the string's registered name for use with WPML string translation.
*
* @param string $name
*
* @return string registered name
*/
private static function get_wpml_string_name( $name ) {
return sprintf( 'Field Label: %s', esc_attr( $name ) );
}

/**
Expand Down Expand Up @@ -439,7 +471,15 @@ public static function render_fields( $registration_fields = null ) {
if ( $field['enabled'] ) {
$output .= '<div class="field">';
$output .= '<label>';
$output .= stripslashes( $field['label'] );
$label = $field['label'];
// Translate registration field labels with WPML String Translation?
// https://wpml.org/documentation/support/translation-for-texts-by-other-plugins-and-themes/
// https://wpml.org/wpml-hook/wpml_translate_single_string/
if ( defined( 'AFFILIATES_WPML' ) && AFFILIATES_WPML ) {
// original value, domain, name, language code (optional and not used here)
$label = apply_filters( 'wpml_translate_single_string', $field['label'], 'affiliates', self::get_wpml_string_name( $name ) );
}
$output .= stripslashes( $label );
$output .= ' ';
$type = isset( $field['type'] ) ? $field['type'] : 'text';
$readonly = is_user_logged_in() && ( ( $name == 'user_login' ) || ( $name == 'user_email' ) ) ? ' readonly="readonly" ' : '';
Expand Down

0 comments on commit 6a3f97a

Please sign in to comment.