From b026f274851102027555f93afae4d6a7b2a717b6 Mon Sep 17 00:00:00 2001 From: Lemuel Smyth <36257395+lemsmyth@users.noreply.github.com> Date: Sat, 12 Feb 2022 12:00:14 -0600 Subject: [PATCH] Speech tab (#46) * Print the speech tab inputs to the Spee.ch page --- classes/LBRYPress.php | 12 ++- classes/LBRY_Admin.php | 177 ++++++++++++++++++++++++++-------------- classes/LBRY_Speech.php | 6 +- 3 files changed, 128 insertions(+), 67 deletions(-) diff --git a/classes/LBRYPress.php b/classes/LBRYPress.php index 8feb5d8..4e80554 100644 --- a/classes/LBRYPress.php +++ b/classes/LBRYPress.php @@ -183,7 +183,7 @@ class LBRYPress // Default options $option_defaults = array( - LBRY_SPEECH => null, + LBRY_LICENSE => $this->licenses[0], LBRY_LBC_PUBLISH => 1 ); @@ -191,6 +191,16 @@ class LBRYPress add_option(LBRY_SETTINGS, $option_defaults, false); } + if ( ! get_option( LBRY_SPEECH_SETTINGS ) ) { + // Default Speech Settings + $option_defaults = array( + LBRY_SPEECH =>'', + LBRY_SPEECH_CHANNEL => '', + LBRY_SPEECH_PW => '', + ); + add_option( LBRY_SPEECH_SETTINGS, $option_defaults, false ); + } + // COMBAK: decide if we need to check for missing or corrupt settings. May be unecessary. // Double check we have all settings, if not, update with default // $current_settings = get_option(LBRY_SETTINGS); diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 67c8c1b..53f4729 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -33,7 +33,7 @@ class LBRY_Admin array($this, 'options_page_html'), plugin_dir_url(LBRY_PLUGIN_FILE) . '/admin/images/lbry-logo.svg' ); - + // Admin stylesheet enqueue function load_admin_stylesheet( $hook ) { @@ -79,30 +79,6 @@ class LBRY_Admin LBRY_SETTINGS_SECTION_GENERAL ); - add_settings_field( - LBRY_SPEECH, - 'Spee.ch URL', - array( $this, 'speech_callback' ), - LBRY_ADMIN_PAGE, - LBRY_SETTINGS_SECTION_GENERAL - ); - - add_settings_field( - LBRY_SPEECH_CHANNEL, - 'Spee.ch Channel', - array( $this, 'speech_channel_callback' ), - LBRY_ADMIN_PAGE, - LBRY_SETTINGS_SECTION_GENERAL - ); - - add_settings_field( - LBRY_SPEECH_PW, - 'Spee.ch Password', - array( $this, 'speech_pw_callback' ), - LBRY_ADMIN_PAGE, - LBRY_SETTINGS_SECTION_GENERAL - ); - add_settings_field( LBRY_LICENSE, 'LBRY Publishing License', @@ -118,6 +94,47 @@ class LBRY_Admin LBRY_ADMIN_PAGE, LBRY_SETTINGS_SECTION_GENERAL ); + + /** + * Speech Admin Page settings + */ + + register_setting( + LBRY_SPEECH_SETTINGS, + LBRY_SPEECH_SETTINGS, + array( $this, 'sanitize_speech_settings' ) + ); + + add_settings_section( + 'lbry_settings_section_speech', // ID + 'Spee.ch Channel Settings', // Title + array( $this, 'speech_section_callback' ), // Callback + 'lbrypress-speech' // Page + ); + + add_settings_field( + LBRY_SPEECH, + 'Spee.ch URL', + array( $this, 'speech_callback' ), + 'lbrypress-speech', + 'lbry_settings_section_speech' + ); + + add_settings_field( + LBRY_SPEECH_CHANNEL, + 'Spee.ch Channel', + array( $this, 'speech_channel_callback' ), + 'lbrypress-speech', + 'lbry_settings_section_speech' + ); + + add_settings_field( + LBRY_SPEECH_PW, + 'Spee.ch Password', + array( $this, 'speech_pw_callback' ), + 'lbrypress-speech', + 'lbry_settings_section_speech' + ); } /** @@ -153,10 +170,33 @@ class LBRY_Admin $input[LBRY_SPEECH_PW] = get_option(LBRY_SETTINGS)[LBRY_SPEECH_PW]; } } - return $input; } + public function sanitize_speech_settings( $input ) + { + $new_input = get_option( LBRY_SPEECH_SETTINGS ); + if ( isset( $input[LBRY_SPEECH] ) ) { + $new_input[LBRY_SPEECH] = sanitize_text_field( $input[LBRY_SPEECH] ); + } + if ( isset( $input[LBRY_SPEECH_CHANNEL] ) ) { + $channel = $input[LBRY_SPEECH_CHANNEL]; + $channel = str_replace( '@', '', $channel ); + $new_input[LBRY_SPEECH_CHANNEL] = sanitize_user( $channel ); + } + if ( isset( $input[LBRY_SPEECH_PW] ) ) { + $input[LBRY_SPEECH_PW] = sanitize_text_field( $input[LBRY_SPEECH_PW] ); + $encrypted = $this->encrypt( $input[LBRY_SPEECH_PW] ); + $new_input[LBRY_SPEECH_PW] = $encrypted; + } else { + // If we have a password and it's empty, keep original password + if ( empty( $input[LBRY_SPEECH_PW] ) ) + $new_input[LBRY_SPEECH_PW] = get_option( LBRY_SPEECH_SETTINGS[LBRY_SPEECH_PW] ); + } + return $new_input; + update_option( LBRY_SPEECH_SETTINGS, $new_input ); + } + /** * Section info for the General Section */ @@ -164,6 +204,13 @@ 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.'; + } /** * Prints Wallet input @@ -181,43 +228,6 @@ class LBRY_Admin ); } - /** - * Prints Spee.ch input - */ - public function speech_callback() - { - printf( - '', - LBRY_SPEECH, - LBRY_SETTINGS, - isset($this->options[LBRY_SPEECH]) ? esc_attr($this->options[LBRY_SPEECH]) : '' - ); - } - - /** - * Prints Spee.ch channel input - */ - public function speech_channel_callback() - { - printf( - '@', - LBRY_SPEECH_CHANNEL, - LBRY_SETTINGS, - isset($this->options[LBRY_SPEECH_CHANNEL]) ? esc_attr($this->options[LBRY_SPEECH_CHANNEL]) : '' - ); - } - - /** - * Prints Spee.ch password input - */ - public function speech_pw_callback() - { - printf( - '', + LBRY_SPEECH, + LBRY_SPEECH_SETTINGS, + isset( $options[LBRY_SPEECH] ) ? $options[LBRY_SPEECH] : '', + ); + } + + /** + * Prints Spee.ch channel input + */ + public function speech_channel_callback() + { + $options = get_option( LBRY_SPEECH_SETTINGS ); + printf( + '', + LBRY_SPEECH_CHANNEL, + LBRY_SPEECH_SETTINGS, + isset( $options[LBRY_SPEECH_CHANNEL] ) ? $options[LBRY_SPEECH_CHANNEL] : '', + ); + } + + /** + * Prints Spee.ch password input + */ + public function speech_pw_callback() + { + printf( + '', + LBRY_SPEECH_PW, + LBRY_SPEECH_SETTINGS, + ); + } + + /** * Handles new channel form submission */ diff --git a/classes/LBRY_Speech.php b/classes/LBRY_Speech.php index 665448d..64b3d33 100644 --- a/classes/LBRY_Speech.php +++ b/classes/LBRY_Speech.php @@ -43,7 +43,7 @@ class LBRY_Speech return false; } - $speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH]; + $speech_url = get_option(LBRY_SPEECH_SETTINGS)[LBRY_SPEECH]; // Die if we don't have a spee.ch url if (!$speech_url || $speech_url === '') { @@ -66,7 +66,7 @@ class LBRY_Speech ); // Pull Channel and Password from config file for now - $speech_channel = get_option(LBRY_SETTINGS)[LBRY_SPEECH_CHANNEL]; + $speech_channel = get_option(LBRY_SPEECH_SETTINGS)[LBRY_SPEECH_CHANNEL]; $speech_pw = LBRY()->admin->get_speech_pw(); if (!empty($speech_channel) && !empty($speech_pw)) { $params['channelName'] = '@' . $speech_channel; @@ -229,7 +229,7 @@ class LBRY_Speech */ private function build_request($method, $params = array()) { - $speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH]; + $speech_url = get_option(LBRY_SPEECH_SETTINGS)[LBRY_SPEECH]; // Die if no URL if (!$speech_url) {