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 / graphql-callbacks.php

graphql-callbacks.php in Faust.js 0.7.1, at includes/replacement/graphql-callbacks.php

58 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Replacements for GraphQL responses.
4 *
5 * @package FaustWP
6 */
7
8 namespace WPE\FaustWP\Replacement;
9
10 use function WPE\FaustWP\Settings\faustwp_get_setting;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 add_filter( 'graphql_request_results', __NAMESPACE__ . '\\url_replacement' );
17 /**
18 * Callback for WP GraphQL 'graphql_request_results' filter.
19 *
20 * Replaces the WordPress Site URL with the replacement domain in 'url' and
21 * 'href' fields. Response data for RootQuery.generalSettings is intentionally
22 * left unaltered.
23 *
24 * @param object $response The default GraphQL query response.
25 *
26 * @return object The modified response with URLs replaced.
27 */
28 function url_replacement( $response ) {
29 if ( ! domain_replacement_enabled() ) {
30 return $response;
31 }
32
33 if (
34 is_object( $response ) &&
35 property_exists( $response, 'data' ) &&
36 is_array( $response->data )
37 ) {
38 url_replace_recursive( $response->data );
39 }
40
41 return $response;
42 }
43
44 function url_replace_recursive( &$data ) {
45 foreach ( $data as $key => &$value ) {
46 if ( $key === 'generalSettings' ) {
47 continue;
48 }
49
50 if ( ( 'url' === $key || 'href' === $key ) && is_string( $value ) ) {
51 $replacement = faustwp_get_setting( 'frontend_uri', '/' );
52 $value = str_replace( site_url(), $replacement, $value );
53 } else if ( is_array( $value ) ) {
54 url_replace_recursive( $value );
55 }
56 }
57 }
58