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