Created form handler for channel list, set up admin notice logging functionality for the whole plugin

This commit is contained in:
Paul Kirby 2018-09-13 14:02:32 -05:00
parent 566a596091
commit ce80e287ff
3 changed files with 120 additions and 36 deletions

View file

@ -10,17 +10,19 @@ class LBRY_Admin
private $options; private $options;
/** /**
* LBRY_Admin Constructor * LBRY_Admin Constructor
*/ */
public function __construct() public function __construct()
{ {
add_action('admin_menu', array($this, 'create_options_page')); add_action('admin_menu', array($this, 'create_options_page'));
add_action('admin_init', array($this, 'page_init')); add_action('admin_init', array($this, 'page_init'));
add_action('admin_post_lbry_add_channel', array($this, 'add_channel'));
add_action('admin_notices', array($this, 'admin_notices'));
} }
/** /**
* Creates the options page in the WP admin interface * Creates the options page in the WP admin interface
*/ */
public function create_options_page() public function create_options_page()
{ {
add_options_page( add_options_page(
@ -33,8 +35,8 @@ class LBRY_Admin
} }
/** /**
* Registers all settings for the plugin * Registers all settings for the plugin
*/ */
public function page_init() public function page_init()
{ {
// Register the LBRY Setting array // Register the LBRY Setting array
@ -42,10 +44,10 @@ class LBRY_Admin
// Add Required Settings Sections // Add Required Settings Sections
add_settings_section( add_settings_section(
LBRY_SETTINGS_SECTION_GENERAL, // ID LBRY_SETTINGS_SECTION_GENERAL, // ID
'General Settings', // Title 'General Settings', // Title
array( $this, 'general_section_info' ), // Callback array( $this, 'general_section_info' ), // Callback
LBRY_ADMIN_PAGE // Page LBRY_ADMIN_PAGE // Page
); );
// Add all settings fields // Add all settings fields
@ -83,8 +85,8 @@ class LBRY_Admin
} }
/** /**
* Returns the Options Page HTML for the plugin * Returns the Options Page HTML for the plugin
*/ */
public function options_page_html() public function options_page_html()
{ {
// Set class property to be referenced in callbacks // Set class property to be referenced in callbacks
@ -93,25 +95,25 @@ class LBRY_Admin
} }
/** /**
* Sanitizes setting input * Sanitizes setting input
* // TODO Actually sanitize the input * // TODO Actually sanitize the input
*/ */
public function sanitize($input) public function sanitize($input)
{ {
return $input; return $input;
} }
/** /**
* Section info for the General Section * Section info for the General Section
*/ */
public function general_section_info() public function general_section_info()
{ {
print 'This is where you can configure how LBRYPress will distribute your content:'; print 'This is where you can configure how LBRYPress will distribute your content:';
} }
/** /**
* Prints Wallet input * Prints Wallet input
*/ */
public function wallet_callback() public function wallet_callback()
{ {
printf( printf(
@ -123,21 +125,21 @@ class LBRY_Admin
} }
/** /**
* Prints Spee.ch input * Prints Spee.ch input
*/ */
public function speech_callback() public function speech_callback()
{ {
printf( printf(
'<input type="text" id="%1$s" name="%2$s[%1$s]" value="%3$s" placeholder="https://your-speech-address.com"/>', '<input type="text" id="%1$s" name="%2$s[%1$s]" value="%3$s" placeholder="https://your-speech-address.com"/>',
LBRY_SPEECH, LBRY_SPEECH,
LBRY_SETTINGS, LBRY_SETTINGS,
isset($this->options[LBRY_SPEECH]) ? esc_attr($this->options[LBRY_SPEECH]) : '' isset($this->options[LBRY_SPEECH]) ? esc_attr($this->options[LBRY_SPEECH]) : ''
); );
} }
/** /**
* Prints License input * Prints License input
*/ */
public function license_callback() public function license_callback()
{ {
// TODO: Maybe make this more elegant? // TODO: Maybe make this more elegant?
@ -162,15 +164,79 @@ class LBRY_Admin
} }
/** /**
* Prints LBC per publish input * Prints LBC per publish input
*/ */
public function lbc_publish_callback() public function lbc_publish_callback()
{ {
printf( printf(
'<input type="number" id="%1$s" name="%2$s[%1$s]" value="%3$s" min="0.01" step="0.01"/>', '<input type="number" id="%1$s" name="%2$s[%1$s]" value="%3$s" min="0.01" step="0.01"/>',
LBRY_LBC_PUBLISH, LBRY_LBC_PUBLISH,
LBRY_SETTINGS, LBRY_SETTINGS,
$this->options[LBRY_LBC_PUBLISH] $this->options[LBRY_LBC_PUBLISH]
); );
} }
/**
* Handles new channel form submission
*/
public function add_channel()
{
$redirect_url = admin_url('options-general.php?page=' . LBRY_ADMIN_PAGE);
// Check that nonce
if (! isset($_POST['_lbrynonce']) || ! wp_verify_nonce($_POST['_lbrynonce'], 'lbry_add_channel')) {
$this->set_notice('error');
} else {
$this->set_notice('success', 'Successfully added a new channel!', true);
}
wp_safe_redirect($redirect_url);
exit();
}
/**
* Displays all messages set with the lbry_notices transient
*/
public function admin_notices()
{
if (get_transient('lbry_notices')) {
$notices = get_transient('lbry_notices');
foreach ($notices as $key => $notice) {
$this->create_admin_notice($notice);
}
delete_transient('lbry_notices');
}
}
/**
* Sets transients for admin errors
*/
private function set_notice($status = 'error', $message = 'Something went wrong', $is_dismissible = false)
{
$notice = array(
'status' => $status,
'message' => $message,
'is_dismissible' => $is_dismissible
);
if (! get_transient('lbry_notices')) {
set_transient('lbry_notices', array($notice));
} else {
$notices = get_transient('lbry_notices');
$notices[] = $notice;
set_transient('lbry_notices', $notices);
}
}
/**
* Prints an admin notice
*/
private function create_admin_notice($notice)
{
$class = 'notice notice-' . $notice['status'];
if ($notice['is_dismissible']) {
$class .= ' is-dismissible';
}
printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($notice['message']));
}
} }

View file

@ -18,6 +18,7 @@ class LBRY_Daemon
/** /**
* Returns an unused wallet address * Returns an unused wallet address
* https://lbryio.github.io/lbry/#wallet_unused_address
* @return string Unused wallet address in base58 * @return string Unused wallet address in base58
*/ */
public function wallet_unused_address() public function wallet_unused_address()
@ -28,6 +29,7 @@ class LBRY_Daemon
/** /**
* Returns the balance of a current LBRY wallet * Returns the balance of a current LBRY wallet
* https://lbryio.github.io/lbry/cli/#wallet_balance
* @param string $address Wallet Address * @param string $address Wallet Address
* @return float Wallet Balance * @return float Wallet Balance
*/ */
@ -49,7 +51,6 @@ class LBRY_Daemon
public function channel_list() public function channel_list()
{ {
$result = $this->request('channel_list'); $result = $this->request('channel_list');
error_log(print_r(json_decode($result), true));
return null; return null;
} }

View file

@ -25,8 +25,25 @@ $channel_list = $LBRY->daemon->channel_list();
<?php endif; ?> <?php endif; ?>
<h2>Add a new channel to publish to:</h2> <h2>Add a new channel to publish to:</h2>
<form action="" method="post"> <form action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="post">
<input type="text" name="new_channel" value="" placeholder="Your New Channel"> <?php wp_nonce_field('lbry_add_channel', '_lbrynonce'); ?>
<input type="hidden" name="action" value="lbry_add_channel">
<table class="form-table">
<tbody>
<tr>
<th scope="row">New Channel Name</th>
<td>
<input type="text" name="new_channel" value="" placeholder="Your New Channel" required>
</td>
</tr>
<tr>
<th scope="row">Amount of LBC to Bid</th>
<td>
<input type="number" step="0.1" min="0.1" name="bid_amount" value="10" required>
</td>
</tr>
</tbody>
</table>
<?php submit_button('Add New Channel'); ?> <?php submit_button('Add New Channel'); ?>
</form> </form>
</div> </div>