From bb7c80a25c53e9dfbd345d166bc3a2cffd5c9973 Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Wed, 31 Oct 2018 14:48:58 -0500 Subject: [PATCH 1/5] Uploading thumbnails --- classes/LBRY_Daemon.php | 7 +++++-- classes/LBRY_Network_Parser.php | 2 +- classes/LBRY_Network_Publisher.php | 14 +++++++------- classes/LBRY_Speech.php | 13 ++++++------- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index 61ccda6..40e8940 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -121,7 +121,7 @@ class LBRY_Daemon * @param string $language Two letter ISO Code of the language * @return string $channel The Claim ID of the Channel */ - public function publish($name, $bid, $filepath, $title, $description, $language, $channel) + public function publish($name, $bid, $filepath, $title, $description, $language, $channel, $thumbnail = false) { $args = array( 'name' => $name, @@ -137,7 +137,10 @@ class LBRY_Daemon $args['channel_id'] = $channel; } - // TODO: Bring thumbnails into the mix + if ($thumbnail) { + $args['thumbnail'] = $thumbnail; + } + try { $result = $this->request( 'publish', diff --git a/classes/LBRY_Network_Parser.php b/classes/LBRY_Network_Parser.php index 6c7a0d7..52cccc3 100644 --- a/classes/LBRY_Network_Parser.php +++ b/classes/LBRY_Network_Parser.php @@ -18,7 +18,7 @@ class LBRY_Network_Parser // COMBAK: Composer is not safe in a wordpress environment. May have to write our own package. require_once LBRY_ABSPATH . 'vendor/autoload.php'; $this->converter = new HtmlConverter(array( - 'strip_tags' => true + 'strip_tags' => false )); } diff --git a/classes/LBRY_Network_Publisher.php b/classes/LBRY_Network_Publisher.php index 2648401..9ed6709 100644 --- a/classes/LBRY_Network_Publisher.php +++ b/classes/LBRY_Network_Publisher.php @@ -33,12 +33,11 @@ class LBRY_Network_Publisher $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); - + $featured_id = get_post_thumbnail_id($post); + $featured_image = wp_get_attachment_image_src($featured_id, 'medium'); $name = $post->post_name; $bid = floatval(get_option(LBRY_SETTINGS)[LBRY_LBC_PUBLISH]); $title = $post->post_title; @@ -46,14 +45,15 @@ class LBRY_Network_Publisher $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; + $thumbnail = $featured_image[0] ? $featured_image[0] : false; - LBRY()->daemon->publish($name, $bid, $filepath, $title, $description, $language, $channel); + LBRY()->daemon->publish($name, $bid, $filepath, $title, $description, $language, $channel, $thumbnail); } + } catch (Exception $e) { + error_log('Issue publishing post ' . $post->ID . ' to LBRY: ' . $e->getMessage()); } finally { // Delete the temporary markdown file - unlink($filepath); + // unlink($filepath); } } } diff --git a/classes/LBRY_Speech.php b/classes/LBRY_Speech.php index c446fe7..a67bba4 100644 --- a/classes/LBRY_Speech.php +++ b/classes/LBRY_Speech.php @@ -21,14 +21,13 @@ class LBRY_Speech if (is_admin()) { add_action('save_post', array($this, 'upload_media'), 10, 2); - } else { - // Replace the image srcsets - add_filter('wp_calculate_image_srcset', array($this->parser, 'replace_image_srcset'), 10, 5); - // Core filter for lots of image source calls - add_filter('wp_get_attachment_image_src', array($this->parser, 'replace_attachment_image_src'), 10, 3); - // Replace any left over urls with speech urls - add_filter('the_content', array($this->parser, 'replace_urls_with_speech')); } + // Replace the image srcsets + add_filter('wp_calculate_image_srcset', array($this->parser, 'replace_image_srcset'), 10, 5); + // Core filter for lots of image source calls + add_filter('wp_get_attachment_image_src', array($this->parser, 'replace_attachment_image_src'), 10, 3); + // Replace any left over urls with speech urls + add_filter('the_content', array($this->parser, 'replace_urls_with_speech')); } /** From ab50c3c6a66337f601b9cf12277f9a8065c6a242 Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Wed, 31 Oct 2018 15:07:21 -0500 Subject: [PATCH 2/5] Minor tweaks --- classes/LBRY_Network_Publisher.php | 4 ++-- classes/LBRY_Speech.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/classes/LBRY_Network_Publisher.php b/classes/LBRY_Network_Publisher.php index 9ed6709..7052daa 100644 --- a/classes/LBRY_Network_Publisher.php +++ b/classes/LBRY_Network_Publisher.php @@ -52,8 +52,8 @@ class LBRY_Network_Publisher } catch (Exception $e) { error_log('Issue publishing post ' . $post->ID . ' to LBRY: ' . $e->getMessage()); } finally { - // Delete the temporary markdown file - // unlink($filepath); + //Delete the temporary markdown file + unlink($filepath); } } } diff --git a/classes/LBRY_Speech.php b/classes/LBRY_Speech.php index a67bba4..2605b98 100644 --- a/classes/LBRY_Speech.php +++ b/classes/LBRY_Speech.php @@ -51,7 +51,7 @@ class LBRY_Speech $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 if ($all_media) { $requests = array(); From 77cd4c2c96508f423c9233d34978937706d0ee61 Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Wed, 31 Oct 2018 19:29:49 -0500 Subject: [PATCH 3/5] Changed balance notifican min value, updated licenses --- classes/LBRY_Admin.php | 4 ++-- classes/lbrypress.php | 17 +++++++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 9c14f3f..fa888f8 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -209,13 +209,13 @@ class LBRY_Admin /** * Checks at most once an hour to see if the wallet balance is too low */ - // COMBAK: Check user permissions possibly + // IDEA: Check user permissions possibly public static function wallet_balance_warning() { // See if we've checked in the past two hours if (!get_transient('lbry_wallet_check')) { $balance = LBRY()->daemon->wallet_balance(); - if ($balance < LBRY_MIN_BALANCE) { + if ($balance < get_option(LBRY_SETTINGS)[LBRY_LBC_PUBLISH] * 20) { // If LBRY Balance is low, send email, but only once per day if (!get_transient('lbry_wallet_warning_email')) { $email = get_option('admin_email'); diff --git a/classes/lbrypress.php b/classes/lbrypress.php index 068731f..8247212 100644 --- a/classes/lbrypress.php +++ b/classes/lbrypress.php @@ -104,11 +104,15 @@ class LBRYPress $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_AVAILABLE_LICENSES', array( - 'mit' => 'MIT', - 'license2' => 'License 2', - 'license3' => 'License 3' + 'Creative Commons Attribution 4.0 International' => 'Creative Commons Attribution 4.0 International', + 'Creative Commons Attribution-ShareAlike 4.0 International' => 'Creative Commons Attribution-ShareAlike 4.0 International', + 'Creative Commons Attribution-NoDerivatives 4.0 International' => 'Creative Commons Attribution-NoDerivatives 4.0 International', + 'Creative Commons Attribution-NonCommercial 4.0 International' => 'Creative Commons Attribution-NonCommercial 4.0 International', + 'Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International' => 'Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International', + 'Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International' => 'Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International', + 'Copyrighted' => 'Copyrighted', + 'Public Domain' => 'Public Domain' )); - $this->define('LBRY_MIN_BALANCE', 2000); } /** @@ -137,8 +141,6 @@ class LBRYPress $this->admin = new LBRY_Admin(); $this->notice = new LBRY_Admin_Notice(); $this->network = new LBRY_Network(); - } else { - // $this->speech->maybe_rewrite_urls(); } } @@ -192,8 +194,7 @@ class LBRYPress */ public function deactivate() { - // Deactivate Wallet Balance cron job - $this->admin->wallet_balance_deactivate(); + // TODO: Stop the daemon error_log('Deactivated'); } From 42c4c36bb2951ecdead68e8be759884bfba40ade Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Thu, 1 Nov 2018 12:13:55 -0500 Subject: [PATCH 4/5] Using description from Yoast or Excerpt for LBRY Description --- classes/LBRY_Daemon.php | 4 ++-- classes/LBRY_Network.php | 8 ++++++-- classes/LBRY_Network_Publisher.php | 21 +++++++++++++++++---- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index 40e8940..c3e230c 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -140,7 +140,7 @@ class LBRY_Daemon if ($thumbnail) { $args['thumbnail'] = $thumbnail; } - + try { $result = $this->request( 'publish', @@ -148,7 +148,7 @@ class LBRY_Daemon ); return $result->result; } catch (LBRYDaemonException $e) { - $this->logger->log('wallet_unused_address error', $e->getMessage() . ' | Code: ' . $e->getCode()); + $this->logger->log('publish error', $e->getMessage() . ' | Code: ' . $e->getCode()); LBRY()->notice->set_notice('error', 'Issue publishing / updating post to LBRY Network.'); return; } diff --git a/classes/LBRY_Network.php b/classes/LBRY_Network.php index 3ad4b8e..a8e792a 100644 --- a/classes/LBRY_Network.php +++ b/classes/LBRY_Network.php @@ -40,7 +40,7 @@ class LBRY_Network add_action('add_meta_boxes', array($this, 'add_meta_boxes')); // Save the post meta on 'save_post' hook - add_action('save_post', array($this, 'save_post_meta'), 10, 2); + add_action('wp_insert_post', array($this, 'save_post_meta'), 11, 2); } /** @@ -48,7 +48,7 @@ class LBRY_Network */ public function add_meta_boxes() { - // TODO: Support post types based on user selection + // IDEA: Support post types based on user selection add_meta_box( 'lbry-network-publishing', // Unique ID 'LBRY Network', // Title @@ -67,6 +67,10 @@ class LBRY_Network */ public function save_post_meta($post_id, $post) { + if ($post->post_type != 'post') { + return; + } + // Verify the nonce before proceeding. if (!isset($_POST['_lbrynonce']) || !wp_verify_nonce($_POST['_lbrynonce'], 'lbry_publish_channels')) { return $post_id; diff --git a/classes/LBRY_Network_Publisher.php b/classes/LBRY_Network_Publisher.php index 7052daa..e306413 100644 --- a/classes/LBRY_Network_Publisher.php +++ b/classes/LBRY_Network_Publisher.php @@ -19,6 +19,7 @@ class LBRY_Network_Publisher * @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) { // Leave if nothing to publish to @@ -32,21 +33,33 @@ class LBRY_Network_Publisher $converted = LBRY()->network->parser->convert_to_markdown($post); $write_status = $file && fwrite($file, $converted); fclose($file); + $endtime = microtime(true); try { // If everything went well with the conversion, carry on if ($write_status) { - $featured_id = get_post_thumbnail_id($post); - $featured_image = wp_get_attachment_image_src($featured_id, 'medium'); $name = $post->post_name; $bid = floatval(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; + + // 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; + // Build description using Yoast if installed and its used, excerpt/title otherwise + $description = false; + if (class_exists('WPSEO_META')) { + $description = WPSEO_META::get_value('opengraph-description', $post->ID); + } + if (!$description) { + $excerpt = get_the_excerpt($post); + $description = $excerpt ? $excerpt : $title; + } + $description .= ' | Originally published at ' . get_permalink($post); + LBRY()->daemon->publish($name, $bid, $filepath, $title, $description, $language, $channel, $thumbnail); } } catch (Exception $e) { From 56a6443b6dfc5c1c89b29317fae62b8f4cea2803 Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Thu, 1 Nov 2018 12:33:18 -0500 Subject: [PATCH 5/5] placeholder lbry published banner --- classes/lbrypress.php | 26 ++++++++++++++++++++++++++ templates/published_on_lbry_banner.php | 4 ++++ 2 files changed, 30 insertions(+) create mode 100644 templates/published_on_lbry_banner.php diff --git a/classes/lbrypress.php b/classes/lbrypress.php index 8247212..d3c8ee7 100644 --- a/classes/lbrypress.php +++ b/classes/lbrypress.php @@ -151,6 +151,10 @@ class LBRYPress { register_activation_hook(LBRY_PLUGIN_FILE, array($this, 'activate')); register_deactivation_hook(LBRY_PLUGIN_FILE, array($this, 'deactivate')); + + // Banner output for published posts + // NOTE: move this to its own class to reduce clutter? + add_action('the_content', array($this, 'published_on_lbry_banner'), 12, 1); } /** @@ -198,6 +202,28 @@ class LBRYPress error_log('Deactivated'); } + public function published_on_lbry_banner($content) + { + if (!is_single() || !in_the_loop() || !is_main_query()) { + return $content; + } + + global $post; + if ($post->post_type != 'post') { + return $content; + } + + if (!get_post_meta($post->ID, LBRY_WILL_PUBLISH, true)) { + return $content; + } + + ob_start(); + require(LBRY_ABSPATH . 'templates/published_on_lbry_banner.php'); + $banner = ob_get_clean(); + + return $content .= $banner; + } + /* * Utility Functions */ diff --git a/templates/published_on_lbry_banner.php b/templates/published_on_lbry_banner.php new file mode 100644 index 0000000..d85b9c5 --- /dev/null +++ b/templates/published_on_lbry_banner.php @@ -0,0 +1,4 @@ +
+
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