PluginProbe ʕ •ᴥ•ʔ
Pods – Custom Content Types and Fields / 3.2.5
Pods – Custom Content Types and Fields v3.2.5
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 / REST / V1 / Validator / Base.php
pods / src / Pods / REST / V1 / Validator Last commit date
Base.php 2 years ago
Base.php
217 lines
1 <?php
2
3 namespace Pods\REST\V1\Validator;
4
5 use Pods\REST\Interfaces\Validator_Interface;
6
7 /**
8 * Class Base
9 *
10 * @credit The Events Calendar team - https://github.com/the-events-calendar/tribe-common
11 *
12 * @since 2.8.0
13 */
14 class Base implements Validator_Interface {
15 /**
16 * Whether the value corresponds to an existing post ID or not.
17 *
18 * @since 2.8.0
19 *
20 * @param mixed $value
21 *
22 * @return bool
23 */
24 public function is_post_id( $value ) {
25 return is_numeric( $value ) && get_post( (int) $value );
26 }
27
28 /**
29 * Determine whether a Pod / Item ID is valid.
30 *
31 * @since 2.8.11
32 *
33 * @param string $pod The pod name.
34 * @param int|string $id_or_slug The Item ID or slug.
35 *
36 * @return bool Whether the Pod / Item ID is valid.
37 */
38 public function is_pod_item_id_or_slug_valid( $pod, $id_or_slug ) {
39 $pod = pods_get_instance( $pod, $id_or_slug );
40
41 return $pod && ! is_wp_error( $pod ) && $pod->valid() && $pod->exists();
42 }
43
44 /**
45 * Determine whether a Pod / Item ID is valid.
46 *
47 * @since 2.8.11
48 *
49 * @param string $pod The pod name.
50 * @param int|string $id The item ID.
51 *
52 * @return bool Whether the Pod / Item ID is valid.
53 */
54 public function is_pod_item_id_valid( $pod, $id ) {
55 return is_numeric( $id ) && $this->is_pod_id_or_slug_valid( $pod, (int) $id );
56 }
57
58 /**
59 * Determine whether a Pod / Item slug is valid.
60 *
61 * @since 2.8.11
62 *
63 * @param string $pod The pod name.
64 * @param int|string $slug The Item slug.
65 *
66 * @return bool Whether the Pod / Item slug is valid.
67 */
68 public function is_pod_item_slug_valid( $pod, $slug ) {
69 return $this->is_pod_id_or_slug_valid( $pod, $slug );
70 }
71
72 /**
73 * Whether the value corresponds to an existing Pod slug or not.
74 *
75 * @since 2.8.0
76 *
77 * @param mixed $value
78 *
79 * @return bool
80 */
81 public function is_pod_slug( $value ) {
82 return null !== get_page_by_path( $value, OBJECT, '_pods_pod' );
83 }
84
85 /**
86 * Whether the value corresponds to an existing Group slug or not.
87 *
88 * @since 2.8.0
89 *
90 * @param mixed $value
91 *
92 * @return bool
93 */
94 public function is_group_slug( $value ) {
95 return null !== get_page_by_path( $value, OBJECT, '_pods_group' );
96 }
97
98 /**
99 * Whether the value corresponds to an existing Field slug or not.
100 *
101 * @since 2.8.0
102 *
103 * @param mixed $value
104 *
105 * @return bool
106 */
107 public function is_field_slug( $value ) {
108 return null !== get_page_by_path( $value, OBJECT, '_pods_field' );
109 }
110
111 /**
112 * Whether the value corresponds to an existing Pod ID or not.
113 *
114 * @since 2.8.0
115 *
116 * @param mixed $value
117 *
118 * @return bool
119 */
120 public function is_pod_id( $value ) {
121 if ( ! is_numeric( $value ) ) {
122 return false;
123 }
124
125 return '_pods_pod' === get_post_type( (int) $value );
126 }
127
128 /**
129 * Whether the value corresponds to an existing Group ID or not.
130 *
131 * @since 2.8.0
132 *
133 * @param mixed $value
134 *
135 * @return bool
136 */
137 public function is_group_id( $value ) {
138 if ( ! is_numeric( $value ) ) {
139 return false;
140 }
141
142 return '_pods_group' === get_post_type( (int) $value );
143 }
144
145 /**
146 * Whether the value corresponds to an existing Field ID or not.
147 *
148 * @since 2.8.0
149 *
150 * @param mixed $value
151 *
152 * @return bool
153 */
154 public function is_field_id( $value ) {
155 if ( ! is_numeric( $value ) ) {
156 return false;
157 }
158
159 return '_pods_field' === get_post_type( (int) $value );
160 }
161
162 /**
163 * Determine whether the value is valid JSON.
164 *
165 * @since 2.8.0
166 *
167 * @param mixed $value The value.
168 *
169 * @return bool Whether the value is valid JSON.
170 */
171 public function is_json( $value ) {
172 return is_string( $value ) && ! is_scalar( json_decode( $value ) );
173 }
174
175 /**
176 * Handle other potential methods automatically if possible.
177 *
178 * @since 2.8.11
179 *
180 * @param string $name The method name.
181 * @param array $arguments The arguments provided to the method.
182 *
183 * @return mixed The method return response.
184 */
185 #[\ReturnTypeWillChange]
186 public function __call( $name, array $arguments ) {
187 $mapped = [
188 'is_pod_item_id_valid_for_pod_' => 'is_pod_item_id_valid',
189 'is_pod_item_slug_valid_for_pod_' => 'is_pod_item_slug_valid',
190 'is_pod_item_id_or_slug_valid_for_pod_' => 'is_pod_item_id_or_slug_valid',
191 ];
192
193 foreach ( $mapped as $prefix_method => $mapped_method ) {
194 if ( 0 === strpos( $name, $prefix_method ) ) {
195 $pod_name = str_replace( $prefix_method, '', $name );
196
197 array_unshift( $pod_name, $arguments );
198
199 return $this->{$mapped_method}( ...$arguments );
200 }
201 }
202 }
203
204 /**
205 * Determine whether the value is not null.
206 *
207 * @since 3.0
208 *
209 * @param mixed $value
210 *
211 * @return bool
212 */
213 public function is_not_null( $value ) {
214 return null !== $value;
215 }
216 }
217