Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions plugins/faustwp/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,12 @@ function term_link( $term_link ) {

add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_preview_scripts' );
/**
* Adds JavaScript file to the Gutenberg editor page that prepends /preview to the preview link.
* Adds a script to the block editor that points its preview links at the Faust.js frontend preview.
*
* XXX: Please remove this once this issue is resolved: https://github.com/WordPress/gutenberg/issues/13998
*/
function enqueue_preview_scripts() {
wp_enqueue_script( 'faustwp-gutenberg-filters', plugins_url( '/previewlinks.js', __FILE__ ), array(), plugin_version(), true );
wp_enqueue_script( 'faustwp-gutenberg-filters', plugins_url( '/previewlinks.js', __FILE__ ), array( 'wp-data' ), plugin_version(), true );
wp_localize_script(
'faustwp-gutenberg-filters',
'_faustwp_preview_data',
Expand Down
12 changes: 12 additions & 0 deletions plugins/faustwp/includes/replacement/previewlinks.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ document.addEventListener('DOMContentLoaded', function () {
}
}

// Once a post is public, the save snackbar's link ("View Post", "View
// Page", etc.) goes to the live post, so only rewrite it before then.
// Private and scheduled posts keep the preview link, since the frontend
// can't show them to visitors.
function isPostPublic() {
const editor = window.wp?.data?.select('core/editor');
return editor
? editor.getCurrentPostAttribute('status') === 'publish'
: false;
}

function updateUIElements() {
const { headerLink, snackbarLink } = getPreviewLinksByVersion(wpVersion);

Expand All @@ -51,6 +62,7 @@ document.addEventListener('DOMContentLoaded', function () {

if (
snackbarLink &&
!isPostPublic() &&
snackbarLink.getAttribute('href') !== faustPreviewLink
) {
snackbarLink.setAttribute('href', faustPreviewLink);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ public function test_enqueue_preview_scripts_action() {
$this->assertSame( 10, has_action( 'enqueue_block_editor_assets', 'WPE\FaustWP\Replacement\enqueue_preview_scripts' ) );
}

/**
* Tests the preview script loads after wp-data, since it reads the post's status from the core/editor store.
*/
public function test_enqueue_preview_scripts_depends_on_wp_data() {
\WPE\FaustWP\Replacement\enqueue_preview_scripts();

$this->assertContains( 'wp-data', wp_scripts()->registered['faustwp-gutenberg-filters']->deps );
}

public function test_wp_sitemaps_posts_entry_filter() {
$this->assertSame( 10, has_action( 'wp_sitemaps_posts_entry', 'WPE\FaustWP\Replacement\sitemaps_posts_entry' ) );
}
Expand Down
Loading