| 1 |
<?php |
| 2 |
/** |
| 3 |
* Replacement related callbacks. |
| 4 |
* |
| 5 |
* @package FaustWP |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WPE\FaustWP\Replacement; |
| 9 |
|
| 10 |
use function WPE\FaustWP\Settings\{ |
| 11 |
faustwp_get_setting, |
| 12 |
is_image_source_replacement_enabled, |
| 13 |
is_rewrites_enabled |
| 14 |
}; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
add_filter( 'the_content', __NAMESPACE__ . '\\content_replacement' ); |
| 21 |
/** |
| 22 |
* Callback for WordPress 'the_content' filter. |
| 23 |
* |
| 24 |
* @param string $content The post content. |
| 25 |
* |
| 26 |
* @return string The post content. |
| 27 |
* @todo Needs work... |
| 28 |
*/ |
| 29 |
function content_replacement( $content ) { |
| 30 |
if ( ! domain_replacement_enabled() ) { |
| 31 |
return $content; |
| 32 |
} |
| 33 |
|
| 34 |
$replacement = faustwp_get_setting( 'frontend_uri' ); |
| 35 |
$site_url = site_url(); |
| 36 |
|
| 37 |
if ( ! $replacement ) { |
| 38 |
$replacement = '/'; |
| 39 |
} |
| 40 |
|
| 41 |
$content = str_replace( "href=\"{$site_url}", "href=\"{$replacement}", $content ); |
| 42 |
|
| 43 |
return str_replace( 'href="//', 'href="/', $content ); |
| 44 |
} |
| 45 |
|
| 46 |
add_filter( 'the_content', __NAMESPACE__ . '\\image_source_replacement' ); |
| 47 |
/** |
| 48 |
* Callback for WordPress 'the_content' filter to replace paths to media. |
| 49 |
* |
| 50 |
* @param string $content The post content. |
| 51 |
* |
| 52 |
* @return string The post content. |
| 53 |
*/ |
| 54 |
function image_source_replacement( $content ) { |
| 55 |
if ( ! is_image_source_replacement_enabled() ) { |
| 56 |
return $content; |
| 57 |
} |
| 58 |
|
| 59 |
$frontend_uri = faustwp_get_setting( 'frontend_uri' ); |
| 60 |
$site_url = site_url(); |
| 61 |
|
| 62 |
// For urls with no domain or the frontend domain, replace with the wp site_url. |
| 63 |
$patterns = array( |
| 64 |
"#src=\"{$frontend_uri}/#", |
| 65 |
'#src="/#', |
| 66 |
); |
| 67 |
return preg_replace( $patterns, "src=\"{$site_url}/", $content ); |
| 68 |
} |
| 69 |
|
| 70 |
add_filter( 'wp_calculate_image_srcset', __NAMESPACE__ . '\\image_source_srcset_replacement' ); |
| 71 |
/** |
| 72 |
* Callback for WordPress 'the_content' filter to replace paths to media. |
| 73 |
* |
| 74 |
* @param array $sources One or more arrays of source data to include in the 'srcset'. |
| 75 |
* |
| 76 |
* @return string One or more arrays of source data. |
| 77 |
*/ |
| 78 |
function image_source_srcset_replacement( $sources ) { |
| 79 |
if ( ! is_image_source_replacement_enabled() ) { |
| 80 |
return $sources; |
| 81 |
} |
| 82 |
|
| 83 |
$frontend_uri = faustwp_get_setting( 'frontend_uri' ); |
| 84 |
$site_url = site_url(); |
| 85 |
|
| 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'] ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $sources; |
| 98 |
} |
| 99 |
|
| 100 |
add_filter( 'preview_post_link', __NAMESPACE__ . '\\post_preview_link', 1000, 2 ); |
| 101 |
/** |
| 102 |
* Callback for WordPress 'preview_post_link' filter. |
| 103 |
* |
| 104 |
* Swap the post preview link for headless front-end and to use the API entry to support Next.js preview mode. |
| 105 |
* |
| 106 |
* @param string $link URL used for the post preview. |
| 107 |
* @param WP_Post $post Post object. |
| 108 |
* |
| 109 |
* @return string URL used for the post preview. |
| 110 |
*/ |
| 111 |
function post_preview_link( $link, $post ) { |
| 112 |
$frontend_uri = faustwp_get_setting( 'frontend_uri' ); |
| 113 |
|
| 114 |
if ( $frontend_uri ) { |
| 115 |
$home_url = trailingslashit( get_home_url() ); |
| 116 |
$frontend_uri = trailingslashit( $frontend_uri ); |
| 117 |
|
| 118 |
$link = str_replace( $home_url, $frontend_uri, $link ); |
| 119 |
|
| 120 |
// Replace the schemes, if different. |
| 121 |
$frontend_uri_scheme = wp_parse_url( $frontend_uri, PHP_URL_SCHEME ); |
| 122 |
$link_scheme = wp_parse_url( $link, PHP_URL_SCHEME ); |
| 123 |
if ( $frontend_uri_scheme !== $link_scheme ) { |
| 124 |
$link = str_replace( $link_scheme . '://', $frontend_uri_scheme . '://', $link ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* This should already be handled by WPE\FaustWP\Replacement\post_link, but |
| 129 |
* it's here for verbosity's sake and if the other filter changes for any reason. |
| 130 |
*/ |
| 131 |
$parsed_link_query = wp_parse_url( $link, PHP_URL_QUERY ); |
| 132 |
$args = wp_parse_args( $parsed_link_query ); |
| 133 |
$preview_id = isset( $args['preview_id'] ) ? $args['preview_id'] : $post->ID; |
| 134 |
|
| 135 |
// Remove ?p=xx&preview=true from link temporarily. |
| 136 |
$link = remove_query_arg( |
| 137 |
array_keys( $args ), |
| 138 |
$link |
| 139 |
); |
| 140 |
|
| 141 |
if ( ! isset( $args['previewPathname'] ) ) { |
| 142 |
$args['previewPathname'] = rawurlencode( wp_make_link_relative( get_permalink( $post ) ) ); |
| 143 |
} |
| 144 |
|
| 145 |
// Add p=xx if it's missing, which is the case for published posts. |
| 146 |
if ( ! isset( $args['p'] ) ) { |
| 147 |
$args['p'] = $preview_id; |
| 148 |
} |
| 149 |
|
| 150 |
// Add preview=true in case other plugins have stripped it. |
| 151 |
if ( ! isset( $args['preview'] ) ) { |
| 152 |
$args['preview'] = 'true'; |
| 153 |
} |
| 154 |
|
| 155 |
// Add page_id=xx if it's missing, which is the case for published pages. |
| 156 |
if ( ! isset( $args['page_id'] ) && 'page' === $post->post_type ) { |
| 157 |
$args['page_id'] = $preview_id; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* We use this query param to get the correct preview post type. |
| 162 |
* This saves us a second request to the GraphQL endpoint, as we first |
| 163 |
* need to determine the preview node post type before retrieving |
| 164 |
* the data. |
| 165 |
*/ |
| 166 |
$post_type_object = get_post_type_object( $post->post_type ); |
| 167 |
if ( |
| 168 |
! isset( $args['typeName'] ) && |
| 169 |
isset( $post_type_object ) && |
| 170 |
! empty( $post_type_object->graphql_single_name ) |
| 171 |
) { |
| 172 |
$gql_type_name = ucfirst( $post_type_object->graphql_single_name ); |
| 173 |
$args['typeName'] = $gql_type_name; |
| 174 |
} |
| 175 |
|
| 176 |
// Add ?p=xx&preview=true to link again. |
| 177 |
$link = add_query_arg( |
| 178 |
array( |
| 179 |
$args, |
| 180 |
), |
| 181 |
$link |
| 182 |
); |
| 183 |
} |
| 184 |
|
| 185 |
return $link; |
| 186 |
} |
| 187 |
|
| 188 |
add_filter( 'post_link', __NAMESPACE__ . '\\post_link', 1000 ); |
| 189 |
add_filter( 'page_link', __NAMESPACE__ . '\\post_link', 1000 ); |
| 190 |
/** |
| 191 |
* Callback for WordPress 'post_link' & 'page_link' filter. |
| 192 |
* |
| 193 |
* Swap post links in admin for headless front-end. |
| 194 |
* |
| 195 |
* @param string $link URL used for the post. |
| 196 |
* |
| 197 |
* @return string URL used for the post. |
| 198 |
*/ |
| 199 |
function post_link( $link ) { |
| 200 |
if ( |
| 201 |
! is_rewrites_enabled() |
| 202 |
|| ( function_exists( 'is_graphql_request' ) && is_graphql_request() ) |
| 203 |
) { |
| 204 |
return $link; |
| 205 |
} |
| 206 |
|
| 207 |
return equivalent_frontend_url( $link ); |
| 208 |
} |
| 209 |
|
| 210 |
add_filter( 'term_link', __NAMESPACE__ . '\\term_link', 1000 ); |
| 211 |
/** |
| 212 |
* Rewrites term links to point to the specified front-end URL. |
| 213 |
* |
| 214 |
* @param string $term_link Term link URL. |
| 215 |
* |
| 216 |
* @return string |
| 217 |
*/ |
| 218 |
function term_link( $term_link ) { |
| 219 |
if ( ! is_rewrites_enabled() ) { |
| 220 |
return $term_link; |
| 221 |
} |
| 222 |
|
| 223 |
return equivalent_frontend_url( $term_link ); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_preview_scripts' ); |
| 228 |
/** |
| 229 |
* Adds JavaScript file to the Gutenberg editor page that prepends /preview to the preview link. |
| 230 |
* |
| 231 |
* XXX: Please remove this once this issue is resolved: https://github.com/WordPress/gutenberg/issues/13998 |
| 232 |
*/ |
| 233 |
function enqueue_preview_scripts() { |
| 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() ) ); |
| 236 |
} |
| 237 |
|
| 238 |
add_filter( 'wp_sitemaps_posts_entry', __NAMESPACE__ . '\\sitemaps_posts_entry' ); |
| 239 |
/** |
| 240 |
* Filters the sitemap entry for an individual post. |
| 241 |
* |
| 242 |
* @param array $sitemap_entry Sitemap entry for the post. |
| 243 |
*/ |
| 244 |
function sitemaps_posts_entry( $sitemap_entry ) { |
| 245 |
return normalize_sitemap_entry( $sitemap_entry ); |
| 246 |
} |
| 247 |
|
| 248 |
add_filter( 'wp_sitemaps_taxonomies_entry', __NAMESPACE__ . '\\sitemaps_taxonomies_entry' ); |
| 249 |
/** |
| 250 |
* Filters the sitemap entry for an individual term. |
| 251 |
* |
| 252 |
* @param array $sitemap_entry Sitemap entry for the term. |
| 253 |
*/ |
| 254 |
function sitemaps_taxonomies_entry( $sitemap_entry ) { |
| 255 |
return normalize_sitemap_entry( $sitemap_entry ); |
| 256 |
} |
| 257 |
|
| 258 |
add_filter( 'wpseo_xml_sitemap_post_url', __NAMESPACE__ . '\\yoast_sitemap_post_url' ); |
| 259 |
/** |
| 260 |
* Filter the URL Yoast SEO uses in the XML sitemap. |
| 261 |
* |
| 262 |
* Note that only absolute local URLs are allowed as the check after this removes external URLs. |
| 263 |
* |
| 264 |
* @param string $url URL to use in the XML sitemap. |
| 265 |
*/ |
| 266 |
function yoast_sitemap_post_url( $url ) { |
| 267 |
return equivalent_wp_url( $url ); |
| 268 |
} |
| 269 |
|