Migrate email editor contentRenderer classes from to WP Coding Standard
[MAILPOET-6240]
This commit is contained in:
@@ -67,12 +67,12 @@ class Flex_Layout_Renderer {
|
||||
* @return array
|
||||
*/
|
||||
private function compute_widths_for_flex_layout( array $parsed_block, Settings_Controller $settings_controller, float $flex_gap ): array {
|
||||
// When there is no parent width we can't compute widths so auto width will be used
|
||||
// When there is no parent width we can't compute widths so auto width will be used.
|
||||
if ( ! isset( $parsed_block['email_attrs']['width'] ) ) {
|
||||
return $parsed_block['innerBlocks'] ?? array();
|
||||
}
|
||||
$blocks_count = count( $parsed_block['innerBlocks'] );
|
||||
$total_used_width = 0; // Total width assuming items without set width would consume proportional width
|
||||
$total_used_width = 0; // Total width assuming items without set width would consume proportional width.
|
||||
$parent_width = $settings_controller->parse_number_from_string_with_pixels( $parsed_block['email_attrs']['width'] );
|
||||
$inner_blocks = $parsed_block['innerBlocks'] ?? array();
|
||||
|
||||
|
@@ -1,9 +1,26 @@
|
||||
<?php declare(strict_types = 1);
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the MailPoet plugin.
|
||||
*
|
||||
* @package MailPoet\EmailEditor
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer;
|
||||
|
||||
use MailPoet\EmailEditor\Engine\Settings_Controller;
|
||||
|
||||
/**
|
||||
* Interface Block_Renderer
|
||||
*/
|
||||
interface Block_Renderer {
|
||||
public function render( string $blockContent, array $parsedBlock, Settings_Controller $settingsController ): string;
|
||||
/**
|
||||
* Renders the block content
|
||||
*
|
||||
* @param string $block_content Block content.
|
||||
* @param array $parsed_block Parsed block.
|
||||
* @param Settings_Controller $settings_controller Settings controller.
|
||||
* @return string
|
||||
*/
|
||||
public function render( string $block_content, array $parsed_block, Settings_Controller $settings_controller ): string;
|
||||
}
|
||||
|
@@ -1,9 +1,18 @@
|
||||
<?php declare(strict_types = 1);
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the MailPoet plugin.
|
||||
*
|
||||
* @package MailPoet\EmailEditor
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer;
|
||||
|
||||
use WP_Block_Parser;
|
||||
|
||||
/**
|
||||
* Class Blocks_Parser
|
||||
*/
|
||||
class Blocks_Parser extends WP_Block_Parser {
|
||||
/**
|
||||
* List of parsed blocks
|
||||
@@ -12,6 +21,12 @@ class Blocks_Parser extends WP_Block_Parser {
|
||||
*/
|
||||
public $output;
|
||||
|
||||
/**
|
||||
* Parse the blocks from the document
|
||||
*
|
||||
* @param string $document Document to parse.
|
||||
* @return \WP_Block_Parser_Block[]
|
||||
*/
|
||||
public function parse( $document ) {
|
||||
parent::parse( $document );
|
||||
return apply_filters( 'mailpoet_blocks_renderer_parsed_blocks', $this->output );
|
||||
|
@@ -1,41 +1,93 @@
|
||||
<?php declare(strict_types = 1);
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the MailPoet plugin.
|
||||
*
|
||||
* @package MailPoet\EmailEditor
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer;
|
||||
|
||||
/**
|
||||
* Class Blocks_Registry
|
||||
*/
|
||||
class Blocks_Registry {
|
||||
/**
|
||||
* Fallback renderer.
|
||||
*
|
||||
* @var $fallback_renderer Block_Renderer
|
||||
*/
|
||||
private $fallback_renderer = null;
|
||||
/**
|
||||
* Array of block renderers.
|
||||
*
|
||||
* @var $block_renderers_map Block_Renderer[]
|
||||
*/
|
||||
private array $block_renderers_map = array();
|
||||
|
||||
/** @var Block_Renderer[] */
|
||||
private $blockRenderersMap = array();
|
||||
/** @var BlockRenderer */
|
||||
private $fallbackRenderer = null;
|
||||
|
||||
public function addBlockRenderer( string $blockName, Block_Renderer $renderer ): void {
|
||||
$this->blockRenderersMap[ $blockName ] = $renderer;
|
||||
/**
|
||||
* Adds block renderer to the registry.
|
||||
*
|
||||
* @param string $block_name Block name.
|
||||
* @param Block_Renderer $renderer Block renderer.
|
||||
*/
|
||||
public function add_block_renderer( string $block_name, Block_Renderer $renderer ): void {
|
||||
$this->block_renderers_map[ $block_name ] = $renderer;
|
||||
}
|
||||
|
||||
public function addFallbackRenderer( BlockRenderer $renderer ): void {
|
||||
$this->fallbackRenderer = $renderer;
|
||||
/**
|
||||
* Adds fallback renderer to the registry.
|
||||
*
|
||||
* @param Block_Renderer $renderer Fallback renderer.
|
||||
*/
|
||||
public function add_fallback_renderer( Block_Renderer $renderer ): void {
|
||||
$this->fallback_renderer = $renderer;
|
||||
}
|
||||
|
||||
public function hasBlockRenderer( string $blockName ): bool {
|
||||
return isset( $this->blockRenderersMap[ $blockName ] );
|
||||
/**
|
||||
* Checks if block renderer is registered.
|
||||
*
|
||||
* @param string $block_name Block name.
|
||||
* @return bool
|
||||
*/
|
||||
public function has_block_renderer( string $block_name ): bool {
|
||||
return isset( $this->block_renderers_map[ $block_name ] );
|
||||
}
|
||||
|
||||
public function getBlockRenderer( string $blockName ): ?Block_Renderer {
|
||||
return $this->blockRenderersMap[ $blockName ] ?? null;
|
||||
/**
|
||||
* Returns block renderer by block name.
|
||||
*
|
||||
* @param string $block_name Block name.
|
||||
* @return Block_Renderer|null
|
||||
*/
|
||||
public function get_block_renderer( string $block_name ): ?Block_Renderer {
|
||||
return $this->block_renderers_map[ $block_name ] ?? null;
|
||||
}
|
||||
|
||||
public function getFallbackRenderer(): ?BlockRenderer {
|
||||
return $this->fallbackRenderer;
|
||||
/**
|
||||
* Returns fallback renderer.
|
||||
*
|
||||
* @return Block_Renderer|null
|
||||
*/
|
||||
public function get_fallback_renderer(): ?Block_Renderer {
|
||||
return $this->fallback_renderer;
|
||||
}
|
||||
|
||||
public function removeAllBlockRenderers(): void {
|
||||
foreach ( array_keys( $this->blockRenderersMap ) as $blockName ) {
|
||||
$this->removeBlockRenderer( $blockName );
|
||||
/**
|
||||
* Removes all block renderers from the registry.
|
||||
*/
|
||||
public function remove_all_block_renderers(): void {
|
||||
foreach ( array_keys( $this->block_renderers_map ) as $block_name ) {
|
||||
$this->remove_block_renderer( $block_name );
|
||||
}
|
||||
}
|
||||
|
||||
private function removeBlockRenderer( string $blockName ): void {
|
||||
unset( $this->blockRenderersMap[ $blockName ] );
|
||||
/**
|
||||
* Removes block renderer from the registry.
|
||||
*
|
||||
* @param string $block_name Block name.
|
||||
*/
|
||||
private function remove_block_renderer( string $block_name ): void {
|
||||
unset( $this->block_renderers_map[ $block_name ] );
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,11 @@
|
||||
<?php declare(strict_types = 1);
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the MailPoet plugin.
|
||||
*
|
||||
* @package MailPoet\EmailEditor
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer;
|
||||
|
||||
use MailPoet\EmailEditor\Engine\Settings_Controller;
|
||||
@@ -8,91 +14,175 @@ use MailPoetVendor\Pelago\Emogrifier\CssInliner;
|
||||
use WP_Block_Template;
|
||||
use WP_Post;
|
||||
|
||||
/**
|
||||
* Class Content_Renderer
|
||||
*/
|
||||
class Content_Renderer {
|
||||
private Blocks_Registry $blocksRegistry;
|
||||
private Process_Manager $processManager;
|
||||
private Settings_Controller $settingsController;
|
||||
private Theme_Controller $themeController;
|
||||
/**
|
||||
* Blocks registry
|
||||
*
|
||||
* @var Blocks_Registry
|
||||
*/
|
||||
private Blocks_Registry $blocks_registry;
|
||||
|
||||
/**
|
||||
* Process manager
|
||||
*
|
||||
* @var Process_Manager
|
||||
*/
|
||||
private Process_Manager $process_manager;
|
||||
|
||||
/**
|
||||
* Settings controller
|
||||
*
|
||||
* @var Settings_Controller
|
||||
*/
|
||||
private Settings_Controller $settings_controller;
|
||||
|
||||
/**
|
||||
* Theme controller
|
||||
*
|
||||
* @var Theme_Controller
|
||||
*/
|
||||
private Theme_Controller $theme_controller;
|
||||
|
||||
/**
|
||||
* Post object
|
||||
*
|
||||
* @var WP_Post
|
||||
*/
|
||||
private $post = null;
|
||||
|
||||
/**
|
||||
* Block template
|
||||
*
|
||||
* @var WP_Block_Template
|
||||
*/
|
||||
private $template = null;
|
||||
|
||||
const CONTENT_STYLES_FILE = 'content.css';
|
||||
|
||||
/**
|
||||
* Content_Renderer constructor.
|
||||
*
|
||||
* @param Process_Manager $preprocess_manager Preprocess manager.
|
||||
* @param Blocks_Registry $blocks_registry Blocks registry.
|
||||
* @param Settings_Controller $settings_controller Settings controller.
|
||||
* @param Theme_Controller $theme_controller Theme controller.
|
||||
*/
|
||||
public function __construct(
|
||||
Process_Manager $preprocessManager,
|
||||
Blocks_Registry $blocksRegistry,
|
||||
Settings_Controller $settingsController,
|
||||
Theme_Controller $themeController
|
||||
Process_Manager $preprocess_manager,
|
||||
Blocks_Registry $blocks_registry,
|
||||
Settings_Controller $settings_controller,
|
||||
Theme_Controller $theme_controller
|
||||
) {
|
||||
$this->processManager = $preprocessManager;
|
||||
$this->blocksRegistry = $blocksRegistry;
|
||||
$this->settingsController = $settingsController;
|
||||
$this->themeController = $themeController;
|
||||
$this->process_manager = $preprocess_manager;
|
||||
$this->blocks_registry = $blocks_registry;
|
||||
$this->settings_controller = $settings_controller;
|
||||
$this->theme_controller = $theme_controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the content renderer
|
||||
*/
|
||||
private function initialize() {
|
||||
add_filter( 'render_block', array( $this, 'renderBlock' ), 10, 2 );
|
||||
add_filter( 'block_parser_class', array( $this, 'blockParser' ) );
|
||||
add_filter( 'mailpoet_blocks_renderer_parsed_blocks', array( $this, 'preprocessParsedBlocks' ) );
|
||||
add_filter( 'render_block', array( $this, 'render_block' ), 10, 2 );
|
||||
add_filter( 'block_parser_class', array( $this, 'block_parser' ) );
|
||||
add_filter( 'mailpoet_blocks_renderer_parsed_blocks', array( $this, 'preprocess_parsed_blocks' ) );
|
||||
|
||||
do_action( 'mailpoet_blocks_renderer_initialized', $this->blocksRegistry );
|
||||
do_action( 'mailpoet_blocks_renderer_initialized', $this->blocks_registry );
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the content
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
* @param WP_Block_Template $template Block template.
|
||||
* @return string
|
||||
*/
|
||||
public function render( WP_Post $post, WP_Block_Template $template ): string {
|
||||
$this->post = $post;
|
||||
$this->template = $template;
|
||||
$this->setTemplateGlobals( $post, $template );
|
||||
$this->set_template_globals( $post, $template );
|
||||
$this->initialize();
|
||||
$renderedHtml = get_the_block_template_html();
|
||||
$rendered_html = get_the_block_template_html();
|
||||
$this->reset();
|
||||
|
||||
return $this->processManager->postprocess( $this->inlineStyles( $renderedHtml, $post, $template ) );
|
||||
return $this->process_manager->postprocess( $this->inline_styles( $rendered_html, $post, $template ) );
|
||||
}
|
||||
|
||||
public function blockParser() {
|
||||
/**
|
||||
* Get block parser class
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function block_parser() {
|
||||
return 'MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Blocks_Parser';
|
||||
}
|
||||
|
||||
public function preprocessParsedBlocks( array $parsedBlocks ): array {
|
||||
return $this->processManager->preprocess( $parsedBlocks, $this->themeController->get_layout_settings(), $this->themeController->get_styles( $this->post, $this->template ) );
|
||||
/**
|
||||
* Preprocess parsed blocks
|
||||
*
|
||||
* @param array $parsed_blocks Parsed blocks.
|
||||
* @return array
|
||||
*/
|
||||
public function preprocess_parsed_blocks( array $parsed_blocks ): array {
|
||||
return $this->process_manager->preprocess( $parsed_blocks, $this->theme_controller->get_layout_settings(), $this->theme_controller->get_styles( $this->post, $this->template ) );
|
||||
}
|
||||
|
||||
public function renderBlock( $blockContent, $parsedBlock ) {
|
||||
$renderer = $this->blocksRegistry->getBlockRenderer( $parsedBlock['blockName'] );
|
||||
/**
|
||||
* Render block
|
||||
*
|
||||
* @param string $block_content Block content.
|
||||
* @param array $parsed_block Parsed block.
|
||||
* @return string
|
||||
*/
|
||||
public function render_block( $block_content, $parsed_block ) {
|
||||
$renderer = $this->blocks_registry->get_block_renderer( $parsed_block['blockName'] );
|
||||
if ( ! $renderer ) {
|
||||
$renderer = $this->blocksRegistry->getFallbackRenderer();
|
||||
$renderer = $this->blocks_registry->get_fallback_renderer();
|
||||
}
|
||||
return $renderer ? $renderer->render( $blockContent, $parsedBlock, $this->settingsController ) : $blockContent;
|
||||
return $renderer ? $renderer->render( $block_content, $parsed_block, $this->settings_controller ) : $block_content;
|
||||
}
|
||||
|
||||
private function setTemplateGlobals( WP_Post $post, WP_Block_Template $template ) {
|
||||
/**
|
||||
* Set template globals
|
||||
*
|
||||
* @param WP_Post $post Post object.
|
||||
* @param WP_Block_Template $template Block template.
|
||||
*/
|
||||
private function set_template_globals( WP_Post $post, WP_Block_Template $template ) {
|
||||
global $_wp_current_template_content, $_wp_current_template_id;
|
||||
$_wp_current_template_id = $template->id;
|
||||
$_wp_current_template_content = $template->content;
|
||||
$GLOBALS['post'] = $post;
|
||||
$GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- I have not found a better way to set the post object for the block renderer.
|
||||
}
|
||||
|
||||
/**
|
||||
* As we use default WordPress filters, we need to remove them after email rendering
|
||||
* so that we don't interfere with possible post rendering that might happen later.
|
||||
*/
|
||||
private function reset() {
|
||||
$this->blocksRegistry->removeAllBlockRenderers();
|
||||
remove_filter( 'render_block', array( $this, 'renderBlock' ) );
|
||||
remove_filter( 'block_parser_class', array( $this, 'blockParser' ) );
|
||||
remove_filter( 'mailpoet_blocks_renderer_parsed_blocks', array( $this, 'preprocessParsedBlocks' ) );
|
||||
private function reset(): void {
|
||||
$this->blocks_registry->remove_all_block_renderers();
|
||||
remove_filter( 'render_block', array( $this, 'render_block' ) );
|
||||
remove_filter( 'block_parser_class', array( $this, 'block_parser' ) );
|
||||
remove_filter( 'mailpoet_blocks_renderer_parsed_blocks', array( $this, 'preprocess_parsed_blocks' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $html
|
||||
* Method to inline styles into the HTML
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @param WP_Post $post Post object.
|
||||
* @param WP_Block_Template|null $template Block template.
|
||||
* @return string
|
||||
*/
|
||||
private function inlineStyles( $html, WP_Post $post, $template = null ) {
|
||||
private function inline_styles( $html, WP_Post $post, $template = null ) {
|
||||
$styles = (string) file_get_contents( __DIR__ . '/' . self::CONTENT_STYLES_FILE );
|
||||
$styles .= (string) file_get_contents( __DIR__ . '/../../content-shared.css' );
|
||||
|
||||
// Apply default contentWidth to constrained blocks.
|
||||
$layout = $this->themeController->get_layout_settings();
|
||||
$layout = $this->theme_controller->get_layout_settings();
|
||||
$styles .= sprintf(
|
||||
'
|
||||
.is-layout-constrained > *:not(.alignleft):not(.alignright):not(.alignfull) {
|
||||
@@ -111,32 +201,37 @@ class Content_Renderer {
|
||||
);
|
||||
|
||||
// Get styles from theme.
|
||||
$styles .= $this->themeController->get_stylesheet_for_rendering( $post, $template );
|
||||
$blockSupportStyles = $this->themeController->get_stylesheet_from_context( 'block-supports', array() );
|
||||
$styles .= $this->theme_controller->get_stylesheet_for_rendering( $post, $template );
|
||||
$block_support_styles = $this->theme_controller->get_stylesheet_from_context( 'block-supports', array() );
|
||||
// Get styles from block-supports stylesheet. This includes rules such as layout (contentWidth) that some blocks use.
|
||||
// @see https://github.com/WordPress/WordPress/blob/3c5da9c74344aaf5bf8097f2e2c6a1a781600e03/wp-includes/script-loader.php#L3134
|
||||
// @internal :where is not supported by emogrifier, so we need to replace it with *.
|
||||
$blockSupportStyles = str_replace(
|
||||
$block_support_styles = str_replace(
|
||||
':where(:not(.alignleft):not(.alignright):not(.alignfull))',
|
||||
'*:not(.alignleft):not(.alignright):not(.alignfull)',
|
||||
$blockSupportStyles
|
||||
$block_support_styles
|
||||
);
|
||||
// Layout CSS assumes the top level block will have a single DIV wrapper with children. Since our blocks use tables,
|
||||
// we need to adjust this to look for children in the TD element. This may requires more advanced replacement but
|
||||
// this works in the current version of Gutenberg.
|
||||
// Example rule we're targetting: .wp-container-core-group-is-layout-1.wp-container-core-group-is-layout-1 > *
|
||||
$blockSupportStyles = preg_replace(
|
||||
|
||||
/*
|
||||
*Layout CSS assumes the top level block will have a single DIV wrapper with children. Since our blocks use tables,
|
||||
* we need to adjust this to look for children in the TD element. This may requires more advanced replacement but
|
||||
* this works in the current version of Gutenberg.
|
||||
* Example rule we're targetting: .wp-container-core-group-is-layout-1.wp-container-core-group-is-layout-1 > *
|
||||
*/
|
||||
$block_support_styles = preg_replace(
|
||||
'/group-is-layout-(\d+) >/',
|
||||
'group-is-layout-$1 > tbody tr td >',
|
||||
$blockSupportStyles
|
||||
$block_support_styles
|
||||
);
|
||||
|
||||
$styles .= $blockSupportStyles;
|
||||
$styles .= $block_support_styles;
|
||||
|
||||
// Debugging for content styles. Remember these get inlined.
|
||||
// echo '<pre>';
|
||||
// var_dump($styles);
|
||||
// echo '</pre>';
|
||||
/*
|
||||
* Debugging for content styles. Remember these get inlined.
|
||||
* echo '<pre>';
|
||||
* var_dump($styles);
|
||||
* echo '</pre>';
|
||||
*/
|
||||
|
||||
$styles = '<style>' . wp_strip_all_tags( (string) apply_filters( 'mailpoet_email_content_renderer_styles', $styles, $post ) ) . '</style>';
|
||||
|
||||
|
@@ -1,5 +1,11 @@
|
||||
<?php declare(strict_types = 1);
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the MailPoet plugin.
|
||||
*
|
||||
* @package MailPoet\EmailEditor
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer;
|
||||
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Postprocessors\Highlighting_Postprocessor;
|
||||
@@ -11,42 +17,71 @@ use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\Preproces
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\Spacing_Preprocessor;
|
||||
use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\Typography_Preprocessor;
|
||||
|
||||
/**
|
||||
* Class Process_Manager
|
||||
*/
|
||||
class Process_Manager {
|
||||
/** @var Preprocessor[] */
|
||||
/**
|
||||
* List of preprocessors
|
||||
*
|
||||
* @var Preprocessor[]
|
||||
*/
|
||||
private $preprocessors = array();
|
||||
|
||||
/** @var Postprocessor[] */
|
||||
/**
|
||||
* List of postprocessors
|
||||
*
|
||||
* @var Postprocessor[]
|
||||
*/
|
||||
private $postprocessors = array();
|
||||
|
||||
/**
|
||||
* Process_Manager constructor.
|
||||
*
|
||||
* @param Cleanup_Preprocessor $cleanup_preprocessor Cleanup preprocessor.
|
||||
* @param Blocks_Width_Preprocessor $blocks_width_preprocessor Blocks width preprocessor.
|
||||
* @param Typography_Preprocessor $typography_preprocessor Typography preprocessor.
|
||||
* @param Spacing_Preprocessor $spacing_preprocessor Spacing preprocessor.
|
||||
* @param Highlighting_Postprocessor $highlighting_postprocessor Highlighting postprocessor.
|
||||
* @param Variables_Postprocessor $variables_postprocessor Variables postprocessor.
|
||||
*/
|
||||
public function __construct(
|
||||
Cleanup_Preprocessor $cleanupPreprocessor,
|
||||
Blocks_Width_Preprocessor $blocksWidthPreprocessor,
|
||||
Typography_Preprocessor $typographyPreprocessor,
|
||||
Spacing_Preprocessor $spacingPreprocessor,
|
||||
Highlighting_Postprocessor $highlightingPostprocessor,
|
||||
Variables_Postprocessor $variablesPostprocessor
|
||||
Cleanup_Preprocessor $cleanup_preprocessor,
|
||||
Blocks_Width_Preprocessor $blocks_width_preprocessor,
|
||||
Typography_Preprocessor $typography_preprocessor,
|
||||
Spacing_Preprocessor $spacing_preprocessor,
|
||||
Highlighting_Postprocessor $highlighting_postprocessor,
|
||||
Variables_Postprocessor $variables_postprocessor
|
||||
) {
|
||||
$this->registerPreprocessor( $cleanupPreprocessor );
|
||||
$this->registerPreprocessor( $blocksWidthPreprocessor );
|
||||
$this->registerPreprocessor( $typographyPreprocessor );
|
||||
$this->registerPreprocessor( $spacingPreprocessor );
|
||||
$this->registerPostprocessor( $highlightingPostprocessor );
|
||||
$this->registerPostprocessor( $variablesPostprocessor );
|
||||
$this->register_preprocessor( $cleanup_preprocessor );
|
||||
$this->register_preprocessor( $blocks_width_preprocessor );
|
||||
$this->register_preprocessor( $typography_preprocessor );
|
||||
$this->register_preprocessor( $spacing_preprocessor );
|
||||
$this->register_postprocessor( $highlighting_postprocessor );
|
||||
$this->register_postprocessor( $variables_postprocessor );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $parsedBlocks
|
||||
* @param array{contentSize: string} $layout
|
||||
* @param array{spacing: array{padding: array{bottom: string, left: string, right: string, top: string}, blockGap: string}} $styles
|
||||
* Method to preprocess blocks
|
||||
*
|
||||
* @param array $parsed_blocks Parsed blocks.
|
||||
* @param array{contentSize: string} $layout Layout.
|
||||
* @param array{spacing: array{padding: array{bottom: string, left: string, right: string, top: string}, blockGap: string}} $styles Styles.
|
||||
* @return array
|
||||
*/
|
||||
public function preprocess( array $parsedBlocks, array $layout, array $styles ): array {
|
||||
public function preprocess( array $parsed_blocks, array $layout, array $styles ): array {
|
||||
foreach ( $this->preprocessors as $preprocessor ) {
|
||||
$parsedBlocks = $preprocessor->preprocess( $parsedBlocks, $layout, $styles );
|
||||
$parsed_blocks = $preprocessor->preprocess( $parsed_blocks, $layout, $styles );
|
||||
}
|
||||
return $parsedBlocks;
|
||||
return $parsed_blocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to postprocess the content
|
||||
*
|
||||
* @param string $html HTML content.
|
||||
* @return string
|
||||
*/
|
||||
public function postprocess( string $html ): string {
|
||||
foreach ( $this->postprocessors as $postprocessor ) {
|
||||
$html = $postprocessor->postprocess( $html );
|
||||
@@ -54,11 +89,21 @@ class Process_Manager {
|
||||
return $html;
|
||||
}
|
||||
|
||||
public function registerPreprocessor( Preprocessor $preprocessor ): void {
|
||||
/**
|
||||
* Register preprocessor
|
||||
*
|
||||
* @param Preprocessor $preprocessor Preprocessor.
|
||||
*/
|
||||
public function register_preprocessor( Preprocessor $preprocessor ): void {
|
||||
$this->preprocessors[] = $preprocessor;
|
||||
}
|
||||
|
||||
public function registerPostprocessor( Postprocessor $postprocessor ): void {
|
||||
/**
|
||||
* Register postprocessor
|
||||
*
|
||||
* @param Postprocessor $postprocessor Postprocessor.
|
||||
*/
|
||||
public function register_postprocessor( Postprocessor $postprocessor ): void {
|
||||
$this->postprocessors[] = $postprocessor;
|
||||
}
|
||||
}
|
||||
|
@@ -33,7 +33,7 @@ use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Blocks_Registry;
|
||||
add_action('mailpoet_blocks_renderer_initialized', 'register_my_block_email_renderer');
|
||||
|
||||
function register_my_block_email_renderer(Blocks_Registry $blocksRegistry): void {
|
||||
$blocksRegistry->addBlockRenderer('core/heading', new Renderer\Blocks\Heading());
|
||||
$blocksRegistry->add_block_renderer('core/heading', new Renderer\Blocks\Heading());
|
||||
}
|
||||
```
|
||||
|
||||
|
@@ -57,10 +57,10 @@ abstract class Abstract_Block_Renderer implements Block_Renderer {
|
||||
);
|
||||
}
|
||||
|
||||
public function render( string $blockContent, array $parsedBlock, Settings_Controller $settingsController ): string {
|
||||
public function render(string $block_content, array $parsed_block, Settings_Controller $settings_controller ): string {
|
||||
return $this->addSpacer(
|
||||
$this->renderContent( $blockContent, $parsedBlock, $settingsController ),
|
||||
$parsedBlock['email_attrs'] ?? array()
|
||||
$this->renderContent( $block_content, $parsed_block, $settings_controller ),
|
||||
$parsed_block['email_attrs'] ?? array()
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -42,8 +42,8 @@ class Button extends Abstract_Block_Renderer {
|
||||
);
|
||||
}
|
||||
|
||||
public function render( string $blockContent, array $parsedBlock, Settings_Controller $settingsController ): string {
|
||||
return $this->renderContent( $blockContent, $parsedBlock, $settingsController );
|
||||
public function render(string $block_content, array $parsed_block, Settings_Controller $settings_controller ): string {
|
||||
return $this->renderContent( $block_content, $parsed_block, $settings_controller );
|
||||
}
|
||||
|
||||
protected function renderContent( $blockContent, array $parsedBlock, Settings_Controller $settingsController ): string {
|
||||
|
@@ -16,18 +16,18 @@ class Initializer {
|
||||
* Register core blocks email renderers when the blocks renderer is initialized.
|
||||
*/
|
||||
public function registerCoreBlocksRenderers( Blocks_Registry $blocksRegistry ): void {
|
||||
$blocksRegistry->addBlockRenderer( 'core/paragraph', new Renderer\Blocks\Text() );
|
||||
$blocksRegistry->addBlockRenderer( 'core/heading', new Renderer\Blocks\Text() );
|
||||
$blocksRegistry->addBlockRenderer( 'core/column', new Renderer\Blocks\Column() );
|
||||
$blocksRegistry->addBlockRenderer( 'core/columns', new Renderer\Blocks\Columns() );
|
||||
$blocksRegistry->addBlockRenderer( 'core/list', new Renderer\Blocks\List_Block() );
|
||||
$blocksRegistry->addBlockRenderer( 'core/list-item', new Renderer\Blocks\List_Item() );
|
||||
$blocksRegistry->addBlockRenderer( 'core/image', new Renderer\Blocks\Image() );
|
||||
$blocksRegistry->addBlockRenderer( 'core/buttons', new Renderer\Blocks\Buttons( new Flex_Layout_Renderer() ) );
|
||||
$blocksRegistry->addBlockRenderer( 'core/button', new Renderer\Blocks\Button() );
|
||||
$blocksRegistry->addBlockRenderer( 'core/group', new Renderer\Blocks\Group() );
|
||||
$blocksRegistry->add_block_renderer( 'core/paragraph', new Renderer\Blocks\Text() );
|
||||
$blocksRegistry->add_block_renderer( 'core/heading', new Renderer\Blocks\Text() );
|
||||
$blocksRegistry->add_block_renderer( 'core/column', new Renderer\Blocks\Column() );
|
||||
$blocksRegistry->add_block_renderer( 'core/columns', new Renderer\Blocks\Columns() );
|
||||
$blocksRegistry->add_block_renderer( 'core/list', new Renderer\Blocks\List_Block() );
|
||||
$blocksRegistry->add_block_renderer( 'core/list-item', new Renderer\Blocks\List_Item() );
|
||||
$blocksRegistry->add_block_renderer( 'core/image', new Renderer\Blocks\Image() );
|
||||
$blocksRegistry->add_block_renderer( 'core/buttons', new Renderer\Blocks\Buttons( new Flex_Layout_Renderer() ) );
|
||||
$blocksRegistry->add_block_renderer( 'core/button', new Renderer\Blocks\Button() );
|
||||
$blocksRegistry->add_block_renderer( 'core/group', new Renderer\Blocks\Group() );
|
||||
// Render used for all other blocks
|
||||
$blocksRegistry->addFallbackRenderer( new Renderer\Blocks\Fallback() );
|
||||
$blocksRegistry->add_fallback_renderer( new Renderer\Blocks\Fallback() );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -17,21 +17,21 @@ class Blocks_Registry_Test extends \MailPoetTest {
|
||||
}
|
||||
|
||||
public function testItReturnsNullForUnknownRenderer() {
|
||||
$storedRenderer = $this->registry->getBlockRenderer('test');
|
||||
$storedRenderer = $this->registry->get_block_renderer('test');
|
||||
verify($storedRenderer)->null();
|
||||
}
|
||||
|
||||
public function testItStoresAddedRenderer() {
|
||||
$renderer = new Text();
|
||||
$this->registry->addBlockRenderer('test', $renderer);
|
||||
$storedRenderer = $this->registry->getBlockRenderer('test');
|
||||
$this->registry->add_block_renderer('test', $renderer);
|
||||
$storedRenderer = $this->registry->get_block_renderer('test');
|
||||
verify($storedRenderer)->equals($renderer);
|
||||
}
|
||||
|
||||
public function testItReportsWhichRenderersAreRegistered() {
|
||||
$renderer = new Text();
|
||||
$this->registry->addBlockRenderer('test', $renderer);
|
||||
verify($this->registry->hasBlockRenderer('test'))->true();
|
||||
verify($this->registry->hasBlockRenderer('unknown'))->false();
|
||||
$this->registry->add_block_renderer('test', $renderer);
|
||||
verify($this->registry->has_block_renderer('test'))->true();
|
||||
verify($this->registry->has_block_renderer('unknown'))->false();
|
||||
}
|
||||
}
|
||||
|
@@ -5,7 +5,7 @@ namespace MailPoet\EmailEditor\Engine\Renderer\ContentRenderer;
|
||||
use MailPoet\EmailEditor\Engine\Settings_Controller;
|
||||
|
||||
class Dummy_Block_Renderer implements Block_Renderer {
|
||||
public function render(string $blockContent, array $parsedBlock, Settings_Controller $settingsController): string {
|
||||
return $parsedBlock['innerHtml'];
|
||||
public function render(string $block_content, array $parsed_block, Settings_Controller $settings_controller): string {
|
||||
return $parsed_block['innerHtml'];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user