diff --git a/functions.php b/functions.php index 91650b22..7e66886f 100644 --- a/functions.php +++ b/functions.php @@ -23,24 +23,18 @@ define( 'ELEMENTARY_BUILD_DIR', untrailingslashit( get_template_directory() ) . '/assets/build' ); endif; -if ( ! function_exists( 'elementary_support' ) ) : - /** - * Add required theme support. - * - * @since 1.0.0 - */ - function elementary_support() { - // Add support for core block styles. - add_theme_support( 'wp-block-styles' ); - } - -endif; - -add_action( 'after_setup_theme', 'elementary_support' ); - require_once ELEMENTARY_TEMP_DIR . '/vendor/autoload.php'; -// Add block patterns. -$elementary_block_patterns_register = new \Elementary\Patterns\Block_Patterns(); -add_action( 'init', array( $elementary_block_patterns_register, 'elementary_register_block_patterns_categories' ) ); -add_action( 'init', array( $elementary_block_patterns_register, 'elementary_register_block_patterns' ) ); +/** + * Theme bootstrap instance. + * + * @since 1.0.0 + * + * @return object Elementary + */ +function elementary_instance() { + return Elementary\Elementary::get_instance(); +} + +// Instantiate theme. +elementary_instance(); diff --git a/inc/classes/class-elementary.php b/inc/classes/class-elementary.php new file mode 100644 index 00000000..955408d6 --- /dev/null +++ b/inc/classes/class-elementary.php @@ -0,0 +1,51 @@ +setup_hooks(); + } + + /** + * Setup hooks. + * + * @since 1.0.0 + */ + public function setup_hooks() { + add_action( 'after_setup_theme', array( $this, 'elementary_support' ) ); + } + + /** + * Add required theme support. + * + * @since 1.0.0 + */ + public function elementary_support() { + // Add support for core block styles. + add_theme_support( 'wp-block-styles' ); + } +} diff --git a/inc/classes/patterns/class-block-patterns.php b/inc/classes/patterns/class-block-patterns.php index 2db8c5e2..9db5a50a 100644 --- a/inc/classes/patterns/class-block-patterns.php +++ b/inc/classes/patterns/class-block-patterns.php @@ -7,6 +7,7 @@ namespace Elementary\Patterns; +use Elementary\Traits\Singleton; use WP_Block_Pattern_Categories_Registry; use WP_Block_Patterns_Registry; @@ -17,6 +18,8 @@ */ class Block_Patterns { + use Singleton; + /** * Blocks Patterns Namespace. * @@ -31,6 +34,23 @@ class Block_Patterns { */ const BLOCK_CONTENT_NAMESPACE = 'Elementary\\Patterns\\Content\\'; + /** + * Constructor. + */ + protected function __construct() { + $this->setup_hooks(); + } + + /** + * Setup hooks. + * + * @since 1.0.0 + */ + public function setup_hooks() { + add_action( 'init', [ $this, 'elementary_register_block_patterns_categories' ] ); + add_action( 'init', [ $this, 'elementary_register_block_patterns' ] ); + } + /** * Register categories for blocks patterns. * diff --git a/inc/traits/trait-singleton.php b/inc/traits/trait-singleton.php new file mode 100644 index 00000000..d11d7c9f --- /dev/null +++ b/inc/traits/trait-singleton.php @@ -0,0 +1,89 @@ +value pair for each `classname => instance` in self::$_instance + * for each sub-class. + */ + $called_class = get_called_class(); + + if ( ! isset( $instance[ $called_class ] ) ) { + + $instance[ $called_class ] = new $called_class(); + + /** + * Dependent items can use the `blank_theme_singleton_init_{$called_class}` hook to execute code + */ + do_action( sprintf( 'blank_theme_singleton_init_%s', $called_class ) ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores, WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound + + } + + return $instance[ $called_class ]; + + } + +}