Logger
10 months ago
Patterns
10 months ago
PersonalizationTags
7 months ago
Renderer
4 weeks ago
Templates
2 months ago
class-assets-manager.php
4 months ago
class-dependency-check.php
1 year ago
class-email-api-controller.php
4 months ago
class-email-editor.php
4 months ago
class-email-styles-schema.php
1 year ago
class-personalizer.php
4 months ago
class-send-preview-email.php
3 months ago
class-settings-controller.php
2 months ago
class-site-style-sync-controller.php
2 months ago
class-theme-controller.php
2 months ago
class-user-theme.php
1 year ago
content-editor.css
9 months ago
content-shared.css
10 months ago
theme.json
2 months ago
class-user-theme.php
96 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This file is part of the WooCommerce Email Editor package. |
| 4 | * |
| 5 | * @package Automattic\WooCommerce\EmailEditor |
| 6 | */ |
| 7 | |
| 8 | declare(strict_types = 1); |
| 9 | namespace Automattic\WooCommerce\EmailEditor\Engine; |
| 10 | |
| 11 | use WP_Post; |
| 12 | use WP_Theme_JSON; |
| 13 | |
| 14 | /** |
| 15 | * This class is responsible for managing and accessing theme data aka email styles created by users. |
| 16 | */ |
| 17 | class User_Theme { |
| 18 | private const USER_THEME_POST_NAME = 'wp-global-styles-woocommerce-email'; |
| 19 | private const INITIAL_THEME_DATA = array( |
| 20 | 'version' => 3, |
| 21 | 'isGlobalStylesUserThemeJSON' => true, |
| 22 | ); |
| 23 | |
| 24 | /** |
| 25 | * Core theme loaded from the WordPress core. |
| 26 | * |
| 27 | * @var WP_Post | null |
| 28 | */ |
| 29 | private ?WP_Post $user_theme_post = null; |
| 30 | |
| 31 | /** |
| 32 | * Getter for user theme. |
| 33 | * |
| 34 | * @throws \Exception If the user theme post cannot be created. |
| 35 | * @return WP_Theme_JSON |
| 36 | */ |
| 37 | public function get_theme(): WP_Theme_JSON { |
| 38 | $post = $this->get_user_theme_post(); |
| 39 | $theme_data = json_decode( $post->post_content, true ); |
| 40 | if ( ! is_array( $theme_data ) ) { |
| 41 | $theme_data = self::INITIAL_THEME_DATA; |
| 42 | } |
| 43 | return new WP_Theme_JSON( $theme_data, 'custom' ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Getter for user theme post. |
| 48 | * If the post does not exist, it will be created. |
| 49 | * |
| 50 | * @throws \Exception If the user theme post cannot be created. |
| 51 | * @return WP_Post |
| 52 | */ |
| 53 | public function get_user_theme_post(): WP_Post { |
| 54 | $this->ensure_theme_post(); |
| 55 | if ( ! $this->user_theme_post instanceof WP_Post ) { |
| 56 | throw new \Exception( 'Error creating user theme post' ); |
| 57 | } |
| 58 | return $this->user_theme_post; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Ensures that the user theme post exists and is loaded. |
| 63 | * |
| 64 | * @throws \Exception If the user theme post cannot be created. |
| 65 | */ |
| 66 | private function ensure_theme_post(): void { |
| 67 | if ( $this->user_theme_post ) { |
| 68 | return; |
| 69 | } |
| 70 | $this->user_theme_post = get_page_by_path( self::USER_THEME_POST_NAME, OBJECT, 'wp_global_styles' ); |
| 71 | if ( $this->user_theme_post instanceof WP_Post ) { |
| 72 | return; |
| 73 | } |
| 74 | $post_data = array( |
| 75 | 'post_title' => __( 'Custom Email Styles', 'woocommerce' ), |
| 76 | 'post_name' => self::USER_THEME_POST_NAME, |
| 77 | 'post_content' => (string) wp_json_encode( self::INITIAL_THEME_DATA, JSON_FORCE_OBJECT ), |
| 78 | 'post_status' => 'publish', |
| 79 | 'post_type' => 'wp_global_styles', |
| 80 | ); |
| 81 | |
| 82 | /** |
| 83 | * The doc is needed since PHPStan thinks that wp_insert_post can't return WP_Error. |
| 84 | * |
| 85 | * @var int|\WP_Error $post_id |
| 86 | */ |
| 87 | $post_id = wp_insert_post( $post_data ); |
| 88 | |
| 89 | if ( is_wp_error( $post_id ) ) { |
| 90 | throw new \Exception( 'Error creating user theme post: ' . esc_html( $post_id->get_error_message() ) ); |
| 91 | } |
| 92 | |
| 93 | $this->user_theme_post = get_post( $post_id ); |
| 94 | } |
| 95 | } |
| 96 |