-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Media / Attachments REST API endpoint: cast args to array before sending to wp_slash > wp_insert_attachment #10415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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. You can also manually ask me to refresh this list by adding the 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. |
westonruter
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter not hypothetical. it was caught by PHPstan , showed up when I was rebasing #7619 on trunk 🚀 |
Thanks @westonruter and @justlevine - I need to double check my local set up. |
This change modifies the `wp_insert_attachment` call to ensure that the attachment post data is properly cast to an array before being passed to `wp_slash()`, enhancing data integrity during database insertion. Additionally, a new PHPUnit test is added to verify that the data is correctly slashed and formatted as an array, ensuring proper escaping of string values.
Co-authored-by: Mukesh Panchal <mukeshpanchal27@users.noreply.github.com>
70288c0 to
6b25134
Compare
|
Committed in r61065 |

Problem
The
prepare_item_for_database()method returns an object, butwp_slash()expects a string or array. Without casting the object to an array,wp_slash()would return the object unchanged, meaning string values within the object wouldn't be properly escaped for database insertion.Props to @justlevine for spotting it
Solution
Cast the object to an array before passing it to
wp_slash():Testing
Ensure no regressions to the
media/editendpoint.Kapture.2025-10-25.at.12.55.35.mp4
To be sure, you can copy that network request as cURL or fetch and add a title or description with apostrophes to the body payload, e.g.,
The results of
wp_slash( (array) $new_attachment_post )should beUnit tests
Run the new unit test:
npm run test:php -- --filter test_edit_image_wp_slash_with_object_castRun all attachment controller tests:
npm run test:php -- --filter WP_Test_REST_Attachments_Controllercc @justlevine
Trac ticket: https://core.trac.wordpress.org/ticket/64149
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.