Skip to content

Commit

Permalink
Fix for #1986. Fixes various issues with content replacement callback…
Browse files Browse the repository at this point in the history
… and also updates tests to use WP 6.6 and 6.7

Fixes various issues with using content replacement for the site URL.
  • Loading branch information
colinmurphy committed Dec 24, 2024
1 parent 836009b commit 2be8bfe
Show file tree
Hide file tree
Showing 8 changed files with 291 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-test-plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
2 changes: 1 addition & 1 deletion plugins/faustwp/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion plugins/faustwp/faustwp.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 32 additions & 17 deletions plugins/faustwp/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions plugins/faustwp/includes/replacement/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion plugins/faustwp/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@faustwp/wordpress-plugin",
"version": "1.5.0",
"version": "1.5.1",
"private": true
}
Loading

0 comments on commit 2be8bfe

Please sign in to comment.