Skip to content

Commit

Permalink
Code Modernization: Fix non-nullable deprecation in get_available_pos…
Browse files Browse the repository at this point in the history
…t_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
}}}

[https://developer.wordpress.org/reference/functions/get_available_post_mime_types/#return This function is documented] to:
* Return `An array of MIME types.`
* as an array of `string`s, 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.

git-svn-id: https://develop.svn.wordpress.org/trunk@58437 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
hellofromtonya committed Jun 18, 2024
1 parent d5a2b44 commit 338f47b
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/wp-includes/post.php
Original file line number Diff line number Diff line change
Expand Up @@ -8094,10 +8094,11 @@ function get_available_post_mime_types( $type = 'attachment' ) {
$mime_types = apply_filters( 'pre_get_available_post_mime_types', null, $type );

if ( ! is_array( $mime_types ) ) {
$mime_types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) );
$mime_types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s AND post_mime_type != ''", $type ) );
}

return $mime_types;
// Remove nulls from returned $mime_types.
return array_values( array_filter( $mime_types ) );
}

/**
Expand Down
58 changes: 58 additions & 0 deletions tests/phpunit/tests/post/getAvailablePostMimeTypes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
/**
* Unit tests covering post mime types.
*
* @ticket 59195
*
* @group post
*
* @covers ::get_available_post_mime_types
*/

class Tests_Post_GetAvailablePostMimeTypes extends WP_UnitTestCase {

public function tear_down() {
// Remove all uploads.
$this->remove_added_uploads();
remove_filter( 'pre_get_available_post_mime_types', array( $this, 'filter_add_null_to_post_mime_types' ) );
parent::tear_down();
}

public function test_should_return_expected_post_mime_types() {
// Upload a JPEG image.
$filename = DIR_TESTDATA . '/images/test-image.jpg';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'], 'Uploading a JPEG file should not result in an error.' );
$this->_make_attachment( $upload );

// Upload a PDF file.
$filename = DIR_TESTDATA . '/images/test-alpha.pdf';
$contents = file_get_contents( $filename );
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
$this->assertEmpty( $upload['error'], 'Uploading a PDF file should not result in an error.' );
$this->_make_attachment( $upload );

$mime_types = get_available_post_mime_types();

$this->assertSame( array( 'image/jpeg', 'application/pdf' ), $mime_types, 'The MIME types returned should match the uploaded file MIME types.' );
}

public function test_should_remove_null() {
// Add filter to inject null into the mime types array.
add_filter( 'pre_get_available_post_mime_types', array( $this, 'filter_add_null_to_post_mime_types' ) );

$mime_types = get_available_post_mime_types();
$this->assertEqualsCanonicalizing( array( 'image/jpeg', 'image/png' ), $mime_types );
}

/**
* Filter to inject null into the mime types array.
*
* @param string $type Post type.
* @return array
*/
public function filter_add_null_to_post_mime_types( $type ) {
return array( 'image/jpeg', null, 'image/png' );
}
}

0 comments on commit 338f47b

Please sign in to comment.