diff --git a/assets/js/gutenberg.js b/assets/js/gutenberg.js new file mode 100644 index 0000000000000000000000000000000000000000..b5cd10acd056b076eefda9f9fdfa24defaff6e88 --- /dev/null +++ b/assets/js/gutenberg.js @@ -0,0 +1,29 @@ +jQuery( document ).ready(function() { + jQuery('.wpdesk').each(function( index ) { + var classList = jQuery(this).attr('class').split(/\s+/); + var type = ''; + jQuery.each(classList, function(index, item) { + if (item.startsWith('notice-')) { + type = item.replace('notice-',''); + } + }); + content = this.innerText; + actions = []; + jQuery.each(jQuery(this).find('a'), function(index, item) { + text = item.innerText; + actions.push({ + url: item.href, + label: text.charAt(0).toUpperCase() + text.slice(1), + }); + }); + isDismiss = jQuery(this).hasClass('is-dismissible'); + window.wp.data.dispatch( 'core/notices' ).createNotice( + type, + content, + { + isDismissible: isDismiss, + actions: actions + } + ); + }); +} ); diff --git a/src/WPDesk/Notice/Notice.php b/src/WPDesk/Notice/Notice.php index b3e69cb13cc23c3b8514647bf103f124d59d0d0f..9b3785696ae4b9aa87cf65dd83e388edfdf6985d 100644 --- a/src/WPDesk/Notice/Notice.php +++ b/src/WPDesk/Notice/Notice.php @@ -83,6 +83,18 @@ class Notice $this->addAction(); } + /** + * @return bool + */ + public function isBlockEditor() + { + if ( !function_exists( 'get_current_screen' ) ) { + require_once ABSPATH . '/wp-admin/includes/screen.php'; + } + + return \get_current_screen()->is_block_editor(); + } + /** * @return string */ @@ -163,6 +175,7 @@ class Notice [$this, 'showNotice'], self::ADMIN_FOOTER_BASE_PRIORITY + intval($this->priority) ); + add_action('admin_head', [$this,'addGutenbergScript']); $this->actionAdded = true; } } @@ -183,6 +196,16 @@ class Notice } } + /** + * Enqueue admin scripts. + */ + public function addGutenbergScript() + { + if ($this->isBlockEditor()) { + include_once 'views/admin-head-js-gutenberg.php'; + } + } + /** * Add attribute. * @@ -202,9 +225,9 @@ class Notice protected function getNoticeClass() { if ('updated' === $this->noticeType) { - $notice_class = 'notice ' . $this->noticeType; + $notice_class = 'wpdesk notice ' . $this->noticeType; } else { - $notice_class = 'notice notice-' . $this->noticeType; + $notice_class = 'wpdesk notice notice-' . $this->noticeType; } if ($this->dismissible) { $notice_class .= ' is-dismissible'; diff --git a/src/WPDesk/Notice/views/admin-head-js-gutenberg.php b/src/WPDesk/Notice/views/admin-head-js-gutenberg.php new file mode 100644 index 0000000000000000000000000000000000000000..4cc3d1227a097dfc28880af3bec840771a45a6b0 --- /dev/null +++ b/src/WPDesk/Notice/views/admin-head-js-gutenberg.php @@ -0,0 +1,8 @@ +<?php +if ( ! defined( 'ABSPATH' ) ) { + exit; +} // Exit if accessed directly +?> +<script type="text/javascript"> + <?php include dirname(__FILE__) . '/../../../../assets/js/gutenberg.js'; ?> +</script>