1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\entities\admin\GraphQLData; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class GraphQLData |
9
|
|
|
* abstract parent class for processing GQL requests |
10
|
|
|
* |
11
|
|
|
* @package EventEspresso\core\domain\entities\admin\GraphQLData |
12
|
|
|
* @author Manzoor Wani |
13
|
|
|
* @since $VID:$ |
14
|
|
|
*/ |
15
|
|
|
abstract class GraphQLData implements GraphQLDataInterface |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string $namespace The graphql namespace/prefix. |
20
|
|
|
*/ |
21
|
|
|
protected $namespace = 'Espresso'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array $params |
25
|
|
|
*/ |
26
|
|
|
private $params = []; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $params |
31
|
|
|
*/ |
32
|
|
|
public function setParams(array $params) |
33
|
|
|
{ |
34
|
|
|
$this->params = $params; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $field_key |
41
|
|
|
* @param array $where_params |
42
|
|
|
* @return mixed|null |
43
|
|
|
* @since $VID:$ |
44
|
|
|
*/ |
45
|
|
|
protected function getQueryResponse(string $field_key, array $where_params = []) |
46
|
|
|
{ |
47
|
|
|
if (! empty($where_params)) { |
48
|
|
|
if (! array_key_exists('variables', $this->params)) { |
49
|
|
|
$this->params['variables'] = []; |
50
|
|
|
} |
51
|
|
|
$this->params['variables']['where'] = $where_params; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$responseData = $this->makeGraphQLRequest($this->params); |
55
|
|
|
return ! empty($responseData[ $field_key ]) ? $responseData[ $field_key ] : null; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $data |
61
|
|
|
* @return array|null |
62
|
|
|
* @since $VID:$ |
63
|
|
|
*/ |
64
|
|
|
protected function makeGraphQLRequest(array $data) |
65
|
|
|
{ |
66
|
|
|
$error = ''; |
67
|
|
|
try { |
68
|
|
|
$response = graphql($data); |
69
|
|
|
if (! empty($response['data'])) { |
70
|
|
|
return $response['data']; |
71
|
|
|
} |
72
|
|
|
$error = ! empty($response['errors']) |
73
|
|
|
? print_r($response['errors'], true) |
74
|
|
|
: esc_html__( |
75
|
|
|
'An unknown error has occurred during the GraphQL request and no data was found to return.', |
76
|
|
|
'event_espresso' |
77
|
|
|
); |
78
|
|
|
} catch (Exception $e) { |
79
|
|
|
if (defined('GRAPHQL_DEBUG') && GRAPHQL_DEBUG) { |
80
|
|
|
$error = $e->getMessage(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
if ($error !== '') { |
84
|
|
|
error_log($error); |
85
|
|
|
} |
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|