Skip to content

Commit

Permalink
Merge pull request #719 from equalizedigital/william/716/first-load-o…
Browse files Browse the repository at this point in the history
…f-new-gutenberg-page-doesnt-run-js-checks

Fix: When creating new post in Gutenberg the JS checks were not run till page reload
  • Loading branch information
pattonwebz authored Jul 31, 2024
2 parents 70e3b53 + 06ea2f8 commit a807f8b
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 6 deletions.
8 changes: 2 additions & 6 deletions admin/class-enqueue-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public static function enqueue_styles() {
*/
public static function maybe_enqueue_admin_and_editor_app_scripts() {


global $pagenow;
$post_types = get_option( 'edac_post_types' );
$current_post_type = get_post_type();
Expand Down Expand Up @@ -75,7 +74,6 @@ public static function maybe_enqueue_admin_and_editor_app_scripts() {
$post_id = is_object( $post ) ? $post->ID : null;
wp_enqueue_script( 'edac', plugin_dir_url( EDAC_PLUGIN_FILE ) . 'build/admin.bundle.js', [ 'jquery' ], EDAC_VERSION, false );


wp_localize_script(
'edac',
'edac_script_vars',
Expand All @@ -87,9 +85,7 @@ public static function maybe_enqueue_admin_and_editor_app_scripts() {
]
);


if ( 'post.php' === $pagenow ) {

if ( 'post.php' === $pagenow || 'post-new.php' === $pagenow ) {

// Is this posttype setup to be checked?
$post_types = get_option( 'edac_post_types' );
Expand All @@ -99,7 +95,7 @@ public static function maybe_enqueue_admin_and_editor_app_scripts() {
$pro = is_plugin_active( 'accessibility-checker-pro/accessibility-checker-pro.php' ) && EDAC_KEY_VALID;

if ( EDAC_DEBUG || strpos( EDAC_VERSION, '-beta' ) !== false ) {
$debug = true;
$debug = true; // @codeCoverageIgnore
} else {
$debug = false;
}
Expand Down
102 changes: 102 additions & 0 deletions tests/phpunit/Admin/EnqueueAdminTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Test cases for the Enqueue_Admin class.
*
* @package accessibility-checker
*/

use EDAC\Admin\Enqueue_Admin;

/**
* Tests for functionality of the Enqueue_Admin class.
*/
class EnqueueAdminTest extends WP_UnitTestCase {

/**
* Holds the instance of the Enqueue_Admin class.
*
* @var Enqueue_Admin the instance of the Enqueue_Admin class.
*/
private $enqueue_admin;

/**
* Setup the option, global wp_scripts and the Enqueue_Admin instance.
*
* @return void
*/
protected function setUp(): void {
parent::setUp();

update_option( 'edac_post_types', [ 'post', 'page' ] );

global $wp_scripts;
$wp_scripts = new \WP_Scripts();

$this->enqueue_admin = new Enqueue_Admin();
}

/**
* Clean up the option, global wp_scripts and the Enqueue_Admin instance.
*
* @return void
*/
protected function tearDown(): void {
parent::tearDown();

delete_option( 'edac_post_types' );

global $wp_scripts;
unset( $wp_scripts );

unset( $this->enqueue_admin );
}

/**
* Test that the base script is enqueued in the admin on non-editor pages.
*
* @return void
*/
public function testEnqueueBaseScriptInAdminNonEditorPage() {
$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();

$this->assertTrue( wp_script_is( 'edac', 'enqueued' ) );
$this->assertFalse( wp_script_is( 'edac-editor-app', 'enqueued' ) );
}

/**
* Test that the base script and editor script is enqueued in the editor for an existing page.
*
* @return void
*/
public function testEnqueueBaseAndEditorScriptsInAdminEditorExisting() {

global $post;
$post = $this->factory()->post->create_and_get();

global $pagenow;
$pagenow = 'post.php';

$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();

$this->assertTrue( wp_script_is( 'edac', 'enqueued' ) );
$this->assertTrue( wp_script_is( 'edac-editor-app', 'enqueued' ) );
}

/**
* Test that the base script and editor script is enqueued in the editor for a new page.
*
* @return void
*/
public function testEnqueueBaseAndEditorScriptsInAdminEditorNew() {
global $post;
$post = $this->factory()->post->create_and_get();

global $pagenow;
$pagenow = 'post-new.php';

$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();

$this->assertTrue( wp_script_is( 'edac', 'enqueued' ) );
$this->assertTrue( wp_script_is( 'edac-editor-app', 'enqueued' ) );
}
}

0 comments on commit a807f8b

Please sign in to comment.