2018-09-01 01:50:29 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Class for publishing to the LBRY Network
|
|
|
|
*
|
|
|
|
* @package LBRYPress
|
|
|
|
*/
|
2022-02-14 02:34:20 +01:00
|
|
|
defined('ABSPATH') || die(); // Exit if accessed directly
|
2018-09-01 01:50:29 +02:00
|
|
|
|
|
|
|
class LBRY_Network_Publisher
|
|
|
|
{
|
2018-10-12 08:22:20 +02:00
|
|
|
/**
|
|
|
|
* Publish the post to the LBRY Network
|
2020-01-28 04:45:24 +01:00
|
|
|
* @param WP_POST $post_id The ID of the post we are publishing
|
2018-11-09 19:34:12 +01: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
|
2022-02-14 15:43:55 +01:00
|
|
|
public function publish( $post, $channel = null, $license ) {
|
|
|
|
|
|
|
|
$post_id = $post->ID;
|
2018-10-12 09:42:40 +02:00
|
|
|
// Get converted markdown into a file
|
|
|
|
$filepath = LBRY_ABSPATH . 'tmp/' . $post->post_name . time() . '.md';
|
2022-02-12 22:28:25 +01:00
|
|
|
$file = fopen( $filepath, 'w' );
|
|
|
|
$converted = LBRY()->network->parser->convert_to_markdown( $post );
|
|
|
|
$write_status = $file && fwrite( $file, $converted );
|
|
|
|
fclose( $file );
|
2018-10-12 09:42:40 +02:00
|
|
|
|
|
|
|
try {
|
2020-01-28 03:45:10 +01:00
|
|
|
if (!$write_status) {
|
2022-02-12 22:28:25 +01:00
|
|
|
throw new \Exception( 'Write Status failure', 1 );
|
2020-01-28 03:45:10 +01:00
|
|
|
}
|
|
|
|
|
2018-10-12 09:42:40 +02:00
|
|
|
// If everything went well with the conversion, carry on
|
2020-01-28 03:45:10 +01:00
|
|
|
$args = array(
|
|
|
|
'name' => $post->post_name,
|
2022-02-12 22:28:25 +01:00
|
|
|
'bid' => number_format( floatval( get_option( LBRY_SETTINGS )[LBRY_LBC_PUBLISH] ), 3, '.', '' ),
|
2020-01-28 03:45:10 +01:00
|
|
|
'file_path' => $filepath,
|
|
|
|
'title' => $post->post_title,
|
2022-02-12 22:28:25 +01:00
|
|
|
'languages' => array( substr( get_locale(), 0, 2 ) ),
|
2022-02-14 15:43:55 +01:00
|
|
|
'license' => $license,
|
2020-01-28 03:45:10 +01:00
|
|
|
);
|
2018-11-09 19:34:12 +01:00
|
|
|
|
2020-01-28 03:45:10 +01:00
|
|
|
// Setup channel
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( $channel && $channel != 'null' ) {
|
2020-01-28 03:45:10 +01:00
|
|
|
$args['channel_id'] = $channel;
|
|
|
|
}
|
2018-11-01 18:13:55 +01:00
|
|
|
|
2020-01-28 03:45:10 +01:00
|
|
|
// Setup featured image
|
2022-02-12 22:28:25 +01:00
|
|
|
$featured_id = get_post_thumbnail_id( $post );
|
|
|
|
$featured_image = wp_get_attachment_image_src( $featured_id, 'medium' );
|
2018-11-09 19:34:12 +01:00
|
|
|
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( $featured_image[0] ) {
|
2020-01-28 03:45:10 +01:00
|
|
|
$args['thumbnail_url'] = $featured_image[0];
|
|
|
|
}
|
|
|
|
|
2020-01-28 04:45:24 +01:00
|
|
|
// Setup Tags
|
|
|
|
$tags = get_the_terms( $post, 'post_tag' );
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( $tags ) {
|
2020-01-28 04:45:24 +01:00
|
|
|
$tag_names = [];
|
2022-02-12 22:28:25 +01:00
|
|
|
foreach ( $tags as $tag ) {
|
2020-01-28 04:45:24 +01:00
|
|
|
$tag_names[] = $tag->name;
|
|
|
|
}
|
|
|
|
$args['tags'] = $tag_names;
|
|
|
|
}
|
|
|
|
|
2020-01-28 03:45:10 +01:00
|
|
|
// Build description using Yoast if installed and its used, excerpt/title otherwise
|
|
|
|
$description = false;
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( class_exists( 'WPSEO_META' ) ) {
|
|
|
|
$description = WPSEO_META::get_value( 'opengraph-description', $post->ID );
|
2020-01-28 03:45:10 +01:00
|
|
|
}
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( ! $description ) {
|
|
|
|
$excerpt = get_the_excerpt( $post );
|
2020-01-28 03:45:10 +01:00
|
|
|
$description = $excerpt ? $excerpt : $post->post_title;
|
|
|
|
}
|
2022-02-12 22:28:25 +01:00
|
|
|
$description .= ' | Originally published at ' . get_permalink( $post );
|
2018-10-12 09:42:40 +02:00
|
|
|
|
2020-01-28 03:45:10 +01:00
|
|
|
$args['description'] = $description;
|
2018-11-01 18:13:55 +01:00
|
|
|
|
2022-02-12 22:28:25 +01:00
|
|
|
$result = LBRY()->daemon->publish( $args );
|
2020-01-28 03:45:10 +01:00
|
|
|
$outputs = $result->outputs;
|
2018-11-09 19:34:12 +01:00
|
|
|
|
2022-02-12 22:28:25 +01:00
|
|
|
if ( $outputs && is_array( $outputs ) ) {
|
2020-01-28 05:33:59 +01:00
|
|
|
$output = $result->outputs[0];
|
|
|
|
$claim_id = $output->claim_id;
|
|
|
|
// Set Claim ID
|
2022-02-12 22:28:25 +01:00
|
|
|
update_post_meta( $post->ID, LBRY_CLAIM_ID, $claim_id );
|
2020-01-28 05:33:59 +01:00
|
|
|
|
2022-02-16 21:52:43 +01:00
|
|
|
// Set Channel Name Published under
|
|
|
|
$published_channel = $output->signing_channel->name;
|
|
|
|
$channel_claim_id = $output->signing_channel->claim_id;
|
|
|
|
update_post_meta( $post->ID, '_lbry_post_published_channel', $published_channel );
|
|
|
|
update_post_meta( $post->ID, '_lbry_post_pub_channel_claim_id', $channel_claim_id );
|
|
|
|
|
|
|
|
// Set License Published under
|
|
|
|
$published_license = $output->value->license;
|
|
|
|
if ( ( $published_license ) && ( $published_license !== null ) ) {
|
|
|
|
update_post_meta( $post->ID, '_lbry_post_published_license', $published_license );
|
|
|
|
}
|
|
|
|
|
2020-01-28 05:33:59 +01:00
|
|
|
// Set Canonical URL
|
2022-02-12 22:28:25 +01:00
|
|
|
$canonical_url = LBRY()->daemon->canonical_url( $claim_id );
|
|
|
|
update_post_meta( $post->ID, LBRY_CANONICAL_URL, $canonical_url );
|
2022-02-16 21:52:43 +01:00
|
|
|
|
|
|
|
// Set _lbry_is_published to true
|
|
|
|
update_post_meta( $post->ID, '_lbry_is_published', true );
|
2018-10-12 09:42:40 +02:00
|
|
|
}
|
2018-10-31 20:48:58 +01:00
|
|
|
} catch (Exception $e) {
|
2022-02-12 22:28:25 +01:00
|
|
|
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
|
2022-02-12 22:28:25 +01:00
|
|
|
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
|
|
|
}
|