Skip to content
Snippets Groups Projects
Commit c9039431 authored by Marcin Kolanko's avatar Marcin Kolanko
Browse files

Merge branch 'bugfix/wordpress-review' into 'master'

Bugfix/wordpress review

See merge request !30
parents a59008f6 bc779625
No related branches found
No related tags found
1 merge request!30Bugfix/wordpress review
Pipeline #507680 failed
......@@ -4,4 +4,10 @@ variables:
DISABLE_CODECEPTION: 1
IS_LIBRARY: 1
include: 'https://gitlab.com/wpdesk/gitlab-ci/raw/master/gitlab-ci-1.2.yml'
include: 'https://gitlab.wpdesk.dev/wpdesk/gitlab-ci/raw/master/gitlab-ci-1.2.yml'
lint:
when: manual
phpcs:
allow_failure: true
## [3.3.0] - 2025-05-16
### Fixed
- Removing JavaScript code from the page content, adding JavaScript files via admin_enqueue_scripts
- Escaping
## [3.2.5] - 2024-07-23
### Fixed
- Hide Gutenberg-targeted notices in classic editor
......
.wpdesk-notice-gutenberg { display: none; }
\ No newline at end of file
jQuery( document ).ready(function() {
jQuery(document).on('click', '.notice-dismiss', function () {
const $notice_div= jQuery(this).closest('div.notice');
const notice_name = $notice_div.data('notice-name');
......@@ -23,3 +24,4 @@ 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();
});
} );
......@@ -22,6 +22,7 @@ class AjaxHandler implements HookablePluginDependant {
const SCRIPTS_VERSION = '4';
const SCRIPT_HANDLE = 'wpdesk_notice';
const SCRIPT_HANDLE_GUTENBERG = 'wpdesk_notice_gutenberg';
/**
* @var string
......@@ -34,21 +35,28 @@ class AjaxHandler implements HookablePluginDependant {
* @param string|null $assetsURL Assets URL.
*/
public function __construct( $assetsURL = null ) {
$this->assetsURL = $assetsURL;
$this->assetsURL = $assetsURL ?? plugins_url('/assets/', dirname(__FILE__, 3));
}
/**
* Hooks.
*/
public function hooks() {
if ( $this->assetsURL ) {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAdminScripts' ] );
} else {
add_action( 'admin_head', [ $this, 'addScriptToAdminHead' ] );
}
add_action( 'wp_ajax_wpdesk_notice_dismiss', [ $this, 'processAjaxNoticeDismiss' ] );
}
public function isBlockEditor():bool
{
if ( !function_exists( 'get_current_screen' ) ) {
return false;
}
$screen = \get_current_screen();
return is_object($screen) ? $screen->is_block_editor() : false;
}
/**
* Enqueue admin scripts.
*/
......@@ -60,13 +68,24 @@ class AjaxHandler implements HookablePluginDependant {
self::SCRIPTS_VERSION
);
wp_enqueue_script( self::SCRIPT_HANDLE );
}
/**
* Add Java Script to admin header.
*/
public function addScriptToAdminHead() {
include __DIR__ . '/views/admin-head-js.php';
if($this->isBlockEditor()){
wp_register_script(
self::SCRIPT_HANDLE_GUTENBERG,
trailingslashit( $this->assetsURL ) . 'js/gutenberg.js',
[ 'jquery' ],
self::SCRIPTS_VERSION
);
wp_enqueue_script( self::SCRIPT_HANDLE_GUTENBERG );
}else{
wp_register_style(
self::SCRIPT_HANDLE,
trailingslashit( $this->assetsURL ) . 'css/admin.css',
[],
self::SCRIPTS_VERSION
);
wp_enqueue_style( self::SCRIPT_HANDLE );
}
}
/**
......
......@@ -92,20 +92,6 @@ class Notice
$this->addAction();
}
/**
* @return bool
*/
public function isBlockEditor()
{
if ( !function_exists( 'get_current_screen' ) ) {
require_once ABSPATH . '/wp-admin/includes/screen.php';
}
$screen = \get_current_screen();
return is_object($screen) ? $screen->is_block_editor() : false;
}
/**
* @return string
*/
......@@ -186,7 +172,6 @@ class Notice
[$this, 'showNotice'],
self::ADMIN_FOOTER_BASE_PRIORITY + intval($this->priority)
);
add_action('admin_head', [$this,'addGutenbergScript']);
$this->actionAdded = true;
}
}
......@@ -207,16 +192,6 @@ class Notice
}
}
/**
* Enqueue admin scripts.
*/
public function addGutenbergScript()
{
if ($this->isBlockEditor()) {
include_once __DIR__ . '/views/admin-head-js-gutenberg.php';
}
}
/**
* Add attribute.
*
......@@ -291,7 +266,7 @@ class Notice
if ($this->addParagraphToContent()) {
$noticeFormat = '<div %1$s><p>%2$s</p></div>';
}
echo sprintf($noticeFormat, $this->getAttributesAsString(), $this->noticeContent);
echo \wp_kses_post( sprintf($noticeFormat, $this->getAttributesAsString(), $this->noticeContent) );
}
}
......
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
?>
<script type="text/javascript">
<?php include dirname(__FILE__, 5) . '/assets/js/gutenberg.js'; ?>
</script>
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
?>
<script type="text/javascript">
<?php include dirname(__FILE__, 5) . '/assets/js/notice.js'; ?>
</script>
<style>.wpdesk-notice-gutenberg { display: none; }</style>
......@@ -48,10 +48,6 @@ class AjaxHandlerTest extends WPTestCase {
$ajaxHandler = new AjaxHandler();
$ajaxHandler->hooks();
$this->assertEquals(
self::WP_DEFAULT_PRIORITY,
has_action( 'admin_head', [ $ajaxHandler, 'addScriptToAdminHead' ] )
);
$this->assertEquals(
self::WP_DEFAULT_PRIORITY,
has_action( 'wp_ajax_wpdesk_notice_dismiss', [ $ajaxHandler, 'processAjaxNoticeDismiss' ] )
......@@ -73,20 +69,6 @@ class AjaxHandlerTest extends WPTestCase {
);
}
public function testAddScriptToAdminHead() {
$ajaxHandler = new AjaxHandler();
$ajaxHandler->hooks();
$this->expectOutputString( '<script type="text/javascript">'
. "\n "
. file_get_contents( __DIR__ . '/../../../../assets/js/notice.js' )
. '</script>
'
);
$ajaxHandler->addScriptToAdminHead();
}
public function testProcessAjaxNoticeDismiss() {
$user_name = 'test_user';
$random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment