Fixed bug with srcset parser missing base images

This commit is contained in:
Paul Kirby 2018-11-08 18:29:08 -06:00
parent 334af6183a
commit 4dbf6b7f23
3 changed files with 22 additions and 10 deletions

View file

@ -22,6 +22,8 @@ class LBRY_Network_Publisher
// NOTE: This is currently sitting at about 150ms, mostly the post parsing
public function publish($post, $channel)
{
// TODO: Handle unnatributed channel
// Leave if nothing to publish to
if (!$channel) {
return;

View file

@ -51,6 +51,8 @@ class LBRY_Speech
$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
if ($all_media) {
$requests = array();
@ -144,11 +146,7 @@ class LBRY_Speech
{
$all_media = array();
// Get content and put into a DOMDocument
$content = get_post_field('post_content', $post_id);
if (!$content) {
return $all_media;
}
$images = $this->parser->scrape_images($content);
@ -161,6 +159,7 @@ class LBRY_Speech
}
}
// Don't forget the featured image
error_log($post_id);
if ($featured_id = get_post_thumbnail_id($post_id)) {
$image_ids = array_merge($image_ids, array($featured_id));
}

View file

@ -13,22 +13,24 @@ class LBRY_Speech_Parser
*/
public function replace_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id)
{
// $time_start = microtime(true);
$new_sources = $sources;
$sizes = $image_meta['sizes'];
$base_image = pathinfo($image_src)['basename'];
foreach ($sources as $width => $source) {
// Check to see if it is using base image first
if ($image_src == $source['url'] && key_exists('speech_asset_url', $image_meta)) {
$new_sources[$width]['url'] = $image_meta['speech_asset_url'];
continue;
}
// Otherwise, find the corresponding size
$speech_url = $this->find_speech_url_by_width($sizes, $width);
if ($speech_url) {
$new_sources[$width]['url'] = $speech_url;
}
}
// $time_end = microtime(true);
// $time = ($time_end - $time_start) * 1000;
// error_log("srcset in $time milliseconds");
return $new_sources;
}
@ -137,6 +139,10 @@ class LBRY_Speech_Parser
*/
public function scrape_images($content)
{
// Return empty array if no images
if (!$content) {
return array();
}
// Find all images
preg_match_all('/<img [^>]+>/', $content, $images);
@ -153,6 +159,11 @@ class LBRY_Speech_Parser
*/
public function scrape_videos($content)
{
// Return empty array if no videos
if (!$content) {
return array();
}
// Only MP4 videos for now
preg_match_all('/\[video.*mp4=".*".*\]/', $content, $videos);
$videos = empty($videos[0]) ? array() : $videos[0];