Skip to content

Commit

Permalink
MERL-1185: Redirect Off Doesn't Rewrite Post Preview Link (#1641)
Browse files Browse the repository at this point in the history
* init commit

* added test for redirects being off

* Update ReplacementCallbacksTests.php

* Update callbacks.php

* setting restored after test

* added setting for redirects to be true for previous tests

* Update ReplacementCallbacksTests.php

* Refactor unit test

* Update callbacks.php

* added settings update back in

---------

Co-authored-by: Joe Fusco <[email protected]>
Co-authored-by: Blake Wilson <[email protected]>
Co-authored-by: John Parris <[email protected]>
  • Loading branch information
4 people authored Nov 17, 2023
1 parent 50a8d08 commit 42cbe14
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 5 additions & 1 deletion plugins/faustwp/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
faustwp_get_setting,
is_image_source_replacement_enabled,
is_rewrites_enabled,
is_redirects_enabled,
use_wp_domain_for_media,
use_wp_domain_for_post_and_category_urls,
};
use function WPE\FaustWP\Utilities\{
plugin_version,
Expand Down Expand Up @@ -146,6 +146,10 @@ function image_source_srcset_replacement( $sources ) {
* @return string URL used for the post preview.
*/
function post_preview_link( $link, $post ) {
// Don't rewrite preview link if redirect is disabled.
if ( ! is_redirects_enabled() ) {
return $link;
}
$frontend_uri = faustwp_get_setting( 'frontend_uri' );

if ( $frontend_uri ) {
Expand Down
19 changes: 18 additions & 1 deletion plugins/faustwp/tests/integration/ReplacementCallbacksTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public function test_post_link_returns_unfiltered_link_on_ajax_requests_to_gener
*/
public function test_post_preview_link_returns_filtered_link() {
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_redirects', true );

$this->assertSame( 'http://moo/?p=' . $this->post_id . '&preview=true&previewPathname=' . rawurlencode( wp_make_link_relative( get_permalink( $this->post_id ) ) ) . '&typeName=Post', get_preview_post_link( $this->post_id ) );
}
Expand All @@ -219,12 +220,26 @@ public function test_post_preview_link_returns_filtered_link() {
*/
public function test_post_preview_link_adds_preview_true_query_param() {
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_redirects', true );

$link = post_preview_link( 'http://moo/', get_post( $this->post_id ) );

$this->assertSame( 'http://moo/?previewPathname=' . rawurlencode( wp_make_link_relative( get_permalink( $this->post_id ) ) ) . '&p=' . $this->post_id . '&preview=true&typeName=Post', $link );
}

/**
* Tests post_preview_link() doesn't rewrite link if enable redirects is false.
*/
public function test_post_preview_doesnt_rewrite_link_with_redirect_off() {
faustwp_update_setting( 'enable_redirects', false );
$expected = 'http://moo/?p=' . $this->post_id;
$link = post_preview_link( $expected, get_post( $this->post_id ) );

$this->assertSame( $expected, $link );

faustwp_update_setting( 'enable_redirects', true );
}

/**
* Tests post_preview_link() uses frontend_uri scheme if different than home_url scheme.
*/
Expand All @@ -244,7 +259,8 @@ 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', '1' );
faustwp_update_setting( 'enable_rewrites', true );
faustwp_update_setting( 'enable_redirects', true );
$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 @@ -269,6 +285,7 @@ public function test_custom_post_type_post_link_returns_unfiltered_link_when_con
*/
public function test_post_preview_link_filters_link_for_posts_not_registered_with_wpgraphql() {
faustwp_update_setting( 'frontend_uri', 'http://moo' );
faustwp_update_setting( 'enable_redirects', true );

register_post_type('notgraphql', ['public' => true]);

Expand Down

0 comments on commit 42cbe14

Please sign in to comment.