Make WordPress Core

Changeset 58447


Ignore:
Timestamp:
06/20/2024 04:02:10 PM (4 months ago)
Author:
adamsilverstein
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    r58130 r58447  
    305305        $attachment->guid           = $url;
    306306
     307        // If the title was not set, use the original filename.
     308        if ( empty( $attachment->post_title ) && ! empty( $files['file']['name'] ) ) {
     309            // Remove the file extension (after the last `.`)
     310            $tmp_title = substr( $files['file']['name'], 0, strrpos( $files['file']['name'], '.' ) );
     311
     312            if ( ! empty( $tmp_title ) ) {
     313                $attachment->post_title = $tmp_title;
     314            }
     315        }
     316
     317        // Fall back to the original approach.
    307318        if ( empty( $attachment->post_title ) ) {
    308319            $attachment->post_title = preg_replace( '/\.[^.]+$/', '', wp_basename( $file ) );
  • trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php

    r58400 r58447  
    20162016
    20172017    /**
     2018     * Tests that the naming behavior of REST media uploads matches core media uploads.
     2019     *
     2020     * In particular, filenames with spaces should maintain the spaces rather than
     2021     * replacing them with hyphens.
     2022     *
     2023     * @ticket 57957
     2024     *
     2025     * @covers WP_REST_Attachments_Controller::insert_attachment
     2026     * @dataProvider rest_upload_filename_spaces
     2027     */
     2028    public function test_rest_upload_filename_spaces( $filename, $expected ) {
     2029        wp_set_current_user( self::$editor_id );
     2030        $request = new WP_REST_Request( 'POST', '/wp/v2/media' );
     2031        $request->set_header( 'Content-Type', 'image/jpeg' );
     2032        $request->set_body( file_get_contents( self::$test_file ) );
     2033        $request->set_file_params(
     2034            array(
     2035                'file' => array(
     2036                    'file'     => file_get_contents( self::$test_file2 ),
     2037                    'name'     => $filename,
     2038                    'size'     => filesize( self::$test_file2 ),
     2039                    'tmp_name' => self::$test_file2,
     2040                ),
     2041            )
     2042        );
     2043        $response = rest_get_server()->dispatch( $request );
     2044        $data     = $response->get_data();
     2045        $this->assertSame( 201, $response->get_status(), 'The file was not uploaded.' );
     2046        $this->assertSame( $expected, $data['title']['raw'], 'An incorrect filename was returned.' );
     2047    }
     2048
     2049    /**
     2050     * Data provider for text_rest_upload_filename_spaces.
     2051     *
     2052     * @return array
     2053     */
     2054    public function rest_upload_filename_spaces() {
     2055        return array(
     2056            'filename with spaces'  => array(
     2057                'Filename With Spaces.jpg',
     2058                'Filename With Spaces',
     2059            ),
     2060            'filename.with.periods' => array(
     2061                'Filename.With.Periods.jpg',
     2062                'Filename.With.Periods',
     2063            ),
     2064            'filename-with-dashes'  => array(
     2065                'Filename-With-Dashes.jpg',
     2066                'Filename-With-Dashes',
     2067            ),
     2068        );
     2069    }
     2070
     2071    /**
    20182072     * Ensure the `rest_after_insert_attachment` and `rest_insert_attachment` hooks only fire
    20192073     * once when attachments are updated.
Note: See TracChangeset for help on using the changeset viewer.