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

This commit is contained in:
Paul Kirby 2018-11-09 12:34:12 -06:00
parent a35f857d05
commit b0e7215427
8 changed files with 76 additions and 104 deletions

View file

@ -115,8 +115,10 @@ class LBRYPress
$this->define('LBRY_SPEECH', 'lbry_speech'); // the spee.ch address $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_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_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_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_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; return 0;
} }
if ($b->name == '(none / unattributed)') { if ($b->claim_id == 'null') {
return -1; return -1;
} }
return strnatcasecmp($a->name, $b->name); return strnatcasecmp($a->name, $b->name);

View file

@ -7,8 +7,16 @@
class LBRY_Daemon class LBRY_Daemon
{ {
/**
* The address of the Lbry Daemon
* @var string
*/
private $address = 'localhost:5279'; private $address = 'localhost:5279';
/**
* The Daemon Logger
* @var LBRY_Daemon_Logger
*/
private $logger = null; private $logger = null;
/** /**
@ -17,17 +25,12 @@ class LBRY_Daemon
public function __construct() public function __construct()
{ {
$this->logger = new LBRY_Daemon_Logger(); $this->logger = new LBRY_Daemon_Logger();
$this->init();
}
public function init()
{
} }
/** /**
* Returns an unused address * Returns an unused address
* https://lbry.tech/api/sdk#address_unused * 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() public function wallet_unused_address()
{ {
@ -44,7 +47,7 @@ class LBRY_Daemon
/** /**
* Returns an array of Address lists * Returns an array of Address lists
* https://lbry.tech/api/sdk#address_list * 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() public function address_list()
{ {
@ -61,8 +64,8 @@ class LBRY_Daemon
/** /**
* Returns the balance of a current LBRY account * Returns the balance of a current LBRY account
* https://lbry.tech/api/sdk#account_balance * https://lbry.tech/api/sdk#account_balance
* @param string $address Wallet Address * @param string $address Wallet Address
* @return float Wallet Balance * @return float Wallet Balance
*/ */
public function wallet_balance() public function wallet_balance()
{ {
@ -79,7 +82,7 @@ class LBRY_Daemon
/** /**
* Returns a list of channels for this account * Returns a list of channels for this account
* https://lbry.tech/api/sdk#channel_list * 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() public function channel_list()
{ {
@ -96,12 +99,11 @@ class LBRY_Daemon
/** /**
* Create a claim for a new channel * Create a claim for a new channel
* https://lbry.tech/api/sdk#channel_new * 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) public function channel_new($channel_name, $bid_amount)
{ {
// TODO: Sanitize channel name and bid // TODO: Sanitize channel name and bid
// Make sure no @ sign, as we will add that // Make sure no @ sign, as we will add that
if (strpos($channel_name, '@')) { if (strpos($channel_name, '@')) {
throw new \Exception('Illegal character "@" in channel name', 1); throw new \Exception('Illegal character "@" in channel name', 1);
@ -133,35 +135,15 @@ class LBRY_Daemon
/** /**
* Publishes a post to the LBRY Network * Publishes a post to the LBRY Network
* https://lbry.tech/api/sdk#publish * https://lbry.tech/api/sdk#publish
* @param string $name The slug for the post * @param array $args An array containing necessary data for publish post
* @param float $bid The amount of LBC to bid *
* @param string $filepath The path of the temporary content file * Available params:
* @param string $title The Title of the post * ['name', 'bid', 'file_path', 'title', 'description', 'language', 'license', 'channel_id', 'thumbnail']
* @param string $description The Description of the Post *
* @param string $language Two letter ISO Code of the language * @return object $result
* @return string $channel The Claim ID of the Channel
*/ */
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 { try {
$result = $this->request( $result = $this->request(
'publish', 'publish',
@ -177,9 +159,9 @@ class LBRY_Daemon
/** /**
* Sends a cURL request to the LBRY Daemon * Sends a cURL request to the LBRY Daemon
* @param string $method The method to call on the LBRY API * @param string $method The method to call on the LBRY API
* @param array $params The Parameters to send the LBRY API Call * @param array $params The Parameters to send the LBRY API Call
* @return string The cURL response * @return string The cURL response
*/ */
private function request($method, $params = array()) private function request($method, $params = array())
{ {

View file

@ -20,9 +20,6 @@ class LBRY_Network
*/ */
public $parser = null; public $parser = null;
/**
* [__construct description]
*/
public function __construct() public function __construct()
{ {
$this->publisher = new LBRY_Network_Publisher(); $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 * 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 int $post_id The ID of the post we are saving
* @param WP_Post $post The Post Object 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) public function save_post_meta($post_id, $post)
{ {
@ -104,7 +101,7 @@ class LBRY_Network
/** /**
* Returns the HTML for the LBRY Meta Box * Returns the HTML for the LBRY Meta Box
* @param [type] $post [description] * @param WP_POST $post
*/ */
public function meta_box_html($post) public function meta_box_html($post)
{ {

View file

@ -7,28 +7,14 @@
class LBRY_Network_Publisher class LBRY_Network_Publisher
{ {
/**
* [__construct description]
*/
public function __construct()
{
}
/** /**
* Publish the post to the LBRY Network * Publish the post to the LBRY Network
* @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 // 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 // Get converted markdown into a file
$filepath = LBRY_ABSPATH . 'tmp/' . $post->post_name . time() . '.md'; $filepath = LBRY_ABSPATH . 'tmp/' . $post->post_name . time() . '.md';
$file = fopen($filepath, 'w'); $file = fopen($filepath, 'w');
@ -40,16 +26,27 @@ class LBRY_Network_Publisher
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) {
$name = $post->post_name; $args = array(
$bid = number_format(floatval(get_option(LBRY_SETTINGS)[LBRY_LBC_PUBLISH]), 2, '.', ''); 'name' => $post->post_name,
$title = $post->post_title; 'bid' => number_format(floatval(get_option(LBRY_SETTINGS)[LBRY_LBC_PUBLISH]), 2, '.', ''),
$language = substr(get_locale(), 0, 2); 'file_path' => $filepath,
$license = get_option(LBRY_SETTINGS)[LBRY_LICENSE]; '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 // Setup featured image
$featured_id = get_post_thumbnail_id($post); $featured_id = get_post_thumbnail_id($post);
$featured_image = wp_get_attachment_image_src($featured_id, 'medium'); $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 // Build description using Yoast if installed and its used, excerpt/title otherwise
$description = false; $description = false;
@ -62,8 +59,12 @@ class LBRY_Network_Publisher
} }
$description .= ' | Originally published at ' . get_permalink($post); $description .= ' | Originally published at ' . get_permalink($post);
//TODO: Switch this to an array of args. This is getting out of hand. $args['description'] = $description;
LBRY()->daemon->publish($name, $bid, $filepath, $title, $description, $language, $license, $channel, $thumbnail);
$result = LBRY()->daemon->publish($args);
if ($result->success) {
update_post_meta($post->ID, LBRY_PERM_URL, $result->output->permanent_url);
}
} }
} catch (Exception $e) { } catch (Exception $e) {
error_log('Issue publishing post ' . $post->ID . ' to LBRY: ' . $e->getMessage()); error_log('Issue publishing post ' . $post->ID . ' to LBRY: ' . $e->getMessage());

View file

@ -120,9 +120,9 @@ class LBRY_Speech
if ($result && $result->success) { if ($result && $result->success) {
$meta = wp_get_attachment_metadata($media->id); $meta = wp_get_attachment_metadata($media->id);
if ($media->image_size) { 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 { } else {
$meta['speech_asset_url'] = $result->data->serveUrl; $meta[LBRY_SPEECH_ASSET_URL] = $result->data->serveUrl;
} }
wp_update_attachment_metadata($media->id, $meta); wp_update_attachment_metadata($media->id, $meta);
} else { // Something unhandled happened here } else { // Something unhandled happened here
@ -219,7 +219,7 @@ class LBRY_Speech
*/ */
public function is_published($meta) 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; return true;
} }

View file

@ -19,8 +19,8 @@ class LBRY_Speech_Parser
foreach ($sources as $width => $source) { foreach ($sources as $width => $source) {
// Check to see if it is using base image first // Check to see if it is using base image first
if ($image_src == $source['url'] && key_exists('speech_asset_url', $image_meta)) { if ($image_src == $source['url'] && key_exists(LBRY_SPEECH_ASSET_URL, $image_meta)) {
$new_sources[$width]['url'] = $image_meta['speech_asset_url']; $new_sources[$width]['url'] = $image_meta[LBRY_SPEECH_ASSET_URL];
continue; 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 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']) { 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; return $new_image;
} }
@ -65,22 +65,19 @@ class LBRY_Speech_Parser
if (is_string($size)) { if (is_string($size)) {
switch ($size) { switch ($size) {
case 'full': case 'full':
$new_image[0] = $image_meta['speech_asset_url']; $new_image[0] = $image_meta[LBRY_SPEECH_ASSET_URL];
break; break;
case 'post-thumbnail': case 'post-thumbnail':
if (LBRY()->speech->is_published($sizes['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; break;
default: default:
if (key_exists($size, $sizes) && LBRY()->speech->is_published($sizes[$size])) { 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; break;
} }
// $time_end = microtime(true);
// $time = ($time_end - $time_start) * 1000;
// error_log("attachment image source in $time milliseconds");
return $new_image; 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 // COMBAK: Need to make this a bit faster. Sitting between 100 and 300ms currently
public function replace_urls_with_speech($content) public function replace_urls_with_speech($content)
{ {
// $time_start = microtime(true);
$new_content = $content; $new_content = $content;
$assets = array(); $assets = array();
@ -131,10 +127,6 @@ class LBRY_Speech_Parser
$new_content = str_replace($asset, $speech_url, $new_content); $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; return $new_content;
} }
@ -247,8 +239,8 @@ class LBRY_Speech_Parser
private function find_speech_url_by_width($sizes, $width) private function find_speech_url_by_width($sizes, $width)
{ {
foreach ($sizes as $key => $size) { foreach ($sizes as $key => $size) {
if ($size['width'] == $width && key_exists('speech_asset_url', $size)) { if ($size['width'] == $width && key_exists(LBRY_SPEECH_ASSET_URL, $size)) {
return $size['speech_asset_url']; return $size[LBRY_SPEECH_ASSET_URL];
} }
} }
@ -265,7 +257,7 @@ class LBRY_Speech_Parser
{ {
// See if this looks like video meta // See if this looks like video meta
if (!key_exists('file', $meta) && key_exists('mime_type', $meta) && $meta['mime_type'] == 'video/mp4') { 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); $pathinfo = pathinfo($url);
@ -273,15 +265,15 @@ class LBRY_Speech_Parser
// Check main file or if $meta is just a url (video) first // Check main file or if $meta is just a url (video) first
if (key_exists('file', $meta) && $basename == wp_basename($meta['file'])) { 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 // Check to see if we have a meta option here
if (key_exists('sizes', $meta)) { if (key_exists('sizes', $meta)) {
// Then check sizes // Then check sizes
foreach ($meta['sizes'] as $size => $meta) { foreach ($meta['sizes'] as $size => $meta) {
if ($basename == $meta['file'] && key_exists('speech_asset_url', $meta)) { if ($basename == $meta['file'] && key_exists(LBRY_SPEECH_ASSET_URL, $meta)) {
return $meta['speech_asset_url']; return $meta[LBRY_SPEECH_ASSET_URL];
} }
} }
} }
@ -326,7 +318,6 @@ class LBRY_Speech_Parser
$post_id = attachment_url_to_postid($url); $post_id = attachment_url_to_postid($url);
} }
} }
return (int) $post_id; return (int) $post_id;
} }
} }

View file

@ -1,9 +1,8 @@
<?php <?php
$unnatributed = (object) array( $unnatributed = (object) array(
'name' => '(none / unattributed)', 'name' => 'none (anonymous)',
'claim_id' => 'null' 'claim_id' => 'null'
); );
// TODO: Test what happens with empty channel list, can't remember the return value
$channels = LBRY()->daemon->channel_list(); $channels = LBRY()->daemon->channel_list();
$channels[] = $unnatributed; $channels[] = $unnatributed;
// Sort the channels in a natural way // Sort the channels in a natural way

View file

@ -1,4 +1,4 @@
<div class="lbry-published-banner"> <blockquote 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>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> <h5><a href="https://lbry.io/learn" target="_blank">Click Here</a> to learn more about the benefits of content freedom.</h5>
</div> </blockquote>