Skip to content

Commit

Permalink
fix: (wpengine#354) warning on widgets.php due to previewlinks.js (wp…
Browse files Browse the repository at this point in the history
…engine#429)

* fix: (wpengine#354) use `load-post-new.php`/`load-post.php` actions for previewlinks.js

* test: (wpengine#354) test preview links action calls
  • Loading branch information
blakewilson authored Sep 1, 2021
1 parent 62171f9 commit 39322aa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
13 changes: 6 additions & 7 deletions plugins/wpe-headless/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,13 @@ function wpe_headless_term_link( $term_link ) {
}


add_action( 'load-post-new.php', 'wpe_headless_enqueue_preview_scripts' );
add_action( 'load-post.php', 'wpe_headless_enqueue_preview_scripts' );
/**
* Adds JavaScript file to the Gutenberg editor page that prepends /preview to the preview link
* Adds JavaScript file to the Gutenberg editor page that prepends /preview to the preview link.
*
* XXX: Please remove this once this issue is resolved: https://github.com/WordPress/gutenberg/issues/13998
*/
add_action(
'enqueue_block_editor_assets',
function() {
wp_enqueue_script( 'awp-gutenberg-filters', plugins_url( '/previewlinks.js', __FILE__ ), array( 'wp-edit-post' ), '1.0.0', true );
}
);
function wpe_headless_enqueue_preview_scripts() {
wp_enqueue_script( 'awp-gutenberg-filters', plugins_url( '/previewlinks.js', __FILE__ ), array(), '1.0.0', true );
}
4 changes: 2 additions & 2 deletions plugins/wpe-headless/includes/replacement/previewlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ window.addEventListener('DOMContentLoaded', function () {
'button[class~="block-editor-post-preview__button-toggle"]',
);

if (!previewButton) {
if (!previewButton.length) {
return;
}

Expand All @@ -20,7 +20,7 @@ window.addEventListener('DOMContentLoaded', function () {
'a[target*="wp-preview"]',
);

if (!links) {
if (!links.length) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public function test_graphql_request_results_filter() {
$this->assertSame( 10, has_action( 'graphql_request_results', 'wpe_headless_url_replacement' ) );
}

public function test_enqueue_preview_scripts_action() {
$this->assertSame( 10, has_action( 'load-post-new.php', 'wpe_headless_enqueue_preview_scripts' ) );
$this->assertSame( 10, has_action( 'load-post.php', 'wpe_headless_enqueue_preview_scripts' ) );
}

/**
* Tests wpe_headless_content_replacement() returns original value when content replacement is not enabled.
*/
Expand Down

0 comments on commit 39322aa

Please sign in to comment.