diff --git a/phpcs.xml.dist b/phpcs.xml.dist
index 9f15c0c3670d6..7d4105ca3df98 100644
--- a/phpcs.xml.dist
+++ b/phpcs.xml.dist
@@ -286,6 +286,11 @@
/wp-tests-config-sample\.php
+
+
+ /src/wp-includes/compat.php
+
+
/tests/phpunit/tests/*
diff --git a/src/wp-admin/includes/comment.php b/src/wp-admin/includes/comment.php
index 5f46be6a5cdfa..1d22980a80425 100644
--- a/src/wp-admin/includes/comment.php
+++ b/src/wp-admin/includes/comment.php
@@ -31,11 +31,13 @@ function comment_exists( $comment_author, $comment_date, $timezone = 'blog' ) {
$date_field = 'comment_date_gmt';
}
+ $stored = _wp_encode_field_values_for_database( array( 'comment_author' => stripslashes( $comment_author ) ), $wpdb->comments, array( 'comment_author' ) );
+
return $wpdb->get_var(
$wpdb->prepare(
"SELECT comment_post_ID FROM $wpdb->comments
WHERE comment_author = %s AND $date_field = %s",
- stripslashes( $comment_author ),
+ $stored['comment_author'],
stripslashes( $comment_date )
)
);
diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php
index 6a1641cbae4d0..898dcd114ab8b 100644
--- a/src/wp-includes/comment.php
+++ b/src/wp-includes/comment.php
@@ -145,6 +145,8 @@ function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent,
)
);
} else {
+ $stored = _wp_encode_field_values_for_database( array( 'comment_author' => $author ), $wpdb->comments, array( 'comment_author' ) );
+
// expected_slashed ($author, $email)
$ok_to_comment = $wpdb->get_var(
$wpdb->prepare(
@@ -154,7 +156,7 @@ function check_comment( $author, $email, $url, $comment, $user_ip, $user_agent,
AND comment_author_email = %s
AND comment_approved = '1'
LIMIT 1",
- $author,
+ $stored['comment_author'],
$email
)
);
@@ -768,6 +770,8 @@ function sanitize_comment_cookies() {
function wp_allow_comment( $commentdata, $wp_error = false ) {
global $wpdb;
+ $stored = _wp_encode_field_values_for_database( $commentdata, $wpdb->comments, array( 'comment_author', 'comment_content' ) );
+
/*
* Simple duplicate check.
* expected_slashed ($comment_post_ID, $comment_author, $comment_author_email, $comment_content)
@@ -776,7 +780,7 @@ function wp_allow_comment( $commentdata, $wp_error = false ) {
"SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = %s AND comment_approved != 'trash' AND ( comment_author = %s ",
wp_unslash( $commentdata['comment_post_ID'] ),
wp_unslash( $commentdata['comment_parent'] ),
- wp_unslash( $commentdata['comment_author'] )
+ wp_unslash( $stored['comment_author'] )
);
if ( $commentdata['comment_author_email'] ) {
$dupe .= $wpdb->prepare(
@@ -786,7 +790,7 @@ function wp_allow_comment( $commentdata, $wp_error = false ) {
}
$dupe .= $wpdb->prepare(
') AND comment_content = %s LIMIT 1',
- wp_unslash( $commentdata['comment_content'] )
+ wp_unslash( $stored['comment_content'] )
);
$dupe_id = $wpdb->get_var( $dupe );
@@ -1322,16 +1326,20 @@ function wp_get_comment_fields_max_lengths() {
}
/**
- * Compares the lengths of comment data against the maximum character limits.
+ * Checks comment field lengths after preparing the author and content for storage.
*
* @since 4.7.0
*
- * @param array $comment_data Array of arguments for inserting a comment.
+ * @global wpdb $wpdb WordPress database abstraction object.
+ * @param array $comment_data Array of unslashed arguments for inserting a comment.
* @return WP_Error|true WP_Error when a comment field exceeds the limit,
* otherwise true.
*/
function wp_check_comment_data_max_lengths( $comment_data ) {
- $max_lengths = wp_get_comment_fields_max_lengths();
+ global $wpdb;
+
+ $comment_data = _wp_encode_field_values_for_database( $comment_data, $wpdb->comments, array( 'comment_author', 'comment_content' ) );
+ $max_lengths = wp_get_comment_fields_max_lengths();
if ( isset( $comment_data['comment_author'] ) && mb_strlen( $comment_data['comment_author'], '8bit' ) > $max_lengths['comment_author'] ) {
return new WP_Error( 'comment_author_column_length', __( 'Error: Your name is too long.' ), 200 );
@@ -2305,6 +2313,8 @@ function wp_insert_comment( $commentdata ) {
'user_id'
);
+ $compacted = _wp_encode_field_values_for_database( $compacted, $wpdb->comments, array( 'comment_author', 'comment_content' ) );
+
if ( ! $wpdb->insert( $wpdb->comments, $compacted ) ) {
return false;
}
@@ -2561,12 +2571,29 @@ function wp_new_comment( $commentdata, $wp_error = false ) {
$comment_id = wp_insert_comment( $commentdata );
if ( ! $comment_id ) {
- $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );
+ $fields = array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content' );
+ $unslashed = wp_unslash( $commentdata );
+ $stored = _wp_encode_field_values_for_database( $unslashed, $wpdb->comments, array( 'comment_author', 'comment_content' ) );
foreach ( $fields as $field ) {
- if ( isset( $commentdata[ $field ] ) ) {
- $commentdata[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $commentdata[ $field ] );
+ if ( ! isset( $commentdata[ $field ] ) ) {
+ continue;
}
+
+ if ( $stored[ $field ] !== $unslashed[ $field ] ) {
+ $value = $stored[ $field ];
+ $check_lengths = wp_check_comment_data_max_lengths( array( $field => $value ) );
+ if ( is_wp_error( $check_lengths ) ) {
+ return $wp_error ? $check_lengths : false;
+ }
+
+ // Keep the original characters for comment filters when the encoded value is valid.
+ if ( wp_is_valid_utf8( $value ) ) {
+ continue;
+ }
+ }
+
+ $commentdata[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->comments, $field, $commentdata[ $field ] );
}
$commentdata = wp_filter_comment( $commentdata );
@@ -3093,6 +3120,8 @@ function wp_update_comment( $commentarr, $wp_error = false ) {
$data = wp_array_slice_assoc( $data, $keys );
+ $data = _wp_encode_field_values_for_database( $data, $wpdb->comments, array( 'comment_author', 'comment_content' ) );
+
$result = $wpdb->update( $wpdb->comments, $data, array( 'comment_ID' => $comment_id ) );
if ( false === $result ) {
diff --git a/src/wp-includes/compat-utf8.php b/src/wp-includes/compat-utf8.php
index 326786fe608f2..15f15a095e510 100644
--- a/src/wp-includes/compat-utf8.php
+++ b/src/wp-includes/compat-utf8.php
@@ -312,6 +312,164 @@ function _wp_scrub_utf8_fallback( string $bytes ): string {
return $scrubbed;
}
+/**
+ * Polyfill for `mb_encode_numericentity()` operating on UTF-8 inputs.
+ *
+ * @since 7.2.0
+ * @access private
+ * @internal
+ *
+ * @param string $bytes The string to be encoded.
+ * @param int[] $map map is array specifies code area to convert.
+ * @param bool|null $hex Optional. Whether the returned entity reference should be in
+ * hexadecimal notation (otherwise it is in decimal notation).
+ * @return string The converted string.
+ */
+function _wp_encode_utf8_numericentity( $bytes, $map, $hex = false ) {
+ $output = '';
+ $at = 0;
+ $was_at = 0;
+ $end = strlen( $bytes );
+ $fffd = $hex ? '�' : '�';
+ $map_end = count( $map );
+
+ if ( 0 !== ( $map_end % 4 ) ) {
+ if ( class_exists( 'ValueError' ) ) {
+ throw new ValueError( 'mb_encode_numericentity(): Argument #2 ($map) must have a multiple of 4 elements' );
+ } else {
+ throw new Error( 'mb_encode_numericentity(): Argument #2 ($map) must have a multiple of 4 elements' );
+ }
+ }
+
+ while ( $at < $end ) {
+ $next_at = $at;
+ $invalid_length = 0;
+ $count = _wp_scan_utf8( $bytes, $next_at, $invalid_length, null, 1 );
+ $char_length = $next_at - $at;
+ $next_char = substr( $bytes, $at, $char_length );
+ $code_point = 0 < $count ? mb_ord( $next_char, 'UTF-8' ) : null;
+
+ // This is not the most efficient algorithm; install the mbstring extension!
+ $needs_remapping = false;
+ $offset = 0;
+ $mask = 0;
+ if ( 0 < $count ) {
+ for ( $i = 0; $i < $map_end; $i += 4 ) {
+ if ( $code_point >= $map[ $i ] && $code_point <= $map[ $i + 1 ] ) {
+ $needs_remapping = true;
+ $offset = $map[ $i + 2 ];
+ $mask = $map[ $i + 3 ];
+ break;
+ }
+ }
+ }
+
+ if ( ! $needs_remapping && 0 === $invalid_length ) {
+ $at = $next_at;
+ continue;
+ }
+
+ if ( isset( $code_point ) && $needs_remapping ) {
+ $code_point = ( $code_point + $offset ) & $mask;
+ $digits = $hex ? strtoupper( dechex( $code_point ) ) : (string) $code_point;
+ $output .= substr( $bytes, $was_at, $at - $was_at );
+ $output .= $hex ? "{$digits};" : "{$digits};";
+ } else {
+ $output .= substr( $bytes, $was_at, $at + $char_length - $was_at );
+ }
+
+ if ( $invalid_length > 0 ) {
+ $output .= $fffd;
+ }
+
+ $at = $next_at + $invalid_length;
+ $was_at = $at;
+ }
+
+ if ( $was_at < $end ) {
+ $output .= substr( $bytes, $was_at );
+ }
+
+ return $output;
+}
+
+/**
+ * Polyfill for `mb_encode_numericentity()` operating on UTF-8 inputs when the goal
+ * is to encode based on byte length of code point spans.
+ *
+ * @since 7.2.0
+ * @access private
+ * @internal
+ *
+ * @param int $byte_length Encode code points taking this many bytes or more to represent.
+ * @param string $bytes The string to be encoded.
+ * @param bool|null $hex Optional. Whether the returned entity reference should be in
+ * hexadecimal notation (otherwise it is in decimal notation).
+ * @return string The converted string.
+ */
+function _wp_encode_utf8_numericentity_bytelength( $byte_length, $bytes, $hex = false ) {
+ $output = '';
+ $at = 0;
+ $was_at = 0;
+ $end = strlen( $bytes );
+ $fffd = $byte_length > 3 ? "\u{FFFD}" : ( $hex ? '�' : '�' );
+
+ while ( $at < $end ) {
+ /*
+ * Quickly skip past US-ASCII bytes, all of which are valid UTF-8.
+ *
+ * This optimization step improves the speed from 10x to 100x
+ * depending on whether the JIT has optimized the function.
+ */
+ $ascii_byte_count = strspn(
+ $bytes,
+ "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" .
+ "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" .
+ " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f",
+ $at,
+ );
+
+ $at += $ascii_byte_count;
+ if ( $at >= $end ) {
+ break;
+ }
+
+ $next_at = $at;
+ $invalid_length = 0;
+ $count = _wp_scan_utf8( $bytes, $next_at, $invalid_length, null, 1 );
+ $char_length = $next_at - $at;
+ $next_char = 0 < $count ? substr( $bytes, $at, $char_length ) : null;
+
+ // The character is within bounds and is followed by no invalid bytes.
+ if ( $char_length < $byte_length && 0 === $invalid_length ) {
+ $at = $next_at;
+ continue;
+ }
+
+ if ( isset( $next_char ) && $char_length >= $byte_length ) {
+ $code_point = mb_ord( $next_char, 'UTF-8' );
+ $output .= substr( $bytes, $was_at, $at - $was_at );
+ $digits = $hex ? strtoupper( dechex( $code_point ) ) : (string) $code_point;
+ $output .= $hex ? "{$digits};" : "{$digits};";
+ } else {
+ $output .= substr( $bytes, $was_at, $at + $char_length - $was_at );
+ }
+
+ if ( $invalid_length > 0 ) {
+ $output .= $fffd;
+ }
+
+ $at = $next_at + $invalid_length;
+ $was_at = $at;
+ }
+
+ if ( $was_at < $end ) {
+ $output .= substr( $bytes, $was_at );
+ }
+
+ return $output;
+}
+
/**
* Returns how many code points are found in the given UTF-8 string.
*
diff --git a/src/wp-includes/compat.php b/src/wp-includes/compat.php
index 0aac0481c5d23..7b8905d059402 100644
--- a/src/wp-includes/compat.php
+++ b/src/wp-includes/compat.php
@@ -378,6 +378,97 @@ function _mb_strlen( $str, $encoding = null ) {
: strlen( $str );
}
+if ( ! function_exists( 'mb_encode_numericentity' ) ) :
+ /**
+ * Internal compat function to mimic mb_encode_numericentity().
+ *
+ * Only supports UTF-8 inputs. All other input encodings will be treated as UTF-8.
+ *
+ * @since 7.2.0
+ * @ignore
+ *
+ * @param string $string The string to be encoded.
+ * @param int[] $map map is array specifies code area to convert.
+ * @param string|null $encoding Optional. The encoding parameter is the character encoding. If it is
+ * omitted or null, the internal character encoding value will be used.
+ * @param bool|null $hex Optional. Whether the returned entity reference should be in
+ * hexadecimal notation (otherwise it is in decimal notation).
+ * @return string The converted string.
+ */
+ function mb_encode_numericentity( $string, $map, $encoding = null, $hex = false ) {
+ if ( isset( $encoding ) && 'UTF-8' !== $encoding ) {
+ wp_trigger_error(
+ __FUNCTION__,
+ 'Only UTF-8 is supported in the wp_encode_numericentity() polyfill; treating as UTF-8.',
+ E_USER_WARNING
+ );
+ }
+
+ if ( array( 0x10000, 0x10FFFF, 0, 0x1FFFFF ) === $map ) {
+ return _wp_encode_utf8_numericentity_bytelength( 4, $string, $hex );
+ }
+
+ if ( array( 0x80, 0x10FFFF, 0, 0x1FFFFF ) === $map ) {
+ return _wp_encode_utf8_numericentity_bytelength( 2, $string, $hex );
+ }
+
+ return _wp_encode_utf8_numericentity( $string, $map, $hex );
+ }
+endif;
+
+if ( ! function_exists( 'mb_substitute_character' ) ) :
+ /**
+ * Internal compat function to mimic mb_substitute_character().
+ *
+ * Effectively does nothing, because while this function stores the
+ * substitute character, it affects no other functions.
+ *
+ * @since 7.2.0
+ * @ignore
+ *
+ * @param string|int|null $substitute_character Specify the Unicode value as an int, or as one of the
+ * following strings: none, long, entity.
+ * @return int|string|true If substitute_character is set, it returns true for success,
+ * otherwise returns false. If substitute_character is not set,
+ * it returns the current setting.
+ */
+ function mb_substitute_character( $substitute_character = null ) {
+ static $character = 0x3F;
+
+ if ( ! isset( $substitute_character ) ) {
+ return $character;
+ }
+
+ if (
+ is_string( $substitute_character ) &&
+ in_array( strtolower( $substitute_character ), array( 'none', 'long', 'entity' ), true )
+ ) {
+ $character = strtolower( $substitute_character );
+ return true;
+ }
+
+ if ( ( is_float( $substitute_character ) && ! is_nan( $substitute_character ) ) || is_bool( $substitute_character ) ) {
+ $substitute_character = (int) $substitute_character;
+ }
+
+ if (
+ is_int( $substitute_character ) &&
+ 0 <= $substitute_character &&
+ ( $substitute_character < 0xD800 || $substitute_character > 0xDFFF ) &&
+ $substitute_character <= 0x10FFFF
+ ) {
+ $character = $substitute_character;
+ return true;
+ }
+
+ if ( class_exists( 'ValueError' ) ) {
+ throw new ValueError( 'Argument #1 ($substitute_character) is not a valid codepoint' );
+ } else {
+ throw new Error( 'Argument #1 ($substitute_character) is not a valid codepoint' );
+ }
+ }
+endif;
+
if ( ! function_exists( 'utf8_encode' ) ) :
if ( extension_loaded( 'mbstring' ) ) :
/**
diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
index ad12579fdafa1..89b43b0605a19 100644
--- a/src/wp-includes/formatting.php
+++ b/src/wp-includes/formatting.php
@@ -6193,6 +6193,101 @@ function wp_encode_emoji( $content ) {
return $content;
}
+/**
+ * Escapes a UTF-8-encoded HTML document for a given MySQL charset, replacing
+ * unrepresentable characters with their character-reference equivalents.
+ *
+ * Because WordPress only supports charsets compatible in the ASCII range,
+ * this function will return a common-demoninator for unrecognized encodings
+ * as the ASCII-safe escaping of the input HTML.
+ *
+ * In the future this function may perform conversion differently in certain
+ * cases, as not all content may be safely transformed; specifically, there
+ * is no representation in HTML for tag names, attribute names, comment
+ * content, using bytes that arenโt valid in the document encoding.
+ *
+ * Example:
+ *
+ * '🅰' === _wp_escape_html_for_mysql_charset( 'latin1', '๐
ฐ' );
+ * '🅰' === _wp_escape_html_for_mysql_charset( 'utf8mb3', '๐
ฐ' );
+ * '๐
ฐ' === _wp_escape_html_for_mysql_charset( 'utf8mb4', '๐
ฐ' );
+ *
+ * Because invalid byte spans are not generally safe for database storage, this
+ * function will replace those spans with an appropriate substitution character.
+ *
+ * Example:
+ *
+ * '๏ฟฝ' === _wp_escape_html_for_mysql_charset( 'utf8mb4', "\xC0" );
+ * '�' === _wp_escape_html_for_mysql_charset( 'utf8mb3', "\xC0" );
+ *
+ * Future improvement would scan through the HTML using the HTML API and make
+ * semantic substitutions where appropriate. For example, JavaScript, JSON,
+ * and JSON attributes in block deloimiters should use JavaScript Unicode
+ * escaping, CSS should use CSS Unicode escaping, CDATA sections should be
+ * split apart and escaped, while other syntax tokens will not be able to
+ * be properly escaped.
+ *
+ * @since 7.2.0
+ * @access private
+ * @ignore
+ *
+ * @param string $mysql_charset Convert into this encoding, according to MySQL charset names.
+ * @param string $html_in_utf8 Convert this HTML.
+ * @return string Transformed input safe for storing in a MySQL column of the given charset.
+ */
+function _wp_escape_html_for_mysql_charset( string $mysql_charset, string $html_in_utf8 ): string {
+ if ( 'utf8mb4' === $mysql_charset ) {
+ return wp_scrub_utf8( $html_in_utf8 );
+ }
+
+ $is_utf8mb3 = 'utf8' === $mysql_charset || 'utf8mb3' === $mysql_charset;
+ $substitute_character = $is_utf8mb3 ? 0xFFFD : 0x3F;
+ $replacement_range = $is_utf8mb3
+ ? array( 0x10000, 0x10FFFF, 0, 0x1FFFFF ) // Replace four-byte UTF-8 code points.
+ : array( 0x80, 0x10FFFF, 0, 0x1FFFFF ); // Replace all multi-byte UTF-8 code points.
+
+ $previous_substitute_character = mb_substitute_character();
+ mb_substitute_character( $substitute_character );
+ $transformed = mb_encode_numericentity( $html_in_utf8, $replacement_range, 'UTF-8', true );
+ mb_substitute_character( $previous_substitute_character );
+
+ return $transformed;
+}
+
+/**
+ * Replaces four-byte UTF-8 characters with HTML character references in fields whose database
+ * columns use `utf8` or `utf8mb3`.
+ *
+ * Use for fields where consumers interpret HTML character references.
+ *
+ * @link https://dev.mysql.com/doc/refman/8.4/en/charset-unicode-utf8.html
+ *
+ * @since 7.2.0
+ * @access private
+ * @ignore
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ *
+ * @param array $data Field values keyed by column name.
+ * @param string $table Database table name.
+ * @param string[] $fields Names of the fields to check.
+ * @return array Field values with four-byte characters encoded where needed.
+ */
+function _wp_encode_field_values_for_database( $data, $table, $fields ) {
+ global $wpdb;
+
+ foreach ( $fields as $field ) {
+ if ( ! isset( $data[ $field ] ) || ! is_string( $data[ $field ] ) ) {
+ continue;
+ }
+
+ $charset = $wpdb->get_col_charset( $table, $field );
+ $data[ $field ] = _wp_escape_html_for_mysql_charset( $charset, $data[ $field ] );
+ }
+
+ return $data;
+}
+
/**
* Converts emoji to a static img element.
*
diff --git a/tests/phpunit/tests/comment.php b/tests/phpunit/tests/comment.php
index c5c40b9b34da6..64d18da58036a 100644
--- a/tests/phpunit/tests/comment.php
+++ b/tests/phpunit/tests/comment.php
@@ -2560,6 +2560,285 @@ public function test_get_comment_should_only_treat_numeric_values_as_comment_ids
$this->assertNull( get_comment( true ), 'Expected true not to be cast to comment ID 1.' ); // @phpstan-ignore argument.type (Intentionally passing an invalid value.)
}
+ /**
+ * `wp_insert_post()` already encodes emoji as HTML entities before saving to a
+ * utf8/utf8mb3 column (see #31242). This forces the same code path for comments
+ * via the `pre_get_col_charset` filter, so it runs deterministically regardless
+ * of the test database's actual charset.
+ *
+ * @ticket 65700
+ *
+ * @covers ::wp_insert_comment
+ * @covers ::_wp_encode_field_values_for_database
+ */
+ public function test_wp_insert_comment_encodes_emoji_for_utf8mb3_columns(): void {
+ add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb3_for_comment_fields' ), 10, 3 );
+
+ $comment_id = wp_insert_comment(
+ array(
+ 'comment_post_ID' => self::$post_id,
+ 'comment_author' => "foo\xf0\x9f\x98\x88bar",
+ 'comment_content' => "foo\xf0\x9f\x98\x8ebaz",
+ )
+ );
+
+ remove_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb3_for_comment_fields' ), 10 );
+
+ $this->assertIsInt( $comment_id, 'Expected wp_insert_comment() to return the new comment ID.' );
+
+ $comment = get_comment( $comment_id );
+
+ $this->assertSame( 'foo😈bar', $comment->comment_author );
+ $this->assertSame( 'foo😎baz', $comment->comment_content );
+ }
+
+ /**
+ * @ticket 65700
+ * @covers ::wp_handle_comment_submission
+ * @covers ::wp_check_comment_data_max_lengths
+ *
+ * @dataProvider data_comment_encoded_length
+ * @param string $input Submission field name.
+ * @param string $column Comment table column name.
+ * @param string $prefix ASCII prefix before the four-byte characters.
+ * @param int $count Number of four-byte characters.
+ * @param bool $should_insert Whether the encoded value fits the comment field limit.
+ */
+ public function test_wp_handle_comment_submission_checks_encoded_length( $input, $column, $prefix, $count, $should_insert ): void {
+ add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb3_for_comment_fields' ), 10, 3 );
+
+ $submission = array(
+ 'comment_post_ID' => self::$post_id,
+ 'author' => 'Comment author',
+ 'email' => 'author@example.org',
+ 'comment' => 'Comment content.',
+ );
+
+ $submission[ $input ] = $prefix . str_repeat( '๐', $count );
+ $before = get_comments( array( 'count' => true ) );
+ $result = wp_handle_comment_submission( $submission );
+
+ if ( $should_insert ) {
+ $this->assertInstanceOf( WP_Comment::class, $result );
+ $this->assertSame( $prefix . str_repeat( '😀', $count ), $result->$column );
+ } else {
+ $this->assertSame( $before, get_comments( array( 'count' => true ) ), 'The overlong comment should not be inserted.' );
+ $this->assertWPError( $result );
+ $this->assertSame( $column . '_column_length', $result->get_error_code() );
+ }
+ }
+
+ public function data_comment_encoded_length() {
+ return array(
+ '245-byte author' => array( 'author', 'comment_author', 'ab', 27, true ),
+ '246-byte author' => array( 'author', 'comment_author', 'abc', 27, false ),
+ '65,525-byte content' => array( 'comment', 'comment_content', 'abcde', 7280, true ),
+ '65,526-byte content' => array( 'comment', 'comment_content', 'abcdef', 7280, false ),
+ );
+ }
+
+ /**
+ * @ticket 65700
+ * @covers ::wp_new_comment
+ */
+ public function test_wp_new_comment_rejects_overlong_encoded_author_on_retry(): void {
+ add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb3_for_comment_fields' ), 10, 3 );
+ add_filter(
+ 'pre_comment_author_name',
+ static function ( $author ) {
+ return 'Comment author ' . $author;
+ }
+ );
+
+ $before = get_comments( array( 'count' => true ) );
+ $result = wp_handle_comment_submission(
+ array(
+ 'comment_post_ID' => self::$post_id,
+ 'author' => 'ab' . str_repeat( '๐', 27 ),
+ 'email' => 'author@example.org',
+ 'comment' => 'Comment with an overlong filtered author name.',
+ )
+ );
+
+ $this->assertSame( $before, get_comments( array( 'count' => true ) ), 'The overlong comment should not be inserted.' );
+ $this->assertWPError( $result );
+ $this->assertSame( 'comment_author_column_length', $result->get_error_code() );
+ }
+
+ /**
+ * Verifies that four-byte characters round-trip on utf8mb4 columns.
+ *
+ * @ticket 65700
+ *
+ * @covers ::wp_insert_comment
+ */
+ public function test_wp_insert_comment_does_not_encode_emoji_for_utf8mb4_columns(): void {
+ global $wpdb;
+
+ if ( 'utf8mb4' !== $wpdb->get_col_charset( $wpdb->comments, 'comment_author' )
+ || 'utf8mb4' !== $wpdb->get_col_charset( $wpdb->comments, 'comment_content' )
+ ) {
+ $this->markTestSkipped( 'This test requires utf8mb4 comment author and content columns.' );
+ }
+
+ $comment_id = wp_insert_comment(
+ array(
+ 'comment_post_ID' => self::$post_id,
+ 'comment_author' => "foo\xf0\x9f\x98\x88bar",
+ 'comment_content' => "foo\xf0\x9f\x98\x8ebaz",
+ )
+ );
+
+ $this->assertIsInt( $comment_id, 'Expected wp_insert_comment() to return the new comment ID.' );
+
+ $comment = get_comment( $comment_id );
+
+ $this->assertSame( "foo\xf0\x9f\x98\x88bar", $comment->comment_author );
+ $this->assertSame( "foo\xf0\x9f\x98\x8ebaz", $comment->comment_content );
+ }
+
+ /**
+ * @ticket 65700
+ *
+ * @covers ::wp_update_comment
+ * @covers ::_wp_encode_field_values_for_database
+ */
+ public function test_wp_update_comment_encodes_emoji_for_utf8mb3_columns(): void {
+ $comment_id = self::factory()->comment->create( array( 'comment_post_ID' => self::$post_id ) );
+
+ add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb3_for_comment_fields' ), 10, 3 );
+
+ $result = wp_update_comment(
+ array(
+ 'comment_ID' => $comment_id,
+ 'comment_author' => "foo\xf0\x9f\x98\x88bar",
+ 'comment_content' => "foo\xf0\x9f\x98\x8ebaz",
+ )
+ );
+
+ remove_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb3_for_comment_fields' ), 10 );
+
+ $this->assertSame( 1, $result );
+
+ $comment = get_comment( $comment_id );
+
+ $this->assertSame( 'foo😈bar', $comment->comment_author );
+ $this->assertSame( 'foo😎baz', $comment->comment_content );
+ }
+
+ /**
+ * @ticket 65700
+ *
+ * @covers ::wp_allow_comment
+ */
+ public function test_duplicate_comment_with_emoji_is_rejected_on_utf8mb3(): void {
+ add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb3_for_comment_fields' ), 10, 3 );
+
+ $submission = array(
+ 'comment_post_ID' => self::$post_id,
+ 'author' => 'Bob ๐',
+ 'email' => 'bob@example.org',
+ 'comment' => 'Hello ๐',
+ );
+ $this->assertInstanceOf( WP_Comment::class, wp_handle_comment_submission( $submission ) );
+
+ $result = wp_handle_comment_submission( $submission );
+ $this->assertWPError( $result );
+ $this->assertSame( 'comment_duplicate', $result->get_error_code() );
+ }
+
+ /**
+ * @ticket 65700
+ *
+ * @covers ::check_comment
+ */
+ public function test_previously_approved_author_with_emoji_is_recognized_on_utf8mb3(): void {
+ add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb3_for_comment_fields' ), 10, 3 );
+ update_option( 'comment_previously_approved', '1' );
+
+ $author = 'Bob ๐';
+ $email = 'bob@example.org';
+ self::factory()->comment->create(
+ array(
+ 'comment_post_ID' => self::$post_id,
+ 'comment_author' => $author,
+ 'comment_author_email' => $email,
+ 'comment_approved' => 1,
+ )
+ );
+
+ $this->assertTrue( check_comment( $author, $email, '', 'Another comment.', '', '', 'comment' ) );
+ }
+
+ /**
+ * @ticket 65700
+ *
+ * @covers ::comment_exists
+ */
+ public function test_comment_exists_finds_author_with_emoji_on_utf8mb3(): void {
+ add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb3_for_comment_fields' ), 10, 3 );
+
+ $author = "O'Brien ๐";
+ $date = '2014-05-06 12:00:00';
+ wp_insert_comment(
+ array(
+ 'comment_post_ID' => self::$post_id,
+ 'comment_author' => wp_slash( $author ),
+ 'comment_date' => $date,
+ )
+ );
+
+ $this->assertSame( (string) self::$post_id, comment_exists( wp_slash( $author ), $date ) );
+ }
+
+ /**
+ * @ticket 65700
+ *
+ * @covers ::wp_new_comment
+ */
+ public function test_wp_new_comment_preserves_emoji_when_another_field_needs_cleanup(): void {
+ add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb3_for_comment_fields' ), 10, 3 );
+
+ $commentdata = array(
+ 'comment_post_ID' => self::$post_id,
+ 'comment_author' => 'Ann ๐',
+ 'comment_author_email' => 'ann@example.org',
+ 'comment_author_url' => 'https://example.org/' . str_repeat( 'a', 181 ),
+ 'comment_content' => 'Hello ๐',
+ );
+
+ $approved_data = null;
+ add_filter(
+ 'pre_comment_approved',
+ static function ( $approved, $data ) use ( &$approved_data ) {
+ $approved_data = $data;
+ return $approved;
+ },
+ 10,
+ 2
+ );
+
+ $comment_id = wp_new_comment( $commentdata, true );
+ $this->assertIsInt( $comment_id );
+ $comment = get_comment( $comment_id );
+
+ $this->assertSame( substr( $commentdata['comment_author_url'], 0, 200 ), $comment->comment_author_url );
+ $this->assertSame( 'Ann 😀', $comment->comment_author );
+ $this->assertSame( 'Hello 😎', $comment->comment_content );
+ $this->assertSame( $commentdata['comment_author'], $approved_data['comment_author'] );
+ $this->assertSame( $commentdata['comment_content'], $approved_data['comment_content'] );
+ }
+
+ public function filter_pre_get_col_charset_utf8mb3_for_comment_fields( $charset, $table, $column ) {
+ global $wpdb;
+
+ if ( $wpdb->comments === $table && in_array( $column, array( 'comment_author', 'comment_content' ), true ) ) {
+ return 'utf8mb3';
+ }
+
+ return $charset;
+ }
+
/**
* Queries made by metadata observers must see the newly added metadata.
*
diff --git a/tests/phpunit/tests/formatting/emoji.php b/tests/phpunit/tests/formatting/emoji.php
index 56ffa301a591c..bb10d04c67b46 100644
--- a/tests/phpunit/tests/formatting/emoji.php
+++ b/tests/phpunit/tests/formatting/emoji.php
@@ -376,4 +376,95 @@ public static function data_wp_staticize_emoji() {
public function test_wp_staticize_emoji( $emoji, $expected ) {
$this->assertSame( $expected, wp_staticize_emoji( $emoji ) );
}
+
+ /**
+ * @ticket 65700
+ *
+ * @covers ::_wp_encode_field_values_for_database
+ */
+ public function test_wp_encode_4byte_chars_in_fields_only_touches_requested_utf8mb3_fields(): void {
+ $data = array(
+ 'legacy_field' => 'โข๐๐ ฎท',
+ 'modern_field' => 'โข๐๐ ฎท',
+ 'ignored_field' => 'โข๐๐ ฎท',
+ );
+
+ add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_for_encode_4byte_chars_in_fields_test' ), 10, 3 );
+
+ $result = _wp_encode_field_values_for_database( $data, 'some_table', array( 'legacy_field', 'modern_field' ) );
+
+ remove_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_for_encode_4byte_chars_in_fields_test' ), 10 );
+
+ $this->assertSame(
+ 'โข🙂𠮷',
+ $result['legacy_field'],
+ 'Expected four-byte characters to be encoded for a field on a utf8/utf8mb3 column.'
+ );
+ $this->assertSame(
+ 'โข๐๐ ฎท',
+ $result['modern_field'],
+ 'Expected four-byte characters to be left alone for a field on a utf8mb4 column.'
+ );
+ $this->assertSame(
+ 'โข๐๐ ฎท',
+ $result['ignored_field'],
+ 'Expected a field not passed in $fields to be left alone entirely.'
+ );
+ }
+
+ /**
+ * @ticket 65700
+ *
+ * @covers ::_wp_encode_field_values_for_database
+ */
+ public function test_wp_encode_4byte_chars_in_fields_recognizes_the_utf8_alias(): void {
+ $data = array( 'legacy_field' => '๐' );
+
+ add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8_alias_for_encode_4byte_chars_in_fields_test' ) );
+
+ $result = _wp_encode_field_values_for_database( $data, 'some_table', array( 'legacy_field' ) );
+
+ remove_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8_alias_for_encode_4byte_chars_in_fields_test' ) );
+
+ $this->assertSame(
+ '🙂',
+ $result['legacy_field'],
+ 'Expected emoji to be encoded for a field reporting the deprecated utf8 charset alias.'
+ );
+ }
+
+ /**
+ * @ticket 65700
+ *
+ * @covers ::_wp_encode_field_values_for_database
+ */
+ public function test_wp_encode_4byte_chars_in_fields_ignores_fields_missing_from_data(): void {
+ $data = array( 'present_field' => '๐' );
+
+ // Short-circuits wpdb::get_col_charset() so this doesn't hit a real,
+ // nonexistent 'some_table' table in the test database.
+ add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb4_for_encode_4byte_chars_in_fields_test' ) );
+
+ $result = _wp_encode_field_values_for_database( $data, 'some_table', array( 'present_field', 'absent_field' ) );
+
+ remove_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset_utf8mb4_for_encode_4byte_chars_in_fields_test' ) );
+
+ $this->assertArrayNotHasKey(
+ 'absent_field',
+ $result,
+ 'Expected a field missing from $data to not be added by _wp_encode_4byte_chars_in_fields().'
+ );
+ }
+
+ public function filter_pre_get_col_charset_for_encode_4byte_chars_in_fields_test( $charset, $table, $column ) {
+ return 'legacy_field' === $column ? 'utf8mb3' : 'utf8mb4';
+ }
+
+ public function filter_pre_get_col_charset_utf8_alias_for_encode_4byte_chars_in_fields_test() {
+ return 'utf8';
+ }
+
+ public function filter_pre_get_col_charset_utf8mb4_for_encode_4byte_chars_in_fields_test() {
+ return 'utf8mb4';
+ }
}