1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\graphql\connection_resolvers; |
4
|
|
|
|
5
|
|
|
use EE_Base_Class; |
6
|
|
|
use WPGraphQL\Data\Connection\AbstractConnectionResolver as WPGraphQLConnectionResolver; |
7
|
|
|
use GraphQLRelay\Relay; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class AbstractConnectionResolver |
11
|
|
|
* Shared logic for ConnectionResolvers |
12
|
|
|
* |
13
|
|
|
* @package EventEspresso\core\services\graphql\connection_resolvers |
14
|
|
|
* @author Manzoor Ahmad Wani |
15
|
|
|
* @since $VID:$ |
16
|
|
|
*/ |
17
|
|
|
abstract class AbstractConnectionResolver extends WPGraphQLConnectionResolver |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Determine whether the Query should execute. If it's determined that the query should |
23
|
|
|
* not be run based on context such as, but not limited to, who the user is, where in the |
24
|
|
|
* ResolveTree the Query is, the relation to the node the Query is connected to, etc |
25
|
|
|
* Return false to prevent the query from executing. |
26
|
|
|
* |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
|
|
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
30
|
|
|
public function should_execute() |
31
|
|
|
{ |
32
|
|
|
return $this->should_execute; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set limit the highest value of first and last, with a (filterable) max of 100 |
37
|
|
|
* |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
protected function getLimit() |
41
|
|
|
{ |
42
|
|
|
$first = ! empty($this->args['first']) ? absint($this->args['first']) : null; |
43
|
|
|
$last = ! empty($this->args['last']) ? absint($this->args['last']) : null; |
44
|
|
|
|
45
|
|
|
$limit = min( |
46
|
|
|
max($first, $last, 100), |
47
|
|
|
$this->query_amount |
48
|
|
|
); |
49
|
|
|
$limit++; |
50
|
|
|
return $limit; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get_amount_requested |
55
|
|
|
* |
56
|
|
|
* This checks the $args to determine the amount requested |
57
|
|
|
* |
58
|
|
|
* @return int|null |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
62
|
|
|
public function get_amount_requested() |
63
|
|
|
{ |
64
|
|
|
$amount_requested = parent::get_amount_requested(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* If both first & last are used in the input args, throw an exception as that won't |
68
|
|
|
* work properly |
69
|
|
|
*/ |
70
|
|
|
if (empty($this->args['first']) && empty($this->args['last']) && $amount_requested === 10) { |
71
|
|
|
return 100; // default. |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $amount_requested; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Determine whether or not the the offset is valid, i.e the entity corresponding to the |
79
|
|
|
* offset exists. Offset is equivalent to entity ID. So this function is equivalent to |
80
|
|
|
* checking if the entity with the given ID exists. |
81
|
|
|
* |
82
|
|
|
* @access public |
83
|
|
|
* |
84
|
|
|
* @param int $offset The ID of the node used for the cursor offset |
85
|
|
|
* |
86
|
|
|
* @return bool |
87
|
|
|
*/ |
88
|
|
|
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
89
|
|
|
public function is_valid_offset($offset) |
90
|
|
|
{ |
91
|
|
|
$entity = $this->get_query()->get_one_by_ID($offset); |
92
|
|
|
|
93
|
|
|
return $entity instanceof EE_Base_Class; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Validates Model. |
98
|
|
|
* |
99
|
|
|
* @param array $entity Entity node. |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
104
|
|
|
protected function is_valid_model($entity) |
105
|
|
|
{ |
106
|
|
|
return $entity instanceof EE_Base_Class; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
112
|
|
|
* friendly keys. |
113
|
|
|
* |
114
|
|
|
* @param array $query_args |
115
|
|
|
* @param array $where_params |
116
|
|
|
* @param string $primary_key |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
protected function mapOrderbyInputArgs(array $query_args, array $where_params, $primary_key) |
120
|
|
|
{ |
121
|
|
|
// ID of the current offset |
122
|
|
|
$offset = $this->get_offset(); |
123
|
|
|
/** |
124
|
|
|
* Map the orderby inputArgs to the WP_Query |
125
|
|
|
*/ |
126
|
|
|
if (! empty($this->args['where']['orderby']) && is_array($this->args['where']['orderby'])) { |
127
|
|
|
$query_args['order_by'] = []; |
128
|
|
|
foreach ($this->args['where']['orderby'] as $orderby_input) { |
129
|
|
|
$query_args['order_by'][ $orderby_input['field'] ] = $orderby_input['order']; |
130
|
|
|
} |
131
|
|
|
} elseif ($offset) { |
|
|
|
|
132
|
|
|
$compare = $this->args['last'] ? '<' : '>'; |
133
|
|
|
$where_params[ $primary_key ] = [ $compare, $offset ]; |
134
|
|
|
} |
135
|
|
|
return [ $query_args, $where_params ]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
141
|
|
|
* friendly keys. |
142
|
|
|
* |
143
|
|
|
* @param array $where_args |
144
|
|
|
* @param array $arg_mapping |
145
|
|
|
* @param array $id_fields The fields to convert from global IDs to DB IDs. |
146
|
|
|
* @return array |
147
|
|
|
*/ |
148
|
|
|
protected function sanitizeWhereArgsForInputFields( |
149
|
|
|
array $where_args, |
150
|
|
|
array $arg_mapping, |
151
|
|
|
array $id_fields |
152
|
|
|
) { |
153
|
|
|
$query_args = []; |
154
|
|
|
|
155
|
|
|
foreach ($where_args as $arg => $value) { |
156
|
|
|
if (! array_key_exists($arg, $arg_mapping)) { |
157
|
|
|
continue; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if (is_array($value) && ! empty($value)) { |
161
|
|
|
$value = array_map( |
162
|
|
|
static function ($value) { |
163
|
|
|
if (is_string($value)) { |
164
|
|
|
$value = sanitize_text_field($value); |
165
|
|
|
} |
166
|
|
|
return $value; |
167
|
|
|
}, |
168
|
|
|
$value |
169
|
|
|
); |
170
|
|
|
} elseif (is_string($value)) { |
171
|
|
|
$value = sanitize_text_field($value); |
172
|
|
|
} |
173
|
|
|
$query_args[ $arg_mapping[ $arg ] ] = in_array($arg, $id_fields, true) |
174
|
|
|
? $this->convertGlobalId($value) |
175
|
|
|
: $value; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Return the Query Args |
180
|
|
|
*/ |
181
|
|
|
return ! empty($query_args) && is_array($query_args) ? $query_args : []; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Converts global ID to DB ID. |
187
|
|
|
* |
188
|
|
|
* @param string|string[] $ID |
189
|
|
|
* @return mixed |
190
|
|
|
*/ |
191
|
|
|
protected function convertGlobalId($ID) |
192
|
|
|
{ |
193
|
|
|
if (is_array($ID)) { |
194
|
|
|
return array_map([ $this, 'convertGlobalId' ], $ID); |
195
|
|
|
} |
196
|
|
|
$parts = Relay::fromGlobalId($ID); |
197
|
|
|
return ! empty($parts['id']) ? $parts['id'] : null; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: