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

@ -16,6 +16,8 @@ class LBRY_Admin
{
add_action('admin_menu', array($this, 'create_options_page'));
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'));
}
/**
@ -173,4 +175,68 @@ class LBRY_Admin
$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
* https://lbryio.github.io/lbry/#wallet_unused_address
* @return string Unused wallet address in base58
*/
public function wallet_unused_address()
@ -28,6 +29,7 @@ class LBRY_Daemon
/**
* Returns the balance of a current LBRY wallet
* https://lbryio.github.io/lbry/cli/#wallet_balance
* @param string $address Wallet Address
* @return float Wallet Balance
*/
@ -49,7 +51,6 @@ class LBRY_Daemon
public function channel_list()
{
$result = $this->request('channel_list');
error_log(print_r(json_decode($result), true));
return null;
}

View file

@ -25,8 +25,25 @@ $channel_list = $LBRY->daemon->channel_list();
<?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">
<form action="<?php echo esc_url(admin_url('admin-post.php')); ?>" method="post">
<?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'); ?>
</form>
</div>