From 2be8bfee0842991cc38fd5015cf129d15ea02f6c Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Tue, 24 Dec 2024 14:29:58 +0000
Subject: [PATCH 1/4] Fix for #1986. Fixes various issues with content
replacement callback and also updates tests to use WP 6.6 and 6.7
Fixes various issues with using content replacement for the site URL.
---
.github/workflows/unit-test-plugin.yml | 2 +-
.gitignore | 3 +
plugins/faustwp/docker-compose.yml | 2 +-
plugins/faustwp/faustwp.php | 2 +-
.../includes/replacement/callbacks.php | 49 +++--
.../includes/replacement/functions.php | 62 ++++++
plugins/faustwp/package.json | 2 +-
.../integration/ReplacementCallbacksTests.php | 196 +++++++++++++++++-
8 files changed, 291 insertions(+), 27 deletions(-)
diff --git a/.github/workflows/unit-test-plugin.yml b/.github/workflows/unit-test-plugin.yml
index 1b16961e5..489e293f8 100644
--- a/.github/workflows/unit-test-plugin.yml
+++ b/.github/workflows/unit-test-plugin.yml
@@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-22.04
strategy:
matrix:
- wordpress: [ '6.5', '6.4', '6.3', '6.2', '6.1' ]
+ wordpress: [ '6.7', '6.6', '6.5', '6.4', '6.3', '6.2', '6.1' ]
steps:
- name: Checkout
uses: actions/checkout@v4
diff --git a/.gitignore b/.gitignore
index 0ecd42389..6e9c3a229 100644
--- a/.gitignore
+++ b/.gitignore
@@ -11,6 +11,9 @@ process.yml
!.env.local.sample
build/
faustjs.code-workspace
+plugins/faustwp/.docker/plugins/akismet/
+plugins/faustwp/.docker/plugins/hello.php
+plugins/faustwp/.docker/plugins/index.php
# Ignore the WordPress source where used by various development environments
wordpress/
diff --git a/plugins/faustwp/docker-compose.yml b/plugins/faustwp/docker-compose.yml
index 1fd9e57ca..1bfef8b2d 100644
--- a/plugins/faustwp/docker-compose.yml
+++ b/plugins/faustwp/docker-compose.yml
@@ -11,7 +11,7 @@ services:
links:
- db
environment:
- WP_VERSION: ${WP_VERSION:-6.5}
+ WP_VERSION: ${WP_VERSION:-6.7}
WORDPRESS_DB_HOST: db
WORDPRESS_DB_NAME: wordpress
WORDPRESS_DB_USER: root
diff --git a/plugins/faustwp/faustwp.php b/plugins/faustwp/faustwp.php
index 033de3d38..01887c660 100644
--- a/plugins/faustwp/faustwp.php
+++ b/plugins/faustwp/faustwp.php
@@ -9,7 +9,7 @@
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: faustwp
* Domain Path: /languages
- * Version: 1.5.0
+ * Version: 1.5.1
* Requires PHP: 7.2
* Requires at least: 5.7
* Update URI: false
diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php
index de4b84cec..3e30f0e71 100644
--- a/plugins/faustwp/includes/replacement/callbacks.php
+++ b/plugins/faustwp/includes/replacement/callbacks.php
@@ -31,36 +31,50 @@
*
* @return string The post content.
*/
-function content_replacement( $content ) {
- $use_wp_domain_for_permalinks = ! domain_replacement_enabled();
- $use_wp_domain_for_media = use_wp_domain_for_media();
+function content_replacement( string $content ): string {
- if ( $use_wp_domain_for_permalinks && $use_wp_domain_for_media ) {
+ $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();
+ if (empty($wp_site_urls)) {
return $content;
}
- $replacement = faustwp_get_setting( 'frontend_uri' );
- if ( ! $replacement ) {
- $replacement = '/';
+ $wp_media_urls = faustwp_get_wp_media_urls();
+ $relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls );
+ $frontend_uri = faustwp_get_setting( 'frontend_uri' );
+ if ( ! $frontend_uri ) {
+ $frontend_uri = '/';
}
- $site_url = site_url();
- $media_dir = str_replace( $site_url, '', wp_upload_dir()['baseurl'] );
- $media_url = $site_url . $media_dir;
+ if ( $replace_content_urls && $replace_media_urls ) {
+
+ foreach ( $wp_site_urls as $site_url ) {
+ $content = str_replace( $site_url, $frontend_uri, $content );
+ }
- if ( $use_wp_domain_for_permalinks && ! $use_wp_domain_for_media ) {
- $content = str_replace( $media_url, $replacement . $media_dir, $content );
return $content;
}
- if ( ! $use_wp_domain_for_permalinks && ! $use_wp_domain_for_media ) {
- $content = str_replace( $site_url, $replacement, $content );
+
+ if ( $replace_media_urls ) {
+ $wp_media_site_url = $frontend_uri . $relative_upload_url;
+
+ foreach ( $wp_media_urls as $media_url ) {
+ $content = str_replace( $media_url, $wp_media_site_url, $content );
+ }
+
return $content;
}
- if ( ! $use_wp_domain_for_permalinks && $use_wp_domain_for_media ) {
- $content = preg_replace( "#{$site_url}(?!{$media_dir})#", "{$replacement}", $content );
- return $content;
+ foreach ( $wp_site_urls as $site_url ) {
+ $pattern_exclude_media_urls = "#" . preg_quote( $site_url, '#' ) . "(?!{$relative_upload_url}(\/|$))#";
+ $content = preg_replace( $pattern_exclude_media_urls, $frontend_uri, $content );
}
return $content;
@@ -102,6 +116,7 @@ function image_source_srcset_replacement( $sources ) {
$frontend_uri = faustwp_get_setting( 'frontend_uri' );
$site_url = site_url();
+
/**
* For urls with no domain or the frontend domain, replace with the WP site_url.
* This was the default replacement pattern until Faust 1.2, at which point this
diff --git a/plugins/faustwp/includes/replacement/functions.php b/plugins/faustwp/includes/replacement/functions.php
index da5f69382..7cc75e4f6 100644
--- a/plugins/faustwp/includes/replacement/functions.php
+++ b/plugins/faustwp/includes/replacement/functions.php
@@ -131,3 +131,65 @@ function is_wp_link_ajax_request(): bool {
&& ! empty( $_POST['action'] )
&& 'wp-link-ajax' === $_POST['action'] );
}
+
+/**
+ * Get all site URLs for each HTTP schema
+ *
+ * @return array
+ */
+function faustwp_get_wp_site_urls() {
+
+ $site_url = site_url();
+ $host_url = parse_url( $site_url, PHP_URL_HOST );
+
+ if ( is_string( $host_url ) ) {
+ $urls = [
+ 'https://' . $host_url,
+ 'http://' . $host_url,
+ '//' . $host_url,
+ ];
+ } else {
+ $urls = [ $site_url ];
+ }
+
+
+ return apply_filters( 'faustwp_get_wp_site_urls', $urls );
+}
+
+/**
+ * Get all media urls based off the available site urls
+ *
+ * @return array
+ */
+function faustwp_get_wp_media_urls() {
+ $site_urls = faustwp_get_wp_site_urls();
+ $upload_url = faustwp_get_relative_upload_url( $site_urls );
+
+ if ( ! is_string( $upload_url ) ) {
+ return apply_filters( 'faustwp_get_wp_site_media_urls', [] );
+ }
+
+ $media_urls = [];
+ foreach ( $site_urls as $site_url ) {
+ $media_urls[] = $site_url . $upload_url;
+ }
+
+ return apply_filters( 'faustwp_get_wp_site_media_urls', $media_urls );
+}
+
+
+/**
+ * @param array $site_urls
+ *
+ * @return false|string
+ */
+function faustwp_get_relative_upload_url( array $site_urls ) {
+ $upload_dir = wp_upload_dir()['baseurl'];
+ foreach ( $site_urls as $site_url ) {
+ if ( strpos( $upload_dir, $site_url ) !== false ) {
+ return (string) str_replace( $site_url, '', $upload_dir );
+ }
+ }
+
+ return false;
+}
diff --git a/plugins/faustwp/package.json b/plugins/faustwp/package.json
index 0d1e80cba..99019bdae 100644
--- a/plugins/faustwp/package.json
+++ b/plugins/faustwp/package.json
@@ -1,5 +1,5 @@
{
"name": "@faustwp/wordpress-plugin",
- "version": "1.5.0",
+ "version": "1.5.1",
"private": true
}
diff --git a/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php b/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php
index 69afb2531..d781da775 100644
--- a/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php
+++ b/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php
@@ -7,17 +7,19 @@
namespace WPE\FaustWP\Tests\Integration;
-use function WPE\FaustWP\Replacement\{
- content_replacement,
+use function WPE\FaustWP\Replacement\{content_replacement,
+ faustwp_get_wp_site_urls,
post_preview_link,
preview_link_in_rest_response,
image_source_replacement,
image_source_srcset_replacement,
- post_link,
-};
+ post_link};
use function WPE\FaustWP\Settings\faustwp_update_setting;
-use WP_REST_Response;
+/**
+ * @group replacement-callback
+ * @group callback
+ */
class ReplacementCallbacksTests extends \WP_UnitTestCase {
protected $post_id;
protected $draft_post_id;
@@ -35,6 +37,7 @@ public function setUp(): void {
'post_content' => 'Hi',
'post_status' => 'draft',
] );
+
}
public function test_the_content_filter() {
@@ -64,7 +67,7 @@ public function test_page_link_filter() {
public function test_term_link_filter() {
$this->assertSame( 1000, has_action( 'term_link', 'WPE\FaustWP\Replacement\term_link' ) );
}
-
+
public function test_post_type_link_filter() {
$this->assertSame( 1000, has_filter( 'post_type_link', 'WPE\FaustWP\Replacement\post_link' ) );
}
@@ -396,4 +399,185 @@ public function get_home_url( $url ) {
return "https://example.com/";
}
+ /**
+ * Test to make sure that site url is replaced for both content and media
+ */
+ public function test_content_replacement_for_content_and_media_urls_for_different_http_protocols() {
+
+ $frontend_uri = 'http://localhost:3000';
+ $site_url = 'http://example.org';
+ $site_url_secure = 'https://example.org';
+
+
+ faustwp_update_setting( 'frontend_uri', $frontend_uri );
+ faustwp_update_setting( 'enable_rewrites', '1' );
+ faustwp_update_setting( 'enable_image_source', '0' );
+
+ $contentExample1 = <<
+
+ Hello World
+
+HTML;
+
+ $contentExample2 = <<
+
+ Hello World
+
+HTML;
+
+ $expected_content = <<
+
+ Hello World
+
+HTML;
+
+ $this->assertSame( $expected_content, content_replacement( $contentExample1 ) );
+ $this->assertSame( $expected_content, content_replacement( $contentExample2 ) );
+ }
+
+
+ /**
+ * Test to make sure that site url is replaced for just media and not content
+ */
+ public function test_content_replacement_for_just_media_urls_for_different_http_protocols() {
+
+ $frontend_uri = 'http://localhost:3000';
+ $site_url = 'http://example.org';
+ $site_url_secure = 'https://example.org';
+
+ faustwp_update_setting( 'frontend_uri', $frontend_uri );
+ faustwp_update_setting( 'enable_rewrites', '0' );
+ faustwp_update_setting( 'enable_image_source', '0' );
+
+ $content = <<
+
+ Hello World
+
+HTML;
+
+ $expected_content = <<
+
+ Hello World
+
+HTML;
+
+ $this->assertSame( $expected_content, content_replacement( $content ) );
+
+
+ $content = <<
+
+ Hello World
+
+HTML;
+
+ $expected_content = <<
+
+ Hello World
+
+HTML;
+ $this->assertSame( $expected_content, content_replacement( $content ) );
+ }
+
+
+ /**
+ * Test to make sure that site url is replaced for just content and not media
+ */
+ public function test_content_replacement_for_just_content_urls_for_different_http_protocols() {
+
+ $frontend_uri = 'http://localhost:3000';
+ $site_url = 'http://example.org';
+ $site_url_secure = 'https://example.org';
+
+ faustwp_update_setting( 'frontend_uri', $frontend_uri );
+ faustwp_update_setting( 'enable_rewrites', '1' );
+ faustwp_update_setting( 'enable_image_source', '1' );
+
+ $content = <<
+
+ Hello World
+
+HTML;
+
+ $expected_content = <<
+
+ Hello World
+
+HTML;
+
+ $this->assertSame( $expected_content, content_replacement( $content ) );
+
+ $content = <<
+
+ Hello World
+
+HTML;
+
+ $expected_content = <<
+
+ Hello World
+
+HTML;
+
+ $this->assertSame( $expected_content, content_replacement( $content ) );
+
+ }
+
+
+ public function test_content_replacement_for_content_and_media_urls_and_add_site_to_available_site_urls() {
+ $frontend_uri = 'http://localhost:3000';
+ $site_url = 'http://example.org';
+ $site_url_secure = 'https://example.org';
+ $additional_site_url = 'https://subdomain.example.org';
+
+
+ faustwp_update_setting( 'frontend_uri', $frontend_uri );
+ faustwp_update_setting( 'enable_rewrites', '1' );
+ faustwp_update_setting( 'enable_image_source', '0' );
+
+ add_filter( 'faustwp_get_wp_site_urls', function ( $site_urls ) {
+ return array_merge( $site_urls, [ 'https://subdomain.example.org' ] );
+ });
+
+ $this->assertSame(faustwp_get_wp_site_urls(), [
+ 'https://example.org',
+ 'http://example.org',
+ '//example.org',
+ $additional_site_url
+ ] );
+
+ $content = <<
+
+ Hello World
+
+Find more information about us here
+
+
+
+HTML;
+
+ $expected_content = <<
+
+ Hello World
+
+Find more information about us here
+
+
+
+HTML;
+ $this->assertSame( $expected_content, content_replacement( $content ) );
+ }
+
}
From 70a67ef28706ba2404e19a8a8433d5894f33ab19 Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Tue, 24 Dec 2024 15:10:27 +0000
Subject: [PATCH 2/4] Fixed issues with PHPCS
---
.../includes/replacement/callbacks.php | 11 ++++-----
.../includes/replacement/functions.php | 24 +++++++++----------
2 files changed, 15 insertions(+), 20 deletions(-)
diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php
index 3e30f0e71..cfd9599e3 100644
--- a/plugins/faustwp/includes/replacement/callbacks.php
+++ b/plugins/faustwp/includes/replacement/callbacks.php
@@ -40,8 +40,8 @@ function content_replacement( string $content ): string {
return $content;
}
- $wp_site_urls = faustwp_get_wp_site_urls();
- if (empty($wp_site_urls)) {
+ $wp_site_urls = faustwp_get_wp_site_urls();
+ if ( empty( $wp_site_urls ) ) {
return $content;
}
@@ -53,7 +53,6 @@ function content_replacement( string $content ): string {
}
if ( $replace_content_urls && $replace_media_urls ) {
-
foreach ( $wp_site_urls as $site_url ) {
$content = str_replace( $site_url, $frontend_uri, $content );
}
@@ -61,7 +60,6 @@ function content_replacement( string $content ): string {
return $content;
}
-
if ( $replace_media_urls ) {
$wp_media_site_url = $frontend_uri . $relative_upload_url;
@@ -73,8 +71,8 @@ function content_replacement( string $content ): string {
}
foreach ( $wp_site_urls as $site_url ) {
- $pattern_exclude_media_urls = "#" . preg_quote( $site_url, '#' ) . "(?!{$relative_upload_url}(\/|$))#";
- $content = preg_replace( $pattern_exclude_media_urls, $frontend_uri, $content );
+ $pattern_exclude_media_urls = '#' . preg_quote( $site_url, '#' ) . "(?!{$relative_upload_url}(\/|$))#";
+ $content = preg_replace( $pattern_exclude_media_urls, $frontend_uri, $content );
}
return $content;
@@ -116,7 +114,6 @@ function image_source_srcset_replacement( $sources ) {
$frontend_uri = faustwp_get_setting( 'frontend_uri' );
$site_url = site_url();
-
/**
* For urls with no domain or the frontend domain, replace with the WP site_url.
* This was the default replacement pattern until Faust 1.2, at which point this
diff --git a/plugins/faustwp/includes/replacement/functions.php b/plugins/faustwp/includes/replacement/functions.php
index 7cc75e4f6..715a85ba8 100644
--- a/plugins/faustwp/includes/replacement/functions.php
+++ b/plugins/faustwp/includes/replacement/functions.php
@@ -133,26 +133,23 @@ function is_wp_link_ajax_request(): bool {
}
/**
- * Get all site URLs for each HTTP schema
- *
- * @return array
+ * Get all site URLs for each HTTP protocol
*/
function faustwp_get_wp_site_urls() {
$site_url = site_url();
- $host_url = parse_url( $site_url, PHP_URL_HOST );
+ $host_url = wp_parse_url( $site_url, PHP_URL_HOST );
if ( is_string( $host_url ) ) {
- $urls = [
+ $urls = array(
'https://' . $host_url,
'http://' . $host_url,
'//' . $host_url,
- ];
+ );
} else {
- $urls = [ $site_url ];
+ $urls = array( $site_url );
}
-
return apply_filters( 'faustwp_get_wp_site_urls', $urls );
}
@@ -166,10 +163,10 @@ function faustwp_get_wp_media_urls() {
$upload_url = faustwp_get_relative_upload_url( $site_urls );
if ( ! is_string( $upload_url ) ) {
- return apply_filters( 'faustwp_get_wp_site_media_urls', [] );
+ return apply_filters( 'faustwp_get_wp_site_media_urls', array() );
}
- $media_urls = [];
+ $media_urls = array();
foreach ( $site_urls as $site_url ) {
$media_urls[] = $site_url . $upload_url;
}
@@ -179,11 +176,12 @@ function faustwp_get_wp_media_urls() {
/**
- * @param array $site_urls
+ * Gets the relative wp-content upload URL.
*
- * @return false|string
+ * @param array $site_urls An array of site URLs.
+ * @return string The relative upload URL.
*/
-function faustwp_get_relative_upload_url( array $site_urls ) {
+function faustwp_get_relative_upload_url( $site_urls ) {
$upload_dir = wp_upload_dir()['baseurl'];
foreach ( $site_urls as $site_url ) {
if ( strpos( $upload_dir, $site_url ) !== false ) {
From 03a33a9ae9c615e9d12425854acfe9aeeb62f6ae Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Mon, 30 Dec 2024 14:35:13 +0000
Subject: [PATCH 3/4] Updated PR to replace srcset URL for different HTTP
Protocols
---
plugins/faustwp/faustwp.php | 2 +-
.../includes/replacement/callbacks.php | 78 +++++++++++--------
.../includes/replacement/functions.php | 38 +++++++--
plugins/faustwp/package.json | 2 +-
.../integration/ReplacementCallbacksTests.php | 72 ++++++++++++++---
5 files changed, 140 insertions(+), 52 deletions(-)
diff --git a/plugins/faustwp/faustwp.php b/plugins/faustwp/faustwp.php
index 01887c660..033de3d38 100644
--- a/plugins/faustwp/faustwp.php
+++ b/plugins/faustwp/faustwp.php
@@ -9,7 +9,7 @@
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: faustwp
* Domain Path: /languages
- * Version: 1.5.1
+ * Version: 1.5.0
* Requires PHP: 7.2
* Requires at least: 5.7
* Update URI: false
diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php
index cfd9599e3..afb3de1f8 100644
--- a/plugins/faustwp/includes/replacement/callbacks.php
+++ b/plugins/faustwp/includes/replacement/callbacks.php
@@ -63,11 +63,7 @@ function content_replacement( string $content ): string {
if ( $replace_media_urls ) {
$wp_media_site_url = $frontend_uri . $relative_upload_url;
- foreach ( $wp_media_urls as $media_url ) {
- $content = str_replace( $media_url, $wp_media_site_url, $content );
- }
-
- return $content;
+ return faustwp_replace_media_url( $content, $wp_media_urls, $wp_media_site_url );
}
foreach ( $wp_site_urls as $site_url ) {
@@ -98,6 +94,7 @@ function image_source_replacement( $content ) {
"#src=\"{$frontend_uri}/#",
'#src="/#',
);
+
return preg_replace( $patterns, "src=\"{$site_url}/", $content );
}
@@ -107,39 +104,44 @@ function image_source_replacement( $content ) {
*
* @param array $sources One or more arrays of source data to include in the 'srcset'.
*
- * @return string One or more arrays of source data.
+ * @return array One or more arrays of source data.
*/
function image_source_srcset_replacement( $sources ) {
- $use_wp_domain_for_media = use_wp_domain_for_media();
- $frontend_uri = faustwp_get_setting( 'frontend_uri' );
- $site_url = site_url();
-
- /**
- * For urls with no domain or the frontend domain, replace with the WP site_url.
- * This was the default replacement pattern until Faust 1.2, at which point this
- * was adjusted to correct replacement bugs.
- */
- $patterns = array(
- "#^{$site_url}/#",
- '#^/#',
- );
- $replacement = $frontend_uri;
+ if ( ! is_array( $sources ) || empty( $sources ) ) {
+ return $sources;
+ }
- /**
- * If using WP domain for media and a frontend URL is encountered, rewrite it to WP URL.
- */
- if ( $use_wp_domain_for_media ) {
- $patterns = array(
- "#^{$frontend_uri}/#",
- '#^/#',
- );
- $replacement = $site_url;
+ $replace_media_urls = ! use_wp_domain_for_media();
+ $wp_site_urls = faustwp_get_wp_site_urls();
+ if ( empty( $wp_site_urls ) ) {
+ return $sources;
}
- if ( is_array( $sources ) ) {
- foreach ( $sources as $width => $source ) {
- $sources[ $width ]['url'] = preg_replace( $patterns, "$replacement/", $source['url'] );
+ $wp_media_urls = faustwp_get_wp_media_urls();
+ $relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls );
+ $frontend_uri = faustwp_get_setting( 'frontend_uri' );
+
+ $wp_media_site_url = $frontend_uri . $relative_upload_url;
+ $patterns = array(
+ "#^{$frontend_uri}/#",
+ '#^/#',
+ );
+
+ foreach ( $sources as $width => $source ) {
+ if ( $replace_media_urls ) {
+ if ( substr( $source['url'], 0, strlen( $relative_upload_url ) ) === $relative_upload_url ) {
+ $sources[ $width ]['url'] = $frontend_uri . $source['url'];
+ } else {
+ $sources[ $width ]['url'] = faustwp_replace_media_url( $source['url'], $wp_media_urls, $wp_media_site_url );
+ }
+ } else {
+ $url = $source['url'];
+
+ foreach ( $wp_site_urls as $wp_site_url ) {
+ $url = preg_replace( $patterns, $wp_site_url . '/', $url );
+ }
+ $sources[ $width ]['url'] = $url;
}
}
@@ -252,7 +254,15 @@ function post_preview_link( $link, $post ) {
*/
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' );
+ $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 ) {
@@ -263,7 +273,7 @@ function post_link( $link ) {
if ( in_array( $pagenow, $target_pages, true )
&& is_ajax_generate_permalink_request()
) {
- return $link;
+ return $link;
}
if (
diff --git a/plugins/faustwp/includes/replacement/functions.php b/plugins/faustwp/includes/replacement/functions.php
index 715a85ba8..3b753f3a8 100644
--- a/plugins/faustwp/includes/replacement/functions.php
+++ b/plugins/faustwp/includes/replacement/functions.php
@@ -133,7 +133,7 @@ function is_wp_link_ajax_request(): bool {
}
/**
- * Get all site URLs for each HTTP protocol
+ * Get all site URLs for each possible HTTP protocol
*/
function faustwp_get_wp_site_urls() {
@@ -141,11 +141,19 @@ function faustwp_get_wp_site_urls() {
$host_url = wp_parse_url( $site_url, PHP_URL_HOST );
if ( is_string( $host_url ) ) {
- $urls = array(
- 'https://' . $host_url,
- 'http://' . $host_url,
- '//' . $host_url,
- );
+ if ( substr( $site_url, 0, 5 ) === 'http:' ) {
+ $urls = array(
+ 'http://' . $host_url,
+ 'https://' . $host_url,
+ '//' . $host_url,
+ );
+ } else {
+ $urls = array(
+ 'https://' . $host_url,
+ 'http://' . $host_url,
+ '//' . $host_url,
+ );
+ }
} else {
$urls = array( $site_url );
}
@@ -191,3 +199,21 @@ function faustwp_get_relative_upload_url( $site_urls ) {
return false;
}
+
+/***
+ * Replaces the media URL for various media urls
+ *
+ * @param string $content The content to be updated with the new media URL.
+ * @param array $wp_media_urls An array of media URLS.
+ * @param string $replace_url The media URL to be updated to.
+ *
+ * @return string
+ */
+function faustwp_replace_media_url( string $content, array $wp_media_urls, string $replace_url ) {
+
+ foreach ( $wp_media_urls as $media_url ) {
+ $content = str_replace( $media_url, $replace_url, $content );
+ }
+
+ return (string) $content;
+}
diff --git a/plugins/faustwp/package.json b/plugins/faustwp/package.json
index 99019bdae..0d1e80cba 100644
--- a/plugins/faustwp/package.json
+++ b/plugins/faustwp/package.json
@@ -1,5 +1,5 @@
{
"name": "@faustwp/wordpress-plugin",
- "version": "1.5.1",
+ "version": "1.5.0",
"private": true
}
diff --git a/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php b/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php
index d781da775..f146656a2 100644
--- a/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php
+++ b/plugins/faustwp/tests/integration/ReplacementCallbacksTests.php
@@ -405,8 +405,8 @@ public function get_home_url( $url ) {
public function test_content_replacement_for_content_and_media_urls_for_different_http_protocols() {
$frontend_uri = 'http://localhost:3000';
- $site_url = 'http://example.org';
- $site_url_secure = 'https://example.org';
+ $site_url = site_url();
+ $site_url_secure = str_replace('http:', 'https:', $site_url);
faustwp_update_setting( 'frontend_uri', $frontend_uri );
@@ -445,8 +445,8 @@ public function test_content_replacement_for_content_and_media_urls_for_differen
public function test_content_replacement_for_just_media_urls_for_different_http_protocols() {
$frontend_uri = 'http://localhost:3000';
- $site_url = 'http://example.org';
- $site_url_secure = 'https://example.org';
+ $site_url = site_url();
+ $site_url_secure = str_replace('http:', 'https:', $site_url);
faustwp_update_setting( 'frontend_uri', $frontend_uri );
faustwp_update_setting( 'enable_rewrites', '0' );
@@ -492,8 +492,8 @@ public function test_content_replacement_for_just_media_urls_for_different_http_
public function test_content_replacement_for_just_content_urls_for_different_http_protocols() {
$frontend_uri = 'http://localhost:3000';
- $site_url = 'http://example.org';
- $site_url_secure = 'https://example.org';
+ $site_url = site_url();
+ $site_url_secure = str_replace('http:', 'https:', $site_url);
faustwp_update_setting( 'frontend_uri', $frontend_uri );
faustwp_update_setting( 'enable_rewrites', '1' );
@@ -536,8 +536,8 @@ public function test_content_replacement_for_just_content_urls_for_different_htt
public function test_content_replacement_for_content_and_media_urls_and_add_site_to_available_site_urls() {
$frontend_uri = 'http://localhost:3000';
- $site_url = 'http://example.org';
- $site_url_secure = 'https://example.org';
+ $site_url = site_url();
+ $site_url_secure = str_replace('http:', 'https:', $site_url);
$additional_site_url = 'https://subdomain.example.org';
@@ -550,8 +550,8 @@ public function test_content_replacement_for_content_and_media_urls_and_add_site
});
$this->assertSame(faustwp_get_wp_site_urls(), [
- 'https://example.org',
- 'http://example.org',
+ $site_url,
+ $site_url_secure,
'//example.org',
$additional_site_url
] );
@@ -580,4 +580,56 @@ public function test_content_replacement_for_content_and_media_urls_and_add_site
$this->assertSame( $expected_content, content_replacement( $content ) );
}
+
+ public function test_image_sourceset_replacement_for_different_http_protocols_with_media_replacement_enabled() {
+
+ $frontend_uri = 'http://localhost:3000';
+ $site_url = site_url();
+ $site_url_secure = str_replace('http:', 'https:', $site_url);
+
+ faustwp_update_setting( 'frontend_uri', $frontend_uri );
+ faustwp_update_setting( 'enable_rewrites', '1' );
+ faustwp_update_setting( 'enable_image_source', '0' );
+
+ $sources = array (
+ 100 => array('url' => $site_url . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp'),
+ 300 => array('url' => $site_url_secure . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp'),
+ 400 => array('url' => '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1.webp'),
+ );
+
+ $expected = array (
+ 100 => array('url' => $frontend_uri . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp'),
+ 300 => array('url' => $frontend_uri . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp'),
+ 400 => array('url' => $frontend_uri . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1.webp'),
+ );
+
+ $this->assertSame( $expected, image_source_srcset_replacement( $sources ) );
+ }
+
+
+ public function test_image_sourceset_replacement_for_different_http_protocols_with_media_replacement_disabled() {
+
+ $frontend_uri = 'http://localhost:3000';
+ $site_url = site_url();
+ $site_url_secure = str_replace('http:', 'https:', $site_url);
+
+ faustwp_update_setting( 'frontend_uri', $frontend_uri );
+ faustwp_update_setting( 'enable_rewrites', '1' );
+ faustwp_update_setting( 'enable_image_source', '1' );
+
+ $sources = array (
+ 100 => array('url' => $site_url . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp'),
+ 300 => array('url' => $site_url_secure . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp'),
+ 400 => array('url' => '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1.webp'),
+ );
+
+ $expected = array (
+ 100 => array('url' => $site_url . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp'),
+ 300 => array('url' => $site_url_secure . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1-300x155.webp'),
+ 400 => array('url' => $site_url . '/wp-content/uploads/sites/2/2024/12/WP-Engine-Blue-888x459-1.webp'),
+ );
+
+ $this->assertSame( $expected, image_source_srcset_replacement( $sources ) );
+ }
+
}
From be14c9ad4924f49758a073643bdc3ddfdb1202e2 Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Mon, 30 Dec 2024 14:45:29 +0000
Subject: [PATCH 4/4] Added changeset
---
.changeset/tidy-toys-bake.md | 14 ++++++++++++++
1 file changed, 14 insertions(+)
create mode 100644 .changeset/tidy-toys-bake.md
diff --git a/.changeset/tidy-toys-bake.md b/.changeset/tidy-toys-bake.md
new file mode 100644
index 000000000..05c249552
--- /dev/null
+++ b/.changeset/tidy-toys-bake.md
@@ -0,0 +1,14 @@
+---
+'@faustwp/wordpress-plugin': minor
+---
+
+### Fixes
+
+- Fixes various issues with content replacement callback functions and replacing the site_url and media_urls
+- Fixed an issue with content replacement when media replacement was disabled and rewrites enabled, it was overwriting and updating the media URL to the frontend URL rather than leaving it as the original site URL
+
+
+### Added
+
+- Added 6.6 and 6.7 to Github Actions
+- Added 2 new filters for `faustwp_get_wp_site_urls` and `faustwp_get_wp_site_media_urls` to allow users add/remove/edit site and media URLS for the content replacement.