Skip to content

Commit

Permalink
Merge branch 'canary' into MERL-1256
Browse files Browse the repository at this point in the history
  • Loading branch information
theodesp authored Nov 16, 2023
2 parents e3b4aa3 + 75f5c80 commit 2043b0e
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 37 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-books-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/wordpress-plugin': patch
---

Fixed a bug that caused links to files in wp-content to be rewritten to the Faust Front-end site URL when they should not have been.
5 changes: 5 additions & 0 deletions .changeset/yellow-foxes-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@faustwp/wordpress-plugin': patch
---

Fixed a bug where links were rewritten to the Faust Front-end Site URL when using the post editor, resulting in those rewritten links being saved to the post content and guid fields when they shouldn't be. These links are now saved with the URL pointing to the WP site, as they should be. They are still rewritten at runtime to link to the Front-end Site URL when appropriate.
2 changes: 1 addition & 1 deletion examples/next/app-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"@apollo/experimental-nextjs-app-support": "^0.5.1",
"@faustwp/cli": "^1.2.0",
"@faustwp/core": "^1.2.0",
"@faustwp/experimental-app-router": "^0.2.0",
"@faustwp/experimental-app-router": "^0.2.1",
"graphql": "^16.7.1",
"next": "^14.0.1",
"react": "^18.3.0-canary-ce2bc58a9-20231102",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"build:faust-cli": "npm run build --workspace=@faustwp/cli",
"build:faust-core": "npm run build --workspace=@faustwp/core",
"build:faust-blocks": "npm run build --workspace=@faustwp/blocks",
"build:experimentala-app-router": "npm run build --workspace=@faustwp/experimental-app-router",
"build:experimental-app-router": "npm run build --workspace=@faustwp/experimental-app-router",
"clean": "npm run clean --workspace=@faustjs/core --workspace=@faustjs/react --workspace=@faustjs/next --workspace=@faustwp/cli --workspace=@faustwp/core --workspace=@faustwp/experimental-app-router --workspace=@faustwp/block-editor-utils",
"clean:examples": "rimraf examples/**/node_modules",
"format": "npm run format --workspace=@faustjs/core --workspace=@faustjs/react --workspace=@faustjs/next --workspace=@faustwp/cli --workspace=@faustwp/core --workspace=@faustwp/experimental-app-router --workspace=@faustwp/block-editor-utils",
Expand All @@ -54,7 +54,7 @@
"version": "changeset version && node scripts/versionPlugin.js",
"version:nightly": "changeset version --snapshot && node scripts/versionPlugin.js",
"version:status": "changeset status",
"release": "npm run build && changeset publish",
"release": "npm run build && npm run build:experimental-app-router && changeset publish",
"release:nightly": "npm run build && changeset publish --tag canary",
"lint": "eslint ./packages --ext js,jsx,ts,tsx --max-warnings=0",
"lint:fix": "eslint ./packages --ext js,jsx,ts,tsx --max-warnings=0 --fix"
Expand Down
6 changes: 6 additions & 0 deletions packages/experimental-app-router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @faustwp/experimental-app-router

## 0.2.1

### Patch Changes

- 6276c80: Fix broken build from 0.2.0

## 0.2.0

### Minor Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/experimental-app-router/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@faustwp/experimental-app-router",
"version": "0.2.0",
"version": "0.2.1",
"description": "Experimental: A Faust package to support Next.js' App Router",
"exports": {
".": "./dist/index.js",
Expand Down
90 changes: 73 additions & 17 deletions plugins/faustwp/includes/replacement/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
use function WPE\FaustWP\Settings\{
faustwp_get_setting,
is_image_source_replacement_enabled,
is_rewrites_enabled
is_rewrites_enabled,
use_wp_domain_for_media,
use_wp_domain_for_post_and_category_urls,
};
use function WPE\FaustWP\Utilities\{
plugin_version,
Expand All @@ -21,32 +23,49 @@
}

add_filter( 'the_content', __NAMESPACE__ . '\\content_replacement' );
add_filter( 'wpgraphql_content_blocks_resolver_content', __NAMESPACE__ . '\\content_replacement' );
/**
* Callback for WordPress 'the_content' filter.
*
* @param string $content The post content.
*
* @return string The post content.
* @todo Needs work...
*/
function content_replacement( $content ) {
if ( ! domain_replacement_enabled() ) {
$use_wp_domain_for_permalinks = ! domain_replacement_enabled();
$use_wp_domain_for_media = use_wp_domain_for_media();

if ( $use_wp_domain_for_permalinks && $use_wp_domain_for_media ) {
return $content;
}

$replacement = faustwp_get_setting( 'frontend_uri' );
$site_url = site_url();

if ( ! $replacement ) {
$replacement = '/';
}

$content = str_replace( "href=\"{$site_url}", "href=\"{$replacement}", $content );
$site_url = site_url();
$media_dir = str_replace( $site_url, '', wp_upload_dir()['baseurl'] );
$media_url = $site_url . $media_dir;

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 );
return $content;
}

if ( ! $use_wp_domain_for_permalinks && $use_wp_domain_for_media ) {
$content = preg_replace( "#{$site_url}(?!{$media_dir})#", "{$replacement}", $content );
return $content;
}

return str_replace( 'href="//', 'href="/', $content );
return $content;
}

