Introduce hooks for registering patterns for email editor
To get rid of hardcoded MailPoet-specific patterns from the editor package We need to introduce an API to register the patterns and their categories. Patterns have to be defined as extenders of Abstract_Pattern. [MAILPOET-6243]
This commit is contained in:
committed by
Oluwaseun Olorunsola
parent
40ed694c02
commit
c06779acbb
@@ -6,20 +6,24 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
declare(strict_types = 1);
|
declare(strict_types = 1);
|
||||||
namespace MailPoet\EmailEditor\Engine\Patterns\Library;
|
namespace MailPoet\EmailEditor\Engine\Patterns;
|
||||||
|
|
||||||
use MailPoet\EmailEditor\Utils\Cdn_Asset_Url;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract class for block patterns.
|
* Abstract class for block patterns.
|
||||||
*/
|
*/
|
||||||
abstract class Abstract_Pattern {
|
abstract class Abstract_Pattern {
|
||||||
/**
|
/**
|
||||||
* Cdn_Asset_Url instance.
|
* Name of the pattern.
|
||||||
*
|
*
|
||||||
* @var Cdn_Asset_Url $cdn_asset_url
|
* @var string $name
|
||||||
*/
|
*/
|
||||||
protected $cdn_asset_url;
|
protected $name = '';
|
||||||
|
/**
|
||||||
|
* Namespace of the pattern.
|
||||||
|
*
|
||||||
|
* @var string $namespace
|
||||||
|
*/
|
||||||
|
protected $namespace = '';
|
||||||
/**
|
/**
|
||||||
* List of block types.
|
* List of block types.
|
||||||
*
|
*
|
||||||
@@ -58,14 +62,21 @@ abstract class Abstract_Pattern {
|
|||||||
protected $viewport_width = 620;
|
protected $viewport_width = 620;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Get name of the pattern.
|
||||||
*
|
*
|
||||||
* @param Cdn_Asset_Url $cdn_asset_url Cdn_Asset_Url instance.
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function get_name(): string {
|
||||||
Cdn_Asset_Url $cdn_asset_url
|
return $this->name;
|
||||||
) {
|
}
|
||||||
$this->cdn_asset_url = $cdn_asset_url;
|
|
||||||
|
/**
|
||||||
|
* Get namespace of the pattern.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function get_namespace(): string {
|
||||||
|
return $this->namespace;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* This file is part of the MailPoet plugin.
|
* This file is part of the MailPoet Email Editor.
|
||||||
*
|
*
|
||||||
* @package MailPoet\EmailEditor
|
* @package MailPoet\EmailEditor
|
||||||
*/
|
*/
|
||||||
@@ -8,43 +8,17 @@
|
|||||||
declare(strict_types = 1);
|
declare(strict_types = 1);
|
||||||
namespace MailPoet\EmailEditor\Engine\Patterns;
|
namespace MailPoet\EmailEditor\Engine\Patterns;
|
||||||
|
|
||||||
use MailPoet\EmailEditor\Utils\Cdn_Asset_Url;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register block patterns.
|
* Register block patterns.
|
||||||
*/
|
*/
|
||||||
class Patterns {
|
class Patterns {
|
||||||
/**
|
|
||||||
* Namespace for block patterns.
|
|
||||||
*
|
|
||||||
* @var string $namespace
|
|
||||||
*/
|
|
||||||
private $namespace = 'mailpoet';
|
|
||||||
/**
|
|
||||||
* Cdn_Asset_Url instance.
|
|
||||||
*
|
|
||||||
* @var Cdn_Asset_Url $cdn_asset_url
|
|
||||||
*/
|
|
||||||
protected $cdn_asset_url;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*
|
|
||||||
* @param Cdn_Asset_Url $cdn_asset_url Cdn_Asset_Url instance.
|
|
||||||
*/
|
|
||||||
public function __construct(
|
|
||||||
Cdn_Asset_Url $cdn_asset_url
|
|
||||||
) {
|
|
||||||
$this->cdn_asset_url = $cdn_asset_url;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize block patterns.
|
* Initialize block patterns.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function initialize(): void {
|
public function initialize(): void {
|
||||||
$this->register_block_pattern_category();
|
$this->register_block_pattern_categories();
|
||||||
$this->register_patterns();
|
$this->register_patterns();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,14 +27,17 @@ class Patterns {
|
|||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function register_block_pattern_category(): void {
|
private function register_block_pattern_categories(): void {
|
||||||
register_block_pattern_category(
|
$categories = apply_filters( 'mailpoet_email_editor_block_pattern_categories', array() );
|
||||||
'mailpoet',
|
foreach ( $categories as $category ) {
|
||||||
array(
|
register_block_pattern_category(
|
||||||
'label' => _x( 'MailPoet', 'Block pattern category', 'mailpoet' ),
|
$category['name'],
|
||||||
'description' => __( 'A collection of email template layouts.', 'mailpoet' ),
|
array(
|
||||||
)
|
'label' => $category['label'],
|
||||||
);
|
'description' => $category['description'] ?? '',
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,17 +46,9 @@ class Patterns {
|
|||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
private function register_patterns() {
|
private function register_patterns() {
|
||||||
$this->register_pattern( 'default', new Library\Default_Content( $this->cdn_asset_url ) );
|
$patterns = apply_filters( 'mailpoet_email_editor_block_patterns', array() );
|
||||||
$this->register_pattern( 'default-full', new Library\Default_Content_Full( $this->cdn_asset_url ) );
|
foreach ( $patterns as $pattern ) {
|
||||||
}
|
register_block_pattern( $pattern->get_namespace() . '/' . $pattern->get_name(), $pattern->get_properties() );
|
||||||
|
}
|
||||||
/**
|
|
||||||
* Register block pattern.
|
|
||||||
*
|
|
||||||
* @param string $name Name of the pattern.
|
|
||||||
* @param Library\Abstract_Pattern $pattern Pattern to register.
|
|
||||||
*/
|
|
||||||
private function register_pattern( $name, $pattern ) {
|
|
||||||
register_block_pattern( $this->namespace . '/' . $name, $pattern->get_properties() );
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* This file is part of the MailPoet plugin.
|
||||||
|
*
|
||||||
|
* @package MailPoet\EmailEditor
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types = 1);
|
||||||
|
namespace MailPoet\EmailEditor\Engine\Patterns;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dummy test pattern
|
||||||
|
*/
|
||||||
|
class Dummy_Test_Pattern extends Abstract_Pattern {
|
||||||
|
/**
|
||||||
|
* Name of the pattern
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $name = 'dummy-test-pattern';
|
||||||
|
/**
|
||||||
|
* Namespace of the pattern
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $namespace = 'dummy';
|
||||||
|
/**
|
||||||
|
* Get the pattern content
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function get_content(): string {
|
||||||
|
return '<!-- wp:paragraph --><p>Test pattern</p><!-- /wp:paragraph -->';
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Get the pattern title
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function get_title(): string {
|
||||||
|
return 'Test pattern title';
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,92 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* This file is part of the MailPoet plugin.
|
||||||
|
*
|
||||||
|
* @package MailPoet\EmailEditor
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types = 1);
|
||||||
|
namespace MailPoet\EmailEditor\Engine\Patterns;
|
||||||
|
|
||||||
|
require_once 'Dummy_Test_Pattern.php';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Integration test for Patterns class
|
||||||
|
*/
|
||||||
|
class Patterns_Test extends \MailPoetTest {
|
||||||
|
/**
|
||||||
|
* Patterns instance
|
||||||
|
*
|
||||||
|
* @var Patterns
|
||||||
|
*/
|
||||||
|
private $patterns;
|
||||||
|
/**
|
||||||
|
* Set up before each test
|
||||||
|
*/
|
||||||
|
public function _before() {
|
||||||
|
parent::_before();
|
||||||
|
$this->patterns = $this->di_container->get( Patterns::class );
|
||||||
|
$this->cleanup_patterns();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that the patterns added via filter are registered in WP_Block_Patterns_Registry
|
||||||
|
*/
|
||||||
|
public function testItRegistersPatterns() {
|
||||||
|
$pattern = new Dummy_Test_Pattern();
|
||||||
|
add_filter(
|
||||||
|
'mailpoet_email_editor_block_patterns',
|
||||||
|
function ( $patterns ) use ( $pattern ) {
|
||||||
|
$patterns[] = $pattern;
|
||||||
|
return $patterns;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$this->patterns->initialize();
|
||||||
|
$block_patterns = \WP_Block_Patterns_Registry::get_instance()->get_all_registered();
|
||||||
|
$block_pattern = array_pop( $block_patterns );
|
||||||
|
$this->assertEquals( 'dummy/dummy-test-pattern', $block_pattern['name'] );
|
||||||
|
$this->assertEquals( $pattern->get_content(), $block_pattern['content'] );
|
||||||
|
$this->assertEquals( $pattern->get_title(), $block_pattern['title'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test that the pattern categories added via filter are registered in WP_Block_Patterns_Registry
|
||||||
|
*/
|
||||||
|
public function testItRegistersPatternCategories() {
|
||||||
|
add_filter(
|
||||||
|
'mailpoet_email_editor_block_pattern_categories',
|
||||||
|
function ( $categories ) {
|
||||||
|
$pattern_category = array(
|
||||||
|
'name' => 'mailpoet-test',
|
||||||
|
'label' => 'MailPoet',
|
||||||
|
'description' => 'A collection of email template layouts.',
|
||||||
|
);
|
||||||
|
$categories[] = $pattern_category;
|
||||||
|
return $categories;
|
||||||
|
}
|
||||||
|
);
|
||||||
|
$this->patterns->initialize();
|
||||||
|
$categories = \WP_Block_Pattern_Categories_Registry::get_instance()->get_all_registered();
|
||||||
|
$category = array_pop( $categories );
|
||||||
|
$this->assertEquals( 'mailpoet-test', $category['name'] );
|
||||||
|
$this->assertEquals( 'MailPoet', $category['label'] );
|
||||||
|
$this->assertEquals( 'A collection of email template layouts.', $category['description'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clean registered patterns and categories
|
||||||
|
*/
|
||||||
|
private function cleanup_patterns() {
|
||||||
|
$registry = \WP_Block_Patterns_Registry::get_instance();
|
||||||
|
$block_patterns = $registry->get_all_registered();
|
||||||
|
foreach ( $block_patterns as $pattern ) {
|
||||||
|
$registry->unregister( $pattern['name'] );
|
||||||
|
}
|
||||||
|
|
||||||
|
$categories_registry = \WP_Block_Pattern_Categories_Registry::get_instance();
|
||||||
|
$categories = $categories_registry->get_all_registered();
|
||||||
|
foreach ( $categories as $category ) {
|
||||||
|
$categories_registry->unregister( $category['name'] );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user