Testing timing on various hooks
This commit is contained in:
parent
b2b16bb129
commit
b0e51bb291
1 changed files with 68 additions and 69 deletions
|
@ -8,16 +8,12 @@
|
||||||
class LBRY_Speech_Parser
|
class LBRY_Speech_Parser
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* [speech_image_srcset description]
|
* Replace img srcset attributes with Spee.ch urls
|
||||||
* @param [type] $sources [description]
|
* Check https://developer.wordpress.org/reference/functions/wp_calculate_image_srcset/ hook for details
|
||||||
* @param [type] $size_array [description]
|
|
||||||
* @param [type] $image_src [description]
|
|
||||||
* @param [type] $image_meta [description]
|
|
||||||
* @param [type] $attachment_id [description]
|
|
||||||
*/
|
*/
|
||||||
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)
|
||||||
{
|
{
|
||||||
$time_start = microtime(true);
|
// $time_start = microtime(true);
|
||||||
|
|
||||||
$new_sources = $sources;
|
$new_sources = $sources;
|
||||||
$sizes = $image_meta['sizes'];
|
$sizes = $image_meta['sizes'];
|
||||||
|
@ -30,65 +26,19 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [replace_urls_with_speech description]
|
* Replaces the output of the 'wp_get_attachment_image_src' function
|
||||||
* @param [type] $content [description]
|
* Check https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/ for details
|
||||||
* @return [type] [description]
|
*/
|
||||||
*/
|
|
||||||
public function replace_urls_with_speech($content)
|
|
||||||
{
|
|
||||||
$new_content = $content;
|
|
||||||
|
|
||||||
$assets = array();
|
|
||||||
|
|
||||||
// Get Images
|
|
||||||
$images = $this->scrape_images($content);
|
|
||||||
error_log(print_r($images, true));
|
|
||||||
foreach ($images as $image) {
|
|
||||||
if ($src = $this->get_src_from_image_tag($image)) {
|
|
||||||
$assets[] = $src;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get Videos
|
|
||||||
$videos = $this->scrape_videos($content);
|
|
||||||
error_log(print_r($videos, true));
|
|
||||||
foreach ($videos as $video) {
|
|
||||||
if ($src = $this->get_src_from_video_tag($video)) {
|
|
||||||
$assets[] = $src;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Replace with Speech Urls
|
|
||||||
error_log(print_r($assets, true));
|
|
||||||
foreach ($assets as $asset) {
|
|
||||||
$id = $this->rigid_attachment_url_to_postid($asset);
|
|
||||||
$meta = wp_get_attachment_metadata($id);
|
|
||||||
|
|
||||||
if ($meta && LBRY()->speech->is_published($meta) && $speech_url = $this->find_speech_url_by_file_url($meta, $asset)) {
|
|
||||||
$new_content = str_replace($asset, $speech_url, $new_content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return $new_content;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [replace_attachment_image_src description]
|
|
||||||
* @param [type] $image [description]
|
|
||||||
* @param [type] $attachment_id [description]
|
|
||||||
* @param [type] $size [description]
|
|
||||||
* @return [type] [description]
|
|
||||||
*/
|
|
||||||
public function replace_attachment_image_src($image, $attachment_id, $size)
|
public function replace_attachment_image_src($image, $attachment_id, $size)
|
||||||
{
|
{
|
||||||
|
// $time_start = microtime(true);
|
||||||
if (!$image) {
|
if (!$image) {
|
||||||
return $image;
|
return $image;
|
||||||
}
|
}
|
||||||
|
@ -106,19 +56,22 @@ class LBRY_Speech_Parser
|
||||||
if (is_string($size)) {
|
if (is_string($size)) {
|
||||||
switch ($size) {
|
switch ($size) {
|
||||||
case 'full':
|
case 'full':
|
||||||
$new_image[0] = $image_meta['speech_asset_url'];
|
$new_image[0] = $image_meta['speech_asset_url'];
|
||||||
break;
|
break;
|
||||||
case 'post-thumbnail':
|
case 'post-thumbnail':
|
||||||
if (LBRY()->speech->is_published($sizes['thumbnail'])) {
|
if (LBRY()->speech->is_published($sizes['thumbnail'])) {
|
||||||
$new_image[0] = $sizes['thumbnail']['speech_asset_url'];
|
$new_image[0] = $sizes['thumbnail']['speech_asset_url'];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (key_exists($size, $sizes) && LBRY()->speech->is_published($sizes[$size])) {
|
if (key_exists($size, $sizes) && LBRY()->speech->is_published($sizes[$size])) {
|
||||||
$new_image[0] = $sizes[$size]['speech_asset_url'];
|
$new_image[0] = $sizes[$size]['speech_asset_url'];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
// $time_end = microtime(true);
|
||||||
|
// $time = ($time_end - $time_start) * 1000;
|
||||||
|
// error_log("attachment image source in $time milliseconds");
|
||||||
return $new_image;
|
return $new_image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,6 +84,52 @@ class LBRY_Speech_Parser
|
||||||
return $new_image;
|
return $new_image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scrape content for urls that have a speech equivalent and replace
|
||||||
|
* @param string $content The content to scrape
|
||||||
|
* @return string The content with Spee.ch urls replaced
|
||||||
|
*/
|
||||||
|
// COMBAK: Need to make this a bit faster. Sitting between 100 and 300ms currently
|
||||||
|
public function replace_urls_with_speech($content)
|
||||||
|
{
|
||||||
|
// $time_start = microtime(true);
|
||||||
|
$new_content = $content;
|
||||||
|
|
||||||
|
$assets = array();
|
||||||
|
|
||||||
|
// Get Images
|
||||||
|
$images = $this->scrape_images($content);
|
||||||
|
foreach ($images as $image) {
|
||||||
|
if ($src = $this->get_src_from_image_tag($image)) {
|
||||||
|
$assets[] = $src;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Videos
|
||||||
|
$videos = $this->scrape_videos($content);
|
||||||
|
foreach ($videos as $video) {
|
||||||
|
if ($src = $this->get_src_from_video_tag($video)) {
|
||||||
|
$assets[] = $src;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Replace with Speech Urls
|
||||||
|
foreach ($assets as $asset) {
|
||||||
|
$id = $this->rigid_attachment_url_to_postid($asset);
|
||||||
|
$meta = wp_get_attachment_metadata($id);
|
||||||
|
|
||||||
|
if ($meta && LBRY()->speech->is_published($meta) && $speech_url = $this->find_speech_url_by_file_url($meta, $asset)) {
|
||||||
|
$new_content = str_replace($asset, $speech_url, $new_content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// $time_end = microtime(true);
|
||||||
|
// $time = ($time_end - $time_start) * 1000;
|
||||||
|
// error_log("replace content urls in $time milliseconds");
|
||||||
|
return $new_content;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Scrapes all image tags from content
|
* Scrapes all image tags from content
|
||||||
* @param string $content The content to scrape
|
* @param string $content The content to scrape
|
||||||
|
|
Loading…
Reference in a new issue