From 05a4709b07d6ad80bdd6472cce852da5496ff686 Mon Sep 17 00:00:00 2001 From: Paul Kirby Date: Tue, 11 Sep 2018 15:10:15 -0500 Subject: [PATCH] Moved to a more rigid class architecture --- classes/LBRY_Admin.php | 15 ++-- classes/LBRY_Daemon.php | 14 +-- classes/LBRY_Daemon_Logger.php | 13 +-- classes/LBRY_Network.php | 13 +-- classes/LBRY_Network_Parser.php | 13 +-- classes/LBRY_Network_Publisher.php | 13 +-- classes/LBRY_Notifier.php | 13 +-- classes/LBRY_Speech.php | 13 +-- classes/LBRY_Speech_Parser.php | 13 +-- classes/lbry_post_handler.php | 13 +-- classes/lbrypress.php | 132 ++++++++++++++++++++++++----- lbrypress.php | 62 ++++---------- templates/options_page.php | 7 +- 13 files changed, 170 insertions(+), 164 deletions(-) diff --git a/classes/LBRY_Admin.php b/classes/LBRY_Admin.php index 4de7f3b..37f3dbd 100644 --- a/classes/LBRY_Admin.php +++ b/classes/LBRY_Admin.php @@ -7,16 +7,11 @@ class LBRY_Admin { - private static $instance = null; - - public static function get_instance() + /** + * [__construct description] + */ + public function __construct() { - // Create the object - if (self::$instance === null) { - self::$instance = new self; - } - - return self::$instance; } /** @@ -49,6 +44,6 @@ class LBRY_Admin */ public function options_page_html() { - require_once(LBRY_URI . '/templates/options_page.php'); + require_once(LBRY_ABSPATH . 'templates/options_page.php'); } } diff --git a/classes/LBRY_Daemon.php b/classes/LBRY_Daemon.php index 16a74e9..3ab8303 100644 --- a/classes/LBRY_Daemon.php +++ b/classes/LBRY_Daemon.php @@ -7,17 +7,13 @@ class LBRY_Daemon { - private static $instance = null; private $address = 'localhost:5279'; - public static function get_instance() + /** + * LBRY Daemon Object constructor + */ + public function __construct() { - // Create the object - if (self::$instance === null) { - self::$instance = new self; - } - - return self::$instance; } /** @@ -43,8 +39,6 @@ class LBRY_Daemon 'include_unconfirmed' => false )); - error_log(print_r($result, true)); - return json_decode($result)->result; } diff --git a/classes/LBRY_Daemon_Logger.php b/classes/LBRY_Daemon_Logger.php index 96a6a2b..356589b 100644 --- a/classes/LBRY_Daemon_Logger.php +++ b/classes/LBRY_Daemon_Logger.php @@ -7,15 +7,10 @@ class LBRY_Daemon_Logger { - private static $instance = null; - - public static function get_instance() + /** + * [__construct description] + */ + public function __construct() { - // Create the object - if (self::$instance === null) { - self::$instance = new self; - } - - return self::$instance; } } diff --git a/classes/LBRY_Network.php b/classes/LBRY_Network.php index d0a665f..e571557 100644 --- a/classes/LBRY_Network.php +++ b/classes/LBRY_Network.php @@ -7,15 +7,10 @@ class LBRY_Network { - private static $instance = null; - - public static function get_instance() + /** + * [__construct description] + */ + public function __construct() { - // Create the object - if (self::$instance === null) { - self::$instance = new self; - } - - return self::$instance; } } diff --git a/classes/LBRY_Network_Parser.php b/classes/LBRY_Network_Parser.php index 5f32712..cc06b84 100644 --- a/classes/LBRY_Network_Parser.php +++ b/classes/LBRY_Network_Parser.php @@ -7,15 +7,10 @@ class LBRY_Network_Parser { - private static $instance = null; - - public static function get_instance() + /** + * [__construct description] + */ + public function __construct() { - // Create the object - if (self::$instance === null) { - self::$instance = new self; - } - - return self::$instance; } } diff --git a/classes/LBRY_Network_Publisher.php b/classes/LBRY_Network_Publisher.php index 0630c67..76b9370 100644 --- a/classes/LBRY_Network_Publisher.php +++ b/classes/LBRY_Network_Publisher.php @@ -7,15 +7,10 @@ class LBRY_Network_Publisher { - private static $instance = null; - - public static function get_instance() + /** + * [__construct description] + */ + public function __construct() { - // Create the object - if (self::$instance === null) { - self::$instance = new self; - } - - return self::$instance; } } diff --git a/classes/LBRY_Notifier.php b/classes/LBRY_Notifier.php index fd16e40..c373442 100644 --- a/classes/LBRY_Notifier.php +++ b/classes/LBRY_Notifier.php @@ -7,15 +7,10 @@ class LBRY_Notifier { - private static $instance = null; - - public static function get_instance() + /** + * [__construct description] + */ + public function __construct() { - // Create the object - if (self::$instance === null) { - self::$instance = new self; - } - - return self::$instance; } } diff --git a/classes/LBRY_Speech.php b/classes/LBRY_Speech.php index 3b5713b..deb2528 100644 --- a/classes/LBRY_Speech.php +++ b/classes/LBRY_Speech.php @@ -9,16 +9,11 @@ class LBRY_Speech { - private static $instance = null; - - public static function get_instance() + /** + * [__construct description] + */ + public function __construct() { - // Create the object - if (self::$instance === null) { - self::$instance = new self; - } - - return self::$instance; } public function get_address() diff --git a/classes/LBRY_Speech_Parser.php b/classes/LBRY_Speech_Parser.php index 05e2a8d..0ca7976 100644 --- a/classes/LBRY_Speech_Parser.php +++ b/classes/LBRY_Speech_Parser.php @@ -7,15 +7,10 @@ class LBRY_Speech_Parser { - private static $instance = null; - - public static function get_instance() + /** + * [__construct description] + */ + public function __construct() { - // Create the object - if (self::$instance === null) { - self::$instance = new self; - } - - return self::$instance; } } diff --git a/classes/lbry_post_handler.php b/classes/lbry_post_handler.php index 1e9d9d1..e5d3487 100644 --- a/classes/lbry_post_handler.php +++ b/classes/lbry_post_handler.php @@ -8,15 +8,10 @@ class LBRY_Post_Handler { - private static $instance = null; - - public static function get_instance() + /** + * [__construct description] + */ + public function __construct() { - // Create the object - if (self::$instance === null) { - self::$instance = new self; - } - - return self::$instance; } } diff --git a/classes/lbrypress.php b/classes/lbrypress.php index 15e1ece..0e1dfd9 100644 --- a/classes/lbrypress.php +++ b/classes/lbrypress.php @@ -7,35 +7,123 @@ class LBRYPress { - // TODO: Remove Singleton pattern, replace with dependency injection for all classes - // private static $instance = null; - // - // public static function get_instance() - // { - // // Create the object - // if (self::$instance === null) { - // self::$instance = new self; - // } - // - // return self::$instance; - // } - protected $daemon; - protected $admin; - public function __construct(LBRY_Daemon $daemon = null, LBRY_Admin $admin = null) + /** + * Version of LBRYPress + */ + public $version = '0.0.1'; + + /** + * The single instance of this class + */ + protected static $_instance = null; + + /** + * The LBRY Daemon Interface + */ + public $daemon = null; + + /** + * The LBRY Admin object + */ + public $admin = null; + + /** + * The LBRY Spee.ch object + */ + public $speech = null; + + /** + * Main LBRYPress Instance. + * + * Ensures only one instance of LBRYPress is loaded or can be loaded. + */ + public static function instance() { - $this->daemon = $daemon ?? new LBRY_Daemon(); - $this->admin = $admin ?? new LBRY_Admin(); - error_log('new LBRYPress constructed'); + if (is_null(self::$_instance)) { + self::$_instance = new self(); + } + return self::$_instance; + } + + + /** + * LBRYPress Constructor + */ + public function __construct() + { + $this->define_constants(); + spl_autoload_register(array($this, 'lbry_autoload_register')); + $this->init(); + $this->init_hooks(); + } + + /** + * Define a constant if its not already set. + * + * @param string $name Constant name. + * @param string|bool $value Constant value. + */ + private function define($name, $value) + { + if (! defined($name)) { + define($name, $value); + } + } + + /** + * Define all LBRYPress constants + */ + private function define_constants() + { + $upload_dir = wp_upload_dir(null, false); + + $this->define('LBRY_ABSPATH', dirname(LBRY_PLUGIN_FILE) . '/'); + $this->define('LBRY_NAME', plugin_basename(LBRY_PLUGIN_FILE)); + $this->define('LBRY_VERSION', $this->version); + + // Library Options Names + $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 + } + + /** + * Autoloader Registration + */ + private function lbry_autoload_register($class) + { + $file_name = LBRY_ABSPATH . 'classes/' . $class . '.php'; + + if (file_exists($file_name)) { + require $file_name; + return; + } + } + + /** + * Initialize this class itself + */ + private function init() + { + $this->daemon = new LBRY_Daemon(); + $this->admin = new LBRY_Admin(); + $this->speech = new LBRY_Speech(); } /** * Set up all hooks and actions necessary for the plugin to run */ - public function init() + private function init_hooks() { - // Initialize the admin interface - $this->admin->settings_init(); + register_activation_hook(LBRY_PLUGIN_FILE, array($this, 'activate')); + register_deactivation_hook(LBRY_PLUGIN_FILE, array($this, 'deactivate')); + + // Admin request + if (is_admin()) { + $this->admin->settings_init(); + } } /** @@ -43,8 +131,6 @@ class LBRYPress */ public function activate() { - $LBRY_Daemon = LBRY_Daemon::get_instance(); - // Add options to the options table we need if (! get_option(LBRY_WALLET)) { $wallet_address = $this->daemon->wallet_unused_address(); diff --git a/lbrypress.php b/lbrypress.php index 8b6f1d3..65d046d 100644 --- a/lbrypress.php +++ b/lbrypress.php @@ -23,19 +23,11 @@ The above copyright notice and this permission notice shall be included in all c 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(); // Exit if accessed directly -// Global Constants -define('LBRY_NAME', 'LBRYPress'); -define('LBRY_URI', dirname(__FILE__)); define('LBRY_REQUIRED_PHP_VERSION', '5.3'); // TODO: Figure out what versions we actually need define('LBRY_REQUIRED_WP_VERSION', '3.1'); - -// Library Options Names -define('LBRY_WALLET', 'lbry_wallet'); // the wallet address -define('LBRY_SPEECH', 'lbry_speech'); // the spee.ch address -define('LBRY_LICENSE', 'lbry_license'); // the license to publish with to the LBRY network -define('LBRY_LBC_PUBLISH', 'lbry_lbc_publish'); // amount of lbc to use per publish +define('LBRY_PLUGIN_FILE', __FILE__); /** * Checks if the system requirements are met @@ -65,45 +57,25 @@ function lbry_requirements_error() { global $wp_version; - require_once(LBRY_URI . '/templates/requirements-error.php'); + require_once(dirname(__FILE__) . '/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; +/** + * Returns the singal instance of LBRYPress + */ +function LBRY() +{ + if (lbry_requirements_met()) { + if (! class_exists('LBRYPress')) { + require_once(dirname(__FILE__) . '/classes/LBRYPress.php'); + } + return LBRYPress::instance(); + } else { + add_action('admin_notices', 'lbry_requirements_error'); return; } } -spl_autoload_register('lbry_autoload_register'); - -/** - * 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. - */ -if (lbry_requirements_met()) { - add_action('init', function () { - $LBRYPress = new LBRYPress(); - $LBRYPress->init(); - }); - -// register_activation_hook(__FILE__, function () { - // LBRYPress::get_instance()->activate(); - // }); - // - // register_deactivation_hook(__FILE__, function () { - // LBRYPress::get_instance()->deactivate(); - // }); -} else { - add_action('admin_notices', 'lbry_requirements_error'); -} - -/** - * Admin Set up - */ +// Kickoff +LBRY(); diff --git a/templates/options_page.php b/templates/options_page.php index 4a9754a..58db3a1 100644 --- a/templates/options_page.php +++ b/templates/options_page.php @@ -1,8 +1,7 @@ wallet_balance(); -$speech_address = $LBRY_Speech->get_address() || ''; +$LBRY = LBRY(); +$wallet_balance = $LBRY->daemon->wallet_balance(); +$speech_address = $LBRY->speech->get_address() || ''; ?>