Storage
3 weeks ago
Block.php
3 weeks ago
Block_Collection.php
3 weeks ago
Block_Field.php
3 weeks ago
Field.php
3 weeks ago
Group.php
3 weeks ago
Legacy_Object.php
3 weeks ago
Object_Field.php
3 weeks ago
Page.php
3 weeks ago
Pod.php
3 weeks ago
Storage.php
3 weeks ago
Store.php
3 weeks ago
Template.php
3 weeks ago
Pod.php
296 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 | * @return string The storage used for the Pod data (meta, table, etc). |
| 26 | */ |
| 27 | public function get_storage() { |
| 28 | return $this->get_arg( 'storage' ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * {@inheritdoc} |
| 33 | */ |
| 34 | public function get_args() { |
| 35 | $args = parent::get_args(); |
| 36 | |
| 37 | // Pods generally have no parent, group, or order. |
| 38 | unset( $args['parent'], $args['group'], $args['weight'] ); |
| 39 | |
| 40 | return $args; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * {@inheritdoc} |
| 45 | */ |
| 46 | public function get_arg( $arg, $default = null, $strict = false ) { |
| 47 | $value = parent::get_arg( $arg, $default, $strict ); |
| 48 | |
| 49 | // Better handle object for extended objects. |
| 50 | if ( 'object' === $arg && 'table' !== $this->get_type() && ( did_action( 'init' ) || doing_action( 'init' ) ) ) { |
| 51 | if ( $this->is_extended() ) { |
| 52 | return $this->get_name(); |
| 53 | } |
| 54 | |
| 55 | return ''; |
| 56 | } |
| 57 | |
| 58 | return $value; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * {@inheritdoc} |
| 63 | */ |
| 64 | public function get_object_fields() { |
| 65 | if ( [] === $this->_object_fields ) { |
| 66 | return []; |
| 67 | } |
| 68 | |
| 69 | $api = pods_api(); |
| 70 | |
| 71 | $object_fields = $api->get_wp_object_fields( $this->get_type(), $this ); |
| 72 | |
| 73 | $object_collection = Store::get_instance(); |
| 74 | |
| 75 | $objects = []; |
| 76 | |
| 77 | foreach ( $object_fields as $object_field ) { |
| 78 | $object_field['object_type'] = 'object-field'; |
| 79 | $object_field['object_storage_type'] = 'collection'; |
| 80 | $object_field['parent'] = $this->get_id(); |
| 81 | |
| 82 | $object = $object_collection->get_object( $object_field ); |
| 83 | |
| 84 | if ( $object ) { |
| 85 | $objects[ $object->get_name() ] = $object; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | $this->_object_fields = $objects; |
| 90 | |
| 91 | return $objects; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * {@inheritdoc} |
| 96 | */ |
| 97 | public function count_object_fields() { |
| 98 | if ( [] === $this->_object_fields ) { |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | $api = pods_api(); |
| 103 | |
| 104 | $object_fields = $api->get_wp_object_fields( $this->get_type(), $this ); |
| 105 | |
| 106 | return count( $object_fields ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * {@inheritdoc} |
| 111 | */ |
| 112 | public function get_table_info() { |
| 113 | if ( null !== $this->_table_info ) { |
| 114 | return $this->_table_info; |
| 115 | } |
| 116 | |
| 117 | $api = pods_api(); |
| 118 | |
| 119 | $table_info = $api->get_table_info( $this->get_type(), $this->get_name(), null, $this ); |
| 120 | |
| 121 | if ( empty( $table_info ) ) { |
| 122 | $table_info = []; |
| 123 | } |
| 124 | |
| 125 | $this->_table_info = $table_info; |
| 126 | |
| 127 | return $table_info; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Determine whether the Pod is an extending an existing content type. |
| 132 | * |
| 133 | * @since 2.8.4 |
| 134 | * |
| 135 | * @return bool Whether the Pod is an extending an existing content type. |
| 136 | */ |
| 137 | public function is_extended() { |
| 138 | $type = $this->get_type(); |
| 139 | $name = $this->get_name(); |
| 140 | |
| 141 | // Simple content type checks. |
| 142 | if ( 'user' === $type ) { |
| 143 | return true; |
| 144 | } elseif ( 'media' === $type ) { |
| 145 | return true; |
| 146 | } elseif ( 'comment' === $type ) { |
| 147 | return true; |
| 148 | } elseif ( 'post_type' !== $type && 'taxonomy' !== $type ) { |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | $cached_var = ''; |
| 153 | |
| 154 | // Simple checks for post types. |
| 155 | if ( 'post_type' === $type ) { |
| 156 | if ( 'post' === $name || 'page' === $name ) { |
| 157 | return true; |
| 158 | } |
| 159 | |
| 160 | $cached_var = 'existing_post_types_cached'; |
| 161 | } |
| 162 | |
| 163 | // Simple checks for taxonomies. |
| 164 | if ( 'taxonomy' === $type ) { |
| 165 | if ( 'category' === $name || 'post_tag' === $name ) { |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | $cached_var = 'existing_taxonomies_cached'; |
| 170 | } |
| 171 | |
| 172 | $existing_cached = pods_init()->refresh_existing_content_types_cache(); |
| 173 | |
| 174 | return ! empty( $existing_cached[ $cached_var ] ) && array_key_exists( $this->get_name(), $existing_cached[ $cached_var ] ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Count the total rows for the pod. |
| 179 | * |
| 180 | * @since 2.8.9 |
| 181 | * |
| 182 | * @return int The total rows for the Pod. |
| 183 | */ |
| 184 | public function count_rows() { |
| 185 | $pod = pods( $this ); |
| 186 | |
| 187 | if ( ! $pod || ! $pod->valid() ) { |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | return $pod->total_all_rows(); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Count the total row meta for the pod. |
| 196 | * |
| 197 | * @since 2.8.9 |
| 198 | * |
| 199 | * @return int The total row meta for the Pod. |
| 200 | */ |
| 201 | public function count_row_meta() { |
| 202 | if ( 'meta' !== $this->get_storage() && ! in_array( $this->get_type(), [ 'post_type', 'taxonomy', 'user', 'comment' ], true ) ) { |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | $pod = pods( $this ); |
| 207 | |
| 208 | if ( ! $pod || ! $pod->valid() ) { |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | $field_id = $this->get_arg( 'field_id' ); |
| 213 | $meta_field_id = $this->get_arg( 'meta_field_id' ); |
| 214 | $meta_table = $this->get_arg( 'meta_table' ); |
| 215 | |
| 216 | if ( empty( $meta_field_id ) || empty( $meta_table ) ) { |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | // Make a simple request so we can perform a total_found() SQL request. |
| 221 | $params = [ |
| 222 | 'distinct' => false, |
| 223 | 'select' => 'meta.' . $meta_field_id, |
| 224 | 'join' => "LEFT JOIN {$meta_table} AS meta ON meta.{$meta_field_id} = t.{$field_id}", |
| 225 | 'limit' => 1, |
| 226 | ]; |
| 227 | |
| 228 | $pod->find( $params ); |
| 229 | |
| 230 | return $pod->total_found(); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Count the total wp_podsrel rows for the pod. |
| 235 | * |
| 236 | * @since 2.8.9 |
| 237 | * |
| 238 | * @return int The total wp_podsrel rows for the pod. |
| 239 | */ |
| 240 | public function count_podsrel_rows() { |
| 241 | if ( pods_tableless() ) { |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | $pod = pods( $this ); |
| 246 | |
| 247 | if ( ! $pod || ! $pod->valid() ) { |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | $fields = $this->get_fields(); |
| 252 | |
| 253 | if ( empty( $fields ) ) { |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | $pod_id = (int) $this->get_id(); |
| 258 | $field_ids = wp_list_pluck( $fields, 'id' ); |
| 259 | $field_ids = array_map( 'absint', $field_ids ); |
| 260 | $field_ids = array_filter( $field_ids ); |
| 261 | |
| 262 | if ( empty( $field_ids ) ) { |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | $field_ids = implode( ', ', $field_ids ); |
| 267 | |
| 268 | $data = pods_data(); |
| 269 | |
| 270 | global $wpdb; |
| 271 | |
| 272 | // Make a simple request so we can perform a total_found() SQL request. |
| 273 | $params = [ |
| 274 | 'distinct' => false, |
| 275 | 'select' => 't.id', |
| 276 | 'table' => $wpdb->prefix . 'podsrel', |
| 277 | 'where' => " |
| 278 | ( |
| 279 | t.pod_id = {$pod_id} |
| 280 | AND t.field_id IN ( {$field_ids} ) |
| 281 | ) |
| 282 | OR ( |
| 283 | t.related_pod_id = {$pod_id} |
| 284 | AND t.related_field_id IN ( {$field_ids} ) |
| 285 | ) |
| 286 | ", |
| 287 | 'limit' => 1, |
| 288 | ]; |
| 289 | |
| 290 | $data->select( $params ); |
| 291 | |
| 292 | return $data->total_found(); |
| 293 | } |
| 294 | |
| 295 | } |
| 296 |