TemplateApiController.php
5 months ago
TemplatesController.php
1 year ago
WooEmailTemplate.php
2 months ago
TemplatesController.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types=1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\EmailEditor\EmailTemplates; |
| 6 | |
| 7 | use Automattic\WooCommerce\EmailEditor\Engine\Templates\Template; |
| 8 | use Automattic\WooCommerce\EmailEditor\Engine\Templates\Templates_Registry; |
| 9 | use Automattic\WooCommerce\Internal\EmailEditor\Integration; |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Controller for managing WooCommerce email templates. |
| 15 | * |
| 16 | * @internal |
| 17 | */ |
| 18 | class TemplatesController { |
| 19 | |
| 20 | /** |
| 21 | * Prefix used for template identification. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | private string $template_prefix = 'woocommerce'; |
| 26 | |
| 27 | /** |
| 28 | * Initialize the controller by registering hooks. |
| 29 | * |
| 30 | * @internal |
| 31 | * @return void |
| 32 | */ |
| 33 | final public function init(): void { |
| 34 | add_filter( 'woocommerce_email_editor_register_templates', array( $this, 'register_templates' ) ); |
| 35 | // Priority 100 ensures this runs last to remove email templates from the Site Editor. |
| 36 | add_filter( 'get_block_templates', array( $this, 'filter_email_templates' ), 100, 1 ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Filters out email templates from the block templates list in the Site Editor. |
| 41 | * |
| 42 | * This function is necessary to prevent email templates from appearing in the Site Editor's |
| 43 | * template list. Email templates are stored in the database with the same post type as site |
| 44 | * templates, which causes them to be included in the Site Editor by default. By filtering |
| 45 | * them out, we ensure that only relevant site templates are displayed, improving the user |
| 46 | * experience and maintaining the intended separation between email and site templates. |
| 47 | * |
| 48 | * @param array $templates The list of block templates. |
| 49 | * @return array The filtered list of block templates. |
| 50 | */ |
| 51 | public function filter_email_templates( $templates ) { |
| 52 | // Skip filtering if we're in a REST API request to avoid affecting API endpoints. |
| 53 | if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 54 | return $templates; |
| 55 | } |
| 56 | |
| 57 | if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) { |
| 58 | return $templates; |
| 59 | } |
| 60 | |
| 61 | $current_screen = get_current_screen(); |
| 62 | if ( $current_screen && 'site-editor' === $current_screen->id ) { |
| 63 | $templates = array_filter( |
| 64 | $templates, |
| 65 | function ( $template ) { |
| 66 | return WooEmailTemplate::TEMPLATE_SLUG !== $template->slug; |
| 67 | } |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | return $templates; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Register WooCommerce email templates with the template registry. |
| 76 | * |
| 77 | * @param Templates_Registry $templates_registry The template registry instance. |
| 78 | * @return Templates_Registry |
| 79 | */ |
| 80 | public function register_templates( Templates_Registry $templates_registry ) { |
| 81 | $templates = array(); |
| 82 | $templates[] = new WooEmailTemplate(); |
| 83 | |
| 84 | foreach ( $templates as $template ) { |
| 85 | $the_template = new Template( |
| 86 | $this->template_prefix, |
| 87 | $template->get_slug(), |
| 88 | $template->get_title(), |
| 89 | $template->get_description(), |
| 90 | $template->get_content(), |
| 91 | array( Integration::EMAIL_POST_TYPE ) |
| 92 | ); |
| 93 | $templates_registry->register( $the_template ); |
| 94 | } |
| 95 | |
| 96 | return $templates_registry; |
| 97 | } |
| 98 | } |
| 99 |