From b0d35d98ee1a54a081348b7d877d0c7ff027ed3d Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Fri, 26 Oct 2018 11:13:24 -0500 Subject: [PATCH] Succesfully creating Speech Media Object --- classes/LBRY_Speech.php | 5 ++++ classes/LBRY_Speech_Media.php | 54 +++++++++++++++++++++++++++-------- lbrypress.php | 3 ++ 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/classes/LBRY_Speech.php b/classes/LBRY_Speech.php index e9feaff..d2cc2e6 100644 --- a/classes/LBRY_Speech.php +++ b/classes/LBRY_Speech.php @@ -64,6 +64,11 @@ class LBRY_Speech 'type' => $media->type ); + if (LBRY_SPEECH_CHANNEL && LBRY_SPEECH_CHANNEL_PASSWORD) { + $params['channelName'] = LBRY_SPEECH_CHANNEL; + $params['channelPassword'] = LBRY_SPEECH_CHANNEL_PASSWORD; + } + $result = $this->request('publish', $params); error_log(print_r($result, true)); diff --git a/classes/LBRY_Speech_Media.php b/classes/LBRY_Speech_Media.php index 679a7e9..fb0e043 100644 --- a/classes/LBRY_Speech_Media.php +++ b/classes/LBRY_Speech_Media.php @@ -9,6 +9,8 @@ class LBRY_Speech_Media { + public $id; + public $name; public $file; @@ -32,7 +34,6 @@ class LBRY_Speech_Media $default = array( 'nsfw' => null, 'license' => null, - 'title' => null, 'description' => null, 'thumbnail' => null ); @@ -43,17 +44,46 @@ class LBRY_Speech_Media $this->{$key} = $value; } - // TODO: Name can't have extension in it. - // TODO: Get attachment ID... so we can mark postmeta when needed. - // Get name, file, and type from the URL - // First, strip of any query params - $url = strtok($url, '?'); - $path = ABSPATH . str_replace(home_url(), '', $url); - error_log($path); - $type = mime_content_type($path); - $name = wp_basename($url); - $this->name = $name; - $this->file = new CURLFile($path, $type, $name); + // Get attachment ID, name, file, and type from the URL + $url = strtok($url, '?'); // Clean up query params first + $id = $this->rigid_attachment_url_to_postid($url); + $attachment = get_post($id); + $path = get_attached_file($id); + $type = $attachment->post_mime_type; + $filename = wp_basename($path); + + $this->id = $id; + // COMBAK: Probably wont need this underscore check with Daemon V3 + $this->name = str_replace('_', '-', $attachment->post_name); + $this->file = new CURLFile($path, $type, $filename); $this->type = $type; + $this->title = $attachment->post_title; + } + + /** + * 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 + */ + private function rigid_attachment_url_to_postid($url) + { + $post_id = attachment_url_to_postid($url); + + if (! $post_id) { + $dir = wp_upload_dir(); + $path = $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); + } + } + + return (int) $post_id; } } diff --git a/lbrypress.php b/lbrypress.php index 65d046d..e77fde3 100644 --- a/lbrypress.php +++ b/lbrypress.php @@ -70,6 +70,9 @@ function LBRY() if (! class_exists('LBRYPress')) { require_once(dirname(__FILE__) . '/classes/LBRYPress.php'); } + // Bring in configuration requirements + // HACK: Will probably be getting rid of configuration once we sort out Spee.ch Implementation + require_once(dirname(__FILE__) . '/lbry_config.php'); return LBRYPress::instance(); } else { add_action('admin_notices', 'lbry_requirements_error');