class-template.php
11 months ago
class-templates-registry.php
11 months ago
class-templates.php
4 months ago
email-general.html
7 months ago
index.php
11 months ago
single-email-post-template.php
11 months ago
class-templates.php
92 lines
| 1 | <?php |
| 2 | declare(strict_types = 1); |
| 3 | namespace Automattic\WooCommerce\EmailEditor\Engine\Templates; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use Automattic\WooCommerce\EmailEditor\Validator\Builder; |
| 6 | use WP_Block_Template; |
| 7 | class Templates { |
| 8 | private string $template_prefix = 'woocommerce'; |
| 9 | private array $post_types = array(); |
| 10 | private string $template_directory = __DIR__ . DIRECTORY_SEPARATOR; |
| 11 | private Templates_Registry $templates_registry; |
| 12 | public function __construct( Templates_Registry $templates_registry ) { |
| 13 | $this->templates_registry = $templates_registry; |
| 14 | } |
| 15 | public function initialize( array $post_types ): void { |
| 16 | $this->post_types = $post_types; |
| 17 | add_filter( 'theme_templates', array( $this, 'add_theme_templates' ), 10, 4 ); // Workaround needed when saving post – template association. |
| 18 | add_filter( 'woocommerce_email_editor_register_templates', array( $this, 'register_templates' ) ); |
| 19 | $this->templates_registry->initialize(); |
| 20 | $this->register_post_types_to_api(); |
| 21 | } |
| 22 | public function get_block_template( $template_slug ) { |
| 23 | // Template id is always prefixed by active theme and get_stylesheet returns the active theme slug. |
| 24 | $template_id = get_stylesheet() . '//' . $template_slug; |
| 25 | return get_block_template( $template_id ); |
| 26 | } |
| 27 | public function register_templates( Templates_Registry $templates_registry ): Templates_Registry { |
| 28 | // Register basic blank template. |
| 29 | $general_email_slug = 'email-general'; |
| 30 | $template_filename = $general_email_slug . '.html'; |
| 31 | $general_email = new Template( |
| 32 | $this->template_prefix, |
| 33 | $general_email_slug, |
| 34 | __( 'General Email', 'woocommerce' ), |
| 35 | __( 'A general template for emails.', 'woocommerce' ), |
| 36 | (string) file_get_contents( $this->template_directory . $template_filename ), |
| 37 | $this->post_types |
| 38 | ); |
| 39 | $templates_registry->register( $general_email ); |
| 40 | return $templates_registry; |
| 41 | } |
| 42 | public function register_post_types_to_api(): void { |
| 43 | $controller = new \WP_REST_Templates_Controller( 'wp_template' ); |
| 44 | $schema = $controller->get_item_schema(); |
| 45 | // Skip registration only when post_types is natively available in the view context. |
| 46 | // WP 7.0 adds the property but only in the edit context; we must still register |
| 47 | // the field so that context=view responses include it. |
| 48 | $post_types_context = $schema['properties']['post_types']['context'] ?? array(); |
| 49 | if ( in_array( 'view', $post_types_context, true ) ) { |
| 50 | return; |
| 51 | } |
| 52 | register_rest_field( |
| 53 | 'wp_template', |
| 54 | 'post_types', |
| 55 | array( |
| 56 | 'get_callback' => array( $this, 'get_post_types' ), |
| 57 | 'update_callback' => null, |
| 58 | 'schema' => Builder::string()->to_array(), |
| 59 | ) |
| 60 | ); |
| 61 | } |
| 62 | public function get_post_types( $response_object ): array { |
| 63 | $template = $this->templates_registry->get_by_slug( $response_object['slug'] ?? '' ); |
| 64 | if ( $template ) { |
| 65 | return $template->get_post_types(); |
| 66 | } |
| 67 | return $response_object['post_types'] ?? array(); |
| 68 | } |
| 69 | public function add_theme_templates( $templates, $theme, $post, $post_type ) { |
| 70 | if ( $post_type && ! in_array( $post_type, $this->post_types, true ) ) { |
| 71 | return $templates; |
| 72 | } |
| 73 | $block_templates = get_block_templates(); |
| 74 | $email_templates_slugs = array_map( |
| 75 | function ( Template $template ) { |
| 76 | return $template->get_slug(); |
| 77 | }, |
| 78 | $this->templates_registry->get_all() |
| 79 | ); |
| 80 | foreach ( $block_templates as $block_template ) { |
| 81 | if ( ! in_array( $block_template->slug, $email_templates_slugs, true ) ) { |
| 82 | continue; |
| 83 | } |
| 84 | if ( isset( $templates[ $block_template->slug ] ) ) { |
| 85 | continue; |
| 86 | } |
| 87 | $templates[ $block_template->slug ] = $block_template->title; // Requires only the template title, not the full template object. |
| 88 | } |
| 89 | return $templates; |
| 90 | } |
| 91 | } |
| 92 |