Acf
3 months ago
Admin
3 months ago
Ajax
3 months ago
ApplyFilter
3 months ago
Asset
3 months ago
Capabilities
3 months ago
Check
3 months ago
Collection
3 months ago
Column
3 months ago
ColumnFactories
3 months ago
ColumnFactory
3 months ago
ColumnIterator
3 months ago
ColumnRepository
3 months ago
ColumnSize
3 months ago
DI
3 months ago
Deprecated
3 months ago
Entity
3 months ago
Exception
3 months ago
Expression
3 months ago
Form
3 months ago
Formatter
3 months ago
Helper
3 months ago
Integration
3 months ago
ListScreenRepository
3 months ago
ListTable
3 months ago
Message
3 months ago
Meta
3 months ago
Nonce
3 months ago
Notice
3 months ago
Plugin
3 months ago
Preferences
3 months ago
Promo
3 months ago
Request
3 months ago
RequestHandler
3 months ago
Response
3 months ago
Sanitize
3 months ago
Service
3 months ago
Setting
3 months ago
Settings
3 months ago
Storage
3 months ago
Table
3 months ago
TableIdsFactory
3 months ago
TableScreen
3 months ago
TableScreenFactory
3 months ago
ThirdParty
3 months ago
Type
3 months ago
Value
3 months ago
View
3 months ago
AdminColumns.php
3 months ago
ArrayIterator.php
3 months ago
Capabilities.php
3 months ago
Collection.php
3 months ago
CollectionFormatter.php
3 months ago
Column.php
3 months ago
ColumnCollection.php
3 months ago
ColumnFactoryCollectionFactory.php
3 months ago
ColumnFactoryDefinitionCollection.php
3 months ago
ColumnGroups.php
3 months ago
ColumnIterator.php
3 months ago
ColumnNamesTrait.php
3 months ago
ColumnRepository.php
3 months ago
ColumnTypeRepository.php
3 months ago
Config.php
3 months ago
Container.php
3 months ago
DateFormats.php
3 months ago
Expirable.php
3 months ago
Formatter.php
3 months ago
FormatterCollection.php
3 months ago
Helper.php
3 months ago
ListScreen.php
3 months ago
ListScreenCollection.php
3 months ago
ListScreenRepository.php
3 months ago
ListScreenRepositoryWritable.php
3 months ago
ListTable.php
3 months ago
ListTableFactory.php
3 months ago
Loader.php
3 months ago
Message.php
3 months ago
MetaType.php
3 months ago
Middleware.php
3 months ago
OpCacheInvalidateTrait.php
3 months ago
PluginActionLinks.php
3 months ago
PluginActionUpgrade.php
3 months ago
PostType.php
3 months ago
PostTypeRepository.php
3 months ago
Registerable.php
3 months ago
Registry.php
3 months ago
Renderable.php
3 months ago
Request.php
3 months ago
RequestAjaxHandler.php
3 months ago
RequestAjaxHandlers.php
3 months ago
RequestAjaxParser.php
3 months ago
RequestHandler.php
3 months ago
RequestHandlerFactory.php
3 months ago
Screen.php
3 months ago
Services.php
3 months ago
TableIdsFactory.php
3 months ago
TableScreen.php
3 months ago
TableScreenFactory.php
3 months ago
Taxonomy.php
3 months ago
Transient.php
3 months ago
TypedArrayIterator.php
3 months ago
View.php
3 months ago
WpListTableFactory.php
3 months ago
ListScreen.php
431 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC; |
| 6 | |
| 7 | use AC\Type\ColumnId; |
| 8 | use AC\Type\EditorUrlFactory; |
| 9 | use AC\Type\ListScreenId; |
| 10 | use AC\Type\ListScreenStatus; |
| 11 | use AC\Type\TableId; |
| 12 | use AC\Type\Uri; |
| 13 | use ACP\ConditionalFormat\RulesCollection; |
| 14 | use ACP\Search\SegmentCollection; |
| 15 | use DateTime; |
| 16 | use WP_User; |
| 17 | |
| 18 | final class ListScreen |
| 19 | { |
| 20 | |
| 21 | private ListScreenId $id; |
| 22 | |
| 23 | private string $title; |
| 24 | |
| 25 | private TableScreen $table_screen; |
| 26 | |
| 27 | private ColumnIterator $columns; |
| 28 | |
| 29 | private array $preferences; |
| 30 | |
| 31 | private ?DateTime $updated; |
| 32 | |
| 33 | private bool $read_only = false; |
| 34 | |
| 35 | private ListScreenStatus $status; |
| 36 | |
| 37 | private ?SegmentCollection $segments; |
| 38 | |
| 39 | private ?RulesCollection $conditional_format; |
| 40 | |
| 41 | public function __construct( |
| 42 | ListScreenId $id, |
| 43 | string $title, |
| 44 | TableScreen $table_screen, |
| 45 | ?ColumnIterator $columns = null, |
| 46 | array $preferences = [], |
| 47 | ?ListScreenStatus $status = null, |
| 48 | ?DateTime $updated = null, |
| 49 | ?SegmentCollection $segments = null, |
| 50 | ?RulesCollection $conditional_format = null |
| 51 | ) { |
| 52 | $this->id = $id; |
| 53 | $this->title = $title; |
| 54 | $this->table_screen = $table_screen; |
| 55 | $this->columns = $columns ?? new ColumnCollection(); |
| 56 | $this->preferences = $preferences; |
| 57 | $this->status = $status ?? new ListScreenStatus(); |
| 58 | $this->updated = $updated ?? new DateTime(); |
| 59 | $this->segments = $segments; |
| 60 | $this->conditional_format = $conditional_format; |
| 61 | } |
| 62 | |
| 63 | public function get_id(): ListScreenId |
| 64 | { |
| 65 | return $this->id; |
| 66 | } |
| 67 | |
| 68 | public function set_id(ListScreenId $id): void |
| 69 | { |
| 70 | $this->id = $id; |
| 71 | } |
| 72 | |
| 73 | public function set_title(string $title): void |
| 74 | { |
| 75 | $this->title = $title; |
| 76 | } |
| 77 | |
| 78 | public function set_table_screen(TableScreen $table_screen): void |
| 79 | { |
| 80 | $this->table_screen = $table_screen; |
| 81 | } |
| 82 | |
| 83 | public function get_title(): string |
| 84 | { |
| 85 | return $this->title; |
| 86 | } |
| 87 | |
| 88 | public function get_column(ColumnId $id): ?Column |
| 89 | { |
| 90 | foreach ($this->columns as $column) { |
| 91 | if ($column->get_id()->equals($id)) { |
| 92 | return $column; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return null; |
| 97 | } |
| 98 | |
| 99 | public function get_columns(): ColumnIterator |
| 100 | { |
| 101 | return $this->columns; |
| 102 | } |
| 103 | |
| 104 | public function set_columns(ColumnCollection $columns): void |
| 105 | { |
| 106 | $this->columns = $columns; |
| 107 | } |
| 108 | |
| 109 | public function get_preferences(): array |
| 110 | { |
| 111 | return $this->preferences; |
| 112 | } |
| 113 | |
| 114 | public function set_segments(SegmentCollection $segments): void |
| 115 | { |
| 116 | $this->segments = $segments; |
| 117 | } |
| 118 | |
| 119 | public function get_segments(): SegmentCollection |
| 120 | { |
| 121 | if ( ! $this->segments) { |
| 122 | $this->segments = new SegmentCollection(); |
| 123 | } |
| 124 | |
| 125 | return $this->segments; |
| 126 | } |
| 127 | |
| 128 | public function set_conditional_format(RulesCollection $rules_collection): void |
| 129 | { |
| 130 | $this->conditional_format = $rules_collection; |
| 131 | } |
| 132 | |
| 133 | public function get_conditional_format(): RulesCollection |
| 134 | { |
| 135 | if ( ! $this->conditional_format) { |
| 136 | $this->conditional_format = new RulesCollection(); |
| 137 | } |
| 138 | |
| 139 | return $this->conditional_format; |
| 140 | } |
| 141 | |
| 142 | public function get_updated(): DateTime |
| 143 | { |
| 144 | return $this->updated; |
| 145 | } |
| 146 | |
| 147 | public function is_read_only(): bool |
| 148 | { |
| 149 | return $this->read_only; |
| 150 | } |
| 151 | |
| 152 | public function set_read_only(bool $read_only): void |
| 153 | { |
| 154 | $this->read_only = $read_only; |
| 155 | } |
| 156 | |
| 157 | public function get_table_id(): TableId |
| 158 | { |
| 159 | return $this->table_screen->get_id(); |
| 160 | } |
| 161 | |
| 162 | public function get_label(): ?string |
| 163 | { |
| 164 | return $this->table_screen->get_labels()->get_plural(); |
| 165 | } |
| 166 | |
| 167 | public function get_meta_type(): string |
| 168 | { |
| 169 | return $this->table_screen instanceof TableScreen\MetaType |
| 170 | ? (string)$this->table_screen->get_meta_type() |
| 171 | : ''; |
| 172 | } |
| 173 | |
| 174 | public function get_screen_id(): string |
| 175 | { |
| 176 | return $this->table_screen->get_screen_id(); |
| 177 | } |
| 178 | |
| 179 | public function get_post_type(): ?string |
| 180 | { |
| 181 | return $this->table_screen instanceof PostType |
| 182 | ? (string)$this->table_screen->get_post_type() |
| 183 | : null; |
| 184 | } |
| 185 | |
| 186 | public function get_table_screen(): TableScreen |
| 187 | { |
| 188 | return $this->table_screen; |
| 189 | } |
| 190 | |
| 191 | public function get_table_url(): Uri |
| 192 | { |
| 193 | return $this->table_screen |
| 194 | ->get_url() |
| 195 | ->with_arg('layout', (string)$this->id); |
| 196 | } |
| 197 | |
| 198 | public function get_editor_url(): Uri |
| 199 | { |
| 200 | return EditorUrlFactory::create( |
| 201 | $this->get_table_id(), |
| 202 | $this->table_screen->is_network(), |
| 203 | $this->id |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | public function is_user_allowed(WP_User $user): bool |
| 208 | { |
| 209 | return user_can($user, Capabilities::MANAGE) || $this->is_user_assigned($user); |
| 210 | } |
| 211 | |
| 212 | public function is_user_assigned(WP_User $user): bool |
| 213 | { |
| 214 | $user_ids = $this->get_preference('users'); |
| 215 | $roles = $this->get_preference('roles'); |
| 216 | |
| 217 | $user_ids = is_array($user_ids) |
| 218 | ? array_filter(array_map('intval', $user_ids)) |
| 219 | : []; |
| 220 | |
| 221 | $roles = is_array($roles) |
| 222 | ? array_filter(array_map('strval', $roles)) |
| 223 | : []; |
| 224 | |
| 225 | if ( ! $user_ids && ! $roles) { |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | foreach ($roles as $role) { |
| 230 | if ($user->has_cap($role)) { |
| 231 | return true; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return in_array($user->ID, $user_ids, true); |
| 236 | } |
| 237 | |
| 238 | public function set_preferences(array $preferences): void |
| 239 | { |
| 240 | $this->preferences = $preferences; |
| 241 | } |
| 242 | |
| 243 | public function get_preference(string $key) |
| 244 | { |
| 245 | return $this->preferences[$key] ?? null; |
| 246 | } |
| 247 | |
| 248 | public function set_preference(string $key, $value): void |
| 249 | { |
| 250 | $this->preferences[$key] = $value; |
| 251 | } |
| 252 | |
| 253 | public function get_status(): ListScreenStatus |
| 254 | { |
| 255 | return $this->status; |
| 256 | } |
| 257 | |
| 258 | public function set_status(ListScreenStatus $status): void |
| 259 | { |
| 260 | $this->status = $status; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @deprecated 7.0 |
| 265 | */ |
| 266 | public function get_singular_label(): string |
| 267 | { |
| 268 | _deprecated_function(__METHOD__, '7.0', 'AC\ListScreen::get_table()->get_labels->get_singular()'); |
| 269 | |
| 270 | return $this->table_screen->get_labels()->get_singular(); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * @deprecated 7.0 |
| 275 | */ |
| 276 | public function get_layout_id(): string |
| 277 | { |
| 278 | _deprecated_function(__METHOD__, '7.0', 'AC\ListScreen::get_id()'); |
| 279 | |
| 280 | return (string)$this->id; |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * @deprecated 7.0 |
| 285 | */ |
| 286 | public function get_column_by_name(string $name): ?Column |
| 287 | { |
| 288 | _deprecated_function(__METHOD__, '7.0', 'AC\ListScreen::get_column()'); |
| 289 | |
| 290 | return $this->get_column(new ColumnId($name)); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @deprecated 7.0 |
| 295 | */ |
| 296 | public function get_screen_link(): string |
| 297 | { |
| 298 | _deprecated_function(__METHOD__, '4.6.5', 'AC\ListScreen::get_table_url()'); |
| 299 | |
| 300 | return (string)$this->get_table_url(); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * @deprecated 7.0 |
| 305 | */ |
| 306 | public function get_edit_link(): string |
| 307 | { |
| 308 | _deprecated_function(__METHOD__, '4.6.5', 'AC\ListScreen::get_editor_url()'); |
| 309 | |
| 310 | return (string)$this->get_editor_url(); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * @deprecated 7.0 |
| 315 | */ |
| 316 | protected function set_meta_type(): void |
| 317 | { |
| 318 | _deprecated_function(__METHOD__, '7.0'); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * @deprecated 7.0 |
| 323 | */ |
| 324 | public function deregister_column(): void |
| 325 | { |
| 326 | _deprecated_function(__METHOD__, '7.0'); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * @deprecated 7.0 |
| 331 | */ |
| 332 | public function set_layout_id(string $layout_id): void |
| 333 | { |
| 334 | _deprecated_function(__METHOD__, '7.0', 'AC\ListScreen::set_id()'); |
| 335 | |
| 336 | if (ListScreenId::is_valid_id($layout_id)) { |
| 337 | $this->id = new ListScreenId($layout_id); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * @deprecated 7.0 |
| 343 | */ |
| 344 | protected function set_label(): void |
| 345 | { |
| 346 | _deprecated_function(__METHOD__, '7.0'); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * @deprecated 7.0 |
| 351 | */ |
| 352 | protected function register_column_types_from_list(): void |
| 353 | { |
| 354 | _deprecated_function(__METHOD__, '7.0', 'AC\TableScreen::set_column_type()'); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * @deprecated 7.0 |
| 359 | */ |
| 360 | public function deregister_column_type(): void |
| 361 | { |
| 362 | _deprecated_function(__METHOD__, '7.0'); |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * @deprecated 7.0 |
| 367 | */ |
| 368 | public function register_column_type(): void |
| 369 | { |
| 370 | _deprecated_function(__METHOD__, '7.0', 'AC\TableScreen::set_column_type()'); |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * @deprecated 7.0 |
| 375 | */ |
| 376 | public function get_original_columns(): array |
| 377 | { |
| 378 | _deprecated_function(__METHOD__, '7.0'); |
| 379 | |
| 380 | return []; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * @deprecated 7.0 |
| 385 | */ |
| 386 | public function get_group(): string |
| 387 | { |
| 388 | _deprecated_function(__METHOD__, '7.0'); |
| 389 | |
| 390 | return ''; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * @deprecated 7.0 |
| 395 | */ |
| 396 | |
| 397 | public function has_id(): bool |
| 398 | { |
| 399 | _deprecated_function(__METHOD__, '7.0'); |
| 400 | |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * @deprecated 7.0 |
| 406 | */ |
| 407 | public function get_storage_key(): string |
| 408 | { |
| 409 | _deprecated_function(__METHOD__, '7.0'); |
| 410 | |
| 411 | return $this->get_table_id() . $this->id; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * @deprecated 7.0 |
| 416 | */ |
| 417 | public function get_settings(): array |
| 418 | { |
| 419 | _deprecated_function(__METHOD__, '7.0'); |
| 420 | |
| 421 | return []; |
| 422 | } |
| 423 | |
| 424 | public function get_key(): TableId |
| 425 | { |
| 426 | _deprecated_function(__METHOD__, '7.0', 'AC\ListScreen::get_table_id()'); |
| 427 | |
| 428 | return $this->get_table_id(); |
| 429 | } |
| 430 | |
| 431 | } |