diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index 9f1c09d..9abcdd2 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -82,29 +82,36 @@ class LBRY_Daemon /** * Publishes a post to the LBRY Network - * @param [type] $name [description] - * @param [type] $bid [description] - * @param [type] $filepath [description] - * @param [type] $title [description] - * @param [type] $description [description] - * @param [type] $language [description] - * @return [type] [description] + * @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', - array( - 'name' => $name, - 'bid' => $bid, - 'file_path' => $filepath, - 'title' => $title, - 'description' => $description, - 'language' => $language, - 'channel_name' => $channel - ) + $args ); + $this->check_for_errors($result); return $result; } diff --git a/classes/LBRY_Network.php b/classes/LBRY_Network.php index 346059b..3ad4b8e 100644 --- a/classes/LBRY_Network.php +++ b/classes/LBRY_Network.php @@ -48,6 +48,7 @@ class LBRY_Network */ public function add_meta_boxes() { + // TODO: Support post types based on user selection add_meta_box( 'lbry-network-publishing', // Unique ID 'LBRY Network', // Title @@ -60,6 +61,9 @@ class 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) { @@ -74,23 +78,30 @@ class LBRY_Network return $post_id; } - $meta_key = 'lbry_channels'; - $new_channels = (isset($_POST[$meta_key]) ? $_POST[$meta_key] : null); - $cur_channels = get_post_meta($post_id, $meta_key); + $will_publish = (isset($_POST[LBRY_WILL_PUBLISH]) ? $_POST[LBRY_WILL_PUBLISH] : false); + $new_channel = (isset($_POST[LBRY_POST_CHANNEL]) ? $_POST[LBRY_POST_CHANNEL] : null); + $cur_channel = get_post_meta($post_id, LBRY_POST_CHANNEL, true); - // COMBAK: Make this a bit more efficent if they have lots of channels - // Start with clean meta, then add new channels if there are any - delete_post_meta($post_id, $meta_key); - if ($new_channels) { - foreach ($new_channels as $channel) { - add_post_meta($post_id, $meta_key, $channel); - } + // Update meta acordingly + if (!$will_publish) { + update_post_meta($post_id, LBRY_WILL_PUBLISH, 'false'); + } else { + update_post_meta($post_id, LBRY_WILL_PUBLISH, 'true'); + } + if ($new_channel !== $cur_channel) { + update_post_meta($post_id, LBRY_POST_CHANNEL, $new_channel); } - // Publish the post on the LBRY Network - $this->publisher->publish($post, $new_channels); + if ($will_publish) { + // 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) { require_once(LBRY_ABSPATH . 'templates/meta_box.php'); diff --git a/classes/LBRY_Network_Publisher.php b/classes/LBRY_Network_Publisher.php index 6d4cefa..2648401 100644 --- a/classes/LBRY_Network_Publisher.php +++ b/classes/LBRY_Network_Publisher.php @@ -17,12 +17,12 @@ class LBRY_Network_Publisher /** * Publish the post to the LBRY Network * @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 - if (!$channels) { + if (!$channel) { return; } @@ -40,7 +40,7 @@ class LBRY_Network_Publisher $featured_image = get_the_post_thumbnail($post); $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; $language = substr(get_locale(), 0, 2); $license = get_option(LBRY_SETTINGS)[LBRY_LICENSE]; @@ -49,9 +49,7 @@ class LBRY_Network_Publisher // TODO: Bring thumbnails into the mix // $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 { // Delete the temporary markdown file diff --git a/classes/lbrypress.php b/classes/lbrypress.php index 721ef56..c228699 100644 --- a/classes/lbrypress.php +++ b/classes/lbrypress.php @@ -66,7 +66,6 @@ class LBRYPress spl_autoload_register(array($this, 'lbry_autoload_register')); $this->init(); $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_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_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( 'mit' => 'MIT', 'license2' => 'License 2', @@ -192,4 +193,19 @@ class LBRYPress { 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); + } } diff --git a/templates/meta_box.php b/templates/meta_box.php index 35d0846..ed05828 100644 --- a/templates/meta_box.php +++ b/templates/meta_box.php @@ -1,28 +1,43 @@ '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(); -array_unshift($channels, $unnatributed); -$cur_channels = get_post_meta($post->ID, 'lbry_channels'); +$channels[] = $unnatributed; +// 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); ?> -