Compare commits

...

8 commits

Author SHA1 Message Date
Paul Kirby 89a7c55dfe Automatically try to start the Daemon 2020-02-17 22:18:26 -06:00
Paul Kirby 920591a171 Initial attempt at starting daemon in background 2020-02-17 20:02:27 -06:00
Paul Kirby 94c52bb5eb slightly more info on the help page 2020-02-17 19:24:20 -06:00
Paul Kirby 8ff7ee890e More useful links on help page 2020-02-17 19:15:59 -06:00
Paul Kirby 983e59cd78 Helpful error if daemon isn't running 2020-02-17 19:13:55 -06:00
Paul Kirby 0ceb343be7 Stubbed out test_daemon 2020-02-17 18:17:57 -06:00
Paul Kirby 81306394b7 Added basic template for help page 2020-02-17 16:56:51 -06:00
Paul Kirby be64d7a523 Added help page, made multi-level menu 2020-02-17 16:40:51 -06:00
5 changed files with 168 additions and 12 deletions

View file

@ -122,6 +122,7 @@ class LBRYPress
$this->define('LBRY_CLAIM_ID', '_lbry_claim_id'); // The Claim ID for the post as it was published on LBRY $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_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 $this->define('LBRY_SPEECH_ASSET_URL', 'speech_asset_url'); // The meta key for an asset's speech url
$this->define('LBRY_DAEMON_PID', 'lbry_daemon_pid');
} }
/** /**
@ -142,13 +143,13 @@ class LBRYPress
*/ */
private function init() private function init()
{ {
$this->notice = new LBRY_Admin_Notice();
$this->daemon = new LBRY_Daemon(); $this->daemon = new LBRY_Daemon();
$this->speech = new LBRY_Speech(); $this->speech = new LBRY_Speech();
// Admin request // Admin request
if (is_admin()) { if (is_admin()) {
$this->admin = new LBRY_Admin(); $this->admin = new LBRY_Admin();
$this->notice = new LBRY_Admin_Notice();
$this->network = new LBRY_Network(); $this->network = new LBRY_Network();
} }
} }
@ -184,7 +185,8 @@ class LBRYPress
$option_defaults = array( $option_defaults = array(
LBRY_SPEECH => null, LBRY_SPEECH => null,
LBRY_LICENSE => $this->licenses[0], LBRY_LICENSE => $this->licenses[0],
LBRY_LBC_PUBLISH => 1 LBRY_LBC_PUBLISH => 1,
LBRY_DAEMON_PID => false
); );
add_option(LBRY_SETTINGS, $option_defaults, false); add_option(LBRY_SETTINGS, $option_defaults, false);

View file

