|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\graphql; |
|
4
|
|
|
|
|
5
|
|
|
use EventEspresso\core\services\database\WordPressOption; |
|
6
|
|
|
use Throwable; |
|
7
|
|
|
use WPGraphQL; |
|
8
|
|
|
use WPGraphQL\Data\Config; |
|
9
|
|
|
use WPGraphQL\Router; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class GraphQLEndpoint |
|
13
|
|
|
* A class for saving and obtaining our own little copy of the GQL request endpoint |
|
14
|
|
|
* |
|
15
|
|
|
* @author Brent Christensen |
|
16
|
|
|
* @package EventEspresso\core\services\graphql |
|
17
|
|
|
* @since $VID:$ |
|
18
|
|
|
*/ |
|
19
|
|
|
class GraphQLEndpoint extends WordPressOption |
|
20
|
|
|
{ |
|
21
|
|
|
const DEFAULT_ENDPOINT = 'graphql'; |
|
22
|
|
|
|
|
23
|
|
|
const OPTION_NAME = 'ee-graphql-endpoint'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var boolean |
|
27
|
|
|
*/ |
|
28
|
|
|
private $is_gql_request; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* GraphQLEndpoint constructor. |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
|
|
parent::__construct(GraphQLEndpoint::OPTION_NAME, GraphQLEndpoint::DEFAULT_ENDPOINT, true); |
|
37
|
|
|
add_action('graphql_register_settings', [$this, 'verifyAndSetEndpoint'], 20); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return false|mixed|void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getEndpoint() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->loadOption(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return mixed|void|bool |
|
52
|
|
|
*/ |
|
53
|
|
|
public function isGraphqlRequest() |
|
54
|
|
|
{ |
|
55
|
|
|
if (! isset($this->is_gql_request)) { |
|
56
|
|
|
// grab the GQL endpoint that we saved in the future... wait... wut? |
|
57
|
|
|
$endpoint = $this->getEndpoint(); |
|
58
|
|
|
if (! class_exists('WPGraphQL')) { |
|
59
|
|
|
require_once EE_THIRD_PARTY . 'wp-graphql/src/Router.php'; |
|
60
|
|
|
} |
|
61
|
|
|
// set our saved endpoint on the WP GQL Pouter class |
|
62
|
|
|
// don't worry, this is a static property that they overwrite when they initialize things, |
|
63
|
|
|
// and we are essentially getting the value from them anyways, so it should be ok (fingers crossed) |
|
64
|
|
|
Router::$route = $endpoint; |
|
65
|
|
|
// now call their function for checking if this is a GQL request |
|
66
|
|
|
// because they use a bunch of filters and stuff we don't want to duplicate here |
|
67
|
|
|
$this->is_gql_request = Router::is_graphql_http_request(); |
|
68
|
|
|
} |
|
69
|
|
|
return $this->is_gql_request; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* callback hooked into the graphql_register_settings action |
|
75
|
|
|
* basically grabs the value for the 'graphql_endpoint' setting and saves it to our own WP option. |
|
76
|
|
|
* |
|
77
|
|
|
* i know, i know, i know... |
|
78
|
|
|
* WHY are we saving the graphql_endpoint to a WP option if WP GraphQL is already doing the same ?!?! |
|
79
|
|
|
* |
|
80
|
|
|
* Because we perform our request type detection during `plugins_loaded`, |
|
81
|
|
|
* but they don't settle on their endpoint until `after_setup_theme` has run, |
|
82
|
|
|
* which is too late for us, so we are just going to save this separately so that we have it. |
|
83
|
|
|
* Oh, and this will essentially recheck this value on EVERY request |
|
84
|
|
|
* but will only update our saved value if it has actually changed |
|
85
|
|
|
*/ |
|
86
|
|
|
public function verifyAndSetEndpoint() |
|
87
|
|
|
{ |
|
88
|
|
|
try { |
|
89
|
|
|
$this->updateOption( |
|
90
|
|
|
get_graphql_setting( |
|
91
|
|
|
'graphql_endpoint', |
|
92
|
|
|
apply_filters('graphql_endpoint', GraphQLEndpoint::DEFAULT_ENDPOINT) |
|
93
|
|
|
) |
|
94
|
|
|
); |
|
95
|
|
|
} catch (Throwable $t) { |
|
|
|
|
|
|
96
|
|
|
// eat it |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|