Merge branch 'lbry-publish'

This commit is contained in:
Lem Smyth 2022-02-14 10:08:30 -06:00
commit a00f979bf3
14 changed files with 168 additions and 136 deletions

1
.gitignore vendored
View file

@ -4,7 +4,6 @@
!*.*
.DS_Store
extensions.json
.vscode/*
tmp/*

2
admin/index.php Normal file
View file

@ -0,0 +1,2 @@
<?php
// Silence is golden

View file

@ -120,8 +120,9 @@ class LBRYPress
$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_post_pub_channel'); // The meta key for which channel to publish
$this->define('LBRY_POST_LICENSE', '_lbry_post_pub_license'); // The meta key for which license to publish on
$this->define('LBRY_POST_CHANNEL', '_lbry_channel'); // The meta key for which channel to publish
$this->define('LBRY_POST_PUB_CHANNEL', '_lbry_post_pub_channel'); // The meta key for which channel to publish on
$this->define('LBRY_POST_PUB_LICENSE', '_lbry_post_pub_license'); // The meta key for which license to publish on
$this->define('LBRY_CLAIM_ID', '_lbry_claim_id'); // The Claim ID for the post as it was published on LBRY
$this->define('LBRY_CANONICAL_URL', '_lbry_canonical_url'); // The canonical url for the published lbry post
$this->define('LBRY_SPEECH_ASSET_URL', 'speech_asset_url'); // The meta key for an asset's speech url

View file

@ -27,14 +27,14 @@ class LBRY_Admin
public function create_options_page()
{
$hook_suffix = add_menu_page(
__( 'LBRYPress Settings', 'lbrypress' ),
__( 'LBRYPress', 'lbrypress' ),
'manage_options',
LBRY_ADMIN_PAGE,
array( $this, 'options_page_html' ),
add_menu_page(
__( 'LBRYPress Settings', 'lbrypress' ),
__( 'LBRYPress', 'lbrypress' ),
'manage_options',
LBRY_ADMIN_PAGE,
array( $this, 'options_page_html' ),
plugin_dir_url( LBRY_PLUGIN_FILE ) . '/admin/images/lbry-icon.png'
);
);
// Admin stylesheet enqueue
function load_admin_stylesheet( $hook ) {
@ -69,11 +69,10 @@ class LBRY_Admin
/**
* Returns the Options Page HTML for the plugin
*/
public function options_page_html() {
//$LBRY = LBRY();
public function options_page_html()
{
// Set class properties to be referenced in callbacks
$this->options = get_option( LBRY_SETTINGS );
//$this->options_channel = get_option( 'lbry_channel_settings' );
$this->options_speech = get_option( LBRY_SPEECH_SETTINGS );
require_once( LBRY_ABSPATH . 'templates/options-page.php' );
}
@ -115,14 +114,6 @@ class LBRY_Admin
LBRY_SETTINGS_SECTION_GENERAL
);
add_settings_field(
'lbry_default_publish_setting',
'Always Publish to LBRY',
array( $this, 'lbry_always_pub_callback' ),
LBRY_ADMIN_PAGE,
LBRY_SETTINGS_SECTION_GENERAL
);
add_settings_field(
'default_lbry_channel',
'Default Publish Channel',
@ -130,14 +121,6 @@ class LBRY_Admin
LBRY_ADMIN_PAGE,
LBRY_SETTINGS_SECTION_GENERAL
);
// remove from commit:
add_settings_field(
'lbry_default_publish_setting',
'Always Publish to LBRY',
array( $this, 'lbry_always_pub_callback' ),
LBRY_ADMIN_PAGE,
LBRY_SETTINGS_SECTION_GENERAL
);
add_settings_field(
'lbry_default_publish_setting',
@ -232,7 +215,7 @@ class LBRY_Admin
return $new_input;
}
public function sanitize_speech_settings( $input )
public function sanitize_speech_settings( $input )
{
$new_input = get_option( LBRY_SPEECH_SETTINGS );
if ( isset( $input[LBRY_SPEECH] ) ) {
@ -306,7 +289,7 @@ class LBRY_Admin
$address
);
}
// remove from commit:
/**
* Checkbox to default to always allow publish on LBRY
*/
@ -383,7 +366,6 @@ class LBRY_Admin
);
}
/**
* Prints LBC per publish input
*/
@ -444,7 +426,6 @@ class LBRY_Admin
);
}
/**
* Handles new channel form submission
*/

