Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion admin/class-enqueue-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ public static function maybe_enqueue_admin_and_editor_app_scripts() {

global $post;
$post_id = is_object( $post ) ? $post->ID : null;

// When the "latest posts" homepage is active (show_on_front=posts), the global
// $post can be set to the first blog post from the main query — for example in
// the FSE site editor while previewing the home template. Apply a filter so that
// extensions can supply the correct post ID (e.g. a Pro virtual-page ID) rather
// than letting the wrong post's ID propagate into scan results.
$post_id = apply_filters( 'edac_filter_admin_post_id', $post_id );
Comment thread
SteveJonesDev marked this conversation as resolved.

wp_enqueue_script( 'edac', plugin_dir_url( EDAC_PLUGIN_FILE ) . 'build/admin.bundle.js', [ 'jquery' ], EDAC_VERSION, false );
wp_set_script_translations( 'edac', 'accessibility-checker', plugin_dir_path( EDAC_PLUGIN_FILE ) . 'languages' );

Expand Down Expand Up @@ -112,7 +120,16 @@ public static function maybe_enqueue_admin_and_editor_app_scripts() {
wp_set_script_translations( 'edac-editor-app', 'accessibility-checker', plugin_dir_path( EDAC_PLUGIN_FILE ) . 'languages' );

// If this is the frontpage or homepage, preview URLs won't work. Use the live URL.
if ( (int) get_option( 'page_on_front' ) === $post_id || (int) get_option( 'page_for_posts' ) === $post_id ) {
// When show_on_front=posts and page_for_posts is not set (0), neither WP option
// will match — allow extensions to flag the current post as the latest-posts
// homepage so we use get_home_url() for the scan rather than a preview link.
$is_latest_posts_home = 'posts' === get_option( 'show_on_front' )
&& 0 === (int) get_option( 'page_for_posts' )
&& apply_filters( 'edac_filter_post_is_latest_posts_home', false, $post_id );
Comment thread
SteveJonesDev marked this conversation as resolved.
Outdated

if ( $is_latest_posts_home ) {
$scan_url = add_query_arg( 'edac_pageScanner', 1, trailingslashit( get_home_url() ) );
} elseif ( (int) get_option( 'page_on_front' ) === $post_id || (int) get_option( 'page_for_posts' ) === $post_id ) {
$scan_url = add_query_arg( 'edac_pageScanner', 1, get_permalink( $post_id ) );
} else {
$post_view_link = apply_filters(
Expand Down
11 changes: 10 additions & 1 deletion includes/classes/class-enqueue-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,16 @@ public static function maybe_enqueue_frontend_highlighter() {

// Don't load on the frontend if we don't have a post to work with.
global $post;
$post_id = apply_filters( 'edac_filter_frontend_highlight_post_id', is_object( $post ) ? $post->ID : null );

// When the homepage displays latest posts (show_on_front=posts), WordPress populates
// the global $post with the first post from the blog query rather than a page object.
// Passing that post's ID would attribute homepage scan results to the wrong entry.
// Pass null instead so the filter can supply a virtual-page ID (Pro) or bail cleanly.
$default_post_id = ( is_home() && is_front_page() && 'posts' === get_option( 'show_on_front' ) )
? null
: ( is_object( $post ) ? $post->ID : null );
Comment thread
SteveJonesDev marked this conversation as resolved.
Outdated

$post_id = apply_filters( 'edac_filter_frontend_highlight_post_id', $default_post_id );

if ( null === $post_id ) {
return;
Expand Down
62 changes: 62 additions & 0 deletions tests/phpunit/Admin/EnqueueAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,68 @@ public function testSrOnlyFormatDoesNotEnqueueOnOtherAdminPages() {
}


/**
* Test that edac_filter_admin_post_id overrides the post ID localized into edac_script_vars.
*
* @return void
*/
public function testAdminPostIdFilterOverridesLocalizedPostId() {
global $post, $pagenow, $wp_scripts;

$original_post = $this->factory()->post->create_and_get();
$alternate_post = $this->factory()->post->create_and_get();
$post = $original_post;
$pagenow = 'post.php';

$filter_callback = static function () use ( $alternate_post ) {
return $alternate_post->ID;
};
add_filter( 'edac_filter_admin_post_id', $filter_callback );

$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();

remove_filter( 'edac_filter_admin_post_id', $filter_callback );

$localized_data = $wp_scripts->get_data( 'edac', 'data' );
$this->assertStringContainsString( (string) $alternate_post->ID, $localized_data );
}

/**
* Test that edac_filter_post_is_latest_posts_home causes the scan URL to use the home URL.
*
* When show_on_front=posts and page_for_posts=0, the standard WP options cannot identify a
* virtual homepage post. The filter lets extensions signal this so we use get_home_url()
* for the scanner iframe rather than an invalid preview URL.
*
* @return void
*/
public function testScanUrlUsesHomeUrlWhenLatestPostsHomeFilterReturnsTrue() {
global $post, $pagenow, $wp_scripts;

$post = $this->factory()->post->create_and_get( [ 'post_type' => 'page' ] );
$pagenow = 'post.php';

update_option( 'show_on_front', 'posts' );
update_option( 'page_for_posts', 0 );

$filter_callback = static function () {
return true;
};
add_filter( 'edac_filter_post_is_latest_posts_home', $filter_callback );

$this->enqueue_admin::maybe_enqueue_admin_and_editor_app_scripts();

remove_filter( 'edac_filter_post_is_latest_posts_home', $filter_callback );
delete_option( 'show_on_front' );
delete_option( 'page_for_posts' );

$localized_data = $wp_scripts->get_data( 'edac-editor-app', 'data' );
$this->assertStringContainsString( 'edac_pageScanner', $localized_data );
$this->assertStringNotContainsString( 'preview=true', $localized_data );
// The scan URL should be based on the home URL, not a preview link.
$this->assertStringContainsString( trailingslashit( get_home_url() ), $localized_data );
}

/**
* Helper to set a mock current screen with block editor context.
*
Expand Down
52 changes: 52 additions & 0 deletions tests/phpunit/includes/classes/EnqueueFrontendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,58 @@ public function testScannerBundleUrlIncludesVersionQueryString(): void {
$this->assertStringContainsString( 'ver=' . EDAC_VERSION, $localized_data );
}

/**
* Highlighter must NOT load on the "latest posts" homepage when no filter overrides the ID.
*
* When show_on_front=posts, the global $post is the first blog post from the main query, not
* the homepage itself. The fix passes null to the filter so the free plugin bails gracefully.
*/
public function testFrontendHighlighterDoesNotLoadOnLatestPostsHomepage(): void {
$admin_id = $this->factory()->user->create( [ 'role' => 'administrator' ] );
wp_set_current_user( $admin_id );

// Create a post so the main query has results.
$this->factory()->post->create_and_get( [ 'post_type' => 'post' ] );

update_option( 'show_on_front', 'posts' );

// Simulate visiting the homepage so is_home() / is_front_page() return true.
$this->go_to( '/' );

Enqueue_Frontend::maybe_enqueue_frontend_highlighter();

$this->assertFalse( wp_script_is( 'edac-frontend-highlighter-app', 'enqueued' ) );

delete_option( 'show_on_front' );
}

/**
* A filter on edac_filter_frontend_highlight_post_id can enable the highlighter on the
* "latest posts" homepage by supplying a valid post ID (e.g. a Pro virtual-page ID).
*/
public function testFrontendHighlighterLoadsOnLatestPostsHomepageWhenFilterProvidesId(): void {
$admin_id = $this->factory()->user->create( [ 'role' => 'administrator' ] );
wp_set_current_user( $admin_id );

$post = $this->factory()->post->create_and_get( [ 'post_type' => 'post' ] );

update_option( 'show_on_front', 'posts' );

$this->go_to( '/' );

$filter_callback = static function () use ( $post ) {
return $post->ID;
};
$this->added_filters['edac_filter_frontend_highlight_post_id'] = $filter_callback;
add_filter( 'edac_filter_frontend_highlight_post_id', $filter_callback );

Enqueue_Frontend::maybe_enqueue_frontend_highlighter();

$this->assertTrue( wp_script_is( 'edac-frontend-highlighter-app', 'enqueued' ) );

delete_option( 'show_on_front' );
}

/**
* Ensure the highlighter uses the filtered post ID when determining scannable post types.
*/
Expand Down
Loading