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
PageContentResource.php
81 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Resources; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Kirki\App\Constants\PageMetaKeys; |
| 8 | use Kirki\App\Models\Page as PageModel; |
| 9 | use Kirki\App\Supports\Facades\Page; |
| 10 | use Kirki\App\Supports\PageUrl; |
| 11 | use Kirki\App\Utils\PostUtil; |
| 12 | use Kirki\Framework\Database\Query\QueryBuilder; |
| 13 | use Kirki\Framework\Resource; |
| 14 | |
| 15 | class PageContentResource extends Resource |
| 16 | { |
| 17 | /** @var string */ |
| 18 | protected $stage_block_key; |
| 19 | |
| 20 | /** @var int|false */ |
| 21 | protected $stage_version = false; |
| 22 | |
| 23 | /** |
| 24 | * PageContentResource constructor. |
| 25 | * |
| 26 | * @param PageModel $page |
| 27 | * @param int|false $stage_version |
| 28 | */ |
| 29 | public function __construct(PageModel $page, $stage_version = false) |
| 30 | { |
| 31 | $page_id = $page->ID; |
| 32 | |
| 33 | if (!$stage_version) { |
| 34 | $stage_version = Page::get_most_recent_stage_version($page_id, false); |
| 35 | } |
| 36 | |
| 37 | $this->stage_version = $stage_version; |
| 38 | |
| 39 | $this->stage_block_key = Page::get_staged_meta_name(PageMetaKeys::BLOCKS, $page_id, $stage_version); |
| 40 | |
| 41 | $page->load_missing([ |
| 42 | 'meta' => function (QueryBuilder $query) use ($page_id) { |
| 43 | $query->where_in('meta_key', [ |
| 44 | PageMetaKeys::BLOCKS, |
| 45 | $this->stage_block_key |
| 46 | ]); |
| 47 | } |
| 48 | ]); |
| 49 | |
| 50 | parent::__construct($page); |
| 51 | } |
| 52 | /** |
| 53 | * Convert the collection resource to an array. |
| 54 | * |
| 55 | * @return array The collection data as an associative array. |
| 56 | */ |
| 57 | public function to_array() |
| 58 | { |
| 59 | $page_url = PageUrl::create($this->resource); |
| 60 | |
| 61 | $meta = isset($this->meta) ? $this->meta->pluck('meta_value', 'meta_key')->to_array() : []; |
| 62 | |
| 63 | $content = get_the_content(null, false, PostUtil::get_wp_post($this->resource)); |
| 64 | |
| 65 | $blocks = !empty($meta[PageMetaKeys::BLOCKS]) ? $meta[PageMetaKeys::BLOCKS] : []; |
| 66 | |
| 67 | if ($this->stage_version) { |
| 68 | $blocks = !empty($meta[$this->stage_block_key]) ? $meta[$this->stage_block_key] : []; |
| 69 | } |
| 70 | |
| 71 | return [ |
| 72 | 'blocks' => !empty($blocks['blocks']) ? $blocks['blocks'] : null, |
| 73 | 'content_length' => strlen($content), |
| 74 | 'is_kirki_editor_mode' => Page::is_kirki_editor_mode($this->ID), |
| 75 | 'preview_url' => $page_url->get_preview_url(), |
| 76 | 'styles' => Page::get_page_styleblocks($this->ID, $this->stage_version), |
| 77 | 'version_info' => Page::get_staged_version_info($this->ID, $this->stage_version), |
| 78 | ]; |
| 79 | } |
| 80 | } |
| 81 |