@@ -21,210 +21,210 @@ |
||
21 | 21 | class DatetimeConnectionResolver extends AbstractConnectionResolver |
22 | 22 | { |
23 | 23 | |
24 | - /** |
|
25 | - * @return EEM_Datetime |
|
26 | - * @throws EE_Error |
|
27 | - * @throws InvalidArgumentException |
|
28 | - * @throws InvalidDataTypeException |
|
29 | - * @throws InvalidInterfaceException |
|
30 | - */ |
|
31 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
32 | - public function get_query() |
|
33 | - { |
|
34 | - return EEM_Datetime::instance(); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Return an array of items from the query |
|
39 | - * |
|
40 | - * @return array |
|
41 | - */ |
|
42 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
43 | - public function get_items() |
|
44 | - { |
|
45 | - $results = $this->query->get_col($this->query_args); |
|
46 | - |
|
47 | - return ! empty($results) ? $results : []; |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Determine whether the Query should execute. If it's determined that the query should |
|
52 | - * not be run based on context such as, but not limited to, who the user is, where in the |
|
53 | - * ResolveTree the Query is, the relation to the node the Query is connected to, etc |
|
54 | - * |
|
55 | - * Return false to prevent the query from executing. |
|
56 | - * |
|
57 | - * @return bool |
|
58 | - */ |
|
59 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
60 | - public function should_execute() |
|
61 | - { |
|
62 | - if (false === $this->should_execute) { |
|
63 | - return false; |
|
64 | - } |
|
65 | - |
|
66 | - return $this->should_execute; |
|
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Here, we map the args from the input, then we make sure that we're only querying |
|
71 | - * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers |
|
72 | - * handle batch resolution of the posts. |
|
73 | - * |
|
74 | - * @return array |
|
75 | - */ |
|
76 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
77 | - public function get_query_args() |
|
78 | - { |
|
79 | - $where_params = []; |
|
80 | - $query_args = []; |
|
81 | - /** |
|
82 | - * Prepare for later use |
|
83 | - */ |
|
84 | - $last = ! empty($this->args['last']) ? $this->args['last'] : null; |
|
85 | - $first = ! empty($this->args['first']) ? $this->args['first'] : null; |
|
86 | - |
|
87 | - /** |
|
88 | - * Set limit the highest value of $first and $last, with a (filterable) max of 100 |
|
89 | - */ |
|
90 | - $query_args['limit'] = min( |
|
91 | - max(absint($first), absint($last), 10), |
|
92 | - $this->query_amount |
|
93 | - ) + 1; |
|
94 | - |
|
95 | - // Avoid multiple entries by join. |
|
96 | - $query_args['group_by'] = 'DTT_ID'; |
|
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 | - * For example, if we're querying for datetime as a field of event query, this will automatically |
|
110 | - * set the query to pull datetimes that belong to that event. |
|
111 | - * We can set more cases for other source types. |
|
112 | - */ |
|
113 | - if (is_object($this->source)) { |
|
114 | - switch (true) { |
|
115 | - // It's surely an event |
|
116 | - case $this->source instanceof Post: |
|
117 | - $where_params['EVT_ID'] = $this->source->ID; |
|
118 | - break; |
|
119 | - case $this->source instanceof EE_Event: |
|
120 | - $where_params['EVT_ID'] = $this->source->ID(); |
|
121 | - break; |
|
122 | - case $this->source instanceof EE_Ticket: |
|
123 | - $where_params['Ticket.TKT_ID'] = $this->source->ID(); |
|
124 | - break; |
|
125 | - case $this->source instanceof EE_Checkin: |
|
126 | - $where_params['Checkin.CHK_ID'] = $this->source->ID(); |
|
127 | - break; |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Merge the input_fields with the default query_args |
|
133 | - */ |
|
134 | - if (! empty($input_fields)) { |
|
135 | - $where_params = array_merge($where_params, $input_fields); |
|
136 | - } |
|
137 | - |
|
138 | - // ID of the offset datetime. |
|
139 | - $offset = $this->get_offset(); |
|
140 | - |
|
141 | - /** |
|
142 | - * Map the orderby inputArgs to the WP_Query |
|
143 | - */ |
|
144 | - if (! empty($this->args['where']['orderby']) && is_array($this->args['where']['orderby'])) { |
|
145 | - $query_args['order_by'] = []; |
|
146 | - foreach ($this->args['where']['orderby'] as $orderby_input) { |
|
147 | - $query_args['order_by'][ $orderby_input['field'] ] = $orderby_input['order']; |
|
148 | - } |
|
149 | - } elseif ($offset) { |
|
150 | - $compare = ! empty($last) ? '<' : '>'; |
|
151 | - $where_params['DTT_ID'] = array($compare, $offset); |
|
152 | - } |
|
153 | - |
|
154 | - if (! empty($this->args['where']['upcoming'])) { |
|
155 | - $where_params['DTT_EVT_start'] = array( |
|
156 | - '>', |
|
157 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start') |
|
158 | - ); |
|
159 | - } |
|
160 | - |
|
161 | - if (! empty($this->args['where']['active'])) { |
|
162 | - $where_params['DTT_EVT_start'] = array( |
|
163 | - '<', |
|
164 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start') |
|
165 | - ); |
|
166 | - $where_params['DTT_EVT_end'] = array( |
|
167 | - '>', |
|
168 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end') |
|
169 | - ); |
|
170 | - } |
|
171 | - |
|
172 | - if (! empty($this->args['where']['expired'])) { |
|
173 | - $where_params['DTT_EVT_end'] = array( |
|
174 | - '<', |
|
175 | - EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end') |
|
176 | - ); |
|
177 | - } |
|
178 | - |
|
179 | - $query_args[] = $where_params; |
|
24 | + /** |
|
25 | + * @return EEM_Datetime |
|
26 | + * @throws EE_Error |
|
27 | + * @throws InvalidArgumentException |
|
28 | + * @throws InvalidDataTypeException |
|
29 | + * @throws InvalidInterfaceException |
|
30 | + */ |
|
31 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
32 | + public function get_query() |
|
33 | + { |
|
34 | + return EEM_Datetime::instance(); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Return an array of items from the query |
|
39 | + * |
|
40 | + * @return array |
|
41 | + */ |
|
42 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
43 | + public function get_items() |
|
44 | + { |
|
45 | + $results = $this->query->get_col($this->query_args); |
|
46 | + |
|
47 | + return ! empty($results) ? $results : []; |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Determine whether the Query should execute. If it's determined that the query should |
|
52 | + * not be run based on context such as, but not limited to, who the user is, where in the |
|
53 | + * ResolveTree the Query is, the relation to the node the Query is connected to, etc |
|
54 | + * |
|
55 | + * Return false to prevent the query from executing. |
|
56 | + * |
|
57 | + * @return bool |
|
58 | + */ |
|
59 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
60 | + public function should_execute() |
|
61 | + { |
|
62 | + if (false === $this->should_execute) { |
|
63 | + return false; |
|
64 | + } |
|
65 | + |
|
66 | + return $this->should_execute; |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Here, we map the args from the input, then we make sure that we're only querying |
|
71 | + * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers |
|
72 | + * handle batch resolution of the posts. |
|
73 | + * |
|
74 | + * @return array |
|
75 | + */ |
|
76 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
77 | + public function get_query_args() |
|
78 | + { |
|
79 | + $where_params = []; |
|
80 | + $query_args = []; |
|
81 | + /** |
|
82 | + * Prepare for later use |
|
83 | + */ |
|
84 | + $last = ! empty($this->args['last']) ? $this->args['last'] : null; |
|
85 | + $first = ! empty($this->args['first']) ? $this->args['first'] : null; |
|
86 | + |
|
87 | + /** |
|
88 | + * Set limit the highest value of $first and $last, with a (filterable) max of 100 |
|
89 | + */ |
|
90 | + $query_args['limit'] = min( |
|
91 | + max(absint($first), absint($last), 10), |
|
92 | + $this->query_amount |
|
93 | + ) + 1; |
|
94 | + |
|
95 | + // Avoid multiple entries by join. |
|
96 | + $query_args['group_by'] = 'DTT_ID'; |
|
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 | + * For example, if we're querying for datetime as a field of event query, this will automatically |
|
110 | + * set the query to pull datetimes that belong to that event. |
|
111 | + * We can set more cases for other source types. |
|
112 | + */ |
|
113 | + if (is_object($this->source)) { |
|
114 | + switch (true) { |
|
115 | + // It's surely an event |
|
116 | + case $this->source instanceof Post: |
|
117 | + $where_params['EVT_ID'] = $this->source->ID; |
|
118 | + break; |
|
119 | + case $this->source instanceof EE_Event: |
|
120 | + $where_params['EVT_ID'] = $this->source->ID(); |
|
121 | + break; |
|
122 | + case $this->source instanceof EE_Ticket: |
|
123 | + $where_params['Ticket.TKT_ID'] = $this->source->ID(); |
|
124 | + break; |
|
125 | + case $this->source instanceof EE_Checkin: |
|
126 | + $where_params['Checkin.CHK_ID'] = $this->source->ID(); |
|
127 | + break; |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Merge the input_fields with the default query_args |
|
133 | + */ |
|
134 | + if (! empty($input_fields)) { |
|
135 | + $where_params = array_merge($where_params, $input_fields); |
|
136 | + } |
|
137 | + |
|
138 | + // ID of the offset datetime. |
|
139 | + $offset = $this->get_offset(); |
|
140 | + |
|
141 | + /** |
|
142 | + * Map the orderby inputArgs to the WP_Query |
|
143 | + */ |
|
144 | + if (! empty($this->args['where']['orderby']) && is_array($this->args['where']['orderby'])) { |
|
145 | + $query_args['order_by'] = []; |
|
146 | + foreach ($this->args['where']['orderby'] as $orderby_input) { |
|
147 | + $query_args['order_by'][ $orderby_input['field'] ] = $orderby_input['order']; |
|
148 | + } |
|
149 | + } elseif ($offset) { |
|
150 | + $compare = ! empty($last) ? '<' : '>'; |
|
151 | + $where_params['DTT_ID'] = array($compare, $offset); |
|
152 | + } |
|
153 | + |
|
154 | + if (! empty($this->args['where']['upcoming'])) { |
|
155 | + $where_params['DTT_EVT_start'] = array( |
|
156 | + '>', |
|
157 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start') |
|
158 | + ); |
|
159 | + } |
|
160 | + |
|
161 | + if (! empty($this->args['where']['active'])) { |
|
162 | + $where_params['DTT_EVT_start'] = array( |
|
163 | + '<', |
|
164 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_start') |
|
165 | + ); |
|
166 | + $where_params['DTT_EVT_end'] = array( |
|
167 | + '>', |
|
168 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end') |
|
169 | + ); |
|
170 | + } |
|
171 | + |
|
172 | + if (! empty($this->args['where']['expired'])) { |
|
173 | + $where_params['DTT_EVT_end'] = array( |
|
174 | + '<', |
|
175 | + EEM_Datetime::instance()->current_time_for_query('DTT_EVT_end') |
|
176 | + ); |
|
177 | + } |
|
178 | + |
|
179 | + $query_args[] = $where_params; |
|
180 | 180 | |
181 | - /** |
|
182 | - * Return the $query_args |
|
183 | - */ |
|
184 | - return $query_args; |
|
185 | - } |
|
186 | - |
|
187 | - |
|
188 | - /** |
|
189 | - * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
|
190 | - * friendly keys. |
|
191 | - * |
|
192 | - * @param array $where_args |
|
193 | - * @return array |
|
194 | - */ |
|
195 | - public function sanitizeInputFields(array $where_args) |
|
196 | - { |
|
197 | - $arg_mapping = [ |
|
198 | - 'eventId' => 'EVT_ID', |
|
199 | - 'ticketId' => 'Ticket.TKT_ID', |
|
200 | - ]; |
|
201 | - |
|
202 | - $query_args = []; |
|
203 | - |
|
204 | - foreach ($where_args as $arg => $value) { |
|
205 | - if (! array_key_exists($arg, $arg_mapping)) { |
|
206 | - continue; |
|
207 | - } |
|
208 | - |
|
209 | - if (is_array($value) && ! empty($value)) { |
|
210 | - $value = array_map( |
|
211 | - function ($value) { |
|
212 | - if (is_string($value)) { |
|
213 | - $value = sanitize_text_field($value); |
|
214 | - } |
|
215 | - return $value; |
|
216 | - }, |
|
217 | - $value |
|
218 | - ); |
|
219 | - } elseif (is_string($value)) { |
|
220 | - $value = sanitize_text_field($value); |
|
221 | - } |
|
222 | - $query_args[ $arg_mapping[ $arg ] ] = $value; |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Return the Query Args |
|
227 | - */ |
|
228 | - return ! empty($query_args) && is_array($query_args) ? $query_args : []; |
|
229 | - } |
|
181 | + /** |
|
182 | + * Return the $query_args |
|
183 | + */ |
|
184 | + return $query_args; |
|
185 | + } |
|
186 | + |
|
187 | + |
|
188 | + /** |
|
189 | + * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
|
190 | + * friendly keys. |
|
191 | + * |
|
192 | + * @param array $where_args |
|
193 | + * @return array |
|
194 | + */ |
|
195 | + public function sanitizeInputFields(array $where_args) |
|
196 | + { |
|
197 | + $arg_mapping = [ |
|
198 | + 'eventId' => 'EVT_ID', |
|
199 | + 'ticketId' => 'Ticket.TKT_ID', |
|
200 | + ]; |
|
201 | + |
|
202 | + $query_args = []; |
|
203 | + |
|
204 | + foreach ($where_args as $arg => $value) { |
|
205 | + if (! array_key_exists($arg, $arg_mapping)) { |
|
206 | + continue; |
|
207 | + } |
|
208 | + |
|
209 | + if (is_array($value) && ! empty($value)) { |
|
210 | + $value = array_map( |
|
211 | + function ($value) { |
|
212 | + if (is_string($value)) { |
|
213 | + $value = sanitize_text_field($value); |
|
214 | + } |
|
215 | + return $value; |
|
216 | + }, |
|
217 | + $value |
|
218 | + ); |
|
219 | + } elseif (is_string($value)) { |
|
220 | + $value = sanitize_text_field($value); |
|
221 | + } |
|
222 | + $query_args[ $arg_mapping[ $arg ] ] = $value; |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Return the Query Args |
|
227 | + */ |
|
228 | + return ! empty($query_args) && is_array($query_args) ? $query_args : []; |
|
229 | + } |
|
230 | 230 | } |