Make WordPress Core

Changeset 58437

Timestamp:
06/18/2024 02:44:23 PM (3 weeks ago)
Author:
hellofromTonya
Message:

Code Modernization: Fix non-nullable deprecation in get_available_post_mime_types().

Fixes a PHP 8.1 and above "null to non-nullable" deprecation notice in get_available_post_mime_types():

Deprecated: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated in ./wp-includes/post.php on line 3395

This function is documented to:

  • Return An array of MIME types.
  • as an array of strings, i.e. string[].

A null or empty element within the returned array is not a valid MIME type. If a null exists in the returned array, it is the root cause of PHP throwing the deprecation notice.

This commit removes the null and empty elements from the returned array of MIME types. It also adds a unit test.

Follow-up to [56623], [56452].

Props nosilver4u, jrf, ironprogrammer, antpb, antonvlasenko, rajinsharwar, hellofromTonya.
Fixes #59195.

Location:
trunk
Files:
1 added
1 edited

Legend:

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

    r58377 r58437  
    80958095
    80968096    if ( ! is_array( $mime_types ) ) {
    8097         $mime_types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) );
    8098     }
    8099 
    8100     return $mime_types;
     8097        $mime_types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s AND post_mime_type != ''", $type ) );
     8098    }
     8099
     8100    // Remove nulls from returned $mime_types.
     8101    return array_values( array_filter( $mime_types ) );
    81018102}
    81028103
Note: See TracChangeset for help on using the changeset viewer.