From 42c4c36bb2951ecdead68e8be759884bfba40ade Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Thu, 1 Nov 2018 12:13:55 -0500 Subject: [PATCH] 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) {