From d7c257e6e8a85f6c83946a703ae0dccb5fe60516 Mon Sep 17 00:00:00 2001 From: Grzegorz Rola <grola@seostudio.pl> Date: Mon, 6 Jun 2022 16:36:00 +0000 Subject: [PATCH] Feature/gutenberg --- CHANGELOG.md | 4 ++ assets/js/gutenberg.js | 29 +++++++++ composer.json | 5 ++ src/WPDesk/Notice/AjaxHandler.php | 2 +- src/WPDesk/Notice/Notice.php | 59 +++++++++++++++---- .../Notice/PermanentDismissibleNotice.php | 8 ++- .../Notice/views/admin-head-js-gutenberg.php | 8 +++ src/WPDesk/Notice/views/admin-head-js.php | 1 - tests/integration/TestAjaxHandler.php | 6 +- tests/integration/bootstrap.php | 2 +- 10 files changed, 104 insertions(+), 20 deletions(-) create mode 100644 assets/js/gutenberg.js create mode 100644 src/WPDesk/Notice/views/admin-head-js-gutenberg.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 84c9df9..60e4e11 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 0000000..5780347 --- /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 459614f..944275d 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 4b2648d..48e7e0b 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 b3e69cb..2a6f334 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 210176f..741b4c4 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 0000000..4cc3d12 --- /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 0a8c096..08532f1 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 44c6540..f930d5c 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 6d981d7..e2d6f4b 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' ); -- GitLab