这是indexloc提供的服务,不要输入任何密码
Skip to content

Fix theme bootstrap file and add singleton trait #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
51 changes: 51 additions & 0 deletions inc/classes/class-elementary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Theme bootstrap file.
*
* @package Elementary
*/

namespace Elementary;

use Elementary\Traits\Singleton;
use Elementary\Patterns\Block_Patterns;

/**
* Class Elementary
*
* @since 1.0.0
*/
class Elementary {

use Singleton;

/**
* Constructor.
*/
protected function __construct() {
// Get classes instance.
Block_Patterns::get_instance();

// Setup hooks.
$this->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' );
}
}
20 changes: 20 additions & 0 deletions inc/classes/patterns/class-block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Elementary\Patterns;

use Elementary\Traits\Singleton;
use WP_Block_Pattern_Categories_Registry;
use WP_Block_Patterns_Registry;

Expand All @@ -17,6 +18,8 @@
*/
class Block_Patterns {

use Singleton;

/**
* Blocks Patterns Namespace.
*
Expand All @@ -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.
*
Expand Down
89 changes: 89 additions & 0 deletions inc/traits/trait-singleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* Singleton trait which implements Singleton pattern in any class in which this trait is used.
*
* Using the singleton pattern in WordPress is an easy way to protect against
* mistakes caused by creating multiple objects or multiple initialization
* of classes which need to be initialized only once.
*
* With complex plugins, there are many cases where multiple copies of
* the plugin would load, and action hooks would load (and trigger) multiple
* times.
*
* If you're planning on using a global variable, then you should implement
* this trait. Singletons are a way to safely use globals; they let you
* access and set the global from anywhere, without risk of collision.
*
* If any method in a class needs to be aware of "state", then you should
* implement this trait in that class.
*
* If any method in the class need to "talk" to another or be aware of what
* another method has done, then you should implement this trait in that class.
*
* If you specifically need multiple objects, then use a normal class.
*
* @package Blank-Theme
*/

namespace Elementary\Traits;

trait Singleton {

/**
* Protected class constructor to prevent direct object creation
*
* This is meant to be overridden in the classes which implement
* this trait. This is ideal for doing stuff that you only want to
* do once, such as hooking into actions and filters, etc.
*/
protected function __construct() {
}

/**
* Prevent object cloning
*/
final protected function __clone() {
}

/**
* This method returns new or existing Singleton instance
* of the class for which it is called. This method is set
* as final intentionally, it is not meant to be overridden.
*
* @return object Singleton instance of the class.
*/
final public static function get_instance() {

/**
* Collection of instance.
*
* @var array
*/
static $instance = [];

/**
* If this trait is implemented in a class which has multiple
* sub-classes then static::$_instance will be overwritten with the most recent
* sub-class instance. Thanks to late static binding
* we use get_called_class() to grab the called class name, and store
* a key=>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 ];

}

}