From b0e7215427a8b0b8d90a3ced2420a130c7004de0 Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Fri, 9 Nov 2018 12:34:12 -0600 Subject: [PATCH] Changed publish call to array of args. Fixed channel sort issue. Fixed anoymous name. LBRY Banner is now in blockquote. Save post perm_url from lbry --- classes/LBRYPress.php | 8 ++-- classes/LBRY_Daemon.php | 66 ++++++++++---------------- classes/LBRY_Network.php | 7 +-- classes/LBRY_Network_Publisher.php | 51 ++++++++++---------- classes/LBRY_Speech.php | 6 +-- classes/LBRY_Speech_Parser.php | 33 +++++-------- templates/meta_box.php | 3 +- templates/published_on_lbry_banner.php | 6 +-- 8 files changed, 76 insertions(+), 104 deletions(-) diff --git a/classes/LBRYPress.php b/classes/LBRYPress.php index 2557ff9..b3ab9d7 100644 --- a/classes/LBRYPress.php +++ b/classes/LBRYPress.php @@ -115,8 +115,10 @@ class LBRYPress $this->define('LBRY_SPEECH', 'lbry_speech'); // the spee.ch address $this->define('LBRY_LICENSE', 'lbry_license'); // the license to publish with to the LBRY network $this->define('LBRY_LBC_PUBLISH', 'lbry_lbc_publish'); // amount of lbc to use per publish - $this->define('LBRY_WILL_PUBLISH', 'lbry_will_publish'); // The meta key for if to publish to LBRY Network or not - $this->define('LBRY_POST_CHANNEL', 'lbry_channel'); // The meta key for which channel to publish + $this->define('LBRY_WILL_PUBLISH', '_lbry_will_publish'); // The meta key for if to publish to LBRY Network or not + $this->define('LBRY_POST_CHANNEL', '_lbry_channel'); // The meta key for which channel to publish + $this->define('LBRY_PERM_URL', '_lbry_perm_url'); // The meta key for the permanent url of the published lbry post + $this->define('LBRY_SPEECH_ASSET_URL', 'speech_asset_url'); // The meta key for an asset's speech url } /** @@ -233,7 +235,7 @@ class LBRYPress return 0; } - if ($b->name == '(none / unattributed)') { + if ($b->claim_id == 'null') { return -1; } return strnatcasecmp($a->name, $b->name); diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index dd5cfd8..5d2ef12 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -7,8 +7,16 @@ class LBRY_Daemon { + /** + * The address of the Lbry Daemon + * @var string + */ private $address = 'localhost:5279'; + /** + * The Daemon Logger + * @var LBRY_Daemon_Logger + */ private $logger = null; /** @@ -17,17 +25,12 @@ class LBRY_Daemon public function __construct() { $this->logger = new LBRY_Daemon_Logger(); - $this->init(); - } - - public function init() - { } /** * Returns an unused address * https://lbry.tech/api/sdk#address_unused - * @return string Unused wallet address in base58 + * @return string Unused wallet address in base58 */ public function wallet_unused_address() { @@ -44,7 +47,7 @@ class LBRY_Daemon /** * Returns an array of Address lists * https://lbry.tech/api/sdk#address_list - * @return array Array of address lists linked to this account + * @return array Array of address lists linked to this account */ public function address_list() { @@ -61,8 +64,8 @@ class LBRY_Daemon /** * Returns the balance of a current LBRY account * https://lbry.tech/api/sdk#account_balance - * @param string $address Wallet Address - * @return float Wallet Balance + * @param string $address Wallet Address + * @return float Wallet Balance */ public function wallet_balance() { @@ -79,7 +82,7 @@ class LBRY_Daemon /** * Returns a list of channels for this account * https://lbry.tech/api/sdk#channel_list - * @return array claim dictionary or null if empty + * @return array claim dictionary or null if empty */ public function channel_list() { @@ -96,12 +99,11 @@ class LBRY_Daemon /** * Create a claim for a new channel * https://lbry.tech/api/sdk#channel_new - * @return array dictionary containing result of the request + * @return array dictionary containing result of the request */ public function channel_new($channel_name, $bid_amount) { // TODO: Sanitize channel name and bid - // Make sure no @ sign, as we will add that if (strpos($channel_name, '@')) { throw new \Exception('Illegal character "@" in channel name', 1); @@ -133,35 +135,15 @@ class LBRY_Daemon /** * Publishes a post to the LBRY Network * https://lbry.tech/api/sdk#publish - * @param string $name The slug for the post - * @param float $bid The amount of LBC to bid - * @param string $filepath The path of the temporary content file - * @param string $title The Title of the post - * @param string $description The Description of the Post - * @param string $language Two letter ISO Code of the language - * @return string $channel The Claim ID of the Channel + * @param array $args An array containing necessary data for publish post + * + * Available params: + * ['name', 'bid', 'file_path', 'title', 'description', 'language', 'license', 'channel_id', 'thumbnail'] + * + * @return object $result */ - public function publish($name, $bid, $filepath, $title, $description, $language, $license, $channel, $thumbnail = false) + public function publish($args) { - $args = array( - 'name' => $name, - 'bid' => $bid, - 'file_path' => $filepath, - 'title' => $title, - 'description' => $description, - 'language' => $language, - 'license' => $license, - ); - - // Make sure we aren't publishing to unattributed - if ($channel != 'null') { - $args['channel_id'] = $channel; - } - - if ($thumbnail) { - $args['thumbnail'] = $thumbnail; - } - try { $result = $this->request( 'publish', @@ -177,9 +159,9 @@ class LBRY_Daemon /** * Sends a cURL request to the LBRY Daemon - * @param string $method The method to call on the LBRY API - * @param array $params The Parameters to send the LBRY API Call - * @return string The cURL response + * @param string $method The method to call on the LBRY API + * @param array $params The Parameters to send the LBRY API Call + * @return string The cURL response */ private function request($method, $params = array()) { diff --git a/classes/LBRY_Network.php b/classes/LBRY_Network.php index a8e792a..aa1400e 100644 --- a/classes/LBRY_Network.php +++ b/classes/LBRY_Network.php @@ -20,9 +20,6 @@ class LBRY_Network */ public $parser = null; - /** - * [__construct description] - */ public function __construct() { $this->publisher = new LBRY_Network_Publisher(); @@ -63,7 +60,7 @@ class LBRY_Network * Handles saving the post meta that is relative to publishing to the LBRY Network * @param int $post_id The ID of the post we are saving * @param WP_Post $post The Post Object we are saving - * @return int Returns post_id if user cannot edit post + * @return int Returns post_id if user cannot edit post */ public function save_post_meta($post_id, $post) { @@ -104,7 +101,7 @@ class LBRY_Network /** * Returns the HTML for the LBRY Meta Box - * @param [type] $post [description] + * @param WP_POST $post */ public function meta_box_html($post) { diff --git a/classes/LBRY_Network_Publisher.php b/classes/LBRY_Network_Publisher.php index fe77145..4f9f9be 100644 --- a/classes/LBRY_Network_Publisher.php +++ b/classes/LBRY_Network_Publisher.php @@ -7,28 +7,14 @@ class LBRY_Network_Publisher { - /** - * [__construct description] - */ - public function __construct() - { - } - /** * Publish the post to the LBRY Network - * @param int $post_id The ID of the post we are publishing - * @param string $channel The Claim ID of the channel we are posting to + * @param int $post_id The ID of the post we are publishing + * @param string $channel The Claim ID of the channel we are posting to */ // NOTE: This is currently sitting at about 150ms, mostly the post parsing - public function publish($post, $channel) + public function publish($post, $channel = null) { - - // TODO: Handle unnatributed channel - // Leave if nothing to publish to - if (!$channel) { - return; - } - // Get converted markdown into a file $filepath = LBRY_ABSPATH . 'tmp/' . $post->post_name . time() . '.md'; $file = fopen($filepath, 'w'); @@ -40,16 +26,27 @@ class LBRY_Network_Publisher try { // If everything went well with the conversion, carry on if ($write_status) { - $name = $post->post_name; - $bid = number_format(floatval(get_option(LBRY_SETTINGS)[LBRY_LBC_PUBLISH]), 2, '.', ''); - $title = $post->post_title; - $language = substr(get_locale(), 0, 2); - $license = get_option(LBRY_SETTINGS)[LBRY_LICENSE]; + $args = array( + 'name' => $post->post_name, + 'bid' => number_format(floatval(get_option(LBRY_SETTINGS)[LBRY_LBC_PUBLISH]), 2, '.', ''), + 'file_path' => $filepath, + 'title' => $post->post_title, + 'language' => substr(get_locale(), 0, 2), + 'license' => get_option(LBRY_SETTINGS)[LBRY_LICENSE] + ); + + // Setup channel + if ($channel && $channel != 'null') { + $args['channel_id'] = $channel; + } // Setup featured image $featured_id = get_post_thumbnail_id($post); $featured_image = wp_get_attachment_image_src($featured_id, 'medium'); - $thumbnail = $featured_image[0] ? $featured_image[0] : false; + + if ($featured_image[0]) { + $args['thumbnail'] = $featured_image[0]; + } // Build description using Yoast if installed and its used, excerpt/title otherwise $description = false; @@ -62,8 +59,12 @@ class LBRY_Network_Publisher } $description .= ' | Originally published at ' . get_permalink($post); - //TODO: Switch this to an array of args. This is getting out of hand. - LBRY()->daemon->publish($name, $bid, $filepath, $title, $description, $language, $license, $channel, $thumbnail); + $args['description'] = $description; + + $result = LBRY()->daemon->publish($args); + if ($result->success) { + update_post_meta($post->ID, LBRY_PERM_URL, $result->output->permanent_url); + } } } catch (Exception $e) { error_log('Issue publishing post ' . $post->ID . ' to LBRY: ' . $e->getMessage()); diff --git a/classes/LBRY_Speech.php b/classes/LBRY_Speech.php index 5b54142..ade141f 100644 --- a/classes/LBRY_Speech.php +++ b/classes/LBRY_Speech.php @@ -120,9 +120,9 @@ class LBRY_Speech if ($result && $result->success) { $meta = wp_get_attachment_metadata($media->id); if ($media->image_size) { - $meta['sizes'][$media->image_size]['speech_asset_url'] = $result->data->serveUrl; + $meta['sizes'][$media->image_size][LBRY_SPEECH_ASSET_URL] = $result->data->serveUrl; } else { - $meta['speech_asset_url'] = $result->data->serveUrl; + $meta[LBRY_SPEECH_ASSET_URL] = $result->data->serveUrl; } wp_update_attachment_metadata($media->id, $meta); } else { // Something unhandled happened here @@ -219,7 +219,7 @@ class LBRY_Speech */ public function is_published($meta) { - if (key_exists('speech_asset_url', $meta) && $meta['speech_asset_url'] !== '') { + if (key_exists(LBRY_SPEECH_ASSET_URL, $meta) && $meta[LBRY_SPEECH_ASSET_URL] !== '') { return true; } diff --git a/classes/LBRY_Speech_Parser.php b/classes/LBRY_Speech_Parser.php index 232434b..1f86ecf 100644 --- a/classes/LBRY_Speech_Parser.php +++ b/classes/LBRY_Speech_Parser.php @@ -19,8 +19,8 @@ class LBRY_Speech_Parser foreach ($sources as $width => $source) { // Check to see if it is using base image first - if ($image_src == $source['url'] && key_exists('speech_asset_url', $image_meta)) { - $new_sources[$width]['url'] = $image_meta['speech_asset_url']; + if ($image_src == $source['url'] && key_exists(LBRY_SPEECH_ASSET_URL, $image_meta)) { + $new_sources[$width]['url'] = $image_meta[LBRY_SPEECH_ASSET_URL]; continue; } @@ -55,7 +55,7 @@ class LBRY_Speech_Parser // If the image is the same as the base image, return the base spee.ch url if (pathinfo($image[0])['basename'] == pathinfo($image_meta['file'])['basename']) { - $new_image[0] = $image_meta['speech_asset_url']; + $new_image[0] = $image_meta[LBRY_SPEECH_ASSET_URL]; return $new_image; } @@ -65,22 +65,19 @@ class LBRY_Speech_Parser if (is_string($size)) { switch ($size) { case 'full': - $new_image[0] = $image_meta['speech_asset_url']; + $new_image[0] = $image_meta[LBRY_SPEECH_ASSET_URL]; break; case 'post-thumbnail': if (LBRY()->speech->is_published($sizes['thumbnail'])) { - $new_image[0] = $sizes['thumbnail']['speech_asset_url']; + $new_image[0] = $sizes['thumbnail'][LBRY_SPEECH_ASSET_URL]; } break; default: if (key_exists($size, $sizes) && LBRY()->speech->is_published($sizes[$size])) { - $new_image[0] = $sizes[$size]['speech_asset_url']; + $new_image[0] = $sizes[$size][LBRY_SPEECH_ASSET_URL]; } break; } - // $time_end = microtime(true); - // $time = ($time_end - $time_start) * 1000; - // error_log("attachment image source in $time milliseconds"); return $new_image; } @@ -101,7 +98,6 @@ class LBRY_Speech_Parser // COMBAK: Need to make this a bit faster. Sitting between 100 and 300ms currently public function replace_urls_with_speech($content) { - // $time_start = microtime(true); $new_content = $content; $assets = array(); @@ -131,10 +127,6 @@ class LBRY_Speech_Parser $new_content = str_replace($asset, $speech_url, $new_content); } } - - // $time_end = microtime(true); - // $time = ($time_end - $time_start) * 1000; - // error_log("replace content urls in $time milliseconds"); return $new_content; } @@ -247,8 +239,8 @@ class LBRY_Speech_Parser private function find_speech_url_by_width($sizes, $width) { foreach ($sizes as $key => $size) { - if ($size['width'] == $width && key_exists('speech_asset_url', $size)) { - return $size['speech_asset_url']; + if ($size['width'] == $width && key_exists(LBRY_SPEECH_ASSET_URL, $size)) { + return $size[LBRY_SPEECH_ASSET_URL]; } } @@ -265,7 +257,7 @@ class LBRY_Speech_Parser { // See if this looks like video meta if (!key_exists('file', $meta) && key_exists('mime_type', $meta) && $meta['mime_type'] == 'video/mp4') { - return $meta['speech_asset_url']; + return $meta[LBRY_SPEECH_ASSET_URL]; } $pathinfo = pathinfo($url); @@ -273,15 +265,15 @@ class LBRY_Speech_Parser // Check main file or if $meta is just a url (video) first if (key_exists('file', $meta) && $basename == wp_basename($meta['file'])) { - return $meta['speech_asset_url']; + return $meta[LBRY_SPEECH_ASSET_URL]; } // Check to see if we have a meta option here if (key_exists('sizes', $meta)) { // Then check sizes foreach ($meta['sizes'] as $size => $meta) { - if ($basename == $meta['file'] && key_exists('speech_asset_url', $meta)) { - return $meta['speech_asset_url']; + if ($basename == $meta['file'] && key_exists(LBRY_SPEECH_ASSET_URL, $meta)) { + return $meta[LBRY_SPEECH_ASSET_URL]; } } } @@ -326,7 +318,6 @@ class LBRY_Speech_Parser $post_id = attachment_url_to_postid($url); } } - return (int) $post_id; } } diff --git a/templates/meta_box.php b/templates/meta_box.php index ed05828..336469f 100644 --- a/templates/meta_box.php +++ b/templates/meta_box.php @@ -1,9 +1,8 @@ '(none / unattributed)', + 'name' => 'none (anonymous)', 'claim_id' => 'null' ); -// TODO: Test what happens with empty channel list, can't remember the return value $channels = LBRY()->daemon->channel_list(); $channels[] = $unnatributed; // Sort the channels in a natural way diff --git a/templates/published_on_lbry_banner.php b/templates/published_on_lbry_banner.php index d85b9c5..4e02fa7 100644 --- a/templates/published_on_lbry_banner.php +++ b/templates/published_on_lbry_banner.php @@ -1,4 +1,4 @@ -
-
This post has also been published to the LBRY blockchain network.
+
+
This post has also been published to the LBRY blockchain network.
Click Here to learn more about the benefits of content freedom.
-
\ No newline at end of file +