diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 0d0d1a3335c7ceffa774e10f2badd4bdbec9ab26..02dc25c318c4bb4db390fe566179259d4d98967d 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 + diff --git a/CHANGELOG.md b/CHANGELOG.md index 39dd4b7d71a7173ae3100aa6d8407110be76d027..bde7de1480a698f8f4deaae1342f1a9496a7e779 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## [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 diff --git a/assets/css/admin.css b/assets/css/admin.css new file mode 100644 index 0000000000000000000000000000000000000000..7dabae8cd0f26616f320819e48d32e8575be8833 --- /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 c5c396fe3cd7bf1da27ce530900dd42154f4a6c4..bcfcaace278322f6c5555180c3c07c5b643da310 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 64c9b7141b4bdc34ef6c2725831842ef26e3240a..3a5bf888a9f4e606c7fa8f01d2bca7834349554a 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 ?? 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( '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_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 ); + } } /** diff --git a/src/WPDesk/Notice/Notice.php b/src/WPDesk/Notice/Notice.php index 047152a8863346d9cd808bc6632aac3f3fe540bd..d39c76349c82b7ed64d8f8ae83bb3ded928de168 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. * @@ -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) ); } } 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 baf57dc3dac8668de6bcb041578be2d27713cf89..0000000000000000000000000000000000000000 --- 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 b443d67de1fe32d074d34dc85925d96926910190..0000000000000000000000000000000000000000 --- 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> diff --git a/tests/codeception/tests/integration/AjaxHandlerTest.php b/tests/codeception/tests/integration/AjaxHandlerTest.php index 486a60f273cc35bbe5f4fe5447854b858c0563f3..ec8af8833c5f646fa13be01267b927446cdb1f5e 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 );