1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\services\graphql\connection_resolvers; |
4
|
|
|
|
5
|
|
|
use EE_Error; |
6
|
|
|
use EEM_Datetime; |
7
|
|
|
use EE_Event; |
8
|
|
|
use EE_Ticket; |
9
|
|
|
use EE_Checkin; |
10
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
11
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use WPGraphQL\Data\Connection\AbstractConnectionResolver; |
14
|
|
|
use WPGraphQL\Model\Post; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class DatetimeConnectionResolver |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class DatetimeConnectionResolver extends AbstractConnectionResolver { |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @return EEM_Datetime |
24
|
|
|
* @throws EE_Error |
25
|
|
|
* @throws InvalidArgumentException |
26
|
|
|
* @throws InvalidDataTypeException |
27
|
|
|
* @throws InvalidInterfaceException |
28
|
|
|
*/ |
29
|
|
|
public function get_query() { |
30
|
|
|
return EEM_Datetime::instance(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Return an array of items from the query |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
public function get_items() { |
39
|
|
|
|
40
|
|
|
$results = $this->query->get_col( $this->query_args ); |
41
|
|
|
|
42
|
|
|
return ! empty( $results ) ? $results : []; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Determine whether the Query should execute. If it's determined that the query should |
47
|
|
|
* not be run based on context such as, but not limited to, who the user is, where in the |
48
|
|
|
* ResolveTree the Query is, the relation to the node the Query is connected to, etc |
49
|
|
|
* |
50
|
|
|
* Return false to prevent the query from executing. |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function should_execute() { |
55
|
|
|
|
56
|
|
|
if ( false === $this->should_execute ) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->should_execute; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Here, we map the args from the input, then we make sure that we're only querying |
65
|
|
|
* for IDs. The IDs are then passed down the resolve tree, and deferred resolvers |
66
|
|
|
* handle batch resolution of the posts. |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function get_query_args() { |
71
|
|
|
|
72
|
|
|
$query_args = []; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Prepare for later use |
76
|
|
|
*/ |
77
|
|
|
$last = ! empty( $this->args['last'] ) ? $this->args['last'] : null; |
78
|
|
|
$first = ! empty( $this->args['first'] ) ? $this->args['first'] : null; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set limit the highest value of $first and $last, with a (filterable) max of 100 |
82
|
|
|
*/ |
83
|
|
|
$query_args['limit'] = min( |
84
|
|
|
max( absint( $first ), absint( $last ), 10 ), |
85
|
|
|
$this->query_amount |
86
|
|
|
) + 1; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Collect the input_fields and sanitize them to prepare them for sending to the Query |
90
|
|
|
*/ |
91
|
|
|
$input_fields = []; |
92
|
|
|
if ( ! empty( $this->args['where'] ) ) { |
93
|
|
|
$input_fields = $this->sanitize_input_fields( $this->args['where'] ); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Determine where we're at in the Graph and adjust the query context appropriately. |
98
|
|
|
* |
99
|
|
|
* For example, if we're querying for datetime as a field of event query, this will automatically |
100
|
|
|
* set the query to pull datetimes that belong to that event. |
101
|
|
|
* We can set more cases for other source types. |
102
|
|
|
*/ |
103
|
|
|
if (is_object( $this->source )) { |
104
|
|
|
switch (true) { |
105
|
|
|
// It's surely an event |
106
|
|
|
case $this->source instanceof Post: |
107
|
|
|
$query_args[] = ['EVT_ID' => $this->source->ID]; |
108
|
|
|
break; |
109
|
|
|
case $this->source instanceof EE_Event: |
110
|
|
|
$query_args[] = ['EVT_ID' => $this->source->ID()]; |
111
|
|
|
break; |
112
|
|
|
case $this->source instanceof EE_Ticket: |
113
|
|
|
$query_args[] = ['Ticket.TKT_ID' => $this->source->ID()]; |
114
|
|
|
break; |
115
|
|
|
case $this->source instanceof EE_Checkin: |
116
|
|
|
$query_args[] = ['Checkin.CHK_ID' => $this->source->ID()]; |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Merge the input_fields with the default query_args |
123
|
|
|
*/ |
124
|
|
|
if ( ! empty( $input_fields ) ) { |
125
|
|
|
$query_args = array_merge( $query_args, $input_fields ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Return the $query_args |
130
|
|
|
*/ |
131
|
|
|
return $query_args; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* This sets up the "allowed" args, and translates the GraphQL-friendly keys to WP_Query |
137
|
|
|
* friendly keys. There's probably a cleaner/more dynamic way to approach this, but |
138
|
|
|
* this was quick. I'd be down to explore more dynamic ways to map this, but for |
139
|
|
|
* now this gets the job done. |
140
|
|
|
* |
141
|
|
|
* @param array $query_args |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
View Code Duplication |
public function sanitize_input_fields(array $query_args) { |
145
|
|
|
|
146
|
|
|
$arg_mapping = [ |
|
|
|
|
147
|
|
|
'orderBy' => 'order_by', |
148
|
|
|
'order' => 'order', |
149
|
|
|
]; |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Return the Query Args |
153
|
|
|
*/ |
154
|
|
|
return ! empty( $query_args ) && is_array( $query_args ) ? $query_args : []; |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.