Make WordPress Core

Changeset 65229


Ignore:
Timestamp:
11/03/2026 02:50:29 PM (12 days ago)
Author:
SergeyBiryukov
Message:

Media: Ignore empty items in a comma-separated post_mime_type string.

wp_post_mime_type_where() splits a comma-separated string of MIME types with explode(). An extra comma left an empty item, and the empty string is one of the wildcards, so a value like image/jpeg, returned no clause and matched every MIME type. This affected WP_Query and get_posts() with post_mime_type, wp_count_attachments() and the media modal's query-attachments Ajax request.

What changes

  • wp_post_mime_type_where(): after a string is split, empty items are removed with array_diff(). A value of '0' is not removed.
  • Arrays are not changed. An empty string, a string of only commas and spaces, and an explicit % still match every MIME type.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/15403.

Follow-up to r6910.

Props ekamran.
Fixes #66454.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r65021 r65229  
    39563956        if ( is_string( $post_mime_types ) ) {
    39573957                $post_mime_types = array_map( 'trim', explode( ',', $post_mime_types ) );
     3958                // Skip empty items left by extra commas, so they do not match the '' wildcard.
     3959                $post_mime_types = array_diff( $post_mime_types, array( '' ) );
    39583960        }
    39593961
  • trunk/tests/phpunit/tests/post/wpPostMimeTypeWhere.php

    r64974 r65229  
    6666                );
    6767        }
     68
     69        /**
     70         * Tests that empty items in a comma-separated string do not remove the restriction.
     71         *
     72         * @ticket 66454
     73         *
     74         * @dataProvider data_ignores_empty_items_in_comma_separated_string
     75         *
     76         * @param string $post_mime_types Comma-separated MIME types.
     77         * @param string $expected        Expected SQL clause.
     78         */
     79        public function test_ignores_empty_items_in_comma_separated_string( $post_mime_types, $expected ) {
     80                $this->assertSame( $expected, wp_post_mime_type_where( $post_mime_types ) );
     81        }
     82
     83        /**
     84         * Data provider.
     85         *
     86         * @return array[]
     87         */
     88        public static function data_ignores_empty_items_in_comma_separated_string() {
     89                return array(
     90                        'trailing comma'           => array( 'image/jpeg,', " AND (post_mime_type = 'image/jpeg') " ),
     91                        'trailing comma and space' => array( 'image/jpeg, ', " AND (post_mime_type = 'image/jpeg') " ),
     92                        'leading comma'            => array( ',image/jpeg', " AND (post_mime_type = 'image/jpeg') " ),
     93                        'double comma'             => array( 'image/jpeg,,video', " AND (post_mime_type = 'image/jpeg' OR post_mime_type LIKE 'video/%') " ),
     94                        'type with trailing comma' => array( 'image,', " AND (post_mime_type LIKE 'image/%') " ),
     95                );
     96        }
     97
     98        /**
     99         * Tests that an empty string and a wildcard in a comma-separated string still match every MIME type.
     100         *
     101         * @ticket 66454
     102         *
     103         * @dataProvider data_matches_every_mime_type
     104         *
     105         * @param string $post_mime_types MIME types.
     106         */
     107        public function test_matches_every_mime_type( $post_mime_types ) {
     108                $this->assertSame( '', wp_post_mime_type_where( $post_mime_types ) );
     109        }
     110
     111        /**
     112         * Data provider.
     113         *
     114         * @return array[]
     115         */
     116        public static function data_matches_every_mime_type() {
     117                return array(
     118                        'empty string'           => array( '' ),
     119                        'only commas and spaces' => array( ' , ' ),
     120                        'wildcard after a type'  => array( 'image/jpeg,%' ),
     121                );
     122        }
    68123}
Note: See TracChangeset for help on using the changeset viewer.