@ -14,7 +14,7 @@ class LBRY_Admin
*/ */
public function __construct() public function __construct()
{ {
add_action('admin_menu', array($this, 'create_options_page')); add_action('admin_menu', array($this, 'create_menu_pages'));
add_action('admin_init', array($this, 'page_init')); add_action('admin_init', array($this, 'page_init'));
add_action('admin_init', array($this, 'wallet_balance_warning')); add_action('admin_init', array($this, 'wallet_balance_warning'));
add_action('admin_post_lbry_add_channel', array($this, 'add_channel')); add_action('admin_post_lbry_add_channel', array($this, 'add_channel'));
@ -23,16 +23,34 @@ class LBRY_Admin
/** /**
* 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_menu_pages()
{ {
add_menu_page( add_menu_page(
__('LBRYPress Settings', 'lbrypress'), __('LBRYPress Settings', 'lbrypress'),
__('LBRYPress', 'lbrypress'), __('LBRYPress', 'lbrypress'),
'manage_options', 'manage_options',
LBRY_ADMIN_PAGE, LBRY_ADMIN_PAGE,
array($this, 'options_page_html'), '',
plugin_dir_url(LBRY_PLUGIN_FILE) . '/admin/images/lbry-logo.svg' plugin_dir_url(LBRY_PLUGIN_FILE) . '/admin/images/lbry-logo.svg'
); );
add_submenu_page(
LBRY_ADMIN_PAGE,
__('LBRYPress Settings', 'lbrypress'),
__('Settings', 'lbrypress'),
'manage_options',
LBRY_ADMIN_PAGE,
array($this, 'options_page_html')
);
add_submenu_page(
LBRY_ADMIN_PAGE,
__('LBRYPress Help', 'lbrypress'),
__('Help', 'lbrypress'),
'manage_options',
'lbrypress-help',
array($this, 'help_page_html')
);
} }
/** /**
@ -111,6 +129,14 @@ class LBRY_Admin
require_once(LBRY_ABSPATH . 'templates/options_page.php'); require_once(LBRY_ABSPATH . 'templates/options_page.php');
} }
/**
* Returns the Help Page HTML for the plugin
*/
public function help_page_html()
{
require_once(LBRY_ABSPATH . 'templates/help_page.php');
}
/** /**
* Sanitizes setting input * Sanitizes setting input
* // COMBAK Potentially sanitize more * // COMBAK Potentially sanitize more

View file

@ -42,7 +42,9 @@ class LBRY_Admin_Notice
set_transient('lbry_notices', array($notice)); set_transient('lbry_notices', array($notice));
} else { } else {
$notices = get_transient('lbry_notices'); $notices = get_transient('lbry_notices');
if (!in_array($notice, $notices)) {
$notices[] = $notice; $notices[] = $notice;
}
set_transient('lbry_notices', $notices); set_transient('lbry_notices', $notices);
} }
} }
@ -56,6 +58,15 @@ class LBRY_Admin_Notice
if ($notice['is_dismissible']) { if ($notice['is_dismissible']) {
$class .= ' is-dismissible'; $class .= ' is-dismissible';
} }
printf('<div class="%1$s"><p>%2$s</p></div>', esc_attr($class), esc_html($notice['message'])); ob_start();
?>
<div class="<?= $class ?>">
<p>
<span style="font-weight:bold">LBRYPress: </span>
<?= $notice['message'] ?>
</p>
</div>
<?php
echo ob_get_clean();
} }
} }

View file

@ -13,6 +13,16 @@ class LBRY_Daemon
*/ */
private $address = 'localhost:5279'; private $address = 'localhost:5279';
/**
* The Daemon's Status
*/
private $daemon_running = false;
/**
* The Daemon Notice Handler
*/
private $notice = null;
/** /**
* The Daemon Logger * The Daemon Logger
* @var LBRY_Daemon_Logger * @var LBRY_Daemon_Logger
@ -25,6 +35,82 @@ class LBRY_Daemon
public function __construct() public function __construct()
{ {
$this->logger = new LBRY_Daemon_Logger(); $this->logger = new LBRY_Daemon_Logger();
$this->notice = new LBRY_Admin_Notice();
$this->daemon_running = $this->test_daemon();
if (!$this->daemon_running) {
$this->start_daemon();
$this->notice->set_notice('error', 'Cannot connect to the LBRY Daemon. Attempting to start daemon server. <br /> If you are still having troubles, click <a href="' . admin_url('admin.php?page=lbrypress-help') . '">HERE</a> for help.');
}
}
/**
* Returns a boolean representation of daemon status
* @return bool Whether or not the daemon is running
*/
private function test_daemon()
{
try {
$result = $this->request('status')->result;
return $result->is_running;
} catch (LBRYDaemonException $e) {
$this->logger->log('daemon_status_error', $e->getMessage() . ' | Code: ' . $e->getCode());
return false;
}
}
/**
* Attempts to start the daemon
*/
private function start_daemon()
{
// Check if a daemon start process is already running
$options = get_option(LBRY_SETTINGS);
if ($options[LBRY_DAEMON_PID]) {
if ($this->is_process_running($options[LBRY_DAEMON_PID])) {
return;
}
$options[LBRY_DAEMON_PID] = false;
update_option(LBRY_SETTINGS, $options);
}
// Using proc_open to set up the request
// Again, this is unix only
$cmd = ABSPATH . "lbrynet start";
$command = $cmd . ' > /dev/null 2> /dev/null & echo $!;';
$pid = exec($command);
if ($pid) {
$options = get_option(LBRY_SETTINGS);
$options[LBRY_DAEMON_PID] = $pid;
update_option(LBRY_SETTINGS, $options);
}
}
/**
* Attempts to stop the daemon
*/
private function stop_daemon()
{
exec(ABSPATH . 'lbrynet stop > /dev/null 2> /dev/null &');
}
/**
* Checks to see if a PID is currently running
* @param int $pid
* @return boolean
*/
private function is_process_running($pid)
{
// This is unix specific
$lines_out = array();
exec('ps '.(int)$pid, $lines_out);
if(count($lines_out) >= 2) {
// Process is running
return true;
}
return false;
} }
/** /**
@ -34,12 +120,14 @@ class LBRY_Daemon
*/ */
public function wallet_unused_address() public function wallet_unused_address()
{ {
if (!$this->daemon_running) return;
try { try {
$result = $this->request('address_unused'); $result = $this->request('address_unused');
return $result->result; return $result->result;
} catch (LBRYDaemonException $e) { } catch (LBRYDaemonException $e) {
$this->logger->log('address_unused error', $e->getMessage() . ' | Code: ' . $e->getCode()); $this->logger->log('address_unused error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue getting unused address.'); $this->notice->set_notice('error', 'Issue getting unused address.');
return; return;
} }
} }
@ -52,6 +140,8 @@ class LBRY_Daemon
*/ */
public function address_list($page = 1) public function address_list($page = 1)
{ {
if (!$this->daemon_running) return;
// Get 20 per page // Get 20 per page
$params = array( $params = array(
'page' => $page, 'page' => $page,
@ -62,7 +152,7 @@ class LBRY_Daemon
return $result->result->items; return $result->result->items;
} catch (LBRYDaemonException $e) { } catch (LBRYDaemonException $e) {
$this->logger->log('address_list error', $e->getMessage() . ' | Code: ' . $e->getCode()); $this->logger->log('address_list error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue getting address list.'); $this->notice->set_notice('error', 'Issue getting address list.');
return; return;
} }
} }
@ -75,12 +165,14 @@ class LBRY_Daemon
*/ */
public function wallet_balance() public function wallet_balance()
{ {
if (!$this->daemon_running) return;
try { try {
$result = $this->request('account_balance'); $result = $this->request('account_balance');
return $result->result->available; return $result->result->available;
} catch (LBRYDaemonException $e) { } catch (LBRYDaemonException $e) {
$this->logger->log('account_balance error', $e->getMessage() . ' | Code: ' . $e->getCode()); $this->logger->log('account_balance error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue getting account balance.'); $this->notice->set_notice('error', 'Issue getting account balance.');
return; return;
} }
} }
@ -93,6 +185,8 @@ class LBRY_Daemon
*/ */
public function channel_list($page = 1) public function channel_list($page = 1)
{ {
if (!$this->daemon_running) return;
$params = array( $params = array(
'page' => $page, 'page' => $page,
'page_size' => 20 'page_size' => 20
@ -103,7 +197,7 @@ class LBRY_Daemon
return empty($result) ? null : $result; return empty($result) ? null : $result;
} catch (LBRYDaemonException $e) { } catch (LBRYDaemonException $e) {
$this->logger->log('channel_list error', $e->getMessage() . ' | Code: ' . $e->getCode()); $this->logger->log('channel_list error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue retrieving channel list.'); $this->notice->set_notice('error', 'Issue retrieving channel list.');
return; return;
} }
} }
@ -115,6 +209,8 @@ class LBRY_Daemon
*/ */
public function channel_new($channel_name, $bid_amount) public function channel_new($channel_name, $bid_amount)
{ {
if (!$this->daemon_running) return;
// TODO: Sanitize channel name and bid // TODO: Sanitize channel name and bid
// Make sure no @ sign, as we will add that // Make sure no @ sign, as we will add that
if (strpos($channel_name, '@')) { if (strpos($channel_name, '@')) {
@ -151,6 +247,8 @@ class LBRY_Daemon
*/ */
public function canonical_url($claim_id = null) public function canonical_url($claim_id = null)
{ {
if (!$this->daemon_running) return;
if (!$claim_id) { if (!$claim_id) {
return null; return null;
} }
@ -188,6 +286,8 @@ class LBRY_Daemon
*/ */
public function publish($args) public function publish($args)
{ {
if (!$this->daemon_running) return;
try { try {
$result = $this->request( $result = $this->request(
'publish', 'publish',
@ -197,7 +297,7 @@ class LBRY_Daemon
return $result->result; return $result->result;
} catch (LBRYDaemonException $e) { } catch (LBRYDaemonException $e) {
$this->logger->log('publish error', $e->getMessage() . ' | Code: ' . $e->getCode()); $this->logger->log('publish error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue publishing / updating post to LBRY Network.'); $this->notice->set_notice('error', 'Issue publishing / updating post to LBRY Network.');
return; return;
} }
} }

17
templates/help_page.php Normal file
View file

@ -0,0 +1,17 @@
<div class="wrap">
<h1><?= esc_html(get_admin_page_title()); ?></h1>
<h2>Installation</h2>
<p>The current supported release <a href="https://github.com/lbryio/lbry-sdk/releases/tag/v0.54.0" target="_blank">can be found here</a>. It contains pre-built binaries for macOS, Debian-based Linux, and Windows.<br>Simply download the proper daemon for your server's OS, extract it, and copy it to your WordPress installations root directory.</p>
<p>For ease of use, our plugin will automatically try to run and start the daemon if installed at the root of your Wordpress install, so its advised you keep it there.</p>
<p>If you want to have your daemon running at a location other than your Wordpress root, feel free to set up a CRON Job on your server that will start the daemon if its not already running.</p>
<h2>Usage</h2>
<p>By default, <code>lbrynet</code> will provide a JSON-RPC server at <code>http://localhost:5279</code>. This is the address our plugin will be expecting to use.</p>
<p>If curious, The full API is documented <a href="https://lbry.tech/api/sdk" target="_blank">here</a></p>
<p><a href="https://github.com/lbryio/lbry-sdk/blob/master/README.md" target="_blank">SDK Github</a></p>
<p><a href="https://lbry.com/" target="_blank">LBRY Home Page</a></p>
</div>