From 04ad7a874610da4239b13a431366303bb34c5b23 Mon Sep 17 00:00:00 2001 From: Lem Smyth Date: Sat, 19 Feb 2022 16:56:57 -0600 Subject: [PATCH 1/3] enqueue js sort table --- admin/js/table-sort.js | 76 ++++++++++++++++++++++++++++++++++++++++++ classes/LBRY_Admin.php | 22 +++++++++--- 2 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 admin/js/table-sort.js diff --git a/admin/js/table-sort.js b/admin/js/table-sort.js new file mode 100644 index 0000000..7912668 --- /dev/null +++ b/admin/js/table-sort.js @@ -0,0 +1,76 @@ +jQuery(document).ready(function($) { + var compare = { + name: function(a, b) { + a = a.replace(/^@/, '') && a.replace(/-/g, ''); + b = b.replace(/^@/, '') && b.replace(/-/g, ''); + + if (a < b) { + return -1; + } else { + return a > b ? 1 : 0; + } + }, + lbryurl: function(a, b) { + a = a.replace(/^lbry:\/\/@/i, '') && a.replace(/#[a-zA-Z0-9]+/, '') && a.replace(/-/g, ''); + b = b.replace(/^lbry:\/\/@/i, '') && b.replace(/#[a-zA-Z0-9]+/, '') && b.replace(/-/g, ''); + + if (a < b) { + return -1; + } else { + return a > b ? 1 : 0; + } + }, + amount: function(a, b) { + a = a.split('.'); + b = b.split('.'); + + a = Number(a[0]) + Number(a[1]); + b = Number(b[0]) + Number(b[1]); + + return a - b; + }, + number: function(a, b) { + a = Number(a); + b = Number(b); + + return a - b; + }, + date: function(a, b) { + a = new Date(a); + b = new Date(b); + + return a - b; + } + }; + $('.lbry-channel-table').each(function() { + var $table = $(this); + var $tbody = $table.find('tbody'); + var $controls = $table.find('th'); + var rows = $tbody.find('tr').toArray(); + + $controls.on('click', function() { + var $header = $(this); + var order = $header.data('sort'); + var column; + + if ($header.is('.ascending') || $header.is('.descending')) { + $header.toggleClass('ascending descending'); + $tbody.append(rows.reverse()); + } else { + $header.addClass('ascending'); + $header.siblings().removeClass('ascending descending'); + if (compare.hasOwnProperty(order)) { + column = $controls.index(this); + + rows.sort(function(a, b) { + a = $(a).find('td').eq(column).text(); + b = $(b).find('td').eq(column).text(); + return compare[order](a, b); + }); + + $tbody.append(rows); + } + } + }); + }); +}); \ No newline at end of file diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 88aabc0..3371f38 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -51,6 +51,20 @@ class LBRY_Admin } } add_action( 'admin_enqueue_scripts', 'load_admin_stylesheet' ); + + // Admin JS enqueue + function load_admin_script() { + if ( ( $_GET['page'] == 'lbrypress') && ( $_GET['tab'] == 'channels' ) ) { + wp_enqueue_script( + 'lbry-table-sort', + plugins_url( '/admin/js/table-sort.js', LBRY_PLUGIN_FILE ), + array('jquery'), + LBRY_VERSION, + true + ); + } + } + add_action( 'admin_enqueue_scripts', 'load_admin_script' ); // Admin Error Notices function lbry_plugin_not_configured_notice() { @@ -258,10 +272,10 @@ class LBRY_Admin - - - - + + + + -- 2.45.2 From c6f28b18ac24adb0a0cc9d3749faa26fe4c8a56b Mon Sep 17 00:00:00 2001 From: Lem Smyth Date: Sun, 20 Feb 2022 09:53:14 -0600 Subject: [PATCH 2/3] channels table sort and style --- admin/css/lbry-admin.css | 24 +++++++++++------------- admin/js/table-sort.js | 27 +++++++++++++++++---------- classes/LBRY_Admin.php | 18 ++++++++++++------ 3 files changed, 40 insertions(+), 29 deletions(-) diff --git a/admin/css/lbry-admin.css b/admin/css/lbry-admin.css index 7763519..a0d2dce 100644 --- a/admin/css/lbry-admin.css +++ b/admin/css/lbry-admin.css @@ -61,10 +61,10 @@ margin-right: 1em; } .post-lbry-display-before { - color: darkgreen; + color: green; } .post-lbry-display { - color: green; + color: #135548; } .lbry-pub-metabox { @@ -86,38 +86,36 @@ .lbry-meta-bx-content-last { padding: .2em .8em 1em .1em; } - +/*--------------------- + Channels Table + -------------------*/ table.lbry-channel-table { width: 100%; border-collapse: collapse; font-family: Georgia; } - .lbry-channel-table th, .lbry-channel-table td { padding: .4em 1.6em; border: 2px solid #fff; - background: #fbd7b4; + background: #97eb9d; font-size: 1em; } .lbry-channel-table thead th { padding: .5em 2em; - background: #f69546; + background: #135548; text-align: left; font-weight: normal; font-size: 1.2em; color: #fff; } -.lbry-channel-table tbody tr:nth-child(odd) *:nth-child(even), .lbry-channel-table tbody tr:nth-child(even) *:nth-child(odd) { - background: #f3eddd; +.lbry-channel-table tbody tr:nth-child(odd) td { + background: #e1fafa; } .lbry-channel-table tfoot th { - padding: .5em 2em; - background: #f69546; + padding: .5em 1.8em; + background: #175248; text-align: left; font-weight: normal; font-size: .9em; color: #fff; } -.lbry-channel-table tr *:nth-child(3), .lbry-channel-table tr *:nth-child(4) { - /* text-align: right; */ -} \ No newline at end of file diff --git a/admin/js/table-sort.js b/admin/js/table-sort.js index 7912668..d418778 100644 --- a/admin/js/table-sort.js +++ b/admin/js/table-sort.js @@ -1,8 +1,8 @@ jQuery(document).ready(function($) { var compare = { - name: function(a, b) { - a = a.replace(/^@/, '') && a.replace(/-/g, ''); - b = b.replace(/^@/, '') && b.replace(/-/g, ''); + channel: function(a, b) { + a = a.replace(/^@/i, '') && a.replace(/[-]/gi, ''); + b = b.replace(/^@/i, '') && b.replace(/[-]/gi, ''); if (a < b) { return -1; @@ -20,16 +20,23 @@ jQuery(document).ready(function($) { return a > b ? 1 : 0; } }, - amount: function(a, b) { - a = a.split('.'); - b = b.split('.'); - - a = Number(a[0]) + Number(a[1]); - b = Number(b[0]) + Number(b[1]); + claim: function(a, b) { + if (a < b) { + return -1; + } else { + return a > b ? 1 : 0; + } + }, + posts: function(a, b) { + a = Number(a); + b = Number(b); return a - b; }, - number: function(a, b) { + support: function(a, b) { + a = a.replace(/,/g, ''); + b = b.replace(/,/g, ''); + a = Number(a); b = Number(b); diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 3371f38..575be85 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -52,7 +52,7 @@ class LBRY_Admin } add_action( 'admin_enqueue_scripts', 'load_admin_stylesheet' ); - // Admin JS enqueue + // Admin channel sort JS enqueue function load_admin_script() { if ( ( $_GET['page'] == 'lbrypress') && ( $_GET['tab'] == 'channels' ) ) { wp_enqueue_script( @@ -272,10 +272,12 @@ class LBRY_Admin
ChannelLBRY URLPostsSupportsChannelLBRY URLPostsSupports
- + - - + + + + @@ -286,6 +288,8 @@ class LBRY_Admin 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 ) ) { @@ -303,14 +307,16 @@ class LBRY_Admin + + - + - +
ChannelChannel LBRY URLPostsSupportsClaim ID~ Date CreatedPostsSupports
name, 'lbrypress' ); ?> AddAdd
LBRYPress
LBRYPress
-- 2.45.2 From 0af8b712ebaab99e03da33918bc7ee7db50a65d6 Mon Sep 17 00:00:00 2001 From: Lem Smyth Date: Sun, 20 Feb 2022 21:06:46 -0600 Subject: [PATCH 3/3] build out the add supports page and method --- classes/LBRY_Admin.php | 29 +++++------ classes/LBRY_Daemon.php | 31 ++++++++++++ templates/meta-box.php | 30 ++++++++---- templates/supports-add-form.php | 87 ++++++++++++++++++++++++++++++--- 4 files changed, 148 insertions(+), 29 deletions(-) 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' ) . '
- -
' . __( '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' ) . '
+ ', $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, + ); ?> + + + + + + + + + + + + + + + + + + + +

- +

+