wppaste
WordPress

Enclosure::get_real_type( bool $find_handler = false ): string|null

Source
wp-includes/SimplePie/src/Enclosure.php:1056
Get the real media type

Description

Often, feeds lie to us, necessitating a bit of deeper inspection. This converts types to their canonical representations based on the file extension

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$find_handlerbooloptional
Internal use only, use get_handler() insteadDefault: false

Return value

string|null
MIME type

Performance profile

How much work a call to Enclosure::get_real_type() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
17–132

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 184.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost Enclosure::get_real_type() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$type === null && $type17–27->get_type(), array_merge()
$type === null && !$type && $extension === null21->get_type(), array_merge(), ->get_extension()
$type !== null && $type21–31->get_type(), strtolower(), array_merge()
$type !== null && !$type && $extension === null25->get_type(), strtolower(), array_merge(), ->get_extension()
$type === null && !$type && $extension !== null27–128->get_type(), array_merge(), ->get_extension(), strtolower()
$type !== null && !$type && $extension !== null31–132->get_type(), strtolower(), array_merge(), ->get_extension(), strtolower()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev18417–13255
8.518417–13255
8.418417–132553 fewer instructions than PHP 8.3
8.318720–13555
8.218720–135551 more instruction than PHP 8.1
8.118620–13655
7.418620–13655

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

  • \SimplePie\Enclosure::get_type()
  • \SimplePie\Enclosure::get_extension()

Source code

    public function get_real_type(bool $find_handler = false)    {        // Mime-types by handler.        $types_flash = ['application/x-shockwave-flash', 'application/futuresplash']; // Flash        $types_fmedia = ['video/flv', 'video/x-flv','flv-application/octet-stream']; // Flash Media Player        $types_quicktime = ['audio/3gpp', 'audio/3gpp2', 'audio/aac', 'audio/x-aac', 'audio/aiff', 'audio/x-aiff', 'audio/mid', 'audio/midi', 'audio/x-midi', 'audio/mp4', 'audio/m4a', 'audio/x-m4a', 'audio/wav', 'audio/x-wav', 'video/3gpp', 'video/3gpp2', 'video/m4v', 'video/x-m4v', 'video/mp4', 'video/mpeg', 'video/x-mpeg', 'video/quicktime', 'video/sd-video']; // QuickTime        $types_wmedia = ['application/asx', 'application/x-mplayer2', 'audio/x-ms-wma', 'audio/x-ms-wax', 'video/x-ms-asf-plugin', 'video/x-ms-asf', 'video/x-ms-wm', 'video/x-ms-wmv', 'video/x-ms-wvx']; // Windows Media        $types_mp3 = ['audio/mp3', 'audio/x-mp3', 'audio/mpeg', 'audio/x-mpeg']; // MP3         $type = $this->get_type();        if ($type !== null) {            $type = strtolower($type);        }         // If we encounter an unsupported mime-type, check the file extension and guess intelligently.        if (!in_array($type, array_merge($types_flash, $types_fmedia, $types_quicktime, $types_wmedia, $types_mp3))) {            $extension = $this->get_extension();            if ($extension === null) {                return null;            }             switch (strtolower($extension)) {                // Audio mime-types                case 'aac':                case 'adts':                    $type = 'audio/acc';                    break;                 case 'aif':                case 'aifc':                case 'aiff':                case 'cdda':                    $type = 'audio/aiff';                    break;                 case 'bwf':                    $type = 'audio/wav';                    break;                 case 'kar':                case 'mid':                case 'midi':                case 'smf':                    $type = 'audio/midi';                    break;                 case 'm4a':                    $type = 'audio/x-m4a';                    break;                 case 'mp3':                case 'swa':                    $type = 'audio/mp3';                    break;                 case 'wav':                    $type = 'audio/wav';                    break;                 case 'wax':                    $type = 'audio/x-ms-wax';                    break;                 case 'wma':                    $type = 'audio/x-ms-wma';                    break;                 case '3gp':                case '3gpp':                    // Video mime-types                    $type = 'video/3gpp';                    break;                 case '3g2':                case '3gp2':                    $type = 'video/3gpp2';                    break;                 case 'asf':                    $type = 'video/x-ms-asf';

Changelog

One change between 6.7.7 and 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.9.7
Return type changed from string to string|null.verified against source

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/SimplePie/src/Enclosure.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.