Skip to content

Commit

Permalink
Bug: Fixed content replacment to allow null values (#2038)
Browse files Browse the repository at this point in the history
* Fixed for content_replcament. Reverted method signature to allow for null values. Added unit tests and added a try and catch aswell to catch any future issues.

* Added changeset. Fixes #2037

* Updated test name.

* Code review changes.

* Changed signature to match what is in content blocks for the content resolver.

* Updated tests. Declared strict_types to the replacement files to catch any futher errors.
  • Loading branch information
colinmurphy authored Feb 7, 2025
1 parent fc8bd26 commit 4dddd20
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-panthers-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@faustwp/wordpress-plugin": patch
---

Bug: Fixed an issue with the function content_replacement throwing a 500 error for a null value.
17 changes: 6 additions & 11 deletions plugins/faustwp/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package FaustWP
*/

declare(strict_types=1);

namespace WPE\FaustWP\Replacement;

use function WPE\FaustWP\Settings\{
Expand All @@ -27,40 +29,33 @@
/**
* Callback for WordPress 'the_content' filter.
*
* @param string $content The post content.
* @param ?string $content The post content.
*
* @return string The post content.
* @return ?string The post content.
*/
function content_replacement( string $content ): string {
function content_replacement( ?string $content ) {

if ( ! $content ) {
return '';
return $content;
}

$replace_content_urls = domain_replacement_enabled();
$replace_media_urls = ! use_wp_domain_for_media();

if ( ! $replace_content_urls && ! $replace_media_urls ) {
return $content;
}

$wp_site_urls = faustwp_get_wp_site_urls( site_url() );
if ( empty( $wp_site_urls ) ) {
return $content;
}

$relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls, wp_upload_dir()['baseurl'] );
$wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls, $relative_upload_url );
$frontend_uri = (string) faustwp_get_setting( 'frontend_uri' ) ?? '/';

if ( $replace_content_urls && $replace_media_urls ) {
return str_replace( $wp_site_urls, $frontend_uri, $content );
}

if ( $replace_media_urls ) {
return str_replace( $wp_media_urls, $frontend_uri . $relative_upload_url, $content );
}

$site_urls_pattern = implode( '|', array_map( 'preg_quote', $wp_site_urls ) );
$pattern = '#(' . $site_urls_pattern . ')(?!' . $relative_upload_url . '(\/|$))#';

Expand Down
4 changes: 3 additions & 1 deletion plugins/faustwp/includes/replacement/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* @package FaustWP
*/

declare(strict_types=1);

namespace WPE\FaustWP\Replacement;

use function WPE\FaustWP\Settings\{
Expand Down Expand Up @@ -144,7 +146,7 @@ function faustwp_get_wp_site_urls( string $site_url ): array {

$host_url = wp_parse_url( $site_url, PHP_URL_HOST );

$is_https = strpos( $site_url, 0, 6 ) === 'https:';
$is_https = strpos( $site_url, 'https://' ) === 0;

return apply_filters(
'faustwp_get_wp_site_urls',
Expand Down
11 changes: 11 additions & 0 deletions plugins/faustwp/tests/integration/ReplacementCallbacksTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ public function test_wp_calculate_image_srcset_filter(): void {
self::assertSame( 10, has_action( 'wp_calculate_image_srcset', 'WPE\FaustWP\Replacement\image_source_srcset_replacement' ) );
}


/**
* Test to make sure content rep can accept null values
*/
public function test_content_replacement_for_different_types() {
$this->assertNull(content_replacement( null));
$this->assertEmpty(content_replacement(''));
$content = '<p>This is a string with no URLs to be replaced.</p>';
$this->assertEquals($content, content_replacement($content));
}

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

0 comments on commit 4dddd20

Please sign in to comment.