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

230 lines 6.3 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 $frontend_uri_path = wp_parse_url( $frontend_uri, PHP_URL_PATH );
126 $parsed_link_path = wp_parse_url( $link, PHP_URL_PATH );
127 $link_path = str_replace( $frontend_uri_path, '', $parsed_link_path );
128 $path = trailingslashit( $link_path );
129
130 $preview_id = isset( $args['preview_id'] ) ? $args['preview_id'] : $post->ID;
131
132 // Remove ?p=xx&preview=true from link temporarily.
133 $link = remove_query_arg(
134 array_keys( $args ),
135 $link
136 );
137
138 // Add p=xx if it's missing, which is the case for published posts.
139 if ( ! isset( $args['p'] ) ) {
140 $args['p'] = $preview_id;
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 $untrailingslash_frontend_uri = untrailingslashit( $frontend_uri );
149 $unleadingslash_path = ltrim( $path, '/\\' );
150 $link = $untrailingslash_frontend_uri . '/' . $unleadingslash_path;
151
152 // Add ?p=xx&preview=true to link again.
153 $link = add_query_arg(
154 array(
155 $args,
156 ),
157 $link
158 );
159 }
160
161 return $link;
162 }
163
164
165 add_filter( 'post_link', __NAMESPACE__ . '\\post_link', 1000 );
166 /**
167 * Callback for WordPress 'preview_post_link' filter and 'post_link' filter.
168 *
169 * Callback for WordPress 'post_link' filter.
170 *
171 * Swap post links in admin for headless front-end.
172 *
173 * @param string $link URL used for the post.
174 *
175 * @return string URL used for the post.
176 */
177 function post_link( $link ) {
178 if (
179 ! is_rewrites_enabled()
180 || ( function_exists( 'is_graphql_request' ) && is_graphql_request() )
181 ) {
182 return $link;
183 }
184
185 $frontend_uri = faustwp_get_setting( 'frontend_uri' );
186
187 if ( $frontend_uri ) {
188 return str_replace( trailingslashit( get_home_url() ), trailingslashit( $frontend_uri ), $link );
189 }
190
191 return $link;
192 }
193
194 add_filter( 'term_link', __NAMESPACE__ . '\\term_link', 1000 );
195 /**
196 * Rewrites term links to point to the specified front-end URL.
197 *
198 * @param string $term_link Term link URL.
199 *
200 * @return string
201 */
202 function term_link( $term_link ) {
203 if ( ! is_rewrites_enabled() ) {
204 return $term_link;
205 }
206
207 $frontend_uri = faustwp_get_setting( 'frontend_uri' );
208
209 if ( empty( $frontend_uri ) ) {
210 return $term_link;
211 }
212
213 $frontend_uri = trailingslashit( $frontend_uri );
214 $site_url = trailingslashit( site_url() );
215
216 return str_replace( $site_url, $frontend_uri, $term_link );
217 }
218
219
220 add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_preview_scripts' );
221 /**
222 * Adds JavaScript file to the Gutenberg editor page that prepends /preview to the preview link.
223 *
224 * XXX: Please remove this once this issue is resolved: https://github.com/WordPress/gutenberg/issues/13998
225 */
226 function enqueue_preview_scripts() {
227 wp_enqueue_script( 'faustwp-gutenberg-filters', plugins_url( '/previewlinks.js', __FILE__ ), array(), '1.0.0', true );
228 wp_localize_script( 'faustwp-gutenberg-filters', '_faustwp_preview_link', array( '_preview_link' => get_preview_post_link() ) );
229 }
230