Working on rewriting image source urls via wp_get_attachment_image_src filter

This commit is contained in:
Paul Kirby 2018-10-31 01:26:19 -05:00
parent ed118f0857
commit b758242410
2 changed files with 41 additions and 7 deletions

View file

@ -22,6 +22,8 @@ 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);
// Core filter for lots of image source calls
add_filter('wp_get_attachment_image_src', array($this->parser, 'replace_attachment_image_src'), 10, 3);
add_filter('the_content', array($this->parser, 'replace_urls_with_speech')); add_filter('the_content', array($this->parser, 'replace_urls_with_speech'));
} }
} }

View file

@ -26,10 +26,10 @@ class LBRY_Speech_Parser
$new_sources = $sources; $new_sources = $sources;
$sizes = $image_meta['sizes']; $sizes = $image_meta['sizes'];
error_log(print_r($image_meta, true)); // error_log(print_r($image_meta, true));
foreach ($sources as $width => $source) { foreach ($sources as $width => $source) {
$speech_url = $this->find_speech_url($sizes, $width); $speech_url = $this->find_speech_url_by_width($sizes, $width);
if ($speech_url) { if ($speech_url) {
$new_sources[$width]['url'] = $speech_url; $new_sources[$width]['url'] = $speech_url;
@ -39,7 +39,7 @@ class LBRY_Speech_Parser
$time_end = microtime(true); $time_end = microtime(true);
$time = ($time_end - $time_start) * 1000; $time = ($time_end - $time_start) * 1000;
error_log("srcset in $time milliseconds"); // error_log("srcset in $time milliseconds");
return $new_sources; return $new_sources;
} }
@ -60,9 +60,10 @@ class LBRY_Speech_Parser
// $attachment_id = $this->rigid_attachment_url_to_postid($src[1]); // $attachment_id = $this->rigid_attachment_url_to_postid($src[1]);
// } // }
// //
// // Look for size class // // // Look for size class, if not continue
// if (!preg_match('/wp-image-([0-9]+)/i', $image, $class_id)) { // // if (!preg_match('/ size-([\w-_]+) /i', $image, $size_class)) {
// } // // continue;
// // }
// //
// if ($attachment_id) { // if ($attachment_id) {
// // Create main image media object // // Create main image media object
@ -94,13 +95,39 @@ class LBRY_Speech_Parser
return $content; return $content;
} }
public function replace_attachment_image_src($image, $attachment_id, $size)
{
// TODO: Need to come back to this.
// if (!$image) {
// return;
// }
//
// $new_image = $image;
// $sizes = $image_meta['sizes'];
//
// // If we have a given size, then use that immediately
// if (is_string($size)) {
//
// if ($size == 'full') {
//
// }
// $new_image[0] = $sizes[$size]['speech_asset_url'];
// return $image;
// }
//
// // Otherwise, we can find it by the url provided
// $new_image[0] = $this->find_speech_url_by_file_url($sizes, $image[0]);
return $image;
}
/** /**
* [find_speech_url description] * [find_speech_url description]
* @param [type] $sizes [description] * @param [type] $sizes [description]
* @param [type] $width [description] * @param [type] $width [description]
* @return [type] [description] * @return [type] [description]
*/ */
private function find_speech_url($sizes, $width) private function find_speech_url_by_width($sizes, $width)
{ {
foreach ($sizes as $key => $size) { foreach ($sizes as $key => $size) {
if ($size['width'] == $width && key_exists('speech_asset_url', $size)) { if ($size['width'] == $width && key_exists('speech_asset_url', $size)) {
@ -111,6 +138,11 @@ class LBRY_Speech_Parser
return false; return false;
} }
private function find_speech_url_by_file_name($sizes, $url)
{
for
}
private function microtime_float() private function microtime_float()
{ {
list($usec, $sec) = explode(" ", microtime()); list($usec, $sec) = explode(" ", microtime());