Asset
3 days ago
Banner
3 days ago
Colors
3 days ago
MenuGroupFactory
3 days ago
MenuPageFactory
3 days ago
Notice
3 days ago
Page
3 days ago
PageFactory
3 days ago
Preference
3 days ago
Type
3 days ago
View
3 days ago
Admin.php
3 days ago
AdminLoader.php
3 days ago
AdminNetwork.php
3 days ago
AdminScripts.php
3 days ago
HelpTab.php
3 days ago
Helpable.php
3 days ago
Menu.php
3 days ago
MenuFactory.php
3 days ago
MenuFactoryInterface.php
3 days ago
MenuGroupFactory.php
3 days ago
MenuListFactory.php
3 days ago
MenuListItems.php
3 days ago
MenuPageFactory.php
3 days ago
PageFactoryInterface.php
3 days ago
PageNetworkRequestHandler.php
3 days ago
PageNetworkRequestHandlers.php
3 days ago
PageRequestHandler.php
3 days ago
PageRequestHandlers.php
3 days ago
RenderableHead.php
3 days ago
RequestHandlerInterface.php
3 days ago
ScreenOption.php
3 days ago
ScreenOptions.php
3 days ago
Scripts.php
3 days ago
Tooltip.php
3 days ago
UninitializedScreens.php
3 days ago
AdminLoader.php
116 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Admin; |
| 6 | |
| 7 | use AC\Asset\Enqueueable; |
| 8 | use AC\Asset\Enqueueables; |
| 9 | use AC\Registerable; |
| 10 | use AC\Renderable; |
| 11 | use AC\Request; |
| 12 | use AC\View; |
| 13 | |
| 14 | class AdminLoader implements Registerable |
| 15 | { |
| 16 | protected string $hook; |
| 17 | |
| 18 | protected RequestHandlerInterface $request_handler; |
| 19 | |
| 20 | protected Enqueueables $assets; |
| 21 | |
| 22 | private ?Renderable $page = null; |
| 23 | |
| 24 | public function __construct(string $hook, RequestHandlerInterface $request_handler, Enqueueables $assets) |
| 25 | { |
| 26 | $this->hook = $hook; |
| 27 | $this->request_handler = $request_handler; |
| 28 | $this->assets = $assets; |
| 29 | } |
| 30 | |
| 31 | public function register(): void |
| 32 | { |
| 33 | add_action('load-' . $this->hook, [$this, 'set_page']); |
| 34 | add_action('load-' . $this->hook, [$this, 'load']); |
| 35 | add_action('in_admin_header', [$this, 'head']); |
| 36 | add_action($this->hook, [$this, 'body']); |
| 37 | } |
| 38 | |
| 39 | public function set_page(): void |
| 40 | { |
| 41 | $this->page = $this->request_handler->handle(new Request()); |
| 42 | } |
| 43 | |
| 44 | public function load(): void |
| 45 | { |
| 46 | if (! $this->page) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | if ($this->page instanceof Registerable) { |
| 51 | $this->page->register(); |
| 52 | } |
| 53 | |
| 54 | $screen = get_current_screen(); |
| 55 | |
| 56 | if ($this->page instanceof Helpable && $screen) { |
| 57 | foreach ($this->page->get_help_tabs() as $help) { |
| 58 | $screen->add_help_tab([ |
| 59 | 'id' => $help->get_id(), |
| 60 | 'title' => $help->get_title(), |
| 61 | 'content' => $help->get_content(), |
| 62 | ]); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | if ($this->page instanceof Enqueueables) { |
| 67 | array_map([$this, 'enqueue'], $this->page->get_assets()->all()); |
| 68 | } |
| 69 | |
| 70 | foreach ($this->assets->get_assets()->all() as $asset) { |
| 71 | $asset->enqueue(); |
| 72 | } |
| 73 | |
| 74 | do_action('ac/admin_scripts', $this->page); |
| 75 | |
| 76 | add_filter('screen_settings', [$this, 'screen_options']); |
| 77 | } |
| 78 | |
| 79 | public function head(): void |
| 80 | { |
| 81 | if ($this->page instanceof RenderableHead) { |
| 82 | echo $this->page->render_head()->render(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public function body(): void |
| 87 | { |
| 88 | if ($this->page instanceof Renderable) { |
| 89 | $view = new View([ |
| 90 | 'content' => $this->page->render(), |
| 91 | ]); |
| 92 | |
| 93 | echo $view->set_template('admin/wrap')->render(); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | protected function enqueue(Enqueueable $asset): void |
| 98 | { |
| 99 | $asset->enqueue(); |
| 100 | } |
| 101 | |
| 102 | public function screen_options($settings) |
| 103 | { |
| 104 | if ($this->page instanceof ScreenOptions) { |
| 105 | $settings .= sprintf('<legend>%s</legend>', __('Display', 'codepress-admin-columns')); |
| 106 | |
| 107 | foreach ($this->page->get_screen_options() as $screen_option) { |
| 108 | $settings .= $screen_option->render(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $settings; |
| 113 | } |
| 114 | |
| 115 | } |
| 116 |