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

@ -75,7 +75,9 @@ class EditorPageRenderer {
public function render() { public function render() {
$postId = isset($_GET['post']) ? intval($_GET['post']) : 0; $postId = isset($_GET['post']) ? intval($_GET['post']) : 0;
$post = $this->wp->getPost($postId); $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; return;
} }
$newsletter = $this->newslettersRepository->findOneBy(['wpPost' => $postId]); $newsletter = $this->newslettersRepository->findOneBy(['wpPost' => $postId]);
@ -142,6 +144,8 @@ class EditorPageRenderer {
'mailpoet_email_editor', 'mailpoet_email_editor',
'MailPoetEmailEditor', 'MailPoetEmailEditor',
[ [
'current_post_type' => esc_js($currentPostType),
'current_post_id' => $post->ID,
'json_api_root' => esc_js($jsonAPIRoot), 'json_api_root' => esc_js($jsonAPIRoot),
'api_token' => esc_js($token), 'api_token' => esc_js($token),
'api_version' => esc_js($apiVersion), 'api_version' => esc_js($apiVersion),

View File

@ -14,7 +14,7 @@ interface Window {
email_styles: unknown; // Can't import type in global.d.ts. Typed in getEmailStyles() in store/settings.ts 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_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 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;
} }

View File

@ -5,6 +5,10 @@ export const mainSidebarDocumentTab = 'document';
export const mainSidebarBlockTab = 'block'; export const mainSidebarBlockTab = 'block';
export const stylesSidebarId = 'email-editor/editor/styles'; 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 = export const editorCurrentPostType =
window.mailpoet_email_editor_current_post_type; window.MailPoetEmailEditor.current_post_type;
export const editorCurrentPostId = window.mailpoet_email_editor_current_post_id; export const editorCurrentPostId = parseInt(
window.MailPoetEmailEditor.current_post_id,
10
);

View File

@ -105,7 +105,6 @@ class Email_Editor {
if ( $is_editor_page ) { if ( $is_editor_page ) {
$this->extend_email_post_api(); $this->extend_email_post_api();
$this->settings_controller->init(); $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_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. 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; 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. * Use a custom page template for the email editor frontend rendering.
* *
@ -277,7 +290,11 @@ class Email_Editor {
* @return string * @return string
*/ */
public function load_email_preview_template( string $template ): 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; $current_post_type = $post->post_type;
@ -297,29 +314,4 @@ class Email_Editor {
return __DIR__ . '/Templates/single-email-post-template.php'; 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
}
} }