From 2be8bfee0842991cc38fd5015cf129d15ea02f6c Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Tue, 24 Dec 2024 14:29:58 +0000
Subject: [PATCH 01/12] 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 02/12] 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 03/12] 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 04/12] 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.
From c6ed6d5afd542aa57a2216b5c3eb1c28f9d2c51a Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Wed, 8 Jan 2025 18:34:15 +0000
Subject: [PATCH 05/12] Refactoring based off @whoami-pwd feedback. Reduced
some of the nesting for cotent replacement.
---
.../includes/replacement/callbacks.php | 38 +++++------
.../includes/replacement/functions.php | 64 ++++++++++++-------
2 files changed, 57 insertions(+), 45 deletions(-)
diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php
index afb3de1f8..16e33d765 100644
--- a/plugins/faustwp/includes/replacement/callbacks.php
+++ b/plugins/faustwp/includes/replacement/callbacks.php
@@ -33,6 +33,10 @@
*/
function content_replacement( string $content ): string {
+ if ( ! $content ) {
+ return '';
+ }
+
$replace_content_urls = domain_replacement_enabled();
$replace_media_urls = ! use_wp_domain_for_media();
@@ -53,17 +57,11 @@ 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 );
- }
-
- return $content;
+ return str_replace( $wp_site_urls, $frontend_uri, $content );
}
if ( $replace_media_urls ) {
- $wp_media_site_url = $frontend_uri . $relative_upload_url;
-
- return faustwp_replace_media_url( $content, $wp_media_urls, $wp_media_site_url );
+ return faustwp_replace_media_url( $content, $wp_media_urls, $frontend_uri . $relative_upload_url );
}
foreach ( $wp_site_urls as $site_url ) {
@@ -129,20 +127,18 @@ function image_source_srcset_replacement( $sources ) {
);
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;
+
+ if ( ! $replace_media_urls ) {
+ $sources[ $width ]['url'] = faustwp_replace_urls( $patterns, $wp_site_urls, $source['url'] );;
+ continue;
}
+
+ if ( strpos( $source['url'], $relative_upload_url ) === 0 ) {
+ $sources[ $width ]['url'] = $frontend_uri . $source['url'];
+ continue;
+ }
+
+ $sources[ $width ]['url'] = faustwp_replace_media_url( $source['url'], $wp_media_urls, $wp_media_site_url );
}
return $sources;
diff --git a/plugins/faustwp/includes/replacement/functions.php b/plugins/faustwp/includes/replacement/functions.php
index 3b753f3a8..f69da13da 100644
--- a/plugins/faustwp/includes/replacement/functions.php
+++ b/plugins/faustwp/includes/replacement/functions.php
@@ -140,25 +140,20 @@ function faustwp_get_wp_site_urls() {
$site_url = site_url();
$host_url = wp_parse_url( $site_url, PHP_URL_HOST );
- if ( is_string( $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 );
+ if ( ! is_string( $host_url ) ) {
+ return apply_filters( 'faustwp_get_wp_site_urls', [ $site_url ] );
}
- return apply_filters( 'faustwp_get_wp_site_urls', $urls );
+ $is_https = substr( $site_url, 0, 6 ) === 'https:';
+
+ return apply_filters(
+ 'faustwp_get_wp_site_urls',
+ array(
+ $is_https ? "https://$host_url" : "http://$host_url",
+ $is_https ? "http://$host_url" : "https://$host_url",
+ "//$host_url"
+ )
+ );
}
/**
@@ -187,33 +182,54 @@ function faustwp_get_wp_media_urls() {
* Gets the relative wp-content upload URL.
*
* @param array $site_urls An array of site URLs.
+ *
* @return string The relative upload URL.
*/
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 ) {
+ if ( false !== strpos( $upload_dir, $site_url ) ) {
return (string) str_replace( $site_url, '', $upload_dir );
}
}
- return false;
+ return '';
}
/***
* 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 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 ) {
+ return str_replace( $wp_media_urls, $replace_url, $content );
+}
- foreach ( $wp_media_urls as $media_url ) {
- $content = str_replace( $media_url, $replace_url, $content );
- }
- return (string) $content;
+/**
+ * @param array $patterns
+ * @param mixed $wp_site_urls
+ * @param mixed $url
+ *
+ * @return array|string|string[]|null
+ */
+function faustwp_replace_urls( array $patterns, mixed $wp_site_urls, mixed $url ) {
+ $i = 0;
+
+ return preg_replace_callback(
+ $patterns,
+ function () use ( &$wp_site_urls, &$i ) {
+ $replacement = $wp_site_urls[ $i ] . '/';
+ $i ++;
+
+ return $replacement;
+ },
+ $url
+ );
}
From 400e7fc7383b31dd7fee5505cf964513dee4570d Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Wed, 8 Jan 2025 19:10:49 +0000
Subject: [PATCH 06/12] Fixes for linting
---
.../includes/replacement/callbacks.php | 4 +--
.../includes/replacement/functions.php | 25 ++++++++++---------
2 files changed, 15 insertions(+), 14 deletions(-)
diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php
index 16e33d765..67aa921b9 100644
--- a/plugins/faustwp/includes/replacement/callbacks.php
+++ b/plugins/faustwp/includes/replacement/callbacks.php
@@ -127,9 +127,9 @@ function image_source_srcset_replacement( $sources ) {
);
foreach ( $sources as $width => $source ) {
-
if ( ! $replace_media_urls ) {
- $sources[ $width ]['url'] = faustwp_replace_urls( $patterns, $wp_site_urls, $source['url'] );;
+ $sources[ $width ]['url'] = faustwp_replace_urls( $patterns, $wp_site_urls, $source['url'] );
+
continue;
}
diff --git a/plugins/faustwp/includes/replacement/functions.php b/plugins/faustwp/includes/replacement/functions.php
index f69da13da..330e78ac5 100644
--- a/plugins/faustwp/includes/replacement/functions.php
+++ b/plugins/faustwp/includes/replacement/functions.php
@@ -141,7 +141,7 @@ function faustwp_get_wp_site_urls() {
$host_url = wp_parse_url( $site_url, PHP_URL_HOST );
if ( ! is_string( $host_url ) ) {
- return apply_filters( 'faustwp_get_wp_site_urls', [ $site_url ] );
+ return apply_filters( 'faustwp_get_wp_site_urls', array( $site_url ) );
}
$is_https = substr( $site_url, 0, 6 ) === 'https:';
@@ -151,7 +151,7 @@ function faustwp_get_wp_site_urls() {
array(
$is_https ? "https://$host_url" : "http://$host_url",
$is_https ? "http://$host_url" : "https://$host_url",
- "//$host_url"
+ "//$host_url",
)
);
}
@@ -188,7 +188,6 @@ function faustwp_get_wp_media_urls() {
function faustwp_get_relative_upload_url( $site_urls ) {
$upload_dir = wp_upload_dir()['baseurl'];
-
foreach ( $site_urls as $site_url ) {
if ( false !== strpos( $upload_dir, $site_url ) ) {
return (string) str_replace( $site_url, '', $upload_dir );
@@ -202,10 +201,10 @@ function faustwp_get_relative_upload_url( $site_urls ) {
* 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 array $wp_media_urls An array of media URLS.
* @param string $replace_url The media URL to be updated to.
*
- * @return string
+ * @return string The replaced string
*/
function faustwp_replace_media_url( string $content, array $wp_media_urls, string $replace_url ) {
return str_replace( $wp_media_urls, $replace_url, $content );
@@ -213,23 +212,25 @@ function faustwp_replace_media_url( string $content, array $wp_media_urls, strin
/**
- * @param array $patterns
- * @param mixed $wp_site_urls
- * @param mixed $url
+ * Replaces urls for multiple patterns
+ *
+ * @param array $patterns The array of patterns.
+ * @param mixed $wp_site_urls The array of site URLs.
+ * @param mixed $content The content to be updated.
*
- * @return array|string|string[]|null
+ * @return mixed The replaced content
*/
-function faustwp_replace_urls( array $patterns, mixed $wp_site_urls, mixed $url ) {
+function faustwp_replace_urls( array $patterns, mixed $wp_site_urls, mixed $content ) {
$i = 0;
return preg_replace_callback(
$patterns,
function () use ( &$wp_site_urls, &$i ) {
$replacement = $wp_site_urls[ $i ] . '/';
- $i ++;
+ $i++;
return $replacement;
},
- $url
+ $content
);
}
From acabb9e8971bc891e63a129e71c4bc9f7890636d Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Thu, 9 Jan 2025 16:04:19 +0000
Subject: [PATCH 07/12] Update
plugins/faustwp/includes/replacement/callbacks.php
Co-authored-by: Alex K. <33621842+whoami-pwd@users.noreply.github.com>
---
plugins/faustwp/includes/replacement/callbacks.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php
index 67aa921b9..6d086afa1 100644
--- a/plugins/faustwp/includes/replacement/callbacks.php
+++ b/plugins/faustwp/includes/replacement/callbacks.php
@@ -51,7 +51,7 @@ function content_replacement( string $content ): string {
$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' );
+ $frontend_uri = (string) faustwp_get_setting( 'frontend_uri' ) ?: '/';
if ( ! $frontend_uri ) {
$frontend_uri = '/';
}
From 8e9a232352f4f6922a1e5f999b71bb4dc6001116 Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Thu, 9 Jan 2025 19:20:12 +0000
Subject: [PATCH 08/12] Added fixes as per code review from @whoami-pwd
---
.../includes/replacement/callbacks.php | 33 ++++++++-----
.../includes/replacement/functions.php | 46 ++++---------------
2 files changed, 32 insertions(+), 47 deletions(-)
diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php
index 6d086afa1..5c23c195b 100644
--- a/plugins/faustwp/includes/replacement/callbacks.php
+++ b/plugins/faustwp/includes/replacement/callbacks.php
@@ -49,9 +49,9 @@ function content_replacement( string $content ): string {
return $content;
}
- $wp_media_urls = faustwp_get_wp_media_urls();
+ $wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls );
$relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls );
- $frontend_uri = (string) faustwp_get_setting( 'frontend_uri' ) ?: '/';
+ $frontend_uri = (string) faustwp_get_setting( 'frontend_uri' );
if ( ! $frontend_uri ) {
$frontend_uri = '/';
}
@@ -64,12 +64,9 @@ function content_replacement( string $content ): string {
return faustwp_replace_media_url( $content, $wp_media_urls, $frontend_uri . $relative_upload_url );
}
- 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;
+ $site_urls_pattern = implode( '|', array_map( 'preg_quote', $wp_site_urls ) );
+ $pattern = '#(' . $site_urls_pattern . ')(?!' . $relative_upload_url . '(\/|$))#';
+ return preg_replace( $pattern, $frontend_uri, $content );
}
/**
@@ -98,7 +95,9 @@ function image_source_replacement( $content ) {
add_filter( 'wp_calculate_image_srcset', __NAMESPACE__ . '\\image_source_srcset_replacement' );
/**
- * Callback for WordPress 'the_content' filter to replace paths to media.
+ * Callback for WordPress 'wp_calculate_image_srcset' filter to replace paths when generating a srcset
+ *
+ * @link https://developer.wordpress.org/reference/functions/wp_calculate_image_srcset/
*
* @param array $sources One or more arrays of source data to include in the 'srcset'.
*
@@ -116,7 +115,7 @@ function image_source_srcset_replacement( $sources ) {
return $sources;
}
- $wp_media_urls = faustwp_get_wp_media_urls();
+ $wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls );
$relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls );
$frontend_uri = faustwp_get_setting( 'frontend_uri' );
@@ -128,7 +127,19 @@ function image_source_srcset_replacement( $sources ) {
foreach ( $sources as $width => $source ) {
if ( ! $replace_media_urls ) {
- $sources[ $width ]['url'] = faustwp_replace_urls( $patterns, $wp_site_urls, $source['url'] );
+ $i = 0;
+ $url = preg_replace_callback(
+ $patterns,
+ function () use ( &$wp_site_urls, &$i ) {
+ $replacement = $wp_site_urls[ $i ] . '/';
+ $i++;
+
+ return $replacement;
+ },
+ $source['url']
+ );
+
+ $sources[ $width ]['url'] = $url;
continue;
}
diff --git a/plugins/faustwp/includes/replacement/functions.php b/plugins/faustwp/includes/replacement/functions.php
index 330e78ac5..5cd67aa5f 100644
--- a/plugins/faustwp/includes/replacement/functions.php
+++ b/plugins/faustwp/includes/replacement/functions.php
@@ -134,17 +134,15 @@ function is_wp_link_ajax_request(): bool {
/**
* Get all site URLs for each possible HTTP protocol
+ *
+ * @return array An array of site urls.
*/
function faustwp_get_wp_site_urls() {
$site_url = site_url();
$host_url = wp_parse_url( $site_url, PHP_URL_HOST );
- if ( ! is_string( $host_url ) ) {
- return apply_filters( 'faustwp_get_wp_site_urls', array( $site_url ) );
- }
-
- $is_https = substr( $site_url, 0, 6 ) === 'https:';
+ $is_https = strpos( $site_url, 0, 6 ) === 'https:';
return apply_filters(
'faustwp_get_wp_site_urls',
@@ -159,18 +157,19 @@ function faustwp_get_wp_site_urls() {
/**
* Get all media urls based off the available site urls
*
- * @return array
+ * @param array $wp_site_urls The array of potential site urls.
+ *
+ * @return array The array of media Urls
*/
-function faustwp_get_wp_media_urls() {
- $site_urls = faustwp_get_wp_site_urls();
- $upload_url = faustwp_get_relative_upload_url( $site_urls );
+function faustwp_get_wp_media_urls( array $wp_site_urls ) {
+ $upload_url = faustwp_get_relative_upload_url( $wp_site_urls );
if ( ! is_string( $upload_url ) ) {
return apply_filters( 'faustwp_get_wp_site_media_urls', array() );
}
$media_urls = array();
- foreach ( $site_urls as $site_url ) {
+ foreach ( $wp_site_urls as $site_url ) {
$media_urls[] = $site_url . $upload_url;
}
@@ -189,7 +188,7 @@ function faustwp_get_relative_upload_url( $site_urls ) {
$upload_dir = wp_upload_dir()['baseurl'];
foreach ( $site_urls as $site_url ) {
- if ( false !== strpos( $upload_dir, $site_url ) ) {
+ if ( strpos( $upload_dir, $site_url ) === 0 ) {
return (string) str_replace( $site_url, '', $upload_dir );
}
}
@@ -209,28 +208,3 @@ function faustwp_get_relative_upload_url( $site_urls ) {
function faustwp_replace_media_url( string $content, array $wp_media_urls, string $replace_url ) {
return str_replace( $wp_media_urls, $replace_url, $content );
}
-
-
-/**
- * Replaces urls for multiple patterns
- *
- * @param array $patterns The array of patterns.
- * @param mixed $wp_site_urls The array of site URLs.
- * @param mixed $content The content to be updated.
- *
- * @return mixed The replaced content
- */
-function faustwp_replace_urls( array $patterns, mixed $wp_site_urls, mixed $content ) {
- $i = 0;
-
- return preg_replace_callback(
- $patterns,
- function () use ( &$wp_site_urls, &$i ) {
- $replacement = $wp_site_urls[ $i ] . '/';
- $i++;
-
- return $replacement;
- },
- $content
- );
-}
From 97706731fa55fc98f1a4d44e6f5bc0b901899991 Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Fri, 10 Jan 2025 14:05:16 +0000
Subject: [PATCH 09/12] Refactoring some of the image srcset replacement.
---
.../includes/replacement/callbacks.php | 42 ++++++++-----------
.../includes/replacement/functions.php | 13 ------
2 files changed, 18 insertions(+), 37 deletions(-)
diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php
index 5c23c195b..99d5105ba 100644
--- a/plugins/faustwp/includes/replacement/callbacks.php
+++ b/plugins/faustwp/includes/replacement/callbacks.php
@@ -61,11 +61,12 @@ function content_replacement( string $content ): string {
}
if ( $replace_media_urls ) {
- return faustwp_replace_media_url( $content, $wp_media_urls, $frontend_uri . $relative_upload_url );
+ 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 . '(\/|$))#';
+
return preg_replace( $pattern, $frontend_uri, $content );
}
@@ -109,15 +110,16 @@ function image_source_srcset_replacement( $sources ) {
return $sources;
}
- $replace_media_urls = ! use_wp_domain_for_media();
- $wp_site_urls = faustwp_get_wp_site_urls();
+ $wp_site_urls = faustwp_get_wp_site_urls();
if ( empty( $wp_site_urls ) ) {
return $sources;
}
+ $replace_media_urls = ! use_wp_domain_for_media();
$wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls );
$relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls );
$frontend_uri = faustwp_get_setting( 'frontend_uri' );
+ $site_url = site_url() . '/';
$wp_media_site_url = $frontend_uri . $relative_upload_url;
$patterns = array(
@@ -125,31 +127,23 @@ function image_source_srcset_replacement( $sources ) {
'#^/#',
);
+ /**
+ * Update each source with the correct replacement URL
+ */
foreach ( $sources as $width => $source ) {
- if ( ! $replace_media_urls ) {
- $i = 0;
- $url = preg_replace_callback(
- $patterns,
- function () use ( &$wp_site_urls, &$i ) {
- $replacement = $wp_site_urls[ $i ] . '/';
- $i++;
-
- return $replacement;
- },
- $source['url']
- );
-
- $sources[ $width ]['url'] = $url;
-
- continue;
- }
+ $url = $source['url'];
+
+ if ( $replace_media_urls ) {
+ $url = ( strpos( $url, $relative_upload_url ) === 0 )
+ ? $frontend_uri . $url
+ : str_replace( $wp_media_urls, $wp_media_site_url, $url );
+ } else {
- if ( strpos( $source['url'], $relative_upload_url ) === 0 ) {
- $sources[ $width ]['url'] = $frontend_uri . $source['url'];
- continue;
+ // We need to make sure that the frontend URL or relative URL (legacy) is updated with the site url
+ $url = preg_replace( $patterns, $site_url, $url );
}
- $sources[ $width ]['url'] = faustwp_replace_media_url( $source['url'], $wp_media_urls, $wp_media_site_url );
+ $sources[ $width ]['url'] = $url;
}
return $sources;
diff --git a/plugins/faustwp/includes/replacement/functions.php b/plugins/faustwp/includes/replacement/functions.php
index 5cd67aa5f..482e9dbee 100644
--- a/plugins/faustwp/includes/replacement/functions.php
+++ b/plugins/faustwp/includes/replacement/functions.php
@@ -195,16 +195,3 @@ function faustwp_get_relative_upload_url( $site_urls ) {
return '';
}
-
-/***
- * 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 The replaced string
- */
-function faustwp_replace_media_url( string $content, array $wp_media_urls, string $replace_url ) {
- return str_replace( $wp_media_urls, $replace_url, $content );
-}
From 71b1d6891c06f85df8081b4eace9ffa100fbf0a4 Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Fri, 10 Jan 2025 14:11:12 +0000
Subject: [PATCH 10/12] Small fix for PHPCS for a missing fullstop
---
plugins/faustwp/includes/replacement/callbacks.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php
index 99d5105ba..525768d47 100644
--- a/plugins/faustwp/includes/replacement/callbacks.php
+++ b/plugins/faustwp/includes/replacement/callbacks.php
@@ -139,7 +139,7 @@ function image_source_srcset_replacement( $sources ) {
: str_replace( $wp_media_urls, $wp_media_site_url, $url );
} else {
- // We need to make sure that the frontend URL or relative URL (legacy) is updated with the site url
+ // We need to make sure that the frontend URL or relative URL (legacy) is updated with the site url.
$url = preg_replace( $patterns, $site_url, $url );
}
From 58d19dff0f2cd598a64244677ff967a55d1fe243 Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Fri, 10 Jan 2025 17:55:05 +0000
Subject: [PATCH 11/12] Refactoring some of the media urls with @whoami-pwd
---
.../includes/replacement/callbacks.php | 31 +++++++++----------
.../includes/replacement/functions.php | 21 ++++++-------
2 files changed, 23 insertions(+), 29 deletions(-)
diff --git a/plugins/faustwp/includes/replacement/callbacks.php b/plugins/faustwp/includes/replacement/callbacks.php
index 525768d47..df500ed5a 100644
--- a/plugins/faustwp/includes/replacement/callbacks.php
+++ b/plugins/faustwp/includes/replacement/callbacks.php
@@ -49,12 +49,10 @@ function content_replacement( string $content ): string {
return $content;
}
- $wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls );
- $relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls );
- $frontend_uri = (string) faustwp_get_setting( 'frontend_uri' );
- if ( ! $frontend_uri ) {
- $frontend_uri = '/';
- }
+ $upload_url = wp_upload_dir()['baseurl'];
+ $wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls, $upload_url );
+ $relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls, $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 );
@@ -100,7 +98,7 @@ function image_source_replacement( $content ) {
*
* @link https://developer.wordpress.org/reference/functions/wp_calculate_image_srcset/
*
- * @param array $sources One or more arrays of source data to include in the 'srcset'.
+ * @param array $sources One or more arrays of source data to include in the 'srcset'.
*
* @return array One or more arrays of source data.
*/
@@ -116,9 +114,10 @@ function image_source_srcset_replacement( $sources ) {
}
$replace_media_urls = ! use_wp_domain_for_media();
- $wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls );
- $relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls );
- $frontend_uri = faustwp_get_setting( 'frontend_uri' );
+ $upload_url = wp_upload_dir()['baseurl'];
+ $wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls, $upload_url );
+ $relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls, $upload_url );
+ $frontend_uri = (string) faustwp_get_setting( 'frontend_uri' );
$site_url = site_url() . '/';
$wp_media_site_url = $frontend_uri . $relative_upload_url;
@@ -134,16 +133,14 @@ function image_source_srcset_replacement( $sources ) {
$url = $source['url'];
if ( $replace_media_urls ) {
- $url = ( strpos( $url, $relative_upload_url ) === 0 )
+ $sources[ $width ]['url'] = ( strpos( $url, $relative_upload_url ) === 0 )
? $frontend_uri . $url
- : str_replace( $wp_media_urls, $wp_media_site_url, $url );
- } else {
-
- // We need to make sure that the frontend URL or relative URL (legacy) is updated with the site url.
- $url = preg_replace( $patterns, $site_url, $url );
+ : str_replace( $wp_media_urls, $wp_media_site_url, $source['url'] );
+ continue;
}
- $sources[ $width ]['url'] = $url;
+ // We need to make sure that the frontend URL or relative URL (legacy) is updated with the site url.
+ $sources[ $width ]['url'] = preg_replace( $patterns, $site_url, $source['url'] );
}
return $sources;
diff --git a/plugins/faustwp/includes/replacement/functions.php b/plugins/faustwp/includes/replacement/functions.php
index 482e9dbee..18b78fe2c 100644
--- a/plugins/faustwp/includes/replacement/functions.php
+++ b/plugins/faustwp/includes/replacement/functions.php
@@ -126,10 +126,6 @@ function is_ajax_generate_permalink_request(): bool {
*/
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'] );
}
/**
@@ -158,19 +154,20 @@ function faustwp_get_wp_site_urls() {
* Get all media urls based off the available site urls
*
* @param array $wp_site_urls The array of potential site urls.
+ * @param string $upload_url An array of site URLs.
*
* @return array The array of media Urls
*/
-function faustwp_get_wp_media_urls( array $wp_site_urls ) {
- $upload_url = faustwp_get_relative_upload_url( $wp_site_urls );
+function faustwp_get_wp_media_urls( array $wp_site_urls, string $upload_url ) {
+ $relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls, $upload_url );
- if ( ! is_string( $upload_url ) ) {
+ if ( ! is_string( $relative_upload_url ) ) {
return apply_filters( 'faustwp_get_wp_site_media_urls', array() );
}
$media_urls = array();
foreach ( $wp_site_urls as $site_url ) {
- $media_urls[] = $site_url . $upload_url;
+ $media_urls[] = $site_url . $relative_upload_url;
}
return apply_filters( 'faustwp_get_wp_site_media_urls', $media_urls );
@@ -181,15 +178,15 @@ function faustwp_get_wp_media_urls( array $wp_site_urls ) {
* Gets the relative wp-content upload URL.
*
* @param array $site_urls An array of site URLs.
+ * @param string $upload_url An array of site URLs.
*
* @return string The relative upload URL.
*/
-function faustwp_get_relative_upload_url( $site_urls ) {
- $upload_dir = wp_upload_dir()['baseurl'];
+function faustwp_get_relative_upload_url( array $site_urls, string $upload_url = '' ): string {
foreach ( $site_urls as $site_url ) {
- if ( strpos( $upload_dir, $site_url ) === 0 ) {
- return (string) str_replace( $site_url, '', $upload_dir );
+ if ( strpos( $upload_url, $site_url ) === 0 ) {
+ return (string) str_replace( $site_url, '', $upload_url );
}
}
From e37f7b00b3994089e41833063ba6a9d633d7986b Mon Sep 17 00:00:00 2001
From: Colin Murphy
Date: Fri, 10 Jan 2025 18:00:37 +0000
Subject: [PATCH 12/12] Fixes
---
plugins/faustwp/includes/replacement/functions.php | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/plugins/faustwp/includes/replacement/functions.php b/plugins/faustwp/includes/replacement/functions.php
index 18b78fe2c..239cb48bb 100644
--- a/plugins/faustwp/includes/replacement/functions.php
+++ b/plugins/faustwp/includes/replacement/functions.php
@@ -126,6 +126,10 @@ function is_ajax_generate_permalink_request(): bool {
*/
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'] );
}
/**