Commit a0df45bb396 for woocommerce
commit a0df45bb396d259632e8b7320b4c4ed493144c4f
Author: Seun Olorunsola <30554163+triple0t@users.noreply.github.com>
Date: Thu Mar 12 12:15:37 2026 +0100
Fix woo_email post deletion not cleaning up email template options in WP-CLI (#63500)
Move the `before_delete_post` hook registration from `register_hooks()`
to `init()` so it fires in non-admin contexts such as WP-CLI. Previously,
the hook was only registered inside `register_hooks()`, which is called
from `initialize()` via the `woocommerce_init` action — a chain that
requires the full editor init and only runs in admin. By registering the
cleanup hook early and unconditionally in `init()`, the associated
`woocommerce_email_templates_*_post_id` options are now properly deleted
when `woo_email` posts are removed via WP-CLI or other non-admin paths.
Made-with: Cursor
diff --git a/plugins/woocommerce/changelog/wooprd-2809-woo_email-posts-deleted-via-wp-cli-dont-clean-up b/plugins/woocommerce/changelog/wooprd-2809-woo_email-posts-deleted-via-wp-cli-dont-clean-up
new file mode 100644
index 00000000000..c77d4118032
--- /dev/null
+++ b/plugins/woocommerce/changelog/wooprd-2809-woo_email-posts-deleted-via-wp-cli-dont-clean-up
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Make sure that woocommerce_email_templates_*_post_id options are properly deleted when woo_email posts are removed in non-admin environments, such as WP-CLI.
diff --git a/plugins/woocommerce/src/Internal/EmailEditor/Integration.php b/plugins/woocommerce/src/Internal/EmailEditor/Integration.php
index dcc7f8f73d2..cfa01a187fb 100644
--- a/plugins/woocommerce/src/Internal/EmailEditor/Integration.php
+++ b/plugins/woocommerce/src/Internal/EmailEditor/Integration.php
@@ -78,6 +78,11 @@ class Integration {
}
add_action( 'woocommerce_init', array( $this, 'initialize' ) );
+
+ // Register the post deletion cleanup hook early and unconditionally so it works in
+ // both admin and non-admin contexts (e.g. WP-CLI). This only needs $wpdb and the
+ // posts manager singleton — it must not depend on the full editor init chain.
+ add_action( 'before_delete_post', array( $this, 'delete_email_template_associated_with_email_editor_post' ), 10, 2 );
}
/**
@@ -133,7 +138,6 @@ class Integration {
add_filter( 'woocommerce_email_editor_post_types', array( $this, 'add_email_post_type' ) );
add_filter( 'woocommerce_is_email_editor_page', array( $this, 'is_editor_page' ), 10, 1 );
add_filter( 'replace_editor', array( $this, 'replace_editor' ), 10, 2 );
- add_action( 'before_delete_post', array( $this, 'delete_email_template_associated_with_email_editor_post' ), 10, 2 );
add_filter( 'woocommerce_email_editor_send_preview_email_rendered_data', array( $this, 'update_send_preview_email_rendered_data' ), 10, 2 );
add_filter( 'woocommerce_email_editor_send_preview_email_personalizer_context', array( $this, 'update_send_preview_email_personalizer_context' ) );
add_filter( 'woocommerce_email_editor_preview_post_template_html', array( $this, 'update_preview_post_template_html_data' ), 100, 1 );