Pulling media from the_content instead of attachments

This commit is contained in:
Paul Kirby 2018-10-26 01:34:13 -05:00
parent b643f1c50f
commit b82ab89d59
2 changed files with 83 additions and 34 deletions

View file

@ -18,7 +18,7 @@ class LBRY_Speech
public function __construct() public function __construct()
{ {
$this->parser = new LBRY_Speech_Parser(); $this->parser = new LBRY_Speech_Parser();
add_action('save_post', array($this, 'upload_attachments')); add_action('save_post', array($this, 'upload_media'));
} }
/** /**
@ -40,7 +40,7 @@ class LBRY_Speech
* @return bool True if successful, false if not or if no Speech URL available * @return bool True if successful, false if not or if no Speech URL available
*/ */
// TODO: set up error reporting // TODO: set up error reporting
public function upload_attachments($post_id) public function upload_media($post_id)
{ {
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH]; $speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
@ -49,30 +49,30 @@ class LBRY_Speech
return false; return false;
} }
$attachments = $this->find_attachments($post_id); $all_media = $this->find_media($post_id);
// IDEA: Notify user if post save time will take a while, may be a concern for request timeouts // IDEA: Notify user if post save time will take a while, may be a concern for request timeouts
if ($attachments) { if ($all_media) {
foreach ($attachments as $attachment) { // error_log(print_r($all_media, true));
error_log(print_r($attachment, true)); foreach ($all_media as $media) {
// TODO: set post meta to see if already uploaded // TODO: set post meta to see if already uploaded
// Create a CURLFile object to pass our attachments to the spee.ch instance // Create a CURLFile object to pass our attachments to the spee.ch instance
$file_url = get_attached_file($attachment->ID); // $file_url = get_attached_file($attachment->ID);
$file_name = wp_basename($file_url); // $file_name = wp_basename($file_url);
$file_type = $attachment->post_mime_type; // $file_type = $attachment->post_mime_type;
$cfile = new CURLFile($file_url, $file_type, $file_name); // $cfile = new CURLFile($file_url, $file_type, $file_name);
//
$params = array( // $params = array(
'name' => $attachment->post_name, // 'name' => $attachment->post_name,
'file' => $cfile, // 'file' => $cfile,
'title' => $attachment->post_title, // 'title' => $attachment->post_title,
'type' => $file_type // 'type' => $file_type
); // );
//
$result = $this->request('publish', $params); // $result = $this->request('publish', $params);
error_log(print_r($result, true)); // error_log(print_r($result, true));
// TODO: Make sure to warn if image name is already taken on channel // TODO: Make sure to warn if image name is already taken on channel
} }
@ -82,24 +82,34 @@ class LBRY_Speech
/** /**
* Finds all media attached to a post * Finds all media attached to a post
* @param int $post_id The post to search * @param int $post_id The post to search
* @return array An array of WP_Post Objects, or false if none found * @return array An array of Speech Media Objects
*/ */
protected function find_attachments($post_id) protected function find_media($post_id)
{ {
// Get all attachments // Get content and put into a DOMDocument
$attachments = get_posts(array( $content = apply_filters('the_content', get_post_field('post_content', $post_id));
'post_type' => 'attachment', $DOM = new DOMDocument();
'numberposts' => -1, // Hide HTML5 Tag warnings
'post_status' => 'any', libxml_use_internal_errors(true);
'post_parent' => $post_id, $DOM->loadHTML($content);
));
// Return attachments arary $images = $DOM->getElementsByTagName('img');
if ($attachments) { $videos = $DOM->getElementsByTagName('video');
return $attachments;
} else { // Get each image attribute
return false; foreach ($images as $image) {
error_log($image->getAttribute('src'));
} }
// Parse video tags based on wordpress output for local embedds
// Because video tag is HTML5, treat it like an XML node
foreach ($videos as $video) {
$source = $video->getElementsByTagName('source');
$src = $source[0]->attributes->getNamedItem('src')->value;
error_log($src);
}
return;
} }
/** /**

View file

@ -0,0 +1,39 @@
<?php
/**
* Data container for necessary Speech api media uploaduploads
*
* Visit https://github.com/lbryio/spee.ch for more info
*
* @package LBRYPress
*/
class LBRY_Speech_Media
{
public $name;
public $file;
public $type;
public $nsfw;
public $license;
public $title;
public $description;
public $thumbnail;
public function __construct($name, $file, $type, $nsfw = null, $license = null, $title = null, $description = null, $thumbnail = null)
{
$this->name = $name;
$this->file = $file;
$this->type = $type;
$this->nsfw = $nsfw;
$this->license = $license;
$this->title = $title;
$this->description = $description;
$this->thumbnail = $thumbnail;
}
}