Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MERL-1270: Do not rewrite links in the post editor #1625

Merged
merged 8 commits into from
Nov 15, 2023
5 changes: 5 additions & 0 deletions .changeset/yellow-foxes-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/wordpress-plugin': patch
---

Fixed a bug where links were rewritten to the Faust Front-end Site URL when using the post editor, resulting in those rewritten links being saved to the post content and guid fields when they shouldn't be. These links are now saved with the URL pointing to the WP site, as they should be. They are still rewritten at runtime to link to the Front-end Site URL when appropriate.
22 changes: 22 additions & 0 deletions plugins/faustwp/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,35 @@ function post_preview_link( $link, $post ) {
* @return string URL used for the post.
*/
Copy link
Member

@theodesp theodesp Nov 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are preventing storing the Headless url in the database I think we need to add the following filter in line 24 for the content_blocks plugin:

add_filter( 'wpgraphql_content_blocks_resolver_content', __NAMESPACE__ . '\\content_replacement');

This is to ensure that we trigger domain replacement when resolving the blocks using parse_blocks.

Without the above filter:
Screenshot 2023-11-02 at 17 08 47

With the above filter:
Screenshot 2023-11-02 at 17 07 49

Unless there is a better way... @josephfusco any hints?

function post_link( $link ) {
global $pagenow;
$target_pages = array( 'admin-ajax.php', 'index.php', 'edit.php', 'post.php', 'post-new.php', 'upload.php', 'media-new.php' );

// phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in `is_ajax_generate_permalink_request()` and `is_wp_link_ajax_request()`.
if ( empty( $_POST ) && 'post-new.php' === $pagenow ) {
return $link;
}

// Ajax requests to generate permalink.
if ( in_array( $pagenow, $target_pages, true )
&& is_ajax_generate_permalink_request()
) {
return $link;
}

if (
! is_rewrites_enabled()
|| ( function_exists( 'is_graphql_request' ) && is_graphql_request() )
// Block editor makes REST requests on these pages to query content.
|| ( in_array( $pagenow, $target_pages, true ) && current_user_can( 'edit_posts' ) && defined( 'REST_REQUEST' ) && REST_REQUEST )
) {
return $link;
}

// Check for wp-link-ajax requests. Used by Classic Editor when linking content.
if ( is_wp_link_ajax_request() ) {
return $link;
}

return equivalent_frontend_url( $link );
}

Expand Down
21 changes: 21 additions & 0 deletions plugins/faustwp/includes/replacement/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,24 @@ function has_file_extension( $string ) {
}
}

/**
* Determines if an AJAX request to generate permalinks is in progress.
*
* @return boolean
*/
function is_ajax_generate_permalink_request(): bool {
return ( ! empty( $_POST['samplepermalinknonce'] ) && check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' ) );
}

/**
* Determines if a wp-link-ajax request is in progress.
*
* @return boolean
*/
function is_wp_link_ajax_request(): bool {
return ( wp_doing_ajax()
&& ! empty( $_POST['_ajax_linking_nonce'] )
&& wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_ajax_linking_nonce'] ) ), 'internal-linking' )
&& ! empty( $_POST['action'] )
&& 'wp-link-ajax' === $_POST['action'] );
}
49 changes: 45 additions & 4 deletions plugins/faustwp/tests/integration/ReplacementCallbacksTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
content_replacement,
post_preview_link,
image_source_replacement,
image_source_srcset_replacement
image_source_srcset_replacement,
post_link
};
use function WPE\FaustWP\Settings\faustwp_update_setting;

Expand Down Expand Up @@ -159,11 +160,51 @@ public function test_post_link_returns_unfiltered_link_when_content_replacement_
*/
public function test_post_link_returns_filtered_link_when_content_replacement_is_enabled() {
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_rewrites', true );
faustwp_update_setting( 'enable_rewrites', '1' );

$this->assertSame( 'http://moo/?p=' . $this->post_id, get_permalink( $this->post_id ) );
}

public function test_post_link_returns_unfiltered_link_when_on_post_new_page(): void {
global $pagenow;
$pagenow = 'post-new.php';
self::assertSame( 'http://example.org/hello-world', post_link( 'http://example.org/hello-world' ) );
}

public function test_post_link_returns_unfiltered_link_on_ajax_requests_to_generate_permalinks_using_samplepermalinknonce(): void {
global $pagenow, $_REQUEST, $_POST;
$pagenow = 'admin-ajax.php';
wp_set_current_user( 1 );
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_rewrites', '1' );
$_REQUEST['samplepermalinknonce'] = wp_create_nonce( 'samplepermalink' );
$_POST['samplepermalinknonce'] = $_REQUEST['samplepermalinknonce'];

self::assertSame( 'http://example.org/hello-world', post_link( 'http://example.org/hello-world' ) );

unset( $_REQUEST['samplepermalinknonce'], $_POST['samplepermalinknonce'] );
unset( $pagenow );
wp_set_current_user( null );
}

public function test_post_link_returns_unfiltered_link_on_ajax_requests_to_generate_permalinks_using_ajax_linking_nonce(): void {
global $pagenow, $_POST;
$pagenow = 'admin-ajax.php';
wp_set_current_user( 1 );
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_rewrites', '1' );
add_filter( 'wp_doing_ajax', '__return_true' );
$_POST['_ajax_linking_nonce'] = wp_create_nonce( 'internal-linking' );
$_POST['action'] = 'wp-link-ajax';

self::assertSame( 'http://example.org/hello-world', post_link( 'http://example.org/hello-world' ) );

unset( $_POST['_ajax_linking_nonce'], $_POST['action'] );
unset( $pagenow );
remove_filter( 'wp_doing_ajax', '__return_true' );
wp_set_current_user( null );
}

/**
* Tests get_preview_post_link() returns rewritten value.
*/
Expand Down Expand Up @@ -203,7 +244,7 @@ public function test_post_preview_link_uses_frontend_uri_scheme() {
public function test_custom_post_type_post_preview_link_returns_filtered_link_when_content_replacement_is_enabled()
{
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_rewrites', true );
faustwp_update_setting( 'enable_rewrites', '1' );
$post_id = $this->getCustomPostType();
$this->assertSame( 'http://moo/?document=' . $post_id . '&preview=true&previewPathname=' . rawurlencode( wp_make_link_relative( get_permalink( $post_id ) ) ) . '&p=' . $post_id . '&typeName=Document', get_preview_post_link( $post_id ) );
faustwp_update_setting( 'frontend_uri', null );
Expand All @@ -216,7 +257,7 @@ public function test_custom_post_type_post_preview_link_returns_filtered_link_wh
public function test_custom_post_type_post_link_returns_unfiltered_link_when_content_replacement_is_enabled()
{
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_rewrites', true );
faustwp_update_setting( 'enable_rewrites', '1' );
$post_id = $this->getCustomPostType();
$this->assertSame( 'http://example.org/?document=' . $post_id, get_permalink($post_id) );
faustwp_update_setting( 'frontend_uri', null );
Expand Down
Loading