Updated the way the post meta box publishes, using select and checkbox instead of checkbox array

This commit is contained in:
Paul Kirby 2018-10-12 15:31:23 -05:00
parent 8bc336952d
commit c9a156bb9b
5 changed files with 105 additions and 58 deletions

View file

@ -82,29 +82,36 @@ class LBRY_Daemon
/** /**
* Publishes a post to the LBRY Network * Publishes a post to the LBRY Network
* @param [type] $name [description] * @param string $name The slug for the post
* @param [type] $bid [description] * @param float $bid The amount of LBC to bid
* @param [type] $filepath [description] * @param string $filepath The path of the temporary content file
* @param [type] $title [description] * @param string $title The Title of the post
* @param [type] $description [description] * @param string $description The Description of the Post
* @param [type] $language [description] * @param string $language Two letter ISO Code of the language
* @return [type] [description] * @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)
{ {
// TODO: Bring thumbnails into the mix $args = array(
$result = $this->request(
'publish',
array(
'name' => $name, 'name' => $name,
'bid' => $bid, 'bid' => $bid,
'file_path' => $filepath, 'file_path' => $filepath,
'title' => $title, 'title' => $title,
'description' => $description, 'description' => $description,
'language' => $language, 'language' => $language,
'channel_name' => $channel
)
); );
// 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); $this->check_for_errors($result);
return $result; return $result;
} }

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);
} }
if ($will_publish) {
// Publish the post on the LBRY Network // Publish the post on the LBRY Network
$this->publisher->publish($post, $new_channels); $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

@ -17,12 +17,12 @@ 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)
{ {
// Leave if nothing to publish to // Leave if nothing to publish to
if (!$channels) { if (!$channel) {
return; return;
} }
@ -40,7 +40,7 @@ class LBRY_Network_Publisher
$featured_image = get_the_post_thumbnail($post); $featured_image = get_the_post_thumbnail($post);
$name = $post->post_name; $name = $post->post_name;
$bid = 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];
@ -49,10 +49,8 @@ class LBRY_Network_Publisher
// TODO: Bring thumbnails into the mix // TODO: Bring thumbnails into the mix
// $thumbnail = $featured_image ? $featured_image : null; // $thumbnail = $featured_image ? $featured_image : null;
foreach ($channels as $channel) {
LBRY()->daemon->publish($name, $bid, $filepath, $title, $description, $language, $channel); LBRY()->daemon->publish($name, $bid, $filepath, $title, $description, $language, $channel);
} }
}
} finally { } finally {
// Delete the temporary markdown file // Delete the temporary markdown file
unlink($filepath); unlink($filepath);

View file

@ -66,7 +66,6 @@ class LBRYPress
spl_autoload_register(array($this, 'lbry_autoload_register')); spl_autoload_register(array($this, 'lbry_autoload_register'));
$this->init(); $this->init();
$this->init_hooks(); $this->init_hooks();
error_log("language: " . get_locale());
} }
/** /**
@ -102,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',
@ -192,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,28 +1,43 @@
<?php <?php
$LBRY = LBRY();
$unnatributed = (object) array( $unnatributed = (object) array(
'name' => 'unattributed', 'name' => '(none / 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->name ?>" }
<?php if (in_array($channel->name, $cur_channels)): ?> ?>
checked="true" >
<?php endif; ?> Sync this post on channel:
</label>
</div>
<select class="lbry-meta-select" name="<?= LBRY_POST_CHANNEL ?>">
<?php foreach ($channels as $index=>$channel): ?>
<option value="<?= $channel->claim_id ?>"
<?php
if ($cur_channel) {
if ($cur_channel === $channel->claim_id) {
echo 'selected';
}
} elseif ($index === 0) {
echo 'selected';
}
?>
> >
<?= $channel->name ?> <?= $channel->name ?>
</label> </option>
<br />
</li>
<?php endforeach; ?> <?php endforeach; ?>
<?php endif; ?> </select>
</ul>