1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\graphql\connection_resolvers; |
4
|
|
|
|
5
|
|
|
use EE_Datetime; |
6
|
|
|
use EE_Error; |
7
|
|
|
use EEM_Ticket; |
8
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
9
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use ReflectionException; |
12
|
|
|
use WPGraphQL\Data\Connection\AbstractConnectionResolver; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DatetimeConnectionResolver |
16
|
|
|
*/ |
17
|
|
|
class TicketConnectionResolver extends AbstractConnectionResolver |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return EEM_Ticket |
22
|
|
|
* @throws EE_Error |
23
|
|
|
* @throws InvalidArgumentException |
24
|
|
|
* @throws InvalidDataTypeException |
25
|
|
|
* @throws InvalidInterfaceException |
26
|
|
|
*/ |
27
|
|
|
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
28
|
|
|
public function get_query() |
29
|
|
|
{ |
30
|
|
|
return EEM_Ticket::instance(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Return an array of items from the query |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
|
|
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
40
|
|
|
public function get_items() |
41
|
|
|
{ |
42
|
|
|
$results = $this->query->get_col($this->query_args); |
43
|
|
|
|
44
|
|
|
return ! empty($results) ? $results : []; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Determine whether the Query should execute. If it's determined that the query should |
50
|
|
|
* not be run based on context such as, but not limited to, who the user is, where in the |
51
|
|
|
* ResolveTree the Query is, the relation to the node the Query is connected to, etc |
52
|
|
|
* Return false to prevent the query from executing. |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
57
|
|
|
public function should_execute() |
58
|
|
|
{ |
59
|
|
|
if (false === $this->should_execute) { |
60
|
|
|
return false; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $this->should_execute; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Here, we map the args from the input, then we make sure that we're only querying |
69
|
|
|
* for IDs. The IDs are then passed down the resolve tree, and deferred resolvers |
70
|
|
|
* handle batch resolution of the posts. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
* @throws EE_Error |
74
|
|
|
* @throws InvalidArgumentException |
75
|
|
|
* @throws ReflectionException |
76
|
|
|
* @throws InvalidDataTypeException |
77
|
|
|
* @throws InvalidInterfaceException |
78
|
|
|
*/ |
79
|
|
|
// phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
80
|
|
|
public function get_query_args() |
81
|
|
|
{ |
82
|
|
|
$where_params = []; |
83
|
|
|
$query_args = []; |
84
|
|
|
/** |
85
|
|
|
* Prepare for later use |
86
|
|
|
*/ |
87
|
|
|
$last = ! empty($this->args['last']) ? $this->args['last'] : null; |
88
|
|
|
$first = ! empty($this->args['first']) ? $this->args['first'] : null; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Set limit the highest value of $first and $last, with a (filterable) max of 100 |
92
|
|
|
*/ |
93
|
|
|
$query_args['limit'] = min( |
94
|
|
|
max(absint($first), absint($last), 10), |
95
|
|
|
$this->query_amount |
96
|
|
|
) + 1; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Collect the input_fields and sanitize them to prepare them for sending to the Query |
100
|
|
|
*/ |
101
|
|
|
$input_fields = []; |
102
|
|
|
if (! empty($this->args['where'])) { |
103
|
|
|
$input_fields = $this->sanitizeInputFields($this->args['where']); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Determine where we're at in the Graph and adjust the query context appropriately. |
108
|
|
|
*/ |
109
|
|
|
if ($this->source instanceof EE_Datetime) { |
110
|
|
|
$where_params['Datetime.DTT_ID'] = $this->source->ID(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Merge the input_fields with the default query_args |
115
|
|
|
*/ |
116
|
|
|
if (! empty($input_fields)) { |
117
|
|
|
$where_params = array_merge($where_params, $input_fields); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// ID of the offset datetime. |
121
|
|
|
$offset = $this->get_offset(); |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Map the orderby inputArgs to the WP_Query |
125
|
|
|
*/ |
126
|
|
View Code Duplication |
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 = ! empty($last) ? '<' : '>'; |
133
|
|
|
$where_params['TKT_ID'] = array($compare, $offset); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
$query_args[] = $where_params; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Return the $query_args |
140
|
|
|
*/ |
141
|
|
|
return $query_args; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
147
|
|
|
* friendly keys. |
148
|
|
|
* |
149
|
|
|
* @param array $where_args |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
public function sanitizeInputFields(array $where_args) |
153
|
|
|
{ |
154
|
|
|
$arg_mapping = [ |
155
|
|
|
'datetimeId' => 'Datetime.DTT_ID', |
156
|
|
|
]; |
157
|
|
|
|
158
|
|
|
$query_args = []; |
159
|
|
|
|
160
|
|
|
foreach ($where_args as $arg => $value) { |
161
|
|
|
if (! array_key_exists($arg, $arg_mapping)) { |
162
|
|
|
continue; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if (is_array($value) && ! empty($value)) { |
166
|
|
|
$value = array_map( |
167
|
|
|
function ($value) { |
168
|
|
|
if (is_string($value)) { |
169
|
|
|
$value = sanitize_text_field($value); |
170
|
|
|
} |
171
|
|
|
return $value; |
172
|
|
|
}, |
173
|
|
|
$value |
174
|
|
|
); |
175
|
|
|
} elseif (is_string($value)) { |
176
|
|
|
$value = sanitize_text_field($value); |
177
|
|
|
} |
178
|
|
|
$query_args[ $arg_mapping[ $arg ] ] = $value; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return the Query Args |
183
|
|
|
*/ |
184
|
|
|
return ! empty($query_args) && is_array($query_args) ? $query_args : []; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|