| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy\handler; |
| 3 |
|
| 4 |
use DOMDocument; |
| 5 |
|
| 6 |
/** |
| 7 |
* oEmbed handler. |
| 8 |
* |
| 9 |
* @author Epiphyt |
| 10 |
* @license GPL2 |
| 11 |
* @package epiphyt\Embed_Privacy |
| 12 |
* @since 1.10.0 |
| 13 |
*/ |
| 14 |
final class Oembed { |
| 15 |
/** |
| 16 |
* Get the dimensions of an oEmbed. |
| 17 |
* |
| 18 |
* @param string $content The content to get the title of |
| 19 |
* @return array The dimensions or an empty array |
| 20 |
*/ |
| 21 |
public static function get_dimensions( $content ) { |
| 22 |
$use_error = \libxml_use_internal_errors( true ); |
| 23 |
$dom = new DOMDocument(); |
| 24 |
$dom->loadHTML( |
| 25 |
'<html><meta charset="utf-8">' . $content . '</html>', |
| 26 |
\LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD |
| 27 |
); |
| 28 |
\libxml_use_internal_errors( $use_error ); |
| 29 |
|
| 30 |
foreach ( [ 'embed', 'iframe', 'img', 'object' ] as $tag ) { |
| 31 |
/** @var \DOMElement $element */ |
| 32 |
foreach ( $dom->getElementsByTagName( $tag ) as $element ) { |
| 33 |
$height = $element->getAttribute( 'height' ); |
| 34 |
$width = $element->getAttribute( 'width' ); |
| 35 |
|
| 36 |
if ( $height && $width ) { |
| 37 |
return [ |
| 38 |
'height' => $height, |
| 39 |
'width' => $width, |
| 40 |
]; |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
return []; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Get an oEmbed title by its title attribute. |
| 50 |
* |
| 51 |
* @param string $content The content to get the title of |
| 52 |
* @return string The title or an empty string |
| 53 |
*/ |
| 54 |
public static function get_title( $content ) { |
| 55 |
$use_error = \libxml_use_internal_errors( true ); |
| 56 |
$dom = new DOMDocument(); |
| 57 |
$dom->loadHTML( |
| 58 |
'<html><meta charset="utf-8">' . $content . '</html>', |
| 59 |
\LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD |
| 60 |
); |
| 61 |
\libxml_use_internal_errors( $use_error ); |
| 62 |
|
| 63 |
foreach ( [ 'embed', 'iframe', 'object' ] as $tag ) { |
| 64 |
/** @var \DOMElement $element */ |
| 65 |
foreach ( $dom->getElementsByTagName( $tag ) as $element ) { |
| 66 |
$title = $element->getAttribute( 'title' ); |
| 67 |
|
| 68 |
if ( $title ) { |
| 69 |
return $title; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return ''; |
| 75 |
} |
| 76 |
} |
| 77 |
|