2018-09-01 01:50:29 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Class for publishing to the LBRY Network
|
|
|
|
*
|
|
|
|
* @package LBRYPress
|
|
|
|
*/
|
|
|
|
|
|
|
|
class LBRY_Network_Publisher
|
|
|
|
{
|
2018-09-11 22:10:15 +02:00
|
|
|
/**
|
|
|
|
* [__construct description]
|
|
|
|
*/
|
|
|
|
public function __construct()
|
2018-09-01 01:50:29 +02:00
|
|
|
{
|
|
|
|
}
|
2018-10-12 08:22:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Publish the post to the LBRY Network
|
|
|
|
* @param int $post_id The ID of the post we are publishing
|
2018-10-12 22:31:23 +02:00
|
|
|
* @param string $channel The Claim ID of the channel we are posting to
|
2018-10-12 08:22:20 +02:00
|
|
|
*/
|
2018-11-01 18:13:55 +01:00
|
|
|
// NOTE: This is currently sitting at about 150ms, mostly the post parsing
|
2018-10-12 22:31:23 +02:00
|
|
|
public function publish($post, $channel)
|
2018-10-12 08:22:20 +02:00
|
|
|
{
|
2018-11-09 01:29:08 +01:00
|
|
|
|
|
|
|
// TODO: Handle unnatributed channel
|
2018-10-12 09:42:40 +02:00
|
|
|
// Leave if nothing to publish to
|
2018-10-12 22:31:23 +02:00
|
|
|
if (!$channel) {
|
2018-10-12 09:42:40 +02:00
|
|
|
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);
|
2018-11-01 18:13:55 +01:00
|
|
|
$endtime = microtime(true);
|
2018-10-12 09:42:40 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
// If everything went well with the conversion, carry on
|
|
|
|
if ($write_status) {
|
|
|
|
$name = $post->post_name;
|
2018-11-04 00:37:32 +01:00
|
|
|
$bid = number_format(floatval(get_option(LBRY_SETTINGS)[LBRY_LBC_PUBLISH]), 2, '.', '');
|
2018-10-12 09:42:40 +02:00
|
|
|
$title = $post->post_title;
|
|
|
|
$language = substr(get_locale(), 0, 2);
|
|
|
|
$license = get_option(LBRY_SETTINGS)[LBRY_LICENSE];
|
2018-11-01 18:13:55 +01:00
|
|
|
|
|
|
|
// Setup featured image
|
|
|
|
$featured_id = get_post_thumbnail_id($post);
|
|
|
|
$featured_image = wp_get_attachment_image_src($featured_id, 'medium');
|
2018-10-31 20:48:58 +01:00
|
|
|
$thumbnail = $featured_image[0] ? $featured_image[0] : false;
|
2018-10-12 09:42:40 +02:00
|
|
|
|
2018-11-01 18:13:55 +01:00
|
|
|
// 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);
|
|
|
|
|
2018-11-04 00:37:32 +01:00
|
|
|
//TODO: Switch this to an array of args. This is getting out of hand.
|
|
|
|
LBRY()->daemon->publish($name, $bid, $filepath, $title, $description, $language, $license, $channel, $thumbnail);
|
2018-10-12 09:42:40 +02:00
|
|
|
}
|
2018-10-31 20:48:58 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
error_log('Issue publishing post ' . $post->ID . ' to LBRY: ' . $e->getMessage());
|
2018-10-12 09:42:40 +02:00
|
|
|
} finally {
|
2018-10-31 21:07:21 +01:00
|
|
|
//Delete the temporary markdown file
|
|
|
|
unlink($filepath);
|
2018-10-12 09:42:40 +02:00
|
|
|
}
|
2018-10-12 08:22:20 +02:00
|
|
|
}
|
2018-09-01 01:50:29 +02:00
|
|
|
}
|