View file

@ -9,7 +9,8 @@ defined('ABSPATH') || die(); // Exit if accessed directly
class LBRY_Admin_Notice
{
public function __construct() {
public function __construct()
{
add_action( 'admin_notices', array( $this, 'admin_notices' ) );
}

View file

@ -75,9 +75,8 @@ class LBRY_Daemon
*/
public function wallet_balance()
{
try { // Convert JSON string to an object
$result = $this->request( 'wallet_balance', array() );
$result = $this->request( 'wallet_balance' );
return $result;
} catch (LBRYDaemonException $e) {
$this->logger->log('wallet_balance error', $e->getMessage() . ' | Code: ' . $e->getCode());

View file

@ -40,9 +40,8 @@ class LBRY_Network
// Save the post meta on 'save_post' hook
add_action( 'wp_insert_post', array( $this, 'save_post_meta' ), 11, 2 );
// Checkbox inside the WordPres meta box near "Publish" button
// Checkbox inside the WordPres meta box near "Publish" button
add_action( 'post_submitbox_misc_actions', array( $this, 'publish_to_lbry_checkbox' ) );
}
/**
@ -84,48 +83,34 @@ class LBRY_Network
if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) {
return $post_id;
}
if ( ( $_POST['_lbry_will_publish'] ) && $_POST['_lbry_will_publish'] != get_post_meta( $post_id, '_lbry_will_publish', true ) ) {
update_post_meta( $post_id, '_lbry_will_publish', $_POST['_lbry_will_publish'] );
} elseif ( ! isset( $_POST['_lbry_will_publish'] ) ) {
update_post_meta( $post_id, '_lbry_will_publish', 0 );
if ( ( $_POST[LBRY_WILL_PUBLISH] ) && $_POST[LBRY_WILL_PUBLISH] != get_post_meta( $post_id, LBRY_WILL_PUBLISH, true ) ) {
update_post_meta( $post_id, LBRY_WILL_PUBLISH, $_POST[LBRY_WILL_PUBLISH] );
} elseif ( ! isset( $_POST[LBRY_WILL_PUBLISH] ) ) {
update_post_meta( $post_id, LBRY_WILL_PUBLISH, 0 );
}
$channel = $_POST[LBRY_POST_CHANNEL];
$cur_channel = ( get_post_meta( $post_id, LBRY_POST_CHANNEL, true ) ? get_post_meta( $post_id,LBRY_POST_CHANNEL, true ) : get_post_meta( $post_id, '_lbry_channel', true ) );
$license = $_POST['_lbry_post_pub_license'];
$cur_license = get_post_meta( $post_id, '_lbry_post_pub_license', true );
$will_publish = $_POST['_lbry_will_publish'];
$channel = $_POST[LBRY_POST_PUB_CHANNEL];
$cur_channel = ( get_post_meta( $post_id, LBRY_POST_PUB_CHANNEL, true ) ? get_post_meta( $post_id, LBRY_POST_PUB_CHANNEL, true ) : get_post_meta( $post_id, '_lbry_channel', true ) );
$license = $_POST[LBRY_POST_PUB_LICENSE];
$cur_license = get_post_meta( $post_id, LBRY_POST_PUB_LICENSE, true );
$will_publish = $_POST[LBRY_WILL_PUBLISH];
// Update meta acordingly
if ( $channel !== $cur_channel ) {
update_post_meta( $post_id, LBRY_POST_CHANNEL, $channel );
update_post_meta( $post_id, LBRY_POST_PUB_CHANNEL, $channel );
delete_post_meta( $post_id, '_lbry_channel'); // remove the _lbry_channel if already set from the post and replaces with _lbry_post_pub_channel to avoid confusion
} elseif ( $channel === $cur_channel && ( $cur_channel === get_post_meta( $post_id, '_lbry_channel', true ) ) ) {
update_post_meta( $post_id, LBRY_POST_CHANNEL, $channel );
update_post_meta( $post_id, LBRY_POST_PUB_CHANNEL, $channel );
delete_post_meta( $post_id, '_lbry_channel'); // remove the _lbry_channel if already set from the post and replaces with _lbry_post_pub_channel to avoid confusion
}
if ( $license !== $cur_license ) {
update_post_meta( $post_id, '_lbry_post_pub_license', $license );
}
update_post_meta( $post_id, LBRY_POST_PUB_LICENSE, $license );
}
if ( ( $will_publish ) && ( $will_publish == 1 ) && $post->post_status == 'publish') {
// Publish the post on the LBRY Network
$this->publisher->publish( $post, $channel, $license );
}
}
/**
* Creates a checkbox that changes the default setting to always publish to LBRY,
* can be reverted individually to not publish on a per post basis. Saves to options table.
*/
public function publish_to_lbry_checkbox( $post )
{
$post_id = $post->ID;
if ( get_post_type( $post_id ) != 'post' ) {
return $post;
}
$default_value = get_option( LBRY_SETTINGS )['lbry_default_publish_setting'];
$new_value = get_post_meta( $post_id, '_lbry_will_publish', true );
if ( ( $new_value ) ? $new_value : $new_value = $default_value );
@ -145,6 +130,38 @@ class LBRY_Network
);
}
/**
* Creates a checkbox that changes the default setting to always publish to LBRY,
* can be reverted individually to not publish on a per post basis. Saves to options table.
*/
public function publish_to_lbry_checkbox( $post )
{
$post_id = $post->ID;
if ( get_post_type( $post_id ) != 'post' ) {
return $post;
}
$default_value = get_option( LBRY_SETTINGS )['lbry_default_publish_setting'];
$new_value = get_post_meta( $post_id, LBRY_WILL_PUBLISH, true );
if ( ( $new_value ) ? $new_value : $new_value = $default_value );
$value = $new_value;
if ( ( $value ) ? $value : 0 );
// nonce set on page meta-box.php
printf (
'<div class="lbry-meta-checkbox-wrapper lbry-meta-checkbox-wrapper-last">
<span class="lbry-pub-metabox"><img src="' . __( '%1$s', '%4$s' ) . '" class="icon icon-lbry meta-icon-lbry"></span><label class="lbry-meta-label">' . esc_html__('%2$s', '%4$s' ) . ' <strong>' . esc_html__('%3$s', '%4$s') . '</strong></label><input type="checkbox" class="lbry-meta-checkbox" value="1"' . esc_attr('%5$s') . ' name="' . esc_attr('%6$s') . '">
</div>',
plugin_dir_url( LBRY_PLUGIN_FILE ) . 'admin/images/lbry.png',
'Publish to',
'LBRY',
'lbrypress',
checked( $value, true, false ),
LBRY_WILL_PUBLISH
);
}
/**
* Returns the HTML for the LBRY Meta Box
* @param WP_POST $post

View file

@ -10,7 +10,6 @@ use League\HTMLToMarkdown\HtmlConverter;
*/
defined('ABSPATH') || die(); // Exit if accessed directly
class LBRY_Network_Parser
{
public $converter = null;

View file

@ -14,7 +14,7 @@ class LBRY_Network_Publisher
* @param string $channel The Claim ID of the channel we are posting to
*/
// NOTE: This is currently sitting at about 150ms, mostly the post parsing
public function publish( $post, $channel, $license ) {
public function publish( $post, $channel = null, $license ) {
$post_id = $post->ID;
// Get converted markdown into a file

View file

@ -0,0 +1,71 @@
<?php
/**
* ============================
* CHANNELS SETTINGS ADMIN PAGE
* Uses the post-admin action so we can use the $_POST global variable to build our cURL request and the settings are not saved to the datbase
* @package LBRYPress
* ============================
*/
defined('ABSPATH') || die(); // Exit if accessed directly
if ( current_user_can( 'manage_options' ) ) {
// Generate a custom nonce
$lbrynonce = wp_create_nonce( 'add_channel_nonce' );
// Build the page
?>
<h3><?php _e( 'Available Channels To Publish', 'lbrypress' ); ?></h3>
<?php LBRY()->admin->available_channels_callback(); ?>
<?php if ( isset( $_POST['lbry_new_channel'] ) ) {
$channel = $_POST['lbry_new_channel'];
$channel = str_replace( '@', '', $channel );
$channel = str_replace( ' ', '-', $channel );
$clean_input['lbry_new_channel'] = sanitize_user( $channel );
}
if ( isset( $_POST['lbry_channel_bid_amount'] ) ) {
$channel_bid = $_POST['lbry_channel_bid_amount'];
$clean_input['lbry_channel_bid_amount'] = number_format( floatval( $channel_bid ), 3, '.', '' );
}
?>
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" id="lbry_add_channel_form">
<input type="hidden" name="action" value="lbry_add_channel">
<input type="hidden" name="_lbrynonce" value="<?php echo $lbrynonce ?>">
<h3><?php echo _e( 'Create a New Channel', 'lbrypress' ); ?></h3>
<table class="form-table" role="presentation">
<tbody>
<tr>
<th scope="row">New Channel Name</th>
<td>
<?php printf(
'<input type="text" id="' . esc_attr('%1$s') . '" name="' . esc_attr('%1$s') . '" value="@' . esc_attr('%2$s') . '" placeholder="your-new-channel" required>',
'lbry_new_channel',
$clean_input['lbry_new_channel'],
); ?>
<p>No Spaces in Channel Names</p>
</td>
</tr>
<tr>
<th scope="row">Amount of LBC to Bid</th>
<td>
<?php printf(
'<input type="number" step="0.001" min="0.001" id="' . esc_attr('%1$s') . '" name="' . esc_attr('%1$s') . '" value="' . esc_attr('%2$.3f') . '" required>',
'lbry_channel_bid_amount',
$clean_input['lbry_channel_bid_amount'],
); ?>
<p>Current minimum bid <img src="<?php echo esc_url( plugin_dir_url( LBRY_PLUGIN_FILE ) . 'admin/images/lbc.png' ) ?>" class="icon icon-lbc bid-icon-lbc"> 0.001</p>
</td>
</tr>
</tbody>
</table>
<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="Create New Channel"></p>
</form>
<?php
} else {
?>
<p> <?php __( "You are not authorized to perform this operation.", $this->plugin_name ) ?> </p>
<?php
}

View file

@ -1,4 +1,11 @@
<?php
/**
* ============================
* META BOX FOR POST PAGE
* Prints the post meta box
* @package LBRYPress
* ============================
*/
defined('ABSPATH') || die(); // Exit if accessed directly
$unnatributed = (object) array(
@ -11,7 +18,7 @@ $lbrynonce = wp_create_nonce( 'lbry_publish_post_nonce' );
$channels = LBRY()->daemon->channel_list();
$channels[] = $unnatributed;
$post_id = $post->ID;
$cur_channel = ( get_post_meta( $post_id, '_lbry_post_pub_channel', true ) ? get_post_meta( $post_id, '_lbry_post_pub_channel', true ) : get_post_meta( $post_id, '_lbry_channel', true ) );
$cur_channel = ( get_post_meta( $post_id, LBRY_POST_PUB_CHANNEL, true ) ? get_post_meta( $post_id, LBRY_POST_PUB_CHANNEL, true ) : get_post_meta( $post_id, '_lbry_channel', true ) );
$default_channel = get_option( LBRY_SETTINGS )['default_lbry_channel'];
// Sort the channels in a natural way
@ -20,43 +27,43 @@ usort( $channels, array( 'LBRYPress', 'channel_name_comp' ) );
<section>
<input type="hidden" id="_lbrynonce" name="_lbrynonce" value="<?php echo $lbrynonce ?>">
<div><label for="_lbry_post_pub_channel" class="lbry-meta-bx-label lbry-meta-bx-channel"><?php esc_html_e( 'Channel to Publish: ', 'lbrypress' ); ?></label></div><?php
<div><label for="LBRY_POST_PUB_CHANNEL" class="lbry-meta-bx-label lbry-meta-bx-channel"><?php esc_html_e( 'Channel to Publish: ', 'lbrypress' ); ?></label></div><?php
$options = '';
if ( $channels ) {
foreach ( $channels as $index=>$channel ) {
$options .= '<option class="lbry-meta-bx-option lbry-meta-option-channel" value="' . esc_attr( $channel->claim_id ) . '"';
if ( ( $cur_channel ) ? $cur_channel : $cur_channel = $default_channel ) {
if ( isset( $cur_channel ) ? $cur_channel : $cur_channel = $default_channel ) {
$options .= selected( $cur_channel, $channel->claim_id, false );
}
$options .= '>' . esc_html__( $channel->name, 'lbrypress' ) . '</option>';
}
$options .= '>' . esc_html__( $channel->name, 'lbrypress' ) . '</option>';
}
printf(
'<select id="' . esc_attr('%1$s') . '" name="' . esc_attr('%1$s') . '">' . esc_html('%2$s') . '</select>',
'_lbry_post_pub_channel',
LBRY_POST_PUB_CHANNEL,
$options
);
} ?>
<div><label for="_lbry_post_pub_license" class="lbry-meta-bx-label lbry-meta-bx-license"><?php esc_html_e( 'Publish License: ', 'lbrypress' ); ?></label></div><?php
<div><label for="LBRY_POST_PUB_LICENSE" class="lbry-meta-bx-label lbry-meta-bx-license"><?php esc_html_e( 'Publish License: ', 'lbrypress' ); ?></label></div><?php
$licenses = LBRY()->licenses;
$options = '';
$default_license = get_option(LBRY_SETTINGS)[LBRY_LICENSE];
$cur_license = get_post_meta( $post_id, '_lbry_post_pub_license', true );
$cur_license = get_post_meta( $post_id, LBRY_POST_PUB_LICENSE, true );
// Create options list, select current license
if ( $licenses ) {
foreach ( $licenses as $value => $name ) {
$options .= '<option class="lbry-meta-bx-option lbry-meta-bx-option-last lbry-meta-option-license" value="' . esc_attr( $value ) . '"';
if ( ( $cur_license ) ? $cur_license : $cur_license = $default_license ) {
if ( isset( $cur_license ) ? $cur_license : $cur_license = $default_license ) {
$options .= selected( $cur_license, $value, false );
}
$options .= '>'. esc_html__( $name, 'lbrypress' ) . '</option>';
}
}
}
printf(
'<select class="" id="'.esc_attr('%1$s').'" name="'. esc_attr('%1$s') .'">' . esc_html('%2$s') . '</select>',
'_lbry_post_pub_license',
LBRY_POST_PUB_LICENSE,
$options
);
?>
?>
</section>

View file

@ -1,44 +0,0 @@
<?php
defined('ABSPATH') || die(); // Exit if accessed directly
$unnatributed = (object) array(
'name' => 'none (anonymous)',
'claim_id' => 'null'
);
$channels = LBRY()->daemon->channel_list();
$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);
?>
<?php wp_nonce_field('lbry_publish_channels', '_lbrynonce'); ?>
<div class="lbry-meta-checkbox-wrapper">
<label class="lbry-meta-label">
<input type="checkbox" class="lbry-meta-checkbox" name="<?= LBRY_WILL_PUBLISH ?>" value="true"
<?php
if ($will_publish === 'true' || $will_publish === '') {
echo 'checked';
}
?>
>
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 ?>
</option>
<?php endforeach; ?>
</select>

View file

@ -14,15 +14,14 @@ $lbry_active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'general';
<div class="wrap">
<h1><?php esc_html_e( get_admin_page_title(), 'lbrypress' ); ?></h1>
<h2 title="<?php echo esc_attr( number_format( $total_balance, 3, '.', ',' ) ); ?> Wallet Total Balance"><img src="<?php echo esc_url( plugin_dir_url( LBRY_PLUGIN_FILE ) . 'admin/images/lbc.png' ) ?>" class="icon icon-lbc wallet-icon-lbc"><code><?php esc_html_e( number_format( $available_balance, 3, '.', ',' ) ); ?></code> Wallet Available Balance</h2>
<h2><img src="<?php echo esc_url( plugin_dir_url( LBRY_PLUGIN_FILE ) . 'admin/images/lbc.png' ) ?>" class="icon icon-lbc wallet-icon-lbc" title="<?php echo esc_attr( number_format( $total_balance, 3, '.', ',' ) ); ?> Wallet Total Balance"><code title="<?php echo esc_attr( number_format( $total_balance, 3, '.', ',' ) ); ?> Wallet Total Balance"><?php esc_html_e( number_format( $available_balance, 3, '.', ',' ) ); ?></code> Wallet Available Balance</h2>
<nav class="nav-tab-wrapper">
<a href="<?php echo esc_url( admin_url( 'options.php?page=lbrypress&tab=general' ) ); ?>" class="nav-tab <?php echo $lbry_active_tab == 'general' || '' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Settings' ); ?></a>
<a href="<?php echo esc_url( admin_url( 'admin.php?page=lbrypress&tab=channels' ) ); ?>" class="nav-tab <?php echo $lbry_active_tab == 'channels' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Channels' ); ?></a>
<a href="<?php echo esc_url( admin_url( 'options.php?page=lbrypress&tab=speech' ) ); ?>" class="nav-tab <?php echo $lbry_active_tab == 'speech' ? 'nav-tab-active' : ''; ?>"><?php esc_html_e( 'Spee.ch' ); ?></a>
</nav>
<?php if ( $lbry_active_tab == 'channels' ) {
include_once( 'partials/channel-page.php' );
include_once( 'channels-page.php' );
} else {
?>
<form class="form-table" action="<?php echo esc_url( admin_url( 'options.php' ) ); ?>" method="post">
@ -33,7 +32,7 @@ $lbry_active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'general';
do_settings_sections( LBRY_ADMIN_PAGE );
submit_button();
} elseif ( $lbry_active_tab == 'channels' ) {
include_once( 'partials/channel-page.php' );
include_once( 'channels-page.php' );
} elseif ( $lbry_active_tab == 'speech' ) {
settings_fields( LBRY_SPEECH_SETTINGS );
do_settings_sections( 'lbrypress-speech' );

View file

@ -4,7 +4,7 @@ defined('ABSPATH') || die(); // Exit if accessed directly
$url = get_post_meta(get_the_id(), LBRY_CANONICAL_URL, true);
if (!$url) {
// Get channel canonical for backwards compatibility
$channel_id = get_post_meta(get_the_id(), LBRY_POST_CHANNEL, true);
$channel_id = ( get_post_meta( get_the_id(), LBRY_POST_PUB_CHANNEL, true ) ? get_post_meta( get_the_id(), LBRY_POST_PUB_CHANNEL, true ) : get_post_meta( get_the_id(), '_lbry_channel', true ) );
$url = LBRY()->daemon->canonical_url($channel_id);
}