CollaborationComment
3 weeks ago
Collection
3 weeks ago
CollectionItem
3 weeks ago
Media
3 weeks ago
PageContentResource.php
3 days ago
PageResource.php
3 days ago
PageSettingsResource.php
3 weeks ago
SymbolResource.php
3 weeks ago
PageResource.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Resources; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Kirki\App\Constants\OptionKeys; |
| 8 | use Kirki\App\Constants\PageMetaKeys; |
| 9 | use Kirki\App\Constants\PostTypes; |
| 10 | use Kirki\App\Models\Page as PageModel; |
| 11 | use Kirki\App\Supports\Facades\Page; |
| 12 | use Kirki\App\Supports\PageUrl; |
| 13 | use Kirki\Framework\Database\Query\QueryBuilder; |
| 14 | use Kirki\Framework\Resource; |
| 15 | use Kirki\Framework\Supports\Facades\Option; |
| 16 | |
| 17 | class PageResource extends Resource |
| 18 | { |
| 19 | public function __construct(PageModel $page) |
| 20 | { |
| 21 | $page->load_missing([ |
| 22 | 'meta' => function (QueryBuilder $query) { |
| 23 | $query->where_in('meta_key', PageMetaKeys::get_single_post_keys()); |
| 24 | } |
| 25 | ]); |
| 26 | |
| 27 | parent::__construct($page); |
| 28 | } |
| 29 | /** |
| 30 | * Convert the collection resource to an array. |
| 31 | * |
| 32 | * @return array The collection data as an associative array. |
| 33 | */ |
| 34 | public function to_array() |
| 35 | { |
| 36 | $page_url = PageUrl::create($this->resource); |
| 37 | |
| 38 | $meta = isset($this->meta) ? $this->meta->pluck('meta_value', 'meta_key')->to_array() : []; |
| 39 | |
| 40 | $data = [ |
| 41 | 'id' => (int) $this->ID, |
| 42 | 'title' => $this->post_title, |
| 43 | 'status' => $this->post_status, |
| 44 | 'post_type' => $this->post_type, |
| 45 | 'post_parent' => (int) $this->post_parent, |
| 46 | 'slug' => $this->post_name, |
| 47 | 'variableMode' => Page::get_variable_mode($this->resource), |
| 48 | 'isFrontPage' => (int) Option::get(OptionKeys::PAGE_ON_FRONT, false) === (int) $this->ID, |
| 49 | 'disabled_page_symbols' => $meta[PageMetaKeys::DISABLED_PAGE_SYMBOLS] ?? [], |
| 50 | 'staged_last_updated' => $this->get_staged_last_updated(), |
| 51 | 'preview_url' => $page_url->get_preview_url(), |
| 52 | 'editor_url' => $page_url->get_editor_url(), |
| 53 | ]; |
| 54 | |
| 55 | if ($this->post_type === PostTypes::UTILITY) { |
| 56 | $data['utility_page_type'] = $meta[PageMetaKeys::UTILITY_PAGE_TYPE] ?? ''; |
| 57 | } |
| 58 | |
| 59 | if ($this->post_type === PostTypes::TEMPLATE) { |
| 60 | $data['conditions'] = $meta[PageMetaKeys::TEMPLATE_CONDITIONS] ?? []; |
| 61 | $data['collection_type'] = $meta[PageMetaKeys::TEMPLATE_COLLECTION_TYPE] ?? ''; |
| 62 | } |
| 63 | |
| 64 | if ($this->post_type === PostTypes::POPUP) { |
| 65 | $data['blocks'] = $meta[PageMetaKeys::BLOCKS] ?? []; |
| 66 | $data['styleBlocks'] = $meta[PageMetaKeys::STYLE_BLOCKS] ?? []; |
| 67 | $data['usedFonts'] = $meta[PageMetaKeys::USED_FONT_LIST] ?? []; |
| 68 | } |
| 69 | |
| 70 | return $data; |
| 71 | } |
| 72 | |
| 73 | protected function get_staged_last_updated() |
| 74 | { |
| 75 | $published_staged_info = Page::get_published_staged_version_info((int) $this->ID); |
| 76 | |
| 77 | return isset($published_staged_info['last_updated']) ? $published_staged_info['last_updated'] : null; |
| 78 | } |
| 79 | } |
| 80 |