From 5bcdec98dbdaa75db60127d7b99e149791487f23 Mon Sep 17 00:00:00 2001 From: Lemuel Smyth <36257395+lemsmyth@users.noreply.github.com> Date: Sun, 27 Feb 2022 13:30:08 -0600 Subject: [PATCH] channel edit page and media uploader enqueue (#78) * outline channel edit page, add hidden tab * enque image uploader hook to admin post action * edit channel method and daemon channel edit method * sanitize supports add form * outline channel edit page, add hidden tab * enque image uploader hook to admin post action * edit channel method and daemon channel edit method --- admin/js/admin-image-uploader.js | 56 +++++++++ classes/LBRY_Admin.php | 83 ++++++++++++- classes/LBRY_Daemon.php | 25 ++++ templates/channel-edit-page.php | 201 +++++++++++++++++++++++++++++++ templates/channels-page.php | 5 +- templates/options-page.php | 13 +- templates/supports-add-form.php | 8 +- 7 files changed, 381 insertions(+), 10 deletions(-) create mode 100644 admin/js/admin-image-uploader.js create mode 100644 templates/channel-edit-page.php diff --git a/admin/js/admin-image-uploader.js b/admin/js/admin-image-uploader.js new file mode 100644 index 0000000..532afad --- /dev/null +++ b/admin/js/admin-image-uploader.js @@ -0,0 +1,56 @@ +jQuery( document ).ready( function( $ ) { + +// Uploading files +var file_frame; +var wp_media_post_id = wp.media.model.settings.post.id; // Store the old id +var set_to_post_id = 10; // Set this + + jQuery('#lbry_upload_thumbnail_button').on('click', function( event ){ + + event.preventDefault(); + + // If the media frame already exists, reopen it. + if ( file_frame ) { + // Set the post ID to what we want + file_frame.uploader.uploader.param( 'post_id', set_to_post_id ); + // Open frame + file_frame.open(); + return; + } else { + // Set the wp.media post id so the uploader grabs the ID we want when initialised + wp.media.model.settings.post.id = set_to_post_id; + } + + // Create the media frame. + file_frame = wp.media.frames.file_frame = wp.media({ + title: jQuery( this ).data( 'uploader_title' ), + button: { + text: jQuery( this ).data( 'uploader_button_text' ), + }, + multiple: true // Set to true to allow multiple files to be selected + }); + + // When an image is selected, run a callback. + file_frame.on( 'select', function() { + // We set multiple to false so only get one image from the uploader + attachment = file_frame.state().get('selection').first().toJSON(); + + // Do something with attachment.id and/or attachment.url here + $( '#thumbnail-preview' ).attr( 'src', attachment.url ).css( 'width', 'auto' ); + $( '#lbry_thumbnail_attachment_id' ).val( attachment.id ); + $( '#lbry_upload_thumbnail_button' ).css( 'display', 'none' ); + $( '.channel-image-info' ).css( 'display', 'none' ); + + // Restore the main post ID + wp.media.model.settings.post.id = wp_media_post_id; + }); + + // Finally, open the modal + file_frame.open(); + }); + + // Restore the main ID when the add media button is pressed + jQuery('a.add_media').on('click', function() { + wp.media.model.settings.post.id = wp_media_post_id; + }); +}); \ No newline at end of file diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index a62fbba..03e0854 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -20,6 +20,7 @@ class LBRY_Admin 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_add_supports', array($this, 'add_supports')); + add_action('admin_post_lbry_edit_channel', array($this, 'edit_channel')); } /** @@ -53,7 +54,7 @@ class LBRY_Admin add_action( 'admin_enqueue_scripts', 'load_admin_stylesheet' ); // Admin channel sort JS enqueue - function load_admin_script() { + function load_channel_sort_script() { if ( ( $_GET['page'] == 'lbrypress') && ( $_GET['tab'] == 'channels' ) ) { wp_enqueue_script( 'lbry-table-sort', @@ -64,7 +65,22 @@ class LBRY_Admin ); } } - add_action( 'admin_enqueue_scripts', 'load_admin_script' ); + add_action( 'admin_enqueue_scripts', 'load_channel_sort_script' ); + + // Admin Media Upload on Edit Channel tab + function load_channel_edit_media_scripts() { + if ( ( $_GET['page'] == 'lbrypress' ) && ( $_GET['tab'] == 'channel-edit' ) ) { + wp_enqueue_media(); + wp_enqueue_script( + 'lbry-media-upload', + plugins_url( '/admin/js/admin-image-uploader.js', LBRY_PLUGIN_FILE ), + array( 'jquery' ), + LBRY_VERSION, + true + ); + } + } + add_action( 'admin_enqueue_scripts', 'load_channel_edit_media_scripts' ); // Admin Error Notices function lbry_plugin_not_configured_notice() { @@ -476,7 +492,7 @@ class LBRY_Admin */ public function add_supports() { - if ( ( $_POST['post_id'] ) && ( $_POST['post_id'] !== null ) ) { + if ( ( $_POST['post_id'] ) && ( absint( $_POST['post_id'] ) ) ) { $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' ) ); @@ -509,6 +525,67 @@ class LBRY_Admin exit(); } + /** + * Handles editing an existing channel form submission + */ + public function edit_channel() + { + $redirect_url = admin_url( add_query_arg( array( 'page' => 'lbrypress', 'tab' => 'channels' ), 'options.php' ) ); + + $claim = $_POST['claim_id']; + $claim_id = sanitize_text_field( $claim ); + $bid = $_POST['lbry_supports_add_bid_amount']; + $channel_bid = number_format( floatval( $bid ), 3, '.', '' ); + $title = $_POST['lbry_edit_channel_title']; + $channel_title = sanitize_text_field( $title ); + $description = $_POST['lbry_edit_channel_description']; + $channel_description = sanitize_text_field( $description ); + $tags = $_POST['lbry_edit_channel_tags']; + $channel_tags = sanitize_text_field( $tags ); + $website = $_POST['lbry_new_channel_website']; + $channel_website = sanitize_text_field( $website ); + $email = $_POST['lbry_new_channel_email']; + $channel_email = sanitize_text_field( $email ); + // $language_array = LBRY()->languages; + // $primlang = $_POST['lbry_new_channel_prim_lang']; + // $primary_language = ( ($primlang) && in_array( $primlang, $language_array ) ); + // $seclang = $_POST['lbry_new_channel_sec_lang']; + // $secondary_language = ( ($seclang) && in_array( $seclang, $language_array ) ); + // $thumbnail = $_POST['']; + // $thumbnail_url = wp_get_attachment_url( get_option( 'lbry_media_selector_thumbnail_id' ) ) + // $header = $_POST['']; + // $header_url = wp_get_attachment_url( get_option( 'lbry_media_selector_header_id' ) ); + + // Check that nonce + if ( isset( $_POST['_lbrynonce'] ) && wp_verify_nonce( $_POST['_lbrynonce'], 'edit_channel_nonce' ) ) { + $args = array( + 'claim_id' => $claim_id, + 'bid' => $channel_bid, + 'title' => $channel_title, + 'description' => $channel_description, + 'tags' => $channel_tags, + 'website_url' => $channel_website, + 'email' => $channel_email, + //'languages' => array( $primary_language, $secondary_language ), + //'thumbnail_url' => $thumbnail_url, + //'cover_url' => $header_url, + ); + // Try to add support to the claim + try { + $result = LBRY()->daemon->channel_edit( $args ); + + } 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 ); + exit(); + } + /** * Checks at most once an hour to see if the wallet balance is too low */ diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index b4c0fec..ffd1ae9 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -171,6 +171,31 @@ class LBRY_Daemon return; } } + + /** + * Edit an existing channel to add missing details + * https://lbry.tech/api/sdk#channel_update + * @return array dictionary containing result of the request + */ + + public function channel_edit( $args ) + { + try { + $result = $this->request( + 'channel_update', + $args + ); + + $this->logger->log( 'channel_update success!', 'Successfully updated channel with result: ' . print_r( $result->result, true ) ); + return $result->result; + + } catch (LBRYDaemonException $e) { + $this->logger->log( 'channel_update error', $e->getMessage() . ' | Code: ' . $e->getCode() ); + throw new \Exception( 'Issue updating channel.', 1 ); + return; + } + } + /** * Add supports to an existing claim * https://lbry.tech/api/sdk# diff --git a/templates/channel-edit-page.php b/templates/channel-edit-page.php new file mode 100644 index 0000000..cf2ec50 --- /dev/null +++ b/templates/channel-edit-page.php @@ -0,0 +1,201 @@ + + + +
+ + + + + + '> + +

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

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

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

+
+ items[0]->amount; ?> - name, 'lbrypress' ); ?> + name, 'lbrypress' ); ?> @@ -91,7 +91,8 @@ if ( current_user_can( 'manage_options' ) ) { -

+

+

Create a Channel that can be edited later to add details or set-up a complete Channel now.

diff --git a/templates/options-page.php b/templates/options-page.php index 2224e06..0a800aa 100644 --- a/templates/options-page.php +++ b/templates/options-page.php @@ -26,16 +26,25 @@ $lbry_active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'general'; '' . esc_html__( 'Supports', 'lbrypress') . '', $admin_url, ); + } + if ( $lbry_active_tab == 'channel-edit' ) { + $admin_url = admin_url( 'admin.php?page=lbrypress&tab=channel-edit' ); + printf( + '' . esc_html__( 'Channel', 'lbrypress' ) . '', + $admin_url, + ); } ?> - " method="post" id="lbry_add_supports_form"> - - - - + + + +