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
Group.php
145 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 Exception; |
| 11 | use Pods\Whatsit; |
| 12 | |
| 13 | /** |
| 14 | * Group class. |
| 15 | * |
| 16 | * @since 2.8.0 |
| 17 | */ |
| 18 | class Group extends Whatsit { |
| 19 | |
| 20 | /** |
| 21 | * {@inheritdoc} |
| 22 | */ |
| 23 | protected static $type = 'group'; |
| 24 | |
| 25 | /** |
| 26 | * {@inheritdoc} |
| 27 | */ |
| 28 | public function get_args() { |
| 29 | $args = parent::get_args(); |
| 30 | |
| 31 | // Groups have no group. |
| 32 | unset( $args['group'] ); |
| 33 | |
| 34 | return $args; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * {@inheritdoc} |
| 39 | */ |
| 40 | public function get_fields( array $args = [] ) { |
| 41 | if ( [] === $this->_fields ) { |
| 42 | return []; |
| 43 | } |
| 44 | |
| 45 | $api = pods_api(); |
| 46 | |
| 47 | $has_custom_args = ! empty( $args ); |
| 48 | |
| 49 | if ( null !== $this->_fields && ! $has_custom_args ) { |
| 50 | $objects = $this->maybe_get_objects_by_identifier( $this->_fields, $args ); |
| 51 | |
| 52 | if ( is_array( $objects ) ) { |
| 53 | $this->_fields = pods_clone_objects( $objects ); |
| 54 | |
| 55 | /** @var Field[] $objects */ |
| 56 | return $objects; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | $filtered_args = [ |
| 61 | 'parent' => $this->get_parent_id(), |
| 62 | 'parent_id' => $this->get_parent_id(), |
| 63 | 'parent_name' => $this->get_parent_name(), |
| 64 | 'parent_identifier' => $this->get_parent_identifier(), |
| 65 | 'group' => $this->get_name(), |
| 66 | 'group_id' => $this->get_id(), |
| 67 | 'group_name' => $this->get_name(), |
| 68 | 'group_identifier' => $this->get_identifier(), |
| 69 | ]; |
| 70 | |
| 71 | if ( empty( $filtered_args['parent_id'] ) || empty( $filtered_args['group_id'] ) ) { |
| 72 | $filtered_args['bypass_post_type_find'] = true; |
| 73 | } |
| 74 | |
| 75 | $filtered_args = array_filter( $filtered_args ); |
| 76 | |
| 77 | $args = array_merge( [ |
| 78 | 'orderby' => 'menu_order title', |
| 79 | 'order' => 'ASC', |
| 80 | ], $filtered_args, $args ); |
| 81 | |
| 82 | try { |
| 83 | if ( empty( $args['parent_identifier'] ) && empty( $args['group_id'] ) ) { |
| 84 | throw new Exception( 'Invalid parent identifier for field lookup by group not by ID: ' . wp_json_encode( $args, JSON_PRETTY_PRINT ) ); |
| 85 | } |
| 86 | |
| 87 | if ( ! empty( $args['object_type'] ) ) { |
| 88 | $objects = $api->_load_objects( $args ); |
| 89 | } else { |
| 90 | $objects = $api->load_fields( $args ); |
| 91 | } |
| 92 | } catch ( Exception $exception ) { |
| 93 | pods_debug_log( $exception ); |
| 94 | |
| 95 | $objects = []; |
| 96 | } |
| 97 | |
| 98 | if ( ! $has_custom_args ) { |
| 99 | $this->_fields = pods_clone_objects( $objects ); |
| 100 | } |
| 101 | |
| 102 | return $objects; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * {@inheritdoc} |
| 107 | */ |
| 108 | public function get_groups( array $args = [] ) { |
| 109 | // Groups do not support groups. |
| 110 | return []; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * {@inheritdoc} |
| 115 | */ |
| 116 | public function get_arg( $arg, $default = null, $strict = false, $raw = false ) { |
| 117 | if ( $raw ) { |
| 118 | return parent::get_arg( $arg, $default, $strict, $raw ); |
| 119 | } |
| 120 | |
| 121 | $arg = (string) $arg; |
| 122 | |
| 123 | $special_args = [ |
| 124 | // Pod args. |
| 125 | 'pod_id' => 'get_parent_id', |
| 126 | 'pod' => 'get_parent_name', |
| 127 | 'pod_name' => 'get_parent_name', |
| 128 | 'pod_identifier' => 'get_parent_identifier', |
| 129 | 'pod_label' => 'get_parent_label', |
| 130 | 'pod_description' => 'get_parent_description', |
| 131 | 'pod_object' => 'get_parent_object', |
| 132 | 'pod_object_type' => 'get_parent_object_type', |
| 133 | 'pod_object_storage_type' => 'get_parent_object_storage_type', |
| 134 | 'pod_type' => 'get_parent_type', |
| 135 | ]; |
| 136 | |
| 137 | if ( isset( $special_args[ $arg ] ) ) { |
| 138 | return $this->{$special_args[ $arg ]}(); |
| 139 | } |
| 140 | |
| 141 | return parent::get_arg( $arg, $default ); |
| 142 | } |
| 143 | |
| 144 | } |
| 145 |