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

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

68 lines 1.7 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 /**
45 * Replaces the WordPress Site URL with the replacement domain in 'url' and
46 * 'href' fields.
47 *
48 * @param array $data The response data.
49 */
50 function url_replace_recursive( &$data ) {
51 foreach ( $data as $key => &$value ) {
52 if ( 'generalSettings' === $key ) {
53 continue;
54 }
55
56 if ( ( 'url' === $key || 'href' === $key ) && is_string( $value ) ) {
57 $replacement = faustwp_get_setting( 'frontend_uri', '/' );
58 $value = str_replace( site_url(), $replacement, $value );
59 } elseif ( ( 'path' === $key && is_multisite() ) && is_string( $value ) ) {
60 $site = get_site();
61 $subdirectory = untrailingslashit( $site->path );
62 $value = str_replace( $subdirectory, '', $value );
63 } elseif ( is_array( $value ) ) {
64 url_replace_recursive( $value );
65 }
66 }
67 }
68