generate thumbnails for oembed able URL documents
This commit is contained in:
parent
658e98c7d1
commit
8114bd9c99
|
@ -78,4 +78,27 @@ class Embed {
|
|||
return $pdf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the thumbnail URL, if provided, for a given URL
|
||||
*
|
||||
* @param $string $url the URL for the content
|
||||
* @return string|null The thumbnail URL or null on failure
|
||||
*/
|
||||
public function oembed_get_thumbnail($url) {
|
||||
|
||||
add_filter( 'oembed_dataparse', ['\Tainacan\Embed', 'oembed_get_thumbnail_filter'], 10, 3);
|
||||
return wp_oembed_get($url);
|
||||
remove_filter( 'oembed_dataparse', ['\Tainacan\Embed', 'oembed_get_thumbnail_filter']);
|
||||
|
||||
}
|
||||
public static function oembed_get_thumbnail_filter($return, $data, $url) {
|
||||
|
||||
if ( isset($data->thumbnail_url) ) {
|
||||
return $data->thumbnail_url;
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
namespace Tainacan;
|
||||
|
||||
/**
|
||||
* Class withe helpful methods to handle media in Tainacan
|
||||
*/
|
||||
class Media {
|
||||
|
||||
private static $instance = null;
|
||||
|
||||
public static function get_instance() {
|
||||
if(!isset(self::$instance)) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
protected function __construct() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert an attachment from an URL address.
|
||||
*
|
||||
* @param String $url
|
||||
* @param Int $post_id (optional) the post this attachement should be attached to. empty for none
|
||||
* @return Int|false Attachment ID. False on failure
|
||||
*/
|
||||
public function insert_attachment_from_url($url, $post_id = null) {
|
||||
if( !class_exists( '\WP_Http' ) )
|
||||
include_once( ABSPATH . WPINC . '/class-http.php' );
|
||||
|
||||
$http = new \WP_Http();
|
||||
|
||||
$response = $http->request( $url );
|
||||
|
||||
if( !is_array($response) || !isset($response['response']) || $response['response']['code'] != 200 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->insert_attachment_from_blob($response['body'], basename($url), $post_id);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert an attachment from an URL address.
|
||||
*
|
||||
* @param blob $blob bitstream of the attachment
|
||||
* @param String $filename The filename that will be created
|
||||
* @param Int $post_id (optional) the post this attachement should be attached to. empty for none
|
||||
* @return Int|false Attachment ID. False on failure
|
||||
*/
|
||||
public function insert_attachment_from_blob($blob, $filename, $post_id = null) {
|
||||
|
||||
$upload = wp_upload_bits( $filename, null, $blob );
|
||||
if( !empty( $upload['error'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$file_path = $upload['file'];
|
||||
$file_name = basename( $file_path );
|
||||
$file_type = wp_check_filetype( $file_name, null );
|
||||
$attachment_title = sanitize_file_name( pathinfo( $file_name, PATHINFO_FILENAME ) );
|
||||
$wp_upload_dir = wp_upload_dir();
|
||||
|
||||
$post_info = array(
|
||||
'guid' => $wp_upload_dir['url'] . '/' . $file_name,
|
||||
'post_mime_type' => $file_type['type'],
|
||||
'post_title' => $attachment_title,
|
||||
'post_content' => '',
|
||||
'post_status' => 'inherit',
|
||||
);
|
||||
|
||||
// Create the attachment
|
||||
$attach_id = wp_insert_attachment( $post_info, $file_path, $post_id );
|
||||
|
||||
// Include image.php
|
||||
require_once( ABSPATH . 'wp-admin/includes/image.php' );
|
||||
|
||||
// Define attachment metadata
|
||||
$attach_data = wp_generate_attachment_metadata( $attach_id, $file_path );
|
||||
|
||||
// Assign metadata to attachment
|
||||
wp_update_attachment_metadata( $attach_id, $attach_data );
|
||||
|
||||
return $attach_id;
|
||||
}
|
||||
|
||||
}
|
|
@ -391,6 +391,24 @@ class Items extends Repository {
|
|||
if ( wp_attachment_is_image( $item->get_document() ) ) {
|
||||
return $item->get_document();
|
||||
}
|
||||
} elseif ( $item->get_document_type() == 'url' ) {
|
||||
|
||||
$TainacanEmbed = \Tainacan\Embed::get_instance();
|
||||
if ( $thumb_url = $TainacanEmbed->oembed_get_thumbnail( $item->get_document() ) ) {
|
||||
$meta_key = '_' . $thumb_url . '__thumb';
|
||||
|
||||
$existing_thumb = get_post_meta($item->get_id(), $meta_key, true);
|
||||
|
||||
if ( is_numeric( $existing_thumb ) ) {
|
||||
return $existing_thumb;
|
||||
} else {
|
||||
$TainacanMedia = \Tainacan\Media::get_instance();
|
||||
$thumb_id = $TainacanMedia->insert_attachment_from_url($thumb_url, $item->get_id());
|
||||
update_post_meta($item->get_id(), $meta_key, $thumb_id);
|
||||
return $thumb_id;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $thumb_id;
|
||||
|
|
Loading…
Reference in New Issue