Acf
2 days ago
Admin
2 days ago
Ajax
2 days ago
ApplyFilter
2 days ago
Asset
2 days ago
Capabilities
2 days ago
Check
2 days ago
Collection
2 days ago
Column
2 days ago
ColumnFactories
2 days ago
ColumnFactory
2 days ago
ColumnIterator
2 days ago
ColumnRepository
2 days ago
ColumnSize
2 days ago
DI
2 days ago
DefaultColumnHandler
2 days ago
Deprecated
2 days ago
Entity
2 days ago
Exception
2 days ago
Expression
2 days ago
Form
2 days ago
Formatter
2 days ago
Helper
2 days ago
Integration
2 days ago
ListScreenRepository
2 days ago
ListTable
2 days ago
Message
2 days ago
Meta
2 days ago
Nonce
2 days ago
Notice
2 days ago
Plugin
2 days ago
Preferences
2 days ago
Promo
2 days ago
Request
2 days ago
RequestHandler
2 days ago
Response
2 days ago
Sanitize
2 days ago
Service
2 days ago
Setting
2 days ago
Settings
2 days ago
Storage
2 days ago
Table
2 days ago
TableIdsFactory
2 days ago
TableScreen
2 days ago
TableScreenFactory
2 days ago
ThirdParty
2 days ago
Type
2 days ago
Value
2 days ago
View
2 days ago
Acf.php
2 days ago
AdminColumns.php
2 days ago
ArrayIterator.php
2 days ago
Capabilities.php
2 days ago
Collection.php
2 days ago
CollectionFormatter.php
2 days ago
Column.php
2 days ago
ColumnCollection.php
2 days ago
ColumnFactoryCollectionFactory.php
2 days ago
ColumnFactoryDefinitionCollection.php
2 days ago
ColumnGroups.php
2 days ago
ColumnIterator.php
2 days ago
ColumnNamesTrait.php
2 days ago
ColumnRepository.php
2 days ago
ColumnTypeRepository.php
2 days ago
Config.php
2 days ago
DateFormats.php
2 days ago
DefaultColumnHandler.php
2 days ago
Expirable.php
2 days ago
Formatter.php
2 days ago
FormatterCollection.php
2 days ago
Helper.php
2 days ago
ListScreen.php
2 days ago
ListScreenCollection.php
2 days ago
ListScreenRepository.php
2 days ago
ListScreenRepositoryWritable.php
2 days ago
ListTable.php
2 days ago
ListTableFactory.php
2 days ago
Loader.php
2 days ago
Message.php
2 days ago
MetaType.php
2 days ago
Middleware.php
2 days ago
OpCacheInvalidateTrait.php
2 days ago
PluginActionLinks.php
2 days ago
PluginActionUpgrade.php
2 days ago
PostType.php
2 days ago
PostTypeRepository.php
2 days ago
Registerable.php
2 days ago
Registry.php
2 days ago
Renderable.php
2 days ago
Request.php
2 days ago
RequestAjaxHandler.php
2 days ago
RequestAjaxHandlers.php
2 days ago
RequestAjaxParser.php
2 days ago
RequestHandler.php
2 days ago
RequestHandlerFactory.php
2 days ago
Screen.php
2 days ago
Services.php
2 days ago
TableIdsFactory.php
2 days ago
TableScreen.php
2 days ago
TableScreenFactory.php
2 days ago
Taxonomy.php
2 days ago
Transient.php
2 days ago
TypedArrayIterator.php
2 days ago
View.php
2 days ago
WooCommerce.php
2 days ago
WpListTableFactory.php
2 days ago
View.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC; |
| 6 | |
| 7 | class View implements Renderable |
| 8 | { |
| 9 | private array $data = []; |
| 10 | |
| 11 | private ?string $template; |
| 12 | |
| 13 | public function __construct(array $data = []) |
| 14 | { |
| 15 | $this->set_data($data); |
| 16 | } |
| 17 | |
| 18 | public function get(string $key) |
| 19 | { |
| 20 | return $this->data[$key] ?? null; |
| 21 | } |
| 22 | |
| 23 | public function __get($key) |
| 24 | { |
| 25 | return $this->get($key); |
| 26 | } |
| 27 | |
| 28 | public function __set($key, $value): void |
| 29 | { |
| 30 | $this->set($key, $value); |
| 31 | } |
| 32 | |
| 33 | public function set(string $key, $value): self |
| 34 | { |
| 35 | $this->data[$key] = $value; |
| 36 | |
| 37 | return $this; |
| 38 | } |
| 39 | |
| 40 | public function get_data(): array |
| 41 | { |
| 42 | return $this->data; |
| 43 | } |
| 44 | |
| 45 | public function set_data(array $data): self |
| 46 | { |
| 47 | foreach ($data as $key => $value) { |
| 48 | $this->set($key, $value); |
| 49 | } |
| 50 | |
| 51 | return $this; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Will try to resolve the current template to a file |
| 56 | */ |
| 57 | public function resolve_template(): bool |
| 58 | { |
| 59 | /** |
| 60 | * Returns the available template paths for column settings |
| 61 | * |
| 62 | * @param array $paths Template paths |
| 63 | * @param string $template Current template path |
| 64 | */ |
| 65 | $paths = (array)apply_filters( |
| 66 | 'ac/view/templates', |
| 67 | [], |
| 68 | $this->template |
| 69 | ); |
| 70 | |
| 71 | foreach ($paths as $path) { |
| 72 | $file = $path . '/' . $this->template . '.php'; |
| 73 | |
| 74 | if (is_readable($file)) { |
| 75 | include $file; |
| 76 | |
| 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | public function render(): string |
| 85 | { |
| 86 | ob_start(); |
| 87 | |
| 88 | $this->resolve_template(); |
| 89 | |
| 90 | return (string)ob_get_clean(); |
| 91 | } |
| 92 | |
| 93 | public function get_template(): ?string |
| 94 | { |
| 95 | return $this->template; |
| 96 | } |
| 97 | |
| 98 | public function set_template(string $template): self |
| 99 | { |
| 100 | $this->template = $template; |
| 101 | |
| 102 | return $this; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Should call self::render when treated as a string |
| 107 | */ |
| 108 | public function __toString(): string |
| 109 | { |
| 110 | return $this->render(); |
| 111 | } |
| 112 | |
| 113 | } |
| 114 |