Repository navigation
Comments: Avoid a database error in the duplicate check for four-byte characters - #16231
ankit-addweb wants to merge 4 commits into
Conversation
… characters. When the `comment_content` column uses the legacy `utf8` character set and the connection uses `utf8mb4`, the duplicate comment check in `wp_allow_comment()` failed with a database error if the comment contained four-byte characters such as emoji, for example "Illegal mix of collations (utf8mb3_general_ci,IMPLICIT) and (utf8mb4_unicode_ci,COERCIBLE) for operation '='". Strip the characters the column cannot store from the value being compared, in the same way `wpdb` does when the comment is saved. This avoids the error and means the value compared matches the value that was stored. See #46947.
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
|
Trac ticket: https://core.trac.wordpress.org/ticket/46947
What
In
wp_allow_comment(), strip the characters thecomment_contentcolumn cannot store from the value used in the duplicate check, using$wpdb->strip_invalid_text_for_column(). This is the same stepwpdbapplies when the comment is saved.Why
With a legacy
utf8comment_contentcolumn and autf8mb4connection, the duplicate check query fails if the comment contains four-byte characters such as emoji:On MySQL 9.x the same failure is worded
Cannot convert string '...' from utf8mb4 to utf8mb3.wp_new_comment()already strips the text and callswp_allow_comment()again after the insert fails, so the comment is still saved without the emoji. The error is logged and the first duplicate check never worked correctly. Comparing the value that would actually be stored avoids the error and makes the first check meaningful.Reproduced on trunk by @Sukhendu2002 (see the ticket). I confirmed the error message above on MySQL 8.0.44 and the
Cannot convertwording on MySQL 9.7.4. I did not test MariaDB or MySQL 5.7.The change only affects columns that cannot store four-byte characters.
utf8mb4columns are unchanged.Tests
Adds tests to
Tests_Comment_WpAllowComment, each run against a legacyutf8column and autf8mb4column on autf8mb4connection:The two legacy-column tests fail without the change and pass with it.
How to test
comment_contentisutf8/utf8_general_ciand the connection isutf8mb4.❤🔥💯🙌.npm run test:php -- --filter Tests_Comment_WpAllowComment