Admin
6 months ago
Ajax
6 months ago
ApplyFilter
6 months ago
Asset
6 months ago
Capabilities
6 months ago
Check
6 months ago
Column
6 months ago
ColumnRepository
6 months ago
ColumnSize
6 months ago
Controller
6 months ago
Deprecated
6 months ago
Entity
6 months ago
Exception
6 months ago
Form
6 months ago
Helper
6 months ago
Integration
6 months ago
ListScreen
6 months ago
ListScreenFactory
6 months ago
ListScreenRepository
6 months ago
ListTable
6 months ago
Message
6 months ago
Meta
6 months ago
Nonce
6 months ago
Plugin
6 months ago
Preferences
6 months ago
Promo
6 months ago
Relation
6 months ago
Request
6 months ago
RequestHandler
6 months ago
Response
6 months ago
Sanitize
6 months ago
Screen
6 months ago
Service
6 months ago
Settings
6 months ago
Storage
6 months ago
Table
6 months ago
ThirdParty
6 months ago
Transient
6 months ago
Type
6 months ago
View
6 months ago
AdminColumns.php
6 months ago
ArrayIterator.php
6 months ago
Capabilities.php
6 months ago
Collection.php
6 months ago
Column.php
6 months ago
ColumnGroups.php
6 months ago
ColumnRepository.php
6 months ago
Config.php
6 months ago
Container.php
6 months ago
DefaultColumnsRepository.php
6 months ago
Dependencies.php
6 months ago
Expirable.php
6 months ago
Groups.php
6 months ago
Helper.php
6 months ago
Integration.php
6 months ago
IntegrationRepository.php
6 months ago
Integrations.php
6 months ago
Iterator.php
6 months ago
ListScreen.php
6 months ago
ListScreenCollection.php
6 months ago
ListScreenFactory.php
6 months ago
ListScreenGroupsFactory.php
6 months ago
ListScreenPost.php
6 months ago
ListScreenRepository.php
6 months ago
ListScreenRepositoryWritable.php
6 months ago
ListTable.php
6 months ago
ListTableFactory.php
6 months ago
Message.php
6 months ago
MetaType.php
6 months ago
Middleware.php
6 months ago
OpCacheInvalidateTrait.php
6 months ago
PluginActionLinks.php
6 months ago
PluginActionUpgrade.php
6 months ago
PluginUpdate.php
6 months ago
PostType.php
6 months ago
PostTypeRepository.php
6 months ago
Preferences.php
6 months ago
Promo.php
6 months ago
PromoCollection.php
6 months ago
Registerable.php
6 months ago
Relation.php
6 months ago
Renderable.php
6 months ago
Request.php
6 months ago
RequestAjaxHandler.php
6 months ago
RequestAjaxHandlers.php
6 months ago
RequestAjaxParser.php
6 months ago
Sanitize.php
6 months ago
Screen.php
6 months ago
ScreenController.php
6 months ago
Services.php
6 months ago
Stringable.php
6 months ago
Transient.php
6 months ago
TypedArrayIterator.php
6 months ago
View.php
6 months ago
ViewCollection.php
6 months ago
WpListTableFactory.php
6 months ago
ListScreen.php
544 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC; |
| 6 | |
| 7 | use AC\Type\ListScreenId; |
| 8 | use AC\Type\Uri; |
| 9 | use AC\Type\Url; |
| 10 | use DateTime; |
| 11 | use LogicException; |
| 12 | use WP_User; |
| 13 | |
| 14 | abstract class ListScreen implements PostType |
| 15 | { |
| 16 | |
| 17 | /** |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $key; |
| 21 | |
| 22 | /** |
| 23 | * The unique ID of the screen. |
| 24 | * @see \WP_Screen::id |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $screen_id; |
| 28 | |
| 29 | /** |
| 30 | * @var string |
| 31 | */ |
| 32 | protected $label; |
| 33 | |
| 34 | /** |
| 35 | * @var string |
| 36 | */ |
| 37 | protected $singular_label; |
| 38 | |
| 39 | /** |
| 40 | * Meta type of list screen; post, user, comment. Mostly used for fetching metadata. |
| 41 | * @var string |
| 42 | */ |
| 43 | protected $meta_type; |
| 44 | |
| 45 | /** |
| 46 | * Group slug. Used for menu. |
| 47 | * @var string |
| 48 | */ |
| 49 | protected $group = ''; |
| 50 | |
| 51 | /** |
| 52 | * @var Column[] |
| 53 | */ |
| 54 | private $columns; |
| 55 | |
| 56 | /** |
| 57 | * @var Column[] |
| 58 | */ |
| 59 | private $column_types; |
| 60 | |
| 61 | /** |
| 62 | * @var ListScreenId |
| 63 | */ |
| 64 | protected $id; |
| 65 | |
| 66 | /** |
| 67 | * @var array Column settings data |
| 68 | */ |
| 69 | private $settings = []; |
| 70 | |
| 71 | /** |
| 72 | * @var array ListScreen settings data |
| 73 | */ |
| 74 | private $preferences = []; |
| 75 | |
| 76 | /** |
| 77 | * @var bool True when column settings can not be overwritten |
| 78 | */ |
| 79 | private $read_only = false; |
| 80 | |
| 81 | /** |
| 82 | * @var string |
| 83 | */ |
| 84 | private $title; |
| 85 | |
| 86 | /** |
| 87 | * @var DateTime |
| 88 | */ |
| 89 | private $updated; |
| 90 | |
| 91 | /** |
| 92 | * @var string |
| 93 | */ |
| 94 | protected $query_type; |
| 95 | |
| 96 | protected $post_type = ''; |
| 97 | |
| 98 | public function __construct(string $key, string $screen_id) |
| 99 | { |
| 100 | $this->key = $key; |
| 101 | $this->screen_id = $screen_id; |
| 102 | } |
| 103 | |
| 104 | public function get_post_type(): string |
| 105 | { |
| 106 | return $this->post_type; |
| 107 | } |
| 108 | |
| 109 | public function has_id(): bool |
| 110 | { |
| 111 | return null !== $this->id; |
| 112 | } |
| 113 | |
| 114 | public function get_id(): ListScreenId |
| 115 | { |
| 116 | if ( ! $this->has_id()) { |
| 117 | throw new LogicException('ListScreen has no identity.'); |
| 118 | } |
| 119 | |
| 120 | return $this->id; |
| 121 | } |
| 122 | |
| 123 | abstract protected function register_column_types(): void; |
| 124 | |
| 125 | /** |
| 126 | * Register column types from a list with (fully qualified) class names |
| 127 | * |
| 128 | * @param string[] $list |
| 129 | */ |
| 130 | protected function register_column_types_from_list(array $list): void |
| 131 | { |
| 132 | foreach ($list as $column) { |
| 133 | $this->register_column_type(new $column()); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | public function get_heading_hookname(): string |
| 138 | { |
| 139 | return sprintf('manage_%s_columns', $this->get_screen_id()); |
| 140 | } |
| 141 | |
| 142 | public function get_key(): string |
| 143 | { |
| 144 | return $this->key; |
| 145 | } |
| 146 | |
| 147 | public function get_label(): ?string |
| 148 | { |
| 149 | return $this->label; |
| 150 | } |
| 151 | |
| 152 | public function get_singular_label(): ?string |
| 153 | { |
| 154 | return $this->singular_label ?: $this->label; |
| 155 | } |
| 156 | |
| 157 | public function get_meta_type(): string |
| 158 | { |
| 159 | return $this->meta_type ?: ''; |
| 160 | } |
| 161 | |
| 162 | public function get_query_type(): string |
| 163 | { |
| 164 | return $this->query_type ?: $this->get_meta_type(); |
| 165 | } |
| 166 | |
| 167 | public function get_screen_id(): string |
| 168 | { |
| 169 | return $this->screen_id; |
| 170 | } |
| 171 | |
| 172 | public function get_group(): string |
| 173 | { |
| 174 | return $this->group; |
| 175 | } |
| 176 | |
| 177 | public function set_group(string $group): void |
| 178 | { |
| 179 | $this->group = $group; |
| 180 | } |
| 181 | |
| 182 | public function get_title(): string |
| 183 | { |
| 184 | return $this->title; |
| 185 | } |
| 186 | |
| 187 | public function set_title(string $title): void |
| 188 | { |
| 189 | $this->title = $title; |
| 190 | } |
| 191 | |
| 192 | public function get_storage_key(): string |
| 193 | { |
| 194 | return $this->key . $this->id; |
| 195 | } |
| 196 | |
| 197 | public function set_id(ListScreenId $id): void |
| 198 | { |
| 199 | $this->id = $id; |
| 200 | } |
| 201 | |
| 202 | public function get_table_attr_id(): string |
| 203 | { |
| 204 | return '#the-list'; |
| 205 | } |
| 206 | |
| 207 | public function is_read_only(): bool |
| 208 | { |
| 209 | return $this->read_only; |
| 210 | } |
| 211 | |
| 212 | public function set_read_only(bool $read_only): void |
| 213 | { |
| 214 | $this->read_only = $read_only; |
| 215 | } |
| 216 | |
| 217 | public function set_updated(DateTime $updated): void |
| 218 | { |
| 219 | $this->updated = $updated; |
| 220 | } |
| 221 | |
| 222 | public function get_updated(): DateTime |
| 223 | { |
| 224 | return $this->updated ?: new DateTime(); |
| 225 | } |
| 226 | |
| 227 | abstract public function get_table_url(): Uri; |
| 228 | |
| 229 | public function get_editor_url(): Uri |
| 230 | { |
| 231 | return new Url\EditorColumns($this->key, $this->id); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @return Column[] |
| 236 | */ |
| 237 | public function get_columns(): array |
| 238 | { |
| 239 | if (null === $this->columns) { |
| 240 | $this->set_columns(); |
| 241 | } |
| 242 | |
| 243 | return $this->columns; |
| 244 | } |
| 245 | |
| 246 | private function set_columns(): void |
| 247 | { |
| 248 | foreach ($this->get_settings() as $name => $data) { |
| 249 | // for backwards compatibility with the upcoming 7.0 release |
| 250 | if (empty($data['name'])) { |
| 251 | $data['name'] = $name; |
| 252 | } |
| 253 | |
| 254 | $column = $this->create_column($data); |
| 255 | |
| 256 | if ($column) { |
| 257 | $this->columns[$column->get_name()] = $column; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | // Nothing stored. Use WP default columns. |
| 262 | if (null === $this->columns) { |
| 263 | foreach ($this->get_original_columns() as $type => $label) { |
| 264 | $column = $this->create_column(['type' => $type, 'original' => true]); |
| 265 | |
| 266 | if ( ! $column) { |
| 267 | continue; |
| 268 | } |
| 269 | |
| 270 | $this->columns[$column->get_name()] = $column; |
| 271 | } |
| 272 | } |
| 273 | |
| 274 | if (null === $this->columns) { |
| 275 | $this->columns = []; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * @return Column[] |
| 281 | */ |
| 282 | public function get_column_types(): array |
| 283 | { |
| 284 | if (null === $this->column_types) { |
| 285 | $this->set_column_types(); |
| 286 | } |
| 287 | |
| 288 | return $this->column_types; |
| 289 | } |
| 290 | |
| 291 | private function set_column_types(): void |
| 292 | { |
| 293 | $this->column_types = []; |
| 294 | |
| 295 | foreach ($this->get_original_columns() as $type => $label) { |
| 296 | // Ignore the mandatory checkbox column |
| 297 | if ('cb' === $type) { |
| 298 | continue; |
| 299 | } |
| 300 | |
| 301 | $column = new Column(); |
| 302 | $column->set_type($type) |
| 303 | ->set_original(true); |
| 304 | |
| 305 | $this->register_column_type($column); |
| 306 | } |
| 307 | |
| 308 | // Load Custom columns |
| 309 | $this->register_column_types(); |
| 310 | |
| 311 | /** |
| 312 | * Register column types |
| 313 | * |
| 314 | * @param ListScreen $this |
| 315 | */ |
| 316 | do_action('ac/column_types', $this); |
| 317 | } |
| 318 | |
| 319 | public function get_column_by_name($name): ?Column |
| 320 | { |
| 321 | foreach ($this->get_columns() as $column) { |
| 322 | // Do not do a strict comparison. All column names are stored as strings, even integers. |
| 323 | if ($column->get_name() == $name) { |
| 324 | return $column; |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | return null; |
| 329 | } |
| 330 | |
| 331 | public function get_column_by_type(string $type): ?Column |
| 332 | { |
| 333 | $column_types = $this->get_column_types(); |
| 334 | |
| 335 | return $column_types[$type] ?? null; |
| 336 | } |
| 337 | |
| 338 | private function get_class_by_type(string $type): ?string |
| 339 | { |
| 340 | $column = $this->get_column_by_type($type); |
| 341 | |
| 342 | return $column |
| 343 | ? get_class($column) |
| 344 | : null; |
| 345 | } |
| 346 | |
| 347 | public function deregister_column_type(string $type): void |
| 348 | { |
| 349 | unset($this->column_types[$type]); |
| 350 | } |
| 351 | |
| 352 | public function register_column_type(Column $column): void |
| 353 | { |
| 354 | if ( ! $column->get_type()) { |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | $column->set_list_screen($this); |
| 359 | |
| 360 | if ( ! $column->is_valid()) { |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | // Skip the custom registered columns which are marked 'original' but are not available for this list screen |
| 365 | if ($column->is_original() && ! array_key_exists($column->get_type(), $this->get_original_columns())) { |
| 366 | return; |
| 367 | } |
| 368 | |
| 369 | $this->column_types[$column->get_type()] = $column; |
| 370 | } |
| 371 | |
| 372 | public function get_original_label(string $type): ?string |
| 373 | { |
| 374 | $columns = $this->get_original_columns(); |
| 375 | |
| 376 | $label = $columns[$type] ?? null; |
| 377 | |
| 378 | return is_string($label) |
| 379 | ? $label |
| 380 | : null; |
| 381 | } |
| 382 | |
| 383 | public function get_original_columns(): array |
| 384 | { |
| 385 | return (new DefaultColumnsRepository())->get($this->get_key()); |
| 386 | } |
| 387 | |
| 388 | private function is_original_column(string $type): bool |
| 389 | { |
| 390 | $column = $this->get_column_by_type($type); |
| 391 | |
| 392 | return $column && $column->is_original(); |
| 393 | } |
| 394 | |
| 395 | public function create_column(array $settings): ?Column |
| 396 | { |
| 397 | if ( ! isset($settings['type'])) { |
| 398 | return null; |
| 399 | } |
| 400 | |
| 401 | $class = $this->get_class_by_type((string)$settings['type']); |
| 402 | |
| 403 | if ( ! $class) { |
| 404 | return null; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * @var Column $column |
| 409 | */ |
| 410 | $column = new $class(); |
| 411 | $column->set_list_screen($this) |
| 412 | ->set_type($settings['type']); |
| 413 | |
| 414 | if (isset($settings['name'])) { |
| 415 | $column->set_name($settings['name']); |
| 416 | } |
| 417 | |
| 418 | // Mark as original |
| 419 | if ($this->is_original_column($settings['type'])) { |
| 420 | $column->set_original(true); |
| 421 | $column->set_name($settings['type']); |
| 422 | } |
| 423 | |
| 424 | $column->set_options($settings); |
| 425 | |
| 426 | do_action('ac/list_screen/column_created', $column, $this); |
| 427 | |
| 428 | return $column; |
| 429 | } |
| 430 | |
| 431 | public function set_settings(array $settings): void |
| 432 | { |
| 433 | $this->settings = $settings; |
| 434 | } |
| 435 | |
| 436 | public function get_settings(): array |
| 437 | { |
| 438 | return $this->settings; |
| 439 | } |
| 440 | |
| 441 | public function is_user_allowed(WP_User $user): bool |
| 442 | { |
| 443 | return user_can($user, Capabilities::MANAGE) || $this->is_user_assigned($user); |
| 444 | } |
| 445 | |
| 446 | public function is_user_assigned(WP_User $user): bool |
| 447 | { |
| 448 | $user_ids = $this->get_preference('users'); |
| 449 | $roles = $this->get_preference('roles'); |
| 450 | |
| 451 | $user_ids = is_array($user_ids) |
| 452 | ? array_filter(array_map('intval', $user_ids)) |
| 453 | : []; |
| 454 | |
| 455 | $roles = is_array($roles) |
| 456 | ? array_filter(array_map('strval', $roles)) |
| 457 | : []; |
| 458 | |
| 459 | if ( ! $user_ids && ! $roles) { |
| 460 | return true; |
| 461 | } |
| 462 | |
| 463 | foreach ($roles as $role) { |
| 464 | if ($user->has_cap($role)) { |
| 465 | return true; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | return in_array($user->ID, $user_ids, true); |
| 470 | } |
| 471 | |
| 472 | public function set_preferences(array $preferences): void |
| 473 | { |
| 474 | $this->preferences = apply_filters('ac/list_screen/preferences', $preferences, $this); |
| 475 | } |
| 476 | |
| 477 | public function set_preference(string $key, $value): void |
| 478 | { |
| 479 | $this->preferences[$key] = $value; |
| 480 | } |
| 481 | |
| 482 | public function get_preferences(): array |
| 483 | { |
| 484 | return $this->preferences; |
| 485 | } |
| 486 | |
| 487 | public function get_preference(string $key) |
| 488 | { |
| 489 | return $this->preferences[$key] ?? null; |
| 490 | } |
| 491 | |
| 492 | /** |
| 493 | * @deprecated NEWVERSION |
| 494 | */ |
| 495 | public function get_layout_id(): ?string |
| 496 | { |
| 497 | return (string)$this->id; |
| 498 | } |
| 499 | |
| 500 | public function get_screen_link(): string |
| 501 | { |
| 502 | _deprecated_function(__METHOD__, '4.6.5', 'AC\ListScreen::get_table_url()'); |
| 503 | |
| 504 | return (string)$this->get_table_url(); |
| 505 | } |
| 506 | |
| 507 | public function get_edit_link(): string |
| 508 | { |
| 509 | _deprecated_function(__METHOD__, '4.6.5', 'AC\ListScreen::get_editor_url()'); |
| 510 | |
| 511 | return (string)$this->get_editor_url(); |
| 512 | } |
| 513 | |
| 514 | protected function set_meta_type(string $meta_type): void |
| 515 | { |
| 516 | _deprecated_function(__METHOD__, '6.4'); |
| 517 | |
| 518 | $this->meta_type = $meta_type; |
| 519 | } |
| 520 | |
| 521 | public function deregister_column(string $column_name): void |
| 522 | { |
| 523 | _deprecated_function(__METHOD__, '6.4'); |
| 524 | |
| 525 | unset($this->columns[$column_name]); |
| 526 | } |
| 527 | |
| 528 | public function set_layout_id(string $layout_id): void |
| 529 | { |
| 530 | _deprecated_function(__METHOD__, '6.4', 'AC\ListScreen::set_id()'); |
| 531 | |
| 532 | if (ListScreenId::is_valid_id($layout_id)) { |
| 533 | $this->id = new ListScreenId($layout_id); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | protected function set_label(string $label): void |
| 538 | { |
| 539 | _deprecated_function(__METHOD__, '6.4'); |
| 540 | |
| 541 | $this->label = $label; |
| 542 | } |
| 543 | |
| 544 | } |