diff options
author | Adam Silverstein <adamsilverstein@git.wordpress.org> | 2024-06-20 16:02:10 +0000 |
---|---|---|
committer | Adam Silverstein <adamsilverstein@git.wordpress.org> | 2024-06-20 16:02:10 +0000 |
commit | 2358de1767168232ff0e7c17e550b8a99f96002e (patch) | |
tree | 1f7c1c07d3ad3a484efbce383eaa279d726d46e2 /src | |
parent | 3bb533ae10af686f429af2b932b7fb93fa616f86 (diff) | |
download | wordpress-2358de1767168232ff0e7c17e550b8a99f96002e.tar.gz wordpress-2358de1767168232ff0e7c17e550b8a99f96002e.zip |
Media: improve titles when inserting via REST API.
Match the naming behavior for uploaded media in the REST API to the way media is named when uploading in the media library. Fix an issue where dashes were replacing spaces unnecessarily.
Props abitofmind, kadamwhite, spacedmonkey, adamsilverstein, audrasjb, hellofromTonya.
Fixes #57957.
git-svn-id: https://develop.svn.wordpress.org/trunk@58447 602fd350-edb4-49c9-b593-d223f7449a82
Diffstat (limited to 'src')
-rw-r--r-- | src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 97ee830261..dbab4c933e 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -304,6 +304,17 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { $attachment->post_mime_type = $type; $attachment->guid = $url; + // If the title was not set, use the original filename. + if ( empty( $attachment->post_title ) && ! empty( $files['file']['name'] ) ) { + // Remove the file extension (after the last `.`) + $tmp_title = substr( $files['file']['name'], 0, strrpos( $files['file']['name'], '.' ) ); + + if ( ! empty( $tmp_title ) ) { + $attachment->post_title = $tmp_title; + } + } + + // Fall back to the original approach. if ( empty( $attachment->post_title ) ) { $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) ); } |