-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathcallbacks.php
More file actions
422 lines (362 loc) · 12.4 KB
/
callbacks.php
File metadata and controls
422 lines (362 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
/**
* Replacement related callbacks.
*
* @package FaustWP
*/
declare(strict_types=1);
namespace WPE\FaustWP\Replacement;
use function WPE\FaustWP\Settings\{
faustwp_get_setting,
is_image_source_replacement_enabled,
is_rewrites_enabled,
is_redirects_enabled,
use_wp_domain_for_media,
};
use function WPE\FaustWP\Utilities\{
plugin_version,
};
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
add_filter( 'the_content', __NAMESPACE__ . '\\content_replacement' );
add_filter( 'wpgraphql_content_blocks_resolver_content', __NAMESPACE__ . '\\content_replacement' );
/**
* Replace the URLs in the post content basesd on two settings if enabled:
* 1. Enable Post and Category URL rewrites - rewrite the WordPress internal URL in the content with the Front-end URL
* 1.1 If NOT enabled - use the WordPress URL
* 2. Use the WordPress domain for media URLs in post content - use WordPress URL for media files (images,csv,pdf)
* 2.1 If NOT enabled - use front-end url
*
* @param ?string $content The post content.
*
* @return ?string The post content.
*/
function content_replacement( ?string $content ) {
if ( ! $content ) {
return $content;
}
$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( site_url() );
if ( empty( $wp_site_urls ) ) {
return $content;
}
$relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls, wp_upload_dir()['baseurl'] );
$wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls, $relative_upload_url );
$frontend_uri = (string) faustwp_get_setting( 'frontend_uri' ) ?? '/';
/* If "Enable Post and Category URL" is enabled, use front-end URL for internal URLs, but not for media links */
if ( $replace_content_urls ) {
// Look for href links.
preg_match_all( '#href="([^"]+)"#i', $content, $href_links );
if ( is_array( $href_links ) && ! empty( $href_links[1] ) ) {
foreach ( $href_links[1] as $i => $url ) {
// skip media links.
$is_media = array_filter( $wp_media_urls, fn( $media ) => strpos( $url, $media ) === 0 );
if ( $is_media ) {
continue;
}
$is_wp_url = array_filter( $wp_site_urls, fn( $base ) => strpos( $url, $base ) === 0 );
if ( ! $is_wp_url ) {
continue;
}
// get relative link.
$relative = str_replace( reset( $is_wp_url ), '', $url );
$updated = 'href="' . $frontend_uri . $relative . '"';
$original = $href_links[0][ $i ];
if ( $original ) {
$content = str_replace( $original, $updated, $content );
}
}
}
}
/* If "Use the WordPress domain for media URLs in post content" is NOT enabled, use front-end URL for media URLs */
if ( $replace_media_urls ) {
$content = str_replace( $wp_media_urls, $frontend_uri . $relative_upload_url, $content );
}
return $content;
}
/**
* Callback for WordPress 'the_content' filter to replace paths to media.
*
* @param string $content The post content.
*
* @return string The post content.
*/
function image_source_replacement( $content ) {
if ( ! is_image_source_replacement_enabled() ) {
return $content;
}
$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.
$patterns = array(
"#src=\"{$frontend_uri}/#",
'#src="/#',
);
return preg_replace( $patterns, "src=\"{$site_url}/", $content );
}
add_filter( 'wp_calculate_image_srcset', __NAMESPACE__ . '\\image_source_srcset_replacement' );
/**
* 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<string> $sources One or more arrays of source data to include in the 'srcset'.
*
* @return array One or more arrays of source data.
*/
function image_source_srcset_replacement( $sources ) {
if ( ! is_array( $sources ) || empty( $sources ) ) {
return $sources;
}
$wp_site_urls = faustwp_get_wp_site_urls( site_url() );
if ( empty( $wp_site_urls ) ) {
return $sources;
}
$replace_media_urls = ! use_wp_domain_for_media();
$relative_upload_url = faustwp_get_relative_upload_url( $wp_site_urls, wp_upload_dir()['baseurl'] );
$wp_media_urls = faustwp_get_wp_media_urls( $wp_site_urls, $relative_upload_url );
$frontend_uri = (string) faustwp_get_setting( 'frontend_uri' );
$site_url = site_url() . '/';
$wp_media_site_url = $frontend_uri . $relative_upload_url;
$patterns = array(
"#^{$frontend_uri}/#",
'#^/#',
);
/**
* Update each source with the correct replacement URL
*/
foreach ( $sources as $width => $source ) {
$url = $source['url'];
if ( $replace_media_urls ) {
$sources[ $width ]['url'] = ( strpos( $url, $relative_upload_url ) === 0 )
? $frontend_uri . $url
: str_replace( $wp_media_urls, $wp_media_site_url, $source['url'] );
continue;
}
// 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;
}
add_filter( 'preview_post_link', __NAMESPACE__ . '\\post_preview_link', 1000, 2 );
/**
* Callback for WordPress 'preview_post_link' filter.
*
* Swap the post preview link for headless front-end and to use the API entry to support Next.js preview mode.
*
* @param string $link URL used for the post preview.
* @param WP_Post $post Post object.
*
* @return string URL used for the post preview.
*/
function post_preview_link( $link, $post ) {
// Don't rewrite preview link if redirect is disabled.
if ( ! is_redirects_enabled() ) {
return $link;
}
$frontend_uri = faustwp_get_setting( 'frontend_uri' );
if ( $frontend_uri ) {
$home_url = trailingslashit( get_home_url() );
$frontend_uri = trailingslashit( $frontend_uri );
$link = str_replace( $home_url, $frontend_uri, $link );
// Replace the schemes, if different.
$frontend_uri_scheme = wp_parse_url( $frontend_uri, PHP_URL_SCHEME );
$link_scheme = wp_parse_url( $link, PHP_URL_SCHEME );
if ( $frontend_uri_scheme !== $link_scheme ) {
$link = str_replace( $link_scheme . '://', $frontend_uri_scheme . '://', $link );
}
/**
* This should already be handled by WPE\FaustWP\Replacement\post_link, but
* it's here for verbosity's sake and if the other filter changes for any reason.
*/
$parsed_link_query = wp_parse_url( $link, PHP_URL_QUERY );
$args = wp_parse_args( $parsed_link_query );
$preview_id = isset( $args['preview_id'] ) ? $args['preview_id'] : $post->ID;
// Remove ?p=xx&preview=true from link temporarily.
$link = remove_query_arg(
array_keys( $args ),
$link
);
if ( ! isset( $args['previewPathname'] ) ) {
$args['previewPathname'] = rawurlencode( wp_make_link_relative( get_permalink( $post ) ) );
}
// Add p=xx if it's missing, which is the case for published posts.
if ( ! isset( $args['p'] ) ) {
$args['p'] = $preview_id;
}
// Add preview=true in case other plugins have stripped it.
if ( ! isset( $args['preview'] ) ) {
$args['preview'] = 'true';
}
// Add page_id=xx if it's missing, which is the case for published pages.
if ( ! isset( $args['page_id'] ) && 'page' === $post->post_type ) {
$args['page_id'] = $preview_id;
}
/**
* We use this query param to get the correct preview post type.
* This saves us a second request to the GraphQL endpoint, as we first
* need to determine the preview node post type before retrieving
* the data.
*/
$post_type_object = get_post_type_object( $post->post_type );
if (
! isset( $args['typeName'] ) &&
isset( $post_type_object ) &&
! empty( $post_type_object->graphql_single_name )
) {
$gql_type_name = ucfirst( $post_type_object->graphql_single_name );
$args['typeName'] = $gql_type_name;
}
// Add ?p=xx&preview=true to link again.
$link = add_query_arg(
array(
$args,
),
$link
);
}
return $link;
}
add_filter( 'post_link', __NAMESPACE__ . '\\post_link', 1000 );
add_filter( 'page_link', __NAMESPACE__ . '\\post_link', 1000 );
add_filter( 'post_type_link', __NAMESPACE__ . '\\post_link', 1000 );
/**
* Callback for WordPress 'post_link', 'page_link', and `post_type_link` filter.
*
* Swap post links in admin for headless front-end.
*
* @param string $link URL used for the 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 );
}
add_filter( 'term_link', __NAMESPACE__ . '\\term_link', 1000 );
/**
* Rewrites term links to point to the specified front-end URL.
*
* @param string $term_link Term link URL.
*
* @return string
*/
function term_link( $term_link ) {
if (
! is_rewrites_enabled()
|| ( function_exists( 'is_graphql_request' ) && is_graphql_request() )
) {
return $term_link;
}
return equivalent_frontend_url( $term_link );
}
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_preview_scripts' );
/**
* Adds JavaScript file to the Gutenberg editor page that prepends /preview to the preview link.
*
* XXX: Please remove this once this issue is resolved: https://github.com/WordPress/gutenberg/issues/13998
*/
function enqueue_preview_scripts() {
wp_enqueue_script( 'faustwp-gutenberg-filters', plugins_url( '/previewlinks.js', __FILE__ ), array(), plugin_version(), true );
wp_localize_script(
'faustwp-gutenberg-filters',
'_faustwp_preview_data',
array(
'_preview_link' => get_preview_post_link(),
'_wp_version' => get_bloginfo( 'version' ),
)
);
}
add_filter( 'rest_api_init', __NAMESPACE__ . '\\register_preview_link_hooks_for_all_draft_post_types' );
/**
* Registers the preview link hooks for all post types.
*/
function register_preview_link_hooks_for_all_draft_post_types() {
$post_types = get_post_types(
array(
'public' => true,
)
);
foreach ( $post_types as $post_type ) {
add_filter( 'rest_prepare_' . $post_type, __NAMESPACE__ . '\\preview_link_in_rest_response', 10, 2 );
}
}
/**
* Adds the preview link to rest responses.
*
* @param WP_REST_Response $response The rest response object.
* @param WP_Post $post Post object.
*
* @return string URL used for the post preview.
*/
function preview_link_in_rest_response( $response, $post ) {
if ( 'draft' === $post->post_status ) {
$response->data['link'] = get_preview_post_link( $post->ID );
}
return $response;
}
add_filter( 'wp_sitemaps_posts_entry', __NAMESPACE__ . '\\sitemaps_posts_entry' );
/**
* Filters the sitemap entry for an individual post.
*
* @param array $sitemap_entry Sitemap entry for the post.
*/
function sitemaps_posts_entry( $sitemap_entry ) {
return normalize_sitemap_entry( $sitemap_entry );
}
add_filter( 'wp_sitemaps_taxonomies_entry', __NAMESPACE__ . '\\sitemaps_taxonomies_entry' );
/**
* Filters the sitemap entry for an individual term.
*
* @param array $sitemap_entry Sitemap entry for the term.
*/
function sitemaps_taxonomies_entry( $sitemap_entry ) {
return normalize_sitemap_entry( $sitemap_entry );
}
add_filter( 'wpseo_xml_sitemap_post_url', __NAMESPACE__ . '\\yoast_sitemap_post_url' );
/**
* Filter the URL Yoast SEO uses in the XML sitemap.
*
* Note that only absolute local URLs are allowed as the check after this removes external URLs.
*
* @param string $url URL to use in the XML sitemap.
*/
function yoast_sitemap_post_url( $url ) {
return equivalent_wp_url( $url );
}