From 338f47b3c5e43955ffb6d231a99d0bea91b385d0 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Tue, 18 Jun 2024 14:44:23 +0000 Subject: [PATCH] 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 }}} [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 --- src/wp-includes/post.php | 5 +- .../tests/post/getAvailablePostMimeTypes.php | 58 +++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/post/getAvailablePostMimeTypes.php diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 2f7cba613f0ed..993056ff2c032 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -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 ) ); } /** diff --git a/tests/phpunit/tests/post/getAvailablePostMimeTypes.php b/tests/phpunit/tests/post/getAvailablePostMimeTypes.php new file mode 100644 index 0000000000000..cf6f46b920324 --- /dev/null +++ b/tests/phpunit/tests/post/getAvailablePostMimeTypes.php @@ -0,0 +1,58 @@ +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' ); + } +}