Skip to content

Commit

Permalink
✨ Add support for WordPress.tv thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
MatzeKitt committed Apr 16, 2024
1 parent 1974d25 commit 6b9c0b9
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 0 deletions.
2 changes: 2 additions & 0 deletions inc/thumbnail/class-thumbnail.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use epiphyt\Embed_Privacy\thumbnail\provider\SlideShare;
use epiphyt\Embed_Privacy\thumbnail\provider\Thumbnail_Provider;
use epiphyt\Embed_Privacy\thumbnail\provider\Vimeo;
use epiphyt\Embed_Privacy\thumbnail\provider\WordPress_TV;
use epiphyt\Embed_Privacy\thumbnail\provider\YouTube;
use WP_Post;

Expand All @@ -23,6 +24,7 @@ final class Thumbnail {
public $providers = [
SlideShare::class,
Vimeo::class,
WordPress_TV::class,
YouTube::class,
];

Expand Down
140 changes: 140 additions & 0 deletions inc/thumbnail/provider/class-wordpress-tv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
namespace epiphyt\Embed_Privacy\thumbnail\provider;

use DOMDocument;
use DOMXPath;
use epiphyt\Embed_Privacy\thumbnail\Thumbnail;

/**
* WordPress TV thumbnail implementation.
*
* @author Epiphyt
* @license GPL2
* @package epiphyt\Embed_Privacy
*/
final class WordPress_TV extends Thumbnail_Provider implements Thumbnail_Provider_Interface {
/**
* @inheritDoc
*/
public static $domains = [
'wordpress.tv',
];

/**
* @inheritDoc
*/
public static $name = 'wordpress-tv';

/**
* @inheritDoc
*/
public static function get( $data, $url ) {
if ( ! self::is_provider_embed( $url ) ) {
return;
}

$id = self::get_id( $data->html );

if ( $id ) {
self::save( $id, $url );
}
}

/**
* @inheritDoc
*/
public static function get_id( $content ) {
if ( \str_contains( $content, '/video.wordpress.com/embed/' ) ) {
$extracted = \preg_replace( '/.*video\.wordpress\.com\/embed\//', '', $content ); // phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText
$parts = \explode( '?', $extracted );
$id = isset( $parts[0] ) ? $parts[0] : '';
}
else {
// get ID from meta key by embed URL
$metadata = Thumbnail::get_metadata( self::$name );

foreach ( $metadata as $meta ) {
if ( ! \str_contains( $meta['meta_value'], $content ) ) {
continue;
}

$id = \str_replace(
[
Thumbnail::METADATA_PREFIX . '_' . self::$name . '_',
'_url',
],
'',
$meta['meta_key']
);
}
}

return $id;
}

/**
* @inheritDoc
*/
public static function get_title() {
return \_x( 'WordPress TV', 'embed provider', 'embed-privacy' );
}

/**
* @inheritDoc
*/
public static function save( $id, $url, $thumbnail_url = '' ) {
$post = \get_post();

if ( ! $post ) {
return;
}

require_once ABSPATH . 'wp-admin/includes/file.php';

$directory = Thumbnail::get_directory();
$thumbnail_path = $directory['base_dir'] . '/' . self::$name . '-' . $id . '.jpg';

if ( ! \file_exists( $thumbnail_path ) ) {
$use_errors = \libxml_use_internal_errors( true );
$dom = new DOMDocument();
// download embedded page
$request = \wp_remote_get( $url );

$dom->loadHTML(
\wp_remote_retrieve_body( $request ),
LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD
);
$xpath = new DOMXPath( $dom );
// get thumbnail URL from og:image meta
$thumbnail_url = $xpath->evaluate( '//meta[@property="og:image"]/@content' )->item( 0 )->value;
$file = \download_url( $thumbnail_url );

\libxml_use_internal_errors( $use_errors );

if ( \is_wp_error( $file ) ) {
return;
}

/** @var \WP_Filesystem_Direct $wp_filesystem */
global $wp_filesystem;

// initialize the WP filesystem if not exists
if ( empty( $wp_filesystem ) ) {
\WP_Filesystem();
}

$wp_filesystem->move( $file, $thumbnail_path );
}

\update_post_meta(
$post->ID,
Thumbnail::METADATA_PREFIX . '_' . self::$name . '_' . $id,
self::$name . '-' . $id . '.jpg'
);
\update_post_meta(
$post->ID,
Thumbnail::METADATA_PREFIX . '_' . self::$name . '_' . $id . '_url',
$url
);
}
}

0 comments on commit 6b9c0b9

Please sign in to comment.