From 3e68befdc128dda8b371d86cf5c5ced4f247e8a4 Mon Sep 17 00:00:00 2001 From: Lemuel Smyth <36257395+lemsmyth@users.noreply.github.com> Date: Sun, 13 Feb 2022 10:39:42 -0600 Subject: [PATCH 1/6] Default license selector (#51) * default license select option * default channel selector --- classes/LBRY_Admin.php | 44 +++++++++++++++++++++++++++++++++++++--- classes/LBRY_Network.php | 8 +++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 8578b91..7cb8b03 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -68,7 +68,7 @@ class LBRY_Admin { // Register the LBRY Setting array register_setting( - LBRY_SETTINGS_GROUP, + 'lbry_general_settings', LBRY_SETTINGS, array( $this, 'sanitize_general_settings' ) ); @@ -90,6 +90,13 @@ class LBRY_Admin LBRY_SETTINGS_SECTION_GENERAL ); + add_settings_field( + 'default_lbry_channel', + 'Default Publish Channel', + array( $this, 'default_channel_callback' ), + LBRY_ADMIN_PAGE, + LBRY_SETTINGS_SECTION_GENERAL + ); add_settings_field( LBRY_LICENSE, 'LBRY Publishing License', @@ -115,7 +122,6 @@ class LBRY_Admin /** - * Speech Admin Page settings */ @@ -255,6 +261,37 @@ class LBRY_Admin ); } + /** + * Prints select to choose a default to publish to channel + */ + public function default_channel_callback() + { + $options = ''; + $channel_list = LBRY()->daemon->channel_list(); + + if ( $channel_list ) { + foreach ( $channel_list as $channel ) { + $selected = $this->options['default_lbry_channel'] === $channel->claim_id; + + $options .= ''; + } + + printf( + '', + 'default_lbry_channel', + LBRY_SETTINGS, + $options + ); + } else { ?> +

Looks like you haven't added any channels yet, you can do that now on the Channels Tab

+ %3$s', + '', LBRY_LICENSE, LBRY_SETTINGS, $options ); } + /** * Prints LBC per publish input */ diff --git a/classes/LBRY_Network.php b/classes/LBRY_Network.php index a32db37..4b4de01 100644 --- a/classes/LBRY_Network.php +++ b/classes/LBRY_Network.php @@ -79,9 +79,11 @@ class LBRY_Network return $post_id; } - $will_publish = (isset($_POST[LBRY_WILL_PUBLISH]) ? $_POST[LBRY_WILL_PUBLISH] : false); - $new_channel = (isset($_POST[LBRY_POST_CHANNEL]) ? $_POST[LBRY_POST_CHANNEL] : null); - $cur_channel = get_post_meta($post_id, LBRY_POST_CHANNEL, true); + $channel = $_POST[LBRY_POST_PUB_CHANNEL]; + $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 ) ); + $license = $_POST[LBRY_POST_PUB_LICENSE]; + $cur_license = get_post_meta( $post_id, LBRY_POST_PUB_LICENSE, true ); + $will_publish = $_POST[LBRY_WILL_PUBLISH]; // Update meta acordingly if (!$will_publish) { From c94a276a76484c3badd5deb617d24689372c414f Mon Sep 17 00:00:00 2001 From: Lemuel Smyth <36257395+lemsmyth@users.noreply.github.com> Date: Sun, 13 Feb 2022 10:57:41 -0600 Subject: [PATCH 2/6] Checkbox set default publish (#52) * checkbox to set default publish --- classes/LBRY_Admin.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 7cb8b03..72d672f 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -91,12 +91,23 @@ class LBRY_Admin ); add_settings_field( + + 'lbry_default_publish_setting', + 'Always Publish to LBRY', + array( $this, 'lbry_always_pub_callback' ), + LBRY_ADMIN_PAGE, + LBRY_SETTINGS_SECTION_GENERAL + ); + + add_settings_field( + 'default_lbry_channel', 'Default Publish Channel', array( $this, 'default_channel_callback' ), LBRY_ADMIN_PAGE, LBRY_SETTINGS_SECTION_GENERAL ); + add_settings_field( LBRY_LICENSE, 'LBRY Publishing License', @@ -261,6 +272,24 @@ class LBRY_Admin ); } + /** + * Checkbox to default to always allow publish on LBRY + */ + public function lbry_always_pub_callback() + { + $options = get_option( LBRY_SETTINGS )['lbry_default_publish_setting']; + if ( ! isset( $options ) ) { + $options = 0; + } + $checked = checked( $options, 1, false ); + printf( + '

Set Default to always Publish to LBRY, this can be adjusted when publishing a New Post.

', + 'lbry_default_publish_setting', + LBRY_SETTINGS, + + ); + } + /** * Prints select to choose a default to publish to channel */ From 992be8ca0ff14a1cdf808b035dd14ba8ee3ccea1 Mon Sep 17 00:00:00 2001 From: Lemuel Smyth <36257395+lemsmyth@users.noreply.github.com> Date: Sun, 13 Feb 2022 11:00:09 -0600 Subject: [PATCH 3/6] Sanitize options callback (#53) * sanitize option settings callbacks --- classes/LBRY_Admin.php | 48 +++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 72d672f..d72ab87 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -181,22 +181,24 @@ class LBRY_Admin public function sanitize_general_settings( $input ) { - if (!empty($input[LBRY_SPEECH_CHANNEL])) { - $channel = $input[LBRY_SPEECH_CHANNEL]; - $channel = str_replace('@', '', $channel); - $input[LBRY_SPEECH_CHANNEL] = $channel; - } + $new_input = get_option( LBRY_SETTINGS ); // get saved data - if (!empty($input[LBRY_SPEECH_PW])) { - $encrypted = $this->encrypt($input['lbry_speech_pw']); - $input[LBRY_SPEECH_PW] = $encrypted; - } else { - // If we have a password and its empty, keep orginal password - if (!empty(get_option(LBRY_SETTINGS)[LBRY_SPEECH_PW])) { - $input[LBRY_SPEECH_PW] = get_option(LBRY_SETTINGS)[LBRY_SPEECH_PW]; - } + if ( isset( $input[LBRY_WALLET] ) ) { + $new_input[LBRY_WALLET] = sanitize_text_field( $input[LBRY_WALLET] ); } - return $input; + $new_input['lbry_default_publish_setting'] = $input['lbry_default_publish_setting']; + + if ( isset( $input['default_lbry_channel'] ) ) { + $new_input['default_lbry_channel'] = sanitize_text_field( $input['default_lbry_channel'] ); + } + $license_array = LBRY()->licenses; + if ( isset( $input[LBRY_LICENSE] ) && ( in_array( $input[LBRY_LICENSE], $license_array ) ) ) { + $new_input[LBRY_LICENSE] = sanitize_text_field( $input[LBRY_LICENSE] ); + } + if ( isset( $input[LBRY_LBC_PUBLISH] ) ) { + $new_input[LBRY_LBC_PUBLISH] = number_format( floatval( $input[LBRY_LBC_PUBLISH] ), 3, '.', '' ); + } + return $new_input; } public function sanitize_speech_settings( $input ) @@ -230,13 +232,6 @@ class LBRY_Admin { print 'This is where you can configure how LBRYPress will distribute your content:'; } - /** - * Section info for the Speech Channel Section - */ - public function speech_section_callback() - { - print 'If you have a Spee.ch account, you can enter your account details here, if you don\'t already have a Spee.ch account, no need to enter anything here.'; - } /** * Section info for the Available Channel(s) Section @@ -256,6 +251,15 @@ class LBRY_Admin options[LBRY_LBC_PUBLISH] ); - } + } /** * Channels Page From f2e0c54e5eee0df75297c4ef79c28a2730e8beec Mon Sep 17 00:00:00 2001 From: Lemuel Smyth <36257395+lemsmyth@users.noreply.github.com> Date: Sun, 13 Feb 2022 11:08:24 -0600 Subject: [PATCH 4/6] Meta box create (#54) * creates and prints the meta box --- classes/LBRYPress.php | 4 +-- classes/LBRY_Admin.php | 5 ++- classes/LBRY_Network.php | 8 ++--- templates/meta-box.php | 69 ++++++++++++++++++++++++++++++++++++++++ templates/meta_box.php | 42 ------------------------ 5 files changed, 77 insertions(+), 51 deletions(-) create mode 100644 templates/meta-box.php delete mode 100644 templates/meta_box.php diff --git a/classes/LBRYPress.php b/classes/LBRYPress.php index 21706fd..89369d0 100644 --- a/classes/LBRYPress.php +++ b/classes/LBRYPress.php @@ -120,8 +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_POST_PUB_CHANNEL', '_lbry_post_pub_channel'); // The meta key for which channel to publish on + $this->define('LBRY_POST_PUB_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 d72ab87..844072a 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -37,7 +37,7 @@ class LBRY_Admin // Admin stylesheet enqueue function load_admin_stylesheet( $hook ) { - if ( ( $_GET['page'] == 'lbrypress' ) ) { + if ( ( $hook == 'post.php' ) || ( $hook == 'post-new.php' ) || ( $_GET['page'] == 'lbrypress' ) ) { wp_enqueue_style( 'lbry-admin', plugins_url( '/admin/css/lbry-admin.css', LBRY_PLUGIN_FILE ), @@ -91,7 +91,6 @@ class LBRY_Admin ); add_settings_field( - 'lbry_default_publish_setting', 'Always Publish to LBRY', array( $this, 'lbry_always_pub_callback' ), @@ -100,7 +99,6 @@ class LBRY_Admin ); add_settings_field( - 'default_lbry_channel', 'Default Publish Channel', array( $this, 'default_channel_callback' ), @@ -276,6 +274,7 @@ class LBRY_Admin ); } + /** * Checkbox to default to always allow publish on LBRY */ diff --git a/classes/LBRY_Network.php b/classes/LBRY_Network.php index 4b4de01..5d88d04 100644 --- a/classes/LBRY_Network.php +++ b/classes/LBRY_Network.php @@ -34,7 +34,7 @@ class LBRY_Network private function post_meta_setup() { // Add the meta boxes - add_action('add_meta_boxes', array($this, 'add_meta_boxes')); + add_action( 'add_meta_boxes', array( $this, 'lbry_meta_boxes' ) ); // Save the post meta on 'save_post' hook add_action('wp_insert_post', array($this, 'save_post_meta'), 11, 2); @@ -43,12 +43,12 @@ class LBRY_Network /** * Adds the meta boxes to the post editing backend */ - public function add_meta_boxes() + public function lbry_meta_boxes( $post ) { // IDEA: Support post types based on user selection add_meta_box( 'lbry-network-publishing', // Unique ID - 'LBRY Network', // Title + __('LBRY Network', 'lbrypress'), // Title array($this, 'meta_box_html'), // Callback function 'post', // Screen Options (or post type) 'side', // Context @@ -107,6 +107,6 @@ class LBRY_Network */ public function meta_box_html( $post ) { - require_once(LBRY_ABSPATH . 'templates/meta_box.php'); + require_once( LBRY_ABSPATH . 'templates/meta-box.php' ); } } diff --git a/templates/meta-box.php b/templates/meta-box.php new file mode 100644 index 0000000..68d51f1 --- /dev/null +++ b/templates/meta-box.php @@ -0,0 +1,69 @@ + 'none (anonymous)', + 'claim_id' => 'null' +); +// Generate a custom nonce +$lbrynonce = wp_create_nonce( 'lbry_publish_post_nonce' ); + +$channels = LBRY()->daemon->channel_list(); +$channels[] = $unnatributed; +$post_id = $post->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']; + +// Sort the channels in a natural way +usort( $channels, array( 'LBRYPress', 'channel_name_comp' ) ); +?> + +
+ +
$channel ) { + $options .= ''; + } + printf( + '', + LBRY_POST_PUB_CHANNEL, + $options + ); + } ?> +
licenses; + $options = ''; + $default_license = get_option(LBRY_SETTINGS)[LBRY_LICENSE]; + $cur_license = get_post_meta( $post_id, LBRY_POST_PUB_LICENSE, true ); + + // Create options list, select current license + if ( $licenses ) { + foreach ( $licenses as $value => $name ) { + $options .= '
diff --git a/templates/meta_box.php b/templates/meta_box.php deleted file mode 100644 index 336469f..0000000 --- a/templates/meta_box.php +++ /dev/null @@ -1,42 +0,0 @@ - 'none (anonymous)', - 'claim_id' => 'null' -); -$channels = LBRY()->daemon->channel_list(); -$channels[] = $unnatributed; -// Sort the channels in a natural way -usort($channels, array('LBRYPress', 'channel_name_comp')); -$cur_channel = get_post_meta($post->ID, LBRY_POST_CHANNEL, true); -$will_publish = get_post_meta($post->ID, LBRY_WILL_PUBLISH, true); -?> - -
- -
- From d6c9c4a55e64d15ee0b6863ed36393ad0f20222d Mon Sep 17 00:00:00 2001 From: Lemuel Smyth <36257395+lemsmyth@users.noreply.github.com> Date: Sun, 13 Feb 2022 11:10:15 -0600 Subject: [PATCH 5/6] Save post meta (#55) * save post meta --- classes/LBRY_Admin.php | 27 ++++++++++++++++++++---- classes/LBRY_Network.php | 44 ++++++++++++++++++++++++---------------- 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 844072a..7829a00 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -117,7 +117,7 @@ class LBRY_Admin add_settings_field( LBRY_LBC_PUBLISH, 'LBC Per Publish', - array( $this, 'lbc_publish_callback' ), + array( $this, 'lbc_per_publish_callback' ), LBRY_ADMIN_PAGE, LBRY_SETTINGS_SECTION_GENERAL ); @@ -275,6 +275,24 @@ class LBRY_Admin } + /** + * Checkbox to default to always allow publish on LBRY + */ + public function lbry_always_pub_callback() + { + $options = get_option( LBRY_SETTINGS )['lbry_default_publish_setting']; + if ( ! isset( $options ) ) { + $options = 0; + } + $checked = checked( $options, 1, false ); + printf( + '

Set Default to always Publish to LBRY, this can be adjusted when publishing a New Post.

', + 'lbry_default_publish_setting', + LBRY_SETTINGS, + + ); + } + /** * Checkbox to default to always allow publish on LBRY */ @@ -355,13 +373,14 @@ class LBRY_Admin /** * Prints LBC per publish input */ - public function lbc_publish_callback() + public function lbc_per_publish_callback() { printf( - '', + '

Current minimum bid 0.001

', LBRY_LBC_PUBLISH, LBRY_SETTINGS, - $this->options[LBRY_LBC_PUBLISH] + $this->options[LBRY_LBC_PUBLISH], + plugin_dir_url( LBRY_PLUGIN_FILE ) . 'admin/images/lbc.png' ); } diff --git a/classes/LBRY_Network.php b/classes/LBRY_Network.php index 5d88d04..54564a2 100644 --- a/classes/LBRY_Network.php +++ b/classes/LBRY_Network.php @@ -37,7 +37,7 @@ class LBRY_Network add_action( 'add_meta_boxes', array( $this, 'lbry_meta_boxes' ) ); // Save the post meta on 'save_post' hook - add_action('wp_insert_post', array($this, 'save_post_meta'), 11, 2); + add_action( 'wp_insert_post', array( $this, 'save_post_meta' ), 11, 2 ); } /** @@ -64,20 +64,26 @@ class LBRY_Network */ public function save_post_meta( $post_id, $post ) { - if ($post->post_type != 'post') { - return; + if ( $post->post_type != 'post' ) { + return $post_id; + } + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { + return $post_id; } - // Verify the nonce before proceeding. - if (!isset($_POST['_lbrynonce']) || !wp_verify_nonce($_POST['_lbrynonce'], 'lbry_publish_channels')) { + if ( ! isset( $_POST['_lbrynonce'] ) || ! wp_verify_nonce( $_POST['_lbrynonce'], 'lbry_publish_post_nonce' ) ) { + //LBRY()->notice->set_notice('error', 'Security check failed' ); return $post_id; } - - // Check if the current user has permission to edit the post. - $post_type = get_post_type_object($post->post_type); - if (!current_user_can($post_type->cap->edit_post, $post_id)) { + $post_type = get_post_type_object( $post->post_type ); + if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) { return $post_id; } + if ( ( $_POST[LBRY_WILL_PUBLISH] ) && $_POST[LBRY_WILL_PUBLISH] != get_post_meta( $post_id, LBRY_WILL_PUBLISH, true ) ) { + update_post_meta( $post_id, LBRY_WILL_PUBLISH, $_POST[LBRY_WILL_PUBLISH] ); + } elseif ( ! isset( $_POST[LBRY_WILL_PUBLISH] ) ) { + update_post_meta( $post_id, LBRY_WILL_PUBLISH, 0 ); + } $channel = $_POST[LBRY_POST_PUB_CHANNEL]; $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 ) ); @@ -86,18 +92,20 @@ class LBRY_Network $will_publish = $_POST[LBRY_WILL_PUBLISH]; // Update meta acordingly - if (!$will_publish) { - update_post_meta($post_id, LBRY_WILL_PUBLISH, 'false'); - } else { - update_post_meta($post_id, LBRY_WILL_PUBLISH, 'true'); + + if ( $channel !== $cur_channel ) { + update_post_meta( $post_id, LBRY_POST_PUB_CHANNEL, $channel ); + delete_post_meta( $post_id, '_lbry_channel'); // remove the _lbry_channel if already set from the post and replaces with _lbry_post_pub_channel to avoid confusion + } elseif ( $channel === $cur_channel && ( $cur_channel === get_post_meta( $post_id, '_lbry_channel', true ) ) ) { + update_post_meta( $post_id, LBRY_POST_PUB_CHANNEL, $channel ); + delete_post_meta( $post_id, '_lbry_channel'); // remove the _lbry_channel if already set from the post and replaces with _lbry_post_pub_channel to avoid confusion } - if ($new_channel !== $cur_channel) { - update_post_meta($post_id, LBRY_POST_CHANNEL, $new_channel); + if ( $license !== $cur_license ) { + update_post_meta( $post_id, LBRY_POST_PUB_LICENSE, $license ); } - - if ($will_publish && $post->post_status == 'publish') { + if ( ( $will_publish ) && ( $will_publish == 1 ) && $post->post_status == 'publish') { // Publish the post on the LBRY Network - $this->publisher->publish($post, get_post_meta($post_id, LBRY_POST_CHANNEL, true)); + $this->publisher->publish( $post, $channel, $license ); } } From 2c9379394fbb88565a1f53b487407a7a3cf22295 Mon Sep 17 00:00:00 2001 From: Lemuel Smyth <36257395+lemsmyth@users.noreply.github.com> Date: Sun, 13 Feb 2022 11:11:14 -0600 Subject: [PATCH 6/6] Defaults set (#56) * set defaults --- classes/LBRYPress.php | 8 +++++--- classes/LBRY_Admin.php | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/classes/LBRYPress.php b/classes/LBRYPress.php index 89369d0..52f392d 100644 --- a/classes/LBRYPress.php +++ b/classes/LBRYPress.php @@ -185,9 +185,11 @@ class LBRYPress //Default options $option_defaults = array( - - LBRY_LICENSE => $this->licenses[0], - LBRY_LBC_PUBLISH => 1 + LBRY_WALLET => '', + 'lbry_default_publish_setting' => '', + 'default_lbry_channel' => '', + LBRY_LICENSE => '', + LBRY_LBC_PUBLISH => 0.001, ); add_option( LBRY_SETTINGS, $option_defaults, false ); diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 7829a00..3c00622 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -311,6 +311,24 @@ class LBRY_Admin ); } + /** + * Checkbox to default to always allow publish on LBRY + */ + public function lbry_always_pub_callback() + { + $options = get_option( LBRY_SETTINGS )['lbry_default_publish_setting']; + if ( ! isset( $options ) ) { + $options = 0; + } + $checked = checked( $options, 1, false ); + printf( + '

Set Default to always Publish to LBRY, this can be adjusted when publishing a New Post.

', + 'lbry_default_publish_setting', + LBRY_SETTINGS, + + ); + } + /** * Prints select to choose a default to publish to channel */