Remove code that is no longer necessary for the templates to work

[MAILPOET-6356]
This commit is contained in:
Rostislav Wolny
2024-12-13 16:52:28 +01:00
committed by Rostislav Wolný
parent 2fc0ad3c9e
commit 2e661f8b8e
5 changed files with 10 additions and 411 deletions

View File

@@ -350,7 +350,6 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Preprocessors\Typography_Preprocessor::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\Renderer\Renderer::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\Templates\Templates::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\Templates\Utils::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\Patterns\Patterns::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Content_Renderer::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Blocks_Registry::class)->setPublic(true);

View File

@@ -14,12 +14,6 @@ use WP_Block_Template;
* Templates class.
*/
class Templates {
/**
* Provides the utils.
*
* @var Utils $utils
*/
private Utils $utils;
/**
* The plugin slug.
*
@@ -45,22 +39,11 @@ class Templates {
*/
private array $templates = array();
/**
* Templates constructor.
*
* @param Utils $utils The utils.
*/
public function __construct(
Utils $utils
) {
$this->utils = $utils;
}
/**
* Initializes the class.
*/
public function initialize(): void {
$this->initialize_templates();
$this->register_templates();
}
/**
@@ -74,127 +57,10 @@ class Templates {
}
/**
* Get block template from file.
*
* @param WP_Block_Template $result The result.
* @param string $template_id The template ID.
* @param string $template_type The template type.
* @return WP_Block_Template
* Register the templates via register_block_template
*/
public function get_block_file_template( $result, $template_id, $template_type ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
['prefix' => $template_prefix, 'slug' => $template_slug] = $this->utils->get_template_id_parts( $template_id );
if ( $this->template_prefix !== $template_prefix ) {
return $result;
}
$template_path = $template_slug . '.html';
if ( ! is_readable( $this->template_directory . $template_path ) ) {
return $result;
}
return $this->get_block_template_from_file( $template_path );
}
/**
* Add block templates to the block templates list.
*
* @param array $query_result The query result.
* @param array $query The query.
* @param string $template_type The template type.
* @return array
*/
public function add_block_templates( $query_result, $query, $template_type ) {
if ( 'wp_template' !== $template_type ) {
return $query_result;
}
$post_type = isset( $query['post_type'] ) ? $query['post_type'] : '';
if ( $post_type && $post_type !== $this->post_type ) {
return $query_result;
}
foreach ( $this->get_block_templates() as $block_template ) {
$fits_slug_query = ! isset( $query['slug__in'] ) || in_array( $block_template->slug, $query['slug__in'], true );
$fits_area_query = ! isset( $query['area'] ) || ( property_exists( $block_template, 'area' ) && $block_template->area === $query['area'] );
$should_include = $fits_slug_query && $fits_area_query;
if ( $should_include ) {
$query_result[] = $block_template;
}
}
return $query_result;
}
/**
* Add theme templates to the theme templates list.
*
* @param array $templates The templates.
* @param string $theme The theme.
* @param \WP_Post $post The post.
* @param string $post_type The post type.
* @return array
*/
public function add_theme_templates( $templates, $theme, $post, $post_type ) {
if ( $post_type && $post_type !== $this->post_type ) {
return $templates;
}
foreach ( $this->get_block_templates() as $block_template ) {
$templates[ $block_template->slug ] = $block_template;
}
return $templates;
}
/**
* This is a workaround to ensure the post object passed to `inject_ignored_hooked_blocks_metadata_attributes` contains
* content to prevent the template being empty when saved. The issue currently occurs when WooCommerce enables block hooks,
* and when older versions of `inject_ignored_hooked_blocks_metadata_attributes` are
* used (before https://github.com/WordPress/WordPress/commit/725f302121c84c648c38789b2e88dbd1eb41fa48).
* This can be removed in the future.
*
* To test the issue create a new email, revert template changes, save a color change, then save a color change again.
* When you refresh if the post is blank, the issue is present.
*
* @param \stdClass $changes The changes to the post object.
* @return \stdClass
*/
public function force_post_content( $changes ) {
if ( empty( $changes->post_content ) && ! empty( $changes->ID ) ) {
// Find the existing post object.
$post = get_post( $changes->ID );
if ( $post && ! empty( $post->post_content ) ) {
$changes->post_content = $post->post_content;
}
}
return $changes;
}
/**
* Add details to templates in editor.
*
* @param WP_Block_Template $block_template Block template object.
* @return WP_Block_Template
*/
public function add_block_template_details( $block_template ) {
if ( ! $block_template || ! isset( $this->templates[ $block_template->slug ] ) ) {
return $block_template;
}
if ( empty( $block_template->title ) ) {
$block_template->title = $this->templates[ $block_template->slug ]['title'];
}
if ( empty( $block_template->description ) ) {
$block_template->description = $this->templates[ $block_template->slug ]['description'];
}
return $block_template;
}
/**
* Initialize template details. This is done at runtime because of localisation.
*/
private function initialize_templates(): void {
private function register_templates(): void {
// The function was added in WordPress 6.7. We can remove this check after we drop support for WordPress 6.6.
if ( ! function_exists( 'register_block_template' ) ) {
return;
}
@@ -208,108 +74,16 @@ class Templates {
);
foreach ( $this->templates as $template_slug => $template ) {
$template = $this->get_block_template_from_file( $template_slug . '.html' );
$template_filename = $template_slug . '.html';
register_block_template(
$this->template_prefix . '//' . $template_slug,
array(
'title' => $template->title,
'description' => $template->description,
'content' => $template->content,
'title' => $template['title'],
'description' => $template['description'],
'content' => (string) file_get_contents( $this->template_directory . $template_filename ),
'post_types' => array( $this->post_type ),
)
);
}
}
/**
* Gets block templates indexed by ID.
*
* @return WP_Block_Template[]
*/
private function get_block_templates() {
$block_templates = array_map(
function ( $template_slug ) {
return $this->get_block_template_from_file( $template_slug . '.html' );
},
array_keys( $this->templates )
);
$custom_templates = $this->get_custom_templates(); // From the DB.
$custom_template_ids = wp_list_pluck( $custom_templates, 'id' );
// Combine to remove duplicates if a custom template has the same ID as a file template.
return array_column(
array_merge(
$custom_templates,
array_filter(
$block_templates,
function ( $block_template ) use ( $custom_template_ids ) {
return ! in_array( $block_template->id, $custom_template_ids, true );
}
),
),
null,
'id'
);
}
/**
* Get a block template from a file.
*
* @param string $template The template file.
* @return WP_Block_Template
*/
private function get_block_template_from_file( string $template ) {
$template_slug = $this->utils->get_block_template_slug_from_path( $template );
$template_object = (object) array(
'slug' => $template_slug,
'id' => $this->template_prefix . '//' . $template_slug,
'title' => $this->templates[ $template_slug ]['title'] ?? '',
'description' => $this->templates[ $template_slug ]['description'] ?? '',
'path' => $this->template_directory . $template,
'type' => 'wp_template',
'theme' => $this->template_prefix,
'source' => 'plugin',
'post_types' => array(
$this->post_type,
),
);
return $this->utils->build_block_template_from_file( $template_object );
}
/**
* Get custom templates from the database.
*
* @param array $slugs Array of template slugs to get.
* @param string $template_type The template type to get.
* @return array
*/
private function get_custom_templates( $slugs = array(), $template_type = 'wp_template' ): array {
$check_query_args = array(
'post_type' => $template_type,
'posts_per_page' => -1,
'no_found_rows' => true,
'tax_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => array( $this->template_prefix, get_stylesheet() ),
),
),
);
if ( is_array( $slugs ) && count( $slugs ) > 0 ) {
$check_query_args['post_name__in'] = $slugs;
}
$check_query = new \WP_Query( $check_query_args );
$custom_templates = $check_query->posts;
return array_map(
function ( $custom_template ) {
/** @var \WP_Post $custom_template */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort -- used for phpstan
return $this->utils->build_block_template_from_post( $custom_template );
},
$custom_templates
);
}
}

View File

@@ -1,132 +0,0 @@
<?php
/**
* This file is part of the MailPoet Email Editor package.
*
* @package MailPoet\EmailEditor
*/
declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine\Templates;
use WP_Block_Template;
use WP_Error;
/**
* Utils class.
*
* @phpstan-type TemplateObject object{
* slug: string,
* id: string,
* title: string,
* description: string,
* path: string,
* type: string,
* theme: string,
* source: string,
* post_types: array<string>
* }
*/
class Utils {
/**
* Gets the prefix and slug from the template ID.
*
* @param string $template_id Id of the template in prefix//slug format.
* @return array Associative array with keys 'prefix' and 'slug'.
*/
public function get_template_id_parts( string $template_id ): array {
$template_name_parts = explode( '//', $template_id );
if ( count( $template_name_parts ) < 2 ) {
return array(
'prefix' => '',
'slug' => '',
);
}
return array(
'prefix' => $template_name_parts[0],
'slug' => $template_name_parts[1],
);
}
/**
* Gets the block template slug from the path.
*
* @param string $path Path to the block template.
* @return string
*/
public static function get_block_template_slug_from_path( $path ) {
return basename( $path, '.html' );
}
/**
* Build a block template from a post.
*
* @param \WP_Post $post Post object.
* @return WP_Block_Template|WP_Error
*/
public function build_block_template_from_post( $post ) {
$terms = get_the_terms( $post, 'wp_theme' );
if ( is_wp_error( $terms ) ) {
return $terms;
}
if ( ! $terms ) {
return new WP_Error( 'template_missing_theme', 'No theme is defined for this template.' );
}
$template_prefix = $terms[0]->name;
$template_slug = $post->post_name;
$template_id = $template_prefix . '//' . $template_slug;
$template = new WP_Block_Template();
$template->wp_id = $post->ID;
$template->id = $template_id;
$template->theme = $template_prefix;
$template->content = $post->post_content ? $post->post_content : '<p>empty</p>';
$template->slug = $template_slug;
$template->source = 'custom';
$template->type = $post->post_type;
$template->description = $post->post_excerpt;
$template->title = $post->post_title;
$template->status = $post->post_status;
$template->has_theme_file = false;
$template->is_custom = true;
$template->post_types = array();
if ( 'wp_template_part' === $post->post_type ) {
$type_terms = get_the_terms( $post, 'wp_template_part_area' );
if ( ! is_wp_error( $type_terms ) && false !== $type_terms ) {
$template->area = $type_terms[0]->name;
}
}
return $template;
}
/**
* Build a block template from a file.
*
* @param TemplateObject $template_object Template object.
* @return WP_Block_Template
*/
public function build_block_template_from_file( $template_object ): WP_Block_Template {
$template = new WP_Block_Template();
$template->id = $template_object->id;
$template->theme = $template_object->theme;
$template->content = (string) file_get_contents( $template_object->path );
$template->source = $template_object->source;
$template->slug = $template_object->slug;
$template->type = $template_object->type;
$template->title = $template_object->title;
$template->description = $template_object->description;
$template->status = 'publish';
$template->has_theme_file = false;
$template->post_types = $template_object->post_types;
$template->is_custom = false; // Templates are only custom if they are loaded from the DB.
$template->area = 'uncategorized';
return $template;
}
}

View File

@@ -44,39 +44,4 @@ class Templates_Test extends \MailPoetTest {
verify( $template->title )->equals( 'General Email' );
verify( $template->description )->equals( 'A general template for emails.' );
}
/**
* Test it can add block templates
*
* @return void
*/
public function testItCanAddBlockTemplates(): void {
$result = $this->templates->add_block_templates( array(), array( 'post_type' => 'mailpoet_email' ), 'wp_template' );
verify( $result )->arrayCount( 2 );
verify( $result[0]->content )->notEmpty();
verify( $result[1]->content )->notEmpty();
}
/**
* Test it can add and update block template details.
*
* @return void
*/
public function testItCanAddBlockTemplateDetails(): void {
// add_block_template_details.
$basic_template = new \WP_Block_Template();
$basic_template->slug = 'simple-light';
// confirm it has no title or description.
verify( $basic_template->title )->equals( '' );
verify( $basic_template->description )->equals( '' );
$result = $this->templates->add_block_template_details( $basic_template );
// confirm template was updated.
verify( $basic_template->title )->equals( 'Simple Light' );
verify( $basic_template->description )->equals( 'A basic template with header and footer.' );
verify( $result )->equals( $basic_template );
}
}

View File

@@ -25,7 +25,6 @@ use MailPoet\EmailEditor\Engine\Renderer\ContentRenderer\Process_Manager;
use MailPoet\EmailEditor\Engine\Renderer\Renderer;
use MailPoet\EmailEditor\Engine\Settings_Controller;
use MailPoet\EmailEditor\Engine\Templates\Templates;
use MailPoet\EmailEditor\Engine\Templates\Utils;
use MailPoet\EmailEditor\Engine\Theme_Controller;
use MailPoet\EmailEditor\Engine\User_Theme;
use MailPoet\EmailEditor\Integrations\Core\Initializer;
@@ -160,12 +159,6 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore
}
);
// End: MailPoet plugin dependencies.
$container->set(
Utils::class,
function () {
return new Utils();
}
);
$container->set(
Theme_Controller::class,
function () {
@@ -192,8 +185,8 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore
);
$container->set(
Templates::class,
function ( $container ) {
return new Templates( $container->get( Utils::class ) );
function () {
return new Templates();
}
);
$container->set(