Storage
4 months ago
Block.php
4 months ago
Block_Collection.php
4 months ago
Block_Field.php
4 months ago
Field.php
4 months ago
Group.php
4 months ago
Legacy_Object.php
4 months ago
Object_Field.php
4 months ago
Page.php
4 months ago
Pod.php
4 months ago
Storage.php
4 months ago
Store.php
4 months ago
Template.php
4 months ago
Pod.php
381 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Pods\Whatsit; |
| 4 | |
| 5 | // Don't load directly. |
| 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 7 | die( '-1' ); |
| 8 | } |
| 9 | |
| 10 | use Pods\Static_Cache; |
| 11 | use Pods\Whatsit; |
| 12 | |
| 13 | /** |
| 14 | * Pod class. |
| 15 | * |
| 16 | * @since 2.8.0 |
| 17 | */ |
| 18 | class Pod extends Whatsit { |
| 19 | |
| 20 | /** |
| 21 | * {@inheritdoc} |
| 22 | */ |
| 23 | protected static $type = 'pod'; |
| 24 | |
| 25 | /** |
| 26 | * Get the storage used for the Pod data (meta, table, etc). |
| 27 | * |
| 28 | * @since 2.8.1 |
| 29 | * |
| 30 | * @param boolean $strict Whether to only get the argument, otherwise the default will be returned. |
| 31 | * |
| 32 | * @return string The storage used for the Pod data (meta, table, etc). |
| 33 | */ |
| 34 | public function get_storage( $strict = false ) { |
| 35 | $storage = parent::get_arg( 'storage' ); |
| 36 | |
| 37 | if ( ! $strict && empty( $storage ) ) { |
| 38 | $storage = $this->get_default_storage(); |
| 39 | } |
| 40 | |
| 41 | return $storage; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get the default storage used for the Pod data (meta, table, etc) based on the current Pod type. |
| 46 | * |
| 47 | * @since 2.9.16 |
| 48 | * |
| 49 | * @return string The default storage used for the Pod data (meta, table, etc). |
| 50 | */ |
| 51 | public function get_default_storage() { |
| 52 | $type = $this->get_type(); |
| 53 | $storage = 'none'; |
| 54 | |
| 55 | if ( in_array( $type, [ 'post_type', 'taxonomy', 'user', 'comment', 'media' ], true ) ) { |
| 56 | $storage = 'meta'; |
| 57 | } elseif ( in_array( $type, [ 'pod', 'table' ], true ) ) { |
| 58 | $storage = 'table'; |
| 59 | } elseif ( 'settings' === $type ) { |
| 60 | $storage = 'option'; |
| 61 | } |
| 62 | |
| 63 | return $storage; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * {@inheritdoc} |
| 68 | */ |
| 69 | public function get_args() { |
| 70 | $args = parent::get_args(); |
| 71 | |
| 72 | $args['storage'] = $this->get_arg( 'storage' ); |
| 73 | |
| 74 | // Pods generally have no parent, group, or order. |
| 75 | unset( $args['parent'], $args['group'], $args['weight'] ); |
| 76 | |
| 77 | return $args; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * {@inheritdoc} |
| 82 | */ |
| 83 | public function export( array $args = [] ) { |
| 84 | $exported = parent::export( $args ); |
| 85 | |
| 86 | // Always make sure we have a storage arg set. |
| 87 | $exported['storage'] = $this->get_storage(); |
| 88 | |
| 89 | return $exported; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * {@inheritdoc} |
| 94 | */ |
| 95 | public function get_arg( $arg, $default = null, $strict = false, $raw = false ) { |
| 96 | if ( $raw ) { |
| 97 | return parent::get_arg( $arg, $default, $strict, $raw ); |
| 98 | } |
| 99 | |
| 100 | if ( 'storage' === $arg ) { |
| 101 | return $this->get_storage(); |
| 102 | } |
| 103 | |
| 104 | if ( 'type' === $arg && null === $default ) { |
| 105 | $default = 'post_type'; |
| 106 | } |
| 107 | |
| 108 | $value = parent::get_arg( $arg, $default, $strict ); |
| 109 | |
| 110 | // Better handle object for extended objects. |
| 111 | if ( 'object' === $arg && 'table' !== $this->get_type() && ( did_action( 'init' ) || doing_action( 'init' ) ) ) { |
| 112 | if ( $this->is_extended() ) { |
| 113 | return $this->get_name(); |
| 114 | } |
| 115 | |
| 116 | return ''; |
| 117 | } |
| 118 | |
| 119 | return $value; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * {@inheritdoc} |
| 124 | */ |
| 125 | public function get_object_fields() { |
| 126 | if ( [] === $this->_object_fields ) { |
| 127 | return []; |
| 128 | } |
| 129 | |
| 130 | $api = pods_api(); |
| 131 | |
| 132 | $object_fields = $api->get_wp_object_fields( $this->get_type(), $this ); |
| 133 | |
| 134 | $object_collection = Store::get_instance(); |
| 135 | |
| 136 | $objects = []; |
| 137 | |
| 138 | foreach ( $object_fields as $object_field ) { |
| 139 | $object_field['object_type'] = 'object-field'; |
| 140 | $object_field['object_storage_type'] = 'collection'; |
| 141 | $object_field['parent'] = $this->get_id(); |
| 142 | |
| 143 | $object = $object_collection->get_object( $object_field ); |
| 144 | |
| 145 | if ( $object ) { |
| 146 | $objects[ $object->get_name() ] = $object; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | $this->_object_fields = $objects; |
| 151 | |
| 152 | return $objects; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * {@inheritdoc} |
| 157 | */ |
| 158 | public function count_object_fields() { |
| 159 | if ( [] === $this->_object_fields ) { |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | $api = pods_api(); |
| 164 | |
| 165 | $object_fields = $api->get_wp_object_fields( $this->get_type(), $this ); |
| 166 | |
| 167 | return count( $object_fields ); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * {@inheritdoc} |
| 172 | */ |
| 173 | public function get_table_info() { |
| 174 | if ( null !== $this->_table_info ) { |
| 175 | return $this->_table_info; |
| 176 | } |
| 177 | |
| 178 | $api = pods_api(); |
| 179 | |
| 180 | $table_info = $api->get_table_info( $this->get_type(), $this->get_name(), null, $this ); |
| 181 | |
| 182 | if ( empty( $table_info ) ) { |
| 183 | $table_info = []; |
| 184 | } |
| 185 | |
| 186 | $this->_table_info = $table_info; |
| 187 | |
| 188 | return $table_info; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Determine whether this is a table-based Pod. |
| 193 | * |
| 194 | * @since 2.9.0 |
| 195 | * |
| 196 | * @return bool Whether this is a table-based Pod. |
| 197 | */ |
| 198 | public function is_table_based() { |
| 199 | return 'table' === $this->get_storage() || 'pod' === $this->get_type(); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Determine whether this is a meta-based Pod. |
| 204 | * |
| 205 | * @since 2.9.0 |
| 206 | * |
| 207 | * @return bool Whether this is a meta-based Pod. |
| 208 | */ |
| 209 | public function is_meta_based() { |
| 210 | return 'meta' === $this->get_storage(); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Determine whether the Pod is an extending an existing content type. |
| 215 | * |
| 216 | * @since 2.8.4 |
| 217 | * |
| 218 | * @return bool Whether the Pod is an extending an existing content type. |
| 219 | */ |
| 220 | public function is_extended() { |
| 221 | $type = $this->get_type(); |
| 222 | $name = $this->get_name(); |
| 223 | |
| 224 | // Simple content type checks. |
| 225 | if ( 'user' === $type ) { |
| 226 | return true; |
| 227 | } elseif ( 'media' === $type ) { |
| 228 | return true; |
| 229 | } elseif ( 'comment' === $type ) { |
| 230 | return true; |
| 231 | } elseif ( $type === $name ) { |
| 232 | return true; |
| 233 | } elseif ( 'post_type' !== $type && 'taxonomy' !== $type ) { |
| 234 | return false; |
| 235 | } |
| 236 | |
| 237 | $cached_var = ''; |
| 238 | |
| 239 | // Simple checks for post types. |
| 240 | if ( 'post_type' === $type ) { |
| 241 | if ( 'post' === $name || 'page' === $name ) { |
| 242 | return true; |
| 243 | } |
| 244 | |
| 245 | $cached_var = 'existing_post_types_cached'; |
| 246 | } |
| 247 | |
| 248 | // Simple checks for taxonomies. |
| 249 | if ( 'taxonomy' === $type ) { |
| 250 | if ( 'category' === $name || 'post_tag' === $name ) { |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | $cached_var = 'existing_taxonomies_cached'; |
| 255 | } |
| 256 | |
| 257 | $existing_cached = pods_init()->refresh_existing_content_types_cache(); |
| 258 | |
| 259 | return ! empty( $existing_cached[ $cached_var ] ) && array_key_exists( $this->get_name(), $existing_cached[ $cached_var ] ); |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * Count the total rows for the pod. |
| 264 | * |
| 265 | * @since 2.8.9 |
| 266 | * |
| 267 | * @return int The total rows for the Pod. |
| 268 | */ |
| 269 | public function count_rows() { |
| 270 | $pod = pods( $this ); |
| 271 | |
| 272 | if ( ! $pod || ! $pod->valid() ) { |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | return $pod->total_all_rows(); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Count the total row meta for the pod. |
| 281 | * |
| 282 | * @since 2.8.9 |
| 283 | * |
| 284 | * @return int The total row meta for the Pod. |
| 285 | */ |
| 286 | public function count_row_meta() { |
| 287 | if ( 'meta' !== $this->get_storage() && ! in_array( $this->get_type(), [ 'post_type', 'taxonomy', 'user', 'comment' ], true ) ) { |
| 288 | return 0; |
| 289 | } |
| 290 | |
| 291 | $pod = pods( $this ); |
| 292 | |
| 293 | if ( ! $pod || ! $pod->valid() ) { |
| 294 | return 0; |
| 295 | } |
| 296 | |
| 297 | $field_id = $this->get_arg( 'field_id' ); |
| 298 | $meta_field_id = $this->get_arg( 'meta_field_id' ); |
| 299 | $meta_table = $this->get_arg( 'meta_table' ); |
| 300 | |
| 301 | if ( empty( $meta_field_id ) || empty( $meta_table ) ) { |
| 302 | return 0; |
| 303 | } |
| 304 | |
| 305 | // Make a simple request so we can perform a total_found() SQL request. |
| 306 | $params = [ |
| 307 | 'distinct' => false, |
| 308 | 'select' => 'meta.' . $meta_field_id, |
| 309 | 'join' => "LEFT JOIN {$meta_table} AS meta ON meta.{$meta_field_id} = t.{$field_id}", |
| 310 | 'limit' => 1, |
| 311 | ]; |
| 312 | |
| 313 | $pod->find( $params ); |
| 314 | |
| 315 | return $pod->total_found(); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Count the total wp_podsrel rows for the pod. |
| 320 | * |
| 321 | * @since 2.8.9 |
| 322 | * |
| 323 | * @return int The total wp_podsrel rows for the pod. |
| 324 | */ |
| 325 | public function count_podsrel_rows() { |
| 326 | if ( pods_tableless() ) { |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | $pod = pods( $this ); |
| 331 | |
| 332 | if ( ! $pod || ! $pod->valid() ) { |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | $fields = $this->get_fields(); |
| 337 | |
| 338 | if ( empty( $fields ) ) { |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | $pod_id = (int) $this->get_id(); |
| 343 | $field_ids = wp_list_pluck( $fields, 'id' ); |
| 344 | $field_ids = array_map( 'absint', $field_ids ); |
| 345 | $field_ids = array_filter( $field_ids ); |
| 346 | |
| 347 | if ( empty( $field_ids ) ) { |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | $field_ids = implode( ', ', $field_ids ); |
| 352 | |
| 353 | $data = pods_data(); |
| 354 | |
| 355 | global $wpdb; |
| 356 | |
| 357 | // Make a simple request so we can perform a total_found() SQL request. |
| 358 | $params = [ |
| 359 | 'distinct' => false, |
| 360 | 'select' => 't.id', |
| 361 | 'table' => $wpdb->prefix . 'podsrel', |
| 362 | 'where' => " |
| 363 | ( |
| 364 | t.pod_id = {$pod_id} |
| 365 | AND t.field_id IN ( {$field_ids} ) |
| 366 | ) |
| 367 | OR ( |
| 368 | t.related_pod_id = {$pod_id} |
| 369 | AND t.related_field_id IN ( {$field_ids} ) |
| 370 | ) |
| 371 | ", |
| 372 | 'limit' => 1, |
| 373 | ]; |
| 374 | |
| 375 | $data->select( $params ); |
| 376 | |
| 377 | return $data->total_found(); |
| 378 | } |
| 379 | |
| 380 | } |
| 381 |