Add User_Theme class for managing user-defined styles and settings
[MAILPOET-6335]
This commit is contained in:
committed by
Aschepikov
parent
de03e84eb4
commit
0f7009438c
95
packages/php/email-editor/src/Engine/class-user-theme.php
Normal file
95
packages/php/email-editor/src/Engine/class-user-theme.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the MailPoet Email Editor package.
|
||||
*
|
||||
* @package MailPoet\EmailEditor
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
namespace MailPoet\EmailEditor\Engine;
|
||||
|
||||
use WP_Post;
|
||||
use WP_Theme_JSON;
|
||||
|
||||
/**
|
||||
* This class is responsible for managing and accessing theme data aka email styles created by users.
|
||||
*/
|
||||
class User_Theme {
|
||||
private const USER_THEME_POST_NAME = 'wp-global-styles-mailpoet-email';
|
||||
private const INITIAL_THEME_DATA = array(
|
||||
'version' => 3,
|
||||
'isGlobalStylesUserThemeJSON' => true,
|
||||
);
|
||||
|
||||
/**
|
||||
* Core theme loaded from the WordPress core.
|
||||
*
|
||||
* @var WP_Post | null
|
||||
*/
|
||||
private ?WP_Post $user_theme_post = null;
|
||||
|
||||
/**
|
||||
* Getter for user theme.
|
||||
*
|
||||
* @throws \Exception If the user theme post cannot be created.
|
||||
* @return WP_Theme_JSON
|
||||
*/
|
||||
public function get_theme(): WP_Theme_JSON {
|
||||
$post = $this->get_user_theme_post();
|
||||
$theme_data = json_decode( $post->post_content, true );
|
||||
if ( ! is_array( $theme_data ) ) {
|
||||
$theme_data = self::INITIAL_THEME_DATA;
|
||||
}
|
||||
return new WP_Theme_JSON( $theme_data, 'custom' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter for user theme post.
|
||||
* If the post does not exist, it will be created.
|
||||
*
|
||||
* @throws \Exception If the user theme post cannot be created.
|
||||
* @return WP_Post
|
||||
*/
|
||||
public function get_user_theme_post(): WP_Post {
|
||||
$this->ensure_theme_post();
|
||||
if ( ! $this->user_theme_post instanceof WP_Post ) {
|
||||
throw new \Exception( 'Error creating user theme post' );
|
||||
}
|
||||
return $this->user_theme_post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the user theme post exists and is loaded.
|
||||
*
|
||||
* @throws \Exception If the user theme post cannot be created.
|
||||
*/
|
||||
private function ensure_theme_post(): void {
|
||||
if ( $this->user_theme_post ) {
|
||||
return;
|
||||
}
|
||||
$this->user_theme_post = get_page_by_path( self::USER_THEME_POST_NAME, OBJECT, 'wp_global_styles' );
|
||||
if ( $this->user_theme_post instanceof WP_Post ) {
|
||||
return;
|
||||
}
|
||||
$post_data = array(
|
||||
'post_title' => __( 'Custom Email Styles', 'mailpoet' ),
|
||||
'post_name' => self::USER_THEME_POST_NAME,
|
||||
'post_content' => (string) wp_json_encode( self::INITIAL_THEME_DATA, JSON_FORCE_OBJECT ),
|
||||
'post_status' => 'publish',
|
||||
'post_type' => 'wp_global_styles',
|
||||
);
|
||||
|
||||
/**
|
||||
* The doc is needed since PHPStan thinks that wp_insert_post can't return WP_Error.
|
||||
*
|
||||
* @var int|\WP_Error $post_id
|
||||
*/
|
||||
$post_id = wp_insert_post( $post_data );
|
||||
|
||||
if ( is_wp_error( $post_id ) ) {
|
||||
throw new \Exception( 'Error creating user theme post: ' . esc_html( $post_id->get_error_message() ) );
|
||||
}
|
||||
|
||||
$this->user_theme_post = get_post( $post_id );
|
||||
}
|
||||
}
|
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of the MailPoet Email Editor package.
|
||||
*
|
||||
* @package MailPoet\EmailEditor
|
||||
*/
|
||||
|
||||
declare(strict_types = 1);
|
||||
namespace MailPoet\EmailEditor\Engine;
|
||||
|
||||
/**
|
||||
* Integration test for User Theme class
|
||||
*/
|
||||
class User_Theme_Test extends \MailPoetTest {
|
||||
/**
|
||||
* Instance of User_Theme created before each test.
|
||||
*
|
||||
* @var User_Theme
|
||||
*/
|
||||
private User_Theme $user_theme;
|
||||
|
||||
/**
|
||||
* Set up before each test.
|
||||
*/
|
||||
protected function _before(): void {
|
||||
parent::_before();
|
||||
$this->user_theme = new User_Theme();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it can create and return a user theme post
|
||||
*/
|
||||
public function testItCreatesUserThemePostLazily(): void {
|
||||
$post = $this->user_theme->get_user_theme_post();
|
||||
$this->assertInstanceOf( \WP_Post::class, $post );
|
||||
$post_content = json_decode( $post->post_content, true );
|
||||
$this->assertIsArray( $post_content );
|
||||
$this->assertArrayHasKey( 'version', $post_content );
|
||||
$this->assertEquals( 3, $post_content['version'] );
|
||||
$this->assertArrayHasKey( 'isGlobalStylesUserThemeJSON', $post_content );
|
||||
$this->assertTrue( $post_content['isGlobalStylesUserThemeJSON'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it fetches previously stored data
|
||||
*/
|
||||
public function testItFetchesPreviouslyStoredData(): void {
|
||||
$styles_data = array(
|
||||
'version' => 3,
|
||||
'isGlobalStylesUserThemeJSON' => true,
|
||||
'styles' => array(
|
||||
'color' => array(
|
||||
'background' => '#000000',
|
||||
'text' => '#ffffff',
|
||||
),
|
||||
),
|
||||
);
|
||||
$post_data = array(
|
||||
'post_title' => __( 'Custom Email Styles', 'mailpoet' ),
|
||||
'post_name' => 'wp-global-styles-mailpoet-email',
|
||||
'post_content' => (string) wp_json_encode( $styles_data, JSON_FORCE_OBJECT ),
|
||||
'post_status' => 'publish',
|
||||
'post_type' => 'wp_global_styles',
|
||||
);
|
||||
wp_insert_post( $post_data );
|
||||
|
||||
$post = $this->user_theme->get_user_theme_post();
|
||||
$this->assertInstanceOf( \WP_Post::class, $post );
|
||||
$post_content = json_decode( $post->post_content, true );
|
||||
$this->assertIsArray( $post_content );
|
||||
$this->assertArrayHasKey( 'version', $post_content );
|
||||
$this->assertEquals( 3, $post_content['version'] );
|
||||
$this->assertArrayHasKey( 'isGlobalStylesUserThemeJSON', $post_content );
|
||||
$this->assertTrue( $post_content['isGlobalStylesUserThemeJSON'] );
|
||||
$this->assertEquals( $styles_data['styles'], $post_content['styles'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test it returns the user WP_Theme_JSON
|
||||
*/
|
||||
public function testItCreatesThemeJson(): void {
|
||||
$theme = $this->user_theme->get_theme();
|
||||
$this->assertInstanceOf( \WP_Theme_JSON::class, $theme );
|
||||
$raw = $theme->get_raw_data();
|
||||
$this->assertArrayHasKey( 'version', $raw );
|
||||
}
|
||||
}
|
@@ -28,6 +28,7 @@ use MailPoet\EmailEditor\Engine\Templates\Template_Preview;
|
||||
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;
|
||||
use MailPoet\EmailEditor\Integrations\MailPoet\Blocks\BlockTypesController;
|
||||
use MailPoet\EmailEditor\Engine\Send_Preview_Email;
|
||||
@@ -170,6 +171,12 @@ abstract class MailPoetTest extends \Codeception\TestCase\Test { // phpcs:ignore
|
||||
return new Theme_Controller();
|
||||
}
|
||||
);
|
||||
$container->set(
|
||||
User_Theme::class,
|
||||
function () {
|
||||
return new User_Theme();
|
||||
}
|
||||
);
|
||||
$container->set(
|
||||
Settings_Controller::class,
|
||||
function ( $container ) {
|
||||
|
Reference in New Issue
Block a user