PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.2.2
Pods – Custom Content Types and Fields v3.2.2
trunk 1.14.8 2.7.31.3 2.8.23.3 2.9.19.3 3.0.10.3 3.1.4.1 3.2.0 3.2.1 3.2.1.1 3.2.2 3.2.4 3.2.5 3.2.6 3.2.7 3.2.7.1 3.2.8 3.2.8.1 3.2.8.2 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 3.3.9
pods / src / Pods / Integrations / WPGraphQL / Connection_Resolver / Custom_Simple.php
pods / src / Pods / Integrations / WPGraphQL / Connection_Resolver Last commit date
Custom_Simple.php 3 years ago Pod.php 3 years ago Pod_Type.php 3 years ago
Custom_Simple.php
219 lines
1 <?php
2
3 namespace Pods\Integrations\WPGraphQL\Connection_Resolver;
4
5 use Exception;
6 use GraphQL\Type\Definition\ResolveInfo;
7 use WPGraphQL\AppContext;
8 use WPGraphQL\Data\Connection\AbstractConnectionResolver;
9
10 /**
11 * GraphQL Connection resolver for Custom Simple Relationships.
12 *
13 * @since 2.9.0
14 */
15 class Custom_Simple extends AbstractConnectionResolver {
16
17 /**
18 * The custom data provided from the relationship field.
19 *
20 * @since 2.9.0
21 *
22 * @var array
23 */
24 protected $custom_data;
25
26 /**
27 * Pod constructor.
28 *
29 * @since 2.9.0
30 *
31 * @param mixed $source The source passed down from the resolve tree.
32 * @param array $args List of arguments input in the field as part of the GraphQL query.
33 * @param AppContext $context Object containing app context that gets passed down the resolve tree.
34 * @param ResolveInfo $info Info about fields passed down the resolve tree.
35 * @param array $custom_data The custom data provided from the relationship field.
36 *
37 * @throws Exception
38 */
39 public function __construct( $source, array $args, AppContext $context, ResolveInfo $info, array $custom_data ) {
40 $this->custom_data = $custom_data;
41
42 /**
43 * Call the parent construct to setup class data
44 */
45 parent::__construct( $source, $args, $context, $info );
46 }
47
48 /**
49 * Determines whether the query should execute at all. It's possible that in some
50 * situations we may want to prevent the underlying query from executing at all.
51 *
52 * In those cases, this would be set to false.
53 *
54 * @since 2.9.0
55 *
56 * @return bool Whether the query should execute.
57 */
58 public function should_execute() {
59 return true;
60 }
61
62 /**
63 * Provide the loader name.
64 *
65 * @since 2.9.0
66 *
67 * @return string The loader name.
68 */
69 public function get_loader_name() {
70 return 'pods_custom_simple';
71 }
72
73 /**
74 * Converts the args that were provided to the connection into args that can be used with the data.
75 *
76 * @since 2.9.0
77 *
78 * @return array The query arguments to use.
79 *
80 * @throws Exception
81 */
82 public function get_query_args() {
83 $query_args = [];
84
85 // Handle preparing the values to filter by.
86 if ( ! empty( $this->args['values'] ) ) {
87 $values = $this->prepare_input_fields( (array) $this->args['values'] );
88
89 if ( $values ) {
90 $query_args['values'] = $values;
91 }
92 }
93
94 // Handle preparing the labels to filter by.
95 if ( ! empty( $this->args['labels'] ) ) {
96 $labels = $this->prepare_input_fields( (array) $this->args['labels'] );
97
98 if ( $labels ) {
99 $query_args['labels'] = $labels;
100 }
101 }
102
103 return $query_args;
104 }
105
106 /**
107 * Get the list of custom data filtered as requested.
108 *
109 * @since 2.9.0
110 *
111 * @return array List of custom data.
112 */
113 public function get_query() {
114 // The data is not set.
115 if ( empty( $this->custom_data ) ) {
116 return [];
117 }
118
119 $matching_data = $this->custom_data;
120
121 // Remove data items from matches if they aren't in the provided values to filter by.
122 if ( isset( $this->query_args['values'] ) ) {
123 foreach ( $matching_data as $value => $label ) {
124 if ( in_array( (string) $value, $this->query_args['values'], true ) ) {
125 continue;
126 }
127
128 unset( $matching_data[ $value ] );
129 }
130 }
131
132 // Remove data items from matches if they aren't in the provided labels to filter by.
133 if ( isset( $this->query_args['labels'] ) ) {
134 foreach ( $matching_data as $value => $label ) {
135 if ( in_array( (string) $label, $this->query_args['labels'], true ) ) {
136 continue;
137 }
138
139 unset( $matching_data[ $value ] );
140 }
141 }
142
143 return $matching_data;
144 }
145
146 /**
147 * Returns an array of ids from the query being executed.
148 *
149 * @since 2.9.0
150 *
151 * @return array List of IDs from the query.
152 */
153 public function get_ids() {
154 if ( ! $this->query ) {
155 return [];
156 }
157
158 // Get the IDs from the list of keys.
159 return array_keys( $this->query );
160 }
161
162 /**
163 * Determine whether or not the the offset is valid, i.e the user corresponding to the offset
164 * exists. Offset is equivalent to user_id. So this function is equivalent to checking if the
165 * user with the given ID exists.
166 *
167 * @since 2.9.0
168 *
169 * @param int $offset The ID of the node used as the offset in the cursor.
170 *
171 * @return bool Whether the offset is valid.
172 */
173 public function is_valid_offset( $offset ) {
174 // The data is not set.
175 if ( empty( $this->custom_data ) ) {
176 return false;
177 }
178
179 return isset( $this->custom_data[ $offset ] );
180 }
181
182 /**
183 * Prepare a list of input fields.
184 *
185 * @since 2.9.0
186 *
187 * @param array $input_fields The list of input fields to prepare.
188 *
189 * @return array The list of prepared input fields.
190 */
191 private function prepare_input_fields( array $input_fields ) {
192 $prepared_input_fields = [];
193
194 // Set the disallowed pattern to prevent against.
195 $disallowed_pattern = '/[^a-zA-Z0-9_\-]/';
196
197 foreach ( $input_fields as $input_field ) {
198 // If the input field is already an array, make it a string and sanitize it.
199 if ( is_array( $input_field ) ) {
200 $input_field = implode( ',', $input_field );
201 }
202
203 $input_field = (string) $input_field;
204
205 // Do not use any input fields that provide disallowed characters.
206 if ( false !== preg_match( $disallowed_pattern, $input_field ) ) {
207 continue;
208 }
209
210 $prepared_input_fields = explode( ',', $input_field );
211 }
212
213 $prepared_input_fields = array_merge( ...$prepared_input_fields );
214 $prepared_input_fields = array_values( array_unique( array_filter( $prepared_input_fields ) ) );
215
216 return $prepared_input_fields;
217 }
218 }
219