diff --git a/CHANGELOG.md b/CHANGELOG.md index 84c9df945fc00bf588ab38e0697ec9244667687b..60e4e11cffd6dc47a350483cd6cc4d16596bf57e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [3.2.0] - 2022-05-27 +### Added +- support to gutenberg + ## [3.1.4] - 2021-09-08 ### Changed - allow wpdesk/wp-builder 2.0 diff --git a/assets/js/gutenberg.js b/assets/js/gutenberg.js new file mode 100644 index 0000000000000000000000000000000000000000..578034736b1742d0d28986c425a20537956f952e --- /dev/null +++ b/assets/js/gutenberg.js @@ -0,0 +1,29 @@ +jQuery( document ).ready(function() { + jQuery('.wpdesk-notice-gutenberg').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/composer.json b/composer.json index 459614ff4fc8cf37d1035eb8d05343bb33159327..944275dd5083028095f701f177d82988bc2e8e20 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,11 @@ "email": "grola@wpdesk.net" } ], + "config": { + "platform": { + "php": "7.0" + } + }, "require": { "php": ">=5.5", "wpdesk/wp-builder": "^1.0|^2.0" diff --git a/src/WPDesk/Notice/AjaxHandler.php b/src/WPDesk/Notice/AjaxHandler.php index 4b2648d36713257d9fa291fd968f40cf301d4fef..48e7e0bc39b3fe2dd5e0c7907b30fabc6dc62360 100644 --- a/src/WPDesk/Notice/AjaxHandler.php +++ b/src/WPDesk/Notice/AjaxHandler.php @@ -71,7 +71,7 @@ class AjaxHandler implements HookablePluginDependant */ public function addScriptToAdminHead() { - include 'views/admin-head-js.php'; + include __DIR__ . '/views/admin-head-js.php'; } /** diff --git a/src/WPDesk/Notice/Notice.php b/src/WPDesk/Notice/Notice.php index b3e69cb13cc23c3b8514647bf103f124d59d0d0f..2a6f3349d0c7ceb5c220ee08d0f75c02e5e240e7 100644 --- a/src/WPDesk/Notice/Notice.php +++ b/src/WPDesk/Notice/Notice.php @@ -58,6 +58,12 @@ class Notice */ protected $attributes = array(); + /** + * Show notice in gutenberg editor. + * + * @var bool + */ + protected $showInGutenberg = false; /** * WPDesk_Flexible_Shipping_Notice constructor. @@ -67,22 +73,37 @@ class Notice * @param bool $dismissible Is dismissible. * @param int $priority Notice priority. * @param array $attributes Attributes. + * @param bool $showInGutenberg Show notice in gutenberg editor. */ public function __construct( $noticeContent, $noticeType = 'info', $dismissible = false, $priority = 10, - $attributes = array() + $attributes = array(), + $showInGutenberg = false ) { - $this->noticeContent = $noticeContent; - $this->noticeType = $noticeType; - $this->dismissible = $dismissible; - $this->priority = $priority; - $this->attributes = $attributes; + $this->noticeContent = $noticeContent; + $this->noticeType = $noticeType; + $this->dismissible = $dismissible; + $this->priority = $priority; + $this->attributes = $attributes; + $this->showInGutenberg = $showInGutenberg; $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 +184,7 @@ class Notice [$this, 'showNotice'], self::ADMIN_FOOTER_BASE_PRIORITY + intval($this->priority) ); + add_action('admin_head', [$this,'addGutenbergScript']); $this->actionAdded = true; } } @@ -183,6 +205,16 @@ class Notice } } + /** + * Enqueue admin scripts. + */ + public function addGutenbergScript() + { + if ($this->isBlockEditor()) { + include_once __DIR__ . '/views/admin-head-js-gutenberg.php'; + } + } + /** * Add attribute. * @@ -201,18 +233,23 @@ class Notice */ protected function getNoticeClass() { + $notice_classes = ['notice']; if ('updated' === $this->noticeType) { - $notice_class = 'notice ' . $this->noticeType; + $notice_classes[] = $this->noticeType; } else { - $notice_class = 'notice notice-' . $this->noticeType; + $notice_classes[] = 'notice-' . $this->noticeType; } if ($this->dismissible) { - $notice_class .= ' is-dismissible'; + $notice_classes[] = 'is-dismissible'; } if (isset($this->attributes['class'])) { - $notice_class .= ' ' . $this->attributes['class']; + $notice_classes[] = $this->attributes['class']; } - return $notice_class; + if ($this->showInGutenberg) { + $notice_classes[] = 'wpdesk-notice-gutenberg'; + } + + return implode( ' ', $notice_classes ); } /** diff --git a/src/WPDesk/Notice/PermanentDismissibleNotice.php b/src/WPDesk/Notice/PermanentDismissibleNotice.php index 210176f9e8620401e9995c6bace4c8a1f89e2f36..741b4c4e008da0431fd638a46639d3c1c4ae8700 100644 --- a/src/WPDesk/Notice/PermanentDismissibleNotice.php +++ b/src/WPDesk/Notice/PermanentDismissibleNotice.php @@ -30,17 +30,19 @@ class PermanentDismissibleNotice extends Notice * @param string $noticeContent Notice content. * @param string $noticeName Notice dismiss option name. * @param string $noticeType Notice type. - * @param int $priority Priority + * @param int $priority Priority * @param array $attributes Attributes. + * @param bool $showInGutenberg Show notice in gutenberg editor. */ public function __construct( $noticeContent, $noticeName, $noticeType = 'info', $priority = 10, - $attributes = array() + $attributes = array(), + $showInGutenberg = false ) { - parent::__construct($noticeContent, $noticeType, true, $priority, $attributes); + parent::__construct($noticeContent, $noticeType, true, $priority, $attributes, $showInGutenberg); $this->noticeName = $noticeName; $this->noticeDismissOptionName = static::OPTION_NAME_PREFIX . $noticeName; if (self::OPTION_VALUE_DISMISSED === get_option($this->noticeDismissOptionName, '')) { 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> diff --git a/src/WPDesk/Notice/views/admin-head-js.php b/src/WPDesk/Notice/views/admin-head-js.php index 0a8c0962bcffde6a3980d41ebcb2a4bca056f0c7..08532f1126aa095d02ba1560f714c6eace8555ca 100644 --- a/src/WPDesk/Notice/views/admin-head-js.php +++ b/src/WPDesk/Notice/views/admin-head-js.php @@ -5,5 +5,4 @@ if ( ! defined( 'ABSPATH' ) ) { ?> <script type="text/javascript"> <?php include dirname(__FILE__) . '/../../../../assets/js/notice.min.js'; ?> - </script> diff --git a/tests/integration/TestAjaxHandler.php b/tests/integration/TestAjaxHandler.php index 44c654068ced1cd5a39cf63c3ac25b73dfa5a30a..f930d5c566eb79c8e01eddf68fe381b70631cc53 100644 --- a/tests/integration/TestAjaxHandler.php +++ b/tests/integration/TestAjaxHandler.php @@ -61,9 +61,9 @@ class TestAjaxHandler extends WP_UnitTestCase $ajaxHandler->hooks(); $this->expectOutputString('<script type="text/javascript"> - jQuery(document).on("click",".notice-dismiss",function(){var a=jQuery(this).closest("div.notice").data("notice-name");var b=jQuery(this).closest("div.notice").data("source");if(""!==a){jQuery.ajax({url:ajaxurl,type:"post",data:{action:"wpdesk_notice_dismiss",notice_name:a,source:b},success:function(c){}})}});jQuery(document).on("click",".notice-dismiss-link",function(){jQuery(this).closest("div.notice").data("source",jQuery(this).data("source"));jQuery(this).closest("div.notice").find(".notice-dismiss").click()}); -</script> -'); + jQuery(document).on("click",".notice-dismiss",function(){var a=jQuery(this).closest("div.notice").data("notice-name");var b=jQuery(this).closest("div.notice").data("source");if(""!==a){jQuery.ajax({url:ajaxurl,type:"post",data:{action:"wpdesk_notice_dismiss",notice_name:a,source:b},success:function(c){}})}});jQuery(document).on("click",".notice-dismiss-link",function(){jQuery(this).closest("div.notice").data("source",jQuery(this).data("source"));jQuery(this).closest("div.notice").find(".notice-dismiss").click()});</script> +' + ); $ajaxHandler->addScriptToAdminHead(); } diff --git a/tests/integration/bootstrap.php b/tests/integration/bootstrap.php index 6d981d794a9bfd2bc854106c55f503dc79bb71ba..e2d6f4b62461ed3e36230bb4fa34f78708fe4c2f 100644 --- a/tests/integration/bootstrap.php +++ b/tests/integration/bootstrap.php @@ -27,5 +27,5 @@ tests_add_filter( 'muplugins_loaded', function () { //new \WPDesk\Notice\AjaxHandler( 'http://test.com/test/vendor/' ); putenv('WP_TESTS_DIR=' . getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit'); -require_once( getenv( 'WC_DEVELOP_DIR' ) . '/tests/bootstrap.php' ); +require_once( getenv( 'WC_DEVELOP_DIR' ) . '/tests/legacy/bootstrap.php' );