Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
CRAP
n/a
0 / 0
jetpack_wpcom_add_m4a_browser_types_to_editor_upload_types
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
5.02
1<?php
2/**
3 * Lets the block editor upload .m4a audio.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8/**
9 * Adds the types browsers report for .m4a files to the block editor's upload types, where m4a is allowed.
10 *
11 * WordPress lists m4a only as audio/mpeg, so the editor refuses the audio/x-m4a or audio/mp4 that
12 * browsers report before uploading. Remove once https://core.trac.wordpress.org/ticket/40175 is fixed.
13 *
14 * @param array $settings Block editor settings.
15 * @return array Block editor settings.
16 */
17function jetpack_wpcom_add_m4a_browser_types_to_editor_upload_types( $settings ) {
18    $allowed = $settings['allowedMimeTypes'] ?? null;
19    if ( ! is_array( $allowed ) ) {
20        return $settings;
21    }
22
23    foreach ( $allowed as $extensions => $mime_type ) {
24        if ( str_starts_with( (string) $mime_type, 'audio/' ) && in_array( 'm4a', explode( '|', (string) $extensions ), true ) ) {
25            // The editor reads only the types; each key has to name m4a and be unique.
26            $settings['allowedMimeTypes'] += array(
27                'm4a'     => 'audio/x-m4a',
28                'm4a|m4b' => 'audio/mp4',
29            );
30            break;
31        }
32    }
33
34    return $settings;
35}
36add_filter( 'block_editor_settings_all', 'jetpack_wpcom_add_m4a_browser_types_to_editor_upload_types' );