Added most of the default options and settings page, handled initialization and updating using WP Settings API

This commit is contained in:
Paul Kirby 2018-09-12 18:39:13 -05:00
parent 05a4709b07
commit 566a596091
4 changed files with 213 additions and 27 deletions

View file

@ -7,25 +7,19 @@
class LBRY_Admin
{
private $options;
/**
* [__construct description]
* LBRY_Admin Constructor
*/
public function __construct()
{
}
/**
* Called to initialize the settings interface
* @return [type] [description]
*/
public function settings_init()
{
add_action('admin_menu', array($this, 'create_options_page'));
add_action('admin_init', array($this, 'page_init'));
}
/**
* Creates the options page in the WP admin interface
* @return [type] [description]
*/
public function create_options_page()
{
@ -33,17 +27,150 @@ class LBRY_Admin
__('LBRYPress Settings', 'lbrypress'),
__('LBRYPress', 'lbrypress'),
'manage_options',
'LBRYPress',
LBRY_ADMIN_PAGE,
array($this, 'options_page_html')
);
}
/**
* Registers all settings for the plugin
*/
public function page_init()
{
// Register the LBRY Setting array
register_setting(LBRY_SETTINGS_GROUP, LBRY_SETTINGS, array($this, 'sanitize'));
// Add Required Settings Sections
add_settings_section(
LBRY_SETTINGS_SECTION_GENERAL, // ID
'General Settings', // Title
array( $this, 'general_section_info' ), // Callback
LBRY_ADMIN_PAGE // Page
);
// Add all settings fields
add_settings_field(
LBRY_WALLET,
'LBRY Wallet Address',
array( $this, 'wallet_callback' ),
LBRY_ADMIN_PAGE,
LBRY_SETTINGS_SECTION_GENERAL
);
add_settings_field(
LBRY_SPEECH, // ID
'Spee.ch URL', // Title
array( $this, 'speech_callback' ), // Callback
LBRY_ADMIN_PAGE, // Page
LBRY_SETTINGS_SECTION_GENERAL // Section
);
add_settings_field(
LBRY_LICENSE,
'LBRY Publishing License',
array( $this, 'license_callback' ),
LBRY_ADMIN_PAGE,
LBRY_SETTINGS_SECTION_GENERAL
);
add_settings_field(
LBRY_LBC_PUBLISH,
'LBC Per Publish',
array( $this, 'lbc_publish_callback' ),
LBRY_ADMIN_PAGE,
LBRY_SETTINGS_SECTION_GENERAL
);
}
/**
* Returns the Options Page HTML for the plugin
* @return [type] [description]
*/
public function options_page_html()
{
// Set class property to be referenced in callbacks
$this->options = get_option(LBRY_SETTINGS);
require_once(LBRY_ABSPATH . 'templates/options_page.php');
}
/**
* Sanitizes setting input
* // TODO Actually sanitize the input
*/
public function sanitize($input)
{
return $input;
}
/**
* Section info for the General Section
*/
public function general_section_info()
{
print 'This is where you can configure how LBRYPress will distribute your content:';
}
/**
* Prints Wallet input
*/
public function wallet_callback()
{
printf(
'<input type="text" id="%1$s" name="%2$s[%1$s]" value="%3$s" readonly />',
LBRY_WALLET,
LBRY_SETTINGS,
isset($this->options[LBRY_WALLET]) ? esc_attr($this->options[LBRY_WALLET]) : ''
);
}
/**
* Prints Spee.ch input
*/
public function speech_callback()
{
printf(
'<input type="text" id="%1$s" name="%2$s[%1$s]" value="%3$s" placeholder="https://your-speech-address.com"/>',
LBRY_SPEECH,
LBRY_SETTINGS,
isset($this->options[LBRY_SPEECH]) ? esc_attr($this->options[LBRY_SPEECH]) : ''
);
}
/**
* Prints License input
*/
public function license_callback()
{
// TODO: Maybe make this more elegant?
$options = '';
// Create options list, select current license
foreach (LBRY_AVAILABLE_LICENSES as $value => $name) {
$selected = $this->options[LBRY_LICENSE] === $value;
$options .= '<option value="' . $value . '"';
if ($selected) {
$options .= ' selected';
}
$options .= '>'. $name . '</option>';
}
printf(
'<select id="%1$s" name="%2$s[%1$s]">%3$s</select>',
LBRY_LICENSE,
LBRY_SETTINGS,
$options
);
}
/**
* Prints LBC per publish input
*/
public function lbc_publish_callback()
{
printf(
'<input type="number" id="%1$s" name="%2$s[%1$s]" value="%3$s" min="0.01" step="0.01"/>',
LBRY_LBC_PUBLISH,
LBRY_SETTINGS,
$this->options[LBRY_LBC_PUBLISH]
);
}
}

View file

