From 41e054f4d1a008e34ee5292472a9987dbe749c36 Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Sun, 17 Jul 2022 16:59:20 -0700 Subject: [PATCH] Introduce singleton to prepare for Block Editor class --- eth-embed-anchor-fm.php | 2 ++ inc/class-plugin.php | 30 ++------------------------ inc/trait-singleton.php | 48 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 28 deletions(-) create mode 100644 inc/trait-singleton.php diff --git a/eth-embed-anchor-fm.php b/eth-embed-anchor-fm.php index 89de486..7f072d8 100644 --- a/eth-embed-anchor-fm.php +++ b/eth-embed-anchor-fm.php @@ -45,5 +45,7 @@ add_action( 'plugins_loaded', __NAMESPACE__ . '\action_plugins_loaded' ); /** * Load plugin classes. */ +require_once __DIR__ . '/inc/trait-singleton.php'; require_once __DIR__ . '/inc/class-plugin.php'; + Plugin::get_instance(); diff --git a/inc/class-plugin.php b/inc/class-plugin.php index ef8c6d6..b930256 100644 --- a/inc/class-plugin.php +++ b/inc/class-plugin.php @@ -11,6 +11,8 @@ namespace ETH_Embed_Anchor_FM; * Class Plugin. */ class Plugin { + use Singleton; + /** * Regex pattern to match URL to be oEmbedded. * @@ -39,34 +41,6 @@ class Plugin { */ private const SHORTCODE_TAG = 'eth_anchor_fm'; - /** - * Singleton. - * - * @var Plugin - */ - private static $_instance = null; - - /** - * Implement singleton. - * - * @return Plugin - */ - public static function get_instance(): Plugin { - if ( ! is_a( self::$_instance, __CLASS__ ) ) { - self::$_instance = new self(); - self::$_instance->_setup(); - } - - return self::$_instance; - } - - /** - * Silence is golden! - */ - private function __construct() { - // Add nothing here. - } - /** * Register hooks. * diff --git a/inc/trait-singleton.php b/inc/trait-singleton.php new file mode 100644 index 0000000..23a9cde --- /dev/null +++ b/inc/trait-singleton.php @@ -0,0 +1,48 @@ +<?php +/** + * Singleton trait. + * + * @package ETH_Embed_Anchor_FM + */ + +namespace ETH_Embed_Anchor_FM; + +/** + * Trait Singleton. + */ +trait Singleton { + /** + * Singleton. + * + * @var self + */ + private static $_instance = null; + + /** + * Implement singleton. + * + * @return self + */ + public static function get_instance(): self { + if ( ! is_a( self::$_instance, __CLASS__ ) ) { + self::$_instance = new self(); + self::$_instance->_setup(); + } + + return self::$_instance; + } + + /** + * Silence is golden! + */ + private function __construct() { + // Add nothing here. + } + + /** + * Register hooks. + * + * @return void + */ + abstract function _setup(): void; +} -- GitLab