From cf9b0199319ec915b033eaea7b4282d7722e819c Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Wed, 17 Apr 2024 10:51:39 +0700 Subject: [PATCH] feat: enqueue mb assets in gutenberg screens --- inc/meta-box.php | 8 +++++++- js/validation/validation.js | 9 +++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/inc/meta-box.php b/inc/meta-box.php index 4aedbf57..75c54004 100644 --- a/inc/meta-box.php +++ b/inc/meta-box.php @@ -110,7 +110,7 @@ protected function object_hooks() { } public function enqueue() { - if ( is_admin() && ! $this->is_edit_screen() ) { + if ( is_admin() && ! $this->is_edit_screen() && ! $this->is_gutenberg_screen() ) { return; } @@ -149,6 +149,12 @@ public function enqueue() { do_action( 'rwmb_enqueue_scripts', $this ); } + private function is_gutenberg_screen() : bool { + global $pagenow; + + return in_array( $pagenow, ['site-editor.php', 'widgets.php'] ); + } + /** * Add meta box for multiple post types */ diff --git a/js/validation/validation.js b/js/validation/validation.js index 6aaee97b..59dc365d 100644 --- a/js/validation/validation.js +++ b/js/validation/validation.js @@ -216,8 +216,13 @@ class GutenbergValidation extends Validation { init() { var that = this, - editor = wp.data.dispatch( 'core/editor' ), - savePost = editor.savePost; // Reference original method. + editor = wp.data.dispatch( 'core/editor' ); + + if ( ! editor ) { + return false; + } + + const savePost = editor.savePost; // Reference original method. if ( that.settings ) { that.$form.validate( that.settings );