From ed1f32fac7dc3e764f5b8d72fb585284f2ee94b9 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Thu, 20 Jun 2024 16:04:12 +0000 Subject: [PATCH] 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 --- .../class-wp-rest-attachments-controller.php | 11 +++++++++++ wp-includes/version.php | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index 97ee830261..dbab4c933e 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/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 ) ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 4ad3ea0e8c..1b483a2320 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @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.