diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 575be85..af17e67 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -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')); } /** @@ -528,26 +528,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 ); diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index 92e6966..b4c0fec 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -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 diff --git a/templates/meta-box.php b/templates/meta-box.php index 23e39a8..e577956 100644 --- a/templates/meta-box.php +++ b/templates/meta-box.php @@ -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( '
' . __( 'Initial bid amount:', 'lbrypress' ) . ' - ' . esc_html__( '%1$s', 'lbrypress' ) . '
-
' . __( 'Supports:', 'lbrypress' ) . ' - ' . esc_html__( '%2$s', 'lbrypress' ) . '' . __( 'Add', 'lbrypress' ) . '
-
' . __( 'LBRY channel published to:', 'lbrypress' ) . '
-
' . esc_html__( '%3$s', 'lbrypress' ) . '
-
' . __( 'License published under:', 'lbrypress' ) .'
-
' . esc_html__( '%5$s', 'lbrypress' ) . '
', - $init_bid, + ' . esc_html__( '%1$s', 'lbrypress' ) . '', + $init_bid + ); + printf( + '
' . __( 'Supports:', 'lbrypress' ) . ' + ' . esc_html__( '%1$s', 'lbrypress' ) . '' . __( 'Add', 'lbrypress' ) . '
', $support_amount, + $lbry_claim_id, + urlencode($lbry_url), $lbry_published_channel, + $support_amount, + $init_bid + ); + printf( + '
' . __( 'LBRY channel published to:', 'lbrypress' ) . '
+
' . esc_html__( '%2$s', 'lbrypress' ) . '
', $chan_open_url, + $lbry_published_channel, + ); + printf( + '
' . __( 'License published under:', 'lbrypress' ) .'
+
' . esc_html__( '%1$s', 'lbrypress' ) . '
', $lbry_published_license, ); } else { diff --git a/templates/supports-add-form.php b/templates/supports-add-form.php index 5a58a0d..cbe0dc8 100644 --- a/templates/supports-add-form.php +++ b/templates/supports-add-form.php @@ -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 ?> -

-
+ - - + + + + + +

+ ' . esc_html__( '%1$s', 'lbrypress' ) . ' +

Claim ID: ' . esc_html__( '%2$s', 'lbrypress' ) . '

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.

', + $lbry_url, + $claim_id, + ); ?> + + + + + + + + + + + + + + + + + + + +

- +

+