Add lbry protocol #74

Merged
lemsmyth merged 8 commits from add-lbry-protocol into master 2022-02-23 16:22:44 +01:00
6 changed files with 223 additions and 87 deletions

View file

@ -164,6 +164,7 @@ class LBRYPress
{
register_activation_hook( LBRY_PLUGIN_FILE, array( $this, 'activate' ) );
register_deactivation_hook( LBRY_PLUGIN_FILE, array( $this, 'deactivate' ) );
add_filter( 'kses_allowed_protocols' , array( $this, 'lbry_add_additional_protocols' ) );
// Banner output for published posts
// NOTE: move this to its own class to reduce clutter?
@ -227,6 +228,19 @@ class LBRYPress
error_log( 'Deactivated LBRYPress' );
}
/**
* Allowing additional URL protocols to list of allowed protocols.
*
* @param array $protocols List of protocols allowed by (default WordPress.)
*
* @return array $protocols Updated list including additional protocols.
*/
public function lbry_add_additional_protocols( $protocols )
{
$protocols[] = 'lbry';
return $protocols;
}
public function published_on_lbry_banner($content)
{
if ( ! is_single() || ! in_the_loop() || ! is_main_query() ) {

View file

@ -19,7 +19,7 @@ class LBRY_Admin
add_action('admin_init', array($this, 'page_init'));
add_action('admin_init', array($this, 'wallet_balance_warning'));
add_action('admin_post_lbry_add_channel', array($this, 'add_channel'));
add_action('admin_post_lbry_supports_add', array($this, 'supports_add'));
add_action('admin_post_lbry_add_supports', array($this, 'add_supports'));
}
/**
@ -267,61 +267,7 @@ class LBRY_Admin
*/
public function available_channels_callback()
{
$channel_list = LBRY()->daemon->channel_list();
if ( $channel_list ) { ?>
<table class="lbry-channel-table">
<thead>
<tr>
<th data-sort="channel">Channel</th>
<th data-sort="lbryurl">LBRY URL</th>
<th data-sort="claim">Claim ID</th>
<th data-sort="date">~ Date Created</th>
<th data-sort="posts">Posts</th>
<th data-sort="support" colspan="2">Supports</th>
</tr>
</thead>
<tbody>
<?php foreach ( $channel_list as $channel ):
$claim_id = $channel->claim_id;
$results = LBRY()->daemon->claim_search( $claim_id );
$lbry_url = $results->items[0]->canonical_url;
if ( $lbry_url ) {
$open_url = str_replace( 'lbry://', 'open.lbry.com/', $lbry_url );
}
$timestamp = $results->items[0]->meta->creation_timestamp;
$created_date = date( 'm-d-y', $timestamp );
$support_amount = $results->items[0]->meta->support_amount;
$claims_published = $results->items[0]->meta->claims_in_channel;
if ( ( $support_amount < 0.001 ) ) {
( $support_amount = '0' );
} elseif ( ( $support_amount < 0.01 ) && ( $support_amount >= 0.001 ) ) {
( $support_amount = '<0.01' );
} elseif ( ( $support_amount <= 0.099 ) && ( $support_amount >= 0.01) ) {
( $support_amount = number_format( floatval( $support_amount ), 2, '.', '' ) );
} elseif ( ( $support_amount <= 0.999 ) && ( $support_amount >= 0.1 ) ) {
( $support_amount = number_format( floatval( $support_amount ), 1, '.', '' ) );
} else {
( $support_amount = number_format( intval( $support_amount ) ) );
}
$init_bid = $results->items[0]->amount; ?>
<tr>
<td><a href="<?php echo esc_url( $open_url, 'lbrypress' ); ?>"><?php esc_html_e( $channel->name, 'lbrypress' ); ?></a></td>
<td><?php esc_html_e( $lbry_url, 'lbrypress' ); ?></td>
<td><?php esc_html_e( $claim_id, 'lbrypress' ); ?></td>
<td><?php esc_html_e( $created_date, 'lbrypress' ); ?></td>
<td><?php esc_html_e( $claims_published, 'lbrypress' ); ?></td>
<td><span title="Initial Bid Amount: <?php esc_html_e( $init_bid, 'lbrypress' ); ?>"><img src="<?php echo esc_url( plugin_dir_url( LBRY_PLUGIN_FILE ) . 'admin/images/lbc.png' ) ?>" class="icon icon-lbc bid-icon-lbc channel-bid-icon-lbc"><?php esc_html_e( $support_amount, 'lbrypress' ); ?></span></td>
<td><a href="<?php echo admin_url( add_query_arg( array( 'page' => 'lbrypress', 'tab' => 'supports', 'claim_id' => $claim_id, 'current_support' => $support_amount, 'init_bid' => $init_bid, 'lbry_url' => urlencode($lbry_url), 'return_page' => 'channels' ), 'admin.php' ) ); ?>">Add</a></td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr><th colspan="7">LBRYPress</th></tr>
</tfoot>
</table>
<?php } else { ?>
<p>Looks like you haven't added any channels yet, feel free to do so below:</p>
<?php }
// Moved to channels-page.php
}
/**
@ -528,26 +474,27 @@ class LBRY_Admin
/**
* Handles adding supports form submission
*/
public function supports_add()
public function add_supports()
{
$redirect_url = admin_url( add_query_arg( array( 'page' => 'lbrypress', 'tab' => 'channels' ), 'options.php' ) );
if ( ( $_POST['post_id'] ) && ( $_POST['post_id'] !== null ) ) {
$redirect_url = admin_url( add_query_arg( array( 'post' => $_POST['post_id'], 'action' => 'edit' ), 'post.php') );
} else {
$redirect_url = admin_url( add_query_arg( array( 'page' => 'lbrypress', 'tab' => 'channels' ), 'options.php' ) );
}
if ( ( $_POST['lbry_url'] ) ? $lbry_url = urldecode($_POST['lbry_url']) : $lbry_url = $_POST['lbry_supports_add_claim_id']);
if ( ( $_POST['supporting_channel'] ) ? $supporting_channel = $_POST['supporting_channel'] : $supporting_channel = null );
// Check that nonce
if ( isset( $_POST['_lbrynonce'] ) && wp_verify_nonce( $_POST['_lbrynonce'], 'add_supports_nonce' ) ) {
if ( empty( $_POST['lbry_supports_add_claim_id'] ) || empty( $_POST['lbry_supports_add_amount'] ) ) {
LBRY()->notice->set_notice( 'error', 'Must supply both channel name and bid amount' );
} elseif ( isset( $_POST['lbry_supports_add_claim_id'] ) && isset( $_POST['lbry_supports_add_amount'] ) ) {
if ( isset( $_POST['lbry_supports_add_claim_id'] ) && isset( $_POST['lbry_supports_add_bid_amount'] ) ) {
$claim_id = $_POST['lbry_supports_add_claim_id']; // TODO: sanitize key() only allows for lowercase chars, dashes, and underscores. maybe remove to allow more characters? and use something else for better control?
$bid = $_POST['lbry_supports_add_amount'];
$support_bid = number_format( floatval( $bid ), 3, '.', '' );
$claim_id = sanitize_text_field( $claim_id );
$bid = $_POST['lbry_supports_add_bid_amount'];
$supports_bid = number_format( floatval( $bid ), 3, '.', '' );
// Try to add the new channel
// Try to add support to the claim
try {
// $result = LBRY()->daemon->channel_new( $claim_id, $supports_bid );
// Tell the user it takes some time to go through
LBRY()->notice->set_notice(
'success', 'Successfully added supports for: @' . esc_html( $claim_name ) . '! Please allow a few minutes for the bid to process.', true );
$result = LBRY()->daemon->supports_add( $claim_id, $supports_bid, $supporting_channel, $lbry_url );
} catch ( \Exception $e ) {
LBRY()->notice->set_notice( 'error', $e->getMessage(), false );

View file

@ -171,6 +171,37 @@ class LBRY_Daemon
return;
}
}
/**
* Add supports to an existing claim
* https://lbry.tech/api/sdk#
* @return array dictionary containing result of the request
*/
public function supports_add( $claim_id, $supports_bid, $supporting_channel = null, $lbry_url = null )
{
try {
$result = $this->request(
'support_create',
array(
'claim_id' => $claim_id,
'channel_name' => $supporting_channel,
'amount' => $supports_bid
)
);
if ( $result ) {
if ( ( ($lbry_url) && ($lbry_url !== null ) ) ? $lbry_url : $lbry_url = $claim_id );
LBRY()->notice->set_notice(
'success', 'Successfully added supports for claim id: ' . esc_html__( $lbry_url, 'lbrypress' ) . '! Please allow a few minutes for the support to process.', true );
}
$this->logger->log( 'support_create success!', 'Successfully added support with result: ' . print_r( $result->result, true ) );
return $result->result;
} catch (LBRYDaemonException $e) {
$this->logger->log( 'support_create error', $e->getMessage() . ' | Code: ' . $e->getCode() );
throw new \Exception( 'Issue creating new support.', 1 );
return;
}
}
/**
* Returns the canonical URL for the supplied claim ID, null otherwise

View file

@ -17,8 +17,9 @@ if ( current_user_can( 'manage_options' ) ) {
?>
<h3><?php _e( 'Available Channels To Publish', 'lbrypress' ); ?></h3>
<?php LBRY()->admin->available_channels_callback(); ?>
<?php if ( isset( $_POST['lbry_new_channel'] ) ) {
<?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 );
@ -28,6 +29,62 @@ if ( current_user_can( 'manage_options' ) ) {
$channel_bid = $_POST['lbry_channel_bid_amount'];
$clean_input['lbry_channel_bid_amount'] = number_format( floatval( $channel_bid ), 3, '.', '' );
}
$channel_list = LBRY()->daemon->channel_list();
if ( $channel_list ) { ?>
<table class="lbry-channel-table">
<thead>
<tr>
<th data-sort="channel">Channel</th>
<th data-sort="lbryurl">LBRY URL</th>
<th data-sort="claim">Claim ID</th>
<th data-sort="date">~ Date Created</th>
<th data-sort="posts">Posts</th>
<th data-sort="support" colspan="2">Supports</th>
</tr>
</thead>
<tbody>
<?php foreach ( $channel_list as $channel ):
$claim_id = $channel->claim_id;
$results = LBRY()->daemon->claim_search( $claim_id );
$lbry_url = $results->items[0]->canonical_url;
if ( $lbry_url ) {
$open_url = str_replace( 'lbry://', 'https://open.lbry.com/', $lbry_url );
}
$timestamp = $results->items[0]->meta->creation_timestamp;
$created_date = date( 'm-d-y', $timestamp );
$support_amount = $results->items[0]->meta->support_amount;
$claims_published = $results->items[0]->meta->claims_in_channel;
if ( ( $support_amount < 0.001 ) ) {
( $support_amount = '0' );
} elseif ( ( $support_amount < 0.01 ) && ( $support_amount >= 0.001 ) ) {
( $support_amount = '<0.01' );
} elseif ( ( $support_amount <= 0.099 ) && ( $support_amount >= 0.01) ) {
( $support_amount = number_format( floatval( $support_amount ), 2, '.', '' ) );
} elseif ( ( $support_amount <= 0.999 ) && ( $support_amount >= 0.1 ) ) {
( $support_amount = number_format( floatval( $support_amount ), 1, '.', '' ) );
} else {
( $support_amount = number_format( intval( $support_amount ) ) );
}
$init_bid = $results->items[0]->amount; ?>
<tr>
<td><a href="<?php echo esc_url( $open_url, 'lbrypress' ); ?>"><?php esc_html_e( $channel->name, 'lbrypress' ); ?></a></td>
<td><?php esc_html_e( esc_url( $lbry_url ), 'lbrypress' ); ?></td>
<td><?php esc_html_e( $claim_id, 'lbrypress' ); ?></td>
<td><?php esc_html_e( $created_date, 'lbrypress' ); ?></td>
<td><?php esc_html_e( $claims_published, 'lbrypress' ); ?></td>
<td><span title="Initial Bid Amount: <?php esc_html_e( $init_bid, 'lbrypress' ); ?>"><img src="<?php echo esc_url( plugin_dir_url( LBRY_PLUGIN_FILE ) . 'admin/images/lbc.png' ) ?>" class="icon icon-lbc bid-icon-lbc channel-bid-icon-lbc"><?php esc_html_e( $support_amount, 'lbrypress' ); ?></span></td>
<td><a href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'lbrypress', 'tab' => 'supports', 'claim_id' => urlencode( esc_html__( $claim_id, 'lbrypress' ) ), 'supporting_channel' => urlencode( esc_html__($channel->name, 'lbrypress' ) ), 'current_support' => urlencode( floatval($support_amount) ), 'init_bid' => urlencode( floatval($init_bid) ), 'lbry_url' => urlencode( esc_url($lbry_url) ) ), 'admin.php' ) ) ); ?>">Add</a></td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr><th colspan="7">LBRYPress</th></tr>
</tfoot>
</table>
<?php } else { ?>
<p>Looks like you haven't added any channels yet, feel free to do so below:</p>
<?php }
?>
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" id="lbry_add_channel_form">
@ -66,6 +123,6 @@ if ( current_user_can( 'manage_options' ) ) {
<?php
} else {
?>
<p> <?php __( "You are not authorized to perform this operation.", $this->plugin_name ) ?> </p>
<p> <?php __( "You are not authorized to perform this operation.", 'lbrypress' ) ?> </p>
<?php
}

View file

@ -31,7 +31,8 @@ if ( ( $lbry_published == true ) && ( ( $lbry_claim_id ) ) && ( ! ( $lbry_publis
$license = $result->items[0]->value->license;
update_post_meta( $post_id, '_lbry_post_pub_license', $license );
}
$lbry_canonical_url = get_post_meta( $post_id, '_lbry_canonical_url', true );
$lbry_url = ( ($lbry_canonical_url) ? $lbry_canonical_url : 'lbry://' . $lbry_published_channel . '#' . $lbry_claim_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 ) );
$default_channel = get_option( LBRY_SETTINGS )['default_lbry_channel'];
$chan_open_url = ( 'open.lbry.com/'. $lbry_published_channel .'#' . $lbry_channel_claim_id . '');
@ -57,17 +58,28 @@ $chan_open_url = ( 'open.lbry.com/'. $lbry_published_channel .'#' . $lbry_channe
}
printf(
'<div class="lbry-meta-label lbry-meta-bx-channel"><strong>' . __( 'Initial bid amount:', 'lbrypress' ) . ' </strong>
<span class="lbry-meta-bx-content lbry-meta-bx-channel"><img src="' . esc_url( plugin_dir_url( LBRY_PLUGIN_FILE ) ) . 'admin/images/lbc.png" class="icon icon-lbc bid-icon-lbc bid-icon-lbc"> ' . esc_html__( '%1$s', 'lbrypress' ) . '</span></div>
<div class="lbry-meta-label lbry-meta-bx-channel"><strong>' . __( 'Supports:', 'lbrypress' ) . ' </strong>
<span class="lbry-meta-bx-content lbry-meta-bx-channel"><img src="' . esc_url( plugin_dir_url( LBRY_PLUGIN_FILE ) ) . 'admin/images/lbc.png" class="icon icon-lbc bid-icon-lbc bid-icon-lbc"> ' . esc_html__( '%2$s', 'lbrypress' ) . '</span><a href="' . admin_url( add_query_arg( array( 'page' => 'lbrypress', 'tab' => 'supports', 'claim_id' => $lbry_claim_id, 'lbry_url' => $lbry_url, 'current_support' => $support_amount, 'init_bid' => $init_bid ), 'admin.php' ) ) . '">' . __( 'Add', 'lbrypress' ) . '</a></div>
<div class="lbry-meta-label lbry-meta-bx-channel"><strong>' . __( 'LBRY channel published to:', 'lbrypress' ) . '</strong></div>
<div class="lbry-meta-bx-content lbry-meta-bx-channel"><a href="' . esc_url( '%4$s', 'lbrypress' ) . '">' . esc_html__( '%3$s', 'lbrypress' ) . '</a></div>
<div class="lbry-meta-label lbry-meta-bx-license"><strong>' . __( 'License published under:', 'lbrypress' ) .'</strong> </div>
<div class="lbry-meta-bx-content lbry-meta-bx-license lbry-meta-bx-content-last">' . esc_html__( '%5$s', 'lbrypress' ) . '</div>',
$init_bid,
<span class="lbry-meta-bx-content lbry-meta-bx-channel"><img src="' . esc_url( plugin_dir_url( LBRY_PLUGIN_FILE ) ) . 'admin/images/lbc.png" class="icon icon-lbc bid-icon-lbc bid-icon-lbc"> ' . esc_html__( '%1$s', 'lbrypress' ) . '</span></div>',
$init_bid
);
printf(
'<div class="lbry-meta-label lbry-meta-bx-channel"><strong>' . __( 'Supports:', 'lbrypress' ) . ' </strong>
<span class="lbry-meta-bx-content lbry-meta-bx-channel"><img src="' . esc_url( plugin_dir_url( LBRY_PLUGIN_FILE ) ) . 'admin/images/lbc.png" class="icon icon-lbc bid-icon-lbc bid-icon-lbc"> ' . esc_html__( '%1$s', 'lbrypress' ) . '</span><a href="' . admin_url( add_query_arg( array( 'page' => 'lbrypress', 'tab' => 'supports', 'post_id' => $post_id, 'claim_id' => '%2$s', 'lbry_url' => '%3$s', 'supporting_channel' => '%4$s', 'current_support' => '%5$.3f', 'init_bid' => '%6$.3f' ), 'admin.php' ) ) . '">' . __( 'Add', 'lbrypress' ) . '</a></div>',
$support_amount,
$lbry_claim_id,
urlencode($lbry_url),
$lbry_published_channel,
$support_amount,
$init_bid
);
printf(
'<div class="lbry-meta-label lbry-meta-bx-channel"><strong>' . __( 'LBRY channel published to:', 'lbrypress' ) . '</strong></div>
<div class="lbry-meta-bx-content lbry-meta-bx-channel"><a href="' . esc_url( '%1$s', 'lbrypress' ) . '">' . esc_html__( '%2$s', 'lbrypress' ) . '</a></div>',
$chan_open_url,
$lbry_published_channel,
);
printf(
'<div class="lbry-meta-label lbry-meta-bx-license"><strong>' . __( 'License published under:', 'lbrypress' ) .'</strong> </div>
<div class="lbry-meta-bx-content lbry-meta-bx-license lbry-meta-bx-content-last">' . esc_html__( '%1$s', 'lbrypress' ) . '</div>',
$lbry_published_license,
);
} else {

View file

@ -13,14 +13,89 @@ if ( current_user_can( 'manage_options' ) ) {
// Generate a custom nonce
$lbrynonce = wp_create_nonce( 'add_supports_nonce' );
// TODO sanitize more
$claim_id = $_GET['claim_id'];
$claim_id = sanitize_text_field( $claim_id );
$lbry_url = $_GET['lbry_url'];
$lbry_url = urldecode($lbry_url);
$lbry_url = sanitize_text_field($lbry_url);
$init_bid = $_GET['init_bid'];
$init_bid = number_format( floatval( $init_bid ), 3, '.', '' );
$supporting_channel = $_GET['supporting_channel'];
$supporting_channel = sanitize_user( $supporting_channel );
$support_amount = $_GET['current_support'];
$support_amount = number_format( floatval( $support_amount ), 3, '.', '' );
$return_post = $_GET['post_id'];
$return_post = intval( $return_post );
// Build the page ?>
<h3><?php _e( 'Add Supports to a Claim', 'lbrypress' ); ?></h3>
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" id="lbry_supports_add_form">
<form action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" method="post" id="lbry_add_supports_form">
<input type="hidden" name="action" value="lbry_supports_add">
<input type="hidden" name="_lbrynonce" value="<?php echo $lbrynonce ?>">
<input type="hidden" name="action" value="lbry_add_supports">
<input type="hidden" name="_lbrynonce" value="<?php echo $lbrynonce; ?>">
<input type="hidden" name="post_id" value="<?php echo $return_post; ?>">
<input type="hidden" name="lbry_url" value="<?php echo esc_attr($lbry_url); ?>">
<input type="hidden" name="supporting_channel" value="<?php echo $supporting_channel; ?>">
<h2><?php echo _e( 'Add Supports to Claim:', 'lbrypress' ); ?></h2>
<?php printf(
'<h3>' . esc_html__( '%1$s', 'lbrypress' ) . '</h3>
<h4>Claim ID: <code>' . esc_html__( '%2$s', 'lbrypress' ) . '</code></h4><p>If you want to add supports to a different channel or post, use the channel or post link that corresponds with that specific claim to add supports.</p>',
$lbry_url,
$claim_id,
); ?>
<table class="form-table" role="presentation">
<tbody>
<tr>
<th scope="row">Claim ID</th>
<td>
<?php printf(
'<input type="text" id="' . esc_attr('%1$s') . '" name="' . esc_attr('%1$s') . '" value="' . esc_attr('%2$s') . '" required readonly>',
'lbry_supports_add_claim_id',
$claim_id,
); ?>
<p>Claims can be for either Channels or Posts</p>
</td>
</tr>
<tr>
<th scope="row">Add LBC as Support</th>
<td>
<?php printf(
'<input type="number" step="0.001" min="0.01" id="' . esc_attr('%1$s') . '" name="' . esc_attr('%1$s') . '" value="' . esc_attr('%2$.3f') . '" required>',
'lbry_supports_add_bid_amount',
$bid_amount,
); ?>
<p>Current minimum support 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.01</p>
</td>
</tr>
<tr>
<th scope=""row>Amount Used to Create Claim</th>
<td>
<?php printf(
'<p><img src="' . esc_attr( '%2$s', 'lbrypress' ) . '" class="icon icon-lbc bid-icon-lbc"> ' . esc_html__( '%1$.3f', 'lbrypress' ) . '</p><p>Initial bid that was used to publish the claim.</p>',
$init_bid,
plugin_dir_url( LBRY_PLUGIN_FILE ) . 'admin/images/lbc.png',
); ?>
</td>
</tr>
<tr>
<th scope="row">Current Supports</th>
<td>
<?php printf(
'<p><img src="' . esc_attr( '%2$s', 'lbrypress' ) . '" class="icon icon-lbc bid-icon-lbc"> ' . esc_html__('%1$.3f', 'lbrypress' ) . '</p><p>May not include very recently added supports, please be patient as it may take a short while to update.</p>',
$support_amount,
plugin_dir_url( LBRY_PLUGIN_FILE ) . 'admin/images/lbc.png',
); ?>
</td>
</tbody>
</table>
<p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="Add Supports"></p>
</form>
<?php
}
<?php
} else {
?>
<p> <?php __( "You are not authorized to perform this operation.", 'lbrypress' ); ?> </p>
<?php
}