Migrate variable from email editor core to MailPoet integration.

The information is already available within the MailPoet integration scope, moving the variable declaration closer.

MAILPOET-6430
This commit is contained in:
Oluwaseun Olorunsola
2025-01-30 09:29:02 +01:00
committed by Oluwaseun Olorunsola
parent bf5cde8363
commit 1cb5eda659
4 changed files with 32 additions and 32 deletions

View File

@@ -105,7 +105,6 @@ class Email_Editor {
if ( $is_editor_page ) {
$this->extend_email_post_api();
$this->settings_controller->init();
add_filter( 'admin_footer', array( $this, 'load_js_vars' ), 24 ); // @phpstan-ignore-line -- Filter callback return statement is missing.
}
add_action( 'rest_api_init', array( $this, 'register_email_editor_api_routes' ) );
add_filter( 'mailpoet_email_editor_send_preview_email', array( $this->send_preview_email, 'send_preview_email' ), 11, 1 ); // allow for other filter methods to take precedent.
@@ -270,6 +269,20 @@ class Email_Editor {
return $theme;
}
/**
* Get the current post object
*
* @return array|mixed|WP_Post|null
*/
public function get_current_post() {
if ( isset( $_GET['post'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$current_post = get_post( intval( $_GET['post'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- data valid
} else {
$current_post = $GLOBALS['post'];
}
return $current_post;
}
/**
* Use a custom page template for the email editor frontend rendering.
*
@@ -277,7 +290,11 @@ class Email_Editor {
* @return string
*/
public function load_email_preview_template( string $template ): string {
global $post;
$post = $this->get_current_post();
if ( ! $post instanceof \WP_Post ) {
return $template;
}
$current_post_type = $post->post_type;
@@ -297,29 +314,4 @@ class Email_Editor {
return __DIR__ . '/Templates/single-email-post-template.php';
}
/**
* Load JS vars
*
* @return void
* @throws \InvalidArgumentException If the post-type is invalid.
*/
public function load_js_vars() {
global $post;
$email_editor_post_type_names = array_column( $this->get_post_types(), 'name' );
$email_editor_current_post_type = $post->post_type;
$current_post_is_email_editor_type = in_array( $email_editor_current_post_type, $email_editor_post_type_names, true );
if ( ! $current_post_is_email_editor_type ) {
throw new \InvalidArgumentException( esc_html__( 'Invalid email post type', 'mailpoet' ) );
}
?>
<script type="text/javascript"> <?php // phpcs:ignore ?>
window.mailpoet_email_editor_current_post_type = '<?php echo esc_js( $email_editor_current_post_type ); ?>';
window.mailpoet_email_editor_current_post_id = <?php echo esc_js( $post->ID ); ?>;
</script>
<?php
}
}