Logger
11 months ago
Patterns
11 months ago
PersonalizationTags
3 days ago
Renderer
3 days ago
Templates
4 months ago
class-assets-manager.php
5 months ago
class-dependency-check.php
11 months ago
class-email-api-controller.php
6 months ago
class-email-editor.php
3 days ago
class-email-styles-schema.php
11 months ago
class-personalizer.php
3 days ago
class-send-preview-email.php
5 months ago
class-settings-controller.php
3 months ago
class-site-style-sync-controller.php
4 months ago
class-theme-controller.php
3 months ago
class-user-theme.php
11 months ago
content-editor.css
2 weeks ago
content-shared.css
11 months ago
index.php
11 months ago
theme.json
3 months ago
class-user-theme.php
51 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Engine; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use WP_Post; |
| 6 | use WP_Theme_JSON; |
| 7 | class User_Theme { |
| 8 | private const USER_THEME_POST_NAME = 'wp-global-styles-woocommerce-email'; |
| 9 | private const INITIAL_THEME_DATA = array( |
| 10 | 'version' => 3, |
| 11 | 'isGlobalStylesUserThemeJSON' => true, |
| 12 | ); |
| 13 | private ?WP_Post $user_theme_post = null; |
| 14 | public function get_theme(): WP_Theme_JSON { |
| 15 | $post = $this->get_user_theme_post(); |
| 16 | $theme_data = json_decode( $post->post_content, true ); |
| 17 | if ( ! is_array( $theme_data ) ) { |
| 18 | $theme_data = self::INITIAL_THEME_DATA; |
| 19 | } |
| 20 | return new WP_Theme_JSON( $theme_data, 'custom' ); |
| 21 | } |
| 22 | public function get_user_theme_post(): WP_Post { |
| 23 | $this->ensure_theme_post(); |
| 24 | if ( ! $this->user_theme_post instanceof WP_Post ) { |
| 25 | throw new \Exception( 'Error creating user theme post' ); |
| 26 | } |
| 27 | return $this->user_theme_post; |
| 28 | } |
| 29 | private function ensure_theme_post(): void { |
| 30 | if ( $this->user_theme_post ) { |
| 31 | return; |
| 32 | } |
| 33 | $this->user_theme_post = get_page_by_path( self::USER_THEME_POST_NAME, OBJECT, 'wp_global_styles' ); |
| 34 | if ( $this->user_theme_post instanceof WP_Post ) { |
| 35 | return; |
| 36 | } |
| 37 | $post_data = array( |
| 38 | 'post_title' => __( 'Custom Email Styles', 'woocommerce' ), |
| 39 | 'post_name' => self::USER_THEME_POST_NAME, |
| 40 | 'post_content' => (string) wp_json_encode( self::INITIAL_THEME_DATA, JSON_FORCE_OBJECT ), |
| 41 | 'post_status' => 'publish', |
| 42 | 'post_type' => 'wp_global_styles', |
| 43 | ); |
| 44 | $post_id = wp_insert_post( $post_data ); |
| 45 | if ( is_wp_error( $post_id ) ) { |
| 46 | throw new \Exception( 'Error creating user theme post: ' . esc_html( $post_id->get_error_message() ) ); |
| 47 | } |
| 48 | $this->user_theme_post = get_post( $post_id ); |
| 49 | } |
| 50 | } |
| 51 |