From e09b27c5710048d8bde99dbd93e7fb910a080dd0 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Mon, 11 Apr 2022 19:39:15 +0530 Subject: [PATCH 01/15] Add footer block pattern --- inc/classes/patterns/content/class-footer.php | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 inc/classes/patterns/content/class-footer.php diff --git a/inc/classes/patterns/content/class-footer.php b/inc/classes/patterns/content/class-footer.php new file mode 100644 index 00000000..8f8a2719 --- /dev/null +++ b/inc/classes/patterns/content/class-footer.php @@ -0,0 +1,61 @@ + __( 'Footer', 'elementary' ), + 'categories' => array( 'footer' ), + 'blockTypes' => array( 'core/template-part/footer' ), + 'content' => $this->block_pattern_content(), + ); + } + + /** + * Footer Block Pattern content. + * + * @return string Block pattern content. + */ + public function block_pattern_content() { + ob_start(); + ?> + +
+ +
+ + +

+ WordPress' + ); + ?> +

+
+
+ Date: Mon, 11 Apr 2022 19:42:38 +0530 Subject: [PATCH 02/15] Add hidden-404 block pattern --- .../patterns/content/class-hidden-404.php | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 inc/classes/patterns/content/class-hidden-404.php diff --git a/inc/classes/patterns/content/class-hidden-404.php b/inc/classes/patterns/content/class-hidden-404.php new file mode 100644 index 00000000..867182ef --- /dev/null +++ b/inc/classes/patterns/content/class-hidden-404.php @@ -0,0 +1,58 @@ + __( '404 content', 'elementary' ), + 'inserter' => false, + 'content' => $this->block_pattern_content(), + ); + } + + /** + * 404 Block Pattern content. + * + * @return string Block pattern content. + */ + public function block_pattern_content() { + ob_start(); + ?> + +

+ + +

+ + + + + + + + + + + + Date: Mon, 11 Apr 2022 19:43:30 +0530 Subject: [PATCH 03/15] Add base class for block pattern classes --- .../patterns/class-block-pattern-base.php | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 inc/classes/patterns/class-block-pattern-base.php diff --git a/inc/classes/patterns/class-block-pattern-base.php b/inc/classes/patterns/class-block-pattern-base.php new file mode 100644 index 00000000..a2bb79d2 --- /dev/null +++ b/inc/classes/patterns/class-block-pattern-base.php @@ -0,0 +1,30 @@ + Date: Mon, 11 Apr 2022 19:44:20 +0530 Subject: [PATCH 04/15] Add register block pattern/pattern-categories --- inc/classes/patterns/class-block-patterns.php | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 inc/classes/patterns/class-block-patterns.php diff --git a/inc/classes/patterns/class-block-patterns.php b/inc/classes/patterns/class-block-patterns.php new file mode 100644 index 00000000..2db8c5e2 --- /dev/null +++ b/inc/classes/patterns/class-block-patterns.php @@ -0,0 +1,98 @@ + array( + 'label' => __( 'Featured', 'elementary' ), + ), + 'footer' => array( + 'label' => __( 'Footer', 'elementary' ), + ), + 'query' => array( + 'label' => __( 'Query', 'elementary' ), + ), + ); + + /** + * Filters the block pattern categories. + * + * @since 1.0.0 + * + * @param array $block_pattern_categories Array of block pattern categories. + */ + $block_pattern_categories = apply_filters( 'elementary_block_patterns_categories', $block_pattern_categories ); + + foreach ( $block_pattern_categories as $name => $properties ) { + if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( $name ) ) { + register_block_pattern_category( $name, $properties ); + } + } + } + + /** + * Register block patterns. + * + * @since 1.0.0 + */ + public function elementary_register_block_patterns() { + $block_patterns_classes = array( + 'footer' => 'Footer', + 'hidden-404' => 'Hidden_404', + ); + + /** + * Filters the theme block patterns. + * + * @since 1.0.0 + * + * @param array $block_patterns The theme block patterns. + */ + $block_patterns = apply_filters( 'elementary_block_patterns', $block_patterns_classes ); + + foreach ( $block_patterns as $name => $class ) { + if ( ! WP_Block_Patterns_Registry::get_instance()->is_registered( $name ) ) { + $class = self::BLOCK_CONTENT_NAMESPACE . $class; + register_block_pattern( + self::PATTERN_NAMESPACE . '/' . $name, + ( new $class() )->block_pattern() + ); + } + } + } +} From fda96b8aac61c8eca738d0c373f961a557d82e80 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Mon, 11 Apr 2022 19:44:56 +0530 Subject: [PATCH 05/15] Add file to define helper functions inside theme --- inc/helpers/custom-functions.php | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 inc/helpers/custom-functions.php diff --git a/inc/helpers/custom-functions.php b/inc/helpers/custom-functions.php new file mode 100644 index 00000000..701cb0db --- /dev/null +++ b/inc/helpers/custom-functions.php @@ -0,0 +1,8 @@ + Date: Mon, 11 Apr 2022 19:45:26 +0530 Subject: [PATCH 06/15] Add theme functions.php file --- functions.php | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 functions.php diff --git a/functions.php b/functions.php new file mode 100644 index 00000000..91650b22 --- /dev/null +++ b/functions.php @@ -0,0 +1,46 @@ +get( 'Version' ) ); +endif; + +if ( ! defined( 'ELEMENTARY_TEMP_DIR' ) ) : + define( 'ELEMENTARY_TEMP_DIR', untrailingslashit( get_template_directory() ) ); +endif; + +if ( ! defined( 'ELEMENTARY_BUILD_URI' ) ) : + define( 'ELEMENTARY_BUILD_URI', untrailingslashit( get_template_directory_uri() ) . '/assets/build' ); +endif; + +if ( ! defined( 'ELEMENTARY_BUILD_DIR' ) ) : + 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' ) ); From 7d46f80606a82a0176fb146baae2bc06b07e880d Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Mon, 11 Apr 2022 19:46:07 +0530 Subject: [PATCH 07/15] Add autoloading of classes inside inc dir --- composer.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/composer.json b/composer.json index 86ae44db..4dc4b5cd 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,14 @@ "dealerdirect/phpcodesniffer-composer-installer": true } }, + "autoload": { + "classmap": [ + "inc/" + ], + "files": [ + "inc/helpers/custom-functions.php" + ] + }, "autoload-dev": { "psr-4": { "Elementary\\Tests\\": "tests/php/" From 93252f18c5206563eeaa3eff81fcf38f8d3ace33 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Tue, 12 Apr 2022 16:18:00 +0530 Subject: [PATCH 08/15] Add singleton trait --- inc/traits/trait-singleton.php | 89 ++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 inc/traits/trait-singleton.php 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 ]; + + } + +} From c1d69c3bde90c7cdc3ef02611201e284fd9db1f0 Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Tue, 12 Apr 2022 16:19:07 +0530 Subject: [PATCH 09/15] Add theme bootstrap file --- inc/classes/class-elementary.php | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 inc/classes/class-elementary.php 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' ); + } +} From f0618941b47192c1a110aac6cf9d72a021ea30dd Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Tue, 12 Apr 2022 16:20:34 +0530 Subject: [PATCH 10/15] Add singleton trait to get object instance of class --- inc/classes/patterns/class-block-patterns.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) 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. * From 764867df339758c5686f6249c064df1c26cd8fda Mon Sep 17 00:00:00 2001 From: thelovekesh Date: Tue, 12 Apr 2022 16:21:35 +0530 Subject: [PATCH 11/15] Remove classes instances and add theme bootstrap class instance --- functions.php | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) 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(); From 31b10e725b2937e910635532a9afa4892ef04c5d Mon Sep 17 00:00:00 2001 From: RahiDroid Date: Mon, 18 Apr 2022 14:48:55 +0530 Subject: [PATCH 12/15] Doc: More appropriate comment for instantiation --- inc/classes/class-elementary.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/classes/class-elementary.php b/inc/classes/class-elementary.php index 955408d6..0f95ddc8 100644 --- a/inc/classes/class-elementary.php +++ b/inc/classes/class-elementary.php @@ -23,7 +23,7 @@ class Elementary { * Constructor. */ protected function __construct() { - // Get classes instance. + // Instantiate classes. Block_Patterns::get_instance(); // Setup hooks. From 395d6904dc9546a5d0cf96035d34c92f927d27dc Mon Sep 17 00:00:00 2001 From: RahiDroid Date: Mon, 18 Apr 2022 14:54:57 +0530 Subject: [PATCH 13/15] Refact: Remove unnecessary registration checks --- inc/classes/patterns/class-block-patterns.php | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/inc/classes/patterns/class-block-patterns.php b/inc/classes/patterns/class-block-patterns.php index 9db5a50a..f5f7fc72 100644 --- a/inc/classes/patterns/class-block-patterns.php +++ b/inc/classes/patterns/class-block-patterns.php @@ -8,8 +8,6 @@ namespace Elementary\Patterns; use Elementary\Traits\Singleton; -use WP_Block_Pattern_Categories_Registry; -use WP_Block_Patterns_Registry; /** * Class Block_Patterns @@ -79,9 +77,7 @@ public function elementary_register_block_patterns_categories() { $block_pattern_categories = apply_filters( 'elementary_block_patterns_categories', $block_pattern_categories ); foreach ( $block_pattern_categories as $name => $properties ) { - if ( ! WP_Block_Pattern_Categories_Registry::get_instance()->is_registered( $name ) ) { - register_block_pattern_category( $name, $properties ); - } + register_block_pattern_category( $name, $properties ); } } @@ -106,13 +102,12 @@ public function elementary_register_block_patterns() { $block_patterns = apply_filters( 'elementary_block_patterns', $block_patterns_classes ); foreach ( $block_patterns as $name => $class ) { - if ( ! WP_Block_Patterns_Registry::get_instance()->is_registered( $name ) ) { - $class = self::BLOCK_CONTENT_NAMESPACE . $class; - register_block_pattern( - self::PATTERN_NAMESPACE . '/' . $name, - ( new $class() )->block_pattern() - ); - } + $class = self::BLOCK_CONTENT_NAMESPACE . $class; + + register_block_pattern( + self::PATTERN_NAMESPACE . '/' . $name, + ( new $class() )->block_pattern() + ); } } } From e8d26ec792e21a5d895471c909e8d565e1334537 Mon Sep 17 00:00:00 2001 From: RahiDroid Date: Mon, 18 Apr 2022 14:59:18 +0530 Subject: [PATCH 14/15] Doc: Add descriptive class comment --- inc/classes/patterns/class-block-pattern-base.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inc/classes/patterns/class-block-pattern-base.php b/inc/classes/patterns/class-block-pattern-base.php index a2bb79d2..5763c5d9 100644 --- a/inc/classes/patterns/class-block-pattern-base.php +++ b/inc/classes/patterns/class-block-pattern-base.php @@ -8,7 +8,8 @@ namespace Elementary\Patterns; /** - * Class Block_Pattern_Base + * Class Block_Pattern_Base. + * An abstract class which ensures uniform names for the content and registration functions. * * @since 1.0.0 */ From 75eefc8ac670737ea8fb77619d897a6ca317a787 Mon Sep 17 00:00:00 2001 From: RahiDroid Date: Mon, 18 Apr 2022 15:01:40 +0530 Subject: [PATCH 15/15] Fix: Incorrect package name and hook prefix --- inc/traits/trait-singleton.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/inc/traits/trait-singleton.php b/inc/traits/trait-singleton.php index d11d7c9f..24aaf157 100644 --- a/inc/traits/trait-singleton.php +++ b/inc/traits/trait-singleton.php @@ -22,7 +22,7 @@ * * If you specifically need multiple objects, then use a normal class. * - * @package Blank-Theme + * @package Elementary */ namespace Elementary\Traits; @@ -76,9 +76,9 @@ final public static function get_instance() { $instance[ $called_class ] = new $called_class(); /** - * Dependent items can use the `blank_theme_singleton_init_{$called_class}` hook to execute code + * Dependent items can use the `elementary_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 + do_action( sprintf( 'elementary_theme_singleton_init_%s', $called_class ) ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores, WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound }