Migrate email editor utils class to WP Coding Standard
[MAILPOET-6240]
This commit is contained in:
@@ -58,7 +58,7 @@ class Templates {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If there is no user edited theme, look for default template themes in files.
|
// If there is no user edited theme, look for default template themes in files.
|
||||||
['prefix' => $templatePrefix, 'slug' => $templateSlug] = $this->utils->getTemplateIdParts( $templateId );
|
['prefix' => $templatePrefix, 'slug' => $templateSlug] = $this->utils->get_template_id_parts( $templateId );
|
||||||
|
|
||||||
if ( $this->pluginSlug !== $templatePrefix ) {
|
if ( $this->pluginSlug !== $templatePrefix ) {
|
||||||
return self::MAILPOET_TEMPLATE_EMPTY_THEME;
|
return self::MAILPOET_TEMPLATE_EMPTY_THEME;
|
||||||
@@ -76,7 +76,7 @@ class Templates {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function getBlockFileTemplate( $return, $templateId, $template_type ) {
|
public function getBlockFileTemplate( $return, $templateId, $template_type ) {
|
||||||
['prefix' => $templatePrefix, 'slug' => $templateSlug] = $this->utils->getTemplateIdParts( $templateId );
|
['prefix' => $templatePrefix, 'slug' => $templateSlug] = $this->utils->get_template_id_parts( $templateId );
|
||||||
|
|
||||||
if ( $this->pluginSlug !== $templatePrefix ) {
|
if ( $this->pluginSlug !== $templatePrefix ) {
|
||||||
return $return;
|
return $return;
|
||||||
@@ -239,7 +239,7 @@ class Templates {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function getBlockTemplateFromFile( string $template ) {
|
private function getBlockTemplateFromFile( string $template ) {
|
||||||
$template_slug = $this->utils->getBlockTemplateSlugFromPath( $template );
|
$template_slug = $this->utils->get_block_template_slug_from_path( $template );
|
||||||
$templateObject = (object) array(
|
$templateObject = (object) array(
|
||||||
'slug' => $template_slug,
|
'slug' => $template_slug,
|
||||||
'id' => $this->pluginSlug . '//' . $template_slug,
|
'id' => $this->pluginSlug . '//' . $template_slug,
|
||||||
@@ -253,7 +253,7 @@ class Templates {
|
|||||||
$this->postType,
|
$this->postType,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
return $this->utils->buildBlockTemplateFromFile( $templateObject );
|
return $this->utils->build_block_template_from_file( $templateObject );
|
||||||
}
|
}
|
||||||
|
|
||||||
private function getCustomTemplates( $slugs = array(), $template_type = 'wp_template' ) {
|
private function getCustomTemplates( $slugs = array(), $template_type = 'wp_template' ) {
|
||||||
@@ -279,7 +279,7 @@ class Templates {
|
|||||||
|
|
||||||
return array_map(
|
return array_map(
|
||||||
function ( $custom_template ) {
|
function ( $custom_template ) {
|
||||||
return $this->utils->buildBlockTemplateFromPost( $custom_template );
|
return $this->utils->build_block_template_from_post( $custom_template );
|
||||||
},
|
},
|
||||||
$custom_templates
|
$custom_templates
|
||||||
);
|
);
|
||||||
|
@@ -1,20 +1,28 @@
|
|||||||
<?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\Templates;
|
namespace MailPoet\EmailEditor\Engine\Templates;
|
||||||
|
|
||||||
use WP_Block_Template;
|
use WP_Block_Template;
|
||||||
use WP_Error;
|
use WP_Error;
|
||||||
|
|
||||||
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
|
/**
|
||||||
|
* Utils class.
|
||||||
|
*/
|
||||||
class Utils {
|
class Utils {
|
||||||
/**
|
/**
|
||||||
* Gets the prefix and slug from the template ID.
|
* Gets the prefix and slug from the template ID.
|
||||||
*
|
*
|
||||||
* @param string $templateId Id of the template in prefix//slug format.
|
* @param string $template_id Id of the template in prefix//slug format.
|
||||||
* @return array Associative array with keys 'prefix' and 'slug'.
|
* @return array Associative array with keys 'prefix' and 'slug'.
|
||||||
*/
|
*/
|
||||||
public function getTemplateIdParts( $templateId ) {
|
public function get_template_id_parts( string $template_id ): array {
|
||||||
$template_name_parts = explode( '//', $templateId );
|
$template_name_parts = explode( '//', $template_id );
|
||||||
|
|
||||||
if ( count( $template_name_parts ) < 2 ) {
|
if ( count( $template_name_parts ) < 2 ) {
|
||||||
return array(
|
return array(
|
||||||
@@ -29,11 +37,23 @@ class Utils {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getBlockTemplateSlugFromPath( $path ) {
|
/**
|
||||||
|
* 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' );
|
return basename( $path, '.html' );
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildBlockTemplateFromPost( $post ) {
|
/**
|
||||||
|
* Build a block template from a post.
|
||||||
|
*
|
||||||
|
* @param object $post Post object.
|
||||||
|
* @return WP_Block_Template|WP_Error
|
||||||
|
*/
|
||||||
|
public function build_block_template_from_post( $post ) {
|
||||||
$terms = get_the_terms( $post, 'wp_theme' );
|
$terms = get_the_terms( $post, 'wp_theme' );
|
||||||
|
|
||||||
if ( is_wp_error( $terms ) ) {
|
if ( is_wp_error( $terms ) ) {
|
||||||
@@ -44,16 +64,16 @@ class Utils {
|
|||||||
return new WP_Error( 'template_missing_theme', 'No theme is defined for this template.' );
|
return new WP_Error( 'template_missing_theme', 'No theme is defined for this template.' );
|
||||||
}
|
}
|
||||||
|
|
||||||
$templatePrefix = $terms[0]->name;
|
$template_prefix = $terms[0]->name;
|
||||||
$templateSlug = $post->post_name;
|
$template_slug = $post->post_name;
|
||||||
$templateId = $templatePrefix . '//' . $templateSlug;
|
$template_id = $template_prefix . '//' . $template_slug;
|
||||||
|
|
||||||
$template = new WP_Block_Template();
|
$template = new WP_Block_Template();
|
||||||
$template->wp_id = $post->ID;
|
$template->wp_id = $post->ID;
|
||||||
$template->id = $templateId;
|
$template->id = $template_id;
|
||||||
$template->theme = $templatePrefix;
|
$template->theme = $template_prefix;
|
||||||
$template->content = $post->post_content ? $post->post_content : '<p>empty</p>';
|
$template->content = $post->post_content ? $post->post_content : '<p>empty</p>';
|
||||||
$template->slug = $templateSlug;
|
$template->slug = $template_slug;
|
||||||
$template->source = 'custom';
|
$template->source = 'custom';
|
||||||
$template->type = $post->post_type;
|
$template->type = $post->post_type;
|
||||||
$template->description = $post->post_excerpt;
|
$template->description = $post->post_excerpt;
|
||||||
@@ -74,19 +94,25 @@ class Utils {
|
|||||||
return $template;
|
return $template;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function buildBlockTemplateFromFile( $templateObject ): WP_Block_Template {
|
/**
|
||||||
|
* Build a block template from a file.
|
||||||
|
*
|
||||||
|
* @param object $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 = new WP_Block_Template();
|
||||||
$template->id = $templateObject->id;
|
$template->id = $template_object->id;
|
||||||
$template->theme = $templateObject->theme;
|
$template->theme = $template_object->theme;
|
||||||
$template->content = (string) file_get_contents( $templateObject->path );
|
$template->content = (string) file_get_contents( $template_object->path );
|
||||||
$template->source = $templateObject->source;
|
$template->source = $template_object->source;
|
||||||
$template->slug = $templateObject->slug;
|
$template->slug = $template_object->slug;
|
||||||
$template->type = $templateObject->type;
|
$template->type = $template_object->type;
|
||||||
$template->title = $templateObject->title;
|
$template->title = $template_object->title;
|
||||||
$template->description = $templateObject->description;
|
$template->description = $template_object->description;
|
||||||
$template->status = 'publish';
|
$template->status = 'publish';
|
||||||
$template->has_theme_file = false;
|
$template->has_theme_file = false;
|
||||||
$template->post_types = $templateObject->post_types;
|
$template->post_types = $template_object->post_types;
|
||||||
$template->is_custom = false; // Templates are only custom if they are loaded from the DB.
|
$template->is_custom = false; // Templates are only custom if they are loaded from the DB.
|
||||||
$template->area = 'uncategorized';
|
$template->area = 'uncategorized';
|
||||||
return $template;
|
return $template;
|
||||||
|
Reference in New Issue
Block a user