@ -23,7 +23,7 @@ class LBRY_Daemon
public function wallet_unused_address()
{
$result = $this->request('wallet_unused_address');
return json_decode($result->result);
return json_decode($result)->result;
}
/**
@ -42,10 +42,31 @@ class LBRY_Daemon
return json_decode($result)->result;
}
/**
* https://lbryio.github.io/lbry/#channel_list
* @return array claim dictionary
*/
public function channel_list()
{
$result = $this->request('channel_list');
error_log(print_r(json_decode($result), true));
return null;
}
/**
* https://lbryio.github.io/lbry/#channel_new
* @return array dictionary containing result of the request
*/
public function channel_new()
{
}
/**
* Sends a cURL request to the LBRY Daemon
* @param string $method The method to call on the LBRY API
* @param array $params The Parameters to send the LBRY API Call
* @return string The cURL response
*/
private function request($method, $params = array())
{
// JSONify our request data
@ -96,7 +117,6 @@ class LBRY_Daemon
$filepath = LBRY_URI . '/' . $output_filename;
`chmod +x {$filepath}`;
error_log(`{$filepath} status`);
`{$filepath} start &`;

View file

@ -83,10 +83,19 @@ class LBRYPress
$this->define('LBRY_VERSION', $this->version);
// Library Options Names
$this->define('LBRY_SETTINGS_GROUP', 'lbry_settings_group');
$this->define('LBRY_SETTINGS', 'lbry_settings');
$this->define('LBRY_SETTINGS_SECTION_GENERAL', 'lbry_settings_section_general');
$this->define('LBRY_ADMIN_PAGE', 'lbrypress');
$this->define('LBRY_WALLET', 'lbry_wallet'); // the wallet address
$this->define('LBRY_SPEECH', 'lbry_speech'); // the spee.ch address
$this->define('LBRY_LICENSE', 'lbry_license'); // the license to publish with to the LBRY network
$this->define('LBRY_LBC_PUBLISH', 'lbry_lbc_publish'); // amount of lbc to use per publish
$this->define('LBRY_AVAILABLE_LICENSES', array(
'mit' => 'MIT',
'license2' => 'License 2',
'license3' => 'License 3'
));
}
/**
@ -108,7 +117,6 @@ class LBRYPress
private function init()
{
$this->daemon = new LBRY_Daemon();
$this->admin = new LBRY_Admin();
$this->speech = new LBRY_Speech();
}
@ -122,7 +130,7 @@ class LBRYPress
// Admin request
if (is_admin()) {
$this->admin->settings_init();
$this->admin = new LBRY_Admin();
}
}
@ -131,12 +139,35 @@ class LBRYPress
*/
public function activate()
{
// TODO: Make sure errors are thrown if daemon can't be contacted, stop activation
// Add options to the options table we need
if (! get_option(LBRY_WALLET)) {
if (! get_option(LBRY_SETTINGS)) {
// Get a wallet address
$wallet_address = $this->daemon->wallet_unused_address();
add_option(LBRY_WALLET, $wallet_address);
// Default options
$option_defaults = array(
LBRY_WALLET => $wallet_address,
LBRY_SPEECH => null,
LBRY_LICENSE => 'mit',
LBRY_LBC_PUBLISH => 1
);
add_option(LBRY_SETTINGS, $option_defaults, false);
}
// TODO: 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);
// $new_settings = $current_settings;
// foreach ($option_defaults as $key => $value) {
// if (! array_key_exists($key, $current_settings)) {
// $new_settings[$key] = $value;
// }
// }
// update_option(LBRY_SETTINGS, $new_settings);
error_log('Activated');
}

View file

@ -1,24 +1,32 @@
<?php
$LBRY = LBRY();
$wallet_balance = $LBRY->daemon->wallet_balance();
$speech_address = $LBRY->speech->get_address() || '';
$channel_list = $LBRY->daemon->channel_list();
?>
<div class="wrap">
<h1><?= esc_html(get_admin_page_title()); ?></h1>
<h2>Your wallet address:</h2>
<code><?= get_option(LBRY_WALLET); ?></code>
<h2>Your wallet amount:</h2>
<code><?= number_format($wallet_balance, 6, '.', ','); ?></code>
<form action="options.php" method="post">
<label for="speech_address">
<h2>Your Spee.ch server address to act as a cdn for assets:</h2>
<p class="form-help">Learn more about spee.ch <a href="https://github.com/lbryio/spee.ch" target="_blank">here</a>.</p>
</label>
<input type="text" name="speech_address" placeholder="https://your-speech-address.com" value="<?= $speech_address ?>">
<?php
settings_fields(LBRY_SETTINGS_GROUP);
do_settings_sections(LBRY_ADMIN_PAGE);
submit_button('Save Settings');
?>
</form>
<h2>Your Publishable Channels</h2>
<?php if ($channel_list): ?>
<?php else: ?>
<p>Looks like you haven't added any channels yet, feel free to do so below:</p>
<?php endif; ?>
<h2>Add a new channel to publish to:</h2>
<form action="" method="post">
<input type="text" name="new_channel" value="" placeholder="Your New Channel">
<?php submit_button('Add New Channel'); ?>
</form>
</div>