Merged in post_widget (pull request #5)

Post widget
This commit is contained in:
Paul Kirby 2018-10-12 22:01:34 +00:00
commit 8953a3cac1
6 changed files with 164 additions and 51 deletions

View file

@ -80,6 +80,42 @@ class LBRY_Daemon
return $result->result; return $result->result;
} }
/**
* Publishes a post to the LBRY Network
* @param string $name The slug for the post
* @param float $bid The amount of LBC to bid
* @param string $filepath The path of the temporary content file
* @param string $title The Title of the post
* @param string $description The Description of the Post
* @param string $language Two letter ISO Code of the language
* @return string $channel The Claim ID of the Channel
*/
public function publish($name, $bid, $filepath, $title, $description, $language, $channel)
{
$args = array(
'name' => $name,
'bid' => $bid,
'file_path' => $filepath,
'title' => $title,
'description' => $description,
'language' => $language,
);
// Make sure we aren't publishing to unattributed
if ($channel != 'null') {
$args['channel_id'] = $channel;
}
// TODO: Bring thumbnails into the mix
$result = $this->request(
'publish',
$args
);
$this->check_for_errors($result);
return $result;
}
/** /**
* 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

View file

@ -48,6 +48,7 @@ class LBRY_Network
*/ */
public function add_meta_boxes() public function add_meta_boxes()
{ {
// TODO: 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
@ -60,6 +61,9 @@ 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 WP_Post $post The Post Object we are saving
* @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)
{ {
@ -74,23 +78,30 @@ class LBRY_Network
return $post_id; return $post_id;
} }
$meta_key = 'lbry_channels'; $will_publish = (isset($_POST[LBRY_WILL_PUBLISH]) ? $_POST[LBRY_WILL_PUBLISH] : false);
$new_channels = (isset($_POST[$meta_key]) ? $_POST[$meta_key] : null); $new_channel = (isset($_POST[LBRY_POST_CHANNEL]) ? $_POST[LBRY_POST_CHANNEL] : null);
$cur_channels = get_post_meta($post_id, $meta_key); $cur_channel = get_post_meta($post_id, LBRY_POST_CHANNEL, true);
// COMBAK: Make this a bit more efficent if they have lots of channels // Update meta acordingly
// Start with clean meta, then add new channels if there are any if (!$will_publish) {
delete_post_meta($post_id, $meta_key); update_post_meta($post_id, LBRY_WILL_PUBLISH, 'false');
if ($new_channels) { } else {
foreach ($new_channels as $channel) { update_post_meta($post_id, LBRY_WILL_PUBLISH, 'true');
add_post_meta($post_id, $meta_key, $channel); }
} if ($new_channel !== $cur_channel) {
update_post_meta($post_id, LBRY_POST_CHANNEL, $new_channel);
} }
// Publish the post on the LBRY Network if ($will_publish) {
$this->publisher->publish($post, $new_channels); // Publish the post on the LBRY Network
$this->publisher->publish($post, get_post_meta($post_id, LBRY_POST_CHANNEL, true));
}
} }
/**
* Returns the HTML for the LBRY Meta Box
* @param [type] $post [description]
*/
public function meta_box_html($post) public function meta_box_html($post)
{ {
require_once(LBRY_ABSPATH . 'templates/meta_box.php'); require_once(LBRY_ABSPATH . 'templates/meta_box.php');

View file

@ -22,18 +22,22 @@ class LBRY_Network_Parser
)); ));
} }
public function convert_to_markdown($post_id) /**
* Converts a post into markdown.
* @param WP_Post $post The post to be converted
* @return string
*/
public function convert_to_markdown($post)
{ {
$post = get_post($post_id); // $title = '<h1>' . $post->post_title . '</h1>';
$title = '<h1>' . $post->post_title . '</h1>'; //
// $featured_image = get_the_post_thumbnail($post);
$featured_image = get_the_post_thumbnail($post); //
// $content = $title;
$content = $title; // if ($featured_image) {
if ($featured_image) { // $content .= $featured_image . '<br />';
$content .= $featured_image . '<br />'; // }
} $content = apply_filters('the_content', $post->post_content);
$content .= apply_filters('the_content', get_post($post_id)->post_content);
$converted = $this->converter->convert($content); $converted = $this->converter->convert($content);
return $converted; return $converted;

View file

@ -17,12 +17,43 @@ class LBRY_Network_Publisher
/** /**
* 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 array $channels An array of channels we are publishing to * @param string $channel The Claim ID of the channel we are posting to
*/ */
public function publish($post, $channels) public function publish($post, $channel)
{ {
$name = $post->post_name; // Leave if nothing to publish to
if (!$channel) {
return; 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);
// TODO: Catch relative exceptions if necessary
try {
// If everything went well with the conversion, carry on
if ($write_status) {
$featured_image = get_the_post_thumbnail($post);
$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;
// TODO: Bring thumbnails into the mix
// $thumbnail = $featured_image ? $featured_image : null;
LBRY()->daemon->publish($name, $bid, $filepath, $title, $description, $language, $channel);
}
} finally {
// Delete the temporary markdown file
unlink($filepath);
}
} }
} }

