PluginProbe
Faust.js / 0.7.7
Faust.js v0.7.7
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.7, at includes/replacement/callbacks.php

252 lines 6.9 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 page_id=xx if it's missing, which is the case for published pages.
139 if ( ! isset( $args['page_id'] ) && 'page' === $post->post_type ) {
140 $args['page_id'] = $preview_id;
141 }
142
143 /**
144 * We use this query param to get the correct preview post type.
145 * This saves us a second request to the GraphQL endpoint, as we first
146 * need to determine the preview node post type before retrieving
147 * the data.
148 */
149 $post_type_object = get_post_type_object( $post->post_type );
150 if (
151 ! isset( $args['typeName'] ) &&
152 isset( $post_type_object ) &&
153 ! empty( $post_type_object->graphql_single_name )
154 ) {
155 $gql_type_name = ucfirst( $post_type_object->graphql_single_name );
156 $args['typeName'] = $gql_type_name;
157 }
158
159 // Add ?p=xx&preview=true to link again.
160 $link = add_query_arg(
161 array(
162 $args,
163 ),
164 $link
165 );
166 }
167
168 return $link;
169 }
170
171
172 add_filter( 'post_link', __NAMESPACE__ . '\\post_link', 1000 );
173 /**
174 * Callback for WordPress 'post_link' filter.
175 *
176 * Swap post links in admin for headless front-end.
177 *
178 * @param string $link URL used for the post.
179 *
180 * @return string URL used for the post.
181 */
182 function post_link( $link ) {
183 if (
184 ! is_rewrites_enabled()
185 || ( function_exists( 'is_graphql_request' ) && is_graphql_request() )
186 ) {
187 return $link;
188 }
189
190 return equivalent_frontend_url( $link );
191 }
192
193 add_filter( 'term_link', __NAMESPACE__ . '\\term_link', 1000 );
194 /**
195 * Rewrites term links to point to the specified front-end URL.
196 *
197 * @param string $term_link Term link URL.
198 *
199 * @return string
200 */
201 function term_link( $term_link ) {
202 if ( ! is_rewrites_enabled() ) {
203 return $term_link;
204 }
205
206 return equivalent_frontend_url( $term_link );
207 }
208
209
210 add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_preview_scripts' );
211 /**
212 * Adds JavaScript file to the Gutenberg editor page that prepends /preview to the preview link.
213 *
214 * XXX: Please remove this once this issue is resolved: https://github.com/WordPress/gutenberg/issues/13998
215 */
216 function enqueue_preview_scripts() {
217 wp_enqueue_script( 'faustwp-gutenberg-filters', plugins_url( '/previewlinks.js', __FILE__ ), array(), '1.0.0', true );
218 wp_localize_script( 'faustwp-gutenberg-filters', '_faustwp_preview_link', array( '_preview_link' => get_preview_post_link() ) );
219 }
220
221 add_filter( 'wp_sitemaps_posts_entry', __NAMESPACE__ . '\\sitemaps_posts_entry' );
222 /**
223 * Filters the sitemap entry for an individual post.
224 *
225 * @param array $sitemap_entry Sitemap entry for the post.
226 */
227 function sitemaps_posts_entry( $sitemap_entry ) {
228 return normalize_sitemap_entry( $sitemap_entry );
229 }
230
231 add_filter( 'wp_sitemaps_taxonomies_entry', __NAMESPACE__ . '\\sitemaps_taxonomies_entry' );
232 /**
233 * Filters the sitemap entry for an individual term.
234 *
235 * @param array $sitemap_entry Sitemap entry for the term.
236 */
237 function sitemaps_taxonomies_entry( $sitemap_entry ) {
238 return normalize_sitemap_entry( $sitemap_entry );
239 }
240
241 add_filter( 'wpseo_xml_sitemap_post_url', __NAMESPACE__ . '\\yoast_sitemap_post_url' );
242 /**
243 * Filter the URL Yoast SEO uses in the XML sitemap.
244 *
245 * Note that only absolute local URLs are allowed as the check after this removes external URLs.
246 *
247 * @param string $url URL to use in the XML sitemap.
248 */
249 function yoast_sitemap_post_url( $url ) {
250 return equivalent_wp_url( $url );
251 }
252