| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Additional available file types. |
| 5 |
* |
| 6 |
* @since 1.0.0 |
| 7 |
* |
| 8 |
* @return array |
| 9 |
*/ |
| 10 |
function fut_get_available_file_types(): array { |
| 11 |
|
| 12 |
// Serve v2 for new installations, and for old installations having multiple mime types support enabled. |
| 13 |
$file = |
| 14 |
( |
| 15 |
! get_option( 'file_upload_types' ) || |
| 16 |
'enabled' === get_option( 'file_upload_types_multiple_mimes' ) |
| 17 |
) |
| 18 |
? 'file-types-list-v2' |
| 19 |
: 'file-types-list'; |
| 20 |
|
| 21 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 22 |
$mime_types_serialized = trim( file_get_contents( dirname( FILE_UPLOAD_TYPES_PLUGIN_FILE ) . '/assets/' . $file . '.json' ) ); |
| 23 |
|
| 24 |
return json_decode( $mime_types_serialized, true ) ?? []; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Formats raw data of file types. |
| 29 |
* |
| 30 |
* @since 1.0.0 |
| 31 |
* |
| 32 |
* @param array $file_data_raw Raw data of the file types. |
| 33 |
* |
| 34 |
* @return array |
| 35 |
*/ |
| 36 |
function fut_format_raw_custom_types( array $file_data_raw ): array { |
| 37 |
|
| 38 |
$description = isset( $file_data_raw['desc'] ) ? array_map( 'sanitize_text_field', $file_data_raw['desc'] ) : []; |
| 39 |
$mime_types = isset( $file_data_raw['mime'] ) ? array_map( 'sanitize_text_field', $file_data_raw['mime'] ) : []; |
| 40 |
$extensions = isset( $file_data_raw['ext'] ) ? array_map( 'sanitize_text_field', $file_data_raw['ext'] ) : []; |
| 41 |
|
| 42 |
$file_data = _fut_update_file_data_description( $description ); |
| 43 |
$file_data = _fut_update_file_data_mime( $file_data, $mime_types ); |
| 44 |
|
| 45 |
return _fut_update_file_data_extensions( $file_data, $extensions ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Format the file types when the multiple mime types for a single extension are entered via + icon interface. |
| 50 |
* |
| 51 |
* Same extensions with multiple mime types are merged, and mime types are placed in an array. |
| 52 |
* |
| 53 |
* @since 1.2.0 |
| 54 |
* |
| 55 |
* @param array $custom_types Custom file types, may contain duplicate extensions. |
| 56 |
* |
| 57 |
* @return array |
| 58 |
*/ |
| 59 |
function fut_format_multiple_file_types( array $custom_types ): array { |
| 60 |
|
| 61 |
$result = []; |
| 62 |
$ext_mime = []; |
| 63 |
$ext_desc = []; |
| 64 |
|
| 65 |
foreach ( $custom_types as $types ) { |
| 66 |
if ( ! isset( $ext_mime[ $types['ext'] ] ) ) { |
| 67 |
$ext_mime[ $types['ext'] ] = $types['mime']; |
| 68 |
} else { |
| 69 |
$ext_mime[ $types['ext'] ] = array_merge( (array) $ext_mime[ $types['ext'] ], (array) $types['mime'] ); |
| 70 |
} |
| 71 |
|
| 72 |
// The last description will be used when several mimes for a single extensions are added using + in plugin admin area. |
| 73 |
$ext_desc[ $types['ext'] ] = $types['desc']; |
| 74 |
} |
| 75 |
|
| 76 |
foreach ( $ext_mime as $ext => $mime ) { |
| 77 |
$result[] = [ |
| 78 |
'desc' => $ext_desc[ $ext ], |
| 79 |
'mime' => $mime, |
| 80 |
'ext' => $ext, |
| 81 |
]; |
| 82 |
} |
| 83 |
|
| 84 |
return $result; |
| 85 |
} |
| 86 |
|
| 87 |
if ( function_exists( 'add_action' ) ) { |
| 88 |
/** |
| 89 |
* Notice about the deprecated filter, if it's in use. |
| 90 |
* |
| 91 |
* @since 1.2.0 |
| 92 |
*/ |
| 93 |
add_action( |
| 94 |
'init', |
| 95 |
static function () { |
| 96 |
|
| 97 |
/** |
| 98 |
* Filter value for the notice about the deprecated filter. |
| 99 |
* |
| 100 |
* @since 1.2.0 |
| 101 |
* |
| 102 |
* @param array $deprecated Boolean value about deprecate status. |
| 103 |
* @param string $version Version number. |
| 104 |
* @param mixed $replacement Replacement. |
| 105 |
* @param string $message Message. |
| 106 |
* |
| 107 |
* @deprecated 1.2.0 |
| 108 |
*/ |
| 109 |
apply_filters_deprecated( 'file_upload_types_strict_check', [ true ], '1.2.0', null, 'Please add multiple MIME types for the extension wherever possible!' ); |
| 110 |
} |
| 111 |
); |
| 112 |
} |
| 113 |
|
| 114 |
if ( ! function_exists( '_fut_update_file_data_description' ) ) { |
| 115 |
/** |
| 116 |
* Update file data description. |
| 117 |
* |
| 118 |
* Use internally only. |
| 119 |
* |
| 120 |
* @since 1.3.0 |
| 121 |
* |
| 122 |
* @see fut_format_raw_custom_types |
| 123 |
* |
| 124 |
* @param array $descriptions Descriptions. |
| 125 |
* |
| 126 |
* @return array |
| 127 |
*/ |
| 128 |
function _fut_update_file_data_description( array $descriptions ): array { |
| 129 |
|
| 130 |
$file_data = []; |
| 131 |
|
| 132 |
foreach ( $descriptions as $key => $desc ) { |
| 133 |
$file_data[ $key ]['desc'] = $desc; |
| 134 |
} |
| 135 |
|
| 136 |
return $file_data; |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
if ( ! function_exists( '_fut_update_file_data_mime' ) ) { |
| 141 |
/** |
| 142 |
* Update file data mime types. |
| 143 |
* |
| 144 |
* Use internally only. |
| 145 |
* |
| 146 |
* @since 1.3.0 |
| 147 |
* |
| 148 |
* @see fut_format_raw_custom_types |
| 149 |
* |
| 150 |
* @param array $file_data File data. |
| 151 |
* @param array $mime Mime types. |
| 152 |
* |
| 153 |
* @return array |
| 154 |
*/ |
| 155 |
function _fut_update_file_data_mime( array $file_data, array $mime ): array { |
| 156 |
|
| 157 |
foreach ( $mime as $key => $mime_type ) { |
| 158 |
$file_data[ $key ]['mime'] = strpos( $mime_type, ',' ) === false ? $mime_type : array_filter( array_map( 'trim', explode( ',', $mime_type ) ) ); |
| 159 |
} |
| 160 |
|
| 161 |
return $file_data; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if ( ! function_exists( '_fut_update_file_data_extensions' ) ) { |
| 166 |
/** |
| 167 |
* Update file data extensions. |
| 168 |
* |
| 169 |
* Use internally only. |
| 170 |
* |
| 171 |
* @since 1.3.0 |
| 172 |
* |
| 173 |
* @see fut_format_raw_custom_types |
| 174 |
* |
| 175 |
* @param array $file_data File data. |
| 176 |
* @param array $extensions Extensions. |
| 177 |
* |
| 178 |
* @return array |
| 179 |
*/ |
| 180 |
function _fut_update_file_data_extensions( array $file_data, array $extensions ): array { |
| 181 |
|
| 182 |
foreach ( $extensions as $key => $extension ) { |
| 183 |
$file_data[ $key ]['ext'] = '.' . strtolower( ltrim( $extension, '.' ) ); |
| 184 |
} |
| 185 |
|
| 186 |
return $file_data; |
| 187 |
} |
| 188 |
} |
| 189 |
|