From 85c2e7b3c97db29bc3f40cd2895a9b7992194579 Mon Sep 17 00:00:00 2001 From: Lem Smyth Date: Sun, 27 Feb 2022 11:33:11 -0600 Subject: [PATCH] enque image uploader hook to admin post action --- admin/js/admin-image-uploader.js | 56 ++++++++++++++++++++++++++++++++ classes/LBRY_Admin.php | 20 ++++++++++-- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 admin/js/admin-image-uploader.js 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..6d15ad7 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() {