Merge pull request #11 from lbryio/speech-image-publishing
Speech image publishing
This commit is contained in:
commit
f32dc8884d
3 changed files with 79 additions and 142 deletions
|
@ -22,6 +22,7 @@ class LBRY_Speech
|
||||||
if (is_admin()) {
|
if (is_admin()) {
|
||||||
add_action('save_post', array($this, 'upload_media'), 10, 2);
|
add_action('save_post', array($this, 'upload_media'), 10, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Replace the image srcsets
|
// Replace the image srcsets
|
||||||
add_filter('wp_calculate_image_srcset', array($this->parser, 'replace_image_srcset'), 10, 5);
|
add_filter('wp_calculate_image_srcset', array($this->parser, 'replace_image_srcset'), 10, 5);
|
||||||
// Core filter for lots of image source calls
|
// Core filter for lots of image source calls
|
||||||
|
@ -51,8 +52,6 @@ class LBRY_Speech
|
||||||
|
|
||||||
$all_media = $this->find_media($post_id);
|
$all_media = $this->find_media($post_id);
|
||||||
|
|
||||||
// error_log(print_r($all_media, true));
|
|
||||||
|
|
||||||
// IDEA: Notify user if post save time will take a while
|
// IDEA: Notify user if post save time will take a while
|
||||||
if ($all_media) {
|
if ($all_media) {
|
||||||
$requests = array();
|
$requests = array();
|
||||||
|
@ -72,9 +71,6 @@ class LBRY_Speech
|
||||||
if (!empty($speech_channel) && !empty($speech_pw)) {
|
if (!empty($speech_channel) && !empty($speech_pw)) {
|
||||||
$params['channelName'] = '@' . $speech_channel;
|
$params['channelName'] = '@' . $speech_channel;
|
||||||
$params['channelPassword'] = $speech_pw;
|
$params['channelPassword'] = $speech_pw;
|
||||||
|
|
||||||
error_log($params['channelName']);
|
|
||||||
error_log($params['channelPassword']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$ch = $this->build_request('publish', $params);
|
$ch = $this->build_request('publish', $params);
|
||||||
|
@ -123,19 +119,14 @@ class LBRY_Speech
|
||||||
// Update image meta
|
// Update image meta
|
||||||
if ($result && $result->success) {
|
if ($result && $result->success) {
|
||||||
$meta = wp_get_attachment_metadata($media->id);
|
$meta = wp_get_attachment_metadata($media->id);
|
||||||
if ($media->image_size) {
|
|
||||||
$meta['sizes'][$media->image_size][LBRY_SPEECH_ASSET_URL] = $result->data->serveUrl;
|
|
||||||
} else {
|
|
||||||
$meta[LBRY_SPEECH_ASSET_URL] = $result->data->serveUrl;
|
$meta[LBRY_SPEECH_ASSET_URL] = $result->data->serveUrl;
|
||||||
}
|
|
||||||
wp_update_attachment_metadata($media->id, $meta);
|
wp_update_attachment_metadata($media->id, $meta);
|
||||||
} else { // Something unhandled happened here
|
} else { // Something unhandled happened here
|
||||||
throw new \Exception("Unknown Speech Upload issue for asset");
|
throw new \Exception("Unknown Speech Upload issue for asset");
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
$image_size = $media->image_size ? $media->image_size : 'full';
|
error_log('Failed to upload asset with ID ' . $media->id . ' to supplied speech URL.');
|
||||||
error_log('Failed to upload asset with ID ' . $media->id . ' for size ' . $image_size . ' to supplied speech URL.');
|
LBRY()->daemon->logger->log('Speech Upload', 'Failed to upload asset with ID ' . $media->id . ' to supplied speech URL. Message | ' . $e->getMessage());
|
||||||
LBRY()->daemon->logger->log('Speech Upload', 'Failed to upload asset with ID ' . $media->id . ' for size ' . $image_size . ' to supplied speech URL. Message | ' . $e->getMessage());
|
|
||||||
error_log($e->getMessage());
|
error_log($e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,13 +177,13 @@ class LBRY_Speech
|
||||||
// COMBAK: find a way to make this more efficient?
|
// COMBAK: find a way to make this more efficient?
|
||||||
// Create a media object for each image size
|
// Create a media object for each image size
|
||||||
// Get images sizes for this attachment, as not all image sizes implemented
|
// Get images sizes for this attachment, as not all image sizes implemented
|
||||||
$image_sizes = wp_get_attachment_metadata($attachment_id)['sizes'];
|
// $image_sizes = wp_get_attachment_metadata($attachment_id)['sizes'];
|
||||||
|
//
|
||||||
foreach ($image_sizes as $size => $meta) {
|
// foreach ($image_sizes as $size => $meta) {
|
||||||
if (!$this->is_published($meta)) {
|
// if (!$this->is_published($meta)) {
|
||||||
$all_media[] = new LBRY_Speech_Media($attachment_id, array('image_size' => $size));
|
// $all_media[] = new LBRY_Speech_Media($attachment_id, array('image_size' => $size));
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
$videos = $this->parser->scrape_videos($content);
|
$videos = $this->parser->scrape_videos($content);
|
||||||
|
|
|
@ -27,7 +27,7 @@ class LBRY_Speech_Media
|
||||||
|
|
||||||
public $thumbnail;
|
public $thumbnail;
|
||||||
|
|
||||||
public $image_size = false;
|
// public $image_size = false;
|
||||||
|
|
||||||
public function __construct(int $attachment_id, $args = array())
|
public function __construct(int $attachment_id, $args = array())
|
||||||
{
|
{
|
||||||
|
@ -38,7 +38,7 @@ class LBRY_Speech_Media
|
||||||
'license' => null,
|
'license' => null,
|
||||||
'description' => null,
|
'description' => null,
|
||||||
'thumbnail' => null,
|
'thumbnail' => null,
|
||||||
'image_size' => false,
|
// 'image_size' => false,
|
||||||
);
|
);
|
||||||
|
|
||||||
$settings = array_merge($default, array_intersect_key($args, $default));
|
$settings = array_merge($default, array_intersect_key($args, $default));
|
||||||
|
@ -55,24 +55,24 @@ class LBRY_Speech_Media
|
||||||
$path = get_attached_file($this->id);
|
$path = get_attached_file($this->id);
|
||||||
|
|
||||||
// Apply data dependent on whether this is an image 'size' or not
|
// Apply data dependent on whether this is an image 'size' or not
|
||||||
if ($this->image_size) {
|
// if ($this->image_size) {
|
||||||
$meta = wp_get_attachment_metadata($this->id)['sizes'][$this->image_size];
|
// $meta = wp_get_attachment_metadata($this->id)['sizes'][$this->image_size];
|
||||||
$pathinfo = pathinfo($meta['file']);
|
// $pathinfo = pathinfo($meta['file']);
|
||||||
$ext = '.' . $pathinfo['extension'];
|
// $ext = '.' . $pathinfo['extension'];
|
||||||
$new_ext = '-' . $meta['width'] . 'x' . $meta['height'] . $ext;
|
// $new_ext = '-' . $meta['width'] . 'x' . $meta['height'] . $ext;
|
||||||
$path = str_replace($ext, $new_ext, $path);
|
// $path = str_replace($ext, $new_ext, $path);
|
||||||
$filename = $pathinfo['basename'];
|
// $filename = $pathinfo['basename'];
|
||||||
// COMBAK: Probably wont need this underscore check with Daemon V3
|
// // COMBAK: Probably wont need this underscore check with Daemon V3
|
||||||
$this->name = str_replace('_', '-', $pathinfo['filename']);
|
// $this->name = str_replace('_', '-', $pathinfo['filename']);
|
||||||
$this->type = $meta['mime-type'];
|
// $this->type = $meta['mime-type'];
|
||||||
$this->title = $pathinfo['filename'];
|
// $this->title = $pathinfo['filename'];
|
||||||
} else {
|
// } else {
|
||||||
$attachment = get_post($this->id);
|
$attachment = get_post($this->id);
|
||||||
$filename = wp_basename($path);
|
$filename = wp_basename($path);
|
||||||
$this->name = str_replace('_', '-', $attachment->post_name);
|
$this->name = str_replace('_', '-', $attachment->post_name);
|
||||||
$this->type = $attachment->post_mime_type;
|
$this->type = $attachment->post_mime_type;
|
||||||
$this->title = $attachment->post_title;
|
$this->title = $attachment->post_title;
|
||||||
}
|
// }
|
||||||
|
|
||||||
$this->file = new CURLFile($path, $this->type, $filename);
|
$this->file = new CURLFile($path, $this->type, $filename);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,28 +7,23 @@
|
||||||
|
|
||||||
class LBRY_Speech_Parser
|
class LBRY_Speech_Parser
|
||||||
{
|
{
|
||||||
|
// COMBAK: May not need this, as replace_attachment_image_src may cover all use cases
|
||||||
/**
|
/**
|
||||||
* Replace img srcset attributes with Spee.ch urls
|
* Replace img srcset attributes with Spee.ch urls
|
||||||
* Check https://developer.wordpress.org/reference/functions/wp_calculate_image_srcset/ hook for details
|
* Check https://developer.wordpress.org/reference/functions/wp_calculate_image_srcset/ hook for details
|
||||||
*/
|
*/
|
||||||
public function replace_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id)
|
public function replace_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id)
|
||||||
{
|
{
|
||||||
$new_sources = $sources;
|
// If we don't have a speech URL, bail
|
||||||
$sizes = $image_meta['sizes'];
|
if (!LBRY()->speech->is_published($image_meta)) {
|
||||||
$base_image = pathinfo($image_src)['basename'];
|
return;
|
||||||
|
|
||||||
foreach ($sources as $width => $source) {
|
|
||||||
// Check to see if it is using base image first
|
|
||||||
if ($image_src == $source['url'] && key_exists(LBRY_SPEECH_ASSET_URL, $image_meta)) {
|
|
||||||
$new_sources[$width]['url'] = $image_meta[LBRY_SPEECH_ASSET_URL];
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, find the corresponding size
|
$new_sources = $sources;
|
||||||
$speech_url = $this->find_speech_url_by_width($sizes, $width);
|
|
||||||
|
|
||||||
if ($speech_url) {
|
foreach ($sources as $width => $source) {
|
||||||
$new_sources[$width]['url'] = $speech_url;
|
if ($cropped_url = $this->get_speech_crop_url($source['url'], $image_meta)) {
|
||||||
|
$new_sources[$width]['url'] = $cropped_url;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $new_sources;
|
return $new_sources;
|
||||||
|
@ -44,48 +39,16 @@ class LBRY_Speech_Parser
|
||||||
return $image;
|
return $image;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Die if we don't have a speech_asset_url
|
|
||||||
$image_meta = wp_get_attachment_metadata($attachment_id);
|
$image_meta = wp_get_attachment_metadata($attachment_id);
|
||||||
|
|
||||||
|
// Die if we don't have a speech_asset_url
|
||||||
if (!LBRY()->speech->is_published($image_meta)) {
|
if (!LBRY()->speech->is_published($image_meta)) {
|
||||||
return $image;
|
return $image;
|
||||||
}
|
}
|
||||||
|
|
||||||
$new_image = $image;
|
$new_image = $image;
|
||||||
|
$cropped_url = $this->get_speech_crop_url($image[0], $image_meta);
|
||||||
// If the image is the same as the base image, return the base spee.ch url
|
$new_image[0] = $cropped_url;
|
||||||
if (pathinfo($image[0])['basename'] == pathinfo($image_meta['file'])['basename']) {
|
|
||||||
$new_image[0] = $image_meta[LBRY_SPEECH_ASSET_URL];
|
|
||||||
return $new_image;
|
|
||||||
}
|
|
||||||
|
|
||||||
$sizes = $image_meta['sizes'];
|
|
||||||
|
|
||||||
// If we have a given size, then use that immediately
|
|
||||||
if (is_string($size)) {
|
|
||||||
switch ($size) {
|
|
||||||
case 'full':
|
|
||||||
$new_image[0] = $image_meta[LBRY_SPEECH_ASSET_URL];
|
|
||||||
break;
|
|
||||||
case 'post-thumbnail':
|
|
||||||
if (LBRY()->speech->is_published($sizes['thumbnail'])) {
|
|
||||||
$new_image[0] = $sizes['thumbnail'][LBRY_SPEECH_ASSET_URL];
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
if (key_exists($size, $sizes) && LBRY()->speech->is_published($sizes[$size])) {
|
|
||||||
$new_image[0] = $sizes[$size][LBRY_SPEECH_ASSET_URL];
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return $new_image;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, we can find it by the url provided
|
|
||||||
$speech_url = $this->find_speech_url_by_file_url($image_meta, $image[0]);
|
|
||||||
if ($speech_url) {
|
|
||||||
$new_image[0] = $speech_url;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $new_image;
|
return $new_image;
|
||||||
}
|
}
|
||||||
|
@ -123,13 +86,48 @@ class LBRY_Speech_Parser
|
||||||
$id = $this->rigid_attachment_url_to_postid($asset);
|
$id = $this->rigid_attachment_url_to_postid($asset);
|
||||||
$meta = wp_get_attachment_metadata($id);
|
$meta = wp_get_attachment_metadata($id);
|
||||||
|
|
||||||
if ($meta && LBRY()->speech->is_published($meta) && $speech_url = $this->find_speech_url_by_file_url($meta, $asset)) {
|
|
||||||
|
if ($meta && LBRY()->speech->is_published($meta)) {
|
||||||
|
// If its a video, handle accordingly
|
||||||
|
if (!key_exists('file', $meta) && key_exists('mime_type', $meta) && $meta['mime_type'] == 'video/mp4') {
|
||||||
|
$speech_url = $meta[LBRY_SPEECH_ASSET_URL];
|
||||||
|
} else {
|
||||||
|
$speech_url = $this->get_speech_crop_url($asset, $meta);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($speech_url) {
|
||||||
$new_content = str_replace($asset, $speech_url, $new_content);
|
$new_content = str_replace($asset, $speech_url, $new_content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return $new_content;
|
return $new_content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculates the crop url for Spee.ch to deliver responsive images
|
||||||
|
* @param string $image_src The src of the image that needs a spee.ch url
|
||||||
|
* @param array $image_meta The meta for the attachment that needs a spee.ch url
|
||||||
|
* @return string The proper Spee.ch URL, false if none found
|
||||||
|
*/
|
||||||
|
private function get_speech_crop_url($image_src, $image_meta)
|
||||||
|
{
|
||||||
|
$base_url = wp_get_upload_dir()['baseurl'];
|
||||||
|
$image_file_info = pathinfo($image_meta['file']);
|
||||||
|
|
||||||
|
// If this is the base image, just return it as is
|
||||||
|
if ($image_file_info['basename'] == pathinfo($image_src)['basename']) {
|
||||||
|
return $image_meta[LBRY_SPEECH_ASSET_URL];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, find the crop size
|
||||||
|
$comparable_url = $base_url . '/' . $image_file_info['dirname'] . '/' . $image_file_info['filename'];
|
||||||
|
$crop_size = str_replace($comparable_url, '', $image_src);
|
||||||
|
if (preg_match('/-(\d+)x(\d+)\..+/', $crop_size, $matches)) {
|
||||||
|
return $image_meta[LBRY_SPEECH_ASSET_URL] . '?w=' . $matches[1] . '&h=' . $matches[2] . '&t=crop';
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scrapes all image tags from content
|
* Scrapes all image tags from content
|
||||||
|
@ -230,58 +228,6 @@ class LBRY_Speech_Parser
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves a speech_asset_url based sizes meta provided
|
|
||||||
* @param array $sizes Image sizes meta array
|
|
||||||
* @param string $width The width to look for
|
|
||||||
* @return string An asset url if found, false if not
|
|
||||||
*/
|
|
||||||
private function find_speech_url_by_width($sizes, $width)
|
|
||||||
{
|
|
||||||
foreach ($sizes as $key => $size) {
|
|
||||||
if ($size['width'] == $width && key_exists(LBRY_SPEECH_ASSET_URL, $size)) {
|
|
||||||
return $size[LBRY_SPEECH_ASSET_URL];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Retrieves a speech_asset_url based on a passed url
|
|
||||||
* @param array $meta The attachment meta data for the asset
|
|
||||||
* @param string $url The URL of the asset being exchanged
|
|
||||||
* @return string The URL of the Speech Asset
|
|
||||||
*/
|
|
||||||
private function find_speech_url_by_file_url($meta, $url)
|
|
||||||
{
|
|
||||||
// See if this looks like video meta
|
|
||||||
if (!key_exists('file', $meta) && key_exists('mime_type', $meta) && $meta['mime_type'] == 'video/mp4') {
|
|
||||||
return $meta[LBRY_SPEECH_ASSET_URL];
|
|
||||||
}
|
|
||||||
|
|
||||||
$pathinfo = pathinfo($url);
|
|
||||||
$basename = $pathinfo['basename'];
|
|
||||||
|
|
||||||
// Check main file or if $meta is just a url (video) first
|
|
||||||
if (key_exists('file', $meta) && $basename == wp_basename($meta['file'])) {
|
|
||||||
return $meta[LBRY_SPEECH_ASSET_URL];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check to see if we have a meta option here
|
|
||||||
if (key_exists('sizes', $meta)) {
|
|
||||||
// Then check sizes
|
|
||||||
foreach ($meta['sizes'] as $size => $meta) {
|
|
||||||
if ($basename == $meta['file'] && key_exists(LBRY_SPEECH_ASSET_URL, $meta)) {
|
|
||||||
return $meta[LBRY_SPEECH_ASSET_URL];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Couldn't make a match
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks to see if a url is local to this installation
|
* Checks to see if a url is local to this installation
|
||||||
* @param string $url
|
* @param string $url
|
||||||
|
|
Loading…
Reference in a new issue