Skip to content
Snippets Groups Projects
Commit d7c257e6 authored by Grzegorz Rola's avatar Grzegorz Rola
Browse files

Feature/gutenberg

parent 73496eac
No related branches found
No related tags found
1 merge request!20Feature/gutenberg
## [3.2.0] - 2022-05-27
### Added
- support to gutenberg
## [3.1.4] - 2021-09-08 ## [3.1.4] - 2021-09-08
### Changed ### Changed
- allow wpdesk/wp-builder 2.0 - allow wpdesk/wp-builder 2.0
......
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
}
);
});
} );
...@@ -11,6 +11,11 @@ ...@@ -11,6 +11,11 @@
"email": "grola@wpdesk.net" "email": "grola@wpdesk.net"
} }
], ],
"config": {
"platform": {
"php": "7.0"
}
},
"require": { "require": {
"php": ">=5.5", "php": ">=5.5",
"wpdesk/wp-builder": "^1.0|^2.0" "wpdesk/wp-builder": "^1.0|^2.0"
......
...@@ -71,7 +71,7 @@ class AjaxHandler implements HookablePluginDependant ...@@ -71,7 +71,7 @@ class AjaxHandler implements HookablePluginDependant
*/ */
public function addScriptToAdminHead() public function addScriptToAdminHead()
{ {
include 'views/admin-head-js.php'; include __DIR__ . '/views/admin-head-js.php';
} }
/** /**
......
...@@ -58,6 +58,12 @@ class Notice ...@@ -58,6 +58,12 @@ class Notice
*/ */
protected $attributes = array(); protected $attributes = array();
/**
* Show notice in gutenberg editor.
*
* @var bool
*/
protected $showInGutenberg = false;
/** /**
* WPDesk_Flexible_Shipping_Notice constructor. * WPDesk_Flexible_Shipping_Notice constructor.
...@@ -67,22 +73,37 @@ class Notice ...@@ -67,22 +73,37 @@ class Notice
* @param bool $dismissible Is dismissible. * @param bool $dismissible Is dismissible.
* @param int $priority Notice priority. * @param int $priority Notice priority.
* @param array $attributes Attributes. * @param array $attributes Attributes.
* @param bool $showInGutenberg Show notice in gutenberg editor.
*/ */
public function __construct( public function __construct(
$noticeContent, $noticeContent,
$noticeType = 'info', $noticeType = 'info',
$dismissible = false, $dismissible = false,
$priority = 10, $priority = 10,
$attributes = array() $attributes = array(),
$showInGutenberg = false
) { ) {
$this->noticeContent = $noticeContent; $this->noticeContent = $noticeContent;
$this->noticeType = $noticeType; $this->noticeType = $noticeType;
$this->dismissible = $dismissible; $this->dismissible = $dismissible;
$this->priority = $priority; $this->priority = $priority;
$this->attributes = $attributes; $this->attributes = $attributes;
$this->showInGutenberg = $showInGutenberg;
$this->addAction(); $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 * @return string
*/ */
...@@ -163,6 +184,7 @@ class Notice ...@@ -163,6 +184,7 @@ class Notice
[$this, 'showNotice'], [$this, 'showNotice'],
self::ADMIN_FOOTER_BASE_PRIORITY + intval($this->priority) self::ADMIN_FOOTER_BASE_PRIORITY + intval($this->priority)
); );
add_action('admin_head', [$this,'addGutenbergScript']);
$this->actionAdded = true; $this->actionAdded = true;
} }
} }
...@@ -183,6 +205,16 @@ class Notice ...@@ -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. * Add attribute.
* *
...@@ -201,18 +233,23 @@ class Notice ...@@ -201,18 +233,23 @@ class Notice
*/ */
protected function getNoticeClass() protected function getNoticeClass()
{ {
$notice_classes = ['notice'];
if ('updated' === $this->noticeType) { if ('updated' === $this->noticeType) {
$notice_class = 'notice ' . $this->noticeType; $notice_classes[] = $this->noticeType;
} else { } else {
$notice_class = 'notice notice-' . $this->noticeType; $notice_classes[] = 'notice-' . $this->noticeType;
} }
if ($this->dismissible) { if ($this->dismissible) {
$notice_class .= ' is-dismissible'; $notice_classes[] = 'is-dismissible';
} }
if (isset($this->attributes['class'])) { 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 );
} }
/** /**
......
...@@ -32,15 +32,17 @@ class PermanentDismissibleNotice extends Notice ...@@ -32,15 +32,17 @@ class PermanentDismissibleNotice extends Notice
* @param string $noticeType Notice type. * @param string $noticeType Notice type.
* @param int $priority Priority * @param int $priority Priority
* @param array $attributes Attributes. * @param array $attributes Attributes.
* @param bool $showInGutenberg Show notice in gutenberg editor.
*/ */
public function __construct( public function __construct(
$noticeContent, $noticeContent,
$noticeName, $noticeName,
$noticeType = 'info', $noticeType = 'info',
$priority = 10, $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->noticeName = $noticeName;
$this->noticeDismissOptionName = static::OPTION_NAME_PREFIX . $noticeName; $this->noticeDismissOptionName = static::OPTION_NAME_PREFIX . $noticeName;
if (self::OPTION_VALUE_DISMISSED === get_option($this->noticeDismissOptionName, '')) { if (self::OPTION_VALUE_DISMISSED === get_option($this->noticeDismissOptionName, '')) {
......
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
?>
<script type="text/javascript">
<?php include dirname(__FILE__) . '/../../../../assets/js/gutenberg.js'; ?>
</script>
...@@ -5,5 +5,4 @@ if ( ! defined( 'ABSPATH' ) ) { ...@@ -5,5 +5,4 @@ if ( ! defined( 'ABSPATH' ) ) {
?> ?>
<script type="text/javascript"> <script type="text/javascript">
<?php include dirname(__FILE__) . '/../../../../assets/js/notice.min.js'; ?> <?php include dirname(__FILE__) . '/../../../../assets/js/notice.min.js'; ?>
</script> </script>
...@@ -61,9 +61,9 @@ class TestAjaxHandler extends WP_UnitTestCase ...@@ -61,9 +61,9 @@ class TestAjaxHandler extends WP_UnitTestCase
$ajaxHandler->hooks(); $ajaxHandler->hooks();
$this->expectOutputString('<script type="text/javascript"> $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()}); 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>
</script> '
'); );
$ajaxHandler->addScriptToAdminHead(); $ajaxHandler->addScriptToAdminHead();
} }
......
...@@ -27,5 +27,5 @@ tests_add_filter( 'muplugins_loaded', function () { ...@@ -27,5 +27,5 @@ tests_add_filter( 'muplugins_loaded', function () {
//new \WPDesk\Notice\AjaxHandler( 'http://test.com/test/vendor/' ); //new \WPDesk\Notice\AjaxHandler( 'http://test.com/test/vendor/' );
putenv('WP_TESTS_DIR=' . getenv( 'WP_DEVELOP_DIR' ) . '/tests/phpunit'); 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' );
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment