title + content), // so the Pages Setup "+" create button and onboarding can build them. return apply_filters('fluent_cart/generatable_pages', $this->corePages()); } public function corePages(): array { return [ 'checkout' => [ 'title' => 'Checkout', 'content' => '[fluent_cart_checkout]' ], 'cart' => [ 'title' => 'Cart', 'content' => '[fluent_cart_cart]' ], 'receipt' => [ 'title' => 'Receipt', 'content' => '[fluent_cart_receipt]' ], 'shop' => [ 'title' => 'Shop', 'content' => static::getShopPageContent() ], 'customer_profile' => [ 'title' => 'Account', 'content' => '' ] ]; } public function createPages(array $exclude = []): array { $storeSettings = new StoreSettings(); $createdPages = []; $pages = Arr::except($this->pages(), $exclude); foreach ($pages as $key => $page) { $title = $page['title']; $pageKey = "{$key}_page_id"; $content = $page['content']; $info = [ 'post_title' => $title, 'post_content' => $content, 'post_status' => 'publish', 'post_type' => 'page', 'post_name' => $title, ]; $pageId = wp_insert_post($info); $storeSettings->set($pageKey, $pageId); $createdPages[$pageKey] = $pageId; } return $createdPages; } public function getGeneratablePage(bool $withDefaultId = false): array { $pages = $this->pages(); if ($withDefaultId) { foreach ($pages as $key => $page) { $title = $page['title']; $pageKey = "{$key}_page_id"; $page = (new DynamicModel([], 'posts')) ->newQuery() ->where('post_type', 'page') ->where('post_status', 'publish') ->where('post_content', 'LIKE', "%" . Str::of($page['content'])->remove('/-->') . '%') ->orderByDesc('ID') ->first(); if (!empty($page)) { $pages[$key]['page_id'] = $page->ID; } } } return $pages; } public static function getPages($searchBy, $ignoreCache = false) { $searchByKey = $searchBy ?? 'all'; if (isset(static::$cachedPages[$searchByKey]) && !$ignoreCache) { return static::$cachedPages[$searchByKey]; } $results = (new DynamicModel([], 'posts')) ->newQuery() ->select('ID', 'post_title') ->where('post_type', 'page') ->where('post_status', 'publish') ->where('post_title', 'LIKE', "%" . esc_sql($searchBy) . '%') ->get(); $matchedPages = []; foreach ($results as $result) { $matchedPages[] = [ 'label' => $result->post_title . "( $result->ID )", 'value' => $result->ID, ]; } static::$cachedPages[$searchByKey] = $matchedPages; return $matchedPages; } public function handlePageDelete() { add_action('wp_trash_post', function ($post_id) { if (get_post_type($post_id) === 'page') { $settings = (new \FluentCart\Api\StoreSettings()); $pageSettings = $settings->getPagesSettings(); $allSettings = $settings->get(); $pageId = $post_id; foreach ($pageSettings as $pageName => $pageSetting) { if ($pageSetting == $pageId) { $allSettings[$pageName] = ''; } } $settings->save($allSettings); } }); } public static function isPage($pageId): bool { static $cachedIsPage = []; if (isset($cachedIsPage[$pageId])) { return $cachedIsPage[$pageId]; } $post = get_post($pageId); if (empty($post) || $post->post_status !== 'publish') { $cachedIsPage[$pageId] = false; return false; } $cachedIsPage[$pageId] = $post->post_type == 'page'; return $cachedIsPage[$pageId]; } public static function getShopPageContent(): string { $fluentCartProductsBlock = "