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