@@ -16,170 +16,170 @@ |
||
16 | 16 | */ |
17 | 17 | class CountryConnectionResolver extends AbstractConnectionResolver |
18 | 18 | { |
19 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
20 | - public function get_loader_name(): string |
|
21 | - { |
|
22 | - return 'espresso_country'; |
|
23 | - } |
|
24 | - |
|
25 | - /** |
|
26 | - * @return EEM_Country |
|
27 | - * @throws EE_Error |
|
28 | - * @throws InvalidArgumentException |
|
29 | - * @throws InvalidDataTypeException |
|
30 | - * @throws InvalidInterfaceException |
|
31 | - * @throws ReflectionException |
|
32 | - */ |
|
33 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
34 | - public function get_query(): EEM_Country |
|
35 | - { |
|
36 | - return EEM_Country::instance(); |
|
37 | - } |
|
38 | - |
|
39 | - |
|
40 | - /** |
|
41 | - * Return an array of item IDs from the query |
|
42 | - * |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
46 | - public function get_ids(): array |
|
47 | - { |
|
48 | - $results = $this->query->get_col($this->query_args); |
|
49 | - |
|
50 | - return ! empty($results) ? $results : []; |
|
51 | - } |
|
52 | - |
|
53 | - /** |
|
54 | - * Get_query_amount |
|
55 | - * |
|
56 | - * Returns the max between what was requested and what is defined as the $max_query_amount to |
|
57 | - * ensure that queries don't exceed unwanted limits when querying data. |
|
58 | - * |
|
59 | - * @return int |
|
60 | - * @throws Exception |
|
61 | - */ |
|
62 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
63 | - public function get_query_amount() |
|
64 | - { |
|
65 | - // Override the default limit (100) for countries |
|
66 | - return 500; |
|
67 | - } |
|
68 | - |
|
69 | - |
|
70 | - /** |
|
71 | - * Here, we map the args from the input, then we make sure that we're only querying |
|
72 | - * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers |
|
73 | - * handle batch resolution of the posts. |
|
74 | - * |
|
75 | - * @return array |
|
76 | - * @throws InvalidArgumentException |
|
77 | - * @throws InvalidDataTypeException |
|
78 | - * @throws InvalidInterfaceException |
|
79 | - */ |
|
80 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
81 | - public function get_query_args(): array |
|
82 | - { |
|
83 | - $where_params = []; |
|
84 | - $query_args = []; |
|
85 | - |
|
86 | - $query_args['limit'] = $this->getLimit(); |
|
87 | - |
|
88 | - // Avoid multiple entries by join. |
|
89 | - $query_args['group_by'] = 'CNT_ISO'; |
|
90 | - |
|
91 | - $query_args['default_where_conditions'] = 'minimum'; |
|
92 | - |
|
93 | - /** |
|
94 | - * Collect the input_fields and sanitize them to prepare them for sending to the Query |
|
95 | - */ |
|
96 | - $input_fields = []; |
|
97 | - if (! empty($this->args['where'])) { |
|
98 | - $input_fields = $this->sanitizeInputFields($this->args['where']); |
|
99 | - |
|
100 | - // Since we do not have any falsy values in query params |
|
101 | - // Lets get rid of empty values |
|
102 | - $input_fields = array_filter($input_fields); |
|
103 | - |
|
104 | - // Use the proper operator. |
|
105 | - if (! empty($input_fields['CNT_ISO']) && is_array($input_fields['CNT_ISO'])) { |
|
106 | - $input_fields['CNT_ISO'] = ['IN', $input_fields['CNT_ISO']]; |
|
107 | - } |
|
108 | - if (! empty($input_fields['CNT_ISO3']) && is_array($input_fields['CNT_ISO3'])) { |
|
109 | - $input_fields['CNT_ISO3'] = ['IN', $input_fields['CNT_ISO3']]; |
|
110 | - } |
|
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 | - // limit to active countries by default. |
|
121 | - if (!isset($this->args['where']['activeOnly']) || $this->args['where']['activeOnly']) { |
|
122 | - $where_params['CNT_active'] = true; |
|
123 | - } |
|
124 | - |
|
125 | - [$query_args, $where_params] = $this->mapOrderbyInputArgs($query_args, $where_params, 'CNT_ISO'); |
|
126 | - |
|
127 | - if (empty($query_args['order_by'])) { |
|
128 | - // set order_by to 'name' by default |
|
129 | - $query_args['order_by'] = [ |
|
130 | - 'CNT_name' => 'ASC', |
|
131 | - ]; |
|
132 | - } |
|
133 | - |
|
134 | - $search = $this->getSearchKeywords($this->args['where']); |
|
135 | - |
|
136 | - if (! empty($search)) { |
|
137 | - // use OR operator to search in any of the fields |
|
138 | - $where_params['OR'] = array( |
|
139 | - 'CNT_name' => array('LIKE', '%' . $search . '%'), |
|
140 | - 'CNT_ISO' => array('LIKE', '%' . $search . '%'), |
|
141 | - ); |
|
142 | - } |
|
143 | - |
|
144 | - $where_params = apply_filters( |
|
145 | - 'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__country_where_params', |
|
146 | - $where_params, |
|
147 | - $this->source, |
|
148 | - $this->args |
|
149 | - ); |
|
150 | - |
|
151 | - $query_args[] = $where_params; |
|
152 | - |
|
153 | - /** |
|
154 | - * Return the $query_args |
|
155 | - */ |
|
156 | - return apply_filters( |
|
157 | - 'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__country_query_args', |
|
158 | - $query_args, |
|
159 | - $this->source, |
|
160 | - $this->args |
|
161 | - ); |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - /** |
|
166 | - * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
|
167 | - * friendly keys. |
|
168 | - * |
|
169 | - * @param array $where_args |
|
170 | - * @return array |
|
171 | - */ |
|
172 | - public function sanitizeInputFields(array $where_args): array |
|
173 | - { |
|
174 | - $arg_mapping = [ |
|
175 | - 'isoIn' => 'CNT_ISO', |
|
176 | - 'in' => 'CNT_ISO', |
|
177 | - 'iso3In' => 'CNT_ISO3', |
|
178 | - ]; |
|
179 | - return $this->sanitizeWhereArgsForInputFields( |
|
180 | - $where_args, |
|
181 | - $arg_mapping, |
|
182 | - ['in'] |
|
183 | - ); |
|
184 | - } |
|
19 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
20 | + public function get_loader_name(): string |
|
21 | + { |
|
22 | + return 'espresso_country'; |
|
23 | + } |
|
24 | + |
|
25 | + /** |
|
26 | + * @return EEM_Country |
|
27 | + * @throws EE_Error |
|
28 | + * @throws InvalidArgumentException |
|
29 | + * @throws InvalidDataTypeException |
|
30 | + * @throws InvalidInterfaceException |
|
31 | + * @throws ReflectionException |
|
32 | + */ |
|
33 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
34 | + public function get_query(): EEM_Country |
|
35 | + { |
|
36 | + return EEM_Country::instance(); |
|
37 | + } |
|
38 | + |
|
39 | + |
|
40 | + /** |
|
41 | + * Return an array of item IDs from the query |
|
42 | + * |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
46 | + public function get_ids(): array |
|
47 | + { |
|
48 | + $results = $this->query->get_col($this->query_args); |
|
49 | + |
|
50 | + return ! empty($results) ? $results : []; |
|
51 | + } |
|
52 | + |
|
53 | + /** |
|
54 | + * Get_query_amount |
|
55 | + * |
|
56 | + * Returns the max between what was requested and what is defined as the $max_query_amount to |
|
57 | + * ensure that queries don't exceed unwanted limits when querying data. |
|
58 | + * |
|
59 | + * @return int |
|
60 | + * @throws Exception |
|
61 | + */ |
|
62 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
63 | + public function get_query_amount() |
|
64 | + { |
|
65 | + // Override the default limit (100) for countries |
|
66 | + return 500; |
|
67 | + } |
|
68 | + |
|
69 | + |
|
70 | + /** |
|
71 | + * Here, we map the args from the input, then we make sure that we're only querying |
|
72 | + * for IDs. The IDs are then passed down the resolve tree, and deferred resolvers |
|
73 | + * handle batch resolution of the posts. |
|
74 | + * |
|
75 | + * @return array |
|
76 | + * @throws InvalidArgumentException |
|
77 | + * @throws InvalidDataTypeException |
|
78 | + * @throws InvalidInterfaceException |
|
79 | + */ |
|
80 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
81 | + public function get_query_args(): array |
|
82 | + { |
|
83 | + $where_params = []; |
|
84 | + $query_args = []; |
|
85 | + |
|
86 | + $query_args['limit'] = $this->getLimit(); |
|
87 | + |
|
88 | + // Avoid multiple entries by join. |
|
89 | + $query_args['group_by'] = 'CNT_ISO'; |
|
90 | + |
|
91 | + $query_args['default_where_conditions'] = 'minimum'; |
|
92 | + |
|
93 | + /** |
|
94 | + * Collect the input_fields and sanitize them to prepare them for sending to the Query |
|
95 | + */ |
|
96 | + $input_fields = []; |
|
97 | + if (! empty($this->args['where'])) { |
|
98 | + $input_fields = $this->sanitizeInputFields($this->args['where']); |
|
99 | + |
|
100 | + // Since we do not have any falsy values in query params |
|
101 | + // Lets get rid of empty values |
|
102 | + $input_fields = array_filter($input_fields); |
|
103 | + |
|
104 | + // Use the proper operator. |
|
105 | + if (! empty($input_fields['CNT_ISO']) && is_array($input_fields['CNT_ISO'])) { |
|
106 | + $input_fields['CNT_ISO'] = ['IN', $input_fields['CNT_ISO']]; |
|
107 | + } |
|
108 | + if (! empty($input_fields['CNT_ISO3']) && is_array($input_fields['CNT_ISO3'])) { |
|
109 | + $input_fields['CNT_ISO3'] = ['IN', $input_fields['CNT_ISO3']]; |
|
110 | + } |
|
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 | + // limit to active countries by default. |
|
121 | + if (!isset($this->args['where']['activeOnly']) || $this->args['where']['activeOnly']) { |
|
122 | + $where_params['CNT_active'] = true; |
|
123 | + } |
|
124 | + |
|
125 | + [$query_args, $where_params] = $this->mapOrderbyInputArgs($query_args, $where_params, 'CNT_ISO'); |
|
126 | + |
|
127 | + if (empty($query_args['order_by'])) { |
|
128 | + // set order_by to 'name' by default |
|
129 | + $query_args['order_by'] = [ |
|
130 | + 'CNT_name' => 'ASC', |
|
131 | + ]; |
|
132 | + } |
|
133 | + |
|
134 | + $search = $this->getSearchKeywords($this->args['where']); |
|
135 | + |
|
136 | + if (! empty($search)) { |
|
137 | + // use OR operator to search in any of the fields |
|
138 | + $where_params['OR'] = array( |
|
139 | + 'CNT_name' => array('LIKE', '%' . $search . '%'), |
|
140 | + 'CNT_ISO' => array('LIKE', '%' . $search . '%'), |
|
141 | + ); |
|
142 | + } |
|
143 | + |
|
144 | + $where_params = apply_filters( |
|
145 | + 'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__country_where_params', |
|
146 | + $where_params, |
|
147 | + $this->source, |
|
148 | + $this->args |
|
149 | + ); |
|
150 | + |
|
151 | + $query_args[] = $where_params; |
|
152 | + |
|
153 | + /** |
|
154 | + * Return the $query_args |
|
155 | + */ |
|
156 | + return apply_filters( |
|
157 | + 'FHEE__EventEspresso_core_domain_services_graphql_connection_resolvers__country_query_args', |
|
158 | + $query_args, |
|
159 | + $this->source, |
|
160 | + $this->args |
|
161 | + ); |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + /** |
|
166 | + * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
|
167 | + * friendly keys. |
|
168 | + * |
|
169 | + * @param array $where_args |
|
170 | + * @return array |
|
171 | + */ |
|
172 | + public function sanitizeInputFields(array $where_args): array |
|
173 | + { |
|
174 | + $arg_mapping = [ |
|
175 | + 'isoIn' => 'CNT_ISO', |
|
176 | + 'in' => 'CNT_ISO', |
|
177 | + 'iso3In' => 'CNT_ISO3', |
|
178 | + ]; |
|
179 | + return $this->sanitizeWhereArgsForInputFields( |
|
180 | + $where_args, |
|
181 | + $arg_mapping, |
|
182 | + ['in'] |
|
183 | + ); |
|
184 | + } |
|
185 | 185 | } |
@@ -19,174 +19,174 @@ |
||
19 | 19 | */ |
20 | 20 | abstract class AbstractConnectionResolver extends WPGraphQLConnectionResolver |
21 | 21 | { |
22 | - const MAX_QUERY_LIMIT = 250; |
|
23 | - |
|
24 | - /** |
|
25 | - * @var Utilities |
|
26 | - */ |
|
27 | - private $utilities; |
|
28 | - |
|
29 | - |
|
30 | - /** |
|
31 | - * @return Utilities |
|
32 | - */ |
|
33 | - public function getUtilities(): Utilities |
|
34 | - { |
|
35 | - if (! $this->utilities instanceof Utilities) { |
|
36 | - $this->utilities = LoaderFactory::getLoader()->getShared(Utilities::class); |
|
37 | - } |
|
38 | - return $this->utilities; |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * Determine whether the Query should execute. If it's determined that the query should |
|
43 | - * not be run based on context such as, but not limited to, who the user is, where in the |
|
44 | - * ResolveTree the Query is, the relation to the node the Query is connected to, etc |
|
45 | - * Return false to prevent the query from executing. |
|
46 | - * |
|
47 | - * @return bool |
|
48 | - */ |
|
49 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
50 | - public function should_execute(): bool |
|
51 | - { |
|
52 | - return $this->should_execute; |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * Set limit the highest value of first and last, with a (filterable) max of 500 |
|
58 | - * |
|
59 | - * @return int |
|
60 | - */ |
|
61 | - protected function getLimit(): int |
|
62 | - { |
|
63 | - $first = ! empty($this->args['first']) |
|
64 | - ? absint($this->args['first']) |
|
65 | - : null; |
|
66 | - $last = ! empty($this->args['last']) |
|
67 | - ? absint($this->args['last']) |
|
68 | - : null; |
|
69 | - |
|
70 | - $limit = min( |
|
71 | - max($first, $last, self::MAX_QUERY_LIMIT), |
|
72 | - $this->query_amount |
|
73 | - ); |
|
74 | - $limit++; |
|
75 | - return $limit; |
|
76 | - } |
|
77 | - |
|
78 | - // /** |
|
79 | - // * Get_amount_requested |
|
80 | - // * |
|
81 | - // * This checks the $args to determine the amount requested |
|
82 | - // * |
|
83 | - // * @return int|null |
|
84 | - // * @throws Exception |
|
85 | - // */ |
|
86 | - // // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
87 | - // public function get_amount_requested(): ?int |
|
88 | - // { |
|
89 | - // $amount_requested = parent::get_amount_requested(); |
|
90 | - // |
|
91 | - // /** |
|
92 | - // * If both first & last are used in the input args, throw an exception as that won't |
|
93 | - // * work properly |
|
94 | - // */ |
|
95 | - // if (empty($this->args['first']) && empty($this->args['last']) && $amount_requested === ConnectionsManager::MAX_AMOUNT_REQUESTED) { |
|
96 | - // return ConnectionsManager::MAX_AMOUNT_REQUESTED; // default. |
|
97 | - // } |
|
98 | - // |
|
99 | - // return $amount_requested; |
|
100 | - // } |
|
101 | - |
|
102 | - /** |
|
103 | - * Determine whether or not the the offset is valid, i.e the entity corresponding to the |
|
104 | - * offset exists. Offset is equivalent to entity ID. So this function is equivalent to |
|
105 | - * checking if the entity with the given ID exists. |
|
106 | - * |
|
107 | - * @param int $offset The ID of the node used for the cursor offset |
|
108 | - * @return bool |
|
109 | - */ |
|
110 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
111 | - public function is_valid_offset($offset): bool |
|
112 | - { |
|
113 | - $entity = $this->get_query()->get_one_by_ID($offset); |
|
114 | - |
|
115 | - return $entity instanceof EE_Base_Class; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Validates Model. |
|
120 | - * |
|
121 | - * @param array $model Entity node. |
|
122 | - * @return bool |
|
123 | - */ |
|
124 | - // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
125 | - protected function is_valid_model($model): bool |
|
126 | - { |
|
127 | - return $model instanceof EE_Base_Class; |
|
128 | - } |
|
129 | - |
|
130 | - |
|
131 | - /** |
|
132 | - * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
|
133 | - * friendly keys. |
|
134 | - * |
|
135 | - * @param array $query_args |
|
136 | - * @param array $where_params |
|
137 | - * @param string $primary_key |
|
138 | - * @return array |
|
139 | - */ |
|
140 | - protected function mapOrderbyInputArgs(array $query_args, array $where_params, string $primary_key): array |
|
141 | - { |
|
142 | - // ID of the current offset |
|
143 | - $offset = $this->get_offset(); |
|
144 | - /** |
|
145 | - * Map the orderby inputArgs to the WP_Query |
|
146 | - */ |
|
147 | - if (! empty($this->args['where']['orderby']) && is_array($this->args['where']['orderby'])) { |
|
148 | - $query_args['order_by'] = []; |
|
149 | - foreach ($this->args['where']['orderby'] as $orderby_input) { |
|
150 | - $query_args['order_by'][ $orderby_input['field'] ] = $orderby_input['order']; |
|
151 | - } |
|
152 | - } elseif ($offset) { |
|
153 | - $compare = $this->args['last'] ? '<' : '>'; |
|
154 | - $where_params[ $primary_key ] = [$compare, $offset]; |
|
155 | - } |
|
156 | - return [$query_args, $where_params]; |
|
157 | - } |
|
158 | - |
|
159 | - |
|
160 | - /** |
|
161 | - * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
|
162 | - * friendly keys. |
|
163 | - * |
|
164 | - * @param array $where_args |
|
165 | - * @param array $arg_mapping |
|
166 | - * @param array $id_fields The fields to convert from global IDs to DB IDs. |
|
167 | - * @return array |
|
168 | - */ |
|
169 | - protected function sanitizeWhereArgsForInputFields(array $where_args, array $arg_mapping, array $id_fields = []): array |
|
170 | - { |
|
171 | - $query_args = $this->getUtilities()->sanitizeWhereArgs($where_args, $arg_mapping, $id_fields); |
|
172 | - return ! empty($query_args) && is_array($query_args) |
|
173 | - ? $query_args |
|
174 | - : []; |
|
175 | - } |
|
176 | - |
|
177 | - |
|
178 | - /** |
|
179 | - * This returns the sanitized "search" keywords from where_args |
|
180 | - * |
|
181 | - * @param array $where_args |
|
182 | - * @return string |
|
183 | - */ |
|
184 | - protected function getSearchKeywords(array $where_args): string |
|
185 | - { |
|
186 | - $search = ''; |
|
187 | - if (! empty($where_args['search'])) { |
|
188 | - $search = sanitize_text_field($where_args['search']); |
|
189 | - } |
|
190 | - return esc_sql($search); |
|
191 | - } |
|
22 | + const MAX_QUERY_LIMIT = 250; |
|
23 | + |
|
24 | + /** |
|
25 | + * @var Utilities |
|
26 | + */ |
|
27 | + private $utilities; |
|
28 | + |
|
29 | + |
|
30 | + /** |
|
31 | + * @return Utilities |
|
32 | + */ |
|
33 | + public function getUtilities(): Utilities |
|
34 | + { |
|
35 | + if (! $this->utilities instanceof Utilities) { |
|
36 | + $this->utilities = LoaderFactory::getLoader()->getShared(Utilities::class); |
|
37 | + } |
|
38 | + return $this->utilities; |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * Determine whether the Query should execute. If it's determined that the query should |
|
43 | + * not be run based on context such as, but not limited to, who the user is, where in the |
|
44 | + * ResolveTree the Query is, the relation to the node the Query is connected to, etc |
|
45 | + * Return false to prevent the query from executing. |
|
46 | + * |
|
47 | + * @return bool |
|
48 | + */ |
|
49 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
50 | + public function should_execute(): bool |
|
51 | + { |
|
52 | + return $this->should_execute; |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * Set limit the highest value of first and last, with a (filterable) max of 500 |
|
58 | + * |
|
59 | + * @return int |
|
60 | + */ |
|
61 | + protected function getLimit(): int |
|
62 | + { |
|
63 | + $first = ! empty($this->args['first']) |
|
64 | + ? absint($this->args['first']) |
|
65 | + : null; |
|
66 | + $last = ! empty($this->args['last']) |
|
67 | + ? absint($this->args['last']) |
|
68 | + : null; |
|
69 | + |
|
70 | + $limit = min( |
|
71 | + max($first, $last, self::MAX_QUERY_LIMIT), |
|
72 | + $this->query_amount |
|
73 | + ); |
|
74 | + $limit++; |
|
75 | + return $limit; |
|
76 | + } |
|
77 | + |
|
78 | + // /** |
|
79 | + // * Get_amount_requested |
|
80 | + // * |
|
81 | + // * This checks the $args to determine the amount requested |
|
82 | + // * |
|
83 | + // * @return int|null |
|
84 | + // * @throws Exception |
|
85 | + // */ |
|
86 | + // // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
87 | + // public function get_amount_requested(): ?int |
|
88 | + // { |
|
89 | + // $amount_requested = parent::get_amount_requested(); |
|
90 | + // |
|
91 | + // /** |
|
92 | + // * If both first & last are used in the input args, throw an exception as that won't |
|
93 | + // * work properly |
|
94 | + // */ |
|
95 | + // if (empty($this->args['first']) && empty($this->args['last']) && $amount_requested === ConnectionsManager::MAX_AMOUNT_REQUESTED) { |
|
96 | + // return ConnectionsManager::MAX_AMOUNT_REQUESTED; // default. |
|
97 | + // } |
|
98 | + // |
|
99 | + // return $amount_requested; |
|
100 | + // } |
|
101 | + |
|
102 | + /** |
|
103 | + * Determine whether or not the the offset is valid, i.e the entity corresponding to the |
|
104 | + * offset exists. Offset is equivalent to entity ID. So this function is equivalent to |
|
105 | + * checking if the entity with the given ID exists. |
|
106 | + * |
|
107 | + * @param int $offset The ID of the node used for the cursor offset |
|
108 | + * @return bool |
|
109 | + */ |
|
110 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
111 | + public function is_valid_offset($offset): bool |
|
112 | + { |
|
113 | + $entity = $this->get_query()->get_one_by_ID($offset); |
|
114 | + |
|
115 | + return $entity instanceof EE_Base_Class; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Validates Model. |
|
120 | + * |
|
121 | + * @param array $model Entity node. |
|
122 | + * @return bool |
|
123 | + */ |
|
124 | + // phpcs:ignore PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
|
125 | + protected function is_valid_model($model): bool |
|
126 | + { |
|
127 | + return $model instanceof EE_Base_Class; |
|
128 | + } |
|
129 | + |
|
130 | + |
|
131 | + /** |
|
132 | + * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
|
133 | + * friendly keys. |
|
134 | + * |
|
135 | + * @param array $query_args |
|
136 | + * @param array $where_params |
|
137 | + * @param string $primary_key |
|
138 | + * @return array |
|
139 | + */ |
|
140 | + protected function mapOrderbyInputArgs(array $query_args, array $where_params, string $primary_key): array |
|
141 | + { |
|
142 | + // ID of the current offset |
|
143 | + $offset = $this->get_offset(); |
|
144 | + /** |
|
145 | + * Map the orderby inputArgs to the WP_Query |
|
146 | + */ |
|
147 | + if (! empty($this->args['where']['orderby']) && is_array($this->args['where']['orderby'])) { |
|
148 | + $query_args['order_by'] = []; |
|
149 | + foreach ($this->args['where']['orderby'] as $orderby_input) { |
|
150 | + $query_args['order_by'][ $orderby_input['field'] ] = $orderby_input['order']; |
|
151 | + } |
|
152 | + } elseif ($offset) { |
|
153 | + $compare = $this->args['last'] ? '<' : '>'; |
|
154 | + $where_params[ $primary_key ] = [$compare, $offset]; |
|
155 | + } |
|
156 | + return [$query_args, $where_params]; |
|
157 | + } |
|
158 | + |
|
159 | + |
|
160 | + /** |
|
161 | + * This sets up the "allowed" args, and translates the GraphQL-friendly keys to model |
|
162 | + * friendly keys. |
|
163 | + * |
|
164 | + * @param array $where_args |
|
165 | + * @param array $arg_mapping |
|
166 | + * @param array $id_fields The fields to convert from global IDs to DB IDs. |
|
167 | + * @return array |
|
168 | + */ |
|
169 | + protected function sanitizeWhereArgsForInputFields(array $where_args, array $arg_mapping, array $id_fields = []): array |
|
170 | + { |
|
171 | + $query_args = $this->getUtilities()->sanitizeWhereArgs($where_args, $arg_mapping, $id_fields); |
|
172 | + return ! empty($query_args) && is_array($query_args) |
|
173 | + ? $query_args |
|
174 | + : []; |
|
175 | + } |
|
176 | + |
|
177 | + |
|
178 | + /** |
|
179 | + * This returns the sanitized "search" keywords from where_args |
|
180 | + * |
|
181 | + * @param array $where_args |
|
182 | + * @return string |
|
183 | + */ |
|
184 | + protected function getSearchKeywords(array $where_args): string |
|
185 | + { |
|
186 | + $search = ''; |
|
187 | + if (! empty($where_args['search'])) { |
|
188 | + $search = sanitize_text_field($where_args['search']); |
|
189 | + } |
|
190 | + return esc_sql($search); |
|
191 | + } |
|
192 | 192 | } |
@@ -17,58 +17,58 @@ |
||
17 | 17 | */ |
18 | 18 | class ConnectionsManager implements GQLManagerInterface |
19 | 19 | { |
20 | - const MAX_AMOUNT_REQUESTED = 250; |
|
20 | + const MAX_AMOUNT_REQUESTED = 250; |
|
21 | 21 | |
22 | - const MAX_QUERY_AMOUNT = 250; |
|
22 | + const MAX_QUERY_AMOUNT = 250; |
|
23 | 23 | |
24 | 24 | |
25 | - /** |
|
26 | - * @var ConnectionCollection|ConnectionInterface[] $connections |
|
27 | - */ |
|
28 | - private $connections; |
|
25 | + /** |
|
26 | + * @var ConnectionCollection|ConnectionInterface[] $connections |
|
27 | + */ |
|
28 | + private $connections; |
|
29 | 29 | |
30 | 30 | |
31 | - /** |
|
32 | - * ConnectionsManager constructor. |
|
33 | - * |
|
34 | - * @param ConnectionCollection|ConnectionInterface[] $connections |
|
35 | - */ |
|
36 | - public function __construct(ConnectionCollection $connections) |
|
37 | - { |
|
38 | - $this->connections = $connections; |
|
39 | - } |
|
31 | + /** |
|
32 | + * ConnectionsManager constructor. |
|
33 | + * |
|
34 | + * @param ConnectionCollection|ConnectionInterface[] $connections |
|
35 | + */ |
|
36 | + public function __construct(ConnectionCollection $connections) |
|
37 | + { |
|
38 | + $this->connections = $connections; |
|
39 | + } |
|
40 | 40 | |
41 | 41 | |
42 | - /** |
|
43 | - * @throws CollectionDetailsException |
|
44 | - * @throws CollectionLoaderException |
|
45 | - * @since $VID:$ |
|
46 | - */ |
|
47 | - public function init() |
|
48 | - { |
|
49 | - $this->connections->loadConnections(); |
|
50 | - add_action('graphql_register_types', [$this, 'registerConnections'], 20); |
|
51 | - add_filter('graphql_connection_amount_requested', [$this, 'setMaxAmountRequested']); |
|
52 | - add_filter('graphql_connection_max_query_amount', [$this, 'setMaxQueryAmount']); |
|
53 | - } |
|
42 | + /** |
|
43 | + * @throws CollectionDetailsException |
|
44 | + * @throws CollectionLoaderException |
|
45 | + * @since $VID:$ |
|
46 | + */ |
|
47 | + public function init() |
|
48 | + { |
|
49 | + $this->connections->loadConnections(); |
|
50 | + add_action('graphql_register_types', [$this, 'registerConnections'], 20); |
|
51 | + add_filter('graphql_connection_amount_requested', [$this, 'setMaxAmountRequested']); |
|
52 | + add_filter('graphql_connection_max_query_amount', [$this, 'setMaxQueryAmount']); |
|
53 | + } |
|
54 | 54 | |
55 | 55 | |
56 | - public function registerConnections() |
|
57 | - { |
|
58 | - // loop through the collection of types and register their fields |
|
59 | - foreach ($this->connections as $connection) { |
|
60 | - register_graphql_connection($connection->config()); |
|
61 | - } |
|
62 | - } |
|
56 | + public function registerConnections() |
|
57 | + { |
|
58 | + // loop through the collection of types and register their fields |
|
59 | + foreach ($this->connections as $connection) { |
|
60 | + register_graphql_connection($connection->config()); |
|
61 | + } |
|
62 | + } |
|
63 | 63 | |
64 | 64 | |
65 | - public function setMaxAmountRequested(): int |
|
66 | - { |
|
67 | - return ConnectionsManager::MAX_AMOUNT_REQUESTED; |
|
68 | - } |
|
65 | + public function setMaxAmountRequested(): int |
|
66 | + { |
|
67 | + return ConnectionsManager::MAX_AMOUNT_REQUESTED; |
|
68 | + } |
|
69 | 69 | |
70 | - public function setMaxQueryAmount(): int |
|
71 | - { |
|
72 | - return ConnectionsManager::MAX_QUERY_AMOUNT; |
|
73 | - } |
|
70 | + public function setMaxQueryAmount(): int |
|
71 | + { |
|
72 | + return ConnectionsManager::MAX_QUERY_AMOUNT; |
|
73 | + } |
|
74 | 74 | } |