From 8bc336952d1a425b914ddeeb3026e33765cf2643 Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Fri, 12 Oct 2018 02:42:40 -0500 Subject: [PATCH 1/2] Published post to lbry network --- classes/LBRY_Daemon.php | 29 ++++++++++++++++++++++ classes/LBRY_Network_Parser.php | 26 +++++++++++--------- classes/LBRY_Network_Publisher.php | 39 +++++++++++++++++++++++++++--- classes/lbrypress.php | 1 + templates/meta_box.php | 7 +++--- 5 files changed, 84 insertions(+), 18 deletions(-) diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index 13171ca..9f1c09d 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -80,6 +80,35 @@ class LBRY_Daemon return $result->result; } + /** + * Publishes a post to the LBRY Network + * @param [type] $name [description] + * @param [type] $bid [description] + * @param [type] $filepath [description] + * @param [type] $title [description] + * @param [type] $description [description] + * @param [type] $language [description] + * @return [type] [description] + */ + public function publish($name, $bid, $filepath, $title, $description, $language, $channel) + { + // TODO: Bring thumbnails into the mix + $result = $this->request( + 'publish', + array( + 'name' => $name, + 'bid' => $bid, + 'file_path' => $filepath, + 'title' => $title, + 'description' => $description, + 'language' => $language, + 'channel_name' => $channel + ) + ); + $this->check_for_errors($result); + return $result; + } + /** * Sends a cURL request to the LBRY Daemon * @param string $method The method to call on the LBRY API diff --git a/classes/LBRY_Network_Parser.php b/classes/LBRY_Network_Parser.php index 4ac3fad..6c7a0d7 100644 --- a/classes/LBRY_Network_Parser.php +++ b/classes/LBRY_Network_Parser.php @@ -22,18 +22,22 @@ class LBRY_Network_Parser )); } - public function convert_to_markdown($post_id) + /** + * Converts a post into markdown. + * @param WP_Post $post The post to be converted + * @return string + */ + public function convert_to_markdown($post) { - $post = get_post($post_id); - $title = '

' . $post->post_title . '

'; - - $featured_image = get_the_post_thumbnail($post); - - $content = $title; - if ($featured_image) { - $content .= $featured_image . '
'; - } - $content .= apply_filters('the_content', get_post($post_id)->post_content); + // $title = '

' . $post->post_title . '

'; + // + // $featured_image = get_the_post_thumbnail($post); + // + // $content = $title; + // if ($featured_image) { + // $content .= $featured_image . '
'; + // } + $content = apply_filters('the_content', $post->post_content); $converted = $this->converter->convert($content); return $converted; diff --git a/classes/LBRY_Network_Publisher.php b/classes/LBRY_Network_Publisher.php index 0cd6235..6d4cefa 100644 --- a/classes/LBRY_Network_Publisher.php +++ b/classes/LBRY_Network_Publisher.php @@ -21,8 +21,41 @@ class LBRY_Network_Publisher */ public function publish($post, $channels) { - $name = $post->post_name; - - return; + // Leave if nothing to publish to + if (!$channels) { + return; + } + + // Get converted markdown into a file + $filepath = LBRY_ABSPATH . 'tmp/' . $post->post_name . time() . '.md'; + $file = fopen($filepath, 'w'); + $converted = LBRY()->network->parser->convert_to_markdown($post); + $write_status = $file && fwrite($file, $converted); + fclose($file); + + // TODO: Catch relative exceptions if necessary + try { + // If everything went well with the conversion, carry on + if ($write_status) { + $featured_image = get_the_post_thumbnail($post); + + $name = $post->post_name; + $bid = get_option(LBRY_SETTINGS)[LBRY_LBC_PUBLISH]; + $title = $post->post_title; + $language = substr(get_locale(), 0, 2); + $license = get_option(LBRY_SETTINGS)[LBRY_LICENSE]; + // TODO: See if we can grab from yoast or a default? + $description = $post->post_title; + // TODO: Bring thumbnails into the mix + // $thumbnail = $featured_image ? $featured_image : null; + + foreach ($channels as $channel) { + LBRY()->daemon->publish($name, $bid, $filepath, $title, $description, $language, $channel); + } + } + } finally { + // Delete the temporary markdown file + unlink($filepath); + } } } diff --git a/classes/lbrypress.php b/classes/lbrypress.php index 9043ef1..721ef56 100644 --- a/classes/lbrypress.php +++ b/classes/lbrypress.php @@ -66,6 +66,7 @@ class LBRYPress spl_autoload_register(array($this, 'lbry_autoload_register')); $this->init(); $this->init_hooks(); + error_log("language: " . get_locale()); } /** diff --git a/templates/meta_box.php b/templates/meta_box.php index 93e73fa..35d0846 100644 --- a/templates/meta_box.php +++ b/templates/meta_box.php @@ -1,8 +1,7 @@ 'Unattributed', - 'permanent_url' => 'unattributed' + 'name' => 'unattributed', ); $channels = LBRY()->daemon->channel_list(); array_unshift($channels, $unnatributed); @@ -15,8 +14,8 @@ $cur_channels = get_post_meta($post->ID, 'lbry_channels');