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