Acf
1 day ago
Admin
1 day ago
Ajax
1 day ago
ApplyFilter
1 day ago
Asset
1 day ago
Capabilities
1 day ago
Check
1 day ago
Collection
1 day ago
Column
1 day ago
ColumnFactories
1 day ago
ColumnFactory
1 day ago
ColumnIterator
1 day ago
ColumnRepository
1 day ago
ColumnSize
1 day ago
DI
1 day ago
DefaultColumnHandler
1 day ago
Deprecated
1 day ago
Entity
1 day ago
Exception
1 day ago
Expression
1 day ago
Form
1 day ago
Formatter
1 day ago
Helper
1 day ago
Integration
1 day ago
ListScreenRepository
1 day ago
ListTable
1 day ago
Message
1 day ago
Meta
1 day ago
Nonce
1 day ago
Notice
1 day ago
Plugin
1 day ago
Preferences
1 day ago
Promo
1 day ago
Request
1 day ago
RequestHandler
1 day ago
Response
1 day ago
Sanitize
1 day ago
Service
1 day ago
Setting
1 day ago
Settings
1 day ago
Storage
1 day ago
Table
1 day ago
TableIdsFactory
1 day ago
TableScreen
1 day ago
TableScreenFactory
1 day ago
ThirdParty
1 day ago
Type
1 day ago
Value
1 day ago
View
1 day ago
Acf.php
1 day ago
AdminColumns.php
1 day ago
ArrayIterator.php
1 day ago
Capabilities.php
1 day ago
Collection.php
1 day ago
CollectionFormatter.php
1 day ago
Column.php
1 day ago
ColumnCollection.php
1 day ago
ColumnFactoryCollectionFactory.php
1 day ago
ColumnFactoryDefinitionCollection.php
1 day ago
ColumnGroups.php
1 day ago
ColumnIterator.php
1 day ago
ColumnNamesTrait.php
1 day ago
ColumnRepository.php
1 day ago
ColumnTypeRepository.php
1 day ago
Config.php
1 day ago
DateFormats.php
1 day ago
DefaultColumnHandler.php
1 day ago
Expirable.php
1 day ago
Formatter.php
1 day ago
FormatterCollection.php
1 day ago
Helper.php
1 day ago
ListScreen.php
1 day ago
ListScreenCollection.php
1 day ago
ListScreenRepository.php
1 day ago
ListScreenRepositoryWritable.php
1 day ago
ListTable.php
1 day ago
ListTableFactory.php
1 day ago
Loader.php
1 day ago
Message.php
1 day ago
MetaType.php
1 day ago
Middleware.php
1 day ago
OpCacheInvalidateTrait.php
1 day ago
PluginActionLinks.php
1 day ago
PluginActionUpgrade.php
1 day ago
PostType.php
1 day ago
PostTypeRepository.php
1 day ago
Registerable.php
1 day ago
Registry.php
1 day ago
Renderable.php
1 day ago
Request.php
1 day ago
RequestAjaxHandler.php
1 day ago
RequestAjaxHandlers.php
1 day ago
RequestAjaxParser.php
1 day ago
RequestHandler.php
1 day ago
RequestHandlerFactory.php
1 day ago
Screen.php
1 day ago
Services.php
1 day ago
TableIdsFactory.php
1 day ago
TableScreen.php
1 day ago
TableScreenFactory.php
1 day ago
Taxonomy.php
1 day ago
Transient.php
1 day ago
TypedArrayIterator.php
1 day ago
View.php
1 day ago
WooCommerce.php
1 day ago
WpListTableFactory.php
1 day ago
Screen.php
125 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC; |
| 6 | |
| 7 | use AC\Admin\Page\Columns; |
| 8 | use AC\Admin\RequestHandlerInterface; |
| 9 | use LogicException; |
| 10 | use WP_Screen; |
| 11 | |
| 12 | class Screen implements Registerable |
| 13 | { |
| 14 | protected ?WP_Screen $screen = null; |
| 15 | |
| 16 | private TableScreenFactory $table_screen_factory; |
| 17 | |
| 18 | public function __construct(TableScreenFactory $table_screen_factory) |
| 19 | { |
| 20 | $this->table_screen_factory = $table_screen_factory; |
| 21 | } |
| 22 | |
| 23 | public function register(): void |
| 24 | { |
| 25 | add_action('current_screen', [$this, 'init']); |
| 26 | } |
| 27 | |
| 28 | public function init(WP_Screen $screen): void |
| 29 | { |
| 30 | $this->set_screen($screen); |
| 31 | |
| 32 | do_action('ac/screen', $this, $screen->id); |
| 33 | } |
| 34 | |
| 35 | public function is_screen(string $id): bool |
| 36 | { |
| 37 | return $this->has_screen() && $this->get_screen()->id === $id; |
| 38 | } |
| 39 | |
| 40 | public function set_screen(WP_Screen $screen): void |
| 41 | { |
| 42 | $this->screen = $screen; |
| 43 | } |
| 44 | |
| 45 | public function get_screen(): WP_Screen |
| 46 | { |
| 47 | if (! $this->screen instanceof WP_Screen) { |
| 48 | throw new LogicException('Screen is not set.'); |
| 49 | } |
| 50 | |
| 51 | return $this->screen; |
| 52 | } |
| 53 | |
| 54 | public function has_screen(): bool |
| 55 | { |
| 56 | return $this->screen instanceof WP_Screen; |
| 57 | } |
| 58 | |
| 59 | public function get_id(): string |
| 60 | { |
| 61 | return $this->get_screen()->id; |
| 62 | } |
| 63 | |
| 64 | public function get_base(): string |
| 65 | { |
| 66 | return $this->get_screen()->base; |
| 67 | } |
| 68 | |
| 69 | public function get_post_type(): string |
| 70 | { |
| 71 | return $this->get_screen()->post_type; |
| 72 | } |
| 73 | |
| 74 | private function is_admin_network(): bool |
| 75 | { |
| 76 | return $this->get_screen()->in_admin('network'); |
| 77 | } |
| 78 | |
| 79 | public function is_table_screen(): bool |
| 80 | { |
| 81 | return $this->table_screen_factory->can_create_from_wp_screen($this->get_screen()); |
| 82 | } |
| 83 | |
| 84 | public function is_plugin_screen(): bool |
| 85 | { |
| 86 | $screen = $this->is_admin_network() |
| 87 | ? 'plugins-network' |
| 88 | : 'plugins'; |
| 89 | |
| 90 | return $this->is_screen($screen); |
| 91 | } |
| 92 | |
| 93 | public function is_admin_screen(?string $slug = null): bool |
| 94 | { |
| 95 | if (null !== $slug) { |
| 96 | $tabs = [$slug]; |
| 97 | |
| 98 | // When the column page is requested from the setting menu the 'tab' querystring is not set. |
| 99 | if (Columns::NAME === $slug) { |
| 100 | $tabs[] = null; |
| 101 | } |
| 102 | |
| 103 | return $this->is_main_admin_screen() && in_array( |
| 104 | filter_input(INPUT_GET, RequestHandlerInterface::PARAM_TAB), |
| 105 | $tabs, |
| 106 | true |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | return $this->is_main_admin_screen(); |
| 111 | } |
| 112 | |
| 113 | private function is_main_admin_screen(): bool |
| 114 | { |
| 115 | $id = 'settings_page_' . Admin\Admin::NAME; |
| 116 | |
| 117 | if ($this->is_admin_network()) { |
| 118 | $id .= '-network'; |
| 119 | } |
| 120 | |
| 121 | return $this->is_screen($id); |
| 122 | } |
| 123 | |
| 124 | } |
| 125 |