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