diff --git a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EditorPageRenderer.php b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EditorPageRenderer.php index be76ff26b7..2f734be3bf 100644 --- a/mailpoet/lib/EmailEditor/Integrations/MailPoet/EditorPageRenderer.php +++ b/mailpoet/lib/EmailEditor/Integrations/MailPoet/EditorPageRenderer.php @@ -75,7 +75,9 @@ class EditorPageRenderer { public function render() { $postId = isset($_GET['post']) ? intval($_GET['post']) : 0; $post = $this->wp->getPost($postId); - if (!$post instanceof \WP_Post || $post->post_type !== EditorInitController::MAILPOET_EMAIL_POST_TYPE) { // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + $currentPostType = $post->post_type; // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps + + if (!$post instanceof \WP_Post || $currentPostType !== EditorInitController::MAILPOET_EMAIL_POST_TYPE) { return; } $newsletter = $this->newslettersRepository->findOneBy(['wpPost' => $postId]); @@ -142,6 +144,8 @@ class EditorPageRenderer { 'mailpoet_email_editor', 'MailPoetEmailEditor', [ + 'current_post_type' => esc_js($currentPostType), + 'current_post_id' => $post->ID, 'json_api_root' => esc_js($jsonAPIRoot), 'api_token' => esc_js($token), 'api_version' => esc_js($apiVersion), diff --git a/packages/js/email-editor/src/global.d.ts b/packages/js/email-editor/src/global.d.ts index 8fe992d9c4..b85ece3a7b 100644 --- a/packages/js/email-editor/src/global.d.ts +++ b/packages/js/email-editor/src/global.d.ts @@ -14,7 +14,7 @@ interface Window { email_styles: unknown; // Can't import type in global.d.ts. Typed in getEmailStyles() in store/settings.ts editor_layout: unknown; // Can't import type in global.d.ts. Typed in getEmailLayout() in store/settings.ts editor_theme: unknown; // Can't import type in global.d.ts. Typed in getEditorTheme() in store/settings.ts + current_post_type: string; + current_post_id: string; }; - mailpoet_email_editor_current_post_type: string; - mailpoet_email_editor_current_post_id: number; } diff --git a/packages/js/email-editor/src/store/constants.ts b/packages/js/email-editor/src/store/constants.ts index 765e3aa0b6..35eba6198b 100644 --- a/packages/js/email-editor/src/store/constants.ts +++ b/packages/js/email-editor/src/store/constants.ts @@ -5,6 +5,10 @@ export const mainSidebarDocumentTab = 'document'; export const mainSidebarBlockTab = 'block'; export const stylesSidebarId = 'email-editor/editor/styles'; +// these values are set once on a page load, so it's fine to keep them here. export const editorCurrentPostType = - window.mailpoet_email_editor_current_post_type; -export const editorCurrentPostId = window.mailpoet_email_editor_current_post_id; + window.MailPoetEmailEditor.current_post_type; +export const editorCurrentPostId = parseInt( + window.MailPoetEmailEditor.current_post_id, + 10 +); diff --git a/packages/php/email-editor/src/Engine/class-email-editor.php b/packages/php/email-editor/src/Engine/class-email-editor.php index 82fa6c6f76..0feb89c47a 100644 --- a/packages/php/email-editor/src/Engine/class-email-editor.php +++ b/packages/php/email-editor/src/Engine/class-email-editor.php @@ -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' ) ); - } - - ?> - -