add_filter( 'the_content', __NAMESPACE__ . '\\image_source_replacement' );
/**
* Callback for WordPress 'the_content' filter to replace paths to media.
*
Expand Down Expand Up @@ -79,21 +98,36 @@ function image_source_replacement( $content ) {
* @return string One or more arrays of source data.
*/
function image_source_srcset_replacement( $sources ) {
if ( ! is_image_source_replacement_enabled() ) {
return $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}/#",
'#^/#',
);

$frontend_uri = faustwp_get_setting( 'frontend_uri' );
$site_url = site_url();
$replacement = $frontend_uri;

if ( is_array( $sources ) ) {
// For urls with no domain or the frontend domain, replace with the wp site_url.
$patterns = array(
/**
* 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;
}

if ( is_array( $sources ) ) {
foreach ( $sources as $width => $source ) {
$sources[ $width ]['url'] = preg_replace( $patterns, "$site_url/", $sources[ $width ]['url'] );
$sources[ $width ]['url'] = preg_replace( $patterns, "$replacement/", $source['url'] );
}
}

Expand Down Expand Up @@ -200,13 +234,35 @@ function post_preview_link( $link, $post ) {
* @return string URL used for the 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' );

// 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 ) {
return $link;
}

// Ajax requests to generate permalink.
if ( in_array( $pagenow, $target_pages, true )
&& is_ajax_generate_permalink_request()
) {
return $link;
}

if (
! is_rewrites_enabled()
|| ( function_exists( 'is_graphql_request' ) && is_graphql_request() )
// Block editor makes REST requests on these pages to query content.
|| ( in_array( $pagenow, $target_pages, true ) && current_user_can( 'edit_posts' ) && defined( 'REST_REQUEST' ) && REST_REQUEST )
) {
return $link;
}

// Check for wp-link-ajax requests. Used by Classic Editor when linking content.
if ( is_wp_link_ajax_request() ) {
return $link;
}

return equivalent_frontend_url( $link );
}

Expand Down
29 changes: 25 additions & 4 deletions plugins/faustwp/includes/replacement/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

use function WPE\FaustWP\Settings\{
faustwp_get_setting,
is_rewrites_enabled
is_rewrites_enabled,
use_wp_domain_for_post_and_category_urls,
};

if ( ! defined( 'ABSPATH' ) ) {
Expand All @@ -19,8 +20,6 @@
/**
* Determine if domain replacement can be done.
*
* Enabled if query string parameter 'replace-domain' is present.
*
* @return bool True if can proceed with replacement, false if else.
*/
function domain_replacement_enabled() {
Expand All @@ -31,7 +30,7 @@ function domain_replacement_enabled() {
*
* @param bool $enabled True if domain replacement is enabled, false if else.
*/
return apply_filters( 'faustwp_domain_replacement_enabled', is_rewrites_enabled() );
return apply_filters( 'faustwp_domain_replacement_enabled', ! use_wp_domain_for_post_and_category_urls() );
}

/**
Expand Down Expand Up @@ -110,3 +109,25 @@ function has_file_extension( $string ) {
return false; // String does not have a file extension.
}
}

/**
* Determines if an AJAX request to generate permalinks is in progress.
*
* @return boolean
*/
function is_ajax_generate_permalink_request(): bool {
return ( ! empty( $_POST['samplepermalinknonce'] ) && check_ajax_referer( 'samplepermalink', 'samplepermalinknonce' ) );
}

/**
* Determines if a wp-link-ajax request is in progress.
*
* @return boolean
*/
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'] );
}
1 change: 0 additions & 1 deletion plugins/faustwp/includes/replacement/graphql-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
exit;
}

add_filter( 'graphql_request_results', __NAMESPACE__ . '\\url_replacement' );
/**
* Callback for WP GraphQL 'graphql_request_results' filter.
*
Expand Down
17 changes: 17 additions & 0 deletions plugins/faustwp/includes/settings/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ function is_rewrites_enabled() {
return '1' === faustwp_get_setting( 'enable_rewrites' );
}

/**
* Determines if posts and category URLs should link to the WP site.
*
* @return bool
*/
function use_wp_domain_for_post_and_category_urls() {
return ! is_rewrites_enabled();
}

/**
* Determine if themes are disabled.
*
Expand All @@ -47,6 +56,14 @@ function is_image_source_replacement_enabled() {
return '1' === faustwp_get_setting( 'enable_image_source' );
}

/**
* Determine if sourcing images from WP domain is enabled.
*
* @return bool True if image sources from WP are enabled, false if else.
*/
function use_wp_domain_for_media() {
return is_image_source_replacement_enabled();
}

/**
* Get the secret key setting.
Expand Down
4 changes: 0 additions & 4 deletions plugins/faustwp/tests/integration/GraphQLCallbacksTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ public function test_graphql_section_field_value() {
$this->assertSame( 10, has_action( 'graphql_get_setting_section_field_value', 'WPE\FaustWP\GraphQL\filter_introspection' ) );
}

public function test_graphql_request_results_filter() {
$this->assertSame( 10, has_action( 'graphql_request_results', 'WPE\FaustWP\Replacement\url_replacement' ) );
}

/**
* Tests url_replacement() returns original data when rewrites are not enabled.
*/
Expand Down
Loading

0 comments on commit 2043b0e

Please sign in to comment.