Working on rewriting base image sources

This commit is contained in:
Paul Kirby 2018-10-29 12:10:43 -05:00
parent 24efeecef8
commit f884a7b674
2 changed files with 72 additions and 3 deletions

View file

@ -22,6 +22,7 @@ class LBRY_Speech
if (!is_admin()) { if (!is_admin()) {
$this->parser = new LBRY_Speech_Parser(); $this->parser = new LBRY_Speech_Parser();
add_filter('wp_calculate_image_srcset', array($this->parser, 'speech_image_srcset'), 10, 5); add_filter('wp_calculate_image_srcset', array($this->parser, 'speech_image_srcset'), 10, 5);
add_filter('the_content', array($this->parser, 'replace_urls_with_speech'));
} }
} }
@ -73,6 +74,8 @@ class LBRY_Speech
'type' => $media->type 'type' => $media->type
); );
// Pull Channel and Password from config file for now
// COMBAK: This will change in the future
if (LBRY_SPEECH_CHANNEL && LBRY_SPEECH_CHANNEL_PASSWORD) { if (LBRY_SPEECH_CHANNEL && LBRY_SPEECH_CHANNEL_PASSWORD) {
$params['channelName'] = LBRY_SPEECH_CHANNEL; $params['channelName'] = LBRY_SPEECH_CHANNEL;
$params['channelPassword'] = LBRY_SPEECH_CHANNEL_PASSWORD; $params['channelPassword'] = LBRY_SPEECH_CHANNEL_PASSWORD;
@ -97,7 +100,7 @@ class LBRY_Speech
$meta['speech_asset_url'] = $result->data->serveUrl; $meta['speech_asset_url'] = $result->data->serveUrl;
} }
wp_update_attachment_metadata($media->id, $meta); wp_update_attachment_metadata($media->id, $meta);
// error_log(print_r($meta, true)); error_log(print_r($meta, true));
} }
} }
} }

View file

@ -11,7 +11,14 @@ class LBRY_Speech_Parser
{ {
} }
/**
* [speech_image_srcset description]
* @param [type] $sources [description]
* @param [type] $size_array [description]
* @param [type] $image_src [description]
* @param [type] $image_meta [description]
* @param [type] $attachment_id [description]
*/
public function speech_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) public function speech_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id)
{ {
$time_start = microtime(true); $time_start = microtime(true);
@ -34,6 +41,63 @@ class LBRY_Speech_Parser
return $new_sources; return $new_sources;
} }
public function replace_urls_with_speech($content)
{
// Find all images
preg_match_all('/<img [^>]+>/', $content, $images);
// Check to make sure we have results
$images = empty($images[0]) ? array() : $images[0];
foreach ($images as $image) {
$attachment_id = null;
// Looks for wp image class first, if not, pull id from source
if (preg_match('/wp-image-([0-9]+)/i', $image, $class_id)) {
$attachment_id = absint($class_id[1]);
} elseif (preg_match('/src="((?:https?:)?\/\/[^"]+)"/', $image, $src) && $this->is_local($src[1])) {
$attachment_id = $this->rigid_attachment_url_to_postid($src[1]);
}
// Look for size class
if (!preg_match('/wp-image-([0-9]+)/i', $image, $class_id)) {
}
if ($attachment_id) {
// Create main image media object
$meta = wp_get_attachment_metadata($attachment_id);
// If we don't have meta, get out because none of this will work
if (!$meta) {
break;
}
if (!$this->is_published($meta)) {
$all_media[] = new LBRY_Speech_Media($attachment_id);
}
// COMBAK: find a way to make this more efficient?
// Create a media object for each image size
// Get images sizes for this attachment, as not all image sizes implemented
$image_sizes = wp_get_attachment_metadata($attachment_id)['sizes'];
foreach ($image_sizes as $size => $meta) {
if (!$this->is_published($meta)) {
$all_media[] = new LBRY_Speech_Media($attachment_id, array('image_size' => $size));
}
}
}
}
}
error_log(print_r($content, true));
return $content;
}
/**
* [find_speech_url description]
* @param [type] $sizes [description]
* @param [type] $width [description]
* @return [type] [description]
*/
private function find_speech_url($sizes, $width) private function find_speech_url($sizes, $width)
{ {
foreach ($sizes as $key => $size) { foreach ($sizes as $key => $size) {
@ -45,7 +109,9 @@ class LBRY_Speech_Parser
return false; return false;
} }
public function microtime_float()
private function microtime_float()
{ {
list($usec, $sec) = explode(" ", microtime()); list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec); return ((float)$usec + (float)$sec);