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-09-14 02:34:11 +02:00
|
|
|
$this->parser = new LBRY_Speech_Parser();
|
2018-10-26 01:30:24 +02:00
|
|
|
add_action('save_post', array($this, 'upload_attachments'));
|
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
|
|
|
|
public function upload_attachments($post_id)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$attachments = $this->find_attachments($post_id);
|
|
|
|
|
|
|
|
if ($attachments) {
|
|
|
|
foreach ($attachments as $attachment) {
|
|
|
|
error_log(print_r($attachment, true));
|
|
|
|
// TODO: set post meta to see if already uploaded
|
|
|
|
|
2018-10-26 02:44:08 +02:00
|
|
|
$file_url = get_attached_file($attachment->ID);
|
|
|
|
$cfile = new CURLFile($file_url, $attachment->post_mime_type, $attachment->post_name . '.jpg');
|
2018-10-26 01:30:24 +02:00
|
|
|
|
|
|
|
$params = array(
|
|
|
|
'name' => $attachment->post_name,
|
|
|
|
'file' => $cfile,
|
2018-10-26 02:44:08 +02:00
|
|
|
'title' => $attachment->post_title,
|
|
|
|
'type' => $attachment->post_mime_type
|
2018-10-26 01:30:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->request('publish', $params);
|
|
|
|
error_log(print_r($result, true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds all media attached to a post
|
|
|
|
* @param int $post_id The post to search
|
|
|
|
* @return array An array of WP_Post Objects, or false if none found
|
|
|
|
*/
|
|
|
|
protected function find_attachments($post_id)
|
|
|
|
{
|
|
|
|
// Get all attachments
|
|
|
|
$attachments = get_posts(array(
|
|
|
|
'post_type' => 'attachment',
|
|
|
|
'numberposts' => -1,
|
|
|
|
'post_status' => 'any',
|
|
|
|
'post_parent' => $post_id,
|
|
|
|
));
|
|
|
|
|
|
|
|
// Return attachments arary
|
|
|
|
if ($attachments) {
|
|
|
|
return $attachments;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sends 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-26 01:30:24 +02:00
|
|
|
* @return string The cURL response
|
|
|
|
*/
|
|
|
|
private function request($method, $params = array())
|
|
|
|
{
|
|
|
|
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
|
|
|
|
|
|
|
|
// Die if no URL
|
|
|
|
if (!$speech_url) {
|
2018-09-14 02:42:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-10-26 01:30:24 +02:00
|
|
|
$address = $speech_url . '/api/claim/' . $method;
|
|
|
|
|
|
|
|
error_log(print_r($params, true));
|
|
|
|
|
|
|
|
// 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-26 01:30:24 +02:00
|
|
|
$result = curl_exec($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
return json_decode($result);
|
2018-09-14 02:42:38 +02:00
|
|
|
}
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|