From 1226273a11c34ba9f453c881ad316c5c89c99bba Mon Sep 17 00:00:00 2001 From: Lem Smyth Date: Wed, 9 Feb 2022 10:17:45 -0600 Subject: [PATCH] build channel-tab --- admin/css/lbry-admin.css | 7 +++- classes/LBRYPress.php | 2 + classes/LBRY_Admin.php | 78 +++++++++++++++++++++++++++++-------- classes/LBRY_Daemon.php | 7 +++- templates/channels-page.php | 71 +++++++++++++++++++++++++++++++++ templates/options-page.php | 4 +- 6 files changed, 148 insertions(+), 21 deletions(-) create mode 100644 templates/channels-page.php diff --git a/admin/css/lbry-admin.css b/admin/css/lbry-admin.css index 7b261b6..c69f6ed 100644 --- a/admin/css/lbry-admin.css +++ b/admin/css/lbry-admin.css @@ -7,4 +7,9 @@ height: 1.8em; margin-right: .5em; margin-bottom: -.5em; - } \ No newline at end of file + } + + .bid-icon-lbc { + height: 1.2em; + margin-bottom: -.2em; + } diff --git a/classes/LBRYPress.php b/classes/LBRYPress.php index 4e80554..95113b7 100644 --- a/classes/LBRYPress.php +++ b/classes/LBRYPress.php @@ -120,6 +120,8 @@ class LBRYPress $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_POST_PUB_CHANNEL', '_lbry_post_pub_channel'); // The meta key for which channel to publish + $this->define('LBRY_POST_POST_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 diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 56e3033..826d8c7 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -95,6 +95,14 @@ class LBRY_Admin LBRY_SETTINGS_SECTION_GENERAL ); + /** + * Channel Page Settings + * We are using a custom page so that we can use the admin-post action and retrieve the $_POST + * global variable to populate the cURL request to create_channel, not saving the inputs to + * our database. + */ + + /** * Speech Admin Page settings */ @@ -204,6 +212,26 @@ class LBRY_Admin { print 'This is where you can configure how LBRYPress will distribute your content:'; } + + /** + * Section info for the Available Channel(s) Section + */ + public function available_channels_callback() + { + $channel_list = LBRY()->daemon->channel_list(); + + if ( $channel_list ) { ?> + + +

Looks like you haven't added any channels yet, feel free to do so below:

+ 'lbrypress', 'tab' => 'channels' ), 'options.php' ) ); + // Check that nonce - if (! isset($_POST['_lbrynonce']) || ! wp_verify_nonce($_POST['_lbrynonce'], 'lbry_add_channel')) { - LBRY()->notice->set_notice('error'); - } elseif (! isset($_POST['new_channel']) || ! isset($_POST['bid_amount'])) { - LBRY()->notice->set_notice('error', 'Must supply both channel name and bid amount'); - } else { - $new_channel = $_POST['new_channel']; - $bid_amount = $_POST['bid_amount']; + if ( isset( $_POST['_lbrynonce'] ) && wp_verify_nonce( $_POST['_lbrynonce'], 'add_channel_nonce' ) ) { + if ( empty( $_POST['lbry_new_channel'] ) || empty( $_POST['lbry_channel_bid_amount'] ) ) { + LBRY()->notice->set_notice( 'error', 'Must supply both channel name and bid amount' ); + } elseif ( isset( $_POST['lbry_new_channel'] ) && isset( $_POST['lbry_channel_bid_amount'] ) ) { + $channel = $_POST['lbry_new_channel']; // TODO: sanitize key() only allows for lowercase chars, dashes, and underscores. maybe remove to allow more characters? and use something else for better control? + $channel = trim( $channel ); + $channel = str_replace( '@', '', $channel ); + $channel = str_replace( ' ', '-', $channel ); + $channel = str_replace( '_', '-', $channel ); + $channel_name = sanitize_user( $channel ); - // Try to add the new channel - try { - $result = LBRY()->daemon->channel_new($new_channel, $bid_amount); - // Tell the user it takes some time to go through - LBRY()->notice->set_notice('success', 'Successfully added a new channel! Please wait a few minutes for the bid to process.', true); - } catch (\Exception $e) { - LBRY()->notice->set_notice('error', $e->getMessage(), false); + $bid = $_POST['lbry_channel_bid_amount']; + $channel_bid = number_format( floatval( $bid ), 3, '.', '' ); + + // Try to add the new channel + try { + $result = LBRY()->daemon->channel_new( $channel_name, $channel_bid ); + // Tell the user it takes some time to go through + LBRY()->notice->set_notice( + 'success', 'Successfully added a new channel: @' . esc_html( $channel_name ) . '! Please allow a few minutes for the bid to process.', true ); + + } catch ( \Exception $e ) { + LBRY()->notice->set_notice( 'error', $e->getMessage(), false ); + } } + } else { + LBRY()->notice->set_notice('error', 'Security check failed' ); + die( __( 'Security check failed', 'lbrypress' ) ); } - wp_safe_redirect($redirect_url); + wp_safe_redirect( $redirect_url ); exit(); } diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index 94c0a26..4cde53d 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -114,7 +114,7 @@ class LBRY_Daemon * https://lbry.tech/api/sdk#channel_create * @return array dictionary containing result of the request */ - public function channel_new($channel_name, $bid_amount) + public function channel_new($channel_name, $channel_bid) { // TODO: Sanitize channel name and bid // Make sure no @ sign, as we will add that @@ -134,10 +134,13 @@ class LBRY_Daemon 'channel_create', array( 'name' => $channel_name, - 'bid' => number_format(floatval($bid_amount), 2, '.', '') + 'bid' => $channel_bid ) ); + + $this->logger->log( 'channel_create success!', 'Successfully created channel with result: ' . print_r( $result->result, true ) ); return $result->result; + } catch (LBRYDaemonException $e) { $this->logger->log('channel_new error', $e->getMessage() . ' | Code: ' . $e->getCode()); throw new \Exception('Issue creating new channel.', 1); diff --git a/templates/channels-page.php b/templates/channels-page.php new file mode 100644 index 0000000..db5b1ac --- /dev/null +++ b/templates/channels-page.php @@ -0,0 +1,71 @@ + + +

+ admin->available_channels_callback(); ?> + + +
+ + + +

+ + + + + + + + + + + + +

+
+ +

plugin_name ) ?>

+daemon->channel_list();
@@ -33,7 +33,7 @@ $channel_list = $LBRY->daemon->channel_list(); 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' );