2018-09-01 01:50:29 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Parses post markup in order to use specified spee.ch url for assets
|
|
|
|
*
|
|
|
|
* @package LBRYPress
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LBRY_Speech_Parser
|
|
|
|
{
|
2018-10-28 19:21:22 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-10-29 18:10:43 +01:00
|
|
|
/**
|
|
|
|
* [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]
|
|
|
|
*/
|
2018-10-28 19:21:22 +01:00
|
|
|
public function speech_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id)
|
2018-10-24 20:49:46 +02:00
|
|
|
{
|
2018-10-28 19:21:22 +01:00
|
|
|
$time_start = microtime(true);
|
|
|
|
|
|
|
|
$new_sources = $sources;
|
|
|
|
$sizes = $image_meta['sizes'];
|
|
|
|
|
2018-10-31 07:26:19 +01:00
|
|
|
// error_log(print_r($image_meta, true));
|
2018-10-29 22:55:26 +01:00
|
|
|
|
2018-10-28 19:21:22 +01:00
|
|
|
foreach ($sources as $width => $source) {
|
2018-10-31 07:26:19 +01:00
|
|
|
$speech_url = $this->find_speech_url_by_width($sizes, $width);
|
2018-10-28 19:21:22 +01:00
|
|
|
|
|
|
|
if ($speech_url) {
|
|
|
|
$new_sources[$width]['url'] = $speech_url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$time_end = microtime(true);
|
|
|
|
|
|
|
|
$time = ($time_end - $time_start) * 1000;
|
2018-10-31 07:26:19 +01:00
|
|
|
// error_log("srcset in $time milliseconds");
|
2018-10-28 19:21:22 +01:00
|
|
|
return $new_sources;
|
2018-10-24 20:49:46 +02:00
|
|
|
}
|
|
|
|
|
2018-10-29 18:10:43 +01:00
|
|
|
public function replace_urls_with_speech($content)
|
|
|
|
{
|
2018-10-29 22:55:26 +01:00
|
|
|
// // 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]);
|
|
|
|
// }
|
|
|
|
//
|
2018-10-31 07:26:19 +01:00
|
|
|
// // // Look for size class, if not continue
|
|
|
|
// // if (!preg_match('/ size-([\w-_]+) /i', $image, $size_class)) {
|
|
|
|
// // continue;
|
|
|
|
// // }
|
2018-10-29 22:55:26 +01:00
|
|
|
//
|
|
|
|
// 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));
|
2018-10-29 18:10:43 +01:00
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2018-10-31 07:26:19 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-10-29 18:10:43 +01:00
|
|
|
/**
|
|
|
|
* [find_speech_url description]
|
|
|
|
* @param [type] $sizes [description]
|
|
|
|
* @param [type] $width [description]
|
|
|
|
* @return [type] [description]
|
|
|
|
*/
|
2018-10-31 07:26:19 +01:00
|
|
|
private function find_speech_url_by_width($sizes, $width)
|
2018-09-01 01:50:29 +02:00
|
|
|
{
|
2018-10-28 19:21:22 +01:00
|
|
|
foreach ($sizes as $key => $size) {
|
|
|
|
if ($size['width'] == $width && key_exists('speech_asset_url', $size)) {
|
|
|
|
return $size['speech_asset_url'];
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 02:34:11 +02:00
|
|
|
|
2018-10-28 19:21:22 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-09-14 02:34:11 +02:00
|
|
|
|
2018-10-31 07:26:19 +01:00
|
|
|
private function find_speech_url_by_file_name($sizes, $url)
|
|
|
|
{
|
|
|
|
for
|
|
|
|
}
|
|
|
|
|
2018-10-29 18:10:43 +01:00
|
|
|
private function microtime_float()
|
2018-10-28 19:21:22 +01:00
|
|
|
{
|
|
|
|
list($usec, $sec) = explode(" ", microtime());
|
|
|
|
return ((float)$usec + (float)$sec);
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|
|
|
|
}
|