PluginProbe
Faust.js / 0.7.9
Faust.js v0.7.9
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
faustwp / includes / replacement / callbacks.php

callbacks.php in Faust.js 0.7.9, at includes/replacement/callbacks.php

257 lines 7.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * This should already be handled by WPE\FaustWP\Replacement\post_link, but
119 * it's here for verbosity's sake and if the other filter changes for any reason.
120 */
121 $link = str_replace( $home_url, $frontend_uri, $link );
122
123 $parsed_link_query = wp_parse_url( $link, PHP_URL_QUERY );
124 $args = wp_parse_args( $parsed_link_query );
125 $preview_id = isset( $args['preview_id'] ) ? $args['preview_id'] : $post->ID;
126
127 // Remove ?p=xx&preview=true from link temporarily.
128 $link = remove_query_arg(
129 array_keys( $args ),
130 $link
131 );
132
133 // Add p=xx if it's missing, which is the case for published posts.
134 if ( ! isset( $args['p'] ) ) {
135 $args['p'] = $preview_id;
136 }
137
138 // Add preview=true in case other plugins have stripped it.
139 if ( ! isset( $args['preview'] ) ) {
140 $args['preview'] = 'true';
141 }
142
143 // Add page_id=xx if it's missing, which is the case for published pages.
144 if ( ! isset( $args['page_id'] ) && 'page' === $post->post_type ) {
145 $args['page_id'] = $preview_id;
146 }
147
148 /**
149 * We use this query param to get the correct preview post type.
150 * This saves us a second request to the GraphQL endpoint, as we first
151 * need to determine the preview node post type before retrieving
152 * the data.
153 */
154 $post_type_object = get_post_type_object( $post->post_type );
155 if (
156 ! isset( $args['typeName'] ) &&
157 isset( $post_type_object ) &&
158 ! empty( $post_type_object->graphql_single_name )
159 ) {
160 $gql_type_name = ucfirst( $post_type_object->graphql_single_name );
161 $args['typeName'] = $gql_type_name;
162 }
163
164 // Add ?p=xx&preview=true to link again.
165 $link = add_query_arg(
166 array(
167 $args,
168 ),
169 $link
170 );
171 }
172
173 return $link;
174 }
175
176 add_filter( 'post_link', __NAMESPACE__ . '\\post_link', 1000 );
177 add_filter( 'page_link', __NAMESPACE__ . '\\post_link', 1000 );
178 /**
179 * Callback for WordPress 'post_link' & 'page_link' filter.
180 *
181 * Swap post links in admin for headless front-end.
182 *
183 * @param string $link URL used for the post.
184 *
185 * @return string URL used for the post.
186 */
187 function post_link( $link ) {
188 if (
189 ! is_rewrites_enabled()
190 || ( function_exists( 'is_graphql_request' ) && is_graphql_request() )
191 ) {
192 return $link;
193 }
194
195 return equivalent_frontend_url( $link );
196 }
197
198 add_filter( 'term_link', __NAMESPACE__ . '\\term_link', 1000 );
199 /**
200 * Rewrites term links to point to the specified front-end URL.
201 *
202 * @param string $term_link Term link URL.
203 *
204 * @return string
205 */
206 function term_link( $term_link ) {
207 if ( ! is_rewrites_enabled() ) {
208 return $term_link;
209 }
210
211 return equivalent_frontend_url( $term_link );
212 }
213
214
215 add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_preview_scripts' );
216 /**
217 * Adds JavaScript file to the Gutenberg editor page that prepends /preview to the preview link.
218 *
219 * XXX: Please remove this once this issue is resolved: https://github.com/WordPress/gutenberg/issues/13998
220 */
221 function enqueue_preview_scripts() {
222 wp_enqueue_script( 'faustwp-gutenberg-filters', plugins_url( '/previewlinks.js', __FILE__ ), array(), '1.0.0', true );
223 wp_localize_script( 'faustwp-gutenberg-filters', '_faustwp_preview_link', array( '_preview_link' => get_preview_post_link() ) );
224 }
225
226 add_filter( 'wp_sitemaps_posts_entry', __NAMESPACE__ . '\\sitemaps_posts_entry' );
227 /**
228 * Filters the sitemap entry for an individual post.
229 *
230 * @param array $sitemap_entry Sitemap entry for the post.
231 */
232 function sitemaps_posts_entry( $sitemap_entry ) {
233 return normalize_sitemap_entry( $sitemap_entry );
234 }
235
236 add_filter( 'wp_sitemaps_taxonomies_entry', __NAMESPACE__ . '\\sitemaps_taxonomies_entry' );
237 /**
238 * Filters the sitemap entry for an individual term.
239 *
240 * @param array $sitemap_entry Sitemap entry for the term.
241 */
242 function sitemaps_taxonomies_entry( $sitemap_entry ) {
243 return normalize_sitemap_entry( $sitemap_entry );
244 }
245
246 add_filter( 'wpseo_xml_sitemap_post_url', __NAMESPACE__ . '\\yoast_sitemap_post_url' );
247 /**
248 * Filter the URL Yoast SEO uses in the XML sitemap.
249 *
250 * Note that only absolute local URLs are allowed as the check after this removes external URLs.
251 *
252 * @param string $url URL to use in the XML sitemap.
253 */
254 function yoast_sitemap_post_url( $url ) {
255 return equivalent_wp_url( $url );
256 }
257