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