From a59123b1a99118732f96856a0e02b6f3f86dc8de Mon Sep 17 00:00:00 2001 From: Marcin Kolanko <marcin.kolanko@wpdesk.net> Date: Fri, 9 May 2025 17:07:30 +0200 Subject: [PATCH 1/8] fix: add css and js as external files --- assets/css/admin.css | 1 + assets/js/notice.js | 50 ++++++++++--------- src/WPDesk/Notice/AjaxHandler.php | 43 +++++++++++----- src/WPDesk/Notice/Notice.php | 25 ---------- .../tests/integration/AjaxHandlerTest.php | 18 ------- 5 files changed, 58 insertions(+), 79 deletions(-) create mode 100644 assets/css/admin.css diff --git a/assets/css/admin.css b/assets/css/admin.css new file mode 100644 index 0000000..7dabae8 --- /dev/null +++ b/assets/css/admin.css @@ -0,0 +1 @@ +.wpdesk-notice-gutenberg { display: none; } \ No newline at end of file diff --git a/assets/js/notice.js b/assets/js/notice.js index c5c396f..bcfcaac 100644 --- a/assets/js/notice.js +++ b/assets/js/notice.js @@ -1,25 +1,27 @@ -jQuery( document ).on( 'click', '.notice-dismiss', function() { - const $notice_div= jQuery(this).closest('div.notice'); - const notice_name = $notice_div.data('notice-name'); - const source = $notice_div.data('source'); - const security = $notice_div.data('security'); - if ('' !== notice_name) { - jQuery.ajax({ - url: ajaxurl, - type: 'post', - data: { - security: security, - action: 'wpdesk_notice_dismiss', - notice_name: notice_name, - source: source, - }, - success: function (response) { - } - }); - } -}); +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'); + const source = $notice_div.data('source'); + const security = $notice_div.data('security'); + if ('' !== notice_name) { + jQuery.ajax({ + url: ajaxurl, + type: 'post', + data: { + security: security, + action: 'wpdesk_notice_dismiss', + notice_name: notice_name, + source: source, + }, + success: function (response) { + } + }); + } + }); -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-link', function() { + jQuery(this).closest('div.notice').data('source',jQuery(this).data('source')); + jQuery(this).closest('div.notice').find('.notice-dismiss').click(); + }); +} ); diff --git a/src/WPDesk/Notice/AjaxHandler.php b/src/WPDesk/Notice/AjaxHandler.php index 64c9b71..4fba4ef 100644 --- a/src/WPDesk/Notice/AjaxHandler.php +++ b/src/WPDesk/Notice/AjaxHandler.php @@ -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 ?? dirname(__FILE__, 5) . '/assets/'; } /** * Hooks. */ public function hooks() { - if ( $this->assetsURL ) { - add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAdminScripts' ] ); - } else { - add_action( 'admin_head', [ $this, 'addScriptToAdminHead' ] ); - } + add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAdminScripts' ] ); 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, + trailingslashit( $this->assetsURL ) . 'js/gutenberg.js', + [ 'jquery' ], + self::SCRIPTS_VERSION + ); + wp_enqueue_script( self::SCRIPT_HANDLE ); + }else{ + wp_register_style( + self::SCRIPT_HANDLE, + trailingslashit( $this->assetsURL ) . 'css/admin.css', + [], + self::SCRIPTS_VERSION + ); + wp_enqueue_style( self::SCRIPT_HANDLE ); + } } /** diff --git a/src/WPDesk/Notice/Notice.php b/src/WPDesk/Notice/Notice.php index 047152a..9c95a73 100644 --- a/src/WPDesk/Notice/Notice.php +++ b/src/WPDesk/Notice/Notice.php @@ -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. * diff --git a/tests/codeception/tests/integration/AjaxHandlerTest.php b/tests/codeception/tests/integration/AjaxHandlerTest.php index 486a60f..ec8af88 100644 --- a/tests/codeception/tests/integration/AjaxHandlerTest.php +++ b/tests/codeception/tests/integration/AjaxHandlerTest.php @@ -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 ); -- GitLab From c982edaa791b6391f86ce3cd66f673c05dce7218 Mon Sep 17 00:00:00 2001 From: Marcin Kolanko <marcin.kolanko@wpdesk.net> Date: Mon, 12 May 2025 17:05:09 +0200 Subject: [PATCH 2/8] fix: secure string --- src/WPDesk/Notice/Notice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WPDesk/Notice/Notice.php b/src/WPDesk/Notice/Notice.php index 9c95a73..d39c763 100644 --- a/src/WPDesk/Notice/Notice.php +++ b/src/WPDesk/Notice/Notice.php @@ -266,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) ); } } -- GitLab From 036b178e90b06ec3908fd753655b1f8b17b1dab3 Mon Sep 17 00:00:00 2001 From: Marcin Kolanko <marcin.kolanko@wpdesk.net> Date: Mon, 12 May 2025 17:08:14 +0200 Subject: [PATCH 3/8] refactor: remove old files --- src/WPDesk/Notice/views/admin-head-js-gutenberg.php | 8 -------- src/WPDesk/Notice/views/admin-head-js.php | 9 --------- 2 files changed, 17 deletions(-) delete mode 100644 src/WPDesk/Notice/views/admin-head-js-gutenberg.php delete mode 100644 src/WPDesk/Notice/views/admin-head-js.php diff --git a/src/WPDesk/Notice/views/admin-head-js-gutenberg.php b/src/WPDesk/Notice/views/admin-head-js-gutenberg.php deleted file mode 100644 index baf57dc..0000000 --- a/src/WPDesk/Notice/views/admin-head-js-gutenberg.php +++ /dev/null @@ -1,8 +0,0 @@ -<?php -if ( ! defined( 'ABSPATH' ) ) { - exit; -} // Exit if accessed directly -?> -<script type="text/javascript"> - <?php include dirname(__FILE__, 5) . '/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 deleted file mode 100644 index b443d67..0000000 --- a/src/WPDesk/Notice/views/admin-head-js.php +++ /dev/null @@ -1,9 +0,0 @@ -<?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> -- GitLab From d3e35eac8569957d3a632af08cb1177b66a6b2e3 Mon Sep 17 00:00:00 2001 From: Marcin Kolanko <marcin.kolanko@wpdesk.net> Date: Fri, 16 May 2025 13:14:38 +0200 Subject: [PATCH 4/8] fix: tests --- .gitlab-ci.yml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0d0d1a3..02dc25c 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -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 + -- GitLab From 73441a3d72cebb97daec837f546ec9e14778c6c5 Mon Sep 17 00:00:00 2001 From: Marcin Kolanko <marcin.kolanko@wpdesk.net> Date: Fri, 16 May 2025 13:21:36 +0200 Subject: [PATCH 5/8] docs: changelog update --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 39dd4b7..c3338e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [3.3.0] +### 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 -- GitLab From 433b356b62e99b81fb7f6187e34a92ffaf01aa4b Mon Sep 17 00:00:00 2001 From: Marcin Kolanko <marcin.kolanko@wpdesk.net> Date: Fri, 16 May 2025 14:17:42 +0200 Subject: [PATCH 6/8] fix: assets dir path --- src/WPDesk/Notice/AjaxHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WPDesk/Notice/AjaxHandler.php b/src/WPDesk/Notice/AjaxHandler.php index 4fba4ef..4e69af2 100644 --- a/src/WPDesk/Notice/AjaxHandler.php +++ b/src/WPDesk/Notice/AjaxHandler.php @@ -35,7 +35,7 @@ class AjaxHandler implements HookablePluginDependant { * @param string|null $assetsURL Assets URL. */ public function __construct( $assetsURL = null ) { - $this->assetsURL = $assetsURL ?? dirname(__FILE__, 5) . '/assets/'; + $this->assetsURL = $assetsURL ?? plugins_url('/assets/', dirname(__FILE__, 3)); } /** -- GitLab From 9634aa3b345488b6885a91d018cf9a0cbcae3238 Mon Sep 17 00:00:00 2001 From: Marcin Kolanko <marcin.kolanko@wpdesk.net> Date: Fri, 16 May 2025 14:21:44 +0200 Subject: [PATCH 7/8] docs: changelog update --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c3338e0..bde7de1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## [3.3.0] +## [3.3.0] - 2025-05-16 ### Fixed - Removing JavaScript code from the page content, adding JavaScript files via admin_enqueue_scripts - Escaping -- GitLab From bc7796258f8bcca0dcc0cca7cc061f6a9963b3b7 Mon Sep 17 00:00:00 2001 From: Marcin Kolanko <marcin.kolanko@wpdesk.net> Date: Fri, 16 May 2025 14:32:17 +0200 Subject: [PATCH 8/8] fix: replace gutenberg handle --- src/WPDesk/Notice/AjaxHandler.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/WPDesk/Notice/AjaxHandler.php b/src/WPDesk/Notice/AjaxHandler.php index 4e69af2..3a5bf88 100644 --- a/src/WPDesk/Notice/AjaxHandler.php +++ b/src/WPDesk/Notice/AjaxHandler.php @@ -71,12 +71,12 @@ class AjaxHandler implements HookablePluginDependant { if($this->isBlockEditor()){ wp_register_script( - self::SCRIPT_HANDLE, + self::SCRIPT_HANDLE_GUTENBERG, trailingslashit( $this->assetsURL ) . 'js/gutenberg.js', [ 'jquery' ], self::SCRIPTS_VERSION ); - wp_enqueue_script( self::SCRIPT_HANDLE ); + wp_enqueue_script( self::SCRIPT_HANDLE_GUTENBERG ); }else{ wp_register_style( self::SCRIPT_HANDLE, -- GitLab