Skip to content

Commit 338f47b

Browse files
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
1 parent d5a2b44 commit 338f47b

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/wp-includes/post.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -8094,10 +8094,11 @@ function get_available_post_mime_types( $type = 'attachment' ) {
80948094
$mime_types = apply_filters( 'pre_get_available_post_mime_types', null, $type );
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 ) );
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 ) );
80988098
}
80998099

8100-
return $mime_types;
8100+
// Remove nulls from returned $mime_types.
8101+
return array_values( array_filter( $mime_types ) );
81018102
}
81028103

81038104
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* Unit tests covering post mime types.
4+
*
5+
* @ticket 59195
6+
*
7+
* @group post
8+
*
9+
* @covers ::get_available_post_mime_types
10+
*/
11+
12+
class Tests_Post_GetAvailablePostMimeTypes extends WP_UnitTestCase {
13+
14+
public function tear_down() {
15+
// Remove all uploads.
16+
$this->remove_added_uploads();
17+
remove_filter( 'pre_get_available_post_mime_types', array( $this, 'filter_add_null_to_post_mime_types' ) );
18+
parent::tear_down();
19+
}
20+
21+
public function test_should_return_expected_post_mime_types() {
22+
// Upload a JPEG image.
23+
$filename = DIR_TESTDATA . '/images/test-image.jpg';
24+
$contents = file_get_contents( $filename );
25+
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
26+
$this->assertEmpty( $upload['error'], 'Uploading a JPEG file should not result in an error.' );
27+
$this->_make_attachment( $upload );
28+
29+
// Upload a PDF file.
30+
$filename = DIR_TESTDATA . '/images/test-alpha.pdf';
31+
$contents = file_get_contents( $filename );
32+
$upload = wp_upload_bits( wp_basename( $filename ), null, $contents );
33+
$this->assertEmpty( $upload['error'], 'Uploading a PDF file should not result in an error.' );
34+
$this->_make_attachment( $upload );
35+
36+
$mime_types = get_available_post_mime_types();
37+
38+
$this->assertSame( array( 'image/jpeg', 'application/pdf' ), $mime_types, 'The MIME types returned should match the uploaded file MIME types.' );
39+
}
40+
41+
public function test_should_remove_null() {
42+
// Add filter to inject null into the mime types array.
43+
add_filter( 'pre_get_available_post_mime_types', array( $this, 'filter_add_null_to_post_mime_types' ) );
44+
45+
$mime_types = get_available_post_mime_types();
46+
$this->assertEqualsCanonicalizing( array( 'image/jpeg', 'image/png' ), $mime_types );
47+
}
48+
49+
/**
50+
* Filter to inject null into the mime types array.
51+
*
52+
* @param string $type Post type.
53+
* @return array
54+
*/
55+
public function filter_add_null_to_post_mime_types( $type ) {
56+
return array( 'image/jpeg', null, 'image/png' );
57+
}
58+
}

0 commit comments

Comments
 (0)