Storage
1 day ago
Block.php
1 day ago
Block_Collection.php
1 day ago
Block_Field.php
1 day ago
Field.php
1 day ago
Group.php
1 day ago
Legacy_Object.php
1 day ago
Object_Field.php
1 day ago
Page.php
1 day ago
Pod.php
1 day ago
Storage.php
1 day ago
Store.php
1 day ago
Template.php
1 day ago
Group.php
129 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Pods\Whatsit; |
| 4 | |
| 5 | use Pods\Whatsit; |
| 6 | |
| 7 | /** |
| 8 | * Group class. |
| 9 | * |
| 10 | * @since 2.8.0 |
| 11 | */ |
| 12 | class Group extends Whatsit { |
| 13 | |
| 14 | /** |
| 15 | * {@inheritdoc} |
| 16 | */ |
| 17 | protected static $type = 'group'; |
| 18 | |
| 19 | /** |
| 20 | * {@inheritdoc} |
| 21 | */ |
| 22 | public function get_args() { |
| 23 | $args = parent::get_args(); |
| 24 | |
| 25 | // Groups have no group. |
| 26 | unset( $args['group'] ); |
| 27 | |
| 28 | return $args; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * {@inheritdoc} |
| 33 | */ |
| 34 | public function get_fields( array $args = [] ) { |
| 35 | if ( [] === $this->_fields ) { |
| 36 | return []; |
| 37 | } |
| 38 | |
| 39 | $object_collection = Store::get_instance(); |
| 40 | |
| 41 | $has_custom_args = ! empty( $args ); |
| 42 | |
| 43 | if ( null === $this->_fields || $has_custom_args ) { |
| 44 | $filtered_args = [ |
| 45 | 'parent' => $this->get_parent_id(), |
| 46 | 'parent_id' => $this->get_parent_id(), |
| 47 | 'parent_name' => $this->get_parent_name(), |
| 48 | 'parent_identifier' => $this->get_parent_identifier(), |
| 49 | 'group' => $this->get_name(), |
| 50 | 'group_id' => $this->get_id(), |
| 51 | 'group_name' => $this->get_name(), |
| 52 | 'group_identifier' => $this->get_identifier(), |
| 53 | ]; |
| 54 | |
| 55 | if ( empty( $filtered_args['parent_id'] ) || empty( $filtered_args['group_id'] ) ) { |
| 56 | $filtered_args['bypass_post_type_find'] = true; |
| 57 | } |
| 58 | |
| 59 | $filtered_args = array_filter( $filtered_args ); |
| 60 | |
| 61 | $args = array_merge( [ |
| 62 | 'orderby' => 'menu_order title', |
| 63 | 'order' => 'ASC', |
| 64 | ], $filtered_args, $args ); |
| 65 | |
| 66 | try { |
| 67 | $api = pods_api(); |
| 68 | |
| 69 | if ( ! empty( $args['object_type'] ) ) { |
| 70 | $objects = $api->_load_objects( $args ); |
| 71 | } else { |
| 72 | $objects = $api->load_fields( $args ); |
| 73 | } |
| 74 | } catch ( \Exception $exception ) { |
| 75 | $objects = []; |
| 76 | } |
| 77 | |
| 78 | if ( ! $has_custom_args ) { |
| 79 | $this->_fields = wp_list_pluck( $objects, 'identifier' ); |
| 80 | } |
| 81 | |
| 82 | return $objects; |
| 83 | } |
| 84 | |
| 85 | $objects = array_map( [ $object_collection, 'get_object' ], $this->_fields ); |
| 86 | $objects = array_filter( $objects ); |
| 87 | |
| 88 | $names = wp_list_pluck( $objects, 'name' ); |
| 89 | |
| 90 | return array_combine( $names, $objects ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * {@inheritdoc} |
| 95 | */ |
| 96 | public function get_groups( array $args = [] ) { |
| 97 | // Groups do not support groups. |
| 98 | return []; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * {@inheritdoc} |
| 103 | */ |
| 104 | public function get_arg( $arg, $default = null, $strict = false ) { |
| 105 | $arg = (string) $arg; |
| 106 | |
| 107 | $special_args = [ |
| 108 | // Pod args. |
| 109 | 'pod_id' => 'get_parent_id', |
| 110 | 'pod' => 'get_parent_name', |
| 111 | 'pod_name' => 'get_parent_name', |
| 112 | 'pod_identifier' => 'get_parent_identifier', |
| 113 | 'pod_label' => 'get_parent_label', |
| 114 | 'pod_description' => 'get_parent_description', |
| 115 | 'pod_object' => 'get_parent_object', |
| 116 | 'pod_object_type' => 'get_parent_object_type', |
| 117 | 'pod_object_storage_type' => 'get_parent_object_storage_type', |
| 118 | 'pod_type' => 'get_parent_type', |
| 119 | ]; |
| 120 | |
| 121 | if ( isset( $special_args[ $arg ] ) ) { |
| 122 | return $this->{$special_args[ $arg ]}(); |
| 123 | } |
| 124 | |
| 125 | return parent::get_arg( $arg, $default ); |
| 126 | } |
| 127 | |
| 128 | } |
| 129 |