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.


Built from https://develop.svn.wordpress.org/trunk@58447


git-svn-id: http://core.svn.wordpress.org/trunk@57896 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Adam Silverstein 2024-06-20 16:04:12 +00:00
parent f4989bdf32
commit ed1f32fac7
2 changed files with 12 additions and 1 deletions

View File

@ -304,6 +304,17 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {
$attachment->post_mime_type = $type; $attachment->post_mime_type = $type;
$attachment->guid = $url; $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 ) ) { if ( empty( $attachment->post_title ) ) {
$attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) ); $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
} }

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.6-beta3-58446'; $wp_version = '6.6-beta3-58447';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.