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