Setup initialization sequence, autoloader, and basic Daemon request

This commit is contained in:
Paul Kirby 2018-08-31 18:50:29 -05:00
parent c50904c49c
commit 9f81309dee
21 changed files with 390 additions and 189 deletions

54
classes/LBRY_Admin.php Normal file
View file

@ -0,0 +1,54 @@
<?php
/**
* Class for intializing and displaying all admin settings
*
* @package LBRYPress
*/
class LBRY_Admin
{
private static $instance = null;
public static function get_instance()
{
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Called to initialize the settings interface
* @return [type] [description]
*/
public function settings_init()
{
add_action('admin_menu', array($this, 'create_options_page'));
}
/**
* Creates the options page in the WP admin interface
* @return [type] [description]
*/
public function create_options_page()
{
add_options_page(
__('LBRYPress Settings', 'lbrypress'),
__('LBRYPress', 'lbrypress'),
'manage_options',
'LBRYPress',
array($this, 'options_page_html')
);
}
/**
* Returns the Options Page HTML for the plugin
* @return [type] [description]
*/
public function options_page_html()
{
require_once(LBRY_URI . '/templates/options_page.php');
}
}

101
classes/LBRY_Daemon.php Normal file
View file

@ -0,0 +1,101 @@
<?php
/**
* Main class for the Daemon setup
*
* @package LBRYPress
*/
class LBRY_Daemon
{
private static $instance = null;
private $address = 'localhost:5279';
public static function get_instance()
{
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Returns an unused wallet address
* @return string Unused wallet address in base58
*/
public function wallet_unused_address()
{
$result = json_decode($this->request('wallet_unused_address'));
return $result->result;
}
/**
* Returns the balance of a current LBRY wallet
* @param string $address Wallet Address
* @return float Wallet Balance
*/
public function wallet_balance($address)
{
return $this->request('wallet_balance', array(
'address' => $address,
'include_unconfirmed' => false
));
}
private function request($method, $params = array())
{
// JSONify our request data
$data = array(
'method' => $method,
'params' => $params
);
$data = json_encode($data);
// Send it via curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->address);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
/**
* Temporary placeholder function for daemon. Not currently in use.
* @return [type] [description]
*/
private function download_daemon()
{
$output_filename = "lbrydaemon";
$host = "http://build.lbry.io/daemon/build-6788_commit-5099e19_branch-lbryum-refactor/mac/lbrynet";
$fp = fopen(LBRY_URI . '/' . $output_filename, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
$filepath = LBRY_URI . '/' . $output_filename;
`chmod +x {$filepath}`;
error_log(`{$filepath} status`);
`{$filepath} start &`;
}
}

View file

@ -0,0 +1,21 @@
<?php
/**
* A class for logging LBRY Daemon interactions
*
* @package LBRYPress
*/
class LBRY_Daemon_Logger
{
private static $instance = null;
public static function get_instance()
{
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
}

21
classes/LBRY_Network.php Normal file
View file

@ -0,0 +1,21 @@
<?php
/**
* Class for connecting with the LBRY Network
*
* @package LBRYPress
*/
class LBRY_Network
{
private static $instance = null;
public static function get_instance()
{
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
}

View file

@ -0,0 +1,21 @@
<?php
/**
* Parses wordpress posts to be ready for the LBRY Network
*
* @package LBRYPress
*/
class LBRY_Network_Parser
{
private static $instance = null;
public static function get_instance()
{
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
}

View file

@ -0,0 +1,21 @@
<?php
/**
* Class for publishing to the LBRY Network
*
* @package LBRYPress
*/
class LBRY_Network_Publisher
{
private static $instance = null;
public static function get_instance()
{
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
}

21
classes/LBRY_Notifier.php Normal file
View file

@ -0,0 +1,21 @@
<?php
/**
* Class for sending LBRYPress related admin notifications
*
* @package LBRYPress
*/
class LBRY_Notifier
{
private static $instance = null;
public static function get_instance()
{
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
}

23
classes/LBRY_Speech.php Normal file
View file

@ -0,0 +1,23 @@
<?php
/**
* Connects to an spee.ch style server to host assets via the LBRY Protocol
*
* Visit https://github.com/lbryio/spee.ch for more info
*
* @package LBRYPress
*/
class LBRY_Speech_Parser
{
private static $instance = null;
public static function get_instance()
{
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
}

View file

@ -0,0 +1,21 @@
<?php
/**
* Parses post markup in order to use specified spee.ch url for assets
*
* @package LBRYPress
*/
class LBRY_Speech_Parser
{
private static $instance = null;
public static function get_instance()
{
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
}
}

View file

@ -1,32 +0,0 @@
<?php
/**
* Class for intializing and displaying all admin settings
*
* @package LBRYPress
*/
if (! class_exists('LBRY_Admin')) {
class LBRY_Admin
{
public function settings_init()
{
add_action('admin_menu', array($this, 'create_options_page'));
}
public function create_options_page()
{
add_options_page(
__('LBRYPress Settings', 'lbrypress'),
__('LBRYPress', 'lbrypress'),
'manage_options',
'LBRYPress',
array($this, 'options_page_html')
);
}
public function options_page_html()
{
require_once(LBRY_URI . '/templates/options_page.php');
}
}
}

View file

@ -1,12 +0,0 @@
<?php
/**
* Class for sending LBRYPress related admin notifications
*
* @package LBRYPress
*/
if (! class_exists('LBRY_Notifier')) {
class LBRY_Notifier
{
}
}

View file

@ -1,12 +0,0 @@
<?php
/**
* Main class for the Daemon setup
*
* @package LBRYPress
*/
if (! class_exists('LBRY_Daemon')) {
class LBRY_Daemon
{
}
}

View file

@ -1,12 +0,0 @@
<?php
/**
* A class for logging LBRY Daemon interactions
*
* @package LBRYPress
*/
if (! class_exists('LBRY_Daemon_Logger')) {
class LBRY_Daemon_Logger
{
}
}

View file

@ -1,12 +0,0 @@
<?php
/**
* Class for connecting with the LBRY Network
*
* @package LBRYPress
*/
if (! class_exists('LBRY_Network')) {
class LBRY_Network
{
}
}

View file

@ -1,12 +0,0 @@
<?php
/**
* Parses wordpress posts to be ready for the LBRY Network
*
* @package LBRYPress
*/
if (! class_exists('LBRY_Network_Parser')) {
class LBRY_Network_Parser
{
}
}

View file

@ -1,12 +0,0 @@
<?php
/**
* Class for publishing to the LBRY Network
*
* @package LBRYPress
*/
if (! class_exists('LBRY_Network_Publisher')) {
class LBRY_Network_Publisher
{
}
}

View file

@ -6,8 +6,17 @@
* @package LBRYPress * @package LBRYPress
*/ */
if (! class_exists('LBRY_Post_Handler')) { class LBRY_Post_Handler
class LBRY_Post_Handler {
private static $instance = null;
public static function get_instance()
{ {
// Create the object
if (self::$instance === null) {
self::$instance = new self;
}
return self::$instance;
} }
} }

View file

@ -5,65 +5,57 @@
* @package LBRYPress * @package LBRYPress
*/ */
if (! class_exists('LBRYPress')) { class LBRYPress
class LBRYPress {
private static $instance = null;
public static function get_instance()
{ {
// Employ Singleton pattern to preserve single instance // Create the object
private static $instance = null; if (self::$instance === null) {
self::$instance = new self;
public static function get_instance()
{
if (null == self::$instance) {
self::$instance = new self;
}
return self::$instance;
} }
// Create instances of all necessary classes return self::$instance;
private $LBRY_Admin; }
private function __construct() /**
{ * Set up all hooks and actions necessary for the plugin to run
$this->require_dependencies(); * @return [type] [description]
$this->LBRY_Admin = new LBRY_Admin(); */
public function init()
{
// Initialize the admin interface
$LBRY_Admin = LBRY_Admin::get_instance();
$LBRY_Admin->settings_init();
$LBRY_Daemon = LBRY_Daemon::get_instance();
}
$this->LBRY_Admin->settings_init(); /**
* Run during plugin activation
* @return [type] [description]
*/
public function activate()
{
$LBRY_Daemon = LBRY_Daemon::get_instance();
$this->download_daemon(); // Add options to the options table we need
if (! get_option(LBRY_WALLET)) {
$wallet_address = $LBRY_Daemon->wallet_unused_address();
add_option(LBRY_WALLET, $wallet_address);
} }
private function require_dependencies()
{
require_once(LBRY_URI . '/classes/admin/lbry_admin.php');
}
private function download_daemon() error_log('Activated');
{ }
$output_filename = "lbrydaemon";
$host = "http://build.lbry.io/daemon/build-6788_commit-5099e19_branch-lbryum-refactor/mac/lbrynet"; /**
$fp = fopen(LBRY_URI . '/' . $output_filename, 'w+'); * Clean up on deactivation
$ch = curl_init(); * @return [type] [description]
curl_setopt($ch, CURLOPT_URL, $host); */
curl_setopt($ch, CURLOPT_VERBOSE, 1); public function deactivate()
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); {
curl_setopt($ch, CURLOPT_FILE, $fp); error_log('Deactivated');
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
$filepath = LBRY_URI . '/' . $output_filename;
`chmod +x {$filepath}`;
error_log(`{$filepath} status`);
`{$filepath} start &`;
}
} }
} }

View file

@ -1,14 +0,0 @@
<?php
/**
* Connects to an spee.ch style server to host assets via the LBRY Protocol
*
* Visit https://github.com/lbryio/spee.ch for more info
*
* @package LBRYPress
*/
if (! class_exists('LBRY_Speech')) {
class LBRY_Speech_Parser
{
}
}

View file

@ -1,12 +0,0 @@
<?php
/**
* Parses post markup in order to use specified spee.ch url for assets
*
* @package LBRYPress
*/
if (! class_exists('LBRY_Speech_Parser')) {
class LBRY_Speech_Parser
{
}
}

View file

@ -21,7 +21,7 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
defined('ABSPATH') || die('No Peeking!'); defined('ABSPATH') || die('No Peeking!');
@ -31,6 +31,9 @@ define('LBRY_URI', dirname(__FILE__));
define('LBRY_REQUIRED_PHP_VERSION', '5.3'); // TODO: Figure out what versions we actually need define('LBRY_REQUIRED_PHP_VERSION', '5.3'); // TODO: Figure out what versions we actually need
define('LBRY_REQUIRED_WP_VERSION', '3.1'); define('LBRY_REQUIRED_WP_VERSION', '3.1');
// Library Options Names
define('LBRY_WALLET', 'lbry_wallet');
/** /**
* Checks if the system requirements are met * Checks if the system requirements are met
* *
@ -62,18 +65,41 @@ function lbry_requirements_error()
require_once(LBRY_URI . '/templates/requirements-error.php'); require_once(LBRY_URI . '/templates/requirements-error.php');
} }
/* /**
* Autoloader Registration
*/
function lbry_autoload_register($class)
{
$file_name = LBRY_URI . '/classes/' . $class . '.php';
if (file_exists($file_name)) {
require $file_name;
return;
}
}
spl_autoload_register('lbry_autoload_register');
/**
* Check requirements and load main class * Check requirements and load main class
* The main program needs to be in a separate file that only gets loaded if the plugin requirements are met. Otherwise older PHP installations could crash when trying to parse it. * The main program needs to be in a separate file that only gets loaded if the plugin requirements are met. Otherwise older PHP installations could crash when trying to parse it.
*/ */
if (lbry_requirements_met()) { if (lbry_requirements_met()) {
require_once(dirname(__FILE__) . '/classes/lbrypress.php'); add_action('init', function () {
LBRYPress::get_instance()->init();
});
if (class_exists('LBRYPress')) { register_activation_hook(__FILE__, function () {
$lbryPress = LBRYPress::get_instance(); LBRYPress::get_instance()->activate();
// register_activation_hook(__FILE__, array( $lbryPress, 'activate' )); });
// register_deactivation_hook(__FILE__, array( $lbryPress, 'deactivate' ));
} register_deactivation_hook(__FILE__, function () {
LBRYPress::get_instance()->deactivate();
});
} else { } else {
add_action('admin_notices', 'lbry_requirements_error'); add_action('admin_notices', 'lbry_requirements_error');
} }
/**
* Admin Set up
*/