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

fix: add css and js as external files

parent a59008f6
No related branches found
No related tags found
1 merge request!30Bugfix/wordpress review
Pipeline #505060 failed
.wpdesk-notice-gutenberg { display: none; }
\ No newline at end of file
jQuery( document ).ready(function() {
jQuery(document).on('click', '.notice-dismiss', function () { jQuery(document).on('click', '.notice-dismiss', function () {
const $notice_div= jQuery(this).closest('div.notice'); const $notice_div= jQuery(this).closest('div.notice');
const notice_name = $notice_div.data('notice-name'); const notice_name = $notice_div.data('notice-name');
...@@ -23,3 +24,4 @@ jQuery( document ).on( 'click', '.notice-dismiss-link', function() { ...@@ -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').data('source',jQuery(this).data('source'));
jQuery(this).closest('div.notice').find('.notice-dismiss').click(); jQuery(this).closest('div.notice').find('.notice-dismiss').click();
}); });
} );
...@@ -22,6 +22,7 @@ class AjaxHandler implements HookablePluginDependant { ...@@ -22,6 +22,7 @@ class AjaxHandler implements HookablePluginDependant {
const SCRIPTS_VERSION = '4'; const SCRIPTS_VERSION = '4';
const SCRIPT_HANDLE = 'wpdesk_notice'; const SCRIPT_HANDLE = 'wpdesk_notice';
const SCRIPT_HANDLE_GUTENBERG = 'wpdesk_notice_gutenberg';
/** /**
* @var string * @var string
...@@ -34,21 +35,28 @@ class AjaxHandler implements HookablePluginDependant { ...@@ -34,21 +35,28 @@ class AjaxHandler implements HookablePluginDependant {
* @param string|null $assetsURL Assets URL. * @param string|null $assetsURL Assets URL.
*/ */
public function __construct( $assetsURL = null ) { public function __construct( $assetsURL = null ) {
$this->assetsURL = $assetsURL; $this->assetsURL = $assetsURL ?? dirname(__FILE__, 5) . '/assets/';
} }
/** /**
* Hooks. * Hooks.
*/ */
public function hooks() { public function hooks() {
if ( $this->assetsURL ) {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAdminScripts' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueueAdminScripts' ] );
} else {
add_action( 'admin_head', [ $this, 'addScriptToAdminHead' ] );
}
add_action( 'wp_ajax_wpdesk_notice_dismiss', [ $this, 'processAjaxNoticeDismiss' ] ); 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. * Enqueue admin scripts.
*/ */
...@@ -60,13 +68,24 @@ class AjaxHandler implements HookablePluginDependant { ...@@ -60,13 +68,24 @@ class AjaxHandler implements HookablePluginDependant {
self::SCRIPTS_VERSION self::SCRIPTS_VERSION
); );
wp_enqueue_script( self::SCRIPT_HANDLE ); wp_enqueue_script( self::SCRIPT_HANDLE );
}
/** if($this->isBlockEditor()){
* Add Java Script to admin header. wp_register_script(
*/ self::SCRIPT_HANDLE,
public function addScriptToAdminHead() { trailingslashit( $this->assetsURL ) . 'js/gutenberg.js',
include __DIR__ . '/views/admin-head-js.php'; [ '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 );
}
} }
/** /**
......
...@@ -92,20 +92,6 @@ class Notice ...@@ -92,20 +92,6 @@ class Notice
$this->addAction(); $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 * @return string
*/ */
...@@ -186,7 +172,6 @@ class Notice ...@@ -186,7 +172,6 @@ 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;
} }
} }
...@@ -207,16 +192,6 @@ class Notice ...@@ -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. * Add attribute.
* *
......
...@@ -48,10 +48,6 @@ class AjaxHandlerTest extends WPTestCase { ...@@ -48,10 +48,6 @@ class AjaxHandlerTest extends WPTestCase {
$ajaxHandler = new AjaxHandler(); $ajaxHandler = new AjaxHandler();
$ajaxHandler->hooks(); $ajaxHandler->hooks();
$this->assertEquals(
self::WP_DEFAULT_PRIORITY,
has_action( 'admin_head', [ $ajaxHandler, 'addScriptToAdminHead' ] )
);
$this->assertEquals( $this->assertEquals(
self::WP_DEFAULT_PRIORITY, self::WP_DEFAULT_PRIORITY,
has_action( 'wp_ajax_wpdesk_notice_dismiss', [ $ajaxHandler, 'processAjaxNoticeDismiss' ] ) has_action( 'wp_ajax_wpdesk_notice_dismiss', [ $ajaxHandler, 'processAjaxNoticeDismiss' ] )
...@@ -73,20 +69,6 @@ class AjaxHandlerTest extends WPTestCase { ...@@ -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() { public function testProcessAjaxNoticeDismiss() {
$user_name = 'test_user'; $user_name = 'test_user';
$random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false ); $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