Merged in lbry_publish (pull request #9)

Lbry publish
This commit is contained in:
Paul Kirby 2018-11-02 23:45:30 +00:00
commit 9051cb1781
8 changed files with 83 additions and 33 deletions

View file

@ -209,13 +209,13 @@ class LBRY_Admin
/** /**
* Checks at most once an hour to see if the wallet balance is too low * 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() public static function wallet_balance_warning()
{ {
// See if we've checked in the past two hours // See if we've checked in the past two hours
if (!get_transient('lbry_wallet_check')) { if (!get_transient('lbry_wallet_check')) {
$balance = LBRY()->daemon->wallet_balance(); $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 LBRY Balance is low, send email, but only once per day
if (!get_transient('lbry_wallet_warning_email')) { if (!get_transient('lbry_wallet_warning_email')) {
$email = get_option('admin_email'); $email = get_option('admin_email');

View file

@ -121,7 +121,7 @@ class LBRY_Daemon
* @param string $language Two letter ISO Code of the language * @param string $language Two letter ISO Code of the language
* @return string $channel The Claim ID of the Channel * @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( $args = array(
'name' => $name, 'name' => $name,
@ -137,7 +137,10 @@ class LBRY_Daemon
$args['channel_id'] = $channel; $args['channel_id'] = $channel;
} }
// TODO: Bring thumbnails into the mix if ($thumbnail) {
$args['thumbnail'] = $thumbnail;
}
try { try {
$result = $this->request( $result = $this->request(
'publish', 'publish',
@ -145,7 +148,7 @@ class LBRY_Daemon
); );
return $result->result; return $result->result;
} catch (LBRYDaemonException $e) { } 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.'); LBRY()->notice->set_notice('error', 'Issue publishing / updating post to LBRY Network.');
return; return;
} }

View file

@ -40,7 +40,7 @@ class LBRY_Network
add_action('add_meta_boxes', array($this, 'add_meta_boxes')); add_action('add_meta_boxes', array($this, 'add_meta_boxes'));
// Save the post meta on 'save_post' hook // 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() public function add_meta_boxes()
{ {
// TODO: Support post types based on user selection // IDEA: Support post types based on user selection
add_meta_box( add_meta_box(
'lbry-network-publishing', // Unique ID 'lbry-network-publishing', // Unique ID
'LBRY Network', // Title 'LBRY Network', // Title
@ -67,6 +67,10 @@ class LBRY_Network
*/ */
public function save_post_meta($post_id, $post) public function save_post_meta($post_id, $post)
{ {
if ($post->post_type != 'post') {
return;
}
// Verify the nonce before proceeding. // Verify the nonce before proceeding.
if (!isset($_POST['_lbrynonce']) || !wp_verify_nonce($_POST['_lbrynonce'], 'lbry_publish_channels')) { if (!isset($_POST['_lbrynonce']) || !wp_verify_nonce($_POST['_lbrynonce'], 'lbry_publish_channels')) {
return $post_id; return $post_id;

View file

@ -18,7 +18,7 @@ class LBRY_Network_Parser
// COMBAK: Composer is not safe in a wordpress environment. May have to write our own package. // COMBAK: Composer is not safe in a wordpress environment. May have to write our own package.
require_once LBRY_ABSPATH . 'vendor/autoload.php'; require_once LBRY_ABSPATH . 'vendor/autoload.php';
$this->converter = new HtmlConverter(array( $this->converter = new HtmlConverter(array(
'strip_tags' => true 'strip_tags' => false
)); ));
} }

View file

@ -19,6 +19,7 @@ class LBRY_Network_Publisher
* @param int $post_id The ID of the post we are publishing * @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 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)
{ {
// Leave if nothing to publish to // Leave if nothing to publish to
@ -32,27 +33,39 @@ class LBRY_Network_Publisher
$converted = LBRY()->network->parser->convert_to_markdown($post); $converted = LBRY()->network->parser->convert_to_markdown($post);
$write_status = $file && fwrite($file, $converted); $write_status = $file && fwrite($file, $converted);
fclose($file); fclose($file);
$endtime = microtime(true);
// TODO: Catch relative exceptions if necessary
try { try {
// If everything went well with the conversion, carry on // If everything went well with the conversion, carry on
if ($write_status) { if ($write_status) {
$featured_image = get_the_post_thumbnail($post);
$name = $post->post_name; $name = $post->post_name;
$bid = floatval(get_option(LBRY_SETTINGS)[LBRY_LBC_PUBLISH]); $bid = floatval(get_option(LBRY_SETTINGS)[LBRY_LBC_PUBLISH]);
$title = $post->post_title; $title = $post->post_title;
$language = substr(get_locale(), 0, 2); $language = substr(get_locale(), 0, 2);
$license = get_option(LBRY_SETTINGS)[LBRY_LICENSE]; $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;
LBRY()->daemon->publish($name, $bid, $filepath, $title, $description, $language, $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;
// 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) {
error_log('Issue publishing post ' . $post->ID . ' to LBRY: ' . $e->getMessage());
} finally { } finally {
// Delete the temporary markdown file //Delete the temporary markdown file
unlink($filepath); unlink($filepath);
} }
} }

View file

@ -21,14 +21,13 @@ class LBRY_Speech
if (is_admin()) { if (is_admin()) {
add_action('save_post', array($this, 'upload_media'), 10, 2); 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'));
} }
/** /**
@ -52,7 +51,7 @@ class LBRY_Speech
$all_media = $this->find_media($post_id); $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) { if ($all_media) {
$requests = array(); $requests = array();

View file

@ -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_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_POST_CHANNEL', 'lbry_channel'); // The meta key for which channel to publish
$this->define('LBRY_AVAILABLE_LICENSES', array( $this->define('LBRY_AVAILABLE_LICENSES', array(
'mit' => 'MIT', 'Creative Commons Attribution 4.0 International' => 'Creative Commons Attribution 4.0 International',
'license2' => 'License 2', 'Creative Commons Attribution-ShareAlike 4.0 International' => 'Creative Commons Attribution-ShareAlike 4.0 International',
'license3' => 'License 3' '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->admin = new LBRY_Admin();
$this->notice = new LBRY_Admin_Notice(); $this->notice = new LBRY_Admin_Notice();
$this->network = new LBRY_Network(); $this->network = new LBRY_Network();
} else {
// $this->speech->maybe_rewrite_urls();
} }
} }
@ -149,6 +151,10 @@ class LBRYPress
{ {
register_activation_hook(LBRY_PLUGIN_FILE, array($this, 'activate')); register_activation_hook(LBRY_PLUGIN_FILE, array($this, 'activate'));
register_deactivation_hook(LBRY_PLUGIN_FILE, array($this, 'deactivate')); 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);
} }
/** /**
@ -192,11 +198,32 @@ class LBRYPress
*/ */
public function deactivate() public function deactivate()
{ {
// Deactivate Wallet Balance cron job // TODO: Stop the daemon
$this->admin->wallet_balance_deactivate();
error_log('Deactivated'); 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 * Utility Functions
*/ */

View file

@ -0,0 +1,4 @@
<div class="lbry-published-banner">
<h5>This post has also been published to the <a href="https://lbry.io" target="_blank">LBRY blockchain</a> network.</h5>
<h5><a href="https://lbry.io/learn" target="_blank">Click Here</a> to learn more about the benefits of content freedom.</h5>
</div>