| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy\thumbnail\provider; |
| 3 |
|
| 4 |
use DOMDocument; |
| 5 |
use DOMXPath; |
| 6 |
use epiphyt\Embed_Privacy\thumbnail\Thumbnail; |
| 7 |
|
| 8 |
/** |
| 9 |
* WordPress TV thumbnail implementation. |
| 10 |
* |
| 11 |
* @author Epiphyt |
| 12 |
* @license GPL2 |
| 13 |
* @package epiphyt\Embed_Privacy |
| 14 |
*/ |
| 15 |
final class WordPress_TV extends Thumbnail_Provider implements Thumbnail_Provider_Interface { |
| 16 |
/** |
| 17 |
* @var string[] List of valid domains for the thumbnail provider |
| 18 |
*/ |
| 19 |
public static $domains = [ |
| 20 |
'wordpress.tv', |
| 21 |
]; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var string Thumbnail provider name |
| 25 |
*/ |
| 26 |
public static $name = 'wordpress-tv'; |
| 27 |
|
| 28 |
/** |
| 29 |
* {@inheritDoc} |
| 30 |
*/ |
| 31 |
public static function get( $data, $url ) { |
| 32 |
if ( ! self::is_provider_embed( $url ) ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
$id = self::get_id( $data->html ); |
| 37 |
|
| 38 |
if ( $id ) { |
| 39 |
self::save( $id, $url ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* {@inheritDoc} |
| 45 |
*/ |
| 46 |
public static function get_id( $content ) { |
| 47 |
if ( \str_contains( $content, '/video.wordpress.com/embed/' ) ) { |
| 48 |
$extracted = \preg_replace( '/.*video\.wordpress\.com\/embed\//', '', $content ); // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText |
| 49 |
$parts = \explode( '?', $extracted ); |
| 50 |
$id = isset( $parts[0] ) ? $parts[0] : ''; |
| 51 |
} |
| 52 |
else { |
| 53 |
// get ID from meta key by embed URL |
| 54 |
$metadata = Thumbnail::get_metadata( self::$name ); |
| 55 |
|
| 56 |
foreach ( $metadata as $meta ) { |
| 57 |
if ( ! \str_contains( $meta['meta_value'], $content ) ) { |
| 58 |
continue; |
| 59 |
} |
| 60 |
|
| 61 |
$id = \str_replace( |
| 62 |
[ |
| 63 |
Thumbnail::METADATA_PREFIX . '_' . self::$name . '_', |
| 64 |
'_url', |
| 65 |
], |
| 66 |
'', |
| 67 |
$meta['meta_key'] |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return $id; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* {@inheritDoc} |
| 77 |
*/ |
| 78 |
public static function get_title() { |
| 79 |
return \_x( 'WordPress TV', 'embed provider', 'embed-privacy' ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* {@inheritDoc} |
| 84 |
*/ |
| 85 |
public static function save( $id, $url, $thumbnail_url = '' ) { |
| 86 |
$post = \get_post(); |
| 87 |
|
| 88 |
if ( ! $post ) { |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
require_once \ABSPATH . 'wp-admin/includes/file.php'; |
| 93 |
|
| 94 |
$directory = Thumbnail::get_directory(); |
| 95 |
$thumbnail_path = $directory['base_dir'] . '/' . self::$name . '-' . $id . '.jpg'; |
| 96 |
|
| 97 |
if ( ! \file_exists( $thumbnail_path ) ) { |
| 98 |
$use_errors = \libxml_use_internal_errors( true ); |
| 99 |
$dom = new DOMDocument(); |
| 100 |
// download embedded page |
| 101 |
$request = \wp_remote_get( $url ); |
| 102 |
|
| 103 |
$dom->loadHTML( |
| 104 |
\wp_remote_retrieve_body( $request ), |
| 105 |
\LIBXML_HTML_NOIMPLIED | \LIBXML_HTML_NODEFDTD |
| 106 |
); |
| 107 |
$xpath = new DOMXPath( $dom ); |
| 108 |
// get thumbnail URL from og:image meta |
| 109 |
$thumbnail_url = $xpath->evaluate( '//meta[@property="og:image"]/@content' )->item( 0 )->value; |
| 110 |
$file = \download_url( $thumbnail_url ); |
| 111 |
|
| 112 |
\libxml_use_internal_errors( $use_errors ); |
| 113 |
|
| 114 |
if ( \is_wp_error( $file ) ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
/** @var \WP_Filesystem_Direct $wp_filesystem */ |
| 119 |
global $wp_filesystem; |
| 120 |
|
| 121 |
// initialize the WP filesystem if not exists |
| 122 |
if ( empty( $wp_filesystem ) ) { |
| 123 |
\WP_Filesystem(); |
| 124 |
} |
| 125 |
|
| 126 |
$wp_filesystem->move( $file, $thumbnail_path ); |
| 127 |
} |
| 128 |
|
| 129 |
\update_post_meta( |
| 130 |
$post->ID, |
| 131 |
Thumbnail::METADATA_PREFIX . '_' . self::$name . '_' . $id, |
| 132 |
self::$name . '-' . $id . '.jpg' |
| 133 |
); |
| 134 |
\update_post_meta( $post->ID, Thumbnail::METADATA_PREFIX . '_' . self::$name . '_' . $id . '_url', $url ); |
| 135 |
} |
| 136 |
} |
| 137 |
|