Addon
8 years ago
Admin
8 years ago
Column
8 years ago
Helper
8 years ago
ListScreen
8 years ago
Meta
8 years ago
Notice
8 years ago
Plugin
8 years ago
Relation
8 years ago
Settings
8 years ago
ThirdParty
8 years ago
API.php
8 years ago
Addon.php
8 years ago
Addons.php
8 years ago
Admin.php
8 years ago
Autoloader.php
8 years ago
Collection.php
8 years ago
Column.php
8 years ago
Container.php
8 years ago
Groups.php
8 years ago
Helper.php
8 years ago
ListScreen.php
8 years ago
ListScreenPost.php
8 years ago
Plugin.php
8 years ago
PluginInformation.php
8 years ago
Preferences.php
8 years ago
Relation.php
8 years ago
TableScreen.php
8 years ago
View.php
8 years ago
ViewInterface.php
8 years ago
Groups.php
155 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Holds the groups to which columns can belong. |
| 9 | */ |
| 10 | final class AC_Groups { |
| 11 | |
| 12 | const SORT_PRIORITY = 1; |
| 13 | |
| 14 | const SORT_SLUG = 2; |
| 15 | |
| 16 | const SORT_LABEL = 3; |
| 17 | |
| 18 | /** |
| 19 | * @var array |
| 20 | */ |
| 21 | private $groups = array(); |
| 22 | |
| 23 | /** |
| 24 | * @return array |
| 25 | */ |
| 26 | public function get_groups() { |
| 27 | return $this->groups; |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Return the registered groups sorted by either label, slug or priority |
| 32 | * |
| 33 | * @param int $sort_by Default is self::SORT_PRIORITY |
| 34 | * |
| 35 | * @return array |
| 36 | */ |
| 37 | public function get_groups_sorted( $sort_by = null ) { |
| 38 | switch ( $sort_by ) { |
| 39 | case self::SORT_LABEL : |
| 40 | $sorted = $this->sort_groups_by_string( $this->get_groups(), 'label' ); |
| 41 | |
| 42 | break; |
| 43 | case self::SORT_SLUG : |
| 44 | $sorted = $this->sort_groups_by_string( $this->get_groups(), 'slug' ); |
| 45 | |
| 46 | break; |
| 47 | default : |
| 48 | $sorted = $this->sort_groups_by_priority( $this->get_groups() ); |
| 49 | } |
| 50 | |
| 51 | return $sorted; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Sort the group by priority |
| 56 | * |
| 57 | * If there are more groups with the same priority it will those groups by label |
| 58 | * |
| 59 | * @param $groups |
| 60 | * |
| 61 | * @return array |
| 62 | */ |
| 63 | private function sort_groups_by_priority( array $groups ) { |
| 64 | $aggregated = $sorted = array(); |
| 65 | |
| 66 | foreach ( $groups as $group ) { |
| 67 | $aggregated[ $group['priority'] ][] = $group; |
| 68 | } |
| 69 | |
| 70 | ksort( $aggregated, SORT_NUMERIC ); |
| 71 | |
| 72 | foreach ( $aggregated as $priority => $groups ) { |
| 73 | $sorted = array_merge( $sorted, $this->sort_groups_by_string( $groups, 'label' ) ); |
| 74 | } |
| 75 | |
| 76 | return $sorted; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Sort the group by label or slug |
| 81 | * |
| 82 | * @param array $groups |
| 83 | * @param string $key |
| 84 | * |
| 85 | * @return array |
| 86 | */ |
| 87 | private function sort_groups_by_string( array $groups, $key ) { |
| 88 | $sorted = array(); |
| 89 | |
| 90 | foreach ( $groups as $k => $group ) { |
| 91 | $sorted[ $k ] = $group[ $key ]; |
| 92 | } |
| 93 | |
| 94 | natcasesort( $sorted ); |
| 95 | |
| 96 | foreach ( array_keys( $sorted ) as $k ) { |
| 97 | $sorted[ $k ] = $groups[ $k ]; |
| 98 | } |
| 99 | |
| 100 | return $sorted; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @param string $slug |
| 105 | * |
| 106 | * @return bool|mixed |
| 107 | */ |
| 108 | public function get_group( $slug ) { |
| 109 | if ( ! $this->has_group( $slug ) ) { |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | return $this->groups[ $slug ]; |
| 114 | } |
| 115 | |
| 116 | public function get_group_label( $slug ) { |
| 117 | $group = $this->get_group( $slug ); |
| 118 | |
| 119 | return $group ? $group['label'] : false; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * @param string $slug |
| 124 | * |
| 125 | * @return bool |
| 126 | */ |
| 127 | public function has_group( $slug ) { |
| 128 | return isset( $this->groups[ $slug ] ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Register a (column) group |
| 133 | * |
| 134 | * @param string $slug |
| 135 | * @param string $label Should be translatable |
| 136 | * @param int $priority |
| 137 | * |
| 138 | * @return bool |
| 139 | */ |
| 140 | public function register_group( $slug, $label, $priority = 10 ) { |
| 141 | if ( $this->has_group( $slug ) ) { |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | $this->groups[ $slug ] = array( |
| 146 | 'slug' => $slug, |
| 147 | 'label' => $label, |
| 148 | 'priority' => $priority, |
| 149 | ); |
| 150 | |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | } |
| 155 |