2018-09-01 01:50:29 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Connects to an spee.ch style server to host assets via the LBRY Protocol
|
|
|
|
*
|
|
|
|
* Visit https://github.com/lbryio/spee.ch for more info
|
|
|
|
*
|
|
|
|
* @package LBRYPress
|
|
|
|
*/
|
|
|
|
|
2018-09-01 03:14:12 +02:00
|
|
|
class LBRY_Speech
|
2018-09-01 01:50:29 +02:00
|
|
|
{
|
2018-09-11 22:10:15 +02:00
|
|
|
/**
|
2018-09-14 02:34:11 +02:00
|
|
|
* HTML Parser
|
|
|
|
* @var LBRY_Speech_Parser
|
2018-09-11 22:10:15 +02:00
|
|
|
*/
|
2018-09-14 02:34:11 +02:00
|
|
|
private $parser = null;
|
2018-09-01 03:14:12 +02:00
|
|
|
|
2018-09-14 02:34:11 +02:00
|
|
|
public function __construct()
|
2018-09-01 03:14:12 +02:00
|
|
|
{
|
2018-10-26 18:46:28 +02:00
|
|
|
add_action('save_post', array($this, 'upload_media'), 10, 2);
|
2018-10-28 19:21:22 +01:00
|
|
|
|
|
|
|
if (!is_admin()) {
|
|
|
|
$this->parser = new LBRY_Speech_Parser();
|
|
|
|
add_filter('wp_calculate_image_srcset', array($this->parser, 'speech_image_srcset'), 10, 5);
|
2018-10-31 07:26:19 +01:00
|
|
|
// Core filter for lots of image source calls
|
|
|
|
add_filter('wp_get_attachment_image_src', array($this->parser, 'replace_attachment_image_src'), 10, 3);
|
2018-10-29 18:10:43 +01:00
|
|
|
add_filter('the_content', array($this->parser, 'replace_urls_with_speech'));
|
2018-10-28 19:21:22 +01:00
|
|
|
}
|
2018-09-01 03:14:12 +02:00
|
|
|
}
|
|
|
|
|
2018-09-14 02:34:11 +02:00
|
|
|
/**
|
|
|
|
* Checks to see if we need to rewrite URLS, does if necessary
|
|
|
|
*/
|
2018-10-26 01:30:24 +02:00
|
|
|
|
2018-09-14 02:34:11 +02:00
|
|
|
public function maybe_rewrite_urls()
|
2018-09-01 03:14:12 +02:00
|
|
|
{
|
2018-09-14 02:34:11 +02:00
|
|
|
// See if we have a Spee.ch URL and if we are on the front-end
|
|
|
|
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
|
|
|
if ($speech_url != '' && !is_admin()) {
|
|
|
|
ob_start(array($this->parser, 'rewrite'));
|
|
|
|
}
|
2018-09-01 03:14:12 +02:00
|
|
|
}
|
2018-09-14 02:42:38 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Uploads assets to the speech server
|
2018-10-26 01:30:24 +02:00
|
|
|
* @param int $post_id The ID of the post to
|
|
|
|
* @return bool True if successful, false if not or if no Speech URL available
|
2018-09-14 02:42:38 +02:00
|
|
|
*/
|
2018-10-26 01:30:24 +02:00
|
|
|
// TODO: set up error reporting
|
2018-10-26 18:46:28 +02:00
|
|
|
public function upload_media($post_id, $post)
|
2018-09-14 02:42:38 +02:00
|
|
|
{
|
2018-10-26 18:46:28 +02:00
|
|
|
// Only check post_type of Post
|
|
|
|
if ('post' !== $post->post_type) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-29 23:51:18 +01:00
|
|
|
// error_log('======================== START =====================');
|
2018-10-27 23:49:44 +02:00
|
|
|
|
2018-09-14 02:42:38 +02:00
|
|
|
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
|
|
|
|
|
|
|
// Die if we don't have a spee.ch url
|
|
|
|
if (!$speech_url || $speech_url === '') {
|
2018-10-26 01:30:24 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-26 08:34:13 +02:00
|
|
|
$all_media = $this->find_media($post_id);
|
2018-10-26 01:30:24 +02:00
|
|
|
|
2018-10-26 02:59:34 +02:00
|
|
|
// IDEA: Notify user if post save time will take a while, may be a concern for request timeouts
|
2018-10-26 08:34:13 +02:00
|
|
|
if ($all_media) {
|
2018-10-28 19:21:22 +01:00
|
|
|
// error_log(print_r($all_media, true));
|
2018-10-29 23:51:18 +01:00
|
|
|
|
|
|
|
$requests = array();
|
|
|
|
|
|
|
|
// Build all the Curl Requests
|
2018-10-26 08:34:13 +02:00
|
|
|
foreach ($all_media as $media) {
|
2018-10-27 23:49:44 +02:00
|
|
|
$params = array(
|
|
|
|
'name' => $media->name,
|
|
|
|
'file' => $media->file,
|
|
|
|
'title' => $media->title,
|
|
|
|
'type' => $media->type
|
|
|
|
);
|
|
|
|
|
2018-10-29 18:10:43 +01:00
|
|
|
// Pull Channel and Password from config file for now
|
|
|
|
// COMBAK: This will change in the future
|
2018-10-27 23:49:44 +02:00
|
|
|
if (LBRY_SPEECH_CHANNEL && LBRY_SPEECH_CHANNEL_PASSWORD) {
|
|
|
|
$params['channelName'] = LBRY_SPEECH_CHANNEL;
|
|
|
|
$params['channelPassword'] = LBRY_SPEECH_CHANNEL_PASSWORD;
|
|
|
|
}
|
|
|
|
|
2018-10-29 23:51:18 +01:00
|
|
|
$ch = $this->build_request('publish', $params);
|
|
|
|
$requests[] = array(
|
|
|
|
'request' => $ch,
|
|
|
|
'media' => $media
|
|
|
|
);
|
|
|
|
}
|
2018-10-28 18:09:22 +01:00
|
|
|
|
2018-10-29 23:51:18 +01:00
|
|
|
// Init the curl multi handle
|
|
|
|
$mh = curl_multi_init();
|
|
|
|
|
|
|
|
// Add each request to the multi handle
|
|
|
|
foreach ($requests as $request) {
|
|
|
|
curl_multi_add_handle($mh, $request['request']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// error_log(print_r($requests, true));
|
|
|
|
|
|
|
|
// Execute all requests simultaneously
|
|
|
|
$running = null;
|
|
|
|
do {
|
|
|
|
curl_multi_exec($mh, $running);
|
|
|
|
} while ($running);
|
|
|
|
|
|
|
|
// Close the handles
|
|
|
|
foreach ($requests as $request) {
|
|
|
|
curl_multi_remove_handle($mh, $request['request']);
|
|
|
|
}
|
|
|
|
curl_multi_close($mh);
|
2018-10-27 23:49:44 +02:00
|
|
|
|
2018-10-29 23:51:18 +01:00
|
|
|
// Run through responses, and upload meta as necessary
|
|
|
|
foreach ($requests as $request) {
|
|
|
|
$result = json_decode(curl_multi_getcontent($request['request']));
|
|
|
|
$media = $request['media'];
|
|
|
|
$response_code = curl_getinfo($request['request'], CURLINFO_RESPONSE_CODE);
|
|
|
|
|
|
|
|
// error_log(print_r($result, true));
|
|
|
|
|
|
|
|
try {
|
|
|
|
// check we got a success code
|
|
|
|
if ($response_code != '200') {
|
|
|
|
if (!empty($result) && !$result->success && $result->message) {
|
|
|
|
throw new \Exception("API Issue with message: " . $result->message);
|
|
|
|
} else {
|
|
|
|
throw new \Exception("Speech URL Connection Issue | Code: " . $response_code, 1);
|
|
|
|
}
|
2018-10-27 23:49:44 +02:00
|
|
|
}
|
2018-10-29 23:51:18 +01:00
|
|
|
|
|
|
|
// Update image meta
|
|
|
|
if ($result && $result->success) {
|
|
|
|
// error_log(print_r($result, true));
|
|
|
|
$meta = wp_get_attachment_metadata($media->id);
|
|
|
|
if ($media->image_size) {
|
|
|
|
$meta['sizes'][$media->image_size]['speech_asset_url'] = $result->data->serveUrl;
|
|
|
|
} else {
|
|
|
|
$meta['speech_asset_url'] = $result->data->serveUrl;
|
|
|
|
}
|
|
|
|
wp_update_attachment_metadata($media->id, $meta);
|
|
|
|
// error_log(print_r($meta, true));
|
|
|
|
} else { // Something unhandled happened here
|
|
|
|
throw new \Exception("Unknown Speech Upload issue for asset");
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$image_size = $media->image_size ? $media->image_size : 'full';
|
|
|
|
error_log('Failed to upload asset with ID ' . $media->id . ' for size ' . $size . ' to supplied speech URL.');
|
|
|
|
error_log($e->getMessage());
|
2018-10-27 23:49:44 +02:00
|
|
|
}
|
2018-10-26 01:30:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all media attached to a post
|
|
|
|
* @param int $post_id The post to search
|
2018-10-26 08:34:13 +02:00
|
|
|
* @return array An array of Speech Media Objects
|
2018-10-26 01:30:24 +02:00
|
|
|
*/
|
2018-10-26 08:34:13 +02:00
|
|
|
protected function find_media($post_id)
|
2018-10-26 01:30:24 +02:00
|
|
|
{
|
2018-10-26 09:43:34 +02:00
|
|
|
$all_media = array();
|
|
|
|
|
2018-10-27 23:49:44 +02:00
|
|
|
|
2018-10-26 08:34:13 +02:00
|
|
|
// Get content and put into a DOMDocument
|
2018-10-27 21:28:20 +02:00
|
|
|
$content = get_post_field('post_content', $post_id);
|
2018-10-26 18:46:28 +02:00
|
|
|
if (!$content) {
|
|
|
|
return $all_media;
|
|
|
|
}
|
2018-10-26 08:34:13 +02:00
|
|
|
|
2018-10-27 23:49:44 +02:00
|
|
|
// Find all images
|
2018-10-27 21:28:20 +02:00
|
|
|
preg_match_all('/<img [^>]+>/', $content, $images);
|
|
|
|
|
|
|
|
// Only MP4 videos for now
|
|
|
|
preg_match_all('/\[video.*mp4=".*".*\]/', $content, $videos);
|
2018-10-26 08:34:13 +02:00
|
|
|
|
2018-10-27 23:49:44 +02:00
|
|
|
// Check to make sure we have results
|
|
|
|
$images = empty($images[0]) ? array() : $images[0];
|
|
|
|
$videos = empty($videos[0]) ? array() : $videos[0];
|
|
|
|
|
2018-10-29 23:51:18 +01:00
|
|
|
// error_log(print_r($images, true));
|
|
|
|
// error_log(print_r($videos, true));
|
2018-10-27 21:28:20 +02:00
|
|
|
|
2018-10-27 23:49:44 +02:00
|
|
|
// TODO: only create media objects if hasn't been uploaded. IE check meta here
|
2018-10-27 21:28:20 +02:00
|
|
|
// Throw each image into a media object
|
2018-10-26 08:34:13 +02:00
|
|
|
foreach ($images as $image) {
|
2018-10-27 23:49:44 +02:00
|
|
|
$attachment_id = null;
|
2018-10-27 21:28:20 +02:00
|
|
|
// Looks for wp image class first, if not, pull id from source
|
2018-10-27 23:49:44 +02:00
|
|
|
if (preg_match('/wp-image-([0-9]+)/i', $image, $class_id)) {
|
2018-10-27 21:28:20 +02:00
|
|
|
$attachment_id = absint($class_id[1]);
|
2018-10-29 23:51:18 +01:00
|
|
|
// error_log('found with wp-image: ' . $attachment_id);
|
2018-10-27 23:49:44 +02:00
|
|
|
} elseif (preg_match('/src="((?:https?:)?\/\/[^"]+)"/', $image, $src) && $this->is_local($src[1])) {
|
2018-10-27 21:28:20 +02:00
|
|
|
$attachment_id = $this->rigid_attachment_url_to_postid($src[1]);
|
2018-10-29 23:51:18 +01:00
|
|
|
// error_log('found with url: ' . $attachment_id);
|
2018-10-27 21:28:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($attachment_id) {
|
2018-10-27 23:49:44 +02:00
|
|
|
// Create main image media object
|
|
|
|
$meta = wp_get_attachment_metadata($attachment_id);
|
|
|
|
|
2018-10-29 23:51:18 +01:00
|
|
|
// error_log(print_r($meta, true));
|
2018-10-29 22:55:26 +01:00
|
|
|
|
2018-10-27 23:49:44 +02:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
}
|
2018-10-26 09:43:34 +02:00
|
|
|
}
|
2018-10-26 01:30:24 +02:00
|
|
|
}
|
2018-10-26 08:34:13 +02:00
|
|
|
|
2018-10-27 21:28:20 +02:00
|
|
|
// Parse video tags based on wordpress shortcode for local embedds
|
2018-10-26 08:34:13 +02:00
|
|
|
foreach ($videos as $video) {
|
2018-10-27 23:49:44 +02:00
|
|
|
$attachment_id = null;
|
|
|
|
if (preg_match('/mp4="((?:https?:)?\/\/[^"]+)"/', $video, $src) && $this->is_local($src[1])) {
|
2018-10-27 21:28:20 +02:00
|
|
|
$attachment_id = $this->rigid_attachment_url_to_postid($src[1]);
|
|
|
|
|
|
|
|
if ($attachment_id) {
|
2018-10-27 23:49:44 +02:00
|
|
|
$meta = wp_get_attachment_metadata($attachment_id);
|
|
|
|
|
|
|
|
if (!$this->is_published($meta)) {
|
|
|
|
$all_media[] = new LBRY_Speech_Media($attachment_id);
|
|
|
|
}
|
2018-10-27 21:28:20 +02:00
|
|
|
}
|
2018-10-26 09:43:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $all_media;
|
|
|
|
}
|
|
|
|
|
2018-10-27 23:49:44 +02:00
|
|
|
/**
|
|
|
|
* Checks to see if a url is local to this installation
|
|
|
|
* @param string $url
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
private function is_local($url)
|
|
|
|
{
|
|
|
|
if (strpos($url, home_url()) !== false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks array to see if a spee.ch url exists
|
|
|
|
*/
|
|
|
|
private function is_published($meta)
|
|
|
|
{
|
|
|
|
if (key_exists('speech_asset_url', $meta) && $meta['speech_asset_url'] !== '') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-26 09:43:34 +02:00
|
|
|
/**
|
2018-10-27 21:28:20 +02:00
|
|
|
* Checks for image crop sizes and filters out query params
|
|
|
|
* Courtesy of this post: http://bordoni.me/get-attachment-id-by-image-url/
|
|
|
|
* @param string $url The url of the attachment you want an ID for
|
|
|
|
* @return int The found post_id
|
2018-10-26 09:43:34 +02:00
|
|
|
*/
|
2018-10-27 21:28:20 +02:00
|
|
|
private function rigid_attachment_url_to_postid($url)
|
2018-10-26 09:43:34 +02:00
|
|
|
{
|
2018-10-27 21:28:20 +02:00
|
|
|
$scrubbed_url = strtok($url, '?'); // Clean up query params first
|
|
|
|
$post_id = attachment_url_to_postid($scrubbed_url);
|
|
|
|
|
|
|
|
if (! $post_id) {
|
|
|
|
$dir = wp_upload_dir();
|
|
|
|
$path = $scrubbed_url;
|
|
|
|
|
|
|
|
if (0 === strpos($path, $dir['baseurl'] . '/')) {
|
|
|
|
$path = substr($path, strlen($dir['baseurl'] . '/'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preg_match('/^(.*)(\-\d*x\d*)(\.\w{1,})/i', $path, $matches)) {
|
|
|
|
$url = $dir['baseurl'] . '/' . $matches[1] . $matches[3];
|
|
|
|
$post_id = attachment_url_to_postid($url);
|
|
|
|
}
|
2018-10-26 08:34:13 +02:00
|
|
|
}
|
|
|
|
|
2018-10-27 21:28:20 +02:00
|
|
|
return (int) $post_id;
|
2018-10-26 01:30:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-10-29 23:51:18 +01:00
|
|
|
* Builds a cURL request to the Speech URL
|
2018-10-26 02:44:08 +02:00
|
|
|
* @param string $method The method to call on the Speech API
|
|
|
|
* @param array $params The Parameters to send the Speech API Call
|
2018-10-29 23:51:18 +01:00
|
|
|
* @return string The cURL object pointer
|
2018-10-26 01:30:24 +02:00
|
|
|
*/
|
2018-10-29 23:51:18 +01:00
|
|
|
private function build_request($method, $params = array())
|
2018-10-26 01:30:24 +02:00
|
|
|
{
|
|
|
|
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
|
|
|
|
|
|
|
// Die if no URL
|
|
|
|
if (!$speech_url) {
|
2018-10-29 23:51:18 +01:00
|
|
|
return false;
|
2018-09-14 02:42:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-26 01:30:24 +02:00
|
|
|
$address = $speech_url . '/api/claim/' . $method;
|
|
|
|
|
|
|
|
// Send it via curl
|
|
|
|
$ch = curl_init();
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $address);
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
|
|
|
|
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
|
|
|
|
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
|
|
|
|
curl_setopt($ch, CURLOPT_HEADER, false);
|
2018-10-24 20:49:46 +02:00
|
|
|
|
2018-10-29 23:51:18 +01:00
|
|
|
return $ch;
|
2018-09-14 02:42:38 +02:00
|
|
|
}
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|