Skip to content

Commit bdde1c4

Browse files
authored
Forms: Add emoji flag to email responses (#45645)
* Forms: Add country flag emoji to response email Introduces a method to convert country codes to emoji flags in the Feedback class and displays the flag alongside the IP address in the contact form footer. Includes unit tests to verify correct flag generation for various country codes and handles cases where the country code is unavailable. * changelog * Lets preserve the previous translation * Update variable name
1 parent ce4af9f commit bdde1c4

File tree

5 files changed

+114
-3
lines changed

5 files changed

+114
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Forms: add emoji flag to the response email next to the IP Address

projects/packages/forms/src/contact-form/class-contact-form.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2094,10 +2094,11 @@ public function process_submission() {
20942094
);
20952095
$footer_ip = null;
20962096
if ( $comment_author_ip ) {
2097-
$footer_ip = sprintf(
2098-
/* translators: Placeholder is the IP address of the person who submitted a form. */
2097+
$comment_author_ip_with_flag = $comment_author_ip . ( $response->get_country_flag() ? ' ' . $response->get_country_flag() : '' );
2098+
$footer_ip = sprintf(
2099+
/* translators: Placeholder is the IP address of the person who submitted a form. */
20992100
esc_html__( 'IP Address: %1$s', 'jetpack-forms' ),
2100-
$comment_author_ip
2101+
$comment_author_ip_with_flag
21012102
) . '<br />';
21022103
}
21032104

projects/packages/forms/src/contact-form/class-feedback.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,50 @@ public function get_country_code() {
784784
return $this->country_code;
785785
}
786786

787+
/**
788+
* Get the emoji flag for the country.
789+
*
790+
* @return string The emoji flag for the country code, or empty string if unavailable.
791+
*/
792+
public function get_country_flag() {
793+
return self::country_code_to_emoji_flag( $this->country_code );
794+
}
795+
796+
/**
797+
* Convert a country code to an emoji flag.
798+
*
799+
* Country codes should already be uppercase as they're stored that way by get_country_code_from_ip().
800+
*
801+
* @param string $country_code - the two-letter country code (e.g., 'US', 'GB', 'DE').
802+
*
803+
* @return string The emoji flag for the country code, or empty string if invalid.
804+
*/
805+
private static function country_code_to_emoji_flag( $country_code ) {
806+
if ( empty( $country_code ) || strlen( $country_code ) !== 2 ) {
807+
return '';
808+
}
809+
810+
// Convert each letter to a regional indicator symbol
811+
// Regional indicator symbols start at Unicode code point 127462 (🇦)
812+
// and correspond to A-Z (ASCII 65-90)
813+
$flag = '';
814+
for ( $i = 0; $i < 2; $i++ ) {
815+
$char = $country_code[ $i ];
816+
817+
// Check if the character is a valid uppercase letter (A-Z)
818+
if ( ord( $char ) < 65 || ord( $char ) > 90 ) {
819+
return '';
820+
}
821+
822+
$code_point = 127462 + ( ord( $char ) - 65 );
823+
824+
// Convert code point to UTF-8 encoded character
825+
$flag .= mb_chr( $code_point, 'UTF-8' );
826+
}
827+
828+
return $flag;
829+
}
830+
787831
/**
788832
* Get country code from IP address.
789833
*

projects/packages/forms/tests/php/contact-form/Feedback_Test.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2972,4 +2972,62 @@ public function test_notification_recipients_validates_capabilities() {
29722972
wp_delete_user( $author_id );
29732973
wp_delete_user( $subscriber_id );
29742974
}
2975+
2976+
/**
2977+
* Test that country flags are returned correctly.
2978+
*/
2979+
public function test_get_country_flag() {
2980+
$form_id = Utility::get_form_id();
2981+
2982+
$_post_data = Utility::get_post_request(
2983+
array(
2984+
'name' => 'John Doe',
2985+
'email' => '[email protected]',
2986+
'message' => 'Test message',
2987+
),
2988+
'g' . $form_id
2989+
);
2990+
2991+
$form = new Contact_Form(
2992+
array(
2993+
'title' => 'Test Form',
2994+
'description' => 'This is a test form.',
2995+
),
2996+
"[contact-field label='Name' type='name' required='1'/][contact-field label='Email' type='email' required='1'/][contact-field label='Message' type='textarea' required='1'/]"
2997+
);
2998+
2999+
// Test valid country codes
3000+
$test_cases = array(
3001+
'US' => '🇺🇸',
3002+
'GB' => '🇬🇧',
3003+
'DE' => '🇩🇪',
3004+
'CA' => '🇨🇦',
3005+
'JP' => '🇯🇵',
3006+
'us' => '🇺🇸', // Test lowercase (should be converted to uppercase internally)
3007+
);
3008+
3009+
foreach ( $test_cases as $country_code => $expected_flag ) {
3010+
$filter_callback = function () use ( $country_code ) {
3011+
return $country_code;
3012+
};
3013+
add_filter( 'jetpack_get_country_from_ip', $filter_callback, 10 );
3014+
3015+
$response = Feedback::from_submission( $_post_data, $form );
3016+
3017+
$this->assertEquals( $expected_flag, $response->get_country_flag(), "Country code {$country_code} should convert to flag emoji {$expected_flag}" );
3018+
3019+
remove_filter( 'jetpack_get_country_from_ip', $filter_callback, 10 );
3020+
}
3021+
3022+
// Test when no country code is available
3023+
$filter_callback = function () {
3024+
return null;
3025+
};
3026+
add_filter( 'jetpack_get_country_from_ip', $filter_callback, 10 );
3027+
3028+
$response = Feedback::from_submission( $_post_data, $form );
3029+
$this->assertSame( '', $response->get_country_flag(), 'Should return empty string when no country code is available' );
3030+
3031+
remove_filter( 'jetpack_get_country_from_ip', $filter_callback, 10 );
3032+
}
29753033
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: enhancement
3+
4+
Forms: Add emoji flag to the response email next to the IP address.

0 commit comments

Comments
 (0)