From 2043bfee9a2827b583bbd6dcd5b3ee5f7b4231ee Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Thu, 5 Jan 2017 16:14:39 +0000 Subject: [PATCH] Media: Improved media titles when created from filename. Preserves spaces and generally creates more accurate, cleaner titles from filenames of uploaded media. Merge of [38615] to the 4.4 branch. Fixes #37989. git-svn-id: https://develop.svn.wordpress.org/branches/4.4@39712 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/media.php | 2 +- tests/phpunit/tests/media.php | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/wp-admin/includes/media.php b/src/wp-admin/includes/media.php index 6635cafac3..4449cdc08d 100644 --- a/src/wp-admin/includes/media.php +++ b/src/wp-admin/includes/media.php @@ -286,7 +286,7 @@ function media_handle_upload($file_id, $post_id, $post_data = array(), $override $url = $file['url']; $type = $file['type']; $file = $file['file']; - $title = sanitize_title( $name ); + $title = sanitize_text_field( $name ); $content = ''; $excerpt = ''; diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index ca1f47be74..558b1a32b1 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -591,6 +591,37 @@ VIDEO; $this->assertEquals( 'This is a comment. / Это комментарий. / Βλέπετε ένα σχόλιο.', $post->post_excerpt ); } + /** + * @ticket 37989 + */ + public function test_media_handle_upload_expected_titles() { + $test_file = DIR_TESTDATA . '/images/test-image.jpg'; + + // Make a copy of this file as it gets moved during the file upload + $tmp_name = wp_tempnam( $test_file ); + + copy( $test_file, $tmp_name ); + + $_FILES['upload'] = array( + 'tmp_name' => $tmp_name, + 'name' => 'This is a test.jpg', + 'type' => 'image/jpeg', + 'error' => 0, + 'size' => filesize( $test_file ), + ); + + $post_id = media_handle_upload( 'upload', 0, array(), array( 'action' => 'test_upload_titles', 'test_form' => false ) ); + + unset( $_FILES['upload'] ); + + $post = get_post( $post_id ); + + // Clean up. + wp_delete_attachment( $post_id ); + + $this->assertEquals( 'This is a test', $post->post_title ); + } + /** * @ticket 33016 */