View file

@ -101,6 +101,8 @@ 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_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', 'mit' => 'MIT',
'license2' => 'License 2', 'license2' => 'License 2',
@ -191,4 +193,19 @@ class LBRYPress
{ {
error_log('Deactivated'); error_log('Deactivated');
} }
/*
* Utility Functions
*/
public static function channel_name_comp($a, $b)
{
if ($a->name === $b->name) {
return 0;
}
if ($b->name == '(none / unattributed)') {
return -1;
}
return strnatcasecmp($a->name, $b->name);
}
} }

View file

@ -1,29 +1,43 @@
<?php <?php
$LBRY = LBRY();
$unnatributed = (object) array( $unnatributed = (object) array(
'name' => 'Unattributed', 'name' => '(none / unattributed)',
'permanent_url' => 'unattributed' '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();
array_unshift($channels, $unnatributed); $channels[] = $unnatributed;
$cur_channels = get_post_meta($post->ID, 'lbry_channels'); // Sort the channels in a natural way
usort($channels, array('LBRYPress', 'channel_name_comp'));
$cur_channel = get_post_meta($post->ID, LBRY_POST_CHANNEL, true);
$will_publish = get_post_meta($post->ID, LBRY_WILL_PUBLISH, true);
?> ?>
<?php wp_nonce_field('lbry_publish_channels', '_lbrynonce'); ?> <?php wp_nonce_field('lbry_publish_channels', '_lbrynonce'); ?>
<h4>Choose which channels you would like to publish this post to:</h4> <div class="lbry-meta-checkbox-wrapper">
<ul class="categorychecklist"> <label class="lbry-meta-label">
<?php if ($channels): ?> <input type="checkbox" class="lbry-meta-checkbox" name="<?= LBRY_WILL_PUBLISH ?>" value="true"
<?php foreach ($channels as $channel): ?> <?php
<li> if ($will_publish === 'true' || $will_publish === '') {
<label class="selectit"> echo 'checked';
<input type="checkbox" name="lbry_channels[]" value="<?= $channel->permanent_url ?>" }
<?php if (in_array($channel->permanent_url, $cur_channels)): ?> ?>
checked="true" >
<?php endif; ?> Sync this post on channel:
> </label>
<?= $channel->name ?> </div>
</label> <select class="lbry-meta-select" name="<?= LBRY_POST_CHANNEL ?>">
<br /> <?php foreach ($channels as $index=>$channel): ?>
</li> <option value="<?= $channel->claim_id ?>"
<?php endforeach; ?> <?php
<?php endif; ?> if ($cur_channel) {
</ul> if ($cur_channel === $channel->claim_id) {
echo 'selected';
}
} elseif ($index === 0) {
echo 'selected';
}
?>
>
<?= $channel->name ?>
</option>
<?php endforeach; ?>
</select>