cssInliner = $cssInliner; $this->processManager = $preprocessManager; $this->blocksRegistry = $blocksRegistry; $this->settingsController = $settingsController; $this->themeController = $themeController; } public function render(\WP_Post $post): string { $parser = new \WP_Block_Parser(); $parsedBlocks = $parser->parse($post->post_content); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps $layout = $this->settingsController->getLayout(); $themeStyles = $this->settingsController->getEmailStyles(); $parsedBlocks = $this->processManager->preprocess($parsedBlocks, $layout, $themeStyles); $renderedBody = $this->renderBlocks($parsedBlocks); $styles = (string)file_get_contents(dirname(__FILE__) . '/' . self::CONTENT_STYLES_FILE); $styles .= $this->themeController->getStylesheetForRendering(); $styles = apply_filters('mailpoet_email_content_renderer_styles', $styles, $post); $renderedBodyDom = $this->inlineCSSStyles("" . $renderedBody); $renderedBody = $this->postProcessTemplate($renderedBodyDom); $renderedBody = $this->processManager->postprocess($renderedBody); return $renderedBody; } public function renderBlocks(array $parsedBlocks): string { do_action('mailpoet_blocks_renderer_initialized', $this->blocksRegistry); $content = ''; foreach ($parsedBlocks as $parsedBlock) { $content .= render_block($parsedBlock); } /** * 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. */ $this->blocksRegistry->removeAllBlockRendererFilters(); return $content; } private function inlineCSSStyles(string $template): DomNode { return $this->cssInliner->inlineCSS($template); } private function postProcessTemplate(DomNode $templateDom): string { // because tburry/pquery contains a bug and replaces the opening non mso condition incorrectly we have to replace the opening tag with correct value $template = $templateDom->__toString(); $template = str_replace('', '', $template); return $template; } }