PluginProbe
Faust.js / 1.0.2
Faust.js v1.0.2
1.8.12 1.8.11 1.4.1 1.8.0 1.8.8 1.8.9 trunk 0.7.0 0.7.1 0.7.10 0.7.11 0.7.3 0.7.4 0.7.5 0.7.6 0.7.7 0.7.8 0.7.9 0.8.0 0.8.1 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 All 40 releases
← All changes | includes/replacement/callbacks.php +33 -187 trunk1.0.2 View file →
@@ -4,22 +4,15 @@
4 4 *
5 5 * @package FaustWP
6 6 */
7 7
8 -declare(strict_types=1);
9 -
10 8 namespace WPE\FaustWP\Replacement;
11 9
12 10 use function WPE\FaustWP\Settings\{
13 11 faustwp_get_setting,
14 12 is_image_source_replacement_enabled,
15 - is_rewrites_enabled,
16 - is_redirects_enabled,
17 - use_wp_domain_for_media,
13 + is_rewrites_enabled
18 14 };
19 -use function WPE\FaustWP\Utilities\{
20 - plugin_version,
21 -};
22 15
23 16 if ( ! defined( 'ABSPATH' ) ) {
24 17 exit;
25 18 }
@@ -24,80 +17,34 @@
24 17 exit;
25 18 }
26 19
27 20 add_filter( 'the_content', __NAMESPACE__ . '\\content_replacement' );
28 -add_filter( 'wpgraphql_content_blocks_resolver_content', __NAMESPACE__ . '\\content_replacement' );
29 21 /**
30 - * Replace the URLs in the post content basesd on two settings if enabled:
31 - * 1. Enable Post and Category URL rewrites - rewrite the WordPress internal URL in the content with the Front-end URL
32 - * 1.1 If NOT enabled - use the WordPress URL
33 - * 2. Use the WordPress domain for media URLs in post content - use WordPress URL for media files (images,csv,pdf)
34 - * 2.1 If NOT enabled - use front-end url
22 + * Callback for WordPress 'the_content' filter.
35 23 *
36 - * @param ?string $content The post content.
24 + * @param string $content The post content.
37 25 *
38 - * @return ?string The post content.
26 + * @return string The post content.
27 + * @todo Needs work...
39 28 */
40 -function content_replacement( ?string $content ) {
41 -
42 - if ( ! $content ) {
29 +function content_replacement( $content ) {
30 + if ( ! domain_replacement_enabled() ) {
43 31 return $content;
44 32 }
45 - $replace_content_urls = domain_replacement_enabled();
46 - $replace_media_urls = ! use_wp_domain_for_media();
47 33
48 - if ( ! $replace_content_urls && ! $replace_media_urls ) {
49 - return $content;
50 - }
51 - $wp_site_urls = faustwp_get_wp_site_urls( site_url() );
52 - if ( empty( $wp_site_urls ) ) {
53 - return $content;
54 - }
55 - $relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls, wp_upload_dir()['baseurl'] );
56 - $wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls, $relative_upload_url );
57 - $frontend_uri = (string) faustwp_get_setting( 'frontend_uri' ) ?? '/';
34 + $replacement = faustwp_get_setting( 'frontend_uri' );
35 + $site_url = site_url();
58 36
59 - /* If "Enable Post and Category URL" is enabled, use front-end URL for internal URLs, but not for media links */
60 -
61 - if ( $replace_content_urls ) {
62 -
63 - // Look for href links.
64 - preg_match_all( '#href="([^"]+)"#i', $content, $href_links );
65 - if ( is_array( $href_links ) && ! empty( $href_links[1] ) ) {
66 - foreach ( $href_links[1] as $i => $url ) {
67 - // skip media links.
68 - $is_media = array_filter( $wp_media_urls, fn( $media ) => strpos( $url, $media ) === 0 );
69 - if ( $is_media ) {
70 - continue;
71 - }
72 -
73 - $is_wp_url = array_filter( $wp_site_urls, fn( $base ) => strpos( $url, $base ) === 0 );
74 - if ( ! $is_wp_url ) {
75 - continue;
76 - }
77 -
78 - // get relative link.
79 - $relative = str_replace( reset( $is_wp_url ), '', $url );
80 - $updated = 'href="' . $frontend_uri . $relative . '"';
81 -
82 - $original = $href_links[0][ $i ];
83 -
84 - if ( $original ) {
85 - $content = str_replace( $original, $updated, $content );
86 - }
87 - }
88 - }
37 + if ( ! $replacement ) {
38 + $replacement = '/';
89 39 }
90 40
91 - /* If "Use the WordPress domain for media URLs in post content" is NOT enabled, use front-end URL for media URLs */
41 + $content = str_replace( "href=\"{$site_url}", "href=\"{$replacement}", $content );
92 42
93 - if ( $replace_media_urls ) {
94 - $content = str_replace( $wp_media_urls, $frontend_uri . $relative_upload_url, $content );
95 - }
96 -
97 - return $content;
43 + return str_replace( 'href="//', 'href="/', $content );
98 44 }
99 45
46 +add_filter( 'the_content', __NAMESPACE__ . '\\image_source_replacement' );
100 47 /**
101 48 * Callback for WordPress 'the_content' filter to replace paths to media.
102 49 *
103 50 * @param string $content The post content.
@@ -116,60 +63,36 @@
116 63 $patterns = array(
117 64 "#src=\"{$frontend_uri}/#",
118 65 '#src="/#',
119 66 );
120 -
121 67 return preg_replace( $patterns, "src=\"{$site_url}/", $content );
122 68 }
123 69
124 70 add_filter( 'wp_calculate_image_srcset', __NAMESPACE__ . '\\image_source_srcset_replacement' );
125 71 /**
126 - * Callback for WordPress 'wp_calculate_image_srcset' filter to replace paths when generating a srcset
72 + * Callback for WordPress 'the_content' filter to replace paths to media.
127 73 *
128 - * @link https://developer.wordpress.org/reference/functions/wp_calculate_image_srcset/
74 + * @param array $sources One or more arrays of source data to include in the 'srcset'.
129 75 *
130 - * @param array<string> $sources One or more arrays of source data to include in the 'srcset'.
131 - *
132 - * @return array One or more arrays of source data.
76 + * @return string One or more arrays of source data.
133 77 */
134 78 function image_source_srcset_replacement( $sources ) {
135 -
136 - if ( ! is_array( $sources ) || empty( $sources ) ) {
79 + if ( ! is_image_source_replacement_enabled() ) {
137 80 return $sources;
138 81 }
139 82
140 - $wp_site_urls = faustwp_get_wp_site_urls( site_url() );
141 - if ( empty( $wp_site_urls ) ) {
142 - return $sources;
143 - }
83 + $frontend_uri = faustwp_get_setting( 'frontend_uri' );
84 + $site_url = site_url();
144 85
145 - $replace_media_urls = ! use_wp_domain_for_media();
146 - $relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls, wp_upload_dir()['baseurl'] );
147 - $wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls, $relative_upload_url );
148 - $frontend_uri = (string) faustwp_get_setting( 'frontend_uri' );
149 - $site_url = site_url() . '/';
150 -
151 - $wp_media_site_url = $frontend_uri . $relative_upload_url;
152 - $patterns = array(
153 - "#^{$frontend_uri}/#",
154 - '#^/#',
155 - );
156 -
157 - /**
158 - * Update each source with the correct replacement URL
159 - */
160 - foreach ( $sources as $width => $source ) {
161 - $url = $source['url'];
162 -
163 - if ( $replace_media_urls ) {
164 - $sources[ $width ]['url'] = ( strpos( $url, $relative_upload_url ) === 0 )
165 - ? $frontend_uri . $url
166 - : str_replace( $wp_media_urls, $wp_media_site_url, $source['url'] );
167 - continue;
86 + if ( is_array( $sources ) ) {
87 + // For urls with no domain or the frontend domain, replace with the wp site_url.
88 + $patterns = array(
89 + "#^{$frontend_uri}/#",
90 + '#^/#',
91 + );
92 + foreach ( $sources as $width => $source ) {
93 + $sources[ $width ]['url'] = preg_replace( $patterns, "$site_url/", $sources[ $width ]['url'] );
168 94 }
169 -
170 - // We need to make sure that the frontend URL or relative URL (legacy) is updated with the site url.
171 - $sources[ $width ]['url'] = preg_replace( $patterns, $site_url, $source['url'] );
172 95 }
173 96
174 97 return $sources;
175 98 }
@@ -185,12 +108,8 @@
185 108 *
186 109 * @return string URL used for the post preview.
187 110 */
188 111 function post_preview_link( $link, $post ) {
189 - // Don't rewrite preview link if redirect is disabled.
190 - if ( ! is_redirects_enabled() ) {
191 - return $link;
192 - }
193 112 $frontend_uri = faustwp_get_setting( 'frontend_uri' );
194 113
195 114 if ( $frontend_uri ) {
196 115 $home_url = trailingslashit( get_home_url() );
@@ -267,11 +186,10 @@
267 186 }
268 187
269 188 add_filter( 'post_link', __NAMESPACE__ . '\\post_link', 1000 );
270 189 add_filter( 'page_link', __NAMESPACE__ . '\\post_link', 1000 );
271 -add_filter( 'post_type_link', __NAMESPACE__ . '\\post_link', 1000 );
272 190 /**
273 - * Callback for WordPress 'post_link', 'page_link', and `post_type_link` filter.
191 + * Callback for WordPress 'post_link' & 'page_link' filter.
274 192 *
275 193 * Swap post links in admin for headless front-end.
276 194 *
277 195 * @param string $link URL used for the post.
@@ -278,45 +196,15 @@
278 196 *
279 197 * @return string URL used for the post.
280 198 */
281 199 function post_link( $link ) {
282 - global $pagenow;
283 - $target_pages = array(
284 - 'admin-ajax.php',
285 - 'index.php',
286 - 'edit.php',
287 - 'post.php',
288 - 'post-new.php',
289 - 'upload.php',
290 - 'media-new.php',
291 - );
292 -
293 - // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Nonce verified in `is_ajax_generate_permalink_request()` and `is_wp_link_ajax_request()`.
294 - if ( empty( $_POST ) && 'post-new.php' === $pagenow ) {
295 - return $link;
296 - }
297 -
298 - // Ajax requests to generate permalink.
299 - if ( in_array( $pagenow, $target_pages, true )
300 - && is_ajax_generate_permalink_request()
301 - ) {
302 - return $link;
303 - }
304 -
305 200 if (
306 201 ! is_rewrites_enabled()
307 202 || ( function_exists( 'is_graphql_request' ) && is_graphql_request() )
308 - // Block editor makes REST requests on these pages to query content.
309 - || ( in_array( $pagenow, $target_pages, true ) && current_user_can( 'edit_posts' ) && defined( 'REST_REQUEST' ) && REST_REQUEST )
310 203 ) {
311 204 return $link;
312 205 }
313 206
314 - // Check for wp-link-ajax requests. Used by Classic Editor when linking content.
315 - if ( is_wp_link_ajax_request() ) {
316 - return $link;
317 - }
318 -
319 207 return equivalent_frontend_url( $link );
320 208 }
321 209
322 210 add_filter( 'term_link', __NAMESPACE__ . '\\term_link', 1000 );
@@ -327,12 +215,9 @@
327 215 *
328 216 * @return string
329 217 */
330 218 function term_link( $term_link ) {
331 - if (
332 - ! is_rewrites_enabled()
333 - || ( function_exists( 'is_graphql_request' ) && is_graphql_request() )
334 - ) {
219 + if ( ! is_rewrites_enabled() ) {
335 220 return $term_link;
336 221 }
337 222
338 223 return equivalent_frontend_url( $term_link );
@@ -337,8 +222,9 @@
337 222
338 223 return equivalent_frontend_url( $term_link );
339 224 }
340 225
226 +
341 227 add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_preview_scripts' );
342 228 /**
343 229 * Adds JavaScript file to the Gutenberg editor page that prepends /preview to the preview link.
344 230 *
@@ -344,50 +230,10 @@
344 230 *
345 231 * XXX: Please remove this once this issue is resolved: https://github.com/WordPress/gutenberg/issues/13998
346 232 */
347 233 function enqueue_preview_scripts() {
348 - wp_enqueue_script( 'faustwp-gutenberg-filters', plugins_url( '/previewlinks.js', __FILE__ ), array(), plugin_version(), true );
349 - wp_localize_script(
350 - 'faustwp-gutenberg-filters',
351 - '_faustwp_preview_data',
352 - array(
353 - '_preview_link' => get_preview_post_link(),
354 - '_wp_version' => get_bloginfo( 'version' ),
355 - )
356 - );
357 -}
358 -
359 -add_filter( 'rest_api_init', __NAMESPACE__ . '\\register_preview_link_hooks_for_all_draft_post_types' );
360 -
361 -/**
362 - * Registers the preview link hooks for all post types.
363 - */
364 -function register_preview_link_hooks_for_all_draft_post_types() {
365 - $post_types = get_post_types(
366 - array(
367 - 'public' => true,
368 - )
369 - );
370 -
371 - foreach ( $post_types as $post_type ) {
372 - add_filter( 'rest_prepare_' . $post_type, __NAMESPACE__ . '\\preview_link_in_rest_response', 10, 2 );
373 - }
374 -}
375 -
376 -/**
377 - * Adds the preview link to rest responses.
378 - *
379 - * @param WP_REST_Response $response The rest response object.
380 - * @param WP_Post $post Post object.
381 - *
382 - * @return string URL used for the post preview.
383 - */
384 -function preview_link_in_rest_response( $response, $post ) {
385 - if ( 'draft' === $post->post_status ) {
386 - $response->data['link'] = get_preview_post_link( $post->ID );
387 - }
388 -
389 - return $response;
234 + wp_enqueue_script( 'faustwp-gutenberg-filters', plugins_url( '/previewlinks.js', __FILE__ ), array( 'jquery' ), '1.0.0', true );
235 + wp_localize_script( 'faustwp-gutenberg-filters', '_faustwp_preview_link', array( '_preview_link' => get_preview_post_link() ) );
390 236 }
391 237
392 238 add_filter( 'wp_sitemaps_posts_entry', __NAMESPACE__ . '\\sitemaps_posts_entry' );
393